blob: 9697720fb3dcec73d1accc7db5ef0ab02eb29fe3 [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
danakj60bc3bc2016-04-09 00:24:4810#include <memory>
vmpstr64cdba32016-03-03 00:38:4011#include <unordered_map>
12#include <unordered_set>
13
ericrk05ce07fc2016-04-08 21:48:5914#include "base/atomic_sequence_num.h"
vmpstre37a753b2016-03-03 20:42:1715#include "base/containers/mru_cache.h"
vmpstr64cdba32016-03-03 00:38:4016#include "base/hash.h"
17#include "base/memory/discardable_memory_allocator.h"
18#include "base/memory/ref_counted.h"
19#include "base/numerics/safe_math.h"
20#include "base/threading/thread_checker.h"
ericrk05ce07fc2016-04-08 21:48:5921#include "base/trace_event/memory_dump_provider.h"
vmpstr64cdba32016-03-03 00:38:4022#include "cc/base/cc_export.h"
23#include "cc/playback/decoded_draw_image.h"
24#include "cc/playback/draw_image.h"
25#include "cc/raster/tile_task_runner.h"
26#include "cc/tiles/image_decode_controller.h"
27#include "skia/ext/refptr.h"
28
29namespace cc {
30
31// ImageDecodeControllerKey is a class that gets a cache key out of a given draw
32// image. That is, this key uniquely identifies an image in the cache. Note that
33// it's insufficient to use SkImage's unique id, since the same image can appear
34// in the cache multiple times at different scales and filter qualities.
35class CC_EXPORT ImageDecodeControllerKey {
36 public:
37 static ImageDecodeControllerKey FromDrawImage(const DrawImage& image);
38
vmpstrbf0d713a2016-03-24 20:22:5439 ImageDecodeControllerKey(const ImageDecodeControllerKey& other);
40
vmpstr64cdba32016-03-03 00:38:4041 bool operator==(const ImageDecodeControllerKey& other) const {
42 // The image_id always has to be the same. However, after that all original
43 // decodes are the same, so if we can use the original decode, return true.
44 // If not, then we have to compare every field.
45 return image_id_ == other.image_id_ &&
46 can_use_original_decode_ == other.can_use_original_decode_ &&
47 (can_use_original_decode_ ||
48 (src_rect_ == other.src_rect_ &&
49 target_size_ == other.target_size_ &&
50 filter_quality_ == other.filter_quality_));
51 }
52
53 bool operator!=(const ImageDecodeControllerKey& other) const {
54 return !(*this == other);
55 }
56
57 uint32_t image_id() const { return image_id_; }
58 SkFilterQuality filter_quality() const { return filter_quality_; }
59 gfx::Rect src_rect() const { return src_rect_; }
60 gfx::Size target_size() const { return target_size_; }
61
62 bool can_use_original_decode() const { return can_use_original_decode_; }
63 size_t get_hash() const { return hash_; }
64
65 // Helper to figure out how much memory the locked image represented by this
66 // key would take.
67 size_t locked_bytes() const {
68 // TODO(vmpstr): Handle formats other than RGBA.
69 base::CheckedNumeric<size_t> result = 4;
70 result *= target_size_.width();
71 result *= target_size_.height();
72 return result.ValueOrDefault(std::numeric_limits<size_t>::max());
73 }
74
75 std::string ToString() const;
76
77 private:
78 ImageDecodeControllerKey(uint32_t image_id,
79 const gfx::Rect& src_rect,
80 const gfx::Size& size,
81 SkFilterQuality filter_quality,
82 bool can_use_original_decode);
83
84 uint32_t image_id_;
85 gfx::Rect src_rect_;
86 gfx::Size target_size_;
87 SkFilterQuality filter_quality_;
88 bool can_use_original_decode_;
89 size_t hash_;
90};
91
92// Hash function for the above ImageDecodeControllerKey.
93struct ImageDecodeControllerKeyHash {
94 size_t operator()(const ImageDecodeControllerKey& key) const {
95 return key.get_hash();
96 }
97};
98
ericrk05ce07fc2016-04-08 21:48:5999class CC_EXPORT SoftwareImageDecodeController
100 : public ImageDecodeController,
101 public base::trace_event::MemoryDumpProvider {
vmpstr64cdba32016-03-03 00:38:40102 public:
103 using ImageKey = ImageDecodeControllerKey;
104 using ImageKeyHash = ImageDecodeControllerKeyHash;
105
vmpstrda84a062016-03-21 22:02:56106 explicit SoftwareImageDecodeController(ResourceFormat format);
107 SoftwareImageDecodeController();
vmpstr64cdba32016-03-03 00:38:40108 ~SoftwareImageDecodeController() override;
109
110 // ImageDecodeController overrides.
111 bool GetTaskForImageAndRef(const DrawImage& image,
112 uint64_t prepare_tiles_id,
113 scoped_refptr<ImageDecodeTask>* task) override;
114 void UnrefImage(const DrawImage& image) override;
115 DecodedDrawImage GetDecodedImageForDraw(const DrawImage& image) override;
116 void DrawWithImageFinished(const DrawImage& image,
117 const DecodedDrawImage& decoded_image) override;
118 void ReduceCacheUsage() override;
ericrkd2ff2a132016-04-11 22:16:00119 // Software doesn't keep outstanding images pinned, so this is a no-op.
120 void SetShouldAggressivelyFreeResources(
121 bool aggressively_free_resources) override {}
vmpstr64cdba32016-03-03 00:38:40122
123 // Decode the given image and store it in the cache. This is only called by an
124 // image decode task from a worker thread.
125 void DecodeImage(const ImageKey& key, const DrawImage& image);
126
127 void RemovePendingTask(const ImageKey& key);
128
ericrk05ce07fc2016-04-08 21:48:59129 // MemoryDumpProvider overrides.
130 bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
131 base::trace_event::ProcessMemoryDump* pmd) override;
132
vmpstr64cdba32016-03-03 00:38:40133 private:
134 // DecodedImage is a convenience storage for discardable memory. It can also
135 // construct an image out of SkImageInfo and stored discardable memory.
vmpstr61c1b9d2016-03-11 23:55:38136 class DecodedImage {
vmpstr64cdba32016-03-03 00:38:40137 public:
138 DecodedImage(const SkImageInfo& info,
danakj60bc3bc2016-04-09 00:24:48139 std::unique_ptr<base::DiscardableMemory> memory,
ericrk05ce07fc2016-04-08 21:48:59140 const SkSize& src_rect_offset,
141 uint64_t tracing_id);
vmpstr61c1b9d2016-03-11 23:55:38142 ~DecodedImage();
vmpstr64cdba32016-03-03 00:38:40143
144 SkImage* image() const {
145 DCHECK(locked_);
146 return image_.get();
147 }
148
149 const SkSize& src_rect_offset() const { return src_rect_offset_; }
150
151 bool is_locked() const { return locked_; }
152 bool Lock();
153 void Unlock();
154
ericrk05ce07fc2016-04-08 21:48:59155 const base::DiscardableMemory* memory() const { return memory_.get(); }
156
157 // An ID which uniquely identifies this DecodedImage within the image decode
158 // controller. Used in memory tracing.
159 uint64_t tracing_id() const { return tracing_id_; }
160
vmpstr64cdba32016-03-03 00:38:40161 private:
vmpstr64cdba32016-03-03 00:38:40162 bool locked_;
163 SkImageInfo image_info_;
danakj60bc3bc2016-04-09 00:24:48164 std::unique_ptr<base::DiscardableMemory> memory_;
vmpstr64cdba32016-03-03 00:38:40165 skia::RefPtr<SkImage> image_;
166 SkSize src_rect_offset_;
ericrk05ce07fc2016-04-08 21:48:59167 uint64_t tracing_id_;
vmpstr64cdba32016-03-03 00:38:40168 };
169
170 // MemoryBudget is a convenience class for memory bookkeeping and ensuring
171 // that we don't go over the limit when pre-decoding.
vmpstr64cdba32016-03-03 00:38:40172 class MemoryBudget {
173 public:
174 explicit MemoryBudget(size_t limit_bytes);
175
176 size_t AvailableMemoryBytes() const;
177 void AddUsage(size_t usage);
178 void SubtractUsage(size_t usage);
179 void ResetUsage();
180
181 private:
182 size_t GetCurrentUsageSafe() const;
183
184 size_t limit_bytes_;
185 base::CheckedNumeric<size_t> current_usage_bytes_;
186 };
187
danakj60bc3bc2016-04-09 00:24:48188 using ImageMRUCache = base::HashingMRUCache<ImageKey,
189 std::unique_ptr<DecodedImage>,
190 ImageKeyHash>;
ericrk05ce07fc2016-04-08 21:48:59191
vmpstr64cdba32016-03-03 00:38:40192 // Looks for the key in the cache and returns true if it was found and was
193 // successfully locked (or if it was already locked). Note that if this
194 // function returns true, then a ref count is increased for the image.
195 bool LockDecodedImageIfPossibleAndRef(const ImageKey& key);
196
197 // Actually decode the image. Note that this function can (and should) be
198 // called with no lock acquired, since it can do a lot of work. Note that it
199 // can also return nullptr to indicate the decode failed.
danakj60bc3bc2016-04-09 00:24:48200 std::unique_ptr<DecodedImage> DecodeImageInternal(
201 const ImageKey& key,
202 const DrawImage& draw_image);
vmpstr64cdba32016-03-03 00:38:40203
204 // Get the decoded draw image for the given key and draw_image. Note that this
205 // function has to be called with no lock acquired, since it will acquire its
206 // own locks and might call DecodeImageInternal above. Also note that this
207 // function will use the provided key, even if
208 // ImageKey::FromDrawImage(draw_image) would return a different key.
209 // Note that when used internally, we still require that
210 // DrawWithImageFinished() is called afterwards.
211 DecodedDrawImage GetDecodedImageForDrawInternal(const ImageKey& key,
212 const DrawImage& draw_image);
213
cblume4c4a73d712016-04-14 21:18:45214 // GetOriginalImageDecode is called by DecodeImageInternal when the quality
215 // does not scale the image. Like DecodeImageInternal, it should be called
216 // with no lock acquired and it returns nullptr if the decoding failed.
217 std::unique_ptr<DecodedImage> GetOriginalImageDecode(const ImageKey& key,
218 const SkImage& image);
219
220 // GetScaledImageDecode is called by DecodeImageInternal when the quality
221 // requires the image be scaled. Like DecodeImageInternal, it should be
222 // called with no lock acquired and it returns nullptr if the decoding or
223 // scaling failed.
224 std::unique_ptr<DecodedImage> GetScaledImageDecode(const ImageKey& key,
225 const SkImage& image);
226
vmpstr64cdba32016-03-03 00:38:40227 void SanityCheckState(int line, bool lock_acquired);
228 void RefImage(const ImageKey& key);
229 void RefAtRasterImage(const ImageKey& key);
230 void UnrefAtRasterImage(const ImageKey& key);
231
232 // These functions indicate whether the images can be handled and cached by
233 // ImageDecodeController or whether they will fall through to Skia (with
234 // exception of possibly prerolling them). Over time these should return
235 // "false" in less cases, as the ImageDecodeController should start handling
236 // more of them.
vmpstrf0348bbb2016-03-03 03:02:59237 bool CanHandleImage(const ImageKey& key);
vmpstr64cdba32016-03-03 00:38:40238
ericrk05ce07fc2016-04-08 21:48:59239 // Helper function which dumps all images in a specific ImageMRUCache.
240 void DumpImageMemoryForCache(const ImageMRUCache& cache,
241 const char* cache_name,
242 base::trace_event::ProcessMemoryDump* pmd) const;
243
vmpstr64cdba32016-03-03 00:38:40244 std::unordered_map<ImageKey, scoped_refptr<ImageDecodeTask>, ImageKeyHash>
245 pending_image_tasks_;
246
247 // The members below this comment can only be accessed if the lock is held to
248 // ensure that they are safe to access on multiple threads.
249 base::Lock lock_;
250
vmpstre37a753b2016-03-03 20:42:17251 // Decoded images and ref counts (predecode path).
252 ImageMRUCache decoded_images_;
vmpstr64cdba32016-03-03 00:38:40253 std::unordered_map<ImageKey, int, ImageKeyHash> decoded_images_ref_counts_;
vmpstre37a753b2016-03-03 20:42:17254
255 // Decoded image and ref counts (at-raster decode path).
256 ImageMRUCache at_raster_decoded_images_;
vmpstr64cdba32016-03-03 00:38:40257 std::unordered_map<ImageKey, int, ImageKeyHash>
258 at_raster_decoded_images_ref_counts_;
vmpstre37a753b2016-03-03 20:42:17259
vmpstr64cdba32016-03-03 00:38:40260 MemoryBudget locked_images_budget_;
261
262 // Note that this is used for cases where the only thing we do is preroll the
263 // image the first time we see it. This mimics the previous behavior and
264 // should over time change as the compositor starts to handle more cases.
265 std::unordered_set<uint32_t> prerolled_images_;
bashi0c87a7ea2016-03-18 01:05:20266
267 ResourceFormat format_;
ericrk05ce07fc2016-04-08 21:48:59268
269 // Used to uniquely identify DecodedImages for memory traces.
270 base::AtomicSequenceNumber next_tracing_id_;
vmpstr64cdba32016-03-03 00:38:40271};
272
273} // namespace cc
274
275#endif // CC_TILES_SOFTWARE_IMAGE_DECODE_CONTROLLER_H_