8156894: Cleanup of sun.java2d.pipe.Region
authorserb
Wed, 18 May 2016 19:23:39 +0300
changeset 38979 bb8a8e95780a
parent 38978 cc1bdac569a6
child 38980 c0b4fd06d439
8156894: Cleanup of sun.java2d.pipe.Region Reviewed-by: flar
jdk/src/java.desktop/share/classes/sun/java2d/SunGraphics2D.java
jdk/src/java.desktop/share/classes/sun/java2d/pipe/DrawImage.java
jdk/src/java.desktop/share/classes/sun/java2d/pipe/Region.java
jdk/src/java.desktop/share/classes/sun/java2d/pipe/ShapeSpanIterator.java
--- a/jdk/src/java.desktop/share/classes/sun/java2d/SunGraphics2D.java	Wed May 18 18:35:54 2016 +0300
+++ b/jdk/src/java.desktop/share/classes/sun/java2d/SunGraphics2D.java	Wed May 18 19:23:39 2016 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2016, 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
@@ -1915,8 +1915,7 @@
                 sr.setOutputArea(devClip);
                 sr.appendPath(cpi);
                 sr.getPathBox(box);
-                Region r = Region.getInstance(box);
-                r.appendSpans(sr);
+                Region r = Region.getInstance(box, sr);
                 clipRegion = r;
                 clipState =
                     r.isRectangular() ? CLIP_RECTANGULAR : CLIP_SHAPE;
--- a/jdk/src/java.desktop/share/classes/sun/java2d/pipe/DrawImage.java	Wed May 18 18:35:54 2016 +0300
+++ b/jdk/src/java.desktop/share/classes/sun/java2d/pipe/DrawImage.java	Wed May 18 19:23:39 2016 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2016, 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
@@ -401,10 +401,10 @@
         }
 
         Region clip = sg.getCompClip();
-        final int dx1 = Math.max((int) Math.floor(ddx1), clip.lox);
-        final int dy1 = Math.max((int) Math.floor(ddy1), clip.loy);
-        final int dx2 = Math.min((int) Math.ceil(ddx2), clip.hix);
-        final int dy2 = Math.min((int) Math.ceil(ddy2), clip.hiy);
+        final int dx1 = Math.max((int) Math.floor(ddx1), clip.getLoX());
+        final int dy1 = Math.max((int) Math.floor(ddy1), clip.getLoY());
+        final int dx2 = Math.min((int) Math.ceil(ddx2), clip.getHiX());
+        final int dy2 = Math.min((int) Math.ceil(ddy2), clip.getHiY());
         if (dx2 <= dx1 || dy2 <= dy1) {
             // empty destination means no output
             return;
--- a/jdk/src/java.desktop/share/classes/sun/java2d/pipe/Region.java	Wed May 18 18:35:54 2016 +0300
+++ b/jdk/src/java.desktop/share/classes/sun/java2d/pipe/Region.java	Wed May 18 19:23:39 2016 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2016, 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
@@ -62,37 +62,21 @@
  * bands[rowstart+3+N*2] = ...    // start of next Y row
  * </pre>
  */
-public class Region {
-    static final int INIT_SIZE = 50;
-    static final int GROW_SIZE = 50;
+public final class Region {
+    private static final int INIT_SIZE = 50;
+    private static final int GROW_SIZE = 50;
 
-    /**
-     * Immutable Region.
-     */
-    private static final class ImmutableRegion extends Region {
-        protected ImmutableRegion(int lox, int loy, int hix, int hiy) {
-            super(lox, loy, hix, hiy);
-        }
-
-        // Override all the methods that mutate the object
-        public void appendSpans(sun.java2d.pipe.SpanIterator si) {}
-        public void setOutputArea(java.awt.Rectangle r) {}
-        public void setOutputAreaXYWH(int x, int y, int w, int h) {}
-        public void setOutputArea(int[] box) {}
-        public void setOutputAreaXYXY(int lox, int loy, int hix, int hiy) {}
-    }
-
-    public static final Region EMPTY_REGION = new ImmutableRegion(0, 0, 0, 0);
-    public static final Region WHOLE_REGION = new ImmutableRegion(
+    public static final Region EMPTY_REGION = new Region(0, 0, 0, 0);
+    public static final Region WHOLE_REGION = new Region(
             Integer.MIN_VALUE,
             Integer.MIN_VALUE,
             Integer.MAX_VALUE,
             Integer.MAX_VALUE);
 
-    int lox;
-    int loy;
-    int hix;
-    int hiy;
+    private int lox;
+    private int loy;
+    private int hix;
+    private int hiy;
 
     int endIndex;
     int[] bands;
@@ -155,7 +139,7 @@
         return (int) Math.round(newv);
     }
 
-    protected Region(int lox, int loy, int hix, int hiy) {
+    private Region(int lox, int loy, int hix, int hiy) {
         this.lox = lox;
         this.loy = loy;
         this.hix = hix;
@@ -258,9 +242,7 @@
             sr.setOutputArea(devBounds);
             sr.appendPath(s.getPathIterator(at));
             sr.getPathBox(box);
-            Region r = Region.getInstance(box);
-            r.appendSpans(sr);
-            return r;
+            return Region.getInstance(box, sr);
         } finally {
             sr.dispose();
         }
@@ -349,56 +331,18 @@
     }
 
     /**
-     * Sets the rectangle of interest for storing and returning
-     * region bands.
-     * <p>
-     * This method can also be used to initialize a simple rectangular
-     * region.
-     */
-    public void setOutputArea(Rectangle r) {
-        setOutputAreaXYWH(r.x, r.y, r.width, r.height);
-    }
-
-    /**
-     * Sets the rectangle of interest for storing and returning
-     * region bands.  The rectangle is specified in x, y, width, height
-     * format and appropriate clipping is performed as per the method
-     * {@code dimAdd}.
-     * <p>
-     * This method can also be used to initialize a simple rectangular
-     * region.
+     * Returns a Region object with a rectangle of interest specified by the
+     * indicated rectangular area in lox, loy, hix, hiy format.
+     * <p/>
+     * Appends the list of spans returned from the indicated SpanIterator. Each
+     * span must be at a higher starting Y coordinate than the previous data or
+     * it must have a Y range equal to the highest Y band in the region and a
+     * higher X coordinate than any of the spans in that band.
      */
-    public void setOutputAreaXYWH(int x, int y, int w, int h) {
-        setOutputAreaXYXY(x, y, dimAdd(x, w), dimAdd(y, h));
-    }
-
-    /**
-     * Sets the rectangle of interest for storing and returning
-     * region bands.  The rectangle is specified as a span array.
-     * <p>
-     * This method can also be used to initialize a simple rectangular
-     * region.
-     */
-    public void setOutputArea(int box[]) {
-        this.lox = box[0];
-        this.loy = box[1];
-        this.hix = box[2];
-        this.hiy = box[3];
-    }
-
-    /**
-     * Sets the rectangle of interest for storing and returning
-     * region bands.  The rectangle is specified in lox, loy,
-     * hix, hiy format.
-     * <p>
-     * This method can also be used to initialize a simple rectangular
-     * region.
-     */
-    public void setOutputAreaXYXY(int lox, int loy, int hix, int hiy) {
-        this.lox = lox;
-        this.loy = loy;
-        this.hix = hix;
-        this.hiy = hiy;
+    public static Region getInstance(int box[], SpanIterator si) {
+        Region ret = new Region(box[0], box[1], box[2], box[3]);
+        ret.appendSpans(si);
+        return ret;
     }
 
     /**
@@ -408,7 +352,7 @@
      * Y range equal to the highest Y band in the region and a
      * higher X coordinate than any of the spans in that band.
      */
-    public void appendSpans(SpanIterator si) {
+    private void appendSpans(SpanIterator si) {
         int[] box = new int[6];
 
         while (si.nextSpan(box)) {
@@ -739,9 +683,9 @@
         return ret;
     }
 
-    static final int INCLUDE_A      = 1;
-    static final int INCLUDE_B      = 2;
-    static final int INCLUDE_COMMON = 4;
+    private static final int INCLUDE_A      = 1;
+    private static final int INCLUDE_B      = 2;
+    private static final int INCLUDE_COMMON = 4;
 
     private void filterSpans(Region ra, Region rb, int flags) {
         int abands[] = ra.bands;
@@ -1080,35 +1024,35 @@
     /**
      * Returns the lowest X coordinate in the Region.
      */
-    public final int getLoX() {
+    public int getLoX() {
         return lox;
     }
 
     /**
      * Returns the lowest Y coordinate in the Region.
      */
-    public final int getLoY() {
+    public int getLoY() {
         return loy;
     }
 
     /**
      * Returns the highest X coordinate in the Region.
      */
-    public final int getHiX() {
+    public int getHiX() {
         return hix;
     }
 
     /**
      * Returns the highest Y coordinate in the Region.
      */
-    public final int getHiY() {
+    public int getHiY() {
         return hiy;
     }
 
     /**
      * Returns the width of this Region clipped to the range (0 - MAX_INT).
      */
-    public final int getWidth() {
+    public int getWidth() {
         if (hix < lox) return 0;
         int w;
         if ((w = hix - lox) < 0) {
@@ -1120,7 +1064,7 @@
     /**
      * Returns the height of this Region clipped to the range (0 - MAX_INT).
      */
-    public final int getHeight() {
+    public int getHeight() {
         if (hiy < loy) return 0;
         int h;
         if ((h = hiy - loy) < 0) {
@@ -1325,6 +1269,7 @@
         return si;
     }
 
+    @Override
     public String toString() {
         StringBuilder sb = new StringBuilder();
         sb.append("Region[[");
@@ -1335,13 +1280,13 @@
         sb.append(hix);
         sb.append(", ");
         sb.append(hiy);
-        sb.append("]");
+        sb.append(']');
         if (bands != null) {
             int col = 0;
             while (col < endIndex) {
                 sb.append("y{");
                 sb.append(bands[col++]);
-                sb.append(",");
+                sb.append(',');
                 sb.append(bands[col++]);
                 sb.append("}[");
                 int end = bands[col++];
@@ -1351,20 +1296,25 @@
                     sb.append(bands[col++]);
                     sb.append(", ");
                     sb.append(bands[col++]);
-                    sb.append(")");
+                    sb.append(')');
                 }
-                sb.append("]");
+                sb.append(']');
             }
         }
-        sb.append("]");
+        sb.append(']');
         return sb.toString();
     }
 
+    @Override
     public int hashCode() {
         return (isEmpty() ? 0 : (lox * 3 + loy * 5 + hix * 7 + hiy * 9));
     }
 
+    @Override
     public boolean equals(Object o) {
+        if (this == o) {
+            return true;
+        }
         if (!(o instanceof Region)) {
             return false;
         }
--- a/jdk/src/java.desktop/share/classes/sun/java2d/pipe/ShapeSpanIterator.java	Wed May 18 18:35:54 2016 +0300
+++ b/jdk/src/java.desktop/share/classes/sun/java2d/pipe/ShapeSpanIterator.java	Wed May 18 19:23:39 2016 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1998, 2007, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1998, 2016, 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
@@ -131,7 +131,7 @@
      * span segments to the bounds of the specified Region.
      */
     public void setOutputArea(Region r) {
-        setOutputAreaXYXY(r.lox, r.loy, r.hix, r.hiy);
+        setOutputAreaXYXY(r.getLoX(), r.getLoY(), r.getHiX(), r.getHiY());
     }
 
     /*