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