Path: blob/master/src/java.desktop/macosx/classes/sun/java2d/metal/MTLLayer.java
41159 views
/*1* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.metal;2627import sun.java2d.NullSurfaceData;28import sun.java2d.SurfaceData;29import sun.lwawt.LWWindowPeer;30import sun.lwawt.macosx.CFLayer;31import java.awt.GraphicsConfiguration;32import java.awt.Insets;3334public class MTLLayer extends CFLayer {3536private native long nativeCreateLayer();37private static native void nativeSetScale(long layerPtr, double scale);3839// Pass the insets to native code to make adjustments in blitTexture40private static native void nativeSetInsets(long layerPtr, int top, int left);41private static native void validate(long layerPtr, MTLSurfaceData mtlsd);42private static native void blitTexture(long layerPtr);4344private int scale = 1;4546public MTLLayer(LWWindowPeer peer) {47super(0, true);4849setPtr(nativeCreateLayer());50this.peer = peer;51}5253public SurfaceData replaceSurfaceData() {54if (getBounds().isEmpty()) {55surfaceData = NullSurfaceData.theInstance;56return surfaceData;57}5859// the layer redirects all painting to the buffer's graphics60// and blits the buffer to the layer surface (in display callback)61MTLGraphicsConfig gc = (MTLGraphicsConfig)getGraphicsConfiguration();62surfaceData = gc.createSurfaceData(this);63setScale(gc.getDevice().getScaleFactor());64if (peer != null) {65Insets insets = peer.getInsets();66execute(ptr -> nativeSetInsets(ptr, insets.top, insets.left));67}68// the layer holds a reference to the buffer, which in69// turn has a reference back to this layer70if (surfaceData instanceof MTLSurfaceData) {71validate((MTLSurfaceData)surfaceData);72}7374return surfaceData;75}7677public void validate(final MTLSurfaceData mtlsd) {78MTLRenderQueue rq = MTLRenderQueue.getInstance();79rq.lock();80try {81execute(ptr -> validate(ptr, mtlsd));82} finally {83rq.unlock();84}85}8687@Override88public void dispose() {89// break the connection between the layer and the buffer90validate(null);91SurfaceData oldData = surfaceData;92surfaceData = NullSurfaceData.theInstance;;93if (oldData != null) {94oldData.flush();95}96super.dispose();97}9899private void setScale(final int _scale) {100if (scale != _scale) {101scale = _scale;102execute(ptr -> nativeSetScale(ptr, scale));103}104}105106// ----------------------------------------------------------------------107// NATIVE CALLBACKS108// ----------------------------------------------------------------------109110private void drawInMTLContext() {111// tell the flusher thread not to update the intermediate buffer112// until we are done blitting from it113MTLRenderQueue rq = MTLRenderQueue.getInstance();114rq.lock();115try {116execute(ptr -> blitTexture(ptr));117} finally {118rq.unlock();119}120}121}122123124