From a87dbf0d08d58b829ca2fa036d589ad4fd48949f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20R=C3=B8dal?= Date: Tue, 26 Oct 2010 15:46:11 +0200 Subject: Fixed race condition in raster paint engine. We need to protect the gradient cache accesses with a mutex. Task-number: QTBUG-14614 Reviewed-by: Bradley T. Hughes --- src/gui/painting/qpaintengine_raster.cpp | 2 ++ tests/auto/qpainter/tst_qpainter.cpp | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/gui/painting/qpaintengine_raster.cpp b/src/gui/painting/qpaintengine_raster.cpp index 975ebb0..c03f82b 100644 --- a/src/gui/painting/qpaintengine_raster.cpp +++ b/src/gui/painting/qpaintengine_raster.cpp @@ -4811,6 +4811,7 @@ public: for (int i = 0; i < stops.size() && i <= 2; i++) hash_val += stops[i].second.rgba(); + QMutexLocker lock(&mutex); QGradientColorTableHash::const_iterator it = cache.constFind(hash_val); if (it == cache.constEnd()) @@ -4844,6 +4845,7 @@ protected: } QGradientColorTableHash cache; + QMutex mutex; }; void QGradientCache::generateGradientColorTable(const QGradient& gradient, uint *colorTable, int size, int opacity) const diff --git a/tests/auto/qpainter/tst_qpainter.cpp b/tests/auto/qpainter/tst_qpainter.cpp index beb83a1..4146c5e 100644 --- a/tests/auto/qpainter/tst_qpainter.cpp +++ b/tests/auto/qpainter/tst_qpainter.cpp @@ -251,6 +251,8 @@ private slots: void QTBUG5939_attachPainterPrivate(); + void QTBUG14614_gradientCacheRaceCondition(); + private: void fillData(); void setPenColor(QPainter& p); @@ -4458,6 +4460,36 @@ void tst_QPainter::QTBUG5939_attachPainterPrivate() QCOMPARE(widget->deviceTransform, proxy->deviceTransform); } +class GradientProducer : public QThread +{ +protected: + void run(); +}; + +void GradientProducer::run() +{ + QImage image(1, 1, QImage::Format_RGB32); + QPainter p(&image); + + for (int i = 0; i < 1000; ++i) { + QLinearGradient g; + g.setColorAt(0, QColor(i % 256, 0, 0)); + g.setColorAt(1, Qt::white); + + p.fillRect(image.rect(), g); + } +} + +void tst_QPainter::QTBUG14614_gradientCacheRaceCondition() +{ + const int threadCount = 16; + GradientProducer producers[threadCount]; + for (int i = 0; i < threadCount; ++i) + producers[i].start(); + for (int i = 0; i < threadCount; ++i) + producers[i].wait(); +} + QTEST_MAIN(tst_QPainter) #include "tst_qpainter.moc" -- cgit v0.12 From 4d0571667463997f5f1145408a2ba73ba293637a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20R=C3=B8dal?= Date: Wed, 27 Oct 2010 12:57:42 +0200 Subject: Fix Windows compilation. Don't take for granted that these are included... Task-number: QTBUG-14614 Reviewed-by: Bradley T. Hughes --- src/gui/painting/qpaintengine_raster.cpp | 1 + tests/auto/qpainter/tst_qpainter.cpp | 1 + 2 files changed, 2 insertions(+) diff --git a/src/gui/painting/qpaintengine_raster.cpp b/src/gui/painting/qpaintengine_raster.cpp index c03f82b..d1c4cab 100644 --- a/src/gui/painting/qpaintengine_raster.cpp +++ b/src/gui/painting/qpaintengine_raster.cpp @@ -40,6 +40,7 @@ ****************************************************************************/ #include +#include #define QT_FT_BEGIN_HEADER #define QT_FT_END_HEADER diff --git a/tests/auto/qpainter/tst_qpainter.cpp b/tests/auto/qpainter/tst_qpainter.cpp index 4146c5e..0a4de99 100644 --- a/tests/auto/qpainter/tst_qpainter.cpp +++ b/tests/auto/qpainter/tst_qpainter.cpp @@ -49,6 +49,7 @@ #include #include #include +#include #include #if !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN) #include -- cgit v0.12