| 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 SkResourceCache_DEFINED | 8 #ifndef SkResourceCache_DEFINED |
| 9 #define SkResourceCache_DEFINED | 9 #define SkResourceCache_DEFINED |
| 10 | 10 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 SkResourceCache::Key Key; | 61 typedef SkResourceCache::Key Key; |
| 62 | 62 |
| 63 Rec() {} | 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 | 79 |
| 79 friend class SkResourceCache; | 80 friend class SkResourceCache; |
| 80 }; | 81 }; |
| 81 | 82 |
| 82 typedef const Rec* ID; | 83 typedef const Rec* ID; |
| 83 | 84 |
| 84 /** | 85 /** |
| 85 * Callback function for find(). If called, the cache will have found a mat
ch for the | |
| 86 * specified Key, and will pass in the corresponding Rec, along with a call
er-specified | |
| 87 * context. The function can read the data in Rec, and copy whatever it lik
es into context | |
| 88 * (casting context to whatever it really is). | |
| 89 * | |
| 90 * The return value determines what the cache will do with the Rec. If the
function returns | |
| 91 * true, then the Rec is considered "valid". If false is returned, the Rec
will be considered | |
| 92 * "stale" and will be purged from the cache. | |
| 93 */ | |
| 94 typedef bool (*VisitorProc)(const Rec&, void* context); | |
| 95 | |
| 96 /** | |
| 97 * Returns a locked/pinned SkDiscardableMemory instance for the specified | 86 * Returns a locked/pinned SkDiscardableMemory instance for the specified |
| 98 * number of bytes, or NULL on failure. | 87 * number of bytes, or NULL on failure. |
| 99 */ | 88 */ |
| 100 typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes); | 89 typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes); |
| 101 | 90 |
| 102 /* | 91 /* |
| 103 * The following static methods are thread-safe wrappers around a global | 92 * The following static methods are thread-safe wrappers around a global |
| 104 * instance of this cache. | 93 * instance of this cache. |
| 105 */ | 94 */ |
| 106 | 95 |
| 107 /** | 96 static const Rec* FindAndLock(const Key& key); |
| 108 * Returns true if the visitor was called on a matching Key, and the visito
r returned true. | 97 static const Rec* AddAndLock(Rec*); |
| 109 * | |
| 110 * Find() will search the cache for the specified Key. If no match is found
, return false and | |
| 111 * do not call the VisitorProc. If a match is found, return whatever the vi
sitor returns. | |
| 112 * Its return value is interpreted to mean: | |
| 113 * true : Rec is valid | |
| 114 * false : Rec is "stale" -- the cache will purge it. | |
| 115 */ | |
| 116 static bool Find(const Key& key, VisitorProc, void* context); | |
| 117 static void Add(Rec*); | 98 static void Add(Rec*); |
| 99 static void Unlock(ID); |
| 100 static void Remove(ID); |
| 118 | 101 |
| 119 static size_t GetTotalBytesUsed(); | 102 static size_t GetTotalBytesUsed(); |
| 120 static size_t GetTotalByteLimit(); | 103 static size_t GetTotalByteLimit(); |
| 121 static size_t SetTotalByteLimit(size_t newLimit); | 104 static size_t SetTotalByteLimit(size_t newLimit); |
| 122 | 105 |
| 123 static size_t SetSingleAllocationByteLimit(size_t); | 106 static size_t SetSingleAllocationByteLimit(size_t); |
| 124 static size_t GetSingleAllocationByteLimit(); | 107 static size_t GetSingleAllocationByteLimit(); |
| 125 | 108 |
| 126 static void PurgeAll(); | 109 static void PurgeAll(); |
| 127 | 110 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 149 | 132 |
| 150 /** | 133 /** |
| 151 * Construct the cache, allocating memory with malloc, and respect the | 134 * Construct the cache, allocating memory with malloc, and respect the |
| 152 * byteLimit, purging automatically when a new image is added to the cache | 135 * byteLimit, purging automatically when a new image is added to the cache |
| 153 * that pushes the total bytesUsed over the limit. Note: The limit can be | 136 * that pushes the total bytesUsed over the limit. Note: The limit can be |
| 154 * changed at runtime with setTotalByteLimit. | 137 * changed at runtime with setTotalByteLimit. |
| 155 */ | 138 */ |
| 156 explicit SkResourceCache(size_t byteLimit); | 139 explicit SkResourceCache(size_t byteLimit); |
| 157 ~SkResourceCache(); | 140 ~SkResourceCache(); |
| 158 | 141 |
| 142 const Rec* findAndLock(const Key& key); |
| 143 const Rec* addAndLock(Rec*); |
| 144 void add(Rec*); |
| 145 void remove(Rec*); |
| 146 |
| 159 /** | 147 /** |
| 160 * Returns true if the visitor was called on a matching Key, and the visito
r returned true. | 148 * Given a non-null ID ptr returned by either findAndLock or addAndLock, |
| 161 * | 149 * this releases the associated resources to be available to be purged |
| 162 * find() will search the cache for the specified Key. If no match is found
, return false and | 150 * if needed. After this, the cached bitmap should no longer be |
| 163 * do not call the VisitorProc. If a match is found, return whatever the vi
sitor returns. | 151 * referenced by the caller. |
| 164 * Its return value is interpreted to mean: | |
| 165 * true : Rec is valid | |
| 166 * false : Rec is "stale" -- the cache will purge it. | |
| 167 */ | 152 */ |
| 168 bool find(const Key&, VisitorProc, void* context); | 153 void unlock(ID); |
| 169 void add(Rec*); | |
| 170 | 154 |
| 171 size_t getTotalBytesUsed() const { return fTotalBytesUsed; } | 155 size_t getTotalBytesUsed() const { return fTotalBytesUsed; } |
| 172 size_t getTotalByteLimit() const { return fTotalByteLimit; } | 156 size_t getTotalByteLimit() const { return fTotalByteLimit; } |
| 173 | 157 |
| 174 /** | 158 /** |
| 175 * This is respected by SkBitmapProcState::possiblyScaleImage. | 159 * This is respected by SkBitmapProcState::possiblyScaleImage. |
| 176 * 0 is no maximum at all; this is the default. | 160 * 0 is no maximum at all; this is the default. |
| 177 * setSingleAllocationByteLimit() returns the previous value. | 161 * setSingleAllocationByteLimit() returns the previous value. |
| 178 */ | 162 */ |
| 179 size_t setSingleAllocationByteLimit(size_t maximumAllocationSize); | 163 size_t setSingleAllocationByteLimit(size_t maximumAllocationSize); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 211 size_t fTotalByteLimit; | 195 size_t fTotalByteLimit; |
| 212 size_t fSingleAllocationByteLimit; | 196 size_t fSingleAllocationByteLimit; |
| 213 int fCount; | 197 int fCount; |
| 214 | 198 |
| 215 void purgeAsNeeded(bool forcePurge = false); | 199 void purgeAsNeeded(bool forcePurge = false); |
| 216 | 200 |
| 217 // linklist management | 201 // linklist management |
| 218 void moveToHead(Rec*); | 202 void moveToHead(Rec*); |
| 219 void addToHead(Rec*); | 203 void addToHead(Rec*); |
| 220 void detach(Rec*); | 204 void detach(Rec*); |
| 221 void remove(Rec*); | |
| 222 | 205 |
| 223 void init(); // called by constructors | 206 void init(); // called by constructors |
| 224 | 207 |
| 225 #ifdef SK_DEBUG | 208 #ifdef SK_DEBUG |
| 226 void validate() const; | 209 void validate() const; |
| 227 #else | 210 #else |
| 228 void validate() const {} | 211 void validate() const {} |
| 229 #endif | 212 #endif |
| 230 }; | 213 }; |
| 231 #endif | 214 #endif |
| OLD | NEW |