Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/awt/Graphics/LineClipTest.java
38811 views
/*1* Copyright (c) 2002, 2013, 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* @bug 4780022 4862193 717952626* @summary Tests that clipped lines are drawn over the same pixels27* as unclipped lines (within the clip bounds)28* @run main/timeout=600/othervm -Dsun.java2d.ddforcevram=true LineClipTest29* @run main/timeout=600/othervm LineClipTest30*/313233/**34* This app tests whether we are drawing clipped lines the same35* as unclipped lines. The problem occurred when we started36* clipping d3d lines using simple integer clipping, which did not37* account for sub-pixel precision and ended up drawing very different38* pixels than the same line drawn unclipped. A supposed fix39* to that problem used floating-point clipping instead, but there40* was some problem with very limited precision inside of d3d41* (presumably in hardware) that caused some variation in pixels.42* We decided that whatever the fix was, we needed a serious43* line check test to make sure that all kinds of different44* lines would be drawn exactly the same inside the clip area,45* regardless of whether clipping was enabled. This test should46* check all kinds of different cases, such as lines that fall47* completely outside, completely inside, start outside and48* end inside, etc., and lines should end and originate in49* all quadrants of the space divided up by the clip box.50*51* The test works as follows:52* We create nine quadrants using the spaces bisected by the53* edges of the clip bounds (note that only one of these54* quadrants is actually visible when clipping is enabled).55* We create several points in each of these quadrants56* (three in each of the invisible quadrants, nine in the57* center/visible quadrant). Our resulting grid looks like58* this:59*60* x x|x x x|x x61* | |62* | |63* | |64* | |65* | |66* x | | x67* -----------------------------------68* x |x x x| x69* | |70* | |71* x |x x x| x72* | |73* | |74* x |x x x| x75* -----------------------------------76* x | | x77* | |78* | |79* | |80* | |81* | |82* x x|x x x|x x83*84* The test then draws lines from every point to every other85* point. First, we draw unclipped lines in blue and86* then we draw clipped lines in red.87* At certain times (after every point during the default88* test, after every quadrant of lines if you run with the -quick89* option), we check for errors and draw the current image90* to the screen. Error checking consists of copying the91* VolatileImage to a BufferedImage (because we need access92* to the pixels directly) and checking every pixel in the93* image. The check is simple: everything outside the94* clip bounds should be blue (or the background color) and95* everything inside the clip bounds should be red (or the96* background color). So any blue pixel inside or red97* pixel outside means that there was a drawing error and98* the test fails.99* There are 4 modes that the test can run in (dynamic mode is100* exclusive to the other modes, but the other modes are combinable):101*102* (default): the clip is set103* to a default size (100x100) and the test is run.104*105* -quick: The error106* check is run only after every quadrant of lines is107* drawn. This speeds up the test considerably with108* some less accuracy in error checking (because pixels109* from some lines may overdrawn pixels from other lines110* before we have verified the correctness of those111* pixels).112*113* -dynamic: There is no error checking, but this version114* of the test automatically resizes the clip bounds and115* reruns the test over and over. Nothing besides the116* visual check verifies that the test is running correctly.117*118* -rect: Instead of drawing lines, the test draws rectangles119* to/from all points in all quadrants. This tests similar120* clipping functionality for drawRect().121*122* n (where "n" is a number): sets the clip size to the123* given value. Just like the default test except that124* the clip size is as specified.125*126* Note: this test must be run with the -Dsun.java2d.ddforcevram=true127* option to force the test image to stay in VRAM. We currently128* punt VRAM images to system memory when we detect lots of129* reads. Since we read the whole buffer on every error check130* to copy it to the BufferedImage), this causes us to punt the131* buffer. A system memory surface will have no d3d capabilities,132* thus we are not testing the d3d line quality when this happens.133* By using the ddforcevram flag, we make sure the buffer134* stays put in VRAM and d3d is used to draw the lines.135*/136137import javax.swing.*;138import java.awt.*;139import java.awt.image.*;140141142public class LineClipTest extends Component implements Runnable {143144int clipBumpVal = 5;145static int clipSize = 100;146int clipX1;147int clipY1;148static final int NUM_QUADS = 9;149Point quadrants[][] = new Point[NUM_QUADS][];150static boolean dynamic = false;151BufferedImage imageChecker = null;152Color unclippedColor = Color.blue;153Color clippedColor = Color.red;154int testW = -1, testH = -1;155VolatileImage testImage = null;156static boolean keepRunning = false;157static boolean quickTest = false;158static boolean rectTest = false;159static boolean runTestDone = false;160static Frame f = null;161162/**163* Check for errors in the grid. This error check consists of164* copying the buffer into a BufferedImage and reading all pixels165* in that image. No pixel outside the clip bounds should be166* of the color clippedColor and no pixel inside should be167* of the color unclippedColor. Any wrong color returns an error.168*/169boolean gridError(Graphics g) {170boolean error = false;171if (imageChecker == null || (imageChecker.getWidth() != testW) ||172(imageChecker.getHeight() != testH))173{174// Recreate BufferedImage as necessary175GraphicsConfiguration gc = getGraphicsConfiguration();176ColorModel cm = gc.getColorModel();177WritableRaster wr =178cm.createCompatibleWritableRaster(getWidth(), getHeight());179imageChecker =180new BufferedImage(cm, wr,181cm.isAlphaPremultiplied(), null);182}183// Copy buffer to BufferedImage184Graphics gChecker = imageChecker.getGraphics();185gChecker.drawImage(testImage, 0, 0, this);186187// Set up pixel colors to check against188int clippedPixelColor = clippedColor.getRGB();189int unclippedPixelColor = unclippedColor.getRGB();190int wrongPixelColor = clippedPixelColor;191boolean insideClip = false;192for (int row = 0; row < getHeight(); ++row) {193for (int col = 0; col < getWidth(); ++col) {194if (row >= clipY1 && row < (clipY1 + clipSize) &&195col >= clipX1 && col < (clipX1 + clipSize))196{197// Inside clip bounds - should not see unclipped color198wrongPixelColor = unclippedPixelColor;199} else {200// Outside clip - should not see clipped color201wrongPixelColor = clippedPixelColor;202}203int pixel = imageChecker.getRGB(col, row);204if (pixel == wrongPixelColor) {205System.out.println("FAILED: pixel = " +206Integer.toHexString(pixel) +207" at (x, y) = " + col + ", " + row);208// Draw magenta rectangle around problem pixel in buffer209// for visual feedback to user210g.setColor(Color.magenta);211g.drawRect(col - 1, row - 1, 2, 2);212error = true;213}214}215}216return error;217}218219/**220* Draw all test lines and check for errors (unless running221* with -dynamic option)222*/223void drawLineGrid(Graphics screenGraphics, Graphics g) {224// Fill buffer with background color225g.setColor(Color.white);226g.fillRect(0, 0, getWidth(), getHeight());227228// Now, iterate through all quadrants229for (int srcQuad = 0; srcQuad < NUM_QUADS; ++srcQuad) {230// Draw lines to all other quadrants231for (int dstQuad = 0; dstQuad < NUM_QUADS; ++dstQuad) {232for (int srcPoint = 0;233srcPoint < quadrants[srcQuad].length;234++srcPoint)235{236// For every point in the source quadrant237int sx = quadrants[srcQuad][srcPoint].x;238int sy = quadrants[srcQuad][srcPoint].y;239for (int dstPoint = 0;240dstPoint < quadrants[dstQuad].length;241++dstPoint)242{243int dx = quadrants[dstQuad][dstPoint].x;244int dy = quadrants[dstQuad][dstPoint].y;245if (!rectTest) {246// Draw unclipped/clipped lines to every247// point in the dst quadrant248g.setColor(unclippedColor);249g.drawLine(sx, sy, dx, dy);250g.setClip(clipX1, clipY1, clipSize, clipSize);251g.setColor(clippedColor);252g.drawLine(sx,sy, dx, dy);253} else {254// Draw unclipped/clipped rectangles to every255// point in the dst quadrant256g.setColor(unclippedColor);257int w = dx - sx;258int h = dy - sy;259g.drawRect(sx, sy, w, h);260g.setClip(clipX1, clipY1, clipSize, clipSize);261g.setColor(clippedColor);262g.drawRect(sx, sy, w, h);263}264g.setClip(null);265}266if (!dynamic) {267// Draw screen update for visual feedback268screenGraphics.drawImage(testImage, 0, 0, this);269// On default test, check for errors after every270// src point271if (!quickTest && gridError(g)) {272throw new java.lang.RuntimeException("Failed");273}274}275}276}277if (!dynamic && quickTest && gridError(g)) {278// On quick test, check for errors only after every279// src quadrant280throw new java.lang.RuntimeException("Failed");281//return;282}283}284if (!dynamic) {285System.out.println("PASSED");286if (!keepRunning) {287f.dispose();288}289}290}291292/**293* If we have not yet run the test, or if the window size has294* changed, or if we are running the test in -dynamic mode,295* run the test. Then draw the test buffer to the screen296*/297public void paint(Graphics g) {298if (dynamic || testImage == null ||299getWidth() != testW || getHeight() != testH)300{301runTest(g);302}303if (testImage != null) {304g.drawImage(testImage, 0, 0, this);305}306}307308/*309* Create the quadrant of points and run the test to draw all the lines310*/311public void runTest(Graphics screenGraphics) {312if (getWidth() == 0 || getHeight() == 0) {313// May get here before window is really ready314return;315}316clipX1 = (getWidth() - clipSize) / 2;317clipY1 = (getHeight() - clipSize) / 2;318int clipX2 = clipX1 + clipSize;319int clipY2 = clipY1 + clipSize;320int centerX = getWidth()/2;321int centerY = getHeight()/2;322int leftX = 0;323int topY = 0;324int rightX = getWidth() - 1;325int bottomY = getHeight() - 1;326int quadIndex = 0;327// Offsets are used to force diagonal (versus hor/vert) lines328int xOffset = 0;329int yOffset = 0;330331if (quadrants[0] == null) {332for (int i = 0; i < 9; ++i) {333int numPoints = (i == 4) ? 9 : 3;334quadrants[i] = new Point[numPoints];335}336}337// Upper-left338quadrants[quadIndex] = new Point[] {339new Point(leftX + xOffset, clipY1 - 1 - yOffset),340new Point(leftX + xOffset, topY + yOffset),341new Point(clipX1 - 1 - xOffset, topY + yOffset),342};343344quadIndex++;345yOffset++;346// Upper-middle347quadrants[quadIndex] = new Point[] {348new Point(clipX1 + 1 + xOffset, topY + yOffset),349new Point(centerX + xOffset, topY + yOffset),350new Point(clipX2 - 1 - xOffset, topY + yOffset),351};352353quadIndex++;354++yOffset;355// Upper-right356quadrants[quadIndex] = new Point[] {357new Point(clipX2 + 1 + xOffset, topY + yOffset),358new Point(rightX - xOffset, topY + yOffset),359new Point(rightX - xOffset, clipY1 - 1 - yOffset),360};361362quadIndex++;363yOffset = 0;364++xOffset;365// Middle-left366quadrants[quadIndex] = new Point[] {367new Point(leftX + xOffset, clipY1 + 1 + yOffset),368new Point(leftX + xOffset, centerY + yOffset),369new Point(leftX + xOffset, clipY2 - 1 - yOffset),370};371372quadIndex++;373++yOffset;374// Middle-middle375quadrants[quadIndex] = new Point[] {376new Point(clipX1 + 1 + xOffset, clipY1 + 1 + yOffset),377new Point(centerX + xOffset, clipY1 + 1 + yOffset),378new Point(clipX2 - 1 - xOffset, clipY1 + 1 + yOffset),379new Point(clipX1 + 1 + xOffset, centerY + yOffset),380new Point(centerX + xOffset, centerY + yOffset),381new Point(clipX2 - 1 - xOffset, centerY + yOffset),382new Point(clipX1 + 1 + xOffset, clipY2 - 1 - yOffset),383new Point(centerX + xOffset, clipY2 - 1 - yOffset),384new Point(clipX2 - 1 - xOffset, clipY2 - 1 - yOffset),385};386387quadIndex++;388++yOffset;389// Middle-right390quadrants[quadIndex] = new Point[] {391new Point(rightX - xOffset, clipY1 + 1 + yOffset),392new Point(rightX - xOffset, centerY + yOffset),393new Point(rightX - xOffset, clipY2 - 1 - yOffset),394};395396quadIndex++;397yOffset = 0;398++xOffset;399// Lower-left400quadrants[quadIndex] = new Point[] {401new Point(leftX + xOffset, clipY2 + 1 + yOffset),402new Point(leftX + xOffset, bottomY - yOffset),403new Point(clipX1 - 1 - xOffset, bottomY - yOffset),404};405406quadIndex++;407++yOffset;408// Lower-middle409quadrants[quadIndex] = new Point[] {410new Point(clipX1 + 1 + xOffset, bottomY - yOffset),411new Point(centerX + xOffset, bottomY - yOffset),412new Point(clipX2 - 1 - xOffset, bottomY - yOffset),413};414415quadIndex++;416++yOffset;417// Lower-right418quadrants[quadIndex] = new Point[] {419new Point(clipX2 + 1 + xOffset, bottomY - yOffset),420new Point(rightX - xOffset, bottomY - yOffset),421new Point(rightX - xOffset, clipY2 + 1 + yOffset),422};423424425if (testImage != null) {426testImage.flush();427}428testW = getWidth();429testH = getHeight();430testImage = createVolatileImage(testW, testH);431Graphics g = testImage.getGraphics();432do {433int valCode = testImage.validate(getGraphicsConfiguration());434if (valCode == VolatileImage.IMAGE_INCOMPATIBLE) {435testImage.flush();436testImage = createVolatileImage(testW, testH);437g = testImage.getGraphics();438}439drawLineGrid(screenGraphics, g);440} while (testImage.contentsLost());441if (dynamic) {442// Draw clip box if dynamic443g.setClip(null);444g.setColor(Color.black);445g.drawRect(clipX1, clipY1, clipSize, clipSize);446screenGraphics.drawImage(testImage, 0, 0, this);447}448runTestDone = true;449}450451/**452* When running -dynamic, resize the clip bounds and run the test453* over and over454*/455public void run() {456while (true) {457clipSize += clipBumpVal;458if (clipSize > getWidth() || clipSize < 0) {459clipBumpVal = -clipBumpVal;460clipSize += clipBumpVal;461}462update(getGraphics());463try {464Thread.sleep(50);465} catch (Exception e) {}466}467}468469public static void main(String args[]) {470for (int i = 0; i < args.length; ++i) {471if (args[i].equals("-dynamic")) {472dynamic = true;473} else if (args[i].equals("-rect")) {474rectTest = true;475} else if (args[i].equals("-quick")) {476quickTest = true;477} else if (args[i].equals("-keep")) {478keepRunning = true;479} else {480// could be clipSize481try {482clipSize = Integer.parseInt(args[i]);483} catch (Exception e) {}484}485}486f = new Frame();487f.setSize(500, 500);488LineClipTest test = new LineClipTest();489f.add(test);490if (dynamic) {491Thread t = new Thread(test);492t.start();493}494f.setVisible(true);495while (!runTestDone) {496// need to make sure jtreg doesn't exit before the497// test is done...498try {499Thread.sleep(50);500} catch (Exception e) {}501}502}503}504505506