1 /*
   2  * Copyright (c) 2014, 2016, 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  * @test
  25  * @key headful
  26  * @bug     8040617
  27  * @summary Test verifies that an attempt to get an accelerated copy of
  28  *          a huge buffered image does not result in an OOME.
  29  *
  30  * @run     main DrawHugeImageTest
  31  */
  32 
  33 import java.awt.Color;
  34 import java.awt.Graphics2D;
  35 import java.awt.GraphicsConfiguration;
  36 import java.awt.GraphicsEnvironment;
  37 import java.awt.image.BufferedImage;
  38 import java.awt.image.VolatileImage;
  39 
  40 public class DrawHugeImageTest {
  41     // we have to render the BI source several times in order
  42     // to get an accelerated copy to be used.
  43     static {
  44         System.setProperty("sun.java2d.accthreshold", "1");
  45     }
  46     private static final int max_rendering_count = 5;
  47 
  48     private static final Color srcColor = Color.red;
  49     private static final Color dstColor = Color.blue;
  50 
  51     public static void main(String[] args) {
  52         BufferedImage src = createSrc();
  53 
  54         VolatileImage dst = createDst();
  55         System.out.println("Dst: " + dst);
  56         boolean status;
  57         int count = max_rendering_count;
  58 
  59         do {
  60             System.out.println("render image: " + (max_rendering_count - count));
  61             status = render(src, dst);
  62 
  63         } while (status && count-- > 0);
  64 
  65         if (!status || count > 0) {
  66             throw new RuntimeException("Test failed: " + count);
  67         }
  68     }
  69 
  70     private static boolean render(BufferedImage src, VolatileImage dst) {
  71         int cnt = 5;
  72         do {
  73             Graphics2D g = dst.createGraphics();
  74             g.setColor(dstColor);
  75             g.fillRect(0, 0, dst.getWidth(), dst.getHeight());
  76             g.drawImage(src, 0, 0, null);
  77             g.dispose();
  78         } while (dst.contentsLost() && (--cnt > 0));
  79 
  80         if (cnt == 0) {
  81             System.err.println("Test failed: unable to render to volatile destination");
  82             return false;
  83         }
  84 
  85         BufferedImage s = dst.getSnapshot();
  86 
  87         return s.getRGB(1,1) == srcColor.getRGB();
  88     }
  89 
  90     private static BufferedImage createSrc() {
  91         final int w = 20000;
  92         final int h = 5;
  93 
  94         BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
  95         Graphics2D g = img.createGraphics();
  96         g.setColor(srcColor);
  97         g.fillRect(0, 0, w, h);
  98         g.dispose();
  99 
 100         return img;
 101     }
 102 
 103     private static VolatileImage createDst() {
 104         GraphicsConfiguration gc =
 105                 GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
 106 
 107         return gc.createCompatibleVolatileImage(200, 200);
 108     }
 109 }