vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef CC_TILES_SOFTWARE_IMAGE_DECODE_CONTROLLER_H_ |
| 6 | #define CC_TILES_SOFTWARE_IMAGE_DECODE_CONTROLLER_H_ |
| 7 | |
| 8 | #include <stdint.h> |
| 9 | |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 10 | #include <memory> |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 11 | #include <unordered_map> |
| 12 | #include <unordered_set> |
| 13 | |
ericrk | 05ce07fc | 2016-04-08 21:48:59 | [diff] [blame] | 14 | #include "base/atomic_sequence_num.h" |
vmpstr | e37a753b | 2016-03-03 20:42:17 | [diff] [blame] | 15 | #include "base/containers/mru_cache.h" |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 16 | #include "base/hash.h" |
| 17 | #include "base/memory/discardable_memory_allocator.h" |
| 18 | #include "base/memory/ref_counted.h" |
| 19 | #include "base/numerics/safe_math.h" |
| 20 | #include "base/threading/thread_checker.h" |
ericrk | 05ce07fc | 2016-04-08 21:48:59 | [diff] [blame] | 21 | #include "base/trace_event/memory_dump_provider.h" |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 22 | #include "cc/base/cc_export.h" |
| 23 | #include "cc/playback/decoded_draw_image.h" |
| 24 | #include "cc/playback/draw_image.h" |
| 25 | #include "cc/raster/tile_task_runner.h" |
| 26 | #include "cc/tiles/image_decode_controller.h" |
| 27 | #include "skia/ext/refptr.h" |
| 28 | |
| 29 | namespace cc { |
| 30 | |
| 31 | // ImageDecodeControllerKey is a class that gets a cache key out of a given draw |
| 32 | // image. That is, this key uniquely identifies an image in the cache. Note that |
| 33 | // it's insufficient to use SkImage's unique id, since the same image can appear |
| 34 | // in the cache multiple times at different scales and filter qualities. |
| 35 | class CC_EXPORT ImageDecodeControllerKey { |
| 36 | public: |
| 37 | static ImageDecodeControllerKey FromDrawImage(const DrawImage& image); |
| 38 | |
vmpstr | bf0d713a | 2016-03-24 20:22:54 | [diff] [blame] | 39 | ImageDecodeControllerKey(const ImageDecodeControllerKey& other); |
| 40 | |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 41 | bool operator==(const ImageDecodeControllerKey& other) const { |
| 42 | // The image_id always has to be the same. However, after that all original |
| 43 | // decodes are the same, so if we can use the original decode, return true. |
| 44 | // If not, then we have to compare every field. |
| 45 | return image_id_ == other.image_id_ && |
| 46 | can_use_original_decode_ == other.can_use_original_decode_ && |
| 47 | (can_use_original_decode_ || |
| 48 | (src_rect_ == other.src_rect_ && |
| 49 | target_size_ == other.target_size_ && |
| 50 | filter_quality_ == other.filter_quality_)); |
| 51 | } |
| 52 | |
| 53 | bool operator!=(const ImageDecodeControllerKey& other) const { |
| 54 | return !(*this == other); |
| 55 | } |
| 56 | |
| 57 | uint32_t image_id() const { return image_id_; } |
| 58 | SkFilterQuality filter_quality() const { return filter_quality_; } |
| 59 | gfx::Rect src_rect() const { return src_rect_; } |
| 60 | gfx::Size target_size() const { return target_size_; } |
| 61 | |
| 62 | bool can_use_original_decode() const { return can_use_original_decode_; } |
| 63 | size_t get_hash() const { return hash_; } |
| 64 | |
| 65 | // Helper to figure out how much memory the locked image represented by this |
| 66 | // key would take. |
| 67 | size_t locked_bytes() const { |
| 68 | // TODO(vmpstr): Handle formats other than RGBA. |
| 69 | base::CheckedNumeric<size_t> result = 4; |
| 70 | result *= target_size_.width(); |
| 71 | result *= target_size_.height(); |
| 72 | return result.ValueOrDefault(std::numeric_limits<size_t>::max()); |
| 73 | } |
| 74 | |
| 75 | std::string ToString() const; |
| 76 | |
| 77 | private: |
| 78 | ImageDecodeControllerKey(uint32_t image_id, |
| 79 | const gfx::Rect& src_rect, |
| 80 | const gfx::Size& size, |
| 81 | SkFilterQuality filter_quality, |
| 82 | bool can_use_original_decode); |
| 83 | |
| 84 | uint32_t image_id_; |
| 85 | gfx::Rect src_rect_; |
| 86 | gfx::Size target_size_; |
| 87 | SkFilterQuality filter_quality_; |
| 88 | bool can_use_original_decode_; |
| 89 | size_t hash_; |
| 90 | }; |
| 91 | |
| 92 | // Hash function for the above ImageDecodeControllerKey. |
| 93 | struct ImageDecodeControllerKeyHash { |
| 94 | size_t operator()(const ImageDecodeControllerKey& key) const { |
| 95 | return key.get_hash(); |
| 96 | } |
| 97 | }; |
| 98 | |
ericrk | 05ce07fc | 2016-04-08 21:48:59 | [diff] [blame] | 99 | class CC_EXPORT SoftwareImageDecodeController |
| 100 | : public ImageDecodeController, |
| 101 | public base::trace_event::MemoryDumpProvider { |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 102 | public: |
| 103 | using ImageKey = ImageDecodeControllerKey; |
| 104 | using ImageKeyHash = ImageDecodeControllerKeyHash; |
| 105 | |
vmpstr | da84a06 | 2016-03-21 22:02:56 | [diff] [blame] | 106 | explicit SoftwareImageDecodeController(ResourceFormat format); |
| 107 | SoftwareImageDecodeController(); |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 108 | ~SoftwareImageDecodeController() override; |
| 109 | |
| 110 | // ImageDecodeController overrides. |
| 111 | bool GetTaskForImageAndRef(const DrawImage& image, |
| 112 | uint64_t prepare_tiles_id, |
| 113 | scoped_refptr<ImageDecodeTask>* task) override; |
| 114 | void UnrefImage(const DrawImage& image) override; |
| 115 | DecodedDrawImage GetDecodedImageForDraw(const DrawImage& image) override; |
| 116 | void DrawWithImageFinished(const DrawImage& image, |
| 117 | const DecodedDrawImage& decoded_image) override; |
| 118 | void ReduceCacheUsage() override; |
ericrk | d2ff2a13 | 2016-04-11 22:16:00 | [diff] [blame] | 119 | // Software doesn't keep outstanding images pinned, so this is a no-op. |
| 120 | void SetShouldAggressivelyFreeResources( |
| 121 | bool aggressively_free_resources) override {} |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 122 | |
| 123 | // Decode the given image and store it in the cache. This is only called by an |
| 124 | // image decode task from a worker thread. |
| 125 | void DecodeImage(const ImageKey& key, const DrawImage& image); |
| 126 | |
| 127 | void RemovePendingTask(const ImageKey& key); |
| 128 | |
ericrk | 05ce07fc | 2016-04-08 21:48:59 | [diff] [blame] | 129 | // MemoryDumpProvider overrides. |
| 130 | bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args, |
| 131 | base::trace_event::ProcessMemoryDump* pmd) override; |
| 132 | |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 133 | private: |
| 134 | // DecodedImage is a convenience storage for discardable memory. It can also |
| 135 | // construct an image out of SkImageInfo and stored discardable memory. |
vmpstr | 61c1b9d | 2016-03-11 23:55:38 | [diff] [blame] | 136 | class DecodedImage { |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 137 | public: |
| 138 | DecodedImage(const SkImageInfo& info, |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 139 | std::unique_ptr<base::DiscardableMemory> memory, |
ericrk | 05ce07fc | 2016-04-08 21:48:59 | [diff] [blame] | 140 | const SkSize& src_rect_offset, |
| 141 | uint64_t tracing_id); |
vmpstr | 61c1b9d | 2016-03-11 23:55:38 | [diff] [blame] | 142 | ~DecodedImage(); |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 143 | |
| 144 | SkImage* image() const { |
| 145 | DCHECK(locked_); |
| 146 | return image_.get(); |
| 147 | } |
| 148 | |
| 149 | const SkSize& src_rect_offset() const { return src_rect_offset_; } |
| 150 | |
| 151 | bool is_locked() const { return locked_; } |
| 152 | bool Lock(); |
| 153 | void Unlock(); |
| 154 | |
ericrk | 05ce07fc | 2016-04-08 21:48:59 | [diff] [blame] | 155 | const base::DiscardableMemory* memory() const { return memory_.get(); } |
| 156 | |
| 157 | // An ID which uniquely identifies this DecodedImage within the image decode |
| 158 | // controller. Used in memory tracing. |
| 159 | uint64_t tracing_id() const { return tracing_id_; } |
| 160 | |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 161 | private: |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 162 | bool locked_; |
| 163 | SkImageInfo image_info_; |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 164 | std::unique_ptr<base::DiscardableMemory> memory_; |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 165 | skia::RefPtr<SkImage> image_; |
| 166 | SkSize src_rect_offset_; |
ericrk | 05ce07fc | 2016-04-08 21:48:59 | [diff] [blame] | 167 | uint64_t tracing_id_; |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | // MemoryBudget is a convenience class for memory bookkeeping and ensuring |
| 171 | // that we don't go over the limit when pre-decoding. |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 172 | class MemoryBudget { |
| 173 | public: |
| 174 | explicit MemoryBudget(size_t limit_bytes); |
| 175 | |
| 176 | size_t AvailableMemoryBytes() const; |
| 177 | void AddUsage(size_t usage); |
| 178 | void SubtractUsage(size_t usage); |
| 179 | void ResetUsage(); |
| 180 | |
| 181 | private: |
| 182 | size_t GetCurrentUsageSafe() const; |
| 183 | |
| 184 | size_t limit_bytes_; |
| 185 | base::CheckedNumeric<size_t> current_usage_bytes_; |
| 186 | }; |
| 187 | |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 188 | using ImageMRUCache = base::HashingMRUCache<ImageKey, |
| 189 | std::unique_ptr<DecodedImage>, |
| 190 | ImageKeyHash>; |
ericrk | 05ce07fc | 2016-04-08 21:48:59 | [diff] [blame] | 191 | |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 192 | // Looks for the key in the cache and returns true if it was found and was |
| 193 | // successfully locked (or if it was already locked). Note that if this |
| 194 | // function returns true, then a ref count is increased for the image. |
| 195 | bool LockDecodedImageIfPossibleAndRef(const ImageKey& key); |
| 196 | |
| 197 | // Actually decode the image. Note that this function can (and should) be |
| 198 | // called with no lock acquired, since it can do a lot of work. Note that it |
| 199 | // can also return nullptr to indicate the decode failed. |
danakj | 60bc3bc | 2016-04-09 00:24:48 | [diff] [blame] | 200 | std::unique_ptr<DecodedImage> DecodeImageInternal( |
| 201 | const ImageKey& key, |
| 202 | const DrawImage& draw_image); |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 203 | |
| 204 | // Get the decoded draw image for the given key and draw_image. Note that this |
| 205 | // function has to be called with no lock acquired, since it will acquire its |
| 206 | // own locks and might call DecodeImageInternal above. Also note that this |
| 207 | // function will use the provided key, even if |
| 208 | // ImageKey::FromDrawImage(draw_image) would return a different key. |
| 209 | // Note that when used internally, we still require that |
| 210 | // DrawWithImageFinished() is called afterwards. |
| 211 | DecodedDrawImage GetDecodedImageForDrawInternal(const ImageKey& key, |
| 212 | const DrawImage& draw_image); |
| 213 | |
cblume | 4c4a73d71 | 2016-04-14 21:18:45 | [diff] [blame] | 214 | // GetOriginalImageDecode is called by DecodeImageInternal when the quality |
| 215 | // does not scale the image. Like DecodeImageInternal, it should be called |
| 216 | // with no lock acquired and it returns nullptr if the decoding failed. |
| 217 | std::unique_ptr<DecodedImage> GetOriginalImageDecode(const ImageKey& key, |
| 218 | const SkImage& image); |
| 219 | |
| 220 | // GetScaledImageDecode is called by DecodeImageInternal when the quality |
| 221 | // requires the image be scaled. Like DecodeImageInternal, it should be |
| 222 | // called with no lock acquired and it returns nullptr if the decoding or |
| 223 | // scaling failed. |
| 224 | std::unique_ptr<DecodedImage> GetScaledImageDecode(const ImageKey& key, |
| 225 | const SkImage& image); |
| 226 | |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 227 | void SanityCheckState(int line, bool lock_acquired); |
| 228 | void RefImage(const ImageKey& key); |
| 229 | void RefAtRasterImage(const ImageKey& key); |
| 230 | void UnrefAtRasterImage(const ImageKey& key); |
| 231 | |
| 232 | // These functions indicate whether the images can be handled and cached by |
| 233 | // ImageDecodeController or whether they will fall through to Skia (with |
| 234 | // exception of possibly prerolling them). Over time these should return |
| 235 | // "false" in less cases, as the ImageDecodeController should start handling |
| 236 | // more of them. |
vmpstr | f0348bbb | 2016-03-03 03:02:59 | [diff] [blame] | 237 | bool CanHandleImage(const ImageKey& key); |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 238 | |
ericrk | 05ce07fc | 2016-04-08 21:48:59 | [diff] [blame] | 239 | // Helper function which dumps all images in a specific ImageMRUCache. |
| 240 | void DumpImageMemoryForCache(const ImageMRUCache& cache, |
| 241 | const char* cache_name, |
| 242 | base::trace_event::ProcessMemoryDump* pmd) const; |
| 243 | |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 244 | std::unordered_map<ImageKey, scoped_refptr<ImageDecodeTask>, ImageKeyHash> |
| 245 | pending_image_tasks_; |
| 246 | |
| 247 | // The members below this comment can only be accessed if the lock is held to |
| 248 | // ensure that they are safe to access on multiple threads. |
| 249 | base::Lock lock_; |
| 250 | |
vmpstr | e37a753b | 2016-03-03 20:42:17 | [diff] [blame] | 251 | // Decoded images and ref counts (predecode path). |
| 252 | ImageMRUCache decoded_images_; |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 253 | std::unordered_map<ImageKey, int, ImageKeyHash> decoded_images_ref_counts_; |
vmpstr | e37a753b | 2016-03-03 20:42:17 | [diff] [blame] | 254 | |
| 255 | // Decoded image and ref counts (at-raster decode path). |
| 256 | ImageMRUCache at_raster_decoded_images_; |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 257 | std::unordered_map<ImageKey, int, ImageKeyHash> |
| 258 | at_raster_decoded_images_ref_counts_; |
vmpstr | e37a753b | 2016-03-03 20:42:17 | [diff] [blame] | 259 | |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 260 | MemoryBudget locked_images_budget_; |
| 261 | |
| 262 | // Note that this is used for cases where the only thing we do is preroll the |
| 263 | // image the first time we see it. This mimics the previous behavior and |
| 264 | // should over time change as the compositor starts to handle more cases. |
| 265 | std::unordered_set<uint32_t> prerolled_images_; |
bashi | 0c87a7ea | 2016-03-18 01:05:20 | [diff] [blame] | 266 | |
| 267 | ResourceFormat format_; |
ericrk | 05ce07fc | 2016-04-08 21:48:59 | [diff] [blame] | 268 | |
| 269 | // Used to uniquely identify DecodedImages for memory traces. |
| 270 | base::AtomicSequenceNumber next_tracing_id_; |
vmpstr | 64cdba3 | 2016-03-03 00:38:40 | [diff] [blame] | 271 | }; |
| 272 | |
| 273 | } // namespace cc |
| 274 | |
| 275 | #endif // CC_TILES_SOFTWARE_IMAGE_DECODE_CONTROLLER_H_ |