Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/pipe/RenderingEngine.java
38918 views
/*1* Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.pipe;2627import java.awt.Shape;28import java.awt.BasicStroke;29import java.awt.geom.PathIterator;30import java.awt.geom.AffineTransform;3132import java.security.PrivilegedAction;33import java.security.AccessController;34import java.util.ServiceLoader;35import sun.security.action.GetPropertyAction;3637import sun.awt.geom.PathConsumer2D;3839/**40* This class abstracts a number of features for which the Java 2D41* implementation relies on proprietary licensed software libraries.42* Access to those features is now achieved by retrieving the singleton43* instance of this class and calling the appropriate methods on it.44* The 3 primary features abstracted here include:45* <dl>46* <dt>Shape createStrokedShape(Shape, [BasicStroke attributes]);47* <dd>This method implements the functionality of the method of the48* same name on the {@link BasicStroke} class.49* <dt>void strokeTo(Shape, [rendering parameters], PathConsumer2D);50* <dd>This method performs widening of the source path on the fly51* and sends the results to the given {@link PathConsumer2D} object.52* This procedure avoids having to create an intermediate Shape53* object to hold the results of the {@code createStrokedShape} method.54* The main user of this method is the Java 2D non-antialiasing renderer.55* <dt>AATileGenerator getAATileGenerator(Shape, [rendering parameters]);56* <dd>This method returns an object which can iterate over the57* specified bounding box and produce tiles of coverage values for58* antialiased rendering. The details of the operation of the59* {@link AATileGenerator} object are explained in its class comments.60* </dl>61* Additionally, the following informational method supplies important62* data about the implementation.63* <dl>64* <dt>float getMinimumAAPenSize()65* <dd>This method provides information on how small the BasicStroke66* line width can get before dropouts occur. Rendering with a BasicStroke67* is defined to never allow the line to have breaks, gaps, or dropouts68* even if the width is set to 0.0f, so this information allows the69* {@link SunGraphics2D} class to detect the "thin line" case and set70* the rendering attributes accordingly.71* </dl>72* At startup the runtime will load a single instance of this class.73* It searches the classpath for a registered provider of this API74* and returns either the last one it finds, or the instance whose75* class name matches the value supplied in the System property76* {@code sun.java2d.renderer}.77* Additionally, a runtime System property flag can be set to trace78* all calls to methods on the {@code RenderingEngine} in use by79* setting the sun.java2d.renderer.trace property to any non-null value.80* <p>81* Parts of the system that need to use any of the above features should82* call {@code RenderingEngine.getInstance()} to obtain the properly83* registered (and possibly trace-enabled) version of the RenderingEngine.84*/85public abstract class RenderingEngine {86private static RenderingEngine reImpl;8788/**89* Returns an instance of {@code RenderingEngine} as determined90* by the installation environment and runtime flags.91* <p>92* A specific instance of the {@code RenderingEngine} can be93* chosen by specifying the runtime flag:94* <pre>95* java -Dsun.java2d.renderer=<classname>96* </pre>97*98* If no specific {@code RenderingEngine} is specified on the command99* or Ductus renderer is specified, it will attempt loading the100* sun.dc.DuctusRenderingEngine class using Class.forName as a fastpath;101* if not found, use the ServiceLoader.102* If no specific {@code RenderingEngine} is specified on the command103* line then the last one returned by enumerating all subclasses of104* {@code RenderingEngine} known to the ServiceLoader is used.105* <p>106* Runtime tracing of the actions of the {@code RenderingEngine}107* can be enabled by specifying the runtime flag:108* <pre>109* java -Dsun.java2d.renderer.trace=<any string>110* </pre>111* @return an instance of {@code RenderingEngine}112* @since 1.7113*/114public static synchronized RenderingEngine getInstance() {115if (reImpl != null) {116return reImpl;117}118119reImpl =120AccessController.doPrivileged(new PrivilegedAction<RenderingEngine>() {121public RenderingEngine run() {122final String ductusREClass = "sun.dc.DuctusRenderingEngine";123String reClass =124System.getProperty("sun.java2d.renderer", ductusREClass);125if (reClass.equals(ductusREClass)) {126try {127Class<?> cls = Class.forName(ductusREClass);128return (RenderingEngine) cls.newInstance();129} catch (ReflectiveOperationException ignored) {130// not found131}132}133134ServiceLoader<RenderingEngine> reLoader =135ServiceLoader.loadInstalled(RenderingEngine.class);136137RenderingEngine service = null;138139for (RenderingEngine re : reLoader) {140service = re;141if (re.getClass().getName().equals(reClass)) {142break;143}144}145return service;146}147});148149if (reImpl == null) {150throw new InternalError("No RenderingEngine module found");151}152153GetPropertyAction gpa =154new GetPropertyAction("sun.java2d.renderer.trace");155String reTrace = AccessController.doPrivileged(gpa);156if (reTrace != null) {157reImpl = new Tracer(reImpl);158}159160return reImpl;161}162163/**164* Create a widened path as specified by the parameters.165* <p>166* The specified {@code src} {@link Shape} is widened according167* to the specified attribute parameters as per the168* {@link BasicStroke} specification.169*170* @param src the source path to be widened171* @param width the width of the widened path as per {@code BasicStroke}172* @param caps the end cap decorations as per {@code BasicStroke}173* @param join the segment join decorations as per {@code BasicStroke}174* @param miterlimit the miter limit as per {@code BasicStroke}175* @param dashes the dash length array as per {@code BasicStroke}176* @param dashphase the initial dash phase as per {@code BasicStroke}177* @return the widened path stored in a new {@code Shape} object178* @since 1.7179*/180public abstract Shape createStrokedShape(Shape src,181float width,182int caps,183int join,184float miterlimit,185float dashes[],186float dashphase);187188/**189* Sends the geometry for a widened path as specified by the parameters190* to the specified consumer.191* <p>192* The specified {@code src} {@link Shape} is widened according193* to the parameters specified by the {@link BasicStroke} object.194* Adjustments are made to the path as appropriate for the195* {@link VALUE_STROKE_NORMALIZE} hint if the {@code normalize}196* boolean parameter is true.197* Adjustments are made to the path as appropriate for the198* {@link VALUE_ANTIALIAS_ON} hint if the {@code antialias}199* boolean parameter is true.200* <p>201* The geometry of the widened path is forwarded to the indicated202* {@link PathConsumer2D} object as it is calculated.203*204* @param src the source path to be widened205* @param bs the {@code BasicSroke} object specifying the206* decorations to be applied to the widened path207* @param normalize indicates whether stroke normalization should208* be applied209* @param antialias indicates whether or not adjustments appropriate210* to antialiased rendering should be applied211* @param consumer the {@code PathConsumer2D} instance to forward212* the widened geometry to213* @since 1.7214*/215public abstract void strokeTo(Shape src,216AffineTransform at,217BasicStroke bs,218boolean thin,219boolean normalize,220boolean antialias,221PathConsumer2D consumer);222223/**224* Construct an antialiased tile generator for the given shape with225* the given rendering attributes and store the bounds of the tile226* iteration in the bbox parameter.227* The {@code at} parameter specifies a transform that should affect228* both the shape and the {@code BasicStroke} attributes.229* The {@code clip} parameter specifies the current clip in effect230* in device coordinates and can be used to prune the data for the231* operation, but the renderer is not required to perform any232* clipping.233* If the {@code BasicStroke} parameter is null then the shape234* should be filled as is, otherwise the attributes of the235* {@code BasicStroke} should be used to specify a draw operation.236* The {@code thin} parameter indicates whether or not the237* transformed {@code BasicStroke} represents coordinates smaller238* than the minimum resolution of the antialiasing rasterizer as239* specified by the {@code getMinimumAAPenWidth()} method.240* <p>241* Upon returning, this method will fill the {@code bbox} parameter242* with 4 values indicating the bounds of the iteration of the243* tile generator.244* The iteration order of the tiles will be as specified by the245* pseudo-code:246* <pre>247* for (y = bbox[1]; y < bbox[3]; y += tileheight) {248* for (x = bbox[0]; x < bbox[2]; x += tilewidth) {249* }250* }251* </pre>252* If there is no output to be rendered, this method may return253* null.254*255* @param s the shape to be rendered (fill or draw)256* @param at the transform to be applied to the shape and the257* stroke attributes258* @param clip the current clip in effect in device coordinates259* @param bs if non-null, a {@code BasicStroke} whose attributes260* should be applied to this operation261* @param thin true if the transformed stroke attributes are smaller262* than the minimum dropout pen width263* @param normalize true if the {@code VALUE_STROKE_NORMALIZE}264* {@code RenderingHint} is in effect265* @param bbox returns the bounds of the iteration266* @return the {@code AATileGenerator} instance to be consulted267* for tile coverages, or null if there is no output to render268* @since 1.7269*/270public abstract AATileGenerator getAATileGenerator(Shape s,271AffineTransform at,272Region clip,273BasicStroke bs,274boolean thin,275boolean normalize,276int bbox[]);277278/**279* Construct an antialiased tile generator for the given parallelogram280* store the bounds of the tile iteration in the bbox parameter.281* The parallelogram is specified as a starting point and 2 delta282* vectors that indicate the slopes of the 2 pairs of sides of the283* parallelogram.284* The 4 corners of the parallelogram are defined by the 4 points:285* <ul>286* <li> {@code x}, {@code y}287* <li> {@code x+dx1}, {@code y+dy1}288* <li> {@code x+dx1+dx2}, {@code y+dy1+dy2}289* <li> {@code x+dx2}, {@code y+dy2}290* </ul>291* The {@code lw1} and {@code lw2} parameters provide a specification292* for an optionally stroked parallelogram if they are positive numbers.293* The {@code lw1} parameter is the ratio of the length of the {@code dx1},294* {@code dx2} delta vector to half of the line width in that same295* direction.296* The {@code lw2} parameter provides the same ratio for the other delta297* vector.298* If {@code lw1} and {@code lw2} are both greater than zero, then299* the parallelogram figure is doubled by both expanding and contracting300* each delta vector by its corresponding {@code lw} value.301* If either (@code lw1) or {@code lw2} are also greater than 1, then302* the inner (contracted) parallelogram disappears and the figure is303* simply a single expanded parallelogram.304* The {@code clip} parameter specifies the current clip in effect305* in device coordinates and can be used to prune the data for the306* operation, but the renderer is not required to perform any307* clipping.308* <p>309* Upon returning, this method will fill the {@code bbox} parameter310* with 4 values indicating the bounds of the iteration of the311* tile generator.312* The iteration order of the tiles will be as specified by the313* pseudo-code:314* <pre>315* for (y = bbox[1]; y < bbox[3]; y += tileheight) {316* for (x = bbox[0]; x < bbox[2]; x += tilewidth) {317* }318* }319* </pre>320* If there is no output to be rendered, this method may return321* null.322*323* @param x the X coordinate of the first corner of the parallelogram324* @param y the Y coordinate of the first corner of the parallelogram325* @param dx1 the X coordinate delta of the first leg of the parallelogram326* @param dy1 the Y coordinate delta of the first leg of the parallelogram327* @param dx2 the X coordinate delta of the second leg of the parallelogram328* @param dy2 the Y coordinate delta of the second leg of the parallelogram329* @param lw1 the line width ratio for the first leg of the parallelogram330* @param lw2 the line width ratio for the second leg of the parallelogram331* @param clip the current clip in effect in device coordinates332* @param bbox returns the bounds of the iteration333* @return the {@code AATileGenerator} instance to be consulted334* for tile coverages, or null if there is no output to render335* @since 1.7336*/337public abstract AATileGenerator getAATileGenerator(double x, double y,338double dx1, double dy1,339double dx2, double dy2,340double lw1, double lw2,341Region clip,342int bbox[]);343344/**345* Returns the minimum pen width that the antialiasing rasterizer346* can represent without dropouts occurring.347* @since 1.7348*/349public abstract float getMinimumAAPenSize();350351/**352* Utility method to feed a {@link PathConsumer2D} object from a353* given {@link PathIterator}.354* This method deals with the details of running the iterator and355* feeding the consumer a segment at a time.356*/357public static void feedConsumer(PathIterator pi, PathConsumer2D consumer) {358float coords[] = new float[6];359while (!pi.isDone()) {360switch (pi.currentSegment(coords)) {361case PathIterator.SEG_MOVETO:362consumer.moveTo(coords[0], coords[1]);363break;364case PathIterator.SEG_LINETO:365consumer.lineTo(coords[0], coords[1]);366break;367case PathIterator.SEG_QUADTO:368consumer.quadTo(coords[0], coords[1],369coords[2], coords[3]);370break;371case PathIterator.SEG_CUBICTO:372consumer.curveTo(coords[0], coords[1],373coords[2], coords[3],374coords[4], coords[5]);375break;376case PathIterator.SEG_CLOSE:377consumer.closePath();378break;379}380pi.next();381}382}383384static class Tracer extends RenderingEngine {385RenderingEngine target;386String name;387388public Tracer(RenderingEngine target) {389this.target = target;390name = target.getClass().getName();391}392393public Shape createStrokedShape(Shape src,394float width,395int caps,396int join,397float miterlimit,398float dashes[],399float dashphase)400{401System.out.println(name+".createStrokedShape("+402src.getClass().getName()+", "+403"width = "+width+", "+404"caps = "+caps+", "+405"join = "+join+", "+406"miter = "+miterlimit+", "+407"dashes = "+dashes+", "+408"dashphase = "+dashphase+")");409return target.createStrokedShape(src,410width, caps, join, miterlimit,411dashes, dashphase);412}413414public void strokeTo(Shape src,415AffineTransform at,416BasicStroke bs,417boolean thin,418boolean normalize,419boolean antialias,420PathConsumer2D consumer)421{422System.out.println(name+".strokeTo("+423src.getClass().getName()+", "+424at+", "+425bs+", "+426(thin ? "thin" : "wide")+", "+427(normalize ? "normalized" : "pure")+", "+428(antialias ? "AA" : "non-AA")+", "+429consumer.getClass().getName()+")");430target.strokeTo(src, at, bs, thin, normalize, antialias, consumer);431}432433public float getMinimumAAPenSize() {434System.out.println(name+".getMinimumAAPenSize()");435return target.getMinimumAAPenSize();436}437438public AATileGenerator getAATileGenerator(Shape s,439AffineTransform at,440Region clip,441BasicStroke bs,442boolean thin,443boolean normalize,444int bbox[])445{446System.out.println(name+".getAATileGenerator("+447s.getClass().getName()+", "+448at+", "+449clip+", "+450bs+", "+451(thin ? "thin" : "wide")+", "+452(normalize ? "normalized" : "pure")+")");453return target.getAATileGenerator(s, at, clip,454bs, thin, normalize,455bbox);456}457public AATileGenerator getAATileGenerator(double x, double y,458double dx1, double dy1,459double dx2, double dy2,460double lw1, double lw2,461Region clip,462int bbox[])463{464System.out.println(name+".getAATileGenerator("+465x+", "+y+", "+466dx1+", "+dy1+", "+467dx2+", "+dy2+", "+468lw1+", "+lw2+", "+469clip+")");470return target.getAATileGenerator(x, y,471dx1, dy1,472dx2, dy2,473lw1, lw2,474clip, bbox);475}476}477}478479480