Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
SkResourceCache.h
Go to the documentation of this file.
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 SkResourceCache_DEFINED
9#define SkResourceCache_DEFINED
10
12
13#include <cstddef>
14#include <cstdint>
15
16class SkCachedData;
19
20/**
21 * Cache object for bitmaps (with possible scale in X Y as part of the key).
22 *
23 * Multiple caches can be instantiated, but each instance is not implicitly
24 * thread-safe, so if a given instance is to be shared across threads, the
25 * caller must manage the access itself (e.g. via a mutex).
26 *
27 * As a convenience, a global instance is also defined, which can be safely
28 * access across threads via the static methods (e.g. FindAndLock, etc.).
29 */
31public:
32 struct Key {
33 /** Key subclasses must call this after their own fields and data are initialized.
34 * All fields and data must be tightly packed.
35 * @param nameSpace must be unique per Key subclass.
36 * @param sharedID == 0 means ignore this field, does not support group purging.
37 * @param dataSize is size of fields and data of the subclass, must be a multiple of 4.
38 */
39 void init(void* nameSpace, uint64_t sharedID, size_t dataSize);
40
41 /** Returns the size of this key. */
42 size_t size() const {
43 return fCount32 << 2;
44 }
45
46 void* getNamespace() const { return fNamespace; }
47 uint64_t getSharedID() const { return ((uint64_t)fSharedID_hi << 32) | fSharedID_lo; }
48
49 // This is only valid after having called init().
50 uint32_t hash() const { return fHash; }
51
52 bool operator==(const Key& other) const {
53 const uint32_t* a = this->as32();
54 const uint32_t* b = other.as32();
55 for (int i = 0; i < fCount32; ++i) { // (This checks fCount == other.fCount first.)
56 if (a[i] != b[i]) {
57 return false;
58 }
59 }
60 return true;
61 }
62
63 private:
64 int32_t fCount32; // local + user contents count32
65 uint32_t fHash;
66 // split uint64_t into hi and lo so we don't force ourselves to pad on 32bit machines.
67 uint32_t fSharedID_lo;
68 uint32_t fSharedID_hi;
69 void* fNamespace; // A unique namespace tag. This is hashed.
70 /* uint32_t fContents32[] */
71
72 const uint32_t* as32() const { return (const uint32_t*)this; }
73 };
74
75 struct Rec {
77
78 Rec() {}
79 virtual ~Rec() {}
80
81 uint32_t getHash() const { return this->getKey().hash(); }
82
83 virtual const Key& getKey() const = 0;
84 virtual size_t bytesUsed() const = 0;
85
86 // Called if the cache needs to purge/remove/delete the Rec. Default returns true.
87 // Subclass may return false if there are outstanding references to it (e.g. bitmaps).
88 // Will only be deleted/removed-from-the-cache when this returns true.
89 virtual bool canBePurged() { return true; }
90
91 // A rec is first created/initialized, and then added to the cache. As part of the add(),
92 // the cache will callback into the rec with postAddInstall, passing in whatever payload
93 // was passed to add/Add.
94 //
95 // This late-install callback exists because the process of add-ing might end up deleting
96 // the new rec (if an existing rec in the cache has the same key and cannot be purged).
97 // If the new rec will be deleted during add, the pre-existing one (with the same key)
98 // will have postAddInstall() called on it instead, so that either way an "install" will
99 // happen during the add.
100 virtual void postAddInstall(void*) {}
101
102 // for memory usage diagnostics
103 virtual const char* getCategory() const = 0;
104 virtual SkDiscardableMemory* diagnostic_only_getDiscardable() const { return nullptr; }
105
106 private:
107 Rec* fNext;
108 Rec* fPrev;
109
110 friend class SkResourceCache;
111 };
112
113 // Used with SkMessageBus
115 PurgeSharedIDMessage(uint64_t sharedID) : fSharedID(sharedID) {}
116 uint64_t fSharedID;
117 };
118
119 typedef const Rec* ID;
120
121 /**
122 * Callback function for find(). If called, the cache will have found a match for the
123 * specified Key, and will pass in the corresponding Rec, along with a caller-specified
124 * context. The function can read the data in Rec, and copy whatever it likes into context
125 * (casting context to whatever it really is).
126 *
127 * The return value determines what the cache will do with the Rec. If the function returns
128 * true, then the Rec is considered "valid". If false is returned, the Rec will be considered
129 * "stale" and will be purged from the cache.
130 */
131 typedef bool (*FindVisitor)(const Rec&, void* context);
132
133 /**
134 * Returns a locked/pinned SkDiscardableMemory instance for the specified
135 * number of bytes, or nullptr on failure.
136 */
137 typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes);
138
139 /*
140 * The following static methods are thread-safe wrappers around a global
141 * instance of this cache.
142 */
143
144 /**
145 * Returns true if the visitor was called on a matching Key, and the visitor returned true.
146 *
147 * Find() will search the cache for the specified Key. If no match is found, return false and
148 * do not call the FindVisitor. If a match is found, return whatever the visitor returns.
149 * Its return value is interpreted to mean:
150 * true : Rec is valid
151 * false : Rec is "stale" -- the cache will purge it.
152 */
153 static bool Find(const Key& key, FindVisitor, void* context);
154 static void Add(Rec*, void* payload = nullptr);
155
156 typedef void (*Visitor)(const Rec&, void* context);
157 // Call the visitor for every Rec in the cache.
158 static void VisitAll(Visitor, void* context);
159
160 static size_t GetTotalBytesUsed();
161 static size_t GetTotalByteLimit();
162 static size_t SetTotalByteLimit(size_t newLimit);
163
164 static size_t SetSingleAllocationByteLimit(size_t);
165 static size_t GetSingleAllocationByteLimit();
167
168 static void PurgeAll();
169 static void CheckMessages();
170
171 static void TestDumpMemoryStatistics();
172
173 /** Dump memory usage statistics of every Rec in the cache using the
174 SkTraceMemoryDump interface.
175 */
177
178 /**
179 * Returns the DiscardableFactory used by the global cache, or nullptr.
180 */
182
183 static SkCachedData* NewCachedData(size_t bytes);
184
185 static void PostPurgeSharedID(uint64_t sharedID);
186
187 /**
188 * Call SkDebugf() with diagnostic information about the state of the cache
189 */
190 static void Dump();
191
192 ///////////////////////////////////////////////////////////////////////////
193
194 /**
195 * Construct the cache to call DiscardableFactory when it
196 * allocates memory for the pixels. In this mode, the cache has
197 * not explicit budget, and so methods like getTotalBytesUsed()
198 * and getTotalByteLimit() will return 0, and setTotalByteLimit
199 * will ignore its argument and return 0.
200 */
202
203 /**
204 * Construct the cache, allocating memory with malloc, and respect the
205 * byteLimit, purging automatically when a new image is added to the cache
206 * that pushes the total bytesUsed over the limit. Note: The limit can be
207 * changed at runtime with setTotalByteLimit.
208 */
209 explicit SkResourceCache(size_t byteLimit);
211
212 /**
213 * Returns true if the visitor was called on a matching Key, and the visitor returned true.
214 *
215 * find() will search the cache for the specified Key. If no match is found, return false and
216 * do not call the FindVisitor. If a match is found, return whatever the visitor returns.
217 * Its return value is interpreted to mean:
218 * true : Rec is valid
219 * false : Rec is "stale" -- the cache will purge it.
220 */
221 bool find(const Key&, FindVisitor, void* context);
222 void add(Rec*, void* payload = nullptr);
223 void visitAll(Visitor, void* context);
224
225 size_t getTotalBytesUsed() const { return fTotalBytesUsed; }
226 size_t getTotalByteLimit() const { return fTotalByteLimit; }
227
228 /**
229 * This is respected by SkBitmapProcState::possiblyScaleImage.
230 * 0 is no maximum at all; this is the default.
231 * setSingleAllocationByteLimit() returns the previous value.
232 */
233 size_t setSingleAllocationByteLimit(size_t maximumAllocationSize);
234 size_t getSingleAllocationByteLimit() const;
235 // returns the logical single allocation size (pinning against the budget when the cache
236 // is not backed by discardable memory.
238
239 /**
240 * Set the maximum number of bytes available to this cache. If the current
241 * cache exceeds this new value, it will be purged to try to fit within
242 * this new limit.
243 */
244 size_t setTotalByteLimit(size_t newLimit);
245
246 void purgeSharedID(uint64_t sharedID);
247
248 void purgeAll() {
249 this->purgeAsNeeded(true);
250 }
251
252 DiscardableFactory discardableFactory() const { return fDiscardableFactory; }
253
254 SkCachedData* newCachedData(size_t bytes);
255
256 /**
257 * Call SkDebugf() with diagnostic information about the state of the cache
258 */
259 void dump() const;
260
261private:
262 Rec* fHead;
263 Rec* fTail;
264
265 class Hash;
266 Hash* fHash;
267
268 DiscardableFactory fDiscardableFactory;
269
270 size_t fTotalBytesUsed;
271 size_t fTotalByteLimit;
272 size_t fSingleAllocationByteLimit;
273 int fCount;
274
276
277 void checkMessages();
278 void purgeAsNeeded(bool forcePurge = false);
279
280 // linklist management
281 void moveToHead(Rec*);
282 void addToHead(Rec*);
283 void release(Rec*);
284 void remove(Rec*);
285
286 void init(); // called by constructors
287
288#ifdef SK_DEBUG
289 void validate() const;
290#else
291 void validate() const {}
292#endif
293};
294#endif
static size_t SetSingleAllocationByteLimit(size_t)
static SkCachedData * NewCachedData(size_t bytes)
static void Add(Rec *, void *payload=nullptr)
size_t getSingleAllocationByteLimit() const
size_t getTotalByteLimit() const
static size_t GetEffectiveSingleAllocationByteLimit()
DiscardableFactory discardableFactory() const
size_t getTotalBytesUsed() const
void visitAll(Visitor, void *context)
static void PurgeAll()
static void PostPurgeSharedID(uint64_t sharedID)
static size_t GetTotalBytesUsed()
void purgeSharedID(uint64_t sharedID)
static void CheckMessages()
static DiscardableFactory GetDiscardableFactory()
bool find(const Key &, FindVisitor, void *context)
static void DumpMemoryStatistics(SkTraceMemoryDump *dump)
static size_t GetSingleAllocationByteLimit()
static void VisitAll(Visitor, void *context)
static size_t GetTotalByteLimit()
static bool Find(const Key &key, FindVisitor, void *context)
static void TestDumpMemoryStatistics()
size_t setTotalByteLimit(size_t newLimit)
SkCachedData * newCachedData(size_t bytes)
SkDiscardableMemory *(* DiscardableFactory)(size_t bytes)
bool(* FindVisitor)(const Rec &, void *context)
size_t setSingleAllocationByteLimit(size_t maximumAllocationSize)
size_t getEffectiveSingleAllocationByteLimit() const
void(* Visitor)(const Rec &, void *context)
static size_t SetTotalByteLimit(size_t newLimit)
void add(Rec *, void *payload=nullptr)
static bool b
struct MyStruct a[10]
uint64_t getSharedID() const
uint32_t hash() const
void * getNamespace() const
bool operator==(const Key &other) const
void init(void *nameSpace, uint64_t sharedID, size_t dataSize)
virtual void postAddInstall(void *)
virtual const char * getCategory() const =0
virtual SkDiscardableMemory * diagnostic_only_getDiscardable() const
virtual size_t bytesUsed() const =0
uint32_t getHash() const
virtual const Key & getKey() const =0
SkResourceCache::Key Key
virtual bool canBePurged()