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