0N/A/*
3909N/A * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.java2d.pipe;
0N/A
0N/Aimport java.awt.Shape;
0N/Aimport java.awt.BasicStroke;
0N/Aimport java.awt.geom.PathIterator;
0N/Aimport java.awt.geom.AffineTransform;
0N/A
0N/Aimport java.security.PrivilegedAction;
0N/Aimport java.security.AccessController;
0N/Aimport java.util.ServiceLoader;
0N/Aimport sun.security.action.GetPropertyAction;
0N/A
0N/Aimport sun.awt.geom.PathConsumer2D;
0N/A
0N/A/**
0N/A * This class abstracts a number of features for which the Java 2D
0N/A * implementation relies on proprietary licensed software libraries.
0N/A * Access to those features is now achieved by retrieving the singleton
0N/A * instance of this class and calling the appropriate methods on it.
0N/A * The 3 primary features abstracted here include:
0N/A * <dl>
0N/A * <dt>Shape createStrokedShape(Shape, [BasicStroke attributes]);
0N/A * <dd>This method implements the functionality of the method of the
0N/A * same name on the {@link BasicStroke} class.
0N/A * <dt>void strokeTo(Shape, [rendering parameters], PathConsumer2D);
0N/A * <dd>This method performs widening of the source path on the fly
0N/A * and sends the results to the given {@link PathConsumer2D} object.
0N/A * This procedure avoids having to create an intermediate Shape
0N/A * object to hold the results of the {@code createStrokedShape} method.
0N/A * The main user of this method is the Java 2D non-antialiasing renderer.
0N/A * <dt>AATileGenerator getAATileGenerator(Shape, [rendering parameters]);
0N/A * <dd>This method returns an object which can iterate over the
0N/A * specified bounding box and produce tiles of coverage values for
0N/A * antialiased rendering. The details of the operation of the
0N/A * {@link AATileGenerator} object are explained in its class comments.
0N/A * </dl>
0N/A * Additionally, the following informational method supplies important
0N/A * data about the implementation.
0N/A * <dl>
0N/A * <dt>float getMinimumAAPenSize()
0N/A * <dd>This method provides information on how small the BasicStroke
0N/A * line width can get before dropouts occur. Rendering with a BasicStroke
0N/A * is defined to never allow the line to have breaks, gaps, or dropouts
0N/A * even if the width is set to 0.0f, so this information allows the
0N/A * {@link SunGraphics2D} class to detect the "thin line" case and set
0N/A * the rendering attributes accordingly.
0N/A * </dl>
0N/A * At startup the runtime will load a single instance of this class.
0N/A * It searches the classpath for a registered provider of this API
0N/A * and returns either the last one it finds, or the instance whose
0N/A * class name matches the value supplied in the System property
0N/A * {@code sun.java2d.renderer}.
0N/A * Additionally, a runtime System property flag can be set to trace
0N/A * all calls to methods on the {@code RenderingEngine} in use by
0N/A * setting the sun.java2d.renderer.trace property to any non-null value.
0N/A * <p>
0N/A * Parts of the system that need to use any of the above features should
0N/A * call {@code RenderingEngine.getInstance()} to obtain the properly
0N/A * registered (and possibly trace-enabled) version of the RenderingEngine.
0N/A */
0N/Apublic abstract class RenderingEngine {
0N/A private static RenderingEngine reImpl;
0N/A
0N/A /**
0N/A * Returns an instance of {@code RenderingEngine} as determined
0N/A * by the installation environment and runtime flags.
0N/A * <p>
0N/A * A specific instance of the {@code RenderingEngine} can be
0N/A * chosen by specifying the runtime flag:
0N/A * <pre>
0N/A * java -Dsun.java2d.renderer=&lt;classname&gt;
0N/A * </pre>
1181N/A *
1181N/A * If no specific {@code RenderingEngine} is specified on the command
1181N/A * or Ductus renderer is specified, it will attempt loading the
1181N/A * sun.dc.DuctusRenderingEngine class using Class.forName as a fastpath;
1181N/A * if not found, use the ServiceLoader.
0N/A * If no specific {@code RenderingEngine} is specified on the command
0N/A * line then the last one returned by enumerating all subclasses of
0N/A * {@code RenderingEngine} known to the ServiceLoader is used.
0N/A * <p>
0N/A * Runtime tracing of the actions of the {@code RenderingEngine}
0N/A * can be enabled by specifying the runtime flag:
0N/A * <pre>
0N/A * java -Dsun.java2d.renderer.trace=&lt;any string&gt;
0N/A * </pre>
0N/A * @return an instance of {@code RenderingEngine}
0N/A * @since 1.7
0N/A */
0N/A public static synchronized RenderingEngine getInstance() {
0N/A if (reImpl != null) {
0N/A return reImpl;
0N/A }
0N/A
0N/A reImpl = (RenderingEngine)
0N/A AccessController.doPrivileged(new PrivilegedAction() {
0N/A public Object run() {
1181N/A final String ductusREClass = "sun.dc.DuctusRenderingEngine";
0N/A String reClass =
1181N/A System.getProperty("sun.java2d.renderer", ductusREClass);
1181N/A if (reClass.equals(ductusREClass)) {
1181N/A try {
1181N/A Class cls = Class.forName(ductusREClass);
1181N/A return cls.newInstance();
1181N/A } catch (ClassNotFoundException x) {
1181N/A // not found
1181N/A } catch (IllegalAccessException x) {
1181N/A // should not reach here
1181N/A } catch (InstantiationException x) {
1181N/A // should not reach here
1181N/A }
1181N/A }
0N/A
0N/A ServiceLoader<RenderingEngine> reLoader =
0N/A ServiceLoader.loadInstalled(RenderingEngine.class);
0N/A
0N/A RenderingEngine service = null;
0N/A
0N/A for (RenderingEngine re : reLoader) {
0N/A service = re;
0N/A if (re.getClass().getName().equals(reClass)) {
0N/A break;
0N/A }
0N/A }
0N/A return service;
0N/A }
0N/A });
0N/A
0N/A if (reImpl == null) {
0N/A throw new InternalError("No RenderingEngine module found");
0N/A }
0N/A
0N/A GetPropertyAction gpa =
0N/A new GetPropertyAction("sun.java2d.renderer.trace");
0N/A String reTrace = (String) AccessController.doPrivileged(gpa);
0N/A if (reTrace != null) {
0N/A reImpl = new Tracer(reImpl);
0N/A }
0N/A
0N/A return reImpl;
0N/A }
0N/A
0N/A /**
0N/A * Create a widened path as specified by the parameters.
0N/A * <p>
0N/A * The specified {@code src} {@link Shape} is widened according
0N/A * to the specified attribute parameters as per the
0N/A * {@link BasicStroke} specification.
0N/A *
0N/A * @param src the source path to be widened
0N/A * @param width the width of the widened path as per {@code BasicStroke}
0N/A * @param caps the end cap decorations as per {@code BasicStroke}
0N/A * @param join the segment join decorations as per {@code BasicStroke}
0N/A * @param miterlimit the miter limit as per {@code BasicStroke}
0N/A * @param dashes the dash length array as per {@code BasicStroke}
0N/A * @param dashphase the initial dash phase as per {@code BasicStroke}
0N/A * @return the widened path stored in a new {@code Shape} object
0N/A * @since 1.7
0N/A */
0N/A public abstract Shape createStrokedShape(Shape src,
0N/A float width,
0N/A int caps,
0N/A int join,
0N/A float miterlimit,
0N/A float dashes[],
0N/A float dashphase);
0N/A
0N/A /**
0N/A * Sends the geometry for a widened path as specified by the parameters
0N/A * to the specified consumer.
0N/A * <p>
0N/A * The specified {@code src} {@link Shape} is widened according
0N/A * to the parameters specified by the {@link BasicStroke} object.
0N/A * Adjustments are made to the path as appropriate for the
0N/A * {@link VALUE_STROKE_NORMALIZE} hint if the {@code normalize}
0N/A * boolean parameter is true.
0N/A * Adjustments are made to the path as appropriate for the
0N/A * {@link VALUE_ANTIALIAS_ON} hint if the {@code antialias}
0N/A * boolean parameter is true.
0N/A * <p>
0N/A * The geometry of the widened path is forwarded to the indicated
0N/A * {@link PathConsumer2D} object as it is calculated.
0N/A *
0N/A * @param src the source path to be widened
0N/A * @param bs the {@code BasicSroke} object specifying the
0N/A * decorations to be applied to the widened path
0N/A * @param normalize indicates whether stroke normalization should
0N/A * be applied
0N/A * @param antialias indicates whether or not adjustments appropriate
0N/A * to antialiased rendering should be applied
0N/A * @param consumer the {@code PathConsumer2D} instance to forward
0N/A * the widened geometry to
0N/A * @since 1.7
0N/A */
0N/A public abstract void strokeTo(Shape src,
0N/A AffineTransform at,
0N/A BasicStroke bs,
0N/A boolean thin,
0N/A boolean normalize,
0N/A boolean antialias,
0N/A PathConsumer2D consumer);
0N/A
0N/A /**
0N/A * Construct an antialiased tile generator for the given shape with
0N/A * the given rendering attributes and store the bounds of the tile
0N/A * iteration in the bbox parameter.
0N/A * The {@code at} parameter specifies a transform that should affect
0N/A * both the shape and the {@code BasicStroke} attributes.
0N/A * The {@code clip} parameter specifies the current clip in effect
0N/A * in device coordinates and can be used to prune the data for the
0N/A * operation, but the renderer is not required to perform any
0N/A * clipping.
0N/A * If the {@code BasicStroke} parameter is null then the shape
0N/A * should be filled as is, otherwise the attributes of the
0N/A * {@code BasicStroke} should be used to specify a draw operation.
0N/A * The {@code thin} parameter indicates whether or not the
0N/A * transformed {@code BasicStroke} represents coordinates smaller
0N/A * than the minimum resolution of the antialiasing rasterizer as
0N/A * specified by the {@code getMinimumAAPenWidth()} method.
0N/A * <p>
0N/A * Upon returning, this method will fill the {@code bbox} parameter
0N/A * with 4 values indicating the bounds of the iteration of the
0N/A * tile generator.
0N/A * The iteration order of the tiles will be as specified by the
0N/A * pseudo-code:
0N/A * <pre>
0N/A * for (y = bbox[1]; y < bbox[3]; y += tileheight) {
0N/A * for (x = bbox[0]; x < bbox[2]; x += tilewidth) {
0N/A * }
0N/A * }
0N/A * </pre>
0N/A * If there is no output to be rendered, this method may return
0N/A * null.
0N/A *
0N/A * @param s the shape to be rendered (fill or draw)
0N/A * @param at the transform to be applied to the shape and the
0N/A * stroke attributes
0N/A * @param clip the current clip in effect in device coordinates
0N/A * @param bs if non-null, a {@code BasicStroke} whose attributes
0N/A * should be applied to this operation
0N/A * @param thin true if the transformed stroke attributes are smaller
0N/A * than the minimum dropout pen width
0N/A * @param normalize true if the {@code VALUE_STROKE_NORMALIZE}
0N/A * {@code RenderingHint} is in effect
0N/A * @param bbox returns the bounds of the iteration
0N/A * @return the {@code AATileGenerator} instance to be consulted
0N/A * for tile coverages, or null if there is no output to render
0N/A * @since 1.7
0N/A */
0N/A public abstract AATileGenerator getAATileGenerator(Shape s,
0N/A AffineTransform at,
0N/A Region clip,
0N/A BasicStroke bs,
0N/A boolean thin,
0N/A boolean normalize,
0N/A int bbox[]);
0N/A
0N/A /**
3265N/A * Construct an antialiased tile generator for the given parallelogram
3265N/A * store the bounds of the tile iteration in the bbox parameter.
3265N/A * The parallelogram is specified as a starting point and 2 delta
3265N/A * vectors that indicate the slopes of the 2 pairs of sides of the
3265N/A * parallelogram.
3265N/A * The 4 corners of the parallelogram are defined by the 4 points:
3265N/A * <ul>
3265N/A * <li> {@code x}, {@code y}
3265N/A * <li> {@code x+dx1}, {@code y+dy1}
3265N/A * <li> {@code x+dx1+dx2}, {@code y+dy1+dy2}
3265N/A * <li> {@code x+dx2}, {@code y+dy2}
3265N/A * </ul>
3265N/A * The {@code lw1} and {@code lw2} parameters provide a specification
3265N/A * for an optionally stroked parallelogram if they are positive numbers.
3265N/A * The {@code lw1} parameter is the ratio of the length of the {@code dx1},
3265N/A * {@code dx2} delta vector to half of the line width in that same
3265N/A * direction.
3265N/A * The {@code lw2} parameter provides the same ratio for the other delta
3265N/A * vector.
3265N/A * If {@code lw1} and {@code lw2} are both greater than zero, then
3265N/A * the parallelogram figure is doubled by both expanding and contracting
3265N/A * each delta vector by its corresponding {@code lw} value.
3265N/A * If either (@code lw1) or {@code lw2} are also greater than 1, then
3265N/A * the inner (contracted) parallelogram disappears and the figure is
3265N/A * simply a single expanded parallelogram.
3265N/A * The {@code clip} parameter specifies the current clip in effect
3265N/A * in device coordinates and can be used to prune the data for the
3265N/A * operation, but the renderer is not required to perform any
3265N/A * clipping.
3265N/A * <p>
3265N/A * Upon returning, this method will fill the {@code bbox} parameter
3265N/A * with 4 values indicating the bounds of the iteration of the
3265N/A * tile generator.
3265N/A * The iteration order of the tiles will be as specified by the
3265N/A * pseudo-code:
3265N/A * <pre>
3265N/A * for (y = bbox[1]; y < bbox[3]; y += tileheight) {
3265N/A * for (x = bbox[0]; x < bbox[2]; x += tilewidth) {
3265N/A * }
3265N/A * }
3265N/A * </pre>
3265N/A * If there is no output to be rendered, this method may return
3265N/A * null.
3265N/A *
3265N/A * @param x the X coordinate of the first corner of the parallelogram
3265N/A * @param y the Y coordinate of the first corner of the parallelogram
3265N/A * @param dx1 the X coordinate delta of the first leg of the parallelogram
3265N/A * @param dy1 the Y coordinate delta of the first leg of the parallelogram
3265N/A * @param dx2 the X coordinate delta of the second leg of the parallelogram
3265N/A * @param dy2 the Y coordinate delta of the second leg of the parallelogram
3265N/A * @param lw1 the line width ratio for the first leg of the parallelogram
3265N/A * @param lw2 the line width ratio for the second leg of the parallelogram
3265N/A * @param clip the current clip in effect in device coordinates
3265N/A * @param bbox returns the bounds of the iteration
3265N/A * @return the {@code AATileGenerator} instance to be consulted
3265N/A * for tile coverages, or null if there is no output to render
3265N/A * @since 1.7
3265N/A */
3265N/A public abstract AATileGenerator getAATileGenerator(double x, double y,
3265N/A double dx1, double dy1,
3265N/A double dx2, double dy2,
3265N/A double lw1, double lw2,
3265N/A Region clip,
3265N/A int bbox[]);
3265N/A
3265N/A /**
0N/A * Returns the minimum pen width that the antialiasing rasterizer
0N/A * can represent without dropouts occuring.
0N/A * @since 1.7
0N/A */
0N/A public abstract float getMinimumAAPenSize();
0N/A
0N/A /**
0N/A * Utility method to feed a {@link PathConsumer2D} object from a
0N/A * given {@link PathIterator}.
0N/A * This method deals with the details of running the iterator and
0N/A * feeding the consumer a segment at a time.
0N/A */
0N/A public static void feedConsumer(PathIterator pi, PathConsumer2D consumer) {
0N/A float coords[] = new float[6];
0N/A while (!pi.isDone()) {
0N/A switch (pi.currentSegment(coords)) {
0N/A case PathIterator.SEG_MOVETO:
0N/A consumer.moveTo(coords[0], coords[1]);
0N/A break;
0N/A case PathIterator.SEG_LINETO:
0N/A consumer.lineTo(coords[0], coords[1]);
0N/A break;
0N/A case PathIterator.SEG_QUADTO:
0N/A consumer.quadTo(coords[0], coords[1],
0N/A coords[2], coords[3]);
0N/A break;
0N/A case PathIterator.SEG_CUBICTO:
0N/A consumer.curveTo(coords[0], coords[1],
0N/A coords[2], coords[3],
0N/A coords[4], coords[5]);
0N/A break;
0N/A case PathIterator.SEG_CLOSE:
0N/A consumer.closePath();
0N/A break;
0N/A }
0N/A pi.next();
0N/A }
0N/A }
0N/A
0N/A static class Tracer extends RenderingEngine {
0N/A RenderingEngine target;
0N/A String name;
0N/A
0N/A public Tracer(RenderingEngine target) {
0N/A this.target = target;
0N/A name = target.getClass().getName();
0N/A }
0N/A
0N/A public Shape createStrokedShape(Shape src,
0N/A float width,
0N/A int caps,
0N/A int join,
0N/A float miterlimit,
0N/A float dashes[],
0N/A float dashphase)
0N/A {
0N/A System.out.println(name+".createStrokedShape("+
0N/A src.getClass().getName()+", "+
0N/A "width = "+width+", "+
0N/A "caps = "+caps+", "+
0N/A "join = "+join+", "+
0N/A "miter = "+miterlimit+", "+
0N/A "dashes = "+dashes+", "+
0N/A "dashphase = "+dashphase+")");
0N/A return target.createStrokedShape(src,
0N/A width, caps, join, miterlimit,
0N/A dashes, dashphase);
0N/A }
0N/A
0N/A public void strokeTo(Shape src,
0N/A AffineTransform at,
0N/A BasicStroke bs,
0N/A boolean thin,
0N/A boolean normalize,
0N/A boolean antialias,
0N/A PathConsumer2D consumer)
0N/A {
0N/A System.out.println(name+".strokeTo("+
0N/A src.getClass().getName()+", "+
0N/A at+", "+
0N/A bs+", "+
0N/A (thin ? "thin" : "wide")+", "+
0N/A (normalize ? "normalized" : "pure")+", "+
0N/A (antialias ? "AA" : "non-AA")+", "+
0N/A consumer.getClass().getName()+")");
0N/A target.strokeTo(src, at, bs, thin, normalize, antialias, consumer);
0N/A }
0N/A
0N/A public float getMinimumAAPenSize() {
0N/A System.out.println(name+".getMinimumAAPenSize()");
0N/A return target.getMinimumAAPenSize();
0N/A }
0N/A
0N/A public AATileGenerator getAATileGenerator(Shape s,
0N/A AffineTransform at,
0N/A Region clip,
0N/A BasicStroke bs,
0N/A boolean thin,
0N/A boolean normalize,
0N/A int bbox[])
0N/A {
0N/A System.out.println(name+".getAATileGenerator("+
0N/A s.getClass().getName()+", "+
0N/A at+", "+
0N/A clip+", "+
0N/A bs+", "+
0N/A (thin ? "thin" : "wide")+", "+
0N/A (normalize ? "normalized" : "pure")+")");
0N/A return target.getAATileGenerator(s, at, clip,
0N/A bs, thin, normalize,
0N/A bbox);
0N/A }
3265N/A public AATileGenerator getAATileGenerator(double x, double y,
3265N/A double dx1, double dy1,
3265N/A double dx2, double dy2,
3265N/A double lw1, double lw2,
3265N/A Region clip,
3265N/A int bbox[])
3265N/A {
3265N/A System.out.println(name+".getAATileGenerator("+
3265N/A x+", "+y+", "+
3265N/A dx1+", "+dy1+", "+
3265N/A dx2+", "+dy2+", "+
3265N/A lw1+", "+lw2+", "+
3265N/A clip+")");
3265N/A return target.getAATileGenerator(x, y,
3265N/A dx1, dy1,
3265N/A dx2, dy2,
3265N/A lw1, lw2,
3265N/A clip, bbox);
3265N/A }
0N/A }
0N/A}