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 void DidChangeLayerCanUseLCDText() override
{}
29 bool FillsBoundsCompletely() const override
{ return false; }
33 const gfx::Rect
& rect
,
34 ContentLayerClient::GraphicsContextStatus gc_status
) override
{
36 paint
.setStyle(SkPaint::kStroke_Style
);
37 paint
.setStrokeWidth(SkIntToScalar(2));
38 paint
.setColor(SK_ColorWHITE
);
40 canvas
->clear(SK_ColorTRANSPARENT
);
41 gfx::Rect
inset_rect(bounds_
);
42 while (!inset_rect
.IsEmpty()) {
43 inset_rect
.Inset(3, 3, 2, 2);
45 SkRect::MakeXYWH(inset_rect
.x(), inset_rect
.y(),
46 inset_rect
.width(), inset_rect
.height()),
48 inset_rect
.Inset(3, 3, 2, 2);
56 TEST_P(LayerTreeHostMasksPixelTest
, MaskOfLayer
) {
57 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
58 gfx::Rect(200, 200), SK_ColorWHITE
);
60 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
61 gfx::Rect(50, 50, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
62 background
->AddChild(green
);
64 gfx::Size
mask_bounds(100, 100);
65 MaskContentLayerClient
client(mask_bounds
);
66 scoped_refptr
<PictureLayer
> mask
= PictureLayer::Create(&client
);
67 mask
->SetBounds(mask_bounds
);
68 mask
->SetIsDrawable(true);
69 mask
->SetIsMask(true);
70 green
->SetMaskLayer(mask
.get());
72 RunPixelResourceTest(background
,
73 base::FilePath(FILE_PATH_LITERAL("mask_of_layer.png")));
76 TEST_P(LayerTreeHostMasksPixelTest
, ImageMaskOfLayer
) {
77 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
78 gfx::Rect(200, 200), SK_ColorWHITE
);
80 gfx::Size
mask_bounds(100, 100);
82 scoped_refptr
<PictureImageLayer
> mask
= PictureImageLayer::Create();
83 mask
->SetIsDrawable(true);
84 mask
->SetIsMask(true);
85 mask
->SetBounds(mask_bounds
);
88 bitmap
.allocN32Pixels(400, 400);
89 SkCanvas
canvas(bitmap
);
90 canvas
.scale(SkIntToScalar(4), SkIntToScalar(4));
91 MaskContentLayerClient
client(mask_bounds
);
92 client
.PaintContents(&canvas
,
93 gfx::Rect(mask_bounds
),
94 ContentLayerClient::GRAPHICS_CONTEXT_ENABLED
);
95 mask
->SetBitmap(bitmap
);
97 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
98 gfx::Rect(50, 50, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
99 green
->SetMaskLayer(mask
.get());
100 background
->AddChild(green
);
102 RunPixelResourceTest(
103 background
, base::FilePath(FILE_PATH_LITERAL("image_mask_of_layer.png")));
106 TEST_P(LayerTreeHostMasksPixelTest
, MaskOfClippedLayer
) {
107 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
108 gfx::Rect(200, 200), SK_ColorWHITE
);
110 // Clip to the top half of the green layer.
111 scoped_refptr
<Layer
> clip
= Layer::Create();
112 clip
->SetPosition(gfx::Point(0, 0));
113 clip
->SetBounds(gfx::Size(200, 100));
114 clip
->SetMasksToBounds(true);
115 background
->AddChild(clip
);
117 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
118 gfx::Rect(50, 50, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
119 clip
->AddChild(green
);
121 gfx::Size
mask_bounds(100, 100);
122 MaskContentLayerClient
client(mask_bounds
);
123 scoped_refptr
<PictureLayer
> mask
= PictureLayer::Create(&client
);
124 mask
->SetBounds(mask_bounds
);
125 mask
->SetIsDrawable(true);
126 mask
->SetIsMask(true);
127 green
->SetMaskLayer(mask
.get());
129 RunPixelResourceTest(
131 base::FilePath(FILE_PATH_LITERAL("mask_of_clipped_layer.png")));
134 TEST_P(LayerTreeHostMasksPixelTest
, MaskWithReplica
) {
135 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
136 gfx::Rect(200, 200), SK_ColorWHITE
);
138 gfx::Size
mask_bounds(100, 100);
139 MaskContentLayerClient
client(mask_bounds
);
140 scoped_refptr
<PictureLayer
> mask
= PictureLayer::Create(&client
);
141 mask
->SetBounds(mask_bounds
);
142 mask
->SetIsDrawable(true);
143 mask
->SetIsMask(true);
145 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
146 gfx::Rect(0, 0, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
147 background
->AddChild(green
);
148 green
->SetMaskLayer(mask
.get());
150 gfx::Transform replica_transform
;
151 replica_transform
.Rotate(-90.0);
153 scoped_refptr
<Layer
> replica
= Layer::Create();
154 replica
->SetTransformOrigin(gfx::Point3F(50.f
, 50.f
, 0.f
));
155 replica
->SetPosition(gfx::Point(100, 100));
156 replica
->SetTransform(replica_transform
);
157 green
->SetReplicaLayer(replica
.get());
159 RunPixelResourceTest(
160 background
, base::FilePath(FILE_PATH_LITERAL("mask_with_replica.png")));
163 TEST_P(LayerTreeHostMasksPixelTest
, MaskWithReplicaOfClippedLayer
) {
164 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
165 gfx::Rect(200, 200), SK_ColorWHITE
);
167 gfx::Size
mask_bounds(100, 100);
168 MaskContentLayerClient
client(mask_bounds
);
169 scoped_refptr
<PictureLayer
> mask
= PictureLayer::Create(&client
);
170 mask
->SetBounds(mask_bounds
);
171 mask
->SetIsDrawable(true);
172 mask
->SetIsMask(true);
174 // Clip to the bottom half of the green layer, and the left half of the
176 scoped_refptr
<Layer
> clip
= Layer::Create();
177 clip
->SetPosition(gfx::Point(0, 50));
178 clip
->SetBounds(gfx::Size(150, 150));
179 clip
->SetMasksToBounds(true);
180 background
->AddChild(clip
);
182 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
183 gfx::Rect(0, -50, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
184 clip
->AddChild(green
);
185 green
->SetMaskLayer(mask
.get());
187 gfx::Transform replica_transform
;
188 replica_transform
.Rotate(-90.0);
190 scoped_refptr
<Layer
> replica
= Layer::Create();
191 replica
->SetTransformOrigin(gfx::Point3F(50.f
, 50.f
, 0.f
));
192 replica
->SetPosition(gfx::Point(100, 100));
193 replica
->SetTransform(replica_transform
);
194 green
->SetReplicaLayer(replica
.get());
196 RunPixelResourceTest(background
,
197 base::FilePath(FILE_PATH_LITERAL(
198 "mask_with_replica_of_clipped_layer.png")));
201 TEST_P(LayerTreeHostMasksPixelTest
, MaskOfReplica
) {
202 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
203 gfx::Rect(200, 200), SK_ColorWHITE
);
205 gfx::Size
mask_bounds(100, 100);
206 MaskContentLayerClient
client(mask_bounds
);
207 scoped_refptr
<PictureLayer
> mask
= PictureLayer::Create(&client
);
208 mask
->SetBounds(mask_bounds
);
209 mask
->SetIsDrawable(true);
210 mask
->SetIsMask(true);
212 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
213 gfx::Rect(50, 0, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
214 background
->AddChild(green
);
216 scoped_refptr
<SolidColorLayer
> orange
= CreateSolidColorLayer(
217 gfx::Rect(-50, 50, 50, 50), kCSSOrange
);
218 green
->AddChild(orange
);
220 gfx::Transform replica_transform
;
221 replica_transform
.Rotate(180.0);
222 replica_transform
.Translate(100.0, 0.0);
224 scoped_refptr
<Layer
> replica
= Layer::Create();
225 replica
->SetTransformOrigin(gfx::Point3F(100.f
, 100.f
, 0.f
));
226 replica
->SetPosition(gfx::Point());
227 replica
->SetTransform(replica_transform
);
228 replica
->SetMaskLayer(mask
.get());
229 green
->SetReplicaLayer(replica
.get());
231 RunPixelResourceTest(
232 background
, base::FilePath(FILE_PATH_LITERAL("mask_of_replica.png")));
235 TEST_P(LayerTreeHostMasksPixelTest
, MaskOfReplicaOfClippedLayer
) {
236 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
237 gfx::Rect(200, 200), SK_ColorWHITE
);
239 gfx::Size
mask_bounds(100, 100);
240 MaskContentLayerClient
client(mask_bounds
);
241 scoped_refptr
<PictureLayer
> mask
= PictureLayer::Create(&client
);
242 mask
->SetBounds(mask_bounds
);
243 mask
->SetIsDrawable(true);
244 mask
->SetIsMask(true);
246 // Clip to the bottom 3/4 of the green layer, and the top 3/4 of the replica.
247 scoped_refptr
<Layer
> clip
= Layer::Create();
248 clip
->SetPosition(gfx::Point(0, 25));
249 clip
->SetBounds(gfx::Size(200, 150));
250 clip
->SetMasksToBounds(true);
251 background
->AddChild(clip
);
253 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
254 gfx::Rect(50, -25, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
255 clip
->AddChild(green
);
257 scoped_refptr
<SolidColorLayer
> orange
= CreateSolidColorLayer(
258 gfx::Rect(-50, 50, 50, 50), kCSSOrange
);
259 green
->AddChild(orange
);
261 gfx::Transform replica_transform
;
262 replica_transform
.Rotate(180.0);
263 replica_transform
.Translate(100.0, 0.0);
265 scoped_refptr
<Layer
> replica
= Layer::Create();
266 replica
->SetTransformOrigin(gfx::Point3F(100.f
, 100.f
, 0.f
));
267 replica
->SetPosition(gfx::Point());
268 replica
->SetTransform(replica_transform
);
269 replica
->SetMaskLayer(mask
.get());
270 green
->SetReplicaLayer(replica
.get());
272 RunPixelResourceTest(background
,
273 base::FilePath(FILE_PATH_LITERAL(
274 "mask_of_replica_of_clipped_layer.png")));