1 /*
   2  * Copyright (c) 2011, 2019, 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 
  59 import static sun.java2d.opengl.OGLContext.OGLContextCaps.CAPS_DOUBLEBUFFERED;
  60 import static sun.java2d.opengl.OGLContext.OGLContextCaps.CAPS_EXT_FBOBJECT;
  61 import static sun.java2d.opengl.OGLSurfaceData.FBOBJECT;
  62 import static sun.java2d.opengl.OGLSurfaceData.TEXTURE;
  63 
  64 public final class CGLGraphicsConfig extends CGraphicsConfig
  65     implements OGLGraphicsConfig
  66 {
  67     //private static final int kOpenGLSwapInterval =
  68     // RuntimeOptions.getCurrentOptions().OpenGLSwapInterval;
  69     private static final int kOpenGLSwapInterval = 0; // TODO
  70     private static boolean cglAvailable;
  71     private static ImageCapabilities imageCaps = new CGLImageCaps();
  72 
  73     private int pixfmt;
  74     private BufferCapabilities bufferCaps;
  75     private long pConfigInfo;
  76     private ContextCapabilities oglCaps;
  77     private final OGLContext context;
  78     private final Object disposerReferent = new Object();
  79     private final int maxTextureSize;
  80 
  81     private static native boolean initCGL();
  82     private static native long getCGLConfigInfo(int displayID, int visualnum,
  83                                                 int swapInterval);
  84     private static native int getOGLCapabilities(long configInfo);
  85 
  86     /**
  87      * Returns GL_MAX_TEXTURE_SIZE from the shared opengl context. Must be
  88      * called under OGLRQ lock, because this method change current context.
  89      *
  90      * @return GL_MAX_TEXTURE_SIZE
  91      */
  92     private static native int nativeGetMaxTextureSize();
  93 
  94     static {
  95         cglAvailable = initCGL();
  96     }
  97 
  98     private CGLGraphicsConfig(CGraphicsDevice device, int pixfmt,
  99                               long configInfo, int maxTextureSize,
 100                               ContextCapabilities oglCaps) {
 101         super(device);
 102 
 103         this.pixfmt = pixfmt;
 104         this.pConfigInfo = configInfo;
 105         this.oglCaps = oglCaps;
 106         this.maxTextureSize = maxTextureSize;
 107         context = new OGLContext(OGLRenderQueue.getInstance());
 108 
 109         // add a record to the Disposer so that we destroy the native
 110         // CGLGraphicsConfigInfo data when this object goes away
 111         Disposer.addRecord(disposerReferent,
 112                            new CGLGCDisposerRecord(pConfigInfo));
 113     }
 114 
 115     @Override
 116     public Object getProxyKey() {
 117         return this;
 118     }
 119 
 120     @Override
 121     public SurfaceData createManagedSurface(int w, int h, int transparency) {
 122         return CGLSurfaceData.createData(this, w, h,
 123                                          getColorModel(transparency),
 124                                          null,
 125                                          OGLSurfaceData.TEXTURE);
 126     }
 127 
 128     public static CGLGraphicsConfig getConfig(CGraphicsDevice device,
 129                                               int displayID, int pixfmt)
 130     {
 131         if (!cglAvailable) {
 132             return null;
 133         }
 134 
 135         long cfginfo = 0;
 136         int textureSize = 0;
 137         final String[] ids = new String[1];
 138         OGLRenderQueue rq = OGLRenderQueue.getInstance();
 139         rq.lock();
 140         try {
 141             // getCGLConfigInfo() creates and destroys temporary
 142             // surfaces/contexts, so we should first invalidate the current
 143             // Java-level context and flush the queue...
 144             OGLContext.invalidateCurrentContext();
 145             cfginfo = getCGLConfigInfo(displayID, pixfmt, kOpenGLSwapInterval);
 146             if (cfginfo != 0L) {
 147                 textureSize = nativeGetMaxTextureSize();
 148                 // 7160609: GL still fails to create a square texture of this
 149                 // size. Half should be safe enough.
 150                 // Explicitly not support a texture more than 2^14, see 8010999.
 151                 textureSize = textureSize <= 16384 ? textureSize / 2 : 8192;
 152                 OGLContext.setScratchSurface(cfginfo);
 153                 rq.flushAndInvokeNow(() -> {
 154                     ids[0] = OGLContext.getOGLIdString();
 155                 });
 156             }
 157         } finally {
 158             rq.unlock();
 159         }
 160         if (cfginfo == 0) {
 161             return null;
 162         }
 163 
 164         int oglCaps = getOGLCapabilities(cfginfo);
 165         ContextCapabilities caps = new OGLContextCaps(oglCaps, ids[0]);
 166         return new CGLGraphicsConfig(device, pixfmt, cfginfo, textureSize, caps);
 167     }
 168 
 169     public static boolean isCGLAvailable() {
 170         return cglAvailable;
 171     }
 172 
 173     /**
 174      * Returns true if the provided capability bit is present for this config.
 175      * See OGLContext.java for a list of supported capabilities.
 176      */
 177     @Override
 178     public boolean isCapPresent(int cap) {
 179         return ((oglCaps.getCaps() & cap) != 0);
 180     }
 181 
 182     @Override
 183     public long getNativeConfigInfo() {
 184         return pConfigInfo;
 185     }
 186 
 187     @Override
 188     public OGLContext getContext() {
 189         return context;
 190     }
 191 
 192     @Override
 193     public BufferedImage createCompatibleImage(int width, int height) {
 194         ColorModel model = new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
 195         WritableRaster
 196             raster = model.createCompatibleWritableRaster(width, height);
 197         return new BufferedImage(model, raster, model.isAlphaPremultiplied(),
 198                                  null);
 199     }
 200 
 201     @Override
 202     public ColorModel getColorModel(int transparency) {
 203         switch (transparency) {
 204         case Transparency.OPAQUE:
 205             // REMIND: once the ColorModel spec is changed, this should be
 206             //         an opaque premultiplied DCM...
 207             return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
 208         case Transparency.BITMASK:
 209             return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);
 210         case Transparency.TRANSLUCENT:
 211             ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
 212             return new DirectColorModel(cs, 32,
 213                                         0xff0000, 0xff00, 0xff, 0xff000000,
 214                                         true, DataBuffer.TYPE_INT);
 215         default:
 216             return null;
 217         }
 218     }
 219 
 220     public boolean isDoubleBuffered() {
 221         return isCapPresent(CAPS_DOUBLEBUFFERED);
 222     }
 223 
 224     private static class CGLGCDisposerRecord implements DisposerRecord {
 225         private long pCfgInfo;
 226         public CGLGCDisposerRecord(long pCfgInfo) {
 227             this.pCfgInfo = pCfgInfo;
 228         }
 229         public void dispose() {
 230             if (pCfgInfo != 0) {
 231                 OGLRenderQueue.disposeGraphicsConfig(pCfgInfo);
 232                 pCfgInfo = 0;
 233             }
 234         }
 235     }
 236 
 237     // TODO: CGraphicsConfig doesn't implement displayChanged() yet
 238     //@Override
 239     public synchronized void displayChanged() {
 240         //super.displayChanged();
 241 
 242         // the context could hold a reference to a CGLSurfaceData, which in
 243         // turn has a reference back to this CGLGraphicsConfig, so in order
 244         // for this instance to be disposed we need to break the connection
 245         OGLRenderQueue rq = OGLRenderQueue.getInstance();
 246         rq.lock();
 247         try {
 248             OGLContext.invalidateCurrentContext();
 249         } finally {
 250             rq.unlock();
 251         }
 252     }
 253 
 254     @Override
 255     public String toString() {
 256         String display = getDevice().getIDstring();
 257         return ("CGLGraphicsConfig[" + display + ", pixfmt=" + pixfmt + "]");
 258     }
 259 
 260     @Override
 261     public SurfaceData createSurfaceData(CGLLayer layer) {
 262         return CGLSurfaceData.createData(layer);
 263     }
 264 
 265     @Override
 266     public Image createAcceleratedImage(Component target,
 267                                         int width, int height)
 268     {
 269         ColorModel model = getColorModel(Transparency.OPAQUE);
 270         WritableRaster wr = model.createCompatibleWritableRaster(width, height);
 271         return new OffScreenImage(target, model, wr,
 272                                   model.isAlphaPremultiplied());
 273     }
 274 
 275     @Override
 276     public void assertOperationSupported(final int numBuffers,
 277                                          final BufferCapabilities caps)
 278             throws AWTException {
 279         // Assume this method is never called with numBuffers != 2, as 0 is
 280         // unsupported, and 1 corresponds to a SingleBufferStrategy which
 281         // doesn't depend on the peer. Screen is considered as a separate
 282         // "buffer".
 283         if (numBuffers != 2) {
 284             throw new AWTException("Only double buffering is supported");
 285         }
 286         final BufferCapabilities configCaps = getBufferCapabilities();
 287         if (!configCaps.isPageFlipping()) {
 288             throw new AWTException("Page flipping is not supported");
 289         }
 290         if (caps.getFlipContents() == BufferCapabilities.FlipContents.PRIOR) {
 291             throw new AWTException("FlipContents.PRIOR is not supported");
 292         }
 293     }
 294 
 295     @Override
 296     public Image createBackBuffer(final LWComponentPeer<?, ?> peer) {
 297         final Rectangle r = peer.getBounds();
 298         // It is possible for the component to have size 0x0, adjust it to
 299         // be at least 1x1 to avoid IAE
 300         final int w = Math.max(1, r.width);
 301         final int h = Math.max(1, r.height);
 302         final int transparency = peer.isTranslucent() ? Transparency.TRANSLUCENT
 303                                                       : Transparency.OPAQUE;
 304         return new SunVolatileImage(this, w, h, transparency, null);
 305     }
 306 
 307     @Override
 308     public void destroyBackBuffer(final Image backBuffer) {
 309         if (backBuffer != null) {
 310             backBuffer.flush();
 311         }
 312     }
 313 
 314     @Override
 315     public void flip(final LWComponentPeer<?, ?> peer, final Image backBuffer,
 316                      final int x1, final int y1, final int x2, final int y2,
 317                      final BufferCapabilities.FlipContents flipAction) {
 318         final Graphics g = peer.getGraphics();
 319         try {
 320             g.drawImage(backBuffer, x1, y1, x2, y2, x1, y1, x2, y2, null);
 321         } finally {
 322             g.dispose();
 323         }
 324         if (flipAction == BufferCapabilities.FlipContents.BACKGROUND) {
 325             final Graphics2D bg = (Graphics2D) backBuffer.getGraphics();
 326             try {
 327                 bg.setBackground(peer.getBackground());
 328                 bg.clearRect(0, 0, backBuffer.getWidth(null),
 329                              backBuffer.getHeight(null));
 330             } finally {
 331                 bg.dispose();
 332             }
 333         }
 334     }
 335 
 336     private static class CGLBufferCaps extends BufferCapabilities {
 337         public CGLBufferCaps(boolean dblBuf) {
 338             super(imageCaps, imageCaps,
 339                   dblBuf ? FlipContents.UNDEFINED : null);
 340         }
 341     }
 342 
 343     @Override
 344     public BufferCapabilities getBufferCapabilities() {
 345         if (bufferCaps == null) {
 346             bufferCaps = new CGLBufferCaps(isDoubleBuffered());
 347         }
 348         return bufferCaps;
 349     }
 350 
 351     private static class CGLImageCaps extends ImageCapabilities {
 352         private CGLImageCaps() {
 353             super(true);
 354         }
 355         public boolean isTrueVolatile() {
 356             return true;
 357         }
 358     }
 359 
 360     @Override
 361     public ImageCapabilities getImageCapabilities() {
 362         return imageCaps;
 363     }
 364 
 365     @Override
 366     public VolatileImage createCompatibleVolatileImage(int width, int height,
 367                                                        int transparency,
 368                                                        int type) {
 369         if ((type != FBOBJECT && type != TEXTURE)
 370                 || transparency == Transparency.BITMASK
 371                 || type == FBOBJECT && !isCapPresent(CAPS_EXT_FBOBJECT)) {
 372             return null;
 373         }
 374         SunVolatileImage vi = new AccelTypedVolatileImage(this, width, height,
 375                                                           transparency, type);
 376         Surface sd = vi.getDestSurface();
 377         if (!(sd instanceof AccelSurface) ||
 378             ((AccelSurface)sd).getType() != type)
 379         {
 380             vi.flush();
 381             vi = null;
 382         }
 383 
 384         return vi;
 385     }
 386 
 387     @Override
 388     public ContextCapabilities getContextCapabilities() {
 389         return oglCaps;
 390     }
 391 
 392     @Override
 393     public int getMaxTextureWidth() {
 394         return Math.max(maxTextureSize / getDevice().getScaleFactor(),
 395                         getBounds().width);
 396     }
 397 
 398     @Override
 399     public int getMaxTextureHeight() {
 400         return Math.max(maxTextureSize / getDevice().getScaleFactor(),
 401                         getBounds().height);
 402     }
 403 }