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/layers/solid_color_scrollbar_layer_impl.h"
9 #include "cc/output/filter_operation.h"
10 #include "cc/output/filter_operations.h"
11 #include "cc/test/fake_impl_proxy.h"
12 #include "cc/test/fake_layer_tree_host_impl.h"
13 #include "cc/test/fake_output_surface.h"
14 #include "cc/test/geometry_test_utils.h"
15 #include "cc/test/test_shared_bitmap_manager.h"
16 #include "cc/test/test_task_graph_runner.h"
17 #include "cc/trees/layer_tree_impl.h"
18 #include "cc/trees/single_thread_proxy.h"
19 #include "cc/trees/tree_synchronizer.h"
20 #include "testing/gmock/include/gmock/gmock.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "third_party/skia/include/effects/SkBlurImageFilter.h"
27 #define EXECUTE_AND_VERIFY_SUBTREE_CHANGED(code_to_test) \
28 root->ResetAllChangeTrackingForSubtree(); \
30 EXPECT_TRUE(root->needs_push_properties()); \
31 EXPECT_FALSE(child->needs_push_properties()); \
32 EXPECT_FALSE(grand_child->needs_push_properties()); \
33 EXPECT_TRUE(root->LayerPropertyChanged()); \
34 EXPECT_TRUE(child->LayerPropertyChanged()); \
35 EXPECT_TRUE(grand_child->LayerPropertyChanged());
37 #define EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(code_to_test) \
38 root->ResetAllChangeTrackingForSubtree(); \
40 EXPECT_FALSE(root->needs_push_properties()); \
41 EXPECT_FALSE(child->needs_push_properties()); \
42 EXPECT_FALSE(grand_child->needs_push_properties()); \
43 EXPECT_FALSE(root->LayerPropertyChanged()); \
44 EXPECT_FALSE(child->LayerPropertyChanged()); \
45 EXPECT_FALSE(grand_child->LayerPropertyChanged());
47 #define EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE( \
49 root->ResetAllChangeTrackingForSubtree(); \
51 EXPECT_TRUE(root->needs_push_properties()); \
52 EXPECT_FALSE(child->needs_push_properties()); \
53 EXPECT_FALSE(grand_child->needs_push_properties()); \
54 EXPECT_FALSE(root->LayerPropertyChanged()); \
55 EXPECT_FALSE(child->LayerPropertyChanged()); \
56 EXPECT_FALSE(grand_child->LayerPropertyChanged());
58 #define EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(code_to_test) \
59 root->ResetAllChangeTrackingForSubtree(); \
61 EXPECT_TRUE(root->needs_push_properties()); \
62 EXPECT_FALSE(child->needs_push_properties()); \
63 EXPECT_FALSE(grand_child->needs_push_properties()); \
64 EXPECT_TRUE(root->LayerPropertyChanged()); \
65 EXPECT_FALSE(child->LayerPropertyChanged()); \
66 EXPECT_FALSE(grand_child->LayerPropertyChanged());
68 #define VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(code_to_test) \
69 root->ResetAllChangeTrackingForSubtree(); \
70 host_impl.ForcePrepareToDraw(); \
71 EXPECT_FALSE(host_impl.active_tree()->needs_update_draw_properties()); \
73 EXPECT_TRUE(host_impl.active_tree()->needs_update_draw_properties());
75 #define VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(code_to_test) \
76 root->ResetAllChangeTrackingForSubtree(); \
77 host_impl.ForcePrepareToDraw(); \
78 EXPECT_FALSE(host_impl.active_tree()->needs_update_draw_properties()); \
80 EXPECT_FALSE(host_impl.active_tree()->needs_update_draw_properties());
82 TEST(LayerImplTest
, VerifyLayerChangesAreTrackedProperly
) {
84 // This test checks that layerPropertyChanged() has the correct behavior.
87 // The constructor on this will fake that we are on the correct thread.
88 // Create a simple LayerImpl tree:
90 TestSharedBitmapManager shared_bitmap_manager
;
91 FakeLayerTreeHostImpl
host_impl(&proxy
, &shared_bitmap_manager
, nullptr);
92 EXPECT_TRUE(host_impl
.InitializeRenderer(FakeOutputSurface::Create3d()));
93 scoped_ptr
<LayerImpl
> root_clip
=
94 LayerImpl::Create(host_impl
.active_tree(), 1);
95 scoped_ptr
<LayerImpl
> root_ptr
=
96 LayerImpl::Create(host_impl
.active_tree(), 2);
97 LayerImpl
* root
= root_ptr
.get();
98 root_clip
->AddChild(root_ptr
.Pass());
99 scoped_ptr
<LayerImpl
> scroll_parent
=
100 LayerImpl::Create(host_impl
.active_tree(), 3);
101 LayerImpl
* scroll_child
= LayerImpl::Create(host_impl
.active_tree(), 4).get();
102 std::set
<LayerImpl
*>* scroll_children
= new std::set
<LayerImpl
*>();
103 scroll_children
->insert(scroll_child
);
104 scroll_children
->insert(root
);
105 root
->SetHasRenderSurface(true);
107 scoped_ptr
<LayerImpl
> clip_parent
=
108 LayerImpl::Create(host_impl
.active_tree(), 5);
109 LayerImpl
* clip_child
= LayerImpl::Create(host_impl
.active_tree(), 6).get();
110 std::set
<LayerImpl
*>* clip_children
= new std::set
<LayerImpl
*>();
111 clip_children
->insert(clip_child
);
112 clip_children
->insert(root
);
114 root
->AddChild(LayerImpl::Create(host_impl
.active_tree(), 7));
115 LayerImpl
* child
= root
->children()[0];
116 child
->AddChild(LayerImpl::Create(host_impl
.active_tree(), 8));
117 LayerImpl
* grand_child
= child
->children()[0];
119 root
->SetScrollClipLayer(root_clip
->id());
121 // Adding children is an internal operation and should not mark layers as
123 EXPECT_FALSE(root
->LayerPropertyChanged());
124 EXPECT_FALSE(child
->LayerPropertyChanged());
125 EXPECT_FALSE(grand_child
->LayerPropertyChanged());
127 gfx::PointF arbitrary_point_f
= gfx::PointF(0.125f
, 0.25f
);
128 gfx::Point3F arbitrary_point_3f
= gfx::Point3F(0.125f
, 0.25f
, 0.f
);
129 float arbitrary_number
= 0.352f
;
130 gfx::Size arbitrary_size
= gfx::Size(111, 222);
131 gfx::Point arbitrary_point
= gfx::Point(333, 444);
132 gfx::Vector2d arbitrary_vector2d
= gfx::Vector2d(111, 222);
133 gfx::Rect arbitrary_rect
= gfx::Rect(arbitrary_point
, arbitrary_size
);
134 gfx::RectF arbitrary_rect_f
=
135 gfx::RectF(arbitrary_point_f
, gfx::SizeF(1.234f
, 5.678f
));
136 SkColor arbitrary_color
= SkColorSetRGB(10, 20, 30);
137 gfx::Transform arbitrary_transform
;
138 arbitrary_transform
.Scale3d(0.1f
, 0.2f
, 0.3f
);
139 FilterOperations arbitrary_filters
;
140 arbitrary_filters
.Append(FilterOperation::CreateOpacityFilter(0.5f
));
141 SkXfermode::Mode arbitrary_blend_mode
= SkXfermode::kMultiply_Mode
;
143 // These properties are internal, and should not be considered "change" when
145 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
146 root
->SetUpdateRect(arbitrary_rect
));
147 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->SetBounds(arbitrary_size
));
149 // Changing these properties affects the entire subtree of layers.
150 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
151 root
->SetTransformOrigin(arbitrary_point_3f
));
152 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetFilters(arbitrary_filters
));
153 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetFilters(FilterOperations()));
154 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
155 root
->SetMaskLayer(LayerImpl::Create(host_impl
.active_tree(), 9)));
156 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetMasksToBounds(true));
157 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetContentsOpaque(true));
158 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
159 root
->SetReplicaLayer(LayerImpl::Create(host_impl
.active_tree(), 10)));
160 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetPosition(arbitrary_point_f
));
161 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetShouldFlattenTransform(false));
162 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->Set3dSortingContextId(1));
163 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
164 root
->SetDoubleSided(false)); // constructor initializes it to "true".
165 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->ScrollBy(arbitrary_vector2d
));
166 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetScrollDelta(gfx::Vector2d()));
167 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->PushScrollOffsetFromMainThread(
168 gfx::ScrollOffset(arbitrary_vector2d
)));
169 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetHideLayerAndSubtree(true));
170 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetOpacity(arbitrary_number
));
171 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetBlendMode(arbitrary_blend_mode
));
172 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetTransform(arbitrary_transform
));
174 // Changing these properties only affects the layer itself.
175 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->SetContentBounds(arbitrary_size
));
176 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(
177 root
->SetContentsScale(arbitrary_number
, arbitrary_number
));
178 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->SetDrawsContent(true));
179 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(
180 root
->SetBackgroundColor(arbitrary_color
));
181 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(
182 root
->SetBackgroundFilters(arbitrary_filters
));
184 // Special case: check that SetBounds changes behavior depending on
186 root
->SetMasksToBounds(false);
187 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->SetBounds(gfx::Size(135, 246)));
188 root
->SetMasksToBounds(true);
189 // Should be a different size than previous call, to ensure it marks tree
191 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetBounds(arbitrary_size
));
193 // Changing this property does not cause the layer to be marked as changed
194 // but does cause the layer to need to push properties.
195 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
196 root
->SetIsRootForIsolatedGroup(true));
198 // Changing these properties should cause the layer to need to push properties
199 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
200 root
->SetScrollParent(scroll_parent
.get()));
201 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
202 root
->SetScrollChildren(scroll_children
));
203 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
204 root
->SetClipParent(clip_parent
.get()));
205 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
206 root
->SetClipChildren(clip_children
));
207 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
208 root
->SetNumDescendantsThatDrawContent(10));
210 // After setting all these properties already, setting to the exact same
211 // values again should not cause any change.
212 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
213 root
->SetTransformOrigin(arbitrary_point_3f
));
214 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->SetMasksToBounds(true));
215 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
216 root
->SetPosition(arbitrary_point_f
));
217 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
218 root
->SetShouldFlattenTransform(false));
219 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->Set3dSortingContextId(1));
220 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
221 root
->SetTransform(arbitrary_transform
));
222 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
223 root
->SetDoubleSided(false)); // constructor initializes it to "true".
224 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
225 root
->SetScrollDelta(gfx::Vector2d()));
226 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
227 root
->PushScrollOffsetFromMainThread(
228 gfx::ScrollOffset(arbitrary_vector2d
)));
229 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
230 root
->SetContentBounds(arbitrary_size
));
231 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
232 root
->SetContentsScale(arbitrary_number
, arbitrary_number
));
233 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->SetContentsOpaque(true));
234 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->SetOpacity(arbitrary_number
));
235 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
236 root
->SetBlendMode(arbitrary_blend_mode
));
237 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
238 root
->SetIsRootForIsolatedGroup(true));
239 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->SetDrawsContent(true));
240 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root
->SetBounds(arbitrary_size
));
241 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
242 root
->SetScrollParent(scroll_parent
.get()));
243 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
244 root
->SetScrollChildren(scroll_children
));
245 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
246 root
->SetClipParent(clip_parent
.get()));
247 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
248 root
->SetClipChildren(clip_children
));
251 TEST(LayerImplTest
, VerifyNeedsUpdateDrawProperties
) {
253 TestSharedBitmapManager shared_bitmap_manager
;
254 FakeLayerTreeHostImpl
host_impl(&proxy
, &shared_bitmap_manager
, nullptr);
255 EXPECT_TRUE(host_impl
.InitializeRenderer(FakeOutputSurface::Create3d()));
256 host_impl
.active_tree()->SetRootLayer(
257 LayerImpl::Create(host_impl
.active_tree(), 1));
258 LayerImpl
* root
= host_impl
.active_tree()->root_layer();
259 root
->SetHasRenderSurface(true);
260 scoped_ptr
<LayerImpl
> layer_ptr
=
261 LayerImpl::Create(host_impl
.active_tree(), 2);
262 LayerImpl
* layer
= layer_ptr
.get();
263 root
->AddChild(layer_ptr
.Pass());
264 layer
->SetScrollClipLayer(root
->id());
265 DCHECK(host_impl
.CanDraw());
267 gfx::PointF arbitrary_point_f
= gfx::PointF(0.125f
, 0.25f
);
268 float arbitrary_number
= 0.352f
;
269 gfx::Size arbitrary_size
= gfx::Size(111, 222);
270 gfx::Point arbitrary_point
= gfx::Point(333, 444);
271 gfx::Vector2d arbitrary_vector2d
= gfx::Vector2d(111, 222);
272 gfx::Size large_size
= gfx::Size(1000, 1000);
273 gfx::Rect arbitrary_rect
= gfx::Rect(arbitrary_point
, arbitrary_size
);
274 gfx::RectF arbitrary_rect_f
=
275 gfx::RectF(arbitrary_point_f
, gfx::SizeF(1.234f
, 5.678f
));
276 SkColor arbitrary_color
= SkColorSetRGB(10, 20, 30);
277 gfx::Transform arbitrary_transform
;
278 arbitrary_transform
.Scale3d(0.1f
, 0.2f
, 0.3f
);
279 FilterOperations arbitrary_filters
;
280 arbitrary_filters
.Append(FilterOperation::CreateOpacityFilter(0.5f
));
281 SkXfermode::Mode arbitrary_blend_mode
= SkXfermode::kMultiply_Mode
;
283 // Render surface functions.
284 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetHasRenderSurface(true));
285 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetHasRenderSurface(true));
286 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetHasRenderSurface(false));
287 // Create a render surface, because we must have a render surface if we have
289 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetHasRenderSurface(true));
291 // Related filter functions.
292 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetFilters(arbitrary_filters
));
293 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetFilters(arbitrary_filters
));
294 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetFilters(FilterOperations()));
295 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetFilters(arbitrary_filters
));
297 // Related scrolling functions.
298 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetBounds(large_size
));
299 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetBounds(large_size
));
300 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->ScrollBy(arbitrary_vector2d
));
301 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->ScrollBy(gfx::Vector2d()));
302 layer
->SetScrollDelta(gfx::Vector2d(0, 0));
303 host_impl
.ForcePrepareToDraw();
304 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
305 layer
->SetScrollDelta(arbitrary_vector2d
));
306 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
307 layer
->SetScrollDelta(arbitrary_vector2d
));
308 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->PushScrollOffsetFromMainThread(
309 gfx::ScrollOffset(arbitrary_vector2d
)));
310 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->PushScrollOffsetFromMainThread(
311 gfx::ScrollOffset(arbitrary_vector2d
)));
313 // Unrelated functions, always set to new values, always set needs update.
314 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
315 layer
->SetMaskLayer(LayerImpl::Create(host_impl
.active_tree(), 4)));
316 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetMasksToBounds(true));
317 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetContentsOpaque(true));
318 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
319 layer
->SetReplicaLayer(LayerImpl::Create(host_impl
.active_tree(), 5)));
320 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetPosition(arbitrary_point_f
));
321 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetShouldFlattenTransform(false));
322 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->Set3dSortingContextId(1));
324 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
325 layer
->SetDoubleSided(false)); // constructor initializes it to "true".
326 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetContentBounds(arbitrary_size
));
327 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
328 layer
->SetContentsScale(arbitrary_number
, arbitrary_number
));
329 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetDrawsContent(true));
330 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
331 layer
->SetBackgroundColor(arbitrary_color
));
332 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
333 layer
->SetBackgroundFilters(arbitrary_filters
));
334 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetOpacity(arbitrary_number
));
335 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
336 layer
->SetBlendMode(arbitrary_blend_mode
));
337 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetTransform(arbitrary_transform
));
338 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetBounds(arbitrary_size
));
340 // Unrelated functions, set to the same values, no needs update.
341 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
342 layer
->SetIsRootForIsolatedGroup(true));
343 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetFilters(arbitrary_filters
));
344 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetMasksToBounds(true));
345 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetContentsOpaque(true));
346 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetPosition(arbitrary_point_f
));
347 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->Set3dSortingContextId(1));
348 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
349 layer
->SetDoubleSided(false)); // constructor initializes it to "true".
350 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
351 layer
->SetContentBounds(arbitrary_size
));
352 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
353 layer
->SetContentsScale(arbitrary_number
, arbitrary_number
));
354 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetDrawsContent(true));
355 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
356 layer
->SetBackgroundColor(arbitrary_color
));
357 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
358 layer
->SetBackgroundFilters(arbitrary_filters
));
359 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetOpacity(arbitrary_number
));
360 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
361 layer
->SetBlendMode(arbitrary_blend_mode
));
362 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
363 layer
->SetIsRootForIsolatedGroup(true));
364 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
365 layer
->SetTransform(arbitrary_transform
));
366 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetBounds(arbitrary_size
));
369 TEST(LayerImplTest
, SafeOpaqueBackgroundColor
) {
371 TestSharedBitmapManager shared_bitmap_manager
;
372 FakeLayerTreeHostImpl
host_impl(&proxy
, &shared_bitmap_manager
, nullptr);
373 EXPECT_TRUE(host_impl
.InitializeRenderer(FakeOutputSurface::Create3d()));
374 scoped_ptr
<LayerImpl
> layer
= LayerImpl::Create(host_impl
.active_tree(), 1);
376 for (int contents_opaque
= 0; contents_opaque
< 2; ++contents_opaque
) {
377 for (int layer_opaque
= 0; layer_opaque
< 2; ++layer_opaque
) {
378 for (int host_opaque
= 0; host_opaque
< 2; ++host_opaque
) {
379 layer
->SetContentsOpaque(!!contents_opaque
);
380 layer
->SetBackgroundColor(layer_opaque
? SK_ColorRED
381 : SK_ColorTRANSPARENT
);
382 host_impl
.active_tree()->set_background_color(
383 host_opaque
? SK_ColorRED
: SK_ColorTRANSPARENT
);
385 SkColor safe_color
= layer
->SafeOpaqueBackgroundColor();
386 if (contents_opaque
) {
387 EXPECT_EQ(SkColorGetA(safe_color
), 255u)
388 << "Flags: " << contents_opaque
<< ", " << layer_opaque
<< ", "
389 << host_opaque
<< "\n";
391 EXPECT_NE(SkColorGetA(safe_color
), 255u)
392 << "Flags: " << contents_opaque
<< ", " << layer_opaque
<< ", "
393 << host_opaque
<< "\n";
400 TEST(LayerImplTest
, TransformInvertibility
) {
402 TestSharedBitmapManager shared_bitmap_manager
;
403 FakeLayerTreeHostImpl
host_impl(&proxy
, &shared_bitmap_manager
, nullptr);
405 scoped_ptr
<LayerImpl
> layer
= LayerImpl::Create(host_impl
.active_tree(), 1);
406 EXPECT_TRUE(layer
->transform().IsInvertible());
407 EXPECT_TRUE(layer
->transform_is_invertible());
409 gfx::Transform transform
;
411 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
412 layer
->SetTransform(transform
);
413 EXPECT_FALSE(layer
->transform().IsInvertible());
414 EXPECT_FALSE(layer
->transform_is_invertible());
416 transform
.MakeIdentity();
417 transform
.ApplyPerspectiveDepth(SkDoubleToMScalar(100.0));
418 transform
.RotateAboutZAxis(75.0);
419 transform
.RotateAboutXAxis(32.2);
420 transform
.RotateAboutZAxis(-75.0);
421 transform
.Translate3d(SkDoubleToMScalar(50.5),
422 SkDoubleToMScalar(42.42),
423 SkDoubleToMScalar(-100.25));
425 layer
->SetTransform(transform
);
426 EXPECT_TRUE(layer
->transform().IsInvertible());
427 EXPECT_TRUE(layer
->transform_is_invertible());
430 class LayerImplScrollTest
: public testing::Test
{
432 LayerImplScrollTest()
433 : host_impl_(settings(),
435 &shared_bitmap_manager_
,
436 &task_graph_runner_
),
438 host_impl_
.active_tree()->SetRootLayer(
439 LayerImpl::Create(host_impl_
.active_tree(), root_id_
));
440 host_impl_
.active_tree()->root_layer()->AddChild(
441 LayerImpl::Create(host_impl_
.active_tree(), root_id_
+ 1));
442 layer()->SetScrollClipLayer(root_id_
);
443 // Set the max scroll offset by noting that the root layer has bounds (1,1),
444 // thus whatever bounds are set for the layer will be the max scroll
445 // offset plus 1 in each direction.
446 host_impl_
.active_tree()->root_layer()->SetBounds(gfx::Size(1, 1));
447 gfx::Vector2d
max_scroll_offset(51, 81);
448 layer()->SetBounds(gfx::Size(max_scroll_offset
.x(), max_scroll_offset
.y()));
452 return host_impl_
.active_tree()->root_layer()->children()[0];
455 LayerTreeHostImpl
& host_impl() { return host_impl_
; }
457 LayerTreeImpl
* tree() { return host_impl_
.active_tree(); }
459 LayerTreeSettings
settings() {
460 LayerTreeSettings settings
;
465 FakeImplProxy proxy_
;
466 TestSharedBitmapManager shared_bitmap_manager_
;
467 TestTaskGraphRunner task_graph_runner_
;
468 FakeLayerTreeHostImpl host_impl_
;
472 TEST_F(LayerImplScrollTest
, ScrollByWithZeroOffset
) {
473 // Test that LayerImpl::ScrollBy only affects ScrollDelta and total scroll
474 // offset is bounded by the range [0, max scroll offset].
476 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->CurrentScrollOffset());
477 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->BaseScrollOffset());
478 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
480 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
481 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->CurrentScrollOffset());
483 EXPECT_VECTOR_EQ(layer()->ScrollDelta(), layer()->CurrentScrollOffset());
484 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->BaseScrollOffset());
486 layer()->ScrollBy(gfx::Vector2dF(100, -100));
487 EXPECT_VECTOR_EQ(gfx::Vector2dF(50, 0), layer()->CurrentScrollOffset());
489 EXPECT_VECTOR_EQ(layer()->ScrollDelta(), layer()->CurrentScrollOffset());
490 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->BaseScrollOffset());
493 TEST_F(LayerImplScrollTest
, ScrollByWithNonZeroOffset
) {
494 gfx::ScrollOffset
scroll_offset(10, 5);
495 layer()->PushScrollOffsetFromMainThread(scroll_offset
);
497 EXPECT_VECTOR_EQ(scroll_offset
, layer()->CurrentScrollOffset());
498 EXPECT_VECTOR_EQ(scroll_offset
, layer()->BaseScrollOffset());
499 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
501 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
502 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->CurrentScrollOffset());
505 gfx::ScrollOffsetWithDelta(scroll_offset
, layer()->ScrollDelta()),
506 layer()->CurrentScrollOffset());
507 EXPECT_VECTOR_EQ(scroll_offset
, layer()->BaseScrollOffset());
509 layer()->ScrollBy(gfx::Vector2dF(100, -100));
510 EXPECT_VECTOR_EQ(gfx::Vector2dF(50, 0), layer()->CurrentScrollOffset());
513 gfx::ScrollOffsetWithDelta(scroll_offset
, layer()->ScrollDelta()),
514 layer()->CurrentScrollOffset());
515 EXPECT_VECTOR_EQ(scroll_offset
, layer()->BaseScrollOffset());
518 TEST_F(LayerImplScrollTest
, ApplySentScrollsNoListener
) {
519 gfx::ScrollOffset
scroll_offset(10, 5);
520 gfx::Vector2dF
scroll_delta(20.5f
, 8.5f
);
521 gfx::Vector2d
sent_scroll_delta(12, -3);
523 layer()->PushScrollOffsetFromMainThread(scroll_offset
);
524 layer()->ScrollBy(sent_scroll_delta
);
525 layer()->PullDeltaForMainThread();
526 layer()->SetCurrentScrollOffset(scroll_offset
+
527 gfx::ScrollOffset(scroll_delta
));
529 EXPECT_VECTOR_EQ(gfx::ScrollOffsetWithDelta(scroll_offset
, scroll_delta
),
530 layer()->CurrentScrollOffset());
531 EXPECT_VECTOR_EQ(scroll_delta
, layer()->ScrollDelta());
532 EXPECT_VECTOR_EQ(scroll_offset
, layer()->BaseScrollOffset());
534 layer()->ApplySentScrollDeltasFromAbortedCommit();
536 EXPECT_VECTOR_EQ(gfx::ScrollOffsetWithDelta(scroll_offset
, scroll_delta
),
537 layer()->CurrentScrollOffset());
538 EXPECT_VECTOR_EQ(scroll_delta
- sent_scroll_delta
, layer()->ScrollDelta());
539 EXPECT_VECTOR_EQ(gfx::ScrollOffsetWithDelta(scroll_offset
, sent_scroll_delta
),
540 layer()->BaseScrollOffset());
543 TEST_F(LayerImplScrollTest
, ScrollUserUnscrollableLayer
) {
544 gfx::ScrollOffset
scroll_offset(10, 5);
545 gfx::Vector2dF
scroll_delta(20.5f
, 8.5f
);
547 layer()->set_user_scrollable_vertical(false);
548 layer()->PushScrollOffsetFromMainThread(scroll_offset
);
549 gfx::Vector2dF unscrolled
= layer()->ScrollBy(scroll_delta
);
551 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 8.5f
), unscrolled
);
552 EXPECT_VECTOR_EQ(gfx::Vector2dF(30.5f
, 5), layer()->CurrentScrollOffset());
555 TEST_F(LayerImplScrollTest
, PushPropertiesToMirrorsCurrentScrollOffset
) {
556 gfx::ScrollOffset
scroll_offset(10, 5);
557 gfx::Vector2dF
scroll_delta(12, 18);
559 host_impl().CreatePendingTree();
561 layer()->PushScrollOffsetFromMainThread(scroll_offset
);
562 gfx::Vector2dF unscrolled
= layer()->ScrollBy(scroll_delta
);
564 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 0), unscrolled
);
565 EXPECT_VECTOR_EQ(gfx::Vector2dF(22, 23), layer()->CurrentScrollOffset());
567 layer()->PullDeltaForMainThread();
569 scoped_ptr
<LayerImpl
> pending_layer
= LayerImpl::Create(
570 host_impl().sync_tree(), layer()->id(), layer()->synced_scroll_offset());
571 pending_layer
->PushScrollOffsetFromMainThread(layer()->CurrentScrollOffset());
573 pending_layer
->PushPropertiesTo(layer());
575 EXPECT_VECTOR_EQ(gfx::Vector2dF(22, 23), layer()->CurrentScrollOffset());
576 EXPECT_VECTOR_EQ(layer()->CurrentScrollOffset(),
577 pending_layer
->CurrentScrollOffset());
580 TEST_F(LayerImplScrollTest
, SetNewScrollbarParameters
) {
581 gfx::ScrollOffset
scroll_offset(10, 5);
582 layer()->PushScrollOffsetFromMainThread(scroll_offset
);
584 scoped_ptr
<PaintedScrollbarLayerImpl
> vertical_scrollbar(
585 PaintedScrollbarLayerImpl::Create(tree(), 100, VERTICAL
));
586 vertical_scrollbar
->SetScrollLayerAndClipLayerByIds(
587 layer()->id(), tree()->root_layer()->id());
589 int expected_vertical_maximum
=
590 layer()->bounds().height() - tree()->root_layer()->bounds().height();
591 EXPECT_EQ(expected_vertical_maximum
, vertical_scrollbar
->maximum());
592 EXPECT_EQ(scroll_offset
.y(), vertical_scrollbar
->current_pos());
594 scoped_ptr
<PaintedScrollbarLayerImpl
> horizontal_scrollbar(
595 PaintedScrollbarLayerImpl::Create(tree(), 101, HORIZONTAL
));
596 horizontal_scrollbar
->SetScrollLayerAndClipLayerByIds(
597 layer()->id(), tree()->root_layer()->id());
599 int expected_horizontal_maximum
=
600 layer()->bounds().width() - tree()->root_layer()->bounds().width();
601 EXPECT_EQ(expected_horizontal_maximum
, horizontal_scrollbar
->maximum());
602 EXPECT_EQ(scroll_offset
.x(), horizontal_scrollbar
->current_pos());
605 class LayerImplScrollbarSyncTest
: public testing::Test
{
622 LayerImplScrollbarSyncTest()
623 : host_impl_(settings(),
625 &shared_bitmap_manager_
,
626 &task_graph_runner_
) {
627 host_impl_
.CreatePendingTree();
629 CreateLayers(host_impl_
.pending_tree());
630 CreateLayers(host_impl_
.active_tree());
633 void CreateLayers(LayerTreeImpl
* tree
) {
634 tree
->SetRootLayer(LayerImpl::Create(tree
, ROOT
));
635 LayerImpl
* root
= tree
->root_layer();
636 ASSERT_TRUE(root
!= nullptr);
638 int hierarchy
[] = {IV_CLIP
, PAGE
, IV_SCROLL
, OLD_ROOT
, OV_CLIP
, OV_SCROLL
};
639 LayerImpl
* parent
= root
;
640 for (int child_id
: hierarchy
) {
641 parent
->AddChild(LayerImpl::Create(tree
, child_id
));
642 parent
= tree
->LayerById(child_id
);
643 ASSERT_TRUE(parent
!= nullptr);
647 SolidColorScrollbarLayerImpl::Create(tree
, SCROLLBAR
, HORIZONTAL
,
651 LayerImpl
* layer(int id
, TreeID tree_id
) {
652 LayerTreeImpl
* tree
=
653 ((tree_id
== PENDING
) ?
654 host_impl_
.pending_tree() : host_impl_
.active_tree());
657 return tree
->LayerById(id
);
660 bool LayerHasScrollbar(int id
, TreeID tree_id
) {
661 return layer(id
, tree_id
)->HasScrollbar(HORIZONTAL
);
664 ScrollbarLayerImplBase
* pending_scrollbar() {
665 LayerImpl
* layer_impl
= layer(SCROLLBAR
, PENDING
);
667 return layer_impl
->ToScrollbarLayer();
670 LayerImpl
* pending_root() {
671 LayerImpl
* result
= layer(ROOT
, PENDING
);
676 LayerImpl
* active_root() {
677 LayerImpl
* result
= layer(ROOT
, ACTIVE
);
682 LayerTreeSettings
settings() {
683 LayerTreeSettings settings
;
688 FakeImplProxy proxy_
;
689 TestSharedBitmapManager shared_bitmap_manager_
;
690 TestTaskGraphRunner task_graph_runner_
;
691 FakeLayerTreeHostImpl host_impl_
;
694 TEST_F(LayerImplScrollbarSyncTest
, LayerImplBecomesScrollable
) {
695 // In the beginning IV_SCROLL layer is not scrollable.
696 ASSERT_FALSE(layer(IV_SCROLL
, PENDING
)->scrollable());
698 // For pinch virtual viewport the clip layer is the inner viewport
699 // clip layer (IV_CLIP) and the scroll one is the outer viewport
700 // scroll layer (OV_SCROLL).
701 pending_scrollbar()->SetScrollLayerAndClipLayerByIds(OV_SCROLL
, IV_CLIP
);
703 ASSERT_TRUE(LayerHasScrollbar(OV_SCROLL
, PENDING
));
704 ASSERT_TRUE(LayerHasScrollbar(IV_CLIP
, PENDING
));
706 // Synchronize with the active tree.
707 TreeSynchronizer::PushProperties(pending_root(), active_root());
709 ASSERT_TRUE(LayerHasScrollbar(OV_SCROLL
, ACTIVE
));
710 ASSERT_TRUE(LayerHasScrollbar(IV_CLIP
, ACTIVE
));
712 // Make IV_SCROLL layer scrollable.
713 layer(IV_SCROLL
, PENDING
)->SetScrollClipLayer(IV_CLIP
);
714 layer(IV_SCROLL
, PENDING
)->SetNeedsPushProperties();
715 ASSERT_TRUE(layer(IV_SCROLL
, PENDING
)->scrollable());
717 pending_scrollbar()->SetScrollLayerAndClipLayerByIds(OV_SCROLL
, IV_CLIP
);
719 // Now IV_CLIP layer should also receive the scrollbar.
720 ASSERT_TRUE(LayerHasScrollbar(OV_SCROLL
, PENDING
));
721 ASSERT_TRUE(LayerHasScrollbar(IV_CLIP
, PENDING
));
722 ASSERT_TRUE(LayerHasScrollbar(IV_SCROLL
, PENDING
));
724 // Synchronize with the active tree.
725 TreeSynchronizer::PushProperties(pending_root(), active_root());
727 ASSERT_TRUE(layer(IV_SCROLL
, ACTIVE
)->scrollable());
729 ASSERT_TRUE(LayerHasScrollbar(OV_SCROLL
, ACTIVE
));
730 ASSERT_TRUE(LayerHasScrollbar(IV_CLIP
, ACTIVE
));
731 ASSERT_TRUE(LayerHasScrollbar(IV_SCROLL
, ACTIVE
));