| 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 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 // FUNCTION | 45 // FUNCTION |
| 46 // | 46 // |
| 47 // ImageDecodingStore is a class used to manage cached decoder objects. | 47 // ImageDecodingStore is a class used to manage cached decoder objects. |
| 48 // | 48 // |
| 49 // EXTERNAL OBJECTS | 49 // EXTERNAL OBJECTS |
| 50 // | 50 // |
| 51 // ImageDecoder | 51 // ImageDecoder |
| 52 // 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. |
| 53 // | 53 // |
| 54 // ImageFrameGenerator | 54 // ImageFrameGenerator |
| 55 // 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 |
| 56 // using an ImageDecoder. It contains encoded image data and is used to repres
ent | 56 // images using an ImageDecoder. It contains encoded image data and is used to |
| 57 // one image file. It is used to index image and decoder objects in the cache. | 57 // represent one image file. It is used to index image and decoder objects in |
| 58 // 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 | 67 |
| 67 public: | 68 public: |
| 68 static std::unique_ptr<ImageDecodingStore> create() { | 69 static std::unique_ptr<ImageDecodingStore> create() { |
| 69 return wrapUnique(new ImageDecodingStore); | 70 return wrapUnique(new ImageDecodingStore); |
| 70 } | 71 } |
| 71 ~ImageDecodingStore(); | 72 ~ImageDecodingStore(); |
| 72 | 73 |
| 73 static ImageDecodingStore& instance(); | 74 static ImageDecodingStore& instance(); |
| 74 | 75 |
| 75 // Access a cached decoder object. A decoder is indexed by origin (ImageFrameG
enerator) | 76 // Accesses a cached decoder object. A decoder is indexed by origin |
| 76 // and scaled size. Return true if the cached object is found. | 77 // (ImageFrameGenerator) and scaled size. Returns true if the cached object is |
| 78 // found. |
| 77 bool lockDecoder(const ImageFrameGenerator*, | 79 bool lockDecoder(const ImageFrameGenerator*, |
| 78 const SkISize& scaledSize, | 80 const SkISize& scaledSize, |
| 79 ImageDecoder**); | 81 ImageDecoder**); |
| 80 void unlockDecoder(const ImageFrameGenerator*, const ImageDecoder*); | 82 void unlockDecoder(const ImageFrameGenerator*, const ImageDecoder*); |
| 81 void insertDecoder(const ImageFrameGenerator*, std::unique_ptr<ImageDecoder>); | 83 void insertDecoder(const ImageFrameGenerator*, std::unique_ptr<ImageDecoder>); |
| 82 void removeDecoder(const ImageFrameGenerator*, const ImageDecoder*); | 84 void removeDecoder(const ImageFrameGenerator*, const ImageDecoder*); |
| 83 | 85 |
| 84 // Remove all cache entries indexed by ImageFrameGenerator. | 86 // Remove all cache entries indexed by ImageFrameGenerator. |
| 85 void removeCacheIndexedByGenerator(const ImageFrameGenerator*); | 87 void removeCacheIndexedByGenerator(const ImageFrameGenerator*); |
| 86 | 88 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 113 virtual ~CacheEntry() { ASSERT(!m_useCount); } | 115 virtual ~CacheEntry() { ASSERT(!m_useCount); } |
| 114 | 116 |
| 115 const ImageFrameGenerator* generator() const { return m_generator; } | 117 const ImageFrameGenerator* generator() const { return m_generator; } |
| 116 int useCount() const { return m_useCount; } | 118 int useCount() const { return m_useCount; } |
| 117 void incrementUseCount() { ++m_useCount; } | 119 void incrementUseCount() { ++m_useCount; } |
| 118 void decrementUseCount() { | 120 void decrementUseCount() { |
| 119 --m_useCount; | 121 --m_useCount; |
| 120 ASSERT(m_useCount >= 0); | 122 ASSERT(m_useCount >= 0); |
| 121 } | 123 } |
| 122 | 124 |
| 123 // FIXME: getSafeSize() returns size in bytes truncated to a 32-bits integer
. | 125 // FIXME: getSafeSize() returns the size in bytes truncated to a 32-bit |
| 124 // Find a way to get the size in 64-bits. | 126 // integer. Find a way to get the size in 64-bits. |
| 125 virtual size_t memoryUsageInBytes() const = 0; | 127 virtual size_t memoryUsageInBytes() const = 0; |
| 126 virtual CacheType type() const = 0; | 128 virtual CacheType type() const = 0; |
| 127 | 129 |
| 128 protected: | 130 protected: |
| 129 const ImageFrameGenerator* m_generator; | 131 const ImageFrameGenerator* m_generator; |
| 130 int m_useCount; | 132 int m_useCount; |
| 131 | 133 |
| 132 private: | 134 private: |
| 133 CacheEntry* m_prev; | 135 CacheEntry* m_prev; |
| 134 CacheEntry* m_next; | 136 CacheEntry* m_next; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 U* cacheMap, | 196 U* cacheMap, |
| 195 V* identifierMap, | 197 V* identifierMap, |
| 196 Vector<std::unique_ptr<CacheEntry>>* deletionList); | 198 Vector<std::unique_ptr<CacheEntry>>* deletionList); |
| 197 | 199 |
| 198 // Helper method to remove a cache entry. Uses the templated version base on | 200 // Helper method to remove a cache entry. Uses the templated version base on |
| 199 // the type of cache entry. | 201 // the type of cache entry. |
| 200 void removeFromCacheInternal( | 202 void removeFromCacheInternal( |
| 201 const CacheEntry*, | 203 const CacheEntry*, |
| 202 Vector<std::unique_ptr<CacheEntry>>* deletionList); | 204 Vector<std::unique_ptr<CacheEntry>>* deletionList); |
| 203 | 205 |
| 204 // Helper method to remove all cache entries associated with a ImageFraneGener
ator. | 206 // Helper method to remove all cache entries associated with an |
| 205 // Ownership of cache entries is transferred to deletionList. | 207 // ImageFrameGenerator. Ownership of the cache entries is transferred to |
| 208 // |deletionList|. |
| 206 template <class U, class V> | 209 template <class U, class V> |
| 207 void removeCacheIndexedByGeneratorInternal( | 210 void removeCacheIndexedByGeneratorInternal( |
| 208 U* cacheMap, | 211 U* cacheMap, |
| 209 V* identifierMap, | 212 V* identifierMap, |
| 210 const ImageFrameGenerator*, | 213 const ImageFrameGenerator*, |
| 211 Vector<std::unique_ptr<CacheEntry>>* deletionList); | 214 Vector<std::unique_ptr<CacheEntry>>* deletionList); |
| 212 | 215 |
| 213 // Helper method to remove cache entry pointers from the LRU list. | 216 // Helper method to remove cache entry pointers from the LRU list. |
| 214 void removeFromCacheListInternal( | 217 void removeFromCacheListInternal( |
| 215 const Vector<std::unique_ptr<CacheEntry>>& deletionList); | 218 const Vector<std::unique_ptr<CacheEntry>>& deletionList); |
| 216 | 219 |
| 217 // A doubly linked list that maintains usage history of cache entries. | 220 // A doubly linked list that maintains usage history of cache entries. |
| 218 // This is used for eviction of old entries. | 221 // This is used for eviction of old entries. |
| 219 // Head of this list is the least recently used cache entry. | 222 // Head of this list is the least recently used cache entry. |
| 220 // Tail of this list is the most recently used cache entry. | 223 // Tail of this list is the most recently used cache entry. |
| 221 DoublyLinkedList<CacheEntry> m_orderedCacheList; | 224 DoublyLinkedList<CacheEntry> m_orderedCacheList; |
| 222 | 225 |
| 223 // A lookup table for all decoder cache objects. Owns all decoder cache object
s. | 226 // A lookup table for all decoder cache objects. Owns all decoder cache |
| 227 // objects. |
| 224 typedef HashMap<DecoderCacheKey, std::unique_ptr<DecoderCacheEntry>> | 228 typedef HashMap<DecoderCacheKey, std::unique_ptr<DecoderCacheEntry>> |
| 225 DecoderCacheMap; | 229 DecoderCacheMap; |
| 226 DecoderCacheMap m_decoderCacheMap; | 230 DecoderCacheMap m_decoderCacheMap; |
| 227 | 231 |
| 228 // A lookup table to map ImageFrameGenerator to all associated | 232 // A lookup table to map ImageFrameGenerator to all associated |
| 229 // decoder cache keys. | 233 // decoder cache keys. |
| 230 typedef HashSet<DecoderCacheKey> DecoderCacheKeySet; | 234 typedef HashSet<DecoderCacheKey> DecoderCacheKeySet; |
| 231 typedef HashMap<const ImageFrameGenerator*, DecoderCacheKeySet> | 235 typedef HashMap<const ImageFrameGenerator*, DecoderCacheKeySet> |
| 232 DecoderCacheKeyMap; | 236 DecoderCacheKeyMap; |
| 233 DecoderCacheKeyMap m_decoderCacheKeyMap; | 237 DecoderCacheKeyMap m_decoderCacheKeyMap; |
| 234 | 238 |
| 235 size_t m_heapLimitInBytes; | 239 size_t m_heapLimitInBytes; |
| 236 size_t m_heapMemoryUsageInBytes; | 240 size_t m_heapMemoryUsageInBytes; |
| 237 | 241 |
| 238 // Protect concurrent access to these members: | 242 // Protect concurrent access to these members: |
| 239 // m_orderedCacheList | 243 // m_orderedCacheList |
| 240 // m_decoderCacheMap and all CacheEntrys stored in it | 244 // m_decoderCacheMap and all CacheEntrys stored in it |
| 241 // m_decoderCacheKeyMap | 245 // m_decoderCacheKeyMap |
| 242 // m_heapLimitInBytes | 246 // m_heapLimitInBytes |
| 243 // m_heapMemoryUsageInBytes | 247 // m_heapMemoryUsageInBytes |
| 244 // This mutex also protects calls to underlying skBitmap's | 248 // This mutex also protects calls to underlying skBitmap's |
| 245 // lockPixels()/unlockPixels() as they are not threadsafe. | 249 // lockPixels()/unlockPixels() as they are not threadsafe. |
| 246 Mutex m_mutex; | 250 Mutex m_mutex; |
| 247 }; | 251 }; |
| 248 | 252 |
| 249 } // namespace blink | 253 } // namespace blink |
| 250 | 254 |
| 251 #endif | 255 #endif |
| OLD | NEW |