1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/images/SkImageRef_GlobalPool.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,100 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2011 Google Inc. 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 +#include "SkImageRef_GlobalPool.h" 1.12 +#include "SkImageRefPool.h" 1.13 +#include "SkThread.h" 1.14 + 1.15 +SK_DECLARE_STATIC_MUTEX(gGlobalPoolMutex); 1.16 + 1.17 +/* 1.18 + * This returns the lazily-allocated global pool. It must be called 1.19 + * from inside the guard mutex, so we safely only ever allocate 1. 1.20 + */ 1.21 +static SkImageRefPool* GetGlobalPool() { 1.22 + static SkImageRefPool* gPool; 1.23 + if (NULL == gPool) { 1.24 + gPool = SkNEW(SkImageRefPool); 1.25 + // call sk_atexit(...) when we have that, to free the global pool 1.26 + } 1.27 + return gPool; 1.28 +} 1.29 + 1.30 +SkImageRef_GlobalPool::SkImageRef_GlobalPool(const SkImageInfo& info, 1.31 + SkStreamRewindable* stream, 1.32 + int sampleSize) 1.33 + : SkImageRef(info, stream, sampleSize, &gGlobalPoolMutex) { 1.34 + SkASSERT(&gGlobalPoolMutex == this->mutex()); 1.35 + SkAutoMutexAcquire ac(gGlobalPoolMutex); 1.36 + GetGlobalPool()->addToHead(this); 1.37 +} 1.38 + 1.39 +SkImageRef_GlobalPool::~SkImageRef_GlobalPool() { 1.40 + SkASSERT(&gGlobalPoolMutex == this->mutex()); 1.41 + SkAutoMutexAcquire ac(gGlobalPoolMutex); 1.42 + GetGlobalPool()->detach(this); 1.43 +} 1.44 + 1.45 +/* By design, onUnlockPixels() already is inside the mutex-lock, 1.46 + * and it is the (indirect) caller of onDecode(), therefore we can assume 1.47 + * that we also are already inside the mutex. Hence, we can reference 1.48 + * the global-pool directly. 1.49 + */ 1.50 +bool SkImageRef_GlobalPool::onDecode(SkImageDecoder* codec, SkStreamRewindable* stream, 1.51 + SkBitmap* bitmap, SkBitmap::Config config, 1.52 + SkImageDecoder::Mode mode) { 1.53 + if (!this->INHERITED::onDecode(codec, stream, bitmap, config, mode)) { 1.54 + return false; 1.55 + } 1.56 + if (mode == SkImageDecoder::kDecodePixels_Mode) { 1.57 + // no need to grab the mutex here, it has already been acquired. 1.58 + GetGlobalPool()->justAddedPixels(this); 1.59 + } 1.60 + return true; 1.61 +} 1.62 + 1.63 +void SkImageRef_GlobalPool::onUnlockPixels() { 1.64 + this->INHERITED::onUnlockPixels(); 1.65 + 1.66 + // by design, onUnlockPixels() already is inside the mutex-lock 1.67 + GetGlobalPool()->canLosePixels(this); 1.68 +} 1.69 + 1.70 +SkImageRef_GlobalPool::SkImageRef_GlobalPool(SkReadBuffer& buffer) 1.71 + : INHERITED(buffer, &gGlobalPoolMutex) { 1.72 + SkASSERT(&gGlobalPoolMutex == this->mutex()); 1.73 + SkAutoMutexAcquire ac(gGlobalPoolMutex); 1.74 + GetGlobalPool()->addToHead(this); 1.75 +} 1.76 + 1.77 +/////////////////////////////////////////////////////////////////////////////// 1.78 +// global imagerefpool wrappers 1.79 + 1.80 +size_t SkImageRef_GlobalPool::GetRAMBudget() { 1.81 + SkAutoMutexAcquire ac(gGlobalPoolMutex); 1.82 + return GetGlobalPool()->getRAMBudget(); 1.83 +} 1.84 + 1.85 +void SkImageRef_GlobalPool::SetRAMBudget(size_t size) { 1.86 + SkAutoMutexAcquire ac(gGlobalPoolMutex); 1.87 + GetGlobalPool()->setRAMBudget(size); 1.88 +} 1.89 + 1.90 +size_t SkImageRef_GlobalPool::GetRAMUsed() { 1.91 + SkAutoMutexAcquire ac(gGlobalPoolMutex); 1.92 + return GetGlobalPool()->getRAMUsed(); 1.93 +} 1.94 + 1.95 +void SkImageRef_GlobalPool::SetRAMUsed(size_t usage) { 1.96 + SkAutoMutexAcquire ac(gGlobalPoolMutex); 1.97 + GetGlobalPool()->setRAMUsed(usage); 1.98 +} 1.99 + 1.100 +void SkImageRef_GlobalPool::DumpPool() { 1.101 + SkAutoMutexAcquire ac(gGlobalPoolMutex); 1.102 + GetGlobalPool()->dump(); 1.103 +}