1 /* 2 * Copyright (c) 2012, 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.awt; 27 28 import java.awt.AWTPermission; 29 import java.awt.DisplayMode; 30 import java.awt.GraphicsConfiguration; 31 import java.awt.GraphicsDevice; 32 import java.awt.Insets; 33 import java.awt.Rectangle; 34 import java.awt.Window; 35 import java.awt.geom.Rectangle2D; 36 import java.util.Objects; 37 38 import sun.java2d.SunGraphicsEnvironment; 39 import sun.java2d.opengl.CGLGraphicsConfig; 40 import sun.java2d.metal.MetalGraphicsConfig; 41 42 public final class CGraphicsDevice extends GraphicsDevice 43 implements DisplayChangedListener { 44 45 /** 46 * CoreGraphics display ID. This identifier can become non-valid at any time 47 * therefore methods, which is using this id should be ready to it. 48 */ 49 private volatile int displayID; 50 private volatile double xResolution; 51 private volatile double yResolution; 52 private volatile Rectangle bounds; 53 private volatile int scale; 54 55 private final GraphicsConfiguration config; 56 57 private static AWTPermission fullScreenExclusivePermission; 58 59 // Save/restore DisplayMode for the Full Screen mode 60 private DisplayMode originalMode; 61 62 public CGraphicsDevice(final int displayID) { 63 this.displayID = displayID; 64 65 if (isMetalSystemProperty()) { 66 config = MetalGraphicsConfig.getConfig(this, displayID, 0); 67 System.out.println("Created MetalGraphicsConfig"); 68 } else { 69 config = CGLGraphicsConfig.getConfig(this, displayID, 0); 70 } 71 } 72 73 private boolean isMetalSystemProperty() { 74 String str = System.getProperty("sun.java2d.metal"); 75 76 if (str != null) { 77 System.out.println("Property : sun.java2d.metal=" + str); 78 if (str.equals("true")) { 79 return true; 80 } 81 } 82 return false; 83 } 84 85 /** 86 * Return a list of all configurations. 87 */ 88 @Override 89 public GraphicsConfiguration[] getConfigurations() { 90 return new GraphicsConfiguration[]{config}; 91 } 92 93 /** 94 * Return the default configuration. 95 */ 96 @Override 97 public GraphicsConfiguration getDefaultConfiguration() { 98 return config; 99 } 100 101 /** 102 * Return a human-readable screen description. 103 */ 104 @Override 105 public String getIDstring() { 106 return "Display " + displayID; 107 } 108 109 /** 110 * Returns the type of the graphics device. 111 * @see #TYPE_RASTER_SCREEN 112 * @see #TYPE_PRINTER 113 * @see #TYPE_IMAGE_BUFFER 114 */ 115 @Override 116 public int getType() { 117 return TYPE_RASTER_SCREEN; 118 } 119 120 public double getXResolution() { 121 return xResolution; 122 } 123 124 public double getYResolution() { 125 return yResolution; 126 } 127 128 Rectangle getBounds() { 129 return bounds.getBounds(); 130 } 131 132 public Insets getScreenInsets() { 133 // the insets are queried synchronously and are not cached 134 // since there are no Quartz or Cocoa means to receive notifications 135 // on insets changes (e.g. when the Dock is resized): 136 // the existing CGDisplayReconfigurationCallBack is not notified 137 // as well as the NSApplicationDidChangeScreenParametersNotification 138 // is fired on the Dock location changes only 139 return nativeGetScreenInsets(displayID); 140 } 141 142 public int getScaleFactor() { 143 return scale; 144 } 145 146 public void invalidate(final int defaultDisplayID) { 147 displayID = defaultDisplayID; 148 } 149 150 @Override 151 public void displayChanged() { 152 xResolution = nativeGetXResolution(displayID); 153 yResolution = nativeGetYResolution(displayID); 154 bounds = nativeGetBounds(displayID).getBounds(); //does integer rounding 155 initScaleFactor(); 156 //TODO configs/fullscreenWindow/modes? 157 } 158 159 @Override 160 public void paletteChanged() { 161 // devices do not need to react to this event. 162 } 163 164 /** 165 * Enters full-screen mode, or returns to windowed mode. 166 */ 167 @Override 168 public synchronized void setFullScreenWindow(Window w) { 169 Window old = getFullScreenWindow(); 170 if (w == old) { 171 return; 172 } 173 174 boolean fsSupported = isFullScreenSupported(); 175 176 if (fsSupported && old != null) { 177 // enter windowed mode and restore original display mode 178 exitFullScreenExclusive(old); 179 if (originalMode != null) { 180 setDisplayMode(originalMode); 181 originalMode = null; 182 } 183 } 184 185 super.setFullScreenWindow(w); 186 187 if (fsSupported && w != null) { 188 if (isDisplayChangeSupported()) { 189 originalMode = getDisplayMode(); 190 } 191 // enter fullscreen mode 192 enterFullScreenExclusive(w); 193 } 194 } 195 196 /** 197 * Returns true if this GraphicsDevice supports 198 * full-screen exclusive mode and false otherwise. 199 */ 200 @Override 201 public boolean isFullScreenSupported() { 202 return isFSExclusiveModeAllowed(); 203 } 204 205 private static boolean isFSExclusiveModeAllowed() { 206 SecurityManager security = System.getSecurityManager(); 207 if (security != null) { 208 if (fullScreenExclusivePermission == null) { 209 fullScreenExclusivePermission = 210 new AWTPermission("fullScreenExclusive"); 211 } 212 try { 213 security.checkPermission(fullScreenExclusivePermission); 214 } catch (SecurityException e) { 215 return false; 216 } 217 } 218 return true; 219 } 220 221 private static void enterFullScreenExclusive(Window w) { 222 FullScreenCapable peer = AWTAccessor.getComponentAccessor().getPeer(w); 223 if (peer != null) { 224 peer.enterFullScreenMode(); 225 } 226 } 227 228 private static void exitFullScreenExclusive(Window w) { 229 FullScreenCapable peer = AWTAccessor.getComponentAccessor().getPeer(w); 230 if (peer != null) { 231 peer.exitFullScreenMode(); 232 } 233 } 234 235 @Override 236 public boolean isDisplayChangeSupported() { 237 return true; 238 } 239 240 @Override 241 public void setDisplayMode(final DisplayMode dm) { 242 if (dm == null) { 243 throw new IllegalArgumentException("Invalid display mode"); 244 } 245 if (!Objects.equals(dm, getDisplayMode())) { 246 nativeSetDisplayMode(displayID, dm.getWidth(), dm.getHeight(), 247 dm.getBitDepth(), dm.getRefreshRate()); 248 if (isFullScreenSupported() && getFullScreenWindow() != null) { 249 getFullScreenWindow().setSize(dm.getWidth(), dm.getHeight()); 250 } 251 } 252 } 253 254 @Override 255 public DisplayMode getDisplayMode() { 256 return nativeGetDisplayMode(displayID); 257 } 258 259 @Override 260 public DisplayMode[] getDisplayModes() { 261 return nativeGetDisplayModes(displayID); 262 } 263 264 private void initScaleFactor() { 265 if (SunGraphicsEnvironment.isUIScaleEnabled()) { 266 double debugScale = SunGraphicsEnvironment.getDebugScale(); 267 scale = (int) (debugScale >= 1 268 ? Math.round(debugScale) 269 : nativeGetScaleFactor(displayID)); 270 } else { 271 scale = 1; 272 } 273 } 274 275 private static native double nativeGetScaleFactor(int displayID); 276 277 private static native void nativeSetDisplayMode(int displayID, int w, int h, int bpp, int refrate); 278 279 private static native DisplayMode nativeGetDisplayMode(int displayID); 280 281 private static native DisplayMode[] nativeGetDisplayModes(int displayID); 282 283 private static native double nativeGetXResolution(int displayID); 284 285 private static native double nativeGetYResolution(int displayID); 286 287 private static native Insets nativeGetScreenInsets(int displayID); 288 289 private static native Rectangle2D nativeGetBounds(int displayID); 290 }