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 import static java.lang.Math.min;
  29 import static java.lang.Math.max;
  30 import static sun.java2d.xr.RectTileManager.MASK_SIZE;
  31 
  32 /**
  33  * This class implements region tracking, used by the tiled-mask code.
  34  *
  35  * @author Clemens Eisserer
  36  */
  37 
  38 public class DirtyRegion implements Cloneable {
  39     int x, y, x2, y2;
  40 
  41     public DirtyRegion() {
  42         clear();
  43     }
  44 
  45     public void clear() {
  46         x = Integer.MAX_VALUE;
  47         y = Integer.MAX_VALUE;
  48         x2 = Integer.MIN_VALUE;
  49         y2 = Integer.MIN_VALUE;
  50     }
  51 
  52     public void growDirtyRegion(int x, int y, int x2, int y2) {
  53         this.x = min(x, this.x);
  54         this.y = min(y, this.y);
  55         this.x2 = max(x2, this.x2);
  56         this.y2 = max(y2, this.y2);
  57     }
  58 
  59     public int getWidth() {
  60         return x2 - x;
  61     }
  62 
  63     public int getHeight() {
  64         return y2 - y;
  65     }
  66 
  67     public void growDirtyRegionTileLimit(int x, int y, int x2, int y2) {
  68         if (x < this.x) {
  69             this.x = max(x, 0);
  70         }
  71         if (y < this.y) {
  72             this.y = max(y, 0);
  73         }
  74         if (x2 > this.x2) {
  75             this.x2 = min(x2, MASK_SIZE);
  76         }
  77         if (y2 > this.y2) {
  78             this.y2 = min(y2, MASK_SIZE);
  79         }
  80     }
  81 
  82     public static DirtyRegion combineRegion(DirtyRegion region1,
  83                                             DirtyRegion region2) {
  84         DirtyRegion region = new DirtyRegion();
  85         region.x = min(region1.x, region2.x);
  86         region.y = min(region1.y, region2.y);
  87         region.x2 = max(region1.x2, region2.x2);
  88         region.y2 = max(region1.y2, region2.y2);
  89         return region;
  90     }
  91 
  92     public void setDirtyLineRegion(int x1, int y1, int x2, int y2) {
  93         if (x1 < x2) {
  94             this.x = x1;
  95             this.x2 = x2;
  96         } else {
  97             this.x = x2;
  98             this.x2 = x1;
  99         }
 100 
 101         if (y1 < y2) {
 102             this.y = y1;
 103             this.y2 = y2;
 104         } else {
 105             this.y = y2;
 106             this.y2 = y1;
 107         }
 108     }
 109 
 110     public void translate(int x, int y) {
 111         if (this.x != Integer.MAX_VALUE) {
 112             this.x += x;
 113             this.x2 += x;
 114             this.y += y;
 115             this.y2 += y;
 116         }
 117     }
 118 
 119     public String toString() {
 120         return this.getClass().getName() +
 121                 "(x: " + x + ", y:" + y + ", x2:" + x2 + ", y2:" + y2 + ")";
 122     }
 123 
 124     public DirtyRegion cloneRegion() {
 125         try {
 126             return (DirtyRegion) clone();
 127         } catch (CloneNotSupportedException ex) {
 128             ex.printStackTrace();
 129         }
 130 
 131         return null;
 132     }
 133 }