| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CC_TILES_GPU_IMAGE_DECODE_CACHE_H_ | 5 #ifndef CC_TILES_GPU_IMAGE_DECODE_CACHE_H_ |
| 6 #define CC_TILES_GPU_IMAGE_DECODE_CACHE_H_ | 6 #define CC_TILES_GPU_IMAGE_DECODE_CACHE_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <unordered_map> | 9 #include <unordered_map> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 // | 96 // |
| 97 class CC_EXPORT GpuImageDecodeCache | 97 class CC_EXPORT GpuImageDecodeCache |
| 98 : public ImageDecodeCache, | 98 : public ImageDecodeCache, |
| 99 public base::trace_event::MemoryDumpProvider, | 99 public base::trace_event::MemoryDumpProvider, |
| 100 public base::MemoryCoordinatorClient { | 100 public base::MemoryCoordinatorClient { |
| 101 public: | 101 public: |
| 102 enum class DecodeTaskType { PART_OF_UPLOAD_TASK, STAND_ALONE_DECODE_TASK }; | 102 enum class DecodeTaskType { PART_OF_UPLOAD_TASK, STAND_ALONE_DECODE_TASK }; |
| 103 | 103 |
| 104 explicit GpuImageDecodeCache(ContextProvider* context, | 104 explicit GpuImageDecodeCache(ContextProvider* context, |
| 105 ResourceFormat decode_format, | 105 ResourceFormat decode_format, |
| 106 size_t max_gpu_image_bytes); | 106 size_t max_working_set_bytes, |
| 107 size_t max_cache_bytes); |
| 107 ~GpuImageDecodeCache() override; | 108 ~GpuImageDecodeCache() override; |
| 108 | 109 |
| 109 // ImageDecodeCache overrides. | 110 // ImageDecodeCache overrides. |
| 110 | 111 |
| 111 // Finds the existing uploaded image for the provided DrawImage. Creates an | 112 // Finds the existing uploaded image for the provided DrawImage. Creates an |
| 112 // upload task to upload the image if an exsiting image does not exist. | 113 // upload task to upload the image if an exsiting image does not exist. |
| 113 bool GetTaskForImageAndRef(const DrawImage& image, | 114 bool GetTaskForImageAndRef(const DrawImage& image, |
| 114 const TracingInfo& tracing_info, | 115 const TracingInfo& tracing_info, |
| 115 scoped_refptr<TileTask>* task) override; | 116 scoped_refptr<TileTask>* task) override; |
| 116 bool GetOutOfRasterDecodeTaskForImageAndRef( | 117 bool GetOutOfRasterDecodeTaskForImageAndRef( |
| (...skipping 18 matching lines...) Expand all Loading... |
| 135 // Called by Decode / Upload tasks. | 136 // Called by Decode / Upload tasks. |
| 136 void DecodeImage(const DrawImage& image); | 137 void DecodeImage(const DrawImage& image); |
| 137 void UploadImage(const DrawImage& image); | 138 void UploadImage(const DrawImage& image); |
| 138 | 139 |
| 139 // Called by Decode / Upload tasks when tasks are finished. | 140 // Called by Decode / Upload tasks when tasks are finished. |
| 140 void OnImageDecodeTaskCompleted(const DrawImage& image, | 141 void OnImageDecodeTaskCompleted(const DrawImage& image, |
| 141 DecodeTaskType task_type); | 142 DecodeTaskType task_type); |
| 142 void OnImageUploadTaskCompleted(const DrawImage& image); | 143 void OnImageUploadTaskCompleted(const DrawImage& image); |
| 143 | 144 |
| 144 // For testing only. | 145 // For testing only. |
| 145 void SetCachedBytesLimitForTesting(size_t limit) { | 146 void SetAllByteLimitsForTesting(size_t limit) { |
| 146 cached_bytes_limit_ = limit; | 147 cached_bytes_limit_ = limit; |
| 148 max_working_set_bytes_ = limit; |
| 147 } | 149 } |
| 148 size_t GetBytesUsedForTesting() const { return bytes_used_; } | 150 size_t GetBytesUsedForTesting() const { return bytes_used_; } |
| 149 size_t GetNumCacheEntriesForTesting() const { | 151 size_t GetNumCacheEntriesForTesting() const { |
| 150 return persistent_cache_.size(); | 152 return persistent_cache_.size(); |
| 151 } | 153 } |
| 152 size_t GetDrawImageSizeForTesting(const DrawImage& image); | 154 size_t GetDrawImageSizeForTesting(const DrawImage& image); |
| 153 void SetImageDecodingFailedForTesting(const DrawImage& image); | 155 void SetImageDecodingFailedForTesting(const DrawImage& image); |
| 154 bool DiscardableIsLockedForTesting(const DrawImage& image); | 156 bool DiscardableIsLockedForTesting(const DrawImage& image); |
| 155 bool IsInInUseCacheForTesting(const DrawImage& image) const; | 157 bool IsInInUseCacheForTesting(const DrawImage& image) const; |
| 156 | 158 |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 285 | 287 |
| 286 void RefImageDecode(const DrawImage& draw_image); | 288 void RefImageDecode(const DrawImage& draw_image); |
| 287 void UnrefImageDecode(const DrawImage& draw_image); | 289 void UnrefImageDecode(const DrawImage& draw_image); |
| 288 void RefImage(const DrawImage& draw_image); | 290 void RefImage(const DrawImage& draw_image); |
| 289 void UnrefImageInternal(const DrawImage& draw_image); | 291 void UnrefImageInternal(const DrawImage& draw_image); |
| 290 | 292 |
| 291 // Called any time the ownership of an object changed. This includes changes | 293 // Called any time the ownership of an object changed. This includes changes |
| 292 // to ref-count or to orphaned status. | 294 // to ref-count or to orphaned status. |
| 293 void OwnershipChanged(const DrawImage& draw_image, ImageData* image_data); | 295 void OwnershipChanged(const DrawImage& draw_image, ImageData* image_data); |
| 294 | 296 |
| 295 // Ensures that the cache can hold an element of |required_size|, freeing | 297 // Ensures that the working set can hold an element of |required_size|, |
| 296 // unreferenced cache entries if necessary to make room. | 298 // freeing unreferenced cache entries to make room. |
| 297 bool EnsureCapacity(size_t required_size); | 299 bool EnsureCapacity(size_t required_size); |
| 298 bool CanFitSize(size_t size) const; | 300 bool CanFitInWorkingSet(size_t size) const; |
| 301 bool CanFitInCache(size_t size) const; |
| 299 bool ExceedsPreferredCount() const; | 302 bool ExceedsPreferredCount() const; |
| 300 | 303 |
| 301 void DecodeImageIfNecessary(const DrawImage& draw_image, | 304 void DecodeImageIfNecessary(const DrawImage& draw_image, |
| 302 ImageData* image_data); | 305 ImageData* image_data); |
| 303 | 306 |
| 304 scoped_refptr<GpuImageDecodeCache::ImageData> CreateImageData( | 307 scoped_refptr<GpuImageDecodeCache::ImageData> CreateImageData( |
| 305 const DrawImage& image); | 308 const DrawImage& image); |
| 306 SkImageInfo CreateImageInfoForDrawImage(const DrawImage& draw_image, | 309 SkImageInfo CreateImageInfoForDrawImage(const DrawImage& draw_image, |
| 307 int upload_scale_mip_level) const; | 310 int upload_scale_mip_level) const; |
| 308 | 311 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 330 // |persistent_cache_| represents the long-lived cache, keeping a certain | 333 // |persistent_cache_| represents the long-lived cache, keeping a certain |
| 331 // budget of ImageDatas alive even when their ref count reaches zero. | 334 // budget of ImageDatas alive even when their ref count reaches zero. |
| 332 using PersistentCache = base::MRUCache<uint32_t, scoped_refptr<ImageData>>; | 335 using PersistentCache = base::MRUCache<uint32_t, scoped_refptr<ImageData>>; |
| 333 PersistentCache persistent_cache_; | 336 PersistentCache persistent_cache_; |
| 334 | 337 |
| 335 // |in_use_cache_| represents the in-use (short-lived) cache. Entries are | 338 // |in_use_cache_| represents the in-use (short-lived) cache. Entries are |
| 336 // cleaned up as soon as their ref count reaches zero. | 339 // cleaned up as soon as their ref count reaches zero. |
| 337 using InUseCache = std::unordered_map<InUseCacheKey, InUseCacheEntry>; | 340 using InUseCache = std::unordered_map<InUseCacheKey, InUseCacheEntry>; |
| 338 InUseCache in_use_cache_; | 341 InUseCache in_use_cache_; |
| 339 | 342 |
| 340 const size_t normal_max_gpu_image_bytes_; | 343 size_t max_working_set_bytes_; |
| 341 size_t cached_bytes_limit_ = normal_max_gpu_image_bytes_; | 344 const size_t normal_max_cache_bytes_; |
| 345 size_t cached_bytes_limit_ = normal_max_cache_bytes_; |
| 342 size_t bytes_used_ = 0; | 346 size_t bytes_used_ = 0; |
| 343 base::MemoryState memory_state_ = base::MemoryState::NORMAL; | 347 base::MemoryState memory_state_ = base::MemoryState::NORMAL; |
| 344 | 348 |
| 345 // We can't release GPU backed SkImages without holding the context lock, | 349 // We can't release GPU backed SkImages without holding the context lock, |
| 346 // so we add them to this list and defer deletion until the next time the lock | 350 // so we add them to this list and defer deletion until the next time the lock |
| 347 // is held. | 351 // is held. |
| 348 std::vector<sk_sp<SkImage>> images_pending_deletion_; | 352 std::vector<sk_sp<SkImage>> images_pending_deletion_; |
| 349 }; | 353 }; |
| 350 | 354 |
| 351 } // namespace cc | 355 } // namespace cc |
| 352 | 356 |
| 353 #endif // CC_TILES_GPU_IMAGE_DECODE_CACHE_H_ | 357 #endif // CC_TILES_GPU_IMAGE_DECODE_CACHE_H_ |
| OLD | NEW |