1 // Copyright 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/layers/layer_impl.h"
7 #include "cc/output/filter_operation.h"
8 #include "cc/output/filter_operations.h"
9 #include "cc/test/fake_impl_proxy.h"
10 #include "cc/test/fake_layer_tree_host_impl.h"
11 #include "cc/test/fake_output_surface.h"
12 #include "cc/test/geometry_test_utils.h"
13 #include "cc/test/test_shared_bitmap_manager.h"
14 #include "cc/trees/layer_tree_impl.h"
15 #include "cc/trees/single_thread_proxy.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18 #include "third_party/skia/include/effects/SkBlurImageFilter.h"
23 #define EXECUTE_AND_VERIFY_SUBTREE_CHANGED(code_to_test) \
24 root->ResetAllChangeTrackingForSubtree(); \
26 EXPECT_TRUE(root->needs_push_properties()); \
27 EXPECT_FALSE(child->needs_push_properties()); \
28 EXPECT_FALSE(grand_child->needs_push_properties()); \
29 EXPECT_TRUE(root->LayerPropertyChanged()); \
30 EXPECT_TRUE(child->LayerPropertyChanged()); \
31 EXPECT_TRUE(grand_child->LayerPropertyChanged());
33 #define EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(code_to_test) \
34 root->ResetAllChangeTrackingForSubtree(); \
36 EXPECT_FALSE(root->needs_push_properties()); \
37 EXPECT_FALSE(child->needs_push_properties()); \
38 EXPECT_FALSE(grand_child->needs_push_properties()); \
39 EXPECT_FALSE(root->LayerPropertyChanged()); \
40 EXPECT_FALSE(child->LayerPropertyChanged()); \
41 EXPECT_FALSE(grand_child->LayerPropertyChanged());
43 #define EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE( \
45 root->ResetAllChangeTrackingForSubtree(); \
47 EXPECT_TRUE(root->needs_push_properties()); \
48 EXPECT_FALSE(child->needs_push_properties()); \
49 EXPECT_FALSE(grand_child->needs_push_properties()); \
50 EXPECT_FALSE(root->LayerPropertyChanged()); \
51 EXPECT_FALSE(child->LayerPropertyChanged()); \
52 EXPECT_FALSE(grand_child->LayerPropertyChanged());
54 #define EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(code_to_test) \
55 root->ResetAllChangeTrackingForSubtree(); \
57 EXPECT_TRUE(root->needs_push_properties()); \
58 EXPECT_FALSE(child->needs_push_properties()); \
59 EXPECT_FALSE(grand_child->needs_push_properties()); \
60 EXPECT_TRUE(root->LayerPropertyChanged()); \
61 EXPECT_FALSE(child->LayerPropertyChanged()); \
62 EXPECT_FALSE(grand_child->LayerPropertyChanged());
64 #define VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(code_to_test) \
65 root->ResetAllChangeTrackingForSubtree(); \
66 host_impl.ForcePrepareToDraw(); \
67 EXPECT_FALSE(host_impl.active_tree()->needs_update_draw_properties()); \
69 EXPECT_TRUE(host_impl.active_tree()->needs_update_draw_properties());
71 #define VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(code_to_test) \
72 root->ResetAllChangeTrackingForSubtree(); \
73 host_impl.ForcePrepareToDraw(); \
74 EXPECT_FALSE(host_impl.active_tree()->needs_update_draw_properties()); \
76 EXPECT_FALSE(host_impl.active_tree()->needs_update_draw_properties());
78 TEST(LayerImplTest
, VerifyLayerChangesAreTrackedProperly
) {
80 // This test checks that layerPropertyChanged() has the correct behavior.
83 // The constructor on this will fake that we are on the correct thread.
84 // Create a simple LayerImpl tree:
86 TestSharedBitmapManager shared_bitmap_manager
;
87 FakeLayerTreeHostImpl
host_impl(&proxy
, &shared_bitmap_manager
);
88 EXPECT_TRUE(host_impl
.InitializeRenderer(
89 FakeOutputSurface::Create3d().PassAs
<OutputSurface
>()));
90 scoped_ptr
<LayerImpl
> root_clip
=
91 LayerImpl::Create(host_impl
.active_tree(), 1);
92 scoped_ptr
<LayerImpl
> root_ptr
=
93 LayerImpl::Create(host_impl
.active_tree(), 2);
94 LayerImpl
* root
= root_ptr
.get();
95 root_clip
->AddChild(root_ptr
.Pass());
96 scoped_ptr
<LayerImpl
> scroll_parent
=
97 LayerImpl::Create(host_impl
.active_tree(), 3);
98 LayerImpl
* scroll_child
= LayerImpl::Create(host_impl
.active_tree(), 4).get();
99 std::set
<LayerImpl
*>* scroll_children
= new std::set
<LayerImpl
*>();
100 scroll_children
->insert(scroll_child
);
101 scroll_children
->insert(root
);
103 scoped_ptr
<LayerImpl
> clip_parent
=
104 LayerImpl::Create(host_impl
.active_tree(), 5);
105 LayerImpl
* clip_child
= LayerImpl::Create(host_impl
.active_tree(), 6).get();
106 std::set
<LayerImpl
*>* clip_children
= new std::set
<LayerImpl
*>();
107 clip_children
->insert(clip_child
);
108 clip_children
->insert(root
);
110 root
->AddChild(LayerImpl::Create(host_impl
.active_tree(), 7));
111 LayerImpl
* child
= root
->children()[0];
112 child
->AddChild(LayerImpl::Create(host_impl
.active_tree(), 8));
113 LayerImpl
* grand_child
= child
->children()[0];
115 root
->SetScrollClipLayer(root_clip
->id());
117 // Adding children is an internal operation and should not mark layers as
119 EXPECT_FALSE(root
->LayerPropertyChanged());
120 EXPECT_FALSE(child
->LayerPropertyChanged());
121 EXPECT_FALSE(grand_child
->LayerPropertyChanged());
123 gfx::PointF arbitrary_point_f
= gfx::PointF(0.125f
, 0.25f
);
124 float arbitrary_number
= 0.352f
;
125 gfx::Size arbitrary_size
= gfx::Size(111, 222);
126 gfx::Point arbitrary_point
= gfx::Point(333, 444);
127 gfx::Vector2d arbitrary_vector2d
= gfx::Vector2d(111, 222);
128 gfx::Rect arbitrary_rect
= gfx::Rect(arbitrary_point
, arbitrary_size
);
129 gfx::RectF arbitrary_rect_f
=
130 gfx::RectF(arbitrary_point_f
, gfx::SizeF(1.234f
, 5.678f
));
131 SkColor arbitrary_color
= SkColorSetRGB(10, 20, 30);
132 gfx::Transform arbitrary_transform
;
133 arbitrary_transform
.Scale3d(0.1f
, 0.2f
, 0.3f
);
134 FilterOperations arbitrary_filters
;
135 arbitrary_filters
.Append(FilterOperation::CreateOpacityFilter(0.5f
));
136 SkXfermode::Mode arbitrary_blend_mode
= SkXfermode::kMultiply_Mode
;
138 // These properties are internal, and should not be considered "change" when
140 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
141 root
->SetUpdateRect(arbitrary_rect_f
));
142 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->SetBounds(arbitrary_size
));
144 // Changing these properties affects the entire subtree of layers.
145 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetAnchorPoint(arbitrary_point_f
));
146 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetAnchorPointZ(arbitrary_number
));
147 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetFilters(arbitrary_filters
));
148 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetFilters(FilterOperations()));
149 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
150 root
->SetMaskLayer(LayerImpl::Create(host_impl
.active_tree(), 9)));
151 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetMasksToBounds(true));
152 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetContentsOpaque(true));
153 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
154 root
->SetReplicaLayer(LayerImpl::Create(host_impl
.active_tree(), 10)));
155 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetPosition(arbitrary_point_f
));
156 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetShouldFlattenTransform(false));
157 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetIs3dSorted(true));
158 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
159 root
->SetDoubleSided(false)); // constructor initializes it to "true".
160 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->ScrollBy(arbitrary_vector2d
));
161 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetScrollDelta(gfx::Vector2d()));
162 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetScrollOffset(arbitrary_vector2d
));
163 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetHideLayerAndSubtree(true));
164 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetOpacity(arbitrary_number
));
165 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetBlendMode(arbitrary_blend_mode
));
166 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetTransform(arbitrary_transform
));
168 // Changing these properties only affects the layer itself.
169 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->SetContentBounds(arbitrary_size
));
170 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(
171 root
->SetContentsScale(arbitrary_number
, arbitrary_number
));
172 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->SetDrawsContent(true));
173 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(
174 root
->SetBackgroundColor(arbitrary_color
));
175 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(
176 root
->SetBackgroundFilters(arbitrary_filters
));
178 // Special case: check that SetBounds changes behavior depending on
180 root
->SetMasksToBounds(false);
181 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root
->SetBounds(gfx::Size(135, 246)));
182 root
->SetMasksToBounds(true);
183 // Should be a different size than previous call, to ensure it marks tree
185 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root
->SetBounds(arbitrary_size
));
187 // Changing this property does not cause the layer to be marked as changed
188 // but does cause the layer to need to push properties.
189 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
190 root
->SetIsRootForIsolatedGroup(true));
192 // Changing these properties should cause the layer to need to push properties
193 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
194 root
->SetScrollParent(scroll_parent
.get()));
195 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
196 root
->SetScrollChildren(scroll_children
));
197 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
198 root
->SetClipParent(clip_parent
.get()));
199 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
200 root
->SetClipChildren(clip_children
));
202 // After setting all these properties already, setting to the exact same
203 // values again should not cause any change.
204 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
205 root
->SetAnchorPoint(arbitrary_point_f
));
206 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
207 root
->SetAnchorPointZ(arbitrary_number
));
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
->SetIs3dSorted(true));
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(layer
->SetAnchorPointZ(arbitrary_number
));
300 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
301 layer
->SetMaskLayer(LayerImpl::Create(host_impl
.active_tree(), 4)));
302 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetMasksToBounds(true));
303 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetContentsOpaque(true));
304 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
305 layer
->SetReplicaLayer(LayerImpl::Create(host_impl
.active_tree(), 5)));
306 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetPosition(arbitrary_point_f
));
307 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetShouldFlattenTransform(false));
308 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetIs3dSorted(true));
310 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
311 layer
->SetDoubleSided(false)); // constructor initializes it to "true".
312 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetContentBounds(arbitrary_size
));
313 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
314 layer
->SetContentsScale(arbitrary_number
, arbitrary_number
));
315 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetDrawsContent(true));
316 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
317 layer
->SetBackgroundColor(arbitrary_color
));
318 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
319 layer
->SetBackgroundFilters(arbitrary_filters
));
320 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetOpacity(arbitrary_number
));
321 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
322 layer
->SetBlendMode(arbitrary_blend_mode
));
323 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetTransform(arbitrary_transform
));
324 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetBounds(arbitrary_size
));
326 // Unrelated functions, set to the same values, no needs update.
327 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
328 layer
->SetAnchorPointZ(arbitrary_number
));
329 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
330 layer
->SetIsRootForIsolatedGroup(true));
331 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetFilters(arbitrary_filters
));
332 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetMasksToBounds(true));
333 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetContentsOpaque(true));
334 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetPosition(arbitrary_point_f
));
335 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetIs3dSorted(true));
336 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
337 layer
->SetDoubleSided(false)); // constructor initializes it to "true".
338 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
339 layer
->SetContentBounds(arbitrary_size
));
340 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
341 layer
->SetContentsScale(arbitrary_number
, arbitrary_number
));
342 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetDrawsContent(true));
343 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
344 layer
->SetBackgroundColor(arbitrary_color
));
345 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
346 layer
->SetBackgroundFilters(arbitrary_filters
));
347 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetOpacity(arbitrary_number
));
348 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
349 layer
->SetBlendMode(arbitrary_blend_mode
));
350 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
351 layer
->SetIsRootForIsolatedGroup(true));
352 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
353 layer
->SetTransform(arbitrary_transform
));
354 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer
->SetBounds(arbitrary_size
));
357 TEST(LayerImplTest
, SafeOpaqueBackgroundColor
) {
359 TestSharedBitmapManager shared_bitmap_manager
;
360 FakeLayerTreeHostImpl
host_impl(&proxy
, &shared_bitmap_manager
);
361 EXPECT_TRUE(host_impl
.InitializeRenderer(
362 FakeOutputSurface::Create3d().PassAs
<OutputSurface
>()));
363 scoped_ptr
<LayerImpl
> layer
= LayerImpl::Create(host_impl
.active_tree(), 1);
365 for (int contents_opaque
= 0; contents_opaque
< 2; ++contents_opaque
) {
366 for (int layer_opaque
= 0; layer_opaque
< 2; ++layer_opaque
) {
367 for (int host_opaque
= 0; host_opaque
< 2; ++host_opaque
) {
368 layer
->SetContentsOpaque(!!contents_opaque
);
369 layer
->SetBackgroundColor(layer_opaque
? SK_ColorRED
370 : SK_ColorTRANSPARENT
);
371 host_impl
.active_tree()->set_background_color(
372 host_opaque
? SK_ColorRED
: SK_ColorTRANSPARENT
);
374 SkColor safe_color
= layer
->SafeOpaqueBackgroundColor();
375 if (contents_opaque
) {
376 EXPECT_EQ(SkColorGetA(safe_color
), 255u)
377 << "Flags: " << contents_opaque
<< ", " << layer_opaque
<< ", "
378 << host_opaque
<< "\n";
380 EXPECT_NE(SkColorGetA(safe_color
), 255u)
381 << "Flags: " << contents_opaque
<< ", " << layer_opaque
<< ", "
382 << host_opaque
<< "\n";
389 TEST(LayerImplTest
, TransformInvertibility
) {
391 TestSharedBitmapManager shared_bitmap_manager
;
392 FakeLayerTreeHostImpl
host_impl(&proxy
, &shared_bitmap_manager
);
394 scoped_ptr
<LayerImpl
> layer
= LayerImpl::Create(host_impl
.active_tree(), 1);
395 EXPECT_TRUE(layer
->transform().IsInvertible());
396 EXPECT_TRUE(layer
->transform_is_invertible());
398 gfx::Transform transform
;
400 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
401 layer
->SetTransform(transform
);
402 EXPECT_FALSE(layer
->transform().IsInvertible());
403 EXPECT_FALSE(layer
->transform_is_invertible());
405 transform
.MakeIdentity();
406 transform
.ApplyPerspectiveDepth(SkDoubleToMScalar(100.0));
407 transform
.RotateAboutZAxis(75.0);
408 transform
.RotateAboutXAxis(32.2);
409 transform
.RotateAboutZAxis(-75.0);
410 transform
.Translate3d(SkDoubleToMScalar(50.5),
411 SkDoubleToMScalar(42.42),
412 SkDoubleToMScalar(-100.25));
414 layer
->SetTransform(transform
);
415 EXPECT_TRUE(layer
->transform().IsInvertible());
416 EXPECT_TRUE(layer
->transform_is_invertible());
419 class LayerImplScrollTest
: public testing::Test
{
421 LayerImplScrollTest()
422 : host_impl_(&proxy_
, &shared_bitmap_manager_
), root_id_(7) {
423 host_impl_
.active_tree()->SetRootLayer(
424 LayerImpl::Create(host_impl_
.active_tree(), root_id_
));
425 host_impl_
.active_tree()->root_layer()->AddChild(
426 LayerImpl::Create(host_impl_
.active_tree(), root_id_
+ 1));
427 layer()->SetScrollClipLayer(root_id_
);
428 // Set the max scroll offset by noting that the root layer has bounds (1,1),
429 // thus whatever bounds are set for the layer will be the max scroll
430 // offset plus 1 in each direction.
431 host_impl_
.active_tree()->root_layer()->SetBounds(gfx::Size(1, 1));
432 gfx::Vector2d
max_scroll_offset(51, 81);
433 layer()->SetBounds(gfx::Size(max_scroll_offset
.x(), max_scroll_offset
.y()));
437 return host_impl_
.active_tree()->root_layer()->children()[0];
441 FakeImplProxy proxy_
;
442 TestSharedBitmapManager shared_bitmap_manager_
;
443 FakeLayerTreeHostImpl host_impl_
;
447 TEST_F(LayerImplScrollTest
, ScrollByWithZeroOffset
) {
448 // Test that LayerImpl::ScrollBy only affects ScrollDelta and total scroll
449 // offset is bounded by the range [0, max scroll offset].
451 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->TotalScrollOffset());
452 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->scroll_offset());
453 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
455 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
456 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
458 EXPECT_VECTOR_EQ(layer()->ScrollDelta(), layer()->TotalScrollOffset());
459 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->scroll_offset());
461 layer()->ScrollBy(gfx::Vector2dF(100, -100));
462 EXPECT_VECTOR_EQ(gfx::Vector2dF(50, 0), layer()->TotalScrollOffset());
464 EXPECT_VECTOR_EQ(layer()->ScrollDelta(), layer()->TotalScrollOffset());
465 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->scroll_offset());
468 TEST_F(LayerImplScrollTest
, ScrollByWithNonZeroOffset
) {
469 gfx::Vector2d
scroll_offset(10, 5);
470 layer()->SetScrollOffset(scroll_offset
);
472 EXPECT_VECTOR_EQ(scroll_offset
, layer()->TotalScrollOffset());
473 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
474 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
476 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
477 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
479 EXPECT_VECTOR_EQ(layer()->ScrollDelta() + scroll_offset
,
480 layer()->TotalScrollOffset());
481 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
483 layer()->ScrollBy(gfx::Vector2dF(100, -100));
484 EXPECT_VECTOR_EQ(gfx::Vector2dF(50, 0), layer()->TotalScrollOffset());
486 EXPECT_VECTOR_EQ(layer()->ScrollDelta() + scroll_offset
,
487 layer()->TotalScrollOffset());
488 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
491 class ScrollDelegateIgnore
: public LayerImpl::ScrollOffsetDelegate
{
493 virtual void SetTotalScrollOffset(const gfx::Vector2dF
& new_value
) OVERRIDE
{}
494 virtual gfx::Vector2dF
GetTotalScrollOffset() OVERRIDE
{
495 return fixed_offset_
;
497 virtual bool IsExternalFlingActive() const OVERRIDE
{ return false; }
499 void set_fixed_offset(const gfx::Vector2dF
& fixed_offset
) {
500 fixed_offset_
= fixed_offset
;
504 gfx::Vector2dF fixed_offset_
;
507 TEST_F(LayerImplScrollTest
, ScrollByWithIgnoringDelegate
) {
508 gfx::Vector2d
scroll_offset(10, 5);
509 layer()->SetScrollOffset(scroll_offset
);
511 EXPECT_VECTOR_EQ(scroll_offset
, layer()->TotalScrollOffset());
512 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
513 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
515 ScrollDelegateIgnore delegate
;
516 gfx::Vector2dF
fixed_offset(32, 12);
517 delegate
.set_fixed_offset(fixed_offset
);
518 layer()->SetScrollOffsetDelegate(&delegate
);
520 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
521 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
523 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
525 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
526 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
528 layer()->SetScrollOffsetDelegate(NULL
);
530 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
531 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
533 gfx::Vector2dF
scroll_delta(1, 1);
534 layer()->ScrollBy(scroll_delta
);
536 EXPECT_VECTOR_EQ(fixed_offset
+ scroll_delta
, layer()->TotalScrollOffset());
537 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
540 class ScrollDelegateAccept
: public LayerImpl::ScrollOffsetDelegate
{
542 virtual void SetTotalScrollOffset(const gfx::Vector2dF
& new_value
) OVERRIDE
{
543 current_offset_
= new_value
;
545 virtual gfx::Vector2dF
GetTotalScrollOffset() OVERRIDE
{
546 return current_offset_
;
548 virtual bool IsExternalFlingActive() const OVERRIDE
{ return false; }
551 gfx::Vector2dF current_offset_
;
554 TEST_F(LayerImplScrollTest
, ScrollByWithAcceptingDelegate
) {
555 gfx::Vector2d
scroll_offset(10, 5);
556 layer()->SetScrollOffset(scroll_offset
);
558 EXPECT_VECTOR_EQ(scroll_offset
, layer()->TotalScrollOffset());
559 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
560 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
562 ScrollDelegateAccept delegate
;
563 layer()->SetScrollOffsetDelegate(&delegate
);
565 EXPECT_VECTOR_EQ(scroll_offset
, layer()->TotalScrollOffset());
566 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
567 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
569 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
571 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
572 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
574 layer()->SetScrollOffsetDelegate(NULL
);
576 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
577 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
579 gfx::Vector2dF
scroll_delta(1, 1);
580 layer()->ScrollBy(scroll_delta
);
582 EXPECT_VECTOR_EQ(gfx::Vector2dF(1, 80), layer()->TotalScrollOffset());
583 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
586 TEST_F(LayerImplScrollTest
, ApplySentScrollsNoDelegate
) {
587 gfx::Vector2d
scroll_offset(10, 5);
588 gfx::Vector2dF
scroll_delta(20.5f
, 8.5f
);
589 gfx::Vector2d
sent_scroll_delta(12, -3);
591 layer()->SetScrollOffset(scroll_offset
);
592 layer()->ScrollBy(scroll_delta
);
593 layer()->SetSentScrollDelta(sent_scroll_delta
);
595 EXPECT_VECTOR_EQ(scroll_offset
+ scroll_delta
, layer()->TotalScrollOffset());
596 EXPECT_VECTOR_EQ(scroll_delta
, layer()->ScrollDelta());
597 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
598 EXPECT_VECTOR_EQ(sent_scroll_delta
, layer()->sent_scroll_delta());
600 layer()->ApplySentScrollDeltasFromAbortedCommit();
602 EXPECT_VECTOR_EQ(scroll_offset
+ scroll_delta
, layer()->TotalScrollOffset());
603 EXPECT_VECTOR_EQ(scroll_delta
- sent_scroll_delta
, layer()->ScrollDelta());
604 EXPECT_VECTOR_EQ(scroll_offset
+ sent_scroll_delta
, layer()->scroll_offset());
605 EXPECT_VECTOR_EQ(gfx::Vector2d(), layer()->sent_scroll_delta());
608 TEST_F(LayerImplScrollTest
, ApplySentScrollsWithIgnoringDelegate
) {
609 gfx::Vector2d
scroll_offset(10, 5);
610 gfx::Vector2d
sent_scroll_delta(12, -3);
611 gfx::Vector2dF
fixed_offset(32, 12);
613 layer()->SetScrollOffset(scroll_offset
);
614 ScrollDelegateIgnore delegate
;
615 delegate
.set_fixed_offset(fixed_offset
);
616 layer()->SetScrollOffsetDelegate(&delegate
);
617 layer()->SetSentScrollDelta(sent_scroll_delta
);
619 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
620 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
621 EXPECT_VECTOR_EQ(sent_scroll_delta
, layer()->sent_scroll_delta());
623 layer()->ApplySentScrollDeltasFromAbortedCommit();
625 EXPECT_VECTOR_EQ(fixed_offset
, layer()->TotalScrollOffset());
626 EXPECT_VECTOR_EQ(scroll_offset
+ sent_scroll_delta
, layer()->scroll_offset());
627 EXPECT_VECTOR_EQ(gfx::Vector2d(), layer()->sent_scroll_delta());
630 TEST_F(LayerImplScrollTest
, ApplySentScrollsWithAcceptingDelegate
) {
631 gfx::Vector2d
scroll_offset(10, 5);
632 gfx::Vector2d
sent_scroll_delta(12, -3);
633 gfx::Vector2dF
scroll_delta(20.5f
, 8.5f
);
635 layer()->SetScrollOffset(scroll_offset
);
636 ScrollDelegateAccept delegate
;
637 layer()->SetScrollOffsetDelegate(&delegate
);
638 layer()->ScrollBy(scroll_delta
);
639 layer()->SetSentScrollDelta(sent_scroll_delta
);
641 EXPECT_VECTOR_EQ(scroll_offset
+ scroll_delta
, layer()->TotalScrollOffset());
642 EXPECT_VECTOR_EQ(scroll_offset
, layer()->scroll_offset());
643 EXPECT_VECTOR_EQ(sent_scroll_delta
, layer()->sent_scroll_delta());
645 layer()->ApplySentScrollDeltasFromAbortedCommit();
647 EXPECT_VECTOR_EQ(scroll_offset
+ scroll_delta
, layer()->TotalScrollOffset());
648 EXPECT_VECTOR_EQ(scroll_offset
+ sent_scroll_delta
, layer()->scroll_offset());
649 EXPECT_VECTOR_EQ(gfx::Vector2d(), layer()->sent_scroll_delta());
652 // The user-scrollability breaks for zoomed-in pages. So disable this.
653 // http://crbug.com/322223
654 TEST_F(LayerImplScrollTest
, DISABLED_ScrollUserUnscrollableLayer
) {
655 gfx::Vector2d
scroll_offset(10, 5);
656 gfx::Vector2dF
scroll_delta(20.5f
, 8.5f
);
658 layer()->set_user_scrollable_vertical(false);
659 layer()->SetScrollOffset(scroll_offset
);
660 gfx::Vector2dF unscrolled
= layer()->ScrollBy(scroll_delta
);
662 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 8.5f
), unscrolled
);
663 EXPECT_VECTOR_EQ(gfx::Vector2dF(30.5f
, 5), layer()->TotalScrollOffset());