Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/pisces/PiscesCache.java
38918 views
/*1* Copyright (c) 2007, 2011, 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*/2425package sun.java2d.pisces;2627import java.util.Arrays;2829/**30* An object used to cache pre-rendered complex paths.31*32* @see PiscesRenderer#render33*/34final class PiscesCache {3536final int bboxX0, bboxY0, bboxX1, bboxY1;3738// rowAARLE[i] holds the encoding of the pixel row with y = bboxY0+i.39// The format of each of the inner arrays is: rowAARLE[i][0,1] = (x0, n)40// where x0 is the first x in row i with nonzero alpha, and n is the41// number of RLE entries in this row. rowAARLE[i][j,j+1] for j>1 is42// (val,runlen)43final int[][] rowAARLE;4445// RLE encodings are added in increasing y rows and then in increasing46// x inside those rows. Therefore, at any one time there is a well47// defined position (x,y) where a run length is about to be added (or48// the row terminated). x0,y0 is this (x,y)-(bboxX0,bboxY0). They49// are used to get indices into the current tile.50private int x0 = Integer.MIN_VALUE, y0 = Integer.MIN_VALUE;5152// touchedTile[i][j] is the sum of all the alphas in the tile with53// y=i*TILE_SIZE+bboxY0 and x=j*TILE_SIZE+bboxX0.54private final int[][] touchedTile;5556static final int TILE_SIZE_LG = 5;57static final int TILE_SIZE = 1 << TILE_SIZE_LG; // 3258private static final int INIT_ROW_SIZE = 8; // enough for 3 run lengths5960PiscesCache(int minx, int miny, int maxx, int maxy) {61assert maxy >= miny && maxx >= minx;62bboxX0 = minx;63bboxY0 = miny;64bboxX1 = maxx + 1;65bboxY1 = maxy + 1;66// we could just leave the inner arrays as null and allocate them67// lazily (which would be beneficial for shapes with gaps), but we68// assume there won't be too many of those so we allocate everything69// up front (which is better for other cases)70rowAARLE = new int[bboxY1 - bboxY0 + 1][INIT_ROW_SIZE];71x0 = 0;72y0 = -1; // -1 makes the first assert in startRow succeed73// the ceiling of (maxy - miny + 1) / TILE_SIZE;74int nyTiles = (maxy - miny + TILE_SIZE) >> TILE_SIZE_LG;75int nxTiles = (maxx - minx + TILE_SIZE) >> TILE_SIZE_LG;7677touchedTile = new int[nyTiles][nxTiles];78}7980void addRLERun(int val, int runLen) {81if (runLen > 0) {82addTupleToRow(y0, val, runLen);83if (val != 0) {84// the x and y of the current row, minus bboxX0, bboxY085int tx = x0 >> TILE_SIZE_LG;86int ty = y0 >> TILE_SIZE_LG;87int tx1 = (x0 + runLen - 1) >> TILE_SIZE_LG;88// while we forbid rows from starting before bboxx0, our users89// can still store rows that go beyond bboxx1 (although this90// shouldn't happen), so it's a good idea to check that i91// is not going out of bounds in touchedTile[ty]92if (tx1 >= touchedTile[ty].length) {93tx1 = touchedTile[ty].length - 1;94}95if (tx <= tx1) {96int nextTileXCoord = (tx + 1) << TILE_SIZE_LG;97if (nextTileXCoord > x0+runLen) {98touchedTile[ty][tx] += val * runLen;99} else {100touchedTile[ty][tx] += val * (nextTileXCoord - x0);101}102tx++;103}104// don't go all the way to tx1 - we need to handle the last105// tile as a special case (just like we did with the first106for (; tx < tx1; tx++) {107// try {108touchedTile[ty][tx] += (val << TILE_SIZE_LG);109// } catch (RuntimeException e) {110// System.out.println("x0, y0: " + x0 + ", " + y0);111// System.out.printf("tx, ty, tx1: %d, %d, %d %n", tx, ty, tx1);112// System.out.printf("bboxX/Y0/1: %d, %d, %d, %d %n",113// bboxX0, bboxY0, bboxX1, bboxY1);114// throw e;115// }116}117// they will be equal unless x0>>TILE_SIZE_LG == tx1118if (tx == tx1) {119int lastXCoord = Math.min(x0 + runLen, (tx + 1) << TILE_SIZE_LG);120int txXCoord = tx << TILE_SIZE_LG;121touchedTile[ty][tx] += val * (lastXCoord - txXCoord);122}123}124x0 += runLen;125}126}127128void startRow(int y, int x) {129// rows are supposed to be added by increasing y.130assert y - bboxY0 > y0;131assert y <= bboxY1; // perhaps this should be < instead of <=132133y0 = y - bboxY0;134// this should be a new, uninitialized row.135assert rowAARLE[y0][1] == 0;136137x0 = x - bboxX0;138assert x0 >= 0 : "Input must not be to the left of bbox bounds";139140// the way addTupleToRow is implemented it would work for this but it's141// not a good idea to use it because it is meant for adding142// RLE tuples, not the first tuple (which is special).143rowAARLE[y0][0] = x;144rowAARLE[y0][1] = 2;145}146147int alphaSumInTile(int x, int y) {148x -= bboxX0;149y -= bboxY0;150return touchedTile[y>>TILE_SIZE_LG][x>>TILE_SIZE_LG];151}152153int minTouched(int rowidx) {154return rowAARLE[rowidx][0];155}156157int rowLength(int rowidx) {158return rowAARLE[rowidx][1];159}160161private void addTupleToRow(int row, int a, int b) {162int end = rowAARLE[row][1];163rowAARLE[row] = Helpers.widenArray(rowAARLE[row], end, 2);164rowAARLE[row][end++] = a;165rowAARLE[row][end++] = b;166rowAARLE[row][1] = end;167}168169@Override170public String toString() {171String ret = "bbox = ["+172bboxX0+", "+bboxY0+" => "+173bboxX1+", "+bboxY1+"]\n";174for (int[] row : rowAARLE) {175if (row != null) {176ret += ("minTouchedX=" + row[0] +177"\tRLE Entries: " + Arrays.toString(178Arrays.copyOfRange(row, 2, row[1])) + "\n");179} else {180ret += "[]\n";181}182}183return ret;184}185}186187188