blob: a5a71a3d9d3fc7bcee4751c591eaa2cc837acb06 [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}
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)
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
djsollen@google.com5370cd92012-03-28 20:47:01 +000074SK_DEFINE_FLATTENABLE_REGISTRAR(SkImageRef_GlobalPool)
reed@android.com8a1c16f2008-12-17 15:59:43 +000075
76///////////////////////////////////////////////////////////////////////////////
77// global imagerefpool wrappers
78
79size_t SkImageRef_GlobalPool::GetRAMBudget() {
80 SkAutoMutexAcquire ac(gImageRefMutex);
reed@google.com72948862012-02-16 20:04:31 +000081 return GetGlobalPool()->getRAMBudget();
reed@android.com8a1c16f2008-12-17 15:59:43 +000082}
83
84void SkImageRef_GlobalPool::SetRAMBudget(size_t size) {
85 SkAutoMutexAcquire ac(gImageRefMutex);
reed@google.com72948862012-02-16 20:04:31 +000086 GetGlobalPool()->setRAMBudget(size);
reed@android.com8a1c16f2008-12-17 15:59:43 +000087}
88
89size_t SkImageRef_GlobalPool::GetRAMUsed() {
rmistry@google.comd6176b02012-08-23 18:14:13 +000090 SkAutoMutexAcquire ac(gImageRefMutex);
reed@google.com72948862012-02-16 20:04:31 +000091 return GetGlobalPool()->getRAMUsed();
reed@android.com8a1c16f2008-12-17 15:59:43 +000092}
93
94void SkImageRef_GlobalPool::SetRAMUsed(size_t usage) {
95 SkAutoMutexAcquire ac(gImageRefMutex);
reed@google.com72948862012-02-16 20:04:31 +000096 GetGlobalPool()->setRAMUsed(usage);
reed@android.com8a1c16f2008-12-17 15:59:43 +000097}
98
99void SkImageRef_GlobalPool::DumpPool() {
100 SkAutoMutexAcquire ac(gImageRefMutex);
reed@google.com72948862012-02-16 20:04:31 +0000101 GetGlobalPool()->dump();
reed@android.com8a1c16f2008-12-17 15:59:43 +0000102}