blob: 1c189f7e0fba92564a77bed9657a4ddab9c50625 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2007 Sun Microsystems, Inc. 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. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.java2d.pipe;
27
28import java.awt.Shape;
29import java.awt.BasicStroke;
30import java.awt.geom.PathIterator;
31import java.awt.geom.AffineTransform;
32
33import java.security.PrivilegedAction;
34import java.security.AccessController;
35import java.util.ServiceLoader;
36import sun.security.action.GetPropertyAction;
37
38import sun.awt.geom.PathConsumer2D;
39
40/**
41 * This class abstracts a number of features for which the Java 2D
42 * implementation relies on proprietary licensed software libraries.
43 * Access to those features is now achieved by retrieving the singleton
44 * instance of this class and calling the appropriate methods on it.
45 * The 3 primary features abstracted here include:
46 * <dl>
47 * <dt>Shape createStrokedShape(Shape, [BasicStroke attributes]);
48 * <dd>This method implements the functionality of the method of the
49 * same name on the {@link BasicStroke} class.
50 * <dt>void strokeTo(Shape, [rendering parameters], PathConsumer2D);
51 * <dd>This method performs widening of the source path on the fly
52 * and sends the results to the given {@link PathConsumer2D} object.
53 * This procedure avoids having to create an intermediate Shape
54 * object to hold the results of the {@code createStrokedShape} method.
55 * The main user of this method is the Java 2D non-antialiasing renderer.
56 * <dt>AATileGenerator getAATileGenerator(Shape, [rendering parameters]);
57 * <dd>This method returns an object which can iterate over the
58 * specified bounding box and produce tiles of coverage values for
59 * antialiased rendering. The details of the operation of the
60 * {@link AATileGenerator} object are explained in its class comments.
61 * </dl>
62 * Additionally, the following informational method supplies important
63 * data about the implementation.
64 * <dl>
65 * <dt>float getMinimumAAPenSize()
66 * <dd>This method provides information on how small the BasicStroke
67 * line width can get before dropouts occur. Rendering with a BasicStroke
68 * is defined to never allow the line to have breaks, gaps, or dropouts
69 * even if the width is set to 0.0f, so this information allows the
70 * {@link SunGraphics2D} class to detect the "thin line" case and set
71 * the rendering attributes accordingly.
72 * </dl>
73 * At startup the runtime will load a single instance of this class.
74 * It searches the classpath for a registered provider of this API
75 * and returns either the last one it finds, or the instance whose
76 * class name matches the value supplied in the System property
77 * {@code sun.java2d.renderer}.
78 * Additionally, a runtime System property flag can be set to trace
79 * all calls to methods on the {@code RenderingEngine} in use by
80 * setting the sun.java2d.renderer.trace property to any non-null value.
81 * <p>
82 * Parts of the system that need to use any of the above features should
83 * call {@code RenderingEngine.getInstance()} to obtain the properly
84 * registered (and possibly trace-enabled) version of the RenderingEngine.
85 */
86public abstract class RenderingEngine {
87 private static RenderingEngine reImpl;
88
89 /**
90 * Returns an instance of {@code RenderingEngine} as determined
91 * by the installation environment and runtime flags.
92 * <p>
93 * A specific instance of the {@code RenderingEngine} can be
94 * chosen by specifying the runtime flag:
95 * <pre>
96 * java -Dsun.java2d.renderer=&lt;classname&gt;
97 * </pre>
98 * If no specific {@code RenderingEngine} is specified on the command
99 * line then the last one returned by enumerating all subclasses of
100 * {@code RenderingEngine} known to the ServiceLoader is used.
101 * <p>
102 * Runtime tracing of the actions of the {@code RenderingEngine}
103 * can be enabled by specifying the runtime flag:
104 * <pre>
105 * java -Dsun.java2d.renderer.trace=&lt;any string&gt;
106 * </pre>
107 * @return an instance of {@code RenderingEngine}
108 * @since 1.7
109 */
110 public static synchronized RenderingEngine getInstance() {
111 if (reImpl != null) {
112 return reImpl;
113 }
114
115 reImpl = (RenderingEngine)
116 AccessController.doPrivileged(new PrivilegedAction() {
117 public Object run() {
118 String reClass =
119 System.getProperty("sun.java2d.renderer",
120 "sun.dc.DuctusRenderingEngine");
121
122 ServiceLoader<RenderingEngine> reLoader =
123 ServiceLoader.loadInstalled(RenderingEngine.class);
124
125 RenderingEngine service = null;
126
127 for (RenderingEngine re : reLoader) {
128 service = re;
129 if (re.getClass().getName().equals(reClass)) {
130 break;
131 }
132 }
133 return service;
134 }
135 });
136
137 if (reImpl == null) {
138 throw new InternalError("No RenderingEngine module found");
139 }
140
141 GetPropertyAction gpa =
142 new GetPropertyAction("sun.java2d.renderer.trace");
143 String reTrace = (String) AccessController.doPrivileged(gpa);
144 if (reTrace != null) {
145 reImpl = new Tracer(reImpl);
146 }
147
148 return reImpl;
149 }
150
151 /**
152 * Create a widened path as specified by the parameters.
153 * <p>
154 * The specified {@code src} {@link Shape} is widened according
155 * to the specified attribute parameters as per the
156 * {@link BasicStroke} specification.
157 *
158 * @param src the source path to be widened
159 * @param width the width of the widened path as per {@code BasicStroke}
160 * @param caps the end cap decorations as per {@code BasicStroke}
161 * @param join the segment join decorations as per {@code BasicStroke}
162 * @param miterlimit the miter limit as per {@code BasicStroke}
163 * @param dashes the dash length array as per {@code BasicStroke}
164 * @param dashphase the initial dash phase as per {@code BasicStroke}
165 * @return the widened path stored in a new {@code Shape} object
166 * @since 1.7
167 */
168 public abstract Shape createStrokedShape(Shape src,
169 float width,
170 int caps,
171 int join,
172 float miterlimit,
173 float dashes[],
174 float dashphase);
175
176 /**
177 * Sends the geometry for a widened path as specified by the parameters
178 * to the specified consumer.
179 * <p>
180 * The specified {@code src} {@link Shape} is widened according
181 * to the parameters specified by the {@link BasicStroke} object.
182 * Adjustments are made to the path as appropriate for the
183 * {@link VALUE_STROKE_NORMALIZE} hint if the {@code normalize}
184 * boolean parameter is true.
185 * Adjustments are made to the path as appropriate for the
186 * {@link VALUE_ANTIALIAS_ON} hint if the {@code antialias}
187 * boolean parameter is true.
188 * <p>
189 * The geometry of the widened path is forwarded to the indicated
190 * {@link PathConsumer2D} object as it is calculated.
191 *
192 * @param src the source path to be widened
193 * @param bs the {@code BasicSroke} object specifying the
194 * decorations to be applied to the widened path
195 * @param normalize indicates whether stroke normalization should
196 * be applied
197 * @param antialias indicates whether or not adjustments appropriate
198 * to antialiased rendering should be applied
199 * @param consumer the {@code PathConsumer2D} instance to forward
200 * the widened geometry to
201 * @since 1.7
202 */
203 public abstract void strokeTo(Shape src,
204 AffineTransform at,
205 BasicStroke bs,
206 boolean thin,
207 boolean normalize,
208 boolean antialias,
209 PathConsumer2D consumer);
210
211 /**
212 * Construct an antialiased tile generator for the given shape with
213 * the given rendering attributes and store the bounds of the tile
214 * iteration in the bbox parameter.
215 * The {@code at} parameter specifies a transform that should affect
216 * both the shape and the {@code BasicStroke} attributes.
217 * The {@code clip} parameter specifies the current clip in effect
218 * in device coordinates and can be used to prune the data for the
219 * operation, but the renderer is not required to perform any
220 * clipping.
221 * If the {@code BasicStroke} parameter is null then the shape
222 * should be filled as is, otherwise the attributes of the
223 * {@code BasicStroke} should be used to specify a draw operation.
224 * The {@code thin} parameter indicates whether or not the
225 * transformed {@code BasicStroke} represents coordinates smaller
226 * than the minimum resolution of the antialiasing rasterizer as
227 * specified by the {@code getMinimumAAPenWidth()} method.
228 * <p>
229 * Upon returning, this method will fill the {@code bbox} parameter
230 * with 4 values indicating the bounds of the iteration of the
231 * tile generator.
232 * The iteration order of the tiles will be as specified by the
233 * pseudo-code:
234 * <pre>
235 * for (y = bbox[1]; y < bbox[3]; y += tileheight) {
236 * for (x = bbox[0]; x < bbox[2]; x += tilewidth) {
237 * }
238 * }
239 * </pre>
240 * If there is no output to be rendered, this method may return
241 * null.
242 *
243 * @param s the shape to be rendered (fill or draw)
244 * @param at the transform to be applied to the shape and the
245 * stroke attributes
246 * @param clip the current clip in effect in device coordinates
247 * @param bs if non-null, a {@code BasicStroke} whose attributes
248 * should be applied to this operation
249 * @param thin true if the transformed stroke attributes are smaller
250 * than the minimum dropout pen width
251 * @param normalize true if the {@code VALUE_STROKE_NORMALIZE}
252 * {@code RenderingHint} is in effect
253 * @param bbox returns the bounds of the iteration
254 * @return the {@code AATileGenerator} instance to be consulted
255 * for tile coverages, or null if there is no output to render
256 * @since 1.7
257 */
258 public abstract AATileGenerator getAATileGenerator(Shape s,
259 AffineTransform at,
260 Region clip,
261 BasicStroke bs,
262 boolean thin,
263 boolean normalize,
264 int bbox[]);
265
266 /**
267 * Returns the minimum pen width that the antialiasing rasterizer
268 * can represent without dropouts occuring.
269 * @since 1.7
270 */
271 public abstract float getMinimumAAPenSize();
272
273 /**
274 * Utility method to feed a {@link PathConsumer2D} object from a
275 * given {@link PathIterator}.
276 * This method deals with the details of running the iterator and
277 * feeding the consumer a segment at a time.
278 */
279 public static void feedConsumer(PathIterator pi, PathConsumer2D consumer) {
280 float coords[] = new float[6];
281 while (!pi.isDone()) {
282 switch (pi.currentSegment(coords)) {
283 case PathIterator.SEG_MOVETO:
284 consumer.moveTo(coords[0], coords[1]);
285 break;
286 case PathIterator.SEG_LINETO:
287 consumer.lineTo(coords[0], coords[1]);
288 break;
289 case PathIterator.SEG_QUADTO:
290 consumer.quadTo(coords[0], coords[1],
291 coords[2], coords[3]);
292 break;
293 case PathIterator.SEG_CUBICTO:
294 consumer.curveTo(coords[0], coords[1],
295 coords[2], coords[3],
296 coords[4], coords[5]);
297 break;
298 case PathIterator.SEG_CLOSE:
299 consumer.closePath();
300 break;
301 }
302 pi.next();
303 }
304 }
305
306 static class Tracer extends RenderingEngine {
307 RenderingEngine target;
308 String name;
309
310 public Tracer(RenderingEngine target) {
311 this.target = target;
312 name = target.getClass().getName();
313 }
314
315 public Shape createStrokedShape(Shape src,
316 float width,
317 int caps,
318 int join,
319 float miterlimit,
320 float dashes[],
321 float dashphase)
322 {
323 System.out.println(name+".createStrokedShape("+
324 src.getClass().getName()+", "+
325 "width = "+width+", "+
326 "caps = "+caps+", "+
327 "join = "+join+", "+
328 "miter = "+miterlimit+", "+
329 "dashes = "+dashes+", "+
330 "dashphase = "+dashphase+")");
331 return target.createStrokedShape(src,
332 width, caps, join, miterlimit,
333 dashes, dashphase);
334 }
335
336 public void strokeTo(Shape src,
337 AffineTransform at,
338 BasicStroke bs,
339 boolean thin,
340 boolean normalize,
341 boolean antialias,
342 PathConsumer2D consumer)
343 {
344 System.out.println(name+".strokeTo("+
345 src.getClass().getName()+", "+
346 at+", "+
347 bs+", "+
348 (thin ? "thin" : "wide")+", "+
349 (normalize ? "normalized" : "pure")+", "+
350 (antialias ? "AA" : "non-AA")+", "+
351 consumer.getClass().getName()+")");
352 target.strokeTo(src, at, bs, thin, normalize, antialias, consumer);
353 }
354
355 public float getMinimumAAPenSize() {
356 System.out.println(name+".getMinimumAAPenSize()");
357 return target.getMinimumAAPenSize();
358 }
359
360 public AATileGenerator getAATileGenerator(Shape s,
361 AffineTransform at,
362 Region clip,
363 BasicStroke bs,
364 boolean thin,
365 boolean normalize,
366 int bbox[])
367 {
368 System.out.println(name+".getAATileGenerator("+
369 s.getClass().getName()+", "+
370 at+", "+
371 clip+", "+
372 bs+", "+
373 (thin ? "thin" : "wide")+", "+
374 (normalize ? "normalized" : "pure")+")");
375 return target.getAATileGenerator(s, at, clip,
376 bs, thin, normalize,
377 bbox);
378 }
379 }
380}