1 // Copyright 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "cc/layers/layer_impl.h"
7 #include "cc/output/filter_operation.h"
8 #include "cc/output/filter_operations.h"
9 #include "cc/test/fake_impl_proxy.h"
10 #include "cc/test/fake_layer_tree_host_impl.h"
11 #include "cc/test/fake_output_surface.h"
12 #include "cc/test/geometry_test_utils.h"
13 #include "cc/trees/layer_tree_impl.h"
14 #include "cc/trees/single_thread_proxy.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/skia/include/effects/SkBlurImageFilter.h"
22 #define EXECUTE_AND_VERIFY_SUBTREE_CHANGED(code_to_test) \
23 root->ResetAllChangeTrackingForSubtree(); \
25 EXPECT_TRUE(root->needs_push_properties()); \
26 EXPECT_FALSE(child->needs_push_properties()); \
27 EXPECT_FALSE(grand_child->needs_push_properties()); \
28 EXPECT_TRUE(root->LayerPropertyChanged()); \
29 EXPECT_TRUE(child->LayerPropertyChanged()); \
30 EXPECT_TRUE(grand_child->LayerPropertyChanged());
32 #define EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(code_to_test) \
33 root->ResetAllChangeTrackingForSubtree(); \
35 EXPECT_FALSE(root->needs_push_properties()); \
36 EXPECT_FALSE(child->needs_push_properties()); \
37 EXPECT_FALSE(grand_child->needs_push_properties()); \
38 EXPECT_FALSE(root->LayerPropertyChanged()); \
39 EXPECT_FALSE(child->LayerPropertyChanged()); \
40 EXPECT_FALSE(grand_child->LayerPropertyChanged());
42 #define EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE( \
44 root->ResetAllChangeTrackingForSubtree(); \
46 EXPECT_TRUE(root->needs_push_properties()); \
47 EXPECT_FALSE(child->needs_push_properties()); \
48 EXPECT_FALSE(grand_child->needs_push_properties()); \
49 EXPECT_FALSE(root->LayerPropertyChanged()); \
50 EXPECT_FALSE(child->LayerPropertyChanged()); \
51 EXPECT_FALSE(grand_child->LayerPropertyChanged());
53 #define EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(code_to_test) \
54 root->ResetAllChangeTrackingForSubtree(); \
56 EXPECT_TRUE(root->needs_push_properties()); \
57 EXPECT_FALSE(child->needs_push_properties()); \
58 EXPECT_FALSE(grand_child->needs_push_properties()); \
59 EXPECT_TRUE(root->LayerPropertyChanged()); \
60 EXPECT_FALSE(child->LayerPropertyChanged()); \
61 EXPECT_FALSE(grand_child->LayerPropertyChanged());
63 #define EXECUTE_AND_VERIFY_ONLY_DESCENDANTS_CHANGED(code_to_test) \
64 root->ResetAllChangeTrackingForSubtree(); \
66 EXPECT_TRUE(root->needs_push_properties()); \
67 EXPECT_FALSE(child->needs_push_properties()); \
68 EXPECT_FALSE(grand_child->needs_push_properties()); \
69 EXPECT_FALSE(root->LayerPropertyChanged()); \
70 EXPECT_TRUE(child->LayerPropertyChanged()); \
71 EXPECT_TRUE(grand_child->LayerPropertyChanged());
73 #define VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(code_to_test) \
74 root->ResetAllChangeTrackingForSubtree(); \
75 host_impl.ForcePrepareToDraw(); \
76 EXPECT_FALSE(host_impl.active_tree()->needs_update_draw_properties()); \
78 EXPECT_TRUE(host_impl.active_tree()->needs_update_draw_properties());
80 #define VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(code_to_test) \
81 root->ResetAllChangeTrackingForSubtree(); \
82 host_impl.ForcePrepareToDraw(); \
83 EXPECT_FALSE(host_impl.active_tree()->needs_update_draw_properties()); \
85 EXPECT_FALSE(host_impl.active_tree()->needs_update_draw_properties());
87 TEST(LayerImplTest, VerifyLayerChangesAreTrackedProperly) {
89 // This test checks that layerPropertyChanged() has the correct behavior.
92 // The constructor on this will fake that we are on the correct thread.
93 // Create a simple LayerImpl tree:
95 FakeLayerTreeHostImpl host_impl(&proxy);
96 EXPECT_TRUE(host_impl.InitializeRenderer(CreateFakeOutputSurface()));
97 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
99 scoped_ptr<LayerImpl> scroll_parent =
100 LayerImpl::Create(host_impl.active_tree(), 2);
101 LayerImpl* scroll_child = LayerImpl::Create(host_impl.active_tree(), 3).get();
102 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>();
103 scroll_children->insert(scroll_child);
104 scroll_children->insert(root.get());
106 scoped_ptr<LayerImpl> clip_parent =
107 LayerImpl::Create(host_impl.active_tree(), 4);
108 LayerImpl* clip_child = LayerImpl::Create(host_impl.active_tree(), 5).get();
109 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>();
110 clip_children->insert(clip_child);
111 clip_children->insert(root.get());
113 root->AddChild(LayerImpl::Create(host_impl.active_tree(), 6));
114 LayerImpl* child = root->children()[0];
115 child->AddChild(LayerImpl::Create(host_impl.active_tree(), 7));
116 LayerImpl* grand_child = child->children()[0];
118 root->SetScrollable(true);
120 // Adding children is an internal operation and should not mark layers as
122 EXPECT_FALSE(root->LayerPropertyChanged());
123 EXPECT_FALSE(child->LayerPropertyChanged());
124 EXPECT_FALSE(grand_child->LayerPropertyChanged());
126 gfx::PointF arbitrary_point_f = gfx::PointF(0.125f, 0.25f);
127 float arbitrary_number = 0.352f;
128 gfx::Size arbitrary_size = gfx::Size(111, 222);
129 gfx::Point arbitrary_point = gfx::Point(333, 444);
130 gfx::Vector2d arbitrary_vector2d = gfx::Vector2d(111, 222);
131 gfx::Rect arbitrary_rect = gfx::Rect(arbitrary_point, arbitrary_size);
132 gfx::RectF arbitrary_rect_f =
133 gfx::RectF(arbitrary_point_f, gfx::SizeF(1.234f, 5.678f));
134 SkColor arbitrary_color = SkColorSetRGB(10, 20, 30);
135 gfx::Transform arbitrary_transform;
136 arbitrary_transform.Scale3d(0.1f, 0.2f, 0.3f);
137 FilterOperations arbitrary_filters;
138 arbitrary_filters.Append(FilterOperation::CreateOpacityFilter(0.5f));
139 SkXfermode::Mode arbitrary_blend_mode = SkXfermode::kMultiply_Mode;
141 // These properties are internal, and should not be considered "change" when
143 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
144 root->SetUpdateRect(arbitrary_rect_f));
145 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
146 root->SetMaxScrollOffset(arbitrary_vector2d));
148 // Changing these properties affects the entire subtree of layers.
149 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetAnchorPoint(arbitrary_point_f));
150 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetAnchorPointZ(arbitrary_number));
151 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetFilters(arbitrary_filters));
152 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetFilters(FilterOperations()));
153 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
154 root->SetMaskLayer(LayerImpl::Create(host_impl.active_tree(), 8)));
155 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetMasksToBounds(true));
156 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetContentsOpaque(true));
157 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
158 root->SetReplicaLayer(LayerImpl::Create(host_impl.active_tree(), 9)));
159 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetPosition(arbitrary_point_f));
160 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetPreserves3d(true));
161 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
162 root->SetDoubleSided(false)); // constructor initializes it to "true".
163 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->ScrollBy(arbitrary_vector2d));
164 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetScrollDelta(gfx::Vector2d()));
165 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetScrollOffset(arbitrary_vector2d));
166 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetHideLayerAndSubtree(true));
167 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetOpacity(arbitrary_number));
168 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetBlendMode(arbitrary_blend_mode));
169 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetTransform(arbitrary_transform));
171 // Changing these properties only affects the layer itself.
172 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root->SetContentBounds(arbitrary_size));
173 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(
174 root->SetContentsScale(arbitrary_number, arbitrary_number));
175 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root->SetDrawsContent(true));
176 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(
177 root->SetBackgroundColor(arbitrary_color));
178 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(
179 root->SetBackgroundFilters(arbitrary_filters));
181 // Changing these properties affects all layer's descendants,
182 // but not the layer itself.
183 EXECUTE_AND_VERIFY_ONLY_DESCENDANTS_CHANGED(
184 root->SetSublayerTransform(arbitrary_transform));
186 // Special case: check that SetBounds changes behavior depending on
188 root->SetMasksToBounds(false);
189 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root->SetBounds(gfx::Size(135, 246)));
190 root->SetMasksToBounds(true);
191 // Should be a different size than previous call, to ensure it marks tree
193 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetBounds(arbitrary_size));
195 // Changing this property does not cause the layer to be marked as changed
196 // but does cause the layer to need to push properties.
197 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
198 root->SetIsRootForIsolatedGroup(true));
200 // Changing these properties should cause the layer to need to push properties
201 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
202 root->SetScrollParent(scroll_parent.get()));
203 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
204 root->SetScrollChildren(scroll_children));
205 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
206 root->RemoveScrollChild(scroll_child));
207 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
208 root->SetClipParent(clip_parent.get()));
209 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
210 root->SetClipChildren(clip_children));
211 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
212 root->RemoveClipChild(clip_child));
214 // After setting all these properties already, setting to the exact same
215 // values again should not cause any change.
216 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
217 root->SetAnchorPoint(arbitrary_point_f));
218 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
219 root->SetAnchorPointZ(arbitrary_number));
220 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root->SetMasksToBounds(true));
221 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
222 root->SetPosition(arbitrary_point_f));
223 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root->SetPreserves3d(true));
224 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
225 root->SetTransform(arbitrary_transform));
226 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
227 root->SetDoubleSided(false)); // constructor initializes it to "true".
228 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
229 root->SetScrollDelta(gfx::Vector2d()));
230 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
231 root->SetScrollOffset(arbitrary_vector2d));
232 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
233 root->SetContentBounds(arbitrary_size));
234 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
235 root->SetContentsScale(arbitrary_number, arbitrary_number));
236 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root->SetContentsOpaque(true));
237 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root->SetOpacity(arbitrary_number));
238 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
239 root->SetBlendMode(arbitrary_blend_mode));
240 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
241 root->SetIsRootForIsolatedGroup(true));
242 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root->SetDrawsContent(true));
243 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
244 root->SetSublayerTransform(arbitrary_transform));
245 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root->SetBounds(arbitrary_size));
246 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
247 root->SetScrollParent(scroll_parent.get()));
248 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
249 root->SetScrollChildren(scroll_children));
250 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
251 root->SetClipParent(clip_parent.get()));
252 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
253 root->SetClipChildren(clip_children));
256 TEST(LayerImplTest, VerifyNeedsUpdateDrawProperties) {
258 FakeLayerTreeHostImpl host_impl(&proxy);
259 EXPECT_TRUE(host_impl.InitializeRenderer(CreateFakeOutputSurface()));
260 scoped_ptr<LayerImpl> root = LayerImpl::Create(host_impl.active_tree(), 1);
261 root->SetScrollable(true);
263 gfx::PointF arbitrary_point_f = gfx::PointF(0.125f, 0.25f);
264 float arbitrary_number = 0.352f;
265 gfx::Size arbitrary_size = gfx::Size(111, 222);
266 gfx::Point arbitrary_point = gfx::Point(333, 444);
267 gfx::Vector2d arbitrary_vector2d = gfx::Vector2d(111, 222);
268 gfx::Vector2d large_vector2d = gfx::Vector2d(1000, 1000);
269 gfx::Rect arbitrary_rect = gfx::Rect(arbitrary_point, arbitrary_size);
270 gfx::RectF arbitrary_rect_f =
271 gfx::RectF(arbitrary_point_f, gfx::SizeF(1.234f, 5.678f));
272 SkColor arbitrary_color = SkColorSetRGB(10, 20, 30);
273 gfx::Transform arbitrary_transform;
274 arbitrary_transform.Scale3d(0.1f, 0.2f, 0.3f);
275 FilterOperations arbitrary_filters;
276 arbitrary_filters.Append(FilterOperation::CreateOpacityFilter(0.5f));
277 SkXfermode::Mode arbitrary_blend_mode = SkXfermode::kMultiply_Mode;
279 // Related filter functions.
280 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetFilters(arbitrary_filters));
281 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetFilters(arbitrary_filters));
282 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetFilters(FilterOperations()));
283 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetFilters(arbitrary_filters));
285 // Related scrolling functions.
286 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetMaxScrollOffset(large_vector2d));
287 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
288 root->SetMaxScrollOffset(large_vector2d));
289 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->ScrollBy(arbitrary_vector2d));
290 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->ScrollBy(gfx::Vector2d()));
291 root->SetScrollDelta(gfx::Vector2d(0, 0));
292 host_impl.ForcePrepareToDraw();
293 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetScrollDelta(arbitrary_vector2d));
294 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
295 root->SetScrollDelta(arbitrary_vector2d));
296 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
297 root->SetScrollOffset(arbitrary_vector2d));
298 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
299 root->SetScrollOffset(arbitrary_vector2d));
301 // Unrelated functions, always set to new values, always set needs update.
302 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetAnchorPointZ(arbitrary_number));
303 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
304 root->SetMaskLayer(LayerImpl::Create(host_impl.active_tree(), 4)));
305 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetMasksToBounds(true));
306 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetContentsOpaque(true));
307 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
308 root->SetReplicaLayer(LayerImpl::Create(host_impl.active_tree(), 5)));
309 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetPosition(arbitrary_point_f));
310 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetPreserves3d(true));
311 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
312 root->SetDoubleSided(false)); // constructor initializes it to "true".
313 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetContentBounds(arbitrary_size));
314 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
315 root->SetContentsScale(arbitrary_number, arbitrary_number));
316 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetDrawsContent(true));
317 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
318 root->SetBackgroundColor(arbitrary_color));
319 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
320 root->SetBackgroundFilters(arbitrary_filters));
321 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetOpacity(arbitrary_number));
322 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetBlendMode(arbitrary_blend_mode));
323 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetTransform(arbitrary_transform));
324 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
325 root->SetSublayerTransform(arbitrary_transform));
326 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetBounds(arbitrary_size));
328 // Unrelated functions, set to the same values, no needs update.
329 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
330 root->SetAnchorPointZ(arbitrary_number));
331 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetIsRootForIsolatedGroup(true));
332 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetFilters(arbitrary_filters));
333 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetMasksToBounds(true));
334 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetContentsOpaque(true));
335 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetPosition(arbitrary_point_f));
336 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetPreserves3d(true));
337 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
338 root->SetDoubleSided(false)); // constructor initializes it to "true".
339 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
340 root->SetContentBounds(arbitrary_size));
341 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
342 root->SetContentsScale(arbitrary_number, arbitrary_number));
343 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetDrawsContent(true));
344 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
345 root->SetBackgroundColor(arbitrary_color));
346 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
347 root->SetBackgroundFilters(arbitrary_filters));
348 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetOpacity(arbitrary_number));
349 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
350 root->SetBlendMode(arbitrary_blend_mode));
351 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetIsRootForIsolatedGroup(true));
352 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
353 root->SetTransform(arbitrary_transform));
354 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
355 root->SetSublayerTransform(arbitrary_transform));
356 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(root->SetBounds(arbitrary_size));
359 TEST(LayerImplTest, SafeOpaqueBackgroundColor) {
361 FakeLayerTreeHostImpl host_impl(&proxy);
362 EXPECT_TRUE(host_impl.InitializeRenderer(CreateFakeOutputSurface()));
363 scoped_ptr<LayerImpl> layer = LayerImpl::Create(host_impl.active_tree(), 1);
365 for (int contents_opaque = 0; contents_opaque < 2; ++contents_opaque) {
366 for (int layer_opaque = 0; layer_opaque < 2; ++layer_opaque) {
367 for (int host_opaque = 0; host_opaque < 2; ++host_opaque) {
368 layer->SetContentsOpaque(!!contents_opaque);
369 layer->SetBackgroundColor(layer_opaque ? SK_ColorRED
370 : SK_ColorTRANSPARENT);
371 host_impl.active_tree()->set_background_color(
372 host_opaque ? SK_ColorRED : SK_ColorTRANSPARENT);
374 SkColor safe_color = layer->SafeOpaqueBackgroundColor();
375 if (contents_opaque) {
376 EXPECT_EQ(SkColorGetA(safe_color), 255u)
377 << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
378 << host_opaque << "\n";
380 EXPECT_NE(SkColorGetA(safe_color), 255u)
381 << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
382 << host_opaque << "\n";
389 class LayerImplScrollTest : public testing::Test {
391 LayerImplScrollTest() : host_impl_(&proxy_), root_id_(7) {
392 host_impl_.active_tree()
393 ->SetRootLayer(LayerImpl::Create(host_impl_.active_tree(), root_id_));
394 host_impl_.active_tree()->root_layer()->SetScrollable(true);
397 LayerImpl* layer() { return host_impl_.active_tree()->root_layer(); }
400 FakeImplProxy proxy_;
401 FakeLayerTreeHostImpl host_impl_;
405 TEST_F(LayerImplScrollTest, ScrollByWithZeroOffset) {
406 // Test that LayerImpl::ScrollBy only affects ScrollDelta and total scroll
407 // offset is bounded by the range [0, max scroll offset].
408 gfx::Vector2d max_scroll_offset(50, 80);
409 layer()->SetMaxScrollOffset(max_scroll_offset);
411 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->TotalScrollOffset());
412 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->scroll_offset());
413 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
415 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
416 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
418 EXPECT_VECTOR_EQ(layer()->ScrollDelta(), layer()->TotalScrollOffset());
419 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->scroll_offset());
421 layer()->ScrollBy(gfx::Vector2dF(100, -100));
422 EXPECT_VECTOR_EQ(gfx::Vector2dF(50, 0), layer()->TotalScrollOffset());
424 EXPECT_VECTOR_EQ(layer()->ScrollDelta(), layer()->TotalScrollOffset());
425 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->scroll_offset());
428 TEST_F(LayerImplScrollTest, ScrollByWithNonZeroOffset) {
429 gfx::Vector2d max_scroll_offset(50, 80);
430 gfx::Vector2d scroll_offset(10, 5);
431 layer()->SetMaxScrollOffset(max_scroll_offset);
432 layer()->SetScrollOffset(scroll_offset);
434 EXPECT_VECTOR_EQ(scroll_offset, layer()->TotalScrollOffset());
435 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset());
436 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
438 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
439 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
441 EXPECT_VECTOR_EQ(layer()->ScrollDelta() + scroll_offset,
442 layer()->TotalScrollOffset());
443 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset());
445 layer()->ScrollBy(gfx::Vector2dF(100, -100));
446 EXPECT_VECTOR_EQ(gfx::Vector2dF(50, 0), layer()->TotalScrollOffset());
448 EXPECT_VECTOR_EQ(layer()->ScrollDelta() + scroll_offset,
449 layer()->TotalScrollOffset());
450 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset());
453 class ScrollDelegateIgnore : public LayerScrollOffsetDelegate {
455 virtual void SetMaxScrollOffset(
456 const gfx::Vector2dF& max_scroll_offset) OVERRIDE {}
457 virtual void SetTotalScrollOffset(const gfx::Vector2dF& new_value) OVERRIDE {}
458 virtual gfx::Vector2dF GetTotalScrollOffset() OVERRIDE {
459 return fixed_offset_;
461 virtual bool IsExternalFlingActive() const OVERRIDE { return false; }
463 void set_fixed_offset(const gfx::Vector2dF& fixed_offset) {
464 fixed_offset_ = fixed_offset;
467 virtual void SetTotalPageScaleFactor(float page_scale_factor) OVERRIDE {}
468 virtual void SetScrollableSize(const gfx::SizeF& scrollable_size) OVERRIDE {}
471 gfx::Vector2dF fixed_offset_;
474 TEST_F(LayerImplScrollTest, ScrollByWithIgnoringDelegate) {
475 gfx::Vector2d max_scroll_offset(50, 80);
476 gfx::Vector2d scroll_offset(10, 5);
477 layer()->SetMaxScrollOffset(max_scroll_offset);
478 layer()->SetScrollOffset(scroll_offset);
480 EXPECT_VECTOR_EQ(scroll_offset, layer()->TotalScrollOffset());
481 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset());
482 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
484 ScrollDelegateIgnore delegate;
485 gfx::Vector2dF fixed_offset(32, 12);
486 delegate.set_fixed_offset(fixed_offset);
487 layer()->SetScrollOffsetDelegate(&delegate);
489 EXPECT_VECTOR_EQ(fixed_offset, layer()->TotalScrollOffset());
490 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset());
492 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
494 EXPECT_VECTOR_EQ(fixed_offset, layer()->TotalScrollOffset());
495 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset());
497 layer()->SetScrollOffsetDelegate(NULL);
499 EXPECT_VECTOR_EQ(fixed_offset, layer()->TotalScrollOffset());
500 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset());
502 gfx::Vector2dF scroll_delta(1, 1);
503 layer()->ScrollBy(scroll_delta);
505 EXPECT_VECTOR_EQ(fixed_offset + scroll_delta, layer()->TotalScrollOffset());
506 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset());
509 class ScrollDelegateAccept : public LayerScrollOffsetDelegate {
511 virtual void SetMaxScrollOffset(
512 const gfx::Vector2dF& max_scroll_offset) OVERRIDE {}
513 virtual void SetTotalScrollOffset(const gfx::Vector2dF& new_value) OVERRIDE {
514 current_offset_ = new_value;
516 virtual gfx::Vector2dF GetTotalScrollOffset() OVERRIDE {
517 return current_offset_;
519 virtual bool IsExternalFlingActive() const OVERRIDE { return false; }
520 virtual void SetTotalPageScaleFactor(float page_scale_factor) OVERRIDE {}
521 virtual void SetScrollableSize(const gfx::SizeF& scrollable_size) OVERRIDE {}
524 gfx::Vector2dF current_offset_;
527 TEST_F(LayerImplScrollTest, ScrollByWithAcceptingDelegate) {
528 gfx::Vector2d max_scroll_offset(50, 80);
529 gfx::Vector2d scroll_offset(10, 5);
530 layer()->SetMaxScrollOffset(max_scroll_offset);
531 layer()->SetScrollOffset(scroll_offset);
533 EXPECT_VECTOR_EQ(scroll_offset, layer()->TotalScrollOffset());
534 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset());
535 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
537 ScrollDelegateAccept delegate;
538 layer()->SetScrollOffsetDelegate(&delegate);
540 EXPECT_VECTOR_EQ(scroll_offset, layer()->TotalScrollOffset());
541 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset());
542 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
544 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
546 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
547 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset());
549 layer()->SetScrollOffsetDelegate(NULL);
551 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->TotalScrollOffset());
552 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset());
554 gfx::Vector2dF scroll_delta(1, 1);
555 layer()->ScrollBy(scroll_delta);
557 EXPECT_VECTOR_EQ(gfx::Vector2dF(1, 80), layer()->TotalScrollOffset());
558 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset());
561 TEST_F(LayerImplScrollTest, ApplySentScrollsNoDelegate) {
562 gfx::Vector2d max_scroll_offset(50, 80);
563 gfx::Vector2d scroll_offset(10, 5);
564 gfx::Vector2dF scroll_delta(20.5f, 8.5f);
565 gfx::Vector2d sent_scroll_delta(12, -3);
567 layer()->SetMaxScrollOffset(max_scroll_offset);
568 layer()->SetScrollOffset(scroll_offset);
569 layer()->ScrollBy(scroll_delta);
570 layer()->SetSentScrollDelta(sent_scroll_delta);
572 EXPECT_VECTOR_EQ(scroll_offset + scroll_delta, layer()->TotalScrollOffset());
573 EXPECT_VECTOR_EQ(scroll_delta, layer()->ScrollDelta());
574 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset());
575 EXPECT_VECTOR_EQ(sent_scroll_delta, layer()->sent_scroll_delta());
577 layer()->ApplySentScrollDeltasFromAbortedCommit();
579 EXPECT_VECTOR_EQ(scroll_offset + scroll_delta, layer()->TotalScrollOffset());
580 EXPECT_VECTOR_EQ(scroll_delta - sent_scroll_delta, layer()->ScrollDelta());
581 EXPECT_VECTOR_EQ(scroll_offset + sent_scroll_delta, layer()->scroll_offset());
582 EXPECT_VECTOR_EQ(gfx::Vector2d(), layer()->sent_scroll_delta());
585 TEST_F(LayerImplScrollTest, ApplySentScrollsWithIgnoringDelegate) {
586 gfx::Vector2d max_scroll_offset(50, 80);
587 gfx::Vector2d scroll_offset(10, 5);
588 gfx::Vector2d sent_scroll_delta(12, -3);
589 gfx::Vector2dF fixed_offset(32, 12);
591 layer()->SetMaxScrollOffset(max_scroll_offset);
592 layer()->SetScrollOffset(scroll_offset);
593 ScrollDelegateIgnore delegate;
594 delegate.set_fixed_offset(fixed_offset);
595 layer()->SetScrollOffsetDelegate(&delegate);
596 layer()->SetSentScrollDelta(sent_scroll_delta);
598 EXPECT_VECTOR_EQ(fixed_offset, layer()->TotalScrollOffset());
599 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset());
600 EXPECT_VECTOR_EQ(sent_scroll_delta, layer()->sent_scroll_delta());
602 layer()->ApplySentScrollDeltasFromAbortedCommit();
604 EXPECT_VECTOR_EQ(fixed_offset, layer()->TotalScrollOffset());
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, ApplySentScrollsWithAcceptingDelegate) {
610 gfx::Vector2d max_scroll_offset(50, 80);
611 gfx::Vector2d scroll_offset(10, 5);
612 gfx::Vector2d sent_scroll_delta(12, -3);
613 gfx::Vector2dF scroll_delta(20.5f, 8.5f);
615 layer()->SetMaxScrollOffset(max_scroll_offset);
616 layer()->SetScrollOffset(scroll_offset);
617 ScrollDelegateAccept delegate;
618 layer()->SetScrollOffsetDelegate(&delegate);
619 layer()->ScrollBy(scroll_delta);
620 layer()->SetSentScrollDelta(sent_scroll_delta);
622 EXPECT_VECTOR_EQ(scroll_offset + scroll_delta, layer()->TotalScrollOffset());
623 EXPECT_VECTOR_EQ(scroll_offset, layer()->scroll_offset());
624 EXPECT_VECTOR_EQ(sent_scroll_delta, layer()->sent_scroll_delta());
626 layer()->ApplySentScrollDeltasFromAbortedCommit();
628 EXPECT_VECTOR_EQ(scroll_offset + scroll_delta, layer()->TotalScrollOffset());
629 EXPECT_VECTOR_EQ(scroll_offset + sent_scroll_delta, layer()->scroll_offset());
630 EXPECT_VECTOR_EQ(gfx::Vector2d(), layer()->sent_scroll_delta());
633 // The user-scrollability breaks for zoomed-in pages. So disable this.
634 // http://crbug.com/322223
635 TEST_F(LayerImplScrollTest, DISABLED_ScrollUserUnscrollableLayer) {
636 gfx::Vector2d max_scroll_offset(50, 80);
637 gfx::Vector2d scroll_offset(10, 5);
638 gfx::Vector2dF scroll_delta(20.5f, 8.5f);
640 layer()->set_user_scrollable_vertical(false);
641 layer()->SetMaxScrollOffset(max_scroll_offset);
642 layer()->SetScrollOffset(scroll_offset);
643 gfx::Vector2dF unscrolled = layer()->ScrollBy(scroll_delta);
645 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 8.5f), unscrolled);
646 EXPECT_VECTOR_EQ(gfx::Vector2dF(30.5f, 5), layer()->TotalScrollOffset());