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/MTLPaints.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
package sun.java2d.metal;
26
27
import sun.java2d.SunGraphics2D;
28
import sun.java2d.SurfaceData;
29
import sun.java2d.loops.CompositeType;
30
31
import java.awt.LinearGradientPaint;
32
import java.awt.MultipleGradientPaint;
33
import java.awt.TexturePaint;
34
35
import java.awt.MultipleGradientPaint.ColorSpaceType;
36
import java.awt.MultipleGradientPaint.CycleMethod;
37
import java.awt.image.BufferedImage;
38
import java.util.HashMap;
39
import java.util.Map;
40
41
import static sun.java2d.metal.MTLContext.MTLContextCaps.CAPS_EXT_GRAD_SHADER;
42
import static sun.java2d.pipe.BufferedPaints.MULTI_MAX_FRACTIONS;
43
44
abstract class MTLPaints {
45
46
/**
47
* Holds all registered implementations, using the corresponding
48
* SunGraphics2D.PAINT_* constant as the hash key.
49
*/
50
private static Map<Integer, MTLPaints> impls =
51
new HashMap<Integer, MTLPaints>(4, 1.0f);
52
53
static {
54
impls.put(SunGraphics2D.PAINT_GRADIENT, new Gradient());
55
impls.put(SunGraphics2D.PAINT_LIN_GRADIENT, new LinearGradient());
56
impls.put(SunGraphics2D.PAINT_RAD_GRADIENT, new RadialGradient());
57
impls.put(SunGraphics2D.PAINT_TEXTURE, new Texture());
58
}
59
60
/**
61
* Attempts to locate an implementation corresponding to the paint state
62
* of the provided SunGraphics2D object. If no implementation can be
63
* found, or if the paint cannot be accelerated under the conditions
64
* of the SunGraphics2D, this method returns false; otherwise, returns
65
* true.
66
*/
67
static boolean isValid(SunGraphics2D sg2d) {
68
MTLPaints impl = impls.get(sg2d.paintState);
69
return (impl != null && impl.isPaintValid(sg2d));
70
}
71
72
/**
73
* Returns true if this implementation is able to accelerate the
74
* Paint object associated with, and under the conditions of, the
75
* provided SunGraphics2D instance; otherwise returns false.
76
*/
77
abstract boolean isPaintValid(SunGraphics2D sg2d);
78
79
/************************* GradientPaint support ****************************/
80
81
private static class Gradient extends MTLPaints {
82
private Gradient() {}
83
84
/**
85
* There are no restrictions for accelerating GradientPaint, so
86
* this method always returns true.
87
*/
88
@Override
89
boolean isPaintValid(SunGraphics2D sg2d) {
90
return true;
91
}
92
}
93
94
/************************** TexturePaint support ****************************/
95
96
private static class Texture extends MTLPaints {
97
private Texture() {}
98
99
/**
100
* Returns true if the given TexturePaint instance can be used by the
101
* accelerated MTLPaints.Texture implementation. A TexturePaint is
102
* considered valid if the following conditions are met:
103
* - the texture image dimensions are power-of-two
104
* - the texture image can be (or is already) cached in an Metal
105
* texture object
106
*/
107
@Override
108
boolean isPaintValid(SunGraphics2D sg2d) {
109
TexturePaint paint = (TexturePaint)sg2d.paint;
110
MTLSurfaceData dstData = (MTLSurfaceData)sg2d.surfaceData;
111
BufferedImage bi = paint.getImage();
112
113
SurfaceData srcData =
114
dstData.getSourceSurfaceData(bi,
115
SunGraphics2D.TRANSFORM_ISIDENT,
116
CompositeType.SrcOver, null);
117
if (!(srcData instanceof MTLSurfaceData)) {
118
// REMIND: this is a hack that attempts to cache the system
119
// memory image from the TexturePaint instance into an
120
// Metal texture...
121
srcData =
122
dstData.getSourceSurfaceData(bi,
123
SunGraphics2D.TRANSFORM_ISIDENT,
124
CompositeType.SrcOver, null);
125
if (!(srcData instanceof MTLSurfaceData)) {
126
return false;
127
}
128
}
129
130
// verify that the source surface is actually a texture
131
MTLSurfaceData mtlData = (MTLSurfaceData)srcData;
132
if (mtlData.getType() != MTLSurfaceData.TEXTURE) {
133
return false;
134
}
135
136
return true;
137
}
138
}
139
140
/****************** Shared MultipleGradientPaint support ********************/
141
142
private abstract static class MultiGradient extends MTLPaints {
143
protected MultiGradient() {}
144
145
/**
146
* Returns true if the given MultipleGradientPaint instance can be
147
* used by the accelerated MTLPaints.MultiGradient implementation.
148
* A MultipleGradientPaint is considered valid if the following
149
* conditions are met:
150
* - the number of gradient "stops" is <= MAX_FRACTIONS
151
* - the destination has support for fragment shaders
152
*/
153
@Override
154
boolean isPaintValid(SunGraphics2D sg2d) {
155
MultipleGradientPaint paint = (MultipleGradientPaint)sg2d.paint;
156
// REMIND: ugh, this creates garbage; would be nicer if
157
// we had a MultipleGradientPaint.getNumStops() method...
158
if (paint.getFractions().length > MULTI_MAX_FRACTIONS) {
159
return false;
160
}
161
162
MTLSurfaceData dstData = (MTLSurfaceData)sg2d.surfaceData;
163
MTLGraphicsConfig gc = dstData.getMTLGraphicsConfig();
164
if (!gc.isCapPresent(CAPS_EXT_GRAD_SHADER)) {
165
return false;
166
}
167
168
return true;
169
}
170
}
171
172
/********************** LinearGradientPaint support *************************/
173
174
private static class LinearGradient extends MultiGradient {
175
private LinearGradient() {}
176
177
@Override
178
boolean isPaintValid(SunGraphics2D sg2d) {
179
LinearGradientPaint paint = (LinearGradientPaint)sg2d.paint;
180
181
if (paint.getFractions().length == 2 &&
182
paint.getCycleMethod() != CycleMethod.REPEAT &&
183
paint.getColorSpace() != ColorSpaceType.LINEAR_RGB)
184
{
185
// we can delegate to the optimized two-color gradient
186
// codepath, which does not require fragment shader support
187
return true;
188
}
189
190
return super.isPaintValid(sg2d);
191
}
192
}
193
194
/********************** RadialGradientPaint support *************************/
195
196
private static class RadialGradient extends MultiGradient {
197
private RadialGradient() {}
198
}
199
}
200
201