Path: blob/master/test/jdk/sun/java2d/SunGraphics2D/SourceClippingBlitTest/SourceClippingBlitTest.java
41153 views
/*1* Copyright (c) 2005, 2010, 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*/2223/*24@test25@key headful26@bug 624457427@bug 625814228@bug 639516529@bug 658888430@summary Tests that source is clipped correctly when blitting31different types of images to the screen32@author Dmitri.Trembovetski: area=Graphics2D33@run main SourceClippingBlitTest34*/3536import java.awt.AWTException;37import java.awt.Canvas;38import java.awt.Color;39import java.awt.Dimension;40import java.awt.Frame;41import java.awt.Graphics;42import java.awt.GraphicsConfiguration;43import java.awt.Image;44import java.awt.Point;45import java.awt.Rectangle;46import java.awt.Robot;47import java.awt.Toolkit;48import java.awt.Transparency;49import java.awt.event.ComponentAdapter;50import java.awt.event.ComponentEvent;51import java.awt.event.WindowAdapter;52import java.awt.event.WindowEvent;53import java.awt.image.BufferedImage;54import java.awt.image.VolatileImage;5556public class SourceClippingBlitTest extends Canvas {57static final int TESTW = 300;58static final int TESTH = 300;59static final int IMAGEW = 50;60static final int IMAGEH = 50;6162static final Rectangle IMAGE_BOUNDS = new Rectangle(0, 0, IMAGEW, IMAGEH);63static Robot robot;64static private boolean showErrors;6566private static final Object lock = new Object();67private static volatile boolean done = false;6869BufferedImage grabbedBI;7071public static void main(String[] args) {72// allow user to override the properties if needed73if (System.getProperty("sun.java2d.pmoffscreen") == null) {74System.setProperty("sun.java2d.pmoffscreen", "true");75}7677if (args.length > 0 && args[0].equals("-showerrors")) {78showErrors = true;79}8081try {82robot = new Robot();83} catch (AWTException e) {84throw new RuntimeException(e);85}8687Frame f = new Frame(SourceClippingBlitTest.class.getName());88final SourceClippingBlitTest test = new SourceClippingBlitTest();89f.add(test);90f.addWindowListener(new WindowAdapter() {91public void windowActivated(WindowEvent e) {92if (!done) {93test.runTests();94}95}9697});98f.pack();99f.setLocation(100, 100);100f.setVisible(true);101synchronized (lock) {102while (!done) {103try {104lock.wait();105} catch (InterruptedException ex) {106ex.printStackTrace();107}108}109}110if (!showErrors) {111f.dispose();112}113}114115public Dimension getPreferredSize() {116return new Dimension(TESTW, TESTH);117}118119public void paint(Graphics g) {120if (showErrors && done && grabbedBI != null) {121g.drawImage(grabbedBI, 0, 0, null);122}123}124125public void runTests() {126GraphicsConfiguration gc = getGraphicsConfiguration();127for (Image srcIm :128new Image[] {129getBufferedImage(gc, IMAGEW, IMAGEH,130BufferedImage.TYPE_INT_RGB, true),131getBufferedImage(gc, IMAGEW, IMAGEH,132BufferedImage.TYPE_INT_RGB, false),133// commented out due to 6593406134// getBMImage(gc, IMAGEW, IMAGEH),135// getBufferedImage(gc, IMAGEW, IMAGEH,136// BufferedImage.TYPE_INT_ARGB, true),137// getBufferedImage(gc, IMAGEW, IMAGEH,138// BufferedImage.TYPE_INT_ARGB, false),139getVImage(gc, IMAGEW, IMAGEH),140})141{142System.out.println("Testing source: " + srcIm);143// wiggle the source and dest rectangles144try {145for (int locationVar = -10; locationVar < 20; locationVar += 10)146{147for (int sizeVar = -10; sizeVar < 20; sizeVar += 10) {148Rectangle srcRect = (Rectangle)IMAGE_BOUNDS.clone();149srcRect.translate(locationVar, locationVar);150srcRect.grow(sizeVar, sizeVar);151152Rectangle dstRect =153new Rectangle(sizeVar, sizeVar,154srcRect.width, srcRect.height);155System.out.println("testing blit rect src: " + srcRect);156System.out.println(" dst: " + dstRect);157render(getGraphics(), srcIm, srcRect, dstRect);158test(srcRect, dstRect);159}160}161System.out.println("Test passed.");162} finally {163synchronized (lock) {164done = true;165lock.notifyAll();166}167}168}169}170171public void render(Graphics g, Image image,172Rectangle srcRect, Rectangle dstRect)173{174int w = getWidth();175int h = getHeight();176g.setColor(Color.green);177g.fillRect(0, 0, w, h);178179int bltWidth = srcRect.width;180int bltHeight = srcRect.height;181VolatileImage vi = null;182if (image instanceof VolatileImage) {183vi = (VolatileImage)image;184}185do {186if (vi != null) {187GraphicsConfiguration gc = getGraphicsConfiguration();188if (vi.validate(gc) != VolatileImage.IMAGE_OK) {189initImage(gc, vi);190}191}192g.drawImage(image,193dstRect.x, dstRect.y,194dstRect.x + bltWidth, dstRect.y + bltHeight,195srcRect.x, srcRect.y,196srcRect.x + bltWidth, srcRect.y + bltHeight,197Color.red,198null);199} while (vi != null && vi.contentsLost());200}201202203public void test(Rectangle srcRect, Rectangle dstRect) {204int w = getWidth();205int h = getHeight();206Toolkit.getDefaultToolkit().sync();207try {208Thread.sleep(2000);209} catch (InterruptedException ex) {}210Point p = getLocationOnScreen();211grabbedBI = robot.createScreenCapture(new Rectangle(p.x, p.y, w, h));212213// calculate the destination rectangle214Rectangle srcBounds = srcRect.intersection(IMAGE_BOUNDS);215int trX = dstRect.x - srcRect.x;216int trY = dstRect.y - srcRect.y;217Rectangle newDstRect = (Rectangle)dstRect.clone();218newDstRect.translate(-trX, -trY);219Rectangle.intersect(newDstRect, srcBounds, newDstRect);220newDstRect.translate(trX, trY);221Rectangle.intersect(newDstRect, new Rectangle(0, 0, w, h), newDstRect);222223System.out.println("calculated dest rect:" + newDstRect);224225// we do implicit clipping of the destination surface226// by only checking pixels within its bounds227for (int y = 0; y < h; y++) {228for (int x = 0; x < w; x++) {229int rgb = 0;230if (newDstRect.contains(x, y)) {231rgb = Color.red.getRGB();232} else {233rgb = Color.green.getRGB();234}235if (grabbedBI.getRGB(x, y) != rgb) {236String msg1 = "Test failed at x="+x+" y="+y;237System.out.println(msg1);238System.out.println(" expected: "+Integer.toHexString(rgb)+239" got:"+Integer.toHexString(grabbedBI.getRGB(x, y)));240throw new RuntimeException(msg1);241}242}243}244System.out.println("subtest passed");245}246247static VolatileImage dstImage;248static void initImage(GraphicsConfiguration gc, Image image) {249Graphics g = image.getGraphics();250g.setColor(Color.RED);251int w = image.getWidth(null);252int h = image.getHeight(null);253g.fillRect(0, 0, w, h);254g.dispose();255256// need to 'accelerate' the image257if (dstImage == null) {258dstImage =259gc.createCompatibleVolatileImage(TESTW, TESTH,260Transparency.OPAQUE);261}262dstImage.validate(gc);263g = dstImage.getGraphics();264g.drawImage(image, 0, 0, null);265g.drawImage(image, 0, 0, null);266g.drawImage(image, 0, 0, null);267}268269static VolatileImage getVImage(GraphicsConfiguration gc,270int w, int h)271{272VolatileImage image =273gc.createCompatibleVolatileImage(w, h, Transparency.OPAQUE);274image.validate(gc);275initImage(gc, image);276return image;277}278279static Image getBMImage(GraphicsConfiguration gc,280int w, int h)281{282Image image =283gc.createCompatibleImage(w, h, Transparency.BITMASK);284initImage(gc, image);285return image;286}287288static Image getBufferedImage(GraphicsConfiguration gc,289int w, int h, int type, boolean acceleratable)290{291BufferedImage image = new BufferedImage(w, h, type);292if (!acceleratable) {293image.setAccelerationPriority(0.0f);294}295initImage(gc, image);296return image;297}298}299300301