Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / cc / layers / layer_impl_unittest.cc
blob0676597eb760f87922263ecca98ea3d1423dc43f
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 TestTaskGraphRunner task_graph_runner;
92 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
93 &task_graph_runner);
94 EXPECT_TRUE(host_impl.InitializeRenderer(FakeOutputSurface::Create3d()));
95 scoped_ptr<LayerImpl> root_clip =
96 LayerImpl::Create(host_impl.active_tree(), 1);
97 scoped_ptr<LayerImpl> root_ptr =
98 LayerImpl::Create(host_impl.active_tree(), 2);
99 LayerImpl* root = root_ptr.get();
100 root_clip->AddChild(root_ptr.Pass());
101 scoped_ptr<LayerImpl> scroll_parent =
102 LayerImpl::Create(host_impl.active_tree(), 3);
103 LayerImpl* scroll_child = LayerImpl::Create(host_impl.active_tree(), 4).get();
104 std::set<LayerImpl*>* scroll_children = new std::set<LayerImpl*>();
105 scroll_children->insert(scroll_child);
106 scroll_children->insert(root);
107 root->SetHasRenderSurface(true);
109 scoped_ptr<LayerImpl> clip_parent =
110 LayerImpl::Create(host_impl.active_tree(), 5);
111 LayerImpl* clip_child = LayerImpl::Create(host_impl.active_tree(), 6).get();
112 std::set<LayerImpl*>* clip_children = new std::set<LayerImpl*>();
113 clip_children->insert(clip_child);
114 clip_children->insert(root);
116 root->AddChild(LayerImpl::Create(host_impl.active_tree(), 7));
117 LayerImpl* child = root->children()[0];
118 child->AddChild(LayerImpl::Create(host_impl.active_tree(), 8));
119 LayerImpl* grand_child = child->children()[0];
121 root->SetScrollClipLayer(root_clip->id());
123 // Adding children is an internal operation and should not mark layers as
124 // changed.
125 EXPECT_FALSE(root->LayerPropertyChanged());
126 EXPECT_FALSE(child->LayerPropertyChanged());
127 EXPECT_FALSE(grand_child->LayerPropertyChanged());
129 gfx::PointF arbitrary_point_f = gfx::PointF(0.125f, 0.25f);
130 gfx::Point3F arbitrary_point_3f = gfx::Point3F(0.125f, 0.25f, 0.f);
131 float arbitrary_number = 0.352f;
132 gfx::Size arbitrary_size = gfx::Size(111, 222);
133 gfx::Point arbitrary_point = gfx::Point(333, 444);
134 gfx::Vector2d arbitrary_vector2d = gfx::Vector2d(111, 222);
135 gfx::Rect arbitrary_rect = gfx::Rect(arbitrary_point, arbitrary_size);
136 gfx::RectF arbitrary_rect_f =
137 gfx::RectF(arbitrary_point_f, gfx::SizeF(1.234f, 5.678f));
138 SkColor arbitrary_color = SkColorSetRGB(10, 20, 30);
139 gfx::Transform arbitrary_transform;
140 arbitrary_transform.Scale3d(0.1f, 0.2f, 0.3f);
141 FilterOperations arbitrary_filters;
142 arbitrary_filters.Append(FilterOperation::CreateOpacityFilter(0.5f));
143 SkXfermode::Mode arbitrary_blend_mode = SkXfermode::kMultiply_Mode;
145 // These properties are internal, and should not be considered "change" when
146 // they are used.
147 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
148 root->SetUpdateRect(arbitrary_rect));
149 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root->SetBounds(arbitrary_size));
151 // Changing these properties affects the entire subtree of layers.
152 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
153 root->SetTransformOrigin(arbitrary_point_3f));
154 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetFilters(arbitrary_filters));
155 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetFilters(FilterOperations()));
156 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
157 root->SetMaskLayer(LayerImpl::Create(host_impl.active_tree(), 9)));
158 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetMasksToBounds(true));
159 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetContentsOpaque(true));
160 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
161 root->SetReplicaLayer(LayerImpl::Create(host_impl.active_tree(), 10)));
162 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetPosition(arbitrary_point_f));
163 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetShouldFlattenTransform(false));
164 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->Set3dSortingContextId(1));
165 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(
166 root->SetDoubleSided(false)); // constructor initializes it to "true".
167 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->ScrollBy(arbitrary_vector2d));
168 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetScrollDelta(gfx::Vector2d()));
169 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->PushScrollOffsetFromMainThread(
170 gfx::ScrollOffset(arbitrary_vector2d)));
171 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetHideLayerAndSubtree(true));
172 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetOpacity(arbitrary_number));
173 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetBlendMode(arbitrary_blend_mode));
174 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetTransform(arbitrary_transform));
176 // Changing these properties only affects the layer itself.
177 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root->SetDrawsContent(true));
178 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(
179 root->SetBackgroundColor(arbitrary_color));
180 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(
181 root->SetBackgroundFilters(arbitrary_filters));
183 // Special case: check that SetBounds changes behavior depending on
184 // masksToBounds.
185 root->SetMasksToBounds(false);
186 EXECUTE_AND_VERIFY_ONLY_LAYER_CHANGED(root->SetBounds(gfx::Size(135, 246)));
187 root->SetMasksToBounds(true);
188 // Should be a different size than previous call, to ensure it marks tree
189 // changed.
190 EXECUTE_AND_VERIFY_SUBTREE_CHANGED(root->SetBounds(arbitrary_size));
192 // Changing this property does not cause the layer to be marked as changed
193 // but does cause the layer to need to push properties.
194 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
195 root->SetIsRootForIsolatedGroup(true));
197 // Changing these properties should cause the layer to need to push properties
198 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
199 root->SetScrollParent(scroll_parent.get()));
200 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
201 root->SetScrollChildren(scroll_children));
202 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
203 root->SetClipParent(clip_parent.get()));
204 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
205 root->SetClipChildren(clip_children));
206 EXECUTE_AND_VERIFY_NEEDS_PUSH_PROPERTIES_AND_SUBTREE_DID_NOT_CHANGE(
207 root->SetNumDescendantsThatDrawContent(10));
209 // After setting all these properties already, setting to the exact same
210 // values again should not cause any change.
211 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
212 root->SetTransformOrigin(arbitrary_point_3f));
213 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root->SetMasksToBounds(true));
214 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
215 root->SetPosition(arbitrary_point_f));
216 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
217 root->SetShouldFlattenTransform(false));
218 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(root->Set3dSortingContextId(1));
219 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
220 root->SetTransform(arbitrary_transform));
221 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
222 root->SetDoubleSided(false)); // constructor initializes it to "true".
223 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
224 root->SetScrollDelta(gfx::Vector2d()));
225 EXECUTE_AND_VERIFY_SUBTREE_DID_NOT_CHANGE(
226 root->PushScrollOffsetFromMainThread(
227 gfx::ScrollOffset(arbitrary_vector2d)));
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) {
247 FakeImplProxy proxy;
248 TestSharedBitmapManager shared_bitmap_manager;
249 TestTaskGraphRunner task_graph_runner;
250 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
251 &task_graph_runner);
252 EXPECT_TRUE(host_impl.InitializeRenderer(FakeOutputSurface::Create3d()));
253 host_impl.active_tree()->SetRootLayer(
254 LayerImpl::Create(host_impl.active_tree(), 1));
255 LayerImpl* root = host_impl.active_tree()->root_layer();
256 root->SetHasRenderSurface(true);
257 scoped_ptr<LayerImpl> layer_ptr =
258 LayerImpl::Create(host_impl.active_tree(), 2);
259 LayerImpl* layer = layer_ptr.get();
260 root->AddChild(layer_ptr.Pass());
261 layer->SetScrollClipLayer(root->id());
262 DCHECK(host_impl.CanDraw());
264 gfx::PointF arbitrary_point_f = gfx::PointF(0.125f, 0.25f);
265 float arbitrary_number = 0.352f;
266 gfx::Size arbitrary_size = gfx::Size(111, 222);
267 gfx::Point arbitrary_point = gfx::Point(333, 444);
268 gfx::Vector2d arbitrary_vector2d = gfx::Vector2d(111, 222);
269 gfx::Size large_size = gfx::Size(1000, 1000);
270 gfx::Rect arbitrary_rect = gfx::Rect(arbitrary_point, arbitrary_size);
271 gfx::RectF arbitrary_rect_f =
272 gfx::RectF(arbitrary_point_f, gfx::SizeF(1.234f, 5.678f));
273 SkColor arbitrary_color = SkColorSetRGB(10, 20, 30);
274 gfx::Transform arbitrary_transform;
275 arbitrary_transform.Scale3d(0.1f, 0.2f, 0.3f);
276 FilterOperations arbitrary_filters;
277 arbitrary_filters.Append(FilterOperation::CreateOpacityFilter(0.5f));
278 SkXfermode::Mode arbitrary_blend_mode = SkXfermode::kMultiply_Mode;
280 // Render surface functions.
281 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetHasRenderSurface(true));
282 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetHasRenderSurface(true));
283 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetHasRenderSurface(false));
284 // Create a render surface, because we must have a render surface if we have
285 // filters.
286 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetHasRenderSurface(true));
288 // Related filter functions.
289 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetFilters(arbitrary_filters));
290 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetFilters(arbitrary_filters));
291 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetFilters(FilterOperations()));
292 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetFilters(arbitrary_filters));
294 // Related scrolling functions.
295 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetBounds(large_size));
296 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetBounds(large_size));
297 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->ScrollBy(arbitrary_vector2d));
298 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer->ScrollBy(gfx::Vector2d()));
299 layer->SetScrollDelta(gfx::Vector2d(0, 0));
300 host_impl.ForcePrepareToDraw();
301 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
302 layer->SetScrollDelta(arbitrary_vector2d));
303 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
304 layer->SetScrollDelta(arbitrary_vector2d));
305 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->PushScrollOffsetFromMainThread(
306 gfx::ScrollOffset(arbitrary_vector2d)));
307 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer->PushScrollOffsetFromMainThread(
308 gfx::ScrollOffset(arbitrary_vector2d)));
310 // Unrelated functions, always set to new values, always set needs update.
311 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
312 layer->SetMaskLayer(LayerImpl::Create(host_impl.active_tree(), 4)));
313 host_impl.active_tree()->BuildPropertyTreesForTesting();
314 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetMasksToBounds(true));
315 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetContentsOpaque(true));
316 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
317 layer->SetReplicaLayer(LayerImpl::Create(host_impl.active_tree(), 5)));
318 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetPosition(arbitrary_point_f));
319 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetShouldFlattenTransform(false));
320 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->Set3dSortingContextId(1));
322 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
323 layer->SetDoubleSided(false)); // constructor initializes it to "true".
324 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetDrawsContent(true));
325 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
326 layer->SetBackgroundColor(arbitrary_color));
327 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
328 layer->SetBackgroundFilters(arbitrary_filters));
329 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetOpacity(arbitrary_number));
330 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
331 layer->SetBlendMode(arbitrary_blend_mode));
332 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetTransform(arbitrary_transform));
333 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetBounds(arbitrary_size));
335 // Unrelated functions, set to the same values, no needs update.
336 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
337 layer->SetIsRootForIsolatedGroup(true));
338 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetFilters(arbitrary_filters));
339 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetMasksToBounds(true));
340 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetContentsOpaque(true));
341 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetPosition(arbitrary_point_f));
342 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer->Set3dSortingContextId(1));
343 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
344 layer->SetDoubleSided(false)); // constructor initializes it to "true".
345 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetDrawsContent(true));
346 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
347 layer->SetBackgroundColor(arbitrary_color));
348 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
349 layer->SetBackgroundFilters(arbitrary_filters));
350 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetOpacity(arbitrary_number));
351 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
352 layer->SetBlendMode(arbitrary_blend_mode));
353 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
354 layer->SetIsRootForIsolatedGroup(true));
355 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
356 layer->SetTransform(arbitrary_transform));
357 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetBounds(arbitrary_size));
360 TEST(LayerImplTest, SafeOpaqueBackgroundColor) {
361 FakeImplProxy proxy;
362 TestSharedBitmapManager shared_bitmap_manager;
363 TestTaskGraphRunner task_graph_runner;
364 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
365 &task_graph_runner);
366 EXPECT_TRUE(host_impl.InitializeRenderer(FakeOutputSurface::Create3d()));
367 scoped_ptr<LayerImpl> layer = LayerImpl::Create(host_impl.active_tree(), 1);
369 for (int contents_opaque = 0; contents_opaque < 2; ++contents_opaque) {
370 for (int layer_opaque = 0; layer_opaque < 2; ++layer_opaque) {
371 for (int host_opaque = 0; host_opaque < 2; ++host_opaque) {
372 layer->SetContentsOpaque(!!contents_opaque);
373 layer->SetBackgroundColor(layer_opaque ? SK_ColorRED
374 : SK_ColorTRANSPARENT);
375 host_impl.active_tree()->set_background_color(
376 host_opaque ? SK_ColorRED : SK_ColorTRANSPARENT);
378 SkColor safe_color = layer->SafeOpaqueBackgroundColor();
379 if (contents_opaque) {
380 EXPECT_EQ(SkColorGetA(safe_color), 255u)
381 << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
382 << host_opaque << "\n";
383 } else {
384 EXPECT_NE(SkColorGetA(safe_color), 255u)
385 << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
386 << host_opaque << "\n";
393 TEST(LayerImplTest, TransformInvertibility) {
394 FakeImplProxy proxy;
395 TestSharedBitmapManager shared_bitmap_manager;
396 TestTaskGraphRunner task_graph_runner;
397 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
398 &task_graph_runner);
400 scoped_ptr<LayerImpl> layer = LayerImpl::Create(host_impl.active_tree(), 1);
401 EXPECT_TRUE(layer->transform().IsInvertible());
402 EXPECT_TRUE(layer->transform_is_invertible());
404 gfx::Transform transform;
405 transform.Scale3d(
406 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
407 layer->SetTransform(transform);
408 EXPECT_FALSE(layer->transform().IsInvertible());
409 EXPECT_FALSE(layer->transform_is_invertible());
411 transform.MakeIdentity();
412 transform.ApplyPerspectiveDepth(SkDoubleToMScalar(100.0));
413 transform.RotateAboutZAxis(75.0);
414 transform.RotateAboutXAxis(32.2);
415 transform.RotateAboutZAxis(-75.0);
416 transform.Translate3d(SkDoubleToMScalar(50.5),
417 SkDoubleToMScalar(42.42),
418 SkDoubleToMScalar(-100.25));
420 layer->SetTransform(transform);
421 EXPECT_TRUE(layer->transform().IsInvertible());
422 EXPECT_TRUE(layer->transform_is_invertible());
425 class LayerImplScrollTest : public testing::Test {
426 public:
427 LayerImplScrollTest()
428 : host_impl_(settings(),
429 &proxy_,
430 &shared_bitmap_manager_,
431 &task_graph_runner_),
432 root_id_(7) {
433 host_impl_.active_tree()->SetRootLayer(
434 LayerImpl::Create(host_impl_.active_tree(), root_id_));
435 host_impl_.active_tree()->root_layer()->AddChild(
436 LayerImpl::Create(host_impl_.active_tree(), root_id_ + 1));
437 layer()->SetScrollClipLayer(root_id_);
438 // Set the max scroll offset by noting that the root layer has bounds (1,1),
439 // thus whatever bounds are set for the layer will be the max scroll
440 // offset plus 1 in each direction.
441 host_impl_.active_tree()->root_layer()->SetBounds(gfx::Size(1, 1));
442 gfx::Vector2d max_scroll_offset(51, 81);
443 layer()->SetBounds(gfx::Size(max_scroll_offset.x(), max_scroll_offset.y()));
446 LayerImpl* layer() {
447 return host_impl_.active_tree()->root_layer()->children()[0];
450 LayerTreeHostImpl& host_impl() { return host_impl_; }
452 LayerTreeImpl* tree() { return host_impl_.active_tree(); }
454 LayerTreeSettings settings() {
455 LayerTreeSettings settings;
456 return settings;
459 private:
460 FakeImplProxy proxy_;
461 TestSharedBitmapManager shared_bitmap_manager_;
462 TestTaskGraphRunner task_graph_runner_;
463 FakeLayerTreeHostImpl host_impl_;
464 int root_id_;
467 TEST_F(LayerImplScrollTest, ScrollByWithZeroOffset) {
468 // Test that LayerImpl::ScrollBy only affects ScrollDelta and total scroll
469 // offset is bounded by the range [0, max scroll offset].
471 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->CurrentScrollOffset());
472 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->BaseScrollOffset());
473 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
475 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
476 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->CurrentScrollOffset());
478 EXPECT_VECTOR_EQ(layer()->ScrollDelta(), layer()->CurrentScrollOffset());
479 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->BaseScrollOffset());
481 layer()->ScrollBy(gfx::Vector2dF(100, -100));
482 EXPECT_VECTOR_EQ(gfx::Vector2dF(50, 0), layer()->CurrentScrollOffset());
484 EXPECT_VECTOR_EQ(layer()->ScrollDelta(), layer()->CurrentScrollOffset());
485 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->BaseScrollOffset());
488 TEST_F(LayerImplScrollTest, ScrollByWithNonZeroOffset) {
489 gfx::ScrollOffset scroll_offset(10, 5);
490 layer()->PushScrollOffsetFromMainThread(scroll_offset);
492 EXPECT_VECTOR_EQ(scroll_offset, layer()->CurrentScrollOffset());
493 EXPECT_VECTOR_EQ(scroll_offset, layer()->BaseScrollOffset());
494 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
496 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
497 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->CurrentScrollOffset());
499 EXPECT_VECTOR_EQ(
500 gfx::ScrollOffsetWithDelta(scroll_offset, layer()->ScrollDelta()),
501 layer()->CurrentScrollOffset());
502 EXPECT_VECTOR_EQ(scroll_offset, layer()->BaseScrollOffset());
504 layer()->ScrollBy(gfx::Vector2dF(100, -100));
505 EXPECT_VECTOR_EQ(gfx::Vector2dF(50, 0), layer()->CurrentScrollOffset());
507 EXPECT_VECTOR_EQ(
508 gfx::ScrollOffsetWithDelta(scroll_offset, layer()->ScrollDelta()),
509 layer()->CurrentScrollOffset());
510 EXPECT_VECTOR_EQ(scroll_offset, layer()->BaseScrollOffset());
513 TEST_F(LayerImplScrollTest, ApplySentScrollsNoListener) {
514 gfx::ScrollOffset scroll_offset(10, 5);
515 gfx::Vector2dF scroll_delta(20.5f, 8.5f);
516 gfx::Vector2d sent_scroll_delta(12, -3);
518 layer()->PushScrollOffsetFromMainThread(scroll_offset);
519 layer()->ScrollBy(sent_scroll_delta);
520 layer()->PullDeltaForMainThread();
521 layer()->SetCurrentScrollOffset(scroll_offset +
522 gfx::ScrollOffset(scroll_delta));
524 EXPECT_VECTOR_EQ(gfx::ScrollOffsetWithDelta(scroll_offset, scroll_delta),
525 layer()->CurrentScrollOffset());
526 EXPECT_VECTOR_EQ(scroll_delta, layer()->ScrollDelta());
527 EXPECT_VECTOR_EQ(scroll_offset, layer()->BaseScrollOffset());
529 layer()->ApplySentScrollDeltasFromAbortedCommit();
531 EXPECT_VECTOR_EQ(gfx::ScrollOffsetWithDelta(scroll_offset, scroll_delta),
532 layer()->CurrentScrollOffset());
533 EXPECT_VECTOR_EQ(scroll_delta - sent_scroll_delta, layer()->ScrollDelta());
534 EXPECT_VECTOR_EQ(gfx::ScrollOffsetWithDelta(scroll_offset, sent_scroll_delta),
535 layer()->BaseScrollOffset());
538 TEST_F(LayerImplScrollTest, ScrollUserUnscrollableLayer) {
539 gfx::ScrollOffset scroll_offset(10, 5);
540 gfx::Vector2dF scroll_delta(20.5f, 8.5f);
542 layer()->set_user_scrollable_vertical(false);
543 layer()->PushScrollOffsetFromMainThread(scroll_offset);
544 gfx::Vector2dF unscrolled = layer()->ScrollBy(scroll_delta);
546 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 8.5f), unscrolled);
547 EXPECT_VECTOR_EQ(gfx::Vector2dF(30.5f, 5), layer()->CurrentScrollOffset());
550 TEST_F(LayerImplScrollTest, PushPropertiesToMirrorsCurrentScrollOffset) {
551 gfx::ScrollOffset scroll_offset(10, 5);
552 gfx::Vector2dF scroll_delta(12, 18);
554 host_impl().CreatePendingTree();
556 layer()->PushScrollOffsetFromMainThread(scroll_offset);
557 gfx::Vector2dF unscrolled = layer()->ScrollBy(scroll_delta);
559 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 0), unscrolled);
560 EXPECT_VECTOR_EQ(gfx::Vector2dF(22, 23), layer()->CurrentScrollOffset());
562 layer()->PullDeltaForMainThread();
564 scoped_ptr<LayerImpl> pending_layer = LayerImpl::Create(
565 host_impl().sync_tree(), layer()->id(), layer()->synced_scroll_offset());
566 pending_layer->PushScrollOffsetFromMainThread(layer()->CurrentScrollOffset());
568 pending_layer->PushPropertiesTo(layer());
570 EXPECT_VECTOR_EQ(gfx::Vector2dF(22, 23), layer()->CurrentScrollOffset());
571 EXPECT_VECTOR_EQ(layer()->CurrentScrollOffset(),
572 pending_layer->CurrentScrollOffset());
575 TEST_F(LayerImplScrollTest, SetNewScrollbarParameters) {
576 gfx::ScrollOffset scroll_offset(10, 5);
577 layer()->PushScrollOffsetFromMainThread(scroll_offset);
579 scoped_ptr<PaintedScrollbarLayerImpl> vertical_scrollbar(
580 PaintedScrollbarLayerImpl::Create(tree(), 100, VERTICAL));
581 vertical_scrollbar->SetScrollLayerAndClipLayerByIds(
582 layer()->id(), tree()->root_layer()->id());
584 int expected_vertical_maximum =
585 layer()->bounds().height() - tree()->root_layer()->bounds().height();
586 EXPECT_EQ(expected_vertical_maximum, vertical_scrollbar->maximum());
587 EXPECT_EQ(scroll_offset.y(), vertical_scrollbar->current_pos());
589 scoped_ptr<PaintedScrollbarLayerImpl> horizontal_scrollbar(
590 PaintedScrollbarLayerImpl::Create(tree(), 101, HORIZONTAL));
591 horizontal_scrollbar->SetScrollLayerAndClipLayerByIds(
592 layer()->id(), tree()->root_layer()->id());
594 int expected_horizontal_maximum =
595 layer()->bounds().width() - tree()->root_layer()->bounds().width();
596 EXPECT_EQ(expected_horizontal_maximum, horizontal_scrollbar->maximum());
597 EXPECT_EQ(scroll_offset.x(), horizontal_scrollbar->current_pos());
600 class LayerImplScrollbarSyncTest : public testing::Test {
601 public:
602 enum {
603 ROOT = 1,
604 IV_CLIP = 2,
605 PAGE = 3,
606 IV_SCROLL = 4,
607 SCROLLBAR = 5,
608 OLD_ROOT = 6,
609 OV_CLIP = 7,
610 OV_SCROLL = 8,
612 enum TreeID {
613 PENDING,
614 ACTIVE
617 LayerImplScrollbarSyncTest()
618 : host_impl_(settings(),
619 &proxy_,
620 &shared_bitmap_manager_,
621 &task_graph_runner_) {
622 host_impl_.CreatePendingTree();
624 CreateLayers(host_impl_.pending_tree());
625 CreateLayers(host_impl_.active_tree());
628 void CreateLayers(LayerTreeImpl * tree) {
629 tree->SetRootLayer(LayerImpl::Create(tree, ROOT));
630 LayerImpl * root = tree->root_layer();
631 ASSERT_TRUE(root != nullptr);
633 int hierarchy[] = {IV_CLIP, PAGE, IV_SCROLL, OLD_ROOT, OV_CLIP, OV_SCROLL};
634 LayerImpl * parent = root;
635 for (int child_id : hierarchy) {
636 parent->AddChild(LayerImpl::Create(tree, child_id));
637 parent = tree->LayerById(child_id);
638 ASSERT_TRUE(parent != nullptr);
641 root->AddChild(
642 SolidColorScrollbarLayerImpl::Create(tree, SCROLLBAR, HORIZONTAL,
643 5, 5, false, true));
646 LayerImpl* layer(int id, TreeID tree_id) {
647 LayerTreeImpl* tree =
648 ((tree_id == PENDING) ?
649 host_impl_.pending_tree() : host_impl_.active_tree());
651 assert(tree);
652 return tree->LayerById(id);
655 bool LayerHasScrollbar(int id, TreeID tree_id) {
656 return layer(id, tree_id)->HasScrollbar(HORIZONTAL);
659 ScrollbarLayerImplBase* pending_scrollbar() {
660 LayerImpl* layer_impl = layer(SCROLLBAR, PENDING);
661 assert(layer_impl);
662 return layer_impl->ToScrollbarLayer();
665 LayerImpl* pending_root() {
666 LayerImpl * result = layer(ROOT, PENDING);
667 assert(result);
668 return result;
671 LayerImpl* active_root() {
672 LayerImpl * result = layer(ROOT, ACTIVE);
673 assert(result);
674 return result;
677 LayerTreeSettings settings() {
678 LayerTreeSettings settings;
679 return settings;
682 private:
683 FakeImplProxy proxy_;
684 TestSharedBitmapManager shared_bitmap_manager_;
685 TestTaskGraphRunner task_graph_runner_;
686 FakeLayerTreeHostImpl host_impl_;
689 TEST_F(LayerImplScrollbarSyncTest, LayerImplBecomesScrollable) {
690 // In the beginning IV_SCROLL layer is not scrollable.
691 ASSERT_FALSE(layer(IV_SCROLL, PENDING)->scrollable());
693 // For pinch virtual viewport the clip layer is the inner viewport
694 // clip layer (IV_CLIP) and the scroll one is the outer viewport
695 // scroll layer (OV_SCROLL).
696 pending_scrollbar()->SetScrollLayerAndClipLayerByIds(OV_SCROLL, IV_CLIP);
698 ASSERT_TRUE(LayerHasScrollbar(OV_SCROLL, PENDING));
699 ASSERT_TRUE(LayerHasScrollbar(IV_CLIP, PENDING));
701 // Synchronize with the active tree.
702 TreeSynchronizer::PushProperties(pending_root(), active_root());
704 ASSERT_TRUE(LayerHasScrollbar(OV_SCROLL, ACTIVE));
705 ASSERT_TRUE(LayerHasScrollbar(IV_CLIP, ACTIVE));
707 // Make IV_SCROLL layer scrollable.
708 layer(IV_SCROLL, PENDING)->SetScrollClipLayer(IV_CLIP);
709 layer(IV_SCROLL, PENDING)->SetNeedsPushProperties();
710 ASSERT_TRUE(layer(IV_SCROLL, PENDING)->scrollable());
712 pending_scrollbar()->SetScrollLayerAndClipLayerByIds(OV_SCROLL, IV_CLIP);
714 // Now IV_CLIP layer should also receive the scrollbar.
715 ASSERT_TRUE(LayerHasScrollbar(OV_SCROLL, PENDING));
716 ASSERT_TRUE(LayerHasScrollbar(IV_CLIP, PENDING));
717 ASSERT_TRUE(LayerHasScrollbar(IV_SCROLL, PENDING));
719 // Synchronize with the active tree.
720 TreeSynchronizer::PushProperties(pending_root(), active_root());
722 ASSERT_TRUE(layer(IV_SCROLL, ACTIVE)->scrollable());
724 ASSERT_TRUE(LayerHasScrollbar(OV_SCROLL, ACTIVE));
725 ASSERT_TRUE(LayerHasScrollbar(IV_CLIP, ACTIVE));
726 ASSERT_TRUE(LayerHasScrollbar(IV_SCROLL, ACTIVE));
729 } // namespace
730 } // namespace cc