author | ohair |
Wed, 06 Apr 2011 22:06:11 -0700 | |
changeset 9035 | 1255eb81cc2f |
parent 8748 | 99ac71f8ef92 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
8748
diff
changeset
|
2 |
* Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package sun.java2d.pisces; |
|
27 |
||
6997
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
28 |
import java.util.Map; |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
29 |
import java.util.concurrent.ConcurrentHashMap; |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
30 |
|
2 | 31 |
import sun.java2d.pipe.AATileGenerator; |
32 |
||
8131
e2932d8114cb
7016856: dashing performance was reduced during latest changes to the OpenJDK rasterizer
dlila
parents:
6997
diff
changeset
|
33 |
final class PiscesTileGenerator implements AATileGenerator { |
6997
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
34 |
public static final int TILE_SIZE = PiscesCache.TILE_SIZE; |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
35 |
|
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
36 |
// perhaps we should be using weak references here, but right now |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
37 |
// that's not necessary. The way the renderer is, this map will |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
38 |
// never contain more than one element - the one with key 64, since |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
39 |
// we only do 8x8 supersampling. |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
40 |
private static final Map<Integer, byte[]> alphaMapsCache = new |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
41 |
ConcurrentHashMap<Integer, byte[]>(); |
2 | 42 |
|
43 |
PiscesCache cache; |
|
44 |
int x, y; |
|
6997
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
45 |
final int maxalpha; |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
46 |
private final int maxTileAlphaSum; |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
47 |
|
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
48 |
// The alpha map used by this object (taken out of our map cache) to convert |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
49 |
// pixel coverage counts gotten from PiscesCache (which are in the range |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
50 |
// [0, maxalpha]) into alpha values, which are in [0,256). |
2 | 51 |
byte alphaMap[]; |
52 |
||
6997
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
53 |
public PiscesTileGenerator(Renderer r, int maxalpha) { |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
54 |
this.cache = r.getCache(); |
2 | 55 |
this.x = cache.bboxX0; |
56 |
this.y = cache.bboxY0; |
|
57 |
this.alphaMap = getAlphaMap(maxalpha); |
|
58 |
this.maxalpha = maxalpha; |
|
6997
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
59 |
this.maxTileAlphaSum = TILE_SIZE*TILE_SIZE*maxalpha; |
2 | 60 |
} |
61 |
||
6997
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
62 |
private static byte[] buildAlphaMap(int maxalpha) { |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
63 |
byte[] alMap = new byte[maxalpha+1]; |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
64 |
int halfmaxalpha = maxalpha>>2; |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
65 |
for (int i = 0; i <= maxalpha; i++) { |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
66 |
alMap[i] = (byte) ((i * 255 + halfmaxalpha) / maxalpha); |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
67 |
} |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
68 |
return alMap; |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
69 |
} |
2 | 70 |
|
6997
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
71 |
public static byte[] getAlphaMap(int maxalpha) { |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
72 |
if (!alphaMapsCache.containsKey(maxalpha)) { |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
73 |
alphaMapsCache.put(maxalpha, buildAlphaMap(maxalpha)); |
2 | 74 |
} |
6997
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
75 |
return alphaMapsCache.get(maxalpha); |
2 | 76 |
} |
77 |
||
78 |
public void getBbox(int bbox[]) { |
|
79 |
bbox[0] = cache.bboxX0; |
|
80 |
bbox[1] = cache.bboxY0; |
|
81 |
bbox[2] = cache.bboxX1; |
|
82 |
bbox[3] = cache.bboxY1; |
|
83 |
//System.out.println("bbox["+bbox[0]+", "+bbox[1]+" => "+bbox[2]+", "+bbox[3]+"]"); |
|
84 |
} |
|
85 |
||
86 |
/** |
|
87 |
* Gets the width of the tiles that the generator batches output into. |
|
88 |
* @return the width of the standard alpha tile |
|
89 |
*/ |
|
90 |
public int getTileWidth() { |
|
91 |
return TILE_SIZE; |
|
92 |
} |
|
93 |
||
94 |
/** |
|
95 |
* Gets the height of the tiles that the generator batches output into. |
|
96 |
* @return the height of the standard alpha tile |
|
97 |
*/ |
|
98 |
public int getTileHeight() { |
|
99 |
return TILE_SIZE; |
|
100 |
} |
|
101 |
||
102 |
/** |
|
103 |
* Gets the typical alpha value that will characterize the current |
|
104 |
* tile. |
|
105 |
* The answer may be 0x00 to indicate that the current tile has |
|
106 |
* no coverage in any of its pixels, or it may be 0xff to indicate |
|
107 |
* that the current tile is completely covered by the path, or any |
|
108 |
* other value to indicate non-trivial coverage cases. |
|
109 |
* @return 0x00 for no coverage, 0xff for total coverage, or any other |
|
110 |
* value for partial coverage of the tile |
|
111 |
*/ |
|
112 |
public int getTypicalAlpha() { |
|
6997
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
113 |
int al = cache.alphaSumInTile(x, y); |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
114 |
// Note: if we have a filled rectangle that doesn't end on a tile |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
115 |
// border, we could still return 0xff, even though al!=maxTileAlphaSum |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
116 |
// This is because if we return 0xff, our users will fill a rectangle |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
117 |
// starting at x,y that has width = Math.min(TILE_SIZE, bboxX1-x), |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
118 |
// and height min(TILE_SIZE,bboxY1-y), which is what should happen. |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
119 |
// However, to support this, we would have to use 2 Math.min's |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
120 |
// and 2 multiplications per tile, instead of just 2 multiplications |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
121 |
// to compute maxTileAlphaSum. The savings offered would probably |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
122 |
// not be worth it, considering how rare this case is. |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
123 |
// Note: I have not tested this, so in the future if it is determined |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
124 |
// that it is worth it, it should be implemented. Perhaps this method's |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
125 |
// interface should be changed to take arguments the width and height |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
126 |
// of the current tile. This would eliminate the 2 Math.min calls that |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
127 |
// would be needed here, since our caller needs to compute these 2 |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
128 |
// values anyway. |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
129 |
return (al == 0x00 ? 0x00 : |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
130 |
(al == maxTileAlphaSum ? 0xff : 0x80)); |
2 | 131 |
} |
132 |
||
133 |
/** |
|
134 |
* Skips the current tile and moves on to the next tile. |
|
135 |
* Either this method, or the getAlpha() method should be called |
|
136 |
* once per tile, but not both. |
|
137 |
*/ |
|
138 |
public void nextTile() { |
|
139 |
if ((x += TILE_SIZE) >= cache.bboxX1) { |
|
140 |
x = cache.bboxX0; |
|
141 |
y += TILE_SIZE; |
|
142 |
} |
|
143 |
} |
|
144 |
||
145 |
/** |
|
146 |
* Gets the alpha coverage values for the current tile. |
|
147 |
* Either this method, or the nextTile() method should be called |
|
148 |
* once per tile, but not both. |
|
149 |
*/ |
|
150 |
public void getAlpha(byte tile[], int offset, int rowstride) { |
|
151 |
// Decode run-length encoded alpha mask data |
|
152 |
// The data for row j begins at cache.rowOffsetsRLE[j] |
|
153 |
// and is encoded as a set of 2-byte pairs (val, runLen) |
|
154 |
// terminated by a (0, 0) pair. |
|
155 |
||
156 |
int x0 = this.x; |
|
157 |
int x1 = x0 + TILE_SIZE; |
|
158 |
int y0 = this.y; |
|
159 |
int y1 = y0 + TILE_SIZE; |
|
160 |
if (x1 > cache.bboxX1) x1 = cache.bboxX1; |
|
161 |
if (y1 > cache.bboxY1) y1 = cache.bboxY1; |
|
162 |
y0 -= cache.bboxY0; |
|
163 |
y1 -= cache.bboxY0; |
|
164 |
||
165 |
int idx = offset; |
|
166 |
for (int cy = y0; cy < y1; cy++) { |
|
6997
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
167 |
int[] row = cache.rowAARLE[cy]; |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
168 |
assert row != null; |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
169 |
int cx = cache.minTouched(cy); |
2 | 170 |
if (cx > x1) cx = x1; |
171 |
||
6997
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
172 |
for (int i = x0; i < cx; i++) { |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
173 |
tile[idx++] = 0x00; |
2 | 174 |
} |
6997
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
175 |
|
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
176 |
int pos = 2; |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
177 |
while (cx < x1 && pos < row[1]) { |
2 | 178 |
byte val; |
179 |
int runLen = 0; |
|
6997
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
180 |
assert row[1] > 2; |
2 | 181 |
try { |
6997
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
182 |
val = alphaMap[row[pos]]; |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
183 |
runLen = row[pos + 1]; |
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
184 |
assert runLen > 0; |
2 | 185 |
} catch (RuntimeException e0) { |
186 |
System.out.println("maxalpha = "+maxalpha); |
|
187 |
System.out.println("tile["+x0+", "+y0+ |
|
188 |
" => "+x1+", "+y1+"]"); |
|
189 |
System.out.println("cx = "+cx+", cy = "+cy); |
|
190 |
System.out.println("idx = "+idx+", pos = "+pos); |
|
191 |
System.out.println("len = "+runLen); |
|
6997
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
192 |
System.out.print(cache.toString()); |
2 | 193 |
e0.printStackTrace(); |
8748
99ac71f8ef92
7019861: Last scanline is skipped in pisces.Renderer.
dlila
parents:
8131
diff
changeset
|
194 |
throw e0; |
2 | 195 |
} |
6997
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
196 |
|
2 | 197 |
int rx0 = cx; |
198 |
cx += runLen; |
|
199 |
int rx1 = cx; |
|
200 |
if (rx0 < x0) rx0 = x0; |
|
201 |
if (rx1 > x1) rx1 = x1; |
|
202 |
runLen = rx1 - rx0; |
|
203 |
//System.out.println("M["+runLen+"]"); |
|
204 |
while (--runLen >= 0) { |
|
205 |
try { |
|
206 |
tile[idx++] = val; |
|
207 |
} catch (RuntimeException e) { |
|
208 |
System.out.println("maxalpha = "+maxalpha); |
|
209 |
System.out.println("tile["+x0+", "+y0+ |
|
210 |
" => "+x1+", "+y1+"]"); |
|
211 |
System.out.println("cx = "+cx+", cy = "+cy); |
|
212 |
System.out.println("idx = "+idx+", pos = "+pos); |
|
213 |
System.out.println("rx0 = "+rx0+", rx1 = "+rx1); |
|
214 |
System.out.println("len = "+runLen); |
|
6997
3642614e2282
6967434: Round joins/caps of scaled up lines have poor quality.
dlila
parents:
5506
diff
changeset
|
215 |
System.out.print(cache.toString()); |
2 | 216 |
e.printStackTrace(); |
8748
99ac71f8ef92
7019861: Last scanline is skipped in pisces.Renderer.
dlila
parents:
8131
diff
changeset
|
217 |
throw e; |
2 | 218 |
} |
219 |
} |
|
220 |
pos += 2; |
|
221 |
} |
|
222 |
if (cx < x0) { cx = x0; } |
|
223 |
while (cx < x1) { |
|
224 |
tile[idx++] = 0x00; |
|
225 |
cx++; |
|
226 |
} |
|
227 |
/* |
|
228 |
for (int i = idx - (x1-x0); i < idx; i++) { |
|
229 |
System.out.print(hex(tile[i], 2)); |
|
230 |
} |
|
231 |
System.out.println(); |
|
232 |
*/ |
|
233 |
idx += (rowstride - (x1-x0)); |
|
234 |
} |
|
235 |
nextTile(); |
|
236 |
} |
|
237 |
||
238 |
static String hex(int v, int d) { |
|
239 |
String s = Integer.toHexString(v); |
|
240 |
while (s.length() < d) { |
|
241 |
s = "0"+s; |
|
242 |
} |
|
243 |
return s.substring(0, d); |
|
244 |
} |
|
245 |
||
246 |
/** |
|
247 |
* Disposes this tile generator. |
|
248 |
* No further calls will be made on this instance. |
|
249 |
*/ |
|
250 |
public void dispose() {} |
|
8748
99ac71f8ef92
7019861: Last scanline is skipped in pisces.Renderer.
dlila
parents:
8131
diff
changeset
|
251 |
} |
99ac71f8ef92
7019861: Last scanline is skipped in pisces.Renderer.
dlila
parents:
8131
diff
changeset
|
252 |