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

Generated by: LCOV version 1.11