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 10 matching lines...) Expand all Loading... |
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 #include "platform/graphics/ImageDecodingStore.h" | 26 #include "platform/graphics/ImageDecodingStore.h" |
27 | 27 |
28 #include "platform/TraceEvent.h" | 28 #include "platform/TraceEvent.h" |
29 #include "platform/graphics/ImageFrameGenerator.h" | 29 #include "platform/graphics/ImageFrameGenerator.h" |
30 #include "wtf/Threading.h" | 30 #include "wtf/Threading.h" |
| 31 #include <memory> |
31 | 32 |
32 namespace blink { | 33 namespace blink { |
33 | 34 |
34 namespace { | 35 namespace { |
35 | 36 |
36 static const size_t defaultMaxTotalSizeOfHeapEntries = 32 * 1024 * 1024; | 37 static const size_t defaultMaxTotalSizeOfHeapEntries = 32 * 1024 * 1024; |
37 | 38 |
38 } // namespace | 39 } // namespace |
39 | 40 |
40 ImageDecodingStore::ImageDecodingStore() | 41 ImageDecodingStore::ImageDecodingStore() |
41 : m_heapLimitInBytes(defaultMaxTotalSizeOfHeapEntries) | 42 : m_heapLimitInBytes(defaultMaxTotalSizeOfHeapEntries) |
42 , m_heapMemoryUsageInBytes(0) | 43 , m_heapMemoryUsageInBytes(0) |
43 { | 44 { |
44 } | 45 } |
45 | 46 |
46 ImageDecodingStore::~ImageDecodingStore() | 47 ImageDecodingStore::~ImageDecodingStore() |
47 { | 48 { |
48 #if ENABLE(ASSERT) | 49 #if ENABLE(ASSERT) |
49 setCacheLimitInBytes(0); | 50 setCacheLimitInBytes(0); |
50 ASSERT(!m_decoderCacheMap.size()); | 51 ASSERT(!m_decoderCacheMap.size()); |
51 ASSERT(!m_orderedCacheList.size()); | 52 ASSERT(!m_orderedCacheList.size()); |
52 ASSERT(!m_decoderCacheKeyMap.size()); | 53 ASSERT(!m_decoderCacheKeyMap.size()); |
53 #endif | 54 #endif |
54 } | 55 } |
55 | 56 |
56 ImageDecodingStore& ImageDecodingStore::instance() | 57 ImageDecodingStore& ImageDecodingStore::instance() |
57 { | 58 { |
58 DEFINE_THREAD_SAFE_STATIC_LOCAL(ImageDecodingStore, store, ImageDecodingStor
e::create().leakPtr()); | 59 DEFINE_THREAD_SAFE_STATIC_LOCAL(ImageDecodingStore, store, ImageDecodingStor
e::create().release()); |
59 return store; | 60 return store; |
60 } | 61 } |
61 | 62 |
62 bool ImageDecodingStore::lockDecoder(const ImageFrameGenerator* generator, const
SkISize& scaledSize, ImageDecoder** decoder) | 63 bool ImageDecodingStore::lockDecoder(const ImageFrameGenerator* generator, const
SkISize& scaledSize, ImageDecoder** decoder) |
63 { | 64 { |
64 ASSERT(decoder); | 65 ASSERT(decoder); |
65 | 66 |
66 MutexLocker lock(m_mutex); | 67 MutexLocker lock(m_mutex); |
67 DecoderCacheMap::iterator iter = m_decoderCacheMap.find(DecoderCacheEntry::m
akeCacheKey(generator, scaledSize)); | 68 DecoderCacheMap::iterator iter = m_decoderCacheMap.find(DecoderCacheEntry::m
akeCacheKey(generator, scaledSize)); |
68 if (iter == m_decoderCacheMap.end()) | 69 if (iter == m_decoderCacheMap.end()) |
(...skipping 15 matching lines...) Expand all Loading... |
84 ASSERT_WITH_SECURITY_IMPLICATION(iter != m_decoderCacheMap.end()); | 85 ASSERT_WITH_SECURITY_IMPLICATION(iter != m_decoderCacheMap.end()); |
85 | 86 |
86 CacheEntry* cacheEntry = iter->value.get(); | 87 CacheEntry* cacheEntry = iter->value.get(); |
87 cacheEntry->decrementUseCount(); | 88 cacheEntry->decrementUseCount(); |
88 | 89 |
89 // Put the entry to the end of list. | 90 // Put the entry to the end of list. |
90 m_orderedCacheList.remove(cacheEntry); | 91 m_orderedCacheList.remove(cacheEntry); |
91 m_orderedCacheList.append(cacheEntry); | 92 m_orderedCacheList.append(cacheEntry); |
92 } | 93 } |
93 | 94 |
94 void ImageDecodingStore::insertDecoder(const ImageFrameGenerator* generator, Pas
sOwnPtr<ImageDecoder> decoder) | 95 void ImageDecodingStore::insertDecoder(const ImageFrameGenerator* generator, std
::unique_ptr<ImageDecoder> decoder) |
95 { | 96 { |
96 // Prune old cache entries to give space for the new one. | 97 // Prune old cache entries to give space for the new one. |
97 prune(); | 98 prune(); |
98 | 99 |
99 OwnPtr<DecoderCacheEntry> newCacheEntry = DecoderCacheEntry::create(generato
r, std::move(decoder)); | 100 std::unique_ptr<DecoderCacheEntry> newCacheEntry = DecoderCacheEntry::create
(generator, std::move(decoder)); |
100 | 101 |
101 MutexLocker lock(m_mutex); | 102 MutexLocker lock(m_mutex); |
102 ASSERT(!m_decoderCacheMap.contains(newCacheEntry->cacheKey())); | 103 ASSERT(!m_decoderCacheMap.contains(newCacheEntry->cacheKey())); |
103 insertCacheInternal(std::move(newCacheEntry), &m_decoderCacheMap, &m_decoder
CacheKeyMap); | 104 insertCacheInternal(std::move(newCacheEntry), &m_decoderCacheMap, &m_decoder
CacheKeyMap); |
104 } | 105 } |
105 | 106 |
106 void ImageDecodingStore::removeDecoder(const ImageFrameGenerator* generator, con
st ImageDecoder* decoder) | 107 void ImageDecodingStore::removeDecoder(const ImageFrameGenerator* generator, con
st ImageDecoder* decoder) |
107 { | 108 { |
108 Vector<OwnPtr<CacheEntry>> cacheEntriesToDelete; | 109 Vector<std::unique_ptr<CacheEntry>> cacheEntriesToDelete; |
109 { | 110 { |
110 MutexLocker lock(m_mutex); | 111 MutexLocker lock(m_mutex); |
111 DecoderCacheMap::iterator iter = m_decoderCacheMap.find(DecoderCacheEntr
y::makeCacheKey(generator, decoder)); | 112 DecoderCacheMap::iterator iter = m_decoderCacheMap.find(DecoderCacheEntr
y::makeCacheKey(generator, decoder)); |
112 ASSERT_WITH_SECURITY_IMPLICATION(iter != m_decoderCacheMap.end()); | 113 ASSERT_WITH_SECURITY_IMPLICATION(iter != m_decoderCacheMap.end()); |
113 | 114 |
114 CacheEntry* cacheEntry = iter->value.get(); | 115 CacheEntry* cacheEntry = iter->value.get(); |
115 ASSERT(cacheEntry->useCount()); | 116 ASSERT(cacheEntry->useCount()); |
116 cacheEntry->decrementUseCount(); | 117 cacheEntry->decrementUseCount(); |
117 | 118 |
118 // Delete only one decoder cache entry. Ownership of the cache entry | 119 // Delete only one decoder cache entry. Ownership of the cache entry |
119 // is transfered to cacheEntriesToDelete such that object can be deleted | 120 // is transfered to cacheEntriesToDelete such that object can be deleted |
120 // outside of the lock. | 121 // outside of the lock. |
121 removeFromCacheInternal(cacheEntry, &cacheEntriesToDelete); | 122 removeFromCacheInternal(cacheEntry, &cacheEntriesToDelete); |
122 | 123 |
123 // Remove from LRU list. | 124 // Remove from LRU list. |
124 removeFromCacheListInternal(cacheEntriesToDelete); | 125 removeFromCacheListInternal(cacheEntriesToDelete); |
125 } | 126 } |
126 } | 127 } |
127 | 128 |
128 void ImageDecodingStore::removeCacheIndexedByGenerator(const ImageFrameGenerator
* generator) | 129 void ImageDecodingStore::removeCacheIndexedByGenerator(const ImageFrameGenerator
* generator) |
129 { | 130 { |
130 Vector<OwnPtr<CacheEntry>> cacheEntriesToDelete; | 131 Vector<std::unique_ptr<CacheEntry>> cacheEntriesToDelete; |
131 { | 132 { |
132 MutexLocker lock(m_mutex); | 133 MutexLocker lock(m_mutex); |
133 | 134 |
134 // Remove image cache objects and decoder cache objects associated | 135 // Remove image cache objects and decoder cache objects associated |
135 // with a ImageFrameGenerator. | 136 // with a ImageFrameGenerator. |
136 removeCacheIndexedByGeneratorInternal(&m_decoderCacheMap, &m_decoderCach
eKeyMap, generator, &cacheEntriesToDelete); | 137 removeCacheIndexedByGeneratorInternal(&m_decoderCacheMap, &m_decoderCach
eKeyMap, generator, &cacheEntriesToDelete); |
137 | 138 |
138 // Remove from LRU list as well. | 139 // Remove from LRU list as well. |
139 removeFromCacheListInternal(cacheEntriesToDelete); | 140 removeFromCacheListInternal(cacheEntriesToDelete); |
140 } | 141 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 int ImageDecodingStore::cacheEntries() | 176 int ImageDecodingStore::cacheEntries() |
176 { | 177 { |
177 MutexLocker lock(m_mutex); | 178 MutexLocker lock(m_mutex); |
178 return m_decoderCacheMap.size(); | 179 return m_decoderCacheMap.size(); |
179 } | 180 } |
180 | 181 |
181 void ImageDecodingStore::prune() | 182 void ImageDecodingStore::prune() |
182 { | 183 { |
183 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("blink.image_decoding"), "ImageDecodi
ngStore::prune"); | 184 TRACE_EVENT0(TRACE_DISABLED_BY_DEFAULT("blink.image_decoding"), "ImageDecodi
ngStore::prune"); |
184 | 185 |
185 Vector<OwnPtr<CacheEntry>> cacheEntriesToDelete; | 186 Vector<std::unique_ptr<CacheEntry>> cacheEntriesToDelete; |
186 { | 187 { |
187 MutexLocker lock(m_mutex); | 188 MutexLocker lock(m_mutex); |
188 | 189 |
189 // Head of the list is the least recently used entry. | 190 // Head of the list is the least recently used entry. |
190 const CacheEntry* cacheEntry = m_orderedCacheList.head(); | 191 const CacheEntry* cacheEntry = m_orderedCacheList.head(); |
191 | 192 |
192 // Walk the list of cache entries starting from the least recently used | 193 // Walk the list of cache entries starting from the least recently used |
193 // and then keep them for deletion later. | 194 // and then keep them for deletion later. |
194 while (cacheEntry) { | 195 while (cacheEntry) { |
195 const bool isPruneNeeded = m_heapMemoryUsageInBytes > m_heapLimitInB
ytes || !m_heapLimitInBytes; | 196 const bool isPruneNeeded = m_heapMemoryUsageInBytes > m_heapLimitInB
ytes || !m_heapLimitInBytes; |
196 if (!isPruneNeeded) | 197 if (!isPruneNeeded) |
197 break; | 198 break; |
198 | 199 |
199 // Cache is not used; Remove it. | 200 // Cache is not used; Remove it. |
200 if (!cacheEntry->useCount()) | 201 if (!cacheEntry->useCount()) |
201 removeFromCacheInternal(cacheEntry, &cacheEntriesToDelete); | 202 removeFromCacheInternal(cacheEntry, &cacheEntriesToDelete); |
202 cacheEntry = cacheEntry->next(); | 203 cacheEntry = cacheEntry->next(); |
203 } | 204 } |
204 | 205 |
205 // Remove from cache list as well. | 206 // Remove from cache list as well. |
206 removeFromCacheListInternal(cacheEntriesToDelete); | 207 removeFromCacheListInternal(cacheEntriesToDelete); |
207 } | 208 } |
208 } | 209 } |
209 | 210 |
210 template<class T, class U, class V> | 211 template<class T, class U, class V> |
211 void ImageDecodingStore::insertCacheInternal(PassOwnPtr<T> cacheEntry, U* cacheM
ap, V* identifierMap) | 212 void ImageDecodingStore::insertCacheInternal(std::unique_ptr<T> cacheEntry, U* c
acheMap, V* identifierMap) |
212 { | 213 { |
213 const size_t cacheEntryBytes = cacheEntry->memoryUsageInBytes(); | 214 const size_t cacheEntryBytes = cacheEntry->memoryUsageInBytes(); |
214 m_heapMemoryUsageInBytes += cacheEntryBytes; | 215 m_heapMemoryUsageInBytes += cacheEntryBytes; |
215 | 216 |
216 // m_orderedCacheList is used to support LRU operations to reorder cache | 217 // m_orderedCacheList is used to support LRU operations to reorder cache |
217 // entries quickly. | 218 // entries quickly. |
218 m_orderedCacheList.append(cacheEntry.get()); | 219 m_orderedCacheList.append(cacheEntry.get()); |
219 | 220 |
220 typename U::KeyType key = cacheEntry->cacheKey(); | 221 typename U::KeyType key = cacheEntry->cacheKey(); |
221 typename V::AddResult result = identifierMap->add(cacheEntry->generator(), t
ypename V::MappedType()); | 222 typename V::AddResult result = identifierMap->add(cacheEntry->generator(), t
ypename V::MappedType()); |
222 result.storedValue->value.add(key); | 223 result.storedValue->value.add(key); |
223 cacheMap->add(key, std::move(cacheEntry)); | 224 cacheMap->add(key, std::move(cacheEntry)); |
224 | 225 |
225 TRACE_COUNTER1(TRACE_DISABLED_BY_DEFAULT("blink.image_decoding"), "ImageDeco
dingStoreHeapMemoryUsageBytes", m_heapMemoryUsageInBytes); | 226 TRACE_COUNTER1(TRACE_DISABLED_BY_DEFAULT("blink.image_decoding"), "ImageDeco
dingStoreHeapMemoryUsageBytes", m_heapMemoryUsageInBytes); |
226 TRACE_COUNTER1(TRACE_DISABLED_BY_DEFAULT("blink.image_decoding"), "ImageDeco
dingStoreNumOfDecoders", m_decoderCacheMap.size()); | 227 TRACE_COUNTER1(TRACE_DISABLED_BY_DEFAULT("blink.image_decoding"), "ImageDeco
dingStoreNumOfDecoders", m_decoderCacheMap.size()); |
227 } | 228 } |
228 | 229 |
229 template<class T, class U, class V> | 230 template<class T, class U, class V> |
230 void ImageDecodingStore::removeFromCacheInternal(const T* cacheEntry, U* cacheMa
p, V* identifierMap, Vector<OwnPtr<CacheEntry>>* deletionList) | 231 void ImageDecodingStore::removeFromCacheInternal(const T* cacheEntry, U* cacheMa
p, V* identifierMap, Vector<std::unique_ptr<CacheEntry>>* deletionList) |
231 { | 232 { |
232 const size_t cacheEntryBytes = cacheEntry->memoryUsageInBytes(); | 233 const size_t cacheEntryBytes = cacheEntry->memoryUsageInBytes(); |
233 ASSERT(m_heapMemoryUsageInBytes >= cacheEntryBytes); | 234 ASSERT(m_heapMemoryUsageInBytes >= cacheEntryBytes); |
234 m_heapMemoryUsageInBytes -= cacheEntryBytes; | 235 m_heapMemoryUsageInBytes -= cacheEntryBytes; |
235 | 236 |
236 // Remove entry from identifier map. | 237 // Remove entry from identifier map. |
237 typename V::iterator iter = identifierMap->find(cacheEntry->generator()); | 238 typename V::iterator iter = identifierMap->find(cacheEntry->generator()); |
238 ASSERT(iter != identifierMap->end()); | 239 ASSERT(iter != identifierMap->end()); |
239 iter->value.remove(cacheEntry->cacheKey()); | 240 iter->value.remove(cacheEntry->cacheKey()); |
240 if (!iter->value.size()) | 241 if (!iter->value.size()) |
241 identifierMap->remove(iter); | 242 identifierMap->remove(iter); |
242 | 243 |
243 // Remove entry from cache map. | 244 // Remove entry from cache map. |
244 deletionList->append(cacheMap->take(cacheEntry->cacheKey())); | 245 deletionList->append(cacheMap->take(cacheEntry->cacheKey())); |
245 | 246 |
246 TRACE_COUNTER1(TRACE_DISABLED_BY_DEFAULT("blink.image_decoding"), "ImageDeco
dingStoreHeapMemoryUsageBytes", m_heapMemoryUsageInBytes); | 247 TRACE_COUNTER1(TRACE_DISABLED_BY_DEFAULT("blink.image_decoding"), "ImageDeco
dingStoreHeapMemoryUsageBytes", m_heapMemoryUsageInBytes); |
247 TRACE_COUNTER1(TRACE_DISABLED_BY_DEFAULT("blink.image_decoding"), "ImageDeco
dingStoreNumOfDecoders", m_decoderCacheMap.size()); | 248 TRACE_COUNTER1(TRACE_DISABLED_BY_DEFAULT("blink.image_decoding"), "ImageDeco
dingStoreNumOfDecoders", m_decoderCacheMap.size()); |
248 } | 249 } |
249 | 250 |
250 void ImageDecodingStore::removeFromCacheInternal(const CacheEntry* cacheEntry, V
ector<OwnPtr<CacheEntry>>* deletionList) | 251 void ImageDecodingStore::removeFromCacheInternal(const CacheEntry* cacheEntry, V
ector<std::unique_ptr<CacheEntry>>* deletionList) |
251 { | 252 { |
252 if (cacheEntry->type() == CacheEntry::TypeDecoder) { | 253 if (cacheEntry->type() == CacheEntry::TypeDecoder) { |
253 removeFromCacheInternal(static_cast<const DecoderCacheEntry*>(cacheEntry
), &m_decoderCacheMap, &m_decoderCacheKeyMap, deletionList); | 254 removeFromCacheInternal(static_cast<const DecoderCacheEntry*>(cacheEntry
), &m_decoderCacheMap, &m_decoderCacheKeyMap, deletionList); |
254 } else { | 255 } else { |
255 ASSERT(false); | 256 ASSERT(false); |
256 } | 257 } |
257 } | 258 } |
258 | 259 |
259 template<class U, class V> | 260 template<class U, class V> |
260 void ImageDecodingStore::removeCacheIndexedByGeneratorInternal(U* cacheMap, V* i
dentifierMap, const ImageFrameGenerator* generator, Vector<OwnPtr<CacheEntry>>*
deletionList) | 261 void ImageDecodingStore::removeCacheIndexedByGeneratorInternal(U* cacheMap, V* i
dentifierMap, const ImageFrameGenerator* generator, Vector<std::unique_ptr<Cache
Entry>>* deletionList) |
261 { | 262 { |
262 typename V::iterator iter = identifierMap->find(generator); | 263 typename V::iterator iter = identifierMap->find(generator); |
263 if (iter == identifierMap->end()) | 264 if (iter == identifierMap->end()) |
264 return; | 265 return; |
265 | 266 |
266 // Get all cache identifiers associated with generator. | 267 // Get all cache identifiers associated with generator. |
267 Vector<typename U::KeyType> cacheIdentifierList; | 268 Vector<typename U::KeyType> cacheIdentifierList; |
268 copyToVector(iter->value, cacheIdentifierList); | 269 copyToVector(iter->value, cacheIdentifierList); |
269 | 270 |
270 // For each cache identifier find the corresponding CacheEntry and remove it
. | 271 // For each cache identifier find the corresponding CacheEntry and remove it
. |
271 for (size_t i = 0; i < cacheIdentifierList.size(); ++i) { | 272 for (size_t i = 0; i < cacheIdentifierList.size(); ++i) { |
272 ASSERT(cacheMap->contains(cacheIdentifierList[i])); | 273 ASSERT(cacheMap->contains(cacheIdentifierList[i])); |
273 const typename U::MappedType::PtrType cacheEntry = cacheMap->get(cacheId
entifierList[i]); | 274 const auto& cacheEntry = cacheMap->get(cacheIdentifierList[i]); |
274 ASSERT(!cacheEntry->useCount()); | 275 ASSERT(!cacheEntry->useCount()); |
275 removeFromCacheInternal(cacheEntry, cacheMap, identifierMap, deletionLis
t); | 276 removeFromCacheInternal(cacheEntry, cacheMap, identifierMap, deletionLis
t); |
276 } | 277 } |
277 } | 278 } |
278 | 279 |
279 void ImageDecodingStore::removeFromCacheListInternal(const Vector<OwnPtr<CacheEn
try>>& deletionList) | 280 void ImageDecodingStore::removeFromCacheListInternal(const Vector<std::unique_pt
r<CacheEntry>>& deletionList) |
280 { | 281 { |
281 for (size_t i = 0; i < deletionList.size(); ++i) | 282 for (size_t i = 0; i < deletionList.size(); ++i) |
282 m_orderedCacheList.remove(deletionList[i].get()); | 283 m_orderedCacheList.remove(deletionList[i].get()); |
283 } | 284 } |
284 | 285 |
285 } // namespace blink | 286 } // namespace blink |
OLD | NEW |