blob: a8da4bd58bbe9c20254a82f8b74d5b9ad137a2e7 [file] [log] [blame]
reed@google.com602a1d72013-07-23 19:13:54 +00001/*
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
reed011f39a2014-08-28 13:35:23 -07008#ifndef SkResourceCache_DEFINED
9#define SkResourceCache_DEFINED
reed@google.com602a1d72013-07-23 19:13:54 +000010
11#include "SkBitmap.h"
reed7eeba252015-02-24 13:54:23 -080012#include "SkMessageBus.h"
13#include "SkTDArray.h"
reed@google.com602a1d72013-07-23 19:13:54 +000014
reed9d93c2e2014-10-08 05:17:12 -070015class SkCachedData;
reed@google.come4eb1222013-12-09 22:29:30 +000016class SkDiscardableMemory;
ssid33c594c2015-08-27 09:23:54 -070017class SkTraceMemoryDump;
reed@google.comd94697c2013-07-24 14:31:33 +000018
reed@google.com602a1d72013-07-23 19:13:54 +000019/**
20 * Cache object for bitmaps (with possible scale in X Y as part of the key).
21 *
22 * 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
24 * caller must manage the access itself (e.g. via a mutex).
25 *
26 * 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.).
28 */
reed011f39a2014-08-28 13:35:23 -070029class SkResourceCache {
reed@google.com602a1d72013-07-23 19:13:54 +000030public:
reed4f987e92014-08-20 13:41:56 -070031 struct Key {
bungeman70bb8082016-02-17 10:13:49 -080032 /** Key subclasses must call this after their own fields and data are initialized.
33 * All fields and data must be tightly packed.
34 * @param nameSpace must be unique per Key subclass.
35 * @param sharedID == 0 means ignore this field, does not support group purging.
36 * @param dataSize is size of fields and data of the subclass, must be a multiple of 4.
37 */
38 void init(void* nameSpace, uint64_t sharedID, size_t dataSize);
reed4f987e92014-08-20 13:41:56 -070039
bungeman70bb8082016-02-17 10:13:49 -080040 /** Returns the size of this key. */
41 size_t size() const {
42 return fCount32 << 2;
43 }
reed7eeba252015-02-24 13:54:23 -080044
45 void* getNamespace() const { return fNamespace; }
46 uint64_t getSharedID() const { return ((uint64_t)fSharedID_hi << 32) | fSharedID_lo; }
reed4f987e92014-08-20 13:41:56 -070047
48 // This is only valid after having called init().
49 uint32_t hash() const { return fHash; }
50
51 bool operator==(const Key& other) const {
52 const uint32_t* a = this->as32();
53 const uint32_t* b = other.as32();
mtklein484bbe52014-10-21 10:55:22 -070054 for (int i = 0; i < fCount32; ++i) { // (This checks fCount == other.fCount first.)
reed4f987e92014-08-20 13:41:56 -070055 if (a[i] != b[i]) {
56 return false;
57 }
58 }
59 return true;
60 }
61
reed4f987e92014-08-20 13:41:56 -070062 private:
fmalita171e5b72014-10-22 11:20:40 -070063 int32_t fCount32; // local + user contents count32
reed4f987e92014-08-20 13:41:56 -070064 uint32_t fHash;
reed7eeba252015-02-24 13:54:23 -080065 // split uint64_t into hi and lo so we don't force ourselves to pad on 32bit machines.
66 uint32_t fSharedID_lo;
67 uint32_t fSharedID_hi;
fmalita171e5b72014-10-22 11:20:40 -070068 void* fNamespace; // A unique namespace tag. This is hashed.
reed4f987e92014-08-20 13:41:56 -070069 /* uint32_t fContents32[] */
70
71 const uint32_t* as32() const { return (const uint32_t*)this; }
reed4f987e92014-08-20 13:41:56 -070072 };
73
reed680fb9e2014-08-26 09:08:04 -070074 struct Rec {
reed011f39a2014-08-28 13:35:23 -070075 typedef SkResourceCache::Key Key;
reed680fb9e2014-08-26 09:08:04 -070076
reedc90e0142014-09-15 11:39:44 -070077 Rec() {}
reed680fb9e2014-08-26 09:08:04 -070078 virtual ~Rec() {}
79
80 uint32_t getHash() const { return this->getKey().hash(); }
piotaixr81591462014-09-02 11:27:11 -070081
reed680fb9e2014-08-26 09:08:04 -070082 virtual const Key& getKey() const = 0;
83 virtual size_t bytesUsed() const = 0;
piotaixr81591462014-09-02 11:27:11 -070084
reed216b6432015-08-19 12:25:40 -070085 // for memory usage diagnostics
86 virtual const char* getCategory() const = 0;
halcanary96fcdcc2015-08-27 07:41:13 -070087 virtual SkDiscardableMemory* diagnostic_only_getDiscardable() const { return nullptr; }
reed216b6432015-08-19 12:25:40 -070088
reed680fb9e2014-08-26 09:08:04 -070089 // for SkTDynamicHash::Traits
90 static uint32_t Hash(const Key& key) { return key.hash(); }
91 static const Key& GetKey(const Rec& rec) { return rec.getKey(); }
92
93 private:
94 Rec* fNext;
95 Rec* fPrev;
piotaixr81591462014-09-02 11:27:11 -070096
reed011f39a2014-08-28 13:35:23 -070097 friend class SkResourceCache;
reed680fb9e2014-08-26 09:08:04 -070098 };
99
reed7eeba252015-02-24 13:54:23 -0800100 // Used with SkMessageBus
101 struct PurgeSharedIDMessage {
102 PurgeSharedIDMessage(uint64_t sharedID) : fSharedID(sharedID) {}
103
104 uint64_t fSharedID;
105 };
106
reed680fb9e2014-08-26 09:08:04 -0700107 typedef const Rec* ID;
108
reed@google.come4eb1222013-12-09 22:29:30 +0000109 /**
reedc90e0142014-09-15 11:39:44 -0700110 * Callback function for find(). If called, the cache will have found a match for the
111 * specified Key, and will pass in the corresponding Rec, along with a caller-specified
112 * context. The function can read the data in Rec, and copy whatever it likes into context
113 * (casting context to whatever it really is).
114 *
115 * The return value determines what the cache will do with the Rec. If the function returns
116 * true, then the Rec is considered "valid". If false is returned, the Rec will be considered
117 * "stale" and will be purged from the cache.
118 */
reed7eeba252015-02-24 13:54:23 -0800119 typedef bool (*FindVisitor)(const Rec&, void* context);
reedc90e0142014-09-15 11:39:44 -0700120
121 /**
reed@google.come4eb1222013-12-09 22:29:30 +0000122 * Returns a locked/pinned SkDiscardableMemory instance for the specified
halcanary96fcdcc2015-08-27 07:41:13 -0700123 * number of bytes, or nullptr on failure.
reed@google.come4eb1222013-12-09 22:29:30 +0000124 */
125 typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes);
126
reed@google.com602a1d72013-07-23 19:13:54 +0000127 /*
128 * The following static methods are thread-safe wrappers around a global
129 * instance of this cache.
130 */
131
reedc90e0142014-09-15 11:39:44 -0700132 /**
133 * Returns true if the visitor was called on a matching Key, and the visitor returned true.
134 *
135 * Find() will search the cache for the specified Key. If no match is found, return false and
reed7eeba252015-02-24 13:54:23 -0800136 * do not call the FindVisitor. If a match is found, return whatever the visitor returns.
reedc90e0142014-09-15 11:39:44 -0700137 * Its return value is interpreted to mean:
138 * true : Rec is valid
139 * false : Rec is "stale" -- the cache will purge it.
140 */
reed7eeba252015-02-24 13:54:23 -0800141 static bool Find(const Key& key, FindVisitor, void* context);
reed680fb9e2014-08-26 09:08:04 -0700142 static void Add(Rec*);
reed@google.com602a1d72013-07-23 19:13:54 +0000143
reed216b6432015-08-19 12:25:40 -0700144 typedef void (*Visitor)(const Rec&, void* context);
145 // Call the visitor for every Rec in the cache.
146 static void VisitAll(Visitor, void* context);
147
halcanary805ef152014-07-17 06:58:01 -0700148 static size_t GetTotalBytesUsed();
149 static size_t GetTotalByteLimit();
150 static size_t SetTotalByteLimit(size_t newLimit);
151
152 static size_t SetSingleAllocationByteLimit(size_t);
153 static size_t GetSingleAllocationByteLimit();
reed1d9e80f2015-01-26 11:24:37 -0800154 static size_t GetEffectiveSingleAllocationByteLimit();
reed@google.com602a1d72013-07-23 19:13:54 +0000155
reed56b00d92014-09-11 12:22:34 -0700156 static void PurgeAll();
157
reed216b6432015-08-19 12:25:40 -0700158 static void TestDumpMemoryStatistics();
159
ssid33c594c2015-08-27 09:23:54 -0700160 /** Dump memory usage statistics of every Rec in the cache using the
161 SkTraceMemoryDump interface.
162 */
163 static void DumpMemoryStatistics(SkTraceMemoryDump* dump);
164
piotaixr81591462014-09-02 11:27:11 -0700165 /**
halcanary96fcdcc2015-08-27 07:41:13 -0700166 * Returns the DiscardableFactory used by the global cache, or nullptr.
reed30ad5302014-09-16 10:39:55 -0700167 */
168 static DiscardableFactory GetDiscardableFactory();
169
170 /**
piotaixr81591462014-09-02 11:27:11 -0700171 * Use this allocator for bitmaps, so they can use ashmem when available.
halcanary96fcdcc2015-08-27 07:41:13 -0700172 * Returns nullptr if the ResourceCache has not been initialized with a DiscardableFactory.
piotaixr81591462014-09-02 11:27:11 -0700173 */
reed@google.come4eb1222013-12-09 22:29:30 +0000174 static SkBitmap::Allocator* GetAllocator();
175
qiankun.miaod9aac342014-10-23 07:58:17 -0700176 static SkCachedData* NewCachedData(size_t bytes);
177
reed7eeba252015-02-24 13:54:23 -0800178 static void PostPurgeSharedID(uint64_t sharedID);
179
reed@google.comfa7fd802013-12-12 21:37:25 +0000180 /**
181 * Call SkDebugf() with diagnostic information about the state of the cache
182 */
183 static void Dump();
184
reed@google.com602a1d72013-07-23 19:13:54 +0000185 ///////////////////////////////////////////////////////////////////////////
186
reed@google.come4eb1222013-12-09 22:29:30 +0000187 /**
188 * Construct the cache to call DiscardableFactory when it
189 * allocates memory for the pixels. In this mode, the cache has
halcanary805ef152014-07-17 06:58:01 -0700190 * not explicit budget, and so methods like getTotalBytesUsed()
191 * and getTotalByteLimit() will return 0, and setTotalByteLimit
192 * will ignore its argument and return 0.
reed@google.come4eb1222013-12-09 22:29:30 +0000193 */
reed011f39a2014-08-28 13:35:23 -0700194 SkResourceCache(DiscardableFactory);
reed@google.come4eb1222013-12-09 22:29:30 +0000195
196 /**
197 * Construct the cache, allocating memory with malloc, and respect the
198 * byteLimit, purging automatically when a new image is added to the cache
199 * that pushes the total bytesUsed over the limit. Note: The limit can be
halcanary805ef152014-07-17 06:58:01 -0700200 * changed at runtime with setTotalByteLimit.
reed@google.come4eb1222013-12-09 22:29:30 +0000201 */
reed011f39a2014-08-28 13:35:23 -0700202 explicit SkResourceCache(size_t byteLimit);
203 ~SkResourceCache();
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +0000204
reed595aa052014-09-15 10:15:18 -0700205 /**
reedc90e0142014-09-15 11:39:44 -0700206 * Returns true if the visitor was called on a matching Key, and the visitor returned true.
207 *
208 * find() will search the cache for the specified Key. If no match is found, return false and
reed7eeba252015-02-24 13:54:23 -0800209 * do not call the FindVisitor. If a match is found, return whatever the visitor returns.
reedc90e0142014-09-15 11:39:44 -0700210 * Its return value is interpreted to mean:
211 * true : Rec is valid
212 * false : Rec is "stale" -- the cache will purge it.
reed595aa052014-09-15 10:15:18 -0700213 */
reed7eeba252015-02-24 13:54:23 -0800214 bool find(const Key&, FindVisitor, void* context);
reedc90e0142014-09-15 11:39:44 -0700215 void add(Rec*);
reed216b6432015-08-19 12:25:40 -0700216 void visitAll(Visitor, void* context);
reed@google.com602a1d72013-07-23 19:13:54 +0000217
halcanary805ef152014-07-17 06:58:01 -0700218 size_t getTotalBytesUsed() const { return fTotalBytesUsed; }
219 size_t getTotalByteLimit() const { return fTotalByteLimit; }
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +0000220
reed@google.com602a1d72013-07-23 19:13:54 +0000221 /**
halcanary805ef152014-07-17 06:58:01 -0700222 * This is respected by SkBitmapProcState::possiblyScaleImage.
223 * 0 is no maximum at all; this is the default.
224 * setSingleAllocationByteLimit() returns the previous value.
225 */
226 size_t setSingleAllocationByteLimit(size_t maximumAllocationSize);
227 size_t getSingleAllocationByteLimit() const;
reed1d9e80f2015-01-26 11:24:37 -0800228 // returns the logical single allocation size (pinning against the budget when the cache
229 // is not backed by discardable memory.
230 size_t getEffectiveSingleAllocationByteLimit() const;
231
halcanary805ef152014-07-17 06:58:01 -0700232 /**
reed@google.com602a1d72013-07-23 19:13:54 +0000233 * Set the maximum number of bytes available to this cache. If the current
234 * cache exceeds this new value, it will be purged to try to fit within
235 * this new limit.
236 */
halcanary805ef152014-07-17 06:58:01 -0700237 size_t setTotalByteLimit(size_t newLimit);
reed@google.com602a1d72013-07-23 19:13:54 +0000238
reed7eeba252015-02-24 13:54:23 -0800239 void purgeSharedID(uint64_t sharedID);
240
reed56b00d92014-09-11 12:22:34 -0700241 void purgeAll() {
242 this->purgeAsNeeded(true);
243 }
244
reed30ad5302014-09-16 10:39:55 -0700245 DiscardableFactory discardableFactory() const { return fDiscardableFactory; }
reed@google.come4eb1222013-12-09 22:29:30 +0000246 SkBitmap::Allocator* allocator() const { return fAllocator; };
247
reed9d93c2e2014-10-08 05:17:12 -0700248 SkCachedData* newCachedData(size_t bytes);
249
reed@google.comfa7fd802013-12-12 21:37:25 +0000250 /**
251 * Call SkDebugf() with diagnostic information about the state of the cache
252 */
253 void dump() const;
254
reed@google.com5d1e5582013-07-25 14:36:15 +0000255private:
reed@google.com602a1d72013-07-23 19:13:54 +0000256 Rec* fHead;
257 Rec* fTail;
258
reed@google.com5d1e5582013-07-25 14:36:15 +0000259 class Hash;
260 Hash* fHash;
261
reed@google.come4eb1222013-12-09 22:29:30 +0000262 DiscardableFactory fDiscardableFactory;
halcanary96fcdcc2015-08-27 07:41:13 -0700263 // the allocator is nullptr or one that matches discardables
reed@google.come4eb1222013-12-09 22:29:30 +0000264 SkBitmap::Allocator* fAllocator;
265
halcanary805ef152014-07-17 06:58:01 -0700266 size_t fTotalBytesUsed;
267 size_t fTotalByteLimit;
268 size_t fSingleAllocationByteLimit;
reed@google.com602a1d72013-07-23 19:13:54 +0000269 int fCount;
270
reed7eeba252015-02-24 13:54:23 -0800271 SkMessageBus<PurgeSharedIDMessage>::Inbox fPurgeSharedIDInbox;
272
273 void checkMessages();
reed56b00d92014-09-11 12:22:34 -0700274 void purgeAsNeeded(bool forcePurge = false);
reed@google.com602a1d72013-07-23 19:13:54 +0000275
276 // linklist management
277 void moveToHead(Rec*);
278 void addToHead(Rec*);
mtklein18300a32016-03-16 13:53:35 -0700279 void release(Rec*);
reedc90e0142014-09-15 11:39:44 -0700280 void remove(Rec*);
reed@google.come4eb1222013-12-09 22:29:30 +0000281
282 void init(); // called by constructors
283
reed@google.com602a1d72013-07-23 19:13:54 +0000284#ifdef SK_DEBUG
285 void validate() const;
286#else
287 void validate() const {}
288#endif
289};
reed@google.com602a1d72013-07-23 19:13:54 +0000290#endif