Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/macosx/classes/sun/java2d/opengl/CGLGraphicsConfig.java
41159 views
1
/*
2
* Copyright (c) 2011, 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.java2d.opengl;
27
28
import java.awt.AWTException;
29
import java.awt.BufferCapabilities;
30
import java.awt.Component;
31
import java.awt.Graphics;
32
import java.awt.Graphics2D;
33
import java.awt.Image;
34
import java.awt.ImageCapabilities;
35
import java.awt.Rectangle;
36
import java.awt.Transparency;
37
import java.awt.color.ColorSpace;
38
import java.awt.image.BufferedImage;
39
import java.awt.image.ColorModel;
40
import java.awt.image.DataBuffer;
41
import java.awt.image.DirectColorModel;
42
import java.awt.image.VolatileImage;
43
import java.awt.image.WritableRaster;
44
45
import sun.awt.CGraphicsConfig;
46
import sun.awt.CGraphicsDevice;
47
import sun.awt.image.OffScreenImage;
48
import sun.awt.image.SunVolatileImage;
49
import sun.java2d.Disposer;
50
import sun.java2d.DisposerRecord;
51
import sun.java2d.Surface;
52
import sun.java2d.SurfaceData;
53
import sun.java2d.opengl.OGLContext.OGLContextCaps;
54
import sun.java2d.pipe.hw.AccelSurface;
55
import sun.java2d.pipe.hw.AccelTypedVolatileImage;
56
import sun.java2d.pipe.hw.ContextCapabilities;
57
import sun.lwawt.LWComponentPeer;
58
import sun.lwawt.macosx.CFRetainedResource;
59
60
import static sun.java2d.opengl.OGLContext.OGLContextCaps.CAPS_DOUBLEBUFFERED;
61
import static sun.java2d.opengl.OGLContext.OGLContextCaps.CAPS_EXT_FBOBJECT;
62
import static sun.java2d.opengl.OGLSurfaceData.FBOBJECT;
63
import static sun.java2d.opengl.OGLSurfaceData.TEXTURE;
64
65
public final class CGLGraphicsConfig extends CGraphicsConfig
66
implements OGLGraphicsConfig
67
{
68
private static boolean cglAvailable;
69
private static ImageCapabilities imageCaps = new CGLImageCaps();
70
71
private BufferCapabilities bufferCaps;
72
private long pConfigInfo;
73
private ContextCapabilities oglCaps;
74
private final OGLContext context;
75
private final Object disposerReferent = new Object();
76
private final int maxTextureSize;
77
78
private static native boolean initCGL();
79
private static native long getCGLConfigInfo();
80
private static native int getOGLCapabilities(long configInfo);
81
82
/**
83
* Returns GL_MAX_TEXTURE_SIZE from the shared opengl context. Must be
84
* called under OGLRQ lock, because this method change current context.
85
*
86
* @return GL_MAX_TEXTURE_SIZE
87
*/
88
private static native int nativeGetMaxTextureSize();
89
90
static {
91
cglAvailable = initCGL();
92
}
93
94
private CGLGraphicsConfig(CGraphicsDevice device, long configInfo,
95
int maxTextureSize, ContextCapabilities oglCaps) {
96
super(device);
97
this.pConfigInfo = configInfo;
98
this.oglCaps = oglCaps;
99
this.maxTextureSize = maxTextureSize;
100
context = new OGLContext(OGLRenderQueue.getInstance());
101
102
// add a record to the Disposer so that we destroy the native
103
// CGLGraphicsConfigInfo data when this object goes away
104
Disposer.addRecord(disposerReferent,
105
new CGLGCDisposerRecord(pConfigInfo));
106
}
107
108
@Override
109
public Object getProxyKey() {
110
return this;
111
}
112
113
@Override
114
public SurfaceData createManagedSurface(int w, int h, int transparency) {
115
return CGLSurfaceData.createData(this, w, h,
116
getColorModel(transparency),
117
null,
118
OGLSurfaceData.TEXTURE);
119
}
120
121
public static CGLGraphicsConfig getConfig(CGraphicsDevice device)
122
{
123
if (!cglAvailable) {
124
return null;
125
}
126
127
long cfginfo = 0;
128
int textureSize = 0;
129
final String[] ids = new String[1];
130
OGLRenderQueue rq = OGLRenderQueue.getInstance();
131
rq.lock();
132
try {
133
// getCGLConfigInfo() creates and destroys temporary
134
// surfaces/contexts, so we should first invalidate the current
135
// Java-level context and flush the queue...
136
OGLContext.invalidateCurrentContext();
137
cfginfo = getCGLConfigInfo();
138
if (cfginfo != 0L) {
139
textureSize = nativeGetMaxTextureSize();
140
// 7160609: GL still fails to create a square texture of this
141
// size. Half should be safe enough.
142
// Explicitly not support a texture more than 2^14, see 8010999.
143
textureSize = textureSize <= 16384 ? textureSize / 2 : 8192;
144
OGLContext.setScratchSurface(cfginfo);
145
rq.flushAndInvokeNow(() -> {
146
ids[0] = OGLContext.getOGLIdString();
147
});
148
}
149
} finally {
150
rq.unlock();
151
}
152
if (cfginfo == 0) {
153
return null;
154
}
155
156
int oglCaps = getOGLCapabilities(cfginfo);
157
ContextCapabilities caps = new OGLContextCaps(oglCaps, ids[0]);
158
return new CGLGraphicsConfig(device, cfginfo, textureSize, caps);
159
}
160
161
public static boolean isCGLAvailable() {
162
return cglAvailable;
163
}
164
165
/**
166
* Returns true if the provided capability bit is present for this config.
167
* See OGLContext.java for a list of supported capabilities.
168
*/
169
@Override
170
public boolean isCapPresent(int cap) {
171
return ((oglCaps.getCaps() & cap) != 0);
172
}
173
174
@Override
175
public long getNativeConfigInfo() {
176
return pConfigInfo;
177
}
178
179
@Override
180
public OGLContext getContext() {
181
return context;
182
}
183
184
@Override
185
public BufferedImage createCompatibleImage(int width, int height) {
186
ColorModel model = new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
187
WritableRaster
188
raster = model.createCompatibleWritableRaster(width, height);
189
return new BufferedImage(model, raster, model.isAlphaPremultiplied(),
190
null);
191
}
192
193
@Override
194
public ColorModel getColorModel(int transparency) {
195
switch (transparency) {
196
case Transparency.OPAQUE:
197
// REMIND: once the ColorModel spec is changed, this should be
198
// an opaque premultiplied DCM...
199
return new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
200
case Transparency.BITMASK:
201
return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);
202
case Transparency.TRANSLUCENT:
203
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
204
return new DirectColorModel(cs, 32,
205
0xff0000, 0xff00, 0xff, 0xff000000,
206
true, DataBuffer.TYPE_INT);
207
default:
208
return null;
209
}
210
}
211
212
public boolean isDoubleBuffered() {
213
return isCapPresent(CAPS_DOUBLEBUFFERED);
214
}
215
216
private static class CGLGCDisposerRecord implements DisposerRecord {
217
private long pCfgInfo;
218
public CGLGCDisposerRecord(long pCfgInfo) {
219
this.pCfgInfo = pCfgInfo;
220
}
221
public void dispose() {
222
if (pCfgInfo != 0) {
223
OGLRenderQueue.disposeGraphicsConfig(pCfgInfo);
224
pCfgInfo = 0;
225
}
226
}
227
}
228
229
// TODO: CGraphicsConfig doesn't implement displayChanged() yet
230
//@Override
231
public synchronized void displayChanged() {
232
//super.displayChanged();
233
234
// the context could hold a reference to a CGLSurfaceData, which in
235
// turn has a reference back to this CGLGraphicsConfig, so in order
236
// for this instance to be disposed we need to break the connection
237
OGLRenderQueue rq = OGLRenderQueue.getInstance();
238
rq.lock();
239
try {
240
OGLContext.invalidateCurrentContext();
241
} finally {
242
rq.unlock();
243
}
244
}
245
246
@Override
247
public String toString() {
248
return ("CGLGraphicsConfig[" + getDevice().getIDstring() + "]");
249
}
250
251
@Override
252
public SurfaceData createSurfaceData(CFRetainedResource layer) {
253
return CGLSurfaceData.createData((CGLLayer) layer);
254
}
255
256
@Override
257
public Image createAcceleratedImage(Component target,
258
int width, int height)
259
{
260
ColorModel model = getColorModel(Transparency.OPAQUE);
261
WritableRaster wr = model.createCompatibleWritableRaster(width, height);
262
return new OffScreenImage(target, model, wr,
263
model.isAlphaPremultiplied());
264
}
265
266
@Override
267
public void assertOperationSupported(final int numBuffers,
268
final BufferCapabilities caps)
269
throws AWTException {
270
// Assume this method is never called with numBuffers != 2, as 0 is
271
// unsupported, and 1 corresponds to a SingleBufferStrategy which
272
// doesn't depend on the peer. Screen is considered as a separate
273
// "buffer".
274
if (numBuffers != 2) {
275
throw new AWTException("Only double buffering is supported");
276
}
277
final BufferCapabilities configCaps = getBufferCapabilities();
278
if (!configCaps.isPageFlipping()) {
279
throw new AWTException("Page flipping is not supported");
280
}
281
if (caps.getFlipContents() == BufferCapabilities.FlipContents.PRIOR) {
282
throw new AWTException("FlipContents.PRIOR is not supported");
283
}
284
}
285
286
@Override
287
public Image createBackBuffer(final LWComponentPeer<?, ?> peer) {
288
final Rectangle r = peer.getBounds();
289
// It is possible for the component to have size 0x0, adjust it to
290
// be at least 1x1 to avoid IAE
291
final int w = Math.max(1, r.width);
292
final int h = Math.max(1, r.height);
293
final int transparency = peer.isTranslucent() ? Transparency.TRANSLUCENT
294
: Transparency.OPAQUE;
295
return new SunVolatileImage(this, w, h, transparency, null);
296
}
297
298
@Override
299
public void destroyBackBuffer(final Image backBuffer) {
300
if (backBuffer != null) {
301
backBuffer.flush();
302
}
303
}
304
305
@Override
306
public void flip(final LWComponentPeer<?, ?> peer, final Image backBuffer,
307
final int x1, final int y1, final int x2, final int y2,
308
final BufferCapabilities.FlipContents flipAction) {
309
final Graphics g = peer.getGraphics();
310
try {
311
g.drawImage(backBuffer, x1, y1, x2, y2, x1, y1, x2, y2, null);
312
} finally {
313
g.dispose();
314
}
315
if (flipAction == BufferCapabilities.FlipContents.BACKGROUND) {
316
final Graphics2D bg = (Graphics2D) backBuffer.getGraphics();
317
try {
318
bg.setBackground(peer.getBackground());
319
bg.clearRect(0, 0, backBuffer.getWidth(null),
320
backBuffer.getHeight(null));
321
} finally {
322
bg.dispose();
323
}
324
}
325
}
326
327
private static class CGLBufferCaps extends BufferCapabilities {
328
public CGLBufferCaps(boolean dblBuf) {
329
super(imageCaps, imageCaps,
330
dblBuf ? FlipContents.UNDEFINED : null);
331
}
332
}
333
334
@Override
335
public BufferCapabilities getBufferCapabilities() {
336
if (bufferCaps == null) {
337
bufferCaps = new CGLBufferCaps(isDoubleBuffered());
338
}
339
return bufferCaps;
340
}
341
342
private static class CGLImageCaps extends ImageCapabilities {
343
private CGLImageCaps() {
344
super(true);
345
}
346
public boolean isTrueVolatile() {
347
return true;
348
}
349
}
350
351
@Override
352
public ImageCapabilities getImageCapabilities() {
353
return imageCaps;
354
}
355
356
@Override
357
public VolatileImage createCompatibleVolatileImage(int width, int height,
358
int transparency,
359
int type) {
360
if ((type != FBOBJECT && type != TEXTURE)
361
|| transparency == Transparency.BITMASK
362
|| type == FBOBJECT && !isCapPresent(CAPS_EXT_FBOBJECT)) {
363
return null;
364
}
365
SunVolatileImage vi = new AccelTypedVolatileImage(this, width, height,
366
transparency, type);
367
Surface sd = vi.getDestSurface();
368
if (!(sd instanceof AccelSurface) ||
369
((AccelSurface)sd).getType() != type)
370
{
371
vi.flush();
372
vi = null;
373
}
374
375
return vi;
376
}
377
378
@Override
379
public ContextCapabilities getContextCapabilities() {
380
return oglCaps;
381
}
382
383
@Override
384
public int getMaxTextureWidth() {
385
return Math.max(maxTextureSize / getDevice().getScaleFactor(),
386
getBounds().width);
387
}
388
389
@Override
390
public int getMaxTextureHeight() {
391
return Math.max(maxTextureSize / getDevice().getScaleFactor(),
392
getBounds().height);
393
}
394
}
395
396