src/gui/image/qpixmapcache.cpp
branchGCC_SURGE
changeset 31 5daf16870df6
parent 30 5dc02b23752f
child 33 3e2da88830cd
equal deleted inserted replaced
27:93b982ccede2 31:5daf16870df6
    37 **
    37 **
    38 ** $QT_END_LICENSE$
    38 ** $QT_END_LICENSE$
    39 **
    39 **
    40 ****************************************************************************/
    40 ****************************************************************************/
    41 
    41 
       
    42 #define Q_TEST_QPIXMAPCACHE
    42 #include "qpixmapcache.h"
    43 #include "qpixmapcache.h"
    43 #include "qobject.h"
    44 #include "qobject.h"
    44 #include "qdebug.h"
    45 #include "qdebug.h"
    45 #include "qpixmapcache_p.h"
    46 #include "qpixmapcache_p.h"
    46 
    47 
   191 
   192 
   192     static inline QPixmapCache::KeyData *get(const QPixmapCache::Key &key)
   193     static inline QPixmapCache::KeyData *get(const QPixmapCache::Key &key)
   193     {return key.d;}
   194     {return key.d;}
   194 
   195 
   195     static QPixmapCache::KeyData* getKeyData(QPixmapCache::Key *key);
   196     static QPixmapCache::KeyData* getKeyData(QPixmapCache::Key *key);
       
   197 
       
   198     QList< QPair<QString,QPixmap> > allPixmaps() const;
       
   199     void flushDetachedPixmaps(bool nt);
   196 
   200 
   197 private:
   201 private:
   198     int *keyArray;
   202     int *keyArray;
   199     int theid;
   203     int theid;
   200     int ps;
   204     int ps;
   233   cache is in active use.
   237   cache is in active use.
   234 
   238 
   235   When the last pixmap has been deleted from the cache, kill the
   239   When the last pixmap has been deleted from the cache, kill the
   236   timer so Qt won't keep the CPU from going into sleep mode.
   240   timer so Qt won't keep the CPU from going into sleep mode.
   237 */
   241 */
   238 void QPMCache::timerEvent(QTimerEvent *)
   242 void QPMCache::flushDetachedPixmaps(bool nt)
   239 {
   243 {
   240     int mc = maxCost();
   244     int mc = maxCost();
   241     bool nt = totalCost() == ps;
       
   242     setMaxCost(nt ? totalCost() * 3 / 4 : totalCost() -1);
   245     setMaxCost(nt ? totalCost() * 3 / 4 : totalCost() -1);
   243     setMaxCost(mc);
   246     setMaxCost(mc);
   244     ps = totalCost();
   247     ps = totalCost();
   245 
   248 
   246     QHash<QString, QPixmapCache::Key>::iterator it = cacheKeys.begin();
   249     QHash<QString, QPixmapCache::Key>::iterator it = cacheKeys.begin();
   250             it = cacheKeys.erase(it);
   253             it = cacheKeys.erase(it);
   251         } else {
   254         } else {
   252             ++it;
   255             ++it;
   253         }
   256         }
   254     }
   257     }
       
   258 }
       
   259 
       
   260 void QPMCache::timerEvent(QTimerEvent *)
       
   261 {
       
   262     bool nt = totalCost() == ps;
       
   263     flushDetachedPixmaps(nt);
   255 
   264 
   256     if (!size()) {
   265     if (!size()) {
   257         killTimer(theid);
   266         killTimer(theid);
   258         theid = 0;
   267         theid = 0;
   259     } else if (nt != t) {
   268     } else if (nt != t) {
   260         killTimer(theid);
   269         killTimer(theid);
   261         theid = startTimer(nt ? 10000 : 30000);
   270         theid = startTimer(nt ? 10000 : 30000);
   262         t = nt;
   271         t = nt;
   263     }
   272     }
   264 }
   273 }
       
   274 
   265 
   275 
   266 QPixmap *QPMCache::object(const QString &key) const
   276 QPixmap *QPMCache::object(const QString &key) const
   267 {
   277 {
   268     QPixmapCache::Key cacheKey = cacheKeys.value(key);
   278     QPixmapCache::Key cacheKey = cacheKeys.value(key);
   269     if (!cacheKey.d || !cacheKey.d->isValid) {
   279     if (!cacheKey.d || !cacheKey.d->isValid) {
   420     if (!key->d)
   430     if (!key->d)
   421         key->d = new QPixmapCache::KeyData;
   431         key->d = new QPixmapCache::KeyData;
   422     return key->d;
   432     return key->d;
   423 }
   433 }
   424 
   434 
       
   435 QList< QPair<QString,QPixmap> > QPMCache::allPixmaps() const
       
   436 {
       
   437     QList< QPair<QString,QPixmap> > r;
       
   438     QHash<QString, QPixmapCache::Key>::const_iterator it = cacheKeys.begin();
       
   439     while (it != cacheKeys.end()) {
       
   440         QPixmap *ptr = QCache<QPixmapCache::Key, QPixmapCacheEntry>::object(it.value());
       
   441         if (ptr)
       
   442             r.append(QPair<QString,QPixmap>(it.key(),*ptr));
       
   443         ++it;
       
   444     }
       
   445     return r;
       
   446 }
       
   447 
       
   448 
   425 Q_GLOBAL_STATIC(QPMCache, pm_cache)
   449 Q_GLOBAL_STATIC(QPMCache, pm_cache)
   426 
   450 
   427 int Q_AUTOTEST_EXPORT q_QPixmapCache_keyHashSize()
   451 int Q_AUTOTEST_EXPORT q_QPixmapCache_keyHashSize()
   428 {
   452 {
   429     return pm_cache()->size();
   453     return pm_cache()->size();
   631         // if we ran out of memory during pm_cache(), it's no leak,
   655         // if we ran out of memory during pm_cache(), it's no leak,
   632         // so just ignore it.
   656         // so just ignore it.
   633     }
   657     }
   634 }
   658 }
   635 
   659 
       
   660 void QPixmapCache::flushDetachedPixmaps()
       
   661 {
       
   662     pm_cache()->flushDetachedPixmaps(true);
       
   663 }
       
   664 
       
   665 int QPixmapCache::totalUsed()
       
   666 {
       
   667     return (pm_cache()->totalCost()+1023) / 1024;
       
   668 }
       
   669 
       
   670 QList< QPair<QString,QPixmap> > QPixmapCache::allPixmaps()
       
   671 {
       
   672     return pm_cache()->allPixmaps();
       
   673 }
       
   674 
   636 QT_END_NAMESPACE
   675 QT_END_NAMESPACE