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