1 2 /* 3 * Copyright 2011 Google Inc. 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 #include "SkImageRef_GlobalPool.h" 9 #include "SkImageRefPool.h" 10 #include "SkThread.h" 11 12 extern SkBaseMutex gImageRefMutex; 13 14 /* 15 * This returns the lazily-allocated global pool. It must be called 16 * from inside the guard mutex, so we safely only ever allocate 1. 17 */ 18 static SkImageRefPool* GetGlobalPool() { 19 static SkImageRefPool* gPool; 20 if (NULL == gPool) { 21 gPool = SkNEW(SkImageRefPool); 22 // call sk_atexit(...) when we have that, to free the global pool 23 } 24 return gPool; 25 } 26 27 SkImageRef_GlobalPool::SkImageRef_GlobalPool(SkStream* stream, 28 SkBitmap::Config config, 29 int sampleSize) 30 : SkImageRef(stream, config, sampleSize) { 31 this->mutex()->acquire(); 32 GetGlobalPool()->addToHead(this); 33 this->mutex()->release(); 34 } 35 36 SkImageRef_GlobalPool::~SkImageRef_GlobalPool() { 37 this->mutex()->acquire(); 38 GetGlobalPool()->detach(this); 39 this->mutex()->release(); 40 } 41 42 /* By design, onUnlockPixels() already is inside the mutex-lock, 43 * and it is the (indirect) caller of onDecode(), therefore we can assume 44 * that we also are already inside the mutex. Hence, we can reference 45 * the global-pool directly. 46 */ 47 bool SkImageRef_GlobalPool::onDecode(SkImageDecoder* codec, SkStream* stream, 48 SkBitmap* bitmap, SkBitmap::Config config, 49 SkImageDecoder::Mode mode) { 50 if (!this->INHERITED::onDecode(codec, stream, bitmap, config, mode)) { 51 return false; 52 } 53 if (mode == SkImageDecoder::kDecodePixels_Mode) { 54 // no need to grab the mutex here, it has already been acquired. 55 GetGlobalPool()->justAddedPixels(this); 56 } 57 return true; 58 } 59 60 void SkImageRef_GlobalPool::onUnlockPixels() { 61 this->INHERITED::onUnlockPixels(); 62 63 // by design, onUnlockPixels() already is inside the mutex-lock 64 GetGlobalPool()->canLosePixels(this); 65 } 66 67 SkImageRef_GlobalPool::SkImageRef_GlobalPool(SkFlattenableReadBuffer& buffer) 68 : INHERITED(buffer) { 69 this->mutex()->acquire(); 70 GetGlobalPool()->addToHead(this); 71 this->mutex()->release(); 72 } 73 74 SkPixelRef* SkImageRef_GlobalPool::Create(SkFlattenableReadBuffer& buffer) { 75 return SkNEW_ARGS(SkImageRef_GlobalPool, (buffer)); 76 } 77 78 SK_DEFINE_PIXEL_REF_REGISTRAR(SkImageRef_GlobalPool) 79 80 /////////////////////////////////////////////////////////////////////////////// 81 // global imagerefpool wrappers 82 83 size_t SkImageRef_GlobalPool::GetRAMBudget() { 84 SkAutoMutexAcquire ac(gImageRefMutex); 85 return GetGlobalPool()->getRAMBudget(); 86 } 87 88 void SkImageRef_GlobalPool::SetRAMBudget(size_t size) { 89 SkAutoMutexAcquire ac(gImageRefMutex); 90 GetGlobalPool()->setRAMBudget(size); 91 } 92 93 size_t SkImageRef_GlobalPool::GetRAMUsed() { 94 SkAutoMutexAcquire ac(gImageRefMutex); 95 return GetGlobalPool()->getRAMUsed(); 96 } 97 98 void SkImageRef_GlobalPool::SetRAMUsed(size_t usage) { 99 SkAutoMutexAcquire ac(gImageRefMutex); 100 GetGlobalPool()->setRAMUsed(usage); 101 } 102 103 void SkImageRef_GlobalPool::DumpPool() { 104 SkAutoMutexAcquire ac(gImageRefMutex); 105 GetGlobalPool()->dump(); 106 } 107