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