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