|
| 1 | +/* |
| 2 | + * Copyright (c) 2004, 2025, 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 | +/* |
| 25 | + * @test |
| 26 | + * @bug 6248561 6264014 |
| 27 | + * @key headful |
| 28 | + * @requires (os.family != "mac") |
| 29 | + * @summary Verifies that bitmask image copies work properly with the |
| 30 | + * OGL pipeline when a SrcOver composite with extra alpha is involved. |
| 31 | + * @run main/othervm -Dsun.java2d.opengl=True DrawBitmaskImage |
| 32 | + * @run main/othervm DrawBitmaskImage |
| 33 | + */ |
| 34 | + |
| 35 | +/* |
| 36 | + * @test |
| 37 | + * @bug 6248561 6264014 |
| 38 | + * @key headful |
| 39 | + * @requires (os.family == "mac") |
| 40 | + * @summary Verifies that bitmask image copies work properly with the |
| 41 | + * OGL pipeline when a SrcOver composite with extra alpha is involved. |
| 42 | + * @run main/othervm -Dsun.java2d.opengl=True DrawBitmaskImage |
| 43 | + * @run main/othervm DrawBitmaskImage |
| 44 | + */ |
| 45 | + |
| 46 | +import java.awt.AlphaComposite; |
| 47 | +import java.awt.Canvas; |
| 48 | +import java.awt.Color; |
| 49 | +import java.awt.Dimension; |
| 50 | +import java.awt.EventQueue; |
| 51 | +import java.awt.Frame; |
| 52 | +import java.awt.Graphics; |
| 53 | +import java.awt.Graphics2D; |
| 54 | +import java.awt.GraphicsConfiguration; |
| 55 | +import java.awt.Panel; |
| 56 | +import java.awt.Point; |
| 57 | +import java.awt.Rectangle; |
| 58 | +import java.awt.Transparency; |
| 59 | +import java.awt.Robot; |
| 60 | +import java.awt.image.BufferedImage; |
| 61 | +import java.awt.image.IndexColorModel; |
| 62 | +import java.io.File; |
| 63 | +import javax.imageio.ImageIO; |
| 64 | + |
| 65 | +public class DrawBitmaskImage extends Panel { |
| 66 | + |
| 67 | + static final int TESTW = 200, TESTH = 200; |
| 68 | + private static volatile DrawBitmaskImage test; |
| 69 | + private static volatile Frame frame; |
| 70 | + |
| 71 | + public void paint(Graphics g) { |
| 72 | + |
| 73 | + Graphics2D g2d = (Graphics2D)g; |
| 74 | + g2d.setColor(Color.black); |
| 75 | + g2d.fillRect(0, 0, getWidth(), getHeight()); |
| 76 | + g2d.setComposite(AlphaComposite.SrcOver.derive(0.50f)); |
| 77 | + |
| 78 | + BufferedImage img = getGraphicsConfiguration().createCompatibleImage(50, 50, |
| 79 | + Transparency.BITMASK); |
| 80 | + Graphics2D gimg = img.createGraphics(); |
| 81 | + gimg.setComposite(AlphaComposite.Src); |
| 82 | + gimg.setColor(new Color(0, 0, 0, 0)); |
| 83 | + gimg.fillRect(0, 0, 50, 50); |
| 84 | + gimg.setColor(Color.red); |
| 85 | + gimg.fillRect(10, 10, 30, 30); |
| 86 | + gimg.dispose(); |
| 87 | + |
| 88 | + |
| 89 | + g2d.drawImage(img, 10, 10, null); |
| 90 | + |
| 91 | + // draw a second time to ensure that the cached copy is used |
| 92 | + g2d.drawImage(img, 80, 10, null); |
| 93 | + } |
| 94 | + |
| 95 | + public Dimension getPreferredSize() { |
| 96 | + return new Dimension(TESTW, TESTH); |
| 97 | + } |
| 98 | + |
| 99 | + static void createUI() { |
| 100 | + test = new DrawBitmaskImage(); |
| 101 | + frame = new Frame("OpenGL DrawBitmaskImage Test"); |
| 102 | + Panel p = new Panel(); |
| 103 | + p.add(test); |
| 104 | + frame.add(p); |
| 105 | + frame.setSize(TESTW+100, TESTH+100); |
| 106 | + frame.setLocationRelativeTo(null); |
| 107 | + frame.setVisible(true); |
| 108 | + } |
| 109 | + |
| 110 | + public static void main(String[] args) throws Exception { |
| 111 | + Robot robot = new Robot(); |
| 112 | + |
| 113 | + EventQueue.invokeAndWait(DrawBitmaskImage::createUI); |
| 114 | + |
| 115 | + robot.waitForIdle(); |
| 116 | + robot.delay(2000); |
| 117 | + |
| 118 | + BufferedImage capture = null; |
| 119 | + try { |
| 120 | + GraphicsConfiguration gc = frame.getGraphicsConfiguration(); |
| 121 | + if (gc.getColorModel() instanceof IndexColorModel) { |
| 122 | + System.out.println("IndexColorModel detected: " + |
| 123 | + "test considered PASSED"); |
| 124 | + return; |
| 125 | + } |
| 126 | + Point pt1 = test.getLocationOnScreen(); |
| 127 | + Rectangle rect = new Rectangle(pt1.x, pt1.y, TESTW, TESTH); |
| 128 | + capture = robot.createScreenCapture(rect); |
| 129 | + } finally { |
| 130 | + if (frame != null) { |
| 131 | + EventQueue.invokeAndWait(frame::dispose); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + // Test background color |
| 136 | + int pixel = capture.getRGB(5, 10); |
| 137 | + if (pixel != 0xff000000) { |
| 138 | + saveImage(capture); |
| 139 | + throw new RuntimeException("Failed: Incorrect color for " + |
| 140 | + "background (actual=" + |
| 141 | + Integer.toHexString(pixel) + ")"); |
| 142 | + } |
| 143 | + |
| 144 | + // Test pixels (allow for small error in the actual red value) |
| 145 | + pixel = capture.getRGB(25, 25); |
| 146 | + System.out.println("pixel1 is " + Integer.toHexString(pixel)); |
| 147 | + |
| 148 | + if ((pixel < 0xff7e0000) || (pixel > 0xff900000)) { |
| 149 | + saveImage(capture); |
| 150 | + throw new RuntimeException("Failed: Incorrect color for " + |
| 151 | + "first pixel (actual=" + |
| 152 | + Integer.toHexString(pixel) + ")"); |
| 153 | + } |
| 154 | + |
| 155 | + pixel = capture.getRGB(95, 25); |
| 156 | + System.out.println("pixel2 is " + Integer.toHexString(pixel)); |
| 157 | + if ((pixel < 0xff7e0000) || (pixel > 0xff900000)) { |
| 158 | + saveImage(capture); |
| 159 | + throw new RuntimeException("Failed: Incorrect color for " + |
| 160 | + "second pixel (actual=" + |
| 161 | + Integer.toHexString(pixel) + ")"); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + static void saveImage(BufferedImage img) { |
| 166 | + try { |
| 167 | + File file = new File("capture.png"); |
| 168 | + ImageIO.write(img, "png", file); |
| 169 | + } catch (Exception e) { |
| 170 | + e.printStackTrace(); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments