blob: 52196985d392cd327516f3dd97a7853955ba8c9a [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
reedc90e0142014-09-15 11:39:44 -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 /**
reedc90e0142014-09-15 11:39:44 -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
reedc90e0142014-09-15 11:39:44 -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 /**
reed30ad5302014-09-16 10:39:55 -0700129 * Returns the DiscardableFactory used by the global cache, or NULL.
130 */
131 static DiscardableFactory GetDiscardableFactory();
132
133 /**
piotaixr81591462014-09-02 11:27:11 -0700134 * Use this allocator for bitmaps, so they can use ashmem when available.
135 * Returns NULL if the ResourceCache has not been initialized with a DiscardableFactory.
136 */
reed@google.come4eb1222013-12-09 22:29:30 +0000137 static SkBitmap::Allocator* GetAllocator();
138
reed@google.comfa7fd802013-12-12 21:37:25 +0000139 /**
140 * Call SkDebugf() with diagnostic information about the state of the cache
141 */
142 static void Dump();
143
reed@google.com602a1d72013-07-23 19:13:54 +0000144 ///////////////////////////////////////////////////////////////////////////
145
reed@google.come4eb1222013-12-09 22:29:30 +0000146 /**
147 * Construct the cache to call DiscardableFactory when it
148 * allocates memory for the pixels. In this mode, the cache has
halcanary805ef152014-07-17 06:58:01 -0700149 * not explicit budget, and so methods like getTotalBytesUsed()
150 * and getTotalByteLimit() will return 0, and setTotalByteLimit
151 * will ignore its argument and return 0.
reed@google.come4eb1222013-12-09 22:29:30 +0000152 */
reed011f39a2014-08-28 13:35:23 -0700153 SkResourceCache(DiscardableFactory);
reed@google.come4eb1222013-12-09 22:29:30 +0000154
155 /**
156 * Construct the cache, allocating memory with malloc, and respect the
157 * byteLimit, purging automatically when a new image is added to the cache
158 * that pushes the total bytesUsed over the limit. Note: The limit can be
halcanary805ef152014-07-17 06:58:01 -0700159 * changed at runtime with setTotalByteLimit.
reed@google.come4eb1222013-12-09 22:29:30 +0000160 */
reed011f39a2014-08-28 13:35:23 -0700161 explicit SkResourceCache(size_t byteLimit);
162 ~SkResourceCache();
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +0000163
reed595aa052014-09-15 10:15:18 -0700164 /**
reedc90e0142014-09-15 11:39:44 -0700165 * Returns true if the visitor was called on a matching Key, and the visitor returned true.
166 *
167 * find() will search the cache for the specified Key. If no match is found, return false and
168 * do not call the VisitorProc. If a match is found, return whatever the visitor returns.
169 * Its return value is interpreted to mean:
170 * true : Rec is valid
171 * false : Rec is "stale" -- the cache will purge it.
reed595aa052014-09-15 10:15:18 -0700172 */
reedc90e0142014-09-15 11:39:44 -0700173 bool find(const Key&, VisitorProc, void* context);
174 void add(Rec*);
reed@google.com602a1d72013-07-23 19:13:54 +0000175
halcanary805ef152014-07-17 06:58:01 -0700176 size_t getTotalBytesUsed() const { return fTotalBytesUsed; }
177 size_t getTotalByteLimit() const { return fTotalByteLimit; }
skia.committer@gmail.com7f1af502013-07-24 07:01:12 +0000178
reed@google.com602a1d72013-07-23 19:13:54 +0000179 /**
halcanary805ef152014-07-17 06:58:01 -0700180 * This is respected by SkBitmapProcState::possiblyScaleImage.
181 * 0 is no maximum at all; this is the default.
182 * setSingleAllocationByteLimit() returns the previous value.
183 */
184 size_t setSingleAllocationByteLimit(size_t maximumAllocationSize);
185 size_t getSingleAllocationByteLimit() const;
186 /**
reed@google.com602a1d72013-07-23 19:13:54 +0000187 * Set the maximum number of bytes available to this cache. If the current
188 * cache exceeds this new value, it will be purged to try to fit within
189 * this new limit.
190 */
halcanary805ef152014-07-17 06:58:01 -0700191 size_t setTotalByteLimit(size_t newLimit);
reed@google.com602a1d72013-07-23 19:13:54 +0000192
reed56b00d92014-09-11 12:22:34 -0700193 void purgeAll() {
194 this->purgeAsNeeded(true);
195 }
196
reed30ad5302014-09-16 10:39:55 -0700197 DiscardableFactory discardableFactory() const { return fDiscardableFactory; }
reed@google.come4eb1222013-12-09 22:29:30 +0000198 SkBitmap::Allocator* allocator() const { return fAllocator; };
199
reed@google.comfa7fd802013-12-12 21:37:25 +0000200 /**
201 * Call SkDebugf() with diagnostic information about the state of the cache
202 */
203 void dump() const;
204
reed@google.com5d1e5582013-07-25 14:36:15 +0000205private:
reed@google.com602a1d72013-07-23 19:13:54 +0000206 Rec* fHead;
207 Rec* fTail;
208
reed@google.com5d1e5582013-07-25 14:36:15 +0000209 class Hash;
210 Hash* fHash;
211
reed@google.come4eb1222013-12-09 22:29:30 +0000212 DiscardableFactory fDiscardableFactory;
213 // the allocator is NULL or one that matches discardables
214 SkBitmap::Allocator* fAllocator;
215
halcanary805ef152014-07-17 06:58:01 -0700216 size_t fTotalBytesUsed;
217 size_t fTotalByteLimit;
218 size_t fSingleAllocationByteLimit;
reed@google.com602a1d72013-07-23 19:13:54 +0000219 int fCount;
220
reed56b00d92014-09-11 12:22:34 -0700221 void purgeAsNeeded(bool forcePurge = false);
reed@google.com602a1d72013-07-23 19:13:54 +0000222
223 // linklist management
224 void moveToHead(Rec*);
225 void addToHead(Rec*);
226 void detach(Rec*);
reedc90e0142014-09-15 11:39:44 -0700227 void remove(Rec*);
reed@google.come4eb1222013-12-09 22:29:30 +0000228
229 void init(); // called by constructors
230
reed@google.com602a1d72013-07-23 19:13:54 +0000231#ifdef SK_DEBUG
232 void validate() const;
233#else
234 void validate() const {}
235#endif
236};
reed@google.com602a1d72013-07-23 19:13:54 +0000237#endif