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