| 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_CONTROLLER_H_ | 5 #ifndef CC_TILES_GPU_IMAGE_DECODE_CONTROLLER_H_ |
| 6 #define CC_TILES_GPU_IMAGE_DECODE_CONTROLLER_H_ | 6 #define CC_TILES_GPU_IMAGE_DECODE_CONTROLLER_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <unordered_map> | 9 #include <unordered_map> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/containers/mru_cache.h" | 12 #include "base/containers/mru_cache.h" |
| 13 #include "base/memory/discardable_memory.h" | 13 #include "base/memory/discardable_memory.h" |
| 14 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 15 #include "base/trace_event/memory_dump_provider.h" | 15 #include "base/trace_event/memory_dump_provider.h" |
| 16 #include "cc/base/cc_export.h" | 16 #include "cc/base/cc_export.h" |
| 17 #include "cc/resources/resource_format.h" | 17 #include "cc/resources/resource_format.h" |
| 18 #include "cc/tiles/image_decode_controller.h" | 18 #include "cc/tiles/image_decode_controller.h" |
| 19 #include "third_party/skia/include/core/SkRefCnt.h" | 19 #include "third_party/skia/include/core/SkRefCnt.h" |
| 20 | 20 |
| 21 class SkImageTextureData; | 21 class SkImageTextureData; |
| 22 | 22 |
| 23 namespace cc { | 23 namespace cc { |
| 24 | 24 |
| 25 class ContextProvider; | 25 class ContextProvider; |
| 26 | 26 |
| 27 // OVERVIEW: |
| 28 // |
| 27 // GpuImageDecodeController handles the decode and upload of images that will | 29 // GpuImageDecodeController handles the decode and upload of images that will |
| 28 // be used by Skia's GPU raster path. It also maintains a cache of these | 30 // be used by Skia's GPU raster path. It also maintains a cache of these |
| 29 // decoded/uploaded images for later re-use. | 31 // decoded/uploaded images for later re-use. |
| 30 // | 32 // |
| 31 // Generally, when an image is required for raster, GpuImageDecodeController | 33 // Generally, when an image is required for raster, GpuImageDecodeController |
| 32 // creates two tasks, one to decode the image, and one to upload the image to | 34 // creates two tasks, one to decode the image, and one to upload the image to |
| 33 // the GPU. These tasks are completed before the raster task which depends on | 35 // the GPU. These tasks are completed before the raster task which depends on |
| 34 // the image. We need to seperate decode and upload tasks, as decode can occur | 36 // the image. We need to seperate decode and upload tasks, as decode can occur |
| 35 // simultaneously on multiple threads, while upload requires the GL context | 37 // simultaneously on multiple threads, while upload requires the GL context |
| 36 // lock must happen on our non-concurrent raster thread. | 38 // lock must happen on our non-concurrent raster thread. |
| 37 // | 39 // |
| 38 // Decoded and Uploaded image data share a single cache entry. Depending on how | 40 // Decoded and Uploaded image data share a single cache entry. Depending on how |
| 39 // far we've progressed, this cache entry may contain CPU-side decoded data, | 41 // far we've progressed, this cache entry may contain CPU-side decoded data, |
| 40 // GPU-side uploaded data, or both. Because CPU-side decoded data is stored in | 42 // GPU-side uploaded data, or both. Because CPU-side decoded data is stored in |
| 41 // discardable memory, and is only locked for short periods of time (until the | 43 // discardable memory, and is only locked for short periods of time (until the |
| 42 // upload completes), this memory is not counted against our sized cache | 44 // upload completes), this memory is not counted against our sized cache |
| 43 // limits. Uploaded GPU memory, being non-discardable, always counts against | 45 // limits. Uploaded GPU memory, being non-discardable, always counts against |
| 44 // our limits. | 46 // our limits. |
| 45 // | 47 // |
| 46 // In cases where the number of images needed exceeds our cache limits, we | 48 // In cases where the number of images needed exceeds our cache limits, we |
| 47 // operate in an "at-raster" mode. In this mode, there are no decode/upload | 49 // operate in an "at-raster" mode. In this mode, there are no decode/upload |
| 48 // tasks, and images are decoded/uploaded as needed, immediately before being | 50 // tasks, and images are decoded/uploaded as needed, immediately before being |
| 49 // used in raster. Cache entries for at-raster tasks are marked as such, which | 51 // used in raster. Cache entries for at-raster tasks are marked as such, which |
| 50 // prevents future tasks from taking a dependency on them and extending their | 52 // prevents future tasks from taking a dependency on them and extending their |
| 51 // lifetime longer than is necessary. | 53 // lifetime longer than is necessary. |
| 54 // |
| 55 // RASTER-SCALE CACHING: |
| 56 // |
| 57 // In order to save memory, images which are going to be scaled may be uploaded |
| 58 // at lower than original resolution. In these cases, we may later need to |
| 59 // re-upload the image at a higher resolution. To handle multiple images of |
| 60 // different scales being in use at the same time, we have a two-part caching |
| 61 // system. |
| 62 // |
| 63 // The first cache, |persistent_cache_|, stores one ImageData per image id. |
| 64 // These ImageDatas are not necessarily associated with a given DrawImage, and |
| 65 // are saved (persisted) even when their ref-count reaches zero (assuming they |
| 66 // fit in the current memory budget). This allows for future re-use of image |
| 67 // resources. |
| 68 // |
| 69 // The second cache, |in_use_cache_|, stores one image data per DrawImage - |
| 70 // this may be the same ImageData that is in the persistent_cache_. These |
| 71 // cache entries are more transient and are deleted as soon as all refs to the |
| 72 // given DrawImage are released (the image is no longer in-use). |
| 73 // |
| 74 // For examples of raster-scale caching, see https://goo.gl/0zCd9Z |
| 75 // |
| 76 // REF COUNTING: |
| 77 // |
| 78 // In dealing with the two caches in GpuImageDecodeController, there are three |
| 79 // ref-counting concepts in use: |
| 80 // 1) ImageData upload/decode ref-counts. |
| 81 // These ref-counts represent the overall number of references to the |
| 82 // upload or decode portion of an ImageData. These ref-counts control |
| 83 // both whether the upload/decode data can be freed, as well as whether an |
| 84 // ImageData can be removed from the |persistent_cache_|. ImageDatas are |
| 85 // only removed from the |persistent_cache_| if their upload/decode |
| 86 // ref-counts are zero or if they are orphaned and replaced by a new entry. |
| 87 // 2) InUseCacheEntry ref-counts. |
| 88 // These ref-counts represent the number of references to an |
| 89 // InUseCacheEntry from a specific DrawImage. When the InUseCacheEntry's |
| 90 // ref-count reaches 0 it will be deleted. |
| 91 // 3) scoped_refptr ref-counts. |
| 92 // Because both the persistent_cache_ and the in_use_cache_ point at the |
| 93 // same ImageDatas (and may need to keep these ImageDatas alive independent |
| 94 // of each other), they hold ImageDatas by scoped_refptr. The scoped_refptr |
| 95 // keeps an ImageData alive while it is present in either the |
| 96 // |persistent_cache_| or |in_use_cache_|. |
| 97 // |
| 52 class CC_EXPORT GpuImageDecodeController | 98 class CC_EXPORT GpuImageDecodeController |
| 53 : public ImageDecodeController, | 99 : public ImageDecodeController, |
| 54 public base::trace_event::MemoryDumpProvider { | 100 public base::trace_event::MemoryDumpProvider { |
| 55 public: | 101 public: |
| 56 explicit GpuImageDecodeController(ContextProvider* context, | 102 explicit GpuImageDecodeController(ContextProvider* context, |
| 57 ResourceFormat decode_format, | 103 ResourceFormat decode_format, |
| 58 size_t max_gpu_image_bytes); | 104 size_t max_gpu_image_bytes); |
| 59 ~GpuImageDecodeController() override; | 105 ~GpuImageDecodeController() override; |
| 60 | 106 |
| 61 // ImageDecodeController overrides. | 107 // ImageDecodeController overrides. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 86 void OnImageUploadTaskCompleted(const DrawImage& image); | 132 void OnImageUploadTaskCompleted(const DrawImage& image); |
| 87 | 133 |
| 88 // For testing only. | 134 // For testing only. |
| 89 void SetCachedItemLimitForTesting(size_t limit) { | 135 void SetCachedItemLimitForTesting(size_t limit) { |
| 90 cached_items_limit_ = limit; | 136 cached_items_limit_ = limit; |
| 91 } | 137 } |
| 92 void SetCachedBytesLimitForTesting(size_t limit) { | 138 void SetCachedBytesLimitForTesting(size_t limit) { |
| 93 cached_bytes_limit_ = limit; | 139 cached_bytes_limit_ = limit; |
| 94 } | 140 } |
| 95 size_t GetBytesUsedForTesting() const { return bytes_used_; } | 141 size_t GetBytesUsedForTesting() const { return bytes_used_; } |
| 142 size_t GetDrawImageSizeForTesting(const DrawImage& image); |
| 96 void SetImageDecodingFailedForTesting(const DrawImage& image); | 143 void SetImageDecodingFailedForTesting(const DrawImage& image); |
| 97 bool DiscardableIsLockedForTesting(const DrawImage& image); | 144 bool DiscardableIsLockedForTesting(const DrawImage& image); |
| 98 | 145 |
| 99 private: | 146 private: |
| 100 enum class DecodedDataMode { GPU, CPU }; | 147 enum class DecodedDataMode { GPU, CPU }; |
| 101 | 148 |
| 102 // Stores the CPU-side decoded bits of an image and supporting fields. | 149 // Stores the CPU-side decoded bits of an image and supporting fields. |
| 103 struct DecodedImageData { | 150 struct DecodedImageData { |
| 104 DecodedImageData(); | 151 DecodedImageData(); |
| 105 ~DecodedImageData(); | 152 ~DecodedImageData(); |
| 106 | 153 |
| 107 bool is_locked() const { return is_locked_; } | 154 bool is_locked() const { return is_locked_; } |
| 108 bool Lock(); | 155 bool Lock(); |
| 109 void Unlock(); | 156 void Unlock(); |
| 110 void SetLockedData(std::unique_ptr<base::DiscardableMemory> data); | 157 void SetLockedData(std::unique_ptr<base::DiscardableMemory> data); |
| 111 void ResetData(); | 158 void ResetData(); |
| 112 base::DiscardableMemory* data() const { return data_.get(); } | 159 base::DiscardableMemory* data() const { return data_.get(); } |
| 113 | |
| 114 void mark_used() { usage_stats_.used = true; } | 160 void mark_used() { usage_stats_.used = true; } |
| 115 | 161 |
| 116 // May be null if image not yet decoded. | |
| 117 uint32_t ref_count = 0; | 162 uint32_t ref_count = 0; |
| 118 // Set to true if the image was corrupt and could not be decoded. | 163 // Set to true if the image was corrupt and could not be decoded. |
| 119 bool decode_failure = false; | 164 bool decode_failure = false; |
| 165 // If non-null, this is the pending decode task for this image. |
| 166 scoped_refptr<TileTask> task; |
| 120 | 167 |
| 121 private: | 168 private: |
| 122 struct UsageStats { | 169 struct UsageStats { |
| 123 int lock_count = 1; | 170 int lock_count = 1; |
| 124 bool used = false; | 171 bool used = false; |
| 125 bool first_lock_wasted = false; | 172 bool first_lock_wasted = false; |
| 126 }; | 173 }; |
| 127 | 174 |
| 128 void ReportUsageStats() const; | 175 void ReportUsageStats() const; |
| 129 | 176 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 142 | 189 |
| 143 void mark_used() { usage_stats_.used = true; } | 190 void mark_used() { usage_stats_.used = true; } |
| 144 void notify_ref_reached_zero() { | 191 void notify_ref_reached_zero() { |
| 145 if (++usage_stats_.ref_reached_zero_count == 1) | 192 if (++usage_stats_.ref_reached_zero_count == 1) |
| 146 usage_stats_.first_ref_wasted = !usage_stats_.used; | 193 usage_stats_.first_ref_wasted = !usage_stats_.used; |
| 147 } | 194 } |
| 148 | 195 |
| 149 // True if the image is counting against our memory limits. | 196 // True if the image is counting against our memory limits. |
| 150 bool budgeted = false; | 197 bool budgeted = false; |
| 151 uint32_t ref_count = 0; | 198 uint32_t ref_count = 0; |
| 199 // If non-null, this is the pending upload task for this image. |
| 200 scoped_refptr<TileTask> task; |
| 152 | 201 |
| 153 private: | 202 private: |
| 154 struct UsageStats { | 203 struct UsageStats { |
| 155 bool used = false; | 204 bool used = false; |
| 156 bool first_ref_wasted = false; | 205 bool first_ref_wasted = false; |
| 157 int ref_reached_zero_count = 0; | 206 int ref_reached_zero_count = 0; |
| 158 }; | 207 }; |
| 159 | 208 |
| 160 void ReportUsageStats() const; | 209 void ReportUsageStats() const; |
| 161 | 210 |
| 162 // May be null if image not yet uploaded / prepared. | 211 // May be null if image not yet uploaded / prepared. |
| 163 sk_sp<SkImage> image_; | 212 sk_sp<SkImage> image_; |
| 164 UsageStats usage_stats_; | 213 UsageStats usage_stats_; |
| 165 }; | 214 }; |
| 166 | 215 |
| 167 struct ImageData { | 216 struct ImageData : public base::RefCounted<ImageData> { |
| 168 ImageData(DecodedDataMode mode, size_t size); | 217 ImageData(DecodedDataMode mode, |
| 169 ~ImageData(); | 218 size_t size, |
| 219 int upload_scale_mip_level, |
| 220 SkFilterQuality upload_scale_filter_quality); |
| 170 | 221 |
| 171 const DecodedDataMode mode; | 222 const DecodedDataMode mode; |
| 172 const size_t size; | 223 const size_t size; |
| 173 bool is_at_raster = false; | 224 bool is_at_raster = false; |
| 174 | 225 |
| 226 // Variables used to identify/track multiple scale levels of a single image. |
| 227 int upload_scale_mip_level = 0; |
| 228 SkFilterQuality upload_scale_filter_quality = kNone_SkFilterQuality; |
| 229 // If true, this image is no longer in our |persistent_cache_| and will be |
| 230 // deleted as soon as its ref count reaches zero. |
| 231 bool is_orphaned = false; |
| 232 |
| 175 DecodedImageData decode; | 233 DecodedImageData decode; |
| 176 UploadedImageData upload; | 234 UploadedImageData upload; |
| 235 |
| 236 private: |
| 237 friend class base::RefCounted<ImageData>; |
| 238 ~ImageData(); |
| 177 }; | 239 }; |
| 178 | 240 |
| 179 using ImageDataMRUCache = | 241 // A ref-count and ImageData, used to associate the ImageData with a specific |
| 180 base::MRUCache<uint32_t, std::unique_ptr<ImageData>>; | 242 // DrawImage in the |in_use_cache_|. |
| 243 struct InUseCacheEntry { |
| 244 explicit InUseCacheEntry(scoped_refptr<ImageData> image_data); |
| 245 InUseCacheEntry(const InUseCacheEntry& other); |
| 246 InUseCacheEntry(InUseCacheEntry&& other); |
| 247 ~InUseCacheEntry(); |
| 248 |
| 249 uint32_t ref_count = 0; |
| 250 scoped_refptr<ImageData> image_data; |
| 251 }; |
| 252 |
| 253 // Uniquely identifies (without collisions) a specific DrawImage for use in |
| 254 // the |in_use_cache_|. |
| 255 using InUseCacheKey = uint64_t; |
| 181 | 256 |
| 182 // All private functions should only be called while holding |lock_|. Some | 257 // All private functions should only be called while holding |lock_|. Some |
| 183 // functions also require the |context_| lock. These are indicated by | 258 // functions also require the |context_| lock. These are indicated by |
| 184 // additional comments. | 259 // additional comments. |
| 185 | 260 |
| 186 // Similar to GetTaskForImageAndRef, but gets the dependent decode task | 261 // Similar to GetTaskForImageAndRef, but gets the dependent decode task |
| 187 // rather than the upload task, if necessary. | 262 // rather than the upload task, if necessary. |
| 188 scoped_refptr<TileTask> GetImageDecodeTaskAndRef( | 263 scoped_refptr<TileTask> GetImageDecodeTaskAndRef( |
| 189 const DrawImage& image, | 264 const DrawImage& image, |
| 190 const TracingInfo& tracing_info); | 265 const TracingInfo& tracing_info); |
| 191 | 266 |
| 192 void RefImageDecode(const DrawImage& draw_image); | 267 void RefImageDecode(const DrawImage& draw_image); |
| 193 void UnrefImageDecode(const DrawImage& draw_image); | 268 void UnrefImageDecode(const DrawImage& draw_image); |
| 194 void RefImage(const DrawImage& draw_image); | 269 void RefImage(const DrawImage& draw_image); |
| 195 void UnrefImageInternal(const DrawImage& draw_image); | 270 void UnrefImageInternal(const DrawImage& draw_image); |
| 196 void RefCountChanged(ImageData* image_data); | 271 |
| 272 // Called any time the ownership of an object changed. This includes changes |
| 273 // to ref-count or to orphaned status. |
| 274 void OwnershipChanged(ImageData* image_data); |
| 197 | 275 |
| 198 // Ensures that the cache can hold an element of |required_size|, freeing | 276 // Ensures that the cache can hold an element of |required_size|, freeing |
| 199 // unreferenced cache entries if necessary to make room. | 277 // unreferenced cache entries if necessary to make room. |
| 200 bool EnsureCapacity(size_t required_size); | 278 bool EnsureCapacity(size_t required_size); |
| 201 bool CanFitSize(size_t size) const; | 279 bool CanFitSize(size_t size) const; |
| 202 bool ExceedsPreferredCount() const; | 280 bool ExceedsPreferredCount() const; |
| 203 | 281 |
| 204 void DecodeImageIfNecessary(const DrawImage& draw_image, | 282 void DecodeImageIfNecessary(const DrawImage& draw_image, |
| 205 ImageData* image_data); | 283 ImageData* image_data); |
| 206 | 284 |
| 207 std::unique_ptr<GpuImageDecodeController::ImageData> CreateImageData( | 285 scoped_refptr<GpuImageDecodeController::ImageData> CreateImageData( |
| 208 const DrawImage& image); | 286 const DrawImage& image); |
| 209 SkImageInfo CreateImageInfoForDrawImage(const DrawImage& draw_image) const; | 287 SkImageInfo CreateImageInfoForDrawImage(const DrawImage& draw_image, |
| 288 int upload_scale_mip_level) const; |
| 289 |
| 290 // Finds the ImageData that should be used for the given DrawImage. Looks |
| 291 // first in the |in_use_cache_|, and then in the |persistent_cache_|. |
| 292 ImageData* GetImageDataForDrawImage(const DrawImage& image); |
| 293 |
| 294 // Returns true if the given ImageData can be used to draw the specified |
| 295 // DrawImage. |
| 296 bool IsCompatible(const ImageData* image_data, |
| 297 const DrawImage& draw_image) const; |
| 210 | 298 |
| 211 // The following two functions also require the |context_| lock to be held. | 299 // The following two functions also require the |context_| lock to be held. |
| 212 void UploadImageIfNecessary(const DrawImage& draw_image, | 300 void UploadImageIfNecessary(const DrawImage& draw_image, |
| 213 ImageData* image_data); | 301 ImageData* image_data); |
| 214 void DeletePendingImages(); | 302 void DeletePendingImages(); |
| 215 | 303 |
| 216 const ResourceFormat format_; | 304 const ResourceFormat format_; |
| 217 ContextProvider* context_; | 305 ContextProvider* context_; |
| 218 sk_sp<GrContextThreadSafeProxy> context_threadsafe_proxy_; | 306 sk_sp<GrContextThreadSafeProxy> context_threadsafe_proxy_; |
| 219 | 307 |
| 220 // All members below this point must only be accessed while holding |lock_|. | 308 // All members below this point must only be accessed while holding |lock_|. |
| 221 base::Lock lock_; | 309 base::Lock lock_; |
| 222 | 310 |
| 223 std::unordered_map<uint32_t, scoped_refptr<TileTask>> | 311 // |persistent_cache_| represents the long-lived cache, keeping a certain |
| 224 pending_image_upload_tasks_; | 312 // budget of ImageDatas alive even when their ref count reaches zero. |
| 225 std::unordered_map<uint32_t, scoped_refptr<TileTask>> | 313 using PersistentCache = base::MRUCache<uint32_t, scoped_refptr<ImageData>>; |
| 226 pending_image_decode_tasks_; | 314 PersistentCache persistent_cache_; |
| 227 | 315 |
| 228 ImageDataMRUCache image_data_; | 316 // |in_use_cache_| represents the in-use (short-lived) cache. Entries are |
| 317 // cleaned up as soon as their ref count reaches zero. |
| 318 using InUseCache = std::unordered_map<InUseCacheKey, InUseCacheEntry>; |
| 319 InUseCache in_use_cache_; |
| 229 | 320 |
| 230 size_t cached_items_limit_; | 321 size_t cached_items_limit_; |
| 231 size_t cached_bytes_limit_; | 322 size_t cached_bytes_limit_; |
| 232 size_t bytes_used_; | 323 size_t bytes_used_; |
| 233 const size_t max_gpu_image_bytes_; | 324 const size_t max_gpu_image_bytes_; |
| 234 | 325 |
| 235 // We can't release GPU backed SkImages without holding the context lock, | 326 // We can't release GPU backed SkImages without holding the context lock, |
| 236 // so we add them to this list and defer deletion until the next time the lock | 327 // so we add them to this list and defer deletion until the next time the lock |
| 237 // is held. | 328 // is held. |
| 238 std::vector<sk_sp<SkImage>> images_pending_deletion_; | 329 std::vector<sk_sp<SkImage>> images_pending_deletion_; |
| 239 }; | 330 }; |
| 240 | 331 |
| 241 } // namespace cc | 332 } // namespace cc |
| 242 | 333 |
| 243 #endif // CC_TILES_GPU_IMAGE_DECODE_CONTROLLER_H_ | 334 #endif // CC_TILES_GPU_IMAGE_DECODE_CONTROLLER_H_ |
| OLD | NEW |