1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
#include "SlsBitmapCache.hxx"
#include "SlsCacheCompactor.hxx"
#include "SlsBitmapCompressor.hxx"
#include "SlsCacheConfiguration.hxx"
#include "sdpage.hxx"
#include "drawdoc.hxx"
// Uncomment the following define for some more OSL_TRACE messages.
#ifdef DEBUG
//#define VERBOSE
#endif
// Define the default value for the maximal cache size that is used for
// previews that are currently not visible. The visible previews are all
// held in memory at all times. This default is used only when the
// configuration does not have a value.
static const sal_Int32 MAXIMAL_CACHE_SIZE = 4L*1024L*1024L;
using namespace ::com::sun::star::uno;
namespace sd { namespace slidesorter { namespace cache {
class BitmapCache::CacheEntry
{
public:
CacheEntry(const Bitmap& rBitmap, sal_Int32 nLastAccessTime, bool bIsPrecious);
CacheEntry(sal_Int32 nLastAccessTime, bool bIsPrecious);
~CacheEntry (void) {};
inline void Recycle (const CacheEntry& rEntry);
inline sal_Int32 GetMemorySize (void) const;
void Compress (const ::boost::shared_ptr<BitmapCompressor>& rpCompressor);
inline void Decompress (void);
bool IsUpToDate (void) const { return mbIsUpToDate; }
void SetUpToDate (bool bIsUpToDate) { mbIsUpToDate = bIsUpToDate; }
sal_Int32 GetAccessTime (void) const { return mnLastAccessTime; }
void SetAccessTime (sal_Int32 nAccessTime) { mnLastAccessTime = nAccessTime; }
Bitmap GetPreview (void) const { return maPreview; }
inline void SetPreview (const Bitmap& rPreview);
bool HasPreview (void) const;
Bitmap GetMarkedPreview (void) const { return maMarkedPreview; }
inline void SetMarkedPreview (const Bitmap& rMarkePreview);
bool HasReplacement (void) const { return (mpReplacement.get() != NULL); }
inline bool HasLosslessReplacement (void) const;
void Clear (void) { maPreview.SetEmpty(); maMarkedPreview.SetEmpty();
mpReplacement.reset(); mpCompressor.reset(); }
void Invalidate (void) { mpReplacement.reset(); mpCompressor.reset(); mbIsUpToDate = false; }
bool IsPrecious (void) const { return mbIsPrecious; }
void SetPrecious (bool bIsPrecious) { mbIsPrecious = bIsPrecious; }
private:
Bitmap maPreview;
Bitmap maMarkedPreview;
::boost::shared_ptr<BitmapReplacement> mpReplacement;
::boost::shared_ptr<BitmapCompressor> mpCompressor;
Size maBitmapSize;
bool mbIsUpToDate;
sal_Int32 mnLastAccessTime;
// When this flag is set then the bitmap is not modified by a cache
// compactor.
bool mbIsPrecious;
};
class CacheEntry;
class CacheHash {
public:
size_t operator()(const BitmapCache::CacheKey& p) const
{ return (size_t)p; }
};
class BitmapCache::CacheBitmapContainer
: public ::boost::unordered_map<CacheKey, CacheEntry, CacheHash>
{
public:
CacheBitmapContainer (void) {}
};
namespace {
typedef ::std::vector<
::std::pair< ::sd::slidesorter::cache::BitmapCache::CacheKey,
::sd::slidesorter::cache::BitmapCache::CacheEntry>
> SortableBitmapContainer;
/** Compare elements of the bitmap cache according to their last access
time.
*/
class AccessTimeComparator
{
public:
bool operator () (
const SortableBitmapContainer::value_type& e1,
const SortableBitmapContainer::value_type& e2)
{
return e1.second.GetAccessTime() < e2.second.GetAccessTime();
}
};
} // end of anonymous namespace
//===== BitmapCache =========================================================
BitmapCache::BitmapCache (const sal_Int32 nMaximalNormalCacheSize)
: maMutex(),
mpBitmapContainer(new CacheBitmapContainer()),
mnNormalCacheSize(0),
mnPreciousCacheSize(0),
mnCurrentAccessTime(0),
mnMaximalNormalCacheSize(MAXIMAL_CACHE_SIZE),
mpCacheCompactor(),
mbIsFull(false)
{
if (nMaximalNormalCacheSize > 0)
mnMaximalNormalCacheSize = nMaximalNormalCacheSize;
else
{
Any aCacheSize (CacheConfiguration::Instance()->GetValue(
::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("CacheSize"))));
if (aCacheSize.has<sal_Int32>())
aCacheSize >>= mnMaximalNormalCacheSize;
}
mpCacheCompactor = CacheCompactor::Create(*this,mnMaximalNormalCacheSize);
}
BitmapCache::~BitmapCache (void)
{
Clear();
}
void BitmapCache::Clear (void)
{
::osl::MutexGuard aGuard (maMutex);
mpBitmapContainer->clear();
mnNormalCacheSize = 0;
mnPreciousCacheSize = 0;
mnCurrentAccessTime = 0;
}
bool BitmapCache::IsFull (void) const
{
return mbIsFull;
}
sal_Int32 BitmapCache::GetSize (void)
{
return mnNormalCacheSize;
}
bool BitmapCache::HasBitmap (const CacheKey& rKey)
{
::osl::MutexGuard aGuard (maMutex);
CacheBitmapContainer::iterator iEntry (mpBitmapContainer->find(rKey));
return (iEntry != mpBitmapContainer->end()
&& (iEntry->second.HasPreview() || iEntry->second.HasReplacement()));
}
bool BitmapCache::BitmapIsUpToDate (const CacheKey& rKey)
{
::osl::MutexGuard aGuard (maMutex);
bool bIsUpToDate = false;
CacheBitmapContainer::iterator aIterator (mpBitmapContainer->find(rKey));
if (aIterator != mpBitmapContainer->end())
bIsUpToDate = aIterator->second.IsUpToDate();
return bIsUpToDate;
}
Bitmap BitmapCache::GetBitmap (const CacheKey& rKey)
{
::osl::MutexGuard aGuard (maMutex);
CacheBitmapContainer::iterator iEntry (mpBitmapContainer->find(rKey));
if (iEntry == mpBitmapContainer->end())
{
// Create an empty bitmap for the given key that acts as placeholder
// until we are given the real one. Mark it as not being up to date.
SetBitmap(rKey, Bitmap(), false);
iEntry = mpBitmapContainer->find(rKey);
iEntry->second.SetUpToDate(false);
}
else
{
iEntry->second.SetAccessTime(mnCurrentAccessTime++);
// Maybe we have to decompress the preview.
if ( ! iEntry->second.HasPreview() && iEntry->second.HasReplacement())
{
UpdateCacheSize(iEntry->second, REMOVE);
iEntry->second.Decompress();
UpdateCacheSize(iEntry->second, ADD);
}
}
return iEntry->second.GetPreview();
}
Bitmap BitmapCache::GetMarkedBitmap (const CacheKey& rKey)
{
::osl::MutexGuard aGuard (maMutex);
CacheBitmapContainer::iterator iEntry (mpBitmapContainer->find(rKey));
if (iEntry != mpBitmapContainer->end())
{
iEntry->second.SetAccessTime(mnCurrentAccessTime++);
return iEntry->second.GetMarkedPreview();
}
else
return Bitmap();
}
void BitmapCache::ReleaseBitmap (const CacheKey& rKey)
{
::osl::MutexGuard aGuard (maMutex);
CacheBitmapContainer::iterator aIterator (mpBitmapContainer->find(rKey));
if (aIterator != mpBitmapContainer->end())
{
UpdateCacheSize(aIterator->second, REMOVE);
mpBitmapContainer->erase(aIterator);
}
}
bool BitmapCache::InvalidateBitmap (const CacheKey& rKey)
{
::osl::MutexGuard aGuard (maMutex);
CacheBitmapContainer::iterator iEntry (mpBitmapContainer->find(rKey));
if (iEntry != mpBitmapContainer->end())
{
iEntry->second.SetUpToDate(false);
// When there is a preview then we release the replacement. The
// preview itself is kept until a new one is created.
if (iEntry->second.HasPreview())
{
UpdateCacheSize(iEntry->second, REMOVE);
iEntry->second.Invalidate();
UpdateCacheSize(iEntry->second, ADD);
}
return true;
}
else
return false;
}
void BitmapCache::InvalidateCache (void)
{
::osl::MutexGuard aGuard (maMutex);
CacheBitmapContainer::iterator iEntry;
for (iEntry=mpBitmapContainer->begin(); iEntry!=mpBitmapContainer->end(); ++iEntry)
{
iEntry->second.Invalidate();
}
ReCalculateTotalCacheSize();
}
void BitmapCache::SetBitmap (
const CacheKey& rKey,
const Bitmap& rPreview,
bool bIsPrecious)
{
::osl::MutexGuard aGuard (maMutex);
CacheBitmapContainer::iterator iEntry (mpBitmapContainer->find(rKey));
if (iEntry != mpBitmapContainer->end())
{
UpdateCacheSize(iEntry->second, REMOVE);
iEntry->second.SetPreview(rPreview);
iEntry->second.SetUpToDate(true);
iEntry->second.SetAccessTime(mnCurrentAccessTime++);
}
else
{
iEntry = mpBitmapContainer->insert(CacheBitmapContainer::value_type (
rKey,
CacheEntry(rPreview, mnCurrentAccessTime++, bIsPrecious))
).first;
}
if (iEntry != mpBitmapContainer->end())
UpdateCacheSize(iEntry->second, ADD);
}
void BitmapCache::SetMarkedBitmap (
const CacheKey& rKey,
const Bitmap& rPreview)
{
::osl::MutexGuard aGuard (maMutex);
CacheBitmapContainer::iterator iEntry (mpBitmapContainer->find(rKey));
if (iEntry != mpBitmapContainer->end())
{
UpdateCacheSize(iEntry->second, REMOVE);
iEntry->second.SetMarkedPreview(rPreview);
iEntry->second.SetAccessTime(mnCurrentAccessTime++);
UpdateCacheSize(iEntry->second, ADD);
}
}
void BitmapCache::SetPrecious (const CacheKey& rKey, bool bIsPrecious)
{
::osl::MutexGuard aGuard (maMutex);
CacheBitmapContainer::iterator iEntry (mpBitmapContainer->find(rKey));
if (iEntry != mpBitmapContainer->end())
{
if (iEntry->second.IsPrecious() != bIsPrecious)
{
UpdateCacheSize(iEntry->second, REMOVE);
iEntry->second.SetPrecious(bIsPrecious);
UpdateCacheSize(iEntry->second, ADD);
}
}
else if (bIsPrecious)
{
iEntry = mpBitmapContainer->insert(CacheBitmapContainer::value_type (
rKey,
CacheEntry(Bitmap(), mnCurrentAccessTime++, bIsPrecious))
).first;
UpdateCacheSize(iEntry->second, ADD);
}
}
void BitmapCache::ReCalculateTotalCacheSize (void)
{
::osl::MutexGuard aGuard (maMutex);
mnNormalCacheSize = 0;
mnPreciousCacheSize = 0;
CacheBitmapContainer::iterator iEntry;
for (iEntry=mpBitmapContainer->begin(); iEntry!=mpBitmapContainer->end(); ++iEntry)
{
if (iEntry->second.IsPrecious())
mnPreciousCacheSize += iEntry->second.GetMemorySize();
else
mnNormalCacheSize += iEntry->second.GetMemorySize();
}
mbIsFull = (mnNormalCacheSize >= mnMaximalNormalCacheSize);
#ifdef VERBOSE
OSL_TRACE("cache size is %d/%d", mnNormalCacheSize, mnPreciousCacheSize);
#endif
}
void BitmapCache::Recycle (const BitmapCache& rCache)
{
::osl::MutexGuard aGuard (maMutex);
CacheBitmapContainer::const_iterator iOtherEntry;
for (iOtherEntry=rCache.mpBitmapContainer->begin();
iOtherEntry!=rCache.mpBitmapContainer->end();
++iOtherEntry)
{
CacheBitmapContainer::iterator iEntry (mpBitmapContainer->find(iOtherEntry->first));
if (iEntry == mpBitmapContainer->end())
{
iEntry = mpBitmapContainer->insert(CacheBitmapContainer::value_type (
iOtherEntry->first,
CacheEntry(mnCurrentAccessTime++, true))
).first;
UpdateCacheSize(iEntry->second, ADD);
}
if (iEntry != mpBitmapContainer->end())
{
UpdateCacheSize(iEntry->second, REMOVE);
iEntry->second.Recycle(iOtherEntry->second);
UpdateCacheSize(iEntry->second, ADD);
}
}
}
SAL_WNODEPRECATED_DECLARATIONS_PUSH
::std::auto_ptr<BitmapCache::CacheIndex> BitmapCache::GetCacheIndex (
bool bIncludePrecious,
bool bIncludeNoPreview) const
{
::osl::MutexGuard aGuard (maMutex);
// Create a copy of the bitmap container.
SortableBitmapContainer aSortedContainer;
aSortedContainer.reserve(mpBitmapContainer->size());
// Copy the relevant entries.
CacheBitmapContainer::iterator iEntry;
for (iEntry=mpBitmapContainer->begin(); iEntry!=mpBitmapContainer->end(); ++iEntry)
{
if ( ! bIncludePrecious && iEntry->second.IsPrecious())
continue;
if ( ! bIncludeNoPreview && ! iEntry->second.HasPreview())
continue;
aSortedContainer.push_back(SortableBitmapContainer::value_type(
iEntry->first,iEntry->second));
}
// Sort the remaining entries.
::std::sort(aSortedContainer.begin(), aSortedContainer.end(), AccessTimeComparator());
// Return a list with the keys of the sorted entries.
::std::auto_ptr<CacheIndex> pIndex(new CacheIndex());
SortableBitmapContainer::iterator iIndexEntry;
pIndex->reserve(aSortedContainer.size());
for (iIndexEntry=aSortedContainer.begin(); iIndexEntry!=aSortedContainer.end(); ++iIndexEntry)
pIndex->push_back(iIndexEntry->first);
return pIndex;
}
SAL_WNODEPRECATED_DECLARATIONS_POP
void BitmapCache::Compress (
const CacheKey& rKey,
const ::boost::shared_ptr<BitmapCompressor>& rpCompressor)
{
::osl::MutexGuard aGuard (maMutex);
CacheBitmapContainer::iterator iEntry (mpBitmapContainer->find(rKey));
if (iEntry != mpBitmapContainer->end() && iEntry->second.HasPreview())
{
UpdateCacheSize(iEntry->second, REMOVE);
iEntry->second.Compress(rpCompressor);
UpdateCacheSize(iEntry->second, ADD);
}
}
void BitmapCache::UpdateCacheSize (const CacheEntry& rEntry, CacheOperation eOperation)
{
sal_Int32 nEntrySize (rEntry.GetMemorySize());
sal_Int32& rCacheSize (rEntry.IsPrecious() ? mnPreciousCacheSize : mnNormalCacheSize);
switch (eOperation)
{
case ADD:
rCacheSize += nEntrySize;
if ( ! rEntry.IsPrecious() && mnNormalCacheSize>mnMaximalNormalCacheSize)
{
mbIsFull = true;
#ifdef VERBOSE
OSL_TRACE("cache size is %d > %d", mnNormalCacheSize,mnMaximalNormalCacheSize);
#endif
mpCacheCompactor->RequestCompaction();
}
break;
case REMOVE:
rCacheSize -= nEntrySize;
if (mnNormalCacheSize < mnMaximalNormalCacheSize)
mbIsFull = false;
break;
default:
OSL_ASSERT(false);
break;
}
}
//===== CacheEntry ============================================================
BitmapCache::CacheEntry::CacheEntry(
sal_Int32 nLastAccessTime,
bool bIsPrecious)
: maPreview(),
maMarkedPreview(),
mbIsUpToDate(true),
mnLastAccessTime(nLastAccessTime),
mbIsPrecious(bIsPrecious)
{
}
BitmapCache::CacheEntry::CacheEntry(
const Bitmap& rPreview,
sal_Int32 nLastAccessTime,
bool bIsPrecious)
: maPreview(rPreview),
maMarkedPreview(),
mbIsUpToDate(true),
mnLastAccessTime(nLastAccessTime),
mbIsPrecious(bIsPrecious)
{
}
inline void BitmapCache::CacheEntry::Recycle (const CacheEntry& rEntry)
{
if ((rEntry.HasPreview() || rEntry.HasLosslessReplacement())
&& ! (HasPreview() || HasLosslessReplacement()))
{
maPreview = rEntry.maPreview;
maMarkedPreview = rEntry.maMarkedPreview;
mpReplacement = rEntry.mpReplacement;
mpCompressor = rEntry.mpCompressor;
mnLastAccessTime = rEntry.mnLastAccessTime;
mbIsUpToDate = rEntry.mbIsUpToDate;
}
}
inline sal_Int32 BitmapCache::CacheEntry::GetMemorySize (void) const
{
sal_Int32 nSize (0);
nSize += maPreview.GetSizeBytes();
nSize += maMarkedPreview.GetSizeBytes();
if (mpReplacement.get() != NULL)
nSize += mpReplacement->GetMemorySize();
return nSize;
}
void BitmapCache::CacheEntry::Compress (const ::boost::shared_ptr<BitmapCompressor>& rpCompressor)
{
if ( ! maPreview.IsEmpty())
{
if (mpReplacement.get() == NULL)
{
mpReplacement = rpCompressor->Compress(maPreview);
#ifdef VERBOSE
sal_uInt32 nOldSize (maPreview.GetSizeBytes());
sal_uInt32 nNewSize (mpReplacement.get()!=NULL ? mpReplacement->GetMemorySize() : 0);
if (nOldSize == 0)
nOldSize = 1;
sal_Int32 nRatio (100L * nNewSize / nOldSize);
OSL_TRACE("compressing bitmap for %x from %d to %d bytes (%d%%)",
this,
nOldSize,
nNewSize,
nRatio);
#endif
mpCompressor = rpCompressor;
}
maPreview.SetEmpty();
maMarkedPreview.SetEmpty();
}
}
inline void BitmapCache::CacheEntry::Decompress (void)
{
if (mpReplacement.get()!=NULL && mpCompressor.get()!=NULL && maPreview.IsEmpty())
{
maPreview = mpCompressor->Decompress(*mpReplacement);
maMarkedPreview.SetEmpty();
if ( ! mpCompressor->IsLossless())
mbIsUpToDate = false;
}
}
inline void BitmapCache::CacheEntry::SetPreview (const Bitmap& rPreview)
{
maPreview = rPreview;
maMarkedPreview.SetEmpty();
mpReplacement.reset();
mpCompressor.reset();
}
bool BitmapCache::CacheEntry::HasPreview (void) const
{
return ! maPreview.IsEmpty();
}
inline void BitmapCache::CacheEntry::SetMarkedPreview (const Bitmap& rMarkedPreview)
{
maMarkedPreview = rMarkedPreview;
}
inline bool BitmapCache::CacheEntry::HasLosslessReplacement (void) const
{
return mpReplacement.get()!=NULL
&& mpCompressor.get()!=NULL
&& mpCompressor->IsLossless();
}
} } } // end of namespace ::sd::slidesorter::cache
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|