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 SkScaledImageCache_DEFINED | 8 #ifndef SkScaledImageCache_DEFINED |
9 #define SkScaledImageCache_DEFINED | 9 #define SkScaledImageCache_DEFINED |
10 | 10 |
11 #include "SkBitmap.h" | 11 #include "SkBitmap.h" |
12 | 12 |
13 class SkDiscardableMemory; | 13 class SkDiscardableMemory; |
14 class SkMipMap; | 14 class SkMipMap; |
15 | 15 |
16 /** | 16 /** |
17 * Cache object for bitmaps (with possible scale in X Y as part of the key). | 17 * Cache object for bitmaps (with possible scale in X Y as part of the key). |
18 * | 18 * |
19 * Multiple caches can be instantiated, but each instance is not implicitly | 19 * Multiple caches can be instantiated, but each instance is not implicitly |
20 * thread-safe, so if a given instance is to be shared across threads, the | 20 * thread-safe, so if a given instance is to be shared across threads, the |
21 * caller must manage the access itself (e.g. via a mutex). | 21 * caller must manage the access itself (e.g. via a mutex). |
22 * | 22 * |
23 * As a convenience, a global instance is also defined, which can be safely | 23 * As a convenience, a global instance is also defined, which can be safely |
24 * access across threads via the static methods (e.g. FindAndLock, etc.). | 24 * access across threads via the static methods (e.g. FindAndLock, etc.). |
25 */ | 25 */ |
26 class SkScaledImageCache { | 26 class SkScaledImageCache { |
27 public: | 27 public: |
28 struct ID; | 28 struct ID; |
29 | 29 |
| 30 struct Key { |
| 31 // Call this to access your private contents. Must not use the address a
fter calling init() |
| 32 void* writableContents() { return this + 1; } |
| 33 |
| 34 // must call this after your private data has been written. |
| 35 // length must be a multiple of 4 |
| 36 void init(size_t length); |
| 37 |
| 38 // This is only valid after having called init(). |
| 39 uint32_t hash() const { return fHash; } |
| 40 |
| 41 bool operator==(const Key& other) const { |
| 42 const uint32_t* a = this->as32(); |
| 43 const uint32_t* b = other.as32(); |
| 44 for (int i = 0; i < fCount32; ++i) { |
| 45 if (a[i] != b[i]) { |
| 46 return false; |
| 47 } |
| 48 } |
| 49 return true; |
| 50 } |
| 51 |
| 52 // delete using sk_free |
| 53 Key* clone() const; |
| 54 |
| 55 private: |
| 56 // store fCount32 first, so we don't consider it in operator< |
| 57 int32_t fCount32; // 2 + user contents count32 |
| 58 uint32_t fHash; |
| 59 /* uint32_t fContents32[] */ |
| 60 |
| 61 const uint32_t* as32() const { return (const uint32_t*)this; } |
| 62 const uint32_t* as32SkipCount() const { return this->as32() + 1; } |
| 63 }; |
| 64 |
30 /** | 65 /** |
31 * Returns a locked/pinned SkDiscardableMemory instance for the specified | 66 * Returns a locked/pinned SkDiscardableMemory instance for the specified |
32 * number of bytes, or NULL on failure. | 67 * number of bytes, or NULL on failure. |
33 */ | 68 */ |
34 typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes); | 69 typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes); |
35 | 70 |
36 /* | 71 /* |
37 * The following static methods are thread-safe wrappers around a global | 72 * The following static methods are thread-safe wrappers around a global |
38 * instance of this cache. | 73 * instance of this cache. |
39 */ | 74 */ |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
166 | 201 |
167 SkBitmap::Allocator* allocator() const { return fAllocator; }; | 202 SkBitmap::Allocator* allocator() const { return fAllocator; }; |
168 | 203 |
169 /** | 204 /** |
170 * Call SkDebugf() with diagnostic information about the state of the cache | 205 * Call SkDebugf() with diagnostic information about the state of the cache |
171 */ | 206 */ |
172 void dump() const; | 207 void dump() const; |
173 | 208 |
174 public: | 209 public: |
175 struct Rec; | 210 struct Rec; |
176 struct Key; | |
177 private: | 211 private: |
178 Rec* fHead; | 212 Rec* fHead; |
179 Rec* fTail; | 213 Rec* fTail; |
180 | 214 |
181 class Hash; | 215 class Hash; |
182 Hash* fHash; | 216 Hash* fHash; |
183 | 217 |
184 DiscardableFactory fDiscardableFactory; | 218 DiscardableFactory fDiscardableFactory; |
185 // the allocator is NULL or one that matches discardables | 219 // the allocator is NULL or one that matches discardables |
186 SkBitmap::Allocator* fAllocator; | 220 SkBitmap::Allocator* fAllocator; |
(...skipping 18 matching lines...) Expand all Loading... |
205 | 239 |
206 void init(); // called by constructors | 240 void init(); // called by constructors |
207 | 241 |
208 #ifdef SK_DEBUG | 242 #ifdef SK_DEBUG |
209 void validate() const; | 243 void validate() const; |
210 #else | 244 #else |
211 void validate() const {} | 245 void validate() const {} |
212 #endif | 246 #endif |
213 }; | 247 }; |
214 #endif | 248 #endif |
OLD | NEW |