blob: 352dd42d9f79a3c94dabafff20edbeeb7fdb8b99 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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.com8a1c16f2008-12-17 15:59:43 +00008#include "SkImageRef_GlobalPool.h"
9#include "SkImageRefPool.h"
10#include "SkThread.h"
11
djsollen@google.com528a5562013-02-01 15:57:13 +000012SK_DECLARE_STATIC_MUTEX(gGlobalPoolMutex);
reed@android.com8a1c16f2008-12-17 15:59:43 +000013
reed@google.com72948862012-02-16 20:04:31 +000014/*
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 */
18static 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.com8a1c16f2008-12-17 15:59:43 +000026
reed@google.com398337b2013-12-11 21:22:39 +000027SkImageRef_GlobalPool::SkImageRef_GlobalPool(SkStreamRewindable* stream,
28 SkBitmap::Config config,
reed@android.com8a1c16f2008-12-17 15:59:43 +000029 int sampleSize)
reed@google.com398337b2013-12-11 21:22:39 +000030 : SkImageRef(stream, config, sampleSize, &gGlobalPoolMutex) {
djsollen@google.com528a5562013-02-01 15:57:13 +000031 SkASSERT(&gGlobalPoolMutex == this->mutex());
32 SkAutoMutexAcquire ac(gGlobalPoolMutex);
reed@google.com72948862012-02-16 20:04:31 +000033 GetGlobalPool()->addToHead(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +000034}
35
36SkImageRef_GlobalPool::~SkImageRef_GlobalPool() {
djsollen@google.com528a5562013-02-01 15:57:13 +000037 SkASSERT(&gGlobalPoolMutex == this->mutex());
38 SkAutoMutexAcquire ac(gGlobalPoolMutex);
reed@google.com72948862012-02-16 20:04:31 +000039 GetGlobalPool()->detach(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +000040}
reed@google.com72948862012-02-16 20:04:31 +000041
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.comb5571b32013-09-25 21:34:24 +000047bool SkImageRef_GlobalPool::onDecode(SkImageDecoder* codec, SkStreamRewindable* stream,
reed@android.com8a1c16f2008-12-17 15:59:43 +000048 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.com72948862012-02-16 20:04:31 +000054 // no need to grab the mutex here, it has already been acquired.
55 GetGlobalPool()->justAddedPixels(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +000056 }
57 return true;
58}
rmistry@google.comd6176b02012-08-23 18:14:13 +000059
reed@android.com8a1c16f2008-12-17 15:59:43 +000060void SkImageRef_GlobalPool::onUnlockPixels() {
61 this->INHERITED::onUnlockPixels();
rmistry@google.comd6176b02012-08-23 18:14:13 +000062
reed@google.com72948862012-02-16 20:04:31 +000063 // by design, onUnlockPixels() already is inside the mutex-lock
64 GetGlobalPool()->canLosePixels(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +000065}
66
67SkImageRef_GlobalPool::SkImageRef_GlobalPool(SkFlattenableReadBuffer& buffer)
djsollen@google.com528a5562013-02-01 15:57:13 +000068 : INHERITED(buffer, &gGlobalPoolMutex) {
69 SkASSERT(&gGlobalPoolMutex == this->mutex());
70 SkAutoMutexAcquire ac(gGlobalPoolMutex);
reed@google.com72948862012-02-16 20:04:31 +000071 GetGlobalPool()->addToHead(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +000072}
73
reed@android.com8a1c16f2008-12-17 15:59:43 +000074///////////////////////////////////////////////////////////////////////////////
75// global imagerefpool wrappers
76
77size_t SkImageRef_GlobalPool::GetRAMBudget() {
djsollen@google.com528a5562013-02-01 15:57:13 +000078 SkAutoMutexAcquire ac(gGlobalPoolMutex);
reed@google.com72948862012-02-16 20:04:31 +000079 return GetGlobalPool()->getRAMBudget();
reed@android.com8a1c16f2008-12-17 15:59:43 +000080}
81
82void SkImageRef_GlobalPool::SetRAMBudget(size_t size) {
djsollen@google.com528a5562013-02-01 15:57:13 +000083 SkAutoMutexAcquire ac(gGlobalPoolMutex);
reed@google.com72948862012-02-16 20:04:31 +000084 GetGlobalPool()->setRAMBudget(size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000085}
86
87size_t SkImageRef_GlobalPool::GetRAMUsed() {
djsollen@google.com528a5562013-02-01 15:57:13 +000088 SkAutoMutexAcquire ac(gGlobalPoolMutex);
reed@google.com72948862012-02-16 20:04:31 +000089 return GetGlobalPool()->getRAMUsed();
reed@android.com8a1c16f2008-12-17 15:59:43 +000090}
91
92void SkImageRef_GlobalPool::SetRAMUsed(size_t usage) {
djsollen@google.com528a5562013-02-01 15:57:13 +000093 SkAutoMutexAcquire ac(gGlobalPoolMutex);
reed@google.com72948862012-02-16 20:04:31 +000094 GetGlobalPool()->setRAMUsed(usage);
reed@android.com8a1c16f2008-12-17 15:59:43 +000095}
96
97void SkImageRef_GlobalPool::DumpPool() {
djsollen@google.com528a5562013-02-01 15:57:13 +000098 SkAutoMutexAcquire ac(gGlobalPoolMutex);
reed@google.com72948862012-02-16 20:04:31 +000099 GetGlobalPool()->dump();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000100}