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