| 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 class SkDiscardableMemory; | |
| 14 class SkMipMap; | |
| 15 | |
| 16 /** | |
| 17 * Cache object for bitmaps (with possible scale in X Y as part of the key). | |
| 18 * | |
| 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 | |
| 21 * caller must manage the access itself (e.g. via a mutex). | |
| 22 * | |
| 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.). | |
| 25 */ | |
| 26 class SkScaledImageCache { | |
| 27 public: | |
| 28 struct Key { | |
| 29 // Call this to access your private contents. Must not use the address a
fter calling init() | |
| 30 void* writableContents() { return this + 1; } | |
| 31 | |
| 32 // must call this after your private data has been written. | |
| 33 // length must be a multiple of 4 | |
| 34 void init(size_t length); | |
| 35 | |
| 36 // This is only valid after having called init(). | |
| 37 uint32_t hash() const { return fHash; } | |
| 38 | |
| 39 bool operator==(const Key& other) const { | |
| 40 const uint32_t* a = this->as32(); | |
| 41 const uint32_t* b = other.as32(); | |
| 42 for (int i = 0; i < fCount32; ++i) { | |
| 43 if (a[i] != b[i]) { | |
| 44 return false; | |
| 45 } | |
| 46 } | |
| 47 return true; | |
| 48 } | |
| 49 | |
| 50 private: | |
| 51 // store fCount32 first, so we don't consider it in operator< | |
| 52 int32_t fCount32; // 2 + user contents count32 | |
| 53 uint32_t fHash; | |
| 54 /* uint32_t fContents32[] */ | |
| 55 | |
| 56 const uint32_t* as32() const { return (const uint32_t*)this; } | |
| 57 const uint32_t* as32SkipCount() const { return this->as32() + 1; } | |
| 58 }; | |
| 59 | |
| 60 struct Rec { | |
| 61 typedef SkScaledImageCache::Key Key; | |
| 62 | |
| 63 Rec() : fLockCount(1) {} | |
| 64 virtual ~Rec() {} | |
| 65 | |
| 66 uint32_t getHash() const { return this->getKey().hash(); } | |
| 67 | |
| 68 virtual const Key& getKey() const = 0; | |
| 69 virtual size_t bytesUsed() const = 0; | |
| 70 | |
| 71 // for SkTDynamicHash::Traits | |
| 72 static uint32_t Hash(const Key& key) { return key.hash(); } | |
| 73 static const Key& GetKey(const Rec& rec) { return rec.getKey(); } | |
| 74 | |
| 75 private: | |
| 76 Rec* fNext; | |
| 77 Rec* fPrev; | |
| 78 int32_t fLockCount; | |
| 79 int32_t fPad; | |
| 80 | |
| 81 friend class SkScaledImageCache; | |
| 82 }; | |
| 83 | |
| 84 typedef const Rec* ID; | |
| 85 | |
| 86 /** | |
| 87 * Returns a locked/pinned SkDiscardableMemory instance for the specified | |
| 88 * number of bytes, or NULL on failure. | |
| 89 */ | |
| 90 typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes); | |
| 91 | |
| 92 /* | |
| 93 * The following static methods are thread-safe wrappers around a global | |
| 94 * instance of this cache. | |
| 95 */ | |
| 96 | |
| 97 static const Rec* FindAndLock(const Key& key); | |
| 98 static const Rec* AddAndLock(Rec*); | |
| 99 static void Add(Rec*); | |
| 100 static void Unlock(ID); | |
| 101 | |
| 102 static size_t GetTotalBytesUsed(); | |
| 103 static size_t GetTotalByteLimit(); | |
| 104 static size_t SetTotalByteLimit(size_t newLimit); | |
| 105 | |
| 106 static size_t SetSingleAllocationByteLimit(size_t); | |
| 107 static size_t GetSingleAllocationByteLimit(); | |
| 108 | |
| 109 static SkBitmap::Allocator* GetAllocator(); | |
| 110 | |
| 111 /** | |
| 112 * Call SkDebugf() with diagnostic information about the state of the cache | |
| 113 */ | |
| 114 static void Dump(); | |
| 115 | |
| 116 /////////////////////////////////////////////////////////////////////////// | |
| 117 | |
| 118 /** | |
| 119 * Construct the cache to call DiscardableFactory when it | |
| 120 * allocates memory for the pixels. In this mode, the cache has | |
| 121 * not explicit budget, and so methods like getTotalBytesUsed() | |
| 122 * and getTotalByteLimit() will return 0, and setTotalByteLimit | |
| 123 * will ignore its argument and return 0. | |
| 124 */ | |
| 125 SkScaledImageCache(DiscardableFactory); | |
| 126 | |
| 127 /** | |
| 128 * Construct the cache, allocating memory with malloc, and respect the | |
| 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 | |
| 131 * changed at runtime with setTotalByteLimit. | |
| 132 */ | |
| 133 explicit SkScaledImageCache(size_t byteLimit); | |
| 134 ~SkScaledImageCache(); | |
| 135 | |
| 136 const Rec* findAndLock(const Key& key); | |
| 137 const Rec* addAndLock(Rec*); | |
| 138 void add(Rec*); | |
| 139 | |
| 140 /** | |
| 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 | |
| 143 * if needed. After this, the cached bitmap should no longer be | |
| 144 * referenced by the caller. | |
| 145 */ | |
| 146 void unlock(ID); | |
| 147 | |
| 148 size_t getTotalBytesUsed() const { return fTotalBytesUsed; } | |
| 149 size_t getTotalByteLimit() const { return fTotalByteLimit; } | |
| 150 | |
| 151 /** | |
| 152 * This is respected by SkBitmapProcState::possiblyScaleImage. | |
| 153 * 0 is no maximum at all; this is the default. | |
| 154 * setSingleAllocationByteLimit() returns the previous value. | |
| 155 */ | |
| 156 size_t setSingleAllocationByteLimit(size_t maximumAllocationSize); | |
| 157 size_t getSingleAllocationByteLimit() const; | |
| 158 /** | |
| 159 * Set the maximum number of bytes available to this cache. If the current | |
| 160 * cache exceeds this new value, it will be purged to try to fit within | |
| 161 * this new limit. | |
| 162 */ | |
| 163 size_t setTotalByteLimit(size_t newLimit); | |
| 164 | |
| 165 SkBitmap::Allocator* allocator() const { return fAllocator; }; | |
| 166 | |
| 167 /** | |
| 168 * Call SkDebugf() with diagnostic information about the state of the cache | |
| 169 */ | |
| 170 void dump() const; | |
| 171 | |
| 172 private: | |
| 173 Rec* fHead; | |
| 174 Rec* fTail; | |
| 175 | |
| 176 class Hash; | |
| 177 Hash* fHash; | |
| 178 | |
| 179 DiscardableFactory fDiscardableFactory; | |
| 180 // the allocator is NULL or one that matches discardables | |
| 181 SkBitmap::Allocator* fAllocator; | |
| 182 | |
| 183 size_t fTotalBytesUsed; | |
| 184 size_t fTotalByteLimit; | |
| 185 size_t fSingleAllocationByteLimit; | |
| 186 int fCount; | |
| 187 | |
| 188 void purgeRec(Rec*); | |
| 189 void purgeAsNeeded(); | |
| 190 | |
| 191 // linklist management | |
| 192 void moveToHead(Rec*); | |
| 193 void addToHead(Rec*); | |
| 194 void detach(Rec*); | |
| 195 | |
| 196 void init(); // called by constructors | |
| 197 | |
| 198 #ifdef SK_DEBUG | |
| 199 void validate() const; | |
| 200 #else | |
| 201 void validate() const {} | |
| 202 #endif | |
| 203 }; | |
| 204 #endif | |
| OLD | NEW |