summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorEirik Aavitsland <eirik.aavitsland@qt.io>2022-01-21 08:05:29 +0100
committerEirik Aavitsland <eirik.aavitsland@qt.io>2022-03-03 07:49:44 +0000
commit5db7260975e3978bd5d502550f0d1308a69fb5ea (patch)
tree756483a5e75d1987419c6b50e1df9ab8e2733844 /tests
parenta7ab6235f70187d345fbdc51cba353de997de44a (diff)
Fix painting clipping glitches with fractional scaling
In QPainter, clipping can only be done on whole pixels. The various ways of specifying a clipping rectangle to the QPainter API have been inconsistent in how fractional rectangles (either specified directly, or as a result of fractional scaling) are mapped (rounded) to integer coordinates. Also, the mappings have not made sure to keep the edge-to-edge property of clip rects under scaling. This is particularly important when scaling QRegions with multiple rects, as QRegion is designed on the assumption that an area can be described as a set of edge-to-edge rects. The fix rounds a clip rect identically with a fill rect. (Indeed, a followup plan would be to merge QRasterPaintEngine's toNormalizedFillRect() with the rectangle rounding function in this commit). Notably, a QRectF clip is now interpreted the same as a QPainterPath clip describing the same area. This modifies d9cc1499954829faf9486fb72056e29f1bad58e3 Task-number: QTBUG-100329 Fixes: QTBUG-95957 Task-number: QTBUG-100343 Change-Id: Iaae6464b9b17f8bf3adc69007f6ef8d623bf2c80 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> (cherry picked from commit 423b6509382c3bcc36eca78ced0254bfb463d017) Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
Diffstat (limited to 'tests')
-rw-r--r--tests/auto/gui/painting/qpainter/tst_qpainter.cpp104
-rw-r--r--tests/auto/gui/qopengl/tst_qopengl.cpp9
2 files changed, 105 insertions, 8 deletions
diff --git a/tests/auto/gui/painting/qpainter/tst_qpainter.cpp b/tests/auto/gui/painting/qpainter/tst_qpainter.cpp
index 45490ef815..03d97470c1 100644
--- a/tests/auto/gui/painting/qpainter/tst_qpainter.cpp
+++ b/tests/auto/gui/painting/qpainter/tst_qpainter.cpp
@@ -69,6 +69,9 @@ Q_OBJECT
public:
tst_QPainter();
+ enum ClipType { ClipRect, ClipRectF, ClipRegionSingle, ClipRegionMulti, ClipPathR, ClipPath };
+ Q_ENUM(ClipType);
+
private slots:
void cleanupTestCase();
void getSetCheck();
@@ -155,6 +158,8 @@ private slots:
void clipBoundingRect();
void transformedClip();
+ void scaledClipConsistency_data();
+ void scaledClipConsistency();
void setOpacity_data();
void setOpacity();
@@ -1749,10 +1754,11 @@ void tst_QPainter::setClipRect()
/*
Verify that the clipping works correctly.
- The red outline should be covered by the blue rect on top and left,
- while it should be clipped on the right and bottom and thus the red outline be visible
+ Just like fillRect, cliprect should snap rightwards and downwards in case of .5 coordinates.
+ The red outline should be covered by the blue rect on top,
+ while it should be clipped on the other edges and thus the red outline be visible
- See: QTBUG-83229
+ See: QTBUG-83229, modified by QTBUG-100329
*/
void tst_QPainter::clipRect()
{
@@ -1778,7 +1784,7 @@ void tst_QPainter::clipRect()
p.end();
QCOMPARE(image.pixelColor(clipRect.left() + 1, clipRect.top()), QColor(Qt::blue));
- QCOMPARE(image.pixelColor(clipRect.left(), clipRect.top() + 1), QColor(Qt::blue));
+ QCOMPARE(image.pixelColor(clipRect.left(), clipRect.top() + 1), QColor(Qt::red));
QCOMPARE(image.pixelColor(clipRect.left() + 1, clipRect.bottom()), QColor(Qt::red));
QCOMPARE(image.pixelColor(clipRect.right(), clipRect.top() + 1), QColor(Qt::red));
}
@@ -4580,6 +4586,96 @@ void tst_QPainter::transformedClip()
}
}
+void tst_QPainter::scaledClipConsistency_data()
+{
+ QTest::addColumn<ClipType>("clipType");
+
+ QTest::newRow("clipRect") << ClipRect;
+ QTest::newRow("clipRectF") << ClipRectF;
+ QTest::newRow("clipRegionSingle") << ClipRegionSingle;
+ QTest::newRow("clipRegionMulti") << ClipRegionMulti;
+ QTest::newRow("clipPathR") << ClipPathR;
+ QTest::newRow("clipPath") << ClipPath;
+}
+
+void tst_QPainter::scaledClipConsistency()
+{
+ QFETCH(ClipType, clipType);
+
+ const QList<QRect> clipRects = {
+ // Varying odd and even coordinates and width/height
+ QRect(1, 1, 7, 8),
+ QRect(8, 0, 8, 9),
+ QRect(0, 9, 8, 7),
+ QRect(8, 9, 8, 7),
+ };
+ // Assert that these are edge to edge:
+ QPointF center = QRectF(clipRects[0]).bottomRight();
+ Q_ASSERT(QRectF(clipRects[1]).bottomLeft() == center);
+ Q_ASSERT(QRectF(clipRects[2]).topRight() == center);
+ Q_ASSERT(QRectF(clipRects[3]).topLeft() == center);
+
+ QRegion multiRegion;
+ for (const QRect &clipRect : clipRects)
+ multiRegion += clipRect;
+
+ QColor fillColor(Qt::black);
+ fillColor.setAlphaF(0.5);
+
+ for (int i = 100; i <= 300; i++) {
+ qreal dpr = qreal(i) / 100.0;
+ QImage img(QSize(16, 16) * dpr, QImage::Format_RGB32);
+ img.fill(Qt::white);
+ img.setDevicePixelRatio(dpr);
+
+ for (const QRect &clipRect : clipRects) {
+ QPainter p(&img);
+ switch (clipType) {
+ case ClipRect:
+ p.setClipRect(clipRect);
+ break;
+ case ClipRectF:
+ p.setClipRect(QRectF(clipRect));
+ break;
+ case ClipRegionSingle:
+ p.setClipRegion(QRegion(clipRect));
+ break;
+ case ClipRegionMulti:
+ p.setClipRegion(multiRegion);
+ break;
+ case ClipPath:
+ p.rotate(0.001); // Avoid the path being optimized to a rectf
+ Q_FALLTHROUGH();
+ case ClipPathR: {
+ QPainterPath path;
+ path.addRect(clipRect); // Will be recognized and converted back to a rectf
+ p.setClipPath(path);
+ break;
+ }
+ default:
+ Q_ASSERT(false);
+ break;
+ }
+ p.fillRect(p.window(), fillColor);
+ if (clipType == ClipRegionMulti)
+ break; // once is enough, we're not using the clipRect anyway
+ }
+
+ int qtWidth = img.width() / 4;
+ int qtHeight = img.height() / 4;
+ QPoint imgCenter = img.rect().center();
+ const QRgb targetColor = img.pixel(qtWidth, qtHeight);
+
+ // Test that there are no gaps or overlaps where the cliprects meet
+ for (int offset = -2; offset <= 2; offset++) {
+ QCOMPARE(img.pixel(imgCenter.x() + offset, qtHeight), targetColor);
+ QCOMPARE(img.pixel(imgCenter.x() + offset, img.height() - qtHeight), targetColor);
+ QCOMPARE(img.pixel(qtWidth, imgCenter.y() + offset), targetColor);
+ QCOMPARE(img.pixel(img.width() - qtWidth, imgCenter.y() + offset), targetColor);
+ }
+ }
+}
+
#if defined(Q_OS_MAC)
// Only Mac supports sub pixel positions in raster engine currently
void tst_QPainter::drawText_subPixelPositionsInRaster_qtbug5053()
diff --git a/tests/auto/gui/qopengl/tst_qopengl.cpp b/tests/auto/gui/qopengl/tst_qopengl.cpp
index e2e7f153a0..a0a1ecca17 100644
--- a/tests/auto/gui/qopengl/tst_qopengl.cpp
+++ b/tests/auto/gui/qopengl/tst_qopengl.cpp
@@ -1695,10 +1695,11 @@ void tst_QOpenGL::nullTextureInitializtion()
/*
Verify that the clipping works correctly.
- The red outline should be covered by the blue rect on top and left,
- while it should be clipped on the right and bottom and thus the red outline be visible
+ Just like fillRect, cliprect should snap rightwards and downwards in case of .5 coordinates.
+ The red outline should be covered by the blue rect on top,
+ while it should be clipped on the other edges and thus the red outline be visible
- See: QTBUG-83229
+ See: QTBUG-83229, modified by QTBUG-100329
*/
void tst_QOpenGL::clipRect()
{
@@ -1754,7 +1755,7 @@ void tst_QOpenGL::clipRect()
QCOMPARE(fb.size(), size);
QCOMPARE(fb.pixelColor(clipRect.left() + 1, clipRect.top()), QColor(Qt::blue));
- QCOMPARE(fb.pixelColor(clipRect.left(), clipRect.top() + 1), QColor(Qt::blue));
+ QCOMPARE(fb.pixelColor(clipRect.left(), clipRect.top() + 1), QColor(Qt::red));
QCOMPARE(fb.pixelColor(clipRect.left() + 1, clipRect.bottom()), QColor(Qt::red));
// Enable this once QTBUG-85286 is fixed