OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkResourceCache_DEFINED | 8 #ifndef SkResourceCache_DEFINED |
9 #define SkResourceCache_DEFINED | 9 #define SkResourceCache_DEFINED |
10 | 10 |
(...skipping 17 matching lines...) Expand all Loading... |
28 public: | 28 public: |
29 struct Key { | 29 struct Key { |
30 // Call this to access your private contents. Must not use the address a
fter calling init() | 30 // Call this to access your private contents. Must not use the address a
fter calling init() |
31 void* writableContents() { return this + 1; } | 31 void* writableContents() { return this + 1; } |
32 | 32 |
33 // must call this after your private data has been written. | 33 // must call this after your private data has been written. |
34 // nameSpace must be unique per Key subclass. | 34 // nameSpace must be unique per Key subclass. |
35 // length must be a multiple of 4 | 35 // length must be a multiple of 4 |
36 void init(void* nameSpace, size_t length); | 36 void init(void* nameSpace, size_t length); |
37 | 37 |
38 void* getNamespace() const { return fNamespace; } | |
39 | |
40 // This is only valid after having called init(). | 38 // This is only valid after having called init(). |
41 uint32_t hash() const { return fHash; } | 39 uint32_t hash() const { return fHash; } |
42 | 40 |
43 bool operator==(const Key& other) const { | 41 bool operator==(const Key& other) const { |
44 const uint32_t* a = this->as32(); | 42 const uint32_t* a = this->as32(); |
45 const uint32_t* b = other.as32(); | 43 const uint32_t* b = other.as32(); |
46 for (int i = 0; i < fCount32; ++i) { // (This checks fCount == othe
r.fCount first.) | 44 for (int i = 0; i < fCount32; ++i) { // (This checks fCount == othe
r.fCount first.) |
47 if (a[i] != b[i]) { | 45 if (a[i] != b[i]) { |
48 return false; | 46 return false; |
49 } | 47 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 /** | 85 /** |
88 * Callback function for find(). If called, the cache will have found a mat
ch for the | 86 * Callback function for find(). If called, the cache will have found a mat
ch for the |
89 * specified Key, and will pass in the corresponding Rec, along with a call
er-specified | 87 * specified Key, and will pass in the corresponding Rec, along with a call
er-specified |
90 * context. The function can read the data in Rec, and copy whatever it lik
es into context | 88 * context. The function can read the data in Rec, and copy whatever it lik
es into context |
91 * (casting context to whatever it really is). | 89 * (casting context to whatever it really is). |
92 * | 90 * |
93 * The return value determines what the cache will do with the Rec. If the
function returns | 91 * The return value determines what the cache will do with the Rec. If the
function returns |
94 * true, then the Rec is considered "valid". If false is returned, the Rec
will be considered | 92 * true, then the Rec is considered "valid". If false is returned, the Rec
will be considered |
95 * "stale" and will be purged from the cache. | 93 * "stale" and will be purged from the cache. |
96 */ | 94 */ |
97 typedef bool (*FindVisitor)(const Rec&, void* context); | 95 typedef bool (*VisitorProc)(const Rec&, void* context); |
98 | |
99 enum PurgeVisitorResult { | |
100 kRetainAndContinue_PurgeVisitorResult, | |
101 kPurgeAndContinue_PurgeVisitorResult, | |
102 kRetainAndStop_PurgeVisitorResult, | |
103 kPurgeAndStop_PurgeVisitorResult, | |
104 }; | |
105 | |
106 /** | |
107 * Callback function for purge(). If called, the cache will have found a ma
tch for the | |
108 * specified Key, and will pass in the corresponding Rec, along with a call
er-specified | |
109 * context. The function can read the data in Rec. | |
110 */ | |
111 typedef PurgeVisitorResult (*PurgeVisitor)(const Rec&, void* context); | |
112 | 96 |
113 /** | 97 /** |
114 * Returns a locked/pinned SkDiscardableMemory instance for the specified | 98 * Returns a locked/pinned SkDiscardableMemory instance for the specified |
115 * number of bytes, or NULL on failure. | 99 * number of bytes, or NULL on failure. |
116 */ | 100 */ |
117 typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes); | 101 typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes); |
118 | 102 |
119 /* | 103 /* |
120 * The following static methods are thread-safe wrappers around a global | 104 * The following static methods are thread-safe wrappers around a global |
121 * instance of this cache. | 105 * instance of this cache. |
122 */ | 106 */ |
123 | 107 |
124 /** | 108 /** |
125 * Returns true if the visitor was called on a matching Key, and the visito
r returned true. | 109 * Returns true if the visitor was called on a matching Key, and the visito
r returned true. |
126 * | 110 * |
127 * Find() will search the cache for the specified Key. If no match is found
, return false and | 111 * Find() will search the cache for the specified Key. If no match is found
, return false and |
128 * do not call the VisitorProc. If a match is found, return whatever the vi
sitor returns. | 112 * do not call the VisitorProc. If a match is found, return whatever the vi
sitor returns. |
129 * Its return value is interpreted to mean: | 113 * Its return value is interpreted to mean: |
130 * true : Rec is valid | 114 * true : Rec is valid |
131 * false : Rec is "stale" -- the cache will purge it. | 115 * false : Rec is "stale" -- the cache will purge it. |
132 */ | 116 */ |
133 static bool Find(const Key& key, FindVisitor, void* context); | 117 static bool Find(const Key& key, VisitorProc, void* context); |
134 static void Add(Rec*); | 118 static void Add(Rec*); |
135 | 119 |
136 static size_t GetTotalBytesUsed(); | 120 static size_t GetTotalBytesUsed(); |
137 static size_t GetTotalByteLimit(); | 121 static size_t GetTotalByteLimit(); |
138 static size_t SetTotalByteLimit(size_t newLimit); | 122 static size_t SetTotalByteLimit(size_t newLimit); |
139 | 123 |
140 static size_t SetSingleAllocationByteLimit(size_t); | 124 static size_t SetSingleAllocationByteLimit(size_t); |
141 static size_t GetSingleAllocationByteLimit(); | 125 static size_t GetSingleAllocationByteLimit(); |
142 static size_t GetEffectiveSingleAllocationByteLimit(); | 126 static size_t GetEffectiveSingleAllocationByteLimit(); |
143 | 127 |
144 /** | |
145 * Visit all Rec that match the specified namespace, and purge entries as i
ndicated by the | |
146 * visitor. | |
147 */ | |
148 static void Purge(const void* nameSpace, PurgeVisitor, void* context); | |
149 | |
150 static void PurgeAll(); | 128 static void PurgeAll(); |
151 | 129 |
152 /** | 130 /** |
153 * Returns the DiscardableFactory used by the global cache, or NULL. | 131 * Returns the DiscardableFactory used by the global cache, or NULL. |
154 */ | 132 */ |
155 static DiscardableFactory GetDiscardableFactory(); | 133 static DiscardableFactory GetDiscardableFactory(); |
156 | 134 |
157 /** | 135 /** |
158 * Use this allocator for bitmaps, so they can use ashmem when available. | 136 * Use this allocator for bitmaps, so they can use ashmem when available. |
159 * Returns NULL if the ResourceCache has not been initialized with a Discard
ableFactory. | 137 * Returns NULL if the ResourceCache has not been initialized with a Discard
ableFactory. |
(...skipping 29 matching lines...) Expand all Loading... |
189 | 167 |
190 /** | 168 /** |
191 * Returns true if the visitor was called on a matching Key, and the visito
r returned true. | 169 * Returns true if the visitor was called on a matching Key, and the visito
r returned true. |
192 * | 170 * |
193 * find() will search the cache for the specified Key. If no match is found
, return false and | 171 * find() will search the cache for the specified Key. If no match is found
, return false and |
194 * do not call the VisitorProc. If a match is found, return whatever the vi
sitor returns. | 172 * do not call the VisitorProc. If a match is found, return whatever the vi
sitor returns. |
195 * Its return value is interpreted to mean: | 173 * Its return value is interpreted to mean: |
196 * true : Rec is valid | 174 * true : Rec is valid |
197 * false : Rec is "stale" -- the cache will purge it. | 175 * false : Rec is "stale" -- the cache will purge it. |
198 */ | 176 */ |
199 bool find(const Key&, FindVisitor, void* context); | 177 bool find(const Key&, VisitorProc, void* context); |
200 void add(Rec*); | 178 void add(Rec*); |
201 | 179 |
202 size_t getTotalBytesUsed() const { return fTotalBytesUsed; } | 180 size_t getTotalBytesUsed() const { return fTotalBytesUsed; } |
203 size_t getTotalByteLimit() const { return fTotalByteLimit; } | 181 size_t getTotalByteLimit() const { return fTotalByteLimit; } |
204 | 182 |
205 /** | 183 /** |
206 * This is respected by SkBitmapProcState::possiblyScaleImage. | 184 * This is respected by SkBitmapProcState::possiblyScaleImage. |
207 * 0 is no maximum at all; this is the default. | 185 * 0 is no maximum at all; this is the default. |
208 * setSingleAllocationByteLimit() returns the previous value. | 186 * setSingleAllocationByteLimit() returns the previous value. |
209 */ | 187 */ |
210 size_t setSingleAllocationByteLimit(size_t maximumAllocationSize); | 188 size_t setSingleAllocationByteLimit(size_t maximumAllocationSize); |
211 size_t getSingleAllocationByteLimit() const; | 189 size_t getSingleAllocationByteLimit() const; |
212 // returns the logical single allocation size (pinning against the budget wh
en the cache | 190 // returns the logical single allocation size (pinning against the budget wh
en the cache |
213 // is not backed by discardable memory. | 191 // is not backed by discardable memory. |
214 size_t getEffectiveSingleAllocationByteLimit() const; | 192 size_t getEffectiveSingleAllocationByteLimit() const; |
215 | 193 |
216 /** | 194 /** |
217 * Set the maximum number of bytes available to this cache. If the current | 195 * Set the maximum number of bytes available to this cache. If the current |
218 * cache exceeds this new value, it will be purged to try to fit within | 196 * cache exceeds this new value, it will be purged to try to fit within |
219 * this new limit. | 197 * this new limit. |
220 */ | 198 */ |
221 size_t setTotalByteLimit(size_t newLimit); | 199 size_t setTotalByteLimit(size_t newLimit); |
222 | 200 |
223 void purge(const void* nameSpace, PurgeVisitor, void* context); | |
224 | |
225 void purgeAll() { | 201 void purgeAll() { |
226 fInsidePurgeAllCounter += 1; | |
227 this->purgeAsNeeded(true); | 202 this->purgeAsNeeded(true); |
228 fInsidePurgeAllCounter -= 1; | |
229 } | 203 } |
230 | 204 |
231 DiscardableFactory discardableFactory() const { return fDiscardableFactory;
} | 205 DiscardableFactory discardableFactory() const { return fDiscardableFactory;
} |
232 SkBitmap::Allocator* allocator() const { return fAllocator; }; | 206 SkBitmap::Allocator* allocator() const { return fAllocator; }; |
233 | 207 |
234 SkCachedData* newCachedData(size_t bytes); | 208 SkCachedData* newCachedData(size_t bytes); |
235 | 209 |
236 /** | 210 /** |
237 * Call SkDebugf() with diagnostic information about the state of the cache | 211 * Call SkDebugf() with diagnostic information about the state of the cache |
238 */ | 212 */ |
239 void dump() const; | 213 void dump() const; |
240 | 214 |
241 private: | 215 private: |
242 Rec* fHead; | 216 Rec* fHead; |
243 Rec* fTail; | 217 Rec* fTail; |
244 | 218 |
245 class Hash; | 219 class Hash; |
246 Hash* fHash; | 220 Hash* fHash; |
247 | 221 |
248 DiscardableFactory fDiscardableFactory; | 222 DiscardableFactory fDiscardableFactory; |
249 // the allocator is NULL or one that matches discardables | 223 // the allocator is NULL or one that matches discardables |
250 SkBitmap::Allocator* fAllocator; | 224 SkBitmap::Allocator* fAllocator; |
251 | 225 |
252 size_t fTotalBytesUsed; | 226 size_t fTotalBytesUsed; |
253 size_t fTotalByteLimit; | 227 size_t fTotalByteLimit; |
254 size_t fSingleAllocationByteLimit; | 228 size_t fSingleAllocationByteLimit; |
255 int fCount; | 229 int fCount; |
256 | 230 |
257 bool insidePurgeAll() const { | |
258 SkASSERT(fInsidePurgeAllCounter >= 0); | |
259 return fInsidePurgeAllCounter > 0; | |
260 } | |
261 int fInsidePurgeAllCounter; | |
262 | |
263 void purgeAsNeeded(bool forcePurge = false); | 231 void purgeAsNeeded(bool forcePurge = false); |
264 | 232 |
265 // linklist management | 233 // linklist management |
266 void moveToHead(Rec*); | 234 void moveToHead(Rec*); |
267 void addToHead(Rec*); | 235 void addToHead(Rec*); |
268 void detach(Rec*); | 236 void detach(Rec*); |
269 void remove(Rec*); | 237 void remove(Rec*); |
270 | 238 |
271 void init(); // called by constructors | 239 void init(); // called by constructors |
272 | 240 |
273 #ifdef SK_DEBUG | 241 #ifdef SK_DEBUG |
274 void validate() const; | 242 void validate() const; |
275 #else | 243 #else |
276 void validate() const {} | 244 void validate() const {} |
277 #endif | 245 #endif |
278 }; | 246 }; |
279 #endif | 247 #endif |
OLD | NEW |