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 "base/containers/hash_tables.h" | 8 #include "base/containers/hash_tables.h" |
9 #include "base/memory/discardable_memory_allocator.h" | |
9 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
11 #include "base/numerics/safe_math.h" | |
12 #include "base/threading/thread_checker.h" | |
10 #include "cc/base/cc_export.h" | 13 #include "cc/base/cc_export.h" |
11 #include "cc/playback/discardable_image_map.h" | 14 #include "cc/playback/decoded_draw_image.h" |
15 #include "cc/playback/draw_image.h" | |
12 #include "cc/raster/tile_task_runner.h" | 16 #include "cc/raster/tile_task_runner.h" |
13 #include "skia/ext/refptr.h" | 17 #include "skia/ext/refptr.h" |
14 | 18 |
19 #include "ui/gfx/transform.h" | |
20 | |
15 namespace cc { | 21 namespace cc { |
16 | 22 |
17 class ImageDecodeController { | 23 // ImageDecodeControllerKey is a class that gets a cache key out of a given draw |
24 // image. That is, this key uniquely identifies an image in the cache. Note that | |
25 // it's insufficient to use SkImage's unique id, since the same image can appear | |
26 // in the cache multiple times at different scales and filter qualities. | |
27 class CC_EXPORT ImageDecodeControllerKey { | |
18 public: | 28 public: |
29 static ImageDecodeControllerKey FromDrawImage(const DrawImage& image); | |
30 | |
31 bool operator==(const ImageDecodeControllerKey& other) const { | |
32 return image_id_ == other.image_id_ && src_rect_ == other.src_rect_ && | |
33 size_ == other.size_ && filter_quality_ == other.filter_quality_; | |
34 } | |
35 | |
36 bool operator!=(const ImageDecodeControllerKey& other) const { | |
37 return !(*this == other); | |
38 } | |
39 | |
40 uint32_t image_id() const { return image_id_; } | |
41 SkFilterQuality filter_quality() const { return filter_quality_; } | |
42 gfx::Rect src_rect() const { return src_rect_; } | |
43 gfx::Size target_size() const { return size_; } | |
44 | |
45 // Helper to figure out how much memory this decoded image would take. | |
46 size_t target_bytes() const { | |
47 // TODO(vmpstr): Handle formats other than RGBA. | |
48 base::CheckedNumeric<size_t> result = 4; | |
49 result *= size_.width(); | |
50 result *= size_.height(); | |
51 return result.ValueOrDefault(std::numeric_limits<size_t>::max()); | |
52 } | |
53 | |
54 std::string ToString() const; | |
55 | |
56 private: | |
57 ImageDecodeControllerKey(uint32_t image_id, | |
58 const gfx::Rect& src_rect, | |
59 const gfx::Size& size, | |
60 SkFilterQuality filter_quality); | |
61 | |
62 uint32_t image_id_; | |
63 gfx::Rect src_rect_; | |
64 gfx::Size size_; | |
65 SkFilterQuality filter_quality_; | |
66 }; | |
67 | |
68 } // namespace cc | |
69 | |
70 // Hash function for the above ImageDecodeControllerKey. | |
71 namespace BASE_HASH_NAMESPACE { | |
72 template <> | |
73 struct hash<cc::ImageDecodeControllerKey> { | |
74 size_t operator()(const cc::ImageDecodeControllerKey& key) const { | |
75 // TODO(vmpstr): This is a mess. Maybe it's faster to just search the vector | |
76 // always (forwards or backwards to account for LRU). | |
77 uint64_t src_rect_hash = | |
78 base::HashPair(static_cast<uint64_t>(base::HashPair( | |
79 key.src_rect().x(), key.src_rect().y())), | |
80 static_cast<uint64_t>(base::HashPair( | |
81 key.src_rect().width(), key.src_rect().height()))); | |
82 | |
83 uint64_t target_size_hash = | |
84 base::HashPair(key.target_size().width(), key.target_size().height()); | |
85 | |
86 return base::HashPair(base::HashPair(src_rect_hash, target_size_hash), | |
87 base::HashPair(key.image_id(), key.filter_quality())); | |
88 } | |
89 }; | |
90 } // namespace BASE_HASH_NAMESPACE | |
91 | |
92 namespace cc { | |
93 | |
94 // ImageDecodeController is responsible for generating decode tasks, decoding | |
95 // images, storing images in cache, and being able to return the decoded images | |
96 // when requested. | |
97 | |
98 // ImageDecodeController is responsible for the following things: | |
99 // 1. Given a DrawImage, it can return an ImageDecodeTask which when run will | |
100 // decode and cache the resulting image. If the image does not need a task to | |
101 // be decoded, then nullptr will be returned. The return value of the | |
102 // function indicates whether the image was or is going to be locked, so an | |
103 // unlock will be required. | |
104 // 2. Given a cache key and a DrawImage, it can decode the image and store it in | |
105 // the cache. Note that it is important that this function is only accessed | |
106 // via an image decode task. | |
107 // 3. Given a DrawImage, it can return a DecodedDrawImage, which represented the | |
108 // decoded version of the image. Note that if the image is not in the cache | |
109 // and it needs to be scaled/decoded, then this decode will happen as part of | |
110 // getting the image. As such, this should only be accessed from a raster | |
111 // thread. | |
112 class CC_EXPORT ImageDecodeController { | |
113 public: | |
114 using ImageKey = ImageDecodeControllerKey; | |
115 | |
19 ImageDecodeController(); | 116 ImageDecodeController(); |
20 ~ImageDecodeController(); | 117 ~ImageDecodeController(); |
21 | 118 |
22 scoped_refptr<ImageDecodeTask> GetTaskForImage(const DrawImage& image, | 119 // Fill in an ImageDecodeTask which will decode the given image when run. In |
23 int layer_id, | 120 // case the image is already cached, fills in nullptr. Returns true if the |
24 uint64_t prepare_tiles_id); | 121 // image needs to be unreffed when the caller is finished with it. |
25 | 122 // |
26 // Note that this function has to remain thread safe. | 123 // This is called by the tile manager (on the compositor thread) when creating |
27 void DecodeImage(const SkImage* image); | 124 // a raster task. |
28 | 125 bool GetTaskForImageAndRef(const DrawImage& image, |
29 // TODO(vmpstr): This should go away once the controller is decoding images | 126 uint64_t prepare_tiles_id, |
30 // based on priority and memory. | 127 scoped_refptr<ImageDecodeTask>* task); |
31 void AddLayerUsedCount(int layer_id); | 128 // Unrefs an image. When the tile is finished, this should be called for every |
32 void SubtractLayerUsedCount(int layer_id); | 129 // GetTaskForImageAndRef call that returned true. |
33 | 130 void UnrefImage(const DrawImage& image); |
34 void OnImageDecodeTaskCompleted(int layer_id, | 131 |
35 const SkImage* image, | 132 // Returns a decoded draw image. If the image isn't found in the cache, a |
36 bool was_canceled); | 133 // decode will happen. |
134 // | |
135 // This is called by a raster task (on a worker thread) when an image is | |
136 // required. | |
137 DecodedDrawImage GetDecodedImageForDraw(const DrawImage& image); | |
138 // Unrefs an image. This should be called for every GetDecodedImageForDraw | |
139 // when the draw with the image is finished. | |
140 void DrawWithImageFinished(const DrawImage& image, | |
141 const DecodedDrawImage& decoded_image); | |
142 | |
143 // Decode the given image and store it in the cache. This is only called by an | |
144 // image decode task from a worker thread. | |
145 void DecodeImage(const ImageKey& key, const DrawImage& image); | |
146 | |
147 void ReduceCacheUsage(); | |
148 | |
149 void RemovePendingTask(const ImageKey& key); | |
150 | |
151 // Info the controller whether we're using gpu rasterization or not. Since the | |
152 // decode and caching behavior is different for SW and GPU decodes, when the | |
153 // state changes, we clear all of the caches. This means that this is only | |
154 // safe to call when there are no pending tasks (and no refs on any images). | |
155 void SetIsUsingGpuRasterization(bool is_using_gpu_rasterization); | |
37 | 156 |
38 private: | 157 private: |
39 scoped_refptr<ImageDecodeTask> CreateTaskForImage(const SkImage* image, | 158 // DecodedImage is a convenience storage for discardable memory. It can also |
40 int layer_id, | 159 // construct an image out of SkImageInfo and stored discardable memory. |
41 uint64_t prepare_tiles_id); | 160 // TODO(vmpstr): Make this scoped_ptr. |
42 | 161 class DecodedImage : public base::RefCounted<DecodedImage> { |
43 using ImageTaskMap = base::hash_map<uint32_t, scoped_refptr<ImageDecodeTask>>; | 162 public: |
44 using LayerImageTaskMap = base::hash_map<int, ImageTaskMap>; | 163 DecodedImage(const SkImageInfo& info, |
45 LayerImageTaskMap image_decode_tasks_; | 164 scoped_ptr<base::DiscardableMemory> memory, |
46 | 165 const SkSize& src_rect_offset); |
47 using LayerCountMap = base::hash_map<int, int>; | 166 |
48 LayerCountMap used_layer_counts_; | 167 SkImage* image() const { |
168 DCHECK(locked_); | |
169 return image_.get(); | |
170 } | |
171 | |
172 const SkSize& src_rect_offset() const { return src_rect_offset_; } | |
173 | |
174 bool is_locked() const { return locked_; } | |
175 bool Lock(); | |
176 void Unlock(); | |
177 | |
178 private: | |
179 friend class base::RefCounted<DecodedImage>; | |
180 | |
181 ~DecodedImage(); | |
182 | |
183 bool locked_; | |
184 SkImageInfo image_info_; | |
185 scoped_ptr<base::DiscardableMemory> memory_; | |
186 skia::RefPtr<SkImage> image_; | |
187 SkSize src_rect_offset_; | |
188 }; | |
189 | |
190 // MemoryBudget is a convenience class for memory bookkeeping and ensuring | |
191 // that we don't go over the limit when pre-decoding. | |
192 // TODO(vmpstr): Add memory infra to keep track of memory usage of this class. | |
193 class MemoryBudget { | |
194 public: | |
195 explicit MemoryBudget(size_t limit_bytes); | |
196 | |
197 size_t AvailableMemoryBytes() const; | |
198 void AddUsage(size_t usage); | |
199 void SubtractUsage(size_t usage); | |
200 void ResetUsage(); | |
201 | |
202 private: | |
203 size_t GetCurrentUsageSafe() const; | |
204 | |
205 size_t limit_bytes_; | |
206 base::CheckedNumeric<size_t> current_usage_bytes_; | |
207 }; | |
208 | |
209 using AnnotatedDecodedImage = | |
210 std::pair<ImageKey, scoped_refptr<DecodedImage>>; | |
211 | |
212 // Looks for the key in the cache and returns true if it was found and was | |
213 // successfully locked (or if it was already locked). Note that if this | |
214 // function returns true, then a ref count is increased for the image. | |
215 bool LockDecodedImageIfPossibleAndRef(const ImageKey& key); | |
216 | |
217 scoped_refptr<DecodedImage> DecodeImageInternal(const ImageKey& key, | |
218 const SkImage* image); | |
219 bool CanHandleFilterQuality(SkFilterQuality filter_quality); | |
220 size_t SanityCheckState(int line, bool lock_acquired); | |
221 void RefImage(const ImageKey& key); | |
222 void RefAtRasterImage(const ImageKey& key); | |
223 void UnrefAtRasterImage(const ImageKey& key); | |
224 bool ShouldDecodeAndScaleImage(const ImageKey& key, const DrawImage& image); | |
aelias_OOO_until_Jul13
2015/12/09 00:09:48
CanHandleImage();
vmpstr
2015/12/09 23:53:41
Done.
| |
225 | |
226 bool is_using_gpu_rasterization_; | |
227 base::hash_map<ImageKey, scoped_refptr<ImageDecodeTask>> pending_image_tasks_; | |
228 | |
229 // The members can only be accessed if the lock is held to ensure that they | |
230 // are safe to access on multiple threads. | |
231 base::Lock lock_; | |
232 std::deque<AnnotatedDecodedImage> decoded_images_; | |
233 base::hash_map<ImageKey, int> decoded_images_ref_counts_; | |
234 std::deque<AnnotatedDecodedImage> at_raster_decoded_images_; | |
235 base::hash_map<ImageKey, int> at_raster_decoded_images_ref_counts_; | |
236 MemoryBudget locked_images_budget_; | |
237 base::hash_set<uint32_t> prerolled_images_; | |
aelias_OOO_until_Jul13
2015/12/09 00:09:48
It took me some time to realize this is only used
vmpstr
2015/12/09 23:53:41
I moved these around a bit and added a few comment
| |
49 }; | 238 }; |
50 | 239 |
51 } // namespace cc | 240 } // namespace cc |
52 | 241 |
53 #endif // CC_TILES_IMAGE_DECODE_CONTROLLER_H_ | 242 #endif // CC_TILES_IMAGE_DECODE_CONTROLLER_H_ |
OLD | NEW |