Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(301)

Side by Side Diff: cc/tiles/image_decode_controller.h

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

Powered by Google App Engine
This is Rietveld 408576698