21#include <unordered_map>
43 CacheEntry(sal_Int32 nLastAccessTime,
bool bIsPrecious);
46 void Compress (
const std::shared_ptr<BitmapCompressor>& rpCompressor);
84 {
return reinterpret_cast<size_t>(
p); }
90 :
public std::unordered_map<CacheKey, CacheEntry, CacheHash>
101 > SortableBitmapContainer;
106 class AccessTimeComparator
110 const SortableBitmapContainer::value_type& e1,
111 const SortableBitmapContainer::value_type& e2)
113 return e1.second.
GetAccessTime() < e2.second.GetAccessTime();
123 mnNormalCacheSize(0),
124 mnPreciousCacheSize(0),
125 mnCurrentAccessTime(0),
130 if (aCacheSize.has<sal_Int32>())
143 std::unique_lock aGuard (
maMutex);
153 std::unique_lock aGuard (
maMutex);
157 && (iEntry->second.HasPreview() || iEntry->second.HasReplacement()));
162 std::unique_lock aGuard (
maMutex);
164 bool bIsUpToDate =
false;
167 bIsUpToDate = aIterator->second.IsUpToDate();
174 std::unique_lock aGuard (
maMutex);
183 iEntry->second.SetUpToDate(
false);
190 if ( ! iEntry->second.HasPreview() && iEntry->second.HasReplacement())
193 iEntry->second.Decompress();
197 return iEntry->second.GetPreview();
202 std::unique_lock aGuard (
maMutex);
208 return iEntry->second.GetMarkedPreview();
216 std::unique_lock aGuard (
maMutex);
228 std::unique_lock aGuard (
maMutex);
233 iEntry->second.SetUpToDate(
false);
237 if (iEntry->second.HasPreview())
240 iEntry->second.Invalidate();
251 std::unique_lock aGuard (
maMutex);
255 rEntry.second.Invalidate();
265 std::unique_lock aGuard (
maMutex);
266 SetBitmap(aGuard, rKey, rPreview, bIsPrecious);
270 std::unique_lock<std::mutex>& rGuard,
279 iEntry->second.SetPreview(rPreview);
280 iEntry->second.SetUpToDate(
true);
299 std::unique_lock aGuard (
maMutex);
305 iEntry->second.SetMarkedPreview(rPreview);
313 std::unique_lock aGuard (
maMutex);
318 if (iEntry->second.IsPrecious() != bIsPrecious)
321 iEntry->second.SetPrecious(bIsPrecious);
325 else if (bIsPrecious)
337 std::unique_lock aGuard (
maMutex);
347 if (rEntry.second.IsPrecious())
359 std::unique_lock aGuard (
maMutex);
363 CacheBitmapContainer::iterator iEntry (
mpBitmapContainer->find(rOtherEntry.first));
375 iEntry->second.Recycle(rOtherEntry.second);
383 std::unique_lock aGuard (
maMutex);
386 SortableBitmapContainer aSortedContainer;
392 if ( rEntry.second.IsPrecious())
395 if ( ! rEntry.second.HasPreview())
398 aSortedContainer.emplace_back(rEntry.first, rEntry.second);
402 ::std::sort(aSortedContainer.begin(), aSortedContainer.end(), AccessTimeComparator());
406 aIndex.reserve(aSortedContainer.size());
407 for (
const auto& rIndexEntry : aSortedContainer)
408 aIndex.push_back(rIndexEntry.first);
414 const std::shared_ptr<BitmapCompressor>& rpCompressor)
416 std::unique_lock aGuard (
maMutex);
422 iEntry->second.Compress(rpCompressor);
434 rCacheSize += nEntrySize;
444 rCacheSize -= nEntrySize;
458 sal_Int32 nLastAccessTime,
460 : mbIsUpToDate(true),
461 mnLastAccessTime(nLastAccessTime),
462 mbIsPrecious(bIsPrecious)
468 sal_Int32 nLastAccessTime,
470 : maPreview(rPreview),
472 mnLastAccessTime(nLastAccessTime),
473 mbIsPrecious(bIsPrecious)
480 && ! (HasPreview() || HasLosslessReplacement()))
494 nSize += maPreview.GetSizeBytes();
495 nSize += maMarkedPreview.GetSizeBytes();
496 if (mpReplacement !=
nullptr)
497 nSize += mpReplacement->GetMemorySize();
503 if ( maPreview.IsEmpty())
506 if (mpReplacement ==
nullptr)
508 mpReplacement = rpCompressor->Compress(maPreview);
510#ifdef DEBUG_SD_SLSBITMAPCACHE
511 sal_uInt32 nOldSize (maPreview.GetSizeBytes());
512 sal_uInt32 nNewSize (mpReplacement.get()!=
NULL ? mpReplacement->GetMemorySize() : 0);
515 sal_Int32 nRatio (100L * nNewSize / nOldSize);
516 SAL_INFO(
"sd.sls", __func__ <<
": compressing bitmap for " << %
x <<
" from " << nOldSize <<
" to " << nNewSize <<
" bytes (" << nRatio <<
"%)");
519 mpCompressor = rpCompressor;
522 maPreview.SetEmpty();
523 maMarkedPreview.SetEmpty();
528 if (mpReplacement !=
nullptr && mpCompressor !=
nullptr && maPreview.IsEmpty())
530 maPreview = mpCompressor->Decompress(*mpReplacement);
531 maMarkedPreview.SetEmpty();
532 if ( ! mpCompressor->IsLossless())
533 mbIsUpToDate =
false;
539 maPreview = rPreview;
541 mpReplacement.reset();
542 mpCompressor.reset();
547 return ! maPreview.IsEmpty();
552 maMarkedPreview = rMarkedPreview;
557 return mpReplacement !=
nullptr && mpCompressor !=
nullptr && mpCompressor->IsLossless();
const sal_Int32 MAXIMAL_CACHE_SIZE
sal_Int32 GetAccessTime() const
void SetPrecious(bool bIsPrecious)
bool HasReplacement() const
const BitmapEx & GetPreview() const
std::shared_ptr< BitmapCompressor > mpCompressor
void Compress(const std::shared_ptr< BitmapCompressor > &rpCompressor)
sal_Int32 GetMemorySize() const
CacheEntry(const BitmapEx &rBitmap, sal_Int32 nLastAccessTime, bool bIsPrecious)
bool HasLosslessReplacement() const
void SetUpToDate(bool bIsUpToDate)
const BitmapEx & GetMarkedPreview() const
void SetMarkedPreview(const BitmapEx &rMarkePreview)
void Recycle(const CacheEntry &rEntry)
sal_Int32 mnLastAccessTime
void SetPreview(const BitmapEx &rPreview)
std::shared_ptr< BitmapReplacement > mpReplacement
void SetAccessTime(sal_Int32 nAccessTime)
This low level cache is the actual bitmap container.
void ReCalculateTotalCacheSize()
Calculate the cache size.
void Recycle(const BitmapCache &rCache)
Use the previews in the given cache to initialize missing previews.
bool InvalidateBitmap(const CacheKey &rKey)
Mark the specified preview bitmap as not being up-to-date anymore.
sal_Int32 mnMaximalNormalCacheSize
The maximal cache size for the off-screen preview bitmaps.
void SetPrecious(const CacheKey &rKey, bool bIsPrecious)
Mark the specified preview bitmap as precious, i.e.
::std::vector< CacheKey > CacheIndex
std::unique_ptr< CacheBitmapContainer > mpBitmapContainer
::std::unique_ptr< CacheCompactor > mpCacheCompactor
The cache compactor is used to reduce the number of bytes used by off-screen preview bitmaps.
BitmapEx GetBitmap(const CacheKey &rKey)
Return the preview bitmap for the given contact object.
void SetMarkedBitmap(const CacheKey &rKey, const BitmapEx &rPreview)
Add or replace a marked bitmap for the given key.
void Compress(const CacheKey &rKey, const std::shared_ptr< BitmapCompressor > &rpCompressor)
Compress the specified preview bitmap with the given bitmap compressor.
bool HasBitmap(const CacheKey &rKey)
Return <TRUE> when a preview bitmap exists for the given key.
void SetBitmap(const CacheKey &rKey, const BitmapEx &rPreview, bool bIsPrecious)
Add or replace a bitmap for the given key.
sal_Int32 mnNormalCacheSize
Total size of bytes that are occupied by bitmaps in the cache for whom the slides are currently not i...
void Clear()
Remove all preview bitmaps from the cache.
CacheIndex GetCacheIndex() const
Return a list of sorted cache keys that represent an index into (a part of) the cache.
void ReleaseBitmap(const CacheKey &rKey)
Release the reference to the preview bitmap that is associated with the given key.
bool BitmapIsUpToDate(const CacheKey &rKey)
Return <TRUE> when a preview bitmap exists for the given key and when it is up-to-date.
const SdrPage * CacheKey
The key for looking up preview bitmaps is a pointer to an SdrPage object.
~BitmapCache()
The destructor clears the cache and releases all bitmaps still in it.
CacheOperation
Update mnNormalCacheSize or mnPreciousCacheSize according to the precious flag of the specified previ...
BitmapCache()
Create a new cache for bitmap objects.
bool mbIsFull
This flag stores if the cache is or recently was full, i.e.
void UpdateCacheSize(std::unique_lock< std::mutex > &rGuard, const CacheEntry &rKey, CacheOperation eOperation)
BitmapEx GetMarkedBitmap(const CacheKey &rKey)
Return the marked preview bitmap for the given contact object.
void InvalidateCache()
Mark all preview bitmaps as not being up-to-date anymore.
sal_Int32 mnCurrentAccessTime
At the moment the access time is not an actual time or date value but a counter that is increased wit...
sal_Int32 mnPreciousCacheSize
Total size of bytes that are occupied by bitmaps in the cache for whom the slides are currently visib...
static ::std::unique_ptr< CacheCompactor > Create(BitmapCache &rCache, sal_Int32 nMaximalCacheSize)
Create a new instance of the CacheCompactor interface class.
static std::shared_ptr< CacheConfiguration > Instance()
Return an instance to this class.
std::deque< AttacherIndex_Impl > aIndex
#define SAL_INFO(area, stream)