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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
|
// Copyright 2013 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.
#include "cc/layers/picture_image_layer.h"
#include "cc/layers/solid_color_layer.h"
#include "cc/test/layer_tree_pixel_resource_test.h"
#include "cc/test/pixel_comparator.h"
#if !defined(OS_ANDROID)
namespace cc {
namespace {
SkXfermode::Mode const kBlendModes[] = {
SkXfermode::kSrcOver_Mode, SkXfermode::kScreen_Mode,
SkXfermode::kOverlay_Mode, SkXfermode::kDarken_Mode,
SkXfermode::kLighten_Mode, SkXfermode::kColorDodge_Mode,
SkXfermode::kColorBurn_Mode, SkXfermode::kHardLight_Mode,
SkXfermode::kSoftLight_Mode, SkXfermode::kDifference_Mode,
SkXfermode::kExclusion_Mode, SkXfermode::kMultiply_Mode,
SkXfermode::kHue_Mode, SkXfermode::kSaturation_Mode,
SkXfermode::kColor_Mode, SkXfermode::kLuminosity_Mode};
SkColor kCSSTestColors[] = {
0xffff0000, // red
0xff00ff00, // lime
0xff0000ff, // blue
0xff00ffff, // aqua
0xffff00ff, // fuchsia
0xffffff00, // yellow
0xff008000, // green
0xff800000, // maroon
0xff000080, // navy
0xff800080, // purple
0xff808000, // olive
0xff008080, // teal
0xfffa8072, // salmon
0xffc0c0c0, // silver
0xff000000, // black
0xff808080, // gray
0x80000000, // black with transparency
0xffffffff, // white
0x80ffffff, // white with transparency
0x00000000 // transparent
};
const int kBlendModesCount = arraysize(kBlendModes);
const int kCSSTestColorsCount = arraysize(kCSSTestColors);
using RenderPassOptions = uint32;
const uint32 kUseMasks = 1 << 0;
const uint32 kUseAntialiasing = 1 << 1;
const uint32 kUseColorMatrix = 1 << 2;
const uint32 kForceShaders = 1 << 3;
class LayerTreeHostBlendingPixelTest : public LayerTreeHostPixelResourceTest {
public:
LayerTreeHostBlendingPixelTest()
: force_antialiasing_(false), force_blending_with_shaders_(false) {
pixel_comparator_.reset(new FuzzyPixelOffByOneComparator(true));
}
void InitializeSettings(LayerTreeSettings* settings) override {
settings->renderer_settings.force_antialiasing = force_antialiasing_;
settings->renderer_settings.force_blending_with_shaders =
force_blending_with_shaders_;
}
protected:
void RunBlendingWithRootPixelTestType(PixelResourceTestCase type) {
const int kLaneWidth = 2;
const int kLaneHeight = kLaneWidth;
const int kRootWidth = (kBlendModesCount + 2) * kLaneWidth;
const int kRootHeight = 2 * kLaneWidth + kLaneHeight;
InitializeFromTestCase(type);
scoped_refptr<SolidColorLayer> background =
CreateSolidColorLayer(gfx::Rect(kRootWidth, kRootHeight), kCSSOrange);
// Orange child layers will blend with the green background
for (int i = 0; i < kBlendModesCount; ++i) {
gfx::Rect child_rect(
(i + 1) * kLaneWidth, kLaneWidth, kLaneWidth, kLaneHeight);
scoped_refptr<SolidColorLayer> green_lane =
CreateSolidColorLayer(child_rect, kCSSGreen);
background->AddChild(green_lane);
green_lane->SetBlendMode(kBlendModes[i]);
}
RunPixelResourceTest(
background,
base::FilePath(FILE_PATH_LITERAL("blending_with_root.png")));
}
void RunBlendingWithTransparentPixelTestType(PixelResourceTestCase type) {
const int kLaneWidth = 2;
const int kLaneHeight = 3 * kLaneWidth;
const int kRootWidth = (kBlendModesCount + 2) * kLaneWidth;
const int kRootHeight = 2 * kLaneWidth + kLaneHeight;
InitializeFromTestCase(type);
scoped_refptr<SolidColorLayer> root =
CreateSolidColorLayer(gfx::Rect(kRootWidth, kRootHeight), kCSSBrown);
scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
gfx::Rect(0, kLaneWidth * 2, kRootWidth, kLaneWidth), kCSSOrange);
root->AddChild(background);
background->SetIsRootForIsolatedGroup(true);
// Orange child layers will blend with the green background
for (int i = 0; i < kBlendModesCount; ++i) {
gfx::Rect child_rect(
(i + 1) * kLaneWidth, -kLaneWidth, kLaneWidth, kLaneHeight);
scoped_refptr<SolidColorLayer> green_lane =
CreateSolidColorLayer(child_rect, kCSSGreen);
background->AddChild(green_lane);
green_lane->SetBlendMode(kBlendModes[i]);
}
RunPixelResourceTest(
root, base::FilePath(FILE_PATH_LITERAL("blending_transparent.png")));
}
scoped_refptr<Layer> CreateColorfulBackdropLayer(int width, int height) {
// Draw the backdrop with horizontal lanes.
const int kLaneWidth = width;
const int kLaneHeight = height / kCSSTestColorsCount;
SkBitmap backing_store;
backing_store.allocN32Pixels(width, height);
SkCanvas canvas(backing_store);
canvas.clear(SK_ColorTRANSPARENT);
for (int i = 0; i < kCSSTestColorsCount; ++i) {
SkPaint paint;
paint.setColor(kCSSTestColors[i]);
canvas.drawRect(
SkRect::MakeXYWH(0, i * kLaneHeight, kLaneWidth, kLaneHeight), paint);
}
scoped_refptr<PictureImageLayer> layer = PictureImageLayer::Create();
layer->SetIsDrawable(true);
layer->SetBounds(gfx::Size(width, height));
layer->SetBitmap(backing_store);
return layer;
}
void SetupMaskLayer(scoped_refptr<Layer> layer) {
const int kMaskOffset = 2;
gfx::Size bounds = layer->bounds();
scoped_refptr<PictureImageLayer> mask = PictureImageLayer::Create();
mask->SetIsDrawable(true);
mask->SetIsMask(true);
mask->SetBounds(bounds);
SkBitmap bitmap;
bitmap.allocN32Pixels(bounds.width(), bounds.height());
SkCanvas canvas(bitmap);
SkPaint paint;
paint.setColor(SK_ColorWHITE);
canvas.clear(SK_ColorTRANSPARENT);
canvas.drawRect(SkRect::MakeXYWH(kMaskOffset,
kMaskOffset,
bounds.width() - kMaskOffset * 2,
bounds.height() - kMaskOffset * 2),
paint);
mask->SetBitmap(bitmap);
layer->SetMaskLayer(mask.get());
}
void SetupColorMatrix(scoped_refptr<Layer> layer) {
FilterOperations filter_operations;
filter_operations.Append(FilterOperation::CreateSepiaFilter(.001f));
layer->SetFilters(filter_operations);
}
void CreateBlendingColorLayers(int lane_width,
int lane_height,
scoped_refptr<Layer> background,
RenderPassOptions flags) {
const int kLanesCount = kBlendModesCount + 4;
const SkColor kMiscOpaqueColor = 0xffc86464;
const SkColor kMiscTransparentColor = 0x80c86464;
const SkXfermode::Mode kCoeffBlendMode = SkXfermode::kScreen_Mode;
const SkXfermode::Mode kShaderBlendMode = SkXfermode::kColorBurn_Mode;
// add vertical lanes with each of the blend modes
for (int i = 0; i < kLanesCount; ++i) {
gfx::Rect child_rect(i * lane_width, 0, lane_width, lane_height);
SkXfermode::Mode blend_mode = SkXfermode::kSrcOver_Mode;
float opacity = 1.f;
SkColor color = kMiscOpaqueColor;
if (i < kBlendModesCount) {
blend_mode = kBlendModes[i];
} else if (i == kBlendModesCount) {
blend_mode = kCoeffBlendMode;
opacity = 0.5f;
} else if (i == kBlendModesCount + 1) {
blend_mode = kCoeffBlendMode;
color = kMiscTransparentColor;
} else if (i == kBlendModesCount + 2) {
blend_mode = kShaderBlendMode;
opacity = 0.5f;
} else if (i == kBlendModesCount + 3) {
blend_mode = kShaderBlendMode;
color = kMiscTransparentColor;
}
scoped_refptr<SolidColorLayer> lane =
CreateSolidColorLayer(child_rect, color);
lane->SetBlendMode(blend_mode);
lane->SetOpacity(opacity);
lane->SetForceRenderSurface(true);
if (flags & kUseMasks)
SetupMaskLayer(lane);
if (flags & kUseColorMatrix) {
SetupColorMatrix(lane);
}
background->AddChild(lane);
}
}
void RunBlendingWithRenderPass(PixelResourceTestCase type,
const base::FilePath::CharType* expected_path,
RenderPassOptions flags) {
const int kLaneWidth = 8;
const int kLaneHeight = kLaneWidth * kCSSTestColorsCount;
const int kRootSize = kLaneHeight;
InitializeFromTestCase(type);
scoped_refptr<SolidColorLayer> root =
CreateSolidColorLayer(gfx::Rect(kRootSize, kRootSize), SK_ColorWHITE);
scoped_refptr<Layer> background =
CreateColorfulBackdropLayer(kRootSize, kRootSize);
background->SetIsRootForIsolatedGroup(true);
root->AddChild(background);
CreateBlendingColorLayers(kLaneWidth, kLaneHeight, background.get(), flags);
this->force_antialiasing_ = (flags & kUseAntialiasing);
this->force_blending_with_shaders_ = (flags & kForceShaders);
if ((flags & kUseAntialiasing) && (test_type_ == PIXEL_TEST_GL)) {
// Anti aliasing causes differences up to 8 pixels at the edges.
int large_error_allowed = 8;
// Blending results might differ with one pixel.
int small_error_allowed = 1;
// Most of the errors are one pixel errors.
float percentage_pixels_small_error = 13.1f;
// Because of anti-aliasing, around 10% of pixels (at the edges) have
// bigger errors (from small_error_allowed + 1 to large_error_allowed).
float percentage_pixels_error = 22.5f;
// The average error is still close to 1.
float average_error_allowed_in_bad_pixels = 1.4f;
pixel_comparator_.reset(
new FuzzyPixelComparator(false, // discard_alpha
percentage_pixels_error,
percentage_pixels_small_error,
average_error_allowed_in_bad_pixels,
large_error_allowed,
small_error_allowed));
}
RunPixelResourceTest(root, base::FilePath(expected_path));
}
bool force_antialiasing_;
bool force_blending_with_shaders_;
};
TEST_F(LayerTreeHostBlendingPixelTest, BlendingWithRoot_GL) {
RunBlendingWithRootPixelTestType(GL_ASYNC_UPLOAD_2D_DRAW);
}
TEST_F(LayerTreeHostBlendingPixelTest, BlendingWithRoot_Software) {
RunBlendingWithRootPixelTestType(SOFTWARE);
}
TEST_F(LayerTreeHostBlendingPixelTest, BlendingWithBackgroundFilter) {
const int kLaneWidth = 2;
const int kLaneHeight = kLaneWidth;
const int kRootWidth = (kBlendModesCount + 2) * kLaneWidth;
const int kRootHeight = 2 * kLaneWidth + kLaneHeight;
InitializeFromTestCase(GL_ASYNC_UPLOAD_2D_DRAW);
scoped_refptr<SolidColorLayer> background =
CreateSolidColorLayer(gfx::Rect(kRootWidth, kRootHeight), kCSSOrange);
// Orange child layers have a background filter set and they will blend with
// the green background
for (int i = 0; i < kBlendModesCount; ++i) {
gfx::Rect child_rect(
(i + 1) * kLaneWidth, kLaneWidth, kLaneWidth, kLaneHeight);
scoped_refptr<SolidColorLayer> green_lane =
CreateSolidColorLayer(child_rect, kCSSGreen);
background->AddChild(green_lane);
FilterOperations filters;
filters.Append(FilterOperation::CreateGrayscaleFilter(.75));
green_lane->SetBackgroundFilters(filters);
green_lane->SetBlendMode(kBlendModes[i]);
}
RunPixelResourceTest(
background, base::FilePath(FILE_PATH_LITERAL("blending_and_filter.png")));
}
TEST_F(LayerTreeHostBlendingPixelTest, BlendingWithTransparent_GL) {
RunBlendingWithTransparentPixelTestType(GL_ASYNC_UPLOAD_2D_DRAW);
}
TEST_F(LayerTreeHostBlendingPixelTest, BlendingWithTransparent_Software) {
RunBlendingWithTransparentPixelTestType(SOFTWARE);
}
// Tests for render passes
TEST_F(LayerTreeHostBlendingPixelTest, BlendingWithRenderPass_GL) {
RunBlendingWithRenderPass(GL_ASYNC_UPLOAD_2D_DRAW,
FILE_PATH_LITERAL("blending_render_pass.png"), 0);
}
TEST_F(LayerTreeHostBlendingPixelTest, BlendingWithRenderPass_Software) {
RunBlendingWithRenderPass(SOFTWARE,
FILE_PATH_LITERAL("blending_render_pass.png"), 0);
}
TEST_F(LayerTreeHostBlendingPixelTest, BlendingWithRenderPassAA_GL) {
RunBlendingWithRenderPass(GL_ASYNC_UPLOAD_2D_DRAW,
FILE_PATH_LITERAL("blending_render_pass.png"),
kUseAntialiasing);
}
TEST_F(LayerTreeHostBlendingPixelTest, BlendingWithRenderPassAA_Software) {
RunBlendingWithRenderPass(SOFTWARE,
FILE_PATH_LITERAL("blending_render_pass.png"),
kUseAntialiasing);
}
TEST_F(LayerTreeHostBlendingPixelTest, BlendingWithRenderPassWithMask_GL) {
RunBlendingWithRenderPass(GL_ASYNC_UPLOAD_2D_DRAW,
FILE_PATH_LITERAL("blending_render_pass_mask.png"),
kUseMasks);
}
TEST_F(LayerTreeHostBlendingPixelTest,
BlendingWithRenderPassWithMask_Software) {
RunBlendingWithRenderPass(
SOFTWARE, FILE_PATH_LITERAL("blending_render_pass_mask.png"), kUseMasks);
}
TEST_F(LayerTreeHostBlendingPixelTest, BlendingWithRenderPassWithMaskAA_GL) {
RunBlendingWithRenderPass(GL_ASYNC_UPLOAD_2D_DRAW,
FILE_PATH_LITERAL("blending_render_pass_mask.png"),
kUseMasks | kUseAntialiasing);
}
TEST_F(LayerTreeHostBlendingPixelTest,
BlendingWithRenderPassWithMaskAA_Software) {
RunBlendingWithRenderPass(SOFTWARE,
FILE_PATH_LITERAL("blending_render_pass_mask.png"),
kUseMasks | kUseAntialiasing);
}
TEST_F(LayerTreeHostBlendingPixelTest, BlendingWithRenderPassColorMatrix_GL) {
RunBlendingWithRenderPass(GL_ASYNC_UPLOAD_2D_DRAW,
FILE_PATH_LITERAL("blending_render_pass.png"),
kUseColorMatrix);
}
TEST_F(LayerTreeHostBlendingPixelTest,
BlendingWithRenderPassColorMatrix_Software) {
RunBlendingWithRenderPass(
SOFTWARE, FILE_PATH_LITERAL("blending_render_pass.png"), kUseColorMatrix);
}
TEST_F(LayerTreeHostBlendingPixelTest, BlendingWithRenderPassColorMatrixAA_GL) {
RunBlendingWithRenderPass(GL_ASYNC_UPLOAD_2D_DRAW,
FILE_PATH_LITERAL("blending_render_pass.png"),
kUseAntialiasing | kUseColorMatrix);
}
TEST_F(LayerTreeHostBlendingPixelTest,
BlendingWithRenderPassColorMatrixAA_Software) {
RunBlendingWithRenderPass(SOFTWARE,
FILE_PATH_LITERAL("blending_render_pass.png"),
kUseAntialiasing | kUseColorMatrix);
}
TEST_F(LayerTreeHostBlendingPixelTest,
BlendingWithRenderPassWithMaskColorMatrix_GL) {
RunBlendingWithRenderPass(GL_ASYNC_UPLOAD_2D_DRAW,
FILE_PATH_LITERAL("blending_render_pass_mask.png"),
kUseMasks | kUseColorMatrix);
}
TEST_F(LayerTreeHostBlendingPixelTest,
BlendingWithRenderPassWithMaskColorMatrix_Software) {
RunBlendingWithRenderPass(SOFTWARE,
FILE_PATH_LITERAL("blending_render_pass_mask.png"),
kUseMasks | kUseColorMatrix);
}
TEST_F(LayerTreeHostBlendingPixelTest,
BlendingWithRenderPassWithMaskColorMatrixAA_GL) {
RunBlendingWithRenderPass(GL_ASYNC_UPLOAD_2D_DRAW,
FILE_PATH_LITERAL("blending_render_pass_mask.png"),
kUseMasks | kUseAntialiasing | kUseColorMatrix);
}
TEST_F(LayerTreeHostBlendingPixelTest,
BlendingWithRenderPassWithMaskColorMatrixAA_Software) {
RunBlendingWithRenderPass(SOFTWARE,
FILE_PATH_LITERAL("blending_render_pass_mask.png"),
kUseMasks | kUseAntialiasing | kUseColorMatrix);
}
// Tests for render passes forcing shaders for all the blend modes.
TEST_F(LayerTreeHostBlendingPixelTest, BlendingWithRenderPassShaders_GL) {
RunBlendingWithRenderPass(GL_ASYNC_UPLOAD_2D_DRAW,
FILE_PATH_LITERAL("blending_render_pass.png"),
kForceShaders);
}
TEST_F(LayerTreeHostBlendingPixelTest, BlendingWithRenderPassShadersAA_GL) {
RunBlendingWithRenderPass(GL_ASYNC_UPLOAD_2D_DRAW,
FILE_PATH_LITERAL("blending_render_pass.png"),
kUseAntialiasing | kForceShaders);
}
TEST_F(LayerTreeHostBlendingPixelTest,
BlendingWithRenderPassShadersWithMask_GL) {
RunBlendingWithRenderPass(GL_ASYNC_UPLOAD_2D_DRAW,
FILE_PATH_LITERAL("blending_render_pass_mask.png"),
kUseMasks | kForceShaders);
}
TEST_F(LayerTreeHostBlendingPixelTest,
BlendingWithRenderPassShadersWithMask_GL_TextureRect) {
RunBlendingWithRenderPass(GL_ZERO_COPY_RECT_DRAW,
FILE_PATH_LITERAL("blending_render_pass_mask.png"),
kUseMasks | kForceShaders);
}
TEST_F(LayerTreeHostBlendingPixelTest,
BlendingWithRenderPassShadersWithMaskAA_GL) {
RunBlendingWithRenderPass(GL_ASYNC_UPLOAD_2D_DRAW,
FILE_PATH_LITERAL("blending_render_pass_mask.png"),
kUseMasks | kUseAntialiasing | kForceShaders);
}
TEST_F(LayerTreeHostBlendingPixelTest,
BlendingWithRenderPassShadersWithMaskAA_GL_TextureRect) {
RunBlendingWithRenderPass(GL_ZERO_COPY_RECT_DRAW,
FILE_PATH_LITERAL("blending_render_pass_mask.png"),
kUseMasks | kUseAntialiasing | kForceShaders);
}
TEST_F(LayerTreeHostBlendingPixelTest,
BlendingWithRenderPassShadersColorMatrix_GL) {
RunBlendingWithRenderPass(GL_ASYNC_UPLOAD_2D_DRAW,
FILE_PATH_LITERAL("blending_render_pass.png"),
kUseColorMatrix | kForceShaders);
}
TEST_F(LayerTreeHostBlendingPixelTest,
BlendingWithRenderPassShadersColorMatrixAA_GL) {
RunBlendingWithRenderPass(GL_ASYNC_UPLOAD_2D_DRAW,
FILE_PATH_LITERAL("blending_render_pass.png"),
kUseAntialiasing | kUseColorMatrix | kForceShaders);
}
TEST_F(LayerTreeHostBlendingPixelTest,
BlendingWithRenderPassShadersWithMaskColorMatrix_GL) {
RunBlendingWithRenderPass(GL_ASYNC_UPLOAD_2D_DRAW,
FILE_PATH_LITERAL("blending_render_pass_mask.png"),
kUseMasks | kUseColorMatrix | kForceShaders);
}
TEST_F(LayerTreeHostBlendingPixelTest,
BlendingWithRenderPassShadersWithMaskColorMatrix_GL_TextureRect) {
RunBlendingWithRenderPass(GL_ZERO_COPY_RECT_DRAW,
FILE_PATH_LITERAL("blending_render_pass_mask.png"),
kUseMasks | kUseColorMatrix | kForceShaders);
}
TEST_F(LayerTreeHostBlendingPixelTest,
BlendingWithRenderPassShadersWithMaskColorMatrixAA_GL) {
RunBlendingWithRenderPass(
GL_ASYNC_UPLOAD_2D_DRAW,
FILE_PATH_LITERAL("blending_render_pass_mask.png"),
kUseMasks | kUseAntialiasing | kUseColorMatrix | kForceShaders);
}
} // namespace
} // namespace cc
#endif // OS_ANDROID
|