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