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