Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.desktop/share/classes/sun/java2d/marlin/RendererContext.java
41159 views
1
/*
2
* Copyright (c) 2015, 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.marlin;
27
28
import java.awt.geom.Path2D;
29
import java.lang.ref.WeakReference;
30
import java.util.concurrent.atomic.AtomicInteger;
31
import sun.java2d.ReentrantContext;
32
import sun.java2d.marlin.ArrayCacheConst.CacheStats;
33
import sun.java2d.marlin.DMarlinRenderingEngine.NormalizingPathIterator;
34
import sun.java2d.marlin.TransformingPathConsumer2D.CurveBasicMonotonizer;
35
import sun.java2d.marlin.TransformingPathConsumer2D.CurveClipSplitter;
36
37
/**
38
* This class is a renderer context dedicated to a single thread
39
*/
40
final class RendererContext extends ReentrantContext implements MarlinConst {
41
42
// RendererContext creation counter
43
private static final AtomicInteger CTX_COUNT = new AtomicInteger(1);
44
45
/**
46
* Create a new renderer context
47
*
48
* @return new RendererContext instance
49
*/
50
static RendererContext createContext() {
51
return new RendererContext("ctx"
52
+ Integer.toString(CTX_COUNT.getAndIncrement()));
53
}
54
55
// Smallest object used as Cleaner's parent reference
56
private final Object cleanerObj;
57
// dirty flag indicating an exception occured during pipeline in pathTo()
58
boolean dirty = false;
59
// shared data
60
final double[] double6 = new double[6];
61
// shared curve (dirty) (Renderer / Stroker)
62
final Curve curve = new Curve();
63
// MarlinRenderingEngine NormalizingPathIterator NearestPixelCenter:
64
final NormalizingPathIterator nPCPathIterator;
65
// MarlinRenderingEngine NearestPixelQuarter NormalizingPathIterator:
66
final NormalizingPathIterator nPQPathIterator;
67
// MarlinRenderingEngine.TransformingPathConsumer2D
68
final TransformingPathConsumer2D transformerPC2D;
69
// recycled Path2D instance (weak)
70
private WeakReference<Path2D.Double> refPath2D = null;
71
final Renderer renderer;
72
final Stroker stroker;
73
// Simplifies out collinear lines
74
final CollinearSimplifier simplifier = new CollinearSimplifier();
75
// Simplifies path
76
final PathSimplifier pathSimplifier = new PathSimplifier();
77
final Dasher dasher;
78
final MarlinTileGenerator ptg;
79
final MarlinCache cache;
80
// flag indicating the shape is stroked (1) or filled (0)
81
int stroking = 0;
82
// flag indicating to clip the shape
83
boolean doClip = false;
84
// flag indicating if the path is closed or not (in advance) to handle properly caps
85
boolean closedPath = false;
86
// clip rectangle (ymin, ymax, xmin, xmax):
87
final double[] clipRect = new double[4];
88
// clip inverse scale (mean) to adjust length checks
89
double clipInvScale = 0.0d;
90
// CurveBasicMonotonizer instance
91
final CurveBasicMonotonizer monotonizer;
92
// CurveClipSplitter instance
93
final CurveClipSplitter curveClipSplitter;
94
95
// Array caches:
96
/* clean int[] cache (zero-filled) = 5 refs */
97
private final IntArrayCache cleanIntCache = new IntArrayCache(true, 5);
98
/* dirty int[] cache = 5 refs */
99
private final IntArrayCache dirtyIntCache = new IntArrayCache(false, 5);
100
/* dirty double[] cache = 4 refs (2 polystack) */
101
private final DoubleArrayCache dirtyDoubleCache = new DoubleArrayCache(false, 4);
102
/* dirty byte[] cache = 2 ref (2 polystack) */
103
private final ByteArrayCache dirtyByteCache = new ByteArrayCache(false, 2);
104
105
// RendererContext statistics
106
final RendererStats stats;
107
108
final PathConsumer2DAdapter p2dAdapter = new PathConsumer2DAdapter();
109
110
/**
111
* Constructor
112
*
113
* @param name context name (debugging)
114
*/
115
RendererContext(final String name) {
116
if (LOG_CREATE_CONTEXT) {
117
MarlinUtils.logInfo("new RendererContext = " + name);
118
}
119
this.cleanerObj = new Object();
120
121
// create first stats (needed by newOffHeapArray):
122
if (DO_STATS || DO_MONITORS) {
123
stats = RendererStats.createInstance(cleanerObj, name);
124
// push cache stats:
125
stats.cacheStats = new CacheStats[] { cleanIntCache.stats,
126
dirtyIntCache.stats, dirtyDoubleCache.stats, dirtyByteCache.stats
127
};
128
} else {
129
stats = null;
130
}
131
132
// NormalizingPathIterator instances:
133
nPCPathIterator = new NormalizingPathIterator.NearestPixelCenter(double6);
134
nPQPathIterator = new NormalizingPathIterator.NearestPixelQuarter(double6);
135
136
// curve monotonizer & clip subdivider (before transformerPC2D init)
137
monotonizer = new CurveBasicMonotonizer(this);
138
curveClipSplitter = new CurveClipSplitter(this);
139
140
// MarlinRenderingEngine.TransformingPathConsumer2D
141
transformerPC2D = new TransformingPathConsumer2D(this);
142
143
// Renderer:
144
cache = new MarlinCache(this);
145
renderer = new Renderer(this); // needs MarlinCache from rdrCtx.cache
146
ptg = new MarlinTileGenerator(stats, renderer, cache);
147
148
stroker = new Stroker(this);
149
dasher = new Dasher(this);
150
}
151
152
/**
153
* Disposes this renderer context:
154
* clean up before reusing this context
155
*/
156
void dispose() {
157
if (DO_STATS) {
158
if (stats.totalOffHeap > stats.totalOffHeapMax) {
159
stats.totalOffHeapMax = stats.totalOffHeap;
160
}
161
stats.totalOffHeap = 0L;
162
}
163
stroking = 0;
164
doClip = false;
165
closedPath = false;
166
clipInvScale = 0.0d;
167
168
// if context is maked as DIRTY:
169
if (dirty) {
170
// may happen if an exception if thrown in the pipeline processing:
171
// force cleanup of all possible pipelined blocks (except Renderer):
172
173
// NormalizingPathIterator instances:
174
this.nPCPathIterator.dispose();
175
this.nPQPathIterator.dispose();
176
// Dasher:
177
this.dasher.dispose();
178
// Stroker:
179
this.stroker.dispose();
180
181
// mark context as CLEAN:
182
dirty = false;
183
}
184
}
185
186
Path2D.Double getPath2D() {
187
// resolve reference:
188
Path2D.Double p2d = (refPath2D != null) ? refPath2D.get() : null;
189
190
// create a new Path2D ?
191
if (p2d == null) {
192
p2d = new Path2D.Double(WIND_NON_ZERO, INITIAL_EDGES_COUNT); // 32K
193
194
// update weak reference:
195
refPath2D = new WeakReference<Path2D.Double>(p2d);
196
}
197
// reset the path anyway:
198
p2d.reset();
199
return p2d;
200
}
201
202
RendererStats stats() {
203
return stats;
204
}
205
206
OffHeapArray newOffHeapArray(final long initialSize) {
207
if (DO_STATS) {
208
stats.totalOffHeapInitial += initialSize;
209
}
210
return new OffHeapArray(cleanerObj, initialSize);
211
}
212
213
IntArrayCache.Reference newCleanIntArrayRef(final int initialSize) {
214
return cleanIntCache.createRef(initialSize);
215
}
216
217
IntArrayCache.Reference newDirtyIntArrayRef(final int initialSize) {
218
return dirtyIntCache.createRef(initialSize);
219
}
220
221
DoubleArrayCache.Reference newDirtyDoubleArrayRef(final int initialSize) {
222
return dirtyDoubleCache.createRef(initialSize);
223
}
224
225
ByteArrayCache.Reference newDirtyByteArrayRef(final int initialSize) {
226
return dirtyByteCache.createRef(initialSize);
227
}
228
229
static final class PathConsumer2DAdapter implements DPathConsumer2D {
230
private sun.awt.geom.PathConsumer2D out;
231
232
PathConsumer2DAdapter() {}
233
234
PathConsumer2DAdapter init(sun.awt.geom.PathConsumer2D out) {
235
this.out = out;
236
return this;
237
}
238
239
@Override
240
public void moveTo(double x0, double y0) {
241
out.moveTo((float)x0, (float)y0);
242
}
243
244
@Override
245
public void lineTo(double x1, double y1) {
246
out.lineTo((float)x1, (float)y1);
247
}
248
249
@Override
250
public void closePath() {
251
out.closePath();
252
}
253
254
@Override
255
public void pathDone() {
256
out.pathDone();
257
}
258
259
@Override
260
public void curveTo(double x1, double y1,
261
double x2, double y2,
262
double x3, double y3)
263
{
264
out.curveTo((float)x1, (float)y1,
265
(float)x2, (float)y2,
266
(float)x3, (float)y3);
267
}
268
269
@Override
270
public void quadTo(double x1, double y1, double x2, double y2) {
271
out.quadTo((float)x1, (float)y1, (float)x2, (float)y2);
272
}
273
274
@Override
275
public long getNativeConsumer() {
276
throw new InternalError("Not using a native peer");
277
}
278
}
279
}
280
281