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

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

Issue 569353002: Change SkResourceCache to take a Visitor inside its find(). (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: remember to purge after the add 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
« no previous file with comments | « src/core/SkBitmapCache.cpp ('k') | src/core/SkResourceCache.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 SkResourceCache_DEFINED 8 #ifndef SkResourceCache_DEFINED
9 #define SkResourceCache_DEFINED 9 #define SkResourceCache_DEFINED
10 10
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
53 uint32_t fHash; 53 uint32_t fHash;
54 /* uint32_t fContents32[] */ 54 /* uint32_t fContents32[] */
55 55
56 const uint32_t* as32() const { return (const uint32_t*)this; } 56 const uint32_t* as32() const { return (const uint32_t*)this; }
57 const uint32_t* as32SkipCount() const { return this->as32() + 1; } 57 const uint32_t* as32SkipCount() const { return this->as32() + 1; }
58 }; 58 };
59 59
60 struct Rec { 60 struct Rec {
61 typedef SkResourceCache::Key Key; 61 typedef SkResourceCache::Key Key;
62 62
63 Rec() : fLockCount(1) {} 63 Rec() {}
64 virtual ~Rec() {} 64 virtual ~Rec() {}
65 65
66 uint32_t getHash() const { return this->getKey().hash(); } 66 uint32_t getHash() const { return this->getKey().hash(); }
67 67
68 virtual const Key& getKey() const = 0; 68 virtual const Key& getKey() const = 0;
69 virtual size_t bytesUsed() const = 0; 69 virtual size_t bytesUsed() const = 0;
70 70
71 // for SkTDynamicHash::Traits 71 // for SkTDynamicHash::Traits
72 static uint32_t Hash(const Key& key) { return key.hash(); } 72 static uint32_t Hash(const Key& key) { return key.hash(); }
73 static const Key& GetKey(const Rec& rec) { return rec.getKey(); } 73 static const Key& GetKey(const Rec& rec) { return rec.getKey(); }
74 74
75 private: 75 private:
76 Rec* fNext; 76 Rec* fNext;
77 Rec* fPrev; 77 Rec* fPrev;
78 int32_t fLockCount;
79 78
80 friend class SkResourceCache; 79 friend class SkResourceCache;
81 }; 80 };
82 81
83 typedef const Rec* ID; 82 typedef const Rec* ID;
84 83
85 /** 84 /**
85 * Callback function for find(). If called, the cache will have found a mat ch for the
86 * specified Key, and will pass in the corresponding Rec, along with a call er-specified
87 * context. The function can read the data in Rec, and copy whatever it lik es 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 /**
86 * Returns a locked/pinned SkDiscardableMemory instance for the specified 97 * Returns a locked/pinned SkDiscardableMemory instance for the specified
87 * number of bytes, or NULL on failure. 98 * number of bytes, or NULL on failure.
88 */ 99 */
89 typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes); 100 typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes);
90 101
91 /* 102 /*
92 * The following static methods are thread-safe wrappers around a global 103 * The following static methods are thread-safe wrappers around a global
93 * instance of this cache. 104 * instance of this cache.
94 */ 105 */
95 106
96 static const Rec* FindAndLock(const Key& key); 107 /**
97 static const Rec* AddAndLock(Rec*); 108 * Returns true if the visitor was called on a matching Key, and the visito r 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 vi sitor 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);
98 static void Add(Rec*); 117 static void Add(Rec*);
99 static void Unlock(ID);
100 static void Remove(ID);
101 118
102 static size_t GetTotalBytesUsed(); 119 static size_t GetTotalBytesUsed();
103 static size_t GetTotalByteLimit(); 120 static size_t GetTotalByteLimit();
104 static size_t SetTotalByteLimit(size_t newLimit); 121 static size_t SetTotalByteLimit(size_t newLimit);
105 122
106 static size_t SetSingleAllocationByteLimit(size_t); 123 static size_t SetSingleAllocationByteLimit(size_t);
107 static size_t GetSingleAllocationByteLimit(); 124 static size_t GetSingleAllocationByteLimit();
108 125
109 static void PurgeAll(); 126 static void PurgeAll();
110 127
(...skipping 21 matching lines...) Expand all
132 149
133 /** 150 /**
134 * Construct the cache, allocating memory with malloc, and respect the 151 * Construct the cache, allocating memory with malloc, and respect the
135 * byteLimit, purging automatically when a new image is added to the cache 152 * byteLimit, purging automatically when a new image is added to the cache
136 * that pushes the total bytesUsed over the limit. Note: The limit can be 153 * that pushes the total bytesUsed over the limit. Note: The limit can be
137 * changed at runtime with setTotalByteLimit. 154 * changed at runtime with setTotalByteLimit.
138 */ 155 */
139 explicit SkResourceCache(size_t byteLimit); 156 explicit SkResourceCache(size_t byteLimit);
140 ~SkResourceCache(); 157 ~SkResourceCache();
141 158
142 const Rec* findAndLock(const Key& key); 159 /**
143 const Rec* addAndLock(Rec*); 160 * Returns true if the visitor was called on a matching Key, and the visito r 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 vi sitor returns.
164 * Its return value is interpreted to mean:
165 * true : Rec is valid
166 * false : Rec is "stale" -- the cache will purge it.
167 */
168 bool find(const Key&, VisitorProc, void* context);
144 void add(Rec*); 169 void add(Rec*);
145 void remove(Rec*);
146
147 /**
148 * Given a non-null ID ptr returned by either findAndLock or addAndLock,
149 * this releases the associated resources to be available to be purged
150 * if needed. After this, the cached bitmap should no longer be
151 * referenced by the caller.
152 */
153 void unlock(ID);
154 170
155 size_t getTotalBytesUsed() const { return fTotalBytesUsed; } 171 size_t getTotalBytesUsed() const { return fTotalBytesUsed; }
156 size_t getTotalByteLimit() const { return fTotalByteLimit; } 172 size_t getTotalByteLimit() const { return fTotalByteLimit; }
157 173
158 /** 174 /**
159 * This is respected by SkBitmapProcState::possiblyScaleImage. 175 * This is respected by SkBitmapProcState::possiblyScaleImage.
160 * 0 is no maximum at all; this is the default. 176 * 0 is no maximum at all; this is the default.
161 * setSingleAllocationByteLimit() returns the previous value. 177 * setSingleAllocationByteLimit() returns the previous value.
162 */ 178 */
163 size_t setSingleAllocationByteLimit(size_t maximumAllocationSize); 179 size_t setSingleAllocationByteLimit(size_t maximumAllocationSize);
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 size_t fTotalByteLimit; 211 size_t fTotalByteLimit;
196 size_t fSingleAllocationByteLimit; 212 size_t fSingleAllocationByteLimit;
197 int fCount; 213 int fCount;
198 214
199 void purgeAsNeeded(bool forcePurge = false); 215 void purgeAsNeeded(bool forcePurge = false);
200 216
201 // linklist management 217 // linklist management
202 void moveToHead(Rec*); 218 void moveToHead(Rec*);
203 void addToHead(Rec*); 219 void addToHead(Rec*);
204 void detach(Rec*); 220 void detach(Rec*);
221 void remove(Rec*);
205 222
206 void init(); // called by constructors 223 void init(); // called by constructors
207 224
208 #ifdef SK_DEBUG 225 #ifdef SK_DEBUG
209 void validate() const; 226 void validate() const;
210 #else 227 #else
211 void validate() const {} 228 void validate() const {}
212 #endif 229 #endif
213 }; 230 };
214 #endif 231 #endif
OLDNEW
« no previous file with comments | « src/core/SkBitmapCache.cpp ('k') | src/core/SkResourceCache.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698