Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/unix/classes/sun/java2d/xr/XRPMBlitLoops.java
41159 views
1
/*
2
* Copyright (c) 2010, 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.java2d.xr;
27
28
import sun.awt.SunToolkit;
29
import sun.awt.image.*;
30
import sun.java2d.loops.*;
31
import sun.java2d.pipe.*;
32
import sun.java2d.*;
33
import java.awt.*;
34
import java.awt.geom.*;
35
import java.lang.ref.*;
36
37
public final class XRPMBlitLoops {
38
39
static WeakReference<SunVolatileImage> argbTmpPM = new WeakReference<SunVolatileImage>(null);
40
static WeakReference<SunVolatileImage> rgbTmpPM = new WeakReference<SunVolatileImage>(null);
41
42
private XRPMBlitLoops() {
43
}
44
45
public static void register() {
46
GraphicsPrimitive[] primitives = { new XRPMBlit(XRSurfaceData.IntRgbX11, XRSurfaceData.IntRgbX11),
47
new XRPMBlit(XRSurfaceData.IntRgbX11, XRSurfaceData.IntArgbPreX11),
48
new XRPMBlit(XRSurfaceData.IntArgbPreX11, XRSurfaceData.IntRgbX11),
49
new XRPMBlit(XRSurfaceData.IntArgbPreX11, XRSurfaceData.IntArgbPreX11),
50
51
new XRPMScaledBlit(XRSurfaceData.IntRgbX11, XRSurfaceData.IntRgbX11),
52
new XRPMScaledBlit(XRSurfaceData.IntRgbX11, XRSurfaceData.IntArgbPreX11),
53
new XRPMScaledBlit(XRSurfaceData.IntArgbPreX11, XRSurfaceData.IntRgbX11),
54
new XRPMScaledBlit(XRSurfaceData.IntArgbPreX11, XRSurfaceData.IntArgbPreX11),
55
56
new XRPMTransformedBlit(XRSurfaceData.IntRgbX11, XRSurfaceData.IntRgbX11),
57
new XRPMTransformedBlit(XRSurfaceData.IntRgbX11, XRSurfaceData.IntArgbPreX11),
58
new XRPMTransformedBlit(XRSurfaceData.IntArgbPreX11, XRSurfaceData.IntRgbX11),
59
new XRPMTransformedBlit(XRSurfaceData.IntArgbPreX11, XRSurfaceData.IntArgbPreX11),
60
61
/* SW -> Surface Blits */
62
new XrSwToPMBlit(SurfaceType.IntArgb, XRSurfaceData.IntRgbX11),
63
new XrSwToPMBlit(SurfaceType.IntRgb, XRSurfaceData.IntRgbX11),
64
new XrSwToPMBlit(SurfaceType.IntBgr, XRSurfaceData.IntRgbX11),
65
new XrSwToPMBlit(SurfaceType.ThreeByteBgr, XRSurfaceData.IntRgbX11),
66
new XrSwToPMBlit(SurfaceType.Ushort565Rgb, XRSurfaceData.IntRgbX11),
67
new XrSwToPMBlit(SurfaceType.Ushort555Rgb, XRSurfaceData.IntRgbX11),
68
new XrSwToPMBlit(SurfaceType.ByteIndexed, XRSurfaceData.IntRgbX11),
69
70
new XrSwToPMBlit(SurfaceType.IntArgb, XRSurfaceData.IntArgbPreX11),
71
new XrSwToPMBlit(SurfaceType.IntRgb, XRSurfaceData.IntArgbPreX11),
72
new XrSwToPMBlit(SurfaceType.IntBgr, XRSurfaceData.IntArgbPreX11),
73
new XrSwToPMBlit(SurfaceType.ThreeByteBgr, XRSurfaceData.IntArgbPreX11),
74
new XrSwToPMBlit(SurfaceType.Ushort565Rgb, XRSurfaceData.IntArgbPreX11),
75
new XrSwToPMBlit(SurfaceType.Ushort555Rgb, XRSurfaceData.IntArgbPreX11),
76
new XrSwToPMBlit(SurfaceType.ByteIndexed, XRSurfaceData.IntArgbPreX11),
77
78
/* SW->Surface Scales */
79
new XrSwToPMScaledBlit(SurfaceType.IntArgb, XRSurfaceData.IntRgbX11),
80
new XrSwToPMScaledBlit(SurfaceType.IntRgb, XRSurfaceData.IntRgbX11),
81
new XrSwToPMScaledBlit(SurfaceType.IntBgr, XRSurfaceData.IntRgbX11),
82
new XrSwToPMScaledBlit(SurfaceType.ThreeByteBgr, XRSurfaceData.IntRgbX11),
83
new XrSwToPMScaledBlit(SurfaceType.Ushort565Rgb, XRSurfaceData.IntRgbX11),
84
new XrSwToPMScaledBlit(SurfaceType.Ushort555Rgb, XRSurfaceData.IntRgbX11),
85
new XrSwToPMScaledBlit(SurfaceType.ByteIndexed, XRSurfaceData.IntRgbX11),
86
87
new XrSwToPMScaledBlit(SurfaceType.IntArgb, XRSurfaceData.IntArgbPreX11),
88
new XrSwToPMScaledBlit(SurfaceType.IntRgb, XRSurfaceData.IntArgbPreX11),
89
new XrSwToPMScaledBlit(SurfaceType.IntBgr, XRSurfaceData.IntArgbPreX11),
90
new XrSwToPMScaledBlit(SurfaceType.ThreeByteBgr, XRSurfaceData.IntArgbPreX11),
91
new XrSwToPMScaledBlit(SurfaceType.Ushort565Rgb, XRSurfaceData.IntArgbPreX11),
92
new XrSwToPMScaledBlit(SurfaceType.Ushort555Rgb, XRSurfaceData.IntArgbPreX11),
93
new XrSwToPMScaledBlit(SurfaceType.ByteIndexed, XRSurfaceData.IntArgbPreX11),
94
95
/* SW->Surface Transforms */
96
new XrSwToPMTransformedBlit(SurfaceType.IntArgb, XRSurfaceData.IntRgbX11),
97
new XrSwToPMTransformedBlit(SurfaceType.IntRgb, XRSurfaceData.IntRgbX11),
98
new XrSwToPMTransformedBlit(SurfaceType.IntBgr, XRSurfaceData.IntRgbX11),
99
new XrSwToPMTransformedBlit(SurfaceType.ThreeByteBgr, XRSurfaceData.IntRgbX11),
100
new XrSwToPMTransformedBlit(SurfaceType.Ushort565Rgb, XRSurfaceData.IntRgbX11),
101
new XrSwToPMTransformedBlit(SurfaceType.Ushort555Rgb, XRSurfaceData.IntRgbX11),
102
new XrSwToPMTransformedBlit(SurfaceType.ByteIndexed, XRSurfaceData.IntRgbX11),
103
104
new XrSwToPMTransformedBlit(SurfaceType.IntArgb, XRSurfaceData.IntArgbPreX11),
105
new XrSwToPMTransformedBlit(SurfaceType.IntRgb, XRSurfaceData.IntArgbPreX11),
106
new XrSwToPMTransformedBlit(SurfaceType.IntBgr, XRSurfaceData.IntArgbPreX11),
107
new XrSwToPMTransformedBlit(SurfaceType.ThreeByteBgr, XRSurfaceData.IntArgbPreX11),
108
new XrSwToPMTransformedBlit(SurfaceType.Ushort565Rgb, XRSurfaceData.IntArgbPreX11),
109
new XrSwToPMTransformedBlit(SurfaceType.Ushort555Rgb, XRSurfaceData.IntArgbPreX11),
110
new XrSwToPMTransformedBlit(SurfaceType.ByteIndexed, XRSurfaceData.IntArgbPreX11), };
111
GraphicsPrimitiveMgr.register(primitives);
112
}
113
114
/**
115
* Caches a SW surface using a temporary pixmap. The pixmap is held by a WeakReference,
116
* allowing it to shrink again after some time.
117
*/
118
protected static XRSurfaceData cacheToTmpSurface(SurfaceData src, XRSurfaceData dst, int w, int h, int sx, int sy) {
119
SunVolatileImage vImg;
120
SurfaceType vImgSurfaceType;
121
122
if (src.getTransparency() == Transparency.OPAQUE) {
123
vImg = rgbTmpPM.get();
124
vImgSurfaceType = SurfaceType.IntRgb;
125
} else {
126
vImg = argbTmpPM.get();
127
vImgSurfaceType = SurfaceType.IntArgbPre;
128
}
129
130
if (vImg == null || vImg.getWidth() < w || vImg.getHeight() < h ||
131
// Sometimes we get volatile image of wrong dest surface type,
132
// so recreating it
133
!(vImg.getDestSurface() instanceof XRSurfaceData))
134
{
135
if (vImg != null) {
136
vImg.flush();
137
}
138
vImg = (SunVolatileImage) dst.getGraphicsConfig().createCompatibleVolatileImage(w, h, src.getTransparency());
139
vImg.setAccelerationPriority(1.0f);
140
141
if (!(vImg.getDestSurface() instanceof XRSurfaceData)) {
142
throw new InvalidPipeException("Could not create XRSurfaceData");
143
}
144
if (src.getTransparency() == SurfaceData.OPAQUE) {
145
rgbTmpPM = new WeakReference<SunVolatileImage>(vImg);
146
} else {
147
argbTmpPM = new WeakReference<SunVolatileImage>(vImg);
148
}
149
}
150
151
Blit swToSurfaceBlit = Blit.getFromCache(src.getSurfaceType(), CompositeType.SrcNoEa, vImgSurfaceType);
152
153
if (!(vImg.getDestSurface() instanceof XRSurfaceData)) {
154
throw new InvalidPipeException("wrong surface data type: " + vImg.getDestSurface());
155
}
156
157
XRSurfaceData vImgSurface = (XRSurfaceData) vImg.getDestSurface();
158
swToSurfaceBlit.Blit(src, vImgSurface, AlphaComposite.Src, null,
159
sx, sy, 0, 0, w, h);
160
161
return vImgSurface;
162
}
163
}
164
165
class XRPMBlit extends Blit {
166
public XRPMBlit(SurfaceType srcType, SurfaceType dstType) {
167
super(srcType, CompositeType.AnyAlpha, dstType);
168
}
169
170
public void Blit(SurfaceData src, SurfaceData dst, Composite comp, Region clip, int sx, int sy, int dx, int dy, int w, int h) {
171
try {
172
SunToolkit.awtLock();
173
174
XRSurfaceData x11sdDst = (XRSurfaceData) dst;
175
x11sdDst.validateAsDestination(null, clip);
176
XRSurfaceData x11sdSrc = (XRSurfaceData) src;
177
x11sdSrc.validateAsSource(null, XRUtils.RepeatNone, XRUtils.FAST);
178
179
x11sdDst.maskBuffer.validateCompositeState(comp, null, null, null);
180
181
x11sdDst.maskBuffer.compositeBlit(x11sdSrc, x11sdDst, sx, sy, dx, dy, w, h);
182
} finally {
183
SunToolkit.awtUnlock();
184
}
185
}
186
}
187
188
class XRPMScaledBlit extends ScaledBlit {
189
public XRPMScaledBlit(SurfaceType srcType, SurfaceType dstType) {
190
super(srcType, CompositeType.AnyAlpha, dstType);
191
}
192
193
@SuppressWarnings("cast")
194
public void Scale(SurfaceData src, SurfaceData dst, Composite comp, Region clip, int sx1, int sy1, int sx2, int sy2, double dx1, double dy1,
195
double dx2, double dy2) {
196
try {
197
SunToolkit.awtLock();
198
199
XRSurfaceData x11sdDst = (XRSurfaceData) dst;
200
x11sdDst.validateAsDestination(null, clip);
201
XRSurfaceData x11sdSrc = (XRSurfaceData) src;
202
x11sdDst.maskBuffer.validateCompositeState(comp, null, null, null);
203
204
double xScale = (dx2 - dx1) / (sx2 - sx1);
205
double yScale = (dy2 - dy1) / (sy2 - sy1);
206
207
sx1 *= xScale;
208
sx2 *= xScale;
209
sy1 *= yScale;
210
sy2 *= yScale;
211
212
dx1 = Math.ceil(dx1 - 0.5);
213
dy1 = Math.ceil(dy1 - 0.5);
214
dx2 = Math.ceil(dx2 - 0.5);
215
dy2 = Math.ceil(dy2 - 0.5);
216
217
AffineTransform xForm = AffineTransform.getScaleInstance(1 / xScale, 1 / yScale);
218
219
x11sdSrc.validateAsSource(xForm, XRUtils.RepeatNone, XRUtils.FAST);
220
x11sdDst.maskBuffer.compositeBlit(x11sdSrc, x11sdDst, (int) sx1, (int) sy1, (int) dx1, (int) dy1, (int) (dx2 - dx1), (int) (dy2 - dy1));
221
} finally {
222
SunToolkit.awtUnlock();
223
}
224
}
225
}
226
227
/**
228
* Called also if scale+transform is set
229
*
230
* @author Clemens Eisserer
231
*/
232
class XRPMTransformedBlit extends TransformBlit {
233
final Rectangle compositeBounds = new Rectangle();
234
final double[] srcCoords = new double[8];
235
final double[] dstCoords = new double[8];
236
237
public XRPMTransformedBlit(SurfaceType srcType, SurfaceType dstType) {
238
super(srcType, CompositeType.AnyAlpha, dstType);
239
}
240
241
/*
242
* Calculates the composition-rectangle required for transformed blits.
243
* For composite operations where the composition-rectangle defines
244
* the modified destination area, coordinates are rounded.
245
* Otherwise the composition window rectangle is sized large enough
246
* to not clip away any pixels.
247
*/
248
protected void adjustCompositeBounds(boolean isQuadrantRotated, AffineTransform tr,
249
int dstx, int dsty, int width, int height) {
250
srcCoords[0] = dstx;
251
srcCoords[1] = dsty;
252
srcCoords[2] = dstx + width;
253
srcCoords[3] = dsty + height;
254
255
double minX, minY, maxX, maxY;
256
if (isQuadrantRotated) {
257
tr.transform(srcCoords, 0, dstCoords, 0, 2);
258
259
minX = Math.min(dstCoords[0], dstCoords[2]);
260
minY = Math.min(dstCoords[1], dstCoords[3]);
261
maxX = Math.max(dstCoords[0], dstCoords[2]);
262
maxY = Math.max(dstCoords[1], dstCoords[3]);
263
264
minX = Math.ceil(minX - 0.5);
265
minY = Math.ceil(minY - 0.5);
266
maxX = Math.ceil(maxX - 0.5);
267
maxY = Math.ceil(maxY - 0.5);
268
} else {
269
srcCoords[4] = dstx;
270
srcCoords[5] = dsty + height;
271
srcCoords[6] = dstx + width;
272
srcCoords[7] = dsty;
273
274
tr.transform(srcCoords, 0, dstCoords, 0, 4);
275
276
minX = Math.min(dstCoords[0], Math.min(dstCoords[2], Math.min(dstCoords[4], dstCoords[6])));
277
minY = Math.min(dstCoords[1], Math.min(dstCoords[3], Math.min(dstCoords[5], dstCoords[7])));
278
maxX = Math.max(dstCoords[0], Math.max(dstCoords[2], Math.max(dstCoords[4], dstCoords[6])));
279
maxY = Math.max(dstCoords[1], Math.max(dstCoords[3], Math.max(dstCoords[5], dstCoords[7])));
280
281
minX = Math.floor(minX);
282
minY = Math.floor(minY);
283
maxX = Math.ceil(maxX);
284
maxY = Math.ceil(maxY);
285
}
286
287
compositeBounds.x = (int) minX;
288
compositeBounds.y = (int) minY;
289
compositeBounds.width = (int) (maxX - minX);
290
compositeBounds.height = (int) (maxY - minY);
291
}
292
293
public void Transform(SurfaceData src, SurfaceData dst, Composite comp, Region clip, AffineTransform xform,
294
int hint, int srcx, int srcy, int dstx, int dsty, int width, int height) {
295
try {
296
SunToolkit.awtLock();
297
298
XRSurfaceData x11sdDst = (XRSurfaceData) dst;
299
XRSurfaceData x11sdSrc = (XRSurfaceData) src;
300
XRCompositeManager xrMgr = XRCompositeManager.getInstance(x11sdSrc);
301
302
float extraAlpha = ((AlphaComposite) comp).getAlpha();
303
int filter = XRUtils.ATransOpToXRQuality(hint);
304
boolean isQuadrantRotated = XRUtils.isTransformQuadrantRotated(xform);
305
306
adjustCompositeBounds(isQuadrantRotated, xform, dstx, dsty, width, height);
307
308
x11sdDst.validateAsDestination(null, clip);
309
x11sdDst.maskBuffer.validateCompositeState(comp, null, null, null);
310
311
AffineTransform trx = AffineTransform.getTranslateInstance(-compositeBounds.x, -compositeBounds.y);
312
trx.concatenate(xform);
313
AffineTransform maskTX = (AffineTransform) trx.clone();
314
trx.translate(-srcx, -srcy);
315
316
try {
317
trx.invert();
318
} catch (NoninvertibleTransformException ex) {
319
trx.setToIdentity();
320
}
321
322
if (filter != XRUtils.FAST && (!isQuadrantRotated || extraAlpha != 1.0f)) {
323
XRMaskImage mask = x11sdSrc.maskBuffer.getMaskImage();
324
325
// For quadrant-transformed blits geometry is not stored inside the mask
326
// therefore we can use a repeating 1x1 mask for applying extra alpha.
327
int maskPicture = isQuadrantRotated ? xrMgr.getExtraAlphaMask()
328
: mask.prepareBlitMask(x11sdDst, maskTX, width, height);
329
330
x11sdSrc.validateAsSource(trx, XRUtils.RepeatPad, filter);
331
x11sdDst.maskBuffer.con.renderComposite(xrMgr.getCompRule(), x11sdSrc.picture,
332
maskPicture, x11sdDst.picture, 0, 0, 0, 0, compositeBounds.x, compositeBounds.y,
333
compositeBounds.width, compositeBounds.height);
334
} else {
335
int repeat = filter == XRUtils.FAST ? XRUtils.RepeatNone : XRUtils.RepeatPad;
336
337
x11sdSrc.validateAsSource(trx, repeat, filter);
338
339
// compositeBlit takes care of extra alpha
340
x11sdDst.maskBuffer.compositeBlit(x11sdSrc, x11sdDst, 0, 0, compositeBounds.x,
341
compositeBounds.y, compositeBounds.width, compositeBounds.height);
342
}
343
} finally {
344
SunToolkit.awtUnlock();
345
}
346
}
347
}
348
349
class XrSwToPMBlit extends Blit {
350
Blit pmToSurfaceBlit;
351
352
XrSwToPMBlit(SurfaceType srcType, SurfaceType dstType) {
353
super(srcType, CompositeType.AnyAlpha, dstType);
354
pmToSurfaceBlit = new XRPMBlit(dstType, dstType);
355
}
356
357
public void Blit(SurfaceData src, SurfaceData dst, Composite comp, Region clip, int sx, int sy, int dx, int dy, int w, int h) {
358
try {
359
SunToolkit.awtLock();
360
361
XRSurfaceData vImgSurface = XRPMBlitLoops.cacheToTmpSurface(src, (XRSurfaceData) dst, w, h, sx, sy);
362
pmToSurfaceBlit.Blit(vImgSurface, dst, comp, clip, 0, 0, dx, dy, w, h);
363
} finally {
364
SunToolkit.awtUnlock();
365
}
366
}
367
}
368
369
class XrSwToPMScaledBlit extends ScaledBlit {
370
ScaledBlit pmToSurfaceBlit;
371
372
XrSwToPMScaledBlit(SurfaceType srcType, SurfaceType dstType) {
373
super(srcType, CompositeType.AnyAlpha, dstType);
374
pmToSurfaceBlit = new XRPMScaledBlit(dstType, dstType);
375
}
376
377
public void Scale(SurfaceData src, SurfaceData dst, Composite comp, Region clip, int sx1, int sy1, int sx2, int sy2, double dx1, double dy1,
378
double dx2, double dy2) {
379
{
380
int w = sx2 - sx1;
381
int h = sy2 - sy1;
382
383
try {
384
SunToolkit.awtLock();
385
XRSurfaceData vImgSurface = XRPMBlitLoops.cacheToTmpSurface(src, (XRSurfaceData) dst, w, h, sx1, sy1);
386
pmToSurfaceBlit.Scale(vImgSurface, dst, comp, clip, 0, 0, w, h, dx1, dy1, dx2, dy2);
387
} finally {
388
SunToolkit.awtUnlock();
389
}
390
}
391
}
392
}
393
394
class XrSwToPMTransformedBlit extends TransformBlit {
395
TransformBlit pmToSurfaceBlit;
396
397
XrSwToPMTransformedBlit(SurfaceType srcType, SurfaceType dstType) {
398
super(srcType, CompositeType.AnyAlpha, dstType);
399
pmToSurfaceBlit = new XRPMTransformedBlit(dstType, dstType);
400
}
401
402
public void Transform(SurfaceData src, SurfaceData dst, Composite comp, Region clip, AffineTransform xform, int hint, int sx, int sy, int dstx,
403
int dsty, int w, int h) {
404
try {
405
SunToolkit.awtLock();
406
407
XRSurfaceData vImgSurface = XRPMBlitLoops.cacheToTmpSurface(src, (XRSurfaceData) dst, w, h, sx, sy);
408
pmToSurfaceBlit.Transform(vImgSurface, dst, comp, clip, xform, hint, 0, 0, dstx, dsty, w, h);
409
} finally {
410
SunToolkit.awtUnlock();
411
}
412
}
413
}
414
415