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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
cc / paint / solid_color_analyzer_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/paint/solid_color_analyzer.h"
#include <array>
#include <optional>
#include "base/memory/ref_counted.h"
#include "build/build_config.h"
#include "cc/paint/display_item_list.h"
#include "cc/paint/paint_filter.h"
#include "cc/paint/record_paint_canvas.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/gfx/geometry/skia_conversions.h"
namespace cc {
namespace {
class SolidColorAnalyzerTest : public testing::Test {
protected:
void TearDown() override { Reset(); }
void Reset() { canvas_.ReleaseAsRecord(); }
bool IsSolidColor(int max_ops_to_analyze = 1,
const gfx::Rect& rect = gfx::Rect(0, 0, 100, 100)) {
auto color = SolidColorAnalyzer::DetermineIfSolidColor(
canvas_.ReleaseAsRecord().buffer(), rect, max_ops_to_analyze, nullptr);
return !!color;
}
SkColor4f GetColor(int max_ops_to_analyze = 1,
const gfx::Rect rect = gfx::Rect(0, 0, 100, 100)) {
auto color = SolidColorAnalyzer::DetermineIfSolidColor(
canvas_.ReleaseAsRecord().buffer(), rect, max_ops_to_analyze, nullptr);
EXPECT_TRUE(color);
return color ? *color : SkColors::kTransparent;
}
RecordPaintCanvas canvas_;
private:
std::optional<SolidColorAnalyzer> analyzer_;
};
TEST_F(SolidColorAnalyzerTest, Empty) {
EXPECT_EQ(SkColors::kTransparent, GetColor());
}
TEST_F(SolidColorAnalyzerTest, ClearTransparent) {
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(0, 12, 34, 56));
canvas_.clear(color);
EXPECT_EQ(SkColors::kTransparent, GetColor());
}
TEST_F(SolidColorAnalyzerTest, ClearSolid) {
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(255, 65, 43, 21));
canvas_.clear(color);
EXPECT_EQ(color, GetColor());
}
TEST_F(SolidColorAnalyzerTest, ClearTranslucent) {
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(128, 11, 22, 33));
canvas_.clear(color);
#if BUILDFLAG(IS_MAC)
// TODO(andrescj): remove the special treatment of OS_MAC once
// https://crbug.com/922899 is fixed.
EXPECT_FALSE(IsSolidColor());
#else
EXPECT_EQ(color, GetColor());
#endif // BUILDFLAG(IS_MAC)
}
TEST_F(SolidColorAnalyzerTest, DrawColor) {
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(255, 11, 22, 33));
canvas_.drawColor(color);
EXPECT_EQ(color, GetColor());
}
TEST_F(SolidColorAnalyzerTest, DrawOval) {
PaintFlags flags;
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(255, 11, 22, 33));
flags.setColor(color);
canvas_.drawOval(SkRect::MakeWH(100, 100), flags);
EXPECT_FALSE(IsSolidColor());
}
TEST_F(SolidColorAnalyzerTest, DrawRect) {
PaintFlags flags;
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(255, 11, 22, 33));
flags.setColor(color);
SkRect rect = SkRect::MakeWH(200, 200);
canvas_.clipRect(rect, SkClipOp::kIntersect, false);
canvas_.drawRect(rect, flags);
EXPECT_EQ(color, GetColor());
}
// TODO(vmpstr): Generalize the DrawRect test cases so that we can test both
// Rect and RRect.
TEST_F(SolidColorAnalyzerTest, DrawRRect) {
SkRect rect = SkRect::MakeWH(200, 200);
SkRRect rrect;
rrect.setRectXY(rect, 5, 5);
PaintFlags flags;
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(255, 11, 22, 33));
flags.setColor(color);
canvas_.drawRRect(rrect, flags);
EXPECT_EQ(color, GetColor(1, gfx::Rect(5, 5, 190, 190)));
}
TEST_F(SolidColorAnalyzerTest, DrawRectClipped) {
PaintFlags flags;
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(255, 11, 22, 33));
flags.setColor(color);
SkRect rect = SkRect::MakeWH(200, 200);
canvas_.clipRect(SkRect::MakeWH(50, 50), SkClipOp::kIntersect, false);
canvas_.drawRect(rect, flags);
EXPECT_FALSE(IsSolidColor());
}
TEST_F(SolidColorAnalyzerTest, DrawRectClippedDifference) {
PaintFlags flags;
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(255, 11, 22, 33));
flags.setColor(color);
SkRect drawRect = SkRect::MakeWH(200, 200);
canvas_.clipRect(drawRect, SkClipOp::kIntersect, false);
SkRect differenceRect = SkRect::MakeXYWH(50, 50, 200, 200);
// Using difference should always make this fail.
canvas_.clipRect(differenceRect, SkClipOp::kDifference, false);
canvas_.drawRect(drawRect, flags);
EXPECT_FALSE(IsSolidColor());
}
TEST_F(SolidColorAnalyzerTest, DrawRectWithTranslateNotSolid) {
PaintFlags flags;
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(255, 11, 22, 33));
flags.setColor(color);
SkRect rect = SkRect::MakeWH(100, 100);
canvas_.translate(1, 1);
canvas_.drawRect(rect, flags);
EXPECT_FALSE(IsSolidColor());
}
TEST_F(SolidColorAnalyzerTest, DrawRectWithTranslateSolid) {
PaintFlags flags;
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(255, 11, 22, 33));
flags.setColor(color);
SkRect rect = SkRect::MakeWH(101, 101);
canvas_.translate(1, 1);
canvas_.drawRect(rect, flags);
EXPECT_FALSE(IsSolidColor());
}
TEST_F(SolidColorAnalyzerTest, TwoOpsNotSolid) {
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(255, 65, 43, 21));
canvas_.clear(color);
canvas_.clear(color);
EXPECT_FALSE(IsSolidColor());
}
TEST_F(SolidColorAnalyzerTest, DrawRectBlendModeClear) {
PaintFlags flags;
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(255, 11, 22, 33));
flags.setColor(color);
flags.setBlendMode(SkBlendMode::kClear);
SkRect rect = SkRect::MakeWH(200, 200);
canvas_.drawRect(rect, flags);
EXPECT_EQ(SkColors::kTransparent, GetColor());
}
TEST_F(SolidColorAnalyzerTest, DrawRectBlendModeSrcOver) {
PaintFlags flags;
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(255, 11, 22, 33));
flags.setColor(color);
flags.setBlendMode(SkBlendMode::kSrcOver);
SkRect rect = SkRect::MakeWH(200, 200);
canvas_.drawRect(rect, flags);
EXPECT_EQ(color, GetColor());
}
TEST_F(SolidColorAnalyzerTest, DrawRectRotated) {
PaintFlags flags;
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(255, 11, 22, 33));
flags.setColor(color);
SkRect rect = SkRect::MakeWH(200, 200);
canvas_.rotate(50);
canvas_.drawRect(rect, flags);
EXPECT_FALSE(IsSolidColor());
}
TEST_F(SolidColorAnalyzerTest, DrawRectScaledNotSolid) {
PaintFlags flags;
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(255, 11, 22, 33));
flags.setColor(color);
SkRect rect = SkRect::MakeWH(200, 200);
canvas_.scale(0.1f, 0.1f);
canvas_.drawRect(rect, flags);
EXPECT_FALSE(IsSolidColor());
}
TEST_F(SolidColorAnalyzerTest, DrawRectScaledSolid) {
PaintFlags flags;
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(255, 11, 22, 33));
flags.setColor(color);
SkRect rect = SkRect::MakeWH(10, 10);
canvas_.scale(10, 10);
canvas_.drawRect(rect, flags);
EXPECT_EQ(color, GetColor());
}
TEST_F(SolidColorAnalyzerTest, DrawRectFilterPaint) {
PaintFlags flags;
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(255, 11, 22, 33));
flags.setColor(color);
flags.setImageFilter(sk_make_sp<OffsetPaintFilter>(10, 10, nullptr));
SkRect rect = SkRect::MakeWH(200, 200);
canvas_.drawRect(rect, flags);
EXPECT_FALSE(IsSolidColor());
}
TEST_F(SolidColorAnalyzerTest, DrawRectClipPath) {
PaintFlags flags;
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(255, 11, 22, 33));
flags.setColor(color);
SkPath path;
path.moveTo(0, 0);
path.lineTo(128, 50);
path.lineTo(255, 0);
path.lineTo(255, 255);
path.lineTo(0, 255);
SkRect rect = SkRect::MakeWH(200, 200);
canvas_.clipPath(path, SkClipOp::kIntersect);
canvas_.drawRect(rect, flags);
EXPECT_FALSE(IsSolidColor());
}
TEST_F(SolidColorAnalyzerTest, DrawRectTranslucent) {
PaintFlags flags;
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(128, 128, 0, 0));
flags.setColor(color);
SkRect rect = SkRect::MakeWH(100, 100);
canvas_.drawRect(rect, flags);
#if BUILDFLAG(IS_MAC)
// TODO(andrescj): remove the special treatment of OS_MAC once
// https://crbug.com/922899 is fixed.
EXPECT_FALSE(IsSolidColor());
#else
EXPECT_EQ(color, GetColor());
#endif // BUILDFLAG(IS_MAC)
}
TEST_F(SolidColorAnalyzerTest, DrawRectTranslucentOverNonSolid) {
PaintFlags flags;
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(255, 128, 0, 0));
flags.setColor(color);
SkRect rect = SkRect::MakeWH(100, 50);
canvas_.drawRect(rect, flags);
color = SkColor4f::FromColor(SkColorSetARGB(128, 0, 128, 0));
flags.setColor(color);
rect = SkRect::MakeWH(100, 100);
canvas_.drawRect(rect, flags);
EXPECT_FALSE(IsSolidColor(2 /* max_ops_to_analyze */));
}
TEST_F(SolidColorAnalyzerTest, DrawRectOpaqueOccludesNonSolid) {
PaintFlags flags;
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(255, 128, 0, 0));
flags.setColor(color);
SkRect rect = SkRect::MakeWH(100, 50);
canvas_.drawRect(rect, flags);
color = SkColor4f::FromColor(SkColorSetARGB(255, 0, 128, 0));
flags.setColor(color);
rect = SkRect::MakeWH(100, 100);
canvas_.drawRect(rect, flags);
EXPECT_EQ(color, GetColor(2 /* max_ops_to_analyze */));
}
TEST_F(SolidColorAnalyzerTest, DrawRectSolidWithSrcOverBlending) {
PaintFlags flags;
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(64, 40, 50, 60));
flags.setColor(color);
SkRect rect = SkRect::MakeWH(100, 100);
canvas_.drawRect(rect, flags);
color = SkColor4f::FromColor(SkColorSetARGB(128, 10, 20, 30));
flags.setColor(color);
rect = SkRect::MakeWH(100, 100);
canvas_.drawRect(rect, flags);
#if BUILDFLAG(IS_MAC)
// TODO(andrescj): remove the special treatment of OS_MAC once
// https://crbug.com/922899 is fixed.
EXPECT_FALSE(IsSolidColor());
#else
// Float precision makes testing for equality a pain, so we'll just use ints
EXPECT_EQ(SkColorSetARGB(160, 16, 26, 36),
GetColor(2 /* max_ops_to_analyze */).toSkColor());
#endif // BUILDFLAG(IS_MAC)
}
TEST_F(SolidColorAnalyzerTest, SaveLayer) {
PaintFlags flags;
SkColor4f color = SkColor4f::FromColor(SkColorSetARGB(255, 11, 22, 33));
flags.setColor(color);
canvas_.saveLayer(SkRect::MakeWH(200, 200), flags);
EXPECT_FALSE(IsSolidColor());
}
TEST_F(SolidColorAnalyzerTest, ClipRRectCoversCanvas) {
SkVector radii[4] = {
SkVector::Make(10.0, 15.0), SkVector::Make(20.0, 25.0),
SkVector::Make(30.0, 35.0), SkVector::Make(40.0, 45.0),
};
SkVector radii_scale[4] = {
SkVector::Make(100.0, 150.0), SkVector::Make(200.0, 250.0),
SkVector::Make(300.0, 350.0), SkVector::Make(400.0, 450.0),
};
int rr_size = 600;
int canvas_size = 255;
gfx::Rect canvas_rect(canvas_size, canvas_size);
PaintFlags flags;
flags.setColor(SkColors::kWhite);
struct Cases {
SkVector offset;
SkVector offset_scale;
bool expected;
};
auto cases = std::to_array<Cases>({
// Not within bounding box of |rr|.
{SkVector::Make(100, 100), SkVector::Make(100, 100), false},
// Intersects UL corner.
{SkVector::Make(0, 0), SkVector::Make(0, 0), false},
// Between UL and UR.
{SkVector::Make(-50, 0), SkVector::Make(-50, -15), true},
// Intersects UR corner.
{SkVector::Make(canvas_size - rr_size, 0),
SkVector::Make(canvas_size - rr_size, 0), false},
// Between UR and LR.
{SkVector::Make(canvas_size - rr_size, -50), SkVector::Make(-305, -80),
true},
// Intersects LR corner.
{SkVector::Make(canvas_size - rr_size, canvas_size - rr_size),
SkVector::Make(canvas_size - rr_size, canvas_size - rr_size), false},
// Between LL and LR
{SkVector::Make(-50, canvas_size - rr_size), SkVector::Make(-205, -310),
true},
// Intersects LL corner
{SkVector::Make(0, canvas_size - rr_size),
SkVector::Make(0, canvas_size - rr_size), false},
// Between UL and LL
{SkVector::Make(0, -50), SkVector::Make(-15, -60), true},
// In center
{SkVector::Make(-100, -100), SkVector::Make(-100, -100), true},
});
for (int case_scale = 0; case_scale < 2; ++case_scale) {
bool scaled = case_scale > 0;
for (size_t i = 0; i < std::size(cases); ++i) {
Reset();
SkRect bounding_rect = SkRect::MakeXYWH(
scaled ? cases[i].offset_scale.x() : cases[i].offset.x(),
scaled ? cases[i].offset_scale.y() : cases[i].offset.y(), rr_size,
rr_size);
SkRRect rr;
rr.setRectRadii(bounding_rect, scaled ? radii_scale : radii);
canvas_.clipRRect(rr, SkClipOp::kIntersect, false);
canvas_.drawRect(RectToSkRect(canvas_rect), flags);
EXPECT_EQ(cases[i].expected, IsSolidColor(1, canvas_rect))
<< "Case " << i << ", " << scaled << " failed.";
}
}
}
} // namespace
} // namespace cc