| 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 <unordered_map> | 9 #include <unordered_map> |
| 9 #include <unordered_set> | 10 #include <vector> |
| 10 | 11 |
| 12 #include "base/containers/mru_cache.h" |
| 13 #include "base/memory/discardable_memory.h" |
| 11 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 15 #include "base/trace_event/memory_dump_provider.h" |
| 12 #include "cc/base/cc_export.h" | 16 #include "cc/base/cc_export.h" |
| 17 #include "cc/resources/resource_format.h" |
| 13 #include "cc/tiles/image_decode_controller.h" | 18 #include "cc/tiles/image_decode_controller.h" |
| 19 #include "skia/ext/refptr.h" |
| 20 |
| 21 class SkImageTextureData; |
| 14 | 22 |
| 15 namespace cc { | 23 namespace cc { |
| 16 | 24 |
| 25 class ContextProvider; |
| 26 |
| 27 // 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 |
| 29 // decoded/uploaded images for later re-use. |
| 30 // |
| 31 // 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 |
| 33 // 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 |
| 35 // simultaneously on multiple threads, while upload requires the GL context |
| 36 // lock must happen on our non-concurrent raster thread. |
| 37 // |
| 38 // 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, |
| 40 // 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 |
| 42 // upload completes), this memory is not counted against our sized cache |
| 43 // limits. Uploaded GPU memory, being non-discardable, always counts against |
| 44 // our limits. |
| 45 // |
| 46 // 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 |
| 48 // 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 |
| 50 // prevents future tasks from taking a dependency on them and extending their |
| 51 // lifetime longer than is necessary. |
| 17 class CC_EXPORT GpuImageDecodeController : public ImageDecodeController { | 52 class CC_EXPORT GpuImageDecodeController : public ImageDecodeController { |
| 18 public: | 53 public: |
| 19 GpuImageDecodeController(); | 54 explicit GpuImageDecodeController(ContextProvider* context, |
| 55 ResourceFormat decode_format); |
| 20 ~GpuImageDecodeController() override; | 56 ~GpuImageDecodeController() override; |
| 21 | 57 |
| 22 // ImageDecodeController overrides. | 58 // ImageDecodeController overrides. |
| 59 |
| 60 // Finds the existing uploaded image for the provided DrawImage. Creates an |
| 61 // upload task to upload the image if an exsiting image does not exist. |
| 23 bool GetTaskForImageAndRef(const DrawImage& image, | 62 bool GetTaskForImageAndRef(const DrawImage& image, |
| 24 uint64_t prepare_tiles_id, | 63 uint64_t prepare_tiles_id, |
| 25 scoped_refptr<ImageDecodeTask>* task) override; | 64 scoped_refptr<ImageDecodeTask>* task) override; |
| 26 void UnrefImage(const DrawImage& image) override; | 65 void UnrefImage(const DrawImage& image) override; |
| 27 DecodedDrawImage GetDecodedImageForDraw(const DrawImage& draw_image) override; | 66 DecodedDrawImage GetDecodedImageForDraw(const DrawImage& draw_image) override; |
| 28 void DrawWithImageFinished(const DrawImage& image, | 67 void DrawWithImageFinished(const DrawImage& image, |
| 29 const DecodedDrawImage& decoded_image) override; | 68 const DecodedDrawImage& decoded_image) override; |
| 30 void ReduceCacheUsage() override; | 69 void ReduceCacheUsage() override; |
| 70 void SetShouldAggressivelyFreeResources( |
| 71 bool aggressively_free_resources) override; |
| 31 | 72 |
| 73 // Called by Decode / Upload tasks. |
| 32 void DecodeImage(const DrawImage& image); | 74 void DecodeImage(const DrawImage& image); |
| 75 void UploadImage(const DrawImage& image); |
| 76 void DecodeTaskCompleted(const DrawImage& image); |
| 77 void UploadTaskCompleted(const DrawImage& image); |
| 33 | 78 |
| 34 void RemovePendingTaskForImage(const DrawImage& image); | 79 // For testing only. |
| 80 void SetCachedItemLimitForTesting(size_t limit) { |
| 81 cached_items_limit_ = limit; |
| 82 } |
| 83 void SetCachedBytesLimitForTesting(size_t limit) { |
| 84 cached_bytes_limit_ = limit; |
| 85 } |
| 86 size_t GetBytesUsedForTesting() const { return bytes_used_; } |
| 35 | 87 |
| 36 private: | 88 private: |
| 89 enum class DecodedDataMode { GPU, CPU }; |
| 90 |
| 91 // Stores the CPU-side decoded bits of an image and supporting fields. |
| 92 struct DecodedImageData { |
| 93 DecodedImageData(); |
| 94 ~DecodedImageData(); |
| 95 |
| 96 // May be null if image not yet decoded. |
| 97 std::unique_ptr<base::DiscardableMemory> data; |
| 98 uint32_t ref_count; |
| 99 bool is_locked; |
| 100 |
| 101 // Set to true if the image was corrupt and could not be decoded. |
| 102 bool decode_failure; |
| 103 }; |
| 104 |
| 105 // Stores the GPU-side image and supporting fields. |
| 106 struct UploadedImageData { |
| 107 UploadedImageData(); |
| 108 ~UploadedImageData(); |
| 109 |
| 110 // May be null if image not yet uploaded / prepared. |
| 111 skia::RefPtr<SkImage> image; |
| 112 // True if the image is counting against our memory limits. |
| 113 bool budgeted; |
| 114 uint32_t ref_count; |
| 115 }; |
| 116 |
| 117 struct ImageData { |
| 118 ImageData(DecodedDataMode mode, size_t size); |
| 119 ~ImageData(); |
| 120 |
| 121 const DecodedDataMode mode; |
| 122 const size_t size; |
| 123 bool is_at_raster; |
| 124 |
| 125 DecodedImageData decode; |
| 126 UploadedImageData upload; |
| 127 }; |
| 128 |
| 129 using ImageDataMRUCache = |
| 130 base::MRUCache<uint32_t, std::unique_ptr<ImageData>>; |
| 131 |
| 132 // All private functions should only be called while holding |lock_|. Some |
| 133 // functions also require the |context_| lock. These are indicated by |
| 134 // additional comments. |
| 135 |
| 136 // Similar to GetTaskForImageAndRef, but gets the dependent decode task |
| 137 // rather than the upload task, if necessary. |
| 138 scoped_refptr<ImageDecodeTask> GetImageDecodeTaskAndRef( |
| 139 const DrawImage& image, |
| 140 uint64_t prepare_tiles_id); |
| 141 |
| 142 void RefImageDecode(const DrawImage& draw_image); |
| 143 void UnrefImageDecode(const DrawImage& draw_image); |
| 144 void RefImage(const DrawImage& draw_image); |
| 145 void UnrefImageInternal(const DrawImage& draw_image); |
| 146 void RefCountChanged(ImageData* image_data); |
| 147 |
| 148 // Ensures that the cache can hold an element of |required_size|, freeing |
| 149 // unreferenced cache entries if necessary to make room. |
| 150 bool EnsureCapacity(size_t required_size); |
| 151 bool CanFitSize(size_t size) const; |
| 152 bool ExceedsPreferredCount() const; |
| 153 |
| 154 void DecodeImageIfNecessary(const DrawImage& draw_image, |
| 155 ImageData* image_data); |
| 156 |
| 157 std::unique_ptr<GpuImageDecodeController::ImageData> CreateImageData( |
| 158 const DrawImage& image); |
| 159 SkImageInfo CreateImageInfoForDrawImage(const DrawImage& draw_image) const; |
| 160 |
| 161 // The following two functions also require the |context_| lock to be held. |
| 162 void UploadImageIfNecessary(const DrawImage& draw_image, |
| 163 ImageData* image_data); |
| 164 void DeletePendingImages(); |
| 165 |
| 166 const ResourceFormat format_; |
| 167 ContextProvider* context_; |
| 168 skia::RefPtr<GrContextThreadSafeProxy> context_threadsafe_proxy_; |
| 169 |
| 170 // All members below this point must only be accessed while holding |lock_|. |
| 37 base::Lock lock_; | 171 base::Lock lock_; |
| 38 | 172 |
| 39 std::unordered_set<uint32_t> prerolled_images_; | |
| 40 std::unordered_map<uint32_t, scoped_refptr<ImageDecodeTask>> | 173 std::unordered_map<uint32_t, scoped_refptr<ImageDecodeTask>> |
| 41 pending_image_tasks_; | 174 pending_image_upload_tasks_; |
| 175 std::unordered_map<uint32_t, scoped_refptr<ImageDecodeTask>> |
| 176 pending_image_decode_tasks_; |
| 177 |
| 178 ImageDataMRUCache image_data_; |
| 179 |
| 180 size_t cached_items_limit_; |
| 181 size_t cached_bytes_limit_; |
| 182 size_t bytes_used_; |
| 183 |
| 184 // We can't release GPU backed SkImages without holding the context lock, |
| 185 // so we add them to this list and defer deletion until the next time the lock |
| 186 // is held. |
| 187 std::vector<skia::RefPtr<SkImage>> images_pending_deletion_; |
| 42 }; | 188 }; |
| 43 | 189 |
| 44 } // namespace cc | 190 } // namespace cc |
| 45 | 191 |
| 46 #endif // CC_TILES_GPU_IMAGE_DECODE_CONTROLLER_H_ | 192 #endif // CC_TILES_GPU_IMAGE_DECODE_CONTROLLER_H_ |
| OLD | NEW |