1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "build/build_config.h"
6 #include "cc/layers/solid_color_layer.h"
7 #include "cc/test/layer_tree_pixel_test.h"
8 #include "cc/test/pixel_comparator.h"
9 #include "third_party/skia/include/effects/SkColorFilterImageFilter.h"
10 #include "third_party/skia/include/effects/SkColorMatrixFilter.h"
12 #if !defined(OS_ANDROID)
17 class LayerTreeHostFiltersPixelTest : public LayerTreePixelTest {};
19 TEST_F(LayerTreeHostFiltersPixelTest, BackgroundFilterBlur) {
20 scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
21 gfx::Rect(200, 200), SK_ColorWHITE);
23 // The green box is entirely behind a layer with background blur, so it
24 // should appear blurred on its edges.
25 scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
26 gfx::Rect(50, 50, 100, 100), kCSSGreen);
27 scoped_refptr<SolidColorLayer> blur = CreateSolidColorLayer(
28 gfx::Rect(30, 30, 140, 140), SK_ColorTRANSPARENT);
29 background->AddChild(green);
30 background->AddChild(blur);
32 FilterOperations filters;
33 filters.Append(FilterOperation::CreateBlurFilter(2.f));
34 blur->SetBackgroundFilters(filters);
37 // Windows has 436 pixels off by 1: crbug.com/259915
38 float percentage_pixels_large_error = 1.09f; // 436px / (200*200)
39 float percentage_pixels_small_error = 0.0f;
40 float average_error_allowed_in_bad_pixels = 1.f;
41 int large_error_allowed = 1;
42 int small_error_allowed = 0;
43 pixel_comparator_.reset(new FuzzyPixelComparator(
44 true, // discard_alpha
45 percentage_pixels_large_error,
46 percentage_pixels_small_error,
47 average_error_allowed_in_bad_pixels,
49 small_error_allowed));
52 RunPixelTest(GL_WITH_BITMAP,
54 base::FilePath(FILE_PATH_LITERAL("background_filter_blur.png")));
57 TEST_F(LayerTreeHostFiltersPixelTest, BackgroundFilterBlurOutsets) {
58 scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
59 gfx::Rect(200, 200), SK_ColorWHITE);
61 // The green border is outside the layer with background blur, but the
62 // background blur should use pixels from outside its layer borders, up to the
63 // radius of the blur effect. So the border should be blurred underneath the
64 // top layer causing the green to bleed under the transparent layer, but not
65 // in the 1px region between the transparent layer and the green border.
66 scoped_refptr<SolidColorLayer> green_border = CreateSolidColorLayerWithBorder(
67 gfx::Rect(1, 1, 198, 198), SK_ColorWHITE, 10, kCSSGreen);
68 scoped_refptr<SolidColorLayer> blur = CreateSolidColorLayer(
69 gfx::Rect(12, 12, 176, 176), SK_ColorTRANSPARENT);
70 background->AddChild(green_border);
71 background->AddChild(blur);
73 FilterOperations filters;
74 filters.Append(FilterOperation::CreateBlurFilter(5.f));
75 blur->SetBackgroundFilters(filters);
78 // Windows has 2596 pixels off by at most 2: crbug.com/259922
79 float percentage_pixels_large_error = 6.5f; // 2596px / (200*200), rounded up
80 float percentage_pixels_small_error = 0.0f;
81 float average_error_allowed_in_bad_pixels = 1.f;
82 int large_error_allowed = 2;
83 int small_error_allowed = 0;
84 pixel_comparator_.reset(new FuzzyPixelComparator(
85 true, // discard_alpha
86 percentage_pixels_large_error,
87 percentage_pixels_small_error,
88 average_error_allowed_in_bad_pixels,
90 small_error_allowed));
93 RunPixelTest(GL_WITH_BITMAP,
95 base::FilePath(FILE_PATH_LITERAL(
96 "background_filter_blur_outsets.png")));
99 TEST_F(LayerTreeHostFiltersPixelTest, BackgroundFilterBlurOffAxis) {
100 scoped_refptr<SolidColorLayer> background = CreateSolidColorLayer(
101 gfx::Rect(200, 200), SK_ColorWHITE);
103 // This verifies that the perspective of the clear layer (with black border)
104 // does not influence the blending of the green box behind it. Also verifies
105 // that the blur is correctly clipped inside the transformed clear layer.
106 scoped_refptr<SolidColorLayer> green = CreateSolidColorLayer(
107 gfx::Rect(50, 50, 100, 100), kCSSGreen);
108 scoped_refptr<SolidColorLayer> blur = CreateSolidColorLayerWithBorder(
109 gfx::Rect(30, 30, 120, 120), SK_ColorTRANSPARENT, 1, SK_ColorBLACK);
110 background->AddChild(green);
111 background->AddChild(blur);
113 background->SetPreserves3d(true);
114 gfx::Transform background_transform;
115 background_transform.ApplyPerspectiveDepth(200.0);
116 background->SetTransform(background_transform);
118 blur->SetPreserves3d(true);
119 gfx::Transform blur_transform;
120 blur_transform.Translate(55.0, 65.0);
121 blur_transform.RotateAboutXAxis(85.0);
122 blur_transform.RotateAboutYAxis(180.0);
123 blur_transform.RotateAboutZAxis(20.0);
124 blur_transform.Translate(-60.0, -60.0);
125 blur->SetTransform(blur_transform);
127 FilterOperations filters;
128 filters.Append(FilterOperation::CreateBlurFilter(2.f));
129 blur->SetBackgroundFilters(filters);
132 // Windows has 153 pixels off by at most 2: crbug.com/225027
133 float percentage_pixels_large_error = 0.3825f; // 153px / (200*200)
134 float percentage_pixels_small_error = 0.0f;
135 float average_error_allowed_in_bad_pixels = 1.f;
136 int large_error_allowed = 2;
137 int small_error_allowed = 0;
138 pixel_comparator_.reset(new FuzzyPixelComparator(
139 true, // discard_alpha
140 percentage_pixels_large_error,
141 percentage_pixels_small_error,
142 average_error_allowed_in_bad_pixels,
144 small_error_allowed));
147 RunPixelTest(GL_WITH_BITMAP,
149 base::FilePath(FILE_PATH_LITERAL(
150 "background_filter_blur_off_axis.png")));
153 class ImageFilterClippedPixelTest : public LayerTreeHostFiltersPixelTest {
155 void RunPixelTestType(PixelTestType test_type) {
156 scoped_refptr<SolidColorLayer> root =
157 CreateSolidColorLayer(gfx::Rect(200, 200), SK_ColorBLACK);
159 scoped_refptr<SolidColorLayer> background =
160 CreateSolidColorLayer(gfx::Rect(200, 200), SK_ColorYELLOW);
161 root->AddChild(background);
163 scoped_refptr<SolidColorLayer> foreground =
164 CreateSolidColorLayer(gfx::Rect(200, 200), SK_ColorRED);
165 background->AddChild(foreground);
168 memset(matrix, 0, 20 * sizeof(matrix[0]));
169 // This filter does a red-blue swap, so the foreground becomes blue.
170 matrix[2] = matrix[6] = matrix[10] = matrix[18] = SK_Scalar1;
171 skia::RefPtr<SkColorFilter> colorFilter(
172 skia::AdoptRef(new SkColorMatrixFilter(matrix)));
173 // We filter only the bottom 200x100 pixels of the foreground.
174 SkImageFilter::CropRect crop_rect(SkRect::MakeXYWH(0, 100, 200, 100));
175 skia::RefPtr<SkImageFilter> filter = skia::AdoptRef(
176 SkColorFilterImageFilter::Create(colorFilter.get(), NULL, &crop_rect));
177 FilterOperations filters;
178 filters.Append(FilterOperation::CreateReferenceFilter(filter));
180 // Make the foreground layer's render surface be clipped by the background
182 background->SetMasksToBounds(true);
183 foreground->SetFilters(filters);
185 // Then we translate the foreground up by 100 pixels in Y, so the cropped
186 // region is moved to to the top. This ensures that the crop rect is being
187 // correctly transformed in skia by the amount of clipping that the
188 // compositor performs.
189 gfx::Transform transform;
190 transform.Translate(0.0, -100.0);
191 foreground->SetTransform(transform);
193 RunPixelTest(test_type,
195 base::FilePath(FILE_PATH_LITERAL("blue_yellow.png")));
199 TEST_F(ImageFilterClippedPixelTest, ImageFilterClipped_GL) {
200 RunPixelTestType(GL_WITH_BITMAP);
203 TEST_F(ImageFilterClippedPixelTest, ImageFilterClipped_Software) {
204 RunPixelTestType(SOFTWARE_WITH_BITMAP);