blob: 0d36e4a9a91a60bb4ea4b2b8cc380b1b9a2bbe6c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
/*
* Copyright 2013 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "SkThread.h"
#include "SkPurgeableImageCache.h"
#include "SkPurgeableMemoryBlock.h"
#ifdef SK_DEBUG
#include "SkTSearch.h"
#endif
SK_DEFINE_INST_COUNT(SkPurgeableImageCache)
SK_DECLARE_STATIC_MUTEX(gPurgeableImageMutex);
SkImageCache* SkPurgeableImageCache::Create() {
if (!SkPurgeableMemoryBlock::IsSupported()) {
return NULL;
}
SkAutoMutexAcquire ac(&gPurgeableImageMutex);
static SkPurgeableImageCache gCache;
gCache.ref();
return &gCache;
}
SkPurgeableImageCache::SkPurgeableImageCache() {}
#ifdef SK_DEBUG
SkPurgeableImageCache::~SkPurgeableImageCache() {
SkASSERT(fRecs.count() == 0);
}
#endif
void* SkPurgeableImageCache::allocAndPinCache(size_t bytes, intptr_t* ID) {
SkAutoMutexAcquire ac(&gPurgeableImageMutex);
SkPurgeableMemoryBlock* block = SkPurgeableMemoryBlock::Create(bytes);
if (NULL == block) {
return NULL;
}
SkPurgeableMemoryBlock::PinResult pinResult;
void* data = block->pin(&pinResult);
if (NULL == data) {
SkDELETE(block);
return NULL;
}
SkASSERT(ID != NULL);
*ID = reinterpret_cast<intptr_t>(block);
#ifdef SK_DEBUG
// Insert into the array of all recs:
int index = this->findRec(*ID);
SkASSERT(index < 0);
fRecs.insert(~index, 1, ID);
#endif
return data;
}
void* SkPurgeableImageCache::pinCache(intptr_t ID, SkImageCache::DataStatus* status) {
SkASSERT(ID != SkImageCache::UNINITIALIZED_ID);
SkAutoMutexAcquire ac(&gPurgeableImageMutex);
SkASSERT(this->findRec(ID) >= 0);
SkPurgeableMemoryBlock* block = reinterpret_cast<SkPurgeableMemoryBlock*>(ID);
SkPurgeableMemoryBlock::PinResult pinResult;
void* data = block->pin(&pinResult);
if (NULL == data) {
this->removeRec(ID);
return NULL;
}
switch (pinResult) {
case SkPurgeableMemoryBlock::kRetained_PinResult:
*status = SkImageCache::kRetained_DataStatus;
break;
case SkPurgeableMemoryBlock::kUninitialized_PinResult:
*status = SkImageCache::kUninitialized_DataStatus;
break;
default:
// Invalid value. Treat as a failure to pin.
SkASSERT(false);
this->removeRec(ID);
return NULL;
}
return data;
}
void SkPurgeableImageCache::releaseCache(intptr_t ID) {
SkASSERT(ID != SkImageCache::UNINITIALIZED_ID);
SkAutoMutexAcquire ac(&gPurgeableImageMutex);
SkASSERT(this->findRec(ID) >= 0);
SkPurgeableMemoryBlock* block = reinterpret_cast<SkPurgeableMemoryBlock*>(ID);
block->unpin();
}
void SkPurgeableImageCache::throwAwayCache(intptr_t ID) {
SkASSERT(ID != SkImageCache::UNINITIALIZED_ID);
SkAutoMutexAcquire ac(&gPurgeableImageMutex);
this->removeRec(ID);
}
#ifdef SK_DEBUG
SkImageCache::MemoryStatus SkPurgeableImageCache::getMemoryStatus(intptr_t ID) const {
SkAutoMutexAcquire ac(&gPurgeableImageMutex);
if (SkImageCache::UNINITIALIZED_ID == ID || this->findRec(ID) < 0) {
return SkImageCache::kFreed_MemoryStatus;
}
SkPurgeableMemoryBlock* block = reinterpret_cast<SkPurgeableMemoryBlock*>(ID);
if (block->isPinned()) {
return SkImageCache::kPinned_MemoryStatus;
}
return SkImageCache::kUnpinned_MemoryStatus;
}
void SkPurgeableImageCache::purgeAllUnpinnedCaches() {
SkAutoMutexAcquire ac(&gPurgeableImageMutex);
if (SkPurgeableMemoryBlock::PlatformSupportsPurgingAllUnpinnedBlocks()) {
SkPurgeableMemoryBlock::PurgeAllUnpinnedBlocks();
} else {
// Go through the blocks, and purge them individually.
// Rather than deleting the blocks, which would interfere with further calls, purge them
// and keep them around.
for (int i = 0; i < fRecs.count(); i++) {
SkPurgeableMemoryBlock* block = reinterpret_cast<SkPurgeableMemoryBlock*>(fRecs[i]);
if (!block->isPinned()) {
if (!block->purge()) {
// FIXME: This should be more meaningful (which one, etc...)
SkDebugf("Failed to purge\n");
}
}
}
}
}
int SkPurgeableImageCache::findRec(intptr_t rec) const {
return SkTSearch(fRecs.begin(), fRecs.count(), rec, sizeof(intptr_t));
}
#endif
void SkPurgeableImageCache::removeRec(intptr_t ID) {
#ifdef SK_DEBUG
int index = this->findRec(ID);
SkASSERT(index >= 0);
fRecs.remove(index);
#endif
SkPurgeableMemoryBlock* block = reinterpret_cast<SkPurgeableMemoryBlock*>(ID);
SkASSERT(!block->isPinned());
SkDELETE(block);
}
|