1 /* 2 * Copyright (c) 2011, 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.lwawt.macosx; 27 28 import java.awt.*; 29 import java.awt.geom.Rectangle2D; 30 import java.util.concurrent.atomic.AtomicBoolean; 31 import java.util.concurrent.atomic.AtomicInteger; 32 import java.util.concurrent.atomic.AtomicReference; 33 34 import sun.awt.CGraphicsConfig; 35 import sun.awt.CGraphicsEnvironment; 36 import sun.java2d.metal.MetalLayer; 37 import sun.java2d.metal.MetalSurfaceData; 38 import sun.lwawt.LWWindowPeer; 39 40 import sun.java2d.SurfaceData; 41 import sun.java2d.opengl.CGLLayer; 42 import sun.java2d.opengl.CGLSurfaceData; 43 import sun.java2d.NullSurfaceData; 44 45 46 public class CPlatformView extends CFRetainedResource { 47 private native long nativeCreateView(int x, int y, int width, int height, long windowLayerPtr); 48 private static native void nativeSetAutoResizable(long awtView, boolean toResize); 49 private static native int nativeGetNSViewDisplayID(long awtView); 50 private static native Rectangle2D nativeGetLocationOnScreen(long awtView); 51 private static native boolean nativeIsViewUnderMouse(long ptr); 52 53 private LWWindowPeer peer; 54 private SurfaceData surfaceData; 55 private CGLLayer windowLayer; 56 private MetalLayer windowMetalLayer; //hack : will be null if opengl is used 57 //private CFRetainedResource windowLayer; 58 //Todo: Have to verify how we can replace CGL layer with more common CFRetaindedResource 59 private CPlatformResponder responder; 60 61 public CPlatformView() { 62 super(0, true); 63 } 64 65 66 public void initialize(LWWindowPeer peer, CPlatformResponder responder) { 67 initializeBase(peer, responder); 68 69 if (!LWCToolkit.getSunAwtDisableCALayers()) { 70 71 if (isMetalSystemProperty()) { 72 this.windowMetalLayer = createMetalLayer(); 73 } else { 74 this.windowLayer = createCGLayer(); 75 } 76 } 77 setPtr(nativeCreateView(0, 0, 0, 0, getWindowLayerPtr())); 78 79 // TODO : We should not simply validate view directly here. 80 //if (isMetalSystemProperty()) { 81 //windowMetalLayer.validate(getAWTView()); 82 //} 83 } 84 85 private boolean isMetalSystemProperty() { 86 String str = System.getProperty("sun.java2d.metal"); 87 88 if (str != null) { 89 System.out.println("Property : sun.java2d.metal=" + str); 90 if (str.equals("true")) { 91 return true; 92 } 93 } 94 return false; 95 } 96 97 public CGLLayer createCGLayer() { 98 return new CGLLayer(peer); 99 } 100 101 public MetalLayer createMetalLayer() { 102 return new MetalLayer(peer); 103 } 104 protected void initializeBase(LWWindowPeer peer, CPlatformResponder responder) { 105 this.peer = peer; 106 this.responder = responder; 107 } 108 109 public long getAWTView() { 110 return ptr; 111 } 112 113 public boolean isOpaque() { 114 return !peer.isTranslucent(); 115 } 116 117 /* 118 * All coordinates passed to the method should be based on the origin being in the bottom-left corner (standard 119 * Cocoa coordinates). 120 */ 121 public void setBounds(int x, int y, int width, int height) { 122 execute(ptr->CWrapper.NSView.setFrame(ptr, x, y, width, height)); 123 124 // TODO : Check the use case that why below code is added. 125 //if (windowMetalLayer != null) { 126 //windowMetalLayer.validate(getAWTView()); 127 //} 128 } 129 130 // REMIND: CGLSurfaceData expects top-level's size 131 public Rectangle getBounds() { 132 return peer.getBounds(); 133 } 134 135 public Object getDestination() { 136 return peer; 137 } 138 139 public void setToolTip(String msg) { 140 execute(ptr -> CWrapper.NSView.setToolTip(ptr, msg)); 141 } 142 143 // ---------------------------------------------------------------------- 144 // PAINTING METHODS 145 // ---------------------------------------------------------------------- 146 public SurfaceData replaceSurfaceData() { 147 if (!LWCToolkit.getSunAwtDisableCALayers()) { 148 149 if (isMetalSystemProperty()) { 150 surfaceData = windowMetalLayer.replaceSurfaceData(); 151 152 // TODO : Why we are checking about NullSurfaceData here 153 //if (surfaceData != NullSurfaceData.theInstance) { 154 //validateSurface(); 155 //windowMetalLayer.drawInMetalContext(getAWTView()); 156 //} 157 158 } else { 159 surfaceData = windowLayer.replaceSurfaceData(); 160 } 161 162 } else { 163 if (surfaceData == null) { 164 CGraphicsConfig graphicsConfig = (CGraphicsConfig)getGraphicsConfiguration(); 165 surfaceData = graphicsConfig.createSurfaceData(this); 166 } else { 167 validateSurface(); 168 } 169 } 170 return surfaceData; 171 } 172 173 private void validateSurface() { 174 175 if (surfaceData != null) { 176 // TODO : Why we are validating with View here 177 if (isMetalSystemProperty()) { 178 //((MetalSurfaceData)surfaceData).validate(); 179 //windowMetalLayer.validate(getAWTView()); 180 ((MetalSurfaceData)surfaceData).validate(); 181 } else { 182 ((CGLSurfaceData)surfaceData).validate(); 183 } 184 } 185 } 186 187 public GraphicsConfiguration getGraphicsConfiguration() { 188 return peer.getGraphicsConfiguration(); 189 } 190 191 public SurfaceData getSurfaceData() { 192 return surfaceData; 193 } 194 195 @Override 196 public void dispose() { 197 if (!LWCToolkit.getSunAwtDisableCALayers()) { 198 if (isMetalSystemProperty()) { 199 windowMetalLayer.dispose(); 200 } else { 201 windowLayer.dispose(); 202 } 203 } 204 super.dispose(); 205 } 206 207 public long getWindowLayerPtr() { 208 if (!LWCToolkit.getSunAwtDisableCALayers()) { 209 if (isMetalSystemProperty()) { 210 return windowMetalLayer.getPointer(); 211 } else { 212 return windowLayer.getPointer(); 213 } 214 } else { 215 return 0; 216 } 217 } 218 219 public void setAutoResizable(boolean toResize) { 220 execute(ptr -> nativeSetAutoResizable(ptr, toResize)); 221 } 222 223 public boolean isUnderMouse() { 224 AtomicBoolean ref = new AtomicBoolean(); 225 execute(ptr -> { 226 ref.set(nativeIsViewUnderMouse(ptr)); 227 }); 228 return ref.get(); 229 } 230 231 public GraphicsDevice getGraphicsDevice() { 232 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 233 CGraphicsEnvironment cge = (CGraphicsEnvironment)ge; | 1 /* 2 * Copyright (c) 2011, 2016, 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.lwawt.macosx; 27 28 import java.awt.*; 29 import java.awt.geom.Rectangle2D; 30 import java.util.concurrent.atomic.AtomicBoolean; 31 import java.util.concurrent.atomic.AtomicInteger; 32 import java.util.concurrent.atomic.AtomicReference; 33 34 import sun.awt.CGraphicsConfig; 35 import sun.awt.CGraphicsEnvironment; 36 import sun.java2d.macos.MacOSFlags; 37 import sun.java2d.metal.MTLLayer; 38 import sun.java2d.metal.MTLSurfaceData; 39 import sun.lwawt.LWWindowPeer; 40 41 import sun.java2d.SurfaceData; 42 import sun.java2d.opengl.CGLLayer; 43 import sun.java2d.opengl.CGLSurfaceData; 44 45 public class CPlatformView extends CFRetainedResource { 46 private native long nativeCreateView(int x, int y, int width, int height, long windowLayerPtr); 47 private static native void nativeSetAutoResizable(long awtView, boolean toResize); 48 private static native int nativeGetNSViewDisplayID(long awtView); 49 private static native Rectangle2D nativeGetLocationOnScreen(long awtView); 50 private static native boolean nativeIsViewUnderMouse(long ptr); 51 52 private LWWindowPeer peer; 53 private SurfaceData surfaceData; 54 private CFRetainedResource windowLayer; 55 private CPlatformResponder responder; 56 57 public CPlatformView() { 58 super(0, true); 59 } 60 61 public void initialize(LWWindowPeer peer, CPlatformResponder responder) { 62 initializeBase(peer, responder); 63 64 if (!LWCToolkit.getSunAwtDisableCALayers()) { 65 this.windowLayer = MacOSFlags.isMetalEnabled()? createMTLLayer() : createCGLayer(); 66 } 67 setPtr(nativeCreateView(0, 0, 0, 0, getWindowLayerPtr())); 68 } 69 70 public CGLLayer createCGLayer() { 71 return new CGLLayer(peer); 72 } 73 74 public MTLLayer createMTLLayer() { 75 return new MTLLayer(peer); 76 } 77 78 79 protected void initializeBase(LWWindowPeer peer, CPlatformResponder responder) { 80 this.peer = peer; 81 this.responder = responder; 82 } 83 84 public long getAWTView() { 85 return ptr; 86 } 87 88 public boolean isOpaque() { 89 return !peer.isTranslucent(); 90 } 91 92 /* 93 * All coordinates passed to the method should be based on the origin being in the bottom-left corner (standard 94 * Cocoa coordinates). 95 */ 96 public void setBounds(int x, int y, int width, int height) { 97 execute(ptr->CWrapper.NSView.setFrame(ptr, x, y, width, height)); 98 } 99 100 // REMIND: CGLSurfaceData expects top-level's size 101 public Rectangle getBounds() { 102 return peer.getBounds(); 103 } 104 105 public Object getDestination() { 106 return peer; 107 } 108 109 public void setToolTip(String msg) { 110 execute(ptr -> CWrapper.NSView.setToolTip(ptr, msg)); 111 } 112 113 // ---------------------------------------------------------------------- 114 // PAINTING METHODS 115 // ---------------------------------------------------------------------- 116 public SurfaceData replaceSurfaceData() { 117 if (!LWCToolkit.getSunAwtDisableCALayers()) { 118 surfaceData = (MacOSFlags.isMetalEnabled()) ? 119 ((MTLLayer)windowLayer).replaceSurfaceData() : 120 ((CGLLayer)windowLayer).replaceSurfaceData() 121 ; 122 } else { 123 if (surfaceData == null) { 124 CGraphicsConfig graphicsConfig = (CGraphicsConfig)getGraphicsConfiguration(); 125 surfaceData = graphicsConfig.createSurfaceData(this); 126 } else { 127 validateSurface(); 128 } 129 } 130 return surfaceData; 131 } 132 133 private void validateSurface() { 134 if (surfaceData != null) { 135 if (MacOSFlags.isMetalEnabled()) { 136 ((MTLSurfaceData) surfaceData).validate(); 137 } else { 138 ((CGLSurfaceData) surfaceData).validate(); 139 } 140 } 141 } 142 143 public GraphicsConfiguration getGraphicsConfiguration() { 144 return peer.getGraphicsConfiguration(); 145 } 146 147 public SurfaceData getSurfaceData() { 148 return surfaceData; 149 } 150 151 @Override 152 public void dispose() { 153 if (!LWCToolkit.getSunAwtDisableCALayers()) { 154 windowLayer.dispose(); 155 } 156 super.dispose(); 157 } 158 159 public long getWindowLayerPtr() { 160 if (!LWCToolkit.getSunAwtDisableCALayers()) { 161 return MacOSFlags.isMetalEnabled() ? 162 ((MTLLayer)windowLayer).getPointer() : 163 ((CGLLayer)windowLayer).getPointer(); 164 } else { 165 return 0; 166 } 167 } 168 169 public void setAutoResizable(boolean toResize) { 170 execute(ptr -> nativeSetAutoResizable(ptr, toResize)); 171 } 172 173 public boolean isUnderMouse() { 174 AtomicBoolean ref = new AtomicBoolean(); 175 execute(ptr -> { 176 ref.set(nativeIsViewUnderMouse(ptr)); 177 }); 178 return ref.get(); 179 } 180 181 public GraphicsDevice getGraphicsDevice() { 182 GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); 183 CGraphicsEnvironment cge = (CGraphicsEnvironment)ge; |