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