1 /*
   2  * Copyright (c) 2006, 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.Color;
  25 import java.awt.Frame;
  26 import java.awt.Graphics;
  27 import java.awt.Graphics2D;
  28 import java.awt.GraphicsConfiguration;
  29 import java.awt.GraphicsEnvironment;
  30 import java.awt.RenderingHints;
  31 import java.awt.Toolkit;
  32 import java.awt.image.BufferedImage;
  33 import java.awt.image.VolatileImage;
  34 import java.io.File;
  35 import java.io.IOException;
  36 import javax.imageio.ImageIO;
  37 
  38 /**
  39  * @test
  40  * @key headful
  41  * @bug 6429665
  42  * @bug 6588884
  43  * @summary Tests that the transform is correctly handled
  44  * @author Dmitri.Trembovetski area=Graphics
  45  * @run main AcceleratedScaleTest
  46  * @run main/othervm -Dsun.java2d.d3d=true AcceleratedScaleTest
  47  * @run main/othervm -Dsun.java2d.opengl=true AcceleratedScaleTest
  48  */
  49 public class AcceleratedScaleTest {
  50     private static final int IMAGE_SIZE = 200;
  51     private static VolatileImage destVI;
  52 
  53     private static void initVI(GraphicsConfiguration gc) {
  54         int res;
  55         if (destVI == null) {
  56             res = VolatileImage.IMAGE_INCOMPATIBLE;
  57         } else {
  58             res = destVI.validate(gc);
  59         }
  60         if (res == VolatileImage.IMAGE_INCOMPATIBLE) {
  61             if (destVI != null) destVI.flush();
  62             destVI = gc.createCompatibleVolatileImage(IMAGE_SIZE, IMAGE_SIZE);
  63             destVI.validate(gc);
  64             res = VolatileImage.IMAGE_RESTORED;
  65         }
  66         if (res == VolatileImage.IMAGE_RESTORED) {
  67             Graphics vig = destVI.getGraphics();
  68             vig.setColor(Color.red);
  69             vig.fillRect(0, 0, destVI.getWidth(), destVI.getHeight());
  70             vig.dispose();
  71         }
  72     }
  73 
  74     public static void main(String[] args) {
  75         Frame f = new Frame();
  76         f.pack();
  77         GraphicsConfiguration gc = f.getGraphicsConfiguration();
  78         if (gc.getColorModel().getPixelSize() < 16) {
  79             System.out.printf("Bit depth: %d . Test considered passed.",
  80                               gc.getColorModel().getPixelSize());
  81             f.dispose();
  82             return;
  83         }
  84 
  85         BufferedImage bi =
  86             new BufferedImage(IMAGE_SIZE/4, IMAGE_SIZE/4,
  87                               BufferedImage.TYPE_INT_RGB);
  88         Graphics2D g = (Graphics2D)bi.getGraphics();
  89         g.setColor(Color.red);
  90         g.fillRect(0, 0, bi.getWidth(), bi.getHeight());
  91         BufferedImage snapshot;
  92         do {
  93             initVI(gc);
  94             g = (Graphics2D)destVI.getGraphics();
  95             // "accelerate" BufferedImage
  96             for (int i = 0; i < 5; i++) {
  97                 g.drawImage(bi, 0, 0, null);
  98             }
  99             g.setColor(Color.white);
 100             g.fillRect(0, 0, destVI.getWidth(), destVI.getHeight());
 101 
 102             // this will force the use of Transform primitive instead of
 103             // Scale (the latter doesn't do bilinear filtering required by
 104             // VALUE_RENDER_QUALITY, which triggers the bug in D3D pipeline
 105             g.setRenderingHint(RenderingHints.KEY_RENDERING,
 106                                RenderingHints.VALUE_RENDER_QUALITY);
 107             g.drawImage(bi, 0, 0, destVI.getWidth(), destVI.getHeight(), null);
 108             g.fillRect(0, 0, destVI.getWidth(), destVI.getHeight());
 109 
 110             g.drawImage(bi, 0, 0, destVI.getWidth(), destVI.getHeight(), null);
 111 
 112             snapshot = destVI.getSnapshot();
 113         } while (destVI.contentsLost());
 114 
 115         f.dispose();
 116         int whitePixel = Color.white.getRGB();
 117         for (int y = 0; y < snapshot.getHeight(); y++) {
 118             for (int x = 0; x < snapshot.getWidth(); x++) {
 119                 if (snapshot.getRGB(x, y) == whitePixel) {
 120                     System.out.printf("Found untouched pixel at %dx%d\n", x, y);
 121                     System.out.println("Dumping the dest. image to " +
 122                                        "AcceleratedScaleTest_dst.png");
 123                     try {
 124                         ImageIO.write(snapshot, "png",
 125                                       new File("AcceleratedScaleTest_dst.png"));
 126                     } catch (IOException ex) {
 127                         ex.printStackTrace();
 128                     }
 129                     throw new RuntimeException("Test failed.");
 130                 }
 131             }
 132         }
 133         System.out.println("Test Passed.");
 134     }
 135 
 136 }