OLD | NEW |
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 13 matching lines...) Expand all Loading... |
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/skia/SkSizeHash.h" | 32 #include "platform/graphics/skia/SkSizeHash.h" |
33 #include "platform/image-decoders/ImageDecoder.h" | 33 #include "platform/image-decoders/ImageDecoder.h" |
34 | |
35 #include "wtf/DoublyLinkedList.h" | 34 #include "wtf/DoublyLinkedList.h" |
36 #include "wtf/HashSet.h" | 35 #include "wtf/HashSet.h" |
37 #include "wtf/OwnPtr.h" | 36 #include "wtf/PtrUtil.h" |
38 #include "wtf/PassOwnPtr.h" | |
39 #include "wtf/ThreadingPrimitives.h" | 37 #include "wtf/ThreadingPrimitives.h" |
40 #include "wtf/Vector.h" | 38 #include "wtf/Vector.h" |
| 39 #include <memory> |
41 | 40 |
42 namespace blink { | 41 namespace blink { |
43 | 42 |
44 class ImageFrameGenerator; | 43 class ImageFrameGenerator; |
45 | 44 |
46 // FUNCTION | 45 // FUNCTION |
47 // | 46 // |
48 // ImageDecodingStore is a class used to manage cached decoder objects. | 47 // ImageDecodingStore is a class used to manage cached decoder objects. |
49 // | 48 // |
50 // EXTERNAL OBJECTS | 49 // EXTERNAL OBJECTS |
51 // | 50 // |
52 // ImageDecoder | 51 // ImageDecoder |
53 // A decoder object. It is used to decode raw data into bitmap images. | 52 // A decoder object. It is used to decode raw data into bitmap images. |
54 // | 53 // |
55 // ImageFrameGenerator | 54 // ImageFrameGenerator |
56 // This is a direct user of this cache. Responsible for generating bitmap imag
es | 55 // This is a direct user of this cache. Responsible for generating bitmap imag
es |
57 // using an ImageDecoder. It contains encoded image data and is used to repres
ent | 56 // using an ImageDecoder. It contains encoded image data and is used to repres
ent |
58 // one image file. It is used to index image and decoder objects in the cache. | 57 // one image file. It is used to index image and decoder objects in the cache. |
59 // | 58 // |
60 // THREAD SAFETY | 59 // THREAD SAFETY |
61 // | 60 // |
62 // All public methods can be used on any thread. | 61 // All public methods can be used on any thread. |
63 | 62 |
64 class PLATFORM_EXPORT ImageDecodingStore final { | 63 class PLATFORM_EXPORT ImageDecodingStore final { |
65 USING_FAST_MALLOC(ImageDecodingStore); | 64 USING_FAST_MALLOC(ImageDecodingStore); |
66 WTF_MAKE_NONCOPYABLE(ImageDecodingStore); | 65 WTF_MAKE_NONCOPYABLE(ImageDecodingStore); |
67 public: | 66 public: |
68 static PassOwnPtr<ImageDecodingStore> create() { return adoptPtr(new ImageDe
codingStore); } | 67 static std::unique_ptr<ImageDecodingStore> create() { return wrapUnique(new
ImageDecodingStore); } |
69 ~ImageDecodingStore(); | 68 ~ImageDecodingStore(); |
70 | 69 |
71 static ImageDecodingStore& instance(); | 70 static ImageDecodingStore& instance(); |
72 | 71 |
73 // 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) |
74 // and scaled size. Return true if the cached object is found. | 73 // and scaled size. Return true if the cached object is found. |
75 bool lockDecoder(const ImageFrameGenerator*, const SkISize& scaledSize, Imag
eDecoder**); | 74 bool lockDecoder(const ImageFrameGenerator*, const SkISize& scaledSize, Imag
eDecoder**); |
76 void unlockDecoder(const ImageFrameGenerator*, const ImageDecoder*); | 75 void unlockDecoder(const ImageFrameGenerator*, const ImageDecoder*); |
77 void insertDecoder(const ImageFrameGenerator*, PassOwnPtr<ImageDecoder>); | 76 void insertDecoder(const ImageFrameGenerator*, std::unique_ptr<ImageDecoder>
); |
78 void removeDecoder(const ImageFrameGenerator*, const ImageDecoder*); | 77 void removeDecoder(const ImageFrameGenerator*, const ImageDecoder*); |
79 | 78 |
80 // Remove all cache entries indexed by ImageFrameGenerator. | 79 // Remove all cache entries indexed by ImageFrameGenerator. |
81 void removeCacheIndexedByGenerator(const ImageFrameGenerator*); | 80 void removeCacheIndexedByGenerator(const ImageFrameGenerator*); |
82 | 81 |
83 void clear(); | 82 void clear(); |
84 void setCacheLimitInBytes(size_t); | 83 void setCacheLimitInBytes(size_t); |
85 size_t memoryUsageInBytes(); | 84 size_t memoryUsageInBytes(); |
86 int cacheEntries(); | 85 int cacheEntries(); |
87 int decoderCacheEntries(); | 86 int decoderCacheEntries(); |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 const ImageFrameGenerator* m_generator; | 128 const ImageFrameGenerator* m_generator; |
130 int m_useCount; | 129 int m_useCount; |
131 | 130 |
132 private: | 131 private: |
133 CacheEntry* m_prev; | 132 CacheEntry* m_prev; |
134 CacheEntry* m_next; | 133 CacheEntry* m_next; |
135 }; | 134 }; |
136 | 135 |
137 class DecoderCacheEntry final : public CacheEntry { | 136 class DecoderCacheEntry final : public CacheEntry { |
138 public: | 137 public: |
139 static PassOwnPtr<DecoderCacheEntry> create(const ImageFrameGenerator* g
enerator, PassOwnPtr<ImageDecoder> decoder) | 138 static std::unique_ptr<DecoderCacheEntry> create(const ImageFrameGenerat
or* generator, std::unique_ptr<ImageDecoder> decoder) |
140 { | 139 { |
141 return adoptPtr(new DecoderCacheEntry(generator, 0, std::move(decode
r))); | 140 return wrapUnique(new DecoderCacheEntry(generator, 0, std::move(deco
der))); |
142 } | 141 } |
143 | 142 |
144 DecoderCacheEntry(const ImageFrameGenerator* generator, int count, PassO
wnPtr<ImageDecoder> decoder) | 143 DecoderCacheEntry(const ImageFrameGenerator* generator, int count, std::
unique_ptr<ImageDecoder> decoder) |
145 : CacheEntry(generator, count) | 144 : CacheEntry(generator, count) |
146 , m_cachedDecoder(std::move(decoder)) | 145 , m_cachedDecoder(std::move(decoder)) |
147 , m_size(SkISize::Make(m_cachedDecoder->decodedSize().width(), m_cac
hedDecoder->decodedSize().height())) | 146 , m_size(SkISize::Make(m_cachedDecoder->decodedSize().width(), m_cac
hedDecoder->decodedSize().height())) |
148 { | 147 { |
149 } | 148 } |
150 | 149 |
151 size_t memoryUsageInBytes() const override { return m_size.width() * m_s
ize.height() * 4; } | 150 size_t memoryUsageInBytes() const override { return m_size.width() * m_s
ize.height() * 4; } |
152 CacheType type() const override { return TypeDecoder; } | 151 CacheType type() const override { return TypeDecoder; } |
153 | 152 |
154 static DecoderCacheKey makeCacheKey(const ImageFrameGenerator* generator
, const SkISize& size) | 153 static DecoderCacheKey makeCacheKey(const ImageFrameGenerator* generator
, const SkISize& size) |
155 { | 154 { |
156 return std::make_pair(generator, size); | 155 return std::make_pair(generator, size); |
157 } | 156 } |
158 static DecoderCacheKey makeCacheKey(const ImageFrameGenerator* generator
, const ImageDecoder* decoder) | 157 static DecoderCacheKey makeCacheKey(const ImageFrameGenerator* generator
, const ImageDecoder* decoder) |
159 { | 158 { |
160 return std::make_pair(generator, SkISize::Make(decoder->decodedSize(
).width(), decoder->decodedSize().height())); | 159 return std::make_pair(generator, SkISize::Make(decoder->decodedSize(
).width(), decoder->decodedSize().height())); |
161 } | 160 } |
162 DecoderCacheKey cacheKey() const { return makeCacheKey(m_generator, m_si
ze); } | 161 DecoderCacheKey cacheKey() const { return makeCacheKey(m_generator, m_si
ze); } |
163 ImageDecoder* cachedDecoder() const { return m_cachedDecoder.get(); } | 162 ImageDecoder* cachedDecoder() const { return m_cachedDecoder.get(); } |
164 | 163 |
165 private: | 164 private: |
166 OwnPtr<ImageDecoder> m_cachedDecoder; | 165 std::unique_ptr<ImageDecoder> m_cachedDecoder; |
167 SkISize m_size; | 166 SkISize m_size; |
168 }; | 167 }; |
169 | 168 |
170 ImageDecodingStore(); | 169 ImageDecodingStore(); |
171 | 170 |
172 void prune(); | 171 void prune(); |
173 | 172 |
174 // These helper methods are called while m_mutex is locked. | 173 // These helper methods are called while m_mutex is locked. |
175 template<class T, class U, class V> void insertCacheInternal(PassOwnPtr<T> c
acheEntry, U* cacheMap, V* identifierMap); | 174 template<class T, class U, class V> void insertCacheInternal(std::unique_ptr
<T> cacheEntry, U* cacheMap, V* identifierMap); |
176 | 175 |
177 // Helper method to remove a cache entry. Ownership is transferred to | 176 // Helper method to remove a cache entry. Ownership is transferred to |
178 // deletionList. Use of Vector<> is handy when removing multiple entries. | 177 // deletionList. Use of Vector<> is handy when removing multiple entries. |
179 template<class T, class U, class V> void removeFromCacheInternal(const T* ca
cheEntry, U* cacheMap, V* identifierMap, Vector<OwnPtr<CacheEntry>>* deletionLis
t); | 178 template<class T, class U, class V> void removeFromCacheInternal(const T* ca
cheEntry, U* cacheMap, V* identifierMap, Vector<std::unique_ptr<CacheEntry>>* de
letionList); |
180 | 179 |
181 // Helper method to remove a cache entry. Uses the templated version base on | 180 // Helper method to remove a cache entry. Uses the templated version base on |
182 // the type of cache entry. | 181 // the type of cache entry. |
183 void removeFromCacheInternal(const CacheEntry*, Vector<OwnPtr<CacheEntry>>*
deletionList); | 182 void removeFromCacheInternal(const CacheEntry*, Vector<std::unique_ptr<Cache
Entry>>* deletionList); |
184 | 183 |
185 // Helper method to remove all cache entries associated with a ImageFraneGen
erator. | 184 // Helper method to remove all cache entries associated with a ImageFraneGen
erator. |
186 // Ownership of cache entries is transferred to deletionList. | 185 // Ownership of cache entries is transferred to deletionList. |
187 template<class U, class V> void removeCacheIndexedByGeneratorInternal(U* cac
heMap, V* identifierMap, const ImageFrameGenerator*, Vector<OwnPtr<CacheEntry>>*
deletionList); | 186 template<class U, class V> void removeCacheIndexedByGeneratorInternal(U* cac
heMap, V* identifierMap, const ImageFrameGenerator*, Vector<std::unique_ptr<Cach
eEntry>>* deletionList); |
188 | 187 |
189 // Helper method to remove cache entry pointers from the LRU list. | 188 // Helper method to remove cache entry pointers from the LRU list. |
190 void removeFromCacheListInternal(const Vector<OwnPtr<CacheEntry>>& deletionL
ist); | 189 void removeFromCacheListInternal(const Vector<std::unique_ptr<CacheEntry>>&
deletionList); |
191 | 190 |
192 // A doubly linked list that maintains usage history of cache entries. | 191 // A doubly linked list that maintains usage history of cache entries. |
193 // This is used for eviction of old entries. | 192 // This is used for eviction of old entries. |
194 // Head of this list is the least recently used cache entry. | 193 // Head of this list is the least recently used cache entry. |
195 // Tail of this list is the most recently used cache entry. | 194 // Tail of this list is the most recently used cache entry. |
196 DoublyLinkedList<CacheEntry> m_orderedCacheList; | 195 DoublyLinkedList<CacheEntry> m_orderedCacheList; |
197 | 196 |
198 // A lookup table for all decoder cache objects. Owns all decoder cache obje
cts. | 197 // A lookup table for all decoder cache objects. Owns all decoder cache obje
cts. |
199 typedef HashMap<DecoderCacheKey, OwnPtr<DecoderCacheEntry>> DecoderCacheMap; | 198 typedef HashMap<DecoderCacheKey, std::unique_ptr<DecoderCacheEntry>> Decoder
CacheMap; |
200 DecoderCacheMap m_decoderCacheMap; | 199 DecoderCacheMap m_decoderCacheMap; |
201 | 200 |
202 // A lookup table to map ImageFrameGenerator to all associated | 201 // A lookup table to map ImageFrameGenerator to all associated |
203 // decoder cache keys. | 202 // decoder cache keys. |
204 typedef HashSet<DecoderCacheKey> DecoderCacheKeySet; | 203 typedef HashSet<DecoderCacheKey> DecoderCacheKeySet; |
205 typedef HashMap<const ImageFrameGenerator*, DecoderCacheKeySet> DecoderCache
KeyMap; | 204 typedef HashMap<const ImageFrameGenerator*, DecoderCacheKeySet> DecoderCache
KeyMap; |
206 DecoderCacheKeyMap m_decoderCacheKeyMap; | 205 DecoderCacheKeyMap m_decoderCacheKeyMap; |
207 | 206 |
208 size_t m_heapLimitInBytes; | 207 size_t m_heapLimitInBytes; |
209 size_t m_heapMemoryUsageInBytes; | 208 size_t m_heapMemoryUsageInBytes; |
210 | 209 |
211 // Protect concurrent access to these members: | 210 // Protect concurrent access to these members: |
212 // m_orderedCacheList | 211 // m_orderedCacheList |
213 // m_decoderCacheMap and all CacheEntrys stored in it | 212 // m_decoderCacheMap and all CacheEntrys stored in it |
214 // m_decoderCacheKeyMap | 213 // m_decoderCacheKeyMap |
215 // m_heapLimitInBytes | 214 // m_heapLimitInBytes |
216 // m_heapMemoryUsageInBytes | 215 // m_heapMemoryUsageInBytes |
217 // This mutex also protects calls to underlying skBitmap's | 216 // This mutex also protects calls to underlying skBitmap's |
218 // lockPixels()/unlockPixels() as they are not threadsafe. | 217 // lockPixels()/unlockPixels() as they are not threadsafe. |
219 Mutex m_mutex; | 218 Mutex m_mutex; |
220 }; | 219 }; |
221 | 220 |
222 } // namespace blink | 221 } // namespace blink |
223 | 222 |
224 #endif | 223 #endif |
OLD | NEW |