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 | |
djsollen@google.com | 528a556 | 2013-02-01 15:57:13 +0000 | [diff] [blame] | 12 | SK_DECLARE_STATIC_MUTEX(gGlobalPoolMutex); |
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 | |
reed@google.com | 398337b | 2013-12-11 21:22:39 +0000 | [diff] [blame^] | 27 | SkImageRef_GlobalPool::SkImageRef_GlobalPool(SkStreamRewindable* stream, |
| 28 | SkBitmap::Config config, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 29 | int sampleSize) |
reed@google.com | 398337b | 2013-12-11 21:22:39 +0000 | [diff] [blame^] | 30 | : SkImageRef(stream, config, sampleSize, &gGlobalPoolMutex) { |
djsollen@google.com | 528a556 | 2013-02-01 15:57:13 +0000 | [diff] [blame] | 31 | SkASSERT(&gGlobalPoolMutex == this->mutex()); |
| 32 | SkAutoMutexAcquire ac(gGlobalPoolMutex); |
reed@google.com | 7294886 | 2012-02-16 20:04:31 +0000 | [diff] [blame] | 33 | GetGlobalPool()->addToHead(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | SkImageRef_GlobalPool::~SkImageRef_GlobalPool() { |
djsollen@google.com | 528a556 | 2013-02-01 15:57:13 +0000 | [diff] [blame] | 37 | SkASSERT(&gGlobalPoolMutex == this->mutex()); |
| 38 | SkAutoMutexAcquire ac(gGlobalPoolMutex); |
reed@google.com | 7294886 | 2012-02-16 20:04:31 +0000 | [diff] [blame] | 39 | GetGlobalPool()->detach(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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 | */ |
scroggo@google.com | b5571b3 | 2013-09-25 21:34:24 +0000 | [diff] [blame] | 47 | bool SkImageRef_GlobalPool::onDecode(SkImageDecoder* codec, SkStreamRewindable* stream, |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 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) |
djsollen@google.com | 528a556 | 2013-02-01 15:57:13 +0000 | [diff] [blame] | 68 | : INHERITED(buffer, &gGlobalPoolMutex) { |
| 69 | SkASSERT(&gGlobalPoolMutex == this->mutex()); |
| 70 | SkAutoMutexAcquire ac(gGlobalPoolMutex); |
reed@google.com | 7294886 | 2012-02-16 20:04:31 +0000 | [diff] [blame] | 71 | GetGlobalPool()->addToHead(this); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 72 | } |
| 73 | |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 74 | /////////////////////////////////////////////////////////////////////////////// |
| 75 | // global imagerefpool wrappers |
| 76 | |
| 77 | size_t SkImageRef_GlobalPool::GetRAMBudget() { |
djsollen@google.com | 528a556 | 2013-02-01 15:57:13 +0000 | [diff] [blame] | 78 | SkAutoMutexAcquire ac(gGlobalPoolMutex); |
reed@google.com | 7294886 | 2012-02-16 20:04:31 +0000 | [diff] [blame] | 79 | return GetGlobalPool()->getRAMBudget(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void SkImageRef_GlobalPool::SetRAMBudget(size_t size) { |
djsollen@google.com | 528a556 | 2013-02-01 15:57:13 +0000 | [diff] [blame] | 83 | SkAutoMutexAcquire ac(gGlobalPoolMutex); |
reed@google.com | 7294886 | 2012-02-16 20:04:31 +0000 | [diff] [blame] | 84 | GetGlobalPool()->setRAMBudget(size); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | size_t SkImageRef_GlobalPool::GetRAMUsed() { |
djsollen@google.com | 528a556 | 2013-02-01 15:57:13 +0000 | [diff] [blame] | 88 | SkAutoMutexAcquire ac(gGlobalPoolMutex); |
reed@google.com | 7294886 | 2012-02-16 20:04:31 +0000 | [diff] [blame] | 89 | return GetGlobalPool()->getRAMUsed(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void SkImageRef_GlobalPool::SetRAMUsed(size_t usage) { |
djsollen@google.com | 528a556 | 2013-02-01 15:57:13 +0000 | [diff] [blame] | 93 | SkAutoMutexAcquire ac(gGlobalPoolMutex); |
reed@google.com | 7294886 | 2012-02-16 20:04:31 +0000 | [diff] [blame] | 94 | GetGlobalPool()->setRAMUsed(usage); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void SkImageRef_GlobalPool::DumpPool() { |
djsollen@google.com | 528a556 | 2013-02-01 15:57:13 +0000 | [diff] [blame] | 98 | SkAutoMutexAcquire ac(gGlobalPoolMutex); |
reed@google.com | 7294886 | 2012-02-16 20:04:31 +0000 | [diff] [blame] | 99 | GetGlobalPool()->dump(); |
reed@android.com | 8a1c16f | 2008-12-17 15:59:43 +0000 | [diff] [blame] | 100 | } |