jdk/src/share/classes/sun/java2d/loops/GraphicsPrimitiveMgr.java
author michaelm
Thu, 24 Oct 2013 20:39:21 +0100
changeset 22339 e91bfaf4360d
parent 5506 202f599c92aa
child 24522 3a0bbf9f5e81
permissions -rw-r--r--
8011786: Better applet networking Reviewed-by: alanb, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @author Charlton Innovations, Inc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
package sun.java2d.loops;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Comparator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.java2d.SunGraphics2D;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *   GraphicsComponentMgr provides services to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *   1. register primitives for later use
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *   2. locate an instance of a primitve based on characteristics
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public final class GraphicsPrimitiveMgr {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final boolean debugTrace = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static GraphicsPrimitive primitives[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static GraphicsPrimitive generalPrimitives[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static boolean needssort = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static native void initIDs(Class GP, Class ST, Class CT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
                                       Class SG2D, Class Color, Class AT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                                       Class XORComp, Class AlphaComp,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
                                       Class Path2D, Class Path2DFloat,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                                       Class SHints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static native void registerNativeLoops();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        initIDs(GraphicsPrimitive.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                SurfaceType.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                CompositeType.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                SunGraphics2D.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                java.awt.Color.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                java.awt.geom.AffineTransform.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                XORComposite.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                java.awt.AlphaComposite.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                java.awt.geom.Path2D.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
                java.awt.geom.Path2D.Float.class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                sun.awt.SunHints.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        CustomComponent.register();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        GeneralRenderer.register();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        registerNativeLoops();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static class PrimitiveSpec {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        public int uniqueID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private static Comparator primSorter = new Comparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        public int compare(Object o1, Object o2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            int id1 = ((GraphicsPrimitive) o1).getUniqueID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            int id2 = ((GraphicsPrimitive) o2).getUniqueID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            return (id1 == id2 ? 0 : (id1 < id2 ? -1 : 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    private static Comparator primFinder = new Comparator() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        public int compare(Object o1, Object o2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            int id1 = ((GraphicsPrimitive) o1).getUniqueID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            int id2 = ((PrimitiveSpec) o2).uniqueID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            return (id1 == id2 ? 0 : (id1 < id2 ? -1 : 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Ensure that noone can instantiate this class.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private GraphicsPrimitiveMgr() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public synchronized static void register(GraphicsPrimitive[] newPrimitives)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        GraphicsPrimitive[] devCollection = primitives;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        int oldSize = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        int newSize = newPrimitives.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (debugTrace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            writeLog("Registering " + newSize + " primitives");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            for (int i = 0; i < newSize; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                writeLog(newPrimitives[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (devCollection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            oldSize = devCollection.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        GraphicsPrimitive[] temp = new GraphicsPrimitive[oldSize + newSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (devCollection != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            System.arraycopy(devCollection, 0, temp, 0, oldSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        System.arraycopy(newPrimitives, 0, temp, oldSize, newSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        needssort = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        primitives = temp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public synchronized static void registerGeneral(GraphicsPrimitive gen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        if (generalPrimitives == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            generalPrimitives = new GraphicsPrimitive[] {gen};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        int len = generalPrimitives.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        GraphicsPrimitive[] newGen = new GraphicsPrimitive[len + 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        System.arraycopy(generalPrimitives, 0, newGen, 0, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        newGen[len] = gen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        generalPrimitives = newGen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    public synchronized static GraphicsPrimitive locate(int primTypeID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                                                        SurfaceType dsttype)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return locate(primTypeID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                      SurfaceType.OpaqueColor,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                      CompositeType.Src,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                      dsttype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public synchronized static GraphicsPrimitive locate(int primTypeID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                                        SurfaceType srctype,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                                                        CompositeType comptype,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                                        SurfaceType dsttype)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
          System.out.println("Looking for:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
          System.out.println("    method: "+signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
          System.out.println("    from:   "+srctype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
          System.out.println("    by:     "+comptype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
          System.out.println("    to:     "+dsttype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        GraphicsPrimitive prim = locatePrim(primTypeID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                                            srctype, comptype, dsttype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (prim == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            //System.out.println("Trying general loop");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            prim = locateGeneral(primTypeID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (prim != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                prim = prim.makePrimitive(srctype, comptype, dsttype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                if (prim != null && GraphicsPrimitive.traceflags != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    prim = prim.traceWrap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        return prim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public synchronized static GraphicsPrimitive
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        locatePrim(int primTypeID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                   SurfaceType srctype,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                   CompositeType comptype,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                   SurfaceType dsttype)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
          System.out.println("Looking for:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
          System.out.println("    method: "+signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
          System.out.println("    from:   "+srctype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
          System.out.println("    by:     "+comptype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
          System.out.println("    to:     "+dsttype);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        SurfaceType src, dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        CompositeType cmp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        GraphicsPrimitive prim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        PrimitiveSpec spec = new PrimitiveSpec();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        for (dst = dsttype; dst != null; dst = dst.getSuperType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            for (src = srctype; src != null; src = src.getSuperType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                for (cmp = comptype; cmp != null; cmp = cmp.getSuperType()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                      System.out.println("Trying:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                      System.out.println("    method: "+spec.methodSignature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                      System.out.println("    from:   "+spec.sourceType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                      System.out.println("    by:     "+spec.compType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                      System.out.println("    to:     "+spec.destType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                    spec.uniqueID =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                        GraphicsPrimitive.makeUniqueID(primTypeID, src, cmp, dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    prim = locate(spec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                    if (prim != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                        //System.out.println("<GPMgr> Found: " + prim + " in " + i + " steps");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                        return prim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    private static GraphicsPrimitive locateGeneral(int primTypeID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (generalPrimitives == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        for (int i = 0; i < generalPrimitives.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            GraphicsPrimitive prim = generalPrimitives[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if (prim.getPrimTypeID() == primTypeID) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                return prim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        //throw new InternalError("No general handler registered for"+signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    private static GraphicsPrimitive locate(PrimitiveSpec spec) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (needssort) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            if (GraphicsPrimitive.traceflags != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                for (int i = 0; i < primitives.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                    primitives[i] = primitives[i].traceWrap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            Arrays.sort(primitives, primSorter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            needssort = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        GraphicsPrimitive[] devCollection = primitives;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        if (devCollection == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        int index = Arrays.binarySearch(devCollection, spec, primFinder);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        if (index >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            GraphicsPrimitive prim = devCollection[index];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            if (prim instanceof GraphicsPrimitiveProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                prim = ((GraphicsPrimitiveProxy) prim).instantiate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                devCollection[index] = prim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
                if (debugTrace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                    writeLog("Instantiated graphics primitive " + prim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            if (debugTrace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                writeLog("Lookup found[" + index + "]["+ prim + "]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            return prim;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (debugTrace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            writeLog("Lookup found nothing for:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            writeLog(" " + spec.uniqueID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    private static void writeLog(String str) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (debugTrace) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            System.err.println(str);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Test that all of the GraphicsPrimitiveProxy objects actually
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * resolve to something.  Throws a RuntimeException if anything
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * is wrong, an has no effect if all is well.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    // This is only really meant to be called from GraphicsPrimitiveProxyTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    // in the regression tests directory, but it has to be here because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    // it needs access to a private data structure.  It is not very
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    // big, though.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
    public static void testPrimitiveInstantiation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        testPrimitiveInstantiation(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    public static void testPrimitiveInstantiation(boolean verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        int resolved = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        int unresolved = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        GraphicsPrimitive[] prims = primitives;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        for (int j = 0; j < prims.length; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            GraphicsPrimitive p = prims[j];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            if (p instanceof GraphicsPrimitiveProxy) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                GraphicsPrimitive r = ((GraphicsPrimitiveProxy) p).instantiate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                if (!r.getSignature().equals(p.getSignature()) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    r.getUniqueID() != p.getUniqueID()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    System.out.println("r.getSignature == "+r.getSignature());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                    System.out.println("r.getUniqueID == " + r.getUniqueID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    System.out.println("p.getSignature == "+p.getSignature());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                    System.out.println("p.getUniqueID == " + p.getUniqueID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                    throw new RuntimeException("Primitive " + p
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                                               + " returns wrong signature for "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                                               + r.getClass());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                // instantiate checks that p.satisfiesSameAs(r)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                unresolved++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                p = r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    System.out.println(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    System.out.println(p + " (not proxied).");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                resolved++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        System.out.println(resolved+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                           " graphics primitives were not proxied.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        System.out.println(unresolved+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                           " proxied graphics primitives resolved correctly.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        System.out.println(resolved+unresolved+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                           " total graphics primitives");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    public static void main(String argv[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
        // REMIND: Should trigger loading of platform primitives somehow...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        if (needssort) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            Arrays.sort(primitives, primSorter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            needssort = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        testPrimitiveInstantiation(argv.length > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
}