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