jdk/src/share/classes/sun/java2d/pisces/PiscesCache.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 3008 e6248b598ca5
child 6997 3642614e2282
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
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: 3008
diff changeset
     2
 * Copyright (c) 2007, 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: 3008
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: 3008
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: 3008
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3008
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3008
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
package sun.java2d.pisces;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * An object used to cache pre-rendered complex paths.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @see PiscesRenderer#render
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public final class PiscesCache {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    int bboxX0, bboxY0, bboxX1, bboxY1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    byte[] rowAARLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    int alphaRLELength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    int[] rowOffsetsRLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    int[] minTouched;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    int alphaRows;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private PiscesCache() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public static PiscesCache createInstance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        return new PiscesCache();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final float ROWAA_RLE_FACTOR = 1.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final float TOUCHED_FACTOR = 1.5f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private static final int MIN_TOUCHED_LEN = 64;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private void reallocRowAARLE(int newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        if (rowAARLE == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            rowAARLE = new byte[newLength];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        } else if (rowAARLE.length < newLength) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            int len = Math.max(newLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                               (int)(rowAARLE.length*ROWAA_RLE_FACTOR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            byte[] newRowAARLE = new byte[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            System.arraycopy(rowAARLE, 0, newRowAARLE, 0, rowAARLE.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            rowAARLE = newRowAARLE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private void reallocRowInfo(int newHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (minTouched == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            int len = Math.max(newHeight, MIN_TOUCHED_LEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            minTouched = new int[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            rowOffsetsRLE = new int[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        } else if (minTouched.length < newHeight) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            int len = Math.max(newHeight,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                               (int)(minTouched.length*TOUCHED_FACTOR));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            int[] newMinTouched = new int[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            int[] newRowOffsetsRLE = new int[len];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            System.arraycopy(minTouched, 0, newMinTouched, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                             alphaRows);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            System.arraycopy(rowOffsetsRLE, 0, newRowOffsetsRLE, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                             alphaRows);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            minTouched = newMinTouched;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            rowOffsetsRLE = newRowOffsetsRLE;
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
    void addRLERun(byte val, int runLen) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        reallocRowAARLE(alphaRLELength + 2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        rowAARLE[alphaRLELength++] = val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        rowAARLE[alphaRLELength++] = (byte)runLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    void startRow(int y, int x0, int x1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (alphaRows == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            bboxY0 = y;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            bboxY1 = y+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            bboxX0 = x0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            bboxX1 = x1+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            if (bboxX0 > x0) bboxX0 = x0;
3008
e6248b598ca5 6829659: Circle is rendered in C shape
jgodinez
parents: 2
diff changeset
    99
            if (bboxX1 < x1 + 1) bboxX1 = x1 + 1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            while (bboxY1++ < y) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                reallocRowInfo(alphaRows+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                minTouched[alphaRows] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                // Assuming last 2 entries in rowAARLE are 0,0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                rowOffsetsRLE[alphaRows] = alphaRLELength-2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                alphaRows++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        reallocRowInfo(alphaRows+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        minTouched[alphaRows] = x0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        rowOffsetsRLE[alphaRows] = alphaRLELength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        alphaRows++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    public synchronized void dispose() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        rowAARLE = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        alphaRLELength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        minTouched = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        rowOffsetsRLE = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        alphaRows = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        bboxX0 = bboxY0 = bboxX1 = bboxY1 = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public void print(java.io.PrintStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        synchronized (out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        out.println("bbox = ["+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    bboxX0+", "+bboxY0+" => "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    bboxX1+", "+bboxY1+"]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        out.println("alphRLELength = "+alphaRLELength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        for (int y = bboxY0; y < bboxY1; y++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            int i = y-bboxY0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            out.println("row["+i+"] == {"+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                        "minX = "+minTouched[i]+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                        ", off = "+rowOffsetsRLE[i]+"}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        for (int i = 0; i < alphaRLELength; i += 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            out.println("rle["+i+"] = "+
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                        (rowAARLE[i+1]&0xff)+" of "+(rowAARLE[i]&0xff));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
}