OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2013 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 8 #ifndef SkScaledImageCache_DEFINED |
| 9 #define SkScaledImageCache_DEFINED |
| 10 |
| 11 #include "SkBitmap.h" |
| 12 |
| 13 /** |
| 14 * Cache object for bitmaps (with possible scale in X Y as part of the key). |
| 15 * |
| 16 * 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 |
| 18 * caller must manage the access itself (e.g. via a mutex). |
| 19 * |
| 20 * 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.). |
| 22 */ |
| 23 class SkScaledImageCache { |
| 24 public: |
| 25 struct ID; |
| 26 |
| 27 /* |
| 28 * The following static methods are thread-safe wrappers around a global |
| 29 * instance of this cache. |
| 30 */ |
| 31 |
| 32 static ID* FindAndLock(const SkBitmap& original, SkScalar scaleX, |
| 33 SkScalar scaleY, SkBitmap* scaled); |
| 34 |
| 35 static ID* AddAndLock(const SkBitmap& original, SkScalar scaleX, |
| 36 SkScalar scaleY, const SkBitmap& scaled); |
| 37 |
| 38 static void Unlock(ID*); |
| 39 |
| 40 static size_t GetBytesUsed(); |
| 41 static size_t GetByteLimit(); |
| 42 static size_t SetByteLimit(size_t newLimit); |
| 43 |
| 44 /////////////////////////////////////////////////////////////////////////// |
| 45 |
| 46 SkScaledImageCache(size_t byteLimit); |
| 47 ~SkScaledImageCache(); |
| 48 |
| 49 /** |
| 50 * 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 |
| 52 * the cache when you are done using scaled. |
| 53 * |
| 54 * If a match is not found, scaled will be unmodifed, and NULL will be |
| 55 * returned. |
| 56 */ |
| 57 ID* findAndLock(const SkBitmap& original, SkScalar scaleX, |
| 58 SkScalar scaleY, SkBitmap* scaled); |
| 59 |
| 60 /** |
| 61 * 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. |
| 63 */ |
| 64 ID* addAndLock(const SkBitmap& original, SkScalar scaleX, |
| 65 SkScalar scaleY, const SkBitmap& scaled); |
| 66 |
| 67 /** |
| 68 * Given a non-null ID ptr returned by either findAndLock or addAndLock, |
| 69 * this releases the associated resources to be available to be purged |
| 70 * if needed. After this, the cached bitmap should no longer be |
| 71 * referenced by the caller. |
| 72 */ |
| 73 void unlock(ID*); |
| 74 |
| 75 size_t getBytesUsed() const { return fBytesUsed; } |
| 76 size_t getByteLimit() const { return fByteLimit; } |
| 77 |
| 78 /** |
| 79 * 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 |
| 81 * this new limit. |
| 82 */ |
| 83 size_t setByteLimit(size_t newLimit); |
| 84 |
| 85 private: |
| 86 struct Rec; |
| 87 Rec* fHead; |
| 88 Rec* fTail; |
| 89 |
| 90 size_t fBytesUsed; |
| 91 size_t fByteLimit; |
| 92 int fCount; |
| 93 |
| 94 void purgeAsNeeded(); |
| 95 |
| 96 // linklist management |
| 97 void moveToHead(Rec*); |
| 98 void addToHead(Rec*); |
| 99 void detach(Rec*); |
| 100 #ifdef SK_DEBUG |
| 101 void validate() const; |
| 102 #else |
| 103 void validate() const {} |
| 104 #endif |
| 105 }; |
| 106 |
| 107 #endif |
OLD | NEW |