image/qpixmapcache.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
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 Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
38** $QT_END_LICENSE$ -
39** -
40****************************************************************************/ -
41 -
42#define Q_TEST_QPIXMAPCACHE -
43#include "qpixmapcache.h" -
44#include "qobject.h" -
45#include "qdebug.h" -
46#include "qpixmapcache_p.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 -
79 2048 KB (2 MB) on embedded platforms, 10240 KB (10 MB) on desktop -
80 platforms; you can change this by calling setCacheLimit() with the -
81 required value. -
82 A pixmap takes roughly (\e{width} * \e{height} * \e{depth})/8 bytes of -
83 memory. -
84 -
85 The \e{Qt Quarterly} article -
86 \l{http://doc.qt.digia.com/qq/qq12-qpixmapcache.html}{Optimizing -
87 with QPixmapCache} explains how to use QPixmapCache to speed up -
88 applications by caching the results of painting. -
89 -
90 \sa QCache, QPixmap -
91*/ -
92 -
93static int cache_limit = 10240; // 10 MB cache limit for desktop -
94 -
95/*! -
96 \class QPixmapCache::Key -
97 \brief The QPixmapCache::Key class can be used for efficient access -
98 to the QPixmapCache. -
99 \inmodule QtGui -
100 \since 4.6 -
101 -
102 Use QPixmapCache::insert() to receive an instance of Key generated -
103 by the pixmap cache. You can store the key in your own objects for -
104 a very efficient one-to-one object-to-pixmap mapping. -
105*/ -
106 -
107/*! -
108 Constructs an empty Key object. -
109*/ -
110QPixmapCache::Key::Key() : d(0) -
111{ -
112}
executed: }
Execution Count:6115
6115
113 -
114/*! -
115 \internal -
116 Constructs a copy of \a other. -
117*/ -
118QPixmapCache::Key::Key(const Key &other) -
119{ -
120 if (other.d)
partially evaluated: other.d
TRUEFALSE
yes
Evaluation Count:10746
no
Evaluation Count:0
0-10746
121 ++(other.d->ref);
executed: ++(other.d->ref);
Execution Count:10746
10746
122 d = other.d;
executed (the execution status of this line is deduced): d = other.d;
-
123}
executed: }
Execution Count:10746
10746
124 -
125/*! -
126 Destroys the key. -
127*/ -
128QPixmapCache::Key::~Key() -
129{ -
130 if (d && --(d->ref) == 0)
evaluated: d
TRUEFALSE
yes
Evaluation Count:13798
yes
Evaluation Count:3056
evaluated: --(d->ref) == 0
TRUEFALSE
yes
Evaluation Count:1519
yes
Evaluation Count:12279
1519-13798
131 delete d;
executed: delete d;
Execution Count:1519
1519
132}
executed: }
Execution Count:16854
16854
133 -
134/*! -
135 \internal -
136 -
137 Returns true if this key is the same as the given \a key; otherwise returns -
138 false. -
139*/ -
140bool QPixmapCache::Key::operator ==(const Key &key) const -
141{ -
142 return (d == key.d);
executed: return (d == key.d);
Execution Count:4810
4810
143} -
144 -
145/*! -
146 \fn bool QPixmapCache::Key::operator !=(const Key &key) const -
147 \internal -
148*/ -
149 -
150/*! -
151 \internal -
152*/ -
153QPixmapCache::Key &QPixmapCache::Key::operator =(const Key &other) -
154{ -
155 if (d != other.d) {
partially evaluated: d != other.d
TRUEFALSE
yes
Evaluation Count:1533
no
Evaluation Count:0
0-1533
156 if (other.d)
partially evaluated: other.d
TRUEFALSE
yes
Evaluation Count:1533
no
Evaluation Count:0
0-1533
157 ++(other.d->ref);
executed: ++(other.d->ref);
Execution Count:1533
1533
158 if (d && --(d->ref) == 0)
evaluated: d
TRUEFALSE
yes
Evaluation Count:8
yes
Evaluation Count:1525
partially evaluated: --(d->ref) == 0
TRUEFALSE
yes
Evaluation Count:8
no
Evaluation Count:0
0-1525
159 delete d;
executed: delete d;
Execution Count:8
8
160 d = other.d;
executed (the execution status of this line is deduced): d = other.d;
-
161 }
executed: }
Execution Count:1533
1533
162 return *this;
executed: return *this;
Execution Count:1533
1533
163} -
164 -
165class QPMCache : public QObject, public QCache<QPixmapCache::Key, QPixmapCacheEntry> -
166{ -
167 Q_OBJECT -
168public: -
169 QPMCache(); -
170 ~QPMCache(); -
171 -
172 void timerEvent(QTimerEvent *); -
173 bool insert(const QString& key, const QPixmap &pixmap, int cost); -
174 QPixmapCache::Key insert(const QPixmap &pixmap, int cost); -
175 bool replace(const QPixmapCache::Key &key, const QPixmap &pixmap, int cost); -
176 bool remove(const QString &key); -
177 bool remove(const QPixmapCache::Key &key); -
178 -
179 void resizeKeyArray(int size); -
180 QPixmapCache::Key createKey(); -
181 void releaseKey(const QPixmapCache::Key &key); -
182 void clear(); -
183 -
184 QPixmap *object(const QString &key) const; -
185 QPixmap *object(const QPixmapCache::Key &key) const; -
186 -
187 static inline QPixmapCache::KeyData *get(const QPixmapCache::Key &key) -
188 {return key.d;}
executed: return key.d;
Execution Count:7907
7907
189 -
190 static QPixmapCache::KeyData* getKeyData(QPixmapCache::Key *key); -
191 -
192 QList< QPair<QString,QPixmap> > allPixmaps() const; -
193 bool flushDetachedPixmaps(bool nt); -
194 -
195private: -
196 enum { soon_time = 10000, flush_time = 30000 }; -
197 int *keyArray; -
198 int theid; -
199 int ps; -
200 int keyArraySize; -
201 int freeKey; -
202 QHash<QString, QPixmapCache::Key> cacheKeys; -
203 bool t; -
204}; -
205 -
206QT_BEGIN_INCLUDE_NAMESPACE -
207#include "qpixmapcache.moc" -
208QT_END_INCLUDE_NAMESPACE -
209 -
210uint qHash(const QPixmapCache::Key &k) -
211{ -
212 return qHash(QPMCache::get(k)->key);
executed: return qHash(QPMCache::get(k)->key);
Execution Count:7907
7907
213} -
214 -
215QPMCache::QPMCache() -
216 : QObject(0), -
217 QCache<QPixmapCache::Key, QPixmapCacheEntry>(cache_limit * 1024), -
218 keyArray(0), theid(0), ps(0), keyArraySize(0), freeKey(0), t(false) -
219{ -
220}
executed: }
Execution Count:177
177
221QPMCache::~QPMCache() -
222{ -
223 clear();
executed (the execution status of this line is deduced): clear();
-
224 free(keyArray);
executed (the execution status of this line is deduced): free(keyArray);
-
225}
executed: }
Execution Count:177
177
226 -
227/* -
228 This is supposed to cut the cache size down by about 25% in a -
229 minute once the application becomes idle, to let any inserted pixmap -
230 remain in the cache for some time before it becomes a candidate for -
231 cleaning-up, and to not cut down the size of the cache while the -
232 cache is in active use. -
233 -
234 When the last detached pixmap has been deleted from the cache, kill the -
235 timer so Qt won't keep the CPU from going into sleep mode. Currently -
236 the timer is not restarted when the pixmap becomes unused, but it does -
237 restart once something else is added (i.e. the cache space is actually needed). -
238 -
239 Returns true if any were removed. -
240*/ -
241bool QPMCache::flushDetachedPixmaps(bool nt) -
242{ -
243 int mc = maxCost();
executed (the execution status of this line is deduced): int mc = maxCost();
-
244 setMaxCost(nt ? totalCost() * 3 / 4 : totalCost() -1);
executed (the execution status of this line is deduced): setMaxCost(nt ? totalCost() * 3 / 4 : totalCost() -1);
-
245 setMaxCost(mc);
executed (the execution status of this line is deduced): setMaxCost(mc);
-
246 ps = totalCost();
executed (the execution status of this line is deduced): ps = totalCost();
-
247 -
248 bool any = false;
executed (the execution status of this line is deduced): bool any = false;
-
249 QHash<QString, QPixmapCache::Key>::iterator it = cacheKeys.begin();
executed (the execution status of this line is deduced): QHash<QString, QPixmapCache::Key>::iterator it = cacheKeys.begin();
-
250 while (it != cacheKeys.end()) {
evaluated: it != cacheKeys.end()
TRUEFALSE
yes
Evaluation Count:120
yes
Evaluation Count:5
5-120
251 if (!contains(it.value())) {
evaluated: !contains(it.value())
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:115
5-115
252 releaseKey(it.value());
executed (the execution status of this line is deduced): releaseKey(it.value());
-
253 it = cacheKeys.erase(it);
executed (the execution status of this line is deduced): it = cacheKeys.erase(it);
-
254 any = true;
executed (the execution status of this line is deduced): any = true;
-
255 } else {
executed: }
Execution Count:5
5
256 ++it;
executed (the execution status of this line is deduced): ++it;
-
257 }
executed: }
Execution Count:115
115
258 } -
259 -
260 return any;
executed: return any;
Execution Count:5
5
261} -
262 -
263void QPMCache::timerEvent(QTimerEvent *) -
264{ -
265 bool nt = totalCost() == ps;
executed (the execution status of this line is deduced): bool nt = totalCost() == ps;
-
266 if (!flushDetachedPixmaps(nt)) {
partially evaluated: !flushDetachedPixmaps(nt)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:5
0-5
267 killTimer(theid);
never executed (the execution status of this line is deduced): killTimer(theid);
-
268 theid = 0;
never executed (the execution status of this line is deduced): theid = 0;
-
269 } else if (nt != t) {
never executed: }
partially evaluated: nt != t
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:5
0-5
270 killTimer(theid);
never executed (the execution status of this line is deduced): killTimer(theid);
-
271 theid = startTimer(nt ? soon_time : flush_time);
never executed (the execution status of this line is deduced): theid = startTimer(nt ? soon_time : flush_time);
-
272 t = nt;
never executed (the execution status of this line is deduced): t = nt;
-
273 }
never executed: }
0
274} -
275 -
276 -
277QPixmap *QPMCache::object(const QString &key) const -
278{ -
279 QPixmapCache::Key cacheKey = cacheKeys.value(key);
executed (the execution status of this line is deduced): QPixmapCache::Key cacheKey = cacheKeys.value(key);
-
280 if (!cacheKey.d || !cacheKey.d->isValid) {
evaluated: !cacheKey.d
TRUEFALSE
yes
Evaluation Count:1534
yes
Evaluation Count:4653
partially evaluated: !cacheKey.d->isValid
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:4653
0-4653
281 const_cast<QPMCache *>(this)->cacheKeys.remove(key);
executed (the execution status of this line is deduced): const_cast<QPMCache *>(this)->cacheKeys.remove(key);
-
282 return 0;
executed: return 0;
Execution Count:1534
1534
283 } -
284 QPixmap *ptr = QCache<QPixmapCache::Key, QPixmapCacheEntry>::object(cacheKey);
executed (the execution status of this line is deduced): QPixmap *ptr = QCache<QPixmapCache::Key, QPixmapCacheEntry>::object(cacheKey);
-
285 //We didn't find the pixmap in the cache, the key is not valid anymore -
286 if (!ptr) {
partially evaluated: !ptr
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:4653
0-4653
287 const_cast<QPMCache *>(this)->cacheKeys.remove(key);
never executed (the execution status of this line is deduced): const_cast<QPMCache *>(this)->cacheKeys.remove(key);
-
288 }
never executed: }
0
289 return ptr;
executed: return ptr;
Execution Count:4653
4653
290} -
291 -
292QPixmap *QPMCache::object(const QPixmapCache::Key &key) const -
293{ -
294 Q_ASSERT(key.d->isValid);
executed (the execution status of this line is deduced): qt_noop();
-
295 QPixmap *ptr = QCache<QPixmapCache::Key, QPixmapCacheEntry>::object(key);
executed (the execution status of this line is deduced): QPixmap *ptr = QCache<QPixmapCache::Key, QPixmapCacheEntry>::object(key);
-
296 //We didn't find the pixmap in the cache, the key is not valid anymore -
297 if (!ptr)
partially evaluated: !ptr
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:9
0-9
298 const_cast<QPMCache *>(this)->releaseKey(key);
never executed: const_cast<QPMCache *>(this)->releaseKey(key);
0
299 return ptr;
executed: return ptr;
Execution Count:9
9
300} -
301 -
302bool QPMCache::insert(const QString& key, const QPixmap &pixmap, int cost) -
303{ -
304 QPixmapCache::Key cacheKey;
executed (the execution status of this line is deduced): QPixmapCache::Key cacheKey;
-
305 QPixmapCache::Key oldCacheKey = cacheKeys.value(key);
executed (the execution status of this line is deduced): QPixmapCache::Key oldCacheKey = cacheKeys.value(key);
-
306 //If for the same key we add already a pixmap we should delete it -
307 if (oldCacheKey.d) {
partially evaluated: oldCacheKey.d
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1513
0-1513
308 QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(oldCacheKey);
never executed (the execution status of this line is deduced): QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(oldCacheKey);
-
309 cacheKeys.remove(key);
never executed (the execution status of this line is deduced): cacheKeys.remove(key);
-
310 }
never executed: }
0
311 -
312 //we create a new key the old one has been removed -
313 cacheKey = createKey();
executed (the execution status of this line is deduced): cacheKey = createKey();
-
314 -
315 bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost);
executed (the execution status of this line is deduced): bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost);
-
316 if (success) {
partially evaluated: success
TRUEFALSE
yes
Evaluation Count:1513
no
Evaluation Count:0
0-1513
317 cacheKeys.insert(key, cacheKey);
executed (the execution status of this line is deduced): cacheKeys.insert(key, cacheKey);
-
318 if (!theid) {
evaluated: !theid
TRUEFALSE
yes
Evaluation Count:44
yes
Evaluation Count:1469
44-1469
319 theid = startTimer(flush_time);
executed (the execution status of this line is deduced): theid = startTimer(flush_time);
-
320 t = false;
executed (the execution status of this line is deduced): t = false;
-
321 }
executed: }
Execution Count:44
44
322 } else {
executed: }
Execution Count:1513
1513
323 //Insertion failed we released the new allocated key -
324 releaseKey(cacheKey);
never executed (the execution status of this line is deduced): releaseKey(cacheKey);
-
325 }
never executed: }
0
326 return success;
executed: return success;
Execution Count:1513
1513
327} -
328 -
329QPixmapCache::Key QPMCache::insert(const QPixmap &pixmap, int cost) -
330{ -
331 QPixmapCache::Key cacheKey = createKey();
executed (the execution status of this line is deduced): QPixmapCache::Key cacheKey = createKey();
-
332 bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost);
executed (the execution status of this line is deduced): bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost);
-
333 if (success) {
partially evaluated: success
TRUEFALSE
yes
Evaluation Count:20
no
Evaluation Count:0
0-20
334 if (!theid) {
evaluated: !theid
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:18
2-18
335 theid = startTimer(flush_time);
executed (the execution status of this line is deduced): theid = startTimer(flush_time);
-
336 t = false;
executed (the execution status of this line is deduced): t = false;
-
337 }
executed: }
Execution Count:2
2
338 } else {
executed: }
Execution Count:20
20
339 //Insertion failed we released the key and return an invalid one -
340 releaseKey(cacheKey);
never executed (the execution status of this line is deduced): releaseKey(cacheKey);
-
341 }
never executed: }
0
342 return cacheKey;
executed: return cacheKey;
Execution Count:20
20
343} -
344 -
345bool QPMCache::replace(const QPixmapCache::Key &key, const QPixmap &pixmap, int cost) -
346{ -
347 Q_ASSERT(key.d->isValid);
never executed (the execution status of this line is deduced): qt_noop();
-
348 //If for the same key we had already an entry so we should delete the pixmap and use the new one -
349 QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(key);
never executed (the execution status of this line is deduced): QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(key);
-
350 -
351 QPixmapCache::Key cacheKey = createKey();
never executed (the execution status of this line is deduced): QPixmapCache::Key cacheKey = createKey();
-
352 -
353 bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost);
never executed (the execution status of this line is deduced): bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost);
-
354 if (success) {
never evaluated: success
0
355 if(!theid) {
never evaluated: !theid
0
356 theid = startTimer(flush_time);
never executed (the execution status of this line is deduced): theid = startTimer(flush_time);
-
357 t = false;
never executed (the execution status of this line is deduced): t = false;
-
358 }
never executed: }
0
359 const_cast<QPixmapCache::Key&>(key) = cacheKey;
never executed (the execution status of this line is deduced): const_cast<QPixmapCache::Key&>(key) = cacheKey;
-
360 } else {
never executed: }
0
361 //Insertion failed we released the key -
362 releaseKey(cacheKey);
never executed (the execution status of this line is deduced): releaseKey(cacheKey);
-
363 }
never executed: }
0
364 return success;
never executed: return success;
0
365} -
366 -
367bool QPMCache::remove(const QString &key) -
368{ -
369 QPixmapCache::Key cacheKey = cacheKeys.value(key);
never executed (the execution status of this line is deduced): QPixmapCache::Key cacheKey = cacheKeys.value(key);
-
370 //The key was not in the cache -
371 if (!cacheKey.d)
never evaluated: !cacheKey.d
0
372 return false;
never executed: return false;
0
373 cacheKeys.remove(key);
never executed (the execution status of this line is deduced): cacheKeys.remove(key);
-
374 return QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(cacheKey);
never executed: return QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(cacheKey);
0
375} -
376 -
377bool QPMCache::remove(const QPixmapCache::Key &key) -
378{ -
379 return QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(key);
executed: return QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(key);
Execution Count:14
14
380} -
381 -
382void QPMCache::resizeKeyArray(int size) -
383{ -
384 if (size <= keyArraySize || size == 0)
partially evaluated: size <= keyArraySize
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:154
partially evaluated: size == 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:154
0-154
385 return;
never executed: return;
0
386 keyArray = q_check_ptr(reinterpret_cast<int *>(realloc(keyArray,
executed (the execution status of this line is deduced): keyArray = q_check_ptr(reinterpret_cast<int *>(realloc(keyArray,
-
387 size * sizeof(int))));
executed (the execution status of this line is deduced): size * sizeof(int))));
-
388 for (int i = keyArraySize; i != size; ++i)
evaluated: i != size
TRUEFALSE
yes
Evaluation Count:2084
yes
Evaluation Count:154
154-2084
389 keyArray[i] = i + 1;
executed: keyArray[i] = i + 1;
Execution Count:2084
2084
390 keyArraySize = size;
executed (the execution status of this line is deduced): keyArraySize = size;
-
391}
executed: }
Execution Count:154
154
392 -
393QPixmapCache::Key QPMCache::createKey() -
394{ -
395 if (freeKey == keyArraySize)
evaluated: freeKey == keyArraySize
TRUEFALSE
yes
Evaluation Count:154
yes
Evaluation Count:1379
154-1379
396 resizeKeyArray(keyArraySize ? keyArraySize << 1 : 2);
executed: resizeKeyArray(keyArraySize ? keyArraySize << 1 : 2);
Execution Count:154
154
397 int id = freeKey;
executed (the execution status of this line is deduced): int id = freeKey;
-
398 freeKey = keyArray[id];
executed (the execution status of this line is deduced): freeKey = keyArray[id];
-
399 QPixmapCache::Key key;
executed (the execution status of this line is deduced): QPixmapCache::Key key;
-
400 QPixmapCache::KeyData *d = QPMCache::getKeyData(&key);
executed (the execution status of this line is deduced): QPixmapCache::KeyData *d = QPMCache::getKeyData(&key);
-
401 d->key = ++id;
executed (the execution status of this line is deduced): d->key = ++id;
-
402 return key;
executed: return key;
Execution Count:1533
1533
403} -
404 -
405void QPMCache::releaseKey(const QPixmapCache::Key &key) -
406{ -
407 if (key.d->key > keyArraySize || key.d->key <= 0)
evaluated: key.d->key > keyArraySize
TRUEFALSE
yes
Evaluation Count:1514
yes
Evaluation Count:24
evaluated: key.d->key <= 0
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:19
5-1514
408 return;
executed: return;
Execution Count:1519
1519
409 key.d->key--;
executed (the execution status of this line is deduced): key.d->key--;
-
410 keyArray[key.d->key] = freeKey;
executed (the execution status of this line is deduced): keyArray[key.d->key] = freeKey;
-
411 freeKey = key.d->key;
executed (the execution status of this line is deduced): freeKey = key.d->key;
-
412 key.d->isValid = false;
executed (the execution status of this line is deduced): key.d->isValid = false;
-
413 key.d->key = 0;
executed (the execution status of this line is deduced): key.d->key = 0;
-
414}
executed: }
Execution Count:19
19
415 -
416void QPMCache::clear() -
417{ -
418 free(keyArray);
executed (the execution status of this line is deduced): free(keyArray);
-
419 keyArray = 0;
executed (the execution status of this line is deduced): keyArray = 0;
-
420 freeKey = 0;
executed (the execution status of this line is deduced): freeKey = 0;
-
421 keyArraySize = 0;
executed (the execution status of this line is deduced): keyArraySize = 0;
-
422 //Mark all keys as invalid -
423 QList<QPixmapCache::Key> keys = QCache<QPixmapCache::Key, QPixmapCacheEntry>::keys();
executed (the execution status of this line is deduced): QList<QPixmapCache::Key> keys = QCache<QPixmapCache::Key, QPixmapCacheEntry>::keys();
-
424 for (int i = 0; i < keys.size(); ++i)
evaluated: i < keys.size()
TRUEFALSE
yes
Evaluation Count:1514
yes
Evaluation Count:618
618-1514
425 keys.at(i).d->isValid = false;
executed: keys.at(i).d->isValid = false;
Execution Count:1514
1514
426 QCache<QPixmapCache::Key, QPixmapCacheEntry>::clear();
executed (the execution status of this line is deduced): QCache<QPixmapCache::Key, QPixmapCacheEntry>::clear();
-
427}
executed: }
Execution Count:618
618
428 -
429QPixmapCache::KeyData* QPMCache::getKeyData(QPixmapCache::Key *key) -
430{ -
431 if (!key->d)
partially evaluated: !key->d
TRUEFALSE
yes
Evaluation Count:1533
no
Evaluation Count:0
0-1533
432 key->d = new QPixmapCache::KeyData;
executed: key->d = new QPixmapCache::KeyData;
Execution Count:1533
1533
433 return key->d;
executed: return key->d;
Execution Count:1533
1533
434} -
435 -
436QList< QPair<QString,QPixmap> > QPMCache::allPixmaps() const -
437{ -
438 QList< QPair<QString,QPixmap> > r;
never executed (the execution status of this line is deduced): QList< QPair<QString,QPixmap> > r;
-
439 QHash<QString, QPixmapCache::Key>::const_iterator it = cacheKeys.begin();
never executed (the execution status of this line is deduced): QHash<QString, QPixmapCache::Key>::const_iterator it = cacheKeys.begin();
-
440 while (it != cacheKeys.end()) {
never evaluated: it != cacheKeys.end()
0
441 QPixmap *ptr = QCache<QPixmapCache::Key, QPixmapCacheEntry>::object(it.value());
never executed (the execution status of this line is deduced): QPixmap *ptr = QCache<QPixmapCache::Key, QPixmapCacheEntry>::object(it.value());
-
442 if (ptr)
never evaluated: ptr
0
443 r.append(QPair<QString,QPixmap>(it.key(),*ptr));
never executed: r.append(QPair<QString,QPixmap>(it.key(),*ptr));
0
444 ++it;
never executed (the execution status of this line is deduced): ++it;
-
445 }
never executed: }
0
446 return r;
never executed: return r;
0
447} -
448 -
449 -
450Q_GLOBAL_STATIC(QPMCache, pm_cache)
never executed: delete x;
executed: return thisGlobalStatic.pointer.load();
Execution Count:9717
partially evaluated: !thisGlobalStatic.pointer.testAndSetOrdered(0, x)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:177
evaluated: !thisGlobalStatic.pointer.load()
TRUEFALSE
yes
Evaluation Count:177
yes
Evaluation Count:9540
partially evaluated: !thisGlobalStatic.destroyed
TRUEFALSE
yes
Evaluation Count:177
no
Evaluation Count:0
0-9717
451 -
452int Q_AUTOTEST_EXPORT q_QPixmapCache_keyHashSize() -
453{ -
454 return pm_cache()->size();
never executed: return pm_cache()->size();
0
455} -
456 -
457QPixmapCacheEntry::~QPixmapCacheEntry() -
458{ -
459 pm_cache()->releaseKey(key);
executed (the execution status of this line is deduced): pm_cache()->releaseKey(key);
-
460}
executed: }
Execution Count:1533
1533
461 -
462/*! -
463 \obsolete -
464 \overload -
465 -
466 Returns the pixmap associated with the \a key in the cache, or -
467 null if there is no such pixmap. -
468 -
469 \warning If valid, you should copy the pixmap immediately (this is -
470 fast). Subsequent insertions into the cache could cause the -
471 pointer to become invalid. For this reason, we recommend you use -
472 bool find(const QString&, QPixmap*) instead. -
473 -
474 Example: -
475 \snippet code/src_gui_image_qpixmapcache.cpp 0 -
476*/ -
477 -
478QPixmap *QPixmapCache::find(const QString &key) -
479{ -
480 return pm_cache()->object(key);
never executed: return pm_cache()->object(key);
0
481} -
482 -
483 -
484/*! -
485 \obsolete -
486 -
487 Use bool find(const QString&, QPixmap*) instead. -
488*/ -
489 -
490bool QPixmapCache::find(const QString &key, QPixmap& pixmap) -
491{ -
492 return find(key, &pixmap);
executed: return find(key, &pixmap);
Execution Count:4989
4989
493} -
494 -
495/*! -
496 Looks for a cached pixmap associated with the given \a key in the cache. -
497 If the pixmap is found, the function sets \a pixmap to that pixmap and -
498 returns true; otherwise it leaves \a pixmap alone and returns false. -
499 -
500 \since 4.6 -
501 -
502 Example: -
503 \snippet code/src_gui_image_qpixmapcache.cpp 1 -
504*/ -
505 -
506bool QPixmapCache::find(const QString &key, QPixmap* pixmap) -
507{ -
508 QPixmap *ptr = pm_cache()->object(key);
executed (the execution status of this line is deduced): QPixmap *ptr = pm_cache()->object(key);
-
509 if (ptr && pixmap)
evaluated: ptr
TRUEFALSE
yes
Evaluation Count:4653
yes
Evaluation Count:1534
partially evaluated: pixmap
TRUEFALSE
yes
Evaluation Count:4653
no
Evaluation Count:0
0-4653
510 *pixmap = *ptr;
executed: *pixmap = *ptr;
Execution Count:4653
4653
511 return ptr != 0;
executed: return ptr != 0;
Execution Count:6187
6187
512} -
513 -
514/*! -
515 Looks for a cached pixmap associated with the given \a key in the cache. -
516 If the pixmap is found, the function sets \a pixmap to that pixmap and -
517 returns true; otherwise it leaves \a pixmap alone and returns false. If -
518 the pixmap is not found, it means that the \a key is no longer valid, -
519 so it will be released for the next insertion. -
520 -
521 \since 4.6 -
522*/ -
523bool QPixmapCache::find(const Key &key, QPixmap* pixmap) -
524{ -
525 //The key is not valid anymore, a flush happened before probably -
526 if (!key.d || !key.d->isValid)
evaluated: !key.d
TRUEFALSE
yes
Evaluation Count:15
yes
Evaluation Count:13
evaluated: !key.d->isValid
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:9
4-15
527 return false;
executed: return false;
Execution Count:19
19
528 QPixmap *ptr = pm_cache()->object(key);
executed (the execution status of this line is deduced): QPixmap *ptr = pm_cache()->object(key);
-
529 if (ptr && pixmap)
partially evaluated: ptr
TRUEFALSE
yes
Evaluation Count:9
no
Evaluation Count:0
partially evaluated: pixmap
TRUEFALSE
yes
Evaluation Count:9
no
Evaluation Count:0
0-9
530 *pixmap = *ptr;
executed: *pixmap = *ptr;
Execution Count:9
9
531 return ptr != 0;
executed: return ptr != 0;
Execution Count:9
9
532} -
533 -
534/*! -
535 Inserts a copy of the pixmap \a pixmap associated with the \a key into -
536 the cache. -
537 -
538 All pixmaps inserted by the Qt library have a key starting with -
539 "$qt", so your own pixmap keys should never begin "$qt". -
540 -
541 When a pixmap is inserted and the cache is about to exceed its -
542 limit, it removes pixmaps until there is enough room for the -
543 pixmap to be inserted. -
544 -
545 The oldest pixmaps (least recently accessed in the cache) are -
546 deleted when more space is needed. -
547 -
548 The function returns true if the object was inserted into the -
549 cache; otherwise it returns false. -
550 -
551 \sa setCacheLimit() -
552*/ -
553 -
554bool QPixmapCache::insert(const QString &key, const QPixmap &pixmap) -
555{ -
556 return pm_cache()->insert(key, pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8);
executed: return pm_cache()->insert(key, pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8);
Execution Count:1513
1513
557} -
558 -
559/*! -
560 Inserts a copy of the given \a pixmap into the cache and returns a key -
561 that can be used to retrieve it. -
562 -
563 When a pixmap is inserted and the cache is about to exceed its -
564 limit, it removes pixmaps until there is enough room for the -
565 pixmap to be inserted. -
566 -
567 The oldest pixmaps (least recently accessed in the cache) are -
568 deleted when more space is needed. -
569 -
570 \sa setCacheLimit(), replace() -
571 -
572 \since 4.6 -
573*/ -
574QPixmapCache::Key QPixmapCache::insert(const QPixmap &pixmap) -
575{ -
576 return pm_cache()->insert(pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8);
executed: return pm_cache()->insert(pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8);
Execution Count:20
20
577} -
578 -
579/*! -
580 Replaces the pixmap associated with the given \a key with the \a pixmap -
581 specified. Returns true if the \a pixmap has been correctly inserted into -
582 the cache; otherwise returns false. -
583 -
584 \sa setCacheLimit(), insert() -
585 -
586 \since 4.6 -
587*/ -
588bool QPixmapCache::replace(const Key &key, const QPixmap &pixmap) -
589{ -
590 //The key is not valid anymore, a flush happened before probably -
591 if (!key.d || !key.d->isValid)
never evaluated: !key.d
never evaluated: !key.d->isValid
0
592 return false;
never executed: return false;
0
593 return pm_cache()->replace(key, pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8);
never executed: return pm_cache()->replace(key, pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8);
0
594} -
595 -
596/*! -
597 Returns the cache limit (in kilobytes). -
598 -
599 The default cache limit is 2048 KB on embedded platforms, 10240 KB on -
600 desktop platforms. -
601 -
602 \sa setCacheLimit() -
603*/ -
604 -
605int QPixmapCache::cacheLimit() -
606{ -
607 return cache_limit;
never executed: return cache_limit;
0
608} -
609 -
610/*! -
611 Sets the cache limit to \a n kilobytes. -
612 -
613 The default setting is 2048 KB on embedded platforms, 10240 KB on -
614 desktop platforms. -
615 -
616 \sa cacheLimit() -
617*/ -
618 -
619void QPixmapCache::setCacheLimit(int n) -
620{ -
621 cache_limit = n;
never executed (the execution status of this line is deduced): cache_limit = n;
-
622 pm_cache()->setMaxCost(1024 * cache_limit);
never executed (the execution status of this line is deduced): pm_cache()->setMaxCost(1024 * cache_limit);
-
623}
never executed: }
0
624 -
625/*! -
626 Removes the pixmap associated with \a key from the cache. -
627*/ -
628void QPixmapCache::remove(const QString &key) -
629{ -
630 pm_cache()->remove(key);
never executed (the execution status of this line is deduced): pm_cache()->remove(key);
-
631}
never executed: }
0
632 -
633/*! -
634 Removes the pixmap associated with \a key from the cache and releases -
635 the key for a future insertion. -
636 -
637 \since 4.6 -
638*/ -
639void QPixmapCache::remove(const Key &key) -
640{ -
641 //The key is not valid anymore, a flush happened before probably -
642 if (!key.d || !key.d->isValid)
evaluated: !key.d
TRUEFALSE
yes
Evaluation Count:96
yes
Evaluation Count:28
evaluated: !key.d->isValid
TRUEFALSE
yes
Evaluation Count:14
yes
Evaluation Count:14
14-96
643 return;
executed: return;
Execution Count:110
110
644 pm_cache()->remove(key);
executed (the execution status of this line is deduced): pm_cache()->remove(key);
-
645}
executed: }
Execution Count:14
14
646 -
647/*! -
648 Removes all pixmaps from the cache. -
649*/ -
650 -
651void QPixmapCache::clear() -
652{ -
653 QT_TRY {
partially evaluated: true
TRUEFALSE
yes
Evaluation Count:441
no
Evaluation Count:0
0-441
654 pm_cache()->clear();
executed (the execution status of this line is deduced): pm_cache()->clear();
-
655 } QT_CATCH(const std::bad_alloc &) {
executed: }
Execution Count:441
441
656 // if we ran out of memory during pm_cache(), it's no leak, -
657 // so just ignore it. -
658 }
never executed: }
0
659} -
660 -
661void QPixmapCache::flushDetachedPixmaps() -
662{ -
663 pm_cache()->flushDetachedPixmaps(true);
never executed (the execution status of this line is deduced): pm_cache()->flushDetachedPixmaps(true);
-
664}
never executed: }
0
665 -
666int QPixmapCache::totalUsed() -
667{ -
668 return (pm_cache()->totalCost()+1023) / 1024;
never executed: return (pm_cache()->totalCost()+1023) / 1024;
0
669} -
670 -
671QList< QPair<QString,QPixmap> > QPixmapCache::allPixmaps() -
672{ -
673 return pm_cache()->allPixmaps();
never executed: return pm_cache()->allPixmaps();
0
674} -
675 -
676QT_END_NAMESPACE -
677 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial