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