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