1 /*
   2  * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.java2d.marlin;
  27 
  28 import static java.lang.Math.PI;
  29 import java.util.Arrays;
  30 import sun.awt.geom.PathConsumer2D;
  31 import static sun.java2d.marlin.MarlinConst.INITIAL_EDGES_COUNT;
  32 import sun.java2d.marlin.stats.Histogram;
  33 import sun.java2d.marlin.stats.StatLong;
  34 
  35 final class Helpers implements MarlinConst {
  36 
  37     private Helpers() {
  38         throw new Error("This is a non instantiable class");
  39     }
  40 
  41     static boolean within(final float x, final float y, final float err) {
  42         final float d = y - x;
  43         return (d <= err && d >= -err);
  44     }
  45 
  46     static boolean within(final double x, final double y, final double err) {
  47         final double d = y - x;
  48         return (d <= err && d >= -err);
  49     }
  50 
  51     static int quadraticRoots(final float a, final float b,
  52                               final float c, float[] zeroes, final int off)
  53     {
  54         int ret = off;
  55         float t;
  56         if (a != 0.0f) {
  57             final float dis = b*b - 4*a*c;
  58             if (dis > 0.0f) {
  59                 final float sqrtDis = (float) Math.sqrt(dis);
  60                 // depending on the sign of b we use a slightly different
  61                 // algorithm than the traditional one to find one of the roots
  62                 // so we can avoid adding numbers of different signs (which
  63                 // might result in loss of precision).
  64                 if (b >= 0.0f) {
  65                     zeroes[ret++] = (2.0f * c) / (-b - sqrtDis);
  66                     zeroes[ret++] = (-b - sqrtDis) / (2.0f * a);
  67                 } else {
  68                     zeroes[ret++] = (-b + sqrtDis) / (2.0f * a);
  69                     zeroes[ret++] = (2.0f * c) / (-b + sqrtDis);
  70                 }
  71             } else if (dis == 0.0f) {
  72                 t = (-b) / (2.0f * a);
  73                 zeroes[ret++] = t;
  74             }
  75         } else {
  76             if (b != 0.0f) {
  77                 t = (-c) / b;
  78                 zeroes[ret++] = t;
  79             }
  80         }
  81         return ret - off;
  82     }
  83 
  84     // find the roots of g(t) = d*t^3 + a*t^2 + b*t + c in [A,B)
  85     static int cubicRootsInAB(float d, float a, float b, float c,
  86                               float[] pts, final int off,
  87                               final float A, final float B)
  88     {
  89         if (d == 0.0f) {
  90             int num = quadraticRoots(a, b, c, pts, off);
  91             return filterOutNotInAB(pts, off, num, A, B) - off;
  92         }
  93         // From Graphics Gems:
  94         // http://tog.acm.org/resources/GraphicsGems/gems/Roots3And4.c
  95         // (also from awt.geom.CubicCurve2D. But here we don't need as
  96         // much accuracy and we don't want to create arrays so we use
  97         // our own customized version).
  98 
  99         // normal form: x^3 + ax^2 + bx + c = 0
 100         a /= d;
 101         b /= d;
 102         c /= d;
 103 
 104         //  substitute x = y - A/3 to eliminate quadratic term:
 105         //     x^3 +Px + Q = 0
 106         //
 107         // Since we actually need P/3 and Q/2 for all of the
 108         // calculations that follow, we will calculate
 109         // p = P/3
 110         // q = Q/2
 111         // instead and use those values for simplicity of the code.
 112         double sq_A = a * a;
 113         double p = (1.0d/3.0d) * ((-1.0d/3.0d) * sq_A + b);
 114         double q = (1.0d/2.0d) * ((2.0d/27.0d) * a * sq_A - (1.0d/3.0d) * a * b + c);
 115 
 116         // use Cardano's formula
 117 
 118         double cb_p = p * p * p;
 119         double D = q * q + cb_p;
 120 
 121         int num;
 122         if (D < 0.0d) {
 123             // see: http://en.wikipedia.org/wiki/Cubic_function#Trigonometric_.28and_hyperbolic.29_method
 124             final double phi = (1.0d/3.0d) * Math.acos(-q / Math.sqrt(-cb_p));
 125             final double t = 2.0d * Math.sqrt(-p);
 126 
 127             pts[ off+0 ] = (float) ( t * Math.cos(phi));
 128             pts[ off+1 ] = (float) (-t * Math.cos(phi + (PI / 3.0d)));
 129             pts[ off+2 ] = (float) (-t * Math.cos(phi - (PI / 3.0d)));
 130             num = 3;
 131         } else {
 132             final double sqrt_D = Math.sqrt(D);
 133             final double u =   Math.cbrt(sqrt_D - q);
 134             final double v = - Math.cbrt(sqrt_D + q);
 135 
 136             pts[ off ] = (float) (u + v);
 137             num = 1;
 138 
 139             if (within(D, 0.0d, 1e-8d)) {
 140                 pts[off+1] = -(pts[off] / 2.0f);
 141                 num = 2;
 142             }
 143         }
 144 
 145         final float sub = (1.0f/3.0f) * a;
 146 
 147         for (int i = 0; i < num; ++i) {
 148             pts[ off+i ] -= sub;
 149         }
 150 
 151         return filterOutNotInAB(pts, off, num, A, B) - off;
 152     }
 153 
 154     static float evalCubic(final float a, final float b,
 155                            final float c, final float d,
 156                            final float t)
 157     {
 158         return t * (t * (t * a + b) + c) + d;
 159     }
 160 
 161     static float evalQuad(final float a, final float b,
 162                           final float c, final float t)
 163     {
 164         return t * (t * a + b) + c;
 165     }
 166 
 167     // returns the index 1 past the last valid element remaining after filtering
 168     static int filterOutNotInAB(float[] nums, final int off, final int len,
 169                                 final float a, final float b)
 170     {
 171         int ret = off;
 172         for (int i = off, end = off + len; i < end; i++) {
 173             if (nums[i] >= a && nums[i] < b) {
 174                 nums[ret++] = nums[i];
 175             }
 176         }
 177         return ret;
 178     }
 179 
 180     static float linelen(float x1, float y1, float x2, float y2) {
 181         final float dx = x2 - x1;
 182         final float dy = y2 - y1;
 183         return (float) Math.sqrt(dx*dx + dy*dy);
 184     }
 185 
 186     static void subdivide(float[] src, int srcoff, float[] left, int leftoff,
 187                           float[] right, int rightoff, int type)
 188     {
 189         switch(type) {
 190         case 6:
 191             Helpers.subdivideQuad(src, srcoff, left, leftoff, right, rightoff);
 192             return;
 193         case 8:
 194             Helpers.subdivideCubic(src, srcoff, left, leftoff, right, rightoff);
 195             return;
 196         default:
 197             throw new InternalError("Unsupported curve type");
 198         }
 199     }
 200 
 201     static void isort(float[] a, int off, int len) {
 202         for (int i = off + 1, end = off + len; i < end; i++) {
 203             float ai = a[i];
 204             int j = i - 1;
 205             for (; j >= off && a[j] > ai; j--) {
 206                 a[j+1] = a[j];
 207             }
 208             a[j+1] = ai;
 209         }
 210     }
 211 
 212     // Most of these are copied from classes in java.awt.geom because we need
 213     // both single and double precision variants of these functions, and Line2D,
 214     // CubicCurve2D, QuadCurve2D don't provide them.
 215     /**
 216      * Subdivides the cubic curve specified by the coordinates
 217      * stored in the <code>src</code> array at indices <code>srcoff</code>
 218      * through (<code>srcoff</code>&nbsp;+&nbsp;7) and stores the
 219      * resulting two subdivided curves into the two result arrays at the
 220      * corresponding indices.
 221      * Either or both of the <code>left</code> and <code>right</code>
 222      * arrays may be <code>null</code> or a reference to the same array
 223      * as the <code>src</code> array.
 224      * Note that the last point in the first subdivided curve is the
 225      * same as the first point in the second subdivided curve. Thus,
 226      * it is possible to pass the same array for <code>left</code>
 227      * and <code>right</code> and to use offsets, such as <code>rightoff</code>
 228      * equals (<code>leftoff</code> + 6), in order
 229      * to avoid allocating extra storage for this common point.
 230      * @param src the array holding the coordinates for the source curve
 231      * @param srcoff the offset into the array of the beginning of the
 232      * the 6 source coordinates
 233      * @param left the array for storing the coordinates for the first
 234      * half of the subdivided curve
 235      * @param leftoff the offset into the array of the beginning of the
 236      * the 6 left coordinates
 237      * @param right the array for storing the coordinates for the second
 238      * half of the subdivided curve
 239      * @param rightoff the offset into the array of the beginning of the
 240      * the 6 right coordinates
 241      * @since 1.7
 242      */
 243     static void subdivideCubic(float[] src, int srcoff,
 244                                float[] left, int leftoff,
 245                                float[] right, int rightoff)
 246     {
 247         float x1 = src[srcoff + 0];
 248         float y1 = src[srcoff + 1];
 249         float ctrlx1 = src[srcoff + 2];
 250         float ctrly1 = src[srcoff + 3];
 251         float ctrlx2 = src[srcoff + 4];
 252         float ctrly2 = src[srcoff + 5];
 253         float x2 = src[srcoff + 6];
 254         float y2 = src[srcoff + 7];
 255         if (left != null) {
 256             left[leftoff + 0] = x1;
 257             left[leftoff + 1] = y1;
 258         }
 259         if (right != null) {
 260             right[rightoff + 6] = x2;
 261             right[rightoff + 7] = y2;
 262         }
 263         x1 = (x1 + ctrlx1) / 2.0f;
 264         y1 = (y1 + ctrly1) / 2.0f;
 265         x2 = (x2 + ctrlx2) / 2.0f;
 266         y2 = (y2 + ctrly2) / 2.0f;
 267         float centerx = (ctrlx1 + ctrlx2) / 2.0f;
 268         float centery = (ctrly1 + ctrly2) / 2.0f;
 269         ctrlx1 = (x1 + centerx) / 2.0f;
 270         ctrly1 = (y1 + centery) / 2.0f;
 271         ctrlx2 = (x2 + centerx) / 2.0f;
 272         ctrly2 = (y2 + centery) / 2.0f;
 273         centerx = (ctrlx1 + ctrlx2) / 2.0f;
 274         centery = (ctrly1 + ctrly2) / 2.0f;
 275         if (left != null) {
 276             left[leftoff + 2] = x1;
 277             left[leftoff + 3] = y1;
 278             left[leftoff + 4] = ctrlx1;
 279             left[leftoff + 5] = ctrly1;
 280             left[leftoff + 6] = centerx;
 281             left[leftoff + 7] = centery;
 282         }
 283         if (right != null) {
 284             right[rightoff + 0] = centerx;
 285             right[rightoff + 1] = centery;
 286             right[rightoff + 2] = ctrlx2;
 287             right[rightoff + 3] = ctrly2;
 288             right[rightoff + 4] = x2;
 289             right[rightoff + 5] = y2;
 290         }
 291     }
 292 
 293 
 294     static void subdivideCubicAt(float t, float[] src, int srcoff,
 295                                  float[] left, int leftoff,
 296                                  float[] right, int rightoff)
 297     {
 298         float x1 = src[srcoff + 0];
 299         float y1 = src[srcoff + 1];
 300         float ctrlx1 = src[srcoff + 2];
 301         float ctrly1 = src[srcoff + 3];
 302         float ctrlx2 = src[srcoff + 4];
 303         float ctrly2 = src[srcoff + 5];
 304         float x2 = src[srcoff + 6];
 305         float y2 = src[srcoff + 7];
 306         if (left != null) {
 307             left[leftoff + 0] = x1;
 308             left[leftoff + 1] = y1;
 309         }
 310         if (right != null) {
 311             right[rightoff + 6] = x2;
 312             right[rightoff + 7] = y2;
 313         }
 314         x1 = x1 + t * (ctrlx1 - x1);
 315         y1 = y1 + t * (ctrly1 - y1);
 316         x2 = ctrlx2 + t * (x2 - ctrlx2);
 317         y2 = ctrly2 + t * (y2 - ctrly2);
 318         float centerx = ctrlx1 + t * (ctrlx2 - ctrlx1);
 319         float centery = ctrly1 + t * (ctrly2 - ctrly1);
 320         ctrlx1 = x1 + t * (centerx - x1);
 321         ctrly1 = y1 + t * (centery - y1);
 322         ctrlx2 = centerx + t * (x2 - centerx);
 323         ctrly2 = centery + t * (y2 - centery);
 324         centerx = ctrlx1 + t * (ctrlx2 - ctrlx1);
 325         centery = ctrly1 + t * (ctrly2 - ctrly1);
 326         if (left != null) {
 327             left[leftoff + 2] = x1;
 328             left[leftoff + 3] = y1;
 329             left[leftoff + 4] = ctrlx1;
 330             left[leftoff + 5] = ctrly1;
 331             left[leftoff + 6] = centerx;
 332             left[leftoff + 7] = centery;
 333         }
 334         if (right != null) {
 335             right[rightoff + 0] = centerx;
 336             right[rightoff + 1] = centery;
 337             right[rightoff + 2] = ctrlx2;
 338             right[rightoff + 3] = ctrly2;
 339             right[rightoff + 4] = x2;
 340             right[rightoff + 5] = y2;
 341         }
 342     }
 343 
 344     static void subdivideQuad(float[] src, int srcoff,
 345                               float[] left, int leftoff,
 346                               float[] right, int rightoff)
 347     {
 348         float x1 = src[srcoff + 0];
 349         float y1 = src[srcoff + 1];
 350         float ctrlx = src[srcoff + 2];
 351         float ctrly = src[srcoff + 3];
 352         float x2 = src[srcoff + 4];
 353         float y2 = src[srcoff + 5];
 354         if (left != null) {
 355             left[leftoff + 0] = x1;
 356             left[leftoff + 1] = y1;
 357         }
 358         if (right != null) {
 359             right[rightoff + 4] = x2;
 360             right[rightoff + 5] = y2;
 361         }
 362         x1 = (x1 + ctrlx) / 2.0f;
 363         y1 = (y1 + ctrly) / 2.0f;
 364         x2 = (x2 + ctrlx) / 2.0f;
 365         y2 = (y2 + ctrly) / 2.0f;
 366         ctrlx = (x1 + x2) / 2.0f;
 367         ctrly = (y1 + y2) / 2.0f;
 368         if (left != null) {
 369             left[leftoff + 2] = x1;
 370             left[leftoff + 3] = y1;
 371             left[leftoff + 4] = ctrlx;
 372             left[leftoff + 5] = ctrly;
 373         }
 374         if (right != null) {
 375             right[rightoff + 0] = ctrlx;
 376             right[rightoff + 1] = ctrly;
 377             right[rightoff + 2] = x2;
 378             right[rightoff + 3] = y2;
 379         }
 380     }
 381 
 382     static void subdivideQuadAt(float t, float[] src, int srcoff,
 383                                 float[] left, int leftoff,
 384                                 float[] right, int rightoff)
 385     {
 386         float x1 = src[srcoff + 0];
 387         float y1 = src[srcoff + 1];
 388         float ctrlx = src[srcoff + 2];
 389         float ctrly = src[srcoff + 3];
 390         float x2 = src[srcoff + 4];
 391         float y2 = src[srcoff + 5];
 392         if (left != null) {
 393             left[leftoff + 0] = x1;
 394             left[leftoff + 1] = y1;
 395         }
 396         if (right != null) {
 397             right[rightoff + 4] = x2;
 398             right[rightoff + 5] = y2;
 399         }
 400         x1 = x1 + t * (ctrlx - x1);
 401         y1 = y1 + t * (ctrly - y1);
 402         x2 = ctrlx + t * (x2 - ctrlx);
 403         y2 = ctrly + t * (y2 - ctrly);
 404         ctrlx = x1 + t * (x2 - x1);
 405         ctrly = y1 + t * (y2 - y1);
 406         if (left != null) {
 407             left[leftoff + 2] = x1;
 408             left[leftoff + 3] = y1;
 409             left[leftoff + 4] = ctrlx;
 410             left[leftoff + 5] = ctrly;
 411         }
 412         if (right != null) {
 413             right[rightoff + 0] = ctrlx;
 414             right[rightoff + 1] = ctrly;
 415             right[rightoff + 2] = x2;
 416             right[rightoff + 3] = y2;
 417         }
 418     }
 419 
 420     static void subdivideAt(float t, float[] src, int srcoff,
 421                             float[] left, int leftoff,
 422                             float[] right, int rightoff, int size)
 423     {
 424         switch(size) {
 425         case 8:
 426             subdivideCubicAt(t, src, srcoff, left, leftoff, right, rightoff);
 427             return;
 428         case 6:
 429             subdivideQuadAt(t, src, srcoff, left, leftoff, right, rightoff);
 430             return;
 431         }
 432     }
 433 
 434     // From sun.java2d.loops.GeneralRenderer:
 435 
 436     static int outcode(final float x, final float y,
 437                        final float[] clipRect)
 438     {
 439         int code;
 440         if (y < clipRect[0]) {
 441             code = OUTCODE_TOP;
 442         } else if (y >= clipRect[1]) {
 443             code = OUTCODE_BOTTOM;
 444         } else {
 445             code = 0;
 446         }
 447         if (x < clipRect[2]) {
 448             code |= OUTCODE_LEFT;
 449         } else if (x >= clipRect[3]) {
 450             code |= OUTCODE_RIGHT;
 451         }
 452         return code;
 453     }
 454 
 455     // a stack of polynomial curves where each curve shares endpoints with
 456     // adjacent ones.
 457     static final class PolyStack {
 458         private static final byte TYPE_LINETO  = (byte) 0;
 459         private static final byte TYPE_QUADTO  = (byte) 1;
 460         private static final byte TYPE_CUBICTO = (byte) 2;
 461 
 462         // curves capacity = edges count (8192) = edges x 2 (coords)
 463         private static final int INITIAL_CURVES_COUNT = INITIAL_EDGES_COUNT << 1;
 464 
 465         // types capacity = edges count (4096)
 466         private static final int INITIAL_TYPES_COUNT = INITIAL_EDGES_COUNT;
 467 
 468         float[] curves;
 469         int end;
 470         byte[] curveTypes;
 471         int numCurves;
 472 
 473         // curves ref (dirty)
 474         final FloatArrayCache.Reference curves_ref;
 475         // curveTypes ref (dirty)
 476         final ByteArrayCache.Reference curveTypes_ref;
 477 
 478         // used marks (stats only)
 479         int curveTypesUseMark;
 480         int curvesUseMark;
 481 
 482         private final StatLong stat_polystack_types;
 483         private final StatLong stat_polystack_curves;
 484         private final Histogram hist_polystack_curves;
 485         private final StatLong stat_array_polystack_curves;
 486         private final StatLong stat_array_polystack_curveTypes;
 487 
 488         PolyStack(final RendererContext rdrCtx) {
 489             this(rdrCtx, null, null, null, null, null);
 490         }
 491 
 492         PolyStack(final RendererContext rdrCtx,
 493                   final StatLong stat_polystack_types,
 494                   final StatLong stat_polystack_curves,
 495                   final Histogram hist_polystack_curves,
 496                   final StatLong stat_array_polystack_curves,
 497                   final StatLong stat_array_polystack_curveTypes)
 498         {
 499             curves_ref = rdrCtx.newDirtyFloatArrayRef(INITIAL_CURVES_COUNT); // 32K
 500             curves     = curves_ref.initial;
 501 
 502             curveTypes_ref = rdrCtx.newDirtyByteArrayRef(INITIAL_TYPES_COUNT); // 4K
 503             curveTypes     = curveTypes_ref.initial;
 504             numCurves = 0;
 505             end = 0;
 506 
 507             if (DO_STATS) {
 508                 curveTypesUseMark = 0;
 509                 curvesUseMark = 0;
 510             }
 511             this.stat_polystack_types = stat_polystack_types;
 512             this.stat_polystack_curves = stat_polystack_curves;
 513             this.hist_polystack_curves = hist_polystack_curves;
 514             this.stat_array_polystack_curves = stat_array_polystack_curves;
 515             this.stat_array_polystack_curveTypes = stat_array_polystack_curveTypes;
 516         }
 517 
 518         /**
 519          * Disposes this PolyStack:
 520          * clean up before reusing this instance
 521          */
 522         void dispose() {
 523             end = 0;
 524             numCurves = 0;
 525 
 526             if (DO_STATS) {
 527                 stat_polystack_types.add(curveTypesUseMark);
 528                 stat_polystack_curves.add(curvesUseMark);
 529                 hist_polystack_curves.add(curvesUseMark);
 530 
 531                 // reset marks
 532                 curveTypesUseMark = 0;
 533                 curvesUseMark = 0;
 534             }
 535 
 536             // Return arrays:
 537             // curves and curveTypes are kept dirty
 538             curves     = curves_ref.putArray(curves);
 539             curveTypes = curveTypes_ref.putArray(curveTypes);
 540         }
 541 
 542         private void ensureSpace(final int n) {
 543             // use substraction to avoid integer overflow:
 544             if (curves.length - end < n) {
 545                 if (DO_STATS) {
 546                     stat_array_polystack_curves.add(end + n);
 547                 }
 548                 curves = curves_ref.widenArray(curves, end, end + n);
 549             }
 550             if (curveTypes.length <= numCurves) {
 551                 if (DO_STATS) {
 552                     stat_array_polystack_curveTypes.add(numCurves + 1);
 553                 }
 554                 curveTypes = curveTypes_ref.widenArray(curveTypes,
 555                                                        numCurves,
 556                                                        numCurves + 1);
 557             }
 558         }
 559 
 560         void pushCubic(float x0, float y0,
 561                        float x1, float y1,
 562                        float x2, float y2)
 563         {
 564             ensureSpace(6);
 565             curveTypes[numCurves++] = TYPE_CUBICTO;
 566             // we reverse the coordinate order to make popping easier
 567             final float[] _curves = curves;
 568             int e = end;
 569             _curves[e++] = x2;    _curves[e++] = y2;
 570             _curves[e++] = x1;    _curves[e++] = y1;
 571             _curves[e++] = x0;    _curves[e++] = y0;
 572             end = e;
 573         }
 574 
 575         void pushQuad(float x0, float y0,
 576                       float x1, float y1)
 577         {
 578             ensureSpace(4);
 579             curveTypes[numCurves++] = TYPE_QUADTO;
 580             final float[] _curves = curves;
 581             int e = end;
 582             _curves[e++] = x1;    _curves[e++] = y1;
 583             _curves[e++] = x0;    _curves[e++] = y0;
 584             end = e;
 585         }
 586 
 587         void pushLine(float x, float y) {
 588             ensureSpace(2);
 589             curveTypes[numCurves++] = TYPE_LINETO;
 590             curves[end++] = x;    curves[end++] = y;
 591         }
 592 
 593         void pullAll(final PathConsumer2D io) {
 594             final int nc = numCurves;
 595             if (nc == 0) {
 596                 return;
 597             }
 598             if (DO_STATS) {
 599                 // update used marks:
 600                 if (numCurves > curveTypesUseMark) {
 601                     curveTypesUseMark = numCurves;
 602                 }
 603                 if (end > curvesUseMark) {
 604                     curvesUseMark = end;
 605                 }
 606             }
 607             final byte[]  _curveTypes = curveTypes;
 608             final float[] _curves = curves;
 609             int e = 0;
 610 
 611             for (int i = 0; i < nc; i++) {
 612                 switch(_curveTypes[i]) {
 613                 case TYPE_LINETO:
 614                     io.lineTo(_curves[e], _curves[e+1]);
 615                     e += 2;
 616                     continue;
 617                 case TYPE_QUADTO:
 618                     io.quadTo(_curves[e+0], _curves[e+1],
 619                               _curves[e+2], _curves[e+3]);
 620                     e += 4;
 621                     continue;
 622                 case TYPE_CUBICTO:
 623                     io.curveTo(_curves[e+0], _curves[e+1],
 624                                _curves[e+2], _curves[e+3],
 625                                _curves[e+4], _curves[e+5]);
 626                     e += 6;
 627                     continue;
 628                 default:
 629                 }
 630             }
 631             numCurves = 0;
 632             end = 0;
 633         }
 634 
 635         void popAll(final PathConsumer2D io) {
 636             int nc = numCurves;
 637             if (nc == 0) {
 638                 return;
 639             }
 640             if (DO_STATS) {
 641                 // update used marks:
 642                 if (numCurves > curveTypesUseMark) {
 643                     curveTypesUseMark = numCurves;
 644                 }
 645                 if (end > curvesUseMark) {
 646                     curvesUseMark = end;
 647                 }
 648             }
 649             final byte[]  _curveTypes = curveTypes;
 650             final float[] _curves = curves;
 651             int e  = end;
 652 
 653             while (nc != 0) {
 654                 switch(_curveTypes[--nc]) {
 655                 case TYPE_LINETO:
 656                     e -= 2;
 657                     io.lineTo(_curves[e], _curves[e+1]);
 658                     continue;
 659                 case TYPE_QUADTO:
 660                     e -= 4;
 661                     io.quadTo(_curves[e+0], _curves[e+1],
 662                               _curves[e+2], _curves[e+3]);
 663                     continue;
 664                 case TYPE_CUBICTO:
 665                     e -= 6;
 666                     io.curveTo(_curves[e+0], _curves[e+1],
 667                                _curves[e+2], _curves[e+3],
 668                                _curves[e+4], _curves[e+5]);
 669                     continue;
 670                 default:
 671                 }
 672             }
 673             numCurves = 0;
 674             end = 0;
 675         }
 676 
 677         @Override
 678         public String toString() {
 679             String ret = "";
 680             int nc = numCurves;
 681             int last = end;
 682             int len;
 683             while (nc != 0) {
 684                 switch(curveTypes[--nc]) {
 685                 case TYPE_LINETO:
 686                     len = 2;
 687                     ret += "line: ";
 688                     break;
 689                 case TYPE_QUADTO:
 690                     len = 4;
 691                     ret += "quad: ";
 692                     break;
 693                 case TYPE_CUBICTO:
 694                     len = 6;
 695                     ret += "cubic: ";
 696                     break;
 697                 default:
 698                     len = 0;
 699                 }
 700                 last -= len;
 701                 ret += Arrays.toString(Arrays.copyOfRange(curves, last, last+len))
 702                                        + "\n";
 703             }
 704             return ret;
 705         }
 706     }
 707 
 708     // a stack of integer indices
 709     static final class IndexStack {
 710 
 711         // integer capacity = edges count / 4 ~ 1024
 712         private static final int INITIAL_COUNT = INITIAL_EDGES_COUNT >> 2;
 713 
 714         private int end;
 715         private int[] indices;
 716 
 717         // indices ref (dirty)
 718         private final IntArrayCache.Reference indices_ref;
 719 
 720         // used marks (stats only)
 721         private int indicesUseMark;
 722 
 723         private final StatLong stat_idxstack_indices;
 724         private final Histogram hist_idxstack_indices;
 725         private final StatLong stat_array_idxstack_indices;
 726 
 727         IndexStack(final RendererContext rdrCtx) {
 728             this(rdrCtx, null, null, null);
 729         }
 730 
 731         IndexStack(final RendererContext rdrCtx,
 732                    final StatLong stat_idxstack_indices,
 733                    final Histogram hist_idxstack_indices,
 734                    final StatLong stat_array_idxstack_indices)
 735         {
 736             indices_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_COUNT); // 4K
 737             indices     = indices_ref.initial;
 738             end = 0;
 739 
 740             if (DO_STATS) {
 741                 indicesUseMark = 0;
 742             }
 743             this.stat_idxstack_indices = stat_idxstack_indices;
 744             this.hist_idxstack_indices = hist_idxstack_indices;
 745             this.stat_array_idxstack_indices = stat_array_idxstack_indices;
 746         }
 747 
 748         /**
 749          * Disposes this PolyStack:
 750          * clean up before reusing this instance
 751          */
 752         void dispose() {
 753             end = 0;
 754 
 755             if (DO_STATS) {
 756                 stat_idxstack_indices.add(indicesUseMark);
 757                 hist_idxstack_indices.add(indicesUseMark);
 758 
 759                 // reset marks
 760                 indicesUseMark = 0;
 761             }
 762 
 763             // Return arrays:
 764             // values is kept dirty
 765             indices = indices_ref.putArray(indices);
 766         }
 767 
 768         boolean isEmpty() {
 769             return (end == 0);
 770         }
 771 
 772         void reset() {
 773             end = 0;
 774         }
 775 
 776         void push(final int v) {
 777             // remove redundant values (reverse order):
 778             int[] _values = indices;
 779             final int nc = end;
 780             if (nc != 0) {
 781                 if (_values[nc - 1] == v) {
 782                     // remove both duplicated values:
 783                     end--;
 784                     return;
 785                 }
 786             }
 787             if (_values.length <= nc) {
 788                 if (DO_STATS) {
 789                     stat_array_idxstack_indices.add(nc + 1);
 790                 }
 791                 indices = _values = indices_ref.widenArray(_values, nc, nc + 1);
 792             }
 793             _values[end++] = v;
 794 
 795             if (DO_STATS) {
 796                 // update used marks:
 797                 if (end > indicesUseMark) {
 798                     indicesUseMark = end;
 799                 }
 800             }
 801         }
 802 
 803         void pullAll(final float[] points, final PathConsumer2D io) {
 804             final int nc = end;
 805             if (nc == 0) {
 806                 return;
 807             }
 808             final int[] _values = indices;
 809 
 810             for (int i = 0, j; i < nc; i++) {
 811                 j = _values[i] << 1;
 812                 io.lineTo(points[j], points[j + 1]);
 813             }
 814             end = 0;
 815         }
 816     }
 817 }