Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/java2d/AcceleratedXORModeTest.java
38833 views
/*1* Copyright (c) 2013, 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* @bug 802434326* @summary Test verifies that accelerated pipelines27* correctly draws primitives in XOR mode.28* @run main/othervm -Dsun.java2d.xrender=True AcceleratedXORModeTest29*/3031import java.awt.Color;32import java.awt.Graphics2D;33import java.awt.GraphicsConfiguration;34import java.awt.GraphicsEnvironment;35import java.awt.image.BufferedImage;36import java.awt.image.VolatileImage;37import java.io.File;38import java.io.IOException;39import javax.imageio.ImageIO;4041public class AcceleratedXORModeTest {42public static void main(String argv[]) {43String fileName = argv.length > 0 ? argv[0] : null;44new AcceleratedXORModeTest(fileName).test();45}4647static final Color backColor = Color.red;48static final Color color1 = Color.green;49static final Color color2 = Color.yellow;50static final Color xorColor1 = Color.blue;51static final Color xorColor2 = Color.white;5253static final int width = 700, height = 300;5455VolatileImage vImg = null;56String fileName;5758public AcceleratedXORModeTest(String fileName) {59this.fileName = fileName;60}6162void draw(Graphics2D g) {63g.setColor(backColor);64g.fillRect(0, 0, width, height);65g.setXORMode(xorColor1);66drawPattern(g, 100);67g.setXORMode(xorColor2);68drawPattern(g, 400);69g.dispose();70}7172void test(BufferedImage bi) {73comparePattern(bi, 150, xorColor1.getRGB());74comparePattern(bi, 450, xorColor2.getRGB());75}7677void comparePattern(BufferedImage bi, int startX, int xorColor) {78int[] expectedColors = {79backColor.getRGB() ^ color1.getRGB() ^ xorColor,80backColor.getRGB() ^ color1.getRGB() ^ xorColor ^81color2.getRGB() ^ xorColor,82backColor.getRGB() ^ color2.getRGB() ^ xorColor83};84for (int i = 0; i < 3; i++) {85int x = startX + 100 * i;86int rgb = bi.getRGB(x, 150);87if (rgb != expectedColors[i]) {88String msg = "Colors mismatch: x = " + x +89", got " + new Color(rgb) + ", expected " +90new Color(expectedColors[i]);91System.err.println(msg);92write(bi);93throw new RuntimeException("FAILED: " + msg);94}95}96}9798void drawPattern(Graphics2D g, int x) {99g.setColor(color1);100g.fillRect(x, 0, 200, 300);101g.setColor(color2);102g.fillRect(x+100, 0, 200, 300);103}104105GraphicsConfiguration getDefaultGC() {106return GraphicsEnvironment.getLocalGraphicsEnvironment().107getDefaultScreenDevice().getDefaultConfiguration();108}109110void createVImg() {111if (vImg != null) {112vImg.flush();113vImg = null;114}115vImg = getDefaultGC().createCompatibleVolatileImage(width, height);116}117118void write(BufferedImage bi) {119if (fileName != null) {120try {121ImageIO.write(bi, "png", new File(fileName));122} catch (IOException e) {123System.err.println("Can't write image file " + fileName);124}125}126}127128void test() {129createVImg();130do {131int valCode = vImg.validate(getDefaultGC());132if (valCode == VolatileImage.IMAGE_INCOMPATIBLE) {133createVImg();134}135Graphics2D g = vImg.createGraphics();136draw(g);137BufferedImage bi = vImg.getSnapshot();138test(bi);139write(bi);140} while (vImg.contentsLost());141}142}143144145