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 cc::ContentLayerClient
{
22 MaskContentLayerClient() {}
23 virtual ~MaskContentLayerClient() {}
25 virtual void DidChangeLayerCanUseLCDText() OVERRIDE
{}
27 virtual void PaintContents(SkCanvas
* canvas
,
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 while (!rect
.IsEmpty()) {
37 rect
.Inset(3, 3, 2, 2);
39 SkRect::MakeXYWH(rect
.x(), rect
.y(), rect
.width(), rect
.height()),
41 rect
.Inset(3, 3, 2, 2);
46 TEST_F(LayerTreeHostMasksPixelTest
, MaskOfLayer
) {
47 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
48 gfx::Rect(200, 200), SK_ColorWHITE
);
50 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
51 gfx::Rect(50, 50, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
52 background
->AddChild(green
);
54 MaskContentLayerClient client
;
55 scoped_refptr
<ContentLayer
> mask
= ContentLayer::Create(&client
);
56 mask
->SetBounds(gfx::Size(100, 100));
57 mask
->SetIsDrawable(true);
58 mask
->SetIsMask(true);
59 green
->SetMaskLayer(mask
.get());
61 RunPixelTest(GL_WITH_BITMAP
,
63 base::FilePath(FILE_PATH_LITERAL("mask_of_layer.png")));
66 TEST_F(LayerTreeHostMasksPixelTest
, ImageMaskOfLayer
) {
67 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
68 gfx::Rect(200, 200), SK_ColorWHITE
);
70 scoped_refptr
<ImageLayer
> mask
= ImageLayer::Create();
71 mask
->SetIsDrawable(true);
72 mask
->SetIsMask(true);
73 mask
->SetBounds(gfx::Size(100, 100));
76 bitmap
.setConfig(SkBitmap::kARGB_8888_Config
, 400, 400);
78 SkCanvas
canvas(bitmap
);
79 canvas
.scale(SkIntToScalar(4), SkIntToScalar(4));
80 MaskContentLayerClient client
;
81 client
.PaintContents(&canvas
,
84 mask
->SetBitmap(bitmap
);
86 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
87 gfx::Rect(50, 50, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
88 green
->SetMaskLayer(mask
.get());
89 background
->AddChild(green
);
91 RunPixelTest(GL_WITH_BITMAP
,
93 base::FilePath(FILE_PATH_LITERAL("image_mask_of_layer.png")));
96 TEST_F(LayerTreeHostMasksPixelTest
, MaskOfClippedLayer
) {
97 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
98 gfx::Rect(200, 200), SK_ColorWHITE
);
100 // Clip to the top half of the green layer.
101 scoped_refptr
<Layer
> clip
= Layer::Create();
102 clip
->SetAnchorPoint(gfx::PointF(0.f
, 0.f
));
103 clip
->SetPosition(gfx::Point(0, 0));
104 clip
->SetBounds(gfx::Size(200, 100));
105 clip
->SetMasksToBounds(true);
106 background
->AddChild(clip
);
108 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
109 gfx::Rect(50, 50, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
110 clip
->AddChild(green
);
112 MaskContentLayerClient client
;
113 scoped_refptr
<ContentLayer
> mask
= ContentLayer::Create(&client
);
114 mask
->SetBounds(gfx::Size(100, 100));
115 mask
->SetIsDrawable(true);
116 mask
->SetIsMask(true);
117 green
->SetMaskLayer(mask
.get());
119 RunPixelTest(GL_WITH_BITMAP
,
121 base::FilePath(FILE_PATH_LITERAL("mask_of_clipped_layer.png")));
124 TEST_F(LayerTreeHostMasksPixelTest
, MaskWithReplica
) {
125 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
126 gfx::Rect(200, 200), SK_ColorWHITE
);
128 MaskContentLayerClient client
;
129 scoped_refptr
<ContentLayer
> mask
= ContentLayer::Create(&client
);
130 mask
->SetBounds(gfx::Size(100, 100));
131 mask
->SetIsDrawable(true);
132 mask
->SetIsMask(true);
134 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
135 gfx::Rect(0, 0, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
136 background
->AddChild(green
);
137 green
->SetMaskLayer(mask
.get());
139 gfx::Transform replica_transform
;
140 replica_transform
.Rotate(-90.0);
142 scoped_refptr
<Layer
> replica
= Layer::Create();
143 replica
->SetAnchorPoint(gfx::PointF(0.5f
, 0.5f
));
144 replica
->SetPosition(gfx::Point(100, 100));
145 replica
->SetTransform(replica_transform
);
146 green
->SetReplicaLayer(replica
.get());
148 RunPixelTest(GL_WITH_BITMAP
,
150 base::FilePath(FILE_PATH_LITERAL("mask_with_replica.png")));
153 TEST_F(LayerTreeHostMasksPixelTest
, MaskWithReplicaOfClippedLayer
) {
154 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
155 gfx::Rect(200, 200), SK_ColorWHITE
);
157 MaskContentLayerClient client
;
158 scoped_refptr
<ContentLayer
> mask
= ContentLayer::Create(&client
);
159 mask
->SetBounds(gfx::Size(100, 100));
160 mask
->SetIsDrawable(true);
161 mask
->SetIsMask(true);
163 // Clip to the bottom half of the green layer, and the left half of the
165 scoped_refptr
<Layer
> clip
= Layer::Create();
166 clip
->SetAnchorPoint(gfx::PointF(0.f
, 0.f
));
167 clip
->SetPosition(gfx::Point(0, 50));
168 clip
->SetBounds(gfx::Size(150, 150));
169 clip
->SetMasksToBounds(true);
170 background
->AddChild(clip
);
172 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
173 gfx::Rect(0, -50, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
174 clip
->AddChild(green
);
175 green
->SetMaskLayer(mask
.get());
177 gfx::Transform replica_transform
;
178 replica_transform
.Rotate(-90.0);
180 scoped_refptr
<Layer
> replica
= Layer::Create();
181 replica
->SetAnchorPoint(gfx::PointF(0.5f
, 0.5f
));
182 replica
->SetPosition(gfx::Point(100, 100));
183 replica
->SetTransform(replica_transform
);
184 green
->SetReplicaLayer(replica
.get());
186 RunPixelTest(GL_WITH_BITMAP
,
188 base::FilePath(FILE_PATH_LITERAL(
189 "mask_with_replica_of_clipped_layer.png")));
192 TEST_F(LayerTreeHostMasksPixelTest
, MaskOfReplica
) {
193 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
194 gfx::Rect(200, 200), SK_ColorWHITE
);
196 MaskContentLayerClient client
;
197 scoped_refptr
<ContentLayer
> mask
= ContentLayer::Create(&client
);
198 mask
->SetBounds(gfx::Size(100, 100));
199 mask
->SetIsDrawable(true);
200 mask
->SetIsMask(true);
202 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
203 gfx::Rect(50, 0, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
204 background
->AddChild(green
);
206 scoped_refptr
<SolidColorLayer
> orange
= CreateSolidColorLayer(
207 gfx::Rect(-50, 50, 50, 50), kCSSOrange
);
208 green
->AddChild(orange
);
210 gfx::Transform replica_transform
;
211 replica_transform
.Rotate(180.0);
212 replica_transform
.Translate(100.0, 0.0);
214 scoped_refptr
<Layer
> replica
= Layer::Create();
215 replica
->SetAnchorPoint(gfx::PointF(1.f
, 1.f
));
216 replica
->SetPosition(gfx::Point());
217 replica
->SetTransform(replica_transform
);
218 replica
->SetMaskLayer(mask
.get());
219 green
->SetReplicaLayer(replica
.get());
221 RunPixelTest(GL_WITH_BITMAP
,
223 base::FilePath(FILE_PATH_LITERAL("mask_of_replica.png")));
226 TEST_F(LayerTreeHostMasksPixelTest
, MaskOfReplicaOfClippedLayer
) {
227 scoped_refptr
<SolidColorLayer
> background
= CreateSolidColorLayer(
228 gfx::Rect(200, 200), SK_ColorWHITE
);
230 MaskContentLayerClient client
;
231 scoped_refptr
<ContentLayer
> mask
= ContentLayer::Create(&client
);
232 mask
->SetBounds(gfx::Size(100, 100));
233 mask
->SetIsDrawable(true);
234 mask
->SetIsMask(true);
236 // Clip to the bottom 3/4 of the green layer, and the top 3/4 of the replica.
237 scoped_refptr
<Layer
> clip
= Layer::Create();
238 clip
->SetAnchorPoint(gfx::PointF(0.f
, 0.f
));
239 clip
->SetPosition(gfx::Point(0, 25));
240 clip
->SetBounds(gfx::Size(200, 150));
241 clip
->SetMasksToBounds(true);
242 background
->AddChild(clip
);
244 scoped_refptr
<SolidColorLayer
> green
= CreateSolidColorLayerWithBorder(
245 gfx::Rect(50, -25, 100, 100), kCSSGreen
, 1, SK_ColorBLACK
);
246 clip
->AddChild(green
);
248 scoped_refptr
<SolidColorLayer
> orange
= CreateSolidColorLayer(
249 gfx::Rect(-50, 50, 50, 50), kCSSOrange
);
250 green
->AddChild(orange
);
252 gfx::Transform replica_transform
;
253 replica_transform
.Rotate(180.0);
254 replica_transform
.Translate(100.0, 0.0);
256 scoped_refptr
<Layer
> replica
= Layer::Create();
257 replica
->SetAnchorPoint(gfx::PointF(1.f
, 1.f
));
258 replica
->SetPosition(gfx::Point());
259 replica
->SetTransform(replica_transform
);
260 replica
->SetMaskLayer(mask
.get());
261 green
->SetReplicaLayer(replica
.get());
263 RunPixelTest(GL_WITH_BITMAP
,
265 base::FilePath(FILE_PATH_LITERAL(
266 "mask_of_replica_of_clipped_layer.png")));