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.h"
7 #include "cc/layers/content_layer_client.h"
8 #include "cc/layers/image_layer.h"
9 #include "cc/layers/solid_color_layer.h"
10 #include "cc/test/layer_tree_pixel_test.h"
11 #include "cc/test/pixel_comparator.h"
13 #if !defined(OS_ANDROID)
18 class LayerTreeHostMasksPixelTest
: public LayerTreePixelTest
{};
20 class MaskContentLayerClient
: public ContentLayerClient
{
22 MaskContentLayerClient() {}
23 virtual ~MaskContentLayerClient() {}
25 virtual void DidChangeLayerCanUseLCDText() OVERRIDE
{}
27 virtual void PaintContents(SkCanvas
* canvas
,
28 const gfx::Rect
& rect
,
29 gfx::RectF
* opaque_rect
) OVERRIDE
{
31 paint
.setStyle(SkPaint::kStroke_Style
);
32 paint
.setStrokeWidth(SkIntToScalar(2));
33 paint
.setColor(SK_ColorWHITE
);
35 canvas
->clear(SK_ColorTRANSPARENT
);
36 gfx::Rect
inset_rect(rect
);
37 while (!inset_rect
.IsEmpty()) {
38 inset_rect
.Inset(3, 3, 2, 2);
40 SkRect::MakeXYWH(inset_rect
.x(), inset_rect
.y(),
41 inset_rect
.width(), inset_rect
.height()),
43 inset_rect
.Inset(3, 3, 2, 2);
48 TEST_F(LayerTreeHostMasksPixelTest
, MaskOfLayer
) {
49 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
50 gfx::Rect(200, 200), SK_ColorWHITE
);
52 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
53 gfx::Rect(50, 50, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
54 background
->AddChild(green
);
56 MaskContentLayerClient client
;
57 scoped_refptr
<ContentLayer
> mask
= ContentLayer::Create(&client
);
58 mask
->SetBounds(gfx::Size(100, 100));
59 mask
->SetIsDrawable(true);
60 mask
->SetIsMask(true);
61 green
->SetMaskLayer(mask
.get());
63 this->impl_side_painting_
= false;
64 RunPixelTest(GL_WITH_BITMAP
,
66 base::FilePath(FILE_PATH_LITERAL("mask_of_layer.png")));
69 TEST_F(LayerTreeHostMasksPixelTest
, ImageMaskOfLayer
) {
70 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
71 gfx::Rect(200, 200), SK_ColorWHITE
);
73 scoped_refptr
<ImageLayer
> mask
= ImageLayer::Create();
74 mask
->SetIsDrawable(true);
75 mask
->SetIsMask(true);
76 mask
->SetBounds(gfx::Size(100, 100));
79 bitmap
.setConfig(SkBitmap::kARGB_8888_Config
, 400, 400);
81 SkCanvas
canvas(bitmap
);
82 canvas
.scale(SkIntToScalar(4), SkIntToScalar(4));
83 MaskContentLayerClient client
;
84 client
.PaintContents(&canvas
,
87 mask
->SetBitmap(bitmap
);
89 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
90 gfx::Rect(50, 50, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
91 green
->SetMaskLayer(mask
.get());
92 background
->AddChild(green
);
94 this->impl_side_painting_
= false;
95 RunPixelTest(GL_WITH_BITMAP
,
97 base::FilePath(FILE_PATH_LITERAL("image_mask_of_layer.png")));
100 TEST_F(LayerTreeHostMasksPixelTest
, MaskOfClippedLayer
) {
101 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
102 gfx::Rect(200, 200), SK_ColorWHITE
);
104 // Clip to the top half of the green layer.
105 scoped_refptr
<Layer
> clip
= Layer::Create();
106 clip
->SetAnchorPoint(gfx::PointF(0.f
, 0.f
));
107 clip
->SetPosition(gfx::Point(0, 0));
108 clip
->SetBounds(gfx::Size(200, 100));
109 clip
->SetMasksToBounds(true);
110 background
->AddChild(clip
);
112 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
113 gfx::Rect(50, 50, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
114 clip
->AddChild(green
);
116 MaskContentLayerClient client
;
117 scoped_refptr
<ContentLayer
> mask
= ContentLayer::Create(&client
);
118 mask
->SetBounds(gfx::Size(100, 100));
119 mask
->SetIsDrawable(true);
120 mask
->SetIsMask(true);
121 green
->SetMaskLayer(mask
.get());
123 this->impl_side_painting_
= false;
124 RunPixelTest(GL_WITH_BITMAP
,
126 base::FilePath(FILE_PATH_LITERAL("mask_of_clipped_layer.png")));
129 TEST_F(LayerTreeHostMasksPixelTest
, MaskWithReplica
) {
130 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
131 gfx::Rect(200, 200), SK_ColorWHITE
);
133 MaskContentLayerClient client
;
134 scoped_refptr
<ContentLayer
> mask
= ContentLayer::Create(&client
);
135 mask
->SetBounds(gfx::Size(100, 100));
136 mask
->SetIsDrawable(true);
137 mask
->SetIsMask(true);
139 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
140 gfx::Rect(0, 0, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
141 background
->AddChild(green
);
142 green
->SetMaskLayer(mask
.get());
144 gfx::Transform replica_transform
;
145 replica_transform
.Rotate(-90.0);
147 scoped_refptr
<Layer
> replica
= Layer::Create();
148 replica
->SetAnchorPoint(gfx::PointF(0.5f
, 0.5f
));
149 replica
->SetPosition(gfx::Point(100, 100));
150 replica
->SetTransform(replica_transform
);
151 green
->SetReplicaLayer(replica
.get());
153 this->impl_side_painting_
= false;
154 RunPixelTest(GL_WITH_BITMAP
,
156 base::FilePath(FILE_PATH_LITERAL("mask_with_replica.png")));
159 TEST_F(LayerTreeHostMasksPixelTest
, MaskWithReplicaOfClippedLayer
) {
160 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
161 gfx::Rect(200, 200), SK_ColorWHITE
);
163 MaskContentLayerClient client
;
164 scoped_refptr
<ContentLayer
> mask
= ContentLayer::Create(&client
);
165 mask
->SetBounds(gfx::Size(100, 100));
166 mask
->SetIsDrawable(true);
167 mask
->SetIsMask(true);
169 // Clip to the bottom half of the green layer, and the left half of the
171 scoped_refptr
<Layer
> clip
= Layer::Create();
172 clip
->SetAnchorPoint(gfx::PointF(0.f
, 0.f
));
173 clip
->SetPosition(gfx::Point(0, 50));
174 clip
->SetBounds(gfx::Size(150, 150));
175 clip
->SetMasksToBounds(true);
176 background
->AddChild(clip
);
178 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
179 gfx::Rect(0, -50, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
180 clip
->AddChild(green
);
181 green
->SetMaskLayer(mask
.get());
183 gfx::Transform replica_transform
;
184 replica_transform
.Rotate(-90.0);
186 scoped_refptr
<Layer
> replica
= Layer::Create();
187 replica
->SetAnchorPoint(gfx::PointF(0.5f
, 0.5f
));
188 replica
->SetPosition(gfx::Point(100, 100));
189 replica
->SetTransform(replica_transform
);
190 green
->SetReplicaLayer(replica
.get());
192 this->impl_side_painting_
= false;
193 RunPixelTest(GL_WITH_BITMAP
,
195 base::FilePath(FILE_PATH_LITERAL(
196 "mask_with_replica_of_clipped_layer.png")));
199 TEST_F(LayerTreeHostMasksPixelTest
, MaskOfReplica
) {
200 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
201 gfx::Rect(200, 200), SK_ColorWHITE
);
203 MaskContentLayerClient client
;
204 scoped_refptr
<ContentLayer
> mask
= ContentLayer::Create(&client
);
205 mask
->SetBounds(gfx::Size(100, 100));
206 mask
->SetIsDrawable(true);
207 mask
->SetIsMask(true);
209 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
210 gfx::Rect(50, 0, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
211 background
->AddChild(green
);
213 scoped_refptr
<SolidColorLayer
> orange
= CreateSolidColorLayer(
214 gfx::Rect(-50, 50, 50, 50), kCSSOrange
);
215 green
->AddChild(orange
);
217 gfx::Transform replica_transform
;
218 replica_transform
.Rotate(180.0);
219 replica_transform
.Translate(100.0, 0.0);
221 scoped_refptr
<Layer
> replica
= Layer::Create();
222 replica
->SetAnchorPoint(gfx::PointF(1.f
, 1.f
));
223 replica
->SetPosition(gfx::Point());
224 replica
->SetTransform(replica_transform
);
225 replica
->SetMaskLayer(mask
.get());
226 green
->SetReplicaLayer(replica
.get());
228 this->impl_side_painting_
= false;
229 RunPixelTest(GL_WITH_BITMAP
,
231 base::FilePath(FILE_PATH_LITERAL("mask_of_replica.png")));
234 TEST_F(LayerTreeHostMasksPixelTest
, MaskOfReplicaOfClippedLayer
) {
235 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
236 gfx::Rect(200, 200), SK_ColorWHITE
);
238 MaskContentLayerClient client
;
239 scoped_refptr
<ContentLayer
> mask
= ContentLayer::Create(&client
);
240 mask
->SetBounds(gfx::Size(100, 100));
241 mask
->SetIsDrawable(true);
242 mask
->SetIsMask(true);
244 // Clip to the bottom 3/4 of the green layer, and the top 3/4 of the replica.
245 scoped_refptr
<Layer
> clip
= Layer::Create();
246 clip
->SetAnchorPoint(gfx::PointF(0.f
, 0.f
));
247 clip
->SetPosition(gfx::Point(0, 25));
248 clip
->SetBounds(gfx::Size(200, 150));
249 clip
->SetMasksToBounds(true);
250 background
->AddChild(clip
);
252 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
253 gfx::Rect(50, -25, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
254 clip
->AddChild(green
);
256 scoped_refptr
<SolidColorLayer
> orange
= CreateSolidColorLayer(
257 gfx::Rect(-50, 50, 50, 50), kCSSOrange
);
258 green
->AddChild(orange
);
260 gfx::Transform replica_transform
;
261 replica_transform
.Rotate(180.0);
262 replica_transform
.Translate(100.0, 0.0);
264 scoped_refptr
<Layer
> replica
= Layer::Create();
265 replica
->SetAnchorPoint(gfx::PointF(1.f
, 1.f
));
266 replica
->SetPosition(gfx::Point());
267 replica
->SetTransform(replica_transform
);
268 replica
->SetMaskLayer(mask
.get());
269 green
->SetReplicaLayer(replica
.get());
271 this->impl_side_painting_
= false;
272 RunPixelTest(GL_WITH_BITMAP
,
274 base::FilePath(FILE_PATH_LITERAL(
275 "mask_of_replica_of_clipped_layer.png")));