diff options
author | Christian Ehrlicher <ch.ehrlicher@gmx.de> | 2018-04-05 21:12:52 +0200 |
---|---|---|
committer | Christian Ehrlicher <ch.ehrlicher@gmx.de> | 2018-04-10 18:45:27 +0000 |
commit | c7cbf216999cd833cae667b8c6fbfa65d33e8efd (patch) | |
tree | 4477267028e833a03a03d7c5b0ab3adcf7896bb7 /src/gui/image | |
parent | 4840ec7c68476c95d2d762ed5591f5e0b71ae3e3 (diff) |
QPixmapCache: make sure to not overflow cache limit
The cost for the pixmap cache was calculated in bytes but
setCacheLimit() takes the size in kilobytes. This lead to the
situation that all values above 2097152 overflowed and disabled
the caching completely. Fix it by calculating the cost in
kilobytes as it is done in QGLContext.
Task-number: QTBUG-45293
Change-Id: Ib8dc2360c8f3201ce0b615a04c38b5ccaa8fc6cf
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
Reviewed-by: Andy Shaw <andy.shaw@qt.io>
Diffstat (limited to 'src/gui/image')
-rw-r--r-- | src/gui/image/qpixmapcache.cpp | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/src/gui/image/qpixmapcache.cpp b/src/gui/image/qpixmapcache.cpp index 4b8b1203d6..3d1652f68b 100644 --- a/src/gui/image/qpixmapcache.cpp +++ b/src/gui/image/qpixmapcache.cpp @@ -86,7 +86,17 @@ QT_BEGIN_NAMESPACE \sa QCache, QPixmap */ -static int cache_limit = 10240; // 10 MB cache limit +static const int cache_limit_default = 10240; // 10 MB cache limit + +static inline int cost(const QPixmap &pixmap) +{ + // make sure to do a 64bit calculation + const qint64 costKb = static_cast<qint64>(pixmap.width()) * + pixmap.height() * pixmap.depth() / (8 * 1024); + const qint64 costMax = std::numeric_limits<int>::max(); + // a small pixmap should have at least a cost of 1(kb) + return static_cast<int>(qBound(1LL, costKb, costMax)); +} /*! \class QPixmapCache::Key @@ -237,7 +247,7 @@ uint qHash(const QPixmapCache::Key &k) QPMCache::QPMCache() : QObject(0), - QCache<QPixmapCache::Key, QPixmapCacheEntry>(cache_limit * 1024), + QCache<QPixmapCache::Key, QPixmapCacheEntry>(cache_limit_default), keyArray(0), theid(0), ps(0), keyArraySize(0), freeKey(0), t(false) { } @@ -553,7 +563,7 @@ bool QPixmapCache::find(const Key &key, QPixmap* pixmap) bool QPixmapCache::insert(const QString &key, const QPixmap &pixmap) { - return pm_cache()->insert(key, pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8); + return pm_cache()->insert(key, pixmap, cost(pixmap)); } /*! @@ -573,7 +583,7 @@ bool QPixmapCache::insert(const QString &key, const QPixmap &pixmap) */ QPixmapCache::Key QPixmapCache::insert(const QPixmap &pixmap) { - return pm_cache()->insert(pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8); + return pm_cache()->insert(pixmap, cost(pixmap)); } /*! @@ -590,7 +600,7 @@ bool QPixmapCache::replace(const Key &key, const QPixmap &pixmap) //The key is not valid anymore, a flush happened before probably if (!key.d || !key.d->isValid) return false; - return pm_cache()->replace(key, pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8); + return pm_cache()->replace(key, pixmap, cost(pixmap)); } /*! @@ -603,7 +613,7 @@ bool QPixmapCache::replace(const Key &key, const QPixmap &pixmap) int QPixmapCache::cacheLimit() { - return cache_limit; + return pm_cache()->maxCost(); } /*! @@ -616,8 +626,7 @@ int QPixmapCache::cacheLimit() void QPixmapCache::setCacheLimit(int n) { - cache_limit = n; - pm_cache()->setMaxCost(1024 * cache_limit); + pm_cache()->setMaxCost(n); } /*! |