LCOV - code coverage report
Current view: top level - sd/source/ui/slidesorter/cache - SlsPageCacheManager.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 122 161 75.8 %
Date: 2014-11-03 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             : #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         705 : class CacheDescriptor
      35             : {
      36             : public:
      37             :     ::sd::slidesorter::cache::PageCacheManager::DocumentKey mpDocument;
      38             :     Size maPreviewSize;
      39             : 
      40         141 :     CacheDescriptor(
      41             :         ::sd::slidesorter::cache::PageCacheManager::DocumentKey pDocument,
      42             :         const Size& rPreviewSize)
      43         141 :         :mpDocument(pDocument),maPreviewSize(rPreviewSize)
      44         141 :     {}
      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         253 :     class Hash {public: size_t operator() (const CacheDescriptor& rDescriptor) const {
      53         253 :         return (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         336 : 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         112 :     RecentlyUsedCacheDescriptor(
      68             :         ::sd::slidesorter::cache::PageCacheManager::DocumentKey pDocument,
      69             :         const Size& rPreviewSize,
      70             :         const ::boost::shared_ptr< ::sd::slidesorter::cache::PageCacheManager::Cache>& rpCache)
      71         112 :         :mpDocument(pDocument),maPreviewSize(rPreviewSize),mpCache(rpCache)
      72         112 :     {}
      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         112 :     BestFittingCacheComparer (const Size& rPreferredSize)
      89         112 :         : maPreferredSize(rPreferredSize)
      90         112 :     {}
      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         212 : class PageCacheManager::PageCacheContainer
     114             :     : public ::boost::unordered_map<CacheDescriptor,
     115             :                              ::boost::shared_ptr<PageCacheManager::Cache>,
     116             :                              CacheDescriptor::Hash,
     117             :                              CacheDescriptor::Equal>
     118             : {
     119             : public:
     120         212 :     PageCacheContainer (void) {}
     121             : 
     122             :     /** Compare entries in the cache container with respect to the cache
     123             :         address only.
     124             :     */
     125        1269 :     class CompareWithCache { public:
     126         141 :         CompareWithCache(const ::boost::shared_ptr<PageCacheManager::Cache>& rpCache)
     127         141 :             : mpCache(rpCache) {}
     128         141 :         bool operator () (const PageCacheContainer::value_type& rValue) const
     129         141 :         { 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         212 : class PageCacheManager::RecentlyUsedPageCaches
     138             :     : public ::std::map<DocumentKey,RecentlyUsedQueue>
     139             : {
     140             : public:
     141         212 :     RecentlyUsedPageCaches (void) {};
     142             : };
     143             : 
     144             : class PageCacheManager::Deleter
     145             : {
     146             : public:
     147         212 :     void operator() (PageCacheManager* pObject) { delete pObject; }
     148             : };
     149             : 
     150             : //===== PageCacheManager ====================================================
     151             : 
     152          38 : ::boost::weak_ptr<PageCacheManager> PageCacheManager::mpInstance;
     153             : 
     154        1413 : ::boost::shared_ptr<PageCacheManager> PageCacheManager::Instance (void)
     155             : {
     156        1413 :     ::boost::shared_ptr<PageCacheManager> pInstance;
     157             : 
     158        2826 :     ::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
     159             : 
     160        1413 :     pInstance = mpInstance.lock();
     161        1413 :     if (pInstance.get() == NULL)
     162             :     {
     163         424 :         pInstance = ::boost::shared_ptr<PageCacheManager>(
     164           0 :             new PageCacheManager(),
     165         212 :             PageCacheManager::Deleter());
     166         212 :         mpInstance = pInstance;
     167             :     }
     168             : 
     169        2826 :     return pInstance;
     170             : }
     171             : 
     172         212 : PageCacheManager::PageCacheManager (void)
     173           0 :     : mpPageCaches(new PageCacheContainer()),
     174           0 :       mpRecentlyUsedPageCaches(new RecentlyUsedPageCaches()),
     175         212 :       mnMaximalRecentlyCacheCount(2)
     176             : {
     177         212 : }
     178             : 
     179         212 : PageCacheManager::~PageCacheManager (void)
     180             : {
     181         212 : }
     182             : 
     183         112 : ::boost::shared_ptr<PageCacheManager::Cache> PageCacheManager::GetCache (
     184             :     DocumentKey pDocument,
     185             :     const Size& rPreviewSize)
     186             : {
     187         112 :     ::boost::shared_ptr<Cache> pResult;
     188             : 
     189             :     // Look for the cache in the list of active caches.
     190         224 :     CacheDescriptor aKey (pDocument, rPreviewSize);
     191         112 :     PageCacheContainer::iterator iCache (mpPageCaches->find(aKey));
     192         112 :     if (iCache != mpPageCaches->end())
     193           0 :         pResult = iCache->second;
     194             : 
     195             :     // Look for the cache in the list of recently used caches.
     196         112 :     if (pResult.get() == NULL)
     197         112 :         pResult = GetRecentlyUsedCache(pDocument, rPreviewSize);
     198             : 
     199             :     // Create the cache when no suitable one does exist.
     200         112 :     if (pResult.get() == NULL)
     201         112 :         pResult.reset(new Cache());
     202             : 
     203             :     // The cache may be newly created and thus empty or is old and may
     204             :     // contain previews that are not up-to-date.  Recycle previews from
     205             :     // other caches to fill in the holes.
     206         112 :     Recycle(pResult, pDocument,rPreviewSize);
     207             : 
     208             :     // Put the new (or old) cache into the container.
     209         112 :     if (pResult.get() != NULL)
     210         112 :         mpPageCaches->insert(PageCacheContainer::value_type(aKey, pResult));
     211             : 
     212         224 :     return pResult;
     213             : }
     214             : 
     215         112 : void PageCacheManager::Recycle (
     216             :     const ::boost::shared_ptr<Cache>& rpCache,
     217             :     DocumentKey pDocument,
     218             :     const Size& rPreviewSize)
     219             : {
     220         112 :     BestFittingPageCaches aCaches;
     221             : 
     222             :     // Add bitmap caches from active caches.
     223         112 :     PageCacheContainer::iterator iActiveCache;
     224         112 :     for (iActiveCache=mpPageCaches->begin(); iActiveCache!=mpPageCaches->end(); ++iActiveCache)
     225             :     {
     226           0 :         if (iActiveCache->first.mpDocument == pDocument)
     227             :             aCaches.push_back(BestFittingPageCaches::value_type(
     228           0 :                 iActiveCache->first.maPreviewSize, iActiveCache->second));
     229             :     }
     230             : 
     231             :     // Add bitmap caches from recently used caches.
     232         112 :     RecentlyUsedPageCaches::iterator iQueue (mpRecentlyUsedPageCaches->find(pDocument));
     233         112 :     if (iQueue != mpRecentlyUsedPageCaches->end())
     234             :     {
     235           0 :         RecentlyUsedQueue::const_iterator iRecentCache;
     236           0 :         for (iRecentCache=iQueue->second.begin();iRecentCache!=iQueue->second.end();++iRecentCache)
     237             :             aCaches.push_back(BestFittingPageCaches::value_type(
     238           0 :                 iRecentCache->maPreviewSize, iRecentCache->mpCache));
     239             :     }
     240             : 
     241         112 :     ::std::sort(aCaches.begin(), aCaches.end(), BestFittingCacheComparer(rPreviewSize));
     242             : 
     243         112 :     BestFittingPageCaches::const_iterator iBestCache;
     244         112 :     for (iBestCache=aCaches.begin(); iBestCache!=aCaches.end(); ++iBestCache)
     245             :     {
     246           0 :         rpCache->Recycle(*iBestCache->second);
     247         112 :     }
     248         112 : }
     249             : 
     250         112 : void PageCacheManager::ReleaseCache (const ::boost::shared_ptr<Cache>& rpCache)
     251             : {
     252             :     PageCacheContainer::iterator iCache (::std::find_if(
     253         112 :         mpPageCaches->begin(),
     254         112 :         mpPageCaches->end(),
     255         336 :         PageCacheContainer::CompareWithCache(rpCache)));
     256             : 
     257         112 :     if (iCache != mpPageCaches->end())
     258             :     {
     259             :         OSL_ASSERT(iCache->second == rpCache);
     260             : 
     261         112 :         PutRecentlyUsedCache(iCache->first.mpDocument,iCache->first.maPreviewSize,rpCache);
     262             : 
     263         112 :         mpPageCaches->erase(iCache);
     264             :     }
     265         112 : }
     266             : 
     267          29 : ::boost::shared_ptr<PageCacheManager::Cache> PageCacheManager::ChangeSize (
     268             :     const ::boost::shared_ptr<Cache>& rpCache,
     269             :     const Size& rOldPreviewSize,
     270             :     const Size& rNewPreviewSize)
     271             : {
     272             :     (void)rOldPreviewSize;
     273             : 
     274          29 :     ::boost::shared_ptr<Cache> pResult;
     275             : 
     276          29 :     if (rpCache.get() != NULL)
     277             :     {
     278             :         // Look up the given cache in the list of active caches.
     279             :         PageCacheContainer::iterator iCacheToChange (::std::find_if(
     280          29 :             mpPageCaches->begin(),
     281          29 :             mpPageCaches->end(),
     282          87 :             PageCacheContainer::CompareWithCache(rpCache)));
     283          29 :         if (iCacheToChange != mpPageCaches->end())
     284             :         {
     285             :             OSL_ASSERT(iCacheToChange->second == rpCache);
     286             : 
     287             :             // Now, we can change the preview size of the existing one by
     288             :             // removing the cache from the list and re-insert it with the
     289             :             // updated size.
     290             :             const ::sd::slidesorter::cache::PageCacheManager::DocumentKey aKey (
     291          29 :                 iCacheToChange->first.mpDocument);
     292          29 :             mpPageCaches->erase(iCacheToChange);
     293          29 :             mpPageCaches->insert(PageCacheContainer::value_type(
     294             :                 CacheDescriptor(aKey,rNewPreviewSize),
     295          58 :                 rpCache));
     296             : 
     297          29 :             pResult = rpCache;
     298             :         }
     299             :         else
     300             :         {
     301             :             OSL_ASSERT(iCacheToChange != mpPageCaches->end());
     302             :         }
     303             :     }
     304             : 
     305          29 :     return pResult;
     306             : }
     307             : 
     308         992 : bool PageCacheManager::InvalidatePreviewBitmap (
     309             :     DocumentKey pDocument,
     310             :     const SdrPage* pKey)
     311             : {
     312         992 :     bool bHasChanged (false);
     313             : 
     314         992 :     if (pDocument!=NULL)
     315             :     {
     316             :         // Iterate over all caches that are currently in use and invalidate
     317             :         // the previews in those that belong to the document.
     318         992 :         PageCacheContainer::iterator iCache;
     319        1959 :         for (iCache=mpPageCaches->begin(); iCache!=mpPageCaches->end();  ++iCache)
     320         967 :             if (iCache->first.mpDocument == pDocument)
     321         967 :                 bHasChanged |= iCache->second->InvalidateBitmap(pKey);
     322             : 
     323             :         // Invalidate the previews in the recently used caches belonging to
     324             :         // the given document.
     325         992 :         RecentlyUsedPageCaches::iterator iQueue (mpRecentlyUsedPageCaches->find(pDocument));
     326         992 :         if (iQueue != mpRecentlyUsedPageCaches->end())
     327             :         {
     328           0 :             RecentlyUsedQueue::const_iterator iCache2;
     329           0 :             for (iCache2=iQueue->second.begin(); iCache2!=iQueue->second.end(); ++iCache2)
     330           0 :                 bHasChanged |= iCache2->mpCache->InvalidateBitmap(pKey);
     331             :         }
     332             :     }
     333             : 
     334         992 :     return bHasChanged;
     335             : }
     336             : 
     337          94 : void PageCacheManager::InvalidateAllPreviewBitmaps (DocumentKey pDocument)
     338             : {
     339          94 :     if (pDocument == NULL)
     340          94 :         return;
     341             : 
     342             :     // Iterate over all caches that are currently in use and invalidate the
     343             :     // previews in those that belong to the document.
     344          94 :     PageCacheContainer::iterator iCache;
     345         177 :     for (iCache=mpPageCaches->begin(); iCache!=mpPageCaches->end();  ++iCache)
     346          83 :         if (iCache->first.mpDocument == pDocument)
     347          83 :             iCache->second->InvalidateCache();
     348             : 
     349             :     // Invalidate the previews in the recently used caches belonging to the
     350             :     // given document.
     351          94 :     RecentlyUsedPageCaches::iterator iQueue (mpRecentlyUsedPageCaches->find(pDocument));
     352          94 :     if (iQueue != mpRecentlyUsedPageCaches->end())
     353             :     {
     354           0 :         RecentlyUsedQueue::const_iterator iCache2;
     355           0 :         for (iCache2=iQueue->second.begin(); iCache2!=iQueue->second.end(); ++iCache2)
     356           0 :             iCache2->mpCache->InvalidateCache();
     357             :     }
     358             : }
     359             : 
     360           0 : void PageCacheManager::InvalidateAllCaches (void)
     361             : {
     362             :     // Iterate over all caches that are currently in use and invalidate
     363             :     // them.
     364           0 :     PageCacheContainer::iterator iCache;
     365           0 :     for (iCache=mpPageCaches->begin(); iCache!=mpPageCaches->end();  ++iCache)
     366           0 :         iCache->second->InvalidateCache();
     367             : 
     368             :     // Remove all recently used caches, there is not much sense in storing
     369             :     // invalidated and unused caches.
     370           0 :     mpRecentlyUsedPageCaches->clear();
     371           0 : }
     372             : 
     373           2 : void PageCacheManager::ReleasePreviewBitmap (const SdrPage* pPage)
     374             : {
     375           2 :     PageCacheContainer::iterator iCache;
     376           4 :     for (iCache=mpPageCaches->begin(); iCache!=mpPageCaches->end(); ++iCache)
     377           2 :         iCache->second->ReleaseBitmap(pPage);
     378           2 : }
     379             : 
     380         112 : ::boost::shared_ptr<PageCacheManager::Cache> PageCacheManager::GetRecentlyUsedCache (
     381             :     DocumentKey pDocument,
     382             :     const Size& rPreviewSize)
     383             : {
     384         112 :     ::boost::shared_ptr<Cache> pCache;
     385             : 
     386             :     // Look for the cache in the list of recently used caches.
     387         112 :     RecentlyUsedPageCaches::iterator iQueue (mpRecentlyUsedPageCaches->find(pDocument));
     388         112 :     if (iQueue != mpRecentlyUsedPageCaches->end())
     389             :     {
     390           0 :         RecentlyUsedQueue::iterator iCache;
     391           0 :         for (iCache=iQueue->second.begin(); iCache!= iQueue->second.end(); ++iCache)
     392           0 :             if (iCache->maPreviewSize == rPreviewSize)
     393             :             {
     394           0 :                 pCache = iCache->mpCache;
     395           0 :                 iQueue->second.erase(iCache);
     396           0 :                 break;
     397             :             }
     398             :     }
     399             : 
     400         112 :     return pCache;
     401             : }
     402             : 
     403         112 : void PageCacheManager::PutRecentlyUsedCache(
     404             :     DocumentKey pDocument,
     405             :     const Size& rPreviewSize,
     406             :     const ::boost::shared_ptr<Cache>& rpCache)
     407             : {
     408             :     // Look up the list of recently used caches for the given document.
     409         112 :     RecentlyUsedPageCaches::iterator iQueue (mpRecentlyUsedPageCaches->find(pDocument));
     410         112 :     if (iQueue == mpRecentlyUsedPageCaches->end())
     411         112 :         iQueue = mpRecentlyUsedPageCaches->insert(
     412             :             RecentlyUsedPageCaches::value_type(pDocument, RecentlyUsedQueue())
     413         224 :             ).first;
     414             : 
     415         112 :     if (iQueue != mpRecentlyUsedPageCaches->end())
     416             :     {
     417         112 :         iQueue->second.push_front(RecentlyUsedCacheDescriptor(pDocument,rPreviewSize,rpCache));
     418             :         // Shorten the list of recently used caches to the allowed maximal length.
     419         224 :         while (iQueue->second.size() > mnMaximalRecentlyCacheCount)
     420           0 :             iQueue->second.pop_back();
     421             :     }
     422         112 : }
     423             : 
     424         114 : } } } // end of namespace ::sd::slidesorter::cache
     425             : 
     426             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10