1 /*
   2  * Copyright (c) 2010, 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.xr;
  27 
  28 /**
  29  * Represents a single tile, used to store the rectangles covering the area
  30  * of the mask where the tile is located.
  31  *
  32  * @author Clemens Eisserer
  33  */
  34 public class MaskTile {
  35     GrowableRectArray rects;
  36     DirtyRegion dirtyArea;
  37 
  38     public MaskTile()
  39     {
  40         rects = new GrowableRectArray(128);
  41         dirtyArea = new DirtyRegion();
  42     }
  43 
  44     public void addRect(int x, int y, int width, int height) {
  45         int index = rects.getNextIndex();
  46         rects.setX(index, x);
  47         rects.setY(index, y);
  48         rects.setWidth(index, width);
  49         rects.setHeight(index, height);
  50     }
  51 
  52     public void addLine(int x1, int y1, int x2, int y2) {
  53         /*
  54          * EXA is not able to accalerate diagonal lines, we try to "guide" it a
  55          * bit to avoid excessive migration See project documentation for an
  56          * detailed explanation
  57          */
  58         DirtyRegion region = new DirtyRegion();
  59         region.setDirtyLineRegion(x1, y1, x2, y2);
  60         int xDiff = region.x2 - region.x;
  61         int yDiff = region.y2 - region.y;
  62 
  63         if (xDiff == 0 || yDiff == 0) {
  64             addRect(region.x, region.y,
  65                     region.x2 - region.x + 1, region.y2 - region.y + 1);
  66         } else if (xDiff == 1 && yDiff == 1) {
  67             addRect(x1, y1, 1, 1);
  68             addRect(x2, y2, 1, 1);
  69         } else {
  70             lineToRects(x1, y1, x2, y2);
  71         }
  72     }
  73 
  74     private void lineToRects(int xstart, int ystart, int xend, int yend) {
  75         int x, y, t, dx, dy, incx, incy, pdx, pdy, ddx, ddy, es, el, err;
  76 
  77         /* Entfernung in beiden Dimensionen berechnen */
  78         dx = xend - xstart;
  79         dy = yend - ystart;
  80 
  81         /* Vorzeichen des Inkrements bestimmen */
  82         incx = dx > 0 ? 1 : (dx < 0) ? -1 : 0;
  83         incy = dy > 0 ? 1 : (dy < 0) ? -1 : 0;
  84         if (dx < 0)
  85             dx = -dx;
  86         if (dy < 0)
  87             dy = -dy;
  88 
  89         /* feststellen, welche Entfernung groesser ist */
  90         if (dx > dy) {
  91             /* x ist schnelle Richtung */
  92             pdx = incx;
  93             pdy = 0; /* pd. ist Parallelschritt */
  94             ddx = incx;
  95             ddy = incy; /* dd. ist Diagonalschritt */
  96             es = dy;
  97             el = dx; /* Fehlerschritte schnell, langsam */
  98         } else {
  99             /* y ist schnelle Richtung */
 100             pdx = 0;
 101             pdy = incy; /* pd. ist Parallelschritt */
 102             ddx = incx;
 103             ddy = incy; /* dd. ist Diagonalschritt */
 104             es = dx;
 105             el = dy; /* Fehlerschritte schnell, langsam */
 106         }
 107 
 108         /* Initialisierungen vor Schleifenbeginn */
 109         x = xstart;
 110         y = ystart;
 111         err = el / 2;
 112         addRect(x, y, 1, 1);
 113 
 114         /* Pixel berechnen */
 115         for (t = 0; t < el; ++t) /* t zaehlt die Pixel, el ist auch Anzahl */
 116         {
 117             /* Aktualisierung Fehlerterm */
 118             err -= es;
 119             if (err < 0) {
 120                 /* Fehlerterm wieder positiv (>=0) machen */
 121                 err += el;
 122                 /* Schritt in langsame Richtung, Diagonalschritt */
 123                 x += ddx;
 124                 y += ddy;
 125             } else {
 126                 /* Schritt in schnelle Richtung, Parallelschritt */
 127                 x += pdx;
 128                 y += pdy;
 129             }
 130             addRect(x, y, 1, 1);
 131             // SetPixel(x,y);
 132             // System.out.println(x+":"+y);
 133         }
 134     }
 135 
 136     public void calculateDirtyAreas()
 137     {
 138         for (int i=0; i < rects.getSize(); i++) {
 139             int x = rects.getX(i);
 140             int y = rects.getY(i);
 141             dirtyArea.growDirtyRegion(x, y,
 142                                       x + rects.getWidth(i),
 143                                       y + rects.getHeight(i));
 144         }
 145     }
 146 
 147     public void reset() {
 148         rects.clear();
 149         dirtyArea.clear();
 150     }
 151 
 152     public void translate(int x, int y) {
 153         if (rects.getSize() > 0) {
 154             dirtyArea.translate(x, y);
 155         }
 156         rects.translateRects(x, y);
 157     }
 158 
 159     public GrowableRectArray getRects() {
 160         return rects;
 161     }
 162 
 163     public DirtyRegion getDirtyArea() {
 164         return dirtyArea;
 165     }
 166 }