blob: d4e1dfa3766fb74a94181ef5ff01092283a84107 [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"
12
reed@google.come4eb1222013-12-09 22:29:30 +000013class SkDiscardableMemory;
reed@google.comd94697c2013-07-24 14:31:33 +000014class SkMipMap;
15
reed@google.com602a1d72013-07-23 19:13:54 +000016/**
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 */
reed011f39a2014-08-28 13:35:23 -070026class SkResourceCache {
reed@google.com602a1d72013-07-23 19:13:54 +000027public:
reed4f987e92014-08-20 13:41:56 -070028 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
reed4f987e92014-08-20 13:41:56 -070050 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
reed680fb9e2014-08-26 09:08:04 -070060 struct Rec {
reed011f39a2014-08-28 13:35:23 -070061 typedef SkResourceCache::Key Key;
reed680fb9e2014-08-26 09:08:04 -070062
reeddee6a8e2014-09-15 06:44:47 -070063 Rec() {}
reed680fb9e2014-08-26 09:08:04 -070064 virtual ~Rec() {}
65
66 uint32_t getHash() const { return this->getKey().hash(); }
piotaixr81591462014-09-02 11:27:11 -070067
reed680fb9e2014-08-26 09:08:04 -070068 virtual const Key& getKey() const = 0;
69 virtual size_t bytesUsed() const = 0;
piotaixr81591462014-09-02 11:27:11 -070070
reed680fb9e2014-08-26 09:08:04 -070071 // 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;
piotaixr81591462014-09-02 11:27:11 -070078
reed011f39a2014-08-28 13:35:23 -070079 friend class SkResourceCache;
reed680fb9e2014-08-26 09:08:04 -070080 };
81
82 typedef const Rec* ID;
83
reed@google.come4eb1222013-12-09 22:29:30 +000084 /**
reeddee6a8e2014-09-15 06:44:47 -070085 * 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.come4eb1222013-12-09 22:29:30 +000097 * 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.com602a1d72013-07-23 19:13:54 +0000102 /*
103 * The following static methods are thread-safe wrappers around a global
104 * instance of this cache.
105 */
106
reeddee6a8e2014-09-15 06:44:47 -0700107 /**
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);
reed680fb9e2014-08-26 09:08:04 -0700117 static void Add(Rec*);
reed@google.com602a1d72013-07-23 19:13:54 +0000118
halcanary805ef152014-07-17 06:58:01 -0700119 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.com602a1d72013-07-23 19:13:54 +0000125
reed56b00d92014-09-11 12:22:34 -0700126 static void PurgeAll();
127
piotaixr81591462014-09-02 11:27:11 -0700128 /**
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.come4eb1222013-12-09 22:29:30 +0000132 static SkBitmap::Allocator* GetAllocator();
133
reed@google.comfa7fd802013-12-12 21:37:25 +0000134 /**
135 * Call SkDebugf() with diagnostic information about the state of the cache
136 */
137 static void Dump();
138
reed@google.com602a1d72013-07-23 19:13:54 +0000139 ///////////////////////////////////////////////////////////////////////////
140
reed@google.come4eb1222013-12-09 22:29:30 +0000141 /**
142 * Construct the cache to call DiscardableFactory when it
143 * allocates memory for the pixels. In this mode, the cache has
halcanary805ef152014-07-17 06:58:01 -0700144 * 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.come4eb1222013-12-09 22:29:30 +0000147 */
reed011f39a2014-08-28 13:35:23 -0700148 SkResourceCache(DiscardableFactory);
reed@google.come4eb1222013-12-09 22:29:30 +0000149
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
halcanary805ef152014-07-17 06:58:01 -0700154 * changed at runtime with setTotalByteLimit.
reed@google.come4eb1222013-12-09 22:29:30 +0000155 */
reed011f39a2014-08-28 13:35:23 -0700156 explicit SkResourceCache(size_t byteLimit);
157 ~SkResourceCache();
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +0000158
reed@google.com602a1d72013-07-23 19:13:54 +0000159 /**
reeddee6a8e2014-09-15 06:44:47 -0700160 * 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.com602a1d72013-07-23 19:13:54 +0000167 */
reeddee6a8e2014-09-15 06:44:47 -0700168 bool find(const Key&, VisitorProc, void* context);
169 void add(Rec*);
reed@google.com602a1d72013-07-23 19:13:54 +0000170
halcanary805ef152014-07-17 06:58:01 -0700171 size_t getTotalBytesUsed() const { return fTotalBytesUsed; }
172 size_t getTotalByteLimit() const { return fTotalByteLimit; }
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +0000173
reed@google.com602a1d72013-07-23 19:13:54 +0000174 /**
halcanary805ef152014-07-17 06:58:01 -0700175 * 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.com602a1d72013-07-23 19:13:54 +0000182 * 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 */
halcanary805ef152014-07-17 06:58:01 -0700186 size_t setTotalByteLimit(size_t newLimit);
reed@google.com602a1d72013-07-23 19:13:54 +0000187
reed56b00d92014-09-11 12:22:34 -0700188 void purgeAll() {
189 this->purgeAsNeeded(true);
190 }
191
reed@google.come4eb1222013-12-09 22:29:30 +0000192 SkBitmap::Allocator* allocator() const { return fAllocator; };
193
reed@google.comfa7fd802013-12-12 21:37:25 +0000194 /**
195 * Call SkDebugf() with diagnostic information about the state of the cache
196 */
197 void dump() const;
198
reed@google.com5d1e5582013-07-25 14:36:15 +0000199private:
reed@google.com602a1d72013-07-23 19:13:54 +0000200 Rec* fHead;
201 Rec* fTail;
202
reed@google.com5d1e5582013-07-25 14:36:15 +0000203 class Hash;
204 Hash* fHash;
205
reed@google.come4eb1222013-12-09 22:29:30 +0000206 DiscardableFactory fDiscardableFactory;
207 // the allocator is NULL or one that matches discardables
208 SkBitmap::Allocator* fAllocator;
209
halcanary805ef152014-07-17 06:58:01 -0700210 size_t fTotalBytesUsed;
211 size_t fTotalByteLimit;
212 size_t fSingleAllocationByteLimit;
reed@google.com602a1d72013-07-23 19:13:54 +0000213 int fCount;
214
reed56b00d92014-09-11 12:22:34 -0700215 void purgeAsNeeded(bool forcePurge = false);
reed@google.com602a1d72013-07-23 19:13:54 +0000216
217 // linklist management
218 void moveToHead(Rec*);
219 void addToHead(Rec*);
220 void detach(Rec*);
reeddee6a8e2014-09-15 06:44:47 -0700221 void remove(Rec*);
reed@google.come4eb1222013-12-09 22:29:30 +0000222
223 void init(); // called by constructors
224
reed@google.com602a1d72013-07-23 19:13:54 +0000225#ifdef SK_DEBUG
226 void validate() const;
227#else
228 void validate() const {}
229#endif
230};
reed@google.com602a1d72013-07-23 19:13:54 +0000231#endif