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