Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(417)

Side by Side Diff: src/core/SkScaledImageCache.h

Issue 507483002: retool image cache to be generic cache (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: rebase Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 SkScaledImageCache_DEFINED 8 #ifndef SkScaledImageCache_DEFINED
9 #define SkScaledImageCache_DEFINED 9 #define SkScaledImageCache_DEFINED
10 10
11 #include "SkBitmap.h" 11 #include "SkBitmap.h"
12 12
13 class SkDiscardableMemory; 13 class SkDiscardableMemory;
14 class SkMipMap; 14 class SkMipMap;
15 15
16 /** 16 /**
17 * Cache object for bitmaps (with possible scale in X Y as part of the key). 17 * Cache object for bitmaps (with possible scale in X Y as part of the key).
18 * 18 *
19 * Multiple caches can be instantiated, but each instance is not implicitly 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 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). 21 * caller must manage the access itself (e.g. via a mutex).
22 * 22 *
23 * As a convenience, a global instance is also defined, which can be safely 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.). 24 * access across threads via the static methods (e.g. FindAndLock, etc.).
25 */ 25 */
26 class SkScaledImageCache { 26 class SkScaledImageCache {
27 public: 27 public:
28 struct ID;
29
30 struct Key { 28 struct Key {
31 // Call this to access your private contents. Must not use the address a fter calling init() 29 // Call this to access your private contents. Must not use the address a fter calling init()
32 void* writableContents() { return this + 1; } 30 void* writableContents() { return this + 1; }
33 31
34 // must call this after your private data has been written. 32 // must call this after your private data has been written.
35 // length must be a multiple of 4 33 // length must be a multiple of 4
36 void init(size_t length); 34 void init(size_t length);
37 35
38 // This is only valid after having called init(). 36 // This is only valid after having called init().
39 uint32_t hash() const { return fHash; } 37 uint32_t hash() const { return fHash; }
40 38
41 bool operator==(const Key& other) const { 39 bool operator==(const Key& other) const {
42 const uint32_t* a = this->as32(); 40 const uint32_t* a = this->as32();
43 const uint32_t* b = other.as32(); 41 const uint32_t* b = other.as32();
44 for (int i = 0; i < fCount32; ++i) { 42 for (int i = 0; i < fCount32; ++i) {
45 if (a[i] != b[i]) { 43 if (a[i] != b[i]) {
46 return false; 44 return false;
47 } 45 }
48 } 46 }
49 return true; 47 return true;
50 } 48 }
51 49
52 // delete using sk_free
53 Key* clone() const;
54
55 private: 50 private:
56 // store fCount32 first, so we don't consider it in operator< 51 // store fCount32 first, so we don't consider it in operator<
57 int32_t fCount32; // 2 + user contents count32 52 int32_t fCount32; // 2 + user contents count32
58 uint32_t fHash; 53 uint32_t fHash;
59 /* uint32_t fContents32[] */ 54 /* uint32_t fContents32[] */
60 55
61 const uint32_t* as32() const { return (const uint32_t*)this; } 56 const uint32_t* as32() const { return (const uint32_t*)this; }
62 const uint32_t* as32SkipCount() const { return this->as32() + 1; } 57 const uint32_t* as32SkipCount() const { return this->as32() + 1; }
63 }; 58 };
64 59
60 struct Rec {
61 typedef SkScaledImageCache::Key Key;
62
63 Rec() : fLockCount(1) {}
64 virtual ~Rec() {}
65
66 uint32_t getHash() const { return this->getKey().hash(); }
67
68 virtual const Key& getKey() const = 0;
69 virtual size_t bytesUsed() const = 0;
70
71 // 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;
78 int32_t fLockCount;
79 int32_t fPad;
80
81 friend class SkScaledImageCache;
82 };
83
84 typedef const Rec* ID;
85
65 /** 86 /**
66 * Returns a locked/pinned SkDiscardableMemory instance for the specified 87 * Returns a locked/pinned SkDiscardableMemory instance for the specified
67 * number of bytes, or NULL on failure. 88 * number of bytes, or NULL on failure.
68 */ 89 */
69 typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes); 90 typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes);
70 91
71 /* 92 /*
72 * The following static methods are thread-safe wrappers around a global 93 * The following static methods are thread-safe wrappers around a global
73 * instance of this cache. 94 * instance of this cache.
74 */ 95 */
75 96
76 static ID* FindAndLock(const Key&, SkBitmap* result); 97 static const Rec* FindAndLock(const Key& key);
77 static ID* AddAndLock(const Key&, const SkBitmap& result); 98 static const Rec* AddAndLock(Rec*);
78 99 static void Unlock(ID);
79 static ID* FindAndLock(const Key&, const SkMipMap** result);
80 static ID* AddAndLock(const Key&, const SkMipMap* result);
81
82 static void Unlock(ID*);
83 100
84 static size_t GetTotalBytesUsed(); 101 static size_t GetTotalBytesUsed();
85 static size_t GetTotalByteLimit(); 102 static size_t GetTotalByteLimit();
86 static size_t SetTotalByteLimit(size_t newLimit); 103 static size_t SetTotalByteLimit(size_t newLimit);
87 104
88 static size_t SetSingleAllocationByteLimit(size_t); 105 static size_t SetSingleAllocationByteLimit(size_t);
89 static size_t GetSingleAllocationByteLimit(); 106 static size_t GetSingleAllocationByteLimit();
90 107
91 static SkBitmap::Allocator* GetAllocator(); 108 static SkBitmap::Allocator* GetAllocator();
92 109
(...skipping 15 matching lines...) Expand all
108 125
109 /** 126 /**
110 * Construct the cache, allocating memory with malloc, and respect the 127 * Construct the cache, allocating memory with malloc, and respect the
111 * byteLimit, purging automatically when a new image is added to the cache 128 * byteLimit, purging automatically when a new image is added to the cache
112 * that pushes the total bytesUsed over the limit. Note: The limit can be 129 * that pushes the total bytesUsed over the limit. Note: The limit can be
113 * changed at runtime with setTotalByteLimit. 130 * changed at runtime with setTotalByteLimit.
114 */ 131 */
115 explicit SkScaledImageCache(size_t byteLimit); 132 explicit SkScaledImageCache(size_t byteLimit);
116 ~SkScaledImageCache(); 133 ~SkScaledImageCache();
117 134
118 /** 135 const Rec* findAndLock(const Key& key);
119 * Search the cache for a matching key. If found, return its bitmap and ret urn its ID pointer. 136 const Rec* addAndLock(Rec*);
120 * Use the returned ID to unlock the cache when you are done using outBitma p.
121 *
122 * If a match is not found, outBitmap will be unmodifed, and NULL will be r eturned.
123 */
124 ID* findAndLock(const Key& key, SkBitmap* outBitmap);
125 ID* findAndLock(const Key& key, const SkMipMap** returnedMipMap);
126
127 /**
128 * To add a new bitmap (or mipMap) to the cache, call
129 * AddAndLock. Use the returned ptr to unlock the cache when you
130 * are done using scaled.
131 */
132 ID* addAndLock(const Key&, const SkBitmap& bitmap);
133 ID* addAndLock(const Key&, const SkMipMap* mipMap);
134 137
135 /** 138 /**
136 * Given a non-null ID ptr returned by either findAndLock or addAndLock, 139 * Given a non-null ID ptr returned by either findAndLock or addAndLock,
137 * this releases the associated resources to be available to be purged 140 * this releases the associated resources to be available to be purged
138 * if needed. After this, the cached bitmap should no longer be 141 * if needed. After this, the cached bitmap should no longer be
139 * referenced by the caller. 142 * referenced by the caller.
140 */ 143 */
141 void unlock(ID*); 144 void unlock(ID);
142 145
143 size_t getTotalBytesUsed() const { return fTotalBytesUsed; } 146 size_t getTotalBytesUsed() const { return fTotalBytesUsed; }
144 size_t getTotalByteLimit() const { return fTotalByteLimit; } 147 size_t getTotalByteLimit() const { return fTotalByteLimit; }
145 148
146 /** 149 /**
147 * This is respected by SkBitmapProcState::possiblyScaleImage. 150 * This is respected by SkBitmapProcState::possiblyScaleImage.
148 * 0 is no maximum at all; this is the default. 151 * 0 is no maximum at all; this is the default.
149 * setSingleAllocationByteLimit() returns the previous value. 152 * setSingleAllocationByteLimit() returns the previous value.
150 */ 153 */
151 size_t setSingleAllocationByteLimit(size_t maximumAllocationSize); 154 size_t setSingleAllocationByteLimit(size_t maximumAllocationSize);
152 size_t getSingleAllocationByteLimit() const; 155 size_t getSingleAllocationByteLimit() const;
153 /** 156 /**
154 * Set the maximum number of bytes available to this cache. If the current 157 * Set the maximum number of bytes available to this cache. If the current
155 * cache exceeds this new value, it will be purged to try to fit within 158 * cache exceeds this new value, it will be purged to try to fit within
156 * this new limit. 159 * this new limit.
157 */ 160 */
158 size_t setTotalByteLimit(size_t newLimit); 161 size_t setTotalByteLimit(size_t newLimit);
159 162
160 SkBitmap::Allocator* allocator() const { return fAllocator; }; 163 SkBitmap::Allocator* allocator() const { return fAllocator; };
161 164
162 /** 165 /**
163 * Call SkDebugf() with diagnostic information about the state of the cache 166 * Call SkDebugf() with diagnostic information about the state of the cache
164 */ 167 */
165 void dump() const; 168 void dump() const;
166 169
167 public:
168 struct Rec;
169 private: 170 private:
170 Rec* fHead; 171 Rec* fHead;
171 Rec* fTail; 172 Rec* fTail;
172 173
173 class Hash; 174 class Hash;
174 Hash* fHash; 175 Hash* fHash;
175 176
176 DiscardableFactory fDiscardableFactory; 177 DiscardableFactory fDiscardableFactory;
177 // the allocator is NULL or one that matches discardables 178 // the allocator is NULL or one that matches discardables
178 SkBitmap::Allocator* fAllocator; 179 SkBitmap::Allocator* fAllocator;
179 180
180 size_t fTotalBytesUsed; 181 size_t fTotalBytesUsed;
181 size_t fTotalByteLimit; 182 size_t fTotalByteLimit;
182 size_t fSingleAllocationByteLimit; 183 size_t fSingleAllocationByteLimit;
183 int fCount; 184 int fCount;
184 185
185 Rec* findAndLock(const Key& key);
186 ID* addAndLock(Rec* rec);
187
188 void purgeRec(Rec*); 186 void purgeRec(Rec*);
189 void purgeAsNeeded(); 187 void purgeAsNeeded();
190 188
191 // linklist management 189 // linklist management
192 void moveToHead(Rec*); 190 void moveToHead(Rec*);
193 void addToHead(Rec*); 191 void addToHead(Rec*);
194 void detach(Rec*); 192 void detach(Rec*);
195 193
196 void init(); // called by constructors 194 void init(); // called by constructors
197 195
198 #ifdef SK_DEBUG 196 #ifdef SK_DEBUG
199 void validate() const; 197 void validate() const;
200 #else 198 #else
201 void validate() const {} 199 void validate() const {}
202 #endif 200 #endif
203 }; 201 };
204 #endif 202 #endif
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698