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