AcceleratedXORModeTest.java revision 14851:980da45565c8
1/*
2 * Copyright (c) 2013, 2016, 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/*
25 * @test
26 * @key headful
27 * @bug     8024343 8042098
28 * @summary Test verifies that accelerated pipelines
29 *          correctly draws primitives in XOR mode.
30 * @run main/othervm -Dsun.java2d.xrender=True AcceleratedXORModeTest
31 */
32
33import java.awt.Color;
34import java.awt.Graphics2D;
35import java.awt.GraphicsConfiguration;
36import java.awt.GraphicsEnvironment;
37import java.awt.image.BufferedImage;
38import java.awt.image.VolatileImage;
39import java.io.File;
40import java.io.IOException;
41import javax.imageio.ImageIO;
42
43public class AcceleratedXORModeTest {
44    public static void main(String argv[]) {
45        String fileName = argv.length > 0 ? argv[0] : null;
46        new AcceleratedXORModeTest(fileName).test();
47    }
48
49    static final Color backColor = Color.red;
50    static final Color color1 = Color.green;
51    static final Color color2 = Color.yellow;
52    static final Color xorColor1 = Color.blue;
53    static final Color xorColor2 = Color.white;
54
55    static final int width = 700, height = 300;
56
57    VolatileImage vImg = null;
58    String fileName;
59
60    public AcceleratedXORModeTest(String fileName) {
61        this.fileName = fileName;
62    }
63
64    void draw(Graphics2D g) {
65        g.setColor(backColor);
66        g.fillRect(0, 0, width, height);
67        g.setXORMode(xorColor1);
68        drawPattern(g, 100);
69        g.setXORMode(xorColor2);
70        drawPattern(g, 400);
71        g.dispose();
72    }
73
74    void test(BufferedImage bi) {
75        comparePattern(bi, 150, xorColor1.getRGB());
76        comparePattern(bi, 450, xorColor2.getRGB());
77    }
78
79    void comparePattern(BufferedImage bi, int startX, int xorColor) {
80        int[] expectedColors = {
81            backColor.getRGB() ^ color1.getRGB() ^ xorColor,
82            backColor.getRGB() ^ color1.getRGB() ^ xorColor ^
83                color2.getRGB() ^ xorColor,
84            backColor.getRGB() ^ color2.getRGB() ^ xorColor
85        };
86        for (int i = 0; i < 3; i++) {
87            int x = startX + 100 * i;
88            int rgb = bi.getRGB(x, 150);
89            if (rgb != expectedColors[i]) {
90                String msg = "Colors mismatch: x = " + x +
91                        ", got " + new Color(rgb) + ", expected " +
92                        new Color(expectedColors[i]);
93                System.err.println(msg);
94                write(bi);
95                throw new RuntimeException("FAILED: " + msg);
96            }
97        }
98    }
99
100    void drawPattern(Graphics2D g, int x) {
101        g.setColor(color1);
102        g.fillRect(x, 0, 200, 300);
103        g.setColor(color2);
104        g.fillRect(x+100, 0, 200, 300);
105    }
106
107    GraphicsConfiguration getDefaultGC() {
108        return GraphicsEnvironment.getLocalGraphicsEnvironment().
109                getDefaultScreenDevice().getDefaultConfiguration();
110    }
111
112    void createVImg() {
113        if (vImg != null) {
114            vImg.flush();
115            vImg = null;
116        }
117        vImg = getDefaultGC().createCompatibleVolatileImage(width, height);
118    }
119
120    void write(BufferedImage bi) {
121        if (fileName != null) {
122            try {
123                ImageIO.write(bi, "png", new File(fileName));
124            } catch (IOException e) {
125                System.err.println("Can't write image file " + fileName);
126            }
127        }
128    }
129
130    void test() {
131        createVImg();
132        BufferedImage bi = null;
133        do {
134            int valCode = vImg.validate(getDefaultGC());
135            if (valCode == VolatileImage.IMAGE_INCOMPATIBLE) {
136                createVImg();
137            }
138            Graphics2D g = vImg.createGraphics();
139            draw(g);
140            bi = vImg.getSnapshot();
141        } while (vImg.contentsLost());
142        if (bi != null) {
143            test(bi);
144            write(bi);
145        }
146    }
147}
148