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
));
203 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
204 root
->SetNumDescendantsThatDrawContent(10));
206 // After setting all these properties already, setting to the exact same
207 // values again should not cause any change.
208 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
209 root
->SetTransformOrigin(arbitrary_point_3f
));
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
->Set3dSortingContextId(1));
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 TestSharedBitmapManager shared_bitmap_manager
;
249 FakeLayerTreeHostImpl
host_impl(&proxy
, &shared_bitmap_manager
);
250 EXPECT_TRUE(host_impl
.InitializeRenderer(
251 FakeOutputSurface::Create3d().PassAs
<OutputSurface
>()));
252 host_impl
.active_tree()->SetRootLayer(
253 LayerImpl::Create(host_impl
.active_tree(), 1));
254 LayerImpl
* root
= host_impl
.active_tree()->root_layer();
255 scoped_ptr
<LayerImpl
> layer_ptr
=
256 LayerImpl::Create(host_impl
.active_tree(), 2);
257 LayerImpl
* layer
= layer_ptr
.get();
258 root
->AddChild(layer_ptr
.Pass());
259 layer
->SetScrollClipLayer(root
->id());
260 DCHECK(host_impl
.CanDraw());
262 gfx::PointF arbitrary_point_f
= gfx::PointF(0.125f
, 0.25f
);
263 float arbitrary_number
= 0.352f
;
264 gfx::Size arbitrary_size
= gfx::Size(111, 222);
265 gfx::Point arbitrary_point
= gfx::Point(333, 444);
266 gfx::Vector2d arbitrary_vector2d
= gfx::Vector2d(111, 222);
267 gfx::Size large_size
= gfx::Size(1000, 1000);
268 gfx::Rect arbitrary_rect
= gfx::Rect(arbitrary_point
, arbitrary_size
);
269 gfx::RectF arbitrary_rect_f
=
270 gfx::RectF(arbitrary_point_f
, gfx::SizeF(1.234f
, 5.678f
));
271 SkColor arbitrary_color
= SkColorSetRGB(10, 20, 30);
272 gfx::Transform arbitrary_transform
;
273 arbitrary_transform
.Scale3d(0.1f
, 0.2f
, 0.3f
);
274 FilterOperations arbitrary_filters
;
275 arbitrary_filters
.Append(FilterOperation::CreateOpacityFilter(0.5f
));
276 SkXfermode::Mode arbitrary_blend_mode
= SkXfermode::kMultiply_Mode
;
278 // Related filter functions.
279 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetFilters(arbitrary_filters
));
280 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetFilters(arbitrary_filters
));
281 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetFilters(FilterOperations()));
282 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetFilters(arbitrary_filters
));
284 // Related scrolling functions.
285 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetBounds(large_size
));
286 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetBounds(large_size
));
287 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->ScrollBy(arbitrary_vector2d
));
288 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->ScrollBy(gfx::Vector2d()));
289 layer
->SetScrollDelta(gfx::Vector2d(0, 0));
290 host_impl
.ForcePrepareToDraw();
291 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
292 layer
->SetScrollDelta(arbitrary_vector2d
));
293 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
294 layer
->SetScrollDelta(arbitrary_vector2d
));
295 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
296 layer
->SetScrollOffset(arbitrary_vector2d
));
297 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
298 layer
->SetScrollOffset(arbitrary_vector2d
));
300 // Unrelated functions, always set to new values, always set needs update.
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
->Set3dSortingContextId(1));
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
->SetIsRootForIsolatedGroup(true));
330 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetFilters(arbitrary_filters
));
331 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetMasksToBounds(true));
332 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetContentsOpaque(true));
333 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetPosition(arbitrary_point_f
));
334 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->Set3dSortingContextId(1));
335 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
336 layer
->SetDoubleSided(false)); // constructor initializes it to "true".
337 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
338 layer
->SetContentBounds(arbitrary_size
));
339 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
340 layer
->SetContentsScale(arbitrary_number
, arbitrary_number
));
341 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetDrawsContent(true));
342 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
343 layer
->SetBackgroundColor(arbitrary_color
));
344 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
345 layer
->SetBackgroundFilters(arbitrary_filters
));
346 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetOpacity(arbitrary_number
));
347 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
348 layer
->SetBlendMode(arbitrary_blend_mode
));
349 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
350 layer
->SetIsRootForIsolatedGroup(true));
351 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
352 layer
->SetTransform(arbitrary_transform
));
353 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetBounds(arbitrary_size
));
356 TEST(LayerImplTest
, SafeOpaqueBackgroundColor
) {
358 TestSharedBitmapManager shared_bitmap_manager
;
359 FakeLayerTreeHostImpl
host_impl(&proxy
, &shared_bitmap_manager
);
360 EXPECT_TRUE(host_impl
.InitializeRenderer(
361 FakeOutputSurface::Create3d().PassAs
<OutputSurface
>()));
362 scoped_ptr
<LayerImpl
> layer
= LayerImpl::Create(host_impl
.active_tree(), 1);
364 for (int contents_opaque
= 0; contents_opaque
< 2; ++contents_opaque
) {
365 for (int layer_opaque
= 0; layer_opaque
< 2; ++layer_opaque
) {
366 for (int host_opaque
= 0; host_opaque
< 2; ++host_opaque
) {
367 layer
->SetContentsOpaque(!!contents_opaque
);
368 layer
->SetBackgroundColor(layer_opaque
? SK_ColorRED
369 : SK_ColorTRANSPARENT
);
370 host_impl
.active_tree()->set_background_color(
371 host_opaque
? SK_ColorRED
: SK_ColorTRANSPARENT
);
373 SkColor safe_color
= layer
->SafeOpaqueBackgroundColor();
374 if (contents_opaque
) {
375 EXPECT_EQ(SkColorGetA(safe_color
), 255u)
376 << "Flags: " << contents_opaque
<< ", " << layer_opaque
<< ", "
377 << host_opaque
<< "\n";
379 EXPECT_NE(SkColorGetA(safe_color
), 255u)
380 << "Flags: " << contents_opaque
<< ", " << layer_opaque
<< ", "
381 << host_opaque
<< "\n";
388 TEST(LayerImplTest
, TransformInvertibility
) {
390 TestSharedBitmapManager shared_bitmap_manager
;
391 FakeLayerTreeHostImpl
host_impl(&proxy
, &shared_bitmap_manager
);
393 scoped_ptr
<LayerImpl
> layer
= LayerImpl::Create(host_impl
.active_tree(), 1);
394 EXPECT_TRUE(layer
->transform().IsInvertible());
395 EXPECT_TRUE(layer
->transform_is_invertible());
397 gfx::Transform transform
;
399 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
400 layer
->SetTransform(transform
);
401 EXPECT_FALSE(layer
->transform().IsInvertible());
402 EXPECT_FALSE(layer
->transform_is_invertible());
404 transform
.MakeIdentity();
405 transform
.ApplyPerspectiveDepth(SkDoubleToMScalar(100.0));
406 transform
.RotateAboutZAxis(75.0);
407 transform
.RotateAboutXAxis(32.2);
408 transform
.RotateAboutZAxis(-75.0);
409 transform
.Translate3d(SkDoubleToMScalar(50.5),
410 SkDoubleToMScalar(42.42),
411 SkDoubleToMScalar(-100.25));
413 layer
->SetTransform(transform
);
414 EXPECT_TRUE(layer
->transform().IsInvertible());
415 EXPECT_TRUE(layer
->transform_is_invertible());
418 class LayerImplScrollTest
: public testing::Test
{
420 LayerImplScrollTest()
421 : host_impl_(&proxy_
, &shared_bitmap_manager_
), root_id_(7) {
422 host_impl_
.active_tree()->SetRootLayer(
423 LayerImpl::Create(host_impl_
.active_tree(), root_id_
));
424 host_impl_
.active_tree()->root_layer()->AddChild(
425 LayerImpl::Create(host_impl_
.active_tree(), root_id_
+ 1));
426 layer()->SetScrollClipLayer(root_id_
);
427 // Set the max scroll offset by noting that the root layer has bounds (1,1),
428 // thus whatever bounds are set for the layer will be the max scroll
429 // offset plus 1 in each direction.
430 host_impl_
.active_tree()->root_layer()->SetBounds(gfx::Size(1, 1));
431 gfx::Vector2d
max_scroll_offset(51, 81);
432 layer()->SetBounds(gfx::Size(max_scroll_offset
.x(), max_scroll_offset
.y()));
436 return host_impl_
.active_tree()->root_layer()->children()[0];
439 LayerTreeImpl
* tree() { return host_impl_
.active_tree(); }
442 FakeImplProxy proxy_
;
443 TestSharedBitmapManager shared_bitmap_manager_
;
444 FakeLayerTreeHostImpl host_impl_
;
448 TEST_F(LayerImplScrollTest
, ScrollByWithZeroOffset
) {
449 // Test that LayerImpl::ScrollBy only affects ScrollDelta and total scroll
450 // offset is bounded by the range [0, max scroll offset].
452 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->TotalScrollOffset());
453 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->scroll_offset());
454 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
456 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
457 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
459 EXPECT_VECTOR_EQ(layer()->ScrollDelta(), layer()->TotalScrollOffset());
460 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->scroll_offset());
462 layer()->ScrollBy(gfx::Vector2dF(100, -100));
463 EXPECT_VECTOR_EQ(gfx::Vector2dF(50, 0), layer()->TotalScrollOffset());
465 EXPECT_VECTOR_EQ(layer()->ScrollDelta(), layer()->TotalScrollOffset());
466 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->scroll_offset());
469 TEST_F(LayerImplScrollTest
, ScrollByWithNonZeroOffset
) {
470 gfx::Vector2d
scroll_offset(10, 5);
471 layer()->SetScrollOffset(scroll_offset
);
473 EXPECT_VECTOR_EQ(scroll_offset
, layer()->TotalScrollOffset());
474 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
475 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
477 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
478 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
480 EXPECT_VECTOR_EQ(layer()->ScrollDelta() + scroll_offset
,
481 layer()->TotalScrollOffset());
482 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
484 layer()->ScrollBy(gfx::Vector2dF(100, -100));
485 EXPECT_VECTOR_EQ(gfx::Vector2dF(50, 0), layer()->TotalScrollOffset());
487 EXPECT_VECTOR_EQ(layer()->ScrollDelta() + scroll_offset
,
488 layer()->TotalScrollOffset());
489 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
492 class ScrollDelegateIgnore
: public LayerImpl::ScrollOffsetDelegate
{
494 virtual void SetTotalScrollOffset(const gfx::Vector2dF
& new_value
) OVERRIDE
{}
495 virtual gfx::Vector2dF
GetTotalScrollOffset() OVERRIDE
{
496 return fixed_offset_
;
498 virtual bool IsExternalFlingActive() const OVERRIDE
{ return false; }
500 void set_fixed_offset(const gfx::Vector2dF
& fixed_offset
) {
501 fixed_offset_
= fixed_offset
;
505 gfx::Vector2dF fixed_offset_
;
508 TEST_F(LayerImplScrollTest
, ScrollByWithIgnoringDelegate
) {
509 gfx::Vector2d
scroll_offset(10, 5);
510 layer()->SetScrollOffset(scroll_offset
);
512 EXPECT_VECTOR_EQ(scroll_offset
, layer()->TotalScrollOffset());
513 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
514 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
516 ScrollDelegateIgnore delegate
;
517 gfx::Vector2dF
fixed_offset(32, 12);
518 delegate
.set_fixed_offset(fixed_offset
);
519 layer()->SetScrollOffsetDelegate(&delegate
);
521 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
522 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
524 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
526 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
527 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
529 layer()->SetScrollOffsetDelegate(NULL
);
531 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
532 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
534 gfx::Vector2dF
scroll_delta(1, 1);
535 layer()->ScrollBy(scroll_delta
);
537 EXPECT_VECTOR_EQ(fixed_offset
+ scroll_delta
, layer()->TotalScrollOffset());
538 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
541 class ScrollDelegateAccept
: public LayerImpl::ScrollOffsetDelegate
{
543 virtual void SetTotalScrollOffset(const gfx::Vector2dF
& new_value
) OVERRIDE
{
544 current_offset_
= new_value
;
546 virtual gfx::Vector2dF
GetTotalScrollOffset() OVERRIDE
{
547 return current_offset_
;
549 virtual bool IsExternalFlingActive() const OVERRIDE
{ return false; }
552 gfx::Vector2dF current_offset_
;
555 TEST_F(LayerImplScrollTest
, ScrollByWithAcceptingDelegate
) {
556 gfx::Vector2d
scroll_offset(10, 5);
557 layer()->SetScrollOffset(scroll_offset
);
559 EXPECT_VECTOR_EQ(scroll_offset
, layer()->TotalScrollOffset());
560 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
561 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
563 ScrollDelegateAccept delegate
;
564 layer()->SetScrollOffsetDelegate(&delegate
);
566 EXPECT_VECTOR_EQ(scroll_offset
, layer()->TotalScrollOffset());
567 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
568 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
570 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
572 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
573 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
575 layer()->SetScrollOffsetDelegate(NULL
);
577 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
578 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
580 gfx::Vector2dF
scroll_delta(1, 1);
581 layer()->ScrollBy(scroll_delta
);
583 EXPECT_VECTOR_EQ(gfx::Vector2dF(1, 80), layer()->TotalScrollOffset());
584 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
587 TEST_F(LayerImplScrollTest
, ApplySentScrollsNoDelegate
) {
588 gfx::Vector2d
scroll_offset(10, 5);
589 gfx::Vector2dF
scroll_delta(20.5f
, 8.5f
);
590 gfx::Vector2d
sent_scroll_delta(12, -3);
592 layer()->SetScrollOffset(scroll_offset
);
593 layer()->ScrollBy(scroll_delta
);
594 layer()->SetSentScrollDelta(sent_scroll_delta
);
596 EXPECT_VECTOR_EQ(scroll_offset
+ scroll_delta
, layer()->TotalScrollOffset());
597 EXPECT_VECTOR_EQ(scroll_delta
, layer()->ScrollDelta());
598 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
599 EXPECT_VECTOR_EQ(sent_scroll_delta
, layer()->sent_scroll_delta());
601 layer()->ApplySentScrollDeltasFromAbortedCommit();
603 EXPECT_VECTOR_EQ(scroll_offset
+ scroll_delta
, layer()->TotalScrollOffset());
604 EXPECT_VECTOR_EQ(scroll_delta
- sent_scroll_delta
, layer()->ScrollDelta());
605 EXPECT_VECTOR_EQ(scroll_offset
+ sent_scroll_delta
, layer()->scroll_offset());
606 EXPECT_VECTOR_EQ(gfx::Vector2d(), layer()->sent_scroll_delta());
609 TEST_F(LayerImplScrollTest
, ApplySentScrollsWithIgnoringDelegate
) {
610 gfx::Vector2d
scroll_offset(10, 5);
611 gfx::Vector2d
sent_scroll_delta(12, -3);
612 gfx::Vector2dF
fixed_offset(32, 12);
614 layer()->SetScrollOffset(scroll_offset
);
615 ScrollDelegateIgnore delegate
;
616 delegate
.set_fixed_offset(fixed_offset
);
617 layer()->SetScrollOffsetDelegate(&delegate
);
618 layer()->SetSentScrollDelta(sent_scroll_delta
);
620 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
621 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
622 EXPECT_VECTOR_EQ(sent_scroll_delta
, layer()->sent_scroll_delta());
624 layer()->ApplySentScrollDeltasFromAbortedCommit();
626 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
627 EXPECT_VECTOR_EQ(scroll_offset
+ sent_scroll_delta
, layer()->scroll_offset());
628 EXPECT_VECTOR_EQ(gfx::Vector2d(), layer()->sent_scroll_delta());
631 TEST_F(LayerImplScrollTest
, ApplySentScrollsWithAcceptingDelegate
) {
632 gfx::Vector2d
scroll_offset(10, 5);
633 gfx::Vector2d
sent_scroll_delta(12, -3);
634 gfx::Vector2dF
scroll_delta(20.5f
, 8.5f
);
636 layer()->SetScrollOffset(scroll_offset
);
637 ScrollDelegateAccept delegate
;
638 layer()->SetScrollOffsetDelegate(&delegate
);
639 layer()->ScrollBy(scroll_delta
);
640 layer()->SetSentScrollDelta(sent_scroll_delta
);
642 EXPECT_VECTOR_EQ(scroll_offset
+ scroll_delta
, layer()->TotalScrollOffset());
643 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
644 EXPECT_VECTOR_EQ(sent_scroll_delta
, layer()->sent_scroll_delta());
646 layer()->ApplySentScrollDeltasFromAbortedCommit();
648 EXPECT_VECTOR_EQ(scroll_offset
+ scroll_delta
, layer()->TotalScrollOffset());
649 EXPECT_VECTOR_EQ(scroll_offset
+ sent_scroll_delta
, layer()->scroll_offset());
650 EXPECT_VECTOR_EQ(gfx::Vector2d(), layer()->sent_scroll_delta());
653 // The user-scrollability breaks for zoomed-in pages. So disable this.
654 // http://crbug.com/322223
655 TEST_F(LayerImplScrollTest
, DISABLED_ScrollUserUnscrollableLayer
) {
656 gfx::Vector2d
scroll_offset(10, 5);
657 gfx::Vector2dF
scroll_delta(20.5f
, 8.5f
);
659 layer()->set_user_scrollable_vertical(false);
660 layer()->SetScrollOffset(scroll_offset
);
661 gfx::Vector2dF unscrolled
= layer()->ScrollBy(scroll_delta
);
663 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 8.5f
), unscrolled
);
664 EXPECT_VECTOR_EQ(gfx::Vector2dF(30.5f
, 5), layer()->TotalScrollOffset());
667 TEST_F(LayerImplScrollTest
, SetNewScrollbarParameters
) {
668 gfx::Vector2d
scroll_offset(10, 5);
669 layer()->SetScrollOffset(scroll_offset
);
671 scoped_ptr
<PaintedScrollbarLayerImpl
> vertical_scrollbar(
672 PaintedScrollbarLayerImpl::Create(tree(), 100, VERTICAL
));
673 vertical_scrollbar
->SetScrollLayerAndClipLayerByIds(
674 layer()->id(), tree()->root_layer()->id());
676 int expected_vertical_maximum
=
677 layer()->bounds().height() - tree()->root_layer()->bounds().height();
678 EXPECT_EQ(expected_vertical_maximum
, vertical_scrollbar
->maximum());
679 EXPECT_EQ(scroll_offset
.y(), vertical_scrollbar
->current_pos());
681 scoped_ptr
<PaintedScrollbarLayerImpl
> horizontal_scrollbar(
682 PaintedScrollbarLayerImpl::Create(tree(), 101, HORIZONTAL
));
683 horizontal_scrollbar
->SetScrollLayerAndClipLayerByIds(
684 layer()->id(), tree()->root_layer()->id());
686 int expected_horizontal_maximum
=
687 layer()->bounds().width() - tree()->root_layer()->bounds().width();
688 EXPECT_EQ(expected_horizontal_maximum
, horizontal_scrollbar
->maximum());
689 EXPECT_EQ(scroll_offset
.x(), horizontal_scrollbar
->current_pos());