OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_IMAGE_DECODE_CONTROLLER_H_ | 5 #ifndef CC_TILES_IMAGE_DECODE_CONTROLLER_H_ |
6 #define CC_TILES_IMAGE_DECODE_CONTROLLER_H_ | 6 #define CC_TILES_IMAGE_DECODE_CONTROLLER_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include "base/containers/hash_tables.h" | 10 #include <unordered_map> |
| 11 #include <unordered_set> |
| 12 |
11 #include "base/hash.h" | 13 #include "base/hash.h" |
12 #include "base/memory/discardable_memory_allocator.h" | 14 #include "base/memory/discardable_memory_allocator.h" |
13 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
14 #include "base/numerics/safe_math.h" | 16 #include "base/numerics/safe_math.h" |
15 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
16 #include "cc/base/cc_export.h" | 18 #include "cc/base/cc_export.h" |
17 #include "cc/playback/decoded_draw_image.h" | 19 #include "cc/playback/decoded_draw_image.h" |
18 #include "cc/playback/draw_image.h" | 20 #include "cc/playback/draw_image.h" |
19 #include "cc/raster/tile_task_runner.h" | 21 #include "cc/raster/tile_task_runner.h" |
20 #include "skia/ext/refptr.h" | 22 #include "skia/ext/refptr.h" |
21 | |
22 #include "ui/gfx/transform.h" | 23 #include "ui/gfx/transform.h" |
23 | 24 |
24 namespace cc { | 25 namespace cc { |
25 | 26 |
26 // ImageDecodeControllerKey is a class that gets a cache key out of a given draw | 27 // ImageDecodeControllerKey is a class that gets a cache key out of a given draw |
27 // image. That is, this key uniquely identifies an image in the cache. Note that | 28 // image. That is, this key uniquely identifies an image in the cache. Note that |
28 // it's insufficient to use SkImage's unique id, since the same image can appear | 29 // it's insufficient to use SkImage's unique id, since the same image can appear |
29 // in the cache multiple times at different scales and filter qualities. | 30 // in the cache multiple times at different scales and filter qualities. |
30 class CC_EXPORT ImageDecodeControllerKey { | 31 class CC_EXPORT ImageDecodeControllerKey { |
31 public: | 32 public: |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 const gfx::Rect& src_rect, | 64 const gfx::Rect& src_rect, |
64 const gfx::Size& size, | 65 const gfx::Size& size, |
65 SkFilterQuality filter_quality); | 66 SkFilterQuality filter_quality); |
66 | 67 |
67 uint32_t image_id_; | 68 uint32_t image_id_; |
68 gfx::Rect src_rect_; | 69 gfx::Rect src_rect_; |
69 gfx::Size target_size_; | 70 gfx::Size target_size_; |
70 SkFilterQuality filter_quality_; | 71 SkFilterQuality filter_quality_; |
71 }; | 72 }; |
72 | 73 |
73 } // namespace cc | |
74 | |
75 // Hash function for the above ImageDecodeControllerKey. | 74 // Hash function for the above ImageDecodeControllerKey. |
76 namespace BASE_HASH_NAMESPACE { | 75 struct ImageDecodeControllerKeyHash { |
77 template <> | 76 size_t operator()(const ImageDecodeControllerKey& key) const { |
78 struct hash<cc::ImageDecodeControllerKey> { | |
79 size_t operator()(const cc::ImageDecodeControllerKey& key) const { | |
80 // TODO(vmpstr): This is a mess. Maybe it's faster to just search the vector | 77 // TODO(vmpstr): This is a mess. Maybe it's faster to just search the vector |
81 // always (forwards or backwards to account for LRU). | 78 // always (forwards or backwards to account for LRU). |
82 uint64_t src_rect_hash = | 79 uint64_t src_rect_hash = |
83 base::HashInts(static_cast<uint64_t>(base::HashInts( | 80 base::HashInts(static_cast<uint64_t>(base::HashInts( |
84 key.src_rect().x(), key.src_rect().y())), | 81 key.src_rect().x(), key.src_rect().y())), |
85 static_cast<uint64_t>(base::HashInts( | 82 static_cast<uint64_t>(base::HashInts( |
86 key.src_rect().width(), key.src_rect().height()))); | 83 key.src_rect().width(), key.src_rect().height()))); |
87 | 84 |
88 uint64_t target_size_hash = | 85 uint64_t target_size_hash = |
89 base::HashInts(key.target_size().width(), key.target_size().height()); | 86 base::HashInts(key.target_size().width(), key.target_size().height()); |
90 | 87 |
91 return base::HashInts(base::HashInts(src_rect_hash, target_size_hash), | 88 return base::HashInts(base::HashInts(src_rect_hash, target_size_hash), |
92 base::HashInts(key.image_id(), key.filter_quality())); | 89 base::HashInts(key.image_id(), key.filter_quality())); |
93 } | 90 } |
94 }; | 91 }; |
95 } // namespace BASE_HASH_NAMESPACE | |
96 | |
97 namespace cc { | |
98 | 92 |
99 // ImageDecodeController is responsible for generating decode tasks, decoding | 93 // ImageDecodeController is responsible for generating decode tasks, decoding |
100 // images, storing images in cache, and being able to return the decoded images | 94 // images, storing images in cache, and being able to return the decoded images |
101 // when requested. | 95 // when requested. |
102 | 96 |
103 // ImageDecodeController is responsible for the following things: | 97 // ImageDecodeController is responsible for the following things: |
104 // 1. Given a DrawImage, it can return an ImageDecodeTask which when run will | 98 // 1. Given a DrawImage, it can return an ImageDecodeTask which when run will |
105 // decode and cache the resulting image. If the image does not need a task to | 99 // decode and cache the resulting image. If the image does not need a task to |
106 // be decoded, then nullptr will be returned. The return value of the | 100 // be decoded, then nullptr will be returned. The return value of the |
107 // function indicates whether the image was or is going to be locked, so an | 101 // function indicates whether the image was or is going to be locked, so an |
108 // unlock will be required. | 102 // unlock will be required. |
109 // 2. Given a cache key and a DrawImage, it can decode the image and store it in | 103 // 2. Given a cache key and a DrawImage, it can decode the image and store it in |
110 // the cache. Note that it is important that this function is only accessed | 104 // the cache. Note that it is important that this function is only accessed |
111 // via an image decode task. | 105 // via an image decode task. |
112 // 3. Given a DrawImage, it can return a DecodedDrawImage, which represented the | 106 // 3. Given a DrawImage, it can return a DecodedDrawImage, which represented the |
113 // decoded version of the image. Note that if the image is not in the cache | 107 // decoded version of the image. Note that if the image is not in the cache |
114 // and it needs to be scaled/decoded, then this decode will happen as part of | 108 // and it needs to be scaled/decoded, then this decode will happen as part of |
115 // getting the image. As such, this should only be accessed from a raster | 109 // getting the image. As such, this should only be accessed from a raster |
116 // thread. | 110 // thread. |
117 class CC_EXPORT ImageDecodeController { | 111 class CC_EXPORT ImageDecodeController { |
118 public: | 112 public: |
119 using ImageKey = ImageDecodeControllerKey; | 113 using ImageKey = ImageDecodeControllerKey; |
| 114 using ImageKeyHash = ImageDecodeControllerKeyHash; |
120 | 115 |
121 ImageDecodeController(); | 116 ImageDecodeController(); |
122 ~ImageDecodeController(); | 117 ~ImageDecodeController(); |
123 | 118 |
124 // Fill in an ImageDecodeTask which will decode the given image when run. In | 119 // Fill in an ImageDecodeTask which will decode the given image when run. In |
125 // case the image is already cached, fills in nullptr. Returns true if the | 120 // case the image is already cached, fills in nullptr. Returns true if the |
126 // image needs to be unreffed when the caller is finished with it. | 121 // image needs to be unreffed when the caller is finished with it. |
127 // | 122 // |
128 // This is called by the tile manager (on the compositor thread) when creating | 123 // This is called by the tile manager (on the compositor thread) when creating |
129 // a raster task. | 124 // a raster task. |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
229 // These functions indicate whether the images can be handled and cached by | 224 // These functions indicate whether the images can be handled and cached by |
230 // ImageDecodeController or whether they will fall through to Skia (with | 225 // ImageDecodeController or whether they will fall through to Skia (with |
231 // exception of possibly prerolling them). Over time these should return | 226 // exception of possibly prerolling them). Over time these should return |
232 // "false" in less cases, as the ImageDecodeController should start handling | 227 // "false" in less cases, as the ImageDecodeController should start handling |
233 // more of them. | 228 // more of them. |
234 bool CanHandleImage(const ImageKey& key, const DrawImage& image); | 229 bool CanHandleImage(const ImageKey& key, const DrawImage& image); |
235 bool CanHandleFilterQuality(SkFilterQuality filter_quality); | 230 bool CanHandleFilterQuality(SkFilterQuality filter_quality); |
236 | 231 |
237 bool is_using_gpu_rasterization_; | 232 bool is_using_gpu_rasterization_; |
238 | 233 |
239 base::hash_map<ImageKey, scoped_refptr<ImageDecodeTask>> pending_image_tasks_; | 234 std::unordered_map<ImageKey, scoped_refptr<ImageDecodeTask>, ImageKeyHash> |
| 235 pending_image_tasks_; |
240 | 236 |
241 // The members below this comment can only be accessed if the lock is held to | 237 // The members below this comment can only be accessed if the lock is held to |
242 // ensure that they are safe to access on multiple threads. | 238 // ensure that they are safe to access on multiple threads. |
243 base::Lock lock_; | 239 base::Lock lock_; |
244 | 240 |
245 std::deque<AnnotatedDecodedImage> decoded_images_; | 241 std::deque<AnnotatedDecodedImage> decoded_images_; |
246 base::hash_map<ImageKey, int> decoded_images_ref_counts_; | 242 std::unordered_map<ImageKey, int, ImageKeyHash> decoded_images_ref_counts_; |
247 std::deque<AnnotatedDecodedImage> at_raster_decoded_images_; | 243 std::deque<AnnotatedDecodedImage> at_raster_decoded_images_; |
248 base::hash_map<ImageKey, int> at_raster_decoded_images_ref_counts_; | 244 std::unordered_map<ImageKey, int, ImageKeyHash> |
| 245 at_raster_decoded_images_ref_counts_; |
249 MemoryBudget locked_images_budget_; | 246 MemoryBudget locked_images_budget_; |
250 | 247 |
251 // Note that this is used for cases where the only thing we do is preroll the | 248 // Note that this is used for cases where the only thing we do is preroll the |
252 // image the first time we see it. This mimics the previous behavior and | 249 // image the first time we see it. This mimics the previous behavior and |
253 // should over time change as the compositor starts to handle more cases. | 250 // should over time change as the compositor starts to handle more cases. |
254 base::hash_set<uint32_t> prerolled_images_; | 251 std::unordered_set<uint32_t> prerolled_images_; |
255 }; | 252 }; |
256 | 253 |
257 } // namespace cc | 254 } // namespace cc |
258 | 255 |
259 #endif // CC_TILES_IMAGE_DECODE_CONTROLLER_H_ | 256 #endif // CC_TILES_IMAGE_DECODE_CONTROLLER_H_ |
OLD | NEW |