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