Adding Peter Thatcher to the owners file.
[chromium-blink-merge.git] / cc / layers / layer_impl_unittest.cc
blobd9811bc1553a4f71bef65cc056a4a7e00b4818ed
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"
24 namespace cc {
25 namespace {
27 #define EXECUTE_AND_VERIFY_SUBTREE_CHANGED(code_to_test) \
28 root->ResetAllChangeTrackingForSubtree(); \
29 code_to_test; \
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(); \
39 code_to_test; \
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( \
48 code_to_test) \
49 root->ResetAllChangeTrackingForSubtree(); \
50 code_to_test; \
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(); \
60 code_to_test; \
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()); \
72 code_to_test; \
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()); \
79 code_to_test; \
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:
89 FakeImplProxy proxy;
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
122 // changed.
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
144 // they are used.
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
185 // masksToBounds.
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
190 // changed.
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) {
252 FakeImplProxy proxy;
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
288 // filters.
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) {
370 FakeImplProxy proxy;
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";
390 } else {
391 EXPECT_NE(SkColorGetA(safe_color), 255u)
392 << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
393 << host_opaque << "\n";
400 TEST(LayerImplTest, TransformInvertibility) {
401 FakeImplProxy proxy;
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;
410 transform.Scale3d(
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 {
431 public:
432 LayerImplScrollTest()
433 : host_impl_(settings(),
434 &proxy_,
435 &shared_bitmap_manager_,
436 &task_graph_runner_),
437 root_id_(7) {
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()));
451 LayerImpl* layer() {
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;
461 settings.use_pinch_virtual_viewport = true;
462 return settings;
465 private:
466 FakeImplProxy proxy_;
467 TestSharedBitmapManager shared_bitmap_manager_;
468 TestTaskGraphRunner task_graph_runner_;
469 FakeLayerTreeHostImpl host_impl_;
470 int root_id_;
473 TEST_F(LayerImplScrollTest, ScrollByWithZeroOffset) {
474 // Test that LayerImpl::ScrollBy only affects ScrollDelta and total scroll
475 // offset is bounded by the range [0, max scroll offset].
477 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->CurrentScrollOffset());
478 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->BaseScrollOffset());
479 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
481 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
482 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->CurrentScrollOffset());
484 EXPECT_VECTOR_EQ(layer()->ScrollDelta(), layer()->CurrentScrollOffset());
485 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->BaseScrollOffset());
487 layer()->ScrollBy(gfx::Vector2dF(100, -100));
488 EXPECT_VECTOR_EQ(gfx::Vector2dF(50, 0), layer()->CurrentScrollOffset());
490 EXPECT_VECTOR_EQ(layer()->ScrollDelta(), layer()->CurrentScrollOffset());
491 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->BaseScrollOffset());
494 TEST_F(LayerImplScrollTest, ScrollByWithNonZeroOffset) {
495 gfx::ScrollOffset scroll_offset(10, 5);
496 layer()->PushScrollOffsetFromMainThread(scroll_offset);
498 EXPECT_VECTOR_EQ(scroll_offset, layer()->CurrentScrollOffset());
499 EXPECT_VECTOR_EQ(scroll_offset, layer()->BaseScrollOffset());
500 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
502 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
503 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->CurrentScrollOffset());
505 EXPECT_VECTOR_EQ(
506 gfx::ScrollOffsetWithDelta(scroll_offset, layer()->ScrollDelta()),
507 layer()->CurrentScrollOffset());
508 EXPECT_VECTOR_EQ(scroll_offset, layer()->BaseScrollOffset());
510 layer()->ScrollBy(gfx::Vector2dF(100, -100));
511 EXPECT_VECTOR_EQ(gfx::Vector2dF(50, 0), layer()->CurrentScrollOffset());
513 EXPECT_VECTOR_EQ(
514 gfx::ScrollOffsetWithDelta(scroll_offset, layer()->ScrollDelta()),
515 layer()->CurrentScrollOffset());
516 EXPECT_VECTOR_EQ(scroll_offset, layer()->BaseScrollOffset());
519 TEST_F(LayerImplScrollTest, ApplySentScrollsNoListener) {
520 gfx::ScrollOffset scroll_offset(10, 5);
521 gfx::Vector2dF scroll_delta(20.5f, 8.5f);
522 gfx::Vector2d sent_scroll_delta(12, -3);
524 layer()->PushScrollOffsetFromMainThread(scroll_offset);
525 layer()->ScrollBy(sent_scroll_delta);
526 layer()->PullDeltaForMainThread();
527 layer()->SetCurrentScrollOffset(scroll_offset +
528 gfx::ScrollOffset(scroll_delta));
530 EXPECT_VECTOR_EQ(gfx::ScrollOffsetWithDelta(scroll_offset, scroll_delta),
531 layer()->CurrentScrollOffset());
532 EXPECT_VECTOR_EQ(scroll_delta, layer()->ScrollDelta());
533 EXPECT_VECTOR_EQ(scroll_offset, layer()->BaseScrollOffset());
535 layer()->ApplySentScrollDeltasFromAbortedCommit();
537 EXPECT_VECTOR_EQ(gfx::ScrollOffsetWithDelta(scroll_offset, scroll_delta),
538 layer()->CurrentScrollOffset());
539 EXPECT_VECTOR_EQ(scroll_delta - sent_scroll_delta, layer()->ScrollDelta());
540 EXPECT_VECTOR_EQ(gfx::ScrollOffsetWithDelta(scroll_offset, sent_scroll_delta),
541 layer()->BaseScrollOffset());
544 TEST_F(LayerImplScrollTest, ScrollUserUnscrollableLayer) {
545 gfx::ScrollOffset scroll_offset(10, 5);
546 gfx::Vector2dF scroll_delta(20.5f, 8.5f);
548 layer()->set_user_scrollable_vertical(false);
549 layer()->PushScrollOffsetFromMainThread(scroll_offset);
550 gfx::Vector2dF unscrolled = layer()->ScrollBy(scroll_delta);
552 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 8.5f), unscrolled);
553 EXPECT_VECTOR_EQ(gfx::Vector2dF(30.5f, 5), layer()->CurrentScrollOffset());
556 TEST_F(LayerImplScrollTest, PushPropertiesToMirrorsCurrentScrollOffset) {
557 gfx::ScrollOffset scroll_offset(10, 5);
558 gfx::Vector2dF scroll_delta(12, 18);
560 host_impl().CreatePendingTree();
562 layer()->PushScrollOffsetFromMainThread(scroll_offset);
563 gfx::Vector2dF unscrolled = layer()->ScrollBy(scroll_delta);
565 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 0), unscrolled);
566 EXPECT_VECTOR_EQ(gfx::Vector2dF(22, 23), layer()->CurrentScrollOffset());
568 layer()->PullDeltaForMainThread();
570 scoped_ptr<LayerImpl> pending_layer = LayerImpl::Create(
571 host_impl().sync_tree(), layer()->id(), layer()->synced_scroll_offset());
572 pending_layer->PushScrollOffsetFromMainThread(layer()->CurrentScrollOffset());
574 pending_layer->PushPropertiesTo(layer());
576 EXPECT_VECTOR_EQ(gfx::Vector2dF(22, 23), layer()->CurrentScrollOffset());
577 EXPECT_VECTOR_EQ(layer()->CurrentScrollOffset(),
578 pending_layer->CurrentScrollOffset());
581 TEST_F(LayerImplScrollTest, SetNewScrollbarParameters) {
582 gfx::ScrollOffset scroll_offset(10, 5);
583 layer()->PushScrollOffsetFromMainThread(scroll_offset);
585 scoped_ptr<PaintedScrollbarLayerImpl> vertical_scrollbar(
586 PaintedScrollbarLayerImpl::Create(tree(), 100, VERTICAL));
587 vertical_scrollbar->SetScrollLayerAndClipLayerByIds(
588 layer()->id(), tree()->root_layer()->id());
590 int expected_vertical_maximum =
591 layer()->bounds().height() - tree()->root_layer()->bounds().height();
592 EXPECT_EQ(expected_vertical_maximum, vertical_scrollbar->maximum());
593 EXPECT_EQ(scroll_offset.y(), vertical_scrollbar->current_pos());
595 scoped_ptr<PaintedScrollbarLayerImpl> horizontal_scrollbar(
596 PaintedScrollbarLayerImpl::Create(tree(), 101, HORIZONTAL));
597 horizontal_scrollbar->SetScrollLayerAndClipLayerByIds(
598 layer()->id(), tree()->root_layer()->id());
600 int expected_horizontal_maximum =
601 layer()->bounds().width() - tree()->root_layer()->bounds().width();
602 EXPECT_EQ(expected_horizontal_maximum, horizontal_scrollbar->maximum());
603 EXPECT_EQ(scroll_offset.x(), horizontal_scrollbar->current_pos());
606 class LayerImplScrollbarSyncTest : public testing::Test {
607 public:
608 enum {
609 ROOT = 1,
610 IV_CLIP = 2,
611 PAGE = 3,
612 IV_SCROLL = 4,
613 SCROLLBAR = 5,
614 OLD_ROOT = 6,
615 OV_CLIP = 7,
616 OV_SCROLL = 8,
618 enum TreeID {
619 PENDING,
620 ACTIVE
623 LayerImplScrollbarSyncTest()
624 : host_impl_(settings(),
625 &proxy_,
626 &shared_bitmap_manager_,
627 &task_graph_runner_) {
628 host_impl_.CreatePendingTree();
630 CreateLayers(host_impl_.pending_tree());
631 CreateLayers(host_impl_.active_tree());
634 void CreateLayers(LayerTreeImpl * tree) {
635 tree->SetRootLayer(LayerImpl::Create(tree, ROOT));
636 LayerImpl * root = tree->root_layer();
637 ASSERT_TRUE(root != nullptr);
639 int hierarchy[] = {IV_CLIP, PAGE, IV_SCROLL, OLD_ROOT, OV_CLIP, OV_SCROLL};
640 LayerImpl * parent = root;
641 for (int child_id : hierarchy) {
642 parent->AddChild(LayerImpl::Create(tree, child_id));
643 parent = tree->LayerById(child_id);
644 ASSERT_TRUE(parent != nullptr);
647 root->AddChild(
648 SolidColorScrollbarLayerImpl::Create(tree, SCROLLBAR, HORIZONTAL,
649 5, 5, false, true));
652 LayerImpl* layer(int id, TreeID tree_id) {
653 LayerTreeImpl* tree =
654 ((tree_id == PENDING) ?
655 host_impl_.pending_tree() : host_impl_.active_tree());
657 assert(tree);
658 return tree->LayerById(id);
661 bool LayerHasScrollbar(int id, TreeID tree_id) {
662 return layer(id, tree_id)->HasScrollbar(HORIZONTAL);
665 ScrollbarLayerImplBase* pending_scrollbar() {
666 LayerImpl* layer_impl = layer(SCROLLBAR, PENDING);
667 assert(layer_impl);
668 return layer_impl->ToScrollbarLayer();
671 LayerImpl* pending_root() {
672 LayerImpl * result = layer(ROOT, PENDING);
673 assert(result);
674 return result;
677 LayerImpl* active_root() {
678 LayerImpl * result = layer(ROOT, ACTIVE);
679 assert(result);
680 return result;
683 LayerTreeSettings settings() {
684 LayerTreeSettings settings;
685 settings.use_pinch_virtual_viewport = true;
686 return settings;
689 private:
690 FakeImplProxy proxy_;
691 TestSharedBitmapManager shared_bitmap_manager_;
692 TestTaskGraphRunner task_graph_runner_;
693 FakeLayerTreeHostImpl host_impl_;
696 TEST_F(LayerImplScrollbarSyncTest, LayerImplBecomesScrollable) {
697 // In the beginning IV_SCROLL layer is not scrollable.
698 ASSERT_FALSE(layer(IV_SCROLL, PENDING)->scrollable());
700 // For pinch virtual viewport the clip layer is the inner viewport
701 // clip layer (IV_CLIP) and the scroll one is the outer viewport
702 // scroll layer (OV_SCROLL).
703 pending_scrollbar()->SetScrollLayerAndClipLayerByIds(OV_SCROLL, IV_CLIP);
705 ASSERT_TRUE(LayerHasScrollbar(OV_SCROLL, PENDING));
706 ASSERT_TRUE(LayerHasScrollbar(IV_CLIP, PENDING));
708 // Synchronize with the active tree.
709 TreeSynchronizer::PushProperties(pending_root(), active_root());
711 ASSERT_TRUE(LayerHasScrollbar(OV_SCROLL, ACTIVE));
712 ASSERT_TRUE(LayerHasScrollbar(IV_CLIP, ACTIVE));
714 // Make IV_SCROLL layer scrollable.
715 layer(IV_SCROLL, PENDING)->SetScrollClipLayer(IV_CLIP);
716 layer(IV_SCROLL, PENDING)->SetNeedsPushProperties();
717 ASSERT_TRUE(layer(IV_SCROLL, PENDING)->scrollable());
719 pending_scrollbar()->SetScrollLayerAndClipLayerByIds(OV_SCROLL, IV_CLIP);
721 // Now IV_CLIP layer should also receive the scrollbar.
722 ASSERT_TRUE(LayerHasScrollbar(OV_SCROLL, PENDING));
723 ASSERT_TRUE(LayerHasScrollbar(IV_CLIP, PENDING));
724 ASSERT_TRUE(LayerHasScrollbar(IV_SCROLL, PENDING));
726 // Synchronize with the active tree.
727 TreeSynchronizer::PushProperties(pending_root(), active_root());
729 ASSERT_TRUE(layer(IV_SCROLL, ACTIVE)->scrollable());
731 ASSERT_TRUE(LayerHasScrollbar(OV_SCROLL, ACTIVE));
732 ASSERT_TRUE(LayerHasScrollbar(IV_CLIP, ACTIVE));
733 ASSERT_TRUE(LayerHasScrollbar(IV_SCROLL, ACTIVE));
736 } // namespace
737 } // namespace cc