1 /* 2 * Copyright (c) 2015, 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.*; 25 import java.awt.event.WindowAdapter; 26 import java.awt.event.WindowEvent; 27 import java.awt.image.BufferedImage; 28 import java.awt.image.VolatileImage; 29 import static sun.awt.OSInfo.*; 30 31 /** 32 * @test 33 * @bug 8069348 34 * @summary SunGraphics2D.copyArea() does not properly work for scaled graphics 35 * @modules java.desktop/sun.awt 36 * @run main/othervm -Dsun.java2d.uiScale=2 CopyScaledAreaTest 37 * @run main/othervm -Dsun.java2d.opengl=true -Dsun.java2d.uiScale=2 CopyScaledAreaTest 38 * @run main/othervm -Dsun.java2d.d3d=true -Dsun.java2d.uiScale=2 CopyScaledAreaTest 39 * @run main/othervm -Dsun.java2d.d3d=false -Dsun.java2d.opengl=false 40 * -Dsun.java2d.uiScale=2 CopyScaledAreaTest 41 */ 42 public class CopyScaledAreaTest { 43 44 private static final int IMAGE_WIDTH = 800; 45 private static final int IMAGE_HEIGHT = 800; 46 private static final double SCALE = 1.3; 47 private static final int X = 50; 48 private static final int Y = 50; 49 private static final int W = 100; 50 private static final int H = 75; 51 private static final int DX = 15; 52 private static final int DY = 10; 53 private static final int N = 3; 54 private static final Color BACKGROUND_COLOR = Color.YELLOW; 55 private static final Color FILL_COLOR = Color.ORANGE; 56 57 private static boolean isSupported() { 58 String d3d = System.getProperty("sun.java2d.d3d"); 59 return !Boolean.getBoolean(d3d) || getOSType() == OSType.WINDOWS; 60 } 61 62 private static int scale(int x) { 63 return (int) Math.floor(x * SCALE); 64 } 65 66 private static VolatileImage createVolatileImage(GraphicsConfiguration conf) { 67 return conf.createCompatibleVolatileImage(IMAGE_WIDTH, IMAGE_HEIGHT); 68 } 69 70 // rendering to the image 71 private static void renderOffscreen(VolatileImage vImg, 72 GraphicsConfiguration conf) { 73 int attempts = 0; 74 do { 75 76 if (attempts > 10) { 77 throw new RuntimeException("Too many attempts!"); 78 } 79 80 if (vImg.validate(conf) == VolatileImage.IMAGE_INCOMPATIBLE) { 81 // old vImg doesn't work with new GraphicsConfig; re-create it 82 vImg = createVolatileImage(conf); 83 } 84 Graphics2D g = vImg.createGraphics(); 85 // 86 // miscellaneous rendering commands... 87 // 88 g.setColor(BACKGROUND_COLOR); 89 g.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT); 90 g.scale(SCALE, SCALE); 91 92 g.setColor(FILL_COLOR); 93 g.fillRect(X, Y, W, H); 94 95 for (int i = 0; i < N; i++) { 96 g.copyArea(X + i * DX, Y + i * DY, W, H, DX, DY); 97 } 98 g.dispose(); 99 attempts++; 100 } while (vImg.contentsLost()); 101 } 102 103 public static void main(String[] args) throws Exception { 104 105 if (!isSupported()) { 106 return; 107 } 108 109 GraphicsConfiguration graphicsConfiguration = 110 GraphicsEnvironment.getLocalGraphicsEnvironment() 111 .getDefaultScreenDevice().getDefaultConfiguration(); 112 113 BufferedImage buffImage = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, 114 BufferedImage.TYPE_INT_RGB); 115 Graphics g = buffImage.createGraphics(); 116 117 // copying from the image (here, gScreen is the Graphics 118 // object for the onscreen window) 119 VolatileImage vImg = createVolatileImage(graphicsConfiguration); 120 121 int attempts = 0; 122 do { 123 124 if (attempts > 10) { 125 throw new RuntimeException("Too many attempts!"); 126 } 127 128 int returnCode = vImg.validate(graphicsConfiguration); 129 if (returnCode == VolatileImage.IMAGE_RESTORED) { 130 // Contents need to be restored 131 renderOffscreen(vImg, graphicsConfiguration); // restore contents 132 } else if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE) { 133 // old vImg doesn't work with new GraphicsConfig; re-create it 134 vImg = createVolatileImage(graphicsConfiguration); 135 renderOffscreen(vImg, graphicsConfiguration); 136 } 137 g.drawImage(vImg, 0, 0, null); 138 attempts++; 139 } while (vImg.contentsLost()); 140 141 g.dispose(); 142 143 int x = scale(X + N * DX); 144 int y = scale(Y + N * DY); 145 int w = scale(W); 146 int h = scale(H); 147 148 for (int i = x; i < x + w; i++) { 149 for (int j = y; j < y + h; j++) { 150 if (buffImage.getRGB(i, j) != FILL_COLOR.getRGB()) { 151 throw new RuntimeException("Wrong rectangle color!"); 152 } 153 } 154 } 155 } 156 }