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

Side by Side Diff: Source/platform/graphics/ImageDecodingStore.h

Issue 485833005: Image decoding: Remove deprecated image caching code (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 4 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 | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2012 Google Inc. All rights reserved. 2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 11 matching lines...) Expand all
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */ 24 */
25 25
26 #ifndef ImageDecodingStore_h 26 #ifndef ImageDecodingStore_h
27 #define ImageDecodingStore_h 27 #define ImageDecodingStore_h
28 28
29 #include "SkSize.h" 29 #include "SkSize.h"
30 #include "SkTypes.h" 30 #include "SkTypes.h"
31 #include "platform/PlatformExport.h" 31 #include "platform/PlatformExport.h"
32 #include "platform/graphics/DiscardablePixelRef.h"
33 #include "platform/graphics/ScaledImageFragment.h"
34 #include "platform/graphics/skia/SkSizeHash.h" 32 #include "platform/graphics/skia/SkSizeHash.h"
35 #include "platform/image-decoders/ImageDecoder.h" 33 #include "platform/image-decoders/ImageDecoder.h"
36 34
37 #include "wtf/DoublyLinkedList.h" 35 #include "wtf/DoublyLinkedList.h"
38 #include "wtf/HashSet.h" 36 #include "wtf/HashSet.h"
39 #include "wtf/OwnPtr.h" 37 #include "wtf/OwnPtr.h"
40 #include "wtf/PassOwnPtr.h" 38 #include "wtf/PassOwnPtr.h"
41 #include "wtf/ThreadingPrimitives.h" 39 #include "wtf/ThreadingPrimitives.h"
42 #include "wtf/Vector.h" 40 #include "wtf/Vector.h"
43 41
44 namespace blink { 42 namespace blink {
45 43
46 class ImageFrameGenerator; 44 class ImageFrameGenerator;
47 class SharedBuffer; 45 class SharedBuffer;
48 46
49 // FUNCTION 47 // FUNCTION
50 // 48 //
51 // ImageDecodingStore is a class used to manage image cache objects. There are t wo 49 // ImageDecodingStore is a class used to manage cached decoder objects.
52 // types of cache objects stored:
53 //
54 // 1. Image objects
55 // Each image object belongs to one frame decoded and/or resampled from an im age
56 // file. The image object can be complete or partial. Complete means the imag e
57 // is fully decoded and will not mutate in future decoding passes. It can hav e
58 // multiple concurrent users. A partial image object has only one user.
59 //
60 // 2. Decoder objects
61 // A decoder object contains an image decoder. This allows decoding to resume
62 // from previous states. There can be one user per one decoder object at any
63 // time.
64 // 50 //
65 // EXTERNAL OBJECTS 51 // EXTERNAL OBJECTS
66 // 52 //
67 // ScaledImageFragment
68 // A cached image object. Contains the bitmap and information about the bitmap
69 // image.
70 //
71 // ImageDecoder 53 // ImageDecoder
72 // A decoder object. It is used to decode raw data into bitmap images. 54 // A decoder object. It is used to decode raw data into bitmap images.
73 // 55 //
74 // ImageFrameGenerator 56 // ImageFrameGenerator
75 // This is a direct user of this cache. Responsible for generating bitmap imag es 57 // This is a direct user of this cache. Responsible for generating bitmap imag es
76 // using an ImageDecoder. It contains encoded image data and is used to repres ent 58 // using an ImageDecoder. It contains encoded image data and is used to repres ent
77 // one image file. It is used to index image and decoder objects in the cache. 59 // one image file. It is used to index image and decoder objects in the cache.
78 // 60 //
79 // LazyDecodingPixelRef
80 // A read only user of this cache.
81 //
82 // THREAD SAFETY 61 // THREAD SAFETY
83 // 62 //
84 // All public methods can be used on any thread. 63 // All public methods can be used on any thread.
85 64
86 class PLATFORM_EXPORT ImageDecodingStore { 65 class PLATFORM_EXPORT ImageDecodingStore {
87 public: 66 public:
88 static PassOwnPtr<ImageDecodingStore> create() { return adoptPtr(new ImageDe codingStore); } 67 static PassOwnPtr<ImageDecodingStore> create() { return adoptPtr(new ImageDe codingStore); }
89 ~ImageDecodingStore(); 68 ~ImageDecodingStore();
90 69
91 static ImageDecodingStore* instance(); 70 static ImageDecodingStore* instance();
92 71
93 // Why do we need this?
94 // ImageDecodingStore is used in two code paths:
95 // 1. Android uses this to cache both images and decoders.
96 // 2. Skia discardable memory path has its own cache. We still want cache
97 // decoders but not images because Skia will take care of it.
98 // Because of the two use cases we want to just disable image caching.
99 //
100 // FIXME: It is weird to have this behavior. We want to remove image
101 // caching from this class once the transition to Skia discardable memory
102 // is complete.
103 static void setImageCachingEnabled(bool);
104
105 // Access a complete cached image object. A complete cached image object is
106 // indexed by the origin (ImageFrameGenerator), scaled size and frame index
107 // within the image file.
108 // Return true if the cache object is found.
109 // Return false if the cache object cannot be found or it is incomplete.
110 bool lockCache(const ImageFrameGenerator*, const SkISize& scaledSize, size_t index, const ScaledImageFragment**);
111 void unlockCache(const ImageFrameGenerator*, const ScaledImageFragment*);
112 const ScaledImageFragment* insertAndLockCache(const ImageFrameGenerator*, Pa ssOwnPtr<ScaledImageFragment>);
113
114 // Access a cached decoder object. A decoder is indexed by origin (ImageFram eGenerator) 72 // Access a cached decoder object. A decoder is indexed by origin (ImageFram eGenerator)
115 // and scaled size. Return true if the cached object is found. 73 // and scaled size. Return true if the cached object is found.
116 bool lockDecoder(const ImageFrameGenerator*, const SkISize& scaledSize, Imag eDecoder**); 74 bool lockDecoder(const ImageFrameGenerator*, const SkISize& scaledSize, Imag eDecoder**);
117 void unlockDecoder(const ImageFrameGenerator*, const ImageDecoder*); 75 void unlockDecoder(const ImageFrameGenerator*, const ImageDecoder*);
118 void insertDecoder(const ImageFrameGenerator*, PassOwnPtr<ImageDecoder>, boo l isDiscardable); 76 void insertDecoder(const ImageFrameGenerator*, PassOwnPtr<ImageDecoder>);
119 void removeDecoder(const ImageFrameGenerator*, const ImageDecoder*); 77 void removeDecoder(const ImageFrameGenerator*, const ImageDecoder*);
120 78
121 // Locks the cache for safety, but does not attempt to lock the object we're checking for.
122 bool isCached(const ImageFrameGenerator*, const SkISize& scaledSize, size_t index);
123
124 // Remove all cache entries indexed by ImageFrameGenerator. 79 // Remove all cache entries indexed by ImageFrameGenerator.
125 void removeCacheIndexedByGenerator(const ImageFrameGenerator*); 80 void removeCacheIndexedByGenerator(const ImageFrameGenerator*);
126 81
127 void clear(); 82 void clear();
128 void setCacheLimitInBytes(size_t); 83 void setCacheLimitInBytes(size_t);
129 size_t memoryUsageInBytes(); 84 size_t memoryUsageInBytes();
130 int cacheEntries(); 85 int cacheEntries();
131 int imageCacheEntries();
132 int decoderCacheEntries(); 86 int decoderCacheEntries();
133 87
134 private: 88 private:
135 // Image cache entry is identified by: 89 // Decoder cache entry is identified by:
136 // 1. Pointer to ImageFrameGenerator. 90 // 1. Pointer to ImageFrameGenerator.
137 // 2. Size of the image. 91 // 2. Size of the image.
138 // 3. LocalFrame index.
139 // 4. LocalFrame generation. Increments on each progressive decode.
140 //
141 // The use of generation ID is to allow multiple versions of an image frame
142 // be stored in the cache. Each generation comes from a progressive decode.
143 //
144 // Decoder entries are identified by (1) and (2) only.
145 typedef std::pair<const ImageFrameGenerator*, SkISize> DecoderCacheKey; 92 typedef std::pair<const ImageFrameGenerator*, SkISize> DecoderCacheKey;
146 typedef std::pair<size_t, size_t> IndexAndGeneration;
147 typedef std::pair<DecoderCacheKey, IndexAndGeneration> ImageCacheKey;
148 93
149 // Base class for all cache entries. 94 // Base class for all cache entries.
150 class CacheEntry : public DoublyLinkedListNode<CacheEntry> { 95 class CacheEntry : public DoublyLinkedListNode<CacheEntry> {
151 friend class WTF::DoublyLinkedListNode<CacheEntry>; 96 friend class WTF::DoublyLinkedListNode<CacheEntry>;
152 public: 97 public:
153 enum CacheType { 98 enum CacheType {
154 TypeImage,
155 TypeDecoder, 99 TypeDecoder,
156 }; 100 };
157 101
158 CacheEntry(const ImageFrameGenerator* generator, int useCount, bool isDi scardable) 102 CacheEntry(const ImageFrameGenerator* generator, int useCount)
159 : m_generator(generator) 103 : m_generator(generator)
160 , m_useCount(useCount) 104 , m_useCount(useCount)
161 , m_isDiscardable(isDiscardable)
162 , m_prev(0) 105 , m_prev(0)
163 , m_next(0) 106 , m_next(0)
164 { 107 {
165 } 108 }
166 109
167 virtual ~CacheEntry() 110 virtual ~CacheEntry()
168 { 111 {
169 ASSERT(!m_useCount); 112 ASSERT(!m_useCount);
170 } 113 }
171 114
172 const ImageFrameGenerator* generator() const { return m_generator; } 115 const ImageFrameGenerator* generator() const { return m_generator; }
173 int useCount() const { return m_useCount; } 116 int useCount() const { return m_useCount; }
174 void incrementUseCount() { ++m_useCount; } 117 void incrementUseCount() { ++m_useCount; }
175 void decrementUseCount() { --m_useCount; ASSERT(m_useCount >= 0); } 118 void decrementUseCount() { --m_useCount; ASSERT(m_useCount >= 0); }
176 bool isDiscardable() const { return m_isDiscardable; }
177 119
178 // FIXME: getSafeSize() returns size in bytes truncated to a 32-bits int eger. 120 // FIXME: getSafeSize() returns size in bytes truncated to a 32-bits int eger.
179 // Find a way to get the size in 64-bits. 121 // Find a way to get the size in 64-bits.
180 virtual size_t memoryUsageInBytes() const = 0; 122 virtual size_t memoryUsageInBytes() const = 0;
181 virtual CacheType type() const = 0; 123 virtual CacheType type() const = 0;
182 124
183 protected: 125 protected:
184 const ImageFrameGenerator* m_generator; 126 const ImageFrameGenerator* m_generator;
185 int m_useCount; 127 int m_useCount;
186 bool m_isDiscardable;
187 128
188 private: 129 private:
189 CacheEntry* m_prev; 130 CacheEntry* m_prev;
190 CacheEntry* m_next; 131 CacheEntry* m_next;
191 }; 132 };
192 133
193 class ImageCacheEntry FINAL : public CacheEntry { 134 class DecoderCacheEntry FINAL : public CacheEntry {
194 public: 135 public:
195 static PassOwnPtr<ImageCacheEntry> createAndUse(const ImageFrameGenerato r* generator, PassOwnPtr<ScaledImageFragment> image) 136 static PassOwnPtr<DecoderCacheEntry> create(const ImageFrameGenerator* g enerator, PassOwnPtr<ImageDecoder> decoder)
196 { 137 {
197 return adoptPtr(new ImageCacheEntry(generator, 1, image)); 138 return adoptPtr(new DecoderCacheEntry(generator, 0, decoder));
198 } 139 }
199 140
200 ImageCacheEntry(const ImageFrameGenerator* generator, int count, PassOwn Ptr<ScaledImageFragment> image) 141 DecoderCacheEntry(const ImageFrameGenerator* generator, int count, PassO wnPtr<ImageDecoder> decoder)
201 : CacheEntry(generator, count, DiscardablePixelRef::isDiscardable(im age->bitmap().pixelRef())) 142 : CacheEntry(generator, count)
202 , m_cachedImage(image)
203 {
204 }
205
206 // FIXME: getSafeSize() returns size in bytes truncated to a 32-bits int eger.
207 // Find a way to get the size in 64-bits.
208 virtual size_t memoryUsageInBytes() const OVERRIDE { return cachedImage( )->bitmap().getSafeSize(); }
209 virtual CacheType type() const OVERRIDE { return TypeImage; }
210
211 static ImageCacheKey makeCacheKey(const ImageFrameGenerator* generator, const SkISize& size, size_t index, size_t generation)
212 {
213 return std::make_pair(std::make_pair(generator, size), std::make_pai r(index, generation));
214 }
215 ImageCacheKey cacheKey() const { return makeCacheKey(m_generator, m_cach edImage->scaledSize(), m_cachedImage->index(), m_cachedImage->generation()); }
216 const ScaledImageFragment* cachedImage() const { return m_cachedImage.ge t(); }
217 ScaledImageFragment* cachedImage() { return m_cachedImage.get(); }
218
219 private:
220 OwnPtr<ScaledImageFragment> m_cachedImage;
221 };
222
223 class DecoderCacheEntry FINAL : public CacheEntry {
224 public:
225 static PassOwnPtr<DecoderCacheEntry> create(const ImageFrameGenerator* g enerator, PassOwnPtr<ImageDecoder> decoder, bool isDiscardable)
226 {
227 return adoptPtr(new DecoderCacheEntry(generator, 0, decoder, isDisca rdable));
228 }
229
230 DecoderCacheEntry(const ImageFrameGenerator* generator, int count, PassO wnPtr<ImageDecoder> decoder, bool isDiscardable)
231 : CacheEntry(generator, count, isDiscardable)
232 , m_cachedDecoder(decoder) 143 , m_cachedDecoder(decoder)
233 , m_size(SkISize::Make(m_cachedDecoder->decodedSize().width(), m_cac hedDecoder->decodedSize().height())) 144 , m_size(SkISize::Make(m_cachedDecoder->decodedSize().width(), m_cac hedDecoder->decodedSize().height()))
234 { 145 {
235 } 146 }
236 147
237 virtual size_t memoryUsageInBytes() const OVERRIDE { return m_size.width () * m_size.height() * 4; } 148 virtual size_t memoryUsageInBytes() const OVERRIDE { return m_size.width () * m_size.height() * 4; }
238 virtual CacheType type() const OVERRIDE { return TypeDecoder; } 149 virtual CacheType type() const OVERRIDE { return TypeDecoder; }
239 150
240 static DecoderCacheKey makeCacheKey(const ImageFrameGenerator* generator , const SkISize& size) 151 static DecoderCacheKey makeCacheKey(const ImageFrameGenerator* generator , const SkISize& size)
241 { 152 {
242 return std::make_pair(generator, size); 153 return std::make_pair(generator, size);
243 } 154 }
244 static DecoderCacheKey makeCacheKey(const ImageFrameGenerator* generator , const ImageDecoder* decoder) 155 static DecoderCacheKey makeCacheKey(const ImageFrameGenerator* generator , const ImageDecoder* decoder)
245 { 156 {
246 return std::make_pair(generator, SkISize::Make(decoder->decodedSize( ).width(), decoder->decodedSize().height())); 157 return std::make_pair(generator, SkISize::Make(decoder->decodedSize( ).width(), decoder->decodedSize().height()));
247 } 158 }
248 DecoderCacheKey cacheKey() const { return makeCacheKey(m_generator, m_si ze); } 159 DecoderCacheKey cacheKey() const { return makeCacheKey(m_generator, m_si ze); }
249 ImageDecoder* cachedDecoder() const { return m_cachedDecoder.get(); } 160 ImageDecoder* cachedDecoder() const { return m_cachedDecoder.get(); }
250 161
251 private: 162 private:
252 OwnPtr<ImageDecoder> m_cachedDecoder; 163 OwnPtr<ImageDecoder> m_cachedDecoder;
253 SkISize m_size; 164 SkISize m_size;
254 }; 165 };
255 166
256 ImageDecodingStore(); 167 ImageDecodingStore();
257 168
258 void prune(); 169 void prune();
259 170
260 // These helper methods are called while m_mutex is locked. 171 // These helper methods are called while m_mutex is locked.
261
262 // Find and lock a cache entry, and return true on success.
263 // Memory of the cache entry can be discarded, in which case it is saved in
264 // deletionList.
265 bool lockCacheEntryInternal(ImageCacheEntry*, const ScaledImageFragment**, V ector<OwnPtr<CacheEntry> >* deletionList);
266
267 template<class T, class U, class V> void insertCacheInternal(PassOwnPtr<T> c acheEntry, U* cacheMap, V* identifierMap); 172 template<class T, class U, class V> void insertCacheInternal(PassOwnPtr<T> c acheEntry, U* cacheMap, V* identifierMap);
268 173
269 // Helper method to remove a cache entry. Ownership is transferred to 174 // Helper method to remove a cache entry. Ownership is transferred to
270 // deletionList. Use of Vector<> is handy when removing multiple entries. 175 // deletionList. Use of Vector<> is handy when removing multiple entries.
271 template<class T, class U, class V> void removeFromCacheInternal(const T* ca cheEntry, U* cacheMap, V* identifierMap, Vector<OwnPtr<CacheEntry> >* deletionLi st); 176 template<class T, class U, class V> void removeFromCacheInternal(const T* ca cheEntry, U* cacheMap, V* identifierMap, Vector<OwnPtr<CacheEntry> >* deletionLi st);
272 177
273 // Helper method to remove a cache entry. Uses the templated version base on 178 // Helper method to remove a cache entry. Uses the templated version base on
274 // the type of cache entry. 179 // the type of cache entry.
275 void removeFromCacheInternal(const CacheEntry*, Vector<OwnPtr<CacheEntry> >* deletionList); 180 void removeFromCacheInternal(const CacheEntry*, Vector<OwnPtr<CacheEntry> >* deletionList);
276 181
277 // Helper method to remove all cache entries associated with a ImageFraneGen erator. 182 // Helper method to remove all cache entries associated with a ImageFraneGen erator.
278 // Ownership of cache entries is transferred to deletionList. 183 // Ownership of cache entries is transferred to deletionList.
279 template<class U, class V> void removeCacheIndexedByGeneratorInternal(U* cac heMap, V* identifierMap, const ImageFrameGenerator*, Vector<OwnPtr<CacheEntry> > * deletionList); 184 template<class U, class V> void removeCacheIndexedByGeneratorInternal(U* cac heMap, V* identifierMap, const ImageFrameGenerator*, Vector<OwnPtr<CacheEntry> > * deletionList);
280 185
281 // Helper method to remove cache entry pointers from the LRU list. 186 // Helper method to remove cache entry pointers from the LRU list.
282 void removeFromCacheListInternal(const Vector<OwnPtr<CacheEntry> >& deletion List); 187 void removeFromCacheListInternal(const Vector<OwnPtr<CacheEntry> >& deletion List);
283 188
284 // A doubly linked list that maintains usage history of cache entries. 189 // A doubly linked list that maintains usage history of cache entries.
285 // This is used for eviction of old entries. 190 // This is used for eviction of old entries.
286 // Head of this list is the least recently used cache entry. 191 // Head of this list is the least recently used cache entry.
287 // Tail of this list is the most recently used cache entry. 192 // Tail of this list is the most recently used cache entry.
288 DoublyLinkedList<CacheEntry> m_orderedCacheList; 193 DoublyLinkedList<CacheEntry> m_orderedCacheList;
289 194
290 // A lookup table for all image cache objects. Owns all image cache objects.
291 typedef HashMap<ImageCacheKey, OwnPtr<ImageCacheEntry> > ImageCacheMap;
292 ImageCacheMap m_imageCacheMap;
293
294 // A lookup table for all decoder cache objects. Owns all decoder cache obje cts. 195 // A lookup table for all decoder cache objects. Owns all decoder cache obje cts.
295 typedef HashMap<DecoderCacheKey, OwnPtr<DecoderCacheEntry> > DecoderCacheMap ; 196 typedef HashMap<DecoderCacheKey, OwnPtr<DecoderCacheEntry> > DecoderCacheMap ;
296 DecoderCacheMap m_decoderCacheMap; 197 DecoderCacheMap m_decoderCacheMap;
297 198
298 // A lookup table to map ImageFrameGenerator to all associated image
299 // cache keys.
300 typedef HashSet<ImageCacheKey> ImageCacheKeySet;
301 typedef HashMap<const ImageFrameGenerator*, ImageCacheKeySet> ImageCacheKeyM ap;
302 ImageCacheKeyMap m_imageCacheKeyMap;
303
304 // A lookup table to map ImageFrameGenerator to all associated 199 // A lookup table to map ImageFrameGenerator to all associated
305 // decoder cache keys. 200 // decoder cache keys.
306 typedef HashSet<DecoderCacheKey> DecoderCacheKeySet; 201 typedef HashSet<DecoderCacheKey> DecoderCacheKeySet;
307 typedef HashMap<const ImageFrameGenerator*, DecoderCacheKeySet> DecoderCache KeyMap; 202 typedef HashMap<const ImageFrameGenerator*, DecoderCacheKeySet> DecoderCache KeyMap;
308 DecoderCacheKeyMap m_decoderCacheKeyMap; 203 DecoderCacheKeyMap m_decoderCacheKeyMap;
309 204
310 size_t m_heapLimitInBytes; 205 size_t m_heapLimitInBytes;
311 size_t m_heapMemoryUsageInBytes; 206 size_t m_heapMemoryUsageInBytes;
312 size_t m_discardableMemoryUsageInBytes;
313 207
314 // Protect concurrent access to these members: 208 // Protect concurrent access to these members:
315 // m_orderedCacheList 209 // m_orderedCacheList
316 // m_imageCacheMap, m_decoderCacheMap and all CacheEntrys stored in it 210 // m_decoderCacheMap and all CacheEntrys stored in it
317 // m_imageCacheKeyMap
318 // m_decoderCacheKeyMap 211 // m_decoderCacheKeyMap
319 // m_heapLimitInBytes 212 // m_heapLimitInBytes
320 // m_heapMemoryUsageInBytes 213 // m_heapMemoryUsageInBytes
321 // m_discardableMemoryUsageInBytes
322 // This mutex also protects calls to underlying skBitmap's 214 // This mutex also protects calls to underlying skBitmap's
323 // lockPixels()/unlockPixels() as they are not threadsafe. 215 // lockPixels()/unlockPixels() as they are not threadsafe.
324 Mutex m_mutex; 216 Mutex m_mutex;
325 }; 217 };
326 218
327 } // namespace blink 219 } // namespace blink
328 220
329 #endif 221 #endif
OLDNEW
« no previous file with comments | « Source/platform/graphics/DiscardablePixelRef.cpp ('k') | Source/platform/graphics/ImageDecodingStore.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698