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