Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/loops/GraphicsPrimitiveMgr.java
38918 views
/*1* Copyright (c) 1997, 2006, 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*/2425/*26* @author Charlton Innovations, Inc.27*/2829package sun.java2d.loops;3031import java.util.Comparator;32import java.util.Arrays;33import sun.java2d.SunGraphics2D;3435/**36* GraphicsComponentMgr provides services to37* 1. register primitives for later use38* 2. locate an instance of a primitve based on characteristics39*/40public final class GraphicsPrimitiveMgr {4142private static final boolean debugTrace = false;43private static GraphicsPrimitive primitives[];44private static GraphicsPrimitive generalPrimitives[];45private static boolean needssort = true;4647private static native void initIDs(Class GP, Class ST, Class CT,48Class SG2D, Class Color, Class AT,49Class XORComp, Class AlphaComp,50Class Path2D, Class Path2DFloat,51Class SHints);52private static native void registerNativeLoops();5354static {55initIDs(GraphicsPrimitive.class,56SurfaceType.class,57CompositeType.class,58SunGraphics2D.class,59java.awt.Color.class,60java.awt.geom.AffineTransform.class,61XORComposite.class,62java.awt.AlphaComposite.class,63java.awt.geom.Path2D.class,64java.awt.geom.Path2D.Float.class,65sun.awt.SunHints.class);66CustomComponent.register();67GeneralRenderer.register();68registerNativeLoops();69}7071private static class PrimitiveSpec {72public int uniqueID;73}7475private static Comparator primSorter = new Comparator() {76public int compare(Object o1, Object o2) {77int id1 = ((GraphicsPrimitive) o1).getUniqueID();78int id2 = ((GraphicsPrimitive) o2).getUniqueID();7980return (id1 == id2 ? 0 : (id1 < id2 ? -1 : 1));81}82};8384private static Comparator primFinder = new Comparator() {85public int compare(Object o1, Object o2) {86int id1 = ((GraphicsPrimitive) o1).getUniqueID();87int id2 = ((PrimitiveSpec) o2).uniqueID;8889return (id1 == id2 ? 0 : (id1 < id2 ? -1 : 1));90}91};9293/**94* Ensure that noone can instantiate this class.95*/96private GraphicsPrimitiveMgr() {97}9899public synchronized static void register(GraphicsPrimitive[] newPrimitives)100{101GraphicsPrimitive[] devCollection = primitives;102int oldSize = 0;103int newSize = newPrimitives.length;104if (debugTrace) {105writeLog("Registering " + newSize + " primitives");106for (int i = 0; i < newSize; i++) {107writeLog(newPrimitives[i].toString());108}109}110if (devCollection != null) {111oldSize = devCollection.length;112}113GraphicsPrimitive[] temp = new GraphicsPrimitive[oldSize + newSize];114if (devCollection != null) {115System.arraycopy(devCollection, 0, temp, 0, oldSize);116}117System.arraycopy(newPrimitives, 0, temp, oldSize, newSize);118needssort = true;119primitives = temp;120}121122public synchronized static void registerGeneral(GraphicsPrimitive gen) {123if (generalPrimitives == null) {124generalPrimitives = new GraphicsPrimitive[] {gen};125return;126}127int len = generalPrimitives.length;128GraphicsPrimitive[] newGen = new GraphicsPrimitive[len + 1];129System.arraycopy(generalPrimitives, 0, newGen, 0, len);130newGen[len] = gen;131generalPrimitives = newGen;132}133134public synchronized static GraphicsPrimitive locate(int primTypeID,135SurfaceType dsttype)136{137return locate(primTypeID,138SurfaceType.OpaqueColor,139CompositeType.Src,140dsttype);141}142143public synchronized static GraphicsPrimitive locate(int primTypeID,144SurfaceType srctype,145CompositeType comptype,146SurfaceType dsttype)147{148/*149System.out.println("Looking for:");150System.out.println(" method: "+signature);151System.out.println(" from: "+srctype);152System.out.println(" by: "+comptype);153System.out.println(" to: "+dsttype);154*/155GraphicsPrimitive prim = locatePrim(primTypeID,156srctype, comptype, dsttype);157158if (prim == null) {159//System.out.println("Trying general loop");160prim = locateGeneral(primTypeID);161if (prim != null) {162prim = prim.makePrimitive(srctype, comptype, dsttype);163if (prim != null && GraphicsPrimitive.traceflags != 0) {164prim = prim.traceWrap();165}166}167}168return prim;169}170171public synchronized static GraphicsPrimitive172locatePrim(int primTypeID,173SurfaceType srctype,174CompositeType comptype,175SurfaceType dsttype)176{177/*178System.out.println("Looking for:");179System.out.println(" method: "+signature);180System.out.println(" from: "+srctype);181System.out.println(" by: "+comptype);182System.out.println(" to: "+dsttype);183*/184SurfaceType src, dst;185CompositeType cmp;186GraphicsPrimitive prim;187PrimitiveSpec spec = new PrimitiveSpec();188189for (dst = dsttype; dst != null; dst = dst.getSuperType()) {190for (src = srctype; src != null; src = src.getSuperType()) {191for (cmp = comptype; cmp != null; cmp = cmp.getSuperType()) {192/*193System.out.println("Trying:");194System.out.println(" method: "+spec.methodSignature);195System.out.println(" from: "+spec.sourceType);196System.out.println(" by: "+spec.compType);197System.out.println(" to: "+spec.destType);198*/199200spec.uniqueID =201GraphicsPrimitive.makeUniqueID(primTypeID, src, cmp, dst);202prim = locate(spec);203if (prim != null) {204//System.out.println("<GPMgr> Found: " + prim + " in " + i + " steps");205return prim;206}207}208}209}210return null;211}212213private static GraphicsPrimitive locateGeneral(int primTypeID) {214if (generalPrimitives == null) {215return null;216}217for (int i = 0; i < generalPrimitives.length; i++) {218GraphicsPrimitive prim = generalPrimitives[i];219if (prim.getPrimTypeID() == primTypeID) {220return prim;221}222}223return null;224//throw new InternalError("No general handler registered for"+signature);225}226227private static GraphicsPrimitive locate(PrimitiveSpec spec) {228if (needssort) {229if (GraphicsPrimitive.traceflags != 0) {230for (int i = 0; i < primitives.length; i++) {231primitives[i] = primitives[i].traceWrap();232}233}234Arrays.sort(primitives, primSorter);235needssort = false;236}237GraphicsPrimitive[] devCollection = primitives;238if (devCollection == null) {239return null;240}241int index = Arrays.binarySearch(devCollection, spec, primFinder);242if (index >= 0) {243GraphicsPrimitive prim = devCollection[index];244if (prim instanceof GraphicsPrimitiveProxy) {245prim = ((GraphicsPrimitiveProxy) prim).instantiate();246devCollection[index] = prim;247if (debugTrace) {248writeLog("Instantiated graphics primitive " + prim);249}250}251if (debugTrace) {252writeLog("Lookup found[" + index + "]["+ prim + "]");253}254return prim;255}256if (debugTrace) {257writeLog("Lookup found nothing for:");258writeLog(" " + spec.uniqueID);259}260return null;261}262263private static void writeLog(String str) {264if (debugTrace) {265System.err.println(str);266}267}268269/**270* Test that all of the GraphicsPrimitiveProxy objects actually271* resolve to something. Throws a RuntimeException if anything272* is wrong, an has no effect if all is well.273*/274// This is only really meant to be called from GraphicsPrimitiveProxyTest275// in the regression tests directory, but it has to be here because276// it needs access to a private data structure. It is not very277// big, though.278public static void testPrimitiveInstantiation() {279testPrimitiveInstantiation(false);280}281282public static void testPrimitiveInstantiation(boolean verbose) {283int resolved = 0;284int unresolved = 0;285GraphicsPrimitive[] prims = primitives;286for (int j = 0; j < prims.length; j++) {287GraphicsPrimitive p = prims[j];288if (p instanceof GraphicsPrimitiveProxy) {289GraphicsPrimitive r = ((GraphicsPrimitiveProxy) p).instantiate();290if (!r.getSignature().equals(p.getSignature()) ||291r.getUniqueID() != p.getUniqueID()) {292System.out.println("r.getSignature == "+r.getSignature());293System.out.println("r.getUniqueID == " + r.getUniqueID());294System.out.println("p.getSignature == "+p.getSignature());295System.out.println("p.getUniqueID == " + p.getUniqueID());296throw new RuntimeException("Primitive " + p297+ " returns wrong signature for "298+ r.getClass());299}300// instantiate checks that p.satisfiesSameAs(r)301unresolved++;302p = r;303if (verbose) {304System.out.println(p);305}306} else {307if (verbose) {308System.out.println(p + " (not proxied).");309}310resolved++;311}312}313System.out.println(resolved+314" graphics primitives were not proxied.");315System.out.println(unresolved+316" proxied graphics primitives resolved correctly.");317System.out.println(resolved+unresolved+318" total graphics primitives");319}320321public static void main(String argv[]) {322// REMIND: Should trigger loading of platform primitives somehow...323if (needssort) {324Arrays.sort(primitives, primSorter);325needssort = false;326}327testPrimitiveInstantiation(argv.length > 0);328}329}330331332