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