| 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 SkResourceCache_DEFINED |
| 9 #define SkScaledImageCache_DEFINED | 9 #define SkResourceCache_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 SkResourceCache { |
| 27 public: | 27 public: |
| 28 struct Key { | 28 struct Key { |
| 29 // Call this to access your private contents. Must not use the address a
fter calling init() | 29 // Call this to access your private contents. Must not use the address a
fter calling init() |
| 30 void* writableContents() { return this + 1; } | 30 void* writableContents() { return this + 1; } |
| 31 | 31 |
| 32 // must call this after your private data has been written. | 32 // must call this after your private data has been written. |
| 33 // length must be a multiple of 4 | 33 // length must be a multiple of 4 |
| 34 void init(size_t length); | 34 void init(size_t length); |
| 35 | 35 |
| 36 // This is only valid after having called init(). | 36 // This is only valid after having called init(). |
| (...skipping 14 matching lines...) Expand all Loading... |
| 51 // store fCount32 first, so we don't consider it in operator< | 51 // store fCount32 first, so we don't consider it in operator< |
| 52 int32_t fCount32; // 2 + user contents count32 | 52 int32_t fCount32; // 2 + user contents count32 |
| 53 uint32_t fHash; | 53 uint32_t fHash; |
| 54 /* uint32_t fContents32[] */ | 54 /* uint32_t fContents32[] */ |
| 55 | 55 |
| 56 const uint32_t* as32() const { return (const uint32_t*)this; } | 56 const uint32_t* as32() const { return (const uint32_t*)this; } |
| 57 const uint32_t* as32SkipCount() const { return this->as32() + 1; } | 57 const uint32_t* as32SkipCount() const { return this->as32() + 1; } |
| 58 }; | 58 }; |
| 59 | 59 |
| 60 struct Rec { | 60 struct Rec { |
| 61 typedef SkScaledImageCache::Key Key; | 61 typedef SkResourceCache::Key Key; |
| 62 | 62 |
| 63 Rec() : fLockCount(1) {} | 63 Rec() : fLockCount(1) {} |
| 64 virtual ~Rec() {} | 64 virtual ~Rec() {} |
| 65 | 65 |
| 66 uint32_t getHash() const { return this->getKey().hash(); } | 66 uint32_t getHash() const { return this->getKey().hash(); } |
| 67 | 67 |
| 68 virtual const Key& getKey() const = 0; | 68 virtual const Key& getKey() const = 0; |
| 69 virtual size_t bytesUsed() const = 0; | 69 virtual size_t bytesUsed() const = 0; |
| 70 | 70 |
| 71 // for SkTDynamicHash::Traits | 71 // for SkTDynamicHash::Traits |
| 72 static uint32_t Hash(const Key& key) { return key.hash(); } | 72 static uint32_t Hash(const Key& key) { return key.hash(); } |
| 73 static const Key& GetKey(const Rec& rec) { return rec.getKey(); } | 73 static const Key& GetKey(const Rec& rec) { return rec.getKey(); } |
| 74 | 74 |
| 75 private: | 75 private: |
| 76 Rec* fNext; | 76 Rec* fNext; |
| 77 Rec* fPrev; | 77 Rec* fPrev; |
| 78 int32_t fLockCount; | 78 int32_t fLockCount; |
| 79 int32_t fPad; | 79 int32_t fPad; |
| 80 | 80 |
| 81 friend class SkScaledImageCache; | 81 friend class SkResourceCache; |
| 82 }; | 82 }; |
| 83 | 83 |
| 84 typedef const Rec* ID; | 84 typedef const Rec* ID; |
| 85 | 85 |
| 86 /** | 86 /** |
| 87 * Returns a locked/pinned SkDiscardableMemory instance for the specified | 87 * Returns a locked/pinned SkDiscardableMemory instance for the specified |
| 88 * number of bytes, or NULL on failure. | 88 * number of bytes, or NULL on failure. |
| 89 */ | 89 */ |
| 90 typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes); | 90 typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes); |
| 91 | 91 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 115 | 115 |
| 116 /////////////////////////////////////////////////////////////////////////// | 116 /////////////////////////////////////////////////////////////////////////// |
| 117 | 117 |
| 118 /** | 118 /** |
| 119 * Construct the cache to call DiscardableFactory when it | 119 * Construct the cache to call DiscardableFactory when it |
| 120 * allocates memory for the pixels. In this mode, the cache has | 120 * allocates memory for the pixels. In this mode, the cache has |
| 121 * not explicit budget, and so methods like getTotalBytesUsed() | 121 * not explicit budget, and so methods like getTotalBytesUsed() |
| 122 * and getTotalByteLimit() will return 0, and setTotalByteLimit | 122 * and getTotalByteLimit() will return 0, and setTotalByteLimit |
| 123 * will ignore its argument and return 0. | 123 * will ignore its argument and return 0. |
| 124 */ | 124 */ |
| 125 SkScaledImageCache(DiscardableFactory); | 125 SkResourceCache(DiscardableFactory); |
| 126 | 126 |
| 127 /** | 127 /** |
| 128 * Construct the cache, allocating memory with malloc, and respect the | 128 * Construct the cache, allocating memory with malloc, and respect the |
| 129 * byteLimit, purging automatically when a new image is added to the cache | 129 * byteLimit, purging automatically when a new image is added to the cache |
| 130 * that pushes the total bytesUsed over the limit. Note: The limit can be | 130 * that pushes the total bytesUsed over the limit. Note: The limit can be |
| 131 * changed at runtime with setTotalByteLimit. | 131 * changed at runtime with setTotalByteLimit. |
| 132 */ | 132 */ |
| 133 explicit SkScaledImageCache(size_t byteLimit); | 133 explicit SkResourceCache(size_t byteLimit); |
| 134 ~SkScaledImageCache(); | 134 ~SkResourceCache(); |
| 135 | 135 |
| 136 const Rec* findAndLock(const Key& key); | 136 const Rec* findAndLock(const Key& key); |
| 137 const Rec* addAndLock(Rec*); | 137 const Rec* addAndLock(Rec*); |
| 138 void add(Rec*); | 138 void add(Rec*); |
| 139 | 139 |
| 140 /** | 140 /** |
| 141 * Given a non-null ID ptr returned by either findAndLock or addAndLock, | 141 * Given a non-null ID ptr returned by either findAndLock or addAndLock, |
| 142 * this releases the associated resources to be available to be purged | 142 * this releases the associated resources to be available to be purged |
| 143 * if needed. After this, the cached bitmap should no longer be | 143 * if needed. After this, the cached bitmap should no longer be |
| 144 * referenced by the caller. | 144 * referenced by the caller. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 | 195 |
| 196 void init(); // called by constructors | 196 void init(); // called by constructors |
| 197 | 197 |
| 198 #ifdef SK_DEBUG | 198 #ifdef SK_DEBUG |
| 199 void validate() const; | 199 void validate() const; |
| 200 #else | 200 #else |
| 201 void validate() const {} | 201 void validate() const {} |
| 202 #endif | 202 #endif |
| 203 }; | 203 }; |
| 204 #endif | 204 #endif |
| OLD | NEW |