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