1 /*
   2  * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.java2d.opengl;
  27 
  28 import java.awt.AWTException;
  29 import java.awt.BufferCapabilities;
  30 import java.awt.Component;
  31 import java.awt.Graphics;
  32 import java.awt.Graphics2D;
  33 import java.awt.Image;
  34 import java.awt.ImageCapabilities;
  35 import java.awt.Rectangle;
  36 import java.awt.Transparency;
  37 import java.awt.color.ColorSpace;
  38 import java.awt.image.BufferedImage;
  39 import java.awt.image.ColorModel;
  40 import java.awt.image.DataBuffer;
  41 import java.awt.image.DirectColorModel;
  42 import java.awt.image.VolatileImage;
  43 import java.awt.image.WritableRaster;
  44 
  45 import sun.awt.CGraphicsConfig;
  46 import sun.awt.CGraphicsDevice;
  47 import sun.awt.image.OffScreenImage;
  48 import sun.awt.image.SunVolatileImage;
  49 import sun.java2d.Disposer;
  50 import sun.java2d.DisposerRecord;
  51 import sun.java2d.Surface;
  52 import sun.java2d.SurfaceData;
  53 import sun.java2d.opengl.OGLContext.OGLContextCaps;
  54 import sun.java2d.pipe.hw.AccelSurface;
  55 import sun.java2d.pipe.hw.AccelTypedVolatileImage;
  56 import sun.java2d.pipe.hw.ContextCapabilities;
  57 import sun.lwawt.LWComponentPeer;
  58 import sun.lwawt.macosx.CFRetainedResource;
  59 import sun.lwawt.macosx.CPlatformView;
  60 
  61 import static sun.java2d.opengl.OGLContext.OGLContextCaps.CAPS_DOUBLEBUFFERED;
  62 import static sun.java2d.opengl.OGLContext.OGLContextCaps.CAPS_EXT_FBOBJECT;
  63 import static sun.java2d.opengl.OGLSurfaceData.FBOBJECT;
  64 import static sun.java2d.opengl.OGLSurfaceData.TEXTURE;
  65 
  66 public final class CGLGraphicsConfig extends CGraphicsConfig
  67     implements OGLGraphicsConfig
  68 {
  69     //private static final int kOpenGLSwapInterval =
  70     // RuntimeOptions.getCurrentOptions().OpenGLSwapInterval;
  71     private static final int kOpenGLSwapInterval = 0; // TODO
  72     private static boolean cglAvailable;
  73     private static ImageCapabilities imageCaps = new CGLImageCaps();
  74 
  75     private int pixfmt;
  76     private BufferCapabilities bufferCaps;
  77     private long pConfigInfo;
  78     private ContextCapabilities oglCaps;
  79     private OGLContext context;
  80     private final Object disposerReferent = new Object();
  81     private final int maxTextureSize;
  82 
  83     private static native boolean initCGL();
  84     private static native long getCGLConfigInfo(int displayID, int visualnum,
  85                                                 int swapInterval);
  86     private static native int getOGLCapabilities(long configInfo);
  87 
  88     /**
  89      * Returns GL_MAX_TEXTURE_SIZE from the shared opengl context. Must be
  90      * called under OGLRQ lock, because this method change current context.
  91      *
  92      * @return GL_MAX_TEXTURE_SIZE
  93      */
  94     private static native int nativeGetMaxTextureSize();
  95 
  96     static {
  97         cglAvailable = initCGL();
  98     }
  99 
 100     private CGLGraphicsConfig(CGraphicsDevice device, int pixfmt,
 101                               long configInfo, int maxTextureSize,
 102                               ContextCapabilities oglCaps) {
 103         super(device);
 104 
 105         this.pixfmt = pixfmt;
 106         this.pConfigInfo = configInfo;
 107         this.oglCaps = oglCaps;
 108         this.maxTextureSize = maxTextureSize;
 109         context = new OGLContext(OGLRenderQueue.getInstance(), this);
 110 
 111         // add a record to the Disposer so that we destroy the native
 112         // CGLGraphicsConfigInfo data when this object goes away
 113         Disposer.addRecord(disposerReferent,
 114                            new CGLGCDisposerRecord(pConfigInfo));
 115     }
 116 
 117     @Override
 118     public Object getProxyKey() {
 119         return this;
 120     }
 121 
 122     @Override
 123     public SurfaceData createManagedSurface(int w, int h, int transparency) {
 124         return CGLSurfaceData.createData(this, w, h,
 125                                          getColorModel(transparency),
 126                                          null,
 127                                          OGLSurfaceData.TEXTURE);
 128     }
 129 
 130     public static CGLGraphicsConfig getConfig(CGraphicsDevice device,
 131                                               int displayID, int pixfmt)
 132     {
 133         if (!cglAvailable) {
 134             return null;
 135         }
 136 
 137         long cfginfo = 0;
 138         int textureSize = 0;
 139         final String[] ids = new String[1];
 140         OGLRenderQueue rq = OGLRenderQueue.getInstance();
 141         rq.lock();
 142         try {
 143             // getCGLConfigInfo() creates and destroys temporary
 144             // surfaces/contexts, so we should first invalidate the current
 145             // Java-level context and flush the queue...
 146             OGLContext.invalidateCurrentContext();
 147             cfginfo = getCGLConfigInfo(displayID, pixfmt, kOpenGLSwapInterval);
 148             if (cfginfo != 0L) {
 149                 textureSize = nativeGetMaxTextureSize();
 150                 // 7160609: GL still fails to create a square texture of this
 151                 // size. Half should be safe enough.
 152                 // Explicitly not support a texture more than 2^14, see 8010999.
 153                 textureSize = textureSize <= 16384 ? textureSize / 2 : 8192;
 154                 OGLContext.setScratchSurface(cfginfo);
 155                 rq.flushAndInvokeNow(() -> {
 156                     ids[0] = OGLContext.getOGLIdString();
 157                 });
 158             }
 159         } finally {
 160             rq.unlock();
 161         }
 162         if (cfginfo == 0) {
 163             return null;
 164         }
 165 
 166         int oglCaps = getOGLCapabilities(cfginfo);
 167         ContextCapabilities caps = new OGLContextCaps(oglCaps, ids[0]);
 168         return new CGLGraphicsConfig(device, pixfmt, cfginfo, textureSize, caps);
 169     }
 170 
 171     public static boolean isCGLAvailable() {
 172         return cglAvailable;
 173     }
 174 
 175     /**
 176      * Returns true if the provided capability bit is present for this config.
 177      * See OGLContext.java for a list of supported capabilities.
 178      */
 179     @Override
 180     public boolean isCapPresent(int cap) {
 181         return ((oglCaps.getCaps() & cap) != 0);
 182     }
 183 
 184     @Override
 185     public long getNativeConfigInfo() {
 186         return pConfigInfo;
 187     }
 188 
 189     /**
 190      * {@inheritDoc}
 191      *
 192      * @see sun.java2d.pipe.hw.BufferedContextProvider#getContext
 193      */
 194     @Override
 195     public OGLContext getContext() {
 196         return context;
 197     }
 198 
 199     @Override
 200     public BufferedImage createCompatibleImage(int width, int height) {
 201         ColorModel model = new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
 202         WritableRaster
 203             raster = model.createCompatibleWritableRaster(width, height);
 204         return new BufferedImage(model, raster, model.isAlphaPremultiplied(),
 205                                  null);
 206     }
 207 
 208     @Override
 209     public ColorModel getColorModel(int transparency) {
 210         switch (transparency) {
 211         case Transparency.OPAQUE:
 212             // REMIND: once the ColorModel spec is changed, this should be
 213             //         an opaque premultiplied DCM...
 214             return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
 215         case Transparency.BITMASK:
 216             return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);
 217         case Transparency.TRANSLUCENT:
 218             ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
 219             return new DirectColorModel(cs, 32,
 220                                         0xff0000, 0xff00, 0xff, 0xff000000,
 221                                         true, DataBuffer.TYPE_INT);
 222         default:
 223             return null;
 224         }
 225     }
 226 
 227     public boolean isDoubleBuffered() {
 228         return isCapPresent(CAPS_DOUBLEBUFFERED);
 229     }
 230 
 231     private static class CGLGCDisposerRecord implements DisposerRecord {
 232         private long pCfgInfo;
 233         public CGLGCDisposerRecord(long pCfgInfo) {
 234             this.pCfgInfo = pCfgInfo;
 235         }
 236         public void dispose() {
 237             if (pCfgInfo != 0) {
 238                 OGLRenderQueue.disposeGraphicsConfig(pCfgInfo);
 239                 pCfgInfo = 0;
 240             }
 241         }
 242     }
 243 
 244     // TODO: CGraphicsConfig doesn't implement displayChanged() yet
 245     //@Override
 246     public synchronized void displayChanged() {
 247         //super.displayChanged();
 248 
 249         // the context could hold a reference to a CGLSurfaceData, which in
 250         // turn has a reference back to this CGLGraphicsConfig, so in order
 251         // for this instance to be disposed we need to break the connection
 252         OGLRenderQueue rq = OGLRenderQueue.getInstance();
 253         rq.lock();
 254         try {
 255             OGLContext.invalidateCurrentContext();
 256         } finally {
 257             rq.unlock();
 258         }
 259     }
 260 
 261     @Override
 262     public String toString() {
 263         String display = getDevice().getIDstring();
 264         return ("CGLGraphicsConfig[" + display + ", pixfmt=" + pixfmt + "]");
 265     }
 266 
 267     @Override
 268     public SurfaceData createSurfaceData(CPlatformView pView) {
 269         return CGLSurfaceData.createData(pView);
 270     }
 271 
 272     @Override
 273     public SurfaceData createSurfaceData(CFRetainedResource layer) {
 274         return CGLSurfaceData.createData((CGLLayer) layer);
 275     }
 276 
 277     @Override
 278     public Image createAcceleratedImage(Component target,
 279                                         int width, int height)
 280     {
 281         ColorModel model = getColorModel(Transparency.OPAQUE);
 282         WritableRaster wr = model.createCompatibleWritableRaster(width, height);
 283         return new OffScreenImage(target, model, wr,
 284                                   model.isAlphaPremultiplied());
 285     }
 286 
 287     @Override
 288     public void assertOperationSupported(final int numBuffers,
 289                                          final BufferCapabilities caps)
 290             throws AWTException {
 291         // Assume this method is never called with numBuffers != 2, as 0 is
 292         // unsupported, and 1 corresponds to a SingleBufferStrategy which
 293         // doesn't depend on the peer. Screen is considered as a separate
 294         // "buffer".
 295         if (numBuffers != 2) {
 296             throw new AWTException("Only double buffering is supported");
 297         }
 298         final BufferCapabilities configCaps = getBufferCapabilities();
 299         if (!configCaps.isPageFlipping()) {
 300             throw new AWTException("Page flipping is not supported");
 301         }
 302         if (caps.getFlipContents() == BufferCapabilities.FlipContents.PRIOR) {
 303             throw new AWTException("FlipContents.PRIOR is not supported");
 304         }
 305     }
 306 
 307     @Override
 308     public Image createBackBuffer(final LWComponentPeer<?, ?> peer) {
 309         final Rectangle r = peer.getBounds();
 310         // It is possible for the component to have size 0x0, adjust it to
 311         // be at least 1x1 to avoid IAE
 312         final int w = Math.max(1, r.width);
 313         final int h = Math.max(1, r.height);
 314         final int transparency = peer.isTranslucent() ? Transparency.TRANSLUCENT
 315                                                       : Transparency.OPAQUE;
 316         return new SunVolatileImage(this, w, h, transparency, null);
 317     }
 318 
 319     @Override
 320     public void destroyBackBuffer(final Image backBuffer) {
 321         if (backBuffer != null) {
 322             backBuffer.flush();
 323         }
 324     }
 325 
 326     @Override
 327     public void flip(final LWComponentPeer<?, ?> peer, final Image backBuffer,
 328                      final int x1, final int y1, final int x2, final int y2,
 329                      final BufferCapabilities.FlipContents flipAction) {
 330         final Graphics g = peer.getGraphics();
 331         try {
 332             g.drawImage(backBuffer, x1, y1, x2, y2, x1, y1, x2, y2, null);
 333         } finally {
 334             g.dispose();
 335         }
 336         if (flipAction == BufferCapabilities.FlipContents.BACKGROUND) {
 337             final Graphics2D bg = (Graphics2D) backBuffer.getGraphics();
 338             try {
 339                 bg.setBackground(peer.getBackground());
 340                 bg.clearRect(0, 0, backBuffer.getWidth(null),
 341                              backBuffer.getHeight(null));
 342             } finally {
 343                 bg.dispose();
 344             }
 345         }
 346     }
 347 
 348     private static class CGLBufferCaps extends BufferCapabilities {
 349         public CGLBufferCaps(boolean dblBuf) {
 350             super(imageCaps, imageCaps,
 351                   dblBuf ? FlipContents.UNDEFINED : null);
 352         }
 353     }
 354 
 355     @Override
 356     public BufferCapabilities getBufferCapabilities() {
 357         if (bufferCaps == null) {
 358             bufferCaps = new CGLBufferCaps(isDoubleBuffered());
 359         }
 360         return bufferCaps;
 361     }
 362 
 363     private static class CGLImageCaps extends ImageCapabilities {
 364         private CGLImageCaps() {
 365             super(true);
 366         }
 367         public boolean isTrueVolatile() {
 368             return true;
 369         }
 370     }
 371 
 372     @Override
 373     public ImageCapabilities getImageCapabilities() {
 374         return imageCaps;
 375     }
 376 
 377     @Override
 378     public VolatileImage createCompatibleVolatileImage(int width, int height,
 379                                                        int transparency,
 380                                                        int type) {
 381         if ((type != FBOBJECT && type != TEXTURE)
 382                 || transparency == Transparency.BITMASK
 383                 || type == FBOBJECT && !isCapPresent(CAPS_EXT_FBOBJECT)) {
 384             return null;
 385         }
 386         SunVolatileImage vi = new AccelTypedVolatileImage(this, width, height,
 387                                                           transparency, type);
 388         Surface sd = vi.getDestSurface();
 389         if (!(sd instanceof AccelSurface) ||
 390             ((AccelSurface)sd).getType() != type)
 391         {
 392             vi.flush();
 393             vi = null;
 394         }
 395 
 396         return vi;
 397     }
 398 
 399     /**
 400      * {@inheritDoc}
 401      *
 402      * @see sun.java2d.pipe.hw.AccelGraphicsConfig#getContextCapabilities
 403      */
 404     @Override
 405     public ContextCapabilities getContextCapabilities() {
 406         return oglCaps;
 407     }
 408 
 409     @Override
 410     public int getMaxTextureWidth() {
 411         return Math.max(maxTextureSize / getDevice().getScaleFactor(),
 412                         getBounds().width);
 413     }
 414 
 415     @Override
 416     public int getMaxTextureHeight() {
 417         return Math.max(maxTextureSize / getDevice().getScaleFactor(),
 418                         getBounds().height);
 419     }
 420 }