1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
|
/*
* Copyright (c) 2002, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @key headful
* @bug 4780022 4862193 7179526
* @summary Tests that clipped lines are drawn over the same pixels
* as unclipped lines (within the clip bounds)
* @run main/timeout=600/othervm -Dsun.java2d.ddforcevram=true LineClipTest
* @run main/timeout=600/othervm LineClipTest
*/
/**
* This app tests whether we are drawing clipped lines the same
* as unclipped lines. The problem occurred when we started
* clipping d3d lines using simple integer clipping, which did not
* account for sub-pixel precision and ended up drawing very different
* pixels than the same line drawn unclipped. A supposed fix
* to that problem used floating-point clipping instead, but there
* was some problem with very limited precision inside of d3d
* (presumably in hardware) that caused some variation in pixels.
* We decided that whatever the fix was, we needed a serious
* line check test to make sure that all kinds of different
* lines would be drawn exactly the same inside the clip area,
* regardless of whether clipping was enabled. This test should
* check all kinds of different cases, such as lines that fall
* completely outside, completely inside, start outside and
* end inside, etc., and lines should end and originate in
* all quadrants of the space divided up by the clip box.
*
* The test works as follows:
* We create nine quadrants using the spaces bisected by the
* edges of the clip bounds (note that only one of these
* quadrants is actually visible when clipping is enabled).
* We create several points in each of these quadrants
* (three in each of the invisible quadrants, nine in the
* center/visible quadrant). Our resulting grid looks like
* this:
*
* x x|x x x|x x
* | |
* | |
* | |
* | |
* | |
* x | | x
* -----------------------------------
* x |x x x| x
* | |
* | |
* x |x x x| x
* | |
* | |
* x |x x x| x
* -----------------------------------
* x | | x
* | |
* | |
* | |
* | |
* | |
* x x|x x x|x x
*
* The test then draws lines from every point to every other
* point. First, we draw unclipped lines in blue and
* then we draw clipped lines in red.
* At certain times (after every point during the default
* test, after every quadrant of lines if you run with the -quick
* option), we check for errors and draw the current image
* to the screen. Error checking consists of copying the
* VolatileImage to a BufferedImage (because we need access
* to the pixels directly) and checking every pixel in the
* image. The check is simple: everything outside the
* clip bounds should be blue (or the background color) and
* everything inside the clip bounds should be red (or the
* background color). So any blue pixel inside or red
* pixel outside means that there was a drawing error and
* the test fails.
* There are 4 modes that the test can run in (dynamic mode is
* exclusive to the other modes, but the other modes are combinable):
*
* (default): the clip is set
* to a default size (100x100) and the test is run.
*
* -quick: The error
* check is run only after every quadrant of lines is
* drawn. This speeds up the test considerably with
* some less accuracy in error checking (because pixels
* from some lines may overdrawn pixels from other lines
* before we have verified the correctness of those
* pixels).
*
* -dynamic: There is no error checking, but this version
* of the test automatically resizes the clip bounds and
* reruns the test over and over. Nothing besides the
* visual check verifies that the test is running correctly.
*
* -rect: Instead of drawing lines, the test draws rectangles
* to/from all points in all quadrants. This tests similar
* clipping functionality for drawRect().
*
* n (where "n" is a number): sets the clip size to the
* given value. Just like the default test except that
* the clip size is as specified.
*
* Note: this test must be run with the -Dsun.java2d.ddforcevram=true
* option to force the test image to stay in VRAM. We currently
* punt VRAM images to system memory when we detect lots of
* reads. Since we read the whole buffer on every error check
* to copy it to the BufferedImage), this causes us to punt the
* buffer. A system memory surface will have no d3d capabilities,
* thus we are not testing the d3d line quality when this happens.
* By using the ddforcevram flag, we make sure the buffer
* stays put in VRAM and d3d is used to draw the lines.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.image.*;
public class LineClipTest extends Component implements Runnable {
int clipBumpVal = 5;
static int clipSize = 100;
int clipX1;
int clipY1;
static final int NUM_QUADS = 9;
Point quadrants[][] = new Point[NUM_QUADS][];
static boolean dynamic = false;
BufferedImage imageChecker = null;
Color unclippedColor = Color.blue;
Color clippedColor = Color.red;
int testW = -1, testH = -1;
VolatileImage testImage = null;
static boolean keepRunning = false;
static boolean quickTest = false;
static boolean rectTest = false;
static boolean runTestDone = false;
static Frame f = null;
/**
* Check for errors in the grid. This error check consists of
* copying the buffer into a BufferedImage and reading all pixels
* in that image. No pixel outside the clip bounds should be
* of the color clippedColor and no pixel inside should be
* of the color unclippedColor. Any wrong color returns an error.
*/
boolean gridError(Graphics g) {
boolean error = false;
if (imageChecker == null || (imageChecker.getWidth() != testW) ||
(imageChecker.getHeight() != testH))
{
// Recreate BufferedImage as necessary
GraphicsConfiguration gc = getGraphicsConfiguration();
ColorModel cm = gc.getColorModel();
WritableRaster wr =
cm.createCompatibleWritableRaster(getWidth(), getHeight());
imageChecker =
new BufferedImage(cm, wr,
cm.isAlphaPremultiplied(), null);
}
// Copy buffer to BufferedImage
Graphics gChecker = imageChecker.getGraphics();
gChecker.drawImage(testImage, 0, 0, this);
// Set up pixel colors to check against
int clippedPixelColor = clippedColor.getRGB();
int unclippedPixelColor = unclippedColor.getRGB();
int wrongPixelColor = clippedPixelColor;
boolean insideClip = false;
for (int row = 0; row < getHeight(); ++row) {
for (int col = 0; col < getWidth(); ++col) {
if (row >= clipY1 && row < (clipY1 + clipSize) &&
col >= clipX1 && col < (clipX1 + clipSize))
{
// Inside clip bounds - should not see unclipped color
wrongPixelColor = unclippedPixelColor;
} else {
// Outside clip - should not see clipped color
wrongPixelColor = clippedPixelColor;
}
int pixel = imageChecker.getRGB(col, row);
if (pixel == wrongPixelColor) {
System.out.println("FAILED: pixel = " +
Integer.toHexString(pixel) +
" at (x, y) = " + col + ", " + row);
// Draw magenta rectangle around problem pixel in buffer
// for visual feedback to user
g.setColor(Color.magenta);
g.drawRect(col - 1, row - 1, 2, 2);
error = true;
}
}
}
return error;
}
/**
* Draw all test lines and check for errors (unless running
* with -dynamic option)
*/
void drawLineGrid(Graphics screenGraphics, Graphics g) {
// Fill buffer with background color
g.setColor(Color.white);
g.fillRect(0, 0, getWidth(), getHeight());
// Now, iterate through all quadrants
for (int srcQuad = 0; srcQuad < NUM_QUADS; ++srcQuad) {
// Draw lines to all other quadrants
for (int dstQuad = 0; dstQuad < NUM_QUADS; ++dstQuad) {
for (int srcPoint = 0;
srcPoint < quadrants[srcQuad].length;
++srcPoint)
{
// For every point in the source quadrant
int sx = quadrants[srcQuad][srcPoint].x;
int sy = quadrants[srcQuad][srcPoint].y;
for (int dstPoint = 0;
dstPoint < quadrants[dstQuad].length;
++dstPoint)
{
int dx = quadrants[dstQuad][dstPoint].x;
int dy = quadrants[dstQuad][dstPoint].y;
if (!rectTest) {
// Draw unclipped/clipped lines to every
// point in the dst quadrant
g.setColor(unclippedColor);
g.drawLine(sx, sy, dx, dy);
g.setClip(clipX1, clipY1, clipSize, clipSize);
g.setColor(clippedColor);
g.drawLine(sx,sy, dx, dy);
} else {
// Draw unclipped/clipped rectangles to every
// point in the dst quadrant
g.setColor(unclippedColor);
int w = dx - sx;
int h = dy - sy;
g.drawRect(sx, sy, w, h);
g.setClip(clipX1, clipY1, clipSize, clipSize);
g.setColor(clippedColor);
g.drawRect(sx, sy, w, h);
}
g.setClip(null);
}
if (!dynamic) {
// Draw screen update for visual feedback
screenGraphics.drawImage(testImage, 0, 0, this);
// On default test, check for errors after every
// src point
if (!quickTest && gridError(g)) {
throw new java.lang.RuntimeException("Failed");
}
}
}
}
if (!dynamic && quickTest && gridError(g)) {
// On quick test, check for errors only after every
// src quadrant
throw new java.lang.RuntimeException("Failed");
//return;
}
}
if (!dynamic) {
System.out.println("PASSED");
if (!keepRunning) {
f.dispose();
}
}
}
/**
* If we have not yet run the test, or if the window size has
* changed, or if we are running the test in -dynamic mode,
* run the test. Then draw the test buffer to the screen
*/
public void paint(Graphics g) {
if (dynamic || testImage == null ||
getWidth() != testW || getHeight() != testH)
{
runTest(g);
}
if (testImage != null) {
g.drawImage(testImage, 0, 0, this);
}
}
/*
* Create the quadrant of points and run the test to draw all the lines
*/
public void runTest(Graphics screenGraphics) {
if (getWidth() == 0 || getHeight() == 0) {
// May get here before window is really ready
return;
}
clipX1 = (getWidth() - clipSize) / 2;
clipY1 = (getHeight() - clipSize) / 2;
int clipX2 = clipX1 + clipSize;
int clipY2 = clipY1 + clipSize;
int centerX = getWidth()/2;
int centerY = getHeight()/2;
int leftX = 0;
int topY = 0;
int rightX = getWidth() - 1;
int bottomY = getHeight() - 1;
int quadIndex = 0;
// Offsets are used to force diagonal (versus hor/vert) lines
int xOffset = 0;
int yOffset = 0;
if (quadrants[0] == null) {
for (int i = 0; i < 9; ++i) {
int numPoints = (i == 4) ? 9 : 3;
quadrants[i] = new Point[numPoints];
}
}
// Upper-left
quadrants[quadIndex] = new Point[] {
new Point(leftX + xOffset, clipY1 - 1 - yOffset),
new Point(leftX + xOffset, topY + yOffset),
new Point(clipX1 - 1 - xOffset, topY + yOffset),
};
quadIndex++;
yOffset++;
// Upper-middle
quadrants[quadIndex] = new Point[] {
new Point(clipX1 + 1 + xOffset, topY + yOffset),
new Point(centerX + xOffset, topY + yOffset),
new Point(clipX2 - 1 - xOffset, topY + yOffset),
};
quadIndex++;
++yOffset;
// Upper-right
quadrants[quadIndex] = new Point[] {
new Point(clipX2 + 1 + xOffset, topY + yOffset),
new Point(rightX - xOffset, topY + yOffset),
new Point(rightX - xOffset, clipY1 - 1 - yOffset),
};
quadIndex++;
yOffset = 0;
++xOffset;
// Middle-left
quadrants[quadIndex] = new Point[] {
new Point(leftX + xOffset, clipY1 + 1 + yOffset),
new Point(leftX + xOffset, centerY + yOffset),
new Point(leftX + xOffset, clipY2 - 1 - yOffset),
};
quadIndex++;
++yOffset;
// Middle-middle
quadrants[quadIndex] = new Point[] {
new Point(clipX1 + 1 + xOffset, clipY1 + 1 + yOffset),
new Point(centerX + xOffset, clipY1 + 1 + yOffset),
new Point(clipX2 - 1 - xOffset, clipY1 + 1 + yOffset),
new Point(clipX1 + 1 + xOffset, centerY + yOffset),
new Point(centerX + xOffset, centerY + yOffset),
new Point(clipX2 - 1 - xOffset, centerY + yOffset),
new Point(clipX1 + 1 + xOffset, clipY2 - 1 - yOffset),
new Point(centerX + xOffset, clipY2 - 1 - yOffset),
new Point(clipX2 - 1 - xOffset, clipY2 - 1 - yOffset),
};
quadIndex++;
++yOffset;
// Middle-right
quadrants[quadIndex] = new Point[] {
new Point(rightX - xOffset, clipY1 + 1 + yOffset),
new Point(rightX - xOffset, centerY + yOffset),
new Point(rightX - xOffset, clipY2 - 1 - yOffset),
};
quadIndex++;
yOffset = 0;
++xOffset;
// Lower-left
quadrants[quadIndex] = new Point[] {
new Point(leftX + xOffset, clipY2 + 1 + yOffset),
new Point(leftX + xOffset, bottomY - yOffset),
new Point(clipX1 - 1 - xOffset, bottomY - yOffset),
};
quadIndex++;
++yOffset;
// Lower-middle
quadrants[quadIndex] = new Point[] {
new Point(clipX1 + 1 + xOffset, bottomY - yOffset),
new Point(centerX + xOffset, bottomY - yOffset),
new Point(clipX2 - 1 - xOffset, bottomY - yOffset),
};
quadIndex++;
++yOffset;
// Lower-right
quadrants[quadIndex] = new Point[] {
new Point(clipX2 + 1 + xOffset, bottomY - yOffset),
new Point(rightX - xOffset, bottomY - yOffset),
new Point(rightX - xOffset, clipY2 + 1 + yOffset),
};
if (testImage != null) {
testImage.flush();
}
testW = getWidth();
testH = getHeight();
testImage = createVolatileImage(testW, testH);
Graphics g = testImage.getGraphics();
do {
int valCode = testImage.validate(getGraphicsConfiguration());
if (valCode == VolatileImage.IMAGE_INCOMPATIBLE) {
testImage.flush();
testImage = createVolatileImage(testW, testH);
g = testImage.getGraphics();
}
drawLineGrid(screenGraphics, g);
} while (testImage.contentsLost());
if (dynamic) {
// Draw clip box if dynamic
g.setClip(null);
g.setColor(Color.black);
g.drawRect(clipX1, clipY1, clipSize, clipSize);
screenGraphics.drawImage(testImage, 0, 0, this);
}
runTestDone = true;
}
/**
* When running -dynamic, resize the clip bounds and run the test
* over and over
*/
public void run() {
while (true) {
clipSize += clipBumpVal;
if (clipSize > getWidth() || clipSize < 0) {
clipBumpVal = -clipBumpVal;
clipSize += clipBumpVal;
}
update(getGraphics());
try {
Thread.sleep(50);
} catch (Exception e) {}
}
}
public static void main(String args[]) {
for (int i = 0; i < args.length; ++i) {
if (args[i].equals("-dynamic")) {
dynamic = true;
} else if (args[i].equals("-rect")) {
rectTest = true;
} else if (args[i].equals("-quick")) {
quickTest = true;
} else if (args[i].equals("-keep")) {
keepRunning = true;
} else {
// could be clipSize
try {
clipSize = Integer.parseInt(args[i]);
} catch (Exception e) {}
}
}
f = new Frame();
f.setSize(500, 500);
LineClipTest test = new LineClipTest();
f.add(test);
if (dynamic) {
Thread t = new Thread(test);
t.start();
}
f.setVisible(true);
while (!runTestDone) {
// need to make sure jtreg doesn't exit before the
// test is done...
try {
Thread.sleep(50);
} catch (Exception e) {}
}
}
}
|