src/java.desktop/macosx/classes/sun/java2d/metal/MTLLayer.java
author jdv
Fri, 21 Jun 2019 12:08:37 +0530
branchmetal-prototype-branch
changeset 57416 e153174dba06
permissions -rw-r--r--
Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch Reviewed-by: avu, prr, kcr Contributed-by: avu(Jetbrains), aghaisas, jdv
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57416
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     1
/*
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     4
 *
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    10
 *
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    15
 * accompanied this code).
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    16
 *
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    20
 *
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    23
 * questions.
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    24
 */
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    25
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    26
package sun.java2d.metal;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    27
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    28
import sun.java2d.NullSurfaceData;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    29
import sun.java2d.SurfaceData;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    30
import sun.lwawt.LWWindowPeer;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    31
import sun.lwawt.macosx.CFRetainedResource;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    32
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    33
import java.awt.*;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    34
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    35
public class MTLLayer extends CFRetainedResource {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    36
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    37
    private native long nativeCreateLayer();
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    38
    private static native void nativeSetScale(long layerPtr, double scale);
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    39
    private static native void validate(long layerPtr, MTLSurfaceData cglsd);
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    40
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    41
    private LWWindowPeer peer;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    42
    private int scale = 1;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    43
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    44
    private SurfaceData surfaceData; // represents intermediate buffer (texture)
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    45
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    46
    public MTLLayer(LWWindowPeer peer) {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    47
        super(0, true);
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    48
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    49
        setPtr(nativeCreateLayer());
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    50
        this.peer = peer;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    51
    }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    52
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    53
    public long getPointer() {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    54
        return ptr;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    55
    }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    56
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    57
    public Rectangle getBounds() {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    58
        return peer.getBounds();
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    59
    }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    60
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    61
    public GraphicsConfiguration getGraphicsConfiguration() {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    62
        return peer.getGraphicsConfiguration();
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    63
    }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    64
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    65
    public boolean isOpaque() {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    66
        return !peer.isTranslucent();
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    67
    }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    68
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    69
    public int getTransparency() {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    70
        return isOpaque() ? Transparency.OPAQUE : Transparency.TRANSLUCENT;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    71
    }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    72
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    73
    public Object getDestination() {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    74
        return peer.getTarget();
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    75
    }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    76
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    77
    public SurfaceData replaceSurfaceData() {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    78
        if (getBounds().isEmpty()) {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    79
            surfaceData = NullSurfaceData.theInstance;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    80
            return surfaceData;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    81
        }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    82
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    83
        // the layer redirects all painting to the buffer's graphics
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    84
        // and blits the buffer to the layer surface (in drawInCGLContext callback)
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    85
        MTLGraphicsConfig gc = (MTLGraphicsConfig)getGraphicsConfiguration();
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    86
        surfaceData = gc.createSurfaceData(this);
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    87
        setScale(gc.getDevice().getScaleFactor());
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    88
        // the layer holds a reference to the buffer, which in
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    89
        // turn has a reference back to this layer
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    90
        if (surfaceData instanceof MTLSurfaceData) {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    91
            validate((MTLSurfaceData)surfaceData);
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    92
        }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    93
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    94
        return surfaceData;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    95
    }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    96
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    97
    public SurfaceData getSurfaceData() {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    98
        return surfaceData;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
    99
    }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   100
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   101
    public void validate(final MTLSurfaceData cglsd) {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   102
        MTLRenderQueue rq = MTLRenderQueue.getInstance();
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   103
        rq.lock();
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   104
        try {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   105
            execute(ptr -> validate(ptr, cglsd));
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   106
        } finally {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   107
            rq.unlock();
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   108
        }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   109
    }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   110
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   111
    @Override
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   112
    public void dispose() {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   113
        // break the connection between the layer and the buffer
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   114
        validate(null);
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   115
        super.dispose();
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   116
    }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   117
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   118
    private void setScale(final int _scale) {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   119
        if (scale != _scale) {
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   120
            scale = _scale;
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   121
            execute(ptr -> nativeSetScale(ptr, scale));
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   122
        }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   123
    }
e153174dba06 Merge JDK-8220154 initial metal implementation patch to the jdk sandbox branch
jdv
parents:
diff changeset
   124
}