Path: blob/master/test/jdk/java/awt/Graphics/CopyScaledArea/CopyScaledAreaTest.java
41154 views
/*1* Copyright (c) 2015, 2018, 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*/2223import java.awt.*;24import java.awt.image.BufferedImage;25import java.awt.image.VolatileImage;26import static sun.awt.OSInfo.*;2728/**29* @test30* @key headful31* @bug 8069348 819861332* @summary SunGraphics2D.copyArea() does not properly work for scaled graphics33* @modules java.desktop/sun.awt34* @run main/othervm -Dsun.java2d.uiScale=2 CopyScaledAreaTest35* @run main/othervm -Dsun.java2d.d3d=true -Dsun.java2d.uiScale=2 CopyScaledAreaTest36* @run main/othervm -Dsun.java2d.d3d=false -Dsun.java2d.opengl=false37* -Dsun.java2d.uiScale=2 CopyScaledAreaTest38*/39public class CopyScaledAreaTest {4041private static final int IMAGE_WIDTH = 800;42private static final int IMAGE_HEIGHT = 800;43private static final int X = 50;44private static final int Y = 50;45private static final int W = 100;46private static final int H = 75;47private static final int DX = 15;48private static final int DY = 10;49private static final int N = 3;50private static final Color BACKGROUND_COLOR = Color.YELLOW;51private static final Color FILL_COLOR = Color.ORANGE;52private static final double[][] SCALES = {{1.3, 1.4}, {0.3, 2.3}, {2.7, 0.1}};5354private static boolean isSupported() {55String d3d = System.getProperty("sun.java2d.d3d");56return !Boolean.getBoolean(d3d) || getOSType() == OSType.WINDOWS;57}5859private static int scale(int x, double scale) {60return (int) Math.floor(x * scale);61}6263private static VolatileImage createVolatileImage(GraphicsConfiguration conf) {64return conf.createCompatibleVolatileImage(IMAGE_WIDTH, IMAGE_HEIGHT);65}6667// rendering to the image68private static void renderOffscreen(VolatileImage vImg,69GraphicsConfiguration conf,70double scaleX,71double scaleY)72{73int attempts = 0;74do {7576if (attempts > 10) {77throw new RuntimeException("Too many attempts!");78}7980if (vImg.validate(conf) == VolatileImage.IMAGE_INCOMPATIBLE) {81// old vImg doesn't work with new GraphicsConfig; re-create it82vImg = createVolatileImage(conf);83}84Graphics2D g = vImg.createGraphics();85//86// miscellaneous rendering commands...87//88g.setColor(BACKGROUND_COLOR);89g.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT);90g.scale(scaleX, scaleY);9192g.setColor(FILL_COLOR);93g.fillRect(X, Y, W, H);9495for (int i = 0; i < N; i++) {96g.copyArea(X + i * DX, Y + i * DY, W, H, DX, DY);97}98g.dispose();99attempts++;100} while (vImg.contentsLost());101}102103public static void main(String[] args) throws Exception {104105if (!isSupported()) {106return;107}108109GraphicsConfiguration graphicsConfiguration =110GraphicsEnvironment.getLocalGraphicsEnvironment()111.getDefaultScreenDevice().getDefaultConfiguration();112113for(double[] scales: SCALES){114testScale(scales[0], scales[1], graphicsConfiguration);115}116}117118private static void testScale(double scaleX, double scaleY,119GraphicsConfiguration gc) throws Exception120{121122BufferedImage buffImage = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT,123BufferedImage.TYPE_INT_RGB);124Graphics g = buffImage.createGraphics();125126VolatileImage vImg = createVolatileImage(gc);127128int attempts = 0;129do {130131if (attempts > 10) {132throw new RuntimeException("Too many attempts!");133}134135int returnCode = vImg.validate(gc);136if (returnCode == VolatileImage.IMAGE_RESTORED) {137// Contents need to be restored138renderOffscreen(vImg, gc, scaleX, scaleY); // restore contents139} else if (returnCode == VolatileImage.IMAGE_INCOMPATIBLE) {140// old vImg doesn't work with new GraphicsConfig; re-create it141vImg = createVolatileImage(gc);142renderOffscreen(vImg, gc, scaleX, scaleY);143}144g.drawImage(vImg, 0, 0, null);145attempts++;146} while (vImg.contentsLost());147148g.dispose();149150int x = scale(X + N * DX, scaleX) + 1;151int y = scale(Y + N * DY, scaleY) + 1;152int w = scale(W, scaleX) - 2;153int h = scale(H, scaleY) - 2;154155for (int i = x; i < x + w; i++) {156for (int j = y; j < y + h; j++) {157if (buffImage.getRGB(i, j) != FILL_COLOR.getRGB()) {158throw new RuntimeException("Wrong rectangle color!");159}160}161}162}163}164165166