| 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 SkMipMap; |
| 14 |
| 13 /** | 15 /** |
| 14 * Cache object for bitmaps (with possible scale in X Y as part of the key). | 16 * Cache object for bitmaps (with possible scale in X Y as part of the key). |
| 15 * | 17 * |
| 16 * Multiple caches can be instantiated, but each instance is not implicitly | 18 * Multiple caches can be instantiated, but each instance is not implicitly |
| 17 * thread-safe, so if a given instance is to be shared across threads, the | 19 * thread-safe, so if a given instance is to be shared across threads, the |
| 18 * caller must manage the access itself (e.g. via a mutex). | 20 * caller must manage the access itself (e.g. via a mutex). |
| 19 * | 21 * |
| 20 * As a convenience, a global instance is also defined, which can be safely | 22 * As a convenience, a global instance is also defined, which can be safely |
| 21 * access across threads via the static methods (e.g. FindAndLock, etc.). | 23 * access across threads via the static methods (e.g. FindAndLock, etc.). |
| 22 */ | 24 */ |
| 23 class SkScaledImageCache { | 25 class SkScaledImageCache { |
| 24 public: | 26 public: |
| 25 struct ID; | 27 struct ID; |
| 26 | 28 |
| 27 /* | 29 /* |
| 28 * The following static methods are thread-safe wrappers around a global | 30 * The following static methods are thread-safe wrappers around a global |
| 29 * instance of this cache. | 31 * instance of this cache. |
| 30 */ | 32 */ |
| 31 | 33 |
| 32 static ID* FindAndLock(const SkBitmap& original, SkScalar scaleX, | 34 static ID* FindAndLock(const SkBitmap& original, SkScalar scaleX, |
| 33 SkScalar scaleY, SkBitmap* scaled); | 35 SkScalar scaleY, SkBitmap* scaled); |
| 34 | 36 static ID* FindAndLockMip(const SkBitmap& original, SkMipMap const**); |
| 37 |
| 35 static ID* AddAndLock(const SkBitmap& original, SkScalar scaleX, | 38 static ID* AddAndLock(const SkBitmap& original, SkScalar scaleX, |
| 36 SkScalar scaleY, const SkBitmap& scaled); | 39 SkScalar scaleY, const SkBitmap& scaled); |
| 37 | 40 static ID* AddAndLockMip(const SkBitmap& original, const SkMipMap*); |
| 41 |
| 38 static void Unlock(ID*); | 42 static void Unlock(ID*); |
| 39 | 43 |
| 40 static size_t GetBytesUsed(); | 44 static size_t GetBytesUsed(); |
| 41 static size_t GetByteLimit(); | 45 static size_t GetByteLimit(); |
| 42 static size_t SetByteLimit(size_t newLimit); | 46 static size_t SetByteLimit(size_t newLimit); |
| 43 | 47 |
| 44 /////////////////////////////////////////////////////////////////////////// | 48 /////////////////////////////////////////////////////////////////////////// |
| 45 | 49 |
| 46 SkScaledImageCache(size_t byteLimit); | 50 SkScaledImageCache(size_t byteLimit); |
| 47 ~SkScaledImageCache(); | 51 ~SkScaledImageCache(); |
| 48 | 52 |
| 49 /** | 53 /** |
| 50 * Search the cache for a scaled version of original. If found, return it | 54 * Search the cache for a scaled version of original. If found, return it |
| 51 * in scaled, and return its ID pointer. Use the returned ptr to unlock | 55 * in scaled, and return its ID pointer. Use the returned ptr to unlock |
| 52 * the cache when you are done using scaled. | 56 * the cache when you are done using scaled. |
| 53 * | 57 * |
| 54 * If a match is not found, scaled will be unmodifed, and NULL will be | 58 * If a match is not found, scaled will be unmodifed, and NULL will be |
| 55 * returned. | 59 * returned. |
| 56 */ | 60 */ |
| 57 ID* findAndLock(const SkBitmap& original, SkScalar scaleX, | 61 ID* findAndLock(const SkBitmap& original, SkScalar scaleX, |
| 58 SkScalar scaleY, SkBitmap* scaled); | 62 SkScalar scaleY, SkBitmap* scaled); |
| 63 ID* findAndLockMip(const SkBitmap& original, SkMipMap const**); |
| 59 | 64 |
| 60 /** | 65 /** |
| 61 * To add a new (scaled) bitmap to the cache, call AddAndLock. Use the | 66 * To add a new (scaled) bitmap to the cache, call AddAndLock. Use the |
| 62 * returned ptr to unlock the cache when you are done using scaled. | 67 * returned ptr to unlock the cache when you are done using scaled. |
| 63 */ | 68 */ |
| 64 ID* addAndLock(const SkBitmap& original, SkScalar scaleX, | 69 ID* addAndLock(const SkBitmap& original, SkScalar scaleX, |
| 65 SkScalar scaleY, const SkBitmap& scaled); | 70 SkScalar scaleY, const SkBitmap& scaled); |
| 71 ID* addAndLockMip(const SkBitmap& original, const SkMipMap*); |
| 66 | 72 |
| 67 /** | 73 /** |
| 68 * Given a non-null ID ptr returned by either findAndLock or addAndLock, | 74 * Given a non-null ID ptr returned by either findAndLock or addAndLock, |
| 69 * this releases the associated resources to be available to be purged | 75 * this releases the associated resources to be available to be purged |
| 70 * if needed. After this, the cached bitmap should no longer be | 76 * if needed. After this, the cached bitmap should no longer be |
| 71 * referenced by the caller. | 77 * referenced by the caller. |
| 72 */ | 78 */ |
| 73 void unlock(ID*); | 79 void unlock(ID*); |
| 74 | 80 |
| 75 size_t getBytesUsed() const { return fBytesUsed; } | 81 size_t getBytesUsed() const { return fBytesUsed; } |
| 76 size_t getByteLimit() const { return fByteLimit; } | 82 size_t getByteLimit() const { return fByteLimit; } |
| 77 | 83 |
| 78 /** | 84 /** |
| 79 * Set the maximum number of bytes available to this cache. If the current | 85 * Set the maximum number of bytes available to this cache. If the current |
| 80 * cache exceeds this new value, it will be purged to try to fit within | 86 * cache exceeds this new value, it will be purged to try to fit within |
| 81 * this new limit. | 87 * this new limit. |
| 82 */ | 88 */ |
| 83 size_t setByteLimit(size_t newLimit); | 89 size_t setByteLimit(size_t newLimit); |
| 84 | 90 |
| 85 private: | 91 private: |
| 86 struct Rec; | 92 struct Rec; |
| 87 Rec* fHead; | 93 Rec* fHead; |
| 88 Rec* fTail; | 94 Rec* fTail; |
| 89 | 95 |
| 90 size_t fBytesUsed; | 96 size_t fBytesUsed; |
| 91 size_t fByteLimit; | 97 size_t fByteLimit; |
| 92 int fCount; | 98 int fCount; |
| 93 | 99 |
| 100 Rec* findAndLock(const SkBitmap& original, SkScalar sx, SkScalar sy); |
| 101 |
| 94 void purgeAsNeeded(); | 102 void purgeAsNeeded(); |
| 95 | 103 |
| 96 // linklist management | 104 // linklist management |
| 97 void moveToHead(Rec*); | 105 void moveToHead(Rec*); |
| 98 void addToHead(Rec*); | 106 void addToHead(Rec*); |
| 99 void detach(Rec*); | 107 void detach(Rec*); |
| 100 #ifdef SK_DEBUG | 108 #ifdef SK_DEBUG |
| 101 void validate() const; | 109 void validate() const; |
| 102 #else | 110 #else |
| 103 void validate() const {} | 111 void validate() const {} |
| 104 #endif | 112 #endif |
| 105 }; | 113 }; |
| 106 | 114 |
| 107 #endif | 115 #endif |
| OLD | NEW |