summaryrefslogtreecommitdiff
path: root/src/java.desktop/share/classes/sun/java2d/marlin/MarlinTileGenerator.java
blob: 9df9f7cfacaf2c1cf189ac75a068ac42b80bc38e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
/*
 * Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package sun.java2d.marlin;

import java.util.Arrays;
import sun.java2d.pipe.AATileGenerator;
import jdk.internal.misc.Unsafe;

final class MarlinTileGenerator implements AATileGenerator, MarlinConst {

    private static final boolean DISABLE_BLEND = false;

    private static final int MAX_TILE_ALPHA_SUM = TILE_W * TILE_H * MAX_AA_ALPHA;

    private static final int TH_AA_ALPHA_FILL_EMPTY = ((MAX_AA_ALPHA + 1) / 3); // 33%
    private static final int TH_AA_ALPHA_FILL_FULL  = ((MAX_AA_ALPHA + 1) * 2 / 3); // 66%

    private static final int FILL_TILE_W = TILE_W >> 1; // half tile width

    static {
        if (MAX_TILE_ALPHA_SUM <= 0) {
            throw new IllegalStateException("Invalid MAX_TILE_ALPHA_SUM: " + MAX_TILE_ALPHA_SUM);
        }
        if (DO_TRACE) {
            MarlinUtils.logInfo("MAX_AA_ALPHA           : " + MAX_AA_ALPHA);
            MarlinUtils.logInfo("TH_AA_ALPHA_FILL_EMPTY : " + TH_AA_ALPHA_FILL_EMPTY);
            MarlinUtils.logInfo("TH_AA_ALPHA_FILL_FULL  : " + TH_AA_ALPHA_FILL_FULL);
            MarlinUtils.logInfo("FILL_TILE_W            : " + FILL_TILE_W);
        }
    }

    private final Renderer renderer;
    private final MarlinCache cache;
    private int x, y;

    // per-thread renderer stats
    final RendererStats rdrStats;

    MarlinTileGenerator(final RendererStats stats, final Renderer r,
                        final MarlinCache cache)
    {
        this.rdrStats = stats;
        this.renderer = r;
        this.cache = cache;
    }

    MarlinTileGenerator init() {
        this.x = cache.bboxX0;
        this.y = cache.bboxY0;

        return this; // fluent API
    }

    /**
     * Disposes this tile generator:
     * clean up before reusing this instance
     */
    @Override
    public void dispose() {
        if (DO_MONITORS) {
            // called from AAShapePipe.renderTiles() (render tiles end):
            rdrStats.mon_pipe_renderTiles.stop();
        }
        // dispose cache:
        cache.dispose();
        // dispose renderer and recycle the RendererContext instance:
        renderer.dispose();
    }

    void getBbox(int[] bbox) {
        bbox[0] = cache.bboxX0;
        bbox[1] = cache.bboxY0;
        bbox[2] = cache.bboxX1;
        bbox[3] = cache.bboxY1;
    }

    /**
     * Gets the width of the tiles that the generator batches output into.
     * @return the width of the standard alpha tile
     */
    @Override
    public int getTileWidth() {
        if (DO_MONITORS) {
            // called from AAShapePipe.renderTiles() (render tiles start):
            rdrStats.mon_pipe_renderTiles.start();
        }
        return TILE_W;
    }

    /**
     * Gets the height of the tiles that the generator batches output into.
     * @return the height of the standard alpha tile
     */
    @Override
    public int getTileHeight() {
        return TILE_H;
    }

    /**
     * Gets the typical alpha value that will characterize the current
     * tile.
     * The answer may be 0x00 to indicate that the current tile has
     * no coverage in any of its pixels, or it may be 0xff to indicate
     * that the current tile is completely covered by the path, or any
     * other value to indicate non-trivial coverage cases.
     * @return 0x00 for no coverage, 0xff for total coverage, or any other
     *         value for partial coverage of the tile
     */
    @Override
    public int getTypicalAlpha() {
        if (DISABLE_BLEND) {
            // always return empty tiles to disable blending operations
            return 0x00;
        }
        int al = cache.alphaSumInTile(x);
        // Note: if we have a filled rectangle that doesn't end on a tile
        // border, we could still return 0xff, even though al!=maxTileAlphaSum
        // This is because if we return 0xff, our users will fill a rectangle
        // starting at x,y that has width = Math.min(TILE_SIZE, bboxX1-x),
        // and height min(TILE_SIZE,bboxY1-y), which is what should happen.
        // However, to support this, we would have to use 2 Math.min's
        // and 2 multiplications per tile, instead of just 2 multiplications
        // to compute maxTileAlphaSum. The savings offered would probably
        // not be worth it, considering how rare this case is.
        // Note: I have not tested this, so in the future if it is determined
        // that it is worth it, it should be implemented. Perhaps this method's
        // interface should be changed to take arguments the width and height
        // of the current tile. This would eliminate the 2 Math.min calls that
        // would be needed here, since our caller needs to compute these 2
        // values anyway.
        final int alpha = (al == 0x00 ? 0x00
                              : (al == MAX_TILE_ALPHA_SUM ? 0xff : 0x80));
        if (DO_STATS) {
            rdrStats.hist_tile_generator_alpha.add(alpha);
        }
        return alpha;
    }

    /**
     * Skips the current tile and moves on to the next tile.
     * Either this method, or the getAlpha() method should be called
     * once per tile, but not both.
     */
    @Override
    public void nextTile() {
        if ((x += TILE_W) >= cache.bboxX1) {
            x = cache.bboxX0;
            y += TILE_H;

            if (y < cache.bboxY1) {
                // compute for the tile line
                // [ y; max(y + TILE_SIZE, bboxY1) ]
                renderer.endRendering(y);
            }
        }
    }

    /**
     * Gets the alpha coverage values for the current tile.
     * Either this method, or the nextTile() method should be called
     * once per tile, but not both.
     */
    @Override
    public void getAlpha(final byte[] tile, final int offset,
                                            final int rowstride)
    {
        if (cache.useRLE) {
            getAlphaRLE(tile, offset, rowstride);
        } else {
            getAlphaNoRLE(tile, offset, rowstride);
        }
    }

    /**
     * Gets the alpha coverage values for the current tile.
     * Either this method, or the nextTile() method should be called
     * once per tile, but not both.
     */
    private void getAlphaNoRLE(final byte[] tile, final int offset,
                               final int rowstride)
    {
        if (DO_MONITORS) {
            rdrStats.mon_ptg_getAlpha.start();
        }

        // local vars for performance:
        final MarlinCache _cache = this.cache;
        final long[] rowAAChunkIndex = _cache.rowAAChunkIndex;
        final int[] rowAAx0 = _cache.rowAAx0;
        final int[] rowAAx1 = _cache.rowAAx1;

        final int x0 = this.x;
        final int x1 = FloatMath.min(x0 + TILE_W, _cache.bboxX1);

        // note: process tile line [0 - 32[
        final int y0 = 0;
        final int y1 = FloatMath.min(this.y + TILE_H, _cache.bboxY1) - this.y;

        if (DO_LOG_BOUNDS) {
            MarlinUtils.logInfo("getAlpha = [" + x0 + " ... " + x1
                                + "[ [" + y0 + " ... " + y1 + "[");
        }

        final Unsafe _unsafe = OffHeapArray.UNSAFE;
        final long SIZE = 1L;
        final long addr_rowAA = _cache.rowAAChunk.address;
        long addr;

        final int skipRowPixels = (rowstride - (x1 - x0));

        int aax0, aax1, end;
        int idx = offset;

        for (int cy = y0, cx; cy < y1; cy++) {
            // empty line (default)
            cx = x0;

            aax1 = rowAAx1[cy]; // exclusive

            // quick check if there is AA data
            // corresponding to this tile [x0; x1[
            if (aax1 > x0) {
                aax0 = rowAAx0[cy]; // inclusive

                if (aax0 < x1) {
                    // note: cx is the cursor pointer in the tile array
                    // (left to right)
                    cx = aax0;

                    // ensure cx >= x0
                    if (cx <= x0) {
                        cx = x0;
                    } else {
                        // fill line start until first AA pixel rowAA exclusive:
                        for (end = x0; end < cx; end++) {
                            tile[idx++] = 0;
                        }
                    }

                    // now: cx >= x0 and cx >= aax0

                    // Copy AA data (sum alpha data):
                    addr = addr_rowAA + rowAAChunkIndex[cy] + (cx - aax0);

                    for (end = (aax1 <= x1) ? aax1 : x1; cx < end; cx++) {
                        // cx inside tile[x0; x1[ :
                        tile[idx++] = _unsafe.getByte(addr); // [0-255]
                        addr += SIZE;
                    }
                }
            }

            // fill line end
            while (cx < x1) {
                tile[idx++] = 0;
                cx++;
            }

            if (DO_TRACE) {
                for (int i = idx - (x1 - x0); i < idx; i++) {
                    System.out.print(hex(tile[i], 2));
                }
                System.out.println();
            }

            idx += skipRowPixels;
        }

        nextTile();

        if (DO_MONITORS) {
            rdrStats.mon_ptg_getAlpha.stop();
        }
    }

    /**
     * Gets the alpha coverage values for the current tile.
     * Either this method, or the nextTile() method should be called
     * once per tile, but not both.
     */
    private void getAlphaRLE(final byte[] tile, final int offset,
                             final int rowstride)
    {
        if (DO_MONITORS) {
            rdrStats.mon_ptg_getAlpha.start();
        }

        // Decode run-length encoded alpha mask data
        // The data for row j begins at cache.rowOffsetsRLE[j]
        // and is encoded as a set of 2-byte pairs (val, runLen)
        // terminated by a (0, 0) pair.

        // local vars for performance:
        final MarlinCache _cache = this.cache;
        final long[] rowAAChunkIndex = _cache.rowAAChunkIndex;
        final int[] rowAAx0 = _cache.rowAAx0;
        final int[] rowAAx1 = _cache.rowAAx1;
        final int[] rowAAEnc = _cache.rowAAEnc;
        final long[] rowAALen = _cache.rowAALen;
        final long[] rowAAPos = _cache.rowAAPos;

        final int x0 = this.x;
        final int x1 = FloatMath.min(x0 + TILE_W, _cache.bboxX1);
        final int w  = x1 - x0;

        // note: process tile line [0 - 32[
        final int y0 = 0;
        final int y1 = FloatMath.min(this.y + TILE_H, _cache.bboxY1) - this.y;

        if (DO_LOG_BOUNDS) {
            MarlinUtils.logInfo("getAlpha = [" + x0 + " ... " + x1
                                + "[ [" + y0 + " ... " + y1 + "[");
        }

        // avoid too small area: fill is not faster !
        final int clearTile;
        final byte refVal;
        final int area;

        if ((w >= FILL_TILE_W) && (area = w * y1) > 64) { // 64 / 4 ie 16 words min (faster)
            final int alphaSum = cache.alphaSumInTile(x0);

            if (alphaSum < area * TH_AA_ALPHA_FILL_EMPTY) {
                clearTile = 1;
                refVal = 0;
            } else if (alphaSum > area * TH_AA_ALPHA_FILL_FULL) {
                clearTile = 2;
                refVal = (byte)0xff;
            } else {
                clearTile = 0;
                refVal = 0;
            }
        } else {
            clearTile = 0;
            refVal = 0;
        }

        final Unsafe _unsafe = OffHeapArray.UNSAFE;
        final long SIZE_BYTE = 1L;
        final long SIZE_INT = 4L;
        final long addr_rowAA = _cache.rowAAChunk.address;
        long addr, addr_row, last_addr, addr_end;

        final int skipRowPixels = (rowstride - w);

        int cx, cy, cx1;
        int rx0, rx1, runLen, end;
        int packed;
        byte val;
        int idx = offset;

        switch (clearTile) {
        case 1: // 0x00
            // Clear full tile rows:
            Arrays.fill(tile, offset, offset + (y1 * rowstride), refVal);

            for (cy = y0; cy < y1; cy++) {
                // empty line (default)
                cx = x0;

                if (rowAAEnc[cy] == 0) {
                    // Raw encoding:

                    final int aax1 = rowAAx1[cy]; // exclusive

                    // quick check if there is AA data
                    // corresponding to this tile [x0; x1[
                    if (aax1 > x0) {
                        final int aax0 = rowAAx0[cy]; // inclusive

                        if (aax0 < x1) {
                            // note: cx is the cursor pointer in the tile array
                            // (left to right)
                            cx = aax0;

                            // ensure cx >= x0
                            if (cx <= x0) {
                                cx = x0;
                            } else {
                                // skip line start until first AA pixel rowAA exclusive:
                                idx += (cx - x0); // > 0
                            }

                            // now: cx >= x0 and cx >= aax0

                            // Copy AA data (sum alpha data):
                            addr = addr_rowAA + rowAAChunkIndex[cy] + (cx - aax0);

                            for (end = (aax1 <= x1) ? aax1 : x1; cx < end; cx++) {
                                tile[idx++] = _unsafe.getByte(addr); // [0-255]
                                addr += SIZE_BYTE;
                            }
                        }
                    }
                } else {
                    // RLE encoding:

                    // quick check if there is AA data
                    // corresponding to this tile [x0; x1[
                    if (rowAAx1[cy] > x0) { // last pixel exclusive

                        cx = rowAAx0[cy]; // inclusive
                        if (cx > x1) {
                            cx = x1;
                        }

                        // skip line start until first AA pixel rowAA exclusive:
                        if (cx > x0) {
                            idx += (cx - x0); // > 0
                        }

                        // get row address:
                        addr_row = addr_rowAA + rowAAChunkIndex[cy];
                        // get row end address:
                        addr_end = addr_row + rowAALen[cy]; // coded length

                        // reuse previous iteration position:
                        addr = addr_row + rowAAPos[cy];

                        last_addr = 0L;

                        while ((cx < x1) && (addr < addr_end)) {
                            // keep current position:
                            last_addr = addr;

                            // packed value:
                            packed = _unsafe.getInt(addr);

                            // last exclusive pixel x-coordinate:
                            cx1 = (packed >> 8);
                            // as bytes:
                            addr += SIZE_INT;

                            rx0 = cx;
                            if (rx0 < x0) {
                                rx0 = x0;
                            }
                            rx1 = cx = cx1;
                            if (rx1 > x1) {
                                rx1 = x1;
                                cx  = x1; // fix last x
                            }
                            // adjust runLen:
                            runLen = rx1 - rx0;

                            // ensure rx1 > rx0:
                            if (runLen > 0) {
                                packed &= 0xFF; // [0-255]

                                if (packed == 0)
                                {
                                    idx += runLen;
                                    continue;
                                }
                                val = (byte) packed; // [0-255]
                                do {
                                    tile[idx++] = val;
                                } while (--runLen > 0);
                            }
                        }

                        // Update last position in RLE entries:
                        if (last_addr != 0L) {
                            // Fix x0:
                            rowAAx0[cy]  = cx; // inclusive
                            // Fix position:
                            rowAAPos[cy] = (last_addr - addr_row);
                        }
                    }
                }

                // skip line end
                if (cx < x1) {
                    idx += (x1 - cx); // > 0
                }

                if (DO_TRACE) {
                    for (int i = idx - (x1 - x0); i < idx; i++) {
                        System.out.print(hex(tile[i], 2));
                    }
                    System.out.println();
                }

                idx += skipRowPixels;
            }
        break;

        case 0:
        default:
            for (cy = y0; cy < y1; cy++) {
                // empty line (default)
                cx = x0;

                if (rowAAEnc[cy] == 0) {
                    // Raw encoding:

                    final int aax1 = rowAAx1[cy]; // exclusive

                    // quick check if there is AA data
                    // corresponding to this tile [x0; x1[
                    if (aax1 > x0) {
                        final int aax0 = rowAAx0[cy]; // inclusive

                        if (aax0 < x1) {
                            // note: cx is the cursor pointer in the tile array
                            // (left to right)
                            cx = aax0;

                            // ensure cx >= x0
                            if (cx <= x0) {
                                cx = x0;
                            } else {
                                for (end = x0; end < cx; end++) {
                                    tile[idx++] = 0;
                                }
                            }

                            // now: cx >= x0 and cx >= aax0

                            // Copy AA data (sum alpha data):
                            addr = addr_rowAA + rowAAChunkIndex[cy] + (cx - aax0);

                            for (end = (aax1 <= x1) ? aax1 : x1; cx < end; cx++) {
                                tile[idx++] = _unsafe.getByte(addr); // [0-255]
                                addr += SIZE_BYTE;
                            }
                        }
                    }
                } else {
                    // RLE encoding:

                    // quick check if there is AA data
                    // corresponding to this tile [x0; x1[
                    if (rowAAx1[cy] > x0) { // last pixel exclusive

                        cx = rowAAx0[cy]; // inclusive
                        if (cx > x1) {
                            cx = x1;
                        }

                        // fill line start until first AA pixel rowAA exclusive:
                        for (end = x0; end < cx; end++) {
                            tile[idx++] = 0;
                        }

                        // get row address:
                        addr_row = addr_rowAA + rowAAChunkIndex[cy];
                        // get row end address:
                        addr_end = addr_row + rowAALen[cy]; // coded length

                        // reuse previous iteration position:
                        addr = addr_row + rowAAPos[cy];

                        last_addr = 0L;

                        while ((cx < x1) && (addr < addr_end)) {
                            // keep current position:
                            last_addr = addr;

                            // packed value:
                            packed = _unsafe.getInt(addr);

                            // last exclusive pixel x-coordinate:
                            cx1 = (packed >> 8);
                            // as bytes:
                            addr += SIZE_INT;

                            rx0 = cx;
                            if (rx0 < x0) {
                                rx0 = x0;
                            }
                            rx1 = cx = cx1;
                            if (rx1 > x1) {
                                rx1 = x1;
                                cx  = x1; // fix last x
                            }
                            // adjust runLen:
                            runLen = rx1 - rx0;

                            // ensure rx1 > rx0:
                            if (runLen > 0) {
                                packed &= 0xFF; // [0-255]

                                val = (byte) packed; // [0-255]
                                do {
                                    tile[idx++] = val;
                                } while (--runLen > 0);
                            }
                        }

                        // Update last position in RLE entries:
                        if (last_addr != 0L) {
                            // Fix x0:
                            rowAAx0[cy]  = cx; // inclusive
                            // Fix position:
                            rowAAPos[cy] = (last_addr - addr_row);
                        }
                    }
                }

                // fill line end
                while (cx < x1) {
                    tile[idx++] = 0;
                    cx++;
                }

                if (DO_TRACE) {
                    for (int i = idx - (x1 - x0); i < idx; i++) {
                        System.out.print(hex(tile[i], 2));
                    }
                    System.out.println();
                }

                idx += skipRowPixels;
            }
        break;

        case 2: // 0xFF
            // Fill full tile rows:
            Arrays.fill(tile, offset, offset + (y1 * rowstride), refVal);

            for (cy = y0; cy < y1; cy++) {
                // empty line (default)
                cx = x0;

                if (rowAAEnc[cy] == 0) {
                    // Raw encoding:

                    final int aax1 = rowAAx1[cy]; // exclusive

                    // quick check if there is AA data
                    // corresponding to this tile [x0; x1[
                    if (aax1 > x0) {
                        final int aax0 = rowAAx0[cy]; // inclusive

                        if (aax0 < x1) {
                            // note: cx is the cursor pointer in the tile array
                            // (left to right)
                            cx = aax0;

                            // ensure cx >= x0
                            if (cx <= x0) {
                                cx = x0;
                            } else {
                                // fill line start until first AA pixel rowAA exclusive:
                                for (end = x0; end < cx; end++) {
                                    tile[idx++] = 0;
                                }
                            }

                            // now: cx >= x0 and cx >= aax0

                            // Copy AA data (sum alpha data):
                            addr = addr_rowAA + rowAAChunkIndex[cy] + (cx - aax0);

                            for (end = (aax1 <= x1) ? aax1 : x1; cx < end; cx++) {
                                tile[idx++] = _unsafe.getByte(addr); // [0-255]
                                addr += SIZE_BYTE;
                            }
                        }
                    }
                } else {
                    // RLE encoding:

                    // quick check if there is AA data
                    // corresponding to this tile [x0; x1[
                    if (rowAAx1[cy] > x0) { // last pixel exclusive

                        cx = rowAAx0[cy]; // inclusive
                        if (cx > x1) {
                            cx = x1;
                        }

                        // fill line start until first AA pixel rowAA exclusive:
                        for (end = x0; end < cx; end++) {
                            tile[idx++] = 0;
                        }

                        // get row address:
                        addr_row = addr_rowAA + rowAAChunkIndex[cy];
                        // get row end address:
                        addr_end = addr_row + rowAALen[cy]; // coded length

                        // reuse previous iteration position:
                        addr = addr_row + rowAAPos[cy];

                        last_addr = 0L;

                        while ((cx < x1) && (addr < addr_end)) {
                            // keep current position:
                            last_addr = addr;

                            // packed value:
                            packed = _unsafe.getInt(addr);

                            // last exclusive pixel x-coordinate:
                            cx1 = (packed >> 8);
                            // as bytes:
                            addr += SIZE_INT;

                            rx0 = cx;
                            if (rx0 < x0) {
                                rx0 = x0;
                            }
                            rx1 = cx = cx1;
                            if (rx1 > x1) {
                                rx1 = x1;
                                cx  = x1; // fix last x
                            }
                            // adjust runLen:
                            runLen = rx1 - rx0;

                            // ensure rx1 > rx0:
                            if (runLen > 0) {
                                packed &= 0xFF; // [0-255]

                                if (packed == 0xFF)
                                {
                                    idx += runLen;
                                    continue;
                                }
                                val = (byte) packed; // [0-255]
                                do {
                                    tile[idx++] = val;
                                } while (--runLen > 0);
                            }
                        }

                        // Update last position in RLE entries:
                        if (last_addr != 0L) {
                            // Fix x0:
                            rowAAx0[cy]  = cx; // inclusive
                            // Fix position:
                            rowAAPos[cy] = (last_addr - addr_row);
                        }
                    }
                }

                // fill line end
                while (cx < x1) {
                    tile[idx++] = 0;
                    cx++;
                }

                if (DO_TRACE) {
                    for (int i = idx - (x1 - x0); i < idx; i++) {
                        System.out.print(hex(tile[i], 2));
                    }
                    System.out.println();
                }

                idx += skipRowPixels;
            }
        }

        nextTile();

        if (DO_MONITORS) {
            rdrStats.mon_ptg_getAlpha.stop();
        }
    }

    static String hex(int v, int d) {
        String s = Integer.toHexString(v);
        while (s.length() < d) {
            s = "0" + s;
        }
        return s.substring(0, d);
    }
}