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