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