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