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 SkScaledImageCache_DEFINED | 8 #ifndef SkScaledImageCache_DEFINED |
9 #define SkScaledImageCache_DEFINED | 9 #define SkScaledImageCache_DEFINED |
10 | 10 |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 int32_t width, | 53 int32_t width, |
54 int32_t height, | 54 int32_t height, |
55 const SkBitmap& bitmap); | 55 const SkBitmap& bitmap); |
56 | 56 |
57 static ID* AddAndLock(const SkBitmap& original, SkScalar scaleX, | 57 static ID* AddAndLock(const SkBitmap& original, SkScalar scaleX, |
58 SkScalar scaleY, const SkBitmap& bitmap); | 58 SkScalar scaleY, const SkBitmap& bitmap); |
59 static ID* AddAndLockMip(const SkBitmap& original, const SkMipMap* mipMap); | 59 static ID* AddAndLockMip(const SkBitmap& original, const SkMipMap* mipMap); |
60 | 60 |
61 static void Unlock(ID*); | 61 static void Unlock(ID*); |
62 | 62 |
63 static size_t GetBytesUsed(); | 63 static size_t GetTotalBytesUsed(); |
64 static size_t GetByteLimit(); | 64 static size_t GetTotalByteLimit(); |
65 static size_t SetByteLimit(size_t newLimit); | 65 static size_t SetTotalByteLimit(size_t newLimit); |
| 66 |
| 67 static size_t SetSingleAllocationByteLimit(size_t); |
| 68 static size_t GetSingleAllocationByteLimit(); |
66 | 69 |
67 static SkBitmap::Allocator* GetAllocator(); | 70 static SkBitmap::Allocator* GetAllocator(); |
68 | 71 |
69 /** | 72 /** |
70 * Call SkDebugf() with diagnostic information about the state of the cache | 73 * Call SkDebugf() with diagnostic information about the state of the cache |
71 */ | 74 */ |
72 static void Dump(); | 75 static void Dump(); |
73 | 76 |
74 /////////////////////////////////////////////////////////////////////////// | 77 /////////////////////////////////////////////////////////////////////////// |
75 | 78 |
76 /** | 79 /** |
77 * Construct the cache to call DiscardableFactory when it | 80 * Construct the cache to call DiscardableFactory when it |
78 * allocates memory for the pixels. In this mode, the cache has | 81 * allocates memory for the pixels. In this mode, the cache has |
79 * not explicit budget, and so methods like getBytesUsed() and | 82 * not explicit budget, and so methods like getTotalBytesUsed() |
80 * getByteLimit() will return 0, and setByteLimit will ignore its argument | 83 * and getTotalByteLimit() will return 0, and setTotalByteLimit |
81 * and return 0. | 84 * will ignore its argument and return 0. |
82 */ | 85 */ |
83 SkScaledImageCache(DiscardableFactory); | 86 SkScaledImageCache(DiscardableFactory); |
84 | 87 |
85 /** | 88 /** |
86 * Construct the cache, allocating memory with malloc, and respect the | 89 * Construct the cache, allocating memory with malloc, and respect the |
87 * byteLimit, purging automatically when a new image is added to the cache | 90 * byteLimit, purging automatically when a new image is added to the cache |
88 * that pushes the total bytesUsed over the limit. Note: The limit can be | 91 * that pushes the total bytesUsed over the limit. Note: The limit can be |
89 * changed at runtime with setByteLimit. | 92 * changed at runtime with setTotalByteLimit. |
90 */ | 93 */ |
91 SkScaledImageCache(size_t byteLimit); | 94 SkScaledImageCache(size_t byteLimit); |
92 | 95 |
93 ~SkScaledImageCache(); | 96 ~SkScaledImageCache(); |
94 | 97 |
95 /** | 98 /** |
96 * Search the cache for a matching bitmap (using generationID, | 99 * Search the cache for a matching bitmap (using generationID, |
97 * width, and height as a search key). If found, return it in | 100 * width, and height as a search key). If found, return it in |
98 * returnedBitmap, and return its ID pointer. Use the returned | 101 * returnedBitmap, and return its ID pointer. Use the returned |
99 * ptr to unlock the cache when you are done using | 102 * ptr to unlock the cache when you are done using |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 ID* addAndLockMip(const SkBitmap& original, const SkMipMap* mipMap); | 140 ID* addAndLockMip(const SkBitmap& original, const SkMipMap* mipMap); |
138 | 141 |
139 /** | 142 /** |
140 * Given a non-null ID ptr returned by either findAndLock or addAndLock, | 143 * Given a non-null ID ptr returned by either findAndLock or addAndLock, |
141 * this releases the associated resources to be available to be purged | 144 * this releases the associated resources to be available to be purged |
142 * if needed. After this, the cached bitmap should no longer be | 145 * if needed. After this, the cached bitmap should no longer be |
143 * referenced by the caller. | 146 * referenced by the caller. |
144 */ | 147 */ |
145 void unlock(ID*); | 148 void unlock(ID*); |
146 | 149 |
147 size_t getBytesUsed() const { return fBytesUsed; } | 150 size_t getTotalBytesUsed() const { return fTotalBytesUsed; } |
148 size_t getByteLimit() const { return fByteLimit; } | 151 size_t getTotalByteLimit() const { return fTotalByteLimit; } |
149 | 152 |
150 /** | 153 /** |
| 154 * This is respected by SkBitmapProcState::possiblyScaleImage. |
| 155 * 0 is no maximum at all; this is the default. |
| 156 * setSingleAllocationByteLimit() returns the previous value. |
| 157 */ |
| 158 size_t setSingleAllocationByteLimit(size_t maximumAllocationSize); |
| 159 size_t getSingleAllocationByteLimit() const; |
| 160 /** |
151 * Set the maximum number of bytes available to this cache. If the current | 161 * Set the maximum number of bytes available to this cache. If the current |
152 * cache exceeds this new value, it will be purged to try to fit within | 162 * cache exceeds this new value, it will be purged to try to fit within |
153 * this new limit. | 163 * this new limit. |
154 */ | 164 */ |
155 size_t setByteLimit(size_t newLimit); | 165 size_t setTotalByteLimit(size_t newLimit); |
156 | 166 |
157 SkBitmap::Allocator* allocator() const { return fAllocator; }; | 167 SkBitmap::Allocator* allocator() const { return fAllocator; }; |
158 | 168 |
159 /** | 169 /** |
160 * Call SkDebugf() with diagnostic information about the state of the cache | 170 * Call SkDebugf() with diagnostic information about the state of the cache |
161 */ | 171 */ |
162 void dump() const; | 172 void dump() const; |
163 | 173 |
164 public: | 174 public: |
165 struct Rec; | 175 struct Rec; |
166 struct Key; | 176 struct Key; |
167 private: | 177 private: |
168 Rec* fHead; | 178 Rec* fHead; |
169 Rec* fTail; | 179 Rec* fTail; |
170 | 180 |
171 class Hash; | 181 class Hash; |
172 Hash* fHash; | 182 Hash* fHash; |
173 | 183 |
174 DiscardableFactory fDiscardableFactory; | 184 DiscardableFactory fDiscardableFactory; |
175 // the allocator is NULL or one that matches discardables | 185 // the allocator is NULL or one that matches discardables |
176 SkBitmap::Allocator* fAllocator; | 186 SkBitmap::Allocator* fAllocator; |
177 | 187 |
178 size_t fBytesUsed; | 188 size_t fTotalBytesUsed; |
179 size_t fByteLimit; | 189 size_t fTotalByteLimit; |
| 190 size_t fSingleAllocationByteLimit; |
180 int fCount; | 191 int fCount; |
181 | 192 |
182 Rec* findAndLock(uint32_t generationID, SkScalar sx, SkScalar sy, | 193 Rec* findAndLock(uint32_t generationID, SkScalar sx, SkScalar sy, |
183 const SkIRect& bounds); | 194 const SkIRect& bounds); |
184 Rec* findAndLock(const Key& key); | 195 Rec* findAndLock(const Key& key); |
185 ID* addAndLock(Rec* rec); | 196 ID* addAndLock(Rec* rec); |
186 | 197 |
187 void purgeRec(Rec*); | 198 void purgeRec(Rec*); |
188 void purgeAsNeeded(); | 199 void purgeAsNeeded(); |
189 | 200 |
190 // linklist management | 201 // linklist management |
191 void moveToHead(Rec*); | 202 void moveToHead(Rec*); |
192 void addToHead(Rec*); | 203 void addToHead(Rec*); |
193 void detach(Rec*); | 204 void detach(Rec*); |
194 | 205 |
195 void init(); // called by constructors | 206 void init(); // called by constructors |
196 | 207 |
197 #ifdef SK_DEBUG | 208 #ifdef SK_DEBUG |
198 void validate() const; | 209 void validate() const; |
199 #else | 210 #else |
200 void validate() const {} | 211 void validate() const {} |
201 #endif | 212 #endif |
202 }; | 213 }; |
203 #endif | 214 #endif |
OLD | NEW |