reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 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 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 8 | #ifndef SkResourceCache_DEFINED |
| 9 | #define SkResourceCache_DEFINED |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 10 | |
| 11 | #include "SkBitmap.h" |
| 12 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 13 | class SkDiscardableMemory; |
reed@google.com | d94697c | 2013-07-24 14:31:33 +0000 | [diff] [blame] | 14 | class SkMipMap; |
| 15 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 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 | */ |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 26 | class SkResourceCache { |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 27 | public: |
reed | 4f987e9 | 2014-08-20 13:41:56 -0700 | [diff] [blame] | 28 | struct Key { |
| 29 | // Call this to access your private contents. Must not use the address after 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 | |
reed | 4f987e9 | 2014-08-20 13:41:56 -0700 | [diff] [blame] | 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 | |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 60 | struct Rec { |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 61 | typedef SkResourceCache::Key Key; |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 62 | |
reed | dee6a8e | 2014-09-15 06:44:47 -0700 | [diff] [blame^] | 63 | Rec() {} |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 64 | virtual ~Rec() {} |
| 65 | |
| 66 | uint32_t getHash() const { return this->getKey().hash(); } |
piotaixr | 8159146 | 2014-09-02 11:27:11 -0700 | [diff] [blame] | 67 | |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 68 | virtual const Key& getKey() const = 0; |
| 69 | virtual size_t bytesUsed() const = 0; |
piotaixr | 8159146 | 2014-09-02 11:27:11 -0700 | [diff] [blame] | 70 | |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 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; |
piotaixr | 8159146 | 2014-09-02 11:27:11 -0700 | [diff] [blame] | 78 | |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 79 | friend class SkResourceCache; |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | typedef const Rec* ID; |
| 83 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 84 | /** |
reed | dee6a8e | 2014-09-15 06:44:47 -0700 | [diff] [blame^] | 85 | * Callback function for find(). If called, the cache will have found a match for the |
| 86 | * specified Key, and will pass in the corresponding Rec, along with a caller-specified |
| 87 | * context. The function can read the data in Rec, and copy whatever it likes 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 | /** |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 97 | * Returns a locked/pinned SkDiscardableMemory instance for the specified |
| 98 | * number of bytes, or NULL on failure. |
| 99 | */ |
| 100 | typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes); |
| 101 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 102 | /* |
| 103 | * The following static methods are thread-safe wrappers around a global |
| 104 | * instance of this cache. |
| 105 | */ |
| 106 | |
reed | dee6a8e | 2014-09-15 06:44:47 -0700 | [diff] [blame^] | 107 | /** |
| 108 | * Returns true if the visitor was called on a matching Key, and the visitor returned true. |
| 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 visitor 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); |
reed | 680fb9e | 2014-08-26 09:08:04 -0700 | [diff] [blame] | 117 | static void Add(Rec*); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 118 | |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 119 | static size_t GetTotalBytesUsed(); |
| 120 | static size_t GetTotalByteLimit(); |
| 121 | static size_t SetTotalByteLimit(size_t newLimit); |
| 122 | |
| 123 | static size_t SetSingleAllocationByteLimit(size_t); |
| 124 | static size_t GetSingleAllocationByteLimit(); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 125 | |
reed | 56b00d9 | 2014-09-11 12:22:34 -0700 | [diff] [blame] | 126 | static void PurgeAll(); |
| 127 | |
piotaixr | 8159146 | 2014-09-02 11:27:11 -0700 | [diff] [blame] | 128 | /** |
| 129 | * Use this allocator for bitmaps, so they can use ashmem when available. |
| 130 | * Returns NULL if the ResourceCache has not been initialized with a DiscardableFactory. |
| 131 | */ |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 132 | static SkBitmap::Allocator* GetAllocator(); |
| 133 | |
reed@google.com | fa7fd80 | 2013-12-12 21:37:25 +0000 | [diff] [blame] | 134 | /** |
| 135 | * Call SkDebugf() with diagnostic information about the state of the cache |
| 136 | */ |
| 137 | static void Dump(); |
| 138 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 139 | /////////////////////////////////////////////////////////////////////////// |
| 140 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 141 | /** |
| 142 | * Construct the cache to call DiscardableFactory when it |
| 143 | * allocates memory for the pixels. In this mode, the cache has |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 144 | * not explicit budget, and so methods like getTotalBytesUsed() |
| 145 | * and getTotalByteLimit() will return 0, and setTotalByteLimit |
| 146 | * will ignore its argument and return 0. |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 147 | */ |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 148 | SkResourceCache(DiscardableFactory); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 149 | |
| 150 | /** |
| 151 | * Construct the cache, allocating memory with malloc, and respect the |
| 152 | * 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 |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 154 | * changed at runtime with setTotalByteLimit. |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 155 | */ |
reed | 011f39a | 2014-08-28 13:35:23 -0700 | [diff] [blame] | 156 | explicit SkResourceCache(size_t byteLimit); |
| 157 | ~SkResourceCache(); |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 158 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 159 | /** |
reed | dee6a8e | 2014-09-15 06:44:47 -0700 | [diff] [blame^] | 160 | * Returns true if the visitor was called on a matching Key, and the visitor returned true. |
| 161 | * |
| 162 | * find() will search the cache for the specified Key. If no match is found, return false and |
| 163 | * do not call the VisitorProc. If a match is found, return whatever the visitor returns. |
| 164 | * Its return value is interpreted to mean: |
| 165 | * true : Rec is valid |
| 166 | * false : Rec is "stale" -- the cache will purge it. |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 167 | */ |
reed | dee6a8e | 2014-09-15 06:44:47 -0700 | [diff] [blame^] | 168 | bool find(const Key&, VisitorProc, void* context); |
| 169 | void add(Rec*); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 170 | |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 171 | size_t getTotalBytesUsed() const { return fTotalBytesUsed; } |
| 172 | size_t getTotalByteLimit() const { return fTotalByteLimit; } |
skia.committer@gmail.com | 7f1af50 | 2013-07-24 07:01:12 +0000 | [diff] [blame] | 173 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 174 | /** |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 175 | * This is respected by SkBitmapProcState::possiblyScaleImage. |
| 176 | * 0 is no maximum at all; this is the default. |
| 177 | * setSingleAllocationByteLimit() returns the previous value. |
| 178 | */ |
| 179 | size_t setSingleAllocationByteLimit(size_t maximumAllocationSize); |
| 180 | size_t getSingleAllocationByteLimit() const; |
| 181 | /** |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 182 | * Set the maximum number of bytes available to this cache. If the current |
| 183 | * cache exceeds this new value, it will be purged to try to fit within |
| 184 | * this new limit. |
| 185 | */ |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 186 | size_t setTotalByteLimit(size_t newLimit); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 187 | |
reed | 56b00d9 | 2014-09-11 12:22:34 -0700 | [diff] [blame] | 188 | void purgeAll() { |
| 189 | this->purgeAsNeeded(true); |
| 190 | } |
| 191 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 192 | SkBitmap::Allocator* allocator() const { return fAllocator; }; |
| 193 | |
reed@google.com | fa7fd80 | 2013-12-12 21:37:25 +0000 | [diff] [blame] | 194 | /** |
| 195 | * Call SkDebugf() with diagnostic information about the state of the cache |
| 196 | */ |
| 197 | void dump() const; |
| 198 | |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 199 | private: |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 200 | Rec* fHead; |
| 201 | Rec* fTail; |
| 202 | |
reed@google.com | 5d1e558 | 2013-07-25 14:36:15 +0000 | [diff] [blame] | 203 | class Hash; |
| 204 | Hash* fHash; |
| 205 | |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 206 | DiscardableFactory fDiscardableFactory; |
| 207 | // the allocator is NULL or one that matches discardables |
| 208 | SkBitmap::Allocator* fAllocator; |
| 209 | |
halcanary | 805ef15 | 2014-07-17 06:58:01 -0700 | [diff] [blame] | 210 | size_t fTotalBytesUsed; |
| 211 | size_t fTotalByteLimit; |
| 212 | size_t fSingleAllocationByteLimit; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 213 | int fCount; |
| 214 | |
reed | 56b00d9 | 2014-09-11 12:22:34 -0700 | [diff] [blame] | 215 | void purgeAsNeeded(bool forcePurge = false); |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 216 | |
| 217 | // linklist management |
| 218 | void moveToHead(Rec*); |
| 219 | void addToHead(Rec*); |
| 220 | void detach(Rec*); |
reed | dee6a8e | 2014-09-15 06:44:47 -0700 | [diff] [blame^] | 221 | void remove(Rec*); |
reed@google.com | e4eb122 | 2013-12-09 22:29:30 +0000 | [diff] [blame] | 222 | |
| 223 | void init(); // called by constructors |
| 224 | |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 225 | #ifdef SK_DEBUG |
| 226 | void validate() const; |
| 227 | #else |
| 228 | void validate() const {} |
| 229 | #endif |
| 230 | }; |
reed@google.com | 602a1d7 | 2013-07-23 19:13:54 +0000 | [diff] [blame] | 231 | #endif |