< prev index next >

openjfx9/modules/javafx.graphics/src/main/java/com/sun/marlin/Renderer.java

Print this page




   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 java.util.Arrays;
  29 import sun.awt.geom.PathConsumer2D;
  30 import static sun.java2d.marlin.OffHeapArray.SIZE_INT;
  31 import jdk.internal.misc.Unsafe;
  32 
  33 final class Renderer implements PathConsumer2D, MarlinConst {
  34 
  35     static final boolean DISABLE_RENDER = false;
  36 
  37     static final boolean ENABLE_BLOCK_FLAGS = MarlinProperties.isUseTileFlags();
  38     static final boolean ENABLE_BLOCK_FLAGS_HEURISTICS = MarlinProperties.isUseTileFlagsWithHeuristics();
  39 
  40     private static final int ALL_BUT_LSB = 0xfffffffe;
  41     private static final int ERR_STEP_MAX = 0x7fffffff; // = 2^31 - 1
  42 
  43     private static final double POWER_2_TO_32 = 0x1.0p32;
  44 
  45     // use float to make tosubpix methods faster (no int to float conversion)
  46     public static final float F_SUBPIXEL_POSITIONS_X
  47         = (float) SUBPIXEL_POSITIONS_X;
  48     public static final float F_SUBPIXEL_POSITIONS_Y
  49         = (float) SUBPIXEL_POSITIONS_Y;
  50     public static final int SUBPIXEL_MASK_X = SUBPIXEL_POSITIONS_X - 1;
  51     public static final int SUBPIXEL_MASK_Y = SUBPIXEL_POSITIONS_Y - 1;
  52 
  53     // number of subpixels corresponding to a tile line
  54     private static final int SUBPIXEL_TILE
  55         = TILE_SIZE << SUBPIXEL_LG_POSITIONS_Y;
  56 
  57     // 2048 (pixelSize) pixels (height) x 8 subpixels = 64K
  58     static final int INITIAL_BUCKET_ARRAY
  59         = INITIAL_PIXEL_DIM * SUBPIXEL_POSITIONS_Y;
  60 
  61     // crossing capacity = edges count / 8 ~ 512
  62     static final int INITIAL_CROSSING_COUNT = INITIAL_EDGES_COUNT >> 3;
  63 
  64     public static final int WIND_EVEN_ODD = 0;
  65     public static final int WIND_NON_ZERO = 1;
  66 
  67     // common to all types of input path segments.
  68     // OFFSET as bytes
  69     // only integer values:
  70     public static final long OFF_CURX_OR  = 0;
  71     public static final long OFF_ERROR    = OFF_CURX_OR  + SIZE_INT;
  72     public static final long OFF_BUMP_X   = OFF_ERROR    + SIZE_INT;
  73     public static final long OFF_BUMP_ERR = OFF_BUMP_X   + SIZE_INT;
  74     public static final long OFF_NEXT     = OFF_BUMP_ERR + SIZE_INT;
  75     public static final long OFF_YMAX     = OFF_NEXT     + SIZE_INT;
  76 
  77     // size of one edge in bytes
  78     public static final int SIZEOF_EDGE_BYTES = (int)(OFF_YMAX + SIZE_INT);
  79 
  80     // curve break into lines
  81     // cubic error in subpixels to decrement step
  82     private static final float CUB_DEC_ERR_SUBPIX
  83         = 2.5f * (NORM_SUBPIXELS / 8f); // 2.5 subpixel for typical 8x8 subpixels
  84     // cubic error in subpixels to increment step
  85     private static final float CUB_INC_ERR_SUBPIX
  86         = 1f * (NORM_SUBPIXELS / 8f); // 1 subpixel for typical 8x8 subpixels
  87 
  88     // cubic bind length to decrement step = 8 * error in subpixels
  89     // pisces: 20 / 8
  90     // openjfx pisces: 8 / 3.2
  91     // multiply by 8 = error scale factor:
  92     public static final float CUB_DEC_BND
  93         = 8f * CUB_DEC_ERR_SUBPIX; // 20f means 2.5 subpixel error
  94     // cubic bind length to increment step = 8 * error in subpixels
  95     public static final float CUB_INC_BND
  96         = 8f * CUB_INC_ERR_SUBPIX; // 8f means 1 subpixel error
  97 
  98     // cubic countlg
  99     public static final int CUB_COUNT_LG = 2;
 100     // cubic count = 2^countlg
 101     private static final int CUB_COUNT = 1 << CUB_COUNT_LG;
 102     // cubic count^2 = 4^countlg
 103     private static final int CUB_COUNT_2 = 1 << (2 * CUB_COUNT_LG);
 104     // cubic count^3 = 8^countlg
 105     private static final int CUB_COUNT_3 = 1 << (3 * CUB_COUNT_LG);
 106     // cubic dt = 1 / count
 107     private static final float CUB_INV_COUNT = 1f / CUB_COUNT;
 108     // cubic dt^2 = 1 / count^2 = 1 / 4^countlg
 109     private static final float CUB_INV_COUNT_2 = 1f / CUB_COUNT_2;
 110     // cubic dt^3 = 1 / count^3 = 1 / 8^countlg
 111     private static final float CUB_INV_COUNT_3 = 1f / CUB_COUNT_3;
 112 
 113     // quad break into lines
 114     // quadratic error in subpixels
 115     private static final float QUAD_DEC_ERR_SUBPIX
 116         = 1f * (NORM_SUBPIXELS / 8f); // 1 subpixel for typical 8x8 subpixels
 117 
 118     // quadratic bind length to decrement step = 8 * error in subpixels
 119     // pisces and openjfx pisces: 32
 120     public static final float QUAD_DEC_BND
 121         = 8f * QUAD_DEC_ERR_SUBPIX; // 8f means 1 subpixel error
 122 
 123 //////////////////////////////////////////////////////////////////////////////
 124 //  SCAN LINE
 125 //////////////////////////////////////////////////////////////////////////////
 126     // crossings ie subpixel edge x coordinates
 127     private int[] crossings;
 128     // auxiliary storage for crossings (merge sort)
 129     private int[] aux_crossings;
 130 
 131     // indices into the segment pointer lists. They indicate the "active"
 132     // sublist in the segment lists (the portion of the list that contains
 133     // all the segments that cross the next scan line).
 134     private int edgeCount;
 135     private int[] edgePtrs;
 136     // auxiliary storage for edge pointers (merge sort)
 137     private int[] aux_edgePtrs;
 138 
 139     // max used for both edgePtrs and crossings (stats only)
 140     private int activeEdgeMaxUsed;
 141 


 148     private final IntArrayCache.Reference aux_crossings_ref;
 149     // aux_edgePtrs ref (dirty)
 150     private final IntArrayCache.Reference aux_edgePtrs_ref;
 151 
 152 //////////////////////////////////////////////////////////////////////////////
 153 //  EDGE LIST
 154 //////////////////////////////////////////////////////////////////////////////
 155     private int edgeMinY = Integer.MAX_VALUE;
 156     private int edgeMaxY = Integer.MIN_VALUE;
 157     private float edgeMinX = Float.POSITIVE_INFINITY;
 158     private float edgeMaxX = Float.NEGATIVE_INFINITY;
 159 
 160     // edges [floats|ints] stored in off-heap memory
 161     private final OffHeapArray edges;
 162 
 163     private int[] edgeBuckets;
 164     private int[] edgeBucketCounts; // 2*newedges + (1 if pruning needed)
 165     // used range for edgeBuckets / edgeBucketCounts
 166     private int buckets_minY;
 167     private int buckets_maxY;
 168     // sum of each edge delta Y (subpixels)
 169     private int edgeSumDeltaY;
 170 
 171     // edgeBuckets ref (clean)
 172     private final IntArrayCache.Reference edgeBuckets_ref;
 173     // edgeBucketCounts ref (clean)
 174     private final IntArrayCache.Reference edgeBucketCounts_ref;
 175 


 176     // Flattens using adaptive forward differencing. This only carries out
 177     // one iteration of the AFD loop. All it does is update AFD variables (i.e.
 178     // X0, Y0, D*[X|Y], COUNT; not variables used for computing scanline crossings).
 179     private void quadBreakIntoLinesAndAdd(float x0, float y0,
 180                                           final Curve c,
 181                                           final float x2, final float y2)
 182     {
 183         int count = 1; // dt = 1 / count
 184 
 185         // maximum(ddX|Y) = norm(dbx, dby) * dt^2 (= 1)
 186         float maxDD = FloatMath.max(Math.abs(c.dbx), Math.abs(c.dby));
 187 
 188         final float _DEC_BND = QUAD_DEC_BND;
 189 
 190         while (maxDD >= _DEC_BND) {
 191             // divide step by half:
 192             maxDD /= 4f; // error divided by 2^2 = 4
 193 
 194             count <<= 1;
 195             if (DO_STATS) {


 467 
 468         final int _boundsMinY = boundsMinY;
 469 
 470         // each bucket is a linked list. this method adds ptr to the
 471         // start of the "bucket"th linked list.
 472         final int bucketIdx = firstCrossing - _boundsMinY;
 473 
 474         // pointer from bucket
 475         _unsafe.putInt(addr, _edgeBuckets[bucketIdx]);
 476         addr += SIZE_INT;
 477         // y max (inclusive)
 478         _unsafe.putInt(addr,  lastCrossing);
 479 
 480         // Update buckets:
 481         // directly the edge struct "pointer"
 482         _edgeBuckets[bucketIdx]       = edgePtr;
 483         _edgeBucketCounts[bucketIdx] += 2; // 1 << 1
 484         // last bit means edge end
 485         _edgeBucketCounts[lastCrossing - _boundsMinY] |= 0x1;
 486 
 487         // update sum of delta Y (subpixels):
 488         edgeSumDeltaY += (lastCrossing - firstCrossing);
 489 
 490         // update free pointer (ie length in bytes)
 491         _edges.used += _SIZEOF_EDGE_BYTES;
 492 
 493         if (DO_MONITORS) {
 494             rdrCtx.stats.mon_rdr_addLine.stop();
 495         }
 496     }
 497 
 498 // END EDGE LIST
 499 //////////////////////////////////////////////////////////////////////////////
 500 
 501     // Cache to store RLE-encoded coverage mask of the current primitive
 502     final MarlinCache cache;
 503 
 504     // Bounds of the drawing region, at subpixel precision.
 505     private int boundsMinX, boundsMinY, boundsMaxX, boundsMaxY;
 506 
 507     // Current winding rule
 508     private int windingRule;
 509 
 510     // Current drawing position, i.e., final point of last segment
 511     private float x0, y0;
 512 
 513     // Position of most recent 'moveTo' command
 514     private float sx0, sy0;
 515 
 516     // per-thread renderer context
 517     final RendererContext rdrCtx;
 518     // dirty curve
 519     private final Curve curve;
 520 
 521     // clean alpha array (zero filled)
 522     private int[] alphaLine;
 523 


 533     // blkFlags ref (clean)
 534     private final IntArrayCache.Reference blkFlags_ref;
 535 
 536     Renderer(final RendererContext rdrCtx) {
 537         this.rdrCtx = rdrCtx;
 538 
 539         this.edges = rdrCtx.newOffHeapArray(INITIAL_EDGES_CAPACITY); // 96K
 540 
 541         this.curve = rdrCtx.curve;
 542 
 543         edgeBuckets_ref      = rdrCtx.newCleanIntArrayRef(INITIAL_BUCKET_ARRAY); // 64K
 544         edgeBucketCounts_ref = rdrCtx.newCleanIntArrayRef(INITIAL_BUCKET_ARRAY); // 64K
 545 
 546         edgeBuckets      = edgeBuckets_ref.initial;
 547         edgeBucketCounts = edgeBucketCounts_ref.initial;
 548 
 549         // 2048 (pixelsize) pixel large
 550         alphaLine_ref = rdrCtx.newCleanIntArrayRef(INITIAL_AA_ARRAY); // 8K
 551         alphaLine     = alphaLine_ref.initial;
 552 
 553         this.cache = rdrCtx.cache;
 554 
 555         crossings_ref     = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K
 556         aux_crossings_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K
 557         edgePtrs_ref      = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K
 558         aux_edgePtrs_ref  = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K
 559 
 560         crossings     = crossings_ref.initial;
 561         aux_crossings = aux_crossings_ref.initial;
 562         edgePtrs      = edgePtrs_ref.initial;
 563         aux_edgePtrs  = aux_edgePtrs_ref.initial;
 564 
 565         blkFlags_ref = rdrCtx.newCleanIntArrayRef(INITIAL_ARRAY); // 1K = 1 tile line
 566         blkFlags     = blkFlags_ref.initial;
 567     }
 568 
 569     Renderer init(final int pix_boundsX, final int pix_boundsY,
 570                   final int pix_boundsWidth, final int pix_boundsHeight,
 571                   final int windingRule) {
 572 
 573         this.windingRule = windingRule;
 574 
 575         // bounds as half-open intervals: minX <= x < maxX and minY <= y < maxY
 576         this.boundsMinX =  pix_boundsX << SUBPIXEL_LG_POSITIONS_X;
 577         this.boundsMaxX =
 578             (pix_boundsX + pix_boundsWidth) << SUBPIXEL_LG_POSITIONS_X;
 579         this.boundsMinY =  pix_boundsY << SUBPIXEL_LG_POSITIONS_Y;
 580         this.boundsMaxY =
 581             (pix_boundsY + pix_boundsHeight) << SUBPIXEL_LG_POSITIONS_Y;
 582 
 583         if (DO_LOG_BOUNDS) {
 584             MarlinUtils.logInfo("boundsXY = [" + boundsMinX + " ... "
 585                                 + boundsMaxX + "[ [" + boundsMinY + " ... "
 586                                 + boundsMaxY + "[");
 587         }
 588 
 589         // see addLine: ceil(boundsMaxY) => boundsMaxY + 1
 590         // +1 for edgeBucketCounts
 591         final int edgeBucketsLength = (boundsMaxY - boundsMinY) + 1;
 592 


 594             if (DO_STATS) {
 595                 rdrCtx.stats.stat_array_renderer_edgeBuckets
 596                     .add(edgeBucketsLength);
 597                 rdrCtx.stats.stat_array_renderer_edgeBucketCounts
 598                     .add(edgeBucketsLength);
 599             }
 600             edgeBuckets = edgeBuckets_ref.getArray(edgeBucketsLength);
 601             edgeBucketCounts = edgeBucketCounts_ref.getArray(edgeBucketsLength);
 602         }
 603 
 604         edgeMinY = Integer.MAX_VALUE;
 605         edgeMaxY = Integer.MIN_VALUE;
 606         edgeMinX = Float.POSITIVE_INFINITY;
 607         edgeMaxX = Float.NEGATIVE_INFINITY;
 608 
 609         // reset used mark:
 610         edgeCount = 0;
 611         activeEdgeMaxUsed = 0;
 612         edges.used = 0;
 613 
 614         edgeSumDeltaY = 0;


 615 
 616         return this; // fluent API
 617     }
 618 
 619     /**
 620      * Disposes this renderer and recycle it clean up before reusing this instance
 621      */
 622     void dispose() {
 623         if (DO_STATS) {
 624             rdrCtx.stats.stat_rdr_activeEdges.add(activeEdgeMaxUsed);
 625             rdrCtx.stats.stat_rdr_edges.add(edges.used);
 626             rdrCtx.stats.stat_rdr_edges_count.add(edges.used / SIZEOF_EDGE_BYTES);
 627             rdrCtx.stats.hist_rdr_edges_count.add(edges.used / SIZEOF_EDGE_BYTES);
 628             rdrCtx.stats.totalOffHeap += edges.length;
 629         }
 630         // Return arrays:
 631         crossings = crossings_ref.putArray(crossings);
 632         aux_crossings = aux_crossings_ref.putArray(aux_crossings);
 633 
 634         edgePtrs = edgePtrs_ref.putArray(edgePtrs);
 635         aux_edgePtrs = aux_edgePtrs_ref.putArray(aux_edgePtrs);
 636 
 637         alphaLine = alphaLine_ref.putArray(alphaLine, 0, 0); // already zero filled
 638         blkFlags  = blkFlags_ref.putArray(blkFlags, 0, 0); // already zero filled
 639 
 640         if (edgeMinY != Integer.MAX_VALUE) {
 641             // if context is maked as DIRTY:
 642             if (rdrCtx.dirty) {


 717     @Override
 718     public void quadTo(float x1, float y1, float x2, float y2) {
 719         final float xe = tosubpixx(x2);
 720         final float ye = tosubpixy(y2);
 721         curve.set(x0, y0, tosubpixx(x1), tosubpixy(y1), xe, ye);
 722         quadBreakIntoLinesAndAdd(x0, y0, curve, xe, ye);
 723         x0 = xe;
 724         y0 = ye;
 725     }
 726 
 727     @Override
 728     public void closePath() {
 729         addLine(x0, y0, sx0, sy0);
 730         x0 = sx0;
 731         y0 = sy0;
 732     }
 733 
 734     @Override
 735     public void pathDone() {
 736         closePath();
 737     }
 738 
 739     @Override
 740     public long getNativeConsumer() {
 741         throw new InternalError("Renderer does not use a native consumer.");
 742     }
 743 
 744     private void _endRendering(final int ymin, final int ymax) {


 745         if (DISABLE_RENDER) {
 746             return;
 747         }
 748 
 749         // Get X bounds as true pixel boundaries to compute correct pixel coverage:
 750         final int bboxx0 = bbox_spminX;
 751         final int bboxx1 = bbox_spmaxX;
 752 
 753         final boolean windingRuleEvenOdd = (windingRule == WIND_EVEN_ODD);
 754 
 755         // Useful when processing tile line by tile line
 756         final int[] _alpha = alphaLine;
 757 
 758         // local vars (performance):
 759         final MarlinCache _cache = cache;
 760         final OffHeapArray _edges = edges;
 761         final int[] _edgeBuckets = edgeBuckets;
 762         final int[] _edgeBucketCounts = edgeBucketCounts;
 763 
 764         int[] _crossings = this.crossings;
 765         int[] _edgePtrs  = this.edgePtrs;
 766 
 767         // merge sort auxiliary storage:
 768         int[] _aux_crossings = this.aux_crossings;
 769         int[] _aux_edgePtrs  = this.aux_edgePtrs;
 770 
 771         // copy constants:
 772         final long _OFF_ERROR    = OFF_ERROR;
 773         final long _OFF_BUMP_X   = OFF_BUMP_X;
 774         final long _OFF_BUMP_ERR = OFF_BUMP_ERR;
 775 
 776         final long _OFF_NEXT     = OFF_NEXT;
 777         final long _OFF_YMAX     = OFF_YMAX;
 778 
 779         final int _ALL_BUT_LSB   = ALL_BUT_LSB;


1159                 prev = curx = x0;
1160                 // to turn {0, 1} into {-1, 1}, multiply by 2 and subtract 1.
1161                 // last bit contains orientation (0 or 1)
1162                 crorientation = ((curxo & 0x1) << 1) - 1;
1163 
1164                 if (windingRuleEvenOdd) {
1165                     sum = crorientation;
1166 
1167                     // Even Odd winding rule: take care of mask ie sum(orientations)
1168                     for (i = 1; i < numCrossings; i++) {
1169                         curxo = _crossings[i];
1170                         curx  =  curxo >> 1;
1171                         // to turn {0, 1} into {-1, 1}, multiply by 2 and subtract 1.
1172                         // last bit contains orientation (0 or 1)
1173                         crorientation = ((curxo & 0x1) << 1) - 1;
1174 
1175                         if ((sum & 0x1) != 0) {
1176                             // TODO: perform line clipping on left-right sides
1177                             // to avoid such bound checks:
1178                             x0 = (prev > bboxx0) ? prev : bboxx0;
1179                             x1 = (curx < bboxx1) ? curx : bboxx1;







1180 
1181                             if (x0 < x1) {
1182                                 x0 -= bboxx0; // turn x0, x1 from coords to indices
1183                                 x1 -= bboxx0; // in the alpha array.
1184 
1185                                 pix_x      =  x0      >> _SUBPIXEL_LG_POSITIONS_X;
1186                                 pix_xmaxm1 = (x1 - 1) >> _SUBPIXEL_LG_POSITIONS_X;
1187 
1188                                 if (pix_x == pix_xmaxm1) {
1189                                     // Start and end in same pixel
1190                                     tmp = (x1 - x0); // number of subpixels
1191                                     _alpha[pix_x    ] += tmp;
1192                                     _alpha[pix_x + 1] -= tmp;
1193 
1194                                     if (useBlkFlags) {
1195                                         // flag used blocks:
1196                                         _blkFlags[pix_x >> _BLK_SIZE_LG] = 1;

1197                                     }
1198                                 } else {
1199                                     tmp = (x0 & _SUBPIXEL_MASK_X);
1200                                     _alpha[pix_x    ]
1201                                         += (_SUBPIXEL_POSITIONS_X - tmp);
1202                                     _alpha[pix_x + 1]
1203                                         += tmp;
1204 
1205                                     pix_xmax = x1 >> _SUBPIXEL_LG_POSITIONS_X;
1206 
1207                                     tmp = (x1 & _SUBPIXEL_MASK_X);
1208                                     _alpha[pix_xmax    ]
1209                                         -= (_SUBPIXEL_POSITIONS_X - tmp);
1210                                     _alpha[pix_xmax + 1]
1211                                         -= tmp;
1212 
1213                                     if (useBlkFlags) {
1214                                         // flag used blocks:
1215                                         _blkFlags[pix_x    >> _BLK_SIZE_LG] = 1;
1216                                         _blkFlags[pix_xmax >> _BLK_SIZE_LG] = 1;


1217                                     }
1218                                 }
1219                             }
1220                         }
1221 
1222                         sum += crorientation;
1223                         prev = curx;
1224                     }
1225                 } else {
1226                     // Non-zero winding rule: optimize that case (default)
1227                     // and avoid processing intermediate crossings
1228                     for (i = 1, sum = 0;; i++) {
1229                         sum += crorientation;
1230 
1231                         if (sum != 0) {
1232                             // prev = min(curx)
1233                             if (prev > curx) {
1234                                 prev = curx;
1235                             }
1236                         } else {
1237                             // TODO: perform line clipping on left-right sides
1238                             // to avoid such bound checks:
1239                             x0 = (prev > bboxx0) ? prev : bboxx0;
1240                             x1 = (curx < bboxx1) ? curx : bboxx1;







1241 
1242                             if (x0 < x1) {
1243                                 x0 -= bboxx0; // turn x0, x1 from coords to indices
1244                                 x1 -= bboxx0; // in the alpha array.
1245 
1246                                 pix_x      =  x0      >> _SUBPIXEL_LG_POSITIONS_X;
1247                                 pix_xmaxm1 = (x1 - 1) >> _SUBPIXEL_LG_POSITIONS_X;
1248 
1249                                 if (pix_x == pix_xmaxm1) {
1250                                     // Start and end in same pixel
1251                                     tmp = (x1 - x0); // number of subpixels
1252                                     _alpha[pix_x    ] += tmp;
1253                                     _alpha[pix_x + 1] -= tmp;
1254 
1255                                     if (useBlkFlags) {
1256                                         // flag used blocks:
1257                                         _blkFlags[pix_x >> _BLK_SIZE_LG] = 1;

1258                                     }
1259                                 } else {
1260                                     tmp = (x0 & _SUBPIXEL_MASK_X);
1261                                     _alpha[pix_x    ]
1262                                         += (_SUBPIXEL_POSITIONS_X - tmp);
1263                                     _alpha[pix_x + 1]
1264                                         += tmp;
1265 
1266                                     pix_xmax = x1 >> _SUBPIXEL_LG_POSITIONS_X;
1267 
1268                                     tmp = (x1 & _SUBPIXEL_MASK_X);
1269                                     _alpha[pix_xmax    ]
1270                                         -= (_SUBPIXEL_POSITIONS_X - tmp);
1271                                     _alpha[pix_xmax + 1]
1272                                         -= tmp;
1273 
1274                                     if (useBlkFlags) {
1275                                         // flag used blocks:
1276                                         _blkFlags[pix_x    >> _BLK_SIZE_LG] = 1;
1277                                         _blkFlags[pix_xmax >> _BLK_SIZE_LG] = 1;


1278                                     }
1279                                 }
1280                             }
1281                             prev = _MAX_VALUE;
1282                         }
1283 
1284                         if (i == numCrossings) {
1285                             break;
1286                         }
1287 
1288                         curxo = _crossings[i];
1289                         curx  =  curxo >> 1;
1290                         // to turn {0, 1} into {-1, 1}, multiply by 2 and subtract 1.
1291                         // last bit contains orientation (0 or 1)
1292                         crorientation = ((curxo & 0x1) << 1) - 1;
1293                     }
1294                 }
1295             } // numCrossings > 0
1296 
1297             // even if this last row had no crossings, alpha will be zeroed
1298             // from the last emitRow call. But this doesn't matter because
1299             // maxX < minX, so no row will be emitted to the MarlinCache.
1300             if ((y & _SUBPIXEL_MASK_Y) == _SUBPIXEL_MASK_Y) {
1301                 lastY = y >> _SUBPIXEL_LG_POSITIONS_Y;
1302 
1303                 // convert subpixel to pixel coordinate within boundaries:
1304                 minX = FloatMath.max(minX, bboxx0) >> _SUBPIXEL_LG_POSITIONS_X;
1305                 maxX = FloatMath.min(maxX, bboxx1) >> _SUBPIXEL_LG_POSITIONS_X;
1306 
1307                 if (maxX >= minX) {
1308                     // note: alpha array will be zeroed by copyAARow()
1309                     // +2 because alpha [pix_minX; pix_maxX+1]
1310                     // fix range [x0; x1[
1311                     copyAARow(_alpha, lastY, minX, maxX + 2, useBlkFlags);



1312 
1313                     // speculative for next pixel row (scanline coherence):
1314                     if (_enableBlkFlagsHeuristics) {
1315                         // Use block flags if large pixel span and few crossings:
1316                         // ie mean(distance between crossings) is larger than
1317                         // 1 block size;
1318 
1319                         // fast check width:
1320                         maxX -= minX;
1321 
1322                         // if stroking: numCrossings /= 2
1323                         // => shift numCrossings by 1
1324                         // condition = (width / (numCrossings - 1)) > blockSize
1325                         useBlkFlags = (maxX > _BLK_SIZE) && (maxX >
1326                             (((numCrossings >> stroking) - 1) << _BLK_SIZE_LG));
1327 
1328                         if (DO_STATS) {
1329                             tmp = FloatMath.max(1,
1330                                     ((numCrossings >> stroking) - 1));
1331                             rdrCtx.stats.hist_tile_generator_encoding_dist
1332                                 .add(maxX / tmp);
1333                         }
1334                     }
1335                 } else {
1336                     _cache.clearAARow(lastY);
1337                 }
1338                 minX = _MAX_VALUE;
1339                 maxX = _MIN_VALUE;
1340             }
1341         } // scan line iterator
1342 
1343         // Emit final row
1344         y--;
1345         y >>= _SUBPIXEL_LG_POSITIONS_Y;
1346 
1347         // convert subpixel to pixel coordinate within boundaries:
1348         minX = FloatMath.max(minX, bboxx0) >> _SUBPIXEL_LG_POSITIONS_X;
1349         maxX = FloatMath.min(maxX, bboxx1) >> _SUBPIXEL_LG_POSITIONS_X;
1350 
1351         if (maxX >= minX) {
1352             // note: alpha array will be zeroed by copyAARow()
1353             // +2 because alpha [pix_minX; pix_maxX+1]
1354             // fix range [x0; x1[
1355             copyAARow(_alpha, y, minX, maxX + 2, useBlkFlags);



1356         } else if (y != lastY) {
1357             _cache.clearAARow(y);
1358         }
1359 
1360         // update member:
1361         edgeCount = numCrossings;
1362         prevUseBlkFlags = useBlkFlags;
1363 
1364         if (DO_STATS) {
1365             // update max used mark
1366             activeEdgeMaxUsed = _arrayMaxUsed;
1367         }
1368     }
1369 
1370     boolean endRendering() {
1371         if (DO_MONITORS) {
1372             rdrCtx.stats.mon_rdr_endRendering.start();
1373         }
1374         if (edgeMinY == Integer.MAX_VALUE) {
1375             return false; // undefined edges bounds
1376         }
1377 
1378         final int _boundsMinY = boundsMinY;
1379         final int _boundsMaxY = boundsMaxY;
1380 
1381         // bounds as inclusive intervals
1382         final int spminX = FloatMath.max(FloatMath.ceil_int(edgeMinX - 0.5f), boundsMinX);
1383         final int spmaxX = FloatMath.min(FloatMath.ceil_int(edgeMaxX - 0.5f), boundsMaxX - 1);
1384 
1385         // edge Min/Max Y are already rounded to subpixels within bounds:
1386         final int spminY = edgeMinY;
1387         final int spmaxY;
1388         int maxY = edgeMaxY;
1389 
1390         if (maxY <= _boundsMaxY - 1) {
1391             spmaxY = maxY;
1392         } else {
1393             spmaxY = _boundsMaxY - 1;
1394             maxY   = _boundsMaxY;
1395         }
1396         buckets_minY = spminY - _boundsMinY;
1397         buckets_maxY = maxY   - _boundsMinY;
1398 
1399         if (DO_LOG_BOUNDS) {
1400             MarlinUtils.logInfo("edgesXY = [" + edgeMinX + " ... " + edgeMaxX
1401                                 + "][" + edgeMinY + " ... " + edgeMaxY + "]");
1402             MarlinUtils.logInfo("spXY    = [" + spminX + " ... " + spmaxX
1403                                 + "][" + spminY + " ... " + spmaxY + "]");
1404         }
1405 
1406         // test clipping for shapes out of bounds
1407         if ((spminX > spmaxX) || (spminY > spmaxY)) {
1408             return false;
1409         }
1410 
1411         // half open intervals
1412         // inclusive:
1413         final int pminX =  spminX                    >> SUBPIXEL_LG_POSITIONS_X;
1414         // exclusive:
1415         final int pmaxX = (spmaxX + SUBPIXEL_MASK_X) >> SUBPIXEL_LG_POSITIONS_X;
1416         // inclusive:
1417         final int pminY =  spminY                    >> SUBPIXEL_LG_POSITIONS_Y;
1418         // exclusive:
1419         final int pmaxY = (spmaxY + SUBPIXEL_MASK_Y) >> SUBPIXEL_LG_POSITIONS_Y;
1420 
1421         // store BBox to answer ptg.getBBox():
1422         this.cache.init(pminX, pminY, pmaxX, pmaxY, edgeSumDeltaY);
1423 
1424         // Heuristics for using block flags:
1425         if (ENABLE_BLOCK_FLAGS) {
1426             enableBlkFlags = this.cache.useRLE;
1427             prevUseBlkFlags = enableBlkFlags && !ENABLE_BLOCK_FLAGS_HEURISTICS;
1428 
1429             if (enableBlkFlags) {
1430                 // ensure blockFlags array is large enough:
1431                 // note: +2 to ensure enough space left at end
1432                 final int nxTiles = ((pmaxX - pminX) >> TILE_SIZE_LG) + 2;
1433                 if (nxTiles > INITIAL_ARRAY) {
1434                     blkFlags = blkFlags_ref.getArray(nxTiles);
1435                 }
1436             }
1437         }
1438 
1439         // memorize the rendering bounding box:
1440         /* note: bbox_spminX and bbox_spmaxX must be pixel boundaries
1441            to have correct coverage computation */
1442         // inclusive:
1443         bbox_spminX = pminX << SUBPIXEL_LG_POSITIONS_X;
1444         // exclusive:
1445         bbox_spmaxX = pmaxX << SUBPIXEL_LG_POSITIONS_X;
1446         // inclusive:
1447         bbox_spminY = spminY;
1448         // exclusive:
1449         bbox_spmaxY = FloatMath.min(spmaxY + 1, pmaxY << SUBPIXEL_LG_POSITIONS_Y);
1450 
1451         if (DO_LOG_BOUNDS) {
1452             MarlinUtils.logInfo("pXY       = [" + pminX + " ... " + pmaxX
1453                                 + "[ [" + pminY + " ... " + pmaxY + "[");
1454             MarlinUtils.logInfo("bbox_spXY = [" + bbox_spminX + " ... "
1455                                 + bbox_spmaxX + "[ [" + bbox_spminY + " ... "
1456                                 + bbox_spmaxY + "[");
1457         }
1458 
1459         // Prepare alpha line:
1460         // add 2 to better deal with the last pixel in a pixel row.
1461         final int width = (pmaxX - pminX) + 2;
1462 
1463         // Useful when processing tile line by tile line
1464         if (width > INITIAL_AA_ARRAY) {
1465             if (DO_STATS) {
1466                 rdrCtx.stats.stat_array_renderer_alphaline.add(width);
1467             }
1468             alphaLine = alphaLine_ref.getArray(width);
1469         }

1470 
1471         // process first tile line:
1472         endRendering(pminY);















1473 
1474         return true;






1475     }
1476 
1477     private int bbox_spminX, bbox_spmaxX, bbox_spminY, bbox_spmaxY;
1478 
1479     void endRendering(final int pminY) {








1480         if (DO_MONITORS) {
1481             rdrCtx.stats.mon_rdr_endRendering_Y.start();
1482         }
1483 
1484         final int spminY       = pminY << SUBPIXEL_LG_POSITIONS_Y;
1485         final int fixed_spminY = FloatMath.max(bbox_spminY, spminY);
1486 
1487         // avoid rendering for last call to nextTile()
1488         if (fixed_spminY < bbox_spmaxY) {
1489             // process a complete tile line ie scanlines for 32 rows
1490             final int spmaxY = FloatMath.min(bbox_spmaxY, spminY + SUBPIXEL_TILE);
1491 
1492             // process tile line [0 - 32]
1493             cache.resetTileLine(pminY);
1494 
1495             // Process only one tile line:
1496             _endRendering(fixed_spminY, spmaxY);
1497         }
1498         if (DO_MONITORS) {
1499             rdrCtx.stats.mon_rdr_endRendering_Y.stop();
1500         }
1501     }
1502 
1503     void copyAARow(final int[] alphaRow,
1504                    final int pix_y, final int pix_from, final int pix_to,
1505                    final boolean useBlockFlags)

1506     {







1507         if (useBlockFlags) {
1508             if (DO_STATS) {
1509                 rdrCtx.stats.hist_tile_generator_encoding.add(1);
1510             }
1511             cache.copyAARowRLE_WithBlockFlags(blkFlags, alphaRow, pix_y, pix_from, pix_to);
1512         } else {
1513             if (DO_STATS) {
1514                 rdrCtx.stats.hist_tile_generator_encoding.add(0);
1515             }
1516             cache.copyAARowNoRLE(alphaRow, pix_y, pix_from, pix_to);



1517         }























1518     }
1519 }


   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 


 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) {


 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 


 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 


 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) {


 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;


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 }
< prev index next >