1 /*
   2  * Copyright (c) 2007, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.java2d.marlin;
  27 
  28 import jdk.internal.misc.Unsafe;
  29 
  30 /**
  31  * An object used to cache pre-rendered complex paths.
  32  *
  33  * @see Renderer
  34  */
  35 public final class MarlinCache implements MarlinConst {
  36 
  37     static final boolean FORCE_RLE = MarlinProperties.isForceRLE();
  38     static final boolean FORCE_NO_RLE = MarlinProperties.isForceNoRLE();
  39     // minimum width to try using RLE encoding:
  40     static final int RLE_MIN_WIDTH
  41         = Math.max(BLOCK_SIZE, MarlinProperties.getRLEMinWidth());
  42     // maximum width for RLE encoding:
  43     // values are stored as int [x|alpha] where alpha is 8 bits
  44     static final int RLE_MAX_WIDTH = 1 << (24 - 1);
  45 
  46     // 2048 (pixelSize) alpha values (width) x 32 rows (tile) = 64K bytes
  47     // x1 instead of 4 bytes (RLE) ie 1/4 capacity or average good RLE compression
  48     static final long INITIAL_CHUNK_ARRAY = TILE_SIZE * INITIAL_PIXEL_DIM; // 64K
  49 
  50     // The alpha map used by this object (taken out of our map cache) to convert
  51     // pixel coverage counts gotten from MarlinCache (which are in the range
  52     // [0, maxalpha]) into alpha values, which are in [0,256).
  53     static final byte[] ALPHA_MAP;
  54 
  55     static final OffHeapArray ALPHA_MAP_UNSAFE;
  56 
  57     static {
  58         final byte[] _ALPHA_MAP = buildAlphaMap(MAX_AA_ALPHA);
  59 
  60         ALPHA_MAP_UNSAFE = new OffHeapArray(_ALPHA_MAP, _ALPHA_MAP.length); // 1K
  61         ALPHA_MAP =_ALPHA_MAP;
  62 
  63         final Unsafe _unsafe = OffHeapArray.unsafe;
  64         final long addr = ALPHA_MAP_UNSAFE.address;
  65 
  66         for (int i = 0; i < _ALPHA_MAP.length; i++) {
  67             _unsafe.putByte(addr + i, _ALPHA_MAP[i]);
  68         }
  69     }
  70 
  71     int bboxX0, bboxY0, bboxX1, bboxY1;
  72 
  73     // 1D dirty arrays
  74     // row index in rowAAChunk[]
  75     final long[] rowAAChunkIndex = new long[TILE_SIZE];
  76     // first pixel (inclusive) for each row
  77     final int[] rowAAx0 = new int[TILE_SIZE];
  78     // last pixel (exclusive) for each row
  79     final int[] rowAAx1 = new int[TILE_SIZE];
  80     // encoding mode (0=raw, 1=RLE encoding) for each row
  81     final int[] rowAAEnc = new int[TILE_SIZE];
  82     // coded length (RLE encoding) for each row
  83     final long[] rowAALen = new long[TILE_SIZE];
  84     // last position in RLE decoding for each row (getAlpha):
  85     final long[] rowAAPos = new long[TILE_SIZE];
  86 
  87     // dirty off-heap array containing pixel coverages for (32) rows (packed)
  88     // if encoding=raw, it contains alpha coverage values (val) as integer
  89     // if encoding=RLE, it contains tuples (val, last x-coordinate exclusive)
  90     // use rowAAx0/rowAAx1 to get row indices within this chunk
  91     final OffHeapArray rowAAChunk;
  92 
  93     // current position in rowAAChunk array
  94     long rowAAChunkPos;
  95 
  96     // touchedTile[i] is the sum of all the alphas in the tile with
  97     // x=j*TILE_SIZE+bboxX0.
  98     int[] touchedTile;
  99 
 100     // per-thread renderer context
 101     final RendererContext rdrCtx;
 102 
 103     // large cached touchedTile (dirty)
 104     final int[] touchedTile_initial = new int[INITIAL_ARRAY]; // 1 tile line
 105 
 106     int tileMin, tileMax;
 107 
 108     boolean useRLE = false;
 109 
 110     MarlinCache(final RendererContext rdrCtx) {
 111         this.rdrCtx = rdrCtx;
 112 
 113         rowAAChunk = new OffHeapArray(rdrCtx, INITIAL_CHUNK_ARRAY);
 114 
 115         touchedTile = touchedTile_initial;
 116 
 117         // tile used marks:
 118         tileMin = Integer.MAX_VALUE;
 119         tileMax = Integer.MIN_VALUE;
 120     }
 121 
 122     void init(int minx, int miny, int maxx, int maxy, int edgeSumDeltaY)
 123     {
 124         // assert maxy >= miny && maxx >= minx;
 125         bboxX0 = minx;
 126         bboxY0 = miny;
 127         bboxX1 = maxx;
 128         bboxY1 = maxy;
 129 
 130         final int width = (maxx - minx);
 131 
 132         if (FORCE_NO_RLE) {
 133             useRLE = false;
 134         } else if (FORCE_RLE) {
 135             useRLE = true;
 136         } else {
 137             // heuristics: use both bbox area and complexity
 138             // ie number of primitives:
 139 
 140             // fast check min and max width (maxx < 23bits):
 141             if (width <= RLE_MIN_WIDTH || width >= RLE_MAX_WIDTH) {
 142                 useRLE = false;
 143             } else {
 144                 // perimeter approach: how fit the total length into given height:
 145 
 146                 // if stroking: meanCrossings /= 2 => divide edgeSumDeltaY by 2
 147                 final int heightSubPixel
 148                     = (((maxy - miny) << SUBPIXEL_LG_POSITIONS_Y) << rdrCtx.stroking);
 149 
 150                 // check meanDist > block size:
 151                 // check width / (meanCrossings - 1) >= RLE_THRESHOLD
 152 
 153                 // fast case: (meanCrossingPerPixel <= 2) means 1 span only
 154                 useRLE = (edgeSumDeltaY <= (heightSubPixel << 1))
 155                     // note: already checked (meanCrossingPerPixel <= 2)
 156                     // rewritten to avoid division:
 157                     || (width * heightSubPixel) >
 158                             ((edgeSumDeltaY - heightSubPixel) << BLOCK_SIZE_LG);
 159 //                            ((edgeSumDeltaY - heightSubPixel) * RLE_THRESHOLD);
 160 //                            ((edgeSumDeltaY - heightSubPixel) << BLOCK_TH_LG);
 161 
 162                 if (doTrace && !useRLE) {
 163                     final float meanCrossings
 164                         = ((float) edgeSumDeltaY) / heightSubPixel;
 165                     final float meanDist = width / (meanCrossings - 1);
 166 
 167                     System.out.println("High complexity: "
 168                         + " for bbox[width = " + width
 169                         + " height = " + (maxy - miny)
 170                         + "] edgeSumDeltaY = " + edgeSumDeltaY
 171                         + " heightSubPixel = " + heightSubPixel
 172                         + " meanCrossings = "+ meanCrossings
 173                         + " meanDist = " + meanDist
 174                         + " width =  " + (width * heightSubPixel)
 175                         + " <= criteria:  " + ((edgeSumDeltaY - heightSubPixel) << BLOCK_SIZE_LG)
 176                     );
 177                 }
 178             }
 179         }
 180 
 181         // the ceiling of (maxy - miny + 1) / TILE_SIZE;
 182         final int nxTiles = (width + TILE_SIZE) >> TILE_SIZE_LG;
 183 
 184         if (nxTiles > INITIAL_ARRAY) {
 185             if (doStats) {
 186                 RendererContext.stats.stat_array_marlincache_touchedTile
 187                     .add(nxTiles);
 188             }
 189             touchedTile = rdrCtx.getIntArray(nxTiles);
 190         }
 191     }
 192 
 193     /**
 194      * Disposes this cache:
 195      * clean up before reusing this instance
 196      */
 197     void dispose() {
 198         // Reset touchedTile if needed:
 199         resetTileLine(0);
 200 
 201         // Return arrays:
 202         if (touchedTile != touchedTile_initial) {
 203             rdrCtx.putIntArray(touchedTile, 0, 0); // already zero filled
 204             touchedTile = touchedTile_initial;
 205         }
 206         // At last: resize back off-heap rowAA to initial size
 207         if (rowAAChunk.length != INITIAL_CHUNK_ARRAY) {
 208             // note: may throw OOME:
 209             rowAAChunk.resize(INITIAL_CHUNK_ARRAY);
 210         }
 211         if (doCleanDirty) {
 212             // Force zero-fill dirty arrays:
 213             rowAAChunk.fill(BYTE_0);
 214         }
 215     }
 216 
 217     void resetTileLine(final int pminY) {
 218         // update bboxY0 to process a complete tile line [0 - 32]
 219         bboxY0 = pminY;
 220 
 221         // reset current pos
 222         if (doStats) {
 223             RendererContext.stats.stat_cache_rowAAChunk.add(rowAAChunkPos);
 224         }
 225         rowAAChunkPos = 0L;
 226 
 227         // Reset touchedTile:
 228         if (tileMin != Integer.MAX_VALUE) {
 229             if (doStats) {
 230                 RendererContext.stats.stat_cache_tiles.add(tileMax - tileMin);
 231             }
 232             // clean only dirty touchedTile:
 233             if (tileMax == 1) {
 234                 touchedTile[0] = 0;
 235             } else {
 236                 IntArrayCache.fill(touchedTile, tileMin, tileMax, 0);
 237             }
 238             // reset tile used marks:
 239             tileMin = Integer.MAX_VALUE;
 240             tileMax = Integer.MIN_VALUE;
 241         }
 242 
 243         if (doCleanDirty) {
 244             // Force zero-fill dirty arrays:
 245             rowAAChunk.fill(BYTE_0);
 246         }
 247     }
 248 
 249     void clearAARow(final int y) {
 250         // process tile line [0 - 32]
 251         final int row = y - bboxY0;
 252 
 253         // update pixel range:
 254         rowAAx0[row]  = 0; // first pixel inclusive
 255         rowAAx1[row]  = 0; //  last pixel exclusive
 256         rowAAEnc[row] = 0; // raw encoding
 257 
 258         // note: leave rowAAChunkIndex[row] undefined
 259         // and rowAALen[row] & rowAAPos[row] (RLE)
 260     }
 261 
 262     /**
 263      * Copy the given alpha data into the rowAA cache
 264      * @param alphaRow alpha data to copy from
 265      * @param y y pixel coordinate
 266      * @param px0 first pixel inclusive x0
 267      * @param px1 last pixel exclusive x1
 268      */
 269     void copyAARowNoRLE(final int[] alphaRow, final int y,
 270                    final int px0, final int px1)
 271     {
 272         if (doMonitors) {
 273             RendererContext.stats.mon_rdr_copyAARow.start();
 274         }
 275 
 276         // skip useless pixels above boundary
 277         final int px_bbox1 = FloatMath.min(px1, bboxX1);
 278 
 279         if (doLogBounds) {
 280             MarlinUtils.logInfo("row = [" + px0 + " ... " + px_bbox1
 281                                 + " (" + px1 + ") [ for y=" + y);
 282         }
 283 
 284         final int row = y - bboxY0;
 285 
 286         // update pixel range:
 287         rowAAx0[row]  = px0;      // first pixel inclusive
 288         rowAAx1[row]  = px_bbox1; //  last pixel exclusive
 289         rowAAEnc[row] = 0; // raw encoding
 290 
 291         // get current position (bytes):
 292         final long pos = rowAAChunkPos;
 293         // update row index to current position:
 294         rowAAChunkIndex[row] = pos;
 295 
 296         // determine need array size (may overflow):
 297         final long needSize = pos + (px_bbox1 - px0);
 298 
 299         // update next position (bytes):
 300         rowAAChunkPos = needSize;
 301 
 302         // update row data:
 303         final OffHeapArray _rowAAChunk = rowAAChunk;
 304         // ensure rowAAChunk capacity:
 305         if (_rowAAChunk.length < needSize) {
 306             expandRowAAChunk(needSize);
 307         }
 308         if (doStats) {
 309             RendererContext.stats.stat_cache_rowAA.add(px_bbox1 - px0);
 310         }
 311 
 312         // rowAA contains only alpha values for range[x0; x1[
 313         final int[] _touchedTile = touchedTile;
 314         final int _TILE_SIZE_LG = TILE_SIZE_LG;
 315 
 316         final int from = px0      - bboxX0; // first pixel inclusive
 317         final int to   = px_bbox1 - bboxX0; //  last pixel exclusive
 318 
 319         final Unsafe _unsafe = OffHeapArray.unsafe;
 320         final long SIZE_BYTE = 1L;
 321         final long addr_alpha = ALPHA_MAP_UNSAFE.address;
 322         long addr_off = _rowAAChunk.address + pos;
 323 
 324         // compute alpha sum into rowAA:
 325         for (int x = from, val = 0; x < to; x++) {
 326             // alphaRow is in [0; MAX_COVERAGE]
 327             val += alphaRow[x]; // [from; to[
 328 
 329             // ensure values are in [0; MAX_AA_ALPHA] range
 330             if (DO_AA_RANGE_CHECK) {
 331                 if (val < 0) {
 332                     System.out.println("Invalid coverage = " + val);
 333                     val = 0;
 334                 }
 335                 if (val > MAX_AA_ALPHA) {
 336                     System.out.println("Invalid coverage = " + val);
 337                     val = MAX_AA_ALPHA;
 338                 }
 339             }
 340 
 341             // store alpha sum (as byte):
 342             if (val == 0) {
 343                 _unsafe.putByte(addr_off, (byte)0); // [0..255]
 344             } else {
 345                 _unsafe.putByte(addr_off, _unsafe.getByte(addr_alpha + val)); // [0..255]
 346 
 347                 // update touchedTile
 348                 _touchedTile[x >> _TILE_SIZE_LG] += val;
 349             }
 350             addr_off += SIZE_BYTE;
 351         }
 352 
 353         // update tile used marks:
 354         int tx = from >> _TILE_SIZE_LG; // inclusive
 355         if (tx < tileMin) {
 356             tileMin = tx;
 357         }
 358 
 359         tx = ((to - 1) >> _TILE_SIZE_LG) + 1; // exclusive (+1 to be sure)
 360         if (tx > tileMax) {
 361             tileMax = tx;
 362         }
 363 
 364         if (doLogBounds) {
 365             MarlinUtils.logInfo("clear = [" + from + " ... " + to + "[");
 366         }
 367 
 368         // Clear alpha row for reuse:
 369         IntArrayCache.fill(alphaRow, from, px1 - bboxX0, 0);
 370 
 371         if (doMonitors) {
 372             RendererContext.stats.mon_rdr_copyAARow.stop();
 373         }
 374     }
 375 
 376     void copyAARowRLE_WithBlockFlags(final int[] blkFlags, final int[] alphaRow,
 377                       final int y, final int px0, final int px1)
 378     {
 379         if (doMonitors) {
 380             RendererContext.stats.mon_rdr_copyAARow.start();
 381         }
 382 
 383         // Copy rowAA data into the piscesCache if one is present
 384         final int _bboxX0 = bboxX0;
 385 
 386         // process tile line [0 - 32]
 387         final int row  = y - bboxY0;
 388         final int from = px0 - _bboxX0; // first pixel inclusive
 389 
 390         // skip useless pixels above boundary
 391         final int px_bbox1 = FloatMath.min(px1, bboxX1);
 392         final int to       = px_bbox1 - _bboxX0; //  last pixel exclusive
 393 
 394         if (doLogBounds) {
 395             MarlinUtils.logInfo("row = [" + px0 + " ... " + px_bbox1
 396                                 + " (" + px1 + ") [ for y=" + y);
 397         }
 398 
 399         // get current position:
 400         final long initialPos = startRLERow(row, px0, px_bbox1);
 401 
 402         // determine need array size:
 403         // pessimistic: max needed size = deltaX x 4 (1 int)
 404         final int maxLen = (to - from);
 405         final long needSize = initialPos + (maxLen << 2);
 406 
 407         // update row data:
 408         OffHeapArray _rowAAChunk = rowAAChunk;
 409         // ensure rowAAChunk capacity:
 410         if (_rowAAChunk.length < needSize) {
 411             expandRowAAChunk(needSize);
 412         }
 413 
 414         final Unsafe _unsafe = OffHeapArray.unsafe;
 415         final long SIZE_INT = 4L;
 416         final long addr_alpha = ALPHA_MAP_UNSAFE.address;
 417         long addr_off = _rowAAChunk.address + initialPos;
 418 
 419         final int[] _touchedTile = touchedTile;
 420         final int _TILE_SIZE_LG = TILE_SIZE_LG;
 421         final int _BLK_SIZE_LG  = BLOCK_SIZE_LG;
 422 
 423         // traverse flagged blocks:
 424         final int blkW = (from >> _BLK_SIZE_LG);
 425         final int blkE = (to   >> _BLK_SIZE_LG) + 1;
 426 
 427         // Perform run-length encoding and store results in the piscesCache
 428         int val = 0;
 429         int cx0 = from;
 430         int runLen;
 431 
 432         final int _MAX_VALUE = Integer.MAX_VALUE;
 433         int last_t0 = _MAX_VALUE;
 434 
 435         int skip = 0;
 436 
 437         for (int t = blkW, blk_x0, blk_x1, cx, delta; t <= blkE; t++) {
 438             if (blkFlags[t] != 0) {
 439                 blkFlags[t] = 0;
 440 
 441                 if (last_t0 == _MAX_VALUE) {
 442                     last_t0 = t;
 443                 }
 444                 continue;
 445             }
 446             if (last_t0 != _MAX_VALUE) {
 447                 // emit blocks:
 448                 blk_x0 = FloatMath.max(last_t0 << _BLK_SIZE_LG, from);
 449                 last_t0 = _MAX_VALUE;
 450 
 451                 // (last block pixel+1) inclusive => +1
 452                 blk_x1 = FloatMath.min((t << _BLK_SIZE_LG) + 1, to);
 453 
 454                 for (cx = blk_x0; cx < blk_x1; cx++) {
 455                     if ((delta = alphaRow[cx]) != 0) {
 456                         alphaRow[cx] = 0;
 457 
 458                         // not first rle entry:
 459                         if (cx != cx0) {
 460                             runLen = cx - cx0;
 461 
 462                             // store alpha coverage (ensure within bounds):
 463                             // as [absX|val] where:
 464                             // absX is the absolute x-coordinate:
 465                             // note: last pixel exclusive (>= 0)
 466                             // note: it should check X is smaller than 23bits (overflow)!
 467 
 468                             // special case to encode entries into a single int:
 469                             if (val == 0) {
 470                                 _unsafe.putInt(addr_off,
 471                                     ((_bboxX0 + cx) << 8)
 472                                 );
 473                             } else {
 474                                 _unsafe.putInt(addr_off,
 475                                     ((_bboxX0 + cx) << 8)
 476                                     | (((int) _unsafe.getByte(addr_alpha + val)) & 0xFF) // [0..255]
 477                                 );
 478 
 479                                 if (runLen == 1) {
 480                                     _touchedTile[cx0 >> _TILE_SIZE_LG] += val;
 481                                 } else {
 482                                     touchTile(cx0, val, cx, runLen, _touchedTile);
 483                                 }
 484                             }
 485                             addr_off += SIZE_INT;
 486 
 487                             if (doStats) {
 488                                 RendererContext.stats.hist_tile_generator_encoding_runLen
 489                                     .add(runLen);
 490                             }
 491                             cx0 = cx;
 492                         }
 493 
 494                         // alpha value = running sum of coverage delta:
 495                         val += delta;
 496 
 497                         // ensure values are in [0; MAX_AA_ALPHA] range
 498                         if (DO_AA_RANGE_CHECK) {
 499                             if (val < 0) {
 500                                 System.out.println("Invalid coverage = " + val);
 501                                 val = 0;
 502                             }
 503                             if (val > MAX_AA_ALPHA) {
 504                                 System.out.println("Invalid coverage = " + val);
 505                                 val = MAX_AA_ALPHA;
 506                             }
 507                         }
 508                     }
 509                 }
 510             } else if (doStats) {
 511                 skip++;
 512             }
 513         }
 514 
 515         // Process remaining RLE run:
 516         runLen = to - cx0;
 517 
 518         // store alpha coverage (ensure within bounds):
 519         // as (int)[absX|val] where:
 520         // absX is the absolute x-coordinate in bits 31 to 8 and val in bits 0..7
 521         // note: last pixel exclusive (>= 0)
 522         // note: it should check X is smaller than 23bits (overflow)!
 523 
 524         // special case to encode entries into a single int:
 525         if (val == 0) {
 526             _unsafe.putInt(addr_off,
 527                 ((_bboxX0 + to) << 8)
 528             );
 529         } else {
 530             _unsafe.putInt(addr_off,
 531                 ((_bboxX0 + to) << 8)
 532                 | (((int) _unsafe.getByte(addr_alpha + val)) & 0xFF) // [0..255]
 533             );
 534 
 535             if (runLen == 1) {
 536                 _touchedTile[cx0 >> _TILE_SIZE_LG] += val;
 537             } else {
 538                 touchTile(cx0, val, to, runLen, _touchedTile);
 539             }
 540         }
 541         addr_off += SIZE_INT;
 542 
 543         if (doStats) {
 544             RendererContext.stats.hist_tile_generator_encoding_runLen
 545                 .add(runLen);
 546         }
 547 
 548         long len = (addr_off - _rowAAChunk.address);
 549 
 550         // update coded length as bytes:
 551         rowAALen[row] = (len - initialPos);
 552 
 553         // update current position:
 554         rowAAChunkPos = len;
 555 
 556         if (doStats) {
 557             RendererContext.stats.stat_cache_rowAA.add(rowAALen[row]);
 558             RendererContext.stats.hist_tile_generator_encoding_ratio.add(
 559                 (100 * skip) / (blkE - blkW)
 560             );
 561         }
 562 
 563         // update tile used marks:
 564         int tx = from >> _TILE_SIZE_LG; // inclusive
 565         if (tx < tileMin) {
 566             tileMin = tx;
 567         }
 568 
 569         tx = ((to - 1) >> _TILE_SIZE_LG) + 1; // exclusive (+1 to be sure)
 570         if (tx > tileMax) {
 571             tileMax = tx;
 572         }
 573 
 574         // Clear alpha row for reuse:
 575         if (px1 > bboxX1) {
 576             alphaRow[to    ] = 0;
 577             alphaRow[to + 1] = 0;
 578         }
 579         if (doChecks) {
 580             IntArrayCache.check(blkFlags, 0, blkFlags.length, 0);
 581             IntArrayCache.check(alphaRow, 0, alphaRow.length, 0);
 582         }
 583 
 584         if (doMonitors) {
 585             RendererContext.stats.mon_rdr_copyAARow.stop();
 586         }
 587     }
 588 
 589     long startRLERow(final int row, final int x0, final int x1) {
 590         // rows are supposed to be added by increasing y.
 591         rowAAx0[row]  = x0; // first pixel inclusive
 592         rowAAx1[row]  = x1; // last pixel exclusive
 593         rowAAEnc[row] = 1; // RLE encoding
 594         rowAAPos[row] = 0L; // position = 0
 595 
 596         // update row index to current position:
 597         return (rowAAChunkIndex[row] = rowAAChunkPos);
 598     }
 599 
 600     private void expandRowAAChunk(final long needSize) {
 601         if (doStats) {
 602             RendererContext.stats.stat_array_marlincache_rowAAChunk
 603                 .add(needSize);
 604         }
 605 
 606         // note: throw IOOB if neededSize > 2Gb:
 607         final long newSize = ArrayCache.getNewLargeSize(rowAAChunk.length, needSize);
 608 
 609         rowAAChunk.resize(newSize);
 610     }
 611 
 612     private void touchTile(final int x0, final int val, final int x1,
 613                            final int runLen,
 614                            final int[] _touchedTile)
 615     {
 616         // the x and y of the current row, minus bboxX0, bboxY0
 617         // process tile line [0 - 32]
 618         final int _TILE_SIZE_LG = TILE_SIZE_LG;
 619 
 620         // update touchedTile
 621         int tx = (x0 >> _TILE_SIZE_LG);
 622 
 623         // handle trivial case: same tile (x0, x0+runLen)
 624         if (tx == (x1 >> _TILE_SIZE_LG)) {
 625             // same tile:
 626             _touchedTile[tx] += val * runLen;
 627             return;
 628         }
 629 
 630         final int tx1 = (x1 - 1) >> _TILE_SIZE_LG;
 631 
 632         if (tx <= tx1) {
 633             final int nextTileXCoord = (tx + 1) << _TILE_SIZE_LG;
 634             _touchedTile[tx++] += val * (nextTileXCoord - x0);
 635         }
 636         if (tx < tx1) {
 637             // don't go all the way to tx1 - we need to handle the last
 638             // tile as a special case (just like we did with the first
 639             final int tileVal = (val << _TILE_SIZE_LG);
 640             for (; tx < tx1; tx++) {
 641                 _touchedTile[tx] += tileVal;
 642             }
 643         }
 644         // they will be equal unless x0 >> TILE_SIZE_LG == tx1
 645         if (tx == tx1) {
 646             final int txXCoord       =  tx      << _TILE_SIZE_LG;
 647             final int nextTileXCoord = (tx + 1) << _TILE_SIZE_LG;
 648 
 649             final int lastXCoord = (nextTileXCoord <= x1) ? nextTileXCoord : x1;
 650             _touchedTile[tx] += val * (lastXCoord - txXCoord);
 651         }
 652     }
 653 
 654     int alphaSumInTile(final int x) {
 655         return touchedTile[(x - bboxX0) >> TILE_SIZE_LG];
 656     }
 657 
 658     @Override
 659     public String toString() {
 660         return "bbox = ["
 661             + bboxX0 + ", " + bboxY0 + " => "
 662             + bboxX1 + ", " + bboxY1 + "]\n";
 663     }
 664 
 665     private static byte[] buildAlphaMap(final int maxalpha) {
 666         // double size !
 667         final byte[] alMap = new byte[maxalpha << 1];
 668         final int halfmaxalpha = maxalpha >> 2;
 669         for (int i = 0; i <= maxalpha; i++) {
 670             alMap[i] = (byte) ((i * 255 + halfmaxalpha) / maxalpha);
 671 //            System.out.println("alphaMap[" + i + "] = "
 672 //                               + Byte.toUnsignedInt(alMap[i]));
 673         }
 674         return alMap;
 675     }
 676 }