Avoid unneeded QBrush::texture() calls

If a QBrush has been created without QPixmap the texture() method will
create one. This patch avoids that in several places by checking the
type of the texture brush before accessing it, or not accessing it at
all.

Task-number: QTBUG-43766
Change-Id: If6009fe1d5bd51b239ae2c838e5c3b904b56b11a
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Reviewed-by: Gunnar Sletta <gunnar@sletta.org>
This commit is contained in:
Allan Sandfeld Jensen 2015-01-12 14:57:08 +01:00 committed by Allan Sandfeld Jensen
parent cc7d84e09e
commit 9cd5c61455
5 changed files with 74 additions and 12 deletions

View File

@ -1029,7 +1029,10 @@ QDataStream &operator<<(QDataStream &s, const QBrush &b)
s << style << b.color();
if (b.style() == Qt::TexturePattern) {
s << b.texture();
if (s.version() >= QDataStream::Qt_5_5)
s << b.textureImage();
else
s << b.texture();
} else if (s.version() >= QDataStream::Qt_4_0 && gradient_style) {
const QGradient *gradient = b.gradient();
int type_as_int = int(gradient->type());
@ -1089,10 +1092,17 @@ QDataStream &operator>>(QDataStream &s, QBrush &b)
QColor color;
s >> style;
s >> color;
b = QBrush(color);
if (style == Qt::TexturePattern) {
QPixmap pm;
s >> pm;
b = QBrush(color, pm);
if (s.version() >= QDataStream::Qt_5_5) {
QImage img;
s >> img;
b.setTextureImage(qMove(img));
} else {
QPixmap pm;
s >> pm;
b.setTexture(qMove(pm));
}
} else if (style == Qt::LinearGradientPattern
|| style == Qt::RadialGradientPattern
|| style == Qt::ConicalGradientPattern) {

View File

@ -2264,9 +2264,9 @@ int QPdfEnginePrivate::addBrushPattern(const QTransform &m, bool *specifyColor,
if (pattern.isEmpty()) {
if (brush.style() != Qt::TexturePattern)
return 0;
QImage image = brush.texture().toImage();
QImage image = brush.textureImage();
bool bitmap = true;
imageObject = addImage(image, &bitmap, brush.texture().cacheKey());
imageObject = addImage(image, &bitmap, image.cacheKey());
if (imageObject != -1) {
QImage::Format f = image.format();
if (f != QImage::Format_MonoLSB && f != QImage::Format_Mono) {

View File

@ -104,7 +104,7 @@ public:
}
QColor outline(const QPalette &pal) const {
if (!pal.window().texture().isNull())
if (pal.window().style() == Qt::TexturePattern)
return QColor(0, 0, 0, 160);
return pal.background().color().darker(140);
}
@ -117,7 +117,7 @@ public:
}
QColor tabFrameColor(const QPalette &pal) const {
if (!pal.button().texture().isNull())
if (pal.window().style() == Qt::TexturePattern)
return QColor(255, 255, 255, 8);
return buttonColor(pal).lighter(104);
}

View File

@ -1525,8 +1525,14 @@ void QWindowsStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPai
p->setBackground(opt->palette.dark().color());
p->setBrush(br);
} else {
QPixmap pm = opt->palette.brush(QPalette::Light).texture();
br = !pm.isNull() ? QBrush(pm) : QBrush(opt->palette.light().color(), Qt::Dense4Pattern);
const QBrush paletteBrush = opt->palette.brush(QPalette::Light);
if (paletteBrush.style() == Qt::TexturePattern) {
if (qHasPixmapTexture(paletteBrush))
br = QBrush(paletteBrush.texture());
else
br = QBrush(paletteBrush.textureImage());
} else
br = QBrush(opt->palette.light().color(), Qt::Dense4Pattern);
p->setBackground(opt->palette.background().color());
p->setBrush(br);
}
@ -1536,8 +1542,15 @@ void QWindowsStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPai
break; }
case CE_ScrollBarSlider:
if (!(opt->state & State_Enabled)) {
QPixmap pm = opt->palette.brush(QPalette::Light).texture();
QBrush br = !pm.isNull() ? QBrush(pm) : QBrush(opt->palette.light().color(), Qt::Dense4Pattern);
QBrush br;
const QBrush paletteBrush = opt->palette.brush(QPalette::Light);
if (paletteBrush.style() == Qt::TexturePattern) {
if (qHasPixmapTexture(paletteBrush))
br = QBrush(paletteBrush.texture());
else
br = QBrush(paletteBrush.textureImage());
} else
br = QBrush(opt->palette.light().color(), Qt::Dense4Pattern);
p->setPen(Qt::NoPen);
p->setBrush(br);
p->setBackgroundMode(Qt::OpaqueMode);

View File

@ -69,6 +69,8 @@ private slots:
void nullBrush();
void isOpaque();
void debug();
void textureBrushStream();
};
@ -407,5 +409,42 @@ void tst_QBrush::debug()
qDebug() << pixmap_brush; // don't crash
}
void tst_QBrush::textureBrushStream()
{
QPixmap pixmap_source(10, 10);
QImage image_source(10, 10, QImage::Format_RGB32);
fill(&pixmap_source);
fill(&image_source);
QBrush pixmap_brush;
pixmap_brush.setTexture(pixmap_source);
QBrush image_brush;
image_brush.setTextureImage(image_source);
QByteArray data1;
QByteArray data2;
{
QDataStream stream1(&data1, QIODevice::WriteOnly);
QDataStream stream2(&data2, QIODevice::WriteOnly);
stream1 << pixmap_brush;
stream2 << image_brush;
}
QBrush loadedBrush1;
QBrush loadedBrush2;
{
QDataStream stream1(&data1, QIODevice::ReadOnly);
QDataStream stream2(&data2, QIODevice::ReadOnly);
stream1 >> loadedBrush1;
stream2 >> loadedBrush2;
}
QCOMPARE(loadedBrush1.style(), Qt::TexturePattern);
QCOMPARE(loadedBrush2.style(), Qt::TexturePattern);
QCOMPARE(loadedBrush1.texture(), pixmap_source);
QCOMPARE(loadedBrush2.textureImage(), image_source);
}
QTEST_MAIN(tst_QBrush)
#include "tst_qbrush.moc"