Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/unix/classes/sun/awt/X11GraphicsConfig.java
41152 views
1
/*
2
* Copyright (c) 1997, 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.AWTException;
29
import java.awt.BufferCapabilities;
30
import java.awt.Component;
31
import java.awt.GraphicsConfiguration;
32
import java.awt.GraphicsDevice;
33
import java.awt.Image;
34
import java.awt.ImageCapabilities;
35
import java.awt.Rectangle;
36
import java.awt.Toolkit;
37
import java.awt.Transparency;
38
import java.awt.color.ColorSpace;
39
import java.awt.geom.AffineTransform;
40
import java.awt.image.ColorModel;
41
import java.awt.image.ComponentColorModel;
42
import java.awt.image.DataBuffer;
43
import java.awt.image.DirectColorModel;
44
import java.awt.image.VolatileImage;
45
import java.awt.image.WritableRaster;
46
47
import sun.awt.image.OffScreenImage;
48
import sun.awt.image.SunVolatileImage;
49
import sun.awt.image.SurfaceManager;
50
import sun.java2d.Disposer;
51
import sun.java2d.DisposerRecord;
52
import sun.java2d.SurfaceData;
53
import sun.java2d.loops.CompositeType;
54
import sun.java2d.loops.RenderLoops;
55
import sun.java2d.loops.SurfaceType;
56
import sun.java2d.pipe.Region;
57
import sun.java2d.x11.X11SurfaceData;
58
59
/**
60
* This is an implementation of a GraphicsConfiguration object for a
61
* single X11 visual.
62
*
63
* @see java.awt.GraphicsEnvironment
64
* @see GraphicsDevice
65
*/
66
public class X11GraphicsConfig extends GraphicsConfiguration
67
implements SurfaceManager.ProxiedGraphicsConfig
68
{
69
private final X11GraphicsDevice device;
70
protected int visual;
71
int depth;
72
int colormap;
73
ColorModel colorModel;
74
long aData;
75
boolean doubleBuffer;
76
private Object disposerReferent = new Object();
77
private BufferCapabilities bufferCaps;
78
private static ImageCapabilities imageCaps =
79
new ImageCapabilities(X11SurfaceData.isAccelerationEnabled());
80
81
// will be set on native level from init()
82
protected int bitsPerPixel;
83
84
protected SurfaceType surfaceType;
85
86
public RenderLoops solidloops;
87
88
public static X11GraphicsConfig getConfig(X11GraphicsDevice device,
89
int visualnum, int depth,
90
int colormap,
91
boolean doubleBuffer)
92
{
93
return new X11GraphicsConfig(device, visualnum, depth, colormap, doubleBuffer);
94
}
95
96
/*
97
* Note this method is currently here for backward compatibility
98
* as this was the method used in jdk 1.2 beta4 to create the
99
* X11GraphicsConfig objects. Java3D code had called this method
100
* explicitly so without this, if a user tries to use JDK1.2 fcs
101
* with Java3D beta1, a NoSuchMethod execption is thrown and
102
* the program exits. REMOVE this method after Java3D fcs is
103
* released!
104
*/
105
public static X11GraphicsConfig getConfig(X11GraphicsDevice device,
106
int visualnum, int depth,
107
int colormap, int type)
108
{
109
return new X11GraphicsConfig(device, visualnum, depth, colormap, false);
110
}
111
112
private native int getNumColors();
113
private native void init(int visualNum, int screen);
114
private native ColorModel makeColorModel();
115
116
protected X11GraphicsConfig(X11GraphicsDevice device,
117
int visualnum, int depth,
118
int colormap, boolean doubleBuffer)
119
{
120
this.device = device;
121
this.visual = visualnum;
122
this.doubleBuffer = doubleBuffer;
123
this.depth = depth;
124
this.colormap = colormap;
125
init (visualnum, device.getScreen());
126
127
// add a record to the Disposer so that we destroy the native
128
// AwtGraphicsConfigData when this object goes away (i.e. after a
129
// display change event)
130
long x11CfgData = getAData();
131
Disposer.addRecord(disposerReferent,
132
new X11GCDisposerRecord(x11CfgData));
133
}
134
135
/**
136
* Return the graphics device associated with this configuration.
137
*/
138
@Override
139
public X11GraphicsDevice getDevice() {
140
return device;
141
}
142
143
/**
144
* Returns the visual id associated with this configuration.
145
*/
146
public int getVisual () {
147
return visual;
148
}
149
150
151
/**
152
* Returns the depth associated with this configuration.
153
*/
154
public int getDepth () {
155
return depth;
156
}
157
158
/**
159
* Returns the colormap associated with this configuration.
160
*/
161
public int getColormap () {
162
return colormap;
163
}
164
165
/**
166
* Returns a number of bits allocated per pixel
167
* (might be different from depth)
168
*/
169
public int getBitsPerPixel() {
170
return bitsPerPixel;
171
}
172
173
public synchronized SurfaceType getSurfaceType() {
174
if (surfaceType != null) {
175
return surfaceType;
176
}
177
178
surfaceType = X11SurfaceData.getSurfaceType(this, Transparency.OPAQUE);
179
return surfaceType;
180
}
181
182
@Override
183
public Object getProxyKey() {
184
return device.getProxyKeyFor(getSurfaceType());
185
}
186
187
/**
188
* Return the RenderLoops this type of destination uses for
189
* solid fills and strokes.
190
*/
191
public synchronized RenderLoops getSolidLoops(SurfaceType stype) {
192
if (solidloops == null) {
193
solidloops = SurfaceData.makeRenderLoops(SurfaceType.OpaqueColor,
194
CompositeType.SrcNoEa,
195
stype);
196
}
197
return solidloops;
198
}
199
200
/**
201
* Returns the color model associated with this configuration.
202
*/
203
@Override
204
public synchronized ColorModel getColorModel() {
205
if (colorModel == null) {
206
// Force SystemColors to be resolved before we create the CM
207
java.awt.SystemColor.window.getRGB();
208
// This method, makeColorModel(), can return null if the
209
// toolkit is not initialized yet.
210
// The toolkit will then call back to this routine after it
211
// is initialized and makeColorModel() should return a non-null
212
// colorModel.
213
colorModel = makeColorModel();
214
if (colorModel == null)
215
colorModel = Toolkit.getDefaultToolkit ().getColorModel ();
216
}
217
218
return colorModel;
219
}
220
221
/**
222
* Returns the color model associated with this configuration that
223
* supports the specified transparency.
224
*/
225
@Override
226
public ColorModel getColorModel(int transparency) {
227
switch (transparency) {
228
case Transparency.OPAQUE:
229
return getColorModel();
230
case Transparency.BITMASK:
231
return new DirectColorModel(25, 0xff0000, 0xff00, 0xff, 0x1000000);
232
case Transparency.TRANSLUCENT:
233
return ColorModel.getRGBdefault();
234
default:
235
return null;
236
}
237
}
238
239
public static DirectColorModel createDCM32(int rMask, int gMask, int bMask,
240
int aMask, boolean aPre) {
241
return new DirectColorModel(
242
ColorSpace.getInstance(ColorSpace.CS_sRGB),
243
32, rMask, gMask, bMask, aMask, aPre, DataBuffer.TYPE_INT);
244
}
245
246
public static ComponentColorModel createABGRCCM() {
247
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
248
int[] nBits = {8, 8, 8, 8};
249
int[] bOffs = {3, 2, 1, 0};
250
return new ComponentColorModel(cs, nBits, true, true,
251
Transparency.TRANSLUCENT,
252
DataBuffer.TYPE_BYTE);
253
}
254
255
/**
256
* Returns the default Transform for this configuration. This
257
* Transform is typically the Identity transform for most normal
258
* screens. Device coordinates for screen and printer devices will
259
* have the origin in the upper left-hand corner of the target region of
260
* the device, with X coordinates
261
* increasing to the right and Y coordinates increasing downwards.
262
* For image buffers, this Transform will be the Identity transform.
263
*/
264
@Override
265
public AffineTransform getDefaultTransform() {
266
double scale = getScale();
267
return AffineTransform.getScaleInstance(scale, scale);
268
}
269
270
public int getScale() {
271
return getDevice().getScaleFactor();
272
}
273
274
public int scaleUp(int x) {
275
return Region.clipRound(x * (double)getScale());
276
}
277
278
public int scaleDown(int x) {
279
return Region.clipRound(x / (double)getScale());
280
}
281
282
/**
283
*
284
* Returns a Transform that can be composed with the default Transform
285
* of a Graphics2D so that 72 units in user space will equal 1 inch
286
* in device space.
287
* Given a Graphics2D, g, one can reset the transformation to create
288
* such a mapping by using the following pseudocode:
289
* <pre>
290
* GraphicsConfiguration gc = g.getGraphicsConfiguration();
291
*
292
* g.setTransform(gc.getDefaultTransform());
293
* g.transform(gc.getNormalizingTransform());
294
* </pre>
295
* Note that sometimes this Transform will be identity (e.g. for
296
* printers or metafile output) and that this Transform is only
297
* as accurate as the information supplied by the underlying system.
298
* For image buffers, this Transform will be the Identity transform,
299
* since there is no valid distance measurement.
300
*/
301
@Override
302
public AffineTransform getNormalizingTransform() {
303
double xscale = getXResolution(device.getScreen()) / 72.0;
304
double yscale = getYResolution(device.getScreen()) / 72.0;
305
return new AffineTransform(xscale, 0.0, 0.0, yscale, 0.0, 0.0);
306
}
307
308
private native double getXResolution(int screen);
309
private native double getYResolution(int screen);
310
311
public long getAData() {
312
return aData;
313
}
314
315
public String toString() {
316
return ("X11GraphicsConfig[dev="+device+
317
",vis=0x"+Integer.toHexString(visual)+
318
"]");
319
}
320
321
/*
322
* Initialize JNI field and method IDs for fields that may be
323
* accessed from C.
324
*/
325
private static native void initIDs();
326
327
static {
328
initIDs ();
329
}
330
331
@Override
332
public final Rectangle getBounds() {
333
return device.getBounds();
334
}
335
336
private static class XDBECapabilities extends BufferCapabilities {
337
public XDBECapabilities() {
338
super(imageCaps, imageCaps, FlipContents.UNDEFINED);
339
}
340
}
341
342
@Override
343
public BufferCapabilities getBufferCapabilities() {
344
if (bufferCaps == null) {
345
if (doubleBuffer) {
346
bufferCaps = new XDBECapabilities();
347
} else {
348
bufferCaps = super.getBufferCapabilities();
349
}
350
}
351
return bufferCaps;
352
}
353
354
@Override
355
public ImageCapabilities getImageCapabilities() {
356
return imageCaps;
357
}
358
359
public boolean isDoubleBuffered() {
360
return doubleBuffer;
361
}
362
363
private static native void dispose(long x11ConfigData);
364
365
private static class X11GCDisposerRecord implements DisposerRecord {
366
private long x11ConfigData;
367
public X11GCDisposerRecord(long x11CfgData) {
368
this.x11ConfigData = x11CfgData;
369
}
370
@Override
371
public synchronized void dispose() {
372
if (x11ConfigData != 0L) {
373
X11GraphicsConfig.dispose(x11ConfigData);
374
x11ConfigData = 0L;
375
}
376
}
377
}
378
379
/**
380
* The following methods are invoked from {M,X}Toolkit.java and
381
* X11ComponentPeer.java rather than having the X11-dependent
382
* implementations hardcoded in those classes. This way the appropriate
383
* actions are taken based on the peer's GraphicsConfig, whether it is
384
* an X11GraphicsConfig or a GLXGraphicsConfig.
385
*/
386
387
/**
388
* Creates a new SurfaceData that will be associated with the given
389
* X11ComponentPeer.
390
*/
391
public SurfaceData createSurfaceData(X11ComponentPeer peer) {
392
return X11SurfaceData.createData(peer);
393
}
394
395
/**
396
* Creates a new hidden-acceleration image of the given width and height
397
* that is associated with the target Component.
398
*/
399
public Image createAcceleratedImage(Component target,
400
int width, int height)
401
{
402
// As of 1.7 we no longer create pmoffscreens here...
403
ColorModel model = getColorModel(Transparency.OPAQUE);
404
WritableRaster wr =
405
model.createCompatibleWritableRaster(width, height);
406
return new OffScreenImage(target, model, wr,
407
model.isAlphaPremultiplied());
408
}
409
410
/**
411
* The following methods correspond to the multibuffering methods in
412
* X11ComponentPeer.java...
413
*/
414
415
private native long createBackBuffer(long window, int swapAction);
416
private native void swapBuffers(long window, int swapAction);
417
418
/**
419
* Attempts to create an XDBE-based backbuffer for the given peer. If
420
* the requested configuration is not natively supported, an AWTException
421
* is thrown. Otherwise, if the backbuffer creation is successful, a
422
* handle to the native backbuffer is returned.
423
*/
424
public long createBackBuffer(X11ComponentPeer peer,
425
int numBuffers, BufferCapabilities caps)
426
throws AWTException
427
{
428
if (!X11GraphicsDevice.isDBESupported()) {
429
throw new AWTException("Page flipping is not supported");
430
}
431
if (numBuffers > 2) {
432
throw new AWTException(
433
"Only double or single buffering is supported");
434
}
435
BufferCapabilities configCaps = getBufferCapabilities();
436
if (!configCaps.isPageFlipping()) {
437
throw new AWTException("Page flipping is not supported");
438
}
439
440
long window = peer.getContentWindow();
441
int swapAction = getSwapAction(caps.getFlipContents());
442
443
return createBackBuffer(window, swapAction);
444
}
445
446
/**
447
* Destroys the backbuffer object represented by the given handle value.
448
*/
449
public native void destroyBackBuffer(long backBuffer);
450
451
/**
452
* Creates a VolatileImage that essentially wraps the target Component's
453
* backbuffer, using the provided backbuffer handle.
454
*/
455
public VolatileImage createBackBufferImage(Component target,
456
long backBuffer)
457
{
458
// it is possible for the component to have size 0x0, adjust it to
459
// be at least 1x1 to avoid IAE
460
int w = Math.max(1, target.getWidth());
461
int h = Math.max(1, target.getHeight());
462
return new SunVolatileImage(target,
463
w, h,
464
Long.valueOf(backBuffer));
465
}
466
467
/**
468
* Performs the native XDBE flip operation for the given target Component.
469
*/
470
public void flip(X11ComponentPeer peer,
471
Component target, VolatileImage xBackBuffer,
472
int x1, int y1, int x2, int y2,
473
BufferCapabilities.FlipContents flipAction)
474
{
475
long window = peer.getContentWindow();
476
int swapAction = getSwapAction(flipAction);
477
swapBuffers(window, swapAction);
478
}
479
480
/**
481
* Maps the given FlipContents constant to the associated XDBE swap
482
* action constant.
483
*/
484
private static int getSwapAction(
485
BufferCapabilities.FlipContents flipAction) {
486
if (flipAction == BufferCapabilities.FlipContents.BACKGROUND) {
487
return 0x01;
488
} else if (flipAction == BufferCapabilities.FlipContents.PRIOR) {
489
return 0x02;
490
} else if (flipAction == BufferCapabilities.FlipContents.COPIED) {
491
return 0x03;
492
} else {
493
return 0x00; // UNDEFINED
494
}
495
}
496
497
@Override
498
public boolean isTranslucencyCapable() {
499
return isTranslucencyCapable(getAData());
500
}
501
502
private native boolean isTranslucencyCapable(long x11ConfigData);
503
}
504
505