Path: blob/jdk8u272-b10-aarch32-20201026/jdk/test/sun/java2d/OpenGL/DrawHugeImageTest.java
48795 views
/*1* Copyright (c) 2014, 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*/22/*23* @test24* @bug 804061725* @summary Test verifies that an attempt to get an accelerated copy of26* a huge buffered image does not result in an OOME.27*28* @run main DrawHugeImageTest29*/3031import java.awt.Color;32import java.awt.Graphics2D;33import java.awt.GraphicsConfiguration;34import java.awt.GraphicsEnvironment;35import java.awt.image.BufferedImage;36import java.awt.image.VolatileImage;3738public class DrawHugeImageTest {39// we have to render the BI source several times in order40// to get an accelerated copy to be used.41static {42System.setProperty("sun.java2d.accthreshold", "1");43}44private static final int max_rendering_count = 5;4546private static final Color srcColor = Color.red;47private static final Color dstColor = Color.blue;4849public static void main(String[] args) {50BufferedImage src = createSrc();5152VolatileImage dst = createDst();53System.out.println("Dst: " + dst);54boolean status;55int count = max_rendering_count;5657do {58System.out.println("render image: " + (max_rendering_count - count));59status = render(src, dst);6061} while (status && count-- > 0);6263if (!status || count > 0) {64throw new RuntimeException("Test failed: " + count);65}66}6768private static boolean render(BufferedImage src, VolatileImage dst) {69int cnt = 5;70do {71Graphics2D g = dst.createGraphics();72g.setColor(dstColor);73g.fillRect(0, 0, dst.getWidth(), dst.getHeight());74g.drawImage(src, 0, 0, null);75g.dispose();76} while (dst.contentsLost() && (--cnt > 0));7778if (cnt == 0) {79System.err.println("Test failed: unable to render to volatile destination");80return false;81}8283BufferedImage s = dst.getSnapshot();8485return s.getRGB(1,1) == srcColor.getRGB();86}8788private static BufferedImage createSrc() {89final int w = 20000;90final int h = 5;9192BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);93Graphics2D g = img.createGraphics();94g.setColor(srcColor);95g.fillRect(0, 0, w, h);96g.dispose();9798return img;99}100101private static VolatileImage createDst() {102GraphicsConfiguration gc =103GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();104105return gc.createCompatibleVolatileImage(200, 200);106}107}108109110