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 import java.awt.AlphaComposite;
  25 import java.awt.Graphics2D;
  26 import java.awt.GraphicsConfiguration;
  27 import java.awt.GraphicsDevice;
  28 import java.awt.GraphicsEnvironment;
  29 import java.awt.Image;
  30 import java.awt.Polygon;
  31 import java.awt.geom.AffineTransform;
  32 import java.awt.image.BufferedImage;
  33 import java.awt.image.DataBuffer;
  34 import java.awt.image.DataBufferByte;
  35 import java.awt.image.DataBufferInt;
  36 import java.awt.image.DataBufferShort;
  37 import java.awt.image.VolatileImage;
  38 
  39 import static java.awt.Transparency.*;
  40 import static java.awt.image.BufferedImage.*;
  41 
  42 /*
  43  * @test
  44  * @bug 8029253
  45  * @summary Unmanaged images should be drawn fast.
  46  * @author Sergey Bylokhov
  47  */
  48 public final class UnmanagedDrawImagePerformance {
  49 
  50     private static final int[] TYPES = {TYPE_INT_RGB, TYPE_INT_ARGB,
  51                                         TYPE_INT_ARGB_PRE, TYPE_INT_BGR,
  52                                         TYPE_3BYTE_BGR, TYPE_4BYTE_ABGR,
  53                                         TYPE_4BYTE_ABGR_PRE,
  54                                         TYPE_USHORT_565_RGB,
  55                                         TYPE_USHORT_555_RGB, TYPE_BYTE_GRAY,
  56                                         TYPE_USHORT_GRAY, TYPE_BYTE_BINARY,
  57                                         TYPE_BYTE_INDEXED};
  58     private static final int[] TRANSPARENCIES = {OPAQUE, BITMASK, TRANSLUCENT};
  59     private static final int SIZE = 1000;
  60     private static final AffineTransform[] TRANSFORMS = {
  61             AffineTransform.getScaleInstance(.5, .5),
  62             AffineTransform.getScaleInstance(1, 1),
  63             AffineTransform.getScaleInstance(2, 2),
  64             AffineTransform.getShearInstance(7, 11)};
  65 
  66     public static void main(final String[] args) {
  67         for (final AffineTransform atfm : TRANSFORMS) {
  68             for (final int viType : TRANSPARENCIES) {
  69                 for (final int biType : TYPES) {
  70                     final BufferedImage bi = makeUnmanagedBI(biType);
  71                     final VolatileImage vi = makeVI(viType);
  72                     final long time = test(bi, vi, atfm) / 1000000000;
  73                     if (time > 1) {
  74                         throw new RuntimeException(String.format(
  75                                 "drawImage is slow: %d seconds", time));
  76                     }
  77                 }
  78             }
  79         }
  80     }
  81 
  82     private static long test(Image bi, Image vi, AffineTransform atfm) {
  83         final Polygon p = new Polygon();
  84         p.addPoint(0, 0);
  85         p.addPoint(SIZE, 0);
  86         p.addPoint(0, SIZE);
  87         p.addPoint(SIZE, SIZE);
  88         p.addPoint(0, 0);
  89         Graphics2D g2d = (Graphics2D) vi.getGraphics();
  90         g2d.clip(p);
  91         g2d.transform(atfm);
  92         g2d.setComposite(AlphaComposite.SrcOver);
  93         final long start = System.nanoTime();
  94         g2d.drawImage(bi, 0, 0, null);
  95         final long time = System.nanoTime() - start;
  96         g2d.dispose();
  97         return time;
  98     }
  99 
 100     private static VolatileImage makeVI(final int type) {
 101         final GraphicsEnvironment ge = GraphicsEnvironment
 102                 .getLocalGraphicsEnvironment();
 103         final GraphicsDevice gd = ge.getDefaultScreenDevice();
 104         final GraphicsConfiguration gc = gd.getDefaultConfiguration();
 105         return gc.createCompatibleVolatileImage(SIZE, SIZE, type);
 106     }
 107 
 108     private static BufferedImage makeUnmanagedBI(final int type) {
 109         final BufferedImage img = new BufferedImage(SIZE, SIZE, type);
 110         final DataBuffer db = img.getRaster().getDataBuffer();
 111         if (db instanceof DataBufferInt) {
 112             ((DataBufferInt) db).getData();
 113         } else if (db instanceof DataBufferShort) {
 114             ((DataBufferShort) db).getData();
 115         } else if (db instanceof DataBufferByte) {
 116             ((DataBufferByte) db).getData();
 117         } else {
 118             try {
 119                 img.setAccelerationPriority(0.0f);
 120             } catch (final Throwable ignored) {
 121             }
 122         }
 123         return img;
 124     }
 125 }