Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/macosx/classes/sun/awt/CGraphicsEnvironment.java
41152 views
1
/*
2
* Copyright (c) 2011, 2020, 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.Font;
29
import java.awt.GraphicsConfiguration;
30
import java.awt.GraphicsDevice;
31
import java.awt.HeadlessException;
32
import java.awt.Toolkit;
33
import java.lang.ref.WeakReference;
34
import java.util.ArrayList;
35
import java.util.HashMap;
36
import java.util.List;
37
import java.util.ListIterator;
38
import java.util.Map;
39
40
import sun.java2d.MacosxSurfaceManagerFactory;
41
import sun.java2d.SunGraphicsEnvironment;
42
import sun.java2d.SurfaceManagerFactory;
43
44
/**
45
* This is an implementation of a GraphicsEnvironment object for the default
46
* local GraphicsEnvironment used by the Java Runtime Environment for Mac OS X
47
* GUI environments.
48
*
49
* @see GraphicsDevice
50
* @see GraphicsConfiguration
51
*/
52
public final class CGraphicsEnvironment extends SunGraphicsEnvironment {
53
54
/**
55
* Fetch an array of all valid CoreGraphics display identifiers.
56
*/
57
private static native int[] getDisplayIDs();
58
59
/**
60
* Fetch the CoreGraphics display ID for the 'main' display.
61
*/
62
private static native int getMainDisplayID();
63
64
/**
65
* Noop function that just acts as an entry point for someone to force a
66
* static initialization of this class.
67
*/
68
public static void init() { }
69
70
static {
71
// Load libraries and initialize the Toolkit.
72
Toolkit.getDefaultToolkit();
73
// Install the correct surface manager factory.
74
SurfaceManagerFactory.setInstance(new MacosxSurfaceManagerFactory());
75
}
76
77
/**
78
* Register the instance with CGDisplayRegisterReconfigurationCallback().
79
* The registration uses a weak global reference -- if our instance is
80
* garbage collected, the reference will be dropped.
81
*
82
* @return Return the registration context (a pointer).
83
*/
84
private native long registerDisplayReconfiguration();
85
86
/**
87
* Remove the instance's registration with CGDisplayRemoveReconfigurationCallback()
88
*/
89
private native void deregisterDisplayReconfiguration(long context);
90
91
/** Available CoreGraphics displays. */
92
private final Map<Integer, CGraphicsDevice> devices = new HashMap<>(5);
93
/**
94
* The key in the {@link #devices} for the main display.
95
*/
96
private int mainDisplayID;
97
98
/** Reference to the display reconfiguration callback context. */
99
private final long displayReconfigContext;
100
101
// list of invalidated graphics devices (those which were removed)
102
private List<WeakReference<CGraphicsDevice>> oldDevices = new ArrayList<>();
103
104
/**
105
* Construct a new instance.
106
*/
107
public CGraphicsEnvironment() {
108
if (isHeadless()) {
109
displayReconfigContext = 0L;
110
return;
111
}
112
113
/* Populate the device table */
114
rebuildDevices();
115
116
/* Register our display reconfiguration listener */
117
displayReconfigContext = registerDisplayReconfiguration();
118
if (displayReconfigContext == 0L) {
119
throw new RuntimeException("Could not register CoreGraphics display reconfiguration callback");
120
}
121
}
122
123
/**
124
* Updates the list of devices and notify listeners.
125
*/
126
private void rebuildDevices() {
127
initDevices();
128
displayChanged();
129
}
130
131
/**
132
* Called by the CoreGraphics Display Reconfiguration Callback.
133
*
134
* @param displayId CoreGraphics displayId
135
* @param removed true if displayId was removed, false otherwise.
136
*/
137
void _displayReconfiguration(int displayId, boolean removed) {
138
// we ignore the passed parameters and check removed devices ourself
139
// Note that it is possible that this callback is called when the
140
// monitors are not added nor removed, but when the video card is
141
// switched to/from the discrete video card, so we should try to map the
142
// old to the new devices.
143
rebuildDevices();
144
}
145
146
@Override
147
@SuppressWarnings("deprecation")
148
protected void finalize() throws Throwable {
149
try {
150
super.finalize();
151
} finally {
152
deregisterDisplayReconfiguration(displayReconfigContext);
153
}
154
}
155
156
/**
157
* (Re)create all CGraphicsDevices, reuses a devices if it is possible.
158
*/
159
private synchronized void initDevices() {
160
Map<Integer, CGraphicsDevice> old = new HashMap<>(devices);
161
devices.clear();
162
mainDisplayID = getMainDisplayID();
163
164
// initialization of the graphics device may change list of displays on
165
// hybrid systems via an activation of discrete video.
166
// So, we initialize the main display first, then retrieve actual list
167
// of displays, and then recheck the main display again.
168
if (!old.containsKey(mainDisplayID)) {
169
old.put(mainDisplayID, new CGraphicsDevice(mainDisplayID));
170
}
171
172
int[] displayIDs = getDisplayIDs();
173
if (displayIDs.length == 0) {
174
// we could throw AWTError in this case.
175
displayIDs = new int[]{mainDisplayID};
176
}
177
for (int id : displayIDs) {
178
devices.put(id, old.containsKey(id) ? old.remove(id)
179
: new CGraphicsDevice(id));
180
}
181
// fetch the main display again, the old value might be outdated
182
mainDisplayID = getMainDisplayID();
183
184
// unlikely but make sure the main screen is in the list of screens,
185
// most probably one more "displayReconfiguration" is on the road if not
186
if (!devices.containsKey(mainDisplayID)) {
187
mainDisplayID = displayIDs[0]; // best we can do
188
}
189
// if a device was not reused it should be invalidated
190
for (CGraphicsDevice gd : old.values()) {
191
oldDevices.add(new WeakReference<>(gd));
192
}
193
// Need to notify old devices, in case the user hold the reference to it
194
for (ListIterator<WeakReference<CGraphicsDevice>> it =
195
oldDevices.listIterator(); it.hasNext(); ) {
196
CGraphicsDevice gd = it.next().get();
197
if (gd != null) {
198
// If the old device has the same bounds as some new device
199
// then map that old device to the new, or to the main screen.
200
CGraphicsDevice similarDevice = getSimilarDevice(gd);
201
if (similarDevice == null) {
202
gd.invalidate(devices.get(mainDisplayID));
203
} else {
204
gd.invalidate(similarDevice);
205
}
206
gd.displayChanged();
207
} else {
208
// no more references to this device, remove it
209
it.remove();
210
}
211
}
212
}
213
214
private CGraphicsDevice getSimilarDevice(CGraphicsDevice old) {
215
for (CGraphicsDevice device : devices.values()) {
216
if (device.getBounds().equals(old.getBounds())) {
217
// for now we will use the bounds only
218
return device;
219
}
220
}
221
return null;
222
}
223
224
@Override
225
public synchronized GraphicsDevice getDefaultScreenDevice() throws HeadlessException {
226
return devices.get(mainDisplayID);
227
}
228
229
@Override
230
public synchronized GraphicsDevice[] getScreenDevices() throws HeadlessException {
231
return devices.values().toArray(new CGraphicsDevice[devices.values().size()]);
232
}
233
234
public synchronized GraphicsDevice getScreenDevice(int displayID) {
235
return devices.get(displayID);
236
}
237
238
@Override
239
protected synchronized int getNumScreens() {
240
return devices.size();
241
}
242
243
@Override
244
protected GraphicsDevice makeScreenDevice(int screennum) {
245
throw new UnsupportedOperationException("This method is unused and should not be called in this implementation");
246
}
247
248
@Override
249
public boolean isDisplayLocal() {
250
return true;
251
}
252
253
static String[] sLogicalFonts = { "Serif", "SansSerif", "Monospaced", "Dialog", "DialogInput" };
254
255
@Override
256
public Font[] getAllFonts() {
257
258
Font[] newFonts;
259
Font[] superFonts = super.getAllFonts();
260
261
int numLogical = sLogicalFonts.length;
262
int numOtherFonts = superFonts.length;
263
264
newFonts = new Font[numOtherFonts + numLogical];
265
System.arraycopy(superFonts,0,newFonts,numLogical,numOtherFonts);
266
267
for (int i = 0; i < numLogical; i++)
268
{
269
newFonts[i] = new Font(sLogicalFonts[i], Font.PLAIN, 1);
270
}
271
return newFonts;
272
}
273
274
}
275
276