OLD | NEW |
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 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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; | 78 int32_t fLockCount; |
79 int32_t fPad; | |
80 | 79 |
81 friend class SkResourceCache; | 80 friend class SkResourceCache; |
82 }; | 81 }; |
83 | 82 |
84 typedef const Rec* ID; | 83 typedef const Rec* ID; |
85 | 84 |
86 /** | 85 /** |
87 * Returns a locked/pinned SkDiscardableMemory instance for the specified | 86 * Returns a locked/pinned SkDiscardableMemory instance for the specified |
88 * number of bytes, or NULL on failure. | 87 * number of bytes, or NULL on failure. |
89 */ | 88 */ |
90 typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes); | 89 typedef SkDiscardableMemory* (*DiscardableFactory)(size_t bytes); |
91 | 90 |
92 /* | 91 /* |
93 * The following static methods are thread-safe wrappers around a global | 92 * The following static methods are thread-safe wrappers around a global |
94 * instance of this cache. | 93 * instance of this cache. |
95 */ | 94 */ |
96 | 95 |
97 static const Rec* FindAndLock(const Key& key); | 96 static const Rec* FindAndLock(const Key& key); |
98 static const Rec* AddAndLock(Rec*); | 97 static const Rec* AddAndLock(Rec*); |
99 static void Add(Rec*); | 98 static void Add(Rec*); |
100 static void Unlock(ID); | 99 static void Unlock(ID); |
| 100 static void Remove(ID); |
101 | 101 |
102 static size_t GetTotalBytesUsed(); | 102 static size_t GetTotalBytesUsed(); |
103 static size_t GetTotalByteLimit(); | 103 static size_t GetTotalByteLimit(); |
104 static size_t SetTotalByteLimit(size_t newLimit); | 104 static size_t SetTotalByteLimit(size_t newLimit); |
105 | 105 |
106 static size_t SetSingleAllocationByteLimit(size_t); | 106 static size_t SetSingleAllocationByteLimit(size_t); |
107 static size_t GetSingleAllocationByteLimit(); | 107 static size_t GetSingleAllocationByteLimit(); |
108 | 108 |
109 /** | 109 /** |
110 * Use this allocator for bitmaps, so they can use ashmem when available. | 110 * Use this allocator for bitmaps, so they can use ashmem when available. |
(...skipping 22 matching lines...) Expand all Loading... |
133 * byteLimit, purging automatically when a new image is added to the cache | 133 * byteLimit, purging automatically when a new image is added to the cache |
134 * that pushes the total bytesUsed over the limit. Note: The limit can be | 134 * that pushes the total bytesUsed over the limit. Note: The limit can be |
135 * changed at runtime with setTotalByteLimit. | 135 * changed at runtime with setTotalByteLimit. |
136 */ | 136 */ |
137 explicit SkResourceCache(size_t byteLimit); | 137 explicit SkResourceCache(size_t byteLimit); |
138 ~SkResourceCache(); | 138 ~SkResourceCache(); |
139 | 139 |
140 const Rec* findAndLock(const Key& key); | 140 const Rec* findAndLock(const Key& key); |
141 const Rec* addAndLock(Rec*); | 141 const Rec* addAndLock(Rec*); |
142 void add(Rec*); | 142 void add(Rec*); |
| 143 void remove(Rec*); |
143 | 144 |
144 /** | 145 /** |
145 * Given a non-null ID ptr returned by either findAndLock or addAndLock, | 146 * Given a non-null ID ptr returned by either findAndLock or addAndLock, |
146 * this releases the associated resources to be available to be purged | 147 * this releases the associated resources to be available to be purged |
147 * if needed. After this, the cached bitmap should no longer be | 148 * if needed. After this, the cached bitmap should no longer be |
148 * referenced by the caller. | 149 * referenced by the caller. |
149 */ | 150 */ |
150 void unlock(ID); | 151 void unlock(ID); |
151 | 152 |
152 size_t getTotalBytesUsed() const { return fTotalBytesUsed; } | 153 size_t getTotalBytesUsed() const { return fTotalBytesUsed; } |
(...skipping 29 matching lines...) Expand all Loading... |
182 | 183 |
183 DiscardableFactory fDiscardableFactory; | 184 DiscardableFactory fDiscardableFactory; |
184 // the allocator is NULL or one that matches discardables | 185 // the allocator is NULL or one that matches discardables |
185 SkBitmap::Allocator* fAllocator; | 186 SkBitmap::Allocator* fAllocator; |
186 | 187 |
187 size_t fTotalBytesUsed; | 188 size_t fTotalBytesUsed; |
188 size_t fTotalByteLimit; | 189 size_t fTotalByteLimit; |
189 size_t fSingleAllocationByteLimit; | 190 size_t fSingleAllocationByteLimit; |
190 int fCount; | 191 int fCount; |
191 | 192 |
192 void purgeRec(Rec*); | |
193 void purgeAsNeeded(); | 193 void purgeAsNeeded(); |
194 | 194 |
195 // linklist management | 195 // linklist management |
196 void moveToHead(Rec*); | 196 void moveToHead(Rec*); |
197 void addToHead(Rec*); | 197 void addToHead(Rec*); |
198 void detach(Rec*); | 198 void detach(Rec*); |
199 | 199 |
200 void init(); // called by constructors | 200 void init(); // called by constructors |
201 | 201 |
202 #ifdef SK_DEBUG | 202 #ifdef SK_DEBUG |
203 void validate() const; | 203 void validate() const; |
204 #else | 204 #else |
205 void validate() const {} | 205 void validate() const {} |
206 #endif | 206 #endif |
207 }; | 207 }; |
208 #endif | 208 #endif |
OLD | NEW |