src/java.desktop/share/classes/sun/java2d/pisces/PiscesCache.java
changeset 48128 2d91c9a4f409
parent 48127 efc459cf351e
parent 48125 4e5124dacf91
child 48129 c134a8bee21a
equal deleted inserted replaced
48127:efc459cf351e 48128:2d91c9a4f409
     1 /*
       
     2  * Copyright (c) 2007, 2011, 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.pisces;
       
    27 
       
    28 import java.util.Arrays;
       
    29 
       
    30 /**
       
    31  * An object used to cache pre-rendered complex paths.
       
    32  */
       
    33 final class PiscesCache {
       
    34 
       
    35     final int bboxX0, bboxY0, bboxX1, bboxY1;
       
    36 
       
    37     // rowAARLE[i] holds the encoding of the pixel row with y = bboxY0+i.
       
    38     // The format of each of the inner arrays is: rowAARLE[i][0,1] = (x0, n)
       
    39     // where x0 is the first x in row i with nonzero alpha, and n is the
       
    40     // number of RLE entries in this row. rowAARLE[i][j,j+1] for j>1 is
       
    41     // (val,runlen)
       
    42     final int[][] rowAARLE;
       
    43 
       
    44     // RLE encodings are added in increasing y rows and then in increasing
       
    45     // x inside those rows. Therefore, at any one time there is a well
       
    46     // defined position (x,y) where a run length is about to be added (or
       
    47     // the row terminated). x0,y0 is this (x,y)-(bboxX0,bboxY0). They
       
    48     // are used to get indices into the current tile.
       
    49     private int x0 = Integer.MIN_VALUE, y0 = Integer.MIN_VALUE;
       
    50 
       
    51     // touchedTile[i][j] is the sum of all the alphas in the tile with
       
    52     // y=i*TILE_SIZE+bboxY0 and x=j*TILE_SIZE+bboxX0.
       
    53     private final int[][] touchedTile;
       
    54 
       
    55     static final int TILE_SIZE_LG = 5;
       
    56     static final int TILE_SIZE = 1 << TILE_SIZE_LG; // 32
       
    57     private static final int INIT_ROW_SIZE = 8; // enough for 3 run lengths
       
    58 
       
    59     PiscesCache(int minx, int miny, int maxx, int maxy) {
       
    60         assert maxy >= miny && maxx >= minx;
       
    61         bboxX0 = minx;
       
    62         bboxY0 = miny;
       
    63         bboxX1 = maxx + 1;
       
    64         bboxY1 = maxy + 1;
       
    65         // we could just leave the inner arrays as null and allocate them
       
    66         // lazily (which would be beneficial for shapes with gaps), but we
       
    67         // assume there won't be too many of those so we allocate everything
       
    68         // up front (which is better for other cases)
       
    69         rowAARLE = new int[bboxY1 - bboxY0 + 1][INIT_ROW_SIZE];
       
    70         x0 = 0;
       
    71         y0 = -1; // -1 makes the first assert in startRow succeed
       
    72         // the ceiling of (maxy - miny + 1) / TILE_SIZE;
       
    73         int nyTiles = (maxy - miny + TILE_SIZE) >> TILE_SIZE_LG;
       
    74         int nxTiles = (maxx - minx + TILE_SIZE) >> TILE_SIZE_LG;
       
    75 
       
    76         touchedTile = new int[nyTiles][nxTiles];
       
    77     }
       
    78 
       
    79     void addRLERun(int val, int runLen) {
       
    80         if (runLen > 0) {
       
    81             addTupleToRow(y0, val, runLen);
       
    82             if (val != 0) {
       
    83                 // the x and y of the current row, minus bboxX0, bboxY0
       
    84                 int tx = x0 >> TILE_SIZE_LG;
       
    85                 int ty = y0 >> TILE_SIZE_LG;
       
    86                 int tx1 = (x0 + runLen - 1) >> TILE_SIZE_LG;
       
    87                 // while we forbid rows from starting before bboxx0, our users
       
    88                 // can still store rows that go beyond bboxx1 (although this
       
    89                 // shouldn't happen), so it's a good idea to check that i
       
    90                 // is not going out of bounds in touchedTile[ty]
       
    91                 if (tx1 >= touchedTile[ty].length) {
       
    92                     tx1 = touchedTile[ty].length - 1;
       
    93                 }
       
    94                 if (tx <= tx1) {
       
    95                     int nextTileXCoord = (tx + 1) << TILE_SIZE_LG;
       
    96                     if (nextTileXCoord > x0+runLen) {
       
    97                         touchedTile[ty][tx] += val * runLen;
       
    98                     } else {
       
    99                         touchedTile[ty][tx] += val * (nextTileXCoord - x0);
       
   100                     }
       
   101                     tx++;
       
   102                 }
       
   103                 // don't go all the way to tx1 - we need to handle the last
       
   104                 // tile as a special case (just like we did with the first
       
   105                 for (; tx < tx1; tx++) {
       
   106 //                    try {
       
   107                     touchedTile[ty][tx] += (val << TILE_SIZE_LG);
       
   108 //                    } catch (RuntimeException e) {
       
   109 //                        System.out.println("x0, y0: " + x0 + ", " + y0);
       
   110 //                        System.out.printf("tx, ty, tx1: %d, %d, %d %n", tx, ty, tx1);
       
   111 //                        System.out.printf("bboxX/Y0/1: %d, %d, %d, %d %n",
       
   112 //                                bboxX0, bboxY0, bboxX1, bboxY1);
       
   113 //                        throw e;
       
   114 //                    }
       
   115                 }
       
   116                 // they will be equal unless x0>>TILE_SIZE_LG == tx1
       
   117                 if (tx == tx1) {
       
   118                     int lastXCoord = Math.min(x0 + runLen, (tx + 1) << TILE_SIZE_LG);
       
   119                     int txXCoord = tx << TILE_SIZE_LG;
       
   120                     touchedTile[ty][tx] += val * (lastXCoord - txXCoord);
       
   121                 }
       
   122             }
       
   123             x0 += runLen;
       
   124         }
       
   125     }
       
   126 
       
   127     void startRow(int y, int x) {
       
   128         // rows are supposed to be added by increasing y.
       
   129         assert y - bboxY0 > y0;
       
   130         assert y <= bboxY1; // perhaps this should be < instead of <=
       
   131 
       
   132         y0 = y - bboxY0;
       
   133         // this should be a new, uninitialized row.
       
   134         assert rowAARLE[y0][1] == 0;
       
   135 
       
   136         x0 = x - bboxX0;
       
   137         assert x0 >= 0 : "Input must not be to the left of bbox bounds";
       
   138 
       
   139         // the way addTupleToRow is implemented it would work for this but it's
       
   140         // not a good idea to use it because it is meant for adding
       
   141         // RLE tuples, not the first tuple (which is special).
       
   142         rowAARLE[y0][0] = x;
       
   143         rowAARLE[y0][1] = 2;
       
   144     }
       
   145 
       
   146     int alphaSumInTile(int x, int y) {
       
   147         x -= bboxX0;
       
   148         y -= bboxY0;
       
   149         return touchedTile[y>>TILE_SIZE_LG][x>>TILE_SIZE_LG];
       
   150     }
       
   151 
       
   152     int minTouched(int rowidx) {
       
   153         return rowAARLE[rowidx][0];
       
   154     }
       
   155 
       
   156     int rowLength(int rowidx) {
       
   157         return rowAARLE[rowidx][1];
       
   158     }
       
   159 
       
   160     private void addTupleToRow(int row, int a, int b) {
       
   161         int end = rowAARLE[row][1];
       
   162         rowAARLE[row] = Helpers.widenArray(rowAARLE[row], end, 2);
       
   163         rowAARLE[row][end++] = a;
       
   164         rowAARLE[row][end++] = b;
       
   165         rowAARLE[row][1] = end;
       
   166     }
       
   167 
       
   168     void getBBox(int bbox[]) {
       
   169         // Since we add +1 to bboxX1,bboxY1 so when PTG asks for bbox,
       
   170         // we will give after -1
       
   171         bbox[0] = bboxX0;
       
   172         bbox[1] = bboxY0;
       
   173         bbox[2] = bboxX1 - 1;
       
   174         bbox[3] = bboxY1 - 1;
       
   175     }
       
   176 
       
   177     @Override
       
   178     public String toString() {
       
   179         String ret = "bbox = ["+
       
   180                       bboxX0+", "+bboxY0+" => "+
       
   181                       bboxX1+", "+bboxY1+"]\n";
       
   182         for (int[] row : rowAARLE) {
       
   183             if (row != null) {
       
   184                 ret += ("minTouchedX=" + row[0] +
       
   185                         "\tRLE Entries: " + Arrays.toString(
       
   186                                 Arrays.copyOfRange(row, 2, row[1])) + "\n");
       
   187             } else {
       
   188                 ret += "[]\n";
       
   189             }
       
   190         }
       
   191         return ret;
       
   192     }
       
   193 }