epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 8 | #include "SkImageRef_GlobalPool.h" |
| 9 | #include "SkImageRefPool.h" |
| 10 | #include "SkThread.h" |
| 11 | |
digit@google.com | 1771cbf | 2012-01-26 21:26:40 +0000 | [diff] [blame] | 12 | extern SkBaseMutex gImageRefMutex; |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 13 | |
reed@google.com | 7294886 | 2012-02-16 20:04:31 +0000 | [diff] [blame] | 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 | } |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 26 | |
| 27 | SkImageRef_GlobalPool::SkImageRef_GlobalPool(SkStream* stream, |
| 28 | SkBitmap::Config config, |
| 29 | int sampleSize) |
| 30 | : SkImageRef(stream, config, sampleSize) { |
| 31 | this->mutex()->acquire(); |
reed@google.com | 7294886 | 2012-02-16 20:04:31 +0000 | [diff] [blame] | 32 | GetGlobalPool()->addToHead(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 33 | this->mutex()->release(); |
| 34 | } |
| 35 | |
| 36 | SkImageRef_GlobalPool::~SkImageRef_GlobalPool() { |
| 37 | this->mutex()->acquire(); |
reed@google.com | 7294886 | 2012-02-16 20:04:31 +0000 | [diff] [blame] | 38 | GetGlobalPool()->detach(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 39 | this->mutex()->release(); |
| 40 | } |
reed@google.com | 7294886 | 2012-02-16 20:04:31 +0000 | [diff] [blame] | 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 | */ |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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) { |
reed@google.com | 7294886 | 2012-02-16 20:04:31 +0000 | [diff] [blame] | 54 | // no need to grab the mutex here, it has already been acquired. |
| 55 | GetGlobalPool()->justAddedPixels(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 56 | } |
| 57 | return true; |
| 58 | } |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 59 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 60 | void SkImageRef_GlobalPool::onUnlockPixels() { |
| 61 | this->INHERITED::onUnlockPixels(); |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 62 | |
reed@google.com | 7294886 | 2012-02-16 20:04:31 +0000 | [diff] [blame] | 63 | // by design, onUnlockPixels() already is inside the mutex-lock |
| 64 | GetGlobalPool()->canLosePixels(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | SkImageRef_GlobalPool::SkImageRef_GlobalPool(SkFlattenableReadBuffer& buffer) |
| 68 | : INHERITED(buffer) { |
| 69 | this->mutex()->acquire(); |
reed@google.com | 7294886 | 2012-02-16 20:04:31 +0000 | [diff] [blame] | 70 | GetGlobalPool()->addToHead(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 71 | this->mutex()->release(); |
| 72 | } |
| 73 | |
djsollen@google.com | 5370cd9 | 2012-03-28 20:47:01 +0000 | [diff] [blame] | 74 | SK_DEFINE_FLATTENABLE_REGISTRAR(SkImageRef_GlobalPool) |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 75 | |
| 76 | /////////////////////////////////////////////////////////////////////////////// |
| 77 | // global imagerefpool wrappers |
| 78 | |
| 79 | size_t SkImageRef_GlobalPool::GetRAMBudget() { |
| 80 | SkAutoMutexAcquire ac(gImageRefMutex); |
reed@google.com | 7294886 | 2012-02-16 20:04:31 +0000 | [diff] [blame] | 81 | return GetGlobalPool()->getRAMBudget(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void SkImageRef_GlobalPool::SetRAMBudget(size_t size) { |
| 85 | SkAutoMutexAcquire ac(gImageRefMutex); |
reed@google.com | 7294886 | 2012-02-16 20:04:31 +0000 | [diff] [blame] | 86 | GetGlobalPool()->setRAMBudget(size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | size_t SkImageRef_GlobalPool::GetRAMUsed() { |
rmistry@google.com | d6176b0 | 2012-08-23 18:14:13 +0000 | [diff] [blame^] | 90 | SkAutoMutexAcquire ac(gImageRefMutex); |
reed@google.com | 7294886 | 2012-02-16 20:04:31 +0000 | [diff] [blame] | 91 | return GetGlobalPool()->getRAMUsed(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | void SkImageRef_GlobalPool::SetRAMUsed(size_t usage) { |
| 95 | SkAutoMutexAcquire ac(gImageRefMutex); |
reed@google.com | 7294886 | 2012-02-16 20:04:31 +0000 | [diff] [blame] | 96 | GetGlobalPool()->setRAMUsed(usage); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void SkImageRef_GlobalPool::DumpPool() { |
| 100 | SkAutoMutexAcquire ac(gImageRefMutex); |
reed@google.com | 7294886 | 2012-02-16 20:04:31 +0000 | [diff] [blame] | 101 | GetGlobalPool()->dump(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 102 | } |