diff options
author | reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2012-02-16 20:04:31 +0000 |
---|---|---|
committer | reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2012-02-16 20:04:31 +0000 |
commit | 72948869808188b6c09517df70d6758f75f8a0df (patch) | |
tree | 58534801b33376cb84d787415e1ac252b263564d /src/images | |
parent | 302b861338a626014153cc7368c54edbc646cfe5 (diff) |
Lazily allocate our global pool for imagerefs
Review URL: https://codereview.appspot.com/5677055
git-svn-id: http://skia.googlecode.com/svn/trunk@3211 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/images')
-rw-r--r-- | src/images/SkImageRef_GlobalPool.cpp | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/src/images/SkImageRef_GlobalPool.cpp b/src/images/SkImageRef_GlobalPool.cpp index 93e7535dc4..b774023956 100644 --- a/src/images/SkImageRef_GlobalPool.cpp +++ b/src/images/SkImageRef_GlobalPool.cpp @@ -11,23 +11,39 @@ extern SkBaseMutex gImageRefMutex; -static SkImageRefPool gGlobalImageRefPool; +/* + * This returns the lazily-allocated global pool. It must be called + * from inside the guard mutex, so we safely only ever allocate 1. + */ +static SkImageRefPool* GetGlobalPool() { + static SkImageRefPool* gPool; + if (NULL == gPool) { + gPool = SkNEW(SkImageRefPool); + // call sk_atexit(...) when we have that, to free the global pool + } + return gPool; +} SkImageRef_GlobalPool::SkImageRef_GlobalPool(SkStream* stream, SkBitmap::Config config, int sampleSize) : SkImageRef(stream, config, sampleSize) { this->mutex()->acquire(); - gGlobalImageRefPool.addToHead(this); + GetGlobalPool()->addToHead(this); this->mutex()->release(); } SkImageRef_GlobalPool::~SkImageRef_GlobalPool() { this->mutex()->acquire(); - gGlobalImageRefPool.detach(this); + GetGlobalPool()->detach(this); this->mutex()->release(); } - + +/* By design, onUnlockPixels() already is inside the mutex-lock, + * and it is the (indirect) caller of onDecode(), therefore we can assume + * that we also are already inside the mutex. Hence, we can reference + * the global-pool directly. + */ bool SkImageRef_GlobalPool::onDecode(SkImageDecoder* codec, SkStream* stream, SkBitmap* bitmap, SkBitmap::Config config, SkImageDecoder::Mode mode) { @@ -35,7 +51,8 @@ bool SkImageRef_GlobalPool::onDecode(SkImageDecoder* codec, SkStream* stream, return false; } if (mode == SkImageDecoder::kDecodePixels_Mode) { - gGlobalImageRefPool.justAddedPixels(this); + // no need to grab the mutex here, it has already been acquired. + GetGlobalPool()->justAddedPixels(this); } return true; } @@ -43,13 +60,14 @@ bool SkImageRef_GlobalPool::onDecode(SkImageDecoder* codec, SkStream* stream, void SkImageRef_GlobalPool::onUnlockPixels() { this->INHERITED::onUnlockPixels(); - gGlobalImageRefPool.canLosePixels(this); + // by design, onUnlockPixels() already is inside the mutex-lock + GetGlobalPool()->canLosePixels(this); } SkImageRef_GlobalPool::SkImageRef_GlobalPool(SkFlattenableReadBuffer& buffer) : INHERITED(buffer) { this->mutex()->acquire(); - gGlobalImageRefPool.addToHead(this); + GetGlobalPool()->addToHead(this); this->mutex()->release(); } @@ -64,25 +82,25 @@ SK_DEFINE_PIXEL_REF_REGISTRAR(SkImageRef_GlobalPool) size_t SkImageRef_GlobalPool::GetRAMBudget() { SkAutoMutexAcquire ac(gImageRefMutex); - return gGlobalImageRefPool.getRAMBudget(); + return GetGlobalPool()->getRAMBudget(); } void SkImageRef_GlobalPool::SetRAMBudget(size_t size) { SkAutoMutexAcquire ac(gImageRefMutex); - gGlobalImageRefPool.setRAMBudget(size); + GetGlobalPool()->setRAMBudget(size); } size_t SkImageRef_GlobalPool::GetRAMUsed() { SkAutoMutexAcquire ac(gImageRefMutex); - return gGlobalImageRefPool.getRAMUsed(); + return GetGlobalPool()->getRAMUsed(); } void SkImageRef_GlobalPool::SetRAMUsed(size_t usage) { SkAutoMutexAcquire ac(gImageRefMutex); - gGlobalImageRefPool.setRAMUsed(usage); + GetGlobalPool()->setRAMUsed(usage); } void SkImageRef_GlobalPool::DumpPool() { SkAutoMutexAcquire ac(gImageRefMutex); - gGlobalImageRefPool.dump(); + GetGlobalPool()->dump(); } |