Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/java2d/OpenGL/DrawBufImgOp.java
47525 views
/*1* Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22/*23* @test24* @bug 651499025* @summary Verifies that calling26* Graphics2D.drawImage(BufferedImage, BufferedImageOp, x, y) to an27* OpenGL-accelerated destination produces the same results when performed28* in software via BufferedImageOp.filter().29* @run main/othervm -Dsun.java2d.opengl=True DrawBufImgOp -ignore30* @author campbelc31*/3233import java.awt.*;34import java.awt.image.*;35import java.io.File;36import javax.imageio.ImageIO;3738/**39* REMIND: This testcase was originally intended to automatically compare40* the results of the software BufferedImageOp implementations against41* the OGL-accelerated codepaths. However, there are just too many open42* bugs in the mediaLib-based codepaths (see below), which means that43* creating the reference image may cause crashes or exceptions,44* and even if we work around those cases using the "-ignore" flag,45* the visual results of the reference image are often buggy as well46* (so the comparison will fail even though the OGL results are correct).47* Therefore, for now we will run the testcase with the "-ignore" flag48* but without the "-compare" flag, so at least it will be checking for49* any exceptions/crashes in the OGL code. When we fix all of the50* outstanding bugs with the software codepaths, we can remove the51* "-ignore" flag and maybe even restore the "-compare" flag. In the52* meantime, it stil functions well as a manual testcase (with either53* the "-show" or "-dump" options).54*/55public class DrawBufImgOp extends Canvas {5657private static final int TESTW = 600;58private static final int TESTH = 500;59private static boolean done;6061/*62* If true, skips tests that are known to trigger bugs (which in63* turn may cause crashes, exceptions, or other artifacts).64*/65private static boolean ignore;6667// Test both pow2 and non-pow2 sized images68private static final int[] srcSizes = { 32, 17 };69private static final int[] srcTypes = {70BufferedImage.TYPE_INT_RGB,71BufferedImage.TYPE_INT_ARGB,72BufferedImage.TYPE_INT_ARGB_PRE,73BufferedImage.TYPE_INT_BGR,74BufferedImage.TYPE_3BYTE_BGR,75BufferedImage.TYPE_4BYTE_ABGR,76BufferedImage.TYPE_USHORT_565_RGB,77BufferedImage.TYPE_BYTE_GRAY,78BufferedImage.TYPE_USHORT_GRAY,79};8081private static final RescaleOp82rescale1band, rescale3band, rescale4band;83private static final LookupOp84lookup1bandbyte, lookup3bandbyte, lookup4bandbyte;85private static final LookupOp86lookup1bandshort, lookup3bandshort, lookup4bandshort;87private static final ConvolveOp88convolve3x3zero, convolve5x5zero, convolve7x7zero;89private static final ConvolveOp90convolve3x3noop, convolve5x5noop, convolve7x7noop;9192static {93rescale1band = new RescaleOp(0.5f, 10.0f, null);94rescale3band = new RescaleOp(95new float[] { 0.6f, 0.4f, 0.6f },96new float[] { 10.0f, -3.0f, 5.0f },97null);98rescale4band = new RescaleOp(99new float[] { 0.6f, 0.4f, 0.6f, 0.9f },100new float[] { -1.0f, 5.0f, 3.0f, 1.0f },101null);102103// REMIND: we should probably test non-zero offsets, but that104// would require massaging the source image data to avoid going105// outside the lookup table array bounds106int offset = 0;107{108byte invert[] = new byte[256];109byte halved[] = new byte[256];110for (int j = 0; j < 256 ; j++) {111invert[j] = (byte) (255-j);112halved[j] = (byte) (j / 2);113}114ByteLookupTable lut1 = new ByteLookupTable(offset, invert);115lookup1bandbyte = new LookupOp(lut1, null);116ByteLookupTable lut3 =117new ByteLookupTable(offset,118new byte[][] {invert, halved, invert});119lookup3bandbyte = new LookupOp(lut3, null);120ByteLookupTable lut4 =121new ByteLookupTable(offset,122new byte[][] {invert, halved, invert, halved});123lookup4bandbyte = new LookupOp(lut4, null);124}125126{127short invert[] = new short[256];128short halved[] = new short[256];129for (int j = 0; j < 256 ; j++) {130invert[j] = (short) ((255-j) * 255);131halved[j] = (short) ((j / 2) * 255);132}133ShortLookupTable lut1 = new ShortLookupTable(offset, invert);134lookup1bandshort = new LookupOp(lut1, null);135ShortLookupTable lut3 =136new ShortLookupTable(offset,137new short[][] {invert, halved, invert});138lookup3bandshort = new LookupOp(lut3, null);139ShortLookupTable lut4 =140new ShortLookupTable(offset,141new short[][] {invert, halved, invert, halved});142lookup4bandshort = new LookupOp(lut4, null);143}144145// 3x3 blur146float[] data3 = {1470.1f, 0.1f, 0.1f,1480.1f, 0.2f, 0.1f,1490.1f, 0.1f, 0.1f,150};151Kernel k3 = new Kernel(3, 3, data3);152153// 5x5 edge154float[] data5 = {155-1.0f, -1.0f, -1.0f, -1.0f, -1.0f,156-1.0f, -1.0f, -1.0f, -1.0f, -1.0f,157-1.0f, -1.0f, 24.0f, -1.0f, -1.0f,158-1.0f, -1.0f, -1.0f, -1.0f, -1.0f,159-1.0f, -1.0f, -1.0f, -1.0f, -1.0f,160};161Kernel k5 = new Kernel(5, 5, data5);162163// 7x7 blur164float[] data7 = {1650.02f, 0.02f, 0.02f, 0.02f, 0.02f, 0.02f, 0.02f,1660.02f, 0.02f, 0.02f, 0.02f, 0.02f, 0.02f, 0.02f,1670.02f, 0.02f, 0.02f, 0.02f, 0.02f, 0.02f, 0.02f,1680.02f, 0.02f, 0.02f, 0.02f, 0.02f, 0.02f, 0.02f,1690.02f, 0.02f, 0.02f, 0.02f, 0.02f, 0.02f, 0.02f,1700.02f, 0.02f, 0.02f, 0.02f, 0.02f, 0.02f, 0.02f,1710.02f, 0.02f, 0.02f, 0.02f, 0.02f, 0.02f, 0.02f,172};173Kernel k7 = new Kernel(7, 7, data7);174175convolve3x3zero = new ConvolveOp(k3, ConvolveOp.EDGE_ZERO_FILL, null);176convolve5x5zero = new ConvolveOp(k5, ConvolveOp.EDGE_ZERO_FILL, null);177convolve7x7zero = new ConvolveOp(k7, ConvolveOp.EDGE_ZERO_FILL, null);178179convolve3x3noop = new ConvolveOp(k3, ConvolveOp.EDGE_NO_OP, null);180convolve5x5noop = new ConvolveOp(k5, ConvolveOp.EDGE_NO_OP, null);181convolve7x7noop = new ConvolveOp(k7, ConvolveOp.EDGE_NO_OP, null);182}183184public void paint(Graphics g) {185synchronized (this) {186if (done) {187return;188}189}190191VolatileImage vimg = createVolatileImage(TESTW, TESTH);192vimg.validate(getGraphicsConfiguration());193194Graphics2D g2d = vimg.createGraphics();195renderTest(g2d);196g2d.dispose();197198g.drawImage(vimg, 0, 0, null);199200Toolkit.getDefaultToolkit().sync();201202synchronized (this) {203done = true;204notifyAll();205}206}207208/*209* foreach source image size (once with pow2, once with non-pow2)210*211* foreach BufferedImage type212*213* RescaleOp (1 band)214* RescaleOp (3 bands, if src has 3 bands)215* RescaleOp (4 bands, if src has 4 bands)216*217* foreach LookupTable type (once with ByteLUT, once with ShortLUT)218* LookupOp (1 band)219* LookupOp (3 bands, if src has 3 bands)220* LookupOp (4 bands, if src has 4 bands)221*222* foreach edge condition (once with ZERO_FILL, once with EDGE_NO_OP)223* ConvolveOp (3x3)224* ConvolveOp (5x5)225* ConvolveOp (7x7)226*/227private void renderTest(Graphics2D g2d) {228g2d.setColor(Color.white);229g2d.fillRect(0, 0, TESTW, TESTH);230231int yorig = 2;232int xinc = 34;233int yinc = srcSizes[0] + srcSizes[1] + 2 + 2;234235for (int srcType : srcTypes) {236int y = yorig;237238for (int srcSize : srcSizes) {239int x = 2;240System.out.printf("type=%d size=%d\n", srcType, srcSize);241242BufferedImage srcImg = makeSourceImage(srcSize, srcType);243ColorModel srcCM = srcImg.getColorModel();244245// RescaleOp246g2d.drawImage(srcImg, rescale1band, x, y);247x += xinc;248// REMIND: 3-band RescaleOp.filter() throws IAE for images249// that contain an alpha channel (bug to be filed)250if (srcCM.getNumColorComponents() == 3 &&251!(ignore && srcCM.hasAlpha()))252{253g2d.drawImage(srcImg, rescale3band, x, y);254}255x += xinc;256if (srcCM.getNumComponents() == 4) {257g2d.drawImage(srcImg, rescale4band, x, y);258}259x += xinc;260261// LookupOp262// REMIND: Our LUTs are only 256 elements long, so won't263// currently work with USHORT_GRAY data264if (srcType != BufferedImage.TYPE_USHORT_GRAY) {265g2d.drawImage(srcImg, lookup1bandbyte, x, y);266x += xinc;267if (srcCM.getNumColorComponents() == 3) {268g2d.drawImage(srcImg, lookup3bandbyte, x, y);269}270x += xinc;271if (srcCM.getNumComponents() == 4) {272g2d.drawImage(srcImg, lookup4bandbyte, x, y);273}274x += xinc;275276// REMIND: LookupOp.createCompatibleDestImage() throws277// IAE for 3BYTE_BGR/4BYTE_ABGR (bug to be filed)278if (!(ignore &&279(srcType == BufferedImage.TYPE_3BYTE_BGR ||280srcType == BufferedImage.TYPE_4BYTE_ABGR)))281{282g2d.drawImage(srcImg, lookup1bandshort, x, y);283x += xinc;284// REMIND: 3-band LookupOp.filter() throws IAE for285// images that contain an alpha channel286// (bug to be filed)287if (srcCM.getNumColorComponents() == 3 &&288!(ignore && srcCM.hasAlpha()))289{290g2d.drawImage(srcImg, lookup3bandshort, x, y);291}292x += xinc;293if (srcCM.getNumComponents() == 4) {294g2d.drawImage(srcImg, lookup4bandshort, x, y);295}296x += xinc;297} else {298x += 3*xinc;299}300} else {301x += 6*xinc;302}303304// ConvolveOp305// REMIND: ConvolveOp.filter() throws ImagingOpException306// for 3BYTE_BGR (see 4957775)307if (srcType != BufferedImage.TYPE_3BYTE_BGR) {308g2d.drawImage(srcImg, convolve3x3zero, x, y);309x += xinc;310g2d.drawImage(srcImg, convolve5x5zero, x, y);311x += xinc;312g2d.drawImage(srcImg, convolve7x7zero, x, y);313x += xinc;314315g2d.drawImage(srcImg, convolve3x3noop, x, y);316x += xinc;317g2d.drawImage(srcImg, convolve5x5noop, x, y);318x += xinc;319g2d.drawImage(srcImg, convolve7x7noop, x, y);320x += xinc;321} else {322x += 6*xinc;323}324325y += srcSize + 2;326}327328yorig += yinc;329}330}331332private BufferedImage makeSourceImage(int size, int type) {333int s2 = size/2;334BufferedImage img = new BufferedImage(size, size, type);335Graphics2D g2d = img.createGraphics();336g2d.setComposite(AlphaComposite.Src);337g2d.setColor(Color.orange);338g2d.fillRect(0, 0, size, size);339g2d.setColor(Color.red);340g2d.fillRect(0, 0, s2, s2);341g2d.setColor(Color.green);342g2d.fillRect(s2, 0, s2, s2);343g2d.setColor(Color.blue);344g2d.fillRect(0, s2, s2, s2);345g2d.setColor(new Color(255, 255, 0, 128));346g2d.fillRect(s2, s2, s2, s2);347g2d.setColor(Color.pink);348g2d.fillOval(s2-3, s2-3, 6, 6);349g2d.dispose();350return img;351}352353public BufferedImage makeReferenceImage() {354BufferedImage img = new BufferedImage(TESTW, TESTH,355BufferedImage.TYPE_INT_RGB);356Graphics2D g2d = img.createGraphics();357renderTest(g2d);358g2d.dispose();359return img;360}361362public Dimension getPreferredSize() {363return new Dimension(TESTW, TESTH);364}365366private static void compareImages(BufferedImage refImg,367BufferedImage testImg,368int tolerance)369{370int x1 = 0;371int y1 = 0;372int x2 = refImg.getWidth();373int y2 = refImg.getHeight();374375for (int y = y1; y < y2; y++) {376for (int x = x1; x < x2; x++) {377Color expected = new Color(refImg.getRGB(x, y));378Color actual = new Color(testImg.getRGB(x, y));379if (!isSameColor(expected, actual, tolerance)) {380throw new RuntimeException("Test failed at x="+x+" y="+y+381" (expected="+expected+382" actual="+actual+383")");384}385}386}387}388389private static boolean isSameColor(Color c1, Color c2, int e) {390int r1 = c1.getRed();391int g1 = c1.getGreen();392int b1 = c1.getBlue();393int r2 = c2.getRed();394int g2 = c2.getGreen();395int b2 = c2.getBlue();396int rmin = Math.max(r2-e, 0);397int gmin = Math.max(g2-e, 0);398int bmin = Math.max(b2-e, 0);399int rmax = Math.min(r2+e, 255);400int gmax = Math.min(g2+e, 255);401int bmax = Math.min(b2+e, 255);402if (r1 >= rmin && r1 <= rmax &&403g1 >= gmin && g1 <= gmax &&404b1 >= bmin && b1 <= bmax)405{406return true;407}408return false;409}410411public static void main(String[] args) throws Exception {412boolean show = false;413boolean dump = false;414boolean compare = false;415416for (String arg : args) {417if (arg.equals("-show")) {418show = true;419} else if (arg.equals("-dump")) {420dump = true;421} else if (arg.equals("-compare")) {422compare = true;423} else if (arg.equals("-ignore")) {424ignore = true;425}426}427428DrawBufImgOp test = new DrawBufImgOp();429Frame frame = new Frame();430frame.add(test);431frame.pack();432frame.setVisible(true);433434// Wait until the component's been painted435synchronized (test) {436while (!done) {437try {438test.wait();439} catch (InterruptedException e) {440throw new RuntimeException("Failed: Interrupted");441}442}443}444445GraphicsConfiguration gc = frame.getGraphicsConfiguration();446if (gc.getColorModel() instanceof IndexColorModel) {447System.out.println("IndexColorModel detected: " +448"test considered PASSED");449frame.dispose();450return;451}452453// Grab the screen region454BufferedImage capture = null;455try {456Robot robot = new Robot();457Point pt1 = test.getLocationOnScreen();458Rectangle rect = new Rectangle(pt1.x, pt1.y, TESTW, TESTH);459capture = robot.createScreenCapture(rect);460} catch (Exception e) {461throw new RuntimeException("Problems creating Robot");462} finally {463if (!show) {464frame.dispose();465}466}467468// Compare the images (allow for +/- 1 bit differences in color comps)469if (dump || compare) {470BufferedImage ref = test.makeReferenceImage();471if (dump) {472ImageIO.write(ref, "png",473new File("DrawBufImgOp.ref.png"));474ImageIO.write(capture, "png",475new File("DrawBufImgOp.cap.png"));476}477if (compare) {478test.compareImages(ref, capture, 1);479}480}481}482}483484485