Path: blob/master/test/jdk/sun/java2d/AcceleratedXORModeTest.java
41144 views
/*1* Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @key headful26* @bug 8024343 804209827* @summary Test verifies that accelerated pipelines28* correctly draws primitives in XOR mode.29* @run main/othervm -Dsun.java2d.xrender=True AcceleratedXORModeTest30*/3132import java.awt.Color;33import java.awt.Graphics2D;34import java.awt.GraphicsConfiguration;35import java.awt.GraphicsEnvironment;36import java.awt.image.BufferedImage;37import java.awt.image.VolatileImage;38import java.io.File;39import java.io.IOException;40import javax.imageio.ImageIO;4142public class AcceleratedXORModeTest {43public static void main(String argv[]) {44String fileName = argv.length > 0 ? argv[0] : null;45new AcceleratedXORModeTest(fileName).test();46}4748static final Color backColor = Color.red;49static final Color color1 = Color.green;50static final Color color2 = Color.yellow;51static final Color xorColor1 = Color.blue;52static final Color xorColor2 = Color.white;5354static final int width = 700, height = 300;5556VolatileImage vImg = null;57String fileName;5859public AcceleratedXORModeTest(String fileName) {60this.fileName = fileName;61}6263void draw(Graphics2D g) {64g.setColor(backColor);65g.fillRect(0, 0, width, height);66g.setXORMode(xorColor1);67drawPattern(g, 100);68g.setXORMode(xorColor2);69drawPattern(g, 400);70g.dispose();71}7273void test(BufferedImage bi) {74comparePattern(bi, 150, xorColor1.getRGB());75comparePattern(bi, 450, xorColor2.getRGB());76}7778void comparePattern(BufferedImage bi, int startX, int xorColor) {79int[] expectedColors = {80backColor.getRGB() ^ color1.getRGB() ^ xorColor,81backColor.getRGB() ^ color1.getRGB() ^ xorColor ^82color2.getRGB() ^ xorColor,83backColor.getRGB() ^ color2.getRGB() ^ xorColor84};85for (int i = 0; i < 3; i++) {86int x = startX + 100 * i;87int rgb = bi.getRGB(x, 150);88if (rgb != expectedColors[i]) {89String msg = "Colors mismatch: x = " + x +90", got " + new Color(rgb) + ", expected " +91new Color(expectedColors[i]);92System.err.println(msg);93write(bi);94throw new RuntimeException("FAILED: " + msg);95}96}97}9899void drawPattern(Graphics2D g, int x) {100g.setColor(color1);101g.fillRect(x, 0, 200, 300);102g.setColor(color2);103g.fillRect(x+100, 0, 200, 300);104}105106GraphicsConfiguration getDefaultGC() {107return GraphicsEnvironment.getLocalGraphicsEnvironment().108getDefaultScreenDevice().getDefaultConfiguration();109}110111void createVImg() {112if (vImg != null) {113vImg.flush();114vImg = null;115}116vImg = getDefaultGC().createCompatibleVolatileImage(width, height);117}118119void write(BufferedImage bi) {120if (fileName != null) {121try {122ImageIO.write(bi, "png", new File(fileName));123} catch (IOException e) {124System.err.println("Can't write image file " + fileName);125}126}127}128129void test() {130createVImg();131BufferedImage bi = null;132do {133int valCode = vImg.validate(getDefaultGC());134if (valCode == VolatileImage.IMAGE_INCOMPATIBLE) {135createVImg();136}137Graphics2D g = vImg.createGraphics();138draw(g);139bi = vImg.getSnapshot();140} while (vImg.contentsLost());141if (bi != null) {142test(bi);143write(bi);144}145}146}147148149