1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#define Q_TEST_QPIXMAPCACHE
41#include "qpixmapcache.h"
42#include "qobject.h"
43#include "qdebug.h"
44#include "qpixmapcache_p.h"
45#include "qthread.h"
46#include "qcoreapplication.h"
47
48QT_BEGIN_NAMESPACE
49
50/*!
51 \class QPixmapCache
52 \inmodule QtGui
53
54 \brief The QPixmapCache class provides an application-wide cache for pixmaps.
55
56 This class is a tool for optimized drawing with QPixmap. You can
57 use it to store temporary pixmaps that are expensive to generate
58 without using more storage space than cacheLimit(). Use insert()
59 to insert pixmaps, find() to find them, and clear() to empty the
60 cache.
61
62 QPixmapCache contains no member data, only static functions to
63 access the global pixmap cache. It creates an internal QCache
64 object for caching the pixmaps.
65
66 The cache associates a pixmap with a user-provided string as a key,
67 or with a QPixmapCache::Key that the cache generates.
68 Using QPixmapCache::Key for keys is faster than using strings. The string API is
69 very convenient for complex keys but the QPixmapCache::Key API will be very
70 efficient and convenient for a one-to-one object-to-pixmap mapping - in
71 this case, you can store the keys as members of an object.
72
73 If two pixmaps are inserted into the cache using equal keys then the
74 last pixmap will replace the first pixmap in the cache. This follows the
75 behavior of the QHash and QCache classes.
76
77 The cache becomes full when the total size of all pixmaps in the
78 cache exceeds cacheLimit(). The initial cache limit is 10240 KB (10 MB);
79 you can change this by calling setCacheLimit() with the required value.
80 A pixmap takes roughly (\e{width} * \e{height} * \e{depth})/8 bytes of
81 memory.
82
83 The \e{Qt Quarterly} article
84 \l{http://doc.qt.io/archives/qq/qq12-qpixmapcache.html}{Optimizing
85 with QPixmapCache} explains how to use QPixmapCache to speed up
86 applications by caching the results of painting.
87
88 \note QPixmapCache is only usable from the application's main thread.
89 Access from other threads will be ignored and return failure.
90
91 \sa QCache, QPixmap
92*/
93
94static const int cache_limit_default = 10240; // 10 MB cache limit
95
96static inline int cost(const QPixmap &pixmap)
97{
98 // make sure to do a 64bit calculation
99 const qint64 costKb = static_cast<qint64>(pixmap.width()) *
100 pixmap.height() * pixmap.depth() / (8 * 1024);
101 const qint64 costMax = std::numeric_limits<int>::max();
102 // a small pixmap should have at least a cost of 1(kb)
103 return static_cast<int>(qBound(1LL, costKb, costMax));
104}
105
106static inline bool qt_pixmapcache_thread_test()
107{
108 if (Q_LIKELY(QCoreApplication::instance() && QThread::currentThread() == QCoreApplication::instance()->thread()))
109 return true;
110
111 return false;
112}
113
114/*!
115 \class QPixmapCache::Key
116 \brief The QPixmapCache::Key class can be used for efficient access
117 to the QPixmapCache.
118 \inmodule QtGui
119 \since 4.6
120
121 Use QPixmapCache::insert() to receive an instance of Key generated
122 by the pixmap cache. You can store the key in your own objects for
123 a very efficient one-to-one object-to-pixmap mapping.
124*/
125
126/*!
127 Constructs an empty Key object.
128*/
129QPixmapCache::Key::Key() : d(nullptr)
130{
131}
132
133/*!
134 \internal
135 Constructs a copy of \a other.
136*/
137QPixmapCache::Key::Key(const Key &other)
138{
139 if (other.d)
140 ++(other.d->ref);
141 d = other.d;
142}
143
144/*!
145 Destroys the key.
146*/
147QPixmapCache::Key::~Key()
148{
149 if (d && --(d->ref) == 0)
150 delete d;
151}
152
153/*!
154 \internal
155
156 Returns \c true if this key is the same as the given \a key; otherwise returns
157 false.
158*/
159bool QPixmapCache::Key::operator ==(const Key &key) const
160{
161 return (d == key.d);
162}
163
164/*!
165 \fn bool QPixmapCache::Key::operator !=(const Key &key) const
166 \internal
167*/
168
169/*!
170 \fn QPixmapCache::Key::Key(Key &&)
171 \internal
172 \since 5.6
173*/
174
175/*!
176 \fn QPixmapCache::Key &QPixmapCache::Key::operator=(Key &&)
177 \internal
178 \since 5.6
179*/
180
181/*!
182 \fn void QPixmapCache::Key::swap(Key &)
183 \internal
184 \since 5.6
185*/
186
187/*!
188 Returns \c true if there is a cached pixmap associated with this key.
189 Otherwise, if pixmap was flushed, the key is no longer valid.
190 \since 5.7
191*/
192bool QPixmapCache::Key::isValid() const noexcept
193{
194 return d && d->isValid;
195}
196
197/*!
198 \internal
199*/
200QPixmapCache::Key &QPixmapCache::Key::operator =(const Key &other)
201{
202 if (d != other.d) {
203 if (other.d)
204 ++(other.d->ref);
205 if (d && --(d->ref) == 0)
206 delete d;
207 d = other.d;
208 }
209 return *this;
210}
211
212class QPMCache : public QObject, public QCache<QPixmapCache::Key, QPixmapCacheEntry>
213{
214 Q_OBJECT
215public:
216 QPMCache();
217 ~QPMCache();
218
219 void timerEvent(QTimerEvent *) override;
220 bool insert(const QString& key, const QPixmap &pixmap, int cost);
221 QPixmapCache::Key insert(const QPixmap &pixmap, int cost);
222 bool replace(const QPixmapCache::Key &key, const QPixmap &pixmap, int cost);
223 bool remove(const QString &key);
224 bool remove(const QPixmapCache::Key &key);
225
226 void resizeKeyArray(int size);
227 QPixmapCache::Key createKey();
228 void releaseKey(const QPixmapCache::Key &key);
229 void clear();
230
231 QPixmap *object(const QString &key) const;
232 QPixmap *object(const QPixmapCache::Key &key) const;
233
234 static inline QPixmapCache::KeyData *get(const QPixmapCache::Key &key)
235 {return key.d;}
236
237 static QPixmapCache::KeyData* getKeyData(QPixmapCache::Key *key);
238
239 bool flushDetachedPixmaps(bool nt);
240
241private:
242 enum { soon_time = 10000, flush_time = 30000 };
243 int *keyArray;
244 int theid;
245 int ps;
246 int keyArraySize;
247 int freeKey;
248 QHash<QString, QPixmapCache::Key> cacheKeys;
249 bool t;
250};
251
252QT_BEGIN_INCLUDE_NAMESPACE
253#include "qpixmapcache.moc"
254QT_END_INCLUDE_NAMESPACE
255
256size_t qHash(const QPixmapCache::Key &k, size_t seed)
257{
258 return qHash(QPMCache::get(k)->key, seed);
259}
260
261QPMCache::QPMCache()
262 : QObject(nullptr),
263 QCache<QPixmapCache::Key, QPixmapCacheEntry>(cache_limit_default),
264 keyArray(nullptr), theid(0), ps(0), keyArraySize(0), freeKey(0), t(false)
265{
266}
267QPMCache::~QPMCache()
268{
269 clear();
270 free(keyArray);
271}
272
273/*
274 This is supposed to cut the cache size down by about 25% in a
275 minute once the application becomes idle, to let any inserted pixmap
276 remain in the cache for some time before it becomes a candidate for
277 cleaning-up, and to not cut down the size of the cache while the
278 cache is in active use.
279
280 When the last detached pixmap has been deleted from the cache, kill the
281 timer so Qt won't keep the CPU from going into sleep mode. Currently
282 the timer is not restarted when the pixmap becomes unused, but it does
283 restart once something else is added (i.e. the cache space is actually needed).
284
285 Returns \c true if any were removed.
286*/
287bool QPMCache::flushDetachedPixmaps(bool nt)
288{
289 int mc = maxCost();
290 setMaxCost(nt ? totalCost() * 3 / 4 : totalCost() -1);
291 setMaxCost(mc);
292 ps = totalCost();
293
294 bool any = false;
295 QHash<QString, QPixmapCache::Key>::iterator it = cacheKeys.begin();
296 while (it != cacheKeys.end()) {
297 if (!contains(it.value())) {
298 releaseKey(it.value());
299 it = cacheKeys.erase(it);
300 any = true;
301 } else {
302 ++it;
303 }
304 }
305
306 return any;
307}
308
309void QPMCache::timerEvent(QTimerEvent *)
310{
311 bool nt = totalCost() == ps;
312 if (!flushDetachedPixmaps(nt)) {
313 killTimer(theid);
314 theid = 0;
315 } else if (nt != t) {
316 killTimer(theid);
317 theid = startTimer(nt ? soon_time : flush_time);
318 t = nt;
319 }
320}
321
322
323QPixmap *QPMCache::object(const QString &key) const
324{
325 QPixmapCache::Key cacheKey = cacheKeys.value(key);
326 if (!cacheKey.d || !cacheKey.d->isValid) {
327 const_cast<QPMCache *>(this)->cacheKeys.remove(key);
328 return nullptr;
329 }
330 QPixmap *ptr = QCache<QPixmapCache::Key, QPixmapCacheEntry>::object(cacheKey);
331 //We didn't find the pixmap in the cache, the key is not valid anymore
332 if (!ptr) {
333 const_cast<QPMCache *>(this)->cacheKeys.remove(key);
334 }
335 return ptr;
336}
337
338QPixmap *QPMCache::object(const QPixmapCache::Key &key) const
339{
340 Q_ASSERT(key.d->isValid);
341 QPixmap *ptr = QCache<QPixmapCache::Key, QPixmapCacheEntry>::object(key);
342 //We didn't find the pixmap in the cache, the key is not valid anymore
343 if (!ptr)
344 const_cast<QPMCache *>(this)->releaseKey(key);
345 return ptr;
346}
347
348bool QPMCache::insert(const QString& key, const QPixmap &pixmap, int cost)
349{
350 QPixmapCache::Key &cacheKey = cacheKeys[key];
351 //If for the same key we add already a pixmap we should delete it
352 if (cacheKey.d)
353 QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(cacheKey);
354
355 //we create a new key the old one has been removed
356 cacheKey = createKey();
357
358 bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost);
359 if (success) {
360 if (!theid) {
361 theid = startTimer(flush_time);
362 t = false;
363 }
364 } else {
365 //Insertion failed we released the new allocated key
366 cacheKeys.remove(key);
367 }
368 return success;
369}
370
371QPixmapCache::Key QPMCache::insert(const QPixmap &pixmap, int cost)
372{
373 QPixmapCache::Key cacheKey = createKey();
374 bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost);
375 if (success) {
376 if (!theid) {
377 theid = startTimer(flush_time);
378 t = false;
379 }
380 }
381 return cacheKey;
382}
383
384bool QPMCache::replace(const QPixmapCache::Key &key, const QPixmap &pixmap, int cost)
385{
386 Q_ASSERT(key.d->isValid);
387 //If for the same key we had already an entry so we should delete the pixmap and use the new one
388 QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(key);
389
390 QPixmapCache::Key cacheKey = createKey();
391
392 bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost);
393 if (success) {
394 if(!theid) {
395 theid = startTimer(flush_time);
396 t = false;
397 }
398 const_cast<QPixmapCache::Key&>(key) = cacheKey;
399 }
400 return success;
401}
402
403bool QPMCache::remove(const QString &key)
404{
405 auto cacheKey = cacheKeys.constFind(key);
406 //The key was not in the cache
407 if (cacheKey == cacheKeys.constEnd())
408 return false;
409 const bool result = QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(cacheKey.value());
410 cacheKeys.erase(cacheKey);
411 return result;
412}
413
414bool QPMCache::remove(const QPixmapCache::Key &key)
415{
416 return QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(key);
417}
418
419void QPMCache::resizeKeyArray(int size)
420{
421 if (size <= keyArraySize || size == 0)
422 return;
423 keyArray = q_check_ptr(reinterpret_cast<int *>(realloc(keyArray,
424 size * sizeof(int))));
425 for (int i = keyArraySize; i != size; ++i)
426 keyArray[i] = i + 1;
427 keyArraySize = size;
428}
429
430QPixmapCache::Key QPMCache::createKey()
431{
432 if (freeKey == keyArraySize)
433 resizeKeyArray(keyArraySize ? keyArraySize << 1 : 2);
434 int id = freeKey;
435 freeKey = keyArray[id];
436 QPixmapCache::Key key;
437 QPixmapCache::KeyData *d = QPMCache::getKeyData(&key);
438 d->key = ++id;
439 return key;
440}
441
442void QPMCache::releaseKey(const QPixmapCache::Key &key)
443{
444 if (key.d->key > keyArraySize || key.d->key <= 0)
445 return;
446 key.d->key--;
447 keyArray[key.d->key] = freeKey;
448 freeKey = key.d->key;
449 key.d->isValid = false;
450 key.d->key = 0;
451}
452
453void QPMCache::clear()
454{
455 free(keyArray);
456 keyArray = nullptr;
457 freeKey = 0;
458 keyArraySize = 0;
459 //Mark all keys as invalid
460 QList<QPixmapCache::Key> keys = QCache<QPixmapCache::Key, QPixmapCacheEntry>::keys();
461 for (int i = 0; i < keys.size(); ++i)
462 keys.at(i).d->isValid = false;
463 QCache<QPixmapCache::Key, QPixmapCacheEntry>::clear();
464}
465
466QPixmapCache::KeyData* QPMCache::getKeyData(QPixmapCache::Key *key)
467{
468 if (!key->d)
469 key->d = new QPixmapCache::KeyData;
470 return key->d;
471}
472
473Q_GLOBAL_STATIC(QPMCache, pm_cache)
474
475int Q_AUTOTEST_EXPORT q_QPixmapCache_keyHashSize()
476{
477 return pm_cache()->size();
478}
479
480QPixmapCacheEntry::~QPixmapCacheEntry()
481{
482 pm_cache()->releaseKey(key);
483}
484
485/*!
486 Looks for a cached pixmap associated with the given \a key in the cache.
487 If the pixmap is found, the function sets \a pixmap to that pixmap and
488 returns \c true; otherwise it leaves \a pixmap alone and returns \c false.
489
490 \since 4.6
491
492 Example:
493 \snippet code/src_gui_image_qpixmapcache.cpp 1
494*/
495
496bool QPixmapCache::find(const QString &key, QPixmap *pixmap)
497{
498 if (!qt_pixmapcache_thread_test())
499 return false;
500 QPixmap *ptr = pm_cache()->object(key);
501 if (ptr && pixmap)
502 *pixmap = *ptr;
503 return ptr != nullptr;
504}
505
506/*!
507 Looks for a cached pixmap associated with the given \a key in the cache.
508 If the pixmap is found, the function sets \a pixmap to that pixmap and
509 returns \c true; otherwise it leaves \a pixmap alone and returns \c false. If
510 the pixmap is not found, it means that the \a key is no longer valid,
511 so it will be released for the next insertion.
512
513 \since 4.6
514*/
515bool QPixmapCache::find(const Key &key, QPixmap *pixmap)
516{
517 if (!qt_pixmapcache_thread_test())
518 return false;
519 //The key is not valid anymore, a flush happened before probably
520 if (!key.d || !key.d->isValid)
521 return false;
522 QPixmap *ptr = pm_cache()->object(key);
523 if (ptr && pixmap)
524 *pixmap = *ptr;
525 return ptr != nullptr;
526}
527
528/*!
529 Inserts a copy of the pixmap \a pixmap associated with the \a key into
530 the cache.
531
532 All pixmaps inserted by the Qt library have a key starting with
533 "$qt", so your own pixmap keys should never begin "$qt".
534
535 When a pixmap is inserted and the cache is about to exceed its
536 limit, it removes pixmaps until there is enough room for the
537 pixmap to be inserted.
538
539 The oldest pixmaps (least recently accessed in the cache) are
540 deleted when more space is needed.
541
542 The function returns \c true if the object was inserted into the
543 cache; otherwise it returns \c false.
544
545 \sa setCacheLimit()
546*/
547
548bool QPixmapCache::insert(const QString &key, const QPixmap &pixmap)
549{
550 if (!qt_pixmapcache_thread_test())
551 return false;
552 return pm_cache()->insert(key, pixmap, cost(pixmap));
553}
554
555/*!
556 Inserts a copy of the given \a pixmap into the cache and returns a key
557 that can be used to retrieve it.
558
559 When a pixmap is inserted and the cache is about to exceed its
560 limit, it removes pixmaps until there is enough room for the
561 pixmap to be inserted.
562
563 The oldest pixmaps (least recently accessed in the cache) are
564 deleted when more space is needed.
565
566 \sa setCacheLimit(), replace()
567
568 \since 4.6
569*/
570QPixmapCache::Key QPixmapCache::insert(const QPixmap &pixmap)
571{
572 if (!qt_pixmapcache_thread_test())
573 return QPixmapCache::Key();
574 return pm_cache()->insert(pixmap, cost(pixmap));
575}
576
577/*!
578 Replaces the pixmap associated with the given \a key with the \a pixmap
579 specified. Returns \c true if the \a pixmap has been correctly inserted into
580 the cache; otherwise returns \c false.
581
582 \sa setCacheLimit(), insert()
583
584 \since 4.6
585*/
586bool QPixmapCache::replace(const Key &key, const QPixmap &pixmap)
587{
588 if (!qt_pixmapcache_thread_test())
589 return false;
590 //The key is not valid anymore, a flush happened before probably
591 if (!key.d || !key.d->isValid)
592 return false;
593 return pm_cache()->replace(key, pixmap, cost(pixmap));
594}
595
596/*!
597 Returns the cache limit (in kilobytes).
598
599 The default cache limit is 10240 KB.
600
601 \sa setCacheLimit()
602*/
603
604int QPixmapCache::cacheLimit()
605{
606 return pm_cache()->maxCost();
607}
608
609/*!
610 Sets the cache limit to \a n kilobytes.
611
612 The default setting is 10240 KB.
613
614 \sa cacheLimit()
615*/
616
617void QPixmapCache::setCacheLimit(int n)
618{
619 if (!qt_pixmapcache_thread_test())
620 return;
621 pm_cache()->setMaxCost(n);
622}
623
624/*!
625 Removes the pixmap associated with \a key from the cache.
626*/
627void QPixmapCache::remove(const QString &key)
628{
629 if (!qt_pixmapcache_thread_test())
630 return;
631 pm_cache()->remove(key);
632}
633
634/*!
635 Removes the pixmap associated with \a key from the cache and releases
636 the key for a future insertion.
637
638 \since 4.6
639*/
640void QPixmapCache::remove(const Key &key)
641{
642 if (!qt_pixmapcache_thread_test())
643 return;
644 //The key is not valid anymore, a flush happened before probably
645 if (!key.d || !key.d->isValid)
646 return;
647 pm_cache()->remove(key);
648}
649
650/*!
651 Removes all pixmaps from the cache.
652*/
653
654void QPixmapCache::clear()
655{
656 if (!QCoreApplication::closingDown() && !qt_pixmapcache_thread_test())
657 return;
658 QT_TRY {
659 if (pm_cache.exists())
660 pm_cache->clear();
661 } QT_CATCH(const std::bad_alloc &) {
662 // if we ran out of memory during pm_cache(), it's no leak,
663 // so just ignore it.
664 }
665}
666
667void QPixmapCache::flushDetachedPixmaps()
668{
669 pm_cache()->flushDetachedPixmaps(true);
670}
671
672int QPixmapCache::totalUsed()
673{
674 return (pm_cache()->totalCost()+1023) / 1024;
675}
676
677/*!
678 \fn QPixmapCache::KeyData::KeyData()
679
680 \internal
681*/
682/*!
683 \fn QPixmapCache::KeyData::KeyData(const KeyData &other)
684 \internal
685*/
686/*!
687 \fn QPixmapCache::KeyData::~KeyData()
688
689 \internal
690*/
691QT_END_NAMESPACE
692