Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.desktop/macosx/classes/sun/awt/CGraphicsDevice.java
66645 views
1
/*
2
* Copyright (c) 2012, 2021, 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.awt.peer.WindowPeer;
37
import java.util.Arrays;
38
import java.util.Objects;
39
40
import sun.java2d.SunGraphicsEnvironment;
41
import sun.java2d.MacOSFlags;
42
import sun.java2d.metal.MTLGraphicsConfig;
43
import sun.java2d.opengl.CGLGraphicsConfig;
44
45
import static java.awt.peer.ComponentPeer.SET_BOUNDS;
46
47
public final class CGraphicsDevice extends GraphicsDevice
48
implements DisplayChangedListener {
49
50
/**
51
* CoreGraphics display ID. This identifier can become non-valid at any time
52
* therefore methods, which is using this id should be ready to it.
53
*/
54
private volatile int displayID;
55
private volatile double xResolution;
56
private volatile double yResolution;
57
private volatile Rectangle bounds;
58
private volatile int scale;
59
60
private GraphicsConfiguration config;
61
private static boolean metalPipelineEnabled = false;
62
private static boolean oglPipelineEnabled = false;
63
64
65
private static AWTPermission fullScreenExclusivePermission;
66
67
// Save/restore DisplayMode for the Full Screen mode
68
private DisplayMode originalMode;
69
private DisplayMode initialMode;
70
71
public CGraphicsDevice(final int displayID) {
72
this.displayID = displayID;
73
this.initialMode = getDisplayMode();
74
75
if (MacOSFlags.isMetalEnabled()) {
76
// Try to create MTLGraphicsConfig, if it fails,
77
// try to create CGLGraphicsConfig as a fallback
78
this.config = MTLGraphicsConfig.getConfig(this, displayID);
79
80
if (this.config != null) {
81
metalPipelineEnabled = true;
82
} else {
83
// Try falling back to OpenGL pipeline
84
if (MacOSFlags.isMetalVerbose()) {
85
System.out.println("Metal rendering pipeline" +
86
" initialization failed,using OpenGL" +
87
" rendering pipeline");
88
}
89
90
this.config = CGLGraphicsConfig.getConfig(this);
91
92
if (this.config != null) {
93
oglPipelineEnabled = true;
94
}
95
}
96
} else {
97
// Try to create CGLGraphicsConfig, if it fails,
98
// try to create MTLGraphicsConfig as a fallback
99
this.config = CGLGraphicsConfig.getConfig(this);
100
101
if (this.config != null) {
102
oglPipelineEnabled = true;
103
} else {
104
// Try falling back to Metal pipeline
105
if (MacOSFlags.isOGLVerbose()) {
106
System.out.println("OpenGL rendering pipeline" +
107
" initialization failed,using Metal" +
108
" rendering pipeline");
109
}
110
111
this.config = MTLGraphicsConfig.getConfig(this, displayID);
112
113
if (this.config != null) {
114
metalPipelineEnabled = true;
115
}
116
}
117
}
118
119
if (!metalPipelineEnabled && !oglPipelineEnabled) {
120
// This indicates fallback to other rendering pipeline also failed.
121
// Should never reach here
122
throw new InternalError("Error - unable to initialize any" +
123
" rendering pipeline.");
124
}
125
126
if (metalPipelineEnabled && MacOSFlags.isMetalVerbose()) {
127
System.out.println("Metal pipeline enabled on screen " + displayID);
128
} else if (oglPipelineEnabled && MacOSFlags.isOGLVerbose()) {
129
System.out.println("OpenGL pipeline enabled on screen " + displayID);
130
}
131
132
// initializes default device state, might be redundant step since we
133
// call "displayChanged()" later anyway, but we do not want to leave the
134
// device in an inconsistent state after construction
135
displayChanged();
136
}
137
138
/**
139
* Return a list of all configurations.
140
*/
141
@Override
142
public GraphicsConfiguration[] getConfigurations() {
143
return new GraphicsConfiguration[]{config};
144
}
145
146
/**
147
* Return the default configuration.
148
*/
149
@Override
150
public GraphicsConfiguration getDefaultConfiguration() {
151
return config;
152
}
153
154
/**
155
* Return a human-readable screen description.
156
*/
157
@Override
158
public String getIDstring() {
159
return "Display " + displayID;
160
}
161
162
/**
163
* Returns the type of the graphics device.
164
* @see #TYPE_RASTER_SCREEN
165
* @see #TYPE_PRINTER
166
* @see #TYPE_IMAGE_BUFFER
167
*/
168
@Override
169
public int getType() {
170
return TYPE_RASTER_SCREEN;
171
}
172
173
public double getXResolution() {
174
return xResolution;
175
}
176
177
public double getYResolution() {
178
return yResolution;
179
}
180
181
Rectangle getBounds() {
182
return bounds.getBounds();
183
}
184
185
public Insets getScreenInsets() {
186
// the insets are queried synchronously and are not cached
187
// since there are no Quartz or Cocoa means to receive notifications
188
// on insets changes (e.g. when the Dock is resized):
189
// the existing CGDisplayReconfigurationCallBack is not notified
190
// as well as the NSApplicationDidChangeScreenParametersNotification
191
// is fired on the Dock location changes only
192
return nativeGetScreenInsets(displayID);
193
}
194
195
public int getScaleFactor() {
196
return scale;
197
}
198
199
/**
200
* Invalidates this device so it will point to some other "new" device.
201
*
202
* @param device the new device, usually the main screen
203
*/
204
public void invalidate(CGraphicsDevice device) {
205
//TODO do we need to restore the full-screen window/modes on old device?
206
displayID = device.displayID;
207
initialMode = device.initialMode;
208
}
209
210
@Override
211
public void displayChanged() {
212
xResolution = nativeGetXResolution(displayID);
213
yResolution = nativeGetYResolution(displayID);
214
bounds = nativeGetBounds(displayID).getBounds(); //does integer rounding
215
initScaleFactor();
216
resizeFSWindow(getFullScreenWindow(), bounds);
217
//TODO configs?
218
}
219
220
@Override
221
public void paletteChanged() {
222
// devices do not need to react to this event.
223
}
224
225
/**
226
* Enters full-screen mode, or returns to windowed mode.
227
*/
228
@Override
229
public synchronized void setFullScreenWindow(Window w) {
230
Window old = getFullScreenWindow();
231
if (w == old) {
232
return;
233
}
234
235
boolean fsSupported = isFullScreenSupported();
236
237
if (fsSupported && old != null) {
238
// enter windowed mode and restore original display mode
239
exitFullScreenExclusive(old);
240
if (originalMode != null) {
241
setDisplayMode(originalMode);
242
originalMode = null;
243
}
244
}
245
246
super.setFullScreenWindow(w);
247
248
if (fsSupported && w != null) {
249
if (isDisplayChangeSupported()) {
250
originalMode = getDisplayMode();
251
}
252
// enter fullscreen mode
253
enterFullScreenExclusive(w);
254
}
255
}
256
257
/**
258
* Returns true if this GraphicsDevice supports
259
* full-screen exclusive mode and false otherwise.
260
*/
261
@Override
262
public boolean isFullScreenSupported() {
263
return isFSExclusiveModeAllowed();
264
}
265
266
private static boolean isFSExclusiveModeAllowed() {
267
@SuppressWarnings("removal")
268
SecurityManager security = System.getSecurityManager();
269
if (security != null) {
270
if (fullScreenExclusivePermission == null) {
271
fullScreenExclusivePermission =
272
new AWTPermission("fullScreenExclusive");
273
}
274
try {
275
security.checkPermission(fullScreenExclusivePermission);
276
} catch (SecurityException e) {
277
return false;
278
}
279
}
280
return true;
281
}
282
283
private static void enterFullScreenExclusive(Window w) {
284
FullScreenCapable peer = AWTAccessor.getComponentAccessor().getPeer(w);
285
if (peer != null) {
286
peer.enterFullScreenMode();
287
}
288
}
289
290
private static void exitFullScreenExclusive(Window w) {
291
FullScreenCapable peer = AWTAccessor.getComponentAccessor().getPeer(w);
292
if (peer != null) {
293
peer.exitFullScreenMode();
294
}
295
}
296
297
/**
298
* Reapplies the size of this device to the full-screen window.
299
*/
300
private static void resizeFSWindow(final Window w, final Rectangle b) {
301
if (w != null) {
302
WindowPeer peer = AWTAccessor.getComponentAccessor().getPeer(w);
303
if (peer != null) {
304
peer.setBounds(b.x, b.y, b.width, b.height, SET_BOUNDS);
305
}
306
}
307
}
308
309
@Override
310
public boolean isDisplayChangeSupported() {
311
return true;
312
}
313
314
/* If the modes are the same or the only difference is that
315
* the new mode will match any refresh rate, no need to change.
316
*/
317
private boolean isSameMode(final DisplayMode newMode,
318
final DisplayMode oldMode) {
319
320
return (Objects.equals(newMode, oldMode) ||
321
(newMode.getRefreshRate() == DisplayMode.REFRESH_RATE_UNKNOWN &&
322
newMode.getWidth() == oldMode.getWidth() &&
323
newMode.getHeight() == oldMode.getHeight() &&
324
newMode.getBitDepth() == oldMode.getBitDepth()));
325
}
326
327
@Override
328
public void setDisplayMode(final DisplayMode dm) {
329
if (dm == null) {
330
throw new IllegalArgumentException("Invalid display mode");
331
}
332
if (!isSameMode(dm, getDisplayMode())) {
333
try {
334
nativeSetDisplayMode(displayID, dm.getWidth(), dm.getHeight(),
335
dm.getBitDepth(), dm.getRefreshRate());
336
} catch (Throwable t) {
337
/* In some cases macOS doesn't report the initial mode
338
* in the list of supported modes.
339
* If trying to reset to that mode causes an exception
340
* try one more time to reset using a different API.
341
* This does not fix everything, such as it doesn't make
342
* that mode reported and it restores all devices, but
343
* this seems a better compromise than failing to restore
344
*/
345
if (isSameMode(dm, initialMode)) {
346
nativeResetDisplayMode();
347
if (!isSameMode(initialMode, getDisplayMode())) {
348
throw new IllegalArgumentException(
349
"Could not reset to initial mode");
350
}
351
} else {
352
throw t;
353
}
354
}
355
}
356
}
357
358
@Override
359
public DisplayMode getDisplayMode() {
360
return nativeGetDisplayMode(displayID);
361
}
362
363
@Override
364
public DisplayMode[] getDisplayModes() {
365
DisplayMode[] nativeModes = nativeGetDisplayModes(displayID);
366
boolean match = false;
367
for (DisplayMode mode : nativeModes) {
368
if (initialMode.equals(mode)) {
369
match = true;
370
break;
371
}
372
}
373
if (match) {
374
return nativeModes;
375
} else {
376
int len = nativeModes.length;
377
DisplayMode[] modes = Arrays.copyOf(nativeModes, len+1, DisplayMode[].class);
378
modes[len] = initialMode;
379
return modes;
380
}
381
}
382
383
public static boolean usingMetalPipeline() {
384
return metalPipelineEnabled;
385
}
386
387
private void initScaleFactor() {
388
if (SunGraphicsEnvironment.isUIScaleEnabled()) {
389
double debugScale = SunGraphicsEnvironment.getDebugScale();
390
scale = (int) (debugScale >= 1
391
? Math.round(debugScale)
392
: nativeGetScaleFactor(displayID));
393
} else {
394
scale = 1;
395
}
396
}
397
398
private static native double nativeGetScaleFactor(int displayID);
399
400
private static native void nativeResetDisplayMode();
401
402
private static native void nativeSetDisplayMode(int displayID, int w, int h, int bpp, int refrate);
403
404
private static native DisplayMode nativeGetDisplayMode(int displayID);
405
406
private static native DisplayMode[] nativeGetDisplayModes(int displayID);
407
408
private static native double nativeGetXResolution(int displayID);
409
410
private static native double nativeGetYResolution(int displayID);
411
412
private static native Insets nativeGetScreenInsets(int displayID);
413
414
private static native Rectangle2D nativeGetBounds(int displayID);
415
}
416
417