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