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