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