< prev index next >

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

Print this page




   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;


 495     // per-thread renderer context
 496     final RendererContext rdrCtx;
 497     // dirty curve
 498     private final Curve curve;
 499 
 500     // clean alpha array (zero filled)
 501     private int[] alphaLine;
 502 
 503     // alphaLine ref (clean)
 504     private final IntArrayCache.Reference alphaLine_ref;
 505 
 506     private boolean enableBlkFlags = false;
 507     private boolean prevUseBlkFlags = false;
 508 
 509     /* block flags (0|1) */
 510     private int[] blkFlags;
 511 
 512     // blkFlags ref (clean)
 513     private final IntArrayCache.Reference blkFlags_ref;
 514 
 515     Renderer(final RendererContext rdrCtx) {
 516         this.rdrCtx = rdrCtx;
 517 
 518         this.edges = rdrCtx.newOffHeapArray(INITIAL_EDGES_CAPACITY); // 96K
 519 
 520         this.curve = rdrCtx.curve;
 521 
 522         edgeBuckets_ref      = rdrCtx.newCleanIntArrayRef(INITIAL_BUCKET_ARRAY); // 64K
 523         edgeBucketCounts_ref = rdrCtx.newCleanIntArrayRef(INITIAL_BUCKET_ARRAY); // 64K
 524 
 525         edgeBuckets      = edgeBuckets_ref.initial;
 526         edgeBucketCounts = edgeBucketCounts_ref.initial;
 527 
 528         // 2048 (pixelsize) pixel large
 529         alphaLine_ref = rdrCtx.newCleanIntArrayRef(INITIAL_AA_ARRAY); // 8K
 530         alphaLine     = alphaLine_ref.initial;
 531 
 532         crossings_ref     = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K
 533         aux_crossings_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K
 534         edgePtrs_ref      = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K
 535         aux_edgePtrs_ref  = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K
 536 
 537         crossings     = crossings_ref.initial;
 538         aux_crossings = aux_crossings_ref.initial;
 539         edgePtrs      = edgePtrs_ref.initial;
 540         aux_edgePtrs  = aux_edgePtrs_ref.initial;
 541 
 542         blkFlags_ref = rdrCtx.newCleanIntArrayRef(INITIAL_ARRAY); // 1K = 1 tile line
 543         blkFlags     = blkFlags_ref.initial;
 544     }
 545 
 546     public Renderer init(final int pix_boundsX, final int pix_boundsY,
 547                   final int pix_boundsWidth, final int pix_boundsHeight,
 548                   final int windingRule)
 549     {
 550         this.windingRule = windingRule;
 551 
 552         // bounds as half-open intervals: minX <= x < maxX and minY <= y < maxY
 553         this.boundsMinX =  pix_boundsX << SUBPIXEL_LG_POSITIONS_X;
 554         this.boundsMaxX =
 555             (pix_boundsX + pix_boundsWidth) << SUBPIXEL_LG_POSITIONS_X;
 556         this.boundsMinY =  pix_boundsY << SUBPIXEL_LG_POSITIONS_Y;
 557         this.boundsMaxY =
 558             (pix_boundsY + pix_boundsHeight) << SUBPIXEL_LG_POSITIONS_Y;
 559 
 560         if (DO_LOG_BOUNDS) {
 561             MarlinUtils.logInfo("boundsXY = [" + boundsMinX + " ... "
 562                                 + boundsMaxX + "[ [" + boundsMinY + " ... "
 563                                 + boundsMaxY + "[");
 564         }
 565 
 566         // see addLine: ceil(boundsMaxY) => boundsMaxY + 1
 567         // +1 for edgeBucketCounts
 568         final int edgeBucketsLength = (boundsMaxY - boundsMinY) + 1;
 569 
 570         if (edgeBucketsLength > INITIAL_BUCKET_ARRAY) {
 571             if (DO_STATS) {
 572                 rdrCtx.stats.stat_array_renderer_edgeBuckets
 573                     .add(edgeBucketsLength);
 574                 rdrCtx.stats.stat_array_renderer_edgeBucketCounts
 575                     .add(edgeBucketsLength);
 576             }
 577             edgeBuckets = edgeBuckets_ref.getArray(edgeBucketsLength);
 578             edgeBucketCounts = edgeBucketCounts_ref.getArray(edgeBucketsLength);


 634             // unused arrays
 635             edgeBuckets = edgeBuckets_ref.putArray(edgeBuckets, 0, 0);
 636             edgeBucketCounts = edgeBucketCounts_ref.putArray(edgeBucketCounts, 0, 0);
 637         }
 638 
 639         // At last: resize back off-heap edges to initial size
 640         if (edges.length != INITIAL_EDGES_CAPACITY) {
 641             // note: may throw OOME:
 642             edges.resize(INITIAL_EDGES_CAPACITY);
 643         }
 644         if (DO_CLEAN_DIRTY) {
 645             // Force zero-fill dirty arrays:
 646             edges.fill(BYTE_0);
 647         }
 648         if (DO_MONITORS) {
 649             rdrCtx.stats.mon_rdr_endRendering.stop();
 650         }
 651     }
 652 
 653     private static float tosubpixx(final float pix_x) {
 654         return F_SUBPIXEL_POSITIONS_X * pix_x;
 655     }
 656 
 657     private static float tosubpixy(final float pix_y) {
 658         // shift y by -0.5 for fast ceil(y - 0.5):
 659         return F_SUBPIXEL_POSITIONS_Y * pix_y - 0.5f;
 660     }
 661 
 662     @Override
 663     public void moveTo(float pix_x0, float pix_y0) {
 664         closePath();
 665         final float sx = tosubpixx(pix_x0);
 666         final float sy = tosubpixy(pix_y0);
 667         this.sx0 = sx;
 668         this.sy0 = sy;
 669         this.x0 = sx;
 670         this.y0 = sy;
 671     }
 672 
 673     @Override
 674     public void lineTo(float pix_x1, float pix_y1) {
 675         final float x1 = tosubpixx(pix_x1);
 676         final float y1 = tosubpixy(pix_y1);
 677         addLine(x0, y0, x1, y1);
 678         x0 = x1;
 679         y0 = y1;


 744 
 745         // merge sort auxiliary storage:
 746         int[] _aux_crossings = this.aux_crossings;
 747         int[] _aux_edgePtrs  = this.aux_edgePtrs;
 748 
 749         // copy constants:
 750         final long _OFF_ERROR    = OFF_ERROR;
 751         final long _OFF_BUMP_X   = OFF_BUMP_X;
 752         final long _OFF_BUMP_ERR = OFF_BUMP_ERR;
 753 
 754         final long _OFF_NEXT     = OFF_NEXT;
 755         final long _OFF_YMAX     = OFF_YMAX;
 756 
 757         final int _ALL_BUT_LSB   = ALL_BUT_LSB;
 758         final int _ERR_STEP_MAX  = ERR_STEP_MAX;
 759 
 760         // unsafe I/O:
 761         final Unsafe _unsafe = OffHeapArray.UNSAFE;
 762         final long    addr0  = _edges.address;
 763         long addr;
 764         final int _SUBPIXEL_LG_POSITIONS_X = SUBPIXEL_LG_POSITIONS_X;
 765         final int _SUBPIXEL_LG_POSITIONS_Y = SUBPIXEL_LG_POSITIONS_Y;
 766         final int _SUBPIXEL_MASK_X = SUBPIXEL_MASK_X;
 767         final int _SUBPIXEL_MASK_Y = SUBPIXEL_MASK_Y;
 768         final int _SUBPIXEL_POSITIONS_X = SUBPIXEL_POSITIONS_X;
 769 
 770         final int _MIN_VALUE = Integer.MIN_VALUE;
 771         final int _MAX_VALUE = Integer.MAX_VALUE;
 772 
 773         // Now we iterate through the scanlines. We must tell emitRow the coord
 774         // of the first non-transparent pixel, so we must keep accumulators for
 775         // the first and last pixels of the section of the current pixel row
 776         // that we will emit.
 777         // We also need to accumulate pix_bbox, but the iterator does it
 778         // for us. We will just get the values from it once this loop is done
 779         int minX = _MAX_VALUE;
 780         int maxX = _MIN_VALUE;
 781 
 782         int y = ymin;
 783         int bucket = y - boundsMinY;
 784 
 785         int numCrossings = this.edgeCount;
 786         int edgePtrsLen = _edgePtrs.length;
 787         int crossingsLen = _crossings.length;
 788         int _arrayMaxUsed = activeEdgeMaxUsed;
 789         int ptrLen = 0, newCount, ptrEnd;
 790 
 791         int bucketcount, i, j, ecur;
 792         int cross, lastCross;
 793         int x0, x1, tmp, sum, prev, curx, curxo, crorientation, err;
 794         int pix_x, pix_xmaxm1, pix_xmax;
 795 
 796         int low, high, mid, prevNumCrossings;
 797         boolean useBinarySearch;
 798 
 799         final int[] _blkFlags = blkFlags;
 800         final int _BLK_SIZE_LG = BLOCK_SIZE_LG;
 801         final int _BLK_SIZE = BLOCK_SIZE;
 802 
 803         final boolean _enableBlkFlagsHeuristics = ENABLE_BLOCK_FLAGS_HEURISTICS && this.enableBlkFlags;
 804 
 805         // Use block flags if large pixel span and few crossings:
 806         // ie mean(distance between crossings) is high
 807         boolean useBlkFlags = this.prevUseBlkFlags;
 808 
 809         final int stroking = rdrCtx.stroking;
 810 
 811         int lastY = -1; // last emited row
 812 
 813 
 814         // Iteration on scanlines


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


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;


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         }


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,




   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 com.sun.javafx.geom.Rectangle;
  29 import static com.sun.marlin.OffHeapArray.SIZE_INT;
  30 import jdk.internal.misc.Unsafe;
  31 
  32 public final class RendererNoAA implements MarlinRenderer, MarlinConst {
  33 
  34     static final boolean DISABLE_RENDER = false;
  35 
  36     private static final int ALL_BUT_LSB = 0xfffffffe;
  37     private static final int ERR_STEP_MAX = 0x7fffffff; // = 2^31 - 1
  38 
  39     private static final double POWER_2_TO_32 = 0x1.0p32;
  40 








  41     // 2048 (pixelSize) pixels (height) x 8 subpixels = 64K
  42     static final int INITIAL_BUCKET_ARRAY = INITIAL_PIXEL_DIM;

  43 
  44     // crossing capacity = edges count / 4 ~ 1024
  45     static final int INITIAL_CROSSING_COUNT = INITIAL_EDGES_COUNT >> 2;
  46 
  47     // common to all types of input path segments.
  48     // OFFSET as bytes
  49     // only integer values:
  50     public static final long OFF_CURX_OR  = 0;
  51     public static final long OFF_ERROR    = OFF_CURX_OR  + SIZE_INT;
  52     public static final long OFF_BUMP_X   = OFF_ERROR    + SIZE_INT;
  53     public static final long OFF_BUMP_ERR = OFF_BUMP_X   + SIZE_INT;
  54     public static final long OFF_NEXT     = OFF_BUMP_ERR + SIZE_INT;
  55     public static final long OFF_YMAX     = OFF_NEXT     + SIZE_INT;
  56 
  57     // size of one edge in bytes
  58     public static final int SIZEOF_EDGE_BYTES = (int)(OFF_YMAX + SIZE_INT);
  59 
  60     // curve break into lines
  61     // cubic error in subpixels to decrement step
  62     private static final float CUB_DEC_ERR_SUBPIX
  63         = 1f * (1f / 8f); // 1 pixel for typical 1x1 subpixels
  64     // cubic error in subpixels to increment step
  65     private static final float CUB_INC_ERR_SUBPIX
  66         = 0.4f * (1f / 8f); // 0.4 pixel for typical 1x1 subpixels
  67 
  68     // cubic bind length to decrement step = 8 * error in subpixels
  69     // multiply by 8 = error scale factor:
  70     public static final float CUB_DEC_BND
  71         = 8f * CUB_DEC_ERR_SUBPIX;
  72     // cubic bind length to increment step = 8 * error in subpixels
  73     public static final float CUB_INC_BND
  74         = 8f * CUB_INC_ERR_SUBPIX;
  75 
  76     // cubic countlg
  77     public static final int CUB_COUNT_LG = 2;
  78     // cubic count = 2^countlg
  79     private static final int CUB_COUNT = 1 << CUB_COUNT_LG;
  80     // cubic count^2 = 4^countlg
  81     private static final int CUB_COUNT_2 = 1 << (2 * CUB_COUNT_LG);
  82     // cubic count^3 = 8^countlg
  83     private static final int CUB_COUNT_3 = 1 << (3 * CUB_COUNT_LG);
  84     // cubic dt = 1 / count
  85     private static final float CUB_INV_COUNT = 1f / CUB_COUNT;
  86     // cubic dt^2 = 1 / count^2 = 1 / 4^countlg
  87     private static final float CUB_INV_COUNT_2 = 1f / CUB_COUNT_2;
  88     // cubic dt^3 = 1 / count^3 = 1 / 8^countlg
  89     private static final float CUB_INV_COUNT_3 = 1f / CUB_COUNT_3;
  90 
  91     // quad break into lines
  92     // quadratic error in subpixels
  93     private static final float QUAD_DEC_ERR_SUBPIX
  94         = 1f * (1f / 8f); // 1 pixel for typical 1x1 subpixels
  95 
  96     // quadratic bind length to decrement step = 8 * error in subpixels
  97     public static final float QUAD_DEC_BND
  98         = 8f * QUAD_DEC_ERR_SUBPIX;
  99 
 100 //////////////////////////////////////////////////////////////////////////////
 101 //  SCAN LINE
 102 //////////////////////////////////////////////////////////////////////////////
 103     // crossings ie subpixel edge x coordinates
 104     private int[] crossings;
 105     // auxiliary storage for crossings (merge sort)
 106     private int[] aux_crossings;
 107 
 108     // indices into the segment pointer lists. They indicate the "active"
 109     // sublist in the segment lists (the portion of the list that contains
 110     // all the segments that cross the next scan line).
 111     private int edgeCount;
 112     private int[] edgePtrs;
 113     // auxiliary storage for edge pointers (merge sort)
 114     private int[] aux_edgePtrs;


 487     // per-thread renderer context
 488     final RendererContext rdrCtx;
 489     // dirty curve
 490     private final Curve curve;
 491 
 492     // clean alpha array (zero filled)
 493     private int[] alphaLine;
 494 
 495     // alphaLine ref (clean)
 496     private final IntArrayCache.Reference alphaLine_ref;
 497 
 498     private boolean enableBlkFlags = false;
 499     private boolean prevUseBlkFlags = false;
 500 
 501     /* block flags (0|1) */
 502     private int[] blkFlags;
 503 
 504     // blkFlags ref (clean)
 505     private final IntArrayCache.Reference blkFlags_ref;
 506 
 507     RendererNoAA(final RendererContext rdrCtx) {
 508         this.rdrCtx = rdrCtx;
 509 
 510         this.edges = rdrCtx.newOffHeapArray(INITIAL_EDGES_CAPACITY); // 96K
 511 
 512         this.curve = rdrCtx.curve;
 513 
 514         edgeBuckets_ref      = rdrCtx.newCleanIntArrayRef(INITIAL_BUCKET_ARRAY); // 64K
 515         edgeBucketCounts_ref = rdrCtx.newCleanIntArrayRef(INITIAL_BUCKET_ARRAY); // 64K
 516 
 517         edgeBuckets      = edgeBuckets_ref.initial;
 518         edgeBucketCounts = edgeBucketCounts_ref.initial;
 519 
 520         // 2048 (pixelsize) pixel large
 521         alphaLine_ref = rdrCtx.newCleanIntArrayRef(INITIAL_AA_ARRAY); // 8K
 522         alphaLine     = alphaLine_ref.initial;
 523 
 524         crossings_ref     = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K
 525         aux_crossings_ref = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K
 526         edgePtrs_ref      = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K
 527         aux_edgePtrs_ref  = rdrCtx.newDirtyIntArrayRef(INITIAL_CROSSING_COUNT); // 2K
 528 
 529         crossings     = crossings_ref.initial;
 530         aux_crossings = aux_crossings_ref.initial;
 531         edgePtrs      = edgePtrs_ref.initial;
 532         aux_edgePtrs  = aux_edgePtrs_ref.initial;
 533 
 534         blkFlags_ref = rdrCtx.newCleanIntArrayRef(INITIAL_ARRAY); // 1K = 1 tile line
 535         blkFlags     = blkFlags_ref.initial;
 536     }
 537 
 538     public RendererNoAA init(final int pix_boundsX, final int pix_boundsY,
 539                   final int pix_boundsWidth, final int pix_boundsHeight,
 540                   final int windingRule)
 541     {
 542         this.windingRule = windingRule;
 543 
 544         // bounds as half-open intervals: minX <= x < maxX and minY <= y < maxY
 545         this.boundsMinX = pix_boundsX;
 546         this.boundsMaxX = pix_boundsX + pix_boundsWidth;
 547         this.boundsMinY = pix_boundsY;
 548         this.boundsMaxY = pix_boundsY + pix_boundsHeight;


 549 
 550         if (DO_LOG_BOUNDS) {
 551             MarlinUtils.logInfo("boundsXY = [" + boundsMinX + " ... "
 552                                 + boundsMaxX + "[ [" + boundsMinY + " ... "
 553                                 + boundsMaxY + "[");
 554         }
 555 
 556         // see addLine: ceil(boundsMaxY) => boundsMaxY + 1
 557         // +1 for edgeBucketCounts
 558         final int edgeBucketsLength = (boundsMaxY - boundsMinY) + 1;
 559 
 560         if (edgeBucketsLength > INITIAL_BUCKET_ARRAY) {
 561             if (DO_STATS) {
 562                 rdrCtx.stats.stat_array_renderer_edgeBuckets
 563                     .add(edgeBucketsLength);
 564                 rdrCtx.stats.stat_array_renderer_edgeBucketCounts
 565                     .add(edgeBucketsLength);
 566             }
 567             edgeBuckets = edgeBuckets_ref.getArray(edgeBucketsLength);
 568             edgeBucketCounts = edgeBucketCounts_ref.getArray(edgeBucketsLength);


 624             // unused arrays
 625             edgeBuckets = edgeBuckets_ref.putArray(edgeBuckets, 0, 0);
 626             edgeBucketCounts = edgeBucketCounts_ref.putArray(edgeBucketCounts, 0, 0);
 627         }
 628 
 629         // At last: resize back off-heap edges to initial size
 630         if (edges.length != INITIAL_EDGES_CAPACITY) {
 631             // note: may throw OOME:
 632             edges.resize(INITIAL_EDGES_CAPACITY);
 633         }
 634         if (DO_CLEAN_DIRTY) {
 635             // Force zero-fill dirty arrays:
 636             edges.fill(BYTE_0);
 637         }
 638         if (DO_MONITORS) {
 639             rdrCtx.stats.mon_rdr_endRendering.stop();
 640         }
 641     }
 642 
 643     private static float tosubpixx(final float pix_x) {
 644         return pix_x;
 645     }
 646 
 647     private static float tosubpixy(final float pix_y) {
 648         // shift y by -0.5 for fast ceil(y - 0.5):
 649         return pix_y - 0.5f;
 650     }
 651 
 652     @Override
 653     public void moveTo(float pix_x0, float pix_y0) {
 654         closePath();
 655         final float sx = tosubpixx(pix_x0);
 656         final float sy = tosubpixy(pix_y0);
 657         this.sx0 = sx;
 658         this.sy0 = sy;
 659         this.x0 = sx;
 660         this.y0 = sy;
 661     }
 662 
 663     @Override
 664     public void lineTo(float pix_x1, float pix_y1) {
 665         final float x1 = tosubpixx(pix_x1);
 666         final float y1 = tosubpixy(pix_y1);
 667         addLine(x0, y0, x1, y1);
 668         x0 = x1;
 669         y0 = y1;


 734 
 735         // merge sort auxiliary storage:
 736         int[] _aux_crossings = this.aux_crossings;
 737         int[] _aux_edgePtrs  = this.aux_edgePtrs;
 738 
 739         // copy constants:
 740         final long _OFF_ERROR    = OFF_ERROR;
 741         final long _OFF_BUMP_X   = OFF_BUMP_X;
 742         final long _OFF_BUMP_ERR = OFF_BUMP_ERR;
 743 
 744         final long _OFF_NEXT     = OFF_NEXT;
 745         final long _OFF_YMAX     = OFF_YMAX;
 746 
 747         final int _ALL_BUT_LSB   = ALL_BUT_LSB;
 748         final int _ERR_STEP_MAX  = ERR_STEP_MAX;
 749 
 750         // unsafe I/O:
 751         final Unsafe _unsafe = OffHeapArray.UNSAFE;
 752         final long    addr0  = _edges.address;
 753         long addr;





 754 
 755         final int _MIN_VALUE = Integer.MIN_VALUE;
 756         final int _MAX_VALUE = Integer.MAX_VALUE;
 757 
 758         // Now we iterate through the scanlines. We must tell emitRow the coord
 759         // of the first non-transparent pixel, so we must keep accumulators for
 760         // the first and last pixels of the section of the current pixel row
 761         // that we will emit.
 762         // We also need to accumulate pix_bbox, but the iterator does it
 763         // for us. We will just get the values from it once this loop is done
 764         int minX = _MAX_VALUE;
 765         int maxX = _MIN_VALUE;
 766 
 767         int y = ymin;
 768         int bucket = y - boundsMinY;
 769 
 770         int numCrossings = this.edgeCount;
 771         int edgePtrsLen = _edgePtrs.length;
 772         int crossingsLen = _crossings.length;
 773         int _arrayMaxUsed = activeEdgeMaxUsed;
 774         int ptrLen = 0, newCount, ptrEnd;
 775 
 776         int bucketcount, i, j, ecur;
 777         int cross, lastCross;
 778         int x0, x1, tmp, sum, prev, curx, curxo, crorientation, err;

 779 
 780         int low, high, mid, prevNumCrossings;
 781         boolean useBinarySearch;
 782 
 783         final int[] _blkFlags = blkFlags;
 784         final int _BLK_SIZE_LG = BLOCK_SIZE_LG;
 785         final int _BLK_SIZE = BLOCK_SIZE;
 786 
 787         final boolean _enableBlkFlagsHeuristics = ENABLE_BLOCK_FLAGS_HEURISTICS && this.enableBlkFlags;
 788 
 789         // Use block flags if large pixel span and few crossings:
 790         // ie mean(distance between crossings) is high
 791         boolean useBlkFlags = this.prevUseBlkFlags;
 792 
 793         final int stroking = rdrCtx.stroking;
 794 
 795         int lastY = -1; // last emited row
 796 
 797 
 798         // Iteration on scanlines


1134                         // last bit contains orientation (0 or 1)
1135                         crorientation = ((curxo & 0x1) << 1) - 1;
1136 
1137                         if ((sum & 0x1) != 0) {
1138                             // TODO: perform line clipping on left-right sides
1139                             // to avoid such bound checks:
1140                             x0 = (prev > bboxx0) ? prev : bboxx0;
1141 
1142                             if (curx < bboxx1) {
1143                                 x1 = curx;
1144                             } else {
1145                                 x1 = bboxx1;
1146                                 // skip right side (fast exit loop):
1147                                 i = numCrossings;
1148                             }
1149 
1150                             if (x0 < x1) {
1151                                 x0 -= bboxx0; // turn x0, x1 from coords to indices
1152                                 x1 -= bboxx0; // in the alpha array.
1153 
1154                                 _alpha[x0] += 1;
1155                                 _alpha[x1] -= 1;
1156 
1157                                 if (useBlkFlags) {
1158                                     // flag used blocks:
1159                                     _blkFlags[x0 >> _BLK_SIZE_LG] = 1;
1160                                     _blkFlags[x1 >> _BLK_SIZE_LG] = 1;





























1161                                 }
1162                             }
1163                         }
1164 
1165                         sum += crorientation;
1166                         prev = curx;
1167                     }
1168                 } else {
1169                     // Non-zero winding rule: optimize that case (default)
1170                     // and avoid processing intermediate crossings
1171                     for (i = 1, sum = 0;; i++) {
1172                         sum += crorientation;
1173 
1174                         if (sum != 0) {
1175                             // prev = min(curx)
1176                             if (prev > curx) {
1177                                 prev = curx;
1178                             }
1179                         } else {
1180                             // TODO: perform line clipping on left-right sides
1181                             // to avoid such bound checks:
1182                             x0 = (prev > bboxx0) ? prev : bboxx0;
1183 
1184                             if (curx < bboxx1) {
1185                                 x1 = curx;
1186                             } else {
1187                                 x1 = bboxx1;
1188                                 // skip right side (fast exit loop):
1189                                 i = numCrossings;
1190                             }
1191 
1192                             if (x0 < x1) {
1193                                 x0 -= bboxx0; // turn x0, x1 from coords to indices
1194                                 x1 -= bboxx0; // in the alpha array.
1195 
1196                                 _alpha[x0] += 1;
1197                                 _alpha[x1] -= 1;
1198 
1199                                 if (useBlkFlags) {
1200                                     // flag used blocks:
1201                                     _blkFlags[x0 >> _BLK_SIZE_LG] = 1;
1202                                     _blkFlags[x1 >> _BLK_SIZE_LG] = 1;





























1203                                 }
1204                             }
1205                             prev = _MAX_VALUE;
1206                         }
1207 
1208                         if (i == numCrossings) {
1209                             break;
1210                         }
1211 
1212                         curxo = _crossings[i];
1213                         curx  =  curxo >> 1;
1214                         // to turn {0, 1} into {-1, 1}, multiply by 2 and subtract 1.
1215                         // last bit contains orientation (0 or 1)
1216                         crorientation = ((curxo & 0x1) << 1) - 1;
1217                     }
1218                 }
1219             } // numCrossings > 0
1220 
1221             // even if this last row had no crossings, alpha will be zeroed
1222             // from the last emitRow call. But this doesn't matter because
1223             // maxX < minX, so no row will be emitted to the AlphaConsumer.
1224             if (true) {
1225                 lastY = y;
1226 
1227                 // convert subpixel to pixel coordinate within boundaries:
1228                 minX = FloatMath.max(minX, bboxx0);
1229                 maxX = FloatMath.min(maxX, bboxx1);
1230 
1231                 if (maxX >= minX) {
1232                     // note: alpha array will be zeroed by copyAARow()
1233                     // +1 because alpha [pix_minX; pix_maxX[
1234                     // fix range [x0; x1[
1235                     // note: if x1=bboxx1, then alpha is written up to bboxx1+1
1236                     // inclusive: alpha[bboxx1] ignored, alpha[bboxx1+1] == 0
1237                     // (normally so never cleared below)
1238                     copyAARow(_alpha, lastY, minX, maxX + 1, useBlkFlags, ac);
1239 
1240                     // speculative for next pixel row (scanline coherence):
1241                     if (_enableBlkFlagsHeuristics) {
1242                         // Use block flags if large pixel span and few crossings:
1243                         // ie mean(distance between crossings) is larger than
1244                         // 1 block size;
1245 
1246                         // fast check width:
1247                         maxX -= minX;
1248 
1249                         // if stroking: numCrossings /= 2


1252                         useBlkFlags = (maxX > _BLK_SIZE) && (maxX >
1253                             (((numCrossings >> stroking) - 1) << _BLK_SIZE_LG));
1254 
1255                         if (DO_STATS) {
1256                             tmp = FloatMath.max(1,
1257                                     ((numCrossings >> stroking) - 1));
1258                             rdrCtx.stats.hist_tile_generator_encoding_dist
1259                                 .add(maxX / tmp);
1260                         }
1261                     }
1262                 } else {
1263                     ac.clearAlphas(lastY);
1264                 }
1265                 minX = _MAX_VALUE;
1266                 maxX = _MIN_VALUE;
1267             }
1268         } // scan line iterator
1269 
1270         // Emit final row
1271         y--;

1272 
1273         // convert subpixel to pixel coordinate within boundaries:
1274         minX = FloatMath.max(minX, bboxx0);
1275         maxX = FloatMath.min(maxX, bboxx1);
1276 
1277         if (maxX >= minX) {
1278             // note: alpha array will be zeroed by copyAARow()
1279             // +1 because alpha [pix_minX; pix_maxX[
1280             // fix range [x0; x1[
1281             // note: if x1=bboxx1, then alpha is written up to bboxx1+1
1282             // inclusive: alpha[bboxx1] ignored then cleared and
1283             // alpha[bboxx1+1] == 0 (normally so never cleared after)
1284             copyAARow(_alpha, y, minX, maxX + 1, useBlkFlags, ac);
1285         } else if (y != lastY) {
1286             ac.clearAlphas(y);
1287         }
1288 
1289         // update member:
1290         edgeCount = numCrossings;
1291         prevUseBlkFlags = useBlkFlags;
1292 
1293         if (DO_STATS) {
1294             // update max used mark
1295             activeEdgeMaxUsed = _arrayMaxUsed;


1322             spmaxY = _boundsMaxY - 1;
1323             maxY   = _boundsMaxY;
1324         }
1325         buckets_minY = spminY - _boundsMinY;
1326         buckets_maxY = maxY   - _boundsMinY;
1327 
1328         if (DO_LOG_BOUNDS) {
1329             MarlinUtils.logInfo("edgesXY = [" + edgeMinX + " ... " + edgeMaxX
1330                                 + "][" + edgeMinY + " ... " + edgeMaxY + "]");
1331             MarlinUtils.logInfo("spXY    = [" + spminX + " ... " + spmaxX
1332                                 + "][" + spminY + " ... " + spmaxY + "]");
1333         }
1334 
1335         // test clipping for shapes out of bounds
1336         if ((spminX > spmaxX) || (spminY > spmaxY)) {
1337             return;
1338         }
1339 
1340         // half open intervals
1341         // inclusive:
1342         final int pminX = spminX;
1343         // exclusive:
1344         final int pmaxX = spmaxX + 1; // +1 to ensure proper upper bound
1345         // inclusive:
1346         final int pminY = spminY;
1347         // exclusive:
1348         final int pmaxY = spmaxY + 1; // +1 to ensure proper upper bound
1349 
1350         // store BBox to answer ptg.getBBox():
1351         initConsumer(pminX, pminY, pmaxX, pmaxY);
1352 
1353         // Heuristics for using block flags:
1354         if (ENABLE_BLOCK_FLAGS) {
1355             enableBlkFlags = this.useRLE;
1356             prevUseBlkFlags = enableBlkFlags && !ENABLE_BLOCK_FLAGS_HEURISTICS;
1357 
1358             if (enableBlkFlags) {
1359                 // ensure blockFlags array is large enough:
1360                 // note: +2 to ensure enough space left at end
1361                 final int blkLen = ((pmaxX - pminX) >> BLOCK_SIZE_LG) + 2;
1362                 if (blkLen > INITIAL_ARRAY) {
1363                     blkFlags = blkFlags_ref.getArray(blkLen);
1364                 }
1365             }
1366         }
1367 
1368         // memorize the rendering bounding box:
1369         /* note: bbox_spminX and bbox_spmaxX must be pixel boundaries
1370            to have correct coverage computation */
1371         // inclusive:
1372         bbox_spminX = pminX;
1373         // exclusive:
1374         bbox_spmaxX = pmaxX;
1375         // inclusive:
1376         bbox_spminY = pminY;
1377         // exclusive:
1378         bbox_spmaxY = pmaxY;
1379 
1380         if (DO_LOG_BOUNDS) {
1381             MarlinUtils.logInfo("pXY       = [" + pminX + " ... " + pmaxX
1382                                 + "[ [" + pminY + " ... " + pmaxY + "[");
1383             MarlinUtils.logInfo("bbox_spXY = [" + bbox_spminX + " ... "
1384                                 + bbox_spmaxX + "[ [" + bbox_spminY + " ... "
1385                                 + bbox_spmaxY + "[");
1386         }
1387 
1388         // Prepare alpha line:
1389         // add 2 to better deal with the last pixel in a pixel row.
1390         final int width = (pmaxX - pminX) + 2;
1391 
1392         // Useful when processing tile line by tile line
1393         if (width > INITIAL_AA_ARRAY) {
1394             if (DO_STATS) {
1395                 rdrCtx.stats.stat_array_renderer_alphaline.add(width);
1396             }
1397             alphaLine = alphaLine_ref.getArray(width);
1398         }


1411         if (FORCE_NO_RLE) {
1412             useRLE = false;
1413         } else if (FORCE_RLE) {
1414             useRLE = true;
1415         } else {
1416             // heuristics: use both bbox area and complexity
1417             // ie number of primitives:
1418 
1419             // fast check min width:
1420             if (width <= RLE_MIN_WIDTH) {
1421                 useRLE = false;
1422             } else {
1423                 useRLE = true;
1424             }
1425         }
1426     }
1427 
1428     private int bbox_spminX, bbox_spmaxX, bbox_spminY, bbox_spmaxY;
1429 
1430     public void produceAlphas(final MarlinAlphaConsumer ac) {
1431         ac.setMaxAlpha(1);
1432 
1433         if (enableBlkFlags && !ac.supportBlockFlags()) {
1434             // consumer does not support block flag optimization:
1435             enableBlkFlags = false;
1436             prevUseBlkFlags = false;
1437         }
1438 
1439         if (DO_MONITORS) {
1440             rdrCtx.stats.mon_rdr_endRendering_Y.start();
1441         }
1442 
1443         // Process all scan lines:
1444         _endRendering(bbox_spminY, bbox_spmaxY, ac);
1445 
1446         if (DO_MONITORS) {
1447             rdrCtx.stats.mon_rdr_endRendering_Y.stop();
1448         }
1449     }
1450 
1451     void copyAARow(final int[] alphaRow,


< prev index next >