Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/gui/image/qpixmapcache.cpp |
Switch to Source code | Preprocessed file |
Line | Source | Count |
---|---|---|
1 | - | |
2 | - | |
3 | static int cache_limit = 10240; | - |
4 | QPixmapCache::Key::Key() : d(0) | - |
5 | { | - |
6 | } | - |
7 | - | |
8 | - | |
9 | - | |
10 | - | |
11 | - | |
12 | QPixmapCache::Key::Key(const Key &other) | - |
13 | { | - |
14 | if (other.d) | - |
15 | ++(other.d->ref); | - |
16 | d = other.d; | - |
17 | } | - |
18 | - | |
19 | - | |
20 | - | |
21 | - | |
22 | QPixmapCache::Key::~Key() | - |
23 | { | - |
24 | if (d && --(d->ref) == 0) | - |
25 | delete d; | - |
26 | } | - |
27 | - | |
28 | - | |
29 | - | |
30 | - | |
31 | - | |
32 | - | |
33 | - | |
34 | bool QPixmapCache::Key::operator ==(const Key &key) const | - |
35 | { | - |
36 | return (d == key.d); | - |
37 | } | - |
38 | bool QPixmapCache::Key::isValid() const noexcept | - |
39 | { | - |
40 | return never executed: d && d->isValid;return d && d->isValid; never executed: return d && d->isValid; | 0 |
41 | } | - |
42 | - | |
43 | - | |
44 | - | |
45 | - | |
46 | QPixmapCache::Key &QPixmapCache::Key::operator =(const Key &other) | - |
47 | { | - |
48 | if (d != other.d) { | - |
49 | if (other.d) | - |
50 | ++(other.d->ref); | - |
51 | if (d && --(d->ref) == 0) | - |
52 | delete d; | - |
53 | d = other.d; | - |
54 | } | - |
55 | return *this; | - |
56 | } | - |
57 | - | |
58 | class QPMCache : public QObject, public QCache<QPixmapCache::Key, QPixmapCacheEntry> | - |
59 | { | - |
60 | public: template <typename ThisObject> inline void qt_check_for_QOBJECT_macro(const ThisObject &_q_argument) const { int i = qYouForgotTheQ_OBJECT_Macro(this, &_q_argument); i = i + 1; } | - |
61 | #pragma GCC diagnostic push | - |
62 | static const QMetaObject staticMetaObject; virtual const QMetaObject *metaObject() const; virtual void *qt_metacast(const char *); virtual int qt_metacall(QMetaObject::Call, int, void **); static inline QString tr(const char *s, const char *c = nullptr, int n = -1) { return staticMetaObject.tr(s, c, n); } __attribute__ ((__deprecated__)) static inline QString trUtf8(const char *s, const char *c = nullptr, int n = -1) { return staticMetaObject.tr(s, c, n); } private: __attribute__((visibility("hidden"))) static void qt_static_metacall(QObject *, QMetaObject::Call, int, void **); | - |
63 | #pragma GCC diagnostic pop | - |
64 | struct QPrivateSignal {}; | - |
65 | public: | - |
66 | QPMCache(); | - |
67 | ~QPMCache(); | - |
68 | - | |
69 | void timerEvent(QTimerEvent *) override; | - |
70 | bool insert(const QString& key, const QPixmap &pixmap, int cost); | - |
71 | QPixmapCache::Key insert(const QPixmap &pixmap, int cost); | - |
72 | bool replace(const QPixmapCache::Key &key, const QPixmap &pixmap, int cost); | - |
73 | bool remove(const QString &key); | - |
74 | bool remove(const QPixmapCache::Key &key); | - |
75 | - | |
76 | void resizeKeyArray(int size); | - |
77 | QPixmapCache::Key createKey(); | - |
78 | void releaseKey(const QPixmapCache::Key &key); | - |
79 | void clear(); | - |
80 | - | |
81 | QPixmap *object(const QString &key) const; | - |
82 | QPixmap *object(const QPixmapCache::Key &key) const; | - |
83 | - | |
84 | static inline QPixmapCache::KeyData *get(const QPixmapCache::Key &key) | - |
85 | {return key.d;} | - |
86 | - | |
87 | static QPixmapCache::KeyData* getKeyData(QPixmapCache::Key *key); | - |
88 | - | |
89 | bool flushDetachedPixmaps(bool nt); | - |
90 | - | |
91 | private: | - |
92 | enum { soon_time = 10000, flush_time = 30000 }; | - |
93 | int *keyArray; | - |
94 | int theid; | - |
95 | int ps; | - |
96 | int keyArraySize; | - |
97 | int freeKey; | - |
98 | QHash<QString, QPixmapCache::Key> cacheKeys; | - |
99 | bool t; | - |
100 | }; | - |
101 | - | |
102 | - | |
103 | - | |
104 | - | |
105 | uint qHash(const QPixmapCache::Key &k) | - |
106 | { | - |
107 | return qHash(QPMCache::get(k)->key); | - |
108 | } | - |
109 | - | |
110 | QPMCache::QPMCache() | - |
111 | : QObject(0), | - |
112 | QCache<QPixmapCache::Key, QPixmapCacheEntry>(cache_limit * 1024), | - |
113 | keyArray(0), theid(0), ps(0), keyArraySize(0), freeKey(0), t(false) | - |
114 | { | - |
115 | } | - |
116 | QPMCache::~QPMCache() | - |
117 | { | - |
118 | clear(); | - |
119 | free(keyArray); | - |
120 | } | - |
121 | bool QPMCache::flushDetachedPixmaps(bool nt) | - |
122 | { | - |
123 | int mc = maxCost(); | - |
124 | setMaxCost(nt ? totalCost() * 3 / 4 : totalCost() -1); | - |
125 | setMaxCost(mc); | - |
126 | ps = totalCost(); | - |
127 | - | |
128 | bool any = false; | - |
129 | QHash<QString, QPixmapCache::Key>::iterator it = cacheKeys.begin(); | - |
130 | while (it != cacheKeys.end()) { | - |
131 | if (!contains(it.value())) { | - |
132 | releaseKey(it.value()); | - |
133 | it = cacheKeys.erase(it); | - |
134 | any = true; | - |
135 | } else { | - |
136 | ++it; | - |
137 | } | - |
138 | } | - |
139 | - | |
140 | return any; | - |
141 | } | - |
142 | - | |
143 | void QPMCache::timerEvent(QTimerEvent *) | - |
144 | { | - |
145 | bool nt = totalCost() == ps; | - |
146 | if (!flushDetachedPixmaps(nt)) { | - |
147 | killTimer(theid); | - |
148 | theid = 0; | - |
149 | } else if (nt != t) { | - |
150 | killTimer(theid); | - |
151 | theid = startTimer(nt ? soon_time : flush_time); | - |
152 | t = nt; | - |
153 | } | - |
154 | } | - |
155 | - | |
156 | - | |
157 | QPixmap *QPMCache::object(const QString &key) const | - |
158 | { | - |
159 | QPixmapCache::Key cacheKey = cacheKeys.value(key); | - |
160 | if (!cacheKey.d || !cacheKey.d->isValid) { | - |
161 | const_cast<QPMCache *>(this)->cacheKeys.remove(key); | - |
162 | return 0; | - |
163 | } | - |
164 | QPixmap *ptr = QCache<QPixmapCache::Key, QPixmapCacheEntry>::object(cacheKey); | - |
165 | - | |
166 | if (!ptr) { | - |
167 | const_cast<QPMCache *>(this)->cacheKeys.remove(key); | - |
168 | } | - |
169 | return ptr; | - |
170 | } | - |
171 | - | |
172 | QPixmap *QPMCache::object(const QPixmapCache::Key &key) const | - |
173 | { | - |
174 | ((!(key.d->isValid)) ? qt_assert("key.d->isValid",__FILE__,301317) : qt_noop()); | - |
175 | QPixmap *ptr = QCache<QPixmapCache::Key, QPixmapCacheEntry>::object(key); | - |
176 | - | |
177 | if (!ptr) | - |
178 | const_cast<QPMCache *>(this)->releaseKey(key); | - |
179 | return ptr; | - |
180 | } | - |
181 | - | |
182 | bool QPMCache::insert(const QString& key, const QPixmap &pixmap, int cost) | - |
183 | { | - |
184 | QPixmapCache::Key cacheKey; | - |
185 | QPixmapCache::Key oldCacheKey = cacheKeys.value(key); | - |
186 | - | |
187 | if (oldCacheKey.d) { | - |
188 | QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(oldCacheKey); | - |
189 | cacheKeys.remove(key); | - |
190 | } | - |
191 | - | |
192 | - | |
193 | cacheKey = createKey(); | - |
194 | - | |
195 | bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost); | - |
196 | if (success) { | - |
197 | cacheKeys.insert(key, cacheKey); | - |
198 | if (!theid) { | - |
199 | theid = startTimer(flush_time); | - |
200 | t = false; | - |
201 | } | - |
202 | } else { | - |
203 | - | |
204 | releaseKey(cacheKey); | - |
205 | } | - |
206 | return success; | - |
207 | } | - |
208 | - | |
209 | QPixmapCache::Key QPMCache::insert(const QPixmap &pixmap, int cost) | - |
210 | { | - |
211 | QPixmapCache::Key cacheKey = createKey(); | - |
212 | bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost); | - |
213 | if (success) { | - |
214 | if (!theid) { | - |
215 | theid = startTimer(flush_time); | - |
216 | t = false; | - |
217 | } | - |
218 | } else { | - |
219 | - | |
220 | releaseKey(cacheKey); | - |
221 | } | - |
222 | return cacheKey; | - |
223 | } | - |
224 | - | |
225 | bool QPMCache::replace(const QPixmapCache::Key &key, const QPixmap &pixmap, int cost) | - |
226 | { | - |
227 | ((!(key.d->isValid)) ? qt_assert("key.d->isValid",__FILE__,354370) : qt_noop()); | - |
228 | - | |
229 | QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(key); | - |
230 | - | |
231 | QPixmapCache::Key cacheKey = createKey(); | - |
232 | - | |
233 | bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost); | - |
234 | if (success) { | - |
235 | if(!theid) { | - |
236 | theid = startTimer(flush_time); | - |
237 | t = false; | - |
238 | } | - |
239 | const_cast<QPixmapCache::Key&>(key) = cacheKey; | - |
240 | } else { | - |
241 | - | |
242 | releaseKey(cacheKey); | - |
243 | } | - |
244 | return success; | - |
245 | } | - |
246 | - | |
247 | bool QPMCache::remove(const QString &key) | - |
248 | { | - |
249 | QPixmapCache::Key cacheKey = cacheKeys.value(key); | - |
250 | - | |
251 | if (!cacheKey.d) | - |
252 | return false; | - |
253 | cacheKeys.remove(key); | - |
254 | return QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(cacheKey); | - |
255 | } | - |
256 | - | |
257 | bool QPMCache::remove(const QPixmapCache::Key &key) | - |
258 | { | - |
259 | return QCache<QPixmapCache::Key, QPixmapCacheEntry>::remove(key); | - |
260 | } | - |
261 | - | |
262 | void QPMCache::resizeKeyArray(int size) | - |
263 | { | - |
264 | if (size <= keyArraySize || size == 0) | - |
265 | return; | - |
266 | keyArray = q_check_ptr(reinterpret_cast<int *>(realloc(keyArray, | - |
267 | size * sizeof(int)))); | - |
268 | for (int i = keyArraySize; i != size; ++i) | - |
269 | keyArray[i] = i + 1; | - |
270 | keyArraySize = size; | - |
271 | } | - |
272 | - | |
273 | QPixmapCache::Key QPMCache::createKey() | - |
274 | { | - |
275 | if (freeKey == keyArraySize) | - |
276 | resizeKeyArray(keyArraySize ? keyArraySize << 1 : 2); | - |
277 | int id = freeKey; | - |
278 | freeKey = keyArray[id]; | - |
279 | QPixmapCache::Key key; | - |
280 | QPixmapCache::KeyData *d = QPMCache::getKeyData(&key); | - |
281 | d->key = ++id; | - |
282 | return key; | - |
283 | } | - |
284 | - | |
285 | void QPMCache::releaseKey(const QPixmapCache::Key &key) | - |
286 | { | - |
287 | if (key.d->key > keyArraySize || key.d->key <= 0) | - |
288 | return; | - |
289 | key.d->key--; | - |
290 | keyArray[key.d->key] = freeKey; | - |
291 | freeKey = key.d->key; | - |
292 | key.d->isValid = false; | - |
293 | key.d->key = 0; | - |
294 | } | - |
295 | - | |
296 | void QPMCache::clear() | - |
297 | { | - |
298 | free(keyArray); | - |
299 | keyArray = 0; | - |
300 | freeKey = 0; | - |
301 | keyArraySize = 0; | - |
302 | - | |
303 | QList<QPixmapCache::Key> keys = QCache<QPixmapCache::Key, QPixmapCacheEntry>::keys(); | - |
304 | for (int i = 0; i < keys.size(); ++i) | - |
305 | keys.at(i).d->isValid = false; | - |
306 | QCache<QPixmapCache::Key, QPixmapCacheEntry>::clear(); | - |
307 | } | - |
308 | - | |
309 | QPixmapCache::KeyData* QPMCache::getKeyData(QPixmapCache::Key *key) | - |
310 | { | - |
311 | if (!key->d) | - |
312 | key->d = new QPixmapCache::KeyData; | - |
313 | return key->d; | - |
314 | } | - |
315 | - | |
316 | namespace { namespace Q_QGS_pm_cache { typedef QPMCache Type; QBasicAtomicInt guard = { QtGlobalStatic::Uninitialized }; __attribute__((visibility("hidden"))) inline Type *innerFunction() { struct HolderBase { ~HolderBase() noexcept { if (guard.load() == QtGlobalStatic::Initialized) guard.store(QtGlobalStatic::Destroyed); } }; static struct Holder : public HolderBase { Type value; Holder() noexcept(noexcept(Type ())) : value () { guard.store(QtGlobalStatic::Initialized); } } holder; return &holder.value; } } } static QGlobalStatic<QPMCache, Q_QGS_pm_cache::innerFunction, Q_QGS_pm_cache::guard> pm_cache; | - |
317 | - | |
318 | int __attribute__((visibility("default"))) q_QPixmapCache_keyHashSize() | - |
319 | { | - |
320 | return pm_cache()->size(); | - |
321 | } | - |
322 | - | |
323 | QPixmapCacheEntry::~QPixmapCacheEntry() | - |
324 | { | - |
325 | pm_cache()->releaseKey(key); | - |
326 | } | - |
327 | QPixmap *QPixmapCache::find(const QString &key) | - |
328 | { | - |
329 | return pm_cache()->object(key); | - |
330 | } | - |
331 | bool QPixmapCache::find(const QString &key, QPixmap& pixmap) | - |
332 | { | - |
333 | return find(key, &pixmap); | - |
334 | } | - |
335 | bool QPixmapCache::find(const QString &key, QPixmap* pixmap) | - |
336 | { | - |
337 | QPixmap *ptr = pm_cache()->object(key); | - |
338 | if (ptr && pixmap) | - |
339 | *pixmap = *ptr; | - |
340 | return ptr != 0; | - |
341 | } | - |
342 | bool QPixmapCache::find(const Key &key, QPixmap* pixmap) | - |
343 | { | - |
344 | - | |
345 | if (!key.d || !key.d->isValid) | - |
346 | return false; | - |
347 | QPixmap *ptr = pm_cache()->object(key); | - |
348 | if (ptr && pixmap) | - |
349 | *pixmap = *ptr; | - |
350 | return ptr != 0; | - |
351 | } | - |
352 | bool QPixmapCache::insert(const QString &key, const QPixmap &pixmap) | - |
353 | { | - |
354 | return pm_cache()->insert(key, pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8); | - |
355 | } | - |
356 | QPixmapCache::Key QPixmapCache::insert(const QPixmap &pixmap) | - |
357 | { | - |
358 | return pm_cache()->insert(pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8); | - |
359 | } | - |
360 | bool QPixmapCache::replace(const Key &key, const QPixmap &pixmap) | - |
361 | { | - |
362 | - | |
363 | if (!key.d || !key.d->isValid) | - |
364 | return false; | - |
365 | return pm_cache()->replace(key, pixmap, pixmap.width() * pixmap.height() * pixmap.depth() / 8); | - |
366 | } | - |
367 | int QPixmapCache::cacheLimit() | - |
368 | { | - |
369 | return cache_limit; | - |
370 | } | - |
371 | void QPixmapCache::setCacheLimit(int n) | - |
372 | { | - |
373 | cache_limit = n; | - |
374 | pm_cache()->setMaxCost(1024 * cache_limit); | - |
375 | } | - |
376 | - | |
377 | - | |
378 | - | |
379 | - | |
380 | void QPixmapCache::remove(const QString &key) | - |
381 | { | - |
382 | pm_cache()->remove(key); | - |
383 | } | - |
384 | - | |
385 | - | |
386 | - | |
387 | - | |
388 | - | |
389 | - | |
390 | - | |
391 | void QPixmapCache::remove(const Key &key) | - |
392 | { | - |
393 | - | |
394 | if (!key.d || !key.d->isValid) | - |
395 | return; | - |
396 | pm_cache()->remove(key); | - |
397 | } | - |
398 | - | |
399 | - | |
400 | - | |
401 | - | |
402 | - | |
403 | void QPixmapCache::clear() | - |
404 | { | - |
405 | if (true) { | - |
406 | if (pm_cache.exists()) | - |
407 | pm_cache->clear(); | - |
408 | } else { dead code: { } | - |
409 | - | |
410 | - | |
411 | } dead code: { } | - |
412 | } | - |
413 | - | |
414 | void QPixmapCache::flushDetachedPixmaps() | - |
415 | { | - |
416 | pm_cache()->flushDetachedPixmaps(true); | - |
417 | } | - |
418 | - | |
419 | int QPixmapCache::totalUsed() | - |
420 | { | - |
421 | return (pm_cache()->totalCost()+1023) / 1024; | - |
422 | } | - |
423 | - | |
Switch to Source code | Preprocessed file |