1 /* 2 * Copyright (c) 2014, 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.AlphaComposite; 25 import java.awt.Color; 26 import java.awt.Graphics2D; 27 import java.awt.GraphicsConfiguration; 28 import java.awt.GraphicsEnvironment; 29 import java.awt.image.BufferedImage; 30 import java.awt.image.VolatileImage; 31 import java.io.File; 32 import java.io.IOException; 33 34 import javax.imageio.ImageIO; 35 36 /** 37 * @test 38 * @bug 8041129 39 * @summary Destination offset should be correct in case of Surface->SW blit. 40 * Destination outside of the drawing area should be untouched. 41 * @author Sergey Bylokhov 42 */ 43 public final class IncorrectDestinationOffset { 44 45 private static final int SIZE = 128; 46 private static final double[] SCALES = {0.25, 0.5, 1, 1.5, 2.0, 4}; 47 48 public static void main(final String[] args) throws IOException { 49 GraphicsEnvironment ge = GraphicsEnvironment 50 .getLocalGraphicsEnvironment(); 51 GraphicsConfiguration gc = ge.getDefaultScreenDevice() 52 .getDefaultConfiguration(); 53 VolatileImage vi = gc.createCompatibleVolatileImage(SIZE, SIZE); 54 BufferedImage bi = new BufferedImage(SIZE, SIZE, 55 BufferedImage.TYPE_INT_ARGB); 56 for (double scale : SCALES) { 57 while (true) { 58 // initialize Volatile Image 59 vi.validate(gc); 60 Graphics2D g2d = vi.createGraphics(); 61 g2d.setColor(Color.green); 62 g2d.fillRect(0, 0, SIZE, SIZE); 63 g2d.dispose(); 64 65 if (vi.validate(gc) != VolatileImage.IMAGE_OK) { 66 try { 67 Thread.sleep(100); 68 } catch (InterruptedException ignored) { 69 } 70 continue; 71 } 72 // Draw the VolatileImage to BI with scale and offsets 73 Graphics2D g = bi.createGraphics(); 74 g.setComposite(AlphaComposite.Src); 75 g.setColor(Color.RED); 76 g.fillRect(0, 0, SIZE / 2, SIZE / 2); 77 g.setColor(Color.BLUE); 78 g.fillRect(SIZE / 2, 0, SIZE / 2, SIZE / 2); 79 g.setColor(Color.ORANGE); 80 g.fillRect(0, SIZE / 2, SIZE / 2, SIZE / 2); 81 g.setColor(Color.MAGENTA); 82 g.fillRect(SIZE / 2, SIZE / 2, SIZE / 2, SIZE / 2); 83 84 int point2draw = (int) (100 * scale); 85 int size2draw = (int) (SIZE * scale); 86 g.drawImage(vi, point2draw, point2draw, size2draw, size2draw, 87 null); 88 g.dispose(); 89 90 if (vi.contentsLost()) { 91 try { 92 Thread.sleep(100); 93 } catch (InterruptedException ignored) { 94 } 95 continue; 96 } 97 validate(bi, point2draw, size2draw); 98 break; 99 } 100 } 101 } 102 103 private static void validate(BufferedImage bi, int point2draw, 104 int size2draw) 105 throws IOException { 106 for (int x = 0; x < SIZE; ++x) { 107 for (int y = 0; y < SIZE; ++y) { 108 if (isInsideGreenArea(point2draw, size2draw, x, y)) { 109 if (bi.getRGB(x, y) != Color.green.getRGB()) { 110 ImageIO.write(bi, "png", new File("image.png")); 111 throw new RuntimeException("Test failed."); 112 } 113 } else { 114 if (isRedArea(x, y)) { 115 if (bi.getRGB(x, y) != Color.red.getRGB()) { 116 ImageIO.write(bi, "png", new File("image.png")); 117 throw new RuntimeException("Test failed."); 118 } 119 } 120 if (isBlueArea(x, y)) { 121 if (bi.getRGB(x, y) != Color.blue.getRGB()) { 122 ImageIO.write(bi, "png", new File("image.png")); 123 throw new RuntimeException("Test failed."); 124 } 125 } 126 if (isOrangeArea(x, y)) { 127 if (bi.getRGB(x, y) != Color.orange.getRGB()) { 128 ImageIO.write(bi, "png", new File("image.png")); 129 throw new RuntimeException("Test failed."); 130 } 131 } 132 if (isMagentaArea(x, y)) { 133 if (bi.getRGB(x, y) != Color.magenta.getRGB()) { 134 ImageIO.write(bi, "png", new File("image.png")); 135 throw new RuntimeException("Test failed."); 136 } 137 } 138 } 139 } 140 } 141 } 142 143 private static boolean isRedArea(int x, int y) { 144 return x < SIZE / 2 && y < SIZE / 2; 145 } 146 147 private static boolean isBlueArea(int x, int y) { 148 return x >= SIZE / 2 && y < SIZE / 2; 149 } 150 151 private static boolean isOrangeArea(int x, int y) { 152 return x < SIZE / 2 && y >= SIZE / 2; 153 } 154 155 private static boolean isMagentaArea(int x, int y) { 156 return x >= SIZE / 2 && y >= SIZE / 2; 157 } 158 159 private static boolean isInsideGreenArea(int point2draw, int size2draw, 160 int x, int y) { 161 return x >= point2draw && x < point2draw + size2draw && y >= 162 point2draw && y < point2draw + size2draw; 163 } 164 }