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/content_layer_client.h"
7 #include "cc/layers/picture_image_layer.h"
8 #include "cc/layers/picture_layer.h"
9 #include "cc/layers/solid_color_layer.h"
10 #include "cc/test/layer_tree_pixel_resource_test.h"
11 #include "cc/test/pixel_comparator.h"
13 #if !defined(OS_ANDROID)
18 typedef ParameterizedPixelResourceTest LayerTreeHostMasksPixelTest
;
20 INSTANTIATE_PIXEL_RESOURCE_TEST_CASE_P(LayerTreeHostMasksPixelTest
);
22 class MaskContentLayerClient
: public ContentLayerClient
{
24 explicit MaskContentLayerClient(const gfx::Size
& bounds
) : bounds_(bounds
) {}
25 ~MaskContentLayerClient() override
{}
27 bool FillsBoundsCompletely() const override
{ return false; }
29 void PaintContents(SkCanvas
* canvas
,
30 const gfx::Rect
& rect
,
31 PaintingControlSetting picture_control
) override
{
33 paint
.setStyle(SkPaint::kStroke_Style
);
34 paint
.setStrokeWidth(SkIntToScalar(2));
35 paint
.setColor(SK_ColorWHITE
);
37 canvas
->clear(SK_ColorTRANSPARENT
);
38 gfx::Rect
inset_rect(bounds_
);
39 while (!inset_rect
.IsEmpty()) {
40 inset_rect
.Inset(3, 3, 2, 2);
42 SkRect::MakeXYWH(inset_rect
.x(), inset_rect
.y(),
43 inset_rect
.width(), inset_rect
.height()),
45 inset_rect
.Inset(3, 3, 2, 2);
49 scoped_refptr
<DisplayItemList
> PaintContentsToDisplayList(
50 const gfx::Rect
& clip
,
51 PaintingControlSetting picture_control
) override
{
53 return DisplayItemList::Create();
60 // TODO(enne): these time out on Windows. http://crbug.com/435632
63 TEST_P(LayerTreeHostMasksPixelTest
, MaskOfLayer
) {
64 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
65 gfx::Rect(200, 200), SK_ColorWHITE
);
67 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
68 gfx::Rect(50, 50, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
69 background
->AddChild(green
);
71 gfx::Size
mask_bounds(100, 100);
72 MaskContentLayerClient
client(mask_bounds
);
73 scoped_refptr
<PictureLayer
> mask
= PictureLayer::Create(&client
);
74 mask
->SetBounds(mask_bounds
);
75 mask
->SetIsDrawable(true);
76 mask
->SetIsMask(true);
77 green
->SetMaskLayer(mask
.get());
79 RunPixelResourceTest(background
,
80 base::FilePath(FILE_PATH_LITERAL("mask_of_layer.png")));
83 TEST_P(LayerTreeHostMasksPixelTest
, ImageMaskOfLayer
) {
84 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
85 gfx::Rect(200, 200), SK_ColorWHITE
);
87 gfx::Size
mask_bounds(100, 100);
89 scoped_refptr
<PictureImageLayer
> mask
= PictureImageLayer::Create();
90 mask
->SetIsDrawable(true);
91 mask
->SetIsMask(true);
92 mask
->SetBounds(mask_bounds
);
95 bitmap
.allocN32Pixels(400, 400);
96 SkCanvas
canvas(bitmap
);
97 canvas
.scale(SkIntToScalar(4), SkIntToScalar(4));
98 MaskContentLayerClient
client(mask_bounds
);
99 client
.PaintContents(&canvas
, gfx::Rect(mask_bounds
),
100 ContentLayerClient::PAINTING_BEHAVIOR_NORMAL
);
101 mask
->SetBitmap(bitmap
);
103 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
104 gfx::Rect(50, 50, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
105 green
->SetMaskLayer(mask
.get());
106 background
->AddChild(green
);
108 RunPixelResourceTest(
109 background
, base::FilePath(FILE_PATH_LITERAL("image_mask_of_layer.png")));
112 TEST_P(LayerTreeHostMasksPixelTest
, MaskOfClippedLayer
) {
113 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
114 gfx::Rect(200, 200), SK_ColorWHITE
);
116 // Clip to the top half of the green layer.
117 scoped_refptr
<Layer
> clip
= Layer::Create();
118 clip
->SetPosition(gfx::Point(0, 0));
119 clip
->SetBounds(gfx::Size(200, 100));
120 clip
->SetMasksToBounds(true);
121 background
->AddChild(clip
);
123 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
124 gfx::Rect(50, 50, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
125 clip
->AddChild(green
);
127 gfx::Size
mask_bounds(100, 100);
128 MaskContentLayerClient
client(mask_bounds
);
129 scoped_refptr
<PictureLayer
> mask
= PictureLayer::Create(&client
);
130 mask
->SetBounds(mask_bounds
);
131 mask
->SetIsDrawable(true);
132 mask
->SetIsMask(true);
133 green
->SetMaskLayer(mask
.get());
135 RunPixelResourceTest(
137 base::FilePath(FILE_PATH_LITERAL("mask_of_clipped_layer.png")));
140 TEST_P(LayerTreeHostMasksPixelTest
, MaskWithReplica
) {
141 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
142 gfx::Rect(200, 200), SK_ColorWHITE
);
144 gfx::Size
mask_bounds(100, 100);
145 MaskContentLayerClient
client(mask_bounds
);
146 scoped_refptr
<PictureLayer
> mask
= PictureLayer::Create(&client
);
147 mask
->SetBounds(mask_bounds
);
148 mask
->SetIsDrawable(true);
149 mask
->SetIsMask(true);
151 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
152 gfx::Rect(0, 0, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
153 background
->AddChild(green
);
154 green
->SetMaskLayer(mask
.get());
156 gfx::Transform replica_transform
;
157 replica_transform
.Rotate(-90.0);
159 scoped_refptr
<Layer
> replica
= Layer::Create();
160 replica
->SetTransformOrigin(gfx::Point3F(50.f
, 50.f
, 0.f
));
161 replica
->SetPosition(gfx::Point(100, 100));
162 replica
->SetTransform(replica_transform
);
163 green
->SetReplicaLayer(replica
.get());
165 RunPixelResourceTest(
166 background
, base::FilePath(FILE_PATH_LITERAL("mask_with_replica.png")));
169 TEST_P(LayerTreeHostMasksPixelTest
, MaskWithReplicaOfClippedLayer
) {
170 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
171 gfx::Rect(200, 200), SK_ColorWHITE
);
173 gfx::Size
mask_bounds(100, 100);
174 MaskContentLayerClient
client(mask_bounds
);
175 scoped_refptr
<PictureLayer
> mask
= PictureLayer::Create(&client
);
176 mask
->SetBounds(mask_bounds
);
177 mask
->SetIsDrawable(true);
178 mask
->SetIsMask(true);
180 // Clip to the bottom half of the green layer, and the left half of the
182 scoped_refptr
<Layer
> clip
= Layer::Create();
183 clip
->SetPosition(gfx::Point(0, 50));
184 clip
->SetBounds(gfx::Size(150, 150));
185 clip
->SetMasksToBounds(true);
186 background
->AddChild(clip
);
188 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
189 gfx::Rect(0, -50, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
190 clip
->AddChild(green
);
191 green
->SetMaskLayer(mask
.get());
193 gfx::Transform replica_transform
;
194 replica_transform
.Rotate(-90.0);
196 scoped_refptr
<Layer
> replica
= Layer::Create();
197 replica
->SetTransformOrigin(gfx::Point3F(50.f
, 50.f
, 0.f
));
198 replica
->SetPosition(gfx::Point(100, 100));
199 replica
->SetTransform(replica_transform
);
200 green
->SetReplicaLayer(replica
.get());
202 RunPixelResourceTest(background
,
203 base::FilePath(FILE_PATH_LITERAL(
204 "mask_with_replica_of_clipped_layer.png")));
207 TEST_P(LayerTreeHostMasksPixelTest
, MaskOfReplica
) {
208 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
209 gfx::Rect(200, 200), SK_ColorWHITE
);
211 gfx::Size
mask_bounds(100, 100);
212 MaskContentLayerClient
client(mask_bounds
);
213 scoped_refptr
<PictureLayer
> mask
= PictureLayer::Create(&client
);
214 mask
->SetBounds(mask_bounds
);
215 mask
->SetIsDrawable(true);
216 mask
->SetIsMask(true);
218 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
219 gfx::Rect(50, 0, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
220 background
->AddChild(green
);
222 scoped_refptr
<SolidColorLayer
> orange
= CreateSolidColorLayer(
223 gfx::Rect(-50, 50, 50, 50), kCSSOrange
);
224 green
->AddChild(orange
);
226 gfx::Transform replica_transform
;
227 replica_transform
.Rotate(180.0);
228 replica_transform
.Translate(100.0, 0.0);
230 scoped_refptr
<Layer
> replica
= Layer::Create();
231 replica
->SetTransformOrigin(gfx::Point3F(100.f
, 100.f
, 0.f
));
232 replica
->SetPosition(gfx::Point());
233 replica
->SetTransform(replica_transform
);
234 replica
->SetMaskLayer(mask
.get());
235 green
->SetReplicaLayer(replica
.get());
237 RunPixelResourceTest(
238 background
, base::FilePath(FILE_PATH_LITERAL("mask_of_replica.png")));
241 TEST_P(LayerTreeHostMasksPixelTest
, MaskOfReplicaOfClippedLayer
) {
242 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
243 gfx::Rect(200, 200), SK_ColorWHITE
);
245 gfx::Size
mask_bounds(100, 100);
246 MaskContentLayerClient
client(mask_bounds
);
247 scoped_refptr
<PictureLayer
> mask
= PictureLayer::Create(&client
);
248 mask
->SetBounds(mask_bounds
);
249 mask
->SetIsDrawable(true);
250 mask
->SetIsMask(true);
252 // Clip to the bottom 3/4 of the green layer, and the top 3/4 of the replica.
253 scoped_refptr
<Layer
> clip
= Layer::Create();
254 clip
->SetPosition(gfx::Point(0, 25));
255 clip
->SetBounds(gfx::Size(200, 150));
256 clip
->SetMasksToBounds(true);
257 background
->AddChild(clip
);
259 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
260 gfx::Rect(50, -25, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
261 clip
->AddChild(green
);
263 scoped_refptr
<SolidColorLayer
> orange
= CreateSolidColorLayer(
264 gfx::Rect(-50, 50, 50, 50), kCSSOrange
);
265 green
->AddChild(orange
);
267 gfx::Transform replica_transform
;
268 replica_transform
.Rotate(180.0);
269 replica_transform
.Translate(100.0, 0.0);
271 scoped_refptr
<Layer
> replica
= Layer::Create();
272 replica
->SetTransformOrigin(gfx::Point3F(100.f
, 100.f
, 0.f
));
273 replica
->SetPosition(gfx::Point());
274 replica
->SetTransform(replica_transform
);
275 replica
->SetMaskLayer(mask
.get());
276 green
->SetReplicaLayer(replica
.get());
278 RunPixelResourceTest(background
,
279 base::FilePath(FILE_PATH_LITERAL(
280 "mask_of_replica_of_clipped_layer.png")));
283 #endif // !defined(OS_WIN)