blob: d68ac90c95318b278c126bda8601f1fd2464dab0 [file] [log] [blame]
vmpstr64cdba32016-03-03 00:38:401// Copyright 2016 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CC_TILES_SOFTWARE_IMAGE_DECODE_CONTROLLER_H_
6#define CC_TILES_SOFTWARE_IMAGE_DECODE_CONTROLLER_H_
7
8#include <stdint.h>
9
10#include <unordered_map>
11#include <unordered_set>
12
vmpstre37a753b2016-03-03 20:42:1713#include "base/containers/mru_cache.h"
vmpstr64cdba32016-03-03 00:38:4014#include "base/hash.h"
15#include "base/memory/discardable_memory_allocator.h"
16#include "base/memory/ref_counted.h"
17#include "base/numerics/safe_math.h"
18#include "base/threading/thread_checker.h"
19#include "cc/base/cc_export.h"
20#include "cc/playback/decoded_draw_image.h"
21#include "cc/playback/draw_image.h"
22#include "cc/raster/tile_task_runner.h"
23#include "cc/tiles/image_decode_controller.h"
24#include "skia/ext/refptr.h"
25
26namespace cc {
27
28// ImageDecodeControllerKey is a class that gets a cache key out of a given draw
29// image. That is, this key uniquely identifies an image in the cache. Note that
30// it's insufficient to use SkImage's unique id, since the same image can appear
31// in the cache multiple times at different scales and filter qualities.
32class CC_EXPORT ImageDecodeControllerKey {
33 public:
34 static ImageDecodeControllerKey FromDrawImage(const DrawImage& image);
35
36 bool operator==(const ImageDecodeControllerKey& other) const {
37 // The image_id always has to be the same. However, after that all original
38 // decodes are the same, so if we can use the original decode, return true.
39 // If not, then we have to compare every field.
40 return image_id_ == other.image_id_ &&
41 can_use_original_decode_ == other.can_use_original_decode_ &&
42 (can_use_original_decode_ ||
43 (src_rect_ == other.src_rect_ &&
44 target_size_ == other.target_size_ &&
45 filter_quality_ == other.filter_quality_));
46 }
47
48 bool operator!=(const ImageDecodeControllerKey& other) const {
49 return !(*this == other);
50 }
51
52 uint32_t image_id() const { return image_id_; }
53 SkFilterQuality filter_quality() const { return filter_quality_; }
54 gfx::Rect src_rect() const { return src_rect_; }
55 gfx::Size target_size() const { return target_size_; }
56
57 bool can_use_original_decode() const { return can_use_original_decode_; }
58 size_t get_hash() const { return hash_; }
59
60 // Helper to figure out how much memory the locked image represented by this
61 // key would take.
62 size_t locked_bytes() const {
63 // TODO(vmpstr): Handle formats other than RGBA.
64 base::CheckedNumeric<size_t> result = 4;
65 result *= target_size_.width();
66 result *= target_size_.height();
67 return result.ValueOrDefault(std::numeric_limits<size_t>::max());
68 }
69
70 std::string ToString() const;
71
72 private:
73 ImageDecodeControllerKey(uint32_t image_id,
74 const gfx::Rect& src_rect,
75 const gfx::Size& size,
76 SkFilterQuality filter_quality,
77 bool can_use_original_decode);
78
79 uint32_t image_id_;
80 gfx::Rect src_rect_;
81 gfx::Size target_size_;
82 SkFilterQuality filter_quality_;
83 bool can_use_original_decode_;
84 size_t hash_;
85};
86
87// Hash function for the above ImageDecodeControllerKey.
88struct ImageDecodeControllerKeyHash {
89 size_t operator()(const ImageDecodeControllerKey& key) const {
90 return key.get_hash();
91 }
92};
93
94class CC_EXPORT SoftwareImageDecodeController : public ImageDecodeController {
95 public:
96 using ImageKey = ImageDecodeControllerKey;
97 using ImageKeyHash = ImageDecodeControllerKeyHash;
98
99 SoftwareImageDecodeController();
100 ~SoftwareImageDecodeController() override;
101
102 // ImageDecodeController overrides.
103 bool GetTaskForImageAndRef(const DrawImage& image,
104 uint64_t prepare_tiles_id,
105 scoped_refptr<ImageDecodeTask>* task) override;
106 void UnrefImage(const DrawImage& image) override;
107 DecodedDrawImage GetDecodedImageForDraw(const DrawImage& image) override;
108 void DrawWithImageFinished(const DrawImage& image,
109 const DecodedDrawImage& decoded_image) override;
110 void ReduceCacheUsage() override;
111
112 // Decode the given image and store it in the cache. This is only called by an
113 // image decode task from a worker thread.
114 void DecodeImage(const ImageKey& key, const DrawImage& image);
115
116 void RemovePendingTask(const ImageKey& key);
117
118 private:
119 // DecodedImage is a convenience storage for discardable memory. It can also
120 // construct an image out of SkImageInfo and stored discardable memory.
121 // TODO(vmpstr): Make this scoped_ptr.
122 class DecodedImage : public base::RefCounted<DecodedImage> {
123 public:
124 DecodedImage(const SkImageInfo& info,
125 scoped_ptr<base::DiscardableMemory> memory,
126 const SkSize& src_rect_offset);
127
128 SkImage* image() const {
129 DCHECK(locked_);
130 return image_.get();
131 }
132
133 const SkSize& src_rect_offset() const { return src_rect_offset_; }
134
135 bool is_locked() const { return locked_; }
136 bool Lock();
137 void Unlock();
138
139 private:
140 friend class base::RefCounted<DecodedImage>;
141
142 ~DecodedImage();
143
144 bool locked_;
145 SkImageInfo image_info_;
146 scoped_ptr<base::DiscardableMemory> memory_;
147 skia::RefPtr<SkImage> image_;
148 SkSize src_rect_offset_;
149 };
150
151 // MemoryBudget is a convenience class for memory bookkeeping and ensuring
152 // that we don't go over the limit when pre-decoding.
153 // TODO(vmpstr): Add memory infra to keep track of memory usage of this class.
154 class MemoryBudget {
155 public:
156 explicit MemoryBudget(size_t limit_bytes);
157
158 size_t AvailableMemoryBytes() const;
159 void AddUsage(size_t usage);
160 void SubtractUsage(size_t usage);
161 void ResetUsage();
162
163 private:
164 size_t GetCurrentUsageSafe() const;
165
166 size_t limit_bytes_;
167 base::CheckedNumeric<size_t> current_usage_bytes_;
168 };
169
vmpstr64cdba32016-03-03 00:38:40170 // Looks for the key in the cache and returns true if it was found and was
171 // successfully locked (or if it was already locked). Note that if this
172 // function returns true, then a ref count is increased for the image.
173 bool LockDecodedImageIfPossibleAndRef(const ImageKey& key);
174
175 // Actually decode the image. Note that this function can (and should) be
176 // called with no lock acquired, since it can do a lot of work. Note that it
177 // can also return nullptr to indicate the decode failed.
178 scoped_refptr<DecodedImage> DecodeImageInternal(const ImageKey& key,
179 const DrawImage& draw_image);
180
181 // Get the decoded draw image for the given key and draw_image. Note that this
182 // function has to be called with no lock acquired, since it will acquire its
183 // own locks and might call DecodeImageInternal above. Also note that this
184 // function will use the provided key, even if
185 // ImageKey::FromDrawImage(draw_image) would return a different key.
186 // Note that when used internally, we still require that
187 // DrawWithImageFinished() is called afterwards.
188 DecodedDrawImage GetDecodedImageForDrawInternal(const ImageKey& key,
189 const DrawImage& draw_image);
190
191 void SanityCheckState(int line, bool lock_acquired);
192 void RefImage(const ImageKey& key);
193 void RefAtRasterImage(const ImageKey& key);
194 void UnrefAtRasterImage(const ImageKey& key);
195
196 // These functions indicate whether the images can be handled and cached by
197 // ImageDecodeController or whether they will fall through to Skia (with
198 // exception of possibly prerolling them). Over time these should return
199 // "false" in less cases, as the ImageDecodeController should start handling
200 // more of them.
vmpstrf0348bbb2016-03-03 03:02:59201 bool CanHandleImage(const ImageKey& key);
vmpstr64cdba32016-03-03 00:38:40202
203 std::unordered_map<ImageKey, scoped_refptr<ImageDecodeTask>, ImageKeyHash>
204 pending_image_tasks_;
205
206 // The members below this comment can only be accessed if the lock is held to
207 // ensure that they are safe to access on multiple threads.
208 base::Lock lock_;
209
vmpstre37a753b2016-03-03 20:42:17210 using ImageMRUCache = base::HashingMRUCache<ImageKey,
211 scoped_refptr<DecodedImage>,
212 ImageKeyHash>;
213
214 // Decoded images and ref counts (predecode path).
215 ImageMRUCache decoded_images_;
vmpstr64cdba32016-03-03 00:38:40216 std::unordered_map<ImageKey, int, ImageKeyHash> decoded_images_ref_counts_;
vmpstre37a753b2016-03-03 20:42:17217
218 // Decoded image and ref counts (at-raster decode path).
219 ImageMRUCache at_raster_decoded_images_;
vmpstr64cdba32016-03-03 00:38:40220 std::unordered_map<ImageKey, int, ImageKeyHash>
221 at_raster_decoded_images_ref_counts_;
vmpstre37a753b2016-03-03 20:42:17222
vmpstr64cdba32016-03-03 00:38:40223 MemoryBudget locked_images_budget_;
224
225 // Note that this is used for cases where the only thing we do is preroll the
226 // image the first time we see it. This mimics the previous behavior and
227 // should over time change as the compositor starts to handle more cases.
228 std::unordered_set<uint32_t> prerolled_images_;
229};
230
231} // namespace cc
232
233#endif // CC_TILES_SOFTWARE_IMAGE_DECODE_CONTROLLER_H_