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.java2d.NullSurfaceData;
  29 import sun.java2d.SurfaceData;
  30 import sun.lwawt.LWWindowPeer;
  31 import sun.lwawt.macosx.CFRetainedResource;
  32 
  33 import java.awt.*;
  34 
  35 public class MTLLayer extends CFRetainedResource {
  36 
  37     private native long nativeCreateLayer();
  38     private static native void nativeSetScale(long layerPtr, double scale);
  39     private static native void validate(long layerPtr, MTLSurfaceData cglsd);
  40     private static native void blitTexture(long layerPtr);
  41 
  42     private LWWindowPeer peer;
  43     private int scale = 1;
  44 
  45     private SurfaceData surfaceData; // represents intermediate buffer (texture)
  46 
  47     public MTLLayer(LWWindowPeer peer) {
  48         super(0, true);
  49 
  50         setPtr(nativeCreateLayer());
  51         this.peer = peer;
  52     }
  53 
  54     public long getPointer() {
  55         return ptr;
  56     }
  57 
  58     public Rectangle getBounds() {
  59         return peer.getBounds();
  60     }
  61 
  62     public GraphicsConfiguration getGraphicsConfiguration() {
  63         return peer.getGraphicsConfiguration();
  64     }
  65 
  66     public boolean isOpaque() {
  67         return !peer.isTranslucent();
  68     }
  69 
  70     public int getTransparency() {
  71         return isOpaque() ? Transparency.OPAQUE : Transparency.TRANSLUCENT;
  72     }
  73 
  74     public Object getDestination() {
  75         return peer.getTarget();
  76     }
  77 
  78     public SurfaceData replaceSurfaceData() {
  79         if (getBounds().isEmpty()) {
  80             surfaceData = NullSurfaceData.theInstance;
  81             return surfaceData;
  82         }
  83 
  84         // the layer redirects all painting to the buffer's graphics
  85         // and blits the buffer to the layer surface (in drawInCGLContext callback)
  86         MTLGraphicsConfig gc = (MTLGraphicsConfig)getGraphicsConfiguration();
  87         surfaceData = gc.createSurfaceData(this);
  88         setScale(gc.getDevice().getScaleFactor());
  89         // the layer holds a reference to the buffer, which in
  90         // turn has a reference back to this layer
  91         if (surfaceData instanceof MTLSurfaceData) {
  92             validate((MTLSurfaceData)surfaceData);
  93         }
  94 
  95         return surfaceData;
  96     }
  97 
  98     public SurfaceData getSurfaceData() {
  99         return surfaceData;
 100     }
 101 
 102     public void validate(final MTLSurfaceData cglsd) {
 103         MTLRenderQueue rq = MTLRenderQueue.getInstance();
 104         rq.lock();
 105         try {
 106             execute(ptr -> validate(ptr, cglsd));
 107         } finally {
 108             rq.unlock();
 109         }
 110     }
 111 
 112     @Override
 113     public void dispose() {
 114         // break the connection between the layer and the buffer
 115         validate(null);
 116         super.dispose();
 117     }
 118 
 119     private void setScale(final int _scale) {
 120         if (scale != _scale) {
 121             scale = _scale;
 122             execute(ptr -> nativeSetScale(ptr, scale));
 123         }
 124     }
 125 
 126     // ----------------------------------------------------------------------
 127     // NATIVE CALLBACKS
 128     // ----------------------------------------------------------------------
 129 
 130     private void drawInCGLContext() {
 131         // tell the flusher thread not to update the intermediate buffer
 132         // until we are done blitting from it
 133         MTLRenderQueue rq = MTLRenderQueue.getInstance();
 134         rq.lock();
 135         try {
 136             execute(ptr -> blitTexture(ptr));
 137         } finally {
 138             rq.unlock();
 139         }
 140     }
 141 }