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