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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
cc / layers / recording_source_unittest.cc [blame]
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <array>
#include <vector>
#include "cc/base/region.h"
#include "cc/raster/raster_source.h"
#include "cc/test/fake_content_layer_client.h"
#include "cc/test/fake_recording_source.h"
#include "cc/test/skia_common.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/skia/include/core/SkRefCnt.h"
namespace cc {
namespace {
TEST(RecordingSourceTest, DiscardableImagesWithTransform) {
FakeRecordingSource recording_source(gfx::Size(256, 256));
std::array<std::array<PaintImage, 2>, 2> discardable_image;
gfx::Transform identity_transform;
discardable_image[0][0] = CreateDiscardablePaintImage(gfx::Size(32, 32));
// Translate transform is equivalent to moving using point.
gfx::Transform translate_transform;
translate_transform.Translate(0, 130);
discardable_image[1][0] = CreateDiscardablePaintImage(gfx::Size(32, 32));
// This moves the bitmap to center of viewport and rotate, this would make
// this bitmap in all four tile grids.
gfx::Transform rotate_transform;
rotate_transform.Translate(112, 112);
rotate_transform.Rotate(45);
discardable_image[1][1] = CreateDiscardablePaintImage(gfx::Size(32, 32));
recording_source.add_draw_image_with_transform(discardable_image[0][0],
identity_transform);
recording_source.add_draw_image_with_transform(discardable_image[1][0],
translate_transform);
recording_source.add_draw_image_with_transform(discardable_image[1][1],
rotate_transform);
recording_source.Rerecord();
scoped_refptr<RasterSource> raster_source =
recording_source.CreateRasterSource();
scoped_refptr<DiscardableImageMap> image_map =
raster_source->GetDisplayItemList()->GenerateDiscardableImageMap();
// Tile sized iterators. These should find only one pixel ref.
{
std::vector<const DrawImage*> images =
image_map->GetDiscardableImagesInRect(gfx::Rect(0, 0, 128, 128));
EXPECT_EQ(2u, images.size());
EXPECT_TRUE(
images[0]->paint_image().IsSameForTesting(discardable_image[0][0]));
EXPECT_TRUE(
images[1]->paint_image().IsSameForTesting(discardable_image[1][1]));
}
// Shifted tile sized iterators. These should find only one pixel ref.
{
std::vector<const DrawImage*> images =
image_map->GetDiscardableImagesInRect(gfx::Rect(130, 140, 128, 128));
EXPECT_EQ(1u, images.size());
EXPECT_TRUE(
images[0]->paint_image().IsSameForTesting(discardable_image[1][1]));
}
// The rotated bitmap would still be in the top right tile.
{
std::vector<const DrawImage*> images =
image_map->GetDiscardableImagesInRect(gfx::Rect(130, 0, 128, 128));
EXPECT_EQ(1u, images.size());
EXPECT_TRUE(
images[0]->paint_image().IsSameForTesting(discardable_image[1][1]));
}
// Layer sized iterators. These should find all pixel refs.
{
std::vector<const DrawImage*> images =
image_map->GetDiscardableImagesInRect(gfx::Rect(0, 0, 256, 256));
EXPECT_EQ(3u, images.size());
// Top left tile with bitmap[0][0] and bitmap[1][1].
EXPECT_TRUE(
images[0]->paint_image().IsSameForTesting(discardable_image[0][0]));
EXPECT_TRUE(
images[1]->paint_image().IsSameForTesting(discardable_image[1][0]));
EXPECT_TRUE(
images[2]->paint_image().IsSameForTesting(discardable_image[1][1]));
}
// Verify different raster scales
for (float scale = 1.f; scale <= 5.f; scale += 0.5f) {
std::vector<const DrawImage*> images =
image_map->GetDiscardableImagesInRect(gfx::Rect(130, 0, 128, 128));
DrawImage image(*images[0], scale, PaintImage::kDefaultFrameIndex,
TargetColorParams());
EXPECT_EQ(1u, images.size());
EXPECT_FLOAT_EQ(scale, image.scale().width());
EXPECT_FLOAT_EQ(scale, image.scale().height());
}
}
TEST(RecordingSourceTest, EmptyImages) {
FakeRecordingSource recording_source(gfx::Size(256, 256));
recording_source.Rerecord();
scoped_refptr<RasterSource> raster_source =
recording_source.CreateRasterSource();
scoped_refptr<DiscardableImageMap> image_map =
raster_source->GetDisplayItemList()->GenerateDiscardableImageMap();
// Tile sized iterators.
{
std::vector<const DrawImage*> images =
image_map->GetDiscardableImagesInRect(gfx::Rect(0, 0, 128, 128));
EXPECT_TRUE(images.empty());
}
// Shifted tile sized iterators.
{
std::vector<const DrawImage*> images =
image_map->GetDiscardableImagesInRect(gfx::Rect(140, 140, 128, 128));
EXPECT_TRUE(images.empty());
}
// Layer sized iterators.
{
std::vector<const DrawImage*> images =
image_map->GetDiscardableImagesInRect(gfx::Rect(0, 0, 256, 256));
EXPECT_TRUE(images.empty());
}
}
TEST(RecordingSourceTest, NoDiscardableImages) {
FakeRecordingSource recording_source(gfx::Size(256, 256));
PaintFlags simple_flags;
simple_flags.setColor(SkColorSetARGB(255, 12, 23, 34));
auto non_discardable_image =
CreateNonDiscardablePaintImage(gfx::Size(128, 128));
recording_source.add_draw_rect_with_flags(gfx::Rect(0, 0, 256, 256),
simple_flags);
recording_source.add_draw_rect_with_flags(gfx::Rect(128, 128, 512, 512),
simple_flags);
recording_source.add_draw_rect_with_flags(gfx::Rect(512, 0, 256, 256),
simple_flags);
recording_source.add_draw_rect_with_flags(gfx::Rect(0, 512, 256, 256),
simple_flags);
recording_source.add_draw_image(non_discardable_image, gfx::Point(128, 0));
recording_source.add_draw_image(non_discardable_image, gfx::Point(0, 128));
recording_source.add_draw_image(non_discardable_image, gfx::Point(150, 150));
recording_source.Rerecord();
scoped_refptr<RasterSource> raster_source =
recording_source.CreateRasterSource();
scoped_refptr<DiscardableImageMap> image_map =
raster_source->GetDisplayItemList()->GenerateDiscardableImageMap();
// Tile sized iterators.
{
std::vector<const DrawImage*> images =
image_map->GetDiscardableImagesInRect(gfx::Rect(0, 0, 128, 128));
EXPECT_TRUE(images.empty());
}
// Shifted tile sized iterators.
{
std::vector<const DrawImage*> images =
image_map->GetDiscardableImagesInRect(gfx::Rect(140, 140, 128, 128));
EXPECT_TRUE(images.empty());
}
// Layer sized iterators.
{
std::vector<const DrawImage*> images =
image_map->GetDiscardableImagesInRect(gfx::Rect(0, 0, 256, 256));
EXPECT_TRUE(images.empty());
}
}
TEST(RecordingSourceTest, DiscardableImages) {
FakeRecordingSource recording_source(gfx::Size(256, 256));
std::array<std::array<PaintImage, 2>, 2> discardable_image;
discardable_image[0][0] = CreateDiscardablePaintImage(gfx::Size(32, 32));
discardable_image[1][0] = CreateDiscardablePaintImage(gfx::Size(32, 32));
discardable_image[1][1] = CreateDiscardablePaintImage(gfx::Size(32, 32));
// Discardable images are found in the following cells:
// |---|---|
// | x | |
// |---|---|
// | x | x |
// |---|---|
recording_source.add_draw_image(discardable_image[0][0], gfx::Point(0, 0));
recording_source.add_draw_image(discardable_image[1][0], gfx::Point(0, 130));
recording_source.add_draw_image(discardable_image[1][1],
gfx::Point(140, 140));
recording_source.Rerecord();
scoped_refptr<RasterSource> raster_source =
recording_source.CreateRasterSource();
scoped_refptr<DiscardableImageMap> image_map =
raster_source->GetDisplayItemList()->GenerateDiscardableImageMap();
// Tile sized iterators. These should find only one image.
{
std::vector<const DrawImage*> images =
image_map->GetDiscardableImagesInRect(gfx::Rect(0, 0, 128, 128));
EXPECT_EQ(1u, images.size());
EXPECT_TRUE(
images[0]->paint_image().IsSameForTesting(discardable_image[0][0]));
}
// Shifted tile sized iterators. These should find only one image.
{
std::vector<const DrawImage*> images =
image_map->GetDiscardableImagesInRect(gfx::Rect(140, 140, 128, 128));
EXPECT_EQ(1u, images.size());
EXPECT_TRUE(
images[0]->paint_image().IsSameForTesting(discardable_image[1][1]));
}
// Ensure there's no discardable images in the empty cell
{
std::vector<const DrawImage*> images =
image_map->GetDiscardableImagesInRect(gfx::Rect(140, 0, 128, 128));
EXPECT_TRUE(images.empty());
}
// Layer sized iterators. These should find all 3 images.
{
std::vector<const DrawImage*> images =
image_map->GetDiscardableImagesInRect(gfx::Rect(0, 0, 256, 256));
EXPECT_EQ(3u, images.size());
EXPECT_TRUE(
images[0]->paint_image().IsSameForTesting(discardable_image[0][0]));
EXPECT_TRUE(
images[1]->paint_image().IsSameForTesting(discardable_image[1][0]));
EXPECT_TRUE(
images[2]->paint_image().IsSameForTesting(discardable_image[1][1]));
}
}
TEST(RecordingSourceTest, DiscardableImagesBaseNonDiscardable) {
FakeRecordingSource recording_source(gfx::Size(512, 512));
PaintImage non_discardable_image =
CreateNonDiscardablePaintImage(gfx::Size(512, 512));
std::array<std::array<PaintImage, 2>, 2> discardable_image;
discardable_image[0][0] = CreateDiscardablePaintImage(gfx::Size(128, 128));
discardable_image[0][1] = CreateDiscardablePaintImage(gfx::Size(128, 128));
discardable_image[1][1] = CreateDiscardablePaintImage(gfx::Size(128, 128));
// One large non-discardable image covers the whole grid.
// Discardable images are found in the following cells:
// |---|---|
// | x | x |
// |---|---|
// | | x |
// |---|---|
recording_source.add_draw_image(non_discardable_image, gfx::Point(0, 0));
recording_source.add_draw_image(discardable_image[0][0], gfx::Point(0, 0));
recording_source.add_draw_image(discardable_image[0][1], gfx::Point(260, 0));
recording_source.add_draw_image(discardable_image[1][1],
gfx::Point(260, 260));
recording_source.Rerecord();
scoped_refptr<RasterSource> raster_source =
recording_source.CreateRasterSource();
scoped_refptr<DiscardableImageMap> image_map =
raster_source->GetDisplayItemList()->GenerateDiscardableImageMap();
// Tile sized iterators. These should find only one image.
{
std::vector<const DrawImage*> images =
image_map->GetDiscardableImagesInRect(gfx::Rect(0, 0, 256, 256));
EXPECT_EQ(1u, images.size());
EXPECT_TRUE(
images[0]->paint_image().IsSameForTesting(discardable_image[0][0]));
}
// Shifted tile sized iterators. These should find only one image.
{
std::vector<const DrawImage*> images =
image_map->GetDiscardableImagesInRect(gfx::Rect(260, 260, 256, 256));
EXPECT_EQ(1u, images.size());
EXPECT_TRUE(
images[0]->paint_image().IsSameForTesting(discardable_image[1][1]));
}
// Ensure there's no discardable images in the empty cell
{
std::vector<const DrawImage*> images =
image_map->GetDiscardableImagesInRect(gfx::Rect(0, 256, 256, 256));
EXPECT_TRUE(images.empty());
}
// Layer sized iterators. These should find three images.
{
std::vector<const DrawImage*> images =
image_map->GetDiscardableImagesInRect(gfx::Rect(0, 0, 512, 512));
EXPECT_EQ(3u, images.size());
EXPECT_TRUE(
images[0]->paint_image().IsSameForTesting(discardable_image[0][0]));
EXPECT_TRUE(
images[1]->paint_image().IsSameForTesting(discardable_image[0][1]));
EXPECT_TRUE(
images[2]->paint_image().IsSameForTesting(discardable_image[1][1]));
}
}
TEST(RecordingSourceTest, AnalyzeIsSolid) {
gfx::Size layer_bounds(400, 400);
const std::vector<float> recording_scales = {1.f, 1.25f, 1.33f, 1.5f, 1.6f,
1.66f, 2.f, 2.25f, 2.5f};
for (float recording_scale : recording_scales) {
FakeRecordingSource recording_source(layer_bounds);
recording_source.SetRecordingScaleFactor(recording_scale);
PaintFlags solid_flags;
SkColor4f solid_color{0.1f, 0.2f, 0.3f, 1.0f};
solid_flags.setColor(solid_color);
SkColor4f non_solid_color{0.2f, 0.3f, 0.4f, 0.5f};
PaintFlags non_solid_flags;
non_solid_flags.setColor(non_solid_color);
recording_source.add_draw_rect_with_flags(
gfx::ScaleToEnclosingRect(gfx::Rect(layer_bounds), recording_scale),
solid_flags);
recording_source.Rerecord();
scoped_refptr<RasterSource> raster = recording_source.CreateRasterSource();
EXPECT_TRUE(raster->IsSolidColor())
<< " recording scale: " << recording_scale;
EXPECT_EQ(raster->GetSolidColor(), solid_color);
for (int y = 0; y < layer_bounds.height(); y += 50) {
for (int x = 0; x < layer_bounds.width(); x += 50) {
recording_source.reset_draws();
recording_source.add_draw_rect_with_flags(
gfx::ScaleToEnclosingRect(gfx::Rect(layer_bounds), recording_scale),
solid_flags);
recording_source.add_draw_rect_with_flags(
gfx::Rect(std::round(x * recording_scale),
std::round(y * recording_scale), 1, 1),
non_solid_flags);
recording_source.Rerecord();
raster = recording_source.CreateRasterSource();
EXPECT_FALSE(raster->IsSolidColor())
<< " recording scale: " << recording_scale << " pixel at: (" << x
<< ", " << y << ") was not solid.";
}
}
}
}
TEST(RecordingSourceTest, RecordedBounds) {
FakeRecordingSource recording_source(gfx::Size(400, 400));
recording_source.add_draw_rect(gfx::Rect(100, 100, 100, 100));
recording_source.add_draw_rect(gfx::Rect(50, 200, 200, 50));
recording_source.Rerecord();
auto raster = recording_source.CreateRasterSource();
EXPECT_EQ(gfx::Rect(50, 100, 200, 150), raster->recorded_bounds());
}
} // namespace
} // namespace cc