LCOV - code coverage report
Current view: top level - sd/source/ui/slidesorter/cache - SlsPageCacheManager.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 122 161 75.8 %
Date: 2014-04-11 Functions: 32 36 88.9 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : 
      21             : #include "cache/SlsPageCacheManager.hxx"
      22             : 
      23             : #include "SlsBitmapCache.hxx"
      24             : #include "view/SlideSorterView.hxx"
      25             : #include "model/SlideSorterModel.hxx"
      26             : 
      27             : #include <deque>
      28             : #include <map>
      29             : #include <boost/weak_ptr.hpp>
      30             : 
      31             : namespace {
      32             : 
      33             : /** Collection of data that is stored for all active preview caches.
      34             : */
      35         335 : class CacheDescriptor
      36             : {
      37             : public:
      38             :     ::sd::slidesorter::cache::PageCacheManager::DocumentKey mpDocument;
      39             :     Size maPreviewSize;
      40             : 
      41          67 :     CacheDescriptor(
      42             :         ::sd::slidesorter::cache::PageCacheManager::DocumentKey pDocument,
      43             :         const Size& rPreviewSize)
      44          67 :         :mpDocument(pDocument),maPreviewSize(rPreviewSize)
      45          67 :     {}
      46             :     /// Test for equality with respect to all members.
      47           0 :     class Equal {public: bool operator() (
      48             :         const CacheDescriptor& rDescriptor1, const CacheDescriptor& rDescriptor2) const {
      49           0 :         return rDescriptor1.mpDocument==rDescriptor2.mpDocument
      50           0 :             && rDescriptor1.maPreviewSize==rDescriptor2.maPreviewSize;
      51             :     } };
      52             :     /// Hash function that takes all members into account.
      53         130 :     class Hash {public: size_t operator() (const CacheDescriptor& rDescriptor) const {
      54         130 :         return (size_t)rDescriptor.mpDocument.get() + rDescriptor.maPreviewSize.Width();
      55             :     } };
      56             : };
      57             : 
      58             : 
      59             : 
      60             : 
      61             : /** Collection of data that is stored for the inactive, recently used
      62             :     caches.
      63             : */
      64         189 : class RecentlyUsedCacheDescriptor
      65             : {
      66             : public:
      67             :     ::sd::slidesorter::cache::PageCacheManager::DocumentKey mpDocument;
      68             :     Size maPreviewSize;
      69             :     ::boost::shared_ptr< ::sd::slidesorter::cache::PageCacheManager::Cache> mpCache;
      70             : 
      71          63 :     RecentlyUsedCacheDescriptor(
      72             :         ::sd::slidesorter::cache::PageCacheManager::DocumentKey pDocument,
      73             :         const Size& rPreviewSize,
      74             :         const ::boost::shared_ptr< ::sd::slidesorter::cache::PageCacheManager::Cache>& rpCache)
      75          63 :         :mpDocument(pDocument),maPreviewSize(rPreviewSize),mpCache(rpCache)
      76          63 :     {}
      77             : };
      78             : 
      79             : 
      80             : 
      81             : 
      82             : /** The list of recently used caches is organized as queue.  When elements
      83             :     are added the list is shortened to the maximally allowed number of
      84             :     elements by removing the least recently used elements.
      85             : */
      86             : typedef ::std::deque<RecentlyUsedCacheDescriptor> RecentlyUsedQueue;
      87             : 
      88             : 
      89             : 
      90             : 
      91             : /** Compare the caches by preview size.  Those that match the given size
      92             :     come first, then, regardless of the given size, the largest ones before
      93             :     the smaller ones.
      94             : */
      95             : class BestFittingCacheComparer
      96             : {
      97             : public:
      98          63 :     BestFittingCacheComparer (const Size& rPreferredSize)
      99          63 :         : maPreferredSize(rPreferredSize)
     100          63 :     {}
     101           0 :     bool operator()(const ::sd::slidesorter::cache::PageCacheManager::BestFittingPageCaches::value_type& rElement1,
     102             :         const ::sd::slidesorter::cache::PageCacheManager::BestFittingPageCaches::value_type& rElement2)
     103             :     {
     104           0 :         if (rElement1.first == maPreferredSize)
     105           0 :             return true;
     106           0 :         else if (rElement2.first == maPreferredSize)
     107           0 :             return false;
     108             :         else
     109           0 :             return (rElement1.first.Width()*rElement1.first.Height()
     110           0 :                 > rElement2.first.Width()*rElement2.first.Height());
     111             :     }
     112             : 
     113             : private:
     114             :     Size maPreferredSize;
     115             : };
     116             : 
     117             : } // end of anonymous namespace
     118             : 
     119             : 
     120             : namespace sd { namespace slidesorter { namespace cache {
     121             : 
     122             : /** Container for the active caches.
     123             : */
     124          73 : class PageCacheManager::PageCacheContainer
     125             :     : public ::boost::unordered_map<CacheDescriptor,
     126             :                              ::boost::shared_ptr<PageCacheManager::Cache>,
     127             :                              CacheDescriptor::Hash,
     128             :                              CacheDescriptor::Equal>
     129             : {
     130             : public:
     131          73 :     PageCacheContainer (void) {}
     132             : 
     133             :     /** Compare entries in the cache container with respect to the cache
     134             :         address only.
     135             :     */
     136         201 :     class CompareWithCache { public:
     137          67 :         CompareWithCache(const ::boost::shared_ptr<PageCacheManager::Cache>& rpCache)
     138          67 :             : mpCache(rpCache) {}
     139          67 :         bool operator () (const PageCacheContainer::value_type& rValue) const
     140          67 :         { return rValue.second == mpCache; }
     141             :     private:
     142             :         ::boost::shared_ptr<PageCacheManager::Cache> mpCache;
     143             :     };
     144             : };
     145             : 
     146             : 
     147             : /** The recently used caches are stored in one queue for each document.
     148             : */
     149          73 : class PageCacheManager::RecentlyUsedPageCaches
     150             :     : public ::std::map<DocumentKey,RecentlyUsedQueue>
     151             : {
     152             : public:
     153          73 :     RecentlyUsedPageCaches (void) {};
     154             : };
     155             : 
     156             : 
     157             : 
     158             : 
     159             : class PageCacheManager::Deleter
     160             : {
     161             : public:
     162          73 :     void operator() (PageCacheManager* pObject) { delete pObject; }
     163             : };
     164             : 
     165             : 
     166             : 
     167             : //===== PageCacheManager ====================================================
     168             : 
     169          16 : ::boost::weak_ptr<PageCacheManager> PageCacheManager::mpInstance;
     170             : 
     171         678 : ::boost::shared_ptr<PageCacheManager> PageCacheManager::Instance (void)
     172             : {
     173         678 :     ::boost::shared_ptr<PageCacheManager> pInstance;
     174             : 
     175        1356 :     ::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
     176             : 
     177         678 :     pInstance = mpInstance.lock();
     178         678 :     if (pInstance.get() == NULL)
     179             :     {
     180         146 :         pInstance = ::boost::shared_ptr<PageCacheManager>(
     181           0 :             new PageCacheManager(),
     182          73 :             PageCacheManager::Deleter());
     183          73 :         mpInstance = pInstance;
     184             :     }
     185             : 
     186        1356 :     return pInstance;
     187             : }
     188             : 
     189             : 
     190             : 
     191             : 
     192          73 : PageCacheManager::PageCacheManager (void)
     193           0 :     : mpPageCaches(new PageCacheContainer()),
     194           0 :       mpRecentlyUsedPageCaches(new RecentlyUsedPageCaches()),
     195          73 :       mnMaximalRecentlyCacheCount(2)
     196             : {
     197          73 : }
     198             : 
     199             : 
     200             : 
     201             : 
     202          73 : PageCacheManager::~PageCacheManager (void)
     203             : {
     204          73 : }
     205             : 
     206             : 
     207             : 
     208             : 
     209          63 : ::boost::shared_ptr<PageCacheManager::Cache> PageCacheManager::GetCache (
     210             :     DocumentKey pDocument,
     211             :     const Size& rPreviewSize)
     212             : {
     213          63 :     ::boost::shared_ptr<Cache> pResult;
     214             : 
     215             :     // Look for the cache in the list of active caches.
     216         126 :     CacheDescriptor aKey (pDocument, rPreviewSize);
     217          63 :     PageCacheContainer::iterator iCache (mpPageCaches->find(aKey));
     218          63 :     if (iCache != mpPageCaches->end())
     219           0 :         pResult = iCache->second;
     220             : 
     221             :     // Look for the cache in the list of recently used caches.
     222          63 :     if (pResult.get() == NULL)
     223          63 :         pResult = GetRecentlyUsedCache(pDocument, rPreviewSize);
     224             : 
     225             :     // Create the cache when no suitable one does exist.
     226          63 :     if (pResult.get() == NULL)
     227          63 :         pResult.reset(new Cache());
     228             : 
     229             :     // The cache may be newly created and thus empty or is old and may
     230             :     // contain previews that are not up-to-date.  Recycle previews from
     231             :     // other caches to fill in the holes.
     232          63 :     Recycle(pResult, pDocument,rPreviewSize);
     233             : 
     234             :     // Put the new (or old) cache into the container.
     235          63 :     if (pResult.get() != NULL)
     236          63 :         mpPageCaches->insert(PageCacheContainer::value_type(aKey, pResult));
     237             : 
     238         126 :     return pResult;
     239             : }
     240             : 
     241             : 
     242             : 
     243             : 
     244          63 : void PageCacheManager::Recycle (
     245             :     const ::boost::shared_ptr<Cache>& rpCache,
     246             :     DocumentKey pDocument,
     247             :     const Size& rPreviewSize)
     248             : {
     249          63 :     BestFittingPageCaches aCaches;
     250             : 
     251             :     // Add bitmap caches from active caches.
     252          63 :     PageCacheContainer::iterator iActiveCache;
     253          63 :     for (iActiveCache=mpPageCaches->begin(); iActiveCache!=mpPageCaches->end(); ++iActiveCache)
     254             :     {
     255           0 :         if (iActiveCache->first.mpDocument == pDocument)
     256             :             aCaches.push_back(BestFittingPageCaches::value_type(
     257           0 :                 iActiveCache->first.maPreviewSize, iActiveCache->second));
     258             :     }
     259             : 
     260             :     // Add bitmap caches from recently used caches.
     261          63 :     RecentlyUsedPageCaches::iterator iQueue (mpRecentlyUsedPageCaches->find(pDocument));
     262          63 :     if (iQueue != mpRecentlyUsedPageCaches->end())
     263             :     {
     264           0 :         RecentlyUsedQueue::const_iterator iRecentCache;
     265           0 :         for (iRecentCache=iQueue->second.begin();iRecentCache!=iQueue->second.end();++iRecentCache)
     266             :             aCaches.push_back(BestFittingPageCaches::value_type(
     267           0 :                 iRecentCache->maPreviewSize, iRecentCache->mpCache));
     268             :     }
     269             : 
     270          63 :     ::std::sort(aCaches.begin(), aCaches.end(), BestFittingCacheComparer(rPreviewSize));
     271             : 
     272          63 :     BestFittingPageCaches::const_iterator iBestCache;
     273          63 :     for (iBestCache=aCaches.begin(); iBestCache!=aCaches.end(); ++iBestCache)
     274             :     {
     275           0 :         rpCache->Recycle(*iBestCache->second);
     276          63 :     }
     277          63 : }
     278             : 
     279             : 
     280             : 
     281             : 
     282          63 : void PageCacheManager::ReleaseCache (const ::boost::shared_ptr<Cache>& rpCache)
     283             : {
     284             :     PageCacheContainer::iterator iCache (::std::find_if(
     285          63 :         mpPageCaches->begin(),
     286          63 :         mpPageCaches->end(),
     287         189 :         PageCacheContainer::CompareWithCache(rpCache)));
     288             : 
     289          63 :     if (iCache != mpPageCaches->end())
     290             :     {
     291             :         OSL_ASSERT(iCache->second == rpCache);
     292             : 
     293          63 :         PutRecentlyUsedCache(iCache->first.mpDocument,iCache->first.maPreviewSize,rpCache);
     294             : 
     295          63 :         mpPageCaches->erase(iCache);
     296             :     }
     297          63 : }
     298             : 
     299             : 
     300             : 
     301             : 
     302           4 : ::boost::shared_ptr<PageCacheManager::Cache> PageCacheManager::ChangeSize (
     303             :     const ::boost::shared_ptr<Cache>& rpCache,
     304             :     const Size& rOldPreviewSize,
     305             :     const Size& rNewPreviewSize)
     306             : {
     307             :     (void)rOldPreviewSize;
     308             : 
     309           4 :     ::boost::shared_ptr<Cache> pResult;
     310             : 
     311           4 :     if (rpCache.get() != NULL)
     312             :     {
     313             :         // Look up the given cache in the list of active caches.
     314             :         PageCacheContainer::iterator iCacheToChange (::std::find_if(
     315           4 :             mpPageCaches->begin(),
     316           4 :             mpPageCaches->end(),
     317          12 :             PageCacheContainer::CompareWithCache(rpCache)));
     318           4 :         if (iCacheToChange != mpPageCaches->end())
     319             :         {
     320             :             OSL_ASSERT(iCacheToChange->second == rpCache);
     321             : 
     322             :             // Now, we can change the preview size of the existing one by
     323             :             // removing the cache from the list and re-insert it with the
     324             :             // updated size.
     325             :             const ::sd::slidesorter::cache::PageCacheManager::DocumentKey aKey (
     326           4 :                 iCacheToChange->first.mpDocument);
     327           4 :             mpPageCaches->erase(iCacheToChange);
     328           4 :             mpPageCaches->insert(PageCacheContainer::value_type(
     329             :                 CacheDescriptor(aKey,rNewPreviewSize),
     330           8 :                 rpCache));
     331             : 
     332           4 :             pResult = rpCache;
     333             :         }
     334             :         else
     335             :         {
     336             :             OSL_ASSERT(iCacheToChange != mpPageCaches->end());
     337             :         }
     338             :     }
     339             : 
     340           4 :     return pResult;
     341             : }
     342             : 
     343             : 
     344             : 
     345             : 
     346         497 : bool PageCacheManager::InvalidatePreviewBitmap (
     347             :     DocumentKey pDocument,
     348             :     const SdrPage* pKey)
     349             : {
     350         497 :     bool bHasChanged (false);
     351             : 
     352         497 :     if (pDocument!=NULL)
     353             :     {
     354             :         // Iterate over all caches that are currently in use and invalidate
     355             :         // the previews in those that belong to the document.
     356         497 :         PageCacheContainer::iterator iCache;
     357         994 :         for (iCache=mpPageCaches->begin(); iCache!=mpPageCaches->end();  ++iCache)
     358         497 :             if (iCache->first.mpDocument == pDocument)
     359         497 :                 bHasChanged |= iCache->second->InvalidateBitmap(pKey);
     360             : 
     361             :         // Invalidate the previews in the recently used caches belonging to
     362             :         // the given document.
     363         497 :         RecentlyUsedPageCaches::iterator iQueue (mpRecentlyUsedPageCaches->find(pDocument));
     364         497 :         if (iQueue != mpRecentlyUsedPageCaches->end())
     365             :         {
     366           0 :             RecentlyUsedQueue::const_iterator iCache2;
     367           0 :             for (iCache2=iQueue->second.begin(); iCache2!=iQueue->second.end(); ++iCache2)
     368           0 :                 bHasChanged |= iCache2->mpCache->InvalidateBitmap(pKey);
     369             :         }
     370             :     }
     371             : 
     372         497 :     return bHasChanged;
     373             : }
     374             : 
     375             : 
     376             : 
     377             : 
     378          47 : void PageCacheManager::InvalidateAllPreviewBitmaps (DocumentKey pDocument)
     379             : {
     380          47 :     if (pDocument == NULL)
     381          47 :         return;
     382             : 
     383             :     // Iterate over all caches that are currently in use and invalidate the
     384             :     // previews in those that belong to the document.
     385          47 :     PageCacheContainer::iterator iCache;
     386          94 :     for (iCache=mpPageCaches->begin(); iCache!=mpPageCaches->end();  ++iCache)
     387          47 :         if (iCache->first.mpDocument == pDocument)
     388          47 :             iCache->second->InvalidateCache();
     389             : 
     390             :     // Invalidate the previews in the recently used caches belonging to the
     391             :     // given document.
     392          47 :     RecentlyUsedPageCaches::iterator iQueue (mpRecentlyUsedPageCaches->find(pDocument));
     393          47 :     if (iQueue != mpRecentlyUsedPageCaches->end())
     394             :     {
     395           0 :         RecentlyUsedQueue::const_iterator iCache2;
     396           0 :         for (iCache2=iQueue->second.begin(); iCache2!=iQueue->second.end(); ++iCache2)
     397           0 :             iCache2->mpCache->InvalidateCache();
     398             :     }
     399             : }
     400             : 
     401             : 
     402             : 
     403             : 
     404           0 : void PageCacheManager::InvalidateAllCaches (void)
     405             : {
     406             :     // Iterate over all caches that are currently in use and invalidate
     407             :     // them.
     408           0 :     PageCacheContainer::iterator iCache;
     409           0 :     for (iCache=mpPageCaches->begin(); iCache!=mpPageCaches->end();  ++iCache)
     410           0 :         iCache->second->InvalidateCache();
     411             : 
     412             :     // Remove all recently used caches, there is not much sense in storing
     413             :     // invalidated and unused caches.
     414           0 :     mpRecentlyUsedPageCaches->clear();
     415           0 : }
     416             : 
     417             : 
     418             : 
     419             : 
     420           1 : void PageCacheManager::ReleasePreviewBitmap (const SdrPage* pPage)
     421             : {
     422           1 :     PageCacheContainer::iterator iCache;
     423           2 :     for (iCache=mpPageCaches->begin(); iCache!=mpPageCaches->end(); ++iCache)
     424           1 :         iCache->second->ReleaseBitmap(pPage);
     425           1 : }
     426             : 
     427             : 
     428             : 
     429             : 
     430          63 : ::boost::shared_ptr<PageCacheManager::Cache> PageCacheManager::GetRecentlyUsedCache (
     431             :     DocumentKey pDocument,
     432             :     const Size& rPreviewSize)
     433             : {
     434          63 :     ::boost::shared_ptr<Cache> pCache;
     435             : 
     436             :     // Look for the cache in the list of recently used caches.
     437          63 :     RecentlyUsedPageCaches::iterator iQueue (mpRecentlyUsedPageCaches->find(pDocument));
     438          63 :     if (iQueue != mpRecentlyUsedPageCaches->end())
     439             :     {
     440           0 :         RecentlyUsedQueue::iterator iCache;
     441           0 :         for (iCache=iQueue->second.begin(); iCache!= iQueue->second.end(); ++iCache)
     442           0 :             if (iCache->maPreviewSize == rPreviewSize)
     443             :             {
     444           0 :                 pCache = iCache->mpCache;
     445           0 :                 iQueue->second.erase(iCache);
     446           0 :                 break;
     447             :             }
     448             :     }
     449             : 
     450          63 :     return pCache;
     451             : }
     452             : 
     453             : 
     454             : 
     455             : 
     456          63 : void PageCacheManager::PutRecentlyUsedCache(
     457             :     DocumentKey pDocument,
     458             :     const Size& rPreviewSize,
     459             :     const ::boost::shared_ptr<Cache>& rpCache)
     460             : {
     461             :     // Look up the list of recently used caches for the given document.
     462          63 :     RecentlyUsedPageCaches::iterator iQueue (mpRecentlyUsedPageCaches->find(pDocument));
     463          63 :     if (iQueue == mpRecentlyUsedPageCaches->end())
     464          63 :         iQueue = mpRecentlyUsedPageCaches->insert(
     465             :             RecentlyUsedPageCaches::value_type(pDocument, RecentlyUsedQueue())
     466         126 :             ).first;
     467             : 
     468          63 :     if (iQueue != mpRecentlyUsedPageCaches->end())
     469             :     {
     470          63 :         iQueue->second.push_front(RecentlyUsedCacheDescriptor(pDocument,rPreviewSize,rpCache));
     471             :         // Shorten the list of recently used caches to the allowed maximal length.
     472         126 :         while (iQueue->second.size() > mnMaximalRecentlyCacheCount)
     473           0 :             iQueue->second.pop_back();
     474             :     }
     475          63 : }
     476             : 
     477             : 
     478             : 
     479          48 : } } } // end of namespace ::sd::slidesorter::cache
     480             : 
     481             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10