1/*
2 * Copyright (c) 2015, 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.Color;
26import java.awt.Graphics2D;
27import java.awt.GraphicsConfiguration;
28import java.awt.GraphicsEnvironment;
29import java.awt.Image;
30import java.awt.image.BufferedImage;
31import java.awt.image.VolatileImage;
32
33import static java.awt.Transparency.BITMASK;
34
35/**
36 * @test
37 * @key headful
38 * @bug 7188942
39 * @summary We should get correct volatile image, when we use BITMASK
40 *          transparency
41 */
42public final class BitmaskVolatileImage {
43
44    public static final int S = 8;
45
46    public static void main(final String[] args) {
47        GraphicsConfiguration gc =
48                GraphicsEnvironment.getLocalGraphicsEnvironment()
49                        .getDefaultScreenDevice().getDefaultConfiguration();
50        VolatileImage vi = gc.createCompatibleVolatileImage(S, S, BITMASK);
51        BufferedImage ci = gc.createCompatibleImage(S, S, BITMASK);
52
53        int attempt = 0;
54        do {
55            if (++attempt > 10) {
56                throw new RuntimeException("Too many attempts: " + attempt);
57            }
58            vi.validate(gc);
59            test(vi, ci, gc);
60        } while (vi.contentsLost());
61    }
62
63    private static void test(VolatileImage vi, BufferedImage ci, GraphicsConfiguration gc) {
64        for (int r = 0; r <= 255; ++r) {
65            for (int a = 0; a <= 255; ++a) {
66                fill(vi, new Color(r, 0, 0, a));
67                fill(ci, new Color(r, 0, 0, a));
68                validate(ci, vi.getSnapshot());
69            }
70        }
71    }
72
73    private static void fill(Image image, Color color) {
74        Graphics2D g2d = (Graphics2D) image.getGraphics();
75        g2d.setColor(color);
76        g2d.setComposite(AlphaComposite.Src);
77        g2d.fillRect(0, 0, S, S);
78        g2d.dispose();
79    }
80
81    private static void validate(BufferedImage ci, BufferedImage snapshot) {
82        for (int y = 0; y < ci.getHeight(); y++) {
83            for (int x = 0; x < ci.getWidth(); x++) {
84                int ci_rgb = ci.getRGB(x, y);
85                int vi_rgb = snapshot.getRGB(x, y);
86                if (ci_rgb != vi_rgb) {
87                    System.err.println("Exp:" + Integer.toHexString(ci_rgb));
88                    System.err.println("Actual:" + Integer.toHexString(vi_rgb));
89                    throw new RuntimeException("Colors mismatch!");
90                }
91            }
92        }
93    }
94}
95