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/layers/painted_scrollbar_layer_impl.h"
8 #include "cc/output/filter_operation.h"
9 #include "cc/output/filter_operations.h"
10 #include "cc/test/fake_impl_proxy.h"
11 #include "cc/test/fake_layer_tree_host_impl.h"
12 #include "cc/test/fake_output_surface.h"
13 #include "cc/test/geometry_test_utils.h"
14 #include "cc/test/test_shared_bitmap_manager.h"
15 #include "cc/trees/layer_tree_impl.h"
16 #include "cc/trees/single_thread_proxy.h"
17 #include "testing/gmock/include/gmock/gmock.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "third_party/skia/include/effects/SkBlurImageFilter.h"
24 #define EXECUTE_AND_VERIFY_SUBTREE_CHANGED(code_to_test) \
25 root->ResetAllChangeTrackingForSubtree(); \
27 EXPECT_TRUE(root->needs_push_properties()); \
28 EXPECT_FALSE(child->needs_push_properties()); \
29 EXPECT_FALSE(grand_child->needs_push_properties()); \
30 EXPECT_TRUE(root->LayerPropertyChanged()); \
31 EXPECT_TRUE(child->LayerPropertyChanged()); \
32 EXPECT_TRUE(grand_child->LayerPropertyChanged());
34 #define EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(code_to_test) \
35 root->ResetAllChangeTrackingForSubtree(); \
37 EXPECT_FALSE(root->needs_push_properties()); \
38 EXPECT_FALSE(child->needs_push_properties()); \
39 EXPECT_FALSE(grand_child->needs_push_properties()); \
40 EXPECT_FALSE(root->LayerPropertyChanged()); \
41 EXPECT_FALSE(child->LayerPropertyChanged()); \
42 EXPECT_FALSE(grand_child->LayerPropertyChanged());
44 #define EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE( \
46 root->ResetAllChangeTrackingForSubtree(); \
48 EXPECT_TRUE(root->needs_push_properties()); \
49 EXPECT_FALSE(child->needs_push_properties()); \
50 EXPECT_FALSE(grand_child->needs_push_properties()); \
51 EXPECT_FALSE(root->LayerPropertyChanged()); \
52 EXPECT_FALSE(child->LayerPropertyChanged()); \
53 EXPECT_FALSE(grand_child->LayerPropertyChanged());
55 #define EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(code_to_test) \
56 root->ResetAllChangeTrackingForSubtree(); \
58 EXPECT_TRUE(root->needs_push_properties()); \
59 EXPECT_FALSE(child->needs_push_properties()); \
60 EXPECT_FALSE(grand_child->needs_push_properties()); \
61 EXPECT_TRUE(root->LayerPropertyChanged()); \
62 EXPECT_FALSE(child->LayerPropertyChanged()); \
63 EXPECT_FALSE(grand_child->LayerPropertyChanged());
65 #define VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(code_to_test) \
66 root->ResetAllChangeTrackingForSubtree(); \
67 host_impl.ForcePrepareToDraw(); \
68 EXPECT_FALSE(host_impl.active_tree()->needs_update_draw_properties()); \
70 EXPECT_TRUE(host_impl.active_tree()->needs_update_draw_properties());
72 #define VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(code_to_test) \
73 root->ResetAllChangeTrackingForSubtree(); \
74 host_impl.ForcePrepareToDraw(); \
75 EXPECT_FALSE(host_impl.active_tree()->needs_update_draw_properties()); \
77 EXPECT_FALSE(host_impl.active_tree()->needs_update_draw_properties());
79 TEST(LayerImplTest
, VerifyLayerChangesAreTrackedProperly
) {
81 // This test checks that layerPropertyChanged() has the correct behavior.
84 // The constructor on this will fake that we are on the correct thread.
85 // Create a simple LayerImpl tree:
87 TestSharedBitmapManager shared_bitmap_manager
;
88 FakeLayerTreeHostImpl
host_impl(&proxy
, &shared_bitmap_manager
);
89 EXPECT_TRUE(host_impl
.InitializeRenderer(
90 FakeOutputSurface::Create3d().PassAs
<OutputSurface
>()));
91 scoped_ptr
<LayerImpl
> root_clip
=
92 LayerImpl::Create(host_impl
.active_tree(), 1);
93 scoped_ptr
<LayerImpl
> root_ptr
=
94 LayerImpl::Create(host_impl
.active_tree(), 2);
95 LayerImpl
* root
= root_ptr
.get();
96 root_clip
->AddChild(root_ptr
.Pass());
97 scoped_ptr
<LayerImpl
> scroll_parent
=
98 LayerImpl::Create(host_impl
.active_tree(), 3);
99 LayerImpl
* scroll_child
= LayerImpl::Create(host_impl
.active_tree(), 4).get();
100 std::set
<LayerImpl
*>* scroll_children
= new std::set
<LayerImpl
*>();
101 scroll_children
->insert(scroll_child
);
102 scroll_children
->insert(root
);
104 scoped_ptr
<LayerImpl
> clip_parent
=
105 LayerImpl::Create(host_impl
.active_tree(), 5);
106 LayerImpl
* clip_child
= LayerImpl::Create(host_impl
.active_tree(), 6).get();
107 std::set
<LayerImpl
*>* clip_children
= new std::set
<LayerImpl
*>();
108 clip_children
->insert(clip_child
);
109 clip_children
->insert(root
);
111 root
->AddChild(LayerImpl::Create(host_impl
.active_tree(), 7));
112 LayerImpl
* child
= root
->children()[0];
113 child
->AddChild(LayerImpl::Create(host_impl
.active_tree(), 8));
114 LayerImpl
* grand_child
= child
->children()[0];
116 root
->SetScrollClipLayer(root_clip
->id());
118 // Adding children is an internal operation and should not mark layers as
120 EXPECT_FALSE(root
->LayerPropertyChanged());
121 EXPECT_FALSE(child
->LayerPropertyChanged());
122 EXPECT_FALSE(grand_child
->LayerPropertyChanged());
124 gfx::PointF arbitrary_point_f
= gfx::PointF(0.125f
, 0.25f
);
125 gfx::Point3F arbitrary_point_3f
= gfx::Point3F(0.125f
, 0.25f
, 0.f
);
126 float arbitrary_number
= 0.352f
;
127 gfx::Size arbitrary_size
= gfx::Size(111, 222);
128 gfx::Point arbitrary_point
= gfx::Point(333, 444);
129 gfx::Vector2d arbitrary_vector2d
= gfx::Vector2d(111, 222);
130 gfx::Rect arbitrary_rect
= gfx::Rect(arbitrary_point
, arbitrary_size
);
131 gfx::RectF arbitrary_rect_f
=
132 gfx::RectF(arbitrary_point_f
, gfx::SizeF(1.234f
, 5.678f
));
133 SkColor arbitrary_color
= SkColorSetRGB(10, 20, 30);
134 gfx::Transform arbitrary_transform
;
135 arbitrary_transform
.Scale3d(0.1f
, 0.2f
, 0.3f
);
136 FilterOperations arbitrary_filters
;
137 arbitrary_filters
.Append(FilterOperation::CreateOpacityFilter(0.5f
));
138 SkXfermode::Mode arbitrary_blend_mode
= SkXfermode::kMultiply_Mode
;
140 // These properties are internal, and should not be considered "change" when
142 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
143 root
->SetUpdateRect(arbitrary_rect_f
));
144 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->SetBounds(arbitrary_size
));
146 // Changing these properties affects the entire subtree of layers.
147 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
148 root
->SetTransformOrigin(arbitrary_point_3f
));
149 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetFilters(arbitrary_filters
));
150 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetFilters(FilterOperations()));
151 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
152 root
->SetMaskLayer(LayerImpl::Create(host_impl
.active_tree(), 9)));
153 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetMasksToBounds(true));
154 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetContentsOpaque(true));
155 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
156 root
->SetReplicaLayer(LayerImpl::Create(host_impl
.active_tree(), 10)));
157 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetPosition(arbitrary_point_f
));
158 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetShouldFlattenTransform(false));
159 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->Set3dSortingContextId(1));
160 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
161 root
->SetDoubleSided(false)); // constructor initializes it to "true".
162 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->ScrollBy(arbitrary_vector2d
));
163 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetScrollDelta(gfx::Vector2d()));
164 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetScrollOffset(arbitrary_vector2d
));
165 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetHideLayerAndSubtree(true));
166 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetOpacity(arbitrary_number
));
167 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetBlendMode(arbitrary_blend_mode
));
168 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetTransform(arbitrary_transform
));
170 // Changing these properties only affects the layer itself.
171 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->SetContentBounds(arbitrary_size
));
172 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(
173 root
->SetContentsScale(arbitrary_number
, arbitrary_number
));
174 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->SetDrawsContent(true));
175 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(
176 root
->SetBackgroundColor(arbitrary_color
));
177 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(
178 root
->SetBackgroundFilters(arbitrary_filters
));
180 // Special case: check that SetBounds changes behavior depending on
182 root
->SetMasksToBounds(false);
183 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->SetBounds(gfx::Size(135, 246)));
184 root
->SetMasksToBounds(true);
185 // Should be a different size than previous call, to ensure it marks tree
187 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetBounds(arbitrary_size
));
189 // Changing this property does not cause the layer to be marked as changed
190 // but does cause the layer to need to push properties.
191 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
192 root
->SetIsRootForIsolatedGroup(true));
194 // Changing these properties should cause the layer to need to push properties
195 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
196 root
->SetScrollParent(scroll_parent
.get()));
197 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
198 root
->SetScrollChildren(scroll_children
));
199 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
200 root
->SetClipParent(clip_parent
.get()));
201 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
202 root
->SetClipChildren(clip_children
));
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
->SetTransformOrigin(arbitrary_point_3f
));
208 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->SetMasksToBounds(true));
209 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
210 root
->SetPosition(arbitrary_point_f
));
211 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
212 root
->SetShouldFlattenTransform(false));
213 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->Set3dSortingContextId(1));
214 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
215 root
->SetTransform(arbitrary_transform
));
216 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
217 root
->SetDoubleSided(false)); // constructor initializes it to "true".
218 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
219 root
->SetScrollDelta(gfx::Vector2d()));
220 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
221 root
->SetScrollOffset(arbitrary_vector2d
));
222 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
223 root
->SetContentBounds(arbitrary_size
));
224 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
225 root
->SetContentsScale(arbitrary_number
, arbitrary_number
));
226 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->SetContentsOpaque(true));
227 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->SetOpacity(arbitrary_number
));
228 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
229 root
->SetBlendMode(arbitrary_blend_mode
));
230 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
231 root
->SetIsRootForIsolatedGroup(true));
232 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->SetDrawsContent(true));
233 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->SetBounds(arbitrary_size
));
234 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
235 root
->SetScrollParent(scroll_parent
.get()));
236 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
237 root
->SetScrollChildren(scroll_children
));
238 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
239 root
->SetClipParent(clip_parent
.get()));
240 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
241 root
->SetClipChildren(clip_children
));
244 TEST(LayerImplTest
, VerifyNeedsUpdateDrawProperties
) {
246 TestSharedBitmapManager shared_bitmap_manager
;
247 FakeLayerTreeHostImpl
host_impl(&proxy
, &shared_bitmap_manager
);
248 EXPECT_TRUE(host_impl
.InitializeRenderer(
249 FakeOutputSurface::Create3d().PassAs
<OutputSurface
>()));
250 host_impl
.active_tree()->SetRootLayer(
251 LayerImpl::Create(host_impl
.active_tree(), 1));
252 LayerImpl
* root
= host_impl
.active_tree()->root_layer();
253 scoped_ptr
<LayerImpl
> layer_ptr
=
254 LayerImpl::Create(host_impl
.active_tree(), 2);
255 LayerImpl
* layer
= layer_ptr
.get();
256 root
->AddChild(layer_ptr
.Pass());
257 layer
->SetScrollClipLayer(root
->id());
258 DCHECK(host_impl
.CanDraw());
260 gfx::PointF arbitrary_point_f
= gfx::PointF(0.125f
, 0.25f
);
261 float arbitrary_number
= 0.352f
;
262 gfx::Size arbitrary_size
= gfx::Size(111, 222);
263 gfx::Point arbitrary_point
= gfx::Point(333, 444);
264 gfx::Vector2d arbitrary_vector2d
= gfx::Vector2d(111, 222);
265 gfx::Size large_size
= gfx::Size(1000, 1000);
266 gfx::Rect arbitrary_rect
= gfx::Rect(arbitrary_point
, arbitrary_size
);
267 gfx::RectF arbitrary_rect_f
=
268 gfx::RectF(arbitrary_point_f
, gfx::SizeF(1.234f
, 5.678f
));
269 SkColor arbitrary_color
= SkColorSetRGB(10, 20, 30);
270 gfx::Transform arbitrary_transform
;
271 arbitrary_transform
.Scale3d(0.1f
, 0.2f
, 0.3f
);
272 FilterOperations arbitrary_filters
;
273 arbitrary_filters
.Append(FilterOperation::CreateOpacityFilter(0.5f
));
274 SkXfermode::Mode arbitrary_blend_mode
= SkXfermode::kMultiply_Mode
;
276 // Related filter functions.
277 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetFilters(arbitrary_filters
));
278 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetFilters(arbitrary_filters
));
279 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetFilters(FilterOperations()));
280 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetFilters(arbitrary_filters
));
282 // Related scrolling functions.
283 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetBounds(large_size
));
284 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetBounds(large_size
));
285 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->ScrollBy(arbitrary_vector2d
));
286 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->ScrollBy(gfx::Vector2d()));
287 layer
->SetScrollDelta(gfx::Vector2d(0, 0));
288 host_impl
.ForcePrepareToDraw();
289 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
290 layer
->SetScrollDelta(arbitrary_vector2d
));
291 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
292 layer
->SetScrollDelta(arbitrary_vector2d
));
293 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
294 layer
->SetScrollOffset(arbitrary_vector2d
));
295 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
296 layer
->SetScrollOffset(arbitrary_vector2d
));
298 // Unrelated functions, always set to new values, always set needs update.
299 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
300 layer
->SetMaskLayer(LayerImpl::Create(host_impl
.active_tree(), 4)));
301 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetMasksToBounds(true));
302 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetContentsOpaque(true));
303 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
304 layer
->SetReplicaLayer(LayerImpl::Create(host_impl
.active_tree(), 5)));
305 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetPosition(arbitrary_point_f
));
306 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetShouldFlattenTransform(false));
307 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->Set3dSortingContextId(1));
309 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
310 layer
->SetDoubleSided(false)); // constructor initializes it to "true".
311 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetContentBounds(arbitrary_size
));
312 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
313 layer
->SetContentsScale(arbitrary_number
, arbitrary_number
));
314 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetDrawsContent(true));
315 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
316 layer
->SetBackgroundColor(arbitrary_color
));
317 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
318 layer
->SetBackgroundFilters(arbitrary_filters
));
319 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetOpacity(arbitrary_number
));
320 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
321 layer
->SetBlendMode(arbitrary_blend_mode
));
322 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetTransform(arbitrary_transform
));
323 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetBounds(arbitrary_size
));
325 // Unrelated functions, set to the same values, no needs update.
326 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
327 layer
->SetIsRootForIsolatedGroup(true));
328 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetFilters(arbitrary_filters
));
329 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetMasksToBounds(true));
330 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetContentsOpaque(true));
331 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetPosition(arbitrary_point_f
));
332 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->Set3dSortingContextId(1));
333 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
334 layer
->SetDoubleSided(false)); // constructor initializes it to "true".
335 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
336 layer
->SetContentBounds(arbitrary_size
));
337 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
338 layer
->SetContentsScale(arbitrary_number
, arbitrary_number
));
339 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetDrawsContent(true));
340 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
341 layer
->SetBackgroundColor(arbitrary_color
));
342 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
343 layer
->SetBackgroundFilters(arbitrary_filters
));
344 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetOpacity(arbitrary_number
));
345 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
346 layer
->SetBlendMode(arbitrary_blend_mode
));
347 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
348 layer
->SetIsRootForIsolatedGroup(true));
349 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
350 layer
->SetTransform(arbitrary_transform
));
351 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetBounds(arbitrary_size
));
354 TEST(LayerImplTest
, SafeOpaqueBackgroundColor
) {
356 TestSharedBitmapManager shared_bitmap_manager
;
357 FakeLayerTreeHostImpl
host_impl(&proxy
, &shared_bitmap_manager
);
358 EXPECT_TRUE(host_impl
.InitializeRenderer(
359 FakeOutputSurface::Create3d().PassAs
<OutputSurface
>()));
360 scoped_ptr
<LayerImpl
> layer
= LayerImpl::Create(host_impl
.active_tree(), 1);
362 for (int contents_opaque
= 0; contents_opaque
< 2; ++contents_opaque
) {
363 for (int layer_opaque
= 0; layer_opaque
< 2; ++layer_opaque
) {
364 for (int host_opaque
= 0; host_opaque
< 2; ++host_opaque
) {
365 layer
->SetContentsOpaque(!!contents_opaque
);
366 layer
->SetBackgroundColor(layer_opaque
? SK_ColorRED
367 : SK_ColorTRANSPARENT
);
368 host_impl
.active_tree()->set_background_color(
369 host_opaque
? SK_ColorRED
: SK_ColorTRANSPARENT
);
371 SkColor safe_color
= layer
->SafeOpaqueBackgroundColor();
372 if (contents_opaque
) {
373 EXPECT_EQ(SkColorGetA(safe_color
), 255u)
374 << "Flags: " << contents_opaque
<< ", " << layer_opaque
<< ", "
375 << host_opaque
<< "\n";
377 EXPECT_NE(SkColorGetA(safe_color
), 255u)
378 << "Flags: " << contents_opaque
<< ", " << layer_opaque
<< ", "
379 << host_opaque
<< "\n";
386 TEST(LayerImplTest
, TransformInvertibility
) {
388 TestSharedBitmapManager shared_bitmap_manager
;
389 FakeLayerTreeHostImpl
host_impl(&proxy
, &shared_bitmap_manager
);
391 scoped_ptr
<LayerImpl
> layer
= LayerImpl::Create(host_impl
.active_tree(), 1);
392 EXPECT_TRUE(layer
->transform().IsInvertible());
393 EXPECT_TRUE(layer
->transform_is_invertible());
395 gfx::Transform transform
;
397 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
398 layer
->SetTransform(transform
);
399 EXPECT_FALSE(layer
->transform().IsInvertible());
400 EXPECT_FALSE(layer
->transform_is_invertible());
402 transform
.MakeIdentity();
403 transform
.ApplyPerspectiveDepth(SkDoubleToMScalar(100.0));
404 transform
.RotateAboutZAxis(75.0);
405 transform
.RotateAboutXAxis(32.2);
406 transform
.RotateAboutZAxis(-75.0);
407 transform
.Translate3d(SkDoubleToMScalar(50.5),
408 SkDoubleToMScalar(42.42),
409 SkDoubleToMScalar(-100.25));
411 layer
->SetTransform(transform
);
412 EXPECT_TRUE(layer
->transform().IsInvertible());
413 EXPECT_TRUE(layer
->transform_is_invertible());
416 class LayerImplScrollTest
: public testing::Test
{
418 LayerImplScrollTest()
419 : host_impl_(&proxy_
, &shared_bitmap_manager_
), root_id_(7) {
420 host_impl_
.active_tree()->SetRootLayer(
421 LayerImpl::Create(host_impl_
.active_tree(), root_id_
));
422 host_impl_
.active_tree()->root_layer()->AddChild(
423 LayerImpl::Create(host_impl_
.active_tree(), root_id_
+ 1));
424 layer()->SetScrollClipLayer(root_id_
);
425 // Set the max scroll offset by noting that the root layer has bounds (1,1),
426 // thus whatever bounds are set for the layer will be the max scroll
427 // offset plus 1 in each direction.
428 host_impl_
.active_tree()->root_layer()->SetBounds(gfx::Size(1, 1));
429 gfx::Vector2d
max_scroll_offset(51, 81);
430 layer()->SetBounds(gfx::Size(max_scroll_offset
.x(), max_scroll_offset
.y()));
434 return host_impl_
.active_tree()->root_layer()->children()[0];
437 LayerTreeImpl
* tree() { return host_impl_
.active_tree(); }
440 FakeImplProxy proxy_
;
441 TestSharedBitmapManager shared_bitmap_manager_
;
442 FakeLayerTreeHostImpl host_impl_
;
446 TEST_F(LayerImplScrollTest
, ScrollByWithZeroOffset
) {
447 // Test that LayerImpl::ScrollBy only affects ScrollDelta and total scroll
448 // offset is bounded by the range [0, max scroll offset].
450 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->TotalScrollOffset());
451 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->scroll_offset());
452 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
454 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
455 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
457 EXPECT_VECTOR_EQ(layer()->ScrollDelta(), layer()->TotalScrollOffset());
458 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->scroll_offset());
460 layer()->ScrollBy(gfx::Vector2dF(100, -100));
461 EXPECT_VECTOR_EQ(gfx::Vector2dF(50, 0), layer()->TotalScrollOffset());
463 EXPECT_VECTOR_EQ(layer()->ScrollDelta(), layer()->TotalScrollOffset());
464 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->scroll_offset());
467 TEST_F(LayerImplScrollTest
, ScrollByWithNonZeroOffset
) {
468 gfx::Vector2d
scroll_offset(10, 5);
469 layer()->SetScrollOffset(scroll_offset
);
471 EXPECT_VECTOR_EQ(scroll_offset
, layer()->TotalScrollOffset());
472 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
473 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
475 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
476 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
478 EXPECT_VECTOR_EQ(layer()->ScrollDelta() + scroll_offset
,
479 layer()->TotalScrollOffset());
480 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
482 layer()->ScrollBy(gfx::Vector2dF(100, -100));
483 EXPECT_VECTOR_EQ(gfx::Vector2dF(50, 0), layer()->TotalScrollOffset());
485 EXPECT_VECTOR_EQ(layer()->ScrollDelta() + scroll_offset
,
486 layer()->TotalScrollOffset());
487 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
490 class ScrollDelegateIgnore
: public LayerImpl::ScrollOffsetDelegate
{
492 virtual void SetTotalScrollOffset(const gfx::Vector2dF
& new_value
) OVERRIDE
{}
493 virtual gfx::Vector2dF
GetTotalScrollOffset() OVERRIDE
{
494 return fixed_offset_
;
496 virtual bool IsExternalFlingActive() const OVERRIDE
{ return false; }
498 void set_fixed_offset(const gfx::Vector2dF
& fixed_offset
) {
499 fixed_offset_
= fixed_offset
;
503 gfx::Vector2dF fixed_offset_
;
506 TEST_F(LayerImplScrollTest
, ScrollByWithIgnoringDelegate
) {
507 gfx::Vector2d
scroll_offset(10, 5);
508 layer()->SetScrollOffset(scroll_offset
);
510 EXPECT_VECTOR_EQ(scroll_offset
, layer()->TotalScrollOffset());
511 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
512 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
514 ScrollDelegateIgnore delegate
;
515 gfx::Vector2dF
fixed_offset(32, 12);
516 delegate
.set_fixed_offset(fixed_offset
);
517 layer()->SetScrollOffsetDelegate(&delegate
);
519 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
520 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
522 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
524 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
525 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
527 layer()->SetScrollOffsetDelegate(NULL
);
529 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
530 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
532 gfx::Vector2dF
scroll_delta(1, 1);
533 layer()->ScrollBy(scroll_delta
);
535 EXPECT_VECTOR_EQ(fixed_offset
+ scroll_delta
, layer()->TotalScrollOffset());
536 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
539 class ScrollDelegateAccept
: public LayerImpl::ScrollOffsetDelegate
{
541 virtual void SetTotalScrollOffset(const gfx::Vector2dF
& new_value
) OVERRIDE
{
542 current_offset_
= new_value
;
544 virtual gfx::Vector2dF
GetTotalScrollOffset() OVERRIDE
{
545 return current_offset_
;
547 virtual bool IsExternalFlingActive() const OVERRIDE
{ return false; }
550 gfx::Vector2dF current_offset_
;
553 TEST_F(LayerImplScrollTest
, ScrollByWithAcceptingDelegate
) {
554 gfx::Vector2d
scroll_offset(10, 5);
555 layer()->SetScrollOffset(scroll_offset
);
557 EXPECT_VECTOR_EQ(scroll_offset
, layer()->TotalScrollOffset());
558 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
559 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
561 ScrollDelegateAccept delegate
;
562 layer()->SetScrollOffsetDelegate(&delegate
);
564 EXPECT_VECTOR_EQ(scroll_offset
, layer()->TotalScrollOffset());
565 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
566 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
568 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
570 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
571 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
573 layer()->SetScrollOffsetDelegate(NULL
);
575 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
576 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
578 gfx::Vector2dF
scroll_delta(1, 1);
579 layer()->ScrollBy(scroll_delta
);
581 EXPECT_VECTOR_EQ(gfx::Vector2dF(1, 80), layer()->TotalScrollOffset());
582 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
585 TEST_F(LayerImplScrollTest
, ApplySentScrollsNoDelegate
) {
586 gfx::Vector2d
scroll_offset(10, 5);
587 gfx::Vector2dF
scroll_delta(20.5f
, 8.5f
);
588 gfx::Vector2d
sent_scroll_delta(12, -3);
590 layer()->SetScrollOffset(scroll_offset
);
591 layer()->ScrollBy(scroll_delta
);
592 layer()->SetSentScrollDelta(sent_scroll_delta
);
594 EXPECT_VECTOR_EQ(scroll_offset
+ scroll_delta
, layer()->TotalScrollOffset());
595 EXPECT_VECTOR_EQ(scroll_delta
, layer()->ScrollDelta());
596 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
597 EXPECT_VECTOR_EQ(sent_scroll_delta
, layer()->sent_scroll_delta());
599 layer()->ApplySentScrollDeltasFromAbortedCommit();
601 EXPECT_VECTOR_EQ(scroll_offset
+ scroll_delta
, layer()->TotalScrollOffset());
602 EXPECT_VECTOR_EQ(scroll_delta
- sent_scroll_delta
, layer()->ScrollDelta());
603 EXPECT_VECTOR_EQ(scroll_offset
+ sent_scroll_delta
, layer()->scroll_offset());
604 EXPECT_VECTOR_EQ(gfx::Vector2d(), layer()->sent_scroll_delta());
607 TEST_F(LayerImplScrollTest
, ApplySentScrollsWithIgnoringDelegate
) {
608 gfx::Vector2d
scroll_offset(10, 5);
609 gfx::Vector2d
sent_scroll_delta(12, -3);
610 gfx::Vector2dF
fixed_offset(32, 12);
612 layer()->SetScrollOffset(scroll_offset
);
613 ScrollDelegateIgnore delegate
;
614 delegate
.set_fixed_offset(fixed_offset
);
615 layer()->SetScrollOffsetDelegate(&delegate
);
616 layer()->SetSentScrollDelta(sent_scroll_delta
);
618 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
619 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
620 EXPECT_VECTOR_EQ(sent_scroll_delta
, layer()->sent_scroll_delta());
622 layer()->ApplySentScrollDeltasFromAbortedCommit();
624 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
625 EXPECT_VECTOR_EQ(scroll_offset
+ sent_scroll_delta
, layer()->scroll_offset());
626 EXPECT_VECTOR_EQ(gfx::Vector2d(), layer()->sent_scroll_delta());
629 TEST_F(LayerImplScrollTest
, ApplySentScrollsWithAcceptingDelegate
) {
630 gfx::Vector2d
scroll_offset(10, 5);
631 gfx::Vector2d
sent_scroll_delta(12, -3);
632 gfx::Vector2dF
scroll_delta(20.5f
, 8.5f
);
634 layer()->SetScrollOffset(scroll_offset
);
635 ScrollDelegateAccept delegate
;
636 layer()->SetScrollOffsetDelegate(&delegate
);
637 layer()->ScrollBy(scroll_delta
);
638 layer()->SetSentScrollDelta(sent_scroll_delta
);
640 EXPECT_VECTOR_EQ(scroll_offset
+ scroll_delta
, layer()->TotalScrollOffset());
641 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
642 EXPECT_VECTOR_EQ(sent_scroll_delta
, layer()->sent_scroll_delta());
644 layer()->ApplySentScrollDeltasFromAbortedCommit();
646 EXPECT_VECTOR_EQ(scroll_offset
+ scroll_delta
, layer()->TotalScrollOffset());
647 EXPECT_VECTOR_EQ(scroll_offset
+ sent_scroll_delta
, layer()->scroll_offset());
648 EXPECT_VECTOR_EQ(gfx::Vector2d(), layer()->sent_scroll_delta());
651 // The user-scrollability breaks for zoomed-in pages. So disable this.
652 // http://crbug.com/322223
653 TEST_F(LayerImplScrollTest
, DISABLED_ScrollUserUnscrollableLayer
) {
654 gfx::Vector2d
scroll_offset(10, 5);
655 gfx::Vector2dF
scroll_delta(20.5f
, 8.5f
);
657 layer()->set_user_scrollable_vertical(false);
658 layer()->SetScrollOffset(scroll_offset
);
659 gfx::Vector2dF unscrolled
= layer()->ScrollBy(scroll_delta
);
661 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 8.5f
), unscrolled
);
662 EXPECT_VECTOR_EQ(gfx::Vector2dF(30.5f
, 5), layer()->TotalScrollOffset());
665 TEST_F(LayerImplScrollTest
, SetNewScrollbarParameters
) {
666 gfx::Vector2d
scroll_offset(10, 5);
667 layer()->SetScrollOffset(scroll_offset
);
669 scoped_ptr
<PaintedScrollbarLayerImpl
> vertical_scrollbar(
670 PaintedScrollbarLayerImpl::Create(tree(), 100, VERTICAL
));
671 vertical_scrollbar
->SetScrollLayerAndClipLayerByIds(
672 layer()->id(), tree()->root_layer()->id());
674 int expected_vertical_maximum
=
675 layer()->bounds().height() - tree()->root_layer()->bounds().height();
676 EXPECT_EQ(expected_vertical_maximum
, vertical_scrollbar
->maximum());
677 EXPECT_EQ(scroll_offset
.y(), vertical_scrollbar
->current_pos());
679 scoped_ptr
<PaintedScrollbarLayerImpl
> horizontal_scrollbar(
680 PaintedScrollbarLayerImpl::Create(tree(), 101, HORIZONTAL
));
681 horizontal_scrollbar
->SetScrollLayerAndClipLayerByIds(
682 layer()->id(), tree()->root_layer()->id());
684 int expected_horizontal_maximum
=
685 layer()->bounds().width() - tree()->root_layer()->bounds().width();
686 EXPECT_EQ(expected_horizontal_maximum
, horizontal_scrollbar
->maximum());
687 EXPECT_EQ(scroll_offset
.x(), horizontal_scrollbar
->current_pos());