Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/java2d/pisces/PiscesTileGenerator.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.Map;28import java.util.concurrent.ConcurrentHashMap;2930import sun.java2d.pipe.AATileGenerator;3132final class PiscesTileGenerator implements AATileGenerator {33public static final int TILE_SIZE = PiscesCache.TILE_SIZE;3435// perhaps we should be using weak references here, but right now36// that's not necessary. The way the renderer is, this map will37// never contain more than one element - the one with key 64, since38// we only do 8x8 supersampling.39private static final Map<Integer, byte[]> alphaMapsCache = new40ConcurrentHashMap<Integer, byte[]>();4142PiscesCache cache;43int x, y;44final int maxalpha;45private final int maxTileAlphaSum;4647// The alpha map used by this object (taken out of our map cache) to convert48// pixel coverage counts gotten from PiscesCache (which are in the range49// [0, maxalpha]) into alpha values, which are in [0,256).50byte alphaMap[];5152public PiscesTileGenerator(Renderer r, int maxalpha) {53this.cache = r.getCache();54this.x = cache.bboxX0;55this.y = cache.bboxY0;56this.alphaMap = getAlphaMap(maxalpha);57this.maxalpha = maxalpha;58this.maxTileAlphaSum = TILE_SIZE*TILE_SIZE*maxalpha;59}6061private static byte[] buildAlphaMap(int maxalpha) {62byte[] alMap = new byte[maxalpha+1];63int halfmaxalpha = maxalpha>>2;64for (int i = 0; i <= maxalpha; i++) {65alMap[i] = (byte) ((i * 255 + halfmaxalpha) / maxalpha);66}67return alMap;68}6970public static byte[] getAlphaMap(int maxalpha) {71if (!alphaMapsCache.containsKey(maxalpha)) {72alphaMapsCache.put(maxalpha, buildAlphaMap(maxalpha));73}74return alphaMapsCache.get(maxalpha);75}7677public void getBbox(int bbox[]) {78bbox[0] = cache.bboxX0;79bbox[1] = cache.bboxY0;80bbox[2] = cache.bboxX1;81bbox[3] = cache.bboxY1;82//System.out.println("bbox["+bbox[0]+", "+bbox[1]+" => "+bbox[2]+", "+bbox[3]+"]");83}8485/**86* Gets the width of the tiles that the generator batches output into.87* @return the width of the standard alpha tile88*/89public int getTileWidth() {90return TILE_SIZE;91}9293/**94* Gets the height of the tiles that the generator batches output into.95* @return the height of the standard alpha tile96*/97public int getTileHeight() {98return TILE_SIZE;99}100101/**102* Gets the typical alpha value that will characterize the current103* tile.104* The answer may be 0x00 to indicate that the current tile has105* no coverage in any of its pixels, or it may be 0xff to indicate106* that the current tile is completely covered by the path, or any107* other value to indicate non-trivial coverage cases.108* @return 0x00 for no coverage, 0xff for total coverage, or any other109* value for partial coverage of the tile110*/111public int getTypicalAlpha() {112int al = cache.alphaSumInTile(x, y);113// Note: if we have a filled rectangle that doesn't end on a tile114// border, we could still return 0xff, even though al!=maxTileAlphaSum115// This is because if we return 0xff, our users will fill a rectangle116// starting at x,y that has width = Math.min(TILE_SIZE, bboxX1-x),117// and height min(TILE_SIZE,bboxY1-y), which is what should happen.118// However, to support this, we would have to use 2 Math.min's119// and 2 multiplications per tile, instead of just 2 multiplications120// to compute maxTileAlphaSum. The savings offered would probably121// not be worth it, considering how rare this case is.122// Note: I have not tested this, so in the future if it is determined123// that it is worth it, it should be implemented. Perhaps this method's124// interface should be changed to take arguments the width and height125// of the current tile. This would eliminate the 2 Math.min calls that126// would be needed here, since our caller needs to compute these 2127// values anyway.128return (al == 0x00 ? 0x00 :129(al == maxTileAlphaSum ? 0xff : 0x80));130}131132/**133* Skips the current tile and moves on to the next tile.134* Either this method, or the getAlpha() method should be called135* once per tile, but not both.136*/137public void nextTile() {138if ((x += TILE_SIZE) >= cache.bboxX1) {139x = cache.bboxX0;140y += TILE_SIZE;141}142}143144/**145* Gets the alpha coverage values for the current tile.146* Either this method, or the nextTile() method should be called147* once per tile, but not both.148*/149public void getAlpha(byte tile[], int offset, int rowstride) {150// Decode run-length encoded alpha mask data151// The data for row j begins at cache.rowOffsetsRLE[j]152// and is encoded as a set of 2-byte pairs (val, runLen)153// terminated by a (0, 0) pair.154155int x0 = this.x;156int x1 = x0 + TILE_SIZE;157int y0 = this.y;158int y1 = y0 + TILE_SIZE;159if (x1 > cache.bboxX1) x1 = cache.bboxX1;160if (y1 > cache.bboxY1) y1 = cache.bboxY1;161y0 -= cache.bboxY0;162y1 -= cache.bboxY0;163164int idx = offset;165for (int cy = y0; cy < y1; cy++) {166int[] row = cache.rowAARLE[cy];167assert row != null;168int cx = cache.minTouched(cy);169if (cx > x1) cx = x1;170171for (int i = x0; i < cx; i++) {172tile[idx++] = 0x00;173}174175int pos = 2;176while (cx < x1 && pos < row[1]) {177byte val;178int runLen = 0;179assert row[1] > 2;180try {181val = alphaMap[row[pos]];182runLen = row[pos + 1];183assert runLen > 0;184} catch (RuntimeException e0) {185System.out.println("maxalpha = "+maxalpha);186System.out.println("tile["+x0+", "+y0+187" => "+x1+", "+y1+"]");188System.out.println("cx = "+cx+", cy = "+cy);189System.out.println("idx = "+idx+", pos = "+pos);190System.out.println("len = "+runLen);191System.out.print(cache.toString());192e0.printStackTrace();193throw e0;194}195196int rx0 = cx;197cx += runLen;198int rx1 = cx;199if (rx0 < x0) rx0 = x0;200if (rx1 > x1) rx1 = x1;201runLen = rx1 - rx0;202//System.out.println("M["+runLen+"]");203while (--runLen >= 0) {204try {205tile[idx++] = val;206} catch (RuntimeException e) {207System.out.println("maxalpha = "+maxalpha);208System.out.println("tile["+x0+", "+y0+209" => "+x1+", "+y1+"]");210System.out.println("cx = "+cx+", cy = "+cy);211System.out.println("idx = "+idx+", pos = "+pos);212System.out.println("rx0 = "+rx0+", rx1 = "+rx1);213System.out.println("len = "+runLen);214System.out.print(cache.toString());215e.printStackTrace();216throw e;217}218}219pos += 2;220}221if (cx < x0) { cx = x0; }222while (cx < x1) {223tile[idx++] = 0x00;224cx++;225}226/*227for (int i = idx - (x1-x0); i < idx; i++) {228System.out.print(hex(tile[i], 2));229}230System.out.println();231*/232idx += (rowstride - (x1-x0));233}234nextTile();235}236237static String hex(int v, int d) {238String s = Integer.toHexString(v);239while (s.length() < d) {240s = "0"+s;241}242return s.substring(0, d);243}244245/**246* Disposes this tile generator.247* No further calls will be made on this instance.248*/249public void dispose() {}250}251252253254