0N/A/*
3909N/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
2956N/Aimport java.util.Map;
2956N/Aimport java.util.concurrent.ConcurrentHashMap;
2956N/A
0N/Aimport sun.java2d.pipe.AATileGenerator;
0N/A
3444N/Afinal class PiscesTileGenerator implements AATileGenerator {
2956N/A public static final int TILE_SIZE = PiscesCache.TILE_SIZE;
2956N/A
2956N/A // perhaps we should be using weak references here, but right now
2956N/A // that's not necessary. The way the renderer is, this map will
2956N/A // never contain more than one element - the one with key 64, since
2956N/A // we only do 8x8 supersampling.
2956N/A private static final Map<Integer, byte[]> alphaMapsCache = new
2956N/A ConcurrentHashMap<Integer, byte[]>();
0N/A
0N/A PiscesCache cache;
0N/A int x, y;
2956N/A final int maxalpha;
2956N/A private final int maxTileAlphaSum;
2956N/A
2956N/A // The alpha map used by this object (taken out of our map cache) to convert
2956N/A // pixel coverage counts gotten from PiscesCache (which are in the range
2956N/A // [0, maxalpha]) into alpha values, which are in [0,256).
0N/A byte alphaMap[];
0N/A
2956N/A public PiscesTileGenerator(Renderer r, int maxalpha) {
2956N/A this.cache = r.getCache();
0N/A this.x = cache.bboxX0;
0N/A this.y = cache.bboxY0;
0N/A this.alphaMap = getAlphaMap(maxalpha);
0N/A this.maxalpha = maxalpha;
2956N/A this.maxTileAlphaSum = TILE_SIZE*TILE_SIZE*maxalpha;
0N/A }
0N/A
2956N/A private static byte[] buildAlphaMap(int maxalpha) {
2956N/A byte[] alMap = new byte[maxalpha+1];
2956N/A int halfmaxalpha = maxalpha>>2;
2956N/A for (int i = 0; i <= maxalpha; i++) {
2956N/A alMap[i] = (byte) ((i * 255 + halfmaxalpha) / maxalpha);
2956N/A }
2956N/A return alMap;
2956N/A }
0N/A
2956N/A public static byte[] getAlphaMap(int maxalpha) {
2956N/A if (!alphaMapsCache.containsKey(maxalpha)) {
2956N/A alphaMapsCache.put(maxalpha, buildAlphaMap(maxalpha));
0N/A }
2956N/A return alphaMapsCache.get(maxalpha);
0N/A }
0N/A
0N/A public void getBbox(int bbox[]) {
0N/A bbox[0] = cache.bboxX0;
0N/A bbox[1] = cache.bboxY0;
0N/A bbox[2] = cache.bboxX1;
0N/A bbox[3] = cache.bboxY1;
0N/A //System.out.println("bbox["+bbox[0]+", "+bbox[1]+" => "+bbox[2]+", "+bbox[3]+"]");
0N/A }
0N/A
0N/A /**
0N/A * Gets the width of the tiles that the generator batches output into.
0N/A * @return the width of the standard alpha tile
0N/A */
0N/A public int getTileWidth() {
0N/A return TILE_SIZE;
0N/A }
0N/A
0N/A /**
0N/A * Gets the height of the tiles that the generator batches output into.
0N/A * @return the height of the standard alpha tile
0N/A */
0N/A public int getTileHeight() {
0N/A return TILE_SIZE;
0N/A }
0N/A
0N/A /**
0N/A * Gets the typical alpha value that will characterize the current
0N/A * tile.
0N/A * The answer may be 0x00 to indicate that the current tile has
0N/A * no coverage in any of its pixels, or it may be 0xff to indicate
0N/A * that the current tile is completely covered by the path, or any
0N/A * other value to indicate non-trivial coverage cases.
0N/A * @return 0x00 for no coverage, 0xff for total coverage, or any other
0N/A * value for partial coverage of the tile
0N/A */
0N/A public int getTypicalAlpha() {
2956N/A int al = cache.alphaSumInTile(x, y);
2956N/A // Note: if we have a filled rectangle that doesn't end on a tile
2956N/A // border, we could still return 0xff, even though al!=maxTileAlphaSum
2956N/A // This is because if we return 0xff, our users will fill a rectangle
2956N/A // starting at x,y that has width = Math.min(TILE_SIZE, bboxX1-x),
2956N/A // and height min(TILE_SIZE,bboxY1-y), which is what should happen.
2956N/A // However, to support this, we would have to use 2 Math.min's
2956N/A // and 2 multiplications per tile, instead of just 2 multiplications
2956N/A // to compute maxTileAlphaSum. The savings offered would probably
2956N/A // not be worth it, considering how rare this case is.
2956N/A // Note: I have not tested this, so in the future if it is determined
2956N/A // that it is worth it, it should be implemented. Perhaps this method's
2956N/A // interface should be changed to take arguments the width and height
2956N/A // of the current tile. This would eliminate the 2 Math.min calls that
2956N/A // would be needed here, since our caller needs to compute these 2
2956N/A // values anyway.
2956N/A return (al == 0x00 ? 0x00 :
2956N/A (al == maxTileAlphaSum ? 0xff : 0x80));
0N/A }
0N/A
0N/A /**
0N/A * Skips the current tile and moves on to the next tile.
0N/A * Either this method, or the getAlpha() method should be called
0N/A * once per tile, but not both.
0N/A */
0N/A public void nextTile() {
0N/A if ((x += TILE_SIZE) >= cache.bboxX1) {
0N/A x = cache.bboxX0;
0N/A y += TILE_SIZE;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Gets the alpha coverage values for the current tile.
0N/A * Either this method, or the nextTile() method should be called
0N/A * once per tile, but not both.
0N/A */
0N/A public void getAlpha(byte tile[], int offset, int rowstride) {
0N/A // Decode run-length encoded alpha mask data
0N/A // The data for row j begins at cache.rowOffsetsRLE[j]
0N/A // and is encoded as a set of 2-byte pairs (val, runLen)
0N/A // terminated by a (0, 0) pair.
0N/A
0N/A int x0 = this.x;
0N/A int x1 = x0 + TILE_SIZE;
0N/A int y0 = this.y;
0N/A int y1 = y0 + TILE_SIZE;
0N/A if (x1 > cache.bboxX1) x1 = cache.bboxX1;
0N/A if (y1 > cache.bboxY1) y1 = cache.bboxY1;
0N/A y0 -= cache.bboxY0;
0N/A y1 -= cache.bboxY0;
0N/A
0N/A int idx = offset;
0N/A for (int cy = y0; cy < y1; cy++) {
2956N/A int[] row = cache.rowAARLE[cy];
2956N/A assert row != null;
2956N/A int cx = cache.minTouched(cy);
0N/A if (cx > x1) cx = x1;
0N/A
2956N/A for (int i = x0; i < cx; i++) {
2956N/A tile[idx++] = 0x00;
0N/A }
2956N/A
2956N/A int pos = 2;
2956N/A while (cx < x1 && pos < row[1]) {
0N/A byte val;
0N/A int runLen = 0;
2956N/A assert row[1] > 2;
0N/A try {
2956N/A val = alphaMap[row[pos]];
2956N/A runLen = row[pos + 1];
2956N/A assert runLen > 0;
0N/A } catch (RuntimeException e0) {
0N/A System.out.println("maxalpha = "+maxalpha);
0N/A System.out.println("tile["+x0+", "+y0+
0N/A " => "+x1+", "+y1+"]");
0N/A System.out.println("cx = "+cx+", cy = "+cy);
0N/A System.out.println("idx = "+idx+", pos = "+pos);
0N/A System.out.println("len = "+runLen);
2956N/A System.out.print(cache.toString());
0N/A e0.printStackTrace();
3719N/A throw e0;
0N/A }
2956N/A
0N/A int rx0 = cx;
0N/A cx += runLen;
0N/A int rx1 = cx;
0N/A if (rx0 < x0) rx0 = x0;
0N/A if (rx1 > x1) rx1 = x1;
0N/A runLen = rx1 - rx0;
0N/A //System.out.println("M["+runLen+"]");
0N/A while (--runLen >= 0) {
0N/A try {
0N/A tile[idx++] = val;
0N/A } catch (RuntimeException e) {
0N/A System.out.println("maxalpha = "+maxalpha);
0N/A System.out.println("tile["+x0+", "+y0+
0N/A " => "+x1+", "+y1+"]");
0N/A System.out.println("cx = "+cx+", cy = "+cy);
0N/A System.out.println("idx = "+idx+", pos = "+pos);
0N/A System.out.println("rx0 = "+rx0+", rx1 = "+rx1);
0N/A System.out.println("len = "+runLen);
2956N/A System.out.print(cache.toString());
0N/A e.printStackTrace();
3719N/A throw e;
0N/A }
0N/A }
0N/A pos += 2;
0N/A }
0N/A if (cx < x0) { cx = x0; }
0N/A while (cx < x1) {
0N/A tile[idx++] = 0x00;
0N/A cx++;
0N/A }
0N/A /*
0N/A for (int i = idx - (x1-x0); i < idx; i++) {
0N/A System.out.print(hex(tile[i], 2));
0N/A }
0N/A System.out.println();
0N/A */
0N/A idx += (rowstride - (x1-x0));
0N/A }
0N/A nextTile();
0N/A }
0N/A
0N/A static String hex(int v, int d) {
0N/A String s = Integer.toHexString(v);
0N/A while (s.length() < d) {
0N/A s = "0"+s;
0N/A }
0N/A return s.substring(0, d);
0N/A }
0N/A
0N/A /**
0N/A * Disposes this tile generator.
0N/A * No further calls will be made on this instance.
0N/A */
0N/A public void dispose() {}
3719N/A}
3719N/A