From 57469a8e10f11ee04964ed9f8f8d00de0d6d45c1 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Wed, 28 Jan 2015 15:52:30 +0100 Subject: Avoid creating a QPixmap on QBrush comparisons We shouldn't create QPixmaps when comparing QBrushes that do not contain a QPixmap. This patch extends the comparison logic to comparing QImage cachekeys if the brushes are QImage based. Note the comparison still produces false negatives on equal content on different pixmaps and images, but this is preserving existing behavior. Task-number: QTBUG-43766 Change-Id: I001b4032172c1e568aad311f7df2eaae6aee8dc6 Reviewed-by: Gunnar Sletta --- src/gui/painting/qbrush.cpp | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/gui/painting/qbrush.cpp b/src/gui/painting/qbrush.cpp index 40dad85cde..06b2ca2b36 100644 --- a/src/gui/painting/qbrush.cpp +++ b/src/gui/painting/qbrush.cpp @@ -35,6 +35,7 @@ #include "qpixmap.h" #include "qbitmap.h" #include "qpixmapcache.h" +#include "qplatformpixmap.h" #include "qdatastream.h" #include "qvariant.h" #include "qline.h" @@ -950,9 +951,34 @@ bool QBrush::operator==(const QBrush &b) const switch (d->style) { case Qt::TexturePattern: { - const QPixmap &us = (static_cast(d.data()))->pixmap(); - const QPixmap &them = (static_cast(b.d.data()))->pixmap(); - return ((us.isNull() && them.isNull()) || us.cacheKey() == them.cacheKey()); + // Note this produces false negatives if the textures have identical data, + // but does not share the same data in memory. Since equality is likely to + // be used to avoid iterating over the data for a texture update, this should + // still be better than doing an accurate comparison. + const QPixmap *us = 0, *them = 0; + qint64 cacheKey1, cacheKey2; + if (qHasPixmapTexture(*this)) { + us = (static_cast(d.data()))->m_pixmap; + cacheKey1 = us->cacheKey(); + } else + cacheKey1 = (static_cast(d.data()))->image().cacheKey(); + + if (qHasPixmapTexture(b)) { + them = (static_cast(b.d.data()))->m_pixmap; + cacheKey2 = them->cacheKey(); + } else + cacheKey2 = (static_cast(b.d.data()))->image().cacheKey(); + + if (cacheKey1 != cacheKey2) + return false; + if (!us == !them) // both images or both pixmaps + return true; + // Only raster QPixmaps use the same cachekeys as QImages. + if (us && us->handle()->classId() == QPlatformPixmap::RasterClass) + return true; + if (them && them->handle()->classId() == QPlatformPixmap::RasterClass) + return true; + return false; } case Qt::LinearGradientPattern: case Qt::RadialGradientPattern: -- cgit v1.2.3