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 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 * | 129 * |
127 * Find() will search the cache for the specified Key. If no match is found
, return false and | 130 * 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. | 131 * do not call the FindVisitor. If a match is found, return whatever the vi
sitor returns. |
129 * Its return value is interpreted to mean: | 132 * Its return value is interpreted to mean: |
130 * true : Rec is valid | 133 * true : Rec is valid |
131 * false : Rec is "stale" -- the cache will purge it. | 134 * false : Rec is "stale" -- the cache will purge it. |
132 */ | 135 */ |
133 static bool Find(const Key& key, FindVisitor, void* context); | 136 static bool Find(const Key& key, FindVisitor, void* context); |
134 static void Add(Rec*); | 137 static void Add(Rec*); |
135 | 138 |
| 139 typedef void (*Visitor)(const Rec&, void* context); |
| 140 // Call the visitor for every Rec in the cache. |
| 141 static void VisitAll(Visitor, void* context); |
| 142 |
136 static size_t GetTotalBytesUsed(); | 143 static size_t GetTotalBytesUsed(); |
137 static size_t GetTotalByteLimit(); | 144 static size_t GetTotalByteLimit(); |
138 static size_t SetTotalByteLimit(size_t newLimit); | 145 static size_t SetTotalByteLimit(size_t newLimit); |
139 | 146 |
140 static size_t SetSingleAllocationByteLimit(size_t); | 147 static size_t SetSingleAllocationByteLimit(size_t); |
141 static size_t GetSingleAllocationByteLimit(); | 148 static size_t GetSingleAllocationByteLimit(); |
142 static size_t GetEffectiveSingleAllocationByteLimit(); | 149 static size_t GetEffectiveSingleAllocationByteLimit(); |
143 | 150 |
144 static void PurgeAll(); | 151 static void PurgeAll(); |
145 | 152 |
| 153 static void TestDumpMemoryStatistics(); |
| 154 |
146 /** | 155 /** |
147 * Returns the DiscardableFactory used by the global cache, or NULL. | 156 * Returns the DiscardableFactory used by the global cache, or NULL. |
148 */ | 157 */ |
149 static DiscardableFactory GetDiscardableFactory(); | 158 static DiscardableFactory GetDiscardableFactory(); |
150 | 159 |
151 /** | 160 /** |
152 * Use this allocator for bitmaps, so they can use ashmem when available. | 161 * 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. | 162 * Returns NULL if the ResourceCache has not been initialized with a Discard
ableFactory. |
154 */ | 163 */ |
155 static SkBitmap::Allocator* GetAllocator(); | 164 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. | 196 * Returns true if the visitor was called on a matching Key, and the visito
r returned true. |
188 * | 197 * |
189 * find() will search the cache for the specified Key. If no match is found
, return false and | 198 * 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. | 199 * do not call the FindVisitor. If a match is found, return whatever the vi
sitor returns. |
191 * Its return value is interpreted to mean: | 200 * Its return value is interpreted to mean: |
192 * true : Rec is valid | 201 * true : Rec is valid |
193 * false : Rec is "stale" -- the cache will purge it. | 202 * false : Rec is "stale" -- the cache will purge it. |
194 */ | 203 */ |
195 bool find(const Key&, FindVisitor, void* context); | 204 bool find(const Key&, FindVisitor, void* context); |
196 void add(Rec*); | 205 void add(Rec*); |
| 206 void visitAll(Visitor, void* context); |
197 | 207 |
198 size_t getTotalBytesUsed() const { return fTotalBytesUsed; } | 208 size_t getTotalBytesUsed() const { return fTotalBytesUsed; } |
199 size_t getTotalByteLimit() const { return fTotalByteLimit; } | 209 size_t getTotalByteLimit() const { return fTotalByteLimit; } |
200 | 210 |
201 /** | 211 /** |
202 * This is respected by SkBitmapProcState::possiblyScaleImage. | 212 * This is respected by SkBitmapProcState::possiblyScaleImage. |
203 * 0 is no maximum at all; this is the default. | 213 * 0 is no maximum at all; this is the default. |
204 * setSingleAllocationByteLimit() returns the previous value. | 214 * setSingleAllocationByteLimit() returns the previous value. |
205 */ | 215 */ |
206 size_t setSingleAllocationByteLimit(size_t maximumAllocationSize); | 216 size_t setSingleAllocationByteLimit(size_t maximumAllocationSize); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 | 271 |
262 void init(); // called by constructors | 272 void init(); // called by constructors |
263 | 273 |
264 #ifdef SK_DEBUG | 274 #ifdef SK_DEBUG |
265 void validate() const; | 275 void validate() const; |
266 #else | 276 #else |
267 void validate() const {} | 277 void validate() const {} |
268 #endif | 278 #endif |
269 }; | 279 }; |
270 #endif | 280 #endif |
OLD | NEW |