|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, 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.Color; |
| 26 | +import java.awt.Graphics2D; |
| 27 | +import java.awt.GraphicsEnvironment; |
| 28 | +import java.awt.Image; |
| 29 | +import java.awt.image.BufferedImage; |
| 30 | +import java.io.File; |
| 31 | +import java.io.IOException; |
| 32 | + |
| 33 | +import javax.imageio.ImageIO; |
| 34 | + |
| 35 | +import static java.awt.Transparency.TRANSLUCENT; |
| 36 | + |
| 37 | +/** |
| 38 | + * @test |
| 39 | + * @bug 8255722 |
| 40 | + * @key headful |
| 41 | + */ |
| 42 | +public class BlitRotateClippedArea { |
| 43 | + |
| 44 | + /** |
| 45 | + * The test use case: |
| 46 | + * 1. The destination image is created of size 1000x1000 |
| 47 | + * 2. The source image is created of size 2000x2000 |
| 48 | + * 3. The source image is painted by the pattern outsize of 1000x1000 |
| 49 | + * 4. If the source image is painted as-is to the destination then the |
| 50 | + * pattern in the source will be ignored, but the test sets some |
| 51 | + * specific rotation that the pattern will hit the source. |
| 52 | + * Note that rotation is used not a scale/translate. |
| 53 | + */ |
| 54 | + public static void main(String[] args) throws Exception { |
| 55 | + // the test check the exact pixels location |
| 56 | + System.setProperty("sun.java2d.uiScale", "1"); |
| 57 | + var ge = GraphicsEnvironment.getLocalGraphicsEnvironment(); |
| 58 | + var gc = ge.getDefaultScreenDevice().getDefaultConfiguration(); |
| 59 | + |
| 60 | + var gold = gc.createCompatibleImage(1000, 1000, TRANSLUCENT); |
| 61 | + var dstVI2BI = gc.createCompatibleImage(1000, 1000, TRANSLUCENT); |
| 62 | + var dstVI2VI = gc.createCompatibleVolatileImage(1000, 1000, TRANSLUCENT); |
| 63 | + var dstBI2VI = gc.createCompatibleVolatileImage(1000, 1000, TRANSLUCENT); |
| 64 | + |
| 65 | + var srcBI = gc.createCompatibleImage(2000, 2000, TRANSLUCENT); |
| 66 | + var srcVI = gc.createCompatibleVolatileImage(2000, 2000, TRANSLUCENT); |
| 67 | + |
| 68 | + int attempt = 0; |
| 69 | + BufferedImage snapshotVI2VI; |
| 70 | + BufferedImage snapshotBI2VI; |
| 71 | + do { |
| 72 | + if (++attempt > 10) { |
| 73 | + throw new RuntimeException("Too many attempts: " + attempt); |
| 74 | + } |
| 75 | + dstVI2VI.validate(gc); |
| 76 | + dstBI2VI.validate(gc); |
| 77 | + srcVI.validate(gc); |
| 78 | + |
| 79 | + fill(srcBI); |
| 80 | + fill(srcVI); |
| 81 | + |
| 82 | + init(gold); |
| 83 | + init(dstVI2BI); |
| 84 | + init(dstVI2VI); |
| 85 | + init(dstBI2VI); |
| 86 | + |
| 87 | + draw(gold, srcBI); |
| 88 | + draw(dstVI2BI, srcVI); |
| 89 | + draw(dstVI2VI, srcVI); |
| 90 | + draw(dstBI2VI, srcBI); |
| 91 | + |
| 92 | + snapshotVI2VI = dstVI2VI.getSnapshot(); |
| 93 | + snapshotBI2VI = dstBI2VI.getSnapshot(); |
| 94 | + } while (dstVI2VI.contentsLost() || dstBI2VI.contentsLost() |
| 95 | + || srcVI.contentsLost()); |
| 96 | + |
| 97 | + validate(gold, snapshotVI2VI); |
| 98 | + validate(gold, snapshotBI2VI); |
| 99 | + validate(gold, dstVI2BI); |
| 100 | + } |
| 101 | + |
| 102 | + private static void validate(BufferedImage gold, BufferedImage img) |
| 103 | + throws IOException { |
| 104 | + for (int x = 0; x < gold.getWidth(); ++x) { |
| 105 | + for (int y = 0; y < gold.getHeight(); ++y) { |
| 106 | + if (gold.getRGB(x, y) != img.getRGB(x, y)) { |
| 107 | + ImageIO.write(gold, "png", new File("gold.png")); |
| 108 | + ImageIO.write(img, "png", new File("snapshot.png")); |
| 109 | + throw new RuntimeException("Test failed."); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private static void draw(Image dstBI, Image src) { |
| 116 | + Graphics2D g = (Graphics2D) dstBI.getGraphics(); |
| 117 | + g.rotate(Math.toRadians(180), 1250, 1150); |
| 118 | + g.drawImage(src, 0, 0, null); |
| 119 | + g.dispose(); |
| 120 | + } |
| 121 | + |
| 122 | + private static void init(Image image) { |
| 123 | + Graphics2D graphics = (Graphics2D) image.getGraphics(); |
| 124 | + graphics.setComposite(AlphaComposite.Src); |
| 125 | + graphics.setColor(Color.YELLOW); |
| 126 | + graphics.fillRect(0, 0, image.getWidth(null), image.getHeight(null)); |
| 127 | + graphics.dispose(); |
| 128 | + } |
| 129 | + |
| 130 | + private static void fill(Image image) { |
| 131 | + Graphics2D graphics = (Graphics2D) image.getGraphics(); |
| 132 | + graphics.setComposite(AlphaComposite.Src); |
| 133 | + for (int x = 1000; x < image.getWidth(null); ++x) { |
| 134 | + for (int y = 1000; y < image.getHeight(null); ++y) { |
| 135 | + graphics.setColor(new Color(x % 256, 0, y % 256, 125)); |
| 136 | + graphics.fillRect(x, y, 1, 1); |
| 137 | + } |
| 138 | + } |
| 139 | + graphics.dispose(); |
| 140 | + } |
| 141 | +} |
| 142 | + |
0 commit comments