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