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