1 /* 2 * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved. 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 7 * published by the Free Software Foundation. Oracle designates this 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.0p32d; 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 static final float RDR_OFFSET_X = 0.501f / SUBPIXEL_SCALE_X; 51 static final float RDR_OFFSET_Y = 0.501f / SUBPIXEL_SCALE_Y; 52 53 // number of subpixels corresponding to a tile line 54 private static final int SUBPIXEL_TILE 55 = TILE_H << 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 / 4 ~ 1024 62 static final int INITIAL_CROSSING_COUNT = INITIAL_EDGES_COUNT >> 2; 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 = MarlinProperties.getCubicDecD2() * (NORM_SUBPIXELS / 8.0f); // 1 pixel 84 // cubic error in subpixels to increment step 85 private static final float CUB_INC_ERR_SUBPIX 86 = MarlinProperties.getCubicIncD1() * (NORM_SUBPIXELS / 8.0f); // 0.4 pixel 87 88 // TestNonAARasterization (JDK-8170879): cubics 89 // bad paths (59294/100000 == 59,29%, 94335 bad pixels (avg = 1,59), 3966 warnings (avg = 0,07) 90 91 // cubic bind length to decrement step 92 public static final float CUB_DEC_BND 93 = 8.0f * CUB_DEC_ERR_SUBPIX; 94 // cubic bind length to increment step 95 public static final float CUB_INC_BND 96 = 8.0f * CUB_INC_ERR_SUBPIX; 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 = 1.0f / CUB_COUNT; 108 // cubic dt^2 = 1 / count^2 = 1 / 4^countlg 109 private static final float CUB_INV_COUNT_2 = 1.0f / CUB_COUNT_2; 110 // cubic dt^3 = 1 / count^3 = 1 / 8^countlg 111 private static final float CUB_INV_COUNT_3 = 1.0f / CUB_COUNT_3; 112 113 // quad break into lines 114 // quadratic error in subpixels 115 private static final float QUAD_DEC_ERR_SUBPIX 116 = MarlinProperties.getQuadDecD2() * (NORM_SUBPIXELS / 8.0f); // 0.5 pixel 117 118 // TestNonAARasterization (JDK-8170879): quads 119 // bad paths (62916/100000 == 62,92%, 103818 bad pixels (avg = 1,65), 6514 warnings (avg = 0,10) 120 121 // quadratic bind length to decrement step 122 public static final float QUAD_DEC_BND 123 = 8.0f * QUAD_DEC_ERR_SUBPIX; 124 125 ////////////////////////////////////////////////////////////////////////////// 126 // SCAN LINE 127 ////////////////////////////////////////////////////////////////////////////// 128 // crossings ie subpixel edge x coordinates 129 private int[] crossings; 130 // auxiliary storage for crossings (merge sort) 131 private int[] aux_crossings; 132 133 // indices into the segment pointer lists. They indicate the "active" 134 // sublist in the segment lists (the portion of the list that contains 135 // all the segments that cross the next scan line). 136 private int edgeCount; 137 private int[] edgePtrs; 138 // auxiliary storage for edge pointers (merge sort) 139 private int[] aux_edgePtrs; 140 141 // max used for both edgePtrs and crossings (stats only) 142 private int activeEdgeMaxUsed; 143 144 // crossings ref (dirty) 145 private final IntArrayCache.Reference crossings_ref; 146 // edgePtrs ref (dirty) 147 private final IntArrayCache.Reference edgePtrs_ref; 148 // merge sort initial arrays (large enough to satisfy most usages) (1024) 149 // aux_crossings ref (dirty) 150 private final IntArrayCache.Reference aux_crossings_ref; 151 // aux_edgePtrs ref (dirty) 152 private final IntArrayCache.Reference aux_edgePtrs_ref; 153 154 ////////////////////////////////////////////////////////////////////////////// 155 // EDGE LIST 156 ////////////////////////////////////////////////////////////////////////////// 157 private int edgeMinY = Integer.MAX_VALUE; 158 private int edgeMaxY = Integer.MIN_VALUE; 159 private float edgeMinX = Float.POSITIVE_INFINITY; 160 private float edgeMaxX = Float.NEGATIVE_INFINITY; 161 162 // edges [ints] stored in off-heap memory 163 private final OffHeapArray edges; 164 165 private int[] edgeBuckets; 166 private int[] edgeBucketCounts; // 2*newedges + (1 if pruning needed) 167 // used range for edgeBuckets / edgeBucketCounts 168 private int buckets_minY; 169 private int buckets_maxY; 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 = 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 /= 4.0f; // 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 = 1.0f / 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; 223 } 224 } 225 addLine(x0, y0, x2, y2); 226 227 if (DO_STATS) { 228 rdrCtx.stats.stat_rdr_quadBreak.add(nL + 1); 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 = 2.0f * c.dax * icount3; 250 dddy = 2.0f * 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) + Math.abs(ddy) >= _DEC_BND) { 266 dddx /= 8.0f; 267 dddy /= 8.0f; 268 ddx = ddx / 4.0f - dddx; 269 ddy = ddy / 4.0f - dddy; 270 dx = (dx - ddx) / 2.0f; 271 dy = (dy - ddy) / 2.0f; 272 273 count <<= 1; 274 if (DO_STATS) { 275 rdrCtx.stats.stat_rdr_curveBreak_dec.add(count); 276 } 277 } 278 279 // double step: 280 // can only do this on even "count" values, because we must divide count by 2 281 while (count % 2 == 0 282 && Math.abs(dx) + Math.abs(dy) <= _INC_BND) 283 { 284 dx = 2.0f * dx + ddx; 285 dy = 2.0f * dy + ddy; 286 ddx = 4.0f * (ddx + dddx); 287 ddy = 4.0f * (ddy + dddy); 288 dddx *= 8.0f; 289 dddy *= 8.0f; 290 291 count >>= 1; 292 if (DO_STATS) { 293 rdrCtx.stats.stat_rdr_curveBreak_inc.add(count); 294 } 295 } 296 if (--count > 0) { 297 x1 += dx; 298 dx += ddx; 299 ddx += dddx; 300 y1 += dy; 301 dy += ddy; 302 ddy += dddy; 303 } else { 304 x1 = x3; 305 y1 = y3; 306 } 307 308 addLine(x0, y0, x1, y1); 309 310 if (DO_STATS) { nL++; } 311 x0 = x1; 312 y0 = y1; 313 } 314 if (DO_STATS) { 315 rdrCtx.stats.stat_rdr_curveBreak.add(nL); 316 } 317 } 318 319 private void addLine(float x1, float y1, float x2, float y2) { 320 if (DO_MONITORS) { 321 rdrCtx.stats.mon_rdr_addLine.start(); 322 } 323 if (DO_STATS) { 324 rdrCtx.stats.stat_rdr_addLine.add(1); 325 } 326 int or = 1; // orientation of the line. 1 if y increases, 0 otherwise. 327 if (y2 < y1) { 328 or = 0; 329 float tmp = y2; 330 y2 = y1; 331 y1 = tmp; 332 tmp = x2; 333 x2 = x1; 334 x1 = tmp; 335 } 336 337 // convert subpixel coordinates [float] into pixel positions [int] 338 339 // The index of the pixel that holds the next HPC is at ceil(trueY - 0.5) 340 // Since y1 and y2 are biased by -0.5 in tosubpixy(), this is simply 341 // ceil(y1) or ceil(y2) 342 // upper integer (inclusive) 343 final int firstCrossing = FloatMath.max(FloatMath.ceil_int(y1), boundsMinY); 344 345 // note: use boundsMaxY (last Y exclusive) to compute correct coverage 346 // upper integer (exclusive) 347 final int lastCrossing = FloatMath.min(FloatMath.ceil_int(y2), boundsMaxY); 348 349 /* skip horizontal lines in pixel space and clip edges 350 out of y range [boundsMinY; boundsMaxY] */ 351 if (firstCrossing >= lastCrossing) { 352 if (DO_MONITORS) { 353 rdrCtx.stats.mon_rdr_addLine.stop(); 354 } 355 if (DO_STATS) { 356 rdrCtx.stats.stat_rdr_addLine_skip.add(1); 357 } 358 return; 359 } 360 361 // edge min/max X/Y are in subpixel space (half-open interval): 362 // note: Use integer crossings to ensure consistent range within 363 // edgeBuckets / edgeBucketCounts arrays in case of NaN values (int = 0) 364 if (firstCrossing < edgeMinY) { 365 edgeMinY = firstCrossing; 366 } 367 if (lastCrossing > edgeMaxY) { 368 edgeMaxY = lastCrossing; 369 } 370 371 // Use double-precision for improved accuracy: 372 final double x1d = x1; 373 final double y1d = y1; 374 final double slope = (x1d - x2) / (y1d - y2); 375 376 if (slope >= 0.0d) { // <==> x1 < x2 377 if (x1 < edgeMinX) { 378 edgeMinX = x1; 379 } 380 if (x2 > edgeMaxX) { 381 edgeMaxX = x2; 382 } 383 } else { 384 if (x2 < edgeMinX) { 385 edgeMinX = x2; 386 } 387 if (x1 > edgeMaxX) { 388 edgeMaxX = x1; 389 } 390 } 391 392 // local variables for performance: 393 final int _SIZEOF_EDGE_BYTES = SIZEOF_EDGE_BYTES; 394 395 final OffHeapArray _edges = edges; 396 397 // get free pointer (ie length in bytes) 398 final int edgePtr = _edges.used; 399 400 // use substraction to avoid integer overflow: 401 if (_edges.length - edgePtr < _SIZEOF_EDGE_BYTES) { 402 // suppose _edges.length > _SIZEOF_EDGE_BYTES 403 // so doubling size is enough to add needed bytes 404 // note: throw IOOB if neededSize > 2Gb: 405 final long edgeNewSize = ArrayCacheConst.getNewLargeSize( 406 _edges.length, 407 edgePtr + _SIZEOF_EDGE_BYTES); 408 409 if (DO_STATS) { 410 rdrCtx.stats.stat_rdr_edges_resizes.add(edgeNewSize); 411 } 412 _edges.resize(edgeNewSize); 413 } 414 415 416 final Unsafe _unsafe = OffHeapArray.UNSAFE; 417 final long SIZE_INT = 4L; 418 long addr = _edges.address + edgePtr; 419 420 // The x value must be bumped up to its position at the next HPC we will evaluate. 421 // "firstcrossing" is the (sub)pixel number where the next crossing occurs 422 // thus, the actual coordinate of the next HPC is "firstcrossing + 0.5" 423 // so the Y distance we cover is "firstcrossing + 0.5 - trueY". 424 // Note that since y1 (and y2) are already biased by -0.5 in tosubpixy(), we have 425 // y1 = trueY - 0.5 426 // trueY = y1 + 0.5 427 // firstcrossing + 0.5 - trueY = firstcrossing + 0.5 - (y1 + 0.5) 428 // = firstcrossing - y1 429 // The x coordinate at that HPC is then: 430 // x1_intercept = x1 + (firstcrossing - y1) * slope 431 // The next VPC is then given by: 432 // VPC index = ceil(x1_intercept - 0.5), or alternately 433 // VPC index = floor(x1_intercept - 0.5 + 1 - epsilon) 434 // epsilon is hard to pin down in floating point, but easy in fixed point, so if 435 // we convert to fixed point then these operations get easier: 436 // long x1_fixed = x1_intercept * 2^32; (fixed point 32.32 format) 437 // curx = next VPC = fixed_floor(x1_fixed - 2^31 + 2^32 - 1) 438 // = fixed_floor(x1_fixed + 2^31 - 1) 439 // = fixed_floor(x1_fixed + 0x7FFFFFFF) 440 // and error = fixed_fract(x1_fixed + 0x7FFFFFFF) 441 final double x1_intercept = x1d + (firstCrossing - y1d) * slope; 442 443 // inlined scalb(x1_intercept, 32): 444 final long x1_fixed_biased = ((long) (POWER_2_TO_32 * x1_intercept)) 445 + 0x7FFFFFFFL; 446 // curx: 447 // last bit corresponds to the orientation 448 _unsafe.putInt(addr, (((int) (x1_fixed_biased >> 31L)) & ALL_BUT_LSB) | or); 449 addr += SIZE_INT; 450 _unsafe.putInt(addr, ((int) x1_fixed_biased) >>> 1); 451 addr += SIZE_INT; 452 453 // inlined scalb(slope, 32): 454 final long slope_fixed = (long) (POWER_2_TO_32 * slope); 455 456 // last bit set to 0 to keep orientation: 457 _unsafe.putInt(addr, (((int) (slope_fixed >> 31L)) & ALL_BUT_LSB)); 458 addr += SIZE_INT; 459 _unsafe.putInt(addr, ((int) slope_fixed) >>> 1); 460 addr += SIZE_INT; 461 462 final int[] _edgeBuckets = edgeBuckets; 463 final int[] _edgeBucketCounts = edgeBucketCounts; 464 465 final int _boundsMinY = boundsMinY; 466 467 // each bucket is a linked list. this method adds ptr to the 468 // start of the "bucket"th linked list. 469 final int bucketIdx = firstCrossing - _boundsMinY; 470 471 // pointer from bucket 472 _unsafe.putInt(addr, _edgeBuckets[bucketIdx]); 473 addr += SIZE_INT; 474 // y max (exclusive) 475 _unsafe.putInt(addr, lastCrossing); 476 477 // Update buckets: 478 // directly the edge struct "pointer" 479 _edgeBuckets[bucketIdx] = edgePtr; 480 _edgeBucketCounts[bucketIdx] += 2; // 1 << 1 481 // last bit means edge end 482 _edgeBucketCounts[lastCrossing - _boundsMinY] |= 0x1; 483 484 // update free pointer (ie length in bytes) 485 _edges.used += _SIZEOF_EDGE_BYTES; 486 487 if (DO_MONITORS) { 488 rdrCtx.stats.mon_rdr_addLine.stop(); 489 } 490 } 491 492 // END EDGE LIST 493 ////////////////////////////////////////////////////////////////////////////// 494 495 // Cache to store RLE-encoded coverage mask of the current primitive 496 final MarlinCache cache; 497 498 // Bounds of the drawing region, at subpixel precision. 499 private int boundsMinX, boundsMinY, boundsMaxX, boundsMaxY; 500 501 // Current winding rule 502 private int windingRule; 503 504 // Current drawing position, i.e., final point of last segment 505 private float x0, y0; 506 507 // Position of most recent 'moveTo' command 508 private float sx0, sy0; 509 510 // per-thread renderer context 511 final RendererContext rdrCtx; 512 // dirty curve 513 private final Curve curve; 514 515 // clean alpha array (zero filled) 516 private int[] alphaLine; 517 518 // alphaLine ref (clean) 519 private final IntArrayCache.Reference alphaLine_ref; 520 521 private boolean enableBlkFlags = false; 522 private boolean prevUseBlkFlags = false; 523 524 /* block flags (0|1) */ 525 private int[] blkFlags; 526 527 // blkFlags ref (clean) 528 private final IntArrayCache.Reference blkFlags_ref; 529 530 Renderer(final RendererContext rdrCtx) { 531 this.rdrCtx = rdrCtx; 532 533 this.edges = rdrCtx.newOffHeapArray(INITIAL_EDGES_CAPACITY); // 96K 534 535 this.curve = rdrCtx.curve; 536 537 edgeBuckets_ref = rdrCtx.newCleanIntArrayRef(INITIAL_BUCKET_ARRAY); // 64K 538 edgeBucketCounts_ref = rdrCtx.newCleanIntArrayRef(INITIAL_BUCKET_ARRAY); // 64K 539 540 edgeBuckets = edgeBuckets_ref.initial; 541 edgeBucketCounts = edgeBucketCounts_ref.initial; 542 543 // 2048 (pixelsize) pixel large 544 alphaLine_ref = rdrCtx.newCleanIntArrayRef(INITIAL_AA_ARRAY); // 8K 545 alphaLine = alphaLine_ref.initial; 546 547 this.cache = rdrCtx.cache; 548 549 crossings_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K 550 aux_crossings_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K 551 edgePtrs_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K 552 aux_edgePtrs_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K 553 554 crossings = crossings_ref.initial; 555 aux_crossings = aux_crossings_ref.initial; 556 edgePtrs = edgePtrs_ref.initial; 557 aux_edgePtrs = aux_edgePtrs_ref.initial; 558 559 blkFlags_ref = rdrCtx.newCleanIntArrayRef(INITIAL_ARRAY); // 1K = 1 tile line 560 blkFlags = blkFlags_ref.initial; 561 } 562 563 Renderer init(final int pix_boundsX, final int pix_boundsY, 564 final int pix_boundsWidth, final int pix_boundsHeight, 565 final int windingRule) 566 { 567 this.windingRule = windingRule; 568 569 // bounds as half-open intervals: minX <= x < maxX and minY <= y < maxY 570 this.boundsMinX = pix_boundsX << SUBPIXEL_LG_POSITIONS_X; 571 this.boundsMaxX = 572 (pix_boundsX + pix_boundsWidth) << SUBPIXEL_LG_POSITIONS_X; 573 this.boundsMinY = pix_boundsY << SUBPIXEL_LG_POSITIONS_Y; 574 this.boundsMaxY = 575 (pix_boundsY + pix_boundsHeight) << SUBPIXEL_LG_POSITIONS_Y; 576 577 if (DO_LOG_BOUNDS) { 578 MarlinUtils.logInfo("boundsXY = [" + boundsMinX + " ... " 579 + boundsMaxX + "[ [" + boundsMinY + " ... " 580 + boundsMaxY + "["); 581 } 582 583 // see addLine: ceil(boundsMaxY) => boundsMaxY + 1 584 // +1 for edgeBucketCounts 585 final int edgeBucketsLength = (boundsMaxY - boundsMinY) + 1; 586 587 if (edgeBucketsLength > INITIAL_BUCKET_ARRAY) { 588 if (DO_STATS) { 589 rdrCtx.stats.stat_array_renderer_edgeBuckets 590 .add(edgeBucketsLength); 591 rdrCtx.stats.stat_array_renderer_edgeBucketCounts 592 .add(edgeBucketsLength); 593 } 594 edgeBuckets = edgeBuckets_ref.getArray(edgeBucketsLength); 595 edgeBucketCounts = edgeBucketCounts_ref.getArray(edgeBucketsLength); 596 } 597 598 edgeMinY = Integer.MAX_VALUE; 599 edgeMaxY = Integer.MIN_VALUE; 600 edgeMinX = Float.POSITIVE_INFINITY; 601 edgeMaxX = Float.NEGATIVE_INFINITY; 602 603 // reset used mark: 604 edgeCount = 0; 605 activeEdgeMaxUsed = 0; 606 edges.used = 0; 607 608 return this; // fluent API 609 } 610 611 /** 612 * Disposes this renderer and recycle it clean up before reusing this instance 613 */ 614 void dispose() { 615 if (DO_STATS) { 616 rdrCtx.stats.stat_rdr_activeEdges.add(activeEdgeMaxUsed); 617 rdrCtx.stats.stat_rdr_edges.add(edges.used); 618 rdrCtx.stats.stat_rdr_edges_count.add(edges.used / SIZEOF_EDGE_BYTES); 619 rdrCtx.stats.hist_rdr_edges_count.add(edges.used / SIZEOF_EDGE_BYTES); 620 rdrCtx.stats.totalOffHeap += edges.length; 621 } 622 // Return arrays: 623 crossings = crossings_ref.putArray(crossings); 624 aux_crossings = aux_crossings_ref.putArray(aux_crossings); 625 626 edgePtrs = edgePtrs_ref.putArray(edgePtrs); 627 aux_edgePtrs = aux_edgePtrs_ref.putArray(aux_edgePtrs); 628 629 alphaLine = alphaLine_ref.putArray(alphaLine, 0, 0); // already zero filled 630 blkFlags = blkFlags_ref.putArray(blkFlags, 0, 0); // already zero filled 631 632 if (edgeMinY != Integer.MAX_VALUE) { 633 // if context is maked as DIRTY: 634 if (rdrCtx.dirty) { 635 // may happen if an exception if thrown in the pipeline processing: 636 // clear completely buckets arrays: 637 buckets_minY = 0; 638 buckets_maxY = boundsMaxY - boundsMinY; 639 } 640 // clear only used part 641 edgeBuckets = edgeBuckets_ref.putArray(edgeBuckets, buckets_minY, 642 buckets_maxY); 643 edgeBucketCounts = edgeBucketCounts_ref.putArray(edgeBucketCounts, 644 buckets_minY, 645 buckets_maxY + 1); 646 } else { 647 // unused arrays 648 edgeBuckets = edgeBuckets_ref.putArray(edgeBuckets, 0, 0); 649 edgeBucketCounts = edgeBucketCounts_ref.putArray(edgeBucketCounts, 0, 0); 650 } 651 652 // At last: resize back off-heap edges to initial size 653 if (edges.length != INITIAL_EDGES_CAPACITY) { 654 // note: may throw OOME: 655 edges.resize(INITIAL_EDGES_CAPACITY); 656 } 657 if (DO_CLEAN_DIRTY) { 658 // Force zero-fill dirty arrays: 659 edges.fill(BYTE_0); 660 } 661 if (DO_MONITORS) { 662 rdrCtx.stats.mon_rdr_endRendering.stop(); 663 } 664 // recycle the RendererContext instance 665 MarlinRenderingEngine.returnRendererContext(rdrCtx); 666 } 667 668 private static float tosubpixx(final float pix_x) { 669 return SUBPIXEL_SCALE_X * pix_x; 670 } 671 672 private static float tosubpixy(final float pix_y) { 673 // shift y by -0.5 for fast ceil(y - 0.5): 674 return SUBPIXEL_SCALE_Y * pix_y - 0.5f; 675 } 676 677 @Override 678 public void moveTo(float pix_x0, float pix_y0) { 679 closePath(); 680 final float sx = tosubpixx(pix_x0); 681 final float sy = tosubpixy(pix_y0); 682 this.sx0 = sx; 683 this.sy0 = sy; 684 this.x0 = sx; 685 this.y0 = sy; 686 } 687 688 @Override 689 public void lineTo(float pix_x1, float pix_y1) { 690 final float x1 = tosubpixx(pix_x1); 691 final float y1 = tosubpixy(pix_y1); 692 addLine(x0, y0, x1, y1); 693 x0 = x1; 694 y0 = y1; 695 } 696 697 @Override 698 public void curveTo(float pix_x1, float pix_y1, 699 float pix_x2, float pix_y2, 700 float pix_x3, float pix_y3) 701 { 702 final float xe = tosubpixx(pix_x3); 703 final float ye = tosubpixy(pix_y3); 704 curve.set(x0, y0, tosubpixx(pix_x1), tosubpixy(pix_y1), 705 tosubpixx(pix_x2), tosubpixy(pix_y2), xe, ye); 706 curveBreakIntoLinesAndAdd(x0, y0, curve, xe, ye); 707 x0 = xe; 708 y0 = ye; 709 } 710 711 @Override 712 public void quadTo(float pix_x1, float pix_y1, 713 float pix_x2, float pix_y2) 714 { 715 final float xe = tosubpixx(pix_x2); 716 final float ye = tosubpixy(pix_y2); 717 curve.set(x0, y0, tosubpixx(pix_x1), tosubpixy(pix_y1), xe, ye); 718 quadBreakIntoLinesAndAdd(x0, y0, curve, xe, ye); 719 x0 = xe; 720 y0 = ye; 721 } 722 723 @Override 724 public void closePath() { 725 if (x0 != sx0 || y0 != sy0) { 726 addLine(x0, y0, sx0, sy0); 727 x0 = sx0; 728 y0 = sy0; 729 } 730 } 731 732 @Override 733 public void pathDone() { 734 closePath(); 735 } 736 737 @Override 738 public long getNativeConsumer() { 739 throw new InternalError("Renderer does not use a native consumer."); 740 } 741 742 private void _endRendering(final int ymin, final int ymax) { 743 if (DISABLE_RENDER) { 744 return; 745 } 746 747 // Get X bounds as true pixel boundaries to compute correct pixel coverage: 748 final int bboxx0 = bbox_spminX; 749 final int bboxx1 = bbox_spmaxX; 750 751 final boolean windingRuleEvenOdd = (windingRule == WIND_EVEN_ODD); 752 753 // Useful when processing tile line by tile line 754 final int[] _alpha = alphaLine; 755 756 // local vars (performance): 757 final MarlinCache _cache = cache; 758 final OffHeapArray _edges = edges; 759 final int[] _edgeBuckets = edgeBuckets; 760 final int[] _edgeBucketCounts = edgeBucketCounts; 761 762 int[] _crossings = this.crossings; 763 int[] _edgePtrs = this.edgePtrs; 764 765 // merge sort auxiliary storage: 766 int[] _aux_crossings = this.aux_crossings; 767 int[] _aux_edgePtrs = this.aux_edgePtrs; 768 769 // copy constants: 770 final long _OFF_ERROR = OFF_ERROR; 771 final long _OFF_BUMP_X = OFF_BUMP_X; 772 final long _OFF_BUMP_ERR = OFF_BUMP_ERR; 773 774 final long _OFF_NEXT = OFF_NEXT; 775 final long _OFF_YMAX = OFF_YMAX; 776 777 final int _ALL_BUT_LSB = ALL_BUT_LSB; 778 final int _ERR_STEP_MAX = ERR_STEP_MAX; 779 780 // unsafe I/O: 781 final Unsafe _unsafe = OffHeapArray.UNSAFE; 782 final long addr0 = _edges.address; 783 long addr; 784 final int _SUBPIXEL_LG_POSITIONS_X = SUBPIXEL_LG_POSITIONS_X; 785 final int _SUBPIXEL_LG_POSITIONS_Y = SUBPIXEL_LG_POSITIONS_Y; 786 final int _SUBPIXEL_MASK_X = SUBPIXEL_MASK_X; 787 final int _SUBPIXEL_MASK_Y = SUBPIXEL_MASK_Y; 788 final int _SUBPIXEL_POSITIONS_X = SUBPIXEL_POSITIONS_X; 789 790 final int _MIN_VALUE = Integer.MIN_VALUE; 791 final int _MAX_VALUE = Integer.MAX_VALUE; 792 793 // Now we iterate through the scanlines. We must tell emitRow the coord 794 // of the first non-transparent pixel, so we must keep accumulators for 795 // the first and last pixels of the section of the current pixel row 796 // that we will emit. 797 // We also need to accumulate pix_bbox, but the iterator does it 798 // for us. We will just get the values from it once this loop is done 799 int minX = _MAX_VALUE; 800 int maxX = _MIN_VALUE; 801 802 int y = ymin; 803 int bucket = y - boundsMinY; 804 805 int numCrossings = this.edgeCount; 806 int edgePtrsLen = _edgePtrs.length; 807 int crossingsLen = _crossings.length; 808 int _arrayMaxUsed = activeEdgeMaxUsed; 809 int ptrLen = 0, newCount, ptrEnd; 810 811 int bucketcount, i, j, ecur; 812 int cross, lastCross; 813 int x0, x1, tmp, sum, prev, curx, curxo, crorientation, err; 814 int pix_x, pix_xmaxm1, pix_xmax; 815 816 int low, high, mid, prevNumCrossings; 817 boolean useBinarySearch; 818 819 final int[] _blkFlags = blkFlags; 820 final int _BLK_SIZE_LG = BLOCK_SIZE_LG; 821 final int _BLK_SIZE = BLOCK_SIZE; 822 823 final boolean _enableBlkFlagsHeuristics = ENABLE_BLOCK_FLAGS_HEURISTICS && this.enableBlkFlags; 824 825 // Use block flags if large pixel span and few crossings: 826 // ie mean(distance between crossings) is high 827 boolean useBlkFlags = this.prevUseBlkFlags; 828 829 final int stroking = rdrCtx.stroking; 830 831 int lastY = -1; // last emited row 832 833 834 // Iteration on scanlines 835 for (; y < ymax; y++, bucket++) { 836 // --- from former ScanLineIterator.next() 837 bucketcount = _edgeBucketCounts[bucket]; 838 839 // marker on previously sorted edges: 840 prevNumCrossings = numCrossings; 841 842 // bucketCount indicates new edge / edge end: 843 if (bucketcount != 0) { 844 if (DO_STATS) { 845 rdrCtx.stats.stat_rdr_activeEdges_updates.add(numCrossings); 846 } 847 848 // last bit set to 1 means that edges ends 849 if ((bucketcount & 0x1) != 0) { 850 // eviction in active edge list 851 // cache edges[] address + offset 852 addr = addr0 + _OFF_YMAX; 853 854 for (i = 0, newCount = 0; i < numCrossings; i++) { 855 // get the pointer to the edge 856 ecur = _edgePtrs[i]; 857 // random access so use unsafe: 858 if (_unsafe.getInt(addr + ecur) > y) { 859 _edgePtrs[newCount++] = ecur; 860 } 861 } 862 // update marker on sorted edges minus removed edges: 863 prevNumCrossings = numCrossings = newCount; 864 } 865 866 ptrLen = bucketcount >> 1; // number of new edge 867 868 if (ptrLen != 0) { 869 if (DO_STATS) { 870 rdrCtx.stats.stat_rdr_activeEdges_adds.add(ptrLen); 871 if (ptrLen > 10) { 872 rdrCtx.stats.stat_rdr_activeEdges_adds_high.add(ptrLen); 873 } 874 } 875 ptrEnd = numCrossings + ptrLen; 876 877 if (edgePtrsLen < ptrEnd) { 878 if (DO_STATS) { 879 rdrCtx.stats.stat_array_renderer_edgePtrs.add(ptrEnd); 880 } 881 this.edgePtrs = _edgePtrs 882 = edgePtrs_ref.widenArray(_edgePtrs, numCrossings, 883 ptrEnd); 884 885 edgePtrsLen = _edgePtrs.length; 886 // Get larger auxiliary storage: 887 aux_edgePtrs_ref.putArray(_aux_edgePtrs); 888 889 // use ArrayCache.getNewSize() to use the same growing 890 // factor than widenArray(): 891 if (DO_STATS) { 892 rdrCtx.stats.stat_array_renderer_aux_edgePtrs.add(ptrEnd); 893 } 894 this.aux_edgePtrs = _aux_edgePtrs 895 = aux_edgePtrs_ref.getArray( 896 ArrayCacheConst.getNewSize(numCrossings, ptrEnd) 897 ); 898 } 899 900 // cache edges[] address + offset 901 addr = addr0 + _OFF_NEXT; 902 903 // add new edges to active edge list: 904 for (ecur = _edgeBuckets[bucket]; 905 numCrossings < ptrEnd; numCrossings++) 906 { 907 // store the pointer to the edge 908 _edgePtrs[numCrossings] = ecur; 909 // random access so use unsafe: 910 ecur = _unsafe.getInt(addr + ecur); 911 } 912 913 if (crossingsLen < numCrossings) { 914 // Get larger array: 915 crossings_ref.putArray(_crossings); 916 917 if (DO_STATS) { 918 rdrCtx.stats.stat_array_renderer_crossings 919 .add(numCrossings); 920 } 921 this.crossings = _crossings 922 = crossings_ref.getArray(numCrossings); 923 924 // Get larger auxiliary storage: 925 aux_crossings_ref.putArray(_aux_crossings); 926 927 if (DO_STATS) { 928 rdrCtx.stats.stat_array_renderer_aux_crossings 929 .add(numCrossings); 930 } 931 this.aux_crossings = _aux_crossings 932 = aux_crossings_ref.getArray(numCrossings); 933 934 crossingsLen = _crossings.length; 935 } 936 if (DO_STATS) { 937 // update max used mark 938 if (numCrossings > _arrayMaxUsed) { 939 _arrayMaxUsed = numCrossings; 940 } 941 } 942 } // ptrLen != 0 943 } // bucketCount != 0 944 945 946 if (numCrossings != 0) { 947 /* 948 * thresholds to switch to optimized merge sort 949 * for newly added edges + final merge pass. 950 */ 951 if ((ptrLen < 10) || (numCrossings < 40)) { 952 if (DO_STATS) { 953 rdrCtx.stats.hist_rdr_crossings.add(numCrossings); 954 rdrCtx.stats.hist_rdr_crossings_adds.add(ptrLen); 955 } 956 957 /* 958 * threshold to use binary insertion sort instead of 959 * straight insertion sort (to reduce minimize comparisons). 960 */ 961 useBinarySearch = (numCrossings >= 20); 962 963 // if small enough: 964 lastCross = _MIN_VALUE; 965 966 for (i = 0; i < numCrossings; i++) { 967 // get the pointer to the edge 968 ecur = _edgePtrs[i]; 969 970 /* convert subpixel coordinates into pixel 971 positions for coming scanline */ 972 /* note: it is faster to always update edges even 973 if it is removed from AEL for coming or last scanline */ 974 975 // random access so use unsafe: 976 addr = addr0 + ecur; // ecur + OFF_F_CURX 977 978 // get current crossing: 979 curx = _unsafe.getInt(addr); 980 981 // update crossing with orientation at last bit: 982 cross = curx; 983 984 // Increment x using DDA (fixed point): 985 curx += _unsafe.getInt(addr + _OFF_BUMP_X); 986 987 // Increment error: 988 err = _unsafe.getInt(addr + _OFF_ERROR) 989 + _unsafe.getInt(addr + _OFF_BUMP_ERR); 990 991 // Manual carry handling: 992 // keep sign and carry bit only and ignore last bit (preserve orientation): 993 _unsafe.putInt(addr, curx - ((err >> 30) & _ALL_BUT_LSB)); 994 _unsafe.putInt(addr + _OFF_ERROR, (err & _ERR_STEP_MAX)); 995 996 if (DO_STATS) { 997 rdrCtx.stats.stat_rdr_crossings_updates.add(numCrossings); 998 } 999 1000 // insertion sort of crossings: 1001 if (cross < lastCross) { 1002 if (DO_STATS) { 1003 rdrCtx.stats.stat_rdr_crossings_sorts.add(i); 1004 } 1005 1006 /* use binary search for newly added edges 1007 in crossings if arrays are large enough */ 1008 if (useBinarySearch && (i >= prevNumCrossings)) { 1009 if (DO_STATS) { 1010 rdrCtx.stats.stat_rdr_crossings_bsearch.add(i); 1011 } 1012 low = 0; 1013 high = i - 1; 1014 1015 do { 1016 // note: use signed shift (not >>>) for performance 1017 // as indices are small enough to exceed Integer.MAX_VALUE 1018 mid = (low + high) >> 1; 1019 1020 if (_crossings[mid] < cross) { 1021 low = mid + 1; 1022 } else { 1023 high = mid - 1; 1024 } 1025 } while (low <= high); 1026 1027 for (j = i - 1; j >= low; j--) { 1028 _crossings[j + 1] = _crossings[j]; 1029 _edgePtrs [j + 1] = _edgePtrs[j]; 1030 } 1031 _crossings[low] = cross; 1032 _edgePtrs [low] = ecur; 1033 1034 } else { 1035 j = i - 1; 1036 _crossings[i] = _crossings[j]; 1037 _edgePtrs[i] = _edgePtrs[j]; 1038 1039 while ((--j >= 0) && (_crossings[j] > cross)) { 1040 _crossings[j + 1] = _crossings[j]; 1041 _edgePtrs [j + 1] = _edgePtrs[j]; 1042 } 1043 _crossings[j + 1] = cross; 1044 _edgePtrs [j + 1] = ecur; 1045 } 1046 1047 } else { 1048 _crossings[i] = lastCross = cross; 1049 } 1050 } 1051 } else { 1052 if (DO_STATS) { 1053 rdrCtx.stats.stat_rdr_crossings_msorts.add(numCrossings); 1054 rdrCtx.stats.hist_rdr_crossings_ratio 1055 .add((1000 * ptrLen) / numCrossings); 1056 rdrCtx.stats.hist_rdr_crossings_msorts.add(numCrossings); 1057 rdrCtx.stats.hist_rdr_crossings_msorts_adds.add(ptrLen); 1058 } 1059 1060 // Copy sorted data in auxiliary arrays 1061 // and perform insertion sort on almost sorted data 1062 // (ie i < prevNumCrossings): 1063 1064 lastCross = _MIN_VALUE; 1065 1066 for (i = 0; i < numCrossings; i++) { 1067 // get the pointer to the edge 1068 ecur = _edgePtrs[i]; 1069 1070 /* convert subpixel coordinates into pixel 1071 positions for coming scanline */ 1072 /* note: it is faster to always update edges even 1073 if it is removed from AEL for coming or last scanline */ 1074 1075 // random access so use unsafe: 1076 addr = addr0 + ecur; // ecur + OFF_F_CURX 1077 1078 // get current crossing: 1079 curx = _unsafe.getInt(addr); 1080 1081 // update crossing with orientation at last bit: 1082 cross = curx; 1083 1084 // Increment x using DDA (fixed point): 1085 curx += _unsafe.getInt(addr + _OFF_BUMP_X); 1086 1087 // Increment error: 1088 err = _unsafe.getInt(addr + _OFF_ERROR) 1089 + _unsafe.getInt(addr + _OFF_BUMP_ERR); 1090 1091 // Manual carry handling: 1092 // keep sign and carry bit only and ignore last bit (preserve orientation): 1093 _unsafe.putInt(addr, curx - ((err >> 30) & _ALL_BUT_LSB)); 1094 _unsafe.putInt(addr + _OFF_ERROR, (err & _ERR_STEP_MAX)); 1095 1096 if (DO_STATS) { 1097 rdrCtx.stats.stat_rdr_crossings_updates.add(numCrossings); 1098 } 1099 1100 if (i >= prevNumCrossings) { 1101 // simply store crossing as edgePtrs is in-place: 1102 // will be copied and sorted efficiently by mergesort later: 1103 _crossings[i] = cross; 1104 1105 } else if (cross < lastCross) { 1106 if (DO_STATS) { 1107 rdrCtx.stats.stat_rdr_crossings_sorts.add(i); 1108 } 1109 1110 // (straight) insertion sort of crossings: 1111 j = i - 1; 1112 _aux_crossings[i] = _aux_crossings[j]; 1113 _aux_edgePtrs[i] = _aux_edgePtrs[j]; 1114 1115 while ((--j >= 0) && (_aux_crossings[j] > cross)) { 1116 _aux_crossings[j + 1] = _aux_crossings[j]; 1117 _aux_edgePtrs [j + 1] = _aux_edgePtrs[j]; 1118 } 1119 _aux_crossings[j + 1] = cross; 1120 _aux_edgePtrs [j + 1] = ecur; 1121 1122 } else { 1123 // auxiliary storage: 1124 _aux_crossings[i] = lastCross = cross; 1125 _aux_edgePtrs [i] = ecur; 1126 } 1127 } 1128 1129 // use Mergesort using auxiliary arrays (sort only right part) 1130 MergeSort.mergeSortNoCopy(_crossings, _edgePtrs, 1131 _aux_crossings, _aux_edgePtrs, 1132 numCrossings, prevNumCrossings); 1133 } 1134 1135 // reset ptrLen 1136 ptrLen = 0; 1137 // --- from former ScanLineIterator.next() 1138 1139 1140 /* note: bboxx0 and bboxx1 must be pixel boundaries 1141 to have correct coverage computation */ 1142 1143 // right shift on crossings to get the x-coordinate: 1144 curxo = _crossings[0]; 1145 x0 = curxo >> 1; 1146 if (x0 < minX) { 1147 minX = x0; // subpixel coordinate 1148 } 1149 1150 x1 = _crossings[numCrossings - 1] >> 1; 1151 if (x1 > maxX) { 1152 maxX = x1; // subpixel coordinate 1153 } 1154 1155 1156 // compute pixel coverages 1157 prev = curx = x0; 1158 // to turn {0, 1} into {-1, 1}, multiply by 2 and subtract 1. 1159 // last bit contains orientation (0 or 1) 1160 crorientation = ((curxo & 0x1) << 1) - 1; 1161 1162 if (windingRuleEvenOdd) { 1163 sum = crorientation; 1164 1165 // Even Odd winding rule: take care of mask ie sum(orientations) 1166 for (i = 1; i < numCrossings; i++) { 1167 curxo = _crossings[i]; 1168 curx = curxo >> 1; 1169 // to turn {0, 1} into {-1, 1}, multiply by 2 and subtract 1. 1170 // last bit contains orientation (0 or 1) 1171 crorientation = ((curxo & 0x1) << 1) - 1; 1172 1173 if ((sum & 0x1) != 0) { 1174 // TODO: perform line clipping on left-right sides 1175 // to avoid such bound checks: 1176 x0 = (prev > bboxx0) ? prev : bboxx0; 1177 1178 if (curx < bboxx1) { 1179 x1 = curx; 1180 } else { 1181 x1 = bboxx1; 1182 // skip right side (fast exit loop): 1183 i = numCrossings; 1184 } 1185 1186 if (x0 < x1) { 1187 x0 -= bboxx0; // turn x0, x1 from coords to indices 1188 x1 -= bboxx0; // in the alpha array. 1189 1190 pix_x = x0 >> _SUBPIXEL_LG_POSITIONS_X; 1191 pix_xmaxm1 = (x1 - 1) >> _SUBPIXEL_LG_POSITIONS_X; 1192 1193 if (pix_x == pix_xmaxm1) { 1194 // Start and end in same pixel 1195 tmp = (x1 - x0); // number of subpixels 1196 _alpha[pix_x ] += tmp; 1197 _alpha[pix_x + 1] -= tmp; 1198 1199 if (useBlkFlags) { 1200 // flag used blocks: 1201 // note: block processing handles extra pixel: 1202 _blkFlags[pix_x >> _BLK_SIZE_LG] = 1; 1203 } 1204 } else { 1205 tmp = (x0 & _SUBPIXEL_MASK_X); 1206 _alpha[pix_x ] 1207 += (_SUBPIXEL_POSITIONS_X - tmp); 1208 _alpha[pix_x + 1] 1209 += tmp; 1210 1211 pix_xmax = x1 >> _SUBPIXEL_LG_POSITIONS_X; 1212 1213 tmp = (x1 & _SUBPIXEL_MASK_X); 1214 _alpha[pix_xmax ] 1215 -= (_SUBPIXEL_POSITIONS_X - tmp); 1216 _alpha[pix_xmax + 1] 1217 -= tmp; 1218 1219 if (useBlkFlags) { 1220 // flag used blocks: 1221 // note: block processing handles extra pixel: 1222 _blkFlags[pix_x >> _BLK_SIZE_LG] = 1; 1223 _blkFlags[pix_xmax >> _BLK_SIZE_LG] = 1; 1224 } 1225 } 1226 } 1227 } 1228 1229 sum += crorientation; 1230 prev = curx; 1231 } 1232 } else { 1233 // Non-zero winding rule: optimize that case (default) 1234 // and avoid processing intermediate crossings 1235 for (i = 1, sum = 0;; i++) { 1236 sum += crorientation; 1237 1238 if (sum != 0) { 1239 // prev = min(curx) 1240 if (prev > curx) { 1241 prev = curx; 1242 } 1243 } else { 1244 // TODO: perform line clipping on left-right sides 1245 // to avoid such bound checks: 1246 x0 = (prev > bboxx0) ? prev : bboxx0; 1247 1248 if (curx < bboxx1) { 1249 x1 = curx; 1250 } else { 1251 x1 = bboxx1; 1252 // skip right side (fast exit loop): 1253 i = numCrossings; 1254 } 1255 1256 if (x0 < x1) { 1257 x0 -= bboxx0; // turn x0, x1 from coords to indices 1258 x1 -= bboxx0; // in the alpha array. 1259 1260 pix_x = x0 >> _SUBPIXEL_LG_POSITIONS_X; 1261 pix_xmaxm1 = (x1 - 1) >> _SUBPIXEL_LG_POSITIONS_X; 1262 1263 if (pix_x == pix_xmaxm1) { 1264 // Start and end in same pixel 1265 tmp = (x1 - x0); // number of subpixels 1266 _alpha[pix_x ] += tmp; 1267 _alpha[pix_x + 1] -= tmp; 1268 1269 if (useBlkFlags) { 1270 // flag used blocks: 1271 // note: block processing handles extra pixel: 1272 _blkFlags[pix_x >> _BLK_SIZE_LG] = 1; 1273 } 1274 } else { 1275 tmp = (x0 & _SUBPIXEL_MASK_X); 1276 _alpha[pix_x ] 1277 += (_SUBPIXEL_POSITIONS_X - tmp); 1278 _alpha[pix_x + 1] 1279 += tmp; 1280 1281 pix_xmax = x1 >> _SUBPIXEL_LG_POSITIONS_X; 1282 1283 tmp = (x1 & _SUBPIXEL_MASK_X); 1284 _alpha[pix_xmax ] 1285 -= (_SUBPIXEL_POSITIONS_X - tmp); 1286 _alpha[pix_xmax + 1] 1287 -= tmp; 1288 1289 if (useBlkFlags) { 1290 // flag used blocks: 1291 // note: block processing handles extra pixel: 1292 _blkFlags[pix_x >> _BLK_SIZE_LG] = 1; 1293 _blkFlags[pix_xmax >> _BLK_SIZE_LG] = 1; 1294 } 1295 } 1296 } 1297 prev = _MAX_VALUE; 1298 } 1299 1300 if (i == numCrossings) { 1301 break; 1302 } 1303 1304 curxo = _crossings[i]; 1305 curx = curxo >> 1; 1306 // to turn {0, 1} into {-1, 1}, multiply by 2 and subtract 1. 1307 // last bit contains orientation (0 or 1) 1308 crorientation = ((curxo & 0x1) << 1) - 1; 1309 } 1310 } 1311 } // numCrossings > 0 1312 1313 // even if this last row had no crossings, alpha will be zeroed 1314 // from the last emitRow call. But this doesn't matter because 1315 // maxX < minX, so no row will be emitted to the MarlinCache. 1316 if ((y & _SUBPIXEL_MASK_Y) == _SUBPIXEL_MASK_Y) { 1317 lastY = y >> _SUBPIXEL_LG_POSITIONS_Y; 1318 1319 // convert subpixel to pixel coordinate within boundaries: 1320 minX = FloatMath.max(minX, bboxx0) >> _SUBPIXEL_LG_POSITIONS_X; 1321 maxX = FloatMath.min(maxX, bboxx1) >> _SUBPIXEL_LG_POSITIONS_X; 1322 1323 if (maxX >= minX) { 1324 // note: alpha array will be zeroed by copyAARow() 1325 // +1 because alpha [pix_minX; pix_maxX[ 1326 // fix range [x0; x1[ 1327 // note: if x1=bboxx1, then alpha is written up to bboxx1+1 1328 // inclusive: alpha[bboxx1] ignored, alpha[bboxx1+1] == 0 1329 // (normally so never cleared below) 1330 copyAARow(_alpha, lastY, minX, maxX + 1, useBlkFlags); 1331 1332 // speculative for next pixel row (scanline coherence): 1333 if (_enableBlkFlagsHeuristics) { 1334 // Use block flags if large pixel span and few crossings: 1335 // ie mean(distance between crossings) is larger than 1336 // 1 block size; 1337 1338 // fast check width: 1339 maxX -= minX; 1340 1341 // if stroking: numCrossings /= 2 1342 // => shift numCrossings by 1 1343 // condition = (width / (numCrossings - 1)) > blockSize 1344 useBlkFlags = (maxX > _BLK_SIZE) && (maxX > 1345 (((numCrossings >> stroking) - 1) << _BLK_SIZE_LG)); 1346 1347 if (DO_STATS) { 1348 tmp = FloatMath.max(1, 1349 ((numCrossings >> stroking) - 1)); 1350 rdrCtx.stats.hist_tile_generator_encoding_dist 1351 .add(maxX / tmp); 1352 } 1353 } 1354 } else { 1355 _cache.clearAARow(lastY); 1356 } 1357 minX = _MAX_VALUE; 1358 maxX = _MIN_VALUE; 1359 } 1360 } // scan line iterator 1361 1362 // Emit final row 1363 y--; 1364 y >>= _SUBPIXEL_LG_POSITIONS_Y; 1365 1366 // convert subpixel to pixel coordinate within boundaries: 1367 minX = FloatMath.max(minX, bboxx0) >> _SUBPIXEL_LG_POSITIONS_X; 1368 maxX = FloatMath.min(maxX, bboxx1) >> _SUBPIXEL_LG_POSITIONS_X; 1369 1370 if (maxX >= minX) { 1371 // note: alpha array will be zeroed by copyAARow() 1372 // +1 because alpha [pix_minX; pix_maxX[ 1373 // fix range [x0; x1[ 1374 // note: if x1=bboxx1, then alpha is written up to bboxx1+1 1375 // inclusive: alpha[bboxx1] ignored then cleared and 1376 // alpha[bboxx1+1] == 0 (normally so never cleared after) 1377 copyAARow(_alpha, y, minX, maxX + 1, useBlkFlags); 1378 } else if (y != lastY) { 1379 _cache.clearAARow(y); 1380 } 1381 1382 // update member: 1383 edgeCount = numCrossings; 1384 prevUseBlkFlags = useBlkFlags; 1385 1386 if (DO_STATS) { 1387 // update max used mark 1388 activeEdgeMaxUsed = _arrayMaxUsed; 1389 } 1390 } 1391 1392 boolean endRendering() { 1393 if (DO_MONITORS) { 1394 rdrCtx.stats.mon_rdr_endRendering.start(); 1395 } 1396 if (edgeMinY == Integer.MAX_VALUE) { 1397 return false; // undefined edges bounds 1398 } 1399 1400 // bounds as half-open intervals 1401 final int spminX = FloatMath.max(FloatMath.ceil_int(edgeMinX - 0.5f), boundsMinX); 1402 final int spmaxX = FloatMath.min(FloatMath.ceil_int(edgeMaxX - 0.5f), boundsMaxX); 1403 1404 // edge Min/Max Y are already rounded to subpixels within bounds: 1405 final int spminY = edgeMinY; 1406 final int spmaxY = edgeMaxY; 1407 1408 buckets_minY = spminY - boundsMinY; 1409 buckets_maxY = spmaxY - boundsMinY; 1410 1411 if (DO_LOG_BOUNDS) { 1412 MarlinUtils.logInfo("edgesXY = [" + edgeMinX + " ... " + edgeMaxX 1413 + "[ [" + edgeMinY + " ... " + edgeMaxY + "["); 1414 MarlinUtils.logInfo("spXY = [" + spminX + " ... " + spmaxX 1415 + "[ [" + spminY + " ... " + spmaxY + "["); 1416 } 1417 1418 // test clipping for shapes out of bounds 1419 if ((spminX >= spmaxX) || (spminY >= spmaxY)) { 1420 return false; 1421 } 1422 1423 // half open intervals 1424 // inclusive: 1425 final int pminX = spminX >> SUBPIXEL_LG_POSITIONS_X; 1426 // exclusive: 1427 final int pmaxX = (spmaxX + SUBPIXEL_MASK_X) >> SUBPIXEL_LG_POSITIONS_X; 1428 // inclusive: 1429 final int pminY = spminY >> SUBPIXEL_LG_POSITIONS_Y; 1430 // exclusive: 1431 final int pmaxY = (spmaxY + SUBPIXEL_MASK_Y) >> SUBPIXEL_LG_POSITIONS_Y; 1432 1433 // store BBox to answer ptg.getBBox(): 1434 this.cache.init(pminX, pminY, pmaxX, pmaxY); 1435 1436 // Heuristics for using block flags: 1437 if (ENABLE_BLOCK_FLAGS) { 1438 enableBlkFlags = this.cache.useRLE; 1439 prevUseBlkFlags = enableBlkFlags && !ENABLE_BLOCK_FLAGS_HEURISTICS; 1440 1441 if (enableBlkFlags) { 1442 // ensure blockFlags array is large enough: 1443 // note: +2 to ensure enough space left at end 1444 final int blkLen = ((pmaxX - pminX) >> BLOCK_SIZE_LG) + 2; 1445 if (blkLen > INITIAL_ARRAY) { 1446 blkFlags = blkFlags_ref.getArray(blkLen); 1447 } 1448 } 1449 } 1450 1451 // memorize the rendering bounding box: 1452 /* note: bbox_spminX and bbox_spmaxX must be pixel boundaries 1453 to have correct coverage computation */ 1454 // inclusive: 1455 bbox_spminX = pminX << SUBPIXEL_LG_POSITIONS_X; 1456 // exclusive: 1457 bbox_spmaxX = pmaxX << SUBPIXEL_LG_POSITIONS_X; 1458 // inclusive: 1459 bbox_spminY = spminY; 1460 // exclusive: 1461 bbox_spmaxY = spmaxY; 1462 1463 if (DO_LOG_BOUNDS) { 1464 MarlinUtils.logInfo("pXY = [" + pminX + " ... " + pmaxX 1465 + "[ [" + pminY + " ... " + pmaxY + "["); 1466 MarlinUtils.logInfo("bbox_spXY = [" + bbox_spminX + " ... " 1467 + bbox_spmaxX + "[ [" + bbox_spminY + " ... " 1468 + bbox_spmaxY + "["); 1469 } 1470 1471 // Prepare alpha line: 1472 // add 2 to better deal with the last pixel in a pixel row. 1473 final int width = (pmaxX - pminX) + 2; 1474 1475 // Useful when processing tile line by tile line 1476 if (width > INITIAL_AA_ARRAY) { 1477 if (DO_STATS) { 1478 rdrCtx.stats.stat_array_renderer_alphaline.add(width); 1479 } 1480 alphaLine = alphaLine_ref.getArray(width); 1481 } 1482 1483 // process first tile line: 1484 endRendering(pminY); 1485 1486 return true; 1487 } 1488 1489 private int bbox_spminX, bbox_spmaxX, bbox_spminY, bbox_spmaxY; 1490 1491 void endRendering(final int pminY) { 1492 if (DO_MONITORS) { 1493 rdrCtx.stats.mon_rdr_endRendering_Y.start(); 1494 } 1495 1496 final int spminY = pminY << SUBPIXEL_LG_POSITIONS_Y; 1497 final int fixed_spminY = FloatMath.max(bbox_spminY, spminY); 1498 1499 // avoid rendering for last call to nextTile() 1500 if (fixed_spminY < bbox_spmaxY) { 1501 // process a complete tile line ie scanlines for 32 rows 1502 final int spmaxY = FloatMath.min(bbox_spmaxY, spminY + SUBPIXEL_TILE); 1503 1504 // process tile line [0 - 32] 1505 cache.resetTileLine(pminY); 1506 1507 // Process only one tile line: 1508 _endRendering(fixed_spminY, spmaxY); 1509 } 1510 if (DO_MONITORS) { 1511 rdrCtx.stats.mon_rdr_endRendering_Y.stop(); 1512 } 1513 } 1514 1515 void copyAARow(final int[] alphaRow, 1516 final int pix_y, final int pix_from, final int pix_to, 1517 final boolean useBlockFlags) 1518 { 1519 if (DO_MONITORS) { 1520 rdrCtx.stats.mon_rdr_copyAARow.start(); 1521 } 1522 if (useBlockFlags) { 1523 if (DO_STATS) { 1524 rdrCtx.stats.hist_tile_generator_encoding.add(1); 1525 } 1526 cache.copyAARowRLE_WithBlockFlags(blkFlags, alphaRow, pix_y, pix_from, pix_to); 1527 } else { 1528 if (DO_STATS) { 1529 rdrCtx.stats.hist_tile_generator_encoding.add(0); 1530 } 1531 cache.copyAARowNoRLE(alphaRow, pix_y, pix_from, pix_to); 1532 } 1533 if (DO_MONITORS) { 1534 rdrCtx.stats.mon_rdr_copyAARow.stop(); 1535 } 1536 } 1537 }