Print this page
rev 54883 : JDK-8220154 Improve java2d rendering performance on macOS by using Metal framework
Split |
Close |
Expand all |
Collapse all |
--- old/src/java.desktop/macosx/classes/sun/awt/CGraphicsDevice.java
+++ new/src/java.desktop/macosx/classes/sun/awt/CGraphicsDevice.java
1 1 /*
2 2 * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation. Oracle designates this
8 8 * particular file as subject to the "Classpath" exception as provided
9 9 * by Oracle in the LICENSE file that accompanied this code.
10 10 *
11 11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 14 * version 2 for more details (a copy is included in the LICENSE file that
15 15 * accompanied this code).
16 16 *
17 17 * You should have received a copy of the GNU General Public License version
18 18 * 2 along with this work; if not, write to the Free Software Foundation,
19 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 20 *
21 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 22 * or visit www.oracle.com if you need additional information or have any
23 23 * questions.
24 24 */
25 25
26 26 package sun.awt;
27 27
28 28 import java.awt.AWTPermission;
↓ open down ↓ |
28 lines elided |
↑ open up ↑ |
29 29 import java.awt.DisplayMode;
30 30 import java.awt.GraphicsConfiguration;
31 31 import java.awt.GraphicsDevice;
32 32 import java.awt.Insets;
33 33 import java.awt.Rectangle;
34 34 import java.awt.Window;
35 35 import java.awt.geom.Rectangle2D;
36 36 import java.util.Objects;
37 37
38 38 import sun.java2d.SunGraphicsEnvironment;
39 +import sun.java2d.macos.MacOSFlags;
40 +import sun.java2d.metal.MTLGraphicsConfig;
39 41 import sun.java2d.opengl.CGLGraphicsConfig;
40 42
41 43 public final class CGraphicsDevice extends GraphicsDevice
42 44 implements DisplayChangedListener {
43 45
44 46 /**
45 47 * CoreGraphics display ID. This identifier can become non-valid at any time
46 48 * therefore methods, which is using this id should be ready to it.
47 49 */
48 50 private volatile int displayID;
49 51 private volatile double xResolution;
50 52 private volatile double yResolution;
51 53 private volatile Rectangle bounds;
52 54 private volatile int scale;
↓ open down ↓ |
4 lines elided |
↑ open up ↑ |
53 55
54 56 private final GraphicsConfiguration config;
55 57
56 58 private static AWTPermission fullScreenExclusivePermission;
57 59
58 60 // Save/restore DisplayMode for the Full Screen mode
59 61 private DisplayMode originalMode;
60 62
61 63 public CGraphicsDevice(final int displayID) {
62 64 this.displayID = displayID;
63 - config = CGLGraphicsConfig.getConfig(this, displayID, 0);
65 + config = MacOSFlags.isMetalEnabled() ?
66 + MTLGraphicsConfig.getConfig(this, displayID, 0) :
67 + CGLGraphicsConfig.getConfig(this, displayID, 0);
64 68 }
65 69
66 70 /**
67 71 * Return a list of all configurations.
68 72 */
69 73 @Override
70 74 public GraphicsConfiguration[] getConfigurations() {
71 75 return new GraphicsConfiguration[]{config};
72 76 }
73 77
74 78 /**
75 79 * Return the default configuration.
76 80 */
77 81 @Override
78 82 public GraphicsConfiguration getDefaultConfiguration() {
79 83 return config;
80 84 }
81 85
82 86 /**
83 87 * Return a human-readable screen description.
84 88 */
85 89 @Override
86 90 public String getIDstring() {
87 91 return "Display " + displayID;
88 92 }
89 93
90 94 /**
91 95 * Returns the type of the graphics device.
92 96 * @see #TYPE_RASTER_SCREEN
93 97 * @see #TYPE_PRINTER
94 98 * @see #TYPE_IMAGE_BUFFER
95 99 */
96 100 @Override
97 101 public int getType() {
98 102 return TYPE_RASTER_SCREEN;
99 103 }
100 104
101 105 public double getXResolution() {
102 106 return xResolution;
103 107 }
104 108
105 109 public double getYResolution() {
106 110 return yResolution;
107 111 }
108 112
109 113 Rectangle getBounds() {
110 114 return bounds.getBounds();
111 115 }
112 116
113 117 public Insets getScreenInsets() {
114 118 // the insets are queried synchronously and are not cached
115 119 // since there are no Quartz or Cocoa means to receive notifications
116 120 // on insets changes (e.g. when the Dock is resized):
117 121 // the existing CGDisplayReconfigurationCallBack is not notified
118 122 // as well as the NSApplicationDidChangeScreenParametersNotification
119 123 // is fired on the Dock location changes only
120 124 return nativeGetScreenInsets(displayID);
121 125 }
122 126
123 127 public int getScaleFactor() {
124 128 return scale;
125 129 }
126 130
127 131 public void invalidate(final int defaultDisplayID) {
128 132 displayID = defaultDisplayID;
129 133 }
130 134
131 135 @Override
132 136 public void displayChanged() {
133 137 xResolution = nativeGetXResolution(displayID);
134 138 yResolution = nativeGetYResolution(displayID);
135 139 bounds = nativeGetBounds(displayID).getBounds(); //does integer rounding
136 140 initScaleFactor();
137 141 //TODO configs/fullscreenWindow/modes?
138 142 }
139 143
140 144 @Override
141 145 public void paletteChanged() {
142 146 // devices do not need to react to this event.
143 147 }
144 148
145 149 /**
146 150 * Enters full-screen mode, or returns to windowed mode.
147 151 */
148 152 @Override
149 153 public synchronized void setFullScreenWindow(Window w) {
150 154 Window old = getFullScreenWindow();
151 155 if (w == old) {
152 156 return;
153 157 }
154 158
155 159 boolean fsSupported = isFullScreenSupported();
156 160
157 161 if (fsSupported && old != null) {
158 162 // enter windowed mode and restore original display mode
159 163 exitFullScreenExclusive(old);
160 164 if (originalMode != null) {
161 165 setDisplayMode(originalMode);
162 166 originalMode = null;
163 167 }
164 168 }
165 169
166 170 super.setFullScreenWindow(w);
167 171
168 172 if (fsSupported && w != null) {
169 173 if (isDisplayChangeSupported()) {
170 174 originalMode = getDisplayMode();
171 175 }
172 176 // enter fullscreen mode
173 177 enterFullScreenExclusive(w);
174 178 }
175 179 }
176 180
177 181 /**
178 182 * Returns true if this GraphicsDevice supports
179 183 * full-screen exclusive mode and false otherwise.
180 184 */
181 185 @Override
182 186 public boolean isFullScreenSupported() {
183 187 return isFSExclusiveModeAllowed();
184 188 }
185 189
186 190 private static boolean isFSExclusiveModeAllowed() {
187 191 SecurityManager security = System.getSecurityManager();
188 192 if (security != null) {
189 193 if (fullScreenExclusivePermission == null) {
190 194 fullScreenExclusivePermission =
191 195 new AWTPermission("fullScreenExclusive");
192 196 }
193 197 try {
194 198 security.checkPermission(fullScreenExclusivePermission);
195 199 } catch (SecurityException e) {
196 200 return false;
197 201 }
198 202 }
199 203 return true;
200 204 }
201 205
202 206 private static void enterFullScreenExclusive(Window w) {
203 207 FullScreenCapable peer = AWTAccessor.getComponentAccessor().getPeer(w);
204 208 if (peer != null) {
205 209 peer.enterFullScreenMode();
206 210 }
207 211 }
208 212
209 213 private static void exitFullScreenExclusive(Window w) {
210 214 FullScreenCapable peer = AWTAccessor.getComponentAccessor().getPeer(w);
211 215 if (peer != null) {
212 216 peer.exitFullScreenMode();
213 217 }
214 218 }
215 219
216 220 @Override
217 221 public boolean isDisplayChangeSupported() {
218 222 return true;
219 223 }
220 224
221 225 @Override
222 226 public void setDisplayMode(final DisplayMode dm) {
223 227 if (dm == null) {
224 228 throw new IllegalArgumentException("Invalid display mode");
225 229 }
226 230 if (!Objects.equals(dm, getDisplayMode())) {
227 231 nativeSetDisplayMode(displayID, dm.getWidth(), dm.getHeight(),
228 232 dm.getBitDepth(), dm.getRefreshRate());
229 233 if (isFullScreenSupported() && getFullScreenWindow() != null) {
230 234 getFullScreenWindow().setSize(dm.getWidth(), dm.getHeight());
231 235 }
232 236 }
233 237 }
234 238
235 239 @Override
236 240 public DisplayMode getDisplayMode() {
237 241 return nativeGetDisplayMode(displayID);
238 242 }
239 243
240 244 @Override
241 245 public DisplayMode[] getDisplayModes() {
242 246 return nativeGetDisplayModes(displayID);
243 247 }
244 248
245 249 private void initScaleFactor() {
246 250 if (SunGraphicsEnvironment.isUIScaleEnabled()) {
247 251 double debugScale = SunGraphicsEnvironment.getDebugScale();
248 252 scale = (int) (debugScale >= 1
249 253 ? Math.round(debugScale)
250 254 : nativeGetScaleFactor(displayID));
251 255 } else {
252 256 scale = 1;
253 257 }
254 258 }
255 259
256 260 private static native double nativeGetScaleFactor(int displayID);
257 261
258 262 private static native void nativeSetDisplayMode(int displayID, int w, int h, int bpp, int refrate);
259 263
260 264 private static native DisplayMode nativeGetDisplayMode(int displayID);
261 265
262 266 private static native DisplayMode[] nativeGetDisplayModes(int displayID);
263 267
264 268 private static native double nativeGetXResolution(int displayID);
265 269
266 270 private static native double nativeGetYResolution(int displayID);
267 271
268 272 private static native Insets nativeGetScreenInsets(int displayID);
269 273
270 274 private static native Rectangle2D nativeGetBounds(int displayID);
271 275 }
↓ open down ↓ |
198 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX