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 |
11 #include "SkBitmap.h" | 11 #include "SkBitmap.h" |
12 #include "SkTDArray.h" | |
12 | 13 |
13 class SkCachedData; | 14 class SkCachedData; |
14 class SkDiscardableMemory; | 15 class SkDiscardableMemory; |
15 class SkMipMap; | 16 class SkMipMap; |
16 | 17 |
17 /** | 18 /** |
18 * Cache object for bitmaps (with possible scale in X Y as part of the key). | 19 * Cache object for bitmaps (with possible scale in X Y as part of the key). |
19 * | 20 * |
20 * Multiple caches can be instantiated, but each instance is not implicitly | 21 * Multiple caches can be instantiated, but each instance is not implicitly |
21 * thread-safe, so if a given instance is to be shared across threads, the | 22 * thread-safe, so if a given instance is to be shared across threads, the |
22 * caller must manage the access itself (e.g. via a mutex). | 23 * caller must manage the access itself (e.g. via a mutex). |
23 * | 24 * |
24 * As a convenience, a global instance is also defined, which can be safely | 25 * As a convenience, a global instance is also defined, which can be safely |
25 * access across threads via the static methods (e.g. FindAndLock, etc.). | 26 * access across threads via the static methods (e.g. FindAndLock, etc.). |
26 */ | 27 */ |
27 class SkResourceCache { | 28 class SkResourceCache { |
28 public: | 29 public: |
29 struct Key { | 30 struct Key { |
30 // Call this to access your private contents. Must not use the address a fter calling init() | 31 // Call this to access your private contents. Must not use the address a fter calling init() |
31 void* writableContents() { return this + 1; } | 32 void* writableContents() { return this + 1; } |
32 | 33 |
33 // must call this after your private data has been written. | 34 // must call this after your private data has been written. |
34 // nameSpace must be unique per Key subclass. | 35 // nameSpace must be unique per Key subclass. |
35 // length must be a multiple of 4 | 36 // length must be a multiple of 4 |
36 void init(void* nameSpace, size_t length); | 37 void init(void* nameSpace, uint64_t sharedID, size_t length); |
mtklein
2015/02/24 16:43:39
Why don't we call this uniqueID? I hate to add an
reed2
2015/02/24 17:44:52
I don't think its unique, since it may appear in s
| |
38 | |
39 void* getNamespace() const { return fNamespace; } | |
40 uint64_t getSharedID() const { return fSharedID; } | |
37 | 41 |
38 // This is only valid after having called init(). | 42 // This is only valid after having called init(). |
39 uint32_t hash() const { return fHash; } | 43 uint32_t hash() const { return fHash; } |
40 | 44 |
41 bool operator==(const Key& other) const { | 45 bool operator==(const Key& other) const { |
42 const uint32_t* a = this->as32(); | 46 const uint32_t* a = this->as32(); |
43 const uint32_t* b = other.as32(); | 47 const uint32_t* b = other.as32(); |
44 for (int i = 0; i < fCount32; ++i) { // (This checks fCount == othe r.fCount first.) | 48 for (int i = 0; i < fCount32; ++i) { // (This checks fCount == othe r.fCount first.) |
45 if (a[i] != b[i]) { | 49 if (a[i] != b[i]) { |
46 return false; | 50 return false; |
47 } | 51 } |
48 } | 52 } |
49 return true; | 53 return true; |
50 } | 54 } |
51 | 55 |
52 private: | 56 private: |
53 int32_t fCount32; // local + user contents count32 | 57 int32_t fCount32; // local + user contents count32 |
54 uint32_t fHash; | 58 uint32_t fHash; |
59 uint64_t fSharedID; // 0 means ignore (nothing is shared) | |
55 void* fNamespace; // A unique namespace tag. This is hashed. | 60 void* fNamespace; // A unique namespace tag. This is hashed. |
56 /* uint32_t fContents32[] */ | 61 /* uint32_t fContents32[] */ |
57 | 62 |
58 const uint32_t* as32() const { return (const uint32_t*)this; } | 63 const uint32_t* as32() const { return (const uint32_t*)this; } |
59 }; | 64 }; |
60 | 65 |
61 struct Rec { | 66 struct Rec { |
62 typedef SkResourceCache::Key Key; | 67 typedef SkResourceCache::Key Key; |
63 | 68 |
64 Rec() {} | 69 Rec() {} |
(...skipping 20 matching lines...) Expand all Loading... | |
85 /** | 90 /** |
86 * Callback function for find(). If called, the cache will have found a mat ch for the | 91 * 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 | 92 * 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 | 93 * 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). | 94 * (casting context to whatever it really is). |
90 * | 95 * |
91 * The return value determines what the cache will do with the Rec. If the function returns | 96 * 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 | 97 * 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. | 98 * "stale" and will be purged from the cache. |
94 */ | 99 */ |
95 typedef bool (*VisitorProc)(const Rec&, void* context); | 100 typedef bool (*FindVisitor)(const Rec&, void* context); |
96 | 101 |
97 /** | 102 /** |
98 * Returns a locked/pinned SkDiscardableMemory instance for the specified | 103 * Returns a locked/pinned SkDiscardableMemory instance for the specified |
99 * number of bytes, or NULL on failure. | 104 * number of bytes, or NULL on failure. |
100 */ | 105 */ |
101 typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes); | 106 typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes); |
102 | 107 |
103 /* | 108 /* |
104 * The following static methods are thread-safe wrappers around a global | 109 * The following static methods are thread-safe wrappers around a global |
105 * instance of this cache. | 110 * instance of this cache. |
106 */ | 111 */ |
107 | 112 |
108 /** | 113 /** |
109 * Returns true if the visitor was called on a matching Key, and the visito r returned true. | 114 * Returns true if the visitor was called on a matching Key, and the visito r returned true. |
110 * | 115 * |
111 * Find() will search the cache for the specified Key. If no match is found , return false and | 116 * 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. | 117 * do not call the VisitorProc. If a match is found, return whatever the vi sitor returns. |
113 * Its return value is interpreted to mean: | 118 * Its return value is interpreted to mean: |
114 * true : Rec is valid | 119 * true : Rec is valid |
115 * false : Rec is "stale" -- the cache will purge it. | 120 * false : Rec is "stale" -- the cache will purge it. |
116 */ | 121 */ |
117 static bool Find(const Key& key, VisitorProc, void* context); | 122 static bool Find(const Key& key, FindVisitor, void* context); |
118 static void Add(Rec*); | 123 static void Add(Rec*); |
119 | 124 |
120 static size_t GetTotalBytesUsed(); | 125 static size_t GetTotalBytesUsed(); |
121 static size_t GetTotalByteLimit(); | 126 static size_t GetTotalByteLimit(); |
122 static size_t SetTotalByteLimit(size_t newLimit); | 127 static size_t SetTotalByteLimit(size_t newLimit); |
123 | 128 |
124 static size_t SetSingleAllocationByteLimit(size_t); | 129 static size_t SetSingleAllocationByteLimit(size_t); |
125 static size_t GetSingleAllocationByteLimit(); | 130 static size_t GetSingleAllocationByteLimit(); |
126 static size_t GetEffectiveSingleAllocationByteLimit(); | 131 static size_t GetEffectiveSingleAllocationByteLimit(); |
127 | 132 |
133 /** | |
134 * Purge all Recs that match the specified sharedID. Passing 0 does nothing . | |
135 */ | |
136 static void PurgeSharedID(uint64_t sharedID); | |
mtklein
2015/02/24 16:43:39
Seems like we don't need both PurgeSharedID and Po
reed2
2015/02/24 17:44:52
Ah, good point. Don't need the purger, just the po
| |
137 | |
128 static void PurgeAll(); | 138 static void PurgeAll(); |
129 | 139 |
130 /** | 140 /** |
131 * Returns the DiscardableFactory used by the global cache, or NULL. | 141 * Returns the DiscardableFactory used by the global cache, or NULL. |
132 */ | 142 */ |
133 static DiscardableFactory GetDiscardableFactory(); | 143 static DiscardableFactory GetDiscardableFactory(); |
134 | 144 |
135 /** | 145 /** |
136 * Use this allocator for bitmaps, so they can use ashmem when available. | 146 * 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. | 147 * Returns NULL if the ResourceCache has not been initialized with a Discard ableFactory. |
138 */ | 148 */ |
139 static SkBitmap::Allocator* GetAllocator(); | 149 static SkBitmap::Allocator* GetAllocator(); |
140 | 150 |
141 static SkCachedData* NewCachedData(size_t bytes); | 151 static SkCachedData* NewCachedData(size_t bytes); |
142 | 152 |
153 static void PostPurgeSharedID(uint64_t sharedID); | |
154 | |
143 /** | 155 /** |
144 * Call SkDebugf() with diagnostic information about the state of the cache | 156 * Call SkDebugf() with diagnostic information about the state of the cache |
145 */ | 157 */ |
146 static void Dump(); | 158 static void Dump(); |
147 | 159 |
148 /////////////////////////////////////////////////////////////////////////// | 160 /////////////////////////////////////////////////////////////////////////// |
149 | 161 |
150 /** | 162 /** |
151 * Construct the cache to call DiscardableFactory when it | 163 * Construct the cache to call DiscardableFactory when it |
152 * allocates memory for the pixels. In this mode, the cache has | 164 * allocates memory for the pixels. In this mode, the cache has |
(...skipping 14 matching lines...) Expand all Loading... | |
167 | 179 |
168 /** | 180 /** |
169 * Returns true if the visitor was called on a matching Key, and the visito r returned true. | 181 * Returns true if the visitor was called on a matching Key, and the visito r returned true. |
170 * | 182 * |
171 * find() will search the cache for the specified Key. If no match is found , return false and | 183 * 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. | 184 * do not call the VisitorProc. If a match is found, return whatever the vi sitor returns. |
173 * Its return value is interpreted to mean: | 185 * Its return value is interpreted to mean: |
174 * true : Rec is valid | 186 * true : Rec is valid |
175 * false : Rec is "stale" -- the cache will purge it. | 187 * false : Rec is "stale" -- the cache will purge it. |
176 */ | 188 */ |
177 bool find(const Key&, VisitorProc, void* context); | 189 bool find(const Key&, FindVisitor, void* context); |
178 void add(Rec*); | 190 void add(Rec*); |
179 | 191 |
180 size_t getTotalBytesUsed() const { return fTotalBytesUsed; } | 192 size_t getTotalBytesUsed() const { return fTotalBytesUsed; } |
181 size_t getTotalByteLimit() const { return fTotalByteLimit; } | 193 size_t getTotalByteLimit() const { return fTotalByteLimit; } |
182 | 194 |
183 /** | 195 /** |
184 * This is respected by SkBitmapProcState::possiblyScaleImage. | 196 * This is respected by SkBitmapProcState::possiblyScaleImage. |
185 * 0 is no maximum at all; this is the default. | 197 * 0 is no maximum at all; this is the default. |
186 * setSingleAllocationByteLimit() returns the previous value. | 198 * setSingleAllocationByteLimit() returns the previous value. |
187 */ | 199 */ |
188 size_t setSingleAllocationByteLimit(size_t maximumAllocationSize); | 200 size_t setSingleAllocationByteLimit(size_t maximumAllocationSize); |
189 size_t getSingleAllocationByteLimit() const; | 201 size_t getSingleAllocationByteLimit() const; |
190 // returns the logical single allocation size (pinning against the budget wh en the cache | 202 // returns the logical single allocation size (pinning against the budget wh en the cache |
191 // is not backed by discardable memory. | 203 // is not backed by discardable memory. |
192 size_t getEffectiveSingleAllocationByteLimit() const; | 204 size_t getEffectiveSingleAllocationByteLimit() const; |
193 | 205 |
194 /** | 206 /** |
195 * Set the maximum number of bytes available to this cache. If the current | 207 * 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 | 208 * cache exceeds this new value, it will be purged to try to fit within |
197 * this new limit. | 209 * this new limit. |
198 */ | 210 */ |
199 size_t setTotalByteLimit(size_t newLimit); | 211 size_t setTotalByteLimit(size_t newLimit); |
200 | 212 |
213 void purgeSharedID(uint64_t sharedID); | |
mtklein
2015/02/24 16:43:39
Probably can be private?
reed2
2015/02/24 17:44:52
public like the others for testing. (which I need
| |
214 | |
201 void purgeAll() { | 215 void purgeAll() { |
202 this->purgeAsNeeded(true); | 216 this->purgeAsNeeded(true); |
203 } | 217 } |
204 | 218 |
205 DiscardableFactory discardableFactory() const { return fDiscardableFactory; } | 219 DiscardableFactory discardableFactory() const { return fDiscardableFactory; } |
206 SkBitmap::Allocator* allocator() const { return fAllocator; }; | 220 SkBitmap::Allocator* allocator() const { return fAllocator; }; |
207 | 221 |
208 SkCachedData* newCachedData(size_t bytes); | 222 SkCachedData* newCachedData(size_t bytes); |
209 | 223 |
210 /** | 224 /** |
(...skipping 10 matching lines...) Expand all Loading... | |
221 | 235 |
222 DiscardableFactory fDiscardableFactory; | 236 DiscardableFactory fDiscardableFactory; |
223 // the allocator is NULL or one that matches discardables | 237 // the allocator is NULL or one that matches discardables |
224 SkBitmap::Allocator* fAllocator; | 238 SkBitmap::Allocator* fAllocator; |
225 | 239 |
226 size_t fTotalBytesUsed; | 240 size_t fTotalBytesUsed; |
227 size_t fTotalByteLimit; | 241 size_t fTotalByteLimit; |
228 size_t fSingleAllocationByteLimit; | 242 size_t fSingleAllocationByteLimit; |
229 int fCount; | 243 int fCount; |
230 | 244 |
245 void checkMessages(); | |
231 void purgeAsNeeded(bool forcePurge = false); | 246 void purgeAsNeeded(bool forcePurge = false); |
232 | 247 |
233 // linklist management | 248 // linklist management |
234 void moveToHead(Rec*); | 249 void moveToHead(Rec*); |
235 void addToHead(Rec*); | 250 void addToHead(Rec*); |
236 void detach(Rec*); | 251 void detach(Rec*); |
237 void remove(Rec*); | 252 void remove(Rec*); |
238 | 253 |
239 void init(); // called by constructors | 254 void init(); // called by constructors |
240 | 255 |
256 static SkTDArray<uint64_t>* DetachPurgeMessages(); | |
257 | |
241 #ifdef SK_DEBUG | 258 #ifdef SK_DEBUG |
242 void validate() const; | 259 void validate() const; |
243 #else | 260 #else |
244 void validate() const {} | 261 void validate() const {} |
245 #endif | 262 #endif |
246 }; | 263 }; |
247 #endif | 264 #endif |
OLD | NEW |