Path: blob/master/test/jdk/sun/java2d/SunGraphics2D/SimplePrimQuality.java
41149 views
/*1* Copyright (c) 2005, 2018, 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*/2223/*24* @test25* @key headful26* @bug 4832224 6322584 6328478 6328481 6322580 6588884 6587863 819861327* @summary Verifies that the pixelization of simple primitives (drawLine,28* fillRect, drawRect, fill, draw) with the OGL pipeline enabled29* matches that produced by our software loops. (The primitives tested here30* are simple enough that the OGL results should match the software results31* exactly.) There is some overlap with PolyVertTest as we test both32* solid and XOR rendering here, but this testcase is a bit simpler and33* more appropriate for quick OGL testing. This test is also useful for34* comparing quality between our X11/GDI and software pipelines.35* @run main/othervm SimplePrimQuality36* @author campbelc37*/3839import java.awt.*;40import java.awt.geom.*;41import java.awt.image.*;42import java.io.File;43import java.io.IOException;44import javax.imageio.ImageIO;4546public class SimplePrimQuality extends Canvas {4748private static final int SIZE = 300;49private static boolean done;50private static boolean testVI;51private static volatile BufferedImage capture;52private static void doCapture(Component test) {53// Grab the screen region54try {55Robot robot = new Robot();56Point pt1 = test.getLocationOnScreen();57Rectangle rect =58new Rectangle(pt1.x, pt1.y, test.getWidth(), test.getHeight());59capture = robot.createScreenCapture(rect);60} catch (Exception e) {61e.printStackTrace();62}63}6465private static final int[][] rpts = {66{2, 0, 0, 0},67{12, 0, 1, 0},68{22, 0, 0, 1},69{32, 0, 1, 1},70{42, 0, 2, 1},71{52, 0, 1, 2},72{62, 0, 2, 2},73{72, 0, 5, 5},74{82, 0, 10, 10},75{97, 0, 15, 15},76};7778private void drawLine(Graphics2D g, int x, int y, int dx, int dy) {79g.drawLine(x, y, x + dx, y + dy);80}8182private void drawLines(Graphics2D g, int s) {83drawLine(g, 2, 0, 0, 0);84drawLine(g, 12, 0, 0, s);85drawLine(g, 22, 0, s, 0);86drawLine(g, 32, 0, s, s);87drawLine(g, 42, 0, 0, -s);88drawLine(g, 52, 0, -s, 0);89drawLine(g, 62, 0, -s, -s);90drawLine(g, 72, 0, -s, s);91drawLine(g, 82, 0, s, -s);92}9394private void fillRects(Graphics2D g) {95for (int i = 0; i < rpts.length; i++) {96g.fillRect(rpts[i][0], rpts[i][1], rpts[i][2], rpts[i][3]);97}98}99100private void drawRects(Graphics2D g) {101for (int i = 0; i < rpts.length; i++) {102g.drawRect(rpts[i][0], rpts[i][1], rpts[i][2], rpts[i][3]);103}104}105106private void fillOvals(Graphics2D g) {107for (int i = 0; i < rpts.length; i++) {108// use fill() instead of fillOval(), since the former is more109// likely to be consistent with our software loops when the110// OGL pipeline cannot be enabled111g.fill(new Ellipse2D.Float(rpts[i][0], rpts[i][1],112rpts[i][2], rpts[i][3]));113}114}115116private void drawOvals(Graphics2D g) {117for (int i = 0; i < rpts.length; i++) {118// use draw() instead of drawOval(), since the former is more119// likely to be consistent with our software loops when the120// OGL pipeline cannot be enabled121g.draw(new Ellipse2D.Float(rpts[i][0], rpts[i][1],122rpts[i][2], rpts[i][3]));123}124}125126private void renderShapes(Graphics2D g) {127// drawLine tests...128g.translate(0, 5);129drawLines(g, 1);130g.translate(0, 10);131drawLines(g, 4);132133// fillRect tests...134g.translate(0, 10);135fillRects(g);136137// drawRect tests...138g.translate(0, 20);139drawRects(g);140141// fillOval tests...142g.translate(0, 20);143fillOvals(g);144145// drawOval tests...146g.translate(0, 20);147drawOvals(g);148}149150private void renderTest(Graphics2D g, int w, int h) {151// on the left side, render the shapes in solid mode152g.setColor(Color.black);153g.fillRect(0, 0, w, h);154g.setColor(Color.green);155renderShapes(g);156157// on the right side, render the shapes in XOR mode158g.setTransform(AffineTransform.getTranslateInstance(SIZE/2, 0));159g.setXORMode(Color.black);160renderShapes(g);161g.setTransform(AffineTransform.getTranslateInstance(SIZE/2, 0));162renderShapes(g);163}164165public void paint(Graphics g) {166167Graphics2D g2d = (Graphics2D)g;168renderTest(g2d, SIZE, SIZE);169170Toolkit.getDefaultToolkit().sync();171172synchronized (this) {173if (!done) {174doCapture(this);175done = true;176}177notifyAll();178}179}180181public Dimension getPreferredSize() {182return new Dimension(SIZE, SIZE);183}184185public static void main(String[] args) {186boolean show = false;187for (String arg : args) {188if (arg.equals("-testvi")) {189System.out.println("Testing VolatileImage, not screen");190testVI = true;191} else if (arg.equals("-show")) {192show = true;193}194}195196SimplePrimQuality test = new SimplePrimQuality();197Frame frame = new Frame();198frame.add(test);199frame.pack();200frame.setVisible(true);201202// Wait until the component's been painted203synchronized (test) {204while (!done) {205try {206test.wait();207} catch (InterruptedException e) {208throw new RuntimeException("Failed: Interrupted");209}210}211}212213// REMIND: We will allow this test to pass silently on Windows214// (when OGL is not enabled) until we fix the GDI pipeline so that215// its stroked/filled GeneralPaths match our software loops (see216// 6322554). This check should be removed when 6322554 is fixed.217GraphicsConfiguration gc = frame.getGraphicsConfiguration();218if (gc.getClass().getSimpleName().startsWith("Win")) {219System.out.println("GDI pipeline detected: " +220"test considered PASSED");221frame.dispose();222return;223}224225226if (testVI) {227// render to a VI instead of the screen228VolatileImage vi = frame.createVolatileImage(SIZE, SIZE);229do {230vi.validate(frame.getGraphicsConfiguration());231Graphics2D g1 = vi.createGraphics();232test.renderTest(g1, SIZE, SIZE);233g1.dispose();234capture = vi.getSnapshot();235} while (vi.contentsLost());236frame.dispose();237}238239if (!show) {240frame.dispose();241}242if (capture == null) {243throw new RuntimeException("Error capturing the rendering");244}245246// Create reference image247int w = SIZE, h = SIZE;248BufferedImage refimg = new BufferedImage(w, h,249BufferedImage.TYPE_INT_RGB);250Graphics2D g = refimg.createGraphics();251test.renderTest(g, w, h);252g.dispose();253254// Test pixels255for (int y = 0; y < h; y++) {256for (int x = 0; x < w; x++) {257int actual = capture.getRGB(x, y);258int expected = refimg.getRGB(x, y);259if (actual != expected) {260String expectedName = "SimplePrimQuality_expected.png";261String actualName = "SimplePrimQuality_actual.png";262try {263System.out.println("Writing expected image to: "+264expectedName);265ImageIO.write(refimg, "png", new File(expectedName));266System.out.println("Writing actual image to: "+267actualName);268ImageIO.write(capture, "png", new File(actualName));269} catch (IOException ex) {}270throw new RuntimeException("Test failed at x="+x+" y="+y+271" (expected="+272Integer.toHexString(expected) +273" actual="+274Integer.toHexString(actual) +275")");276}277}278}279}280}281282283