Path: blob/master/src/java.desktop/macosx/classes/sun/java2d/opengl/CGLLayer.java
41159 views
/*1* Copyright (c) 2011, 2019, 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.opengl;2627import java.awt.GraphicsConfiguration;28import sun.awt.CGraphicsConfig;29import sun.java2d.NullSurfaceData;30import sun.lwawt.LWWindowPeer;31import sun.java2d.SurfaceData;32import sun.lwawt.macosx.CFLayer;3334public class CGLLayer extends CFLayer {3536private native long nativeCreateLayer();37private static native void nativeSetScale(long layerPtr, double scale);38private static native void validate(long layerPtr, CGLSurfaceData cglsd);39private static native void blitTexture(long layerPtr);4041private int scale = 1;4243public CGLLayer(LWWindowPeer peer) {44super(0, true);4546setPtr(nativeCreateLayer());47this.peer = peer;48}4950public SurfaceData replaceSurfaceData() {51if (getBounds().isEmpty()) {52surfaceData = NullSurfaceData.theInstance;53return surfaceData;54}5556// the layer redirects all painting to the buffer's graphics57// and blits the buffer to the layer surface (in drawInCGLContext callback)58CGraphicsConfig gc = (CGraphicsConfig)getGraphicsConfiguration();59surfaceData = gc.createSurfaceData(this);60setScale(gc.getDevice().getScaleFactor());61// the layer holds a reference to the buffer, which in62// turn has a reference back to this layer63if (surfaceData instanceof CGLSurfaceData) {64validate((CGLSurfaceData)surfaceData);65}6667return surfaceData;68}6970public void validate(final CGLSurfaceData cglsd) {71OGLRenderQueue rq = OGLRenderQueue.getInstance();72rq.lock();73try {74execute(ptr -> validate(ptr, cglsd));75} finally {76rq.unlock();77}78}7980@Override81public void dispose() {82// break the connection between the layer and the buffer83validate(null);84SurfaceData oldData = surfaceData;85surfaceData = NullSurfaceData.theInstance;;86if (oldData != null) {87oldData.flush();88}89super.dispose();90}9192private void setScale(final int _scale) {93if (scale != _scale) {94scale = _scale;95execute(ptr -> nativeSetScale(ptr, scale));96}97}9899// ----------------------------------------------------------------------100// NATIVE CALLBACKS101// ----------------------------------------------------------------------102103private void drawInCGLContext() {104// tell the flusher thread not to update the intermediate buffer105// until we are done blitting from it106OGLRenderQueue rq = OGLRenderQueue.getInstance();107rq.lock();108try {109execute(ptr -> blitTexture(ptr));110} finally {111rq.unlock();112}113}114}115116117