src/java.desktop/macosx/classes/sun/java2d/metal/MTLLayer.java
branchmetal-prototype-branch
changeset 57416 e153174dba06
equal deleted inserted replaced
57400:978ffc56771f 57416:e153174dba06
       
     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 
       
    41     private LWWindowPeer peer;
       
    42     private int scale = 1;
       
    43 
       
    44     private SurfaceData surfaceData; // represents intermediate buffer (texture)
       
    45 
       
    46     public MTLLayer(LWWindowPeer peer) {
       
    47         super(0, true);
       
    48 
       
    49         setPtr(nativeCreateLayer());
       
    50         this.peer = peer;
       
    51     }
       
    52 
       
    53     public long getPointer() {
       
    54         return ptr;
       
    55     }
       
    56 
       
    57     public Rectangle getBounds() {
       
    58         return peer.getBounds();
       
    59     }
       
    60 
       
    61     public GraphicsConfiguration getGraphicsConfiguration() {
       
    62         return peer.getGraphicsConfiguration();
       
    63     }
       
    64 
       
    65     public boolean isOpaque() {
       
    66         return !peer.isTranslucent();
       
    67     }
       
    68 
       
    69     public int getTransparency() {
       
    70         return isOpaque() ? Transparency.OPAQUE : Transparency.TRANSLUCENT;
       
    71     }
       
    72 
       
    73     public Object getDestination() {
       
    74         return peer.getTarget();
       
    75     }
       
    76 
       
    77     public SurfaceData replaceSurfaceData() {
       
    78         if (getBounds().isEmpty()) {
       
    79             surfaceData = NullSurfaceData.theInstance;
       
    80             return surfaceData;
       
    81         }
       
    82 
       
    83         // the layer redirects all painting to the buffer's graphics
       
    84         // and blits the buffer to the layer surface (in drawInCGLContext callback)
       
    85         MTLGraphicsConfig gc = (MTLGraphicsConfig)getGraphicsConfiguration();
       
    86         surfaceData = gc.createSurfaceData(this);
       
    87         setScale(gc.getDevice().getScaleFactor());
       
    88         // the layer holds a reference to the buffer, which in
       
    89         // turn has a reference back to this layer
       
    90         if (surfaceData instanceof MTLSurfaceData) {
       
    91             validate((MTLSurfaceData)surfaceData);
       
    92         }
       
    93 
       
    94         return surfaceData;
       
    95     }
       
    96 
       
    97     public SurfaceData getSurfaceData() {
       
    98         return surfaceData;
       
    99     }
       
   100 
       
   101     public void validate(final MTLSurfaceData cglsd) {
       
   102         MTLRenderQueue rq = MTLRenderQueue.getInstance();
       
   103         rq.lock();
       
   104         try {
       
   105             execute(ptr -> validate(ptr, cglsd));
       
   106         } finally {
       
   107             rq.unlock();
       
   108         }
       
   109     }
       
   110 
       
   111     @Override
       
   112     public void dispose() {
       
   113         // break the connection between the layer and the buffer
       
   114         validate(null);
       
   115         super.dispose();
       
   116     }
       
   117 
       
   118     private void setScale(final int _scale) {
       
   119         if (scale != _scale) {
       
   120             scale = _scale;
       
   121             execute(ptr -> nativeSetScale(ptr, scale));
       
   122         }
       
   123     }
       
   124 }