1 /*
2  * Copyright (c) 2015, 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.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 import java.awt.Color;
25 import java.awt.Graphics2D;
26 import java.awt.GraphicsConfiguration;
27 import java.awt.GraphicsEnvironment;
28 import java.awt.Image;
29 import java.awt.Rectangle;
30 import java.awt.Shape;
31 import java.awt.geom.AffineTransform;
32 import java.awt.image.BufferedImage;
33 import java.awt.image.VolatileImage;
34 import java.io.File;
35 import java.io.IOException;
36 
37 import javax.imageio.ImageIO;
38 
39 import static java.awt.geom.Rectangle2D.Double;
40 
41 /**
42  * @test
43  * @key headful
44  * @bug 8061831 8130400
45  * @summary Tests drawing volatile image to volatile image using different
46  *          clips + xor mode. Results of the blit compatibleImage to
47  *          compatibleImage is used for comparison.
48  * @run main/othervm -Dsun.java2d.uiScale=1 IncorrectClipXorModeSurface2Surface
49  * @run main/othervm -Dsun.java2d.uiScale=2 IncorrectClipXorModeSurface2Surface
50  * @run main/othervm -Dsun.java2d.uiScale=4 IncorrectClipXorModeSurface2Surface
51  */
52 public final class IncorrectClipXorModeSurface2Surface {
53 
54     private static int[] SIZES = {2, 10, 100};
55     private static final Shape[] SHAPES = {
56             new Rectangle(0, 0, 0, 0),
57             new Rectangle(0, 0, 1, 1),
58             new Rectangle(0, 1, 1, 1),
59             new Rectangle(1, 0, 1, 1),
60             new Rectangle(1, 1, 1, 1),
61 
62             new Double(0, 0, 0.5, 0.5),
63             new Double(0, 0.5, 0.5, 0.5),
64             new Double(0.5, 0, 0.5, 0.5),
65             new Double(0.5, 0.5, 0.5, 0.5),
66             new Double(0.25, 0.25, 0.5, 0.5),
67             new Double(0, 0.25, 1, 0.5),
68             new Double(0.25, 0, 0.5, 1),
69 
70             new Double(.10, .10, .20, .20),
71             new Double(.75, .75, .20, .20),
72             new Double(.75, .10, .20, .20),
73             new Double(.10, .75, .20, .20),
74     };
75 
main(final String[] args)76     public static void main(final String[] args) throws IOException {
77         GraphicsEnvironment ge = GraphicsEnvironment
78                 .getLocalGraphicsEnvironment();
79         GraphicsConfiguration gc = ge.getDefaultScreenDevice()
80                 .getDefaultConfiguration();
81         AffineTransform at;
82         for (int size : SIZES) {
83             at = AffineTransform.getScaleInstance(size, size);
84             for (Shape clip : SHAPES) {
85                 clip = at.createTransformedShape(clip);
86                 for (Shape to : SHAPES) {
87                     to = at.createTransformedShape(to);
88                     // Prepare test images
89                     BufferedImage snapshot;
90                     VolatileImage source = getVolatileImage(gc, size);
91                     VolatileImage target = getVolatileImage(gc, size);
92                     int attempt = 0;
93                     while (true) {
94                         if (++attempt > 10) {
95                             throw new RuntimeException("Too many attempts: " + attempt);
96                         }
97                         // Prepare source images
98                         source.validate(gc);
99                         Graphics2D g2d = source.createGraphics();
100                         g2d.setColor(Color.RED);
101                         g2d.fillRect(0, 0, size, size);
102                         g2d.dispose();
103                         if (source.validate(gc) != VolatileImage.IMAGE_OK) {
104                             continue;
105                         }
106                         // Prepare target images
107                         target.validate(gc);
108                         g2d = target.createGraphics();
109                         g2d.setColor(Color.GREEN);
110                         g2d.fillRect(0, 0, size, size);
111                         g2d.dispose();
112                         if (target.validate(gc) != VolatileImage.IMAGE_OK) {
113                             continue;
114                         }
115 
116                         draw(clip, to, source, target);
117                         snapshot = target.getSnapshot();
118                         if (source.contentsLost() || target.contentsLost()) {
119                             continue;
120                         }
121                         break;
122                     }
123                     // Prepare gold images
124                     BufferedImage goldS = getSourceGold(gc, size);
125                     BufferedImage goldT = getTargetGold(gc, size);
126                     draw(clip, to, goldS, goldT);
127                     validate(snapshot, goldT);
128                     source.flush();
129                     target.flush();
130                 }
131             }
132         }
133     }
134 
draw(Shape clip, Shape shape, Image from, Image to)135     private static void draw(Shape clip, Shape shape, Image from, Image to) {
136         Graphics2D g2d = (Graphics2D) to.getGraphics();
137         g2d.setXORMode(Color.BLACK);
138         g2d.setClip(clip);
139         Rectangle toBounds = shape.getBounds();
140         g2d.drawImage(from, toBounds.x, toBounds.y, toBounds.width,
141                       toBounds.height, null);
142         g2d.dispose();
143     }
144 
getSourceGold(GraphicsConfiguration gc, int size)145     private static BufferedImage getSourceGold(GraphicsConfiguration gc,
146                                                int size) {
147         final BufferedImage bi = gc.createCompatibleImage(size, size);
148         Graphics2D g2d = bi.createGraphics();
149         g2d.setColor(Color.RED);
150         g2d.fillRect(0, 0, size, size);
151         g2d.dispose();
152         return bi;
153     }
154 
getTargetGold(GraphicsConfiguration gc, int size)155     private static BufferedImage getTargetGold(GraphicsConfiguration gc,
156                                                int size) {
157         BufferedImage image = gc.createCompatibleImage(size, size);
158         Graphics2D g2d = image.createGraphics();
159         g2d.setColor(Color.GREEN);
160         g2d.fillRect(0, 0, size, size);
161         g2d.dispose();
162         return image;
163     }
164 
getVolatileImage(GraphicsConfiguration gc, int size)165     private static VolatileImage getVolatileImage(GraphicsConfiguration gc,
166                                                   int size) {
167         return gc.createCompatibleVolatileImage(size, size);
168     }
169 
validate(BufferedImage bi, BufferedImage goldbi)170     private static void validate(BufferedImage bi, BufferedImage goldbi)
171             throws IOException {
172         for (int x = 0; x < bi.getWidth(); ++x) {
173             for (int y = 0; y < bi.getHeight(); ++y) {
174                 if (goldbi.getRGB(x, y) != bi.getRGB(x, y)) {
175                     ImageIO.write(bi, "png", new File("actual.png"));
176                     ImageIO.write(goldbi, "png", new File("expected.png"));
177                     throw new RuntimeException("Test failed.");
178                 }
179             }
180         }
181     }
182 }
183