blob: b7740239566d622157f12b7ea38956572caec9ab [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
digit@google.com1771cbf2012-01-26 21:26:40 +000012extern SkBaseMutex gImageRefMutex;
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
27SkImageRef_GlobalPool::SkImageRef_GlobalPool(SkStream* stream,
28 SkBitmap::Config config,
29 int sampleSize)
30 : SkImageRef(stream, config, sampleSize) {
31 this->mutex()->acquire();
reed@google.com72948862012-02-16 20:04:31 +000032 GetGlobalPool()->addToHead(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +000033 this->mutex()->release();
34}
35
36SkImageRef_GlobalPool::~SkImageRef_GlobalPool() {
37 this->mutex()->acquire();
reed@google.com72948862012-02-16 20:04:31 +000038 GetGlobalPool()->detach(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +000039 this->mutex()->release();
40}
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 */
reed@android.com8a1c16f2008-12-17 15:59:43 +000047bool 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.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}
59
60void SkImageRef_GlobalPool::onUnlockPixels() {
61 this->INHERITED::onUnlockPixels();
62
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)
68 : INHERITED(buffer) {
69 this->mutex()->acquire();
reed@google.com72948862012-02-16 20:04:31 +000070 GetGlobalPool()->addToHead(this);
reed@android.com8a1c16f2008-12-17 15:59:43 +000071 this->mutex()->release();
72}
73
74SkPixelRef* SkImageRef_GlobalPool::Create(SkFlattenableReadBuffer& buffer) {
75 return SkNEW_ARGS(SkImageRef_GlobalPool, (buffer));
76}
77
caryclark@google.comd26147a2011-12-15 14:16:43 +000078SK_DEFINE_PIXEL_REF_REGISTRAR(SkImageRef_GlobalPool)
reed@android.com8a1c16f2008-12-17 15:59:43 +000079
80///////////////////////////////////////////////////////////////////////////////
81// global imagerefpool wrappers
82
83size_t SkImageRef_GlobalPool::GetRAMBudget() {
84 SkAutoMutexAcquire ac(gImageRefMutex);
reed@google.com72948862012-02-16 20:04:31 +000085 return GetGlobalPool()->getRAMBudget();
reed@android.com8a1c16f2008-12-17 15:59:43 +000086}
87
88void SkImageRef_GlobalPool::SetRAMBudget(size_t size) {
89 SkAutoMutexAcquire ac(gImageRefMutex);
reed@google.com72948862012-02-16 20:04:31 +000090 GetGlobalPool()->setRAMBudget(size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000091}
92
93size_t SkImageRef_GlobalPool::GetRAMUsed() {
94 SkAutoMutexAcquire ac(gImageRefMutex);
reed@google.com72948862012-02-16 20:04:31 +000095 return GetGlobalPool()->getRAMUsed();
reed@android.com8a1c16f2008-12-17 15:59:43 +000096}
97
98void SkImageRef_GlobalPool::SetRAMUsed(size_t usage) {
99 SkAutoMutexAcquire ac(gImageRefMutex);
reed@google.com72948862012-02-16 20:04:31 +0000100 GetGlobalPool()->setRAMUsed(usage);
reed@android.com8a1c16f2008-12-17 15:59:43 +0000101}
102
103void SkImageRef_GlobalPool::DumpPool() {
104 SkAutoMutexAcquire ac(gImageRefMutex);
reed@google.com72948862012-02-16 20:04:31 +0000105 GetGlobalPool()->dump();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000106}