Path: blob/master/src/java.desktop/share/classes/sun/java2d/loops/GraphicsPrimitiveMgr.java
41159 views
/*1* Copyright (c) 1997, 2018, 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<GraphicsPrimitive> primSorter =76new Comparator<GraphicsPrimitive>() {77public int compare(GraphicsPrimitive o1, GraphicsPrimitive o2) {78int id1 = o1.getUniqueID();79int id2 = o2.getUniqueID();8081return (id1 == id2 ? 0 : (id1 < id2 ? -1 : 1));82}83};8485private static Comparator<Object> primFinder = new Comparator<Object>() {86public int compare(Object o1, Object o2) {87int id1 = ((GraphicsPrimitive) o1).getUniqueID();88int id2 = ((PrimitiveSpec) o2).uniqueID;8990return (id1 == id2 ? 0 : (id1 < id2 ? -1 : 1));91}92};9394/**95* Ensure that noone can instantiate this class.96*/97private GraphicsPrimitiveMgr() {98}99100public static synchronized void register(GraphicsPrimitive[] newPrimitives)101{102GraphicsPrimitive[] devCollection = primitives;103int oldSize = 0;104int newSize = newPrimitives.length;105if (debugTrace) {106writeLog("Registering " + newSize + " primitives");107for (int i = 0; i < newSize; i++) {108writeLog(newPrimitives[i].toString());109}110}111if (devCollection != null) {112oldSize = devCollection.length;113}114GraphicsPrimitive[] temp = new GraphicsPrimitive[oldSize + newSize];115if (devCollection != null) {116System.arraycopy(devCollection, 0, temp, 0, oldSize);117}118System.arraycopy(newPrimitives, 0, temp, oldSize, newSize);119needssort = true;120primitives = temp;121}122123/**124* Registers the general loop which will be used to produce specific125* primitives by the {@link GraphicsPrimitive#makePrimitive} function.126*127* @param gen the graphics primitive to be registered as the general loop128*/129public static synchronized void registerGeneral(GraphicsPrimitive gen) {130if (generalPrimitives == null) {131generalPrimitives = new GraphicsPrimitive[] {gen};132return;133}134int len = generalPrimitives.length;135GraphicsPrimitive[] newGen = new GraphicsPrimitive[len + 1];136System.arraycopy(generalPrimitives, 0, newGen, 0, len);137newGen[len] = gen;138generalPrimitives = newGen;139}140141public static synchronized GraphicsPrimitive locate(int primTypeID,142SurfaceType dsttype)143{144return locate(primTypeID,145SurfaceType.OpaqueColor,146CompositeType.Src,147dsttype);148}149150public static synchronized GraphicsPrimitive locate(int primTypeID,151SurfaceType srctype,152CompositeType comptype,153SurfaceType dsttype)154{155/*156System.out.println("Looking for:");157System.out.println(" method: "+signature);158System.out.println(" from: "+srctype);159System.out.println(" by: "+comptype);160System.out.println(" to: "+dsttype);161*/162GraphicsPrimitive prim = locatePrim(primTypeID,163srctype, comptype, dsttype);164165if (prim == null) {166//System.out.println("Trying general loop");167prim = locateGeneral(primTypeID);168if (prim != null) {169prim = prim.makePrimitive(srctype, comptype, dsttype);170if (prim != null && GraphicsPrimitive.traceflags != 0) {171prim = prim.traceWrap();172}173}174}175return prim;176}177178public static synchronized GraphicsPrimitive179locatePrim(int primTypeID,180SurfaceType srctype,181CompositeType comptype,182SurfaceType dsttype)183{184/*185System.out.println("Looking for:");186System.out.println(" method: "+signature);187System.out.println(" from: "+srctype);188System.out.println(" by: "+comptype);189System.out.println(" to: "+dsttype);190*/191SurfaceType src, dst;192CompositeType cmp;193GraphicsPrimitive prim;194PrimitiveSpec spec = new PrimitiveSpec();195196for (dst = dsttype; dst != null; dst = dst.getSuperType()) {197for (src = srctype; src != null; src = src.getSuperType()) {198for (cmp = comptype; cmp != null; cmp = cmp.getSuperType()) {199/*200System.out.println("Trying:");201System.out.println(" method: "+spec.methodSignature);202System.out.println(" from: "+spec.sourceType);203System.out.println(" by: "+spec.compType);204System.out.println(" to: "+spec.destType);205*/206207spec.uniqueID =208GraphicsPrimitive.makeUniqueID(primTypeID, src, cmp, dst);209prim = locate(spec);210if (prim != null) {211//System.out.println("<GPMgr> Found: " + prim + " in " + i + " steps");212return prim;213}214}215}216}217return null;218}219220private static GraphicsPrimitive locateGeneral(int primTypeID) {221if (generalPrimitives == null) {222return null;223}224for (int i = 0; i < generalPrimitives.length; i++) {225GraphicsPrimitive prim = generalPrimitives[i];226if (prim.getPrimTypeID() == primTypeID) {227return prim;228}229}230return null;231//throw new InternalError("No general handler registered for"+signature);232}233234private static GraphicsPrimitive locate(PrimitiveSpec spec) {235if (needssort) {236if (GraphicsPrimitive.traceflags != 0) {237for (int i = 0; i < primitives.length; i++) {238primitives[i] = primitives[i].traceWrap();239}240}241Arrays.sort(primitives, primSorter);242needssort = false;243}244GraphicsPrimitive[] devCollection = primitives;245if (devCollection == null) {246return null;247}248int index = Arrays.binarySearch(devCollection, spec, primFinder);249if (index >= 0) {250GraphicsPrimitive prim = devCollection[index];251if (prim instanceof GraphicsPrimitiveProxy) {252prim = ((GraphicsPrimitiveProxy) prim).instantiate();253devCollection[index] = prim;254if (debugTrace) {255writeLog("Instantiated graphics primitive " + prim);256}257}258if (debugTrace) {259writeLog("Lookup found[" + index + "]["+ prim + "]");260}261return prim;262}263if (debugTrace) {264writeLog("Lookup found nothing for:");265writeLog(" " + spec.uniqueID);266}267return null;268}269270private static void writeLog(String str) {271if (debugTrace) {272System.err.println(str);273}274}275276/**277* Test that all of the GraphicsPrimitiveProxy objects actually278* resolve to something. Throws a RuntimeException if anything279* is wrong, an has no effect if all is well.280*/281// This is only really meant to be called from GraphicsPrimitiveProxyTest282// in the regression tests directory, but it has to be here because283// it needs access to a private data structure. It is not very284// big, though.285public static void testPrimitiveInstantiation() {286testPrimitiveInstantiation(false);287}288289public static void testPrimitiveInstantiation(boolean verbose) {290int resolved = 0;291int unresolved = 0;292GraphicsPrimitive[] prims = primitives;293for (int j = 0; j < prims.length; j++) {294GraphicsPrimitive p = prims[j];295if (p instanceof GraphicsPrimitiveProxy) {296GraphicsPrimitive r = ((GraphicsPrimitiveProxy) p).instantiate();297if (!r.getSignature().equals(p.getSignature()) ||298r.getUniqueID() != p.getUniqueID()) {299System.out.println("r.getSignature == "+r.getSignature());300System.out.println("r.getUniqueID == " + r.getUniqueID());301System.out.println("p.getSignature == "+p.getSignature());302System.out.println("p.getUniqueID == " + p.getUniqueID());303throw new RuntimeException("Primitive " + p304+ " returns wrong signature for "305+ r.getClass());306}307// instantiate checks that p.satisfiesSameAs(r)308unresolved++;309p = r;310if (verbose) {311System.out.println(p);312}313} else {314if (verbose) {315System.out.println(p + " (not proxied).");316}317resolved++;318}319}320System.out.println(resolved+321" graphics primitives were not proxied.");322System.out.println(unresolved+323" proxied graphics primitives resolved correctly.");324System.out.println(resolved+unresolved+325" total graphics primitives");326}327328public static void main(String[] argv) {329// REMIND: Should trigger loading of platform primitives somehow...330if (needssort) {331Arrays.sort(primitives, primSorter);332needssort = false;333}334testPrimitiveInstantiation(argv.length > 0);335}336}337338339