Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/java2d/SunGraphics2D/SourceClippingBlitTest/SourceClippingBlitTest.java
38918 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@bug 624457426@bug 625814227@bug 639516528@bug 658888429@summary Tests that source is clipped correctly when blitting30different types of images to the screen31@author Dmitri.Trembovetski: area=Graphics2D32@run main SourceClippingBlitTest33*/3435import java.awt.AWTException;36import java.awt.Canvas;37import java.awt.Color;38import java.awt.Dimension;39import java.awt.Frame;40import java.awt.Graphics;41import java.awt.GraphicsConfiguration;42import java.awt.Image;43import java.awt.Point;44import java.awt.Rectangle;45import java.awt.Robot;46import java.awt.Toolkit;47import java.awt.Transparency;48import java.awt.event.ComponentAdapter;49import java.awt.event.ComponentEvent;50import java.awt.event.WindowAdapter;51import java.awt.event.WindowEvent;52import java.awt.image.BufferedImage;53import java.awt.image.VolatileImage;5455public class SourceClippingBlitTest extends Canvas {56static final int TESTW = 300;57static final int TESTH = 300;58static final int IMAGEW = 50;59static final int IMAGEH = 50;6061static final Rectangle IMAGE_BOUNDS = new Rectangle(0, 0, IMAGEW, IMAGEH);62static Robot robot;63static private boolean showErrors;6465private static final Object lock = new Object();66private static volatile boolean done = false;6768BufferedImage grabbedBI;6970public static void main(String[] args) {71// allow user to override the properties if needed72if (System.getProperty("sun.java2d.pmoffscreen") == null) {73System.setProperty("sun.java2d.pmoffscreen", "true");74}7576if (args.length > 0 && args[0].equals("-showerrors")) {77showErrors = true;78}7980try {81robot = new Robot();82} catch (AWTException e) {83throw new RuntimeException(e);84}8586Frame f = new Frame(SourceClippingBlitTest.class.getName());87final SourceClippingBlitTest test = new SourceClippingBlitTest();88f.add(test);89f.addWindowListener(new WindowAdapter() {90public void windowActivated(WindowEvent e) {91if (!done) {92test.runTests();93}94}9596});97f.pack();98f.setLocation(100, 100);99f.setVisible(true);100synchronized (lock) {101while (!done) {102try {103lock.wait();104} catch (InterruptedException ex) {105ex.printStackTrace();106}107}108}109if (!showErrors) {110f.dispose();111}112}113114public Dimension getPreferredSize() {115return new Dimension(TESTW, TESTH);116}117118public void paint(Graphics g) {119if (showErrors && done && grabbedBI != null) {120g.drawImage(grabbedBI, 0, 0, null);121}122}123124public void runTests() {125GraphicsConfiguration gc = getGraphicsConfiguration();126for (Image srcIm :127new Image[] {128getBufferedImage(gc, IMAGEW, IMAGEH,129BufferedImage.TYPE_INT_RGB, true),130getBufferedImage(gc, IMAGEW, IMAGEH,131BufferedImage.TYPE_INT_RGB, false),132// commented out due to 6593406133// getBMImage(gc, IMAGEW, IMAGEH),134// getBufferedImage(gc, IMAGEW, IMAGEH,135// BufferedImage.TYPE_INT_ARGB, true),136// getBufferedImage(gc, IMAGEW, IMAGEH,137// BufferedImage.TYPE_INT_ARGB, false),138getVImage(gc, IMAGEW, IMAGEH),139})140{141System.out.println("Testing source: " + srcIm);142// wiggle the source and dest rectangles143try {144for (int locationVar = -10; locationVar < 20; locationVar += 10)145{146for (int sizeVar = -10; sizeVar < 20; sizeVar += 10) {147Rectangle srcRect = (Rectangle)IMAGE_BOUNDS.clone();148srcRect.translate(locationVar, locationVar);149srcRect.grow(sizeVar, sizeVar);150151Rectangle dstRect =152new Rectangle(sizeVar, sizeVar,153srcRect.width, srcRect.height);154System.out.println("testing blit rect src: " + srcRect);155System.out.println(" dst: " + dstRect);156render(getGraphics(), srcIm, srcRect, dstRect);157test(srcRect, dstRect);158}159}160System.out.println("Test passed.");161} finally {162synchronized (lock) {163done = true;164lock.notifyAll();165}166}167}168}169170public void render(Graphics g, Image image,171Rectangle srcRect, Rectangle dstRect)172{173int w = getWidth();174int h = getHeight();175g.setColor(Color.green);176g.fillRect(0, 0, w, h);177178int bltWidth = srcRect.width;179int bltHeight = srcRect.height;180VolatileImage vi = null;181if (image instanceof VolatileImage) {182vi = (VolatileImage)image;183}184do {185if (vi != null) {186GraphicsConfiguration gc = getGraphicsConfiguration();187if (vi.validate(gc) != VolatileImage.IMAGE_OK) {188initImage(gc, vi);189}190}191g.drawImage(image,192dstRect.x, dstRect.y,193dstRect.x + bltWidth, dstRect.y + bltHeight,194srcRect.x, srcRect.y,195srcRect.x + bltWidth, srcRect.y + bltHeight,196Color.red,197null);198} while (vi != null && vi.contentsLost());199}200201202public void test(Rectangle srcRect, Rectangle dstRect) {203int w = getWidth();204int h = getHeight();205Toolkit.getDefaultToolkit().sync();206try {207Thread.sleep(2000);208} catch (InterruptedException ex) {}209Point p = getLocationOnScreen();210grabbedBI = robot.createScreenCapture(new Rectangle(p.x, p.y, w, h));211212// calculate the destination rectangle213Rectangle srcBounds = srcRect.intersection(IMAGE_BOUNDS);214int trX = dstRect.x - srcRect.x;215int trY = dstRect.y - srcRect.y;216Rectangle newDstRect = (Rectangle)dstRect.clone();217newDstRect.translate(-trX, -trY);218Rectangle.intersect(newDstRect, srcBounds, newDstRect);219newDstRect.translate(trX, trY);220Rectangle.intersect(newDstRect, new Rectangle(0, 0, w, h), newDstRect);221222System.out.println("calculated dest rect:" + newDstRect);223224// we do implicit clipping of the destination surface225// by only checking pixels within its bounds226for (int y = 0; y < h; y++) {227for (int x = 0; x < w; x++) {228int rgb = 0;229if (newDstRect.contains(x, y)) {230rgb = Color.red.getRGB();231} else {232rgb = Color.green.getRGB();233}234if (grabbedBI.getRGB(x, y) != rgb) {235String msg1 = "Test failed at x="+x+" y="+y;236System.out.println(msg1);237System.out.println(" expected: "+Integer.toHexString(rgb)+238" got:"+Integer.toHexString(grabbedBI.getRGB(x, y)));239throw new RuntimeException(msg1);240}241}242}243System.out.println("subtest passed");244}245246static VolatileImage dstImage;247static void initImage(GraphicsConfiguration gc, Image image) {248Graphics g = image.getGraphics();249g.setColor(Color.RED);250int w = image.getWidth(null);251int h = image.getHeight(null);252g.fillRect(0, 0, w, h);253g.dispose();254255// need to 'accelerate' the image256if (dstImage == null) {257dstImage =258gc.createCompatibleVolatileImage(TESTW, TESTH,259Transparency.OPAQUE);260}261dstImage.validate(gc);262g = dstImage.getGraphics();263g.drawImage(image, 0, 0, null);264g.drawImage(image, 0, 0, null);265g.drawImage(image, 0, 0, null);266}267268static VolatileImage getVImage(GraphicsConfiguration gc,269int w, int h)270{271VolatileImage image =272gc.createCompatibleVolatileImage(w, h, Transparency.OPAQUE);273image.validate(gc);274initImage(gc, image);275return image;276}277278static Image getBMImage(GraphicsConfiguration gc,279int w, int h)280{281Image image =282gc.createCompatibleImage(w, h, Transparency.BITMASK);283initImage(gc, image);284return image;285}286287static Image getBufferedImage(GraphicsConfiguration gc,288int w, int h, int type, boolean acceleratable)289{290BufferedImage image = new BufferedImage(w, h, type);291if (!acceleratable) {292image.setAccelerationPriority(0.0f);293}294initImage(gc, image);295return image;296}297}298299300