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 |
11 #include "SkBitmap.h" | 11 #include "SkBitmap.h" |
12 #include "SkMessageBus.h" | 12 #include "SkMessageBus.h" |
13 #include "SkTDArray.h" | 13 #include "SkTDArray.h" |
14 | 14 |
15 class SkCachedData; | 15 class SkCachedData; |
16 class SkDiscardableMemory; | 16 class SkDiscardableMemory; |
17 class SkMipMap; | |
18 | 17 |
19 /** | 18 /** |
20 * Cache object for bitmaps (with possible scale in X Y as part of the key). | 19 * Cache object for bitmaps (with possible scale in X Y as part of the key). |
21 * | 20 * |
22 * Multiple caches can be instantiated, but each instance is not implicitly | 21 * Multiple caches can be instantiated, but each instance is not implicitly |
23 * thread-safe, so if a given instance is to be shared across threads, the | 22 * thread-safe, so if a given instance is to be shared across threads, the |
24 * caller must manage the access itself (e.g. via a mutex). | 23 * caller must manage the access itself (e.g. via a mutex). |
25 * | 24 * |
26 * As a convenience, a global instance is also defined, which can be safely | 25 * As a convenience, a global instance is also defined, which can be safely |
27 * access across threads via the static methods (e.g. FindAndLock, etc.). | 26 * access across threads via the static methods (e.g. FindAndLock, etc.). |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
71 typedef SkResourceCache::Key Key; | 70 typedef SkResourceCache::Key Key; |
72 | 71 |
73 Rec() {} | 72 Rec() {} |
74 virtual ~Rec() {} | 73 virtual ~Rec() {} |
75 | 74 |
76 uint32_t getHash() const { return this->getKey().hash(); } | 75 uint32_t getHash() const { return this->getKey().hash(); } |
77 | 76 |
78 virtual const Key& getKey() const = 0; | 77 virtual const Key& getKey() const = 0; |
79 virtual size_t bytesUsed() const = 0; | 78 virtual size_t bytesUsed() const = 0; |
80 | 79 |
80 // for memory usage diagnostics | |
81 virtual const char* getCategory() const = 0; | |
82 virtual SkDiscardableMemory* diagnostic_only_getDiscardable() const { re turn NULL; } | |
83 | |
81 // for SkTDynamicHash::Traits | 84 // for SkTDynamicHash::Traits |
82 static uint32_t Hash(const Key& key) { return key.hash(); } | 85 static uint32_t Hash(const Key& key) { return key.hash(); } |
83 static const Key& GetKey(const Rec& rec) { return rec.getKey(); } | 86 static const Key& GetKey(const Rec& rec) { return rec.getKey(); } |
84 | 87 |
85 private: | 88 private: |
86 Rec* fNext; | 89 Rec* fNext; |
87 Rec* fPrev; | 90 Rec* fPrev; |
88 | 91 |
89 friend class SkResourceCache; | 92 friend class SkResourceCache; |
90 }; | 93 }; |
(...skipping 18 matching lines...) Expand all Loading... | |
109 * "stale" and will be purged from the cache. | 112 * "stale" and will be purged from the cache. |
110 */ | 113 */ |
111 typedef bool (*FindVisitor)(const Rec&, void* context); | 114 typedef bool (*FindVisitor)(const Rec&, void* context); |
112 | 115 |
113 /** | 116 /** |
114 * Returns a locked/pinned SkDiscardableMemory instance for the specified | 117 * Returns a locked/pinned SkDiscardableMemory instance for the specified |
115 * number of bytes, or NULL on failure. | 118 * number of bytes, or NULL on failure. |
116 */ | 119 */ |
117 typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes); | 120 typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes); |
118 | 121 |
122 /** | |
123 * Will be called for each rec in the cache. | |
124 */ | |
125 typedef void (*Visitor)(const Rec&, void* context); | |
mtklein_C
2015/08/04 20:27:37
It'd be nice to have Visitor and VisitAll right ne
reed1
2015/08/07 16:19:59
Done.
| |
126 | |
119 /* | 127 /* |
120 * The following static methods are thread-safe wrappers around a global | 128 * The following static methods are thread-safe wrappers around a global |
121 * instance of this cache. | 129 * instance of this cache. |
122 */ | 130 */ |
123 | 131 |
124 /** | 132 /** |
125 * Returns true if the visitor was called on a matching Key, and the visito r returned true. | 133 * Returns true if the visitor was called on a matching Key, and the visito r returned true. |
126 * | 134 * |
127 * Find() will search the cache for the specified Key. If no match is found , return false and | 135 * Find() will search the cache for the specified Key. If no match is found , return false and |
128 * do not call the FindVisitor. If a match is found, return whatever the vi sitor returns. | 136 * do not call the FindVisitor. If a match is found, return whatever the vi sitor returns. |
129 * Its return value is interpreted to mean: | 137 * Its return value is interpreted to mean: |
130 * true : Rec is valid | 138 * true : Rec is valid |
131 * false : Rec is "stale" -- the cache will purge it. | 139 * false : Rec is "stale" -- the cache will purge it. |
132 */ | 140 */ |
133 static bool Find(const Key& key, FindVisitor, void* context); | 141 static bool Find(const Key& key, FindVisitor, void* context); |
134 static void Add(Rec*); | 142 static void Add(Rec*); |
143 static void VisitAll(Visitor, void* context); | |
135 | 144 |
136 static size_t GetTotalBytesUsed(); | 145 static size_t GetTotalBytesUsed(); |
137 static size_t GetTotalByteLimit(); | 146 static size_t GetTotalByteLimit(); |
138 static size_t SetTotalByteLimit(size_t newLimit); | 147 static size_t SetTotalByteLimit(size_t newLimit); |
139 | 148 |
140 static size_t SetSingleAllocationByteLimit(size_t); | 149 static size_t SetSingleAllocationByteLimit(size_t); |
141 static size_t GetSingleAllocationByteLimit(); | 150 static size_t GetSingleAllocationByteLimit(); |
142 static size_t GetEffectiveSingleAllocationByteLimit(); | 151 static size_t GetEffectiveSingleAllocationByteLimit(); |
143 | 152 |
144 static void PurgeAll(); | 153 static void PurgeAll(); |
145 | 154 |
155 static void TestDumpMemoryStatistics(); | |
156 | |
146 /** | 157 /** |
147 * Returns the DiscardableFactory used by the global cache, or NULL. | 158 * Returns the DiscardableFactory used by the global cache, or NULL. |
148 */ | 159 */ |
149 static DiscardableFactory GetDiscardableFactory(); | 160 static DiscardableFactory GetDiscardableFactory(); |
150 | 161 |
151 /** | 162 /** |
152 * Use this allocator for bitmaps, so they can use ashmem when available. | 163 * Use this allocator for bitmaps, so they can use ashmem when available. |
153 * Returns NULL if the ResourceCache has not been initialized with a Discard ableFactory. | 164 * Returns NULL if the ResourceCache has not been initialized with a Discard ableFactory. |
154 */ | 165 */ |
155 static SkBitmap::Allocator* GetAllocator(); | 166 static SkBitmap::Allocator* GetAllocator(); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
187 * Returns true if the visitor was called on a matching Key, and the visito r returned true. | 198 * Returns true if the visitor was called on a matching Key, and the visito r returned true. |
188 * | 199 * |
189 * find() will search the cache for the specified Key. If no match is found , return false and | 200 * find() will search the cache for the specified Key. If no match is found , return false and |
190 * do not call the FindVisitor. If a match is found, return whatever the vi sitor returns. | 201 * do not call the FindVisitor. If a match is found, return whatever the vi sitor returns. |
191 * Its return value is interpreted to mean: | 202 * Its return value is interpreted to mean: |
192 * true : Rec is valid | 203 * true : Rec is valid |
193 * false : Rec is "stale" -- the cache will purge it. | 204 * false : Rec is "stale" -- the cache will purge it. |
194 */ | 205 */ |
195 bool find(const Key&, FindVisitor, void* context); | 206 bool find(const Key&, FindVisitor, void* context); |
196 void add(Rec*); | 207 void add(Rec*); |
208 void visitAll(Visitor, void* context); | |
197 | 209 |
198 size_t getTotalBytesUsed() const { return fTotalBytesUsed; } | 210 size_t getTotalBytesUsed() const { return fTotalBytesUsed; } |
199 size_t getTotalByteLimit() const { return fTotalByteLimit; } | 211 size_t getTotalByteLimit() const { return fTotalByteLimit; } |
200 | 212 |
201 /** | 213 /** |
202 * This is respected by SkBitmapProcState::possiblyScaleImage. | 214 * This is respected by SkBitmapProcState::possiblyScaleImage. |
203 * 0 is no maximum at all; this is the default. | 215 * 0 is no maximum at all; this is the default. |
204 * setSingleAllocationByteLimit() returns the previous value. | 216 * setSingleAllocationByteLimit() returns the previous value. |
205 */ | 217 */ |
206 size_t setSingleAllocationByteLimit(size_t maximumAllocationSize); | 218 size_t setSingleAllocationByteLimit(size_t maximumAllocationSize); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
261 | 273 |
262 void init(); // called by constructors | 274 void init(); // called by constructors |
263 | 275 |
264 #ifdef SK_DEBUG | 276 #ifdef SK_DEBUG |
265 void validate() const; | 277 void validate() const; |
266 #else | 278 #else |
267 void validate() const {} | 279 void validate() const {} |
268 #endif | 280 #endif |
269 }; | 281 }; |
270 #endif | 282 #endif |
OLD | NEW |