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

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

Powered by Google App Engine
This is Rietveld 408576698