0N/A/*
3261N/A * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.java2d.pisces;
0N/A
0N/Aimport java.util.Arrays;
0N/A
0N/A/**
0N/A * An object used to cache pre-rendered complex paths.
0N/A *
0N/A * @see PiscesRenderer#render
0N/A */
0N/Afinal class PiscesCache {
0N/A
0N/A final int bboxX0, bboxY0, bboxX1, bboxY1;
0N/A
0N/A // rowAARLE[i] holds the encoding of the pixel row with y = bboxY0+i.
0N/A // The format of each of the inner arrays is: rowAARLE[i][0,1] = (x0, n)
0N/A // where x0 is the first x in row i with nonzero alpha, and n is the
0N/A // number of RLE entries in this row. rowAARLE[i][j,j+1] for j>1 is
0N/A // (val,runlen)
0N/A final int[][] rowAARLE;
0N/A
0N/A // RLE encodings are added in increasing y rows and then in increasing
0N/A // x inside those rows. Therefore, at any one time there is a well
0N/A // defined position (x,y) where a run length is about to be added (or
0N/A // the row terminated). x0,y0 is this (x,y)-(bboxX0,bboxY0). They
0N/A // are used to get indices into the current tile.
0N/A private int x0 = Integer.MIN_VALUE, y0 = Integer.MIN_VALUE;
0N/A
0N/A // touchedTile[i][j] is the sum of all the alphas in the tile with
0N/A // y=i*TILE_SIZE+bboxY0 and x=j*TILE_SIZE+bboxX0.
0N/A private final int[][] touchedTile;
0N/A
0N/A static final int TILE_SIZE_LG = 5;
0N/A static final int TILE_SIZE = 1 << TILE_SIZE_LG; // 32
0N/A private static final int INIT_ROW_SIZE = 8; // enough for 3 run lengths
0N/A
0N/A PiscesCache(int minx, int miny, int maxx, int maxy) {
0N/A assert maxy >= miny && maxx >= minx;
0N/A bboxX0 = minx;
0N/A bboxY0 = miny;
0N/A bboxX1 = maxx + 1;
0N/A bboxY1 = maxy + 1;
0N/A // we could just leave the inner arrays as null and allocate them
0N/A // lazily (which would be beneficial for shapes with gaps), but we
0N/A // assume there won't be too many of those so we allocate everything
0N/A // up front (which is better for other cases)
0N/A rowAARLE = new int[bboxY1 - bboxY0 + 1][INIT_ROW_SIZE];
0N/A x0 = 0;
0N/A y0 = -1; // -1 makes the first assert in startRow succeed
0N/A // the ceiling of (maxy - miny + 1) / TILE_SIZE;
0N/A int nyTiles = (maxy - miny + TILE_SIZE) >> TILE_SIZE_LG;
0N/A int nxTiles = (maxx - minx + TILE_SIZE) >> TILE_SIZE_LG;
0N/A
0N/A touchedTile = new int[nyTiles][nxTiles];
0N/A }
0N/A
0N/A void addRLERun(int val, int runLen) {
0N/A if (runLen > 0) {
524N/A addTupleToRow(y0, val, runLen);
524N/A if (val != 0) {
524N/A // the x and y of the current row, minus bboxX0, bboxY0
0N/A int tx = x0 >> TILE_SIZE_LG;
0N/A int ty = y0 >> TILE_SIZE_LG;
0N/A int tx1 = (x0 + runLen - 1) >> TILE_SIZE_LG;
0N/A // while we forbid rows from starting before bboxx0, our users
0N/A // can still store rows that go beyond bboxx1 (although this
0N/A // shouldn't happen), so it's a good idea to check that i
0N/A // is not going out of bounds in touchedTile[ty]
0N/A if (tx1 >= touchedTile[ty].length) {
0N/A tx1 = touchedTile[ty].length - 1;
0N/A }
0N/A if (tx <= tx1) {
0N/A int nextTileXCoord = (tx + 1) << TILE_SIZE_LG;
0N/A if (nextTileXCoord > x0+runLen) {
524N/A touchedTile[ty][tx] += val * runLen;
0N/A } else {
524N/A touchedTile[ty][tx] += val * (nextTileXCoord - x0);
0N/A }
524N/A tx++;
524N/A }
524N/A // don't go all the way to tx1 - we need to handle the last
0N/A // tile as a special case (just like we did with the first
0N/A for (; tx < tx1; tx++) {
0N/A// try {
touchedTile[ty][tx] += (val << TILE_SIZE_LG);
// } catch (RuntimeException e) {
// System.out.println("x0, y0: " + x0 + ", " + y0);
// System.out.printf("tx, ty, tx1: %d, %d, %d %n", tx, ty, tx1);
// System.out.printf("bboxX/Y0/1: %d, %d, %d, %d %n",
// bboxX0, bboxY0, bboxX1, bboxY1);
// throw e;
// }
}
// they will be equal unless x0>>TILE_SIZE_LG == tx1
if (tx == tx1) {
int lastXCoord = Math.min(x0 + runLen, (tx + 1) << TILE_SIZE_LG);
int txXCoord = tx << TILE_SIZE_LG;
touchedTile[ty][tx] += val * (lastXCoord - txXCoord);
}
}
x0 += runLen;
}
}
void startRow(int y, int x) {
// rows are supposed to be added by increasing y.
assert y - bboxY0 > y0;
assert y <= bboxY1; // perhaps this should be < instead of <=
y0 = y - bboxY0;
// this should be a new, uninitialized row.
assert rowAARLE[y0][1] == 0;
x0 = x - bboxX0;
assert x0 >= 0 : "Input must not be to the left of bbox bounds";
// the way addTupleToRow is implemented it would work for this but it's
// not a good idea to use it because it is meant for adding
// RLE tuples, not the first tuple (which is special).
rowAARLE[y0][0] = x;
rowAARLE[y0][1] = 2;
}
int alphaSumInTile(int x, int y) {
x -= bboxX0;
y -= bboxY0;
return touchedTile[y>>TILE_SIZE_LG][x>>TILE_SIZE_LG];
}
int minTouched(int rowidx) {
return rowAARLE[rowidx][0];
}
int rowLength(int rowidx) {
return rowAARLE[rowidx][1];
}
private void addTupleToRow(int row, int a, int b) {
int end = rowAARLE[row][1];
rowAARLE[row] = Helpers.widenArray(rowAARLE[row], end, 2);
rowAARLE[row][end++] = a;
rowAARLE[row][end++] = b;
rowAARLE[row][1] = end;
}
@Override
public String toString() {
String ret = "bbox = ["+
bboxX0+", "+bboxY0+" => "+
bboxX1+", "+bboxY1+"]\n";
for (int[] row : rowAARLE) {
if (row != null) {
ret += ("minTouchedX=" + row[0] +
"\tRLE Entries: " + Arrays.toString(
Arrays.copyOfRange(row, 2, row[1])) + "\n");
} else {
ret += "[]\n";
}
}
return ret;
}
}