Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/sun/java2d/loops/GraphicsPrimitiveMgr.java
41159 views
1
/*
2
* Copyright (c) 1997, 2018, 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
/*
27
* @author Charlton Innovations, Inc.
28
*/
29
30
package sun.java2d.loops;
31
32
import java.util.Comparator;
33
import java.util.Arrays;
34
import sun.java2d.SunGraphics2D;
35
36
/**
37
* GraphicsComponentMgr provides services to
38
* 1. register primitives for later use
39
* 2. locate an instance of a primitve based on characteristics
40
*/
41
public final class GraphicsPrimitiveMgr {
42
43
private static final boolean debugTrace = false;
44
private static GraphicsPrimitive[] primitives;
45
private static GraphicsPrimitive[] generalPrimitives;
46
private static boolean needssort = true;
47
48
private static native void initIDs(Class<?> GP, Class<?> ST, Class<?> CT,
49
Class<?> SG2D, Class<?> Color, Class<?> AT,
50
Class<?> XORComp, Class<?> AlphaComp,
51
Class<?> Path2D, Class<?> Path2DFloat,
52
Class<?> SHints);
53
private static native void registerNativeLoops();
54
55
static {
56
initIDs(GraphicsPrimitive.class,
57
SurfaceType.class,
58
CompositeType.class,
59
SunGraphics2D.class,
60
java.awt.Color.class,
61
java.awt.geom.AffineTransform.class,
62
XORComposite.class,
63
java.awt.AlphaComposite.class,
64
java.awt.geom.Path2D.class,
65
java.awt.geom.Path2D.Float.class,
66
sun.awt.SunHints.class);
67
CustomComponent.register();
68
GeneralRenderer.register();
69
registerNativeLoops();
70
}
71
72
private static class PrimitiveSpec {
73
public int uniqueID;
74
}
75
76
private static Comparator<GraphicsPrimitive> primSorter =
77
new Comparator<GraphicsPrimitive>() {
78
public int compare(GraphicsPrimitive o1, GraphicsPrimitive o2) {
79
int id1 = o1.getUniqueID();
80
int id2 = o2.getUniqueID();
81
82
return (id1 == id2 ? 0 : (id1 < id2 ? -1 : 1));
83
}
84
};
85
86
private static Comparator<Object> primFinder = new Comparator<Object>() {
87
public int compare(Object o1, Object o2) {
88
int id1 = ((GraphicsPrimitive) o1).getUniqueID();
89
int id2 = ((PrimitiveSpec) o2).uniqueID;
90
91
return (id1 == id2 ? 0 : (id1 < id2 ? -1 : 1));
92
}
93
};
94
95
/**
96
* Ensure that noone can instantiate this class.
97
*/
98
private GraphicsPrimitiveMgr() {
99
}
100
101
public static synchronized void register(GraphicsPrimitive[] newPrimitives)
102
{
103
GraphicsPrimitive[] devCollection = primitives;
104
int oldSize = 0;
105
int newSize = newPrimitives.length;
106
if (debugTrace) {
107
writeLog("Registering " + newSize + " primitives");
108
for (int i = 0; i < newSize; i++) {
109
writeLog(newPrimitives[i].toString());
110
}
111
}
112
if (devCollection != null) {
113
oldSize = devCollection.length;
114
}
115
GraphicsPrimitive[] temp = new GraphicsPrimitive[oldSize + newSize];
116
if (devCollection != null) {
117
System.arraycopy(devCollection, 0, temp, 0, oldSize);
118
}
119
System.arraycopy(newPrimitives, 0, temp, oldSize, newSize);
120
needssort = true;
121
primitives = temp;
122
}
123
124
/**
125
* Registers the general loop which will be used to produce specific
126
* primitives by the {@link GraphicsPrimitive#makePrimitive} function.
127
*
128
* @param gen the graphics primitive to be registered as the general loop
129
*/
130
public static synchronized void registerGeneral(GraphicsPrimitive gen) {
131
if (generalPrimitives == null) {
132
generalPrimitives = new GraphicsPrimitive[] {gen};
133
return;
134
}
135
int len = generalPrimitives.length;
136
GraphicsPrimitive[] newGen = new GraphicsPrimitive[len + 1];
137
System.arraycopy(generalPrimitives, 0, newGen, 0, len);
138
newGen[len] = gen;
139
generalPrimitives = newGen;
140
}
141
142
public static synchronized GraphicsPrimitive locate(int primTypeID,
143
SurfaceType dsttype)
144
{
145
return locate(primTypeID,
146
SurfaceType.OpaqueColor,
147
CompositeType.Src,
148
dsttype);
149
}
150
151
public static synchronized GraphicsPrimitive locate(int primTypeID,
152
SurfaceType srctype,
153
CompositeType comptype,
154
SurfaceType dsttype)
155
{
156
/*
157
System.out.println("Looking for:");
158
System.out.println(" method: "+signature);
159
System.out.println(" from: "+srctype);
160
System.out.println(" by: "+comptype);
161
System.out.println(" to: "+dsttype);
162
*/
163
GraphicsPrimitive prim = locatePrim(primTypeID,
164
srctype, comptype, dsttype);
165
166
if (prim == null) {
167
//System.out.println("Trying general loop");
168
prim = locateGeneral(primTypeID);
169
if (prim != null) {
170
prim = prim.makePrimitive(srctype, comptype, dsttype);
171
if (prim != null && GraphicsPrimitive.traceflags != 0) {
172
prim = prim.traceWrap();
173
}
174
}
175
}
176
return prim;
177
}
178
179
public static synchronized GraphicsPrimitive
180
locatePrim(int primTypeID,
181
SurfaceType srctype,
182
CompositeType comptype,
183
SurfaceType dsttype)
184
{
185
/*
186
System.out.println("Looking for:");
187
System.out.println(" method: "+signature);
188
System.out.println(" from: "+srctype);
189
System.out.println(" by: "+comptype);
190
System.out.println(" to: "+dsttype);
191
*/
192
SurfaceType src, dst;
193
CompositeType cmp;
194
GraphicsPrimitive prim;
195
PrimitiveSpec spec = new PrimitiveSpec();
196
197
for (dst = dsttype; dst != null; dst = dst.getSuperType()) {
198
for (src = srctype; src != null; src = src.getSuperType()) {
199
for (cmp = comptype; cmp != null; cmp = cmp.getSuperType()) {
200
/*
201
System.out.println("Trying:");
202
System.out.println(" method: "+spec.methodSignature);
203
System.out.println(" from: "+spec.sourceType);
204
System.out.println(" by: "+spec.compType);
205
System.out.println(" to: "+spec.destType);
206
*/
207
208
spec.uniqueID =
209
GraphicsPrimitive.makeUniqueID(primTypeID, src, cmp, dst);
210
prim = locate(spec);
211
if (prim != null) {
212
//System.out.println("<GPMgr> Found: " + prim + " in " + i + " steps");
213
return prim;
214
}
215
}
216
}
217
}
218
return null;
219
}
220
221
private static GraphicsPrimitive locateGeneral(int primTypeID) {
222
if (generalPrimitives == null) {
223
return null;
224
}
225
for (int i = 0; i < generalPrimitives.length; i++) {
226
GraphicsPrimitive prim = generalPrimitives[i];
227
if (prim.getPrimTypeID() == primTypeID) {
228
return prim;
229
}
230
}
231
return null;
232
//throw new InternalError("No general handler registered for"+signature);
233
}
234
235
private static GraphicsPrimitive locate(PrimitiveSpec spec) {
236
if (needssort) {
237
if (GraphicsPrimitive.traceflags != 0) {
238
for (int i = 0; i < primitives.length; i++) {
239
primitives[i] = primitives[i].traceWrap();
240
}
241
}
242
Arrays.sort(primitives, primSorter);
243
needssort = false;
244
}
245
GraphicsPrimitive[] devCollection = primitives;
246
if (devCollection == null) {
247
return null;
248
}
249
int index = Arrays.binarySearch(devCollection, spec, primFinder);
250
if (index >= 0) {
251
GraphicsPrimitive prim = devCollection[index];
252
if (prim instanceof GraphicsPrimitiveProxy) {
253
prim = ((GraphicsPrimitiveProxy) prim).instantiate();
254
devCollection[index] = prim;
255
if (debugTrace) {
256
writeLog("Instantiated graphics primitive " + prim);
257
}
258
}
259
if (debugTrace) {
260
writeLog("Lookup found[" + index + "]["+ prim + "]");
261
}
262
return prim;
263
}
264
if (debugTrace) {
265
writeLog("Lookup found nothing for:");
266
writeLog(" " + spec.uniqueID);
267
}
268
return null;
269
}
270
271
private static void writeLog(String str) {
272
if (debugTrace) {
273
System.err.println(str);
274
}
275
}
276
277
/**
278
* Test that all of the GraphicsPrimitiveProxy objects actually
279
* resolve to something. Throws a RuntimeException if anything
280
* is wrong, an has no effect if all is well.
281
*/
282
// This is only really meant to be called from GraphicsPrimitiveProxyTest
283
// in the regression tests directory, but it has to be here because
284
// it needs access to a private data structure. It is not very
285
// big, though.
286
public static void testPrimitiveInstantiation() {
287
testPrimitiveInstantiation(false);
288
}
289
290
public static void testPrimitiveInstantiation(boolean verbose) {
291
int resolved = 0;
292
int unresolved = 0;
293
GraphicsPrimitive[] prims = primitives;
294
for (int j = 0; j < prims.length; j++) {
295
GraphicsPrimitive p = prims[j];
296
if (p instanceof GraphicsPrimitiveProxy) {
297
GraphicsPrimitive r = ((GraphicsPrimitiveProxy) p).instantiate();
298
if (!r.getSignature().equals(p.getSignature()) ||
299
r.getUniqueID() != p.getUniqueID()) {
300
System.out.println("r.getSignature == "+r.getSignature());
301
System.out.println("r.getUniqueID == " + r.getUniqueID());
302
System.out.println("p.getSignature == "+p.getSignature());
303
System.out.println("p.getUniqueID == " + p.getUniqueID());
304
throw new RuntimeException("Primitive " + p
305
+ " returns wrong signature for "
306
+ r.getClass());
307
}
308
// instantiate checks that p.satisfiesSameAs(r)
309
unresolved++;
310
p = r;
311
if (verbose) {
312
System.out.println(p);
313
}
314
} else {
315
if (verbose) {
316
System.out.println(p + " (not proxied).");
317
}
318
resolved++;
319
}
320
}
321
System.out.println(resolved+
322
" graphics primitives were not proxied.");
323
System.out.println(unresolved+
324
" proxied graphics primitives resolved correctly.");
325
System.out.println(resolved+unresolved+
326
" total graphics primitives");
327
}
328
329
public static void main(String[] argv) {
330
// REMIND: Should trigger loading of platform primitives somehow...
331
if (needssort) {
332
Arrays.sort(primitives, primSorter);
333
needssort = false;
334
}
335
testPrimitiveInstantiation(argv.length > 0);
336
}
337
}
338
339