1 // Copyright 2011 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 "cc/layers/layer_impl.h"
7 #include "cc/output/filter_operation.h"
8 #include "cc/output/filter_operations.h"
9 #include "cc/test/fake_impl_proxy.h"
10 #include "cc/test/fake_layer_tree_host_impl.h"
11 #include "cc/test/fake_output_surface.h"
12 #include "cc/test/geometry_test_utils.h"
13 #include "cc/trees/layer_tree_impl.h"
14 #include "cc/trees/single_thread_proxy.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/skia/include/effects/SkBlurImageFilter.h"
22 #define EXECUTE_AND_VERIFY_SUBTREE_CHANGED(code_to_test) \
23 root->ResetAllChangeTrackingForSubtree(); \
25 EXPECT_TRUE(root->needs_push_properties()); \
26 EXPECT_FALSE(child->needs_push_properties()); \
27 EXPECT_FALSE(grand_child->needs_push_properties()); \
28 EXPECT_TRUE(root->LayerPropertyChanged()); \
29 EXPECT_TRUE(child->LayerPropertyChanged()); \
30 EXPECT_TRUE(grand_child->LayerPropertyChanged());
32 #define EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(code_to_test) \
33 root->ResetAllChangeTrackingForSubtree(); \
35 EXPECT_FALSE(root->needs_push_properties()); \
36 EXPECT_FALSE(child->needs_push_properties()); \
37 EXPECT_FALSE(grand_child->needs_push_properties()); \
38 EXPECT_FALSE(root->LayerPropertyChanged()); \
39 EXPECT_FALSE(child->LayerPropertyChanged()); \
40 EXPECT_FALSE(grand_child->LayerPropertyChanged());
42 #define EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE( \
44 root->ResetAllChangeTrackingForSubtree(); \
46 EXPECT_TRUE(root->needs_push_properties()); \
47 EXPECT_FALSE(child->needs_push_properties()); \
48 EXPECT_FALSE(grand_child->needs_push_properties()); \
49 EXPECT_FALSE(root->LayerPropertyChanged()); \
50 EXPECT_FALSE(child->LayerPropertyChanged()); \
51 EXPECT_FALSE(grand_child->LayerPropertyChanged());
53 #define EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(code_to_test) \
54 root->ResetAllChangeTrackingForSubtree(); \
56 EXPECT_TRUE(root->needs_push_properties()); \
57 EXPECT_FALSE(child->needs_push_properties()); \
58 EXPECT_FALSE(grand_child->needs_push_properties()); \
59 EXPECT_TRUE(root->LayerPropertyChanged()); \
60 EXPECT_FALSE(child->LayerPropertyChanged()); \
61 EXPECT_FALSE(grand_child->LayerPropertyChanged());
63 #define VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(code_to_test) \
64 root->ResetAllChangeTrackingForSubtree(); \
65 host_impl.ForcePrepareToDraw(); \
66 EXPECT_FALSE(host_impl.active_tree()->needs_update_draw_properties()); \
68 EXPECT_TRUE(host_impl.active_tree()->needs_update_draw_properties());
70 #define VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(code_to_test) \
71 root->ResetAllChangeTrackingForSubtree(); \
72 host_impl.ForcePrepareToDraw(); \
73 EXPECT_FALSE(host_impl.active_tree()->needs_update_draw_properties()); \
75 EXPECT_FALSE(host_impl.active_tree()->needs_update_draw_properties());
77 TEST(LayerImplTest
, VerifyLayerChangesAreTrackedProperly
) {
79 // This test checks that layerPropertyChanged() has the correct behavior.
82 // The constructor on this will fake that we are on the correct thread.
83 // Create a simple LayerImpl tree:
85 FakeLayerTreeHostImpl
host_impl(&proxy
);
86 EXPECT_TRUE(host_impl
.InitializeRenderer(
87 FakeOutputSurface::Create3d().PassAs
<OutputSurface
>()));
88 scoped_ptr
<LayerImpl
> root_clip
=
89 LayerImpl::Create(host_impl
.active_tree(), 1);
90 scoped_ptr
<LayerImpl
> root_ptr
=
91 LayerImpl::Create(host_impl
.active_tree(), 2);
92 LayerImpl
* root
= root_ptr
.get();
93 root_clip
->AddChild(root_ptr
.Pass());
94 scoped_ptr
<LayerImpl
> scroll_parent
=
95 LayerImpl::Create(host_impl
.active_tree(), 3);
96 LayerImpl
* scroll_child
= LayerImpl::Create(host_impl
.active_tree(), 4).get();
97 std::set
<LayerImpl
*>* scroll_children
= new std::set
<LayerImpl
*>();
98 scroll_children
->insert(scroll_child
);
99 scroll_children
->insert(root
);
101 scoped_ptr
<LayerImpl
> clip_parent
=
102 LayerImpl::Create(host_impl
.active_tree(), 5);
103 LayerImpl
* clip_child
= LayerImpl::Create(host_impl
.active_tree(), 6).get();
104 std::set
<LayerImpl
*>* clip_children
= new std::set
<LayerImpl
*>();
105 clip_children
->insert(clip_child
);
106 clip_children
->insert(root
);
108 root
->AddChild(LayerImpl::Create(host_impl
.active_tree(), 7));
109 LayerImpl
* child
= root
->children()[0];
110 child
->AddChild(LayerImpl::Create(host_impl
.active_tree(), 8));
111 LayerImpl
* grand_child
= child
->children()[0];
113 root
->SetScrollClipLayer(root_clip
->id());
115 // Adding children is an internal operation and should not mark layers as
117 EXPECT_FALSE(root
->LayerPropertyChanged());
118 EXPECT_FALSE(child
->LayerPropertyChanged());
119 EXPECT_FALSE(grand_child
->LayerPropertyChanged());
121 gfx::PointF arbitrary_point_f
= gfx::PointF(0.125f
, 0.25f
);
122 float arbitrary_number
= 0.352f
;
123 gfx::Size arbitrary_size
= gfx::Size(111, 222);
124 gfx::Point arbitrary_point
= gfx::Point(333, 444);
125 gfx::Vector2d arbitrary_vector2d
= gfx::Vector2d(111, 222);
126 gfx::Rect arbitrary_rect
= gfx::Rect(arbitrary_point
, arbitrary_size
);
127 gfx::RectF arbitrary_rect_f
=
128 gfx::RectF(arbitrary_point_f
, gfx::SizeF(1.234f
, 5.678f
));
129 SkColor arbitrary_color
= SkColorSetRGB(10, 20, 30);
130 gfx::Transform arbitrary_transform
;
131 arbitrary_transform
.Scale3d(0.1f
, 0.2f
, 0.3f
);
132 FilterOperations arbitrary_filters
;
133 arbitrary_filters
.Append(FilterOperation::CreateOpacityFilter(0.5f
));
134 SkXfermode::Mode arbitrary_blend_mode
= SkXfermode::kMultiply_Mode
;
136 // These properties are internal, and should not be considered "change" when
138 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
139 root
->SetUpdateRect(arbitrary_rect_f
));
140 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->SetBounds(arbitrary_size
));
142 // Changing these properties affects the entire subtree of layers.
143 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetAnchorPoint(arbitrary_point_f
));
144 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetAnchorPointZ(arbitrary_number
));
145 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetFilters(arbitrary_filters
));
146 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetFilters(FilterOperations()));
147 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
148 root
->SetMaskLayer(LayerImpl::Create(host_impl
.active_tree(), 9)));
149 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetMasksToBounds(true));
150 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetContentsOpaque(true));
151 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
152 root
->SetReplicaLayer(LayerImpl::Create(host_impl
.active_tree(), 10)));
153 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetPosition(arbitrary_point_f
));
154 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetShouldFlattenTransform(false));
155 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetIs3dSorted(true));
156 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
157 root
->SetDoubleSided(false)); // constructor initializes it to "true".
158 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->ScrollBy(arbitrary_vector2d
));
159 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetScrollDelta(gfx::Vector2d()));
160 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetScrollOffset(arbitrary_vector2d
));
161 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetHideLayerAndSubtree(true));
162 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetOpacity(arbitrary_number
));
163 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetBlendMode(arbitrary_blend_mode
));
164 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetTransform(arbitrary_transform
));
166 // Changing these properties only affects the layer itself.
167 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->SetContentBounds(arbitrary_size
));
168 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(
169 root
->SetContentsScale(arbitrary_number
, arbitrary_number
));
170 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->SetDrawsContent(true));
171 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(
172 root
->SetBackgroundColor(arbitrary_color
));
173 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(
174 root
->SetBackgroundFilters(arbitrary_filters
));
176 // Special case: check that SetBounds changes behavior depending on
178 root
->SetMasksToBounds(false);
179 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->SetBounds(gfx::Size(135, 246)));
180 root
->SetMasksToBounds(true);
181 // Should be a different size than previous call, to ensure it marks tree
183 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetBounds(arbitrary_size
));
185 // Changing this property does not cause the layer to be marked as changed
186 // but does cause the layer to need to push properties.
187 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
188 root
->SetIsRootForIsolatedGroup(true));
190 // Changing these properties should cause the layer to need to push properties
191 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
192 root
->SetScrollParent(scroll_parent
.get()));
193 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
194 root
->SetScrollChildren(scroll_children
));
195 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
196 root
->RemoveScrollChild(scroll_child
));
197 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
198 root
->SetClipParent(clip_parent
.get()));
199 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
200 root
->SetClipChildren(clip_children
));
201 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
202 root
->RemoveClipChild(clip_child
));
204 // After setting all these properties already, setting to the exact same
205 // values again should not cause any change.
206 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
207 root
->SetAnchorPoint(arbitrary_point_f
));
208 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
209 root
->SetAnchorPointZ(arbitrary_number
));
210 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->SetMasksToBounds(true));
211 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
212 root
->SetPosition(arbitrary_point_f
));
213 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
214 root
->SetShouldFlattenTransform(false));
215 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->SetIs3dSorted(true));
216 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
217 root
->SetTransform(arbitrary_transform
));
218 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
219 root
->SetDoubleSided(false)); // constructor initializes it to "true".
220 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
221 root
->SetScrollDelta(gfx::Vector2d()));
222 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
223 root
->SetScrollOffset(arbitrary_vector2d
));
224 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
225 root
->SetContentBounds(arbitrary_size
));
226 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
227 root
->SetContentsScale(arbitrary_number
, arbitrary_number
));
228 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->SetContentsOpaque(true));
229 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->SetOpacity(arbitrary_number
));
230 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
231 root
->SetBlendMode(arbitrary_blend_mode
));
232 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
233 root
->SetIsRootForIsolatedGroup(true));
234 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->SetDrawsContent(true));
235 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->SetBounds(arbitrary_size
));
236 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
237 root
->SetScrollParent(scroll_parent
.get()));
238 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
239 root
->SetScrollChildren(scroll_children
));
240 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
241 root
->SetClipParent(clip_parent
.get()));
242 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
243 root
->SetClipChildren(clip_children
));
246 TEST(LayerImplTest
, VerifyNeedsUpdateDrawProperties
) {
248 FakeLayerTreeHostImpl
host_impl(&proxy
);
249 EXPECT_TRUE(host_impl
.InitializeRenderer(
250 FakeOutputSurface::Create3d().PassAs
<OutputSurface
>()));
251 host_impl
.active_tree()->SetRootLayer(
252 LayerImpl::Create(host_impl
.active_tree(), 1));
253 LayerImpl
* root
= host_impl
.active_tree()->root_layer();
254 scoped_ptr
<LayerImpl
> layer_ptr
=
255 LayerImpl::Create(host_impl
.active_tree(), 2);
256 LayerImpl
* layer
= layer_ptr
.get();
257 root
->AddChild(layer_ptr
.Pass());
258 layer
->SetScrollClipLayer(root
->id());
259 DCHECK(host_impl
.CanDraw());
261 gfx::PointF arbitrary_point_f
= gfx::PointF(0.125f
, 0.25f
);
262 float arbitrary_number
= 0.352f
;
263 gfx::Size arbitrary_size
= gfx::Size(111, 222);
264 gfx::Point arbitrary_point
= gfx::Point(333, 444);
265 gfx::Vector2d arbitrary_vector2d
= gfx::Vector2d(111, 222);
266 gfx::Size large_size
= gfx::Size(1000, 1000);
267 gfx::Rect arbitrary_rect
= gfx::Rect(arbitrary_point
, arbitrary_size
);
268 gfx::RectF arbitrary_rect_f
=
269 gfx::RectF(arbitrary_point_f
, gfx::SizeF(1.234f
, 5.678f
));
270 SkColor arbitrary_color
= SkColorSetRGB(10, 20, 30);
271 gfx::Transform arbitrary_transform
;
272 arbitrary_transform
.Scale3d(0.1f
, 0.2f
, 0.3f
);
273 FilterOperations arbitrary_filters
;
274 arbitrary_filters
.Append(FilterOperation::CreateOpacityFilter(0.5f
));
275 SkXfermode::Mode arbitrary_blend_mode
= SkXfermode::kMultiply_Mode
;
277 // Related filter functions.
278 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetFilters(arbitrary_filters
));
279 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetFilters(arbitrary_filters
));
280 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetFilters(FilterOperations()));
281 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetFilters(arbitrary_filters
));
283 // Related scrolling functions.
284 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetBounds(large_size
));
285 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetBounds(large_size
));
286 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->ScrollBy(arbitrary_vector2d
));
287 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->ScrollBy(gfx::Vector2d()));
288 layer
->SetScrollDelta(gfx::Vector2d(0, 0));
289 host_impl
.ForcePrepareToDraw();
290 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
291 layer
->SetScrollDelta(arbitrary_vector2d
));
292 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
293 layer
->SetScrollDelta(arbitrary_vector2d
));
294 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
295 layer
->SetScrollOffset(arbitrary_vector2d
));
296 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
297 layer
->SetScrollOffset(arbitrary_vector2d
));
299 // Unrelated functions, always set to new values, always set needs update.
300 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetAnchorPointZ(arbitrary_number
));
301 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
302 layer
->SetMaskLayer(LayerImpl::Create(host_impl
.active_tree(), 4)));
303 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetMasksToBounds(true));
304 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetContentsOpaque(true));
305 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
306 layer
->SetReplicaLayer(LayerImpl::Create(host_impl
.active_tree(), 5)));
307 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetPosition(arbitrary_point_f
));
308 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetShouldFlattenTransform(false));
309 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetIs3dSorted(true));
311 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
312 layer
->SetDoubleSided(false)); // constructor initializes it to "true".
313 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetContentBounds(arbitrary_size
));
314 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
315 layer
->SetContentsScale(arbitrary_number
, arbitrary_number
));
316 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetDrawsContent(true));
317 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
318 layer
->SetBackgroundColor(arbitrary_color
));
319 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
320 layer
->SetBackgroundFilters(arbitrary_filters
));
321 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetOpacity(arbitrary_number
));
322 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
323 layer
->SetBlendMode(arbitrary_blend_mode
));
324 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetTransform(arbitrary_transform
));
325 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetBounds(arbitrary_size
));
327 // Unrelated functions, set to the same values, no needs update.
328 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
329 layer
->SetAnchorPointZ(arbitrary_number
));
330 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
331 layer
->SetIsRootForIsolatedGroup(true));
332 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetFilters(arbitrary_filters
));
333 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetMasksToBounds(true));
334 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetContentsOpaque(true));
335 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetPosition(arbitrary_point_f
));
336 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetIs3dSorted(true));
337 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
338 layer
->SetDoubleSided(false)); // constructor initializes it to "true".
339 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
340 layer
->SetContentBounds(arbitrary_size
));
341 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
342 layer
->SetContentsScale(arbitrary_number
, arbitrary_number
));
343 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetDrawsContent(true));
344 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
345 layer
->SetBackgroundColor(arbitrary_color
));
346 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
347 layer
->SetBackgroundFilters(arbitrary_filters
));
348 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetOpacity(arbitrary_number
));
349 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
350 layer
->SetBlendMode(arbitrary_blend_mode
));
351 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
352 layer
->SetIsRootForIsolatedGroup(true));
353 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
354 layer
->SetTransform(arbitrary_transform
));
355 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetBounds(arbitrary_size
));
358 TEST(LayerImplTest
, SafeOpaqueBackgroundColor
) {
360 FakeLayerTreeHostImpl
host_impl(&proxy
);
361 EXPECT_TRUE(host_impl
.InitializeRenderer(
362 FakeOutputSurface::Create3d().PassAs
<OutputSurface
>()));
363 scoped_ptr
<LayerImpl
> layer
= LayerImpl::Create(host_impl
.active_tree(), 1);
365 for (int contents_opaque
= 0; contents_opaque
< 2; ++contents_opaque
) {
366 for (int layer_opaque
= 0; layer_opaque
< 2; ++layer_opaque
) {
367 for (int host_opaque
= 0; host_opaque
< 2; ++host_opaque
) {
368 layer
->SetContentsOpaque(!!contents_opaque
);
369 layer
->SetBackgroundColor(layer_opaque
? SK_ColorRED
370 : SK_ColorTRANSPARENT
);
371 host_impl
.active_tree()->set_background_color(
372 host_opaque
? SK_ColorRED
: SK_ColorTRANSPARENT
);
374 SkColor safe_color
= layer
->SafeOpaqueBackgroundColor();
375 if (contents_opaque
) {
376 EXPECT_EQ(SkColorGetA(safe_color
), 255u)
377 << "Flags: " << contents_opaque
<< ", " << layer_opaque
<< ", "
378 << host_opaque
<< "\n";
380 EXPECT_NE(SkColorGetA(safe_color
), 255u)
381 << "Flags: " << contents_opaque
<< ", " << layer_opaque
<< ", "
382 << host_opaque
<< "\n";
389 class LayerImplScrollTest
: public testing::Test
{
391 LayerImplScrollTest() : host_impl_(&proxy_
), root_id_(7) {
392 host_impl_
.active_tree()
393 ->SetRootLayer(LayerImpl::Create(host_impl_
.active_tree(), root_id_
));
394 host_impl_
.active_tree()->root_layer()->AddChild(
395 LayerImpl::Create(host_impl_
.active_tree(), root_id_
+ 1));
396 layer()->SetScrollClipLayer(root_id_
);
397 // Set the max scroll offset by noting that the root layer has bounds (1,1),
398 // thus whatever bounds are set for the layer will be the max scroll
399 // offset plus 1 in each direction.
400 host_impl_
.active_tree()->root_layer()->SetBounds(gfx::Size(1, 1));
401 gfx::Vector2d
max_scroll_offset(51, 81);
402 layer()->SetBounds(gfx::Size(max_scroll_offset
.x(), max_scroll_offset
.y()));
406 return host_impl_
.active_tree()->root_layer()->children()[0];
410 FakeImplProxy proxy_
;
411 FakeLayerTreeHostImpl host_impl_
;
415 TEST_F(LayerImplScrollTest
, ScrollByWithZeroOffset
) {
416 // Test that LayerImpl::ScrollBy only affects ScrollDelta and total scroll
417 // offset is bounded by the range [0, max scroll offset].
419 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->TotalScrollOffset());
420 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->scroll_offset());
421 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
423 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
424 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
426 EXPECT_VECTOR_EQ(layer()->ScrollDelta(), layer()->TotalScrollOffset());
427 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->scroll_offset());
429 layer()->ScrollBy(gfx::Vector2dF(100, -100));
430 EXPECT_VECTOR_EQ(gfx::Vector2dF(50, 0), layer()->TotalScrollOffset());
432 EXPECT_VECTOR_EQ(layer()->ScrollDelta(), layer()->TotalScrollOffset());
433 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->scroll_offset());
436 TEST_F(LayerImplScrollTest
, ScrollByWithNonZeroOffset
) {
437 gfx::Vector2d
scroll_offset(10, 5);
438 layer()->SetScrollOffset(scroll_offset
);
440 EXPECT_VECTOR_EQ(scroll_offset
, layer()->TotalScrollOffset());
441 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
442 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
444 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
445 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
447 EXPECT_VECTOR_EQ(layer()->ScrollDelta() + scroll_offset
,
448 layer()->TotalScrollOffset());
449 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
451 layer()->ScrollBy(gfx::Vector2dF(100, -100));
452 EXPECT_VECTOR_EQ(gfx::Vector2dF(50, 0), layer()->TotalScrollOffset());
454 EXPECT_VECTOR_EQ(layer()->ScrollDelta() + scroll_offset
,
455 layer()->TotalScrollOffset());
456 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
459 class ScrollDelegateIgnore
: public LayerScrollOffsetDelegate
{
461 virtual void SetMaxScrollOffset(
462 const gfx::Vector2dF
& max_scroll_offset
) OVERRIDE
{}
463 virtual void SetTotalScrollOffset(const gfx::Vector2dF
& new_value
) OVERRIDE
{}
464 virtual gfx::Vector2dF
GetTotalScrollOffset() OVERRIDE
{
465 return fixed_offset_
;
467 virtual bool IsExternalFlingActive() const OVERRIDE
{ return false; }
469 void set_fixed_offset(const gfx::Vector2dF
& fixed_offset
) {
470 fixed_offset_
= fixed_offset
;
473 virtual void SetTotalPageScaleFactorAndLimits(
474 float page_scale_factor
,
475 float min_page_scale_factor
,
476 float max_page_scale_factor
) OVERRIDE
{}
477 virtual void SetScrollableSize(const gfx::SizeF
& scrollable_size
) OVERRIDE
{}
480 gfx::Vector2dF fixed_offset_
;
483 TEST_F(LayerImplScrollTest
, ScrollByWithIgnoringDelegate
) {
484 gfx::Vector2d
scroll_offset(10, 5);
485 layer()->SetScrollOffset(scroll_offset
);
487 EXPECT_VECTOR_EQ(scroll_offset
, layer()->TotalScrollOffset());
488 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
489 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
491 ScrollDelegateIgnore delegate
;
492 gfx::Vector2dF
fixed_offset(32, 12);
493 delegate
.set_fixed_offset(fixed_offset
);
494 layer()->SetScrollOffsetDelegate(&delegate
);
496 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
497 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
499 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
501 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
502 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
504 layer()->SetScrollOffsetDelegate(NULL
);
506 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
507 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
509 gfx::Vector2dF
scroll_delta(1, 1);
510 layer()->ScrollBy(scroll_delta
);
512 EXPECT_VECTOR_EQ(fixed_offset
+ scroll_delta
, layer()->TotalScrollOffset());
513 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
516 class ScrollDelegateAccept
: public LayerScrollOffsetDelegate
{
518 virtual void SetMaxScrollOffset(
519 const gfx::Vector2dF
& max_scroll_offset
) OVERRIDE
{}
520 virtual void SetTotalScrollOffset(const gfx::Vector2dF
& new_value
) OVERRIDE
{
521 current_offset_
= new_value
;
523 virtual gfx::Vector2dF
GetTotalScrollOffset() OVERRIDE
{
524 return current_offset_
;
526 virtual bool IsExternalFlingActive() const OVERRIDE
{ return false; }
527 virtual void SetTotalPageScaleFactorAndLimits(
528 float page_scale_factor
,
529 float min_page_scale_factor
,
530 float max_page_scale_factor
) OVERRIDE
{}
531 virtual void SetScrollableSize(const gfx::SizeF
& scrollable_size
) OVERRIDE
{}
534 gfx::Vector2dF current_offset_
;
537 TEST_F(LayerImplScrollTest
, ScrollByWithAcceptingDelegate
) {
538 gfx::Vector2d
scroll_offset(10, 5);
539 layer()->SetScrollOffset(scroll_offset
);
541 EXPECT_VECTOR_EQ(scroll_offset
, layer()->TotalScrollOffset());
542 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
543 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
545 ScrollDelegateAccept delegate
;
546 layer()->SetScrollOffsetDelegate(&delegate
);
548 EXPECT_VECTOR_EQ(scroll_offset
, layer()->TotalScrollOffset());
549 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
550 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
552 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
554 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
555 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
557 layer()->SetScrollOffsetDelegate(NULL
);
559 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
560 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
562 gfx::Vector2dF
scroll_delta(1, 1);
563 layer()->ScrollBy(scroll_delta
);
565 EXPECT_VECTOR_EQ(gfx::Vector2dF(1, 80), layer()->TotalScrollOffset());
566 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
569 TEST_F(LayerImplScrollTest
, ApplySentScrollsNoDelegate
) {
570 gfx::Vector2d
scroll_offset(10, 5);
571 gfx::Vector2dF
scroll_delta(20.5f
, 8.5f
);
572 gfx::Vector2d
sent_scroll_delta(12, -3);
574 layer()->SetScrollOffset(scroll_offset
);
575 layer()->ScrollBy(scroll_delta
);
576 layer()->SetSentScrollDelta(sent_scroll_delta
);
578 EXPECT_VECTOR_EQ(scroll_offset
+ scroll_delta
, layer()->TotalScrollOffset());
579 EXPECT_VECTOR_EQ(scroll_delta
, layer()->ScrollDelta());
580 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
581 EXPECT_VECTOR_EQ(sent_scroll_delta
, layer()->sent_scroll_delta());
583 layer()->ApplySentScrollDeltasFromAbortedCommit();
585 EXPECT_VECTOR_EQ(scroll_offset
+ scroll_delta
, layer()->TotalScrollOffset());
586 EXPECT_VECTOR_EQ(scroll_delta
- sent_scroll_delta
, layer()->ScrollDelta());
587 EXPECT_VECTOR_EQ(scroll_offset
+ sent_scroll_delta
, layer()->scroll_offset());
588 EXPECT_VECTOR_EQ(gfx::Vector2d(), layer()->sent_scroll_delta());
591 TEST_F(LayerImplScrollTest
, ApplySentScrollsWithIgnoringDelegate
) {
592 gfx::Vector2d
scroll_offset(10, 5);
593 gfx::Vector2d
sent_scroll_delta(12, -3);
594 gfx::Vector2dF
fixed_offset(32, 12);
596 layer()->SetScrollOffset(scroll_offset
);
597 ScrollDelegateIgnore delegate
;
598 delegate
.set_fixed_offset(fixed_offset
);
599 layer()->SetScrollOffsetDelegate(&delegate
);
600 layer()->SetSentScrollDelta(sent_scroll_delta
);
602 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
603 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
604 EXPECT_VECTOR_EQ(sent_scroll_delta
, layer()->sent_scroll_delta());
606 layer()->ApplySentScrollDeltasFromAbortedCommit();
608 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
609 EXPECT_VECTOR_EQ(scroll_offset
+ sent_scroll_delta
, layer()->scroll_offset());
610 EXPECT_VECTOR_EQ(gfx::Vector2d(), layer()->sent_scroll_delta());
613 TEST_F(LayerImplScrollTest
, ApplySentScrollsWithAcceptingDelegate
) {
614 gfx::Vector2d
scroll_offset(10, 5);
615 gfx::Vector2d
sent_scroll_delta(12, -3);
616 gfx::Vector2dF
scroll_delta(20.5f
, 8.5f
);
618 layer()->SetScrollOffset(scroll_offset
);
619 ScrollDelegateAccept delegate
;
620 layer()->SetScrollOffsetDelegate(&delegate
);
621 layer()->ScrollBy(scroll_delta
);
622 layer()->SetSentScrollDelta(sent_scroll_delta
);
624 EXPECT_VECTOR_EQ(scroll_offset
+ scroll_delta
, layer()->TotalScrollOffset());
625 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
626 EXPECT_VECTOR_EQ(sent_scroll_delta
, layer()->sent_scroll_delta());
628 layer()->ApplySentScrollDeltasFromAbortedCommit();
630 EXPECT_VECTOR_EQ(scroll_offset
+ scroll_delta
, layer()->TotalScrollOffset());
631 EXPECT_VECTOR_EQ(scroll_offset
+ sent_scroll_delta
, layer()->scroll_offset());
632 EXPECT_VECTOR_EQ(gfx::Vector2d(), layer()->sent_scroll_delta());
635 // The user-scrollability breaks for zoomed-in pages. So disable this.
636 // http://crbug.com/322223
637 TEST_F(LayerImplScrollTest
, DISABLED_ScrollUserUnscrollableLayer
) {
638 gfx::Vector2d
scroll_offset(10, 5);
639 gfx::Vector2dF
scroll_delta(20.5f
, 8.5f
);
641 layer()->set_user_scrollable_vertical(false);
642 layer()->SetScrollOffset(scroll_offset
);
643 gfx::Vector2dF unscrolled
= layer()->ScrollBy(scroll_delta
);
645 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 8.5f
), unscrolled
);
646 EXPECT_VECTOR_EQ(gfx::Vector2dF(30.5f
, 5), layer()->TotalScrollOffset());