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
cc / raster / playback_image_provider_unittest.cc [blame]
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "cc/raster/playback_image_provider.h"
#include <utility>
#include <vector>
#include "cc/paint/paint_image_builder.h"
#include "cc/test/skia_common.h"
#include "cc/test/stub_decode_cache.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "third_party/skia/include/core/SkImage.h"
#include "third_party/skia/include/core/SkM44.h"
#include "third_party/skia/include/core/SkRect.h"
#include "third_party/skia/include/core/SkRefCnt.h"
#include "third_party/skia/include/core/SkSize.h"
#include "third_party/skia/include/gpu/ganesh/GrDirectContext.h"
#include "third_party/skia/include/gpu/ganesh/gl/GrGLInterface.h"
namespace cc {
namespace {
sk_sp<SkImage> CreateRasterImage() {
SkBitmap bitmap;
bitmap.allocN32Pixels(10, 10);
return SkImages::RasterFromBitmap(bitmap);
}
DecodedDrawImage CreateDecode() {
return DecodedDrawImage(CreateRasterImage(), nullptr, SkSize::MakeEmpty(),
SkSize::Make(1.0f, 1.0f),
PaintFlags::FilterQuality::kMedium, true);
}
class MockDecodeCache : public StubDecodeCache {
public:
MockDecodeCache() = default;
~MockDecodeCache() override { EXPECT_EQ(refed_image_count_, 0); }
DecodedDrawImage GetDecodedImageForDraw(
const DrawImage& draw_image) override {
last_image_ = draw_image;
images_decoded_++;
refed_image_count_++;
return CreateDecode();
}
void DrawWithImageFinished(
const DrawImage& draw_image,
const DecodedDrawImage& decoded_draw_image) override {
refed_image_count_--;
EXPECT_GE(refed_image_count_, 0);
}
bool UseCacheForDrawImage(const DrawImage& image) const override {
return use_cache_for_draw_image_;
}
void set_use_cache_for_draw_image(bool use) {
use_cache_for_draw_image_ = use;
}
int refed_image_count() const { return refed_image_count_; }
int images_decoded() const { return images_decoded_; }
const DrawImage& last_image() { return last_image_; }
private:
int refed_image_count_ = 0;
int images_decoded_ = 0;
bool use_cache_for_draw_image_ = true;
DrawImage last_image_;
};
TEST(PlaybackImageProviderTest, SkipsAllImages) {
MockDecodeCache cache;
PlaybackImageProvider provider(&cache, TargetColorParams(), std::nullopt);
SkIRect rect = SkIRect::MakeWH(10, 10);
SkM44 matrix = SkM44();
EXPECT_FALSE(provider.GetRasterContent(DrawImage(
PaintImageBuilder::WithDefault()
.set_id(PaintImage::GetNextId())
.set_image(CreateRasterImage(), PaintImage::GetNextContentId())
.TakePaintImage(),
false, rect, PaintFlags::FilterQuality::kMedium, matrix)));
EXPECT_EQ(cache.images_decoded(), 0);
EXPECT_FALSE(provider.GetRasterContent(
CreateDiscardableDrawImage(gfx::Size(10, 10), nullptr, SkRect::Make(rect),
PaintFlags::FilterQuality::kMedium, matrix)));
EXPECT_EQ(cache.images_decoded(), 0);
}
TEST(PlaybackImageProviderTest, SkipsSomeImages) {
MockDecodeCache cache;
PaintImage skip_image = CreateDiscardablePaintImage(gfx::Size(10, 10));
std::optional<PlaybackImageProvider::Settings> settings;
settings.emplace();
settings->images_to_skip = {skip_image.stable_id()};
PlaybackImageProvider provider(&cache, TargetColorParams(),
std::move(settings));
SkIRect rect = SkIRect::MakeWH(10, 10);
SkM44 matrix = SkM44();
EXPECT_FALSE(provider.GetRasterContent(DrawImage(
skip_image, false, rect, PaintFlags::FilterQuality::kMedium, matrix)));
EXPECT_EQ(cache.images_decoded(), 0);
}
TEST(PlaybackImageProviderTest, RefAndUnrefDecode) {
MockDecodeCache cache;
std::optional<PlaybackImageProvider::Settings> settings;
settings.emplace();
PlaybackImageProvider provider(&cache, TargetColorParams(),
std::move(settings));
{
SkRect rect = SkRect::MakeWH(10, 10);
SkM44 matrix = SkM44();
auto decode = provider.GetRasterContent(
CreateDiscardableDrawImage(gfx::Size(10, 10), nullptr, rect,
PaintFlags::FilterQuality::kMedium, matrix));
EXPECT_TRUE(decode);
EXPECT_EQ(cache.refed_image_count(), 1);
}
// Destroying the decode unrefs the image from the cache.
EXPECT_EQ(cache.refed_image_count(), 0);
}
TEST(PlaybackImageProviderTest, SwapsGivenFrames) {
MockDecodeCache cache;
std::vector<FrameMetadata> frames = {
FrameMetadata(true, base::Milliseconds(2)),
FrameMetadata(true, base::Milliseconds(3))};
PaintImage image = CreateAnimatedImage(gfx::Size(10, 10), frames);
base::flat_map<PaintImage::Id, size_t> image_to_frame;
image_to_frame[image.stable_id()] = 1u;
std::optional<PlaybackImageProvider::Settings> settings;
settings.emplace();
settings->image_to_current_frame_index = image_to_frame;
PlaybackImageProvider provider(&cache, TargetColorParams(),
std::move(settings));
SkIRect rect = SkIRect::MakeWH(10, 10);
SkM44 matrix = SkM44();
DrawImage draw_image(image, false, rect, PaintFlags::FilterQuality::kMedium,
matrix);
provider.GetRasterContent(draw_image);
ASSERT_TRUE(cache.last_image().paint_image());
ASSERT_TRUE(cache.last_image().paint_image().IsSameForTesting(image));
ASSERT_EQ(cache.last_image().frame_index(), 1u);
}
TEST(PlaybackImageProviderTest, BitmapImages) {
MockDecodeCache cache;
std::optional<PlaybackImageProvider::Settings> settings;
settings.emplace();
PlaybackImageProvider provider(&cache, TargetColorParams(),
std::move(settings));
{
SkIRect rect = SkIRect::MakeWH(10, 10);
SkM44 matrix = SkM44();
auto draw_image =
DrawImage(CreateBitmapImage(gfx::Size(10, 10)), false, rect,
PaintFlags::FilterQuality::kMedium, matrix);
auto decode = provider.GetRasterContent(draw_image);
EXPECT_TRUE(decode);
EXPECT_EQ(cache.refed_image_count(), 1);
}
// Destroying the decode unrefs the image from the cache.
EXPECT_EQ(cache.refed_image_count(), 0);
}
TEST(PlaybackImageProviderTest, IgnoresImagesNotSupportedByCache) {
MockDecodeCache cache;
cache.set_use_cache_for_draw_image(false);
std::optional<PlaybackImageProvider::Settings> settings;
settings.emplace();
PlaybackImageProvider provider(&cache, TargetColorParams(),
std::move(settings));
{
SkIRect rect = SkIRect::MakeWH(10, 10);
SkM44 matrix = SkM44();
auto draw_image =
DrawImage(CreateBitmapImage(gfx::Size(10, 10)), false, rect,
PaintFlags::FilterQuality::kMedium, matrix);
auto decode = provider.GetRasterContent(draw_image);
EXPECT_TRUE(decode);
EXPECT_EQ(cache.refed_image_count(), 0);
}
EXPECT_EQ(cache.refed_image_count(), 0);
}
} // namespace
} // namespace cc