8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 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 * 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. 24 */ 25 26 package sun.java2d.marlin; 27 28 import java.util.Arrays; 29 import sun.awt.geom.PathConsumer2D; 30 import static sun.java2d.marlin.OffHeapArray.SIZE_INT; 31 import jdk.internal.misc.Unsafe; 32 33 final class Renderer implements PathConsumer2D, MarlinConst { 34 35 static final boolean DISABLE_RENDER = false; 36 37 static final boolean ENABLE_BLOCK_FLAGS = MarlinProperties.isUseTileFlags(); 38 static final boolean ENABLE_BLOCK_FLAGS_HEURISTICS = MarlinProperties.isUseTileFlagsWithHeuristics(); 39 40 private static final int ALL_BUT_LSB = 0xfffffffe; 41 private static final int ERR_STEP_MAX = 0x7fffffff; // = 2^31 - 1 42 43 private static final double POWER_2_TO_32 = 0x1.0p32; 44 45 // use float to make tosubpix methods faster (no int to float conversion) 46 public static final float F_SUBPIXEL_POSITIONS_X 47 = (float) SUBPIXEL_POSITIONS_X; 48 public static final float F_SUBPIXEL_POSITIONS_Y 49 = (float) SUBPIXEL_POSITIONS_Y; 50 public static final int SUBPIXEL_MASK_X = SUBPIXEL_POSITIONS_X - 1; 51 public static final int SUBPIXEL_MASK_Y = SUBPIXEL_POSITIONS_Y - 1; 52 53 // number of subpixels corresponding to a tile line 54 private static final int SUBPIXEL_TILE 55 = TILE_SIZE << SUBPIXEL_LG_POSITIONS_Y; 56 57 // 2048 (pixelSize) pixels (height) x 8 subpixels = 64K 58 static final int INITIAL_BUCKET_ARRAY 59 = INITIAL_PIXEL_DIM * SUBPIXEL_POSITIONS_Y; 60 61 // crossing capacity = edges count / 8 ~ 512 62 static final int INITIAL_CROSSING_COUNT = INITIAL_EDGES_COUNT >> 3; 63 64 public static final int WIND_EVEN_ODD = 0; 65 public static final int WIND_NON_ZERO = 1; 66 67 // common to all types of input path segments. 68 // OFFSET as bytes 69 // only integer values: 70 public static final long OFF_CURX_OR = 0; 71 public static final long OFF_ERROR = OFF_CURX_OR + SIZE_INT; 72 public static final long OFF_BUMP_X = OFF_ERROR + SIZE_INT; 73 public static final long OFF_BUMP_ERR = OFF_BUMP_X + SIZE_INT; 74 public static final long OFF_NEXT = OFF_BUMP_ERR + SIZE_INT; 75 public static final long OFF_YMAX = OFF_NEXT + SIZE_INT; 76 77 // size of one edge in bytes 78 public static final int SIZEOF_EDGE_BYTES = (int)(OFF_YMAX + SIZE_INT); 79 80 // curve break into lines 81 // cubic error in subpixels to decrement step 82 private static final float CUB_DEC_ERR_SUBPIX 83 = 2.5f * (NORM_SUBPIXELS / 8f); // 2.5 subpixel for typical 8x8 subpixels 84 // cubic error in subpixels to increment step 85 private static final float CUB_INC_ERR_SUBPIX 86 = 1f * (NORM_SUBPIXELS / 8f); // 1 subpixel for typical 8x8 subpixels 87 88 // cubic bind length to decrement step = 8 * error in subpixels 89 // pisces: 20 / 8 90 // openjfx pisces: 8 / 3.2 91 // multiply by 8 = error scale factor: 92 public static final float CUB_DEC_BND 93 = 8f * CUB_DEC_ERR_SUBPIX; // 20f means 2.5 subpixel error 94 // cubic bind length to increment step = 8 * error in subpixels 95 public static final float CUB_INC_BND 96 = 8f * CUB_INC_ERR_SUBPIX; // 8f means 1 subpixel error 97 98 // cubic countlg 99 public static final int CUB_COUNT_LG = 2; 100 // cubic count = 2^countlg 101 private static final int CUB_COUNT = 1 << CUB_COUNT_LG; 102 // cubic count^2 = 4^countlg 103 private static final int CUB_COUNT_2 = 1 << (2 * CUB_COUNT_LG); 104 // cubic count^3 = 8^countlg 105 private static final int CUB_COUNT_3 = 1 << (3 * CUB_COUNT_LG); 106 // cubic dt = 1 / count 107 private static final float CUB_INV_COUNT = 1f / CUB_COUNT; 108 // cubic dt^2 = 1 / count^2 = 1 / 4^countlg 109 private static final float CUB_INV_COUNT_2 = 1f / CUB_COUNT_2; 110 // cubic dt^3 = 1 / count^3 = 1 / 8^countlg 111 private static final float CUB_INV_COUNT_3 = 1f / CUB_COUNT_3; 112 113 // quad break into lines 114 // quadratic error in subpixels 115 private static final float QUAD_DEC_ERR_SUBPIX 116 = 1f * (NORM_SUBPIXELS / 8f); // 1 subpixel for typical 8x8 subpixels 117 118 // quadratic bind length to decrement step = 8 * error in subpixels 119 // pisces and openjfx pisces: 32 120 public static final float QUAD_DEC_BND 121 = 8f * QUAD_DEC_ERR_SUBPIX; // 8f means 1 subpixel error 122 123 ////////////////////////////////////////////////////////////////////////////// 124 // SCAN LINE 125 ////////////////////////////////////////////////////////////////////////////// 126 // crossings ie subpixel edge x coordinates 127 private int[] crossings; 128 // auxiliary storage for crossings (merge sort) 129 private int[] aux_crossings; 130 131 // indices into the segment pointer lists. They indicate the "active" 132 // sublist in the segment lists (the portion of the list that contains 133 // all the segments that cross the next scan line). 134 private int edgeCount; 135 private int[] edgePtrs; 136 // auxiliary storage for edge pointers (merge sort) 137 private int[] aux_edgePtrs; 138 139 // max used for both edgePtrs and crossings (stats only) 140 private int activeEdgeMaxUsed; 141 142 // crossings ref (dirty) 143 private final IntArrayCache.Reference crossings_ref; 144 // edgePtrs ref (dirty) 145 private final IntArrayCache.Reference edgePtrs_ref; 146 // merge sort initial arrays (large enough to satisfy most usages) (1024) 147 // aux_crossings ref (dirty) 148 private final IntArrayCache.Reference aux_crossings_ref; 149 // aux_edgePtrs ref (dirty) 150 private final IntArrayCache.Reference aux_edgePtrs_ref; 151 152 ////////////////////////////////////////////////////////////////////////////// 153 // EDGE LIST 154 ////////////////////////////////////////////////////////////////////////////// 155 private int edgeMinY = Integer.MAX_VALUE; 156 private int edgeMaxY = Integer.MIN_VALUE; 157 private float edgeMinX = Float.POSITIVE_INFINITY; 158 private float edgeMaxX = Float.NEGATIVE_INFINITY; 159 160 // edges [floats|ints] stored in off-heap memory 161 private final OffHeapArray edges; 162 163 private int[] edgeBuckets; 164 private int[] edgeBucketCounts; // 2*newedges + (1 if pruning needed) 165 // used range for edgeBuckets / edgeBucketCounts 166 private int buckets_minY; 167 private int buckets_maxY; 168 // sum of each edge delta Y (subpixels) 169 private int edgeSumDeltaY; 170 171 // edgeBuckets ref (clean) 172 private final IntArrayCache.Reference edgeBuckets_ref; 173 // edgeBucketCounts ref (clean) 174 private final IntArrayCache.Reference edgeBucketCounts_ref; 175 176 // Flattens using adaptive forward differencing. This only carries out 177 // one iteration of the AFD loop. All it does is update AFD variables (i.e. 178 // X0, Y0, D*[X|Y], COUNT; not variables used for computing scanline crossings). 179 private void quadBreakIntoLinesAndAdd(float x0, float y0, 180 final Curve c, 181 final float x2, final float y2) 182 { 183 int count = 1; // dt = 1 / count 184 185 // maximum(ddX|Y) = norm(dbx, dby) * dt^2 (= 1) 186 float maxDD = FloatMath.max(Math.abs(c.dbx), Math.abs(c.dby)); 187 188 final float _DEC_BND = QUAD_DEC_BND; 189 190 while (maxDD >= _DEC_BND) { 191 // divide step by half: 192 maxDD /= 4f; // error divided by 2^2 = 4 193 194 count <<= 1; 195 if (DO_STATS) { 196 rdrCtx.stats.stat_rdr_quadBreak_dec.add(count); 197 } 198 } 199 200 int nL = 0; // line count 201 if (count > 1) { 202 final float icount = 1f / count; // dt 203 final float icount2 = icount * icount; // dt^2 204 205 final float ddx = c.dbx * icount2; 206 final float ddy = c.dby * icount2; 207 float dx = c.bx * icount2 + c.cx * icount; 208 float dy = c.by * icount2 + c.cy * icount; 209 210 float x1, y1; 211 212 while (--count > 0) { 213 x1 = x0 + dx; 214 dx += ddx; 215 y1 = y0 + dy; 216 dy += ddy; 217 218 addLine(x0, y0, x1, y1); 219 220 if (DO_STATS) { nL++; } 221 x0 = x1; 222 y0 = y1; 229 } 230 } 231 232 // x0, y0 and x3,y3 are the endpoints of the curve. We could compute these 233 // using c.xat(0),c.yat(0) and c.xat(1),c.yat(1), but this might introduce 234 // numerical errors, and our callers already have the exact values. 235 // Another alternative would be to pass all the control points, and call 236 // c.set here, but then too many numbers are passed around. 237 private void curveBreakIntoLinesAndAdd(float x0, float y0, 238 final Curve c, 239 final float x3, final float y3) 240 { 241 int count = CUB_COUNT; 242 final float icount = CUB_INV_COUNT; // dt 243 final float icount2 = CUB_INV_COUNT_2; // dt^2 244 final float icount3 = CUB_INV_COUNT_3; // dt^3 245 246 // the dx and dy refer to forward differencing variables, not the last 247 // coefficients of the "points" polynomial 248 float dddx, dddy, ddx, ddy, dx, dy; 249 dddx = 2f * c.dax * icount3; 250 dddy = 2f * c.day * icount3; 251 ddx = dddx + c.dbx * icount2; 252 ddy = dddy + c.dby * icount2; 253 dx = c.ax * icount3 + c.bx * icount2 + c.cx * icount; 254 dy = c.ay * icount3 + c.by * icount2 + c.cy * icount; 255 256 // we use x0, y0 to walk the line 257 float x1 = x0, y1 = y0; 258 int nL = 0; // line count 259 260 final float _DEC_BND = CUB_DEC_BND; 261 final float _INC_BND = CUB_INC_BND; 262 263 while (count > 0) { 264 // divide step by half: 265 while (Math.abs(ddx) >= _DEC_BND || Math.abs(ddy) >= _DEC_BND) { 266 dddx /= 8f; 267 dddy /= 8f; 268 ddx = ddx/4f - dddx; 269 ddy = ddy/4f - dddy; 270 dx = (dx - ddx) / 2f; 271 dy = (dy - ddy) / 2f; 272 273 count <<= 1; 274 if (DO_STATS) { 275 rdrCtx.stats.stat_rdr_curveBreak_dec.add(count); 276 } 277 } 278 279 // double step: 280 // TODO: why use first derivative dX|Y instead of second ddX|Y ? 281 // both scale changes should use speed or acceleration to have the same metric. 282 283 // can only do this on even "count" values, because we must divide count by 2 284 while (count % 2 == 0 285 && Math.abs(dx) <= _INC_BND && Math.abs(dy) <= _INC_BND) 286 { 287 dx = 2f * dx + ddx; 288 dy = 2f * dy + ddy; 289 ddx = 4f * (ddx + dddx); 290 ddy = 4f * (ddy + dddy); 291 dddx *= 8f; 292 dddy *= 8f; 293 294 count >>= 1; 295 if (DO_STATS) { 296 rdrCtx.stats.stat_rdr_curveBreak_inc.add(count); 297 } 298 } 299 if (--count > 0) { 300 x1 += dx; 301 dx += ddx; 302 ddx += dddx; 303 y1 += dy; 304 dy += ddy; 305 ddy += dddy; 306 } else { 307 x1 = x3; 308 y1 = y3; 309 } 310 311 addLine(x0, y0, x1, y1); 312 320 } 321 322 private void addLine(float x1, float y1, float x2, float y2) { 323 if (DO_MONITORS) { 324 rdrCtx.stats.mon_rdr_addLine.start(); 325 } 326 if (DO_STATS) { 327 rdrCtx.stats.stat_rdr_addLine.add(1); 328 } 329 int or = 1; // orientation of the line. 1 if y increases, 0 otherwise. 330 if (y2 < y1) { 331 or = 0; 332 float tmp = y2; 333 y2 = y1; 334 y1 = tmp; 335 tmp = x2; 336 x2 = x1; 337 x1 = tmp; 338 } 339 340 // convert subpixel coordinates (float) into pixel positions (int) 341 342 // The index of the pixel that holds the next HPC is at ceil(trueY - 0.5) 343 // Since y1 and y2 are biased by -0.5 in tosubpixy(), this is simply 344 // ceil(y1) or ceil(y2) 345 // upper integer (inclusive) 346 final int firstCrossing = FloatMath.max(FloatMath.ceil_int(y1), boundsMinY); 347 348 // note: use boundsMaxY (last Y exclusive) to compute correct coverage 349 // upper integer (exclusive) 350 final int lastCrossing = FloatMath.min(FloatMath.ceil_int(y2), boundsMaxY); 351 352 /* skip horizontal lines in pixel space and clip edges 353 out of y range [boundsMinY; boundsMaxY] */ 354 if (firstCrossing >= lastCrossing) { 355 if (DO_MONITORS) { 356 rdrCtx.stats.mon_rdr_addLine.stop(); 357 } 358 if (DO_STATS) { 359 rdrCtx.stats.stat_rdr_addLine_skip.add(1); 360 } 361 return; 362 } 363 364 // edge min/max X/Y are in subpixel space (inclusive) within bounds: 365 // note: Use integer crossings to ensure consistent range within 366 // edgeBuckets / edgeBucketCounts arrays in case of NaN values (int = 0) 367 if (firstCrossing < edgeMinY) { 368 edgeMinY = firstCrossing; 369 } 370 if (lastCrossing > edgeMaxY) { 371 edgeMaxY = lastCrossing; 372 } 373 374 // Use double-precision for improved accuracy: 375 final double x1d = x1; 376 final double y1d = y1; 377 final double slope = (x1d - x2) / (y1d - y2); 378 379 if (slope >= 0.0) { // <==> x1 < x2 380 if (x1 < edgeMinX) { 381 edgeMinX = x1; 382 } 383 if (x2 > edgeMaxX) { 384 edgeMaxX = x2; 385 } 386 } else { 387 if (x2 < edgeMinX) { 388 edgeMinX = x2; 389 } 390 if (x1 > edgeMaxX) { 391 edgeMaxX = x1; 392 } 393 } 394 395 // local variables for performance: 396 final int _SIZEOF_EDGE_BYTES = SIZEOF_EDGE_BYTES; 397 398 final OffHeapArray _edges = edges; 399 422 423 // The x value must be bumped up to its position at the next HPC we will evaluate. 424 // "firstcrossing" is the (sub)pixel number where the next crossing occurs 425 // thus, the actual coordinate of the next HPC is "firstcrossing + 0.5" 426 // so the Y distance we cover is "firstcrossing + 0.5 - trueY". 427 // Note that since y1 (and y2) are already biased by -0.5 in tosubpixy(), we have 428 // y1 = trueY - 0.5 429 // trueY = y1 + 0.5 430 // firstcrossing + 0.5 - trueY = firstcrossing + 0.5 - (y1 + 0.5) 431 // = firstcrossing - y1 432 // The x coordinate at that HPC is then: 433 // x1_intercept = x1 + (firstcrossing - y1) * slope 434 // The next VPC is then given by: 435 // VPC index = ceil(x1_intercept - 0.5), or alternately 436 // VPC index = floor(x1_intercept - 0.5 + 1 - epsilon) 437 // epsilon is hard to pin down in floating point, but easy in fixed point, so if 438 // we convert to fixed point then these operations get easier: 439 // long x1_fixed = x1_intercept * 2^32; (fixed point 32.32 format) 440 // curx = next VPC = fixed_floor(x1_fixed - 2^31 + 2^32 - 1) 441 // = fixed_floor(x1_fixed + 2^31 - 1) 442 // = fixed_floor(x1_fixed + 0x7fffffff) 443 // and error = fixed_fract(x1_fixed + 0x7fffffff) 444 final double x1_intercept = x1d + (firstCrossing - y1d) * slope; 445 446 // inlined scalb(x1_intercept, 32): 447 final long x1_fixed_biased = ((long) (POWER_2_TO_32 * x1_intercept)) 448 + 0x7fffffffL; 449 // curx: 450 // last bit corresponds to the orientation 451 _unsafe.putInt(addr, (((int) (x1_fixed_biased >> 31L)) & ALL_BUT_LSB) | or); 452 addr += SIZE_INT; 453 _unsafe.putInt(addr, ((int) x1_fixed_biased) >>> 1); 454 addr += SIZE_INT; 455 456 // inlined scalb(slope, 32): 457 final long slope_fixed = (long) (POWER_2_TO_32 * slope); 458 459 // last bit set to 0 to keep orientation: 460 _unsafe.putInt(addr, (((int) (slope_fixed >> 31L)) & ALL_BUT_LSB)); 461 addr += SIZE_INT; 462 _unsafe.putInt(addr, ((int) slope_fixed) >>> 1); 463 addr += SIZE_INT; 464 465 final int[] _edgeBuckets = edgeBuckets; 466 final int[] _edgeBucketCounts = edgeBucketCounts; 467 468 final int _boundsMinY = boundsMinY; 469 470 // each bucket is a linked list. this method adds ptr to the 471 // start of the "bucket"th linked list. 472 final int bucketIdx = firstCrossing - _boundsMinY; 473 474 // pointer from bucket 475 _unsafe.putInt(addr, _edgeBuckets[bucketIdx]); 476 addr += SIZE_INT; 477 // y max (inclusive) 478 _unsafe.putInt(addr, lastCrossing); 479 480 // Update buckets: 481 // directly the edge struct "pointer" 482 _edgeBuckets[bucketIdx] = edgePtr; 483 _edgeBucketCounts[bucketIdx] += 2; // 1 << 1 484 // last bit means edge end 485 _edgeBucketCounts[lastCrossing - _boundsMinY] |= 0x1; 486 487 // update sum of delta Y (subpixels): 488 edgeSumDeltaY += (lastCrossing - firstCrossing); 489 490 // update free pointer (ie length in bytes) 491 _edges.used += _SIZEOF_EDGE_BYTES; 492 493 if (DO_MONITORS) { 494 rdrCtx.stats.mon_rdr_addLine.stop(); 495 } 496 } 497 498 // END EDGE LIST 499 ////////////////////////////////////////////////////////////////////////////// 500 501 // Cache to store RLE-encoded coverage mask of the current primitive 502 final MarlinCache cache; 503 504 // Bounds of the drawing region, at subpixel precision. 505 private int boundsMinX, boundsMinY, boundsMaxX, boundsMaxY; 506 507 // Current winding rule 508 private int windingRule; 509 551 alphaLine = alphaLine_ref.initial; 552 553 this.cache = rdrCtx.cache; 554 555 crossings_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K 556 aux_crossings_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K 557 edgePtrs_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K 558 aux_edgePtrs_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K 559 560 crossings = crossings_ref.initial; 561 aux_crossings = aux_crossings_ref.initial; 562 edgePtrs = edgePtrs_ref.initial; 563 aux_edgePtrs = aux_edgePtrs_ref.initial; 564 565 blkFlags_ref = rdrCtx.newCleanIntArrayRef(INITIAL_ARRAY); // 1K = 1 tile line 566 blkFlags = blkFlags_ref.initial; 567 } 568 569 Renderer init(final int pix_boundsX, final int pix_boundsY, 570 final int pix_boundsWidth, final int pix_boundsHeight, 571 final int windingRule) { 572 573 this.windingRule = windingRule; 574 575 // bounds as half-open intervals: minX <= x < maxX and minY <= y < maxY 576 this.boundsMinX = pix_boundsX << SUBPIXEL_LG_POSITIONS_X; 577 this.boundsMaxX = 578 (pix_boundsX + pix_boundsWidth) << SUBPIXEL_LG_POSITIONS_X; 579 this.boundsMinY = pix_boundsY << SUBPIXEL_LG_POSITIONS_Y; 580 this.boundsMaxY = 581 (pix_boundsY + pix_boundsHeight) << SUBPIXEL_LG_POSITIONS_Y; 582 583 if (DO_LOG_BOUNDS) { 584 MarlinUtils.logInfo("boundsXY = [" + boundsMinX + " ... " 585 + boundsMaxX + "[ [" + boundsMinY + " ... " 586 + boundsMaxY + "["); 587 } 588 589 // see addLine: ceil(boundsMaxY) => boundsMaxY + 1 590 // +1 for edgeBucketCounts 591 final int edgeBucketsLength = (boundsMaxY - boundsMinY) + 1; 592 594 if (DO_STATS) { 595 rdrCtx.stats.stat_array_renderer_edgeBuckets 596 .add(edgeBucketsLength); 597 rdrCtx.stats.stat_array_renderer_edgeBucketCounts 598 .add(edgeBucketsLength); 599 } 600 edgeBuckets = edgeBuckets_ref.getArray(edgeBucketsLength); 601 edgeBucketCounts = edgeBucketCounts_ref.getArray(edgeBucketsLength); 602 } 603 604 edgeMinY = Integer.MAX_VALUE; 605 edgeMaxY = Integer.MIN_VALUE; 606 edgeMinX = Float.POSITIVE_INFINITY; 607 edgeMaxX = Float.NEGATIVE_INFINITY; 608 609 // reset used mark: 610 edgeCount = 0; 611 activeEdgeMaxUsed = 0; 612 edges.used = 0; 613 614 edgeSumDeltaY = 0; 615 616 return this; // fluent API 617 } 618 619 /** 620 * Disposes this renderer and recycle it clean up before reusing this instance 621 */ 622 void dispose() { 623 if (DO_STATS) { 624 rdrCtx.stats.stat_rdr_activeEdges.add(activeEdgeMaxUsed); 625 rdrCtx.stats.stat_rdr_edges.add(edges.used); 626 rdrCtx.stats.stat_rdr_edges_count.add(edges.used / SIZEOF_EDGE_BYTES); 627 rdrCtx.stats.hist_rdr_edges_count.add(edges.used / SIZEOF_EDGE_BYTES); 628 rdrCtx.stats.totalOffHeap += edges.length; 629 } 630 // Return arrays: 631 crossings = crossings_ref.putArray(crossings); 632 aux_crossings = aux_crossings_ref.putArray(aux_crossings); 633 634 edgePtrs = edgePtrs_ref.putArray(edgePtrs); 635 aux_edgePtrs = aux_edgePtrs_ref.putArray(aux_edgePtrs); 652 buckets_minY, 653 buckets_maxY + 1); 654 } else { 655 // unused arrays 656 edgeBuckets = edgeBuckets_ref.putArray(edgeBuckets, 0, 0); 657 edgeBucketCounts = edgeBucketCounts_ref.putArray(edgeBucketCounts, 0, 0); 658 } 659 660 // At last: resize back off-heap edges to initial size 661 if (edges.length != INITIAL_EDGES_CAPACITY) { 662 // note: may throw OOME: 663 edges.resize(INITIAL_EDGES_CAPACITY); 664 } 665 if (DO_CLEAN_DIRTY) { 666 // Force zero-fill dirty arrays: 667 edges.fill(BYTE_0); 668 } 669 if (DO_MONITORS) { 670 rdrCtx.stats.mon_rdr_endRendering.stop(); 671 } 672 } 673 674 private static float tosubpixx(final float pix_x) { 675 return F_SUBPIXEL_POSITIONS_X * pix_x; 676 } 677 678 private static float tosubpixy(final float pix_y) { 679 // shift y by -0.5 for fast ceil(y - 0.5): 680 return F_SUBPIXEL_POSITIONS_Y * pix_y - 0.5f; 681 } 682 683 @Override 684 public void moveTo(float pix_x0, float pix_y0) { 685 closePath(); 686 final float sx = tosubpixx(pix_x0); 687 final float sy = tosubpixy(pix_y0); 688 this.sx0 = sx; 689 this.sy0 = sy; 690 this.x0 = sx; 691 this.y0 = sy; 692 } 693 694 @Override 695 public void lineTo(float pix_x1, float pix_y1) { 696 final float x1 = tosubpixx(pix_x1); 697 final float y1 = tosubpixy(pix_y1); 698 addLine(x0, y0, x1, y1); 699 x0 = x1; 700 y0 = y1; 701 } 702 703 @Override 704 public void curveTo(float x1, float y1, 705 float x2, float y2, 706 float x3, float y3) 707 { 708 final float xe = tosubpixx(x3); 709 final float ye = tosubpixy(y3); 710 curve.set(x0, y0, tosubpixx(x1), tosubpixy(y1), 711 tosubpixx(x2), tosubpixy(y2), xe, ye); 712 curveBreakIntoLinesAndAdd(x0, y0, curve, xe, ye); 713 x0 = xe; 714 y0 = ye; 715 } 716 717 @Override 718 public void quadTo(float x1, float y1, float x2, float y2) { 719 final float xe = tosubpixx(x2); 720 final float ye = tosubpixy(y2); 721 curve.set(x0, y0, tosubpixx(x1), tosubpixy(y1), xe, ye); 722 quadBreakIntoLinesAndAdd(x0, y0, curve, xe, ye); 723 x0 = xe; 724 y0 = ye; 725 } 726 952 */ 953 if ((ptrLen < 10) || (numCrossings < 40)) { 954 if (DO_STATS) { 955 rdrCtx.stats.hist_rdr_crossings.add(numCrossings); 956 rdrCtx.stats.hist_rdr_crossings_adds.add(ptrLen); 957 } 958 959 /* 960 * threshold to use binary insertion sort instead of 961 * straight insertion sort (to reduce minimize comparisons). 962 */ 963 useBinarySearch = (numCrossings >= 20); 964 965 // if small enough: 966 lastCross = _MIN_VALUE; 967 968 for (i = 0; i < numCrossings; i++) { 969 // get the pointer to the edge 970 ecur = _edgePtrs[i]; 971 972 /* convert subpixel coordinates (float) into pixel 973 positions (int) for coming scanline */ 974 /* note: it is faster to always update edges even 975 if it is removed from AEL for coming or last scanline */ 976 977 // random access so use unsafe: 978 addr = addr0 + ecur; // ecur + OFF_F_CURX 979 980 // get current crossing: 981 curx = _unsafe.getInt(addr); 982 983 // update crossing with orientation at last bit: 984 cross = curx; 985 986 // Increment x using DDA (fixed point): 987 curx += _unsafe.getInt(addr + _OFF_BUMP_X); 988 989 // Increment error: 990 err = _unsafe.getInt(addr + _OFF_ERROR) 991 + _unsafe.getInt(addr + _OFF_BUMP_ERR); 992 993 // Manual carry handling: 1052 } 1053 } else { 1054 if (DO_STATS) { 1055 rdrCtx.stats.stat_rdr_crossings_msorts.add(numCrossings); 1056 rdrCtx.stats.hist_rdr_crossings_ratio 1057 .add((1000 * ptrLen) / numCrossings); 1058 rdrCtx.stats.hist_rdr_crossings_msorts.add(numCrossings); 1059 rdrCtx.stats.hist_rdr_crossings_msorts_adds.add(ptrLen); 1060 } 1061 1062 // Copy sorted data in auxiliary arrays 1063 // and perform insertion sort on almost sorted data 1064 // (ie i < prevNumCrossings): 1065 1066 lastCross = _MIN_VALUE; 1067 1068 for (i = 0; i < numCrossings; i++) { 1069 // get the pointer to the edge 1070 ecur = _edgePtrs[i]; 1071 1072 /* convert subpixel coordinates (float) into pixel 1073 positions (int) for coming scanline */ 1074 /* note: it is faster to always update edges even 1075 if it is removed from AEL for coming or last scanline */ 1076 1077 // random access so use unsafe: 1078 addr = addr0 + ecur; // ecur + OFF_F_CURX 1079 1080 // get current crossing: 1081 curx = _unsafe.getInt(addr); 1082 1083 // update crossing with orientation at last bit: 1084 cross = curx; 1085 1086 // Increment x using DDA (fixed point): 1087 curx += _unsafe.getInt(addr + _OFF_BUMP_X); 1088 1089 // Increment error: 1090 err = _unsafe.getInt(addr + _OFF_ERROR) 1091 + _unsafe.getInt(addr + _OFF_BUMP_ERR); 1092 1093 // Manual carry handling: 1159 prev = curx = x0; 1160 // to turn {0, 1} into {-1, 1}, multiply by 2 and subtract 1. 1161 // last bit contains orientation (0 or 1) 1162 crorientation = ((curxo & 0x1) << 1) - 1; 1163 1164 if (windingRuleEvenOdd) { 1165 sum = crorientation; 1166 1167 // Even Odd winding rule: take care of mask ie sum(orientations) 1168 for (i = 1; i < numCrossings; i++) { 1169 curxo = _crossings[i]; 1170 curx = curxo >> 1; 1171 // to turn {0, 1} into {-1, 1}, multiply by 2 and subtract 1. 1172 // last bit contains orientation (0 or 1) 1173 crorientation = ((curxo & 0x1) << 1) - 1; 1174 1175 if ((sum & 0x1) != 0) { 1176 // TODO: perform line clipping on left-right sides 1177 // to avoid such bound checks: 1178 x0 = (prev > bboxx0) ? prev : bboxx0; 1179 x1 = (curx < bboxx1) ? curx : bboxx1; 1180 1181 if (x0 < x1) { 1182 x0 -= bboxx0; // turn x0, x1 from coords to indices 1183 x1 -= bboxx0; // in the alpha array. 1184 1185 pix_x = x0 >> _SUBPIXEL_LG_POSITIONS_X; 1186 pix_xmaxm1 = (x1 - 1) >> _SUBPIXEL_LG_POSITIONS_X; 1187 1188 if (pix_x == pix_xmaxm1) { 1189 // Start and end in same pixel 1190 tmp = (x1 - x0); // number of subpixels 1191 _alpha[pix_x ] += tmp; 1192 _alpha[pix_x + 1] -= tmp; 1193 1194 if (useBlkFlags) { 1195 // flag used blocks: 1196 _blkFlags[pix_x >> _BLK_SIZE_LG] = 1; 1197 } 1198 } else { 1199 tmp = (x0 & _SUBPIXEL_MASK_X); 1200 _alpha[pix_x ] 1201 += (_SUBPIXEL_POSITIONS_X - tmp); 1202 _alpha[pix_x + 1] 1203 += tmp; 1204 1205 pix_xmax = x1 >> _SUBPIXEL_LG_POSITIONS_X; 1206 1207 tmp = (x1 & _SUBPIXEL_MASK_X); 1208 _alpha[pix_xmax ] 1209 -= (_SUBPIXEL_POSITIONS_X - tmp); 1210 _alpha[pix_xmax + 1] 1211 -= tmp; 1212 1213 if (useBlkFlags) { 1214 // flag used blocks: 1215 _blkFlags[pix_x >> _BLK_SIZE_LG] = 1; 1216 _blkFlags[pix_xmax >> _BLK_SIZE_LG] = 1; 1217 } 1218 } 1219 } 1220 } 1221 1222 sum += crorientation; 1223 prev = curx; 1224 } 1225 } else { 1226 // Non-zero winding rule: optimize that case (default) 1227 // and avoid processing intermediate crossings 1228 for (i = 1, sum = 0;; i++) { 1229 sum += crorientation; 1230 1231 if (sum != 0) { 1232 // prev = min(curx) 1233 if (prev > curx) { 1234 prev = curx; 1235 } 1236 } else { 1237 // TODO: perform line clipping on left-right sides 1238 // to avoid such bound checks: 1239 x0 = (prev > bboxx0) ? prev : bboxx0; 1240 x1 = (curx < bboxx1) ? curx : bboxx1; 1241 1242 if (x0 < x1) { 1243 x0 -= bboxx0; // turn x0, x1 from coords to indices 1244 x1 -= bboxx0; // in the alpha array. 1245 1246 pix_x = x0 >> _SUBPIXEL_LG_POSITIONS_X; 1247 pix_xmaxm1 = (x1 - 1) >> _SUBPIXEL_LG_POSITIONS_X; 1248 1249 if (pix_x == pix_xmaxm1) { 1250 // Start and end in same pixel 1251 tmp = (x1 - x0); // number of subpixels 1252 _alpha[pix_x ] += tmp; 1253 _alpha[pix_x + 1] -= tmp; 1254 1255 if (useBlkFlags) { 1256 // flag used blocks: 1257 _blkFlags[pix_x >> _BLK_SIZE_LG] = 1; 1258 } 1259 } else { 1260 tmp = (x0 & _SUBPIXEL_MASK_X); 1261 _alpha[pix_x ] 1262 += (_SUBPIXEL_POSITIONS_X - tmp); 1263 _alpha[pix_x + 1] 1264 += tmp; 1265 1266 pix_xmax = x1 >> _SUBPIXEL_LG_POSITIONS_X; 1267 1268 tmp = (x1 & _SUBPIXEL_MASK_X); 1269 _alpha[pix_xmax ] 1270 -= (_SUBPIXEL_POSITIONS_X - tmp); 1271 _alpha[pix_xmax + 1] 1272 -= tmp; 1273 1274 if (useBlkFlags) { 1275 // flag used blocks: 1276 _blkFlags[pix_x >> _BLK_SIZE_LG] = 1; 1277 _blkFlags[pix_xmax >> _BLK_SIZE_LG] = 1; 1278 } 1279 } 1280 } 1281 prev = _MAX_VALUE; 1282 } 1283 1284 if (i == numCrossings) { 1285 break; 1286 } 1287 1288 curxo = _crossings[i]; 1289 curx = curxo >> 1; 1290 // to turn {0, 1} into {-1, 1}, multiply by 2 and subtract 1. 1291 // last bit contains orientation (0 or 1) 1292 crorientation = ((curxo & 0x1) << 1) - 1; 1293 } 1294 } 1295 } // numCrossings > 0 1296 1297 // even if this last row had no crossings, alpha will be zeroed 1298 // from the last emitRow call. But this doesn't matter because 1299 // maxX < minX, so no row will be emitted to the MarlinCache. 1300 if ((y & _SUBPIXEL_MASK_Y) == _SUBPIXEL_MASK_Y) { 1301 lastY = y >> _SUBPIXEL_LG_POSITIONS_Y; 1302 1303 // convert subpixel to pixel coordinate within boundaries: 1304 minX = FloatMath.max(minX, bboxx0) >> _SUBPIXEL_LG_POSITIONS_X; 1305 maxX = FloatMath.min(maxX, bboxx1) >> _SUBPIXEL_LG_POSITIONS_X; 1306 1307 if (maxX >= minX) { 1308 // note: alpha array will be zeroed by copyAARow() 1309 // +2 because alpha [pix_minX; pix_maxX+1] 1310 // fix range [x0; x1[ 1311 copyAARow(_alpha, lastY, minX, maxX + 2, useBlkFlags); 1312 1313 // speculative for next pixel row (scanline coherence): 1314 if (_enableBlkFlagsHeuristics) { 1315 // Use block flags if large pixel span and few crossings: 1316 // ie mean(distance between crossings) is larger than 1317 // 1 block size; 1318 1319 // fast check width: 1320 maxX -= minX; 1321 1322 // if stroking: numCrossings /= 2 1323 // => shift numCrossings by 1 1324 // condition = (width / (numCrossings - 1)) > blockSize 1325 useBlkFlags = (maxX > _BLK_SIZE) && (maxX > 1326 (((numCrossings >> stroking) - 1) << _BLK_SIZE_LG)); 1327 1328 if (DO_STATS) { 1329 tmp = FloatMath.max(1, 1330 ((numCrossings >> stroking) - 1)); 1331 rdrCtx.stats.hist_tile_generator_encoding_dist 1333 } 1334 } 1335 } else { 1336 _cache.clearAARow(lastY); 1337 } 1338 minX = _MAX_VALUE; 1339 maxX = _MIN_VALUE; 1340 } 1341 } // scan line iterator 1342 1343 // Emit final row 1344 y--; 1345 y >>= _SUBPIXEL_LG_POSITIONS_Y; 1346 1347 // convert subpixel to pixel coordinate within boundaries: 1348 minX = FloatMath.max(minX, bboxx0) >> _SUBPIXEL_LG_POSITIONS_X; 1349 maxX = FloatMath.min(maxX, bboxx1) >> _SUBPIXEL_LG_POSITIONS_X; 1350 1351 if (maxX >= minX) { 1352 // note: alpha array will be zeroed by copyAARow() 1353 // +2 because alpha [pix_minX; pix_maxX+1] 1354 // fix range [x0; x1[ 1355 copyAARow(_alpha, y, minX, maxX + 2, useBlkFlags); 1356 } else if (y != lastY) { 1357 _cache.clearAARow(y); 1358 } 1359 1360 // update member: 1361 edgeCount = numCrossings; 1362 prevUseBlkFlags = useBlkFlags; 1363 1364 if (DO_STATS) { 1365 // update max used mark 1366 activeEdgeMaxUsed = _arrayMaxUsed; 1367 } 1368 } 1369 1370 boolean endRendering() { 1371 if (DO_MONITORS) { 1372 rdrCtx.stats.mon_rdr_endRendering.start(); 1373 } 1374 if (edgeMinY == Integer.MAX_VALUE) { 1375 return false; // undefined edges bounds 1376 } 1377 1378 final int _boundsMinY = boundsMinY; 1379 final int _boundsMaxY = boundsMaxY; 1380 1381 // bounds as inclusive intervals 1382 final int spminX = FloatMath.max(FloatMath.ceil_int(edgeMinX - 0.5f), boundsMinX); 1383 final int spmaxX = FloatMath.min(FloatMath.ceil_int(edgeMaxX - 0.5f), boundsMaxX - 1); 1384 1385 // edge Min/Max Y are already rounded to subpixels within bounds: 1386 final int spminY = edgeMinY; 1387 final int spmaxY; 1388 int maxY = edgeMaxY; 1389 1390 if (maxY <= _boundsMaxY - 1) { 1391 spmaxY = maxY; 1392 } else { 1393 spmaxY = _boundsMaxY - 1; 1394 maxY = _boundsMaxY; 1395 } 1396 buckets_minY = spminY - _boundsMinY; 1397 buckets_maxY = maxY - _boundsMinY; 1398 1399 if (DO_LOG_BOUNDS) { 1400 MarlinUtils.logInfo("edgesXY = [" + edgeMinX + " ... " + edgeMaxX 1401 + "][" + edgeMinY + " ... " + edgeMaxY + "]"); 1402 MarlinUtils.logInfo("spXY = [" + spminX + " ... " + spmaxX 1403 + "][" + spminY + " ... " + spmaxY + "]"); 1404 } 1405 1406 // test clipping for shapes out of bounds 1407 if ((spminX > spmaxX) || (spminY > spmaxY)) { 1408 return false; 1409 } 1410 1411 // half open intervals 1412 // inclusive: 1413 final int pminX = spminX >> SUBPIXEL_LG_POSITIONS_X; 1414 // exclusive: 1415 final int pmaxX = (spmaxX + SUBPIXEL_MASK_X) >> SUBPIXEL_LG_POSITIONS_X; 1416 // inclusive: 1417 final int pminY = spminY >> SUBPIXEL_LG_POSITIONS_Y; 1418 // exclusive: 1419 final int pmaxY = (spmaxY + SUBPIXEL_MASK_Y) >> SUBPIXEL_LG_POSITIONS_Y; 1420 1421 // store BBox to answer ptg.getBBox(): 1422 this.cache.init(pminX, pminY, pmaxX, pmaxY, edgeSumDeltaY); 1423 1424 // Heuristics for using block flags: 1425 if (ENABLE_BLOCK_FLAGS) { 1426 enableBlkFlags = this.cache.useRLE; 1427 prevUseBlkFlags = enableBlkFlags && !ENABLE_BLOCK_FLAGS_HEURISTICS; 1428 1429 if (enableBlkFlags) { 1430 // ensure blockFlags array is large enough: 1431 // note: +2 to ensure enough space left at end 1432 final int nxTiles = ((pmaxX - pminX) >> TILE_SIZE_LG) + 2; 1433 if (nxTiles > INITIAL_ARRAY) { 1434 blkFlags = blkFlags_ref.getArray(nxTiles); 1435 } 1436 } 1437 } 1438 1439 // memorize the rendering bounding box: 1440 /* note: bbox_spminX and bbox_spmaxX must be pixel boundaries 1441 to have correct coverage computation */ 1442 // inclusive: 1443 bbox_spminX = pminX << SUBPIXEL_LG_POSITIONS_X; 1444 // exclusive: 1445 bbox_spmaxX = pmaxX << SUBPIXEL_LG_POSITIONS_X; 1446 // inclusive: 1447 bbox_spminY = spminY; 1448 // exclusive: 1449 bbox_spmaxY = FloatMath.min(spmaxY + 1, pmaxY << SUBPIXEL_LG_POSITIONS_Y); 1450 1451 if (DO_LOG_BOUNDS) { 1452 MarlinUtils.logInfo("pXY = [" + pminX + " ... " + pmaxX 1453 + "[ [" + pminY + " ... " + pmaxY + "["); 1454 MarlinUtils.logInfo("bbox_spXY = [" + bbox_spminX + " ... " 1455 + bbox_spmaxX + "[ [" + bbox_spminY + " ... " 1456 + bbox_spmaxY + "["); 1457 } 1458 1459 // Prepare alpha line: 1460 // add 2 to better deal with the last pixel in a pixel row. 1461 final int width = (pmaxX - pminX) + 2; 1462 1463 // Useful when processing tile line by tile line 1464 if (width > INITIAL_AA_ARRAY) { 1465 if (DO_STATS) { 1466 rdrCtx.stats.stat_array_renderer_alphaline.add(width); 1467 } 1468 alphaLine = alphaLine_ref.getArray(width); 1469 } 1487 // avoid rendering for last call to nextTile() 1488 if (fixed_spminY < bbox_spmaxY) { 1489 // process a complete tile line ie scanlines for 32 rows 1490 final int spmaxY = FloatMath.min(bbox_spmaxY, spminY + SUBPIXEL_TILE); 1491 1492 // process tile line [0 - 32] 1493 cache.resetTileLine(pminY); 1494 1495 // Process only one tile line: 1496 _endRendering(fixed_spminY, spmaxY); 1497 } 1498 if (DO_MONITORS) { 1499 rdrCtx.stats.mon_rdr_endRendering_Y.stop(); 1500 } 1501 } 1502 1503 void copyAARow(final int[] alphaRow, 1504 final int pix_y, final int pix_from, final int pix_to, 1505 final boolean useBlockFlags) 1506 { 1507 if (useBlockFlags) { 1508 if (DO_STATS) { 1509 rdrCtx.stats.hist_tile_generator_encoding.add(1); 1510 } 1511 cache.copyAARowRLE_WithBlockFlags(blkFlags, alphaRow, pix_y, pix_from, pix_to); 1512 } else { 1513 if (DO_STATS) { 1514 rdrCtx.stats.hist_tile_generator_encoding.add(0); 1515 } 1516 cache.copyAARowNoRLE(alphaRow, pix_y, pix_from, pix_to); 1517 } 1518 } 1519 } | 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 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 * 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. 24 */ 25 26 package sun.java2d.marlin; 27 28 import sun.awt.geom.PathConsumer2D; 29 import static sun.java2d.marlin.OffHeapArray.SIZE_INT; 30 import jdk.internal.misc.Unsafe; 31 32 final class Renderer implements PathConsumer2D, MarlinRenderer { 33 34 static final boolean DISABLE_RENDER = false; 35 36 static final boolean ENABLE_BLOCK_FLAGS = MarlinProperties.isUseTileFlags(); 37 static final boolean ENABLE_BLOCK_FLAGS_HEURISTICS = MarlinProperties.isUseTileFlagsWithHeuristics(); 38 39 private static final int ALL_BUT_LSB = 0xFFFFFFFE; 40 private static final int ERR_STEP_MAX = 0x7FFFFFFF; // = 2^31 - 1 41 42 private static final double POWER_2_TO_32 = 0x1.0p32; 43 44 // use float to make tosubpix methods faster (no int to float conversion) 45 static final float SUBPIXEL_SCALE_X = (float) SUBPIXEL_POSITIONS_X; 46 static final float SUBPIXEL_SCALE_Y = (float) SUBPIXEL_POSITIONS_Y; 47 static final int SUBPIXEL_MASK_X = SUBPIXEL_POSITIONS_X - 1; 48 static final int SUBPIXEL_MASK_Y = SUBPIXEL_POSITIONS_Y - 1; 49 50 // number of subpixels corresponding to a tile line 51 private static final int SUBPIXEL_TILE 52 = TILE_H << SUBPIXEL_LG_POSITIONS_Y; 53 54 // 2048 (pixelSize) pixels (height) x 8 subpixels = 64K 55 static final int INITIAL_BUCKET_ARRAY 56 = INITIAL_PIXEL_DIM * SUBPIXEL_POSITIONS_Y; 57 58 // crossing capacity = edges count / 4 ~ 1024 59 static final int INITIAL_CROSSING_COUNT = INITIAL_EDGES_COUNT >> 2; 60 61 public static final int WIND_EVEN_ODD = 0; 62 public static final int WIND_NON_ZERO = 1; 63 64 // common to all types of input path segments. 65 // OFFSET as bytes 66 // only integer values: 67 public static final long OFF_CURX_OR = 0; 68 public static final long OFF_ERROR = OFF_CURX_OR + SIZE_INT; 69 public static final long OFF_BUMP_X = OFF_ERROR + SIZE_INT; 70 public static final long OFF_BUMP_ERR = OFF_BUMP_X + SIZE_INT; 71 public static final long OFF_NEXT = OFF_BUMP_ERR + SIZE_INT; 72 public static final long OFF_YMAX = OFF_NEXT + SIZE_INT; 73 74 // size of one edge in bytes 75 public static final int SIZEOF_EDGE_BYTES = (int)(OFF_YMAX + SIZE_INT); 76 77 // curve break into lines 78 // cubic error in subpixels to decrement step 79 private static final float CUB_DEC_ERR_SUBPIX 80 = MarlinProperties.getCubicDecD2() * (NORM_SUBPIXELS / 8.0f); // 1 pixel 81 // cubic error in subpixels to increment step 82 private static final float CUB_INC_ERR_SUBPIX 83 = MarlinProperties.getCubicIncD1() * (NORM_SUBPIXELS / 8.0f); // 0.4 pixel 84 85 // bad paths (59294/100000 == 59,29%, 94335 bad pixels (avg = 1,59), 3966 warnings (avg = 0,07) 86 87 // cubic bind length to decrement step 88 public static final float CUB_DEC_BND 89 = 8.0f * CUB_DEC_ERR_SUBPIX; 90 // cubic bind length to increment step 91 public static final float CUB_INC_BND 92 = 8.0f * CUB_INC_ERR_SUBPIX; 93 94 // cubic countlg 95 public static final int CUB_COUNT_LG = 2; 96 // cubic count = 2^countlg 97 private static final int CUB_COUNT = 1 << CUB_COUNT_LG; 98 // cubic count^2 = 4^countlg 99 private static final int CUB_COUNT_2 = 1 << (2 * CUB_COUNT_LG); 100 // cubic count^3 = 8^countlg 101 private static final int CUB_COUNT_3 = 1 << (3 * CUB_COUNT_LG); 102 // cubic dt = 1 / count 103 private static final float CUB_INV_COUNT = 1.0f / CUB_COUNT; 104 // cubic dt^2 = 1 / count^2 = 1 / 4^countlg 105 private static final float CUB_INV_COUNT_2 = 1.0f / CUB_COUNT_2; 106 // cubic dt^3 = 1 / count^3 = 1 / 8^countlg 107 private static final float CUB_INV_COUNT_3 = 1.0f / CUB_COUNT_3; 108 109 // quad break into lines 110 // quadratic error in subpixels 111 private static final float QUAD_DEC_ERR_SUBPIX 112 = MarlinProperties.getQuadDecD2() * (NORM_SUBPIXELS / 8.0f); // 0.5 pixel 113 114 // bad paths (62916/100000 == 62,92%, 103818 bad pixels (avg = 1,65), 6514 warnings (avg = 0,10) 115 116 // quadratic bind length to decrement step 117 public static final float QUAD_DEC_BND 118 = 8.0f * QUAD_DEC_ERR_SUBPIX; 119 120 ////////////////////////////////////////////////////////////////////////////// 121 // SCAN LINE 122 ////////////////////////////////////////////////////////////////////////////// 123 // crossings ie subpixel edge x coordinates 124 private int[] crossings; 125 // auxiliary storage for crossings (merge sort) 126 private int[] aux_crossings; 127 128 // indices into the segment pointer lists. They indicate the "active" 129 // sublist in the segment lists (the portion of the list that contains 130 // all the segments that cross the next scan line). 131 private int edgeCount; 132 private int[] edgePtrs; 133 // auxiliary storage for edge pointers (merge sort) 134 private int[] aux_edgePtrs; 135 136 // max used for both edgePtrs and crossings (stats only) 137 private int activeEdgeMaxUsed; 138 139 // crossings ref (dirty) 140 private final IntArrayCache.Reference crossings_ref; 141 // edgePtrs ref (dirty) 142 private final IntArrayCache.Reference edgePtrs_ref; 143 // merge sort initial arrays (large enough to satisfy most usages) (1024) 144 // aux_crossings ref (dirty) 145 private final IntArrayCache.Reference aux_crossings_ref; 146 // aux_edgePtrs ref (dirty) 147 private final IntArrayCache.Reference aux_edgePtrs_ref; 148 149 ////////////////////////////////////////////////////////////////////////////// 150 // EDGE LIST 151 ////////////////////////////////////////////////////////////////////////////// 152 private int edgeMinY = Integer.MAX_VALUE; 153 private int edgeMaxY = Integer.MIN_VALUE; 154 private float edgeMinX = Float.POSITIVE_INFINITY; 155 private float edgeMaxX = Float.NEGATIVE_INFINITY; 156 157 // edges [ints] stored in off-heap memory 158 private final OffHeapArray edges; 159 160 private int[] edgeBuckets; 161 private int[] edgeBucketCounts; // 2*newedges + (1 if pruning needed) 162 // used range for edgeBuckets / edgeBucketCounts 163 private int buckets_minY; 164 private int buckets_maxY; 165 166 // edgeBuckets ref (clean) 167 private final IntArrayCache.Reference edgeBuckets_ref; 168 // edgeBucketCounts ref (clean) 169 private final IntArrayCache.Reference edgeBucketCounts_ref; 170 171 // Flattens using adaptive forward differencing. This only carries out 172 // one iteration of the AFD loop. All it does is update AFD variables (i.e. 173 // X0, Y0, D*[X|Y], COUNT; not variables used for computing scanline crossings). 174 private void quadBreakIntoLinesAndAdd(float x0, float y0, 175 final Curve c, 176 final float x2, final float y2) 177 { 178 int count = 1; // dt = 1 / count 179 180 // maximum(ddX|Y) = norm(dbx, dby) * dt^2 (= 1) 181 float maxDD = Math.abs(c.dbx) + Math.abs(c.dby); 182 183 final float _DEC_BND = QUAD_DEC_BND; 184 185 while (maxDD >= _DEC_BND) { 186 // divide step by half: 187 maxDD /= 4.0f; // error divided by 2^2 = 4 188 189 count <<= 1; 190 if (DO_STATS) { 191 rdrCtx.stats.stat_rdr_quadBreak_dec.add(count); 192 } 193 } 194 195 int nL = 0; // line count 196 if (count > 1) { 197 final float icount = 1.0f / count; // dt 198 final float icount2 = icount * icount; // dt^2 199 200 final float ddx = c.dbx * icount2; 201 final float ddy = c.dby * icount2; 202 float dx = c.bx * icount2 + c.cx * icount; 203 float dy = c.by * icount2 + c.cy * icount; 204 205 float x1, y1; 206 207 while (--count > 0) { 208 x1 = x0 + dx; 209 dx += ddx; 210 y1 = y0 + dy; 211 dy += ddy; 212 213 addLine(x0, y0, x1, y1); 214 215 if (DO_STATS) { nL++; } 216 x0 = x1; 217 y0 = y1; 224 } 225 } 226 227 // x0, y0 and x3,y3 are the endpoints of the curve. We could compute these 228 // using c.xat(0),c.yat(0) and c.xat(1),c.yat(1), but this might introduce 229 // numerical errors, and our callers already have the exact values. 230 // Another alternative would be to pass all the control points, and call 231 // c.set here, but then too many numbers are passed around. 232 private void curveBreakIntoLinesAndAdd(float x0, float y0, 233 final Curve c, 234 final float x3, final float y3) 235 { 236 int count = CUB_COUNT; 237 final float icount = CUB_INV_COUNT; // dt 238 final float icount2 = CUB_INV_COUNT_2; // dt^2 239 final float icount3 = CUB_INV_COUNT_3; // dt^3 240 241 // the dx and dy refer to forward differencing variables, not the last 242 // coefficients of the "points" polynomial 243 float dddx, dddy, ddx, ddy, dx, dy; 244 dddx = 2.0f * c.dax * icount3; 245 dddy = 2.0f * c.day * icount3; 246 ddx = dddx + c.dbx * icount2; 247 ddy = dddy + c.dby * icount2; 248 dx = c.ax * icount3 + c.bx * icount2 + c.cx * icount; 249 dy = c.ay * icount3 + c.by * icount2 + c.cy * icount; 250 251 // we use x0, y0 to walk the line 252 float x1 = x0, y1 = y0; 253 int nL = 0; // line count 254 255 final float _DEC_BND = CUB_DEC_BND; 256 final float _INC_BND = CUB_INC_BND; 257 258 while (count > 0) { 259 // divide step by half: 260 while (Math.abs(ddx) + Math.abs(ddy) >= _DEC_BND) { 261 dddx /= 8.0f; 262 dddy /= 8.0f; 263 ddx = ddx / 4.0f - dddx; 264 ddy = ddy / 4.0f - dddy; 265 dx = (dx - ddx) / 2.0f; 266 dy = (dy - ddy) / 2.0f; 267 268 count <<= 1; 269 if (DO_STATS) { 270 rdrCtx.stats.stat_rdr_curveBreak_dec.add(count); 271 } 272 } 273 274 // double step: 275 // can only do this on even "count" values, because we must divide count by 2 276 while (count % 2 == 0 277 && Math.abs(dx) + Math.abs(dy) <= _INC_BND) 278 { 279 dx = 2.0f * dx + ddx; 280 dy = 2.0f * dy + ddy; 281 ddx = 4.0f * (ddx + dddx); 282 ddy = 4.0f * (ddy + dddy); 283 dddx *= 8.0f; 284 dddy *= 8.0f; 285 286 count >>= 1; 287 if (DO_STATS) { 288 rdrCtx.stats.stat_rdr_curveBreak_inc.add(count); 289 } 290 } 291 if (--count > 0) { 292 x1 += dx; 293 dx += ddx; 294 ddx += dddx; 295 y1 += dy; 296 dy += ddy; 297 ddy += dddy; 298 } else { 299 x1 = x3; 300 y1 = y3; 301 } 302 303 addLine(x0, y0, x1, y1); 304 312 } 313 314 private void addLine(float x1, float y1, float x2, float y2) { 315 if (DO_MONITORS) { 316 rdrCtx.stats.mon_rdr_addLine.start(); 317 } 318 if (DO_STATS) { 319 rdrCtx.stats.stat_rdr_addLine.add(1); 320 } 321 int or = 1; // orientation of the line. 1 if y increases, 0 otherwise. 322 if (y2 < y1) { 323 or = 0; 324 float tmp = y2; 325 y2 = y1; 326 y1 = tmp; 327 tmp = x2; 328 x2 = x1; 329 x1 = tmp; 330 } 331 332 // convert subpixel coordinates [float] into pixel positions [int] 333 334 // The index of the pixel that holds the next HPC is at ceil(trueY - 0.5) 335 // Since y1 and y2 are biased by -0.5 in tosubpixy(), this is simply 336 // ceil(y1) or ceil(y2) 337 // upper integer (inclusive) 338 final int firstCrossing = FloatMath.max(FloatMath.ceil_int(y1), boundsMinY); 339 340 // note: use boundsMaxY (last Y exclusive) to compute correct coverage 341 // upper integer (exclusive) 342 final int lastCrossing = FloatMath.min(FloatMath.ceil_int(y2), boundsMaxY); 343 344 /* skip horizontal lines in pixel space and clip edges 345 out of y range [boundsMinY; boundsMaxY] */ 346 if (firstCrossing >= lastCrossing) { 347 if (DO_MONITORS) { 348 rdrCtx.stats.mon_rdr_addLine.stop(); 349 } 350 if (DO_STATS) { 351 rdrCtx.stats.stat_rdr_addLine_skip.add(1); 352 } 353 return; 354 } 355 356 // edge min/max X/Y are in subpixel space (inclusive) within bounds: 357 // note: Use integer crossings to ensure consistent range within 358 // edgeBuckets / edgeBucketCounts arrays in case of NaN values (int = 0) 359 if (firstCrossing < edgeMinY) { 360 edgeMinY = firstCrossing; 361 } 362 if (lastCrossing > edgeMaxY) { 363 edgeMaxY = lastCrossing; 364 } 365 366 // Use double-precision for improved accuracy: 367 final double x1d = x1; 368 final double y1d = y1; 369 final double slope = (x1d - x2) / (y1d - y2); 370 371 if (slope >= 0.0d) { // <==> x1 < x2 372 if (x1 < edgeMinX) { 373 edgeMinX = x1; 374 } 375 if (x2 > edgeMaxX) { 376 edgeMaxX = x2; 377 } 378 } else { 379 if (x2 < edgeMinX) { 380 edgeMinX = x2; 381 } 382 if (x1 > edgeMaxX) { 383 edgeMaxX = x1; 384 } 385 } 386 387 // local variables for performance: 388 final int _SIZEOF_EDGE_BYTES = SIZEOF_EDGE_BYTES; 389 390 final OffHeapArray _edges = edges; 391 414 415 // The x value must be bumped up to its position at the next HPC we will evaluate. 416 // "firstcrossing" is the (sub)pixel number where the next crossing occurs 417 // thus, the actual coordinate of the next HPC is "firstcrossing + 0.5" 418 // so the Y distance we cover is "firstcrossing + 0.5 - trueY". 419 // Note that since y1 (and y2) are already biased by -0.5 in tosubpixy(), we have 420 // y1 = trueY - 0.5 421 // trueY = y1 + 0.5 422 // firstcrossing + 0.5 - trueY = firstcrossing + 0.5 - (y1 + 0.5) 423 // = firstcrossing - y1 424 // The x coordinate at that HPC is then: 425 // x1_intercept = x1 + (firstcrossing - y1) * slope 426 // The next VPC is then given by: 427 // VPC index = ceil(x1_intercept - 0.5), or alternately 428 // VPC index = floor(x1_intercept - 0.5 + 1 - epsilon) 429 // epsilon is hard to pin down in floating point, but easy in fixed point, so if 430 // we convert to fixed point then these operations get easier: 431 // long x1_fixed = x1_intercept * 2^32; (fixed point 32.32 format) 432 // curx = next VPC = fixed_floor(x1_fixed - 2^31 + 2^32 - 1) 433 // = fixed_floor(x1_fixed + 2^31 - 1) 434 // = fixed_floor(x1_fixed + 0x7FFFFFFF) 435 // and error = fixed_fract(x1_fixed + 0x7FFFFFFF) 436 final double x1_intercept = x1d + (firstCrossing - y1d) * slope; 437 438 // inlined scalb(x1_intercept, 32): 439 final long x1_fixed_biased = ((long) (POWER_2_TO_32 * x1_intercept)) 440 + 0x7FFFFFFFL; 441 // curx: 442 // last bit corresponds to the orientation 443 _unsafe.putInt(addr, (((int) (x1_fixed_biased >> 31L)) & ALL_BUT_LSB) | or); 444 addr += SIZE_INT; 445 _unsafe.putInt(addr, ((int) x1_fixed_biased) >>> 1); 446 addr += SIZE_INT; 447 448 // inlined scalb(slope, 32): 449 final long slope_fixed = (long) (POWER_2_TO_32 * slope); 450 451 // last bit set to 0 to keep orientation: 452 _unsafe.putInt(addr, (((int) (slope_fixed >> 31L)) & ALL_BUT_LSB)); 453 addr += SIZE_INT; 454 _unsafe.putInt(addr, ((int) slope_fixed) >>> 1); 455 addr += SIZE_INT; 456 457 final int[] _edgeBuckets = edgeBuckets; 458 final int[] _edgeBucketCounts = edgeBucketCounts; 459 460 final int _boundsMinY = boundsMinY; 461 462 // each bucket is a linked list. this method adds ptr to the 463 // start of the "bucket"th linked list. 464 final int bucketIdx = firstCrossing - _boundsMinY; 465 466 // pointer from bucket 467 _unsafe.putInt(addr, _edgeBuckets[bucketIdx]); 468 addr += SIZE_INT; 469 // y max (inclusive) 470 _unsafe.putInt(addr, lastCrossing); 471 472 // Update buckets: 473 // directly the edge struct "pointer" 474 _edgeBuckets[bucketIdx] = edgePtr; 475 _edgeBucketCounts[bucketIdx] += 2; // 1 << 1 476 // last bit means edge end 477 _edgeBucketCounts[lastCrossing - _boundsMinY] |= 0x1; 478 479 // update free pointer (ie length in bytes) 480 _edges.used += _SIZEOF_EDGE_BYTES; 481 482 if (DO_MONITORS) { 483 rdrCtx.stats.mon_rdr_addLine.stop(); 484 } 485 } 486 487 // END EDGE LIST 488 ////////////////////////////////////////////////////////////////////////////// 489 490 // Cache to store RLE-encoded coverage mask of the current primitive 491 final MarlinCache cache; 492 493 // Bounds of the drawing region, at subpixel precision. 494 private int boundsMinX, boundsMinY, boundsMaxX, boundsMaxY; 495 496 // Current winding rule 497 private int windingRule; 498 540 alphaLine = alphaLine_ref.initial; 541 542 this.cache = rdrCtx.cache; 543 544 crossings_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K 545 aux_crossings_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K 546 edgePtrs_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K 547 aux_edgePtrs_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K 548 549 crossings = crossings_ref.initial; 550 aux_crossings = aux_crossings_ref.initial; 551 edgePtrs = edgePtrs_ref.initial; 552 aux_edgePtrs = aux_edgePtrs_ref.initial; 553 554 blkFlags_ref = rdrCtx.newCleanIntArrayRef(INITIAL_ARRAY); // 1K = 1 tile line 555 blkFlags = blkFlags_ref.initial; 556 } 557 558 Renderer init(final int pix_boundsX, final int pix_boundsY, 559 final int pix_boundsWidth, final int pix_boundsHeight, 560 final int windingRule) 561 { 562 this.windingRule = windingRule; 563 564 // bounds as half-open intervals: minX <= x < maxX and minY <= y < maxY 565 this.boundsMinX = pix_boundsX << SUBPIXEL_LG_POSITIONS_X; 566 this.boundsMaxX = 567 (pix_boundsX + pix_boundsWidth) << SUBPIXEL_LG_POSITIONS_X; 568 this.boundsMinY = pix_boundsY << SUBPIXEL_LG_POSITIONS_Y; 569 this.boundsMaxY = 570 (pix_boundsY + pix_boundsHeight) << SUBPIXEL_LG_POSITIONS_Y; 571 572 if (DO_LOG_BOUNDS) { 573 MarlinUtils.logInfo("boundsXY = [" + boundsMinX + " ... " 574 + boundsMaxX + "[ [" + boundsMinY + " ... " 575 + boundsMaxY + "["); 576 } 577 578 // see addLine: ceil(boundsMaxY) => boundsMaxY + 1 579 // +1 for edgeBucketCounts 580 final int edgeBucketsLength = (boundsMaxY - boundsMinY) + 1; 581 583 if (DO_STATS) { 584 rdrCtx.stats.stat_array_renderer_edgeBuckets 585 .add(edgeBucketsLength); 586 rdrCtx.stats.stat_array_renderer_edgeBucketCounts 587 .add(edgeBucketsLength); 588 } 589 edgeBuckets = edgeBuckets_ref.getArray(edgeBucketsLength); 590 edgeBucketCounts = edgeBucketCounts_ref.getArray(edgeBucketsLength); 591 } 592 593 edgeMinY = Integer.MAX_VALUE; 594 edgeMaxY = Integer.MIN_VALUE; 595 edgeMinX = Float.POSITIVE_INFINITY; 596 edgeMaxX = Float.NEGATIVE_INFINITY; 597 598 // reset used mark: 599 edgeCount = 0; 600 activeEdgeMaxUsed = 0; 601 edges.used = 0; 602 603 return this; // fluent API 604 } 605 606 /** 607 * Disposes this renderer and recycle it clean up before reusing this instance 608 */ 609 void dispose() { 610 if (DO_STATS) { 611 rdrCtx.stats.stat_rdr_activeEdges.add(activeEdgeMaxUsed); 612 rdrCtx.stats.stat_rdr_edges.add(edges.used); 613 rdrCtx.stats.stat_rdr_edges_count.add(edges.used / SIZEOF_EDGE_BYTES); 614 rdrCtx.stats.hist_rdr_edges_count.add(edges.used / SIZEOF_EDGE_BYTES); 615 rdrCtx.stats.totalOffHeap += edges.length; 616 } 617 // Return arrays: 618 crossings = crossings_ref.putArray(crossings); 619 aux_crossings = aux_crossings_ref.putArray(aux_crossings); 620 621 edgePtrs = edgePtrs_ref.putArray(edgePtrs); 622 aux_edgePtrs = aux_edgePtrs_ref.putArray(aux_edgePtrs); 639 buckets_minY, 640 buckets_maxY + 1); 641 } else { 642 // unused arrays 643 edgeBuckets = edgeBuckets_ref.putArray(edgeBuckets, 0, 0); 644 edgeBucketCounts = edgeBucketCounts_ref.putArray(edgeBucketCounts, 0, 0); 645 } 646 647 // At last: resize back off-heap edges to initial size 648 if (edges.length != INITIAL_EDGES_CAPACITY) { 649 // note: may throw OOME: 650 edges.resize(INITIAL_EDGES_CAPACITY); 651 } 652 if (DO_CLEAN_DIRTY) { 653 // Force zero-fill dirty arrays: 654 edges.fill(BYTE_0); 655 } 656 if (DO_MONITORS) { 657 rdrCtx.stats.mon_rdr_endRendering.stop(); 658 } 659 // recycle the RendererContext instance 660 MarlinRenderingEngine.returnRendererContext(rdrCtx); 661 } 662 663 private static float tosubpixx(final float pix_x) { 664 return SUBPIXEL_SCALE_X * pix_x; 665 } 666 667 private static float tosubpixy(final float pix_y) { 668 // shift y by -0.5 for fast ceil(y - 0.5): 669 return SUBPIXEL_SCALE_Y * pix_y - 0.5f; 670 } 671 672 @Override 673 public void moveTo(float pix_x0, float pix_y0) { 674 closePath(); 675 final float sx = tosubpixx(pix_x0); 676 final float sy = tosubpixy(pix_y0); 677 this.sx0 = sx; 678 this.sy0 = sy; 679 this.x0 = sx; 680 this.y0 = sy; 681 } 682 683 @Override 684 public void lineTo(float pix_x1, float pix_y1) { 685 final float x1 = tosubpixx(pix_x1); 686 final float y1 = tosubpixy(pix_y1); 687 addLine(x0, y0, x1, y1); 688 x0 = x1; 689 y0 = y1; 690 } 691 692 @Override 693 public void curveTo(float x1, float y1, 694 float x2, float y2, 695 float x3, float y3) 696 { 697 final float xe = tosubpixx(x3); 698 final float ye = tosubpixy(y3); 699 curve.set(x0, y0, tosubpixx(x1), tosubpixy(y1), 700 tosubpixx(x2), tosubpixy(y2), xe, ye); 701 curveBreakIntoLinesAndAdd(x0, y0, curve, xe, ye); 702 x0 = xe; 703 y0 = ye; 704 } 705 706 @Override 707 public void quadTo(float x1, float y1, float x2, float y2) { 708 final float xe = tosubpixx(x2); 709 final float ye = tosubpixy(y2); 710 curve.set(x0, y0, tosubpixx(x1), tosubpixy(y1), xe, ye); 711 quadBreakIntoLinesAndAdd(x0, y0, curve, xe, ye); 712 x0 = xe; 713 y0 = ye; 714 } 715 941 */ 942 if ((ptrLen < 10) || (numCrossings < 40)) { 943 if (DO_STATS) { 944 rdrCtx.stats.hist_rdr_crossings.add(numCrossings); 945 rdrCtx.stats.hist_rdr_crossings_adds.add(ptrLen); 946 } 947 948 /* 949 * threshold to use binary insertion sort instead of 950 * straight insertion sort (to reduce minimize comparisons). 951 */ 952 useBinarySearch = (numCrossings >= 20); 953 954 // if small enough: 955 lastCross = _MIN_VALUE; 956 957 for (i = 0; i < numCrossings; i++) { 958 // get the pointer to the edge 959 ecur = _edgePtrs[i]; 960 961 /* convert subpixel coordinates into pixel 962 positions for coming scanline */ 963 /* note: it is faster to always update edges even 964 if it is removed from AEL for coming or last scanline */ 965 966 // random access so use unsafe: 967 addr = addr0 + ecur; // ecur + OFF_F_CURX 968 969 // get current crossing: 970 curx = _unsafe.getInt(addr); 971 972 // update crossing with orientation at last bit: 973 cross = curx; 974 975 // Increment x using DDA (fixed point): 976 curx += _unsafe.getInt(addr + _OFF_BUMP_X); 977 978 // Increment error: 979 err = _unsafe.getInt(addr + _OFF_ERROR) 980 + _unsafe.getInt(addr + _OFF_BUMP_ERR); 981 982 // Manual carry handling: 1041 } 1042 } else { 1043 if (DO_STATS) { 1044 rdrCtx.stats.stat_rdr_crossings_msorts.add(numCrossings); 1045 rdrCtx.stats.hist_rdr_crossings_ratio 1046 .add((1000 * ptrLen) / numCrossings); 1047 rdrCtx.stats.hist_rdr_crossings_msorts.add(numCrossings); 1048 rdrCtx.stats.hist_rdr_crossings_msorts_adds.add(ptrLen); 1049 } 1050 1051 // Copy sorted data in auxiliary arrays 1052 // and perform insertion sort on almost sorted data 1053 // (ie i < prevNumCrossings): 1054 1055 lastCross = _MIN_VALUE; 1056 1057 for (i = 0; i < numCrossings; i++) { 1058 // get the pointer to the edge 1059 ecur = _edgePtrs[i]; 1060 1061 /* convert subpixel coordinates into pixel 1062 positions for coming scanline */ 1063 /* note: it is faster to always update edges even 1064 if it is removed from AEL for coming or last scanline */ 1065 1066 // random access so use unsafe: 1067 addr = addr0 + ecur; // ecur + OFF_F_CURX 1068 1069 // get current crossing: 1070 curx = _unsafe.getInt(addr); 1071 1072 // update crossing with orientation at last bit: 1073 cross = curx; 1074 1075 // Increment x using DDA (fixed point): 1076 curx += _unsafe.getInt(addr + _OFF_BUMP_X); 1077 1078 // Increment error: 1079 err = _unsafe.getInt(addr + _OFF_ERROR) 1080 + _unsafe.getInt(addr + _OFF_BUMP_ERR); 1081 1082 // Manual carry handling: 1148 prev = curx = x0; 1149 // to turn {0, 1} into {-1, 1}, multiply by 2 and subtract 1. 1150 // last bit contains orientation (0 or 1) 1151 crorientation = ((curxo & 0x1) << 1) - 1; 1152 1153 if (windingRuleEvenOdd) { 1154 sum = crorientation; 1155 1156 // Even Odd winding rule: take care of mask ie sum(orientations) 1157 for (i = 1; i < numCrossings; i++) { 1158 curxo = _crossings[i]; 1159 curx = curxo >> 1; 1160 // to turn {0, 1} into {-1, 1}, multiply by 2 and subtract 1. 1161 // last bit contains orientation (0 or 1) 1162 crorientation = ((curxo & 0x1) << 1) - 1; 1163 1164 if ((sum & 0x1) != 0) { 1165 // TODO: perform line clipping on left-right sides 1166 // to avoid such bound checks: 1167 x0 = (prev > bboxx0) ? prev : bboxx0; 1168 1169 if (curx < bboxx1) { 1170 x1 = curx; 1171 } else { 1172 x1 = bboxx1; 1173 // skip right side (fast exit loop): 1174 i = numCrossings; 1175 } 1176 1177 if (x0 < x1) { 1178 x0 -= bboxx0; // turn x0, x1 from coords to indices 1179 x1 -= bboxx0; // in the alpha array. 1180 1181 pix_x = x0 >> _SUBPIXEL_LG_POSITIONS_X; 1182 pix_xmaxm1 = (x1 - 1) >> _SUBPIXEL_LG_POSITIONS_X; 1183 1184 if (pix_x == pix_xmaxm1) { 1185 // Start and end in same pixel 1186 tmp = (x1 - x0); // number of subpixels 1187 _alpha[pix_x ] += tmp; 1188 _alpha[pix_x + 1] -= tmp; 1189 1190 if (useBlkFlags) { 1191 // flag used blocks: 1192 // note: block processing handles extra pixel: 1193 _blkFlags[pix_x >> _BLK_SIZE_LG] = 1; 1194 } 1195 } else { 1196 tmp = (x0 & _SUBPIXEL_MASK_X); 1197 _alpha[pix_x ] 1198 += (_SUBPIXEL_POSITIONS_X - tmp); 1199 _alpha[pix_x + 1] 1200 += tmp; 1201 1202 pix_xmax = x1 >> _SUBPIXEL_LG_POSITIONS_X; 1203 1204 tmp = (x1 & _SUBPIXEL_MASK_X); 1205 _alpha[pix_xmax ] 1206 -= (_SUBPIXEL_POSITIONS_X - tmp); 1207 _alpha[pix_xmax + 1] 1208 -= tmp; 1209 1210 if (useBlkFlags) { 1211 // flag used blocks: 1212 // note: block processing handles extra pixel: 1213 _blkFlags[pix_x >> _BLK_SIZE_LG] = 1; 1214 _blkFlags[pix_xmax >> _BLK_SIZE_LG] = 1; 1215 } 1216 } 1217 } 1218 } 1219 1220 sum += crorientation; 1221 prev = curx; 1222 } 1223 } else { 1224 // Non-zero winding rule: optimize that case (default) 1225 // and avoid processing intermediate crossings 1226 for (i = 1, sum = 0;; i++) { 1227 sum += crorientation; 1228 1229 if (sum != 0) { 1230 // prev = min(curx) 1231 if (prev > curx) { 1232 prev = curx; 1233 } 1234 } else { 1235 // TODO: perform line clipping on left-right sides 1236 // to avoid such bound checks: 1237 x0 = (prev > bboxx0) ? prev : bboxx0; 1238 1239 if (curx < bboxx1) { 1240 x1 = curx; 1241 } else { 1242 x1 = bboxx1; 1243 // skip right side (fast exit loop): 1244 i = numCrossings; 1245 } 1246 1247 if (x0 < x1) { 1248 x0 -= bboxx0; // turn x0, x1 from coords to indices 1249 x1 -= bboxx0; // in the alpha array. 1250 1251 pix_x = x0 >> _SUBPIXEL_LG_POSITIONS_X; 1252 pix_xmaxm1 = (x1 - 1) >> _SUBPIXEL_LG_POSITIONS_X; 1253 1254 if (pix_x == pix_xmaxm1) { 1255 // Start and end in same pixel 1256 tmp = (x1 - x0); // number of subpixels 1257 _alpha[pix_x ] += tmp; 1258 _alpha[pix_x + 1] -= tmp; 1259 1260 if (useBlkFlags) { 1261 // flag used blocks: 1262 // note: block processing handles extra pixel: 1263 _blkFlags[pix_x >> _BLK_SIZE_LG] = 1; 1264 } 1265 } else { 1266 tmp = (x0 & _SUBPIXEL_MASK_X); 1267 _alpha[pix_x ] 1268 += (_SUBPIXEL_POSITIONS_X - tmp); 1269 _alpha[pix_x + 1] 1270 += tmp; 1271 1272 pix_xmax = x1 >> _SUBPIXEL_LG_POSITIONS_X; 1273 1274 tmp = (x1 & _SUBPIXEL_MASK_X); 1275 _alpha[pix_xmax ] 1276 -= (_SUBPIXEL_POSITIONS_X - tmp); 1277 _alpha[pix_xmax + 1] 1278 -= tmp; 1279 1280 if (useBlkFlags) { 1281 // flag used blocks: 1282 // note: block processing handles extra pixel: 1283 _blkFlags[pix_x >> _BLK_SIZE_LG] = 1; 1284 _blkFlags[pix_xmax >> _BLK_SIZE_LG] = 1; 1285 } 1286 } 1287 } 1288 prev = _MAX_VALUE; 1289 } 1290 1291 if (i == numCrossings) { 1292 break; 1293 } 1294 1295 curxo = _crossings[i]; 1296 curx = curxo >> 1; 1297 // to turn {0, 1} into {-1, 1}, multiply by 2 and subtract 1. 1298 // last bit contains orientation (0 or 1) 1299 crorientation = ((curxo & 0x1) << 1) - 1; 1300 } 1301 } 1302 } // numCrossings > 0 1303 1304 // even if this last row had no crossings, alpha will be zeroed 1305 // from the last emitRow call. But this doesn't matter because 1306 // maxX < minX, so no row will be emitted to the MarlinCache. 1307 if ((y & _SUBPIXEL_MASK_Y) == _SUBPIXEL_MASK_Y) { 1308 lastY = y >> _SUBPIXEL_LG_POSITIONS_Y; 1309 1310 // convert subpixel to pixel coordinate within boundaries: 1311 minX = FloatMath.max(minX, bboxx0) >> _SUBPIXEL_LG_POSITIONS_X; 1312 maxX = FloatMath.min(maxX, bboxx1) >> _SUBPIXEL_LG_POSITIONS_X; 1313 1314 if (maxX >= minX) { 1315 // note: alpha array will be zeroed by copyAARow() 1316 // +1 because alpha [pix_minX; pix_maxX[ 1317 // fix range [x0; x1[ 1318 // note: if x1=bboxx1, then alpha is written up to bboxx1+1 1319 // inclusive: alpha[bboxx1] ignored, alpha[bboxx1+1] == 0 1320 // (normally so never cleared below) 1321 copyAARow(_alpha, lastY, minX, maxX + 1, useBlkFlags); 1322 1323 // speculative for next pixel row (scanline coherence): 1324 if (_enableBlkFlagsHeuristics) { 1325 // Use block flags if large pixel span and few crossings: 1326 // ie mean(distance between crossings) is larger than 1327 // 1 block size; 1328 1329 // fast check width: 1330 maxX -= minX; 1331 1332 // if stroking: numCrossings /= 2 1333 // => shift numCrossings by 1 1334 // condition = (width / (numCrossings - 1)) > blockSize 1335 useBlkFlags = (maxX > _BLK_SIZE) && (maxX > 1336 (((numCrossings >> stroking) - 1) << _BLK_SIZE_LG)); 1337 1338 if (DO_STATS) { 1339 tmp = FloatMath.max(1, 1340 ((numCrossings >> stroking) - 1)); 1341 rdrCtx.stats.hist_tile_generator_encoding_dist 1343 } 1344 } 1345 } else { 1346 _cache.clearAARow(lastY); 1347 } 1348 minX = _MAX_VALUE; 1349 maxX = _MIN_VALUE; 1350 } 1351 } // scan line iterator 1352 1353 // Emit final row 1354 y--; 1355 y >>= _SUBPIXEL_LG_POSITIONS_Y; 1356 1357 // convert subpixel to pixel coordinate within boundaries: 1358 minX = FloatMath.max(minX, bboxx0) >> _SUBPIXEL_LG_POSITIONS_X; 1359 maxX = FloatMath.min(maxX, bboxx1) >> _SUBPIXEL_LG_POSITIONS_X; 1360 1361 if (maxX >= minX) { 1362 // note: alpha array will be zeroed by copyAARow() 1363 // +1 because alpha [pix_minX; pix_maxX[ 1364 // fix range [x0; x1[ 1365 // note: if x1=bboxx1, then alpha is written up to bboxx1+1 1366 // inclusive: alpha[bboxx1] ignored then cleared and 1367 // alpha[bboxx1+1] == 0 (normally so never cleared after) 1368 copyAARow(_alpha, y, minX, maxX + 1, useBlkFlags); 1369 } else if (y != lastY) { 1370 _cache.clearAARow(y); 1371 } 1372 1373 // update member: 1374 edgeCount = numCrossings; 1375 prevUseBlkFlags = useBlkFlags; 1376 1377 if (DO_STATS) { 1378 // update max used mark 1379 activeEdgeMaxUsed = _arrayMaxUsed; 1380 } 1381 } 1382 1383 boolean endRendering() { 1384 if (DO_MONITORS) { 1385 rdrCtx.stats.mon_rdr_endRendering.start(); 1386 } 1387 if (edgeMinY == Integer.MAX_VALUE) { 1388 return false; // undefined edges bounds 1389 } 1390 1391 // bounds as half-open intervals 1392 final int spminX = FloatMath.max(FloatMath.ceil_int(edgeMinX - 0.5f), boundsMinX); 1393 final int spmaxX = FloatMath.min(FloatMath.ceil_int(edgeMaxX - 0.5f), boundsMaxX); 1394 1395 // edge Min/Max Y are already rounded to subpixels within bounds: 1396 final int spminY = edgeMinY; 1397 final int spmaxY = edgeMaxY; 1398 1399 buckets_minY = spminY - boundsMinY; 1400 buckets_maxY = spmaxY - boundsMinY; 1401 1402 if (DO_LOG_BOUNDS) { 1403 MarlinUtils.logInfo("edgesXY = [" + edgeMinX + " ... " + edgeMaxX 1404 + "[ [" + edgeMinY + " ... " + edgeMaxY + "["); 1405 MarlinUtils.logInfo("spXY = [" + spminX + " ... " + spmaxX 1406 + "[ [" + spminY + " ... " + spmaxY + "["); 1407 } 1408 1409 // test clipping for shapes out of bounds 1410 if ((spminX >= spmaxX) || (spminY >= spmaxY)) { 1411 return false; 1412 } 1413 1414 // half open intervals 1415 // inclusive: 1416 final int pminX = spminX >> SUBPIXEL_LG_POSITIONS_X; 1417 // exclusive: 1418 final int pmaxX = (spmaxX + SUBPIXEL_MASK_X) >> SUBPIXEL_LG_POSITIONS_X; 1419 // inclusive: 1420 final int pminY = spminY >> SUBPIXEL_LG_POSITIONS_Y; 1421 // exclusive: 1422 final int pmaxY = (spmaxY + SUBPIXEL_MASK_Y) >> SUBPIXEL_LG_POSITIONS_Y; 1423 1424 // store BBox to answer ptg.getBBox(): 1425 this.cache.init(pminX, pminY, pmaxX, pmaxY); 1426 1427 // Heuristics for using block flags: 1428 if (ENABLE_BLOCK_FLAGS) { 1429 enableBlkFlags = this.cache.useRLE; 1430 prevUseBlkFlags = enableBlkFlags && !ENABLE_BLOCK_FLAGS_HEURISTICS; 1431 1432 if (enableBlkFlags) { 1433 // ensure blockFlags array is large enough: 1434 // note: +2 to ensure enough space left at end 1435 final int blkLen = ((pmaxX - pminX) >> BLOCK_SIZE_LG) + 2; 1436 if (blkLen > INITIAL_ARRAY) { 1437 blkFlags = blkFlags_ref.getArray(blkLen); 1438 } 1439 } 1440 } 1441 1442 // memorize the rendering bounding box: 1443 /* note: bbox_spminX and bbox_spmaxX must be pixel boundaries 1444 to have correct coverage computation */ 1445 // inclusive: 1446 bbox_spminX = pminX << SUBPIXEL_LG_POSITIONS_X; 1447 // exclusive: 1448 bbox_spmaxX = pmaxX << SUBPIXEL_LG_POSITIONS_X; 1449 // inclusive: 1450 bbox_spminY = spminY; 1451 // exclusive: 1452 bbox_spmaxY = spmaxY; 1453 1454 if (DO_LOG_BOUNDS) { 1455 MarlinUtils.logInfo("pXY = [" + pminX + " ... " + pmaxX 1456 + "[ [" + pminY + " ... " + pmaxY + "["); 1457 MarlinUtils.logInfo("bbox_spXY = [" + bbox_spminX + " ... " 1458 + bbox_spmaxX + "[ [" + bbox_spminY + " ... " 1459 + bbox_spmaxY + "["); 1460 } 1461 1462 // Prepare alpha line: 1463 // add 2 to better deal with the last pixel in a pixel row. 1464 final int width = (pmaxX - pminX) + 2; 1465 1466 // Useful when processing tile line by tile line 1467 if (width > INITIAL_AA_ARRAY) { 1468 if (DO_STATS) { 1469 rdrCtx.stats.stat_array_renderer_alphaline.add(width); 1470 } 1471 alphaLine = alphaLine_ref.getArray(width); 1472 } 1490 // avoid rendering for last call to nextTile() 1491 if (fixed_spminY < bbox_spmaxY) { 1492 // process a complete tile line ie scanlines for 32 rows 1493 final int spmaxY = FloatMath.min(bbox_spmaxY, spminY + SUBPIXEL_TILE); 1494 1495 // process tile line [0 - 32] 1496 cache.resetTileLine(pminY); 1497 1498 // Process only one tile line: 1499 _endRendering(fixed_spminY, spmaxY); 1500 } 1501 if (DO_MONITORS) { 1502 rdrCtx.stats.mon_rdr_endRendering_Y.stop(); 1503 } 1504 } 1505 1506 void copyAARow(final int[] alphaRow, 1507 final int pix_y, final int pix_from, final int pix_to, 1508 final boolean useBlockFlags) 1509 { 1510 if (DO_MONITORS) { 1511 rdrCtx.stats.mon_rdr_copyAARow.start(); 1512 } 1513 if (useBlockFlags) { 1514 if (DO_STATS) { 1515 rdrCtx.stats.hist_tile_generator_encoding.add(1); 1516 } 1517 cache.copyAARowRLE_WithBlockFlags(blkFlags, alphaRow, pix_y, pix_from, pix_to); 1518 } else { 1519 if (DO_STATS) { 1520 rdrCtx.stats.hist_tile_generator_encoding.add(0); 1521 } 1522 cache.copyAARowNoRLE(alphaRow, pix_y, pix_from, pix_to); 1523 } 1524 if (DO_MONITORS) { 1525 rdrCtx.stats.mon_rdr_copyAARow.stop(); 1526 } 1527 } 1528 } |