src/gui/image/qpixmapcache.cpp
changeset 33 3e2da88830cd
parent 30 5dc02b23752f
equal deleted inserted replaced
30:5dc02b23752f 33:3e2da88830cd
   194     {return key.d;}
   194     {return key.d;}
   195 
   195 
   196     static QPixmapCache::KeyData* getKeyData(QPixmapCache::Key *key);
   196     static QPixmapCache::KeyData* getKeyData(QPixmapCache::Key *key);
   197 
   197 
   198     QList< QPair<QString,QPixmap> > allPixmaps() const;
   198     QList< QPair<QString,QPixmap> > allPixmaps() const;
   199     void flushDetachedPixmaps(bool nt);
   199     bool flushDetachedPixmaps(bool nt);
   200 
   200 
   201 private:
   201 private:
       
   202     enum { soon_time = 10000, flush_time = 30000 };
   202     int *keyArray;
   203     int *keyArray;
   203     int theid;
   204     int theid;
   204     int ps;
   205     int ps;
   205     int keyArraySize;
   206     int keyArraySize;
   206     int freeKey;
   207     int freeKey;
   234   minute once the application becomes idle, to let any inserted pixmap
   235   minute once the application becomes idle, to let any inserted pixmap
   235   remain in the cache for some time before it becomes a candidate for
   236   remain in the cache for some time before it becomes a candidate for
   236   cleaning-up, and to not cut down the size of the cache while the
   237   cleaning-up, and to not cut down the size of the cache while the
   237   cache is in active use.
   238   cache is in active use.
   238 
   239 
   239   When the last pixmap has been deleted from the cache, kill the
   240   When the last detached pixmap has been deleted from the cache, kill the
   240   timer so Qt won't keep the CPU from going into sleep mode.
   241   timer so Qt won't keep the CPU from going into sleep mode. Currently
   241 */
   242   the timer is not restarted when the pixmap becomes unused, but it does
   242 void QPMCache::flushDetachedPixmaps(bool nt)
   243   restart once something else is added (i.e. the cache space is actually needed).
       
   244 
       
   245   Returns true if any were removed.
       
   246 */
       
   247 bool QPMCache::flushDetachedPixmaps(bool nt)
   243 {
   248 {
   244     int mc = maxCost();
   249     int mc = maxCost();
   245     setMaxCost(nt ? totalCost() * 3 / 4 : totalCost() -1);
   250     setMaxCost(nt ? totalCost() * 3 / 4 : totalCost() -1);
   246     setMaxCost(mc);
   251     setMaxCost(mc);
   247     ps = totalCost();
   252     ps = totalCost();
   248 
   253 
       
   254     bool any = false;
   249     QHash<QString, QPixmapCache::Key>::iterator it = cacheKeys.begin();
   255     QHash<QString, QPixmapCache::Key>::iterator it = cacheKeys.begin();
   250     while (it != cacheKeys.end()) {
   256     while (it != cacheKeys.end()) {
   251         if (!contains(it.value())) {
   257         if (!contains(it.value())) {
   252             releaseKey(it.value());
   258             releaseKey(it.value());
   253             it = cacheKeys.erase(it);
   259             it = cacheKeys.erase(it);
       
   260             any = true;
   254         } else {
   261         } else {
   255             ++it;
   262             ++it;
   256         }
   263         }
   257     }
   264     }
       
   265 
       
   266     return any;
   258 }
   267 }
   259 
   268 
   260 void QPMCache::timerEvent(QTimerEvent *)
   269 void QPMCache::timerEvent(QTimerEvent *)
   261 {
   270 {
   262     bool nt = totalCost() == ps;
   271     bool nt = totalCost() == ps;
   263     flushDetachedPixmaps(nt);
   272     if (!flushDetachedPixmaps(nt)) {
   264 
       
   265     if (!size()) {
       
   266         killTimer(theid);
   273         killTimer(theid);
   267         theid = 0;
   274         theid = 0;
   268     } else if (nt != t) {
   275     } else if (nt != t) {
   269         killTimer(theid);
   276         killTimer(theid);
   270         theid = startTimer(nt ? 10000 : 30000);
   277         theid = startTimer(nt ? soon_time : flush_time);
   271         t = nt;
   278         t = nt;
   272     }
   279     }
   273 }
   280 }
   274 
   281 
   275 
   282 
   313 
   320 
   314     bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost);
   321     bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost);
   315     if (success) {
   322     if (success) {
   316         cacheKeys.insert(key, cacheKey);
   323         cacheKeys.insert(key, cacheKey);
   317         if (!theid) {
   324         if (!theid) {
   318             theid = startTimer(30000);
   325             theid = startTimer(flush_time);
   319             t = false;
   326             t = false;
   320         }
   327         }
   321     } else {
   328     } else {
   322         //Insertion failed we released the new allocated key
   329         //Insertion failed we released the new allocated key
   323         releaseKey(cacheKey);
   330         releaseKey(cacheKey);
   329 {
   336 {
   330     QPixmapCache::Key cacheKey = createKey();
   337     QPixmapCache::Key cacheKey = createKey();
   331     bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost);
   338     bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost);
   332     if (success) {
   339     if (success) {
   333         if (!theid) {
   340         if (!theid) {
   334             theid = startTimer(30000);
   341             theid = startTimer(flush_time);
   335             t = false;
   342             t = false;
   336         }
   343         }
   337     } else {
   344     } else {
   338         //Insertion failed we released the key and return an invalid one
   345         //Insertion failed we released the key and return an invalid one
   339         releaseKey(cacheKey);
   346         releaseKey(cacheKey);
   350     QPixmapCache::Key cacheKey = createKey();
   357     QPixmapCache::Key cacheKey = createKey();
   351 
   358 
   352     bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost);
   359     bool success = QCache<QPixmapCache::Key, QPixmapCacheEntry>::insert(cacheKey, new QPixmapCacheEntry(cacheKey, pixmap), cost);
   353     if (success) {
   360     if (success) {
   354         if(!theid) {
   361         if(!theid) {
   355             theid = startTimer(30000);
   362             theid = startTimer(flush_time);
   356             t = false;
   363             t = false;
   357         }
   364         }
   358         const_cast<QPixmapCache::Key&>(key) = cacheKey;
   365         const_cast<QPixmapCache::Key&>(key) = cacheKey;
   359     } else {
   366     } else {
   360         //Insertion failed we released the key
   367         //Insertion failed we released the key