Path: blob/master/test/jdk/sun/java2d/DirectX/AcceleratedScaleTest/AcceleratedScaleTest.java
41153 views
/*1* Copyright (c) 2006, 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.Color;24import java.awt.Frame;25import java.awt.Graphics;26import java.awt.Graphics2D;27import java.awt.GraphicsConfiguration;28import java.awt.GraphicsEnvironment;29import java.awt.RenderingHints;30import java.awt.Toolkit;31import java.awt.image.BufferedImage;32import java.awt.image.VolatileImage;33import java.io.File;34import java.io.IOException;35import javax.imageio.ImageIO;3637/**38* @test39* @key headful40* @bug 6429665 6588884 819861341* @summary Tests that the transform is correctly handled42* @author Dmitri.Trembovetski area=Graphics43* @run main AcceleratedScaleTest44* @run main/othervm -Dsun.java2d.d3d=true AcceleratedScaleTest45*/46public class AcceleratedScaleTest {47private static final int IMAGE_SIZE = 200;48private static VolatileImage destVI;4950private static void initVI(GraphicsConfiguration gc) {51int res;52if (destVI == null) {53res = VolatileImage.IMAGE_INCOMPATIBLE;54} else {55res = destVI.validate(gc);56}57if (res == VolatileImage.IMAGE_INCOMPATIBLE) {58if (destVI != null) destVI.flush();59destVI = gc.createCompatibleVolatileImage(IMAGE_SIZE, IMAGE_SIZE);60destVI.validate(gc);61res = VolatileImage.IMAGE_RESTORED;62}63if (res == VolatileImage.IMAGE_RESTORED) {64Graphics vig = destVI.getGraphics();65vig.setColor(Color.red);66vig.fillRect(0, 0, destVI.getWidth(), destVI.getHeight());67vig.dispose();68}69}7071public static void main(String[] args) {72Frame f = new Frame();73f.pack();74GraphicsConfiguration gc = f.getGraphicsConfiguration();75if (gc.getColorModel().getPixelSize() < 16) {76System.out.printf("Bit depth: %d . Test considered passed.",77gc.getColorModel().getPixelSize());78f.dispose();79return;80}8182BufferedImage bi =83new BufferedImage(IMAGE_SIZE/4, IMAGE_SIZE/4,84BufferedImage.TYPE_INT_RGB);85Graphics2D g = (Graphics2D)bi.getGraphics();86g.setColor(Color.red);87g.fillRect(0, 0, bi.getWidth(), bi.getHeight());88BufferedImage snapshot;89do {90initVI(gc);91g = (Graphics2D)destVI.getGraphics();92// "accelerate" BufferedImage93for (int i = 0; i < 5; i++) {94g.drawImage(bi, 0, 0, null);95}96g.setColor(Color.white);97g.fillRect(0, 0, destVI.getWidth(), destVI.getHeight());9899// this will force the use of Transform primitive instead of100// Scale (the latter doesn't do bilinear filtering required by101// VALUE_RENDER_QUALITY, which triggers the bug in D3D pipeline102g.setRenderingHint(RenderingHints.KEY_RENDERING,103RenderingHints.VALUE_RENDER_QUALITY);104g.drawImage(bi, 0, 0, destVI.getWidth(), destVI.getHeight(), null);105g.fillRect(0, 0, destVI.getWidth(), destVI.getHeight());106107g.drawImage(bi, 0, 0, destVI.getWidth(), destVI.getHeight(), null);108109snapshot = destVI.getSnapshot();110} while (destVI.contentsLost());111112f.dispose();113int whitePixel = Color.white.getRGB();114for (int y = 0; y < snapshot.getHeight(); y++) {115for (int x = 0; x < snapshot.getWidth(); x++) {116if (snapshot.getRGB(x, y) == whitePixel) {117System.out.printf("Found untouched pixel at %dx%d\n", x, y);118System.out.println("Dumping the dest. image to " +119"AcceleratedScaleTest_dst.png");120try {121ImageIO.write(snapshot, "png",122new File("AcceleratedScaleTest_dst.png"));123} catch (IOException ex) {124ex.printStackTrace();125}126throw new RuntimeException("Test failed.");127}128}129}130System.out.println("Test Passed.");131}132133}134135136