From 25ae30256bd7980ed42a65ab73a4d4395040391a Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Mon, 17 May 2010 10:11:50 +0200 Subject: Optimized pixmapcache key generation for icons and styles This re-apply commit c8d2e18c4b431870560f324a17f20904bb47ae68 from Jens that was reverted in b0f4dab4 because it did not compile. Added the missing include to make it compile on mac Task-number: QTBUG-9850 --- src/gui/image/qicon.cpp | 25 ++++++++--------- src/gui/styles/qgtkpainter.cpp | 9 ++++-- src/gui/styles/qstylehelper.cpp | 62 ++++++----------------------------------- src/gui/styles/qstylehelper_p.h | 32 +++++++++++++++++++++ 4 files changed, 59 insertions(+), 69 deletions(-) diff --git a/src/gui/image/qicon.cpp b/src/gui/image/qicon.cpp index 891b1db..7696632 100644 --- a/src/gui/image/qicon.cpp +++ b/src/gui/image/qicon.cpp @@ -55,6 +55,7 @@ #include "qcache.h" #include "qdebug.h" #include "private/qguiplatformplugin_p.h" +#include "private/qstylehelper_p.h" #ifdef Q_WS_MAC #include @@ -261,21 +262,17 @@ QPixmap QPixmapIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::St if (!actualSize.isNull() && (actualSize.width() > size.width() || actualSize.height() > size.height())) actualSize.scale(size, Qt::KeepAspectRatio); - QString key = QLatin1String("$qt_icon_") - + QString::number(pm.cacheKey()) - + QString::number(pe->mode) - + QString::number(QApplication::palette().cacheKey()) - + QLatin1Char('_') - + QString::number(actualSize.width()) - + QLatin1Char('_') - + QString::number(actualSize.height()) - + QLatin1Char('_'); - + QString key = QLatin1Literal("qt_") + % HexString(pm.cacheKey()) + % HexString(pe->mode) + % HexString(QApplication::palette().cacheKey()) + % HexString(actualSize.width()) + % HexString(actualSize.height()); if (mode == QIcon::Active) { - if (QPixmapCache::find(key + QString::number(mode), pm)) + if (QPixmapCache::find(key % HexString(mode), pm)) return pm; // horray - if (QPixmapCache::find(key + QString::number(QIcon::Normal), pm)) { + if (QPixmapCache::find(key % HexString(QIcon::Normal), pm)) { QStyleOption opt(0); opt.palette = QApplication::palette(); QPixmap active = QApplication::style()->generatedIconPixmap(QIcon::Active, pm, &opt); @@ -284,7 +281,7 @@ QPixmap QPixmapIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::St } } - if (!QPixmapCache::find(key + QString::number(mode), pm)) { + if (!QPixmapCache::find(key % HexString(mode), pm)) { if (pm.size() != actualSize) pm = pm.scaled(actualSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); if (pe->mode != mode && mode != QIcon::Normal) { @@ -294,7 +291,7 @@ QPixmap QPixmapIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::St if (!generated.isNull()) pm = generated; } - QPixmapCache::insert(key + QString::number(mode), pm); + QPixmapCache::insert(key % HexString(mode), pm); } return pm; } diff --git a/src/gui/styles/qgtkpainter.cpp b/src/gui/styles/qgtkpainter.cpp index 1f68f2f..79c53e9 100644 --- a/src/gui/styles/qgtkpainter.cpp +++ b/src/gui/styles/qgtkpainter.cpp @@ -47,6 +47,7 @@ // This class is primarily a wrapper around the gtk painter functions // and takes care of converting all such calls into cached Qt pixmaps. +#include #include #include #include @@ -155,8 +156,12 @@ static QString uniqueName(const QString &key, GtkStateType state, GtkShadowType const QSize &size, GtkWidget *widget = 0) { // Note the widget arg should ideally use the widget path, though would compromise performance - QString tmp = QString(QLS("%0-%1-%2-%3x%4-%5")).arg(key).arg(uint(state)).arg(shadow) - .arg(size.width()).arg(size.height()).arg(quintptr(widget)); + QString tmp = key + % HexString(state) + % HexString(shadow) + % HexString(size.width()) + % HexString(size.height()) + % HexString(quint64(widget)); return tmp; } diff --git a/src/gui/styles/qstylehelper.cpp b/src/gui/styles/qstylehelper.cpp index 296c51c..d09d7fa 100644 --- a/src/gui/styles/qstylehelper.cpp +++ b/src/gui/styles/qstylehelper.cpp @@ -58,67 +58,23 @@ QT_BEGIN_NAMESPACE -// internal helper. Converts an integer value to an unique string token -template -struct HexString -{ - inline HexString(const T t) - : val(t) - {} - - inline void write(QChar *&dest) const - { - const ushort hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; - const char *c = reinterpret_cast(&val); - for (uint i = 0; i < sizeof(T); ++i) { - *dest++ = hexChars[*c & 0xf]; - *dest++ = hexChars[(*c & 0xf0) >> 4]; - ++c; - } - } - - const T val; -}; - -// specialization to enable fast concatenating of our string tokens to a string -template -struct QConcatenable > -{ - typedef HexString type; - enum { ExactSize = true }; - static int size(const HexString &str) { return sizeof(str.val) * 2; } - static inline void appendTo(const HexString &str, QChar *&out) { str.write(out); } -}; - namespace QStyleHelper { QString uniqueName(const QString &key, const QStyleOption *option, const QSize &size) { const QStyleOptionComplex *complexOption = qstyleoption_cast(option); - - QString tmp = key - % QLatin1Char('-') - % HexString(option->state) - % QLatin1Char('-') - % HexString(option->direction) - % QLatin1Char('-') - % HexString(complexOption ? uint(complexOption->activeSubControls) : 0u) - % QLatin1Char('-') - % HexString(option->palette.cacheKey()) - % QLatin1Char('-') - % HexString(size.width()) - % QLatin1Char('x') - % HexString(size.height()); + QString tmp = key % HexString(option->state) + % HexString(option->direction) + % HexString(complexOption ? uint(complexOption->activeSubControls) : 0u) + % HexString(option->palette.cacheKey()) + % HexString(size.width()) + % HexString(size.height()); #ifndef QT_NO_SPINBOX if (const QStyleOptionSpinBox *spinBox = qstyleoption_cast(option)) { - tmp = tmp - % QLatin1Char('-') - % HexString(spinBox->buttonSymbols) - % QLatin1Char('-') - % HexString(spinBox->stepEnabled) - % QLatin1Char('-') - % QLatin1Char(spinBox->frame ? '1' : '0'); + tmp = tmp % HexString(spinBox->buttonSymbols) + % HexString(spinBox->stepEnabled) + % QLatin1Char(spinBox->frame ? '1' : '0'); ; } #endif // QT_NO_SPINBOX return tmp; diff --git a/src/gui/styles/qstylehelper_p.h b/src/gui/styles/qstylehelper_p.h index 31cc4ed..555ad60 100644 --- a/src/gui/styles/qstylehelper_p.h +++ b/src/gui/styles/qstylehelper_p.h @@ -41,6 +41,7 @@ #include #include +#include #include #ifndef QSTYLEHELPER_P_H @@ -79,6 +80,37 @@ namespace QStyleHelper int bottom = 0); } +// internal helper. Converts an integer value to an unique string token +template + struct HexString +{ + inline HexString(const T t) + : val(t) + {} + + inline void write(QChar *&dest) const + { + const ushort hexChars[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; + const char *c = reinterpret_cast(&val); + for (uint i = 0; i < sizeof(T); ++i) { + *dest++ = hexChars[*c & 0xf]; + *dest++ = hexChars[(*c & 0xf0) >> 4]; + ++c; + } + } + const T val; +}; + +// specialization to enable fast concatenating of our string tokens to a string +template + struct QConcatenable > +{ + typedef HexString type; + enum { ExactSize = true }; + static int size(const HexString &str) { return sizeof(str.val) * 2; } + static inline void appendTo(const HexString &str, QChar *&out) { str.write(out); } +}; + QT_END_NAMESPACE #endif // QSTYLEHELPER_P_H -- cgit v0.12 From 45791722b9491076a7308a30159eba7309220664 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Mon, 17 May 2010 16:48:12 +0200 Subject: Compile with Gcc 4.1 --- src/gui/styles/qstylehelper_p.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/gui/styles/qstylehelper_p.h b/src/gui/styles/qstylehelper_p.h index 555ad60..71fce55 100644 --- a/src/gui/styles/qstylehelper_p.h +++ b/src/gui/styles/qstylehelper_p.h @@ -43,6 +43,7 @@ #include #include #include +#include #ifndef QSTYLEHELPER_P_H #define QSTYLEHELPER_P_H -- cgit v0.12