27#include <unordered_map>
44 const Size& rPreviewSize)
45 :mpDocument(
std::move(pDocument)),maPreviewSize(rPreviewSize)
48 class Equal {
public:
bool operator() (
49 const CacheDescriptor& rDescriptor1,
const CacheDescriptor& rDescriptor2)
const {
50 return rDescriptor1.mpDocument==rDescriptor2.mpDocument
51 && rDescriptor1.maPreviewSize==rDescriptor2.maPreviewSize;
54 class Hash {
public:
size_t operator() (
const CacheDescriptor& rDescriptor)
const {
55 return reinterpret_cast<size_t>(rDescriptor.mpDocument.get()) + rDescriptor.maPreviewSize.Width();
62class RecentlyUsedCacheDescriptor
66 std::shared_ptr< ::sd::slidesorter::cache::BitmapCache> mpCache;
68 RecentlyUsedCacheDescriptor(
69 const Size& rPreviewSize,
70 std::shared_ptr< ::sd::slidesorter::cache::BitmapCache> pCache)
71 :maPreviewSize(rPreviewSize),mpCache(
std::move(pCache))
79typedef ::std::deque<RecentlyUsedCacheDescriptor> RecentlyUsedQueue;
85class BestFittingCacheComparer
88 explicit BestFittingCacheComparer (
const Size& rPreferredSize)
89 : maPreferredSize(rPreferredSize)
91 bool operator()(const ::sd::slidesorter::cache::PageCacheManager::BestFittingPageCaches::value_type& rElement1,
92 const ::sd::slidesorter::cache::PageCacheManager::BestFittingPageCaches::value_type& rElement2)
94 if (rElement2.first == maPreferredSize)
96 else if (rElement1.first == maPreferredSize)
99 return (rElement1.first.Width()*rElement1.first.Height()
100 > rElement2.first.Width()*rElement2.first.Height());
104 Size maPreferredSize;
114 :
public std::unordered_map<CacheDescriptor,
115 std::shared_ptr<BitmapCache>,
116 CacheDescriptor::Hash,
117 CacheDescriptor::Equal>
128 bool operator () (
const PageCacheContainer::value_type& rValue)
const
129 {
return rValue.second ==
mpCache; }
142 typedef std::map<key_type,mapped_type>::iterator
iterator;
144 std::map<key_type,mapped_type>
maMap;
151 template<
class... Args>
152 std::pair<iterator,bool>
emplace(Args&&... args) {
return maMap.emplace(std::forward<Args>(
args)...); }
167 std::shared_ptr<PageCacheManager> pInstance;
169 ::osl::MutexGuard aGuard (::osl::Mutex::getGlobalMutex());
172 if (pInstance ==
nullptr)
174 pInstance = std::shared_ptr<PageCacheManager>(
195 const Size& rPreviewSize)
197 std::shared_ptr<BitmapCache> pResult;
200 CacheDescriptor aKey (pDocument, rPreviewSize);
201 PageCacheContainer::iterator iCache (
mpPageCaches->find(aKey));
203 pResult = iCache->second;
206 if (pResult ==
nullptr)
210 if (pResult ==
nullptr)
211 pResult = std::make_shared<BitmapCache>();
216 Recycle(pResult, pDocument,rPreviewSize);
225 const std::shared_ptr<BitmapCache>& rpCache,
227 const Size& rPreviewSize)
234 if (rActiveCache.first.mpDocument == pDocument)
235 aCaches.emplace_back(
236 rActiveCache.first.maPreviewSize, rActiveCache.second);
243 for (
const auto& rRecentCache : iQueue->second)
244 aCaches.emplace_back(
245 rRecentCache.maPreviewSize, rRecentCache.mpCache);
248 ::std::sort(aCaches.begin(), aCaches.end(), BestFittingCacheComparer(rPreviewSize));
250 for (
const auto& rBestCache : aCaches)
252 rpCache->Recycle(*rBestCache.second);
258 PageCacheContainer::iterator iCache (::std::find_if(
265 assert(iCache->second == rpCache);
274 const std::shared_ptr<BitmapCache>& rpCache,
276 const Size& rNewPreviewSize)
278 std::shared_ptr<BitmapCache> pResult;
280 if (rpCache !=
nullptr)
283 PageCacheContainer::iterator iCacheToChange (::std::find_if(
289 assert(iCacheToChange->second == rpCache);
294 const ::sd::slidesorter::cache::PageCacheManager::DocumentKey aKey (
295 iCacheToChange->first.mpDocument);
298 CacheDescriptor(aKey,rNewPreviewSize),
317 bool bHasChanged (
false);
319 if (pDocument!=
nullptr)
324 if (rCache.first.mpDocument == pDocument)
325 bHasChanged |= rCache.second->InvalidateBitmap(pKey);
332 for (
const auto& rCache2 : iQueue->second)
333 bHasChanged |= rCache2.mpCache->InvalidateBitmap(pKey);
342 if (pDocument ==
nullptr)
348 if (rCache.first.mpDocument == pDocument)
349 rCache.second->InvalidateCache();
356 for (
const auto& rCache2 : iQueue->second)
357 rCache2.mpCache->InvalidateCache();
366 rCache.second->InvalidateCache();
376 rCache.second->ReleaseBitmap(pPage);
381 const Size& rPreviewSize)
383 std::shared_ptr<BitmapCache> pCache;
389 RecentlyUsedQueue::iterator iCache = std::find_if(iQueue->second.begin(), iQueue->second.end(),
390 [&rPreviewSize](
const RecentlyUsedCacheDescriptor& rCache) { return rCache.maPreviewSize == rPreviewSize; });
391 if (iCache != iQueue->second.end())
393 pCache = iCache->mpCache;
394 iQueue->second.erase(iCache);
403 const Size& rPreviewSize,
404 const std::shared_ptr<BitmapCache>& rpCache)
410 pDocument, RecentlyUsedQueue()
415 iQueue->second.push_front(RecentlyUsedCacheDescriptor(rPreviewSize,rpCache));
418 iQueue->second.pop_back();
void operator()(PageCacheManager *pObject)
Compare entries in the cache container with respect to the cache address only.
bool operator()(const PageCacheContainer::value_type &rValue) const
CompareWithCache(std::shared_ptr< BitmapCache > pCache)
std::shared_ptr< BitmapCache > mpCache
Container for the active caches.
The recently used caches are stored in one queue for each document.
std::pair< iterator, bool > emplace(Args &&... args)
iterator find(const key_type &key)
RecentlyUsedQueue mapped_type
std::map< key_type, mapped_type > maMap
std::map< key_type, mapped_type >::iterator iterator
Provide and manage the preview bitmap caches for all slide sorter instances.
static std::shared_ptr< PageCacheManager > Instance()
Return the one instance of the PageCacheManager class.
std::shared_ptr< BitmapCache > GetRecentlyUsedCache(const DocumentKey &pDocument, const Size &rSize)
std::shared_ptr< BitmapCache > GetCache(const DocumentKey &pDocument, const Size &rPreviewSize)
Look up the cache for the given model in which the previews have the specified size.
css::uno::Reference< css::uno::XInterface > DocumentKey
static const sal_uInt32 mnMaximalRecentlyCacheCount
The maximal number of recently used caches that are kept alive after they have become inactive,...
std::unique_ptr< RecentlyUsedPageCaches > mpRecentlyUsedPageCaches
void ReleaseCache(const std::shared_ptr< BitmapCache > &rpCache)
Tell the cache manager to release its own reference to the specified cache.
std::vector< std::pair< Size, std::shared_ptr< BitmapCache > > > BestFittingPageCaches
void InvalidateAllCaches()
Invalidate all the caches that are currently in use and destroy those that are not.
void Recycle(const std::shared_ptr< BitmapCache > &rpCache, const DocumentKey &pDocument, const Size &rPreviewSize)
This method is used internally to initialize a newly created BitmapCache with already existing previe...
void ReleasePreviewBitmap(const SdrPage *pPage)
Call this method when a page has been deleted and its preview is not needed anymore.
void InvalidateAllPreviewBitmaps(const DocumentKey &pDocument)
Invalidate the preview bitmaps for all slides that belong to the specified document.
bool InvalidatePreviewBitmap(const DocumentKey &pDocument, const SdrPage *pPage)
Invalidate the preview bitmap for one slide that belongs to the specified document.
std::shared_ptr< BitmapCache > ChangeSize(const std::shared_ptr< BitmapCache > &rpCache, const Size &rOldPreviewSize, const Size &rNewPreviewSize)
This is an information to the cache manager that the size of preview bitmaps in the specified cache h...
std::unique_ptr< PageCacheContainer > mpPageCaches
void PutRecentlyUsedCache(DocumentKey const &pDocument, const Size &rPreviewSize, const std::shared_ptr< BitmapCache > &rpCache)
Add the given cache to the list of recently used caches for the document.
static std::weak_ptr< PageCacheManager > mpInstance
Singleton instance of the cache manager.
EmbeddedObjectRef * pObject