Apple: cache all pixmaps from the icon engine

So far we have only cached the most recently requested pixmap, under the
assumption that this is the one usually requested several times in a
row. However, our menu integration requests the pixmaps for all
available sizes when populating the native menu item with an NSImage.
Not caching the pixmaps for all sizes results in repeated rendering of
the image, which significantly slows down the opening of native menus
e.g. using Qt Quick's labs.platform Menu.

Use a per-icon-engine cache for the pixmaps, with a cache key that is a
struct holding the mode, state, size, and scale factor.

Pick-to: 6.7
Fixes: QTBUG-127614
Change-Id: Ie13cae8c69a8ee979907d4b9ea01bcdd89dd585b
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
(cherry picked from commit c8a2794c441dfc6aa0ceec3ae847a8b1e353a89f)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Volker Hilsheimer 2024-08-01 09:34:24 +02:00 committed by Qt Cherry-pick Bot
parent e1cc2bfeb2
commit 5aafe2851f
2 changed files with 32 additions and 18 deletions

View File

@ -397,26 +397,26 @@ auto *configuredImage(const UIImage *image, const QColor &color)
QPixmap QAppleIconEngine::scaledPixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale) QPixmap QAppleIconEngine::scaledPixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale)
{ {
const quint64 cacheKey = calculateCacheKey(mode, state); const CacheKey key(mode, state, size, scale);
if (cacheKey != m_cacheKey || m_pixmap.size() != size || m_pixmap.devicePixelRatio() != scale) { QPixmap pixmap = m_cache.value(key);
if (pixmap.isNull()) {
const QSize paintSize = actualSize(size, mode, state); const QSize paintSize = actualSize(size, mode, state);
const QSize paintOffset = paintSize != size const QSize paintOffset = paintSize != size
? (QSizeF(size - paintSize) * 0.5).toSize() ? (QSizeF(size - paintSize) * 0.5).toSize()
: QSize(); : QSize();
m_pixmap = QPixmap(size * scale); pixmap = QPixmap(size * scale);
m_pixmap.setDevicePixelRatio(scale); pixmap.setDevicePixelRatio(scale);
m_pixmap.fill(Qt::transparent); pixmap.fill(Qt::transparent);
if (!m_pixmap.isNull()) { if (!pixmap.isNull()) {
QPainter painter(&m_pixmap); QPainter painter(&pixmap);
paint(&painter, QRect(paintOffset.width(), paintOffset.height(), paint(&painter, QRect(paintOffset.width(), paintOffset.height(),
paintSize.width(), paintSize.height()), mode, state); paintSize.width(), paintSize.height()), mode, state);
m_cache.insert(key, pixmap);
} }
m_cacheKey = cacheKey;
} }
return m_pixmap; return pixmap;
} }
void QAppleIconEngine::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state) void QAppleIconEngine::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state)

View File

@ -17,6 +17,8 @@
#include <QtGui/qiconengine.h> #include <QtGui/qiconengine.h>
#include <QtCore/qhash.h>
#include <QtCore/private/qcore_mac_p.h> #include <QtCore/private/qcore_mac_p.h>
Q_FORWARD_DECLARE_OBJC_CLASS(UIImage); Q_FORWARD_DECLARE_OBJC_CLASS(UIImage);
@ -43,19 +45,31 @@ public:
static QList<QSize> availableIconSizes(double aspectRatio = 1.0); static QList<QSize> availableIconSizes(double aspectRatio = 1.0);
private: private:
static constexpr quint64 calculateCacheKey(QIcon::Mode mode, QIcon::State state)
{
return (quint64(mode) << 32) | state;
}
const QString m_iconName; const QString m_iconName;
#if defined(Q_OS_MACOS) #if defined(Q_OS_MACOS)
const NSImage *m_image; const NSImage *m_image;
#elif defined(QT_PLATFORM_UIKIT) #elif defined(QT_PLATFORM_UIKIT)
const UIImage *m_image; const UIImage *m_image;
#endif #endif
mutable QPixmap m_pixmap; struct CacheKey {
mutable quint64 m_cacheKey = {}; constexpr CacheKey(QIcon::Mode mode, QIcon::State state, QSize size, qreal scale) noexcept
: modeAndState((quint64(mode) << 32) | state), size(size), scale(scale)
{}
quint64 modeAndState;
QSize size;
qreal scale;
friend constexpr bool operator==(const CacheKey &lhs, const CacheKey &rhs) noexcept
{
return lhs.modeAndState == rhs.modeAndState
&& lhs.size == rhs.size
&& lhs.scale == rhs.scale;
}
friend constexpr size_t qHash(const CacheKey &key, size_t seed) noexcept
{ return qHashMulti(seed, key.modeAndState, key.size, key.scale); }
};
mutable QHash<CacheKey, QPixmap> m_cache;
}; };