Compute can_use_lcd_text using property trees.
[chromium-blink-merge.git] / cc / layers / layer_impl_unittest.cc
blob964b9994085e0e50ba997923f61fbc738aee13d0
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 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetMasksToBounds(true));
314 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetContentsOpaque(true));
315 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
316 layer->SetReplicaLayer(LayerImpl::Create(host_impl.active_tree(), 5)));
317 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetPosition(arbitrary_point_f));
318 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetShouldFlattenTransform(false));
319 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->Set3dSortingContextId(1));
321 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
322 layer->SetDoubleSided(false)); // constructor initializes it to "true".
323 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetDrawsContent(true));
324 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
325 layer->SetBackgroundColor(arbitrary_color));
326 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
327 layer->SetBackgroundFilters(arbitrary_filters));
328 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetOpacity(arbitrary_number));
329 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(
330 layer->SetBlendMode(arbitrary_blend_mode));
331 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetTransform(arbitrary_transform));
332 VERIFY_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetBounds(arbitrary_size));
334 // Unrelated functions, set to the same values, no needs update.
335 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
336 layer->SetIsRootForIsolatedGroup(true));
337 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetFilters(arbitrary_filters));
338 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetMasksToBounds(true));
339 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetContentsOpaque(true));
340 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetPosition(arbitrary_point_f));
341 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer->Set3dSortingContextId(1));
342 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
343 layer->SetDoubleSided(false)); // constructor initializes it to "true".
344 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetDrawsContent(true));
345 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
346 layer->SetBackgroundColor(arbitrary_color));
347 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
348 layer->SetBackgroundFilters(arbitrary_filters));
349 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetOpacity(arbitrary_number));
350 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
351 layer->SetBlendMode(arbitrary_blend_mode));
352 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
353 layer->SetIsRootForIsolatedGroup(true));
354 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(
355 layer->SetTransform(arbitrary_transform));
356 VERIFY_NO_NEEDS_UPDATE_DRAW_PROPERTIES(layer->SetBounds(arbitrary_size));
359 TEST(LayerImplTest, SafeOpaqueBackgroundColor) {
360 FakeImplProxy proxy;
361 TestSharedBitmapManager shared_bitmap_manager;
362 TestTaskGraphRunner task_graph_runner;
363 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
364 &task_graph_runner);
365 EXPECT_TRUE(host_impl.InitializeRenderer(FakeOutputSurface::Create3d()));
366 scoped_ptr<LayerImpl> layer = LayerImpl::Create(host_impl.active_tree(), 1);
368 for (int contents_opaque = 0; contents_opaque < 2; ++contents_opaque) {
369 for (int layer_opaque = 0; layer_opaque < 2; ++layer_opaque) {
370 for (int host_opaque = 0; host_opaque < 2; ++host_opaque) {
371 layer->SetContentsOpaque(!!contents_opaque);
372 layer->SetBackgroundColor(layer_opaque ? SK_ColorRED
373 : SK_ColorTRANSPARENT);
374 host_impl.active_tree()->set_background_color(
375 host_opaque ? SK_ColorRED : SK_ColorTRANSPARENT);
377 SkColor safe_color = layer->SafeOpaqueBackgroundColor();
378 if (contents_opaque) {
379 EXPECT_EQ(SkColorGetA(safe_color), 255u)
380 << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
381 << host_opaque << "\n";
382 } else {
383 EXPECT_NE(SkColorGetA(safe_color), 255u)
384 << "Flags: " << contents_opaque << ", " << layer_opaque << ", "
385 << host_opaque << "\n";
392 TEST(LayerImplTest, TransformInvertibility) {
393 FakeImplProxy proxy;
394 TestSharedBitmapManager shared_bitmap_manager;
395 TestTaskGraphRunner task_graph_runner;
396 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
397 &task_graph_runner);
399 scoped_ptr<LayerImpl> layer = LayerImpl::Create(host_impl.active_tree(), 1);
400 EXPECT_TRUE(layer->transform().IsInvertible());
401 EXPECT_TRUE(layer->transform_is_invertible());
403 gfx::Transform transform;
404 transform.Scale3d(
405 SkDoubleToMScalar(1.0), SkDoubleToMScalar(1.0), SkDoubleToMScalar(0.0));
406 layer->SetTransform(transform);
407 EXPECT_FALSE(layer->transform().IsInvertible());
408 EXPECT_FALSE(layer->transform_is_invertible());
410 transform.MakeIdentity();
411 transform.ApplyPerspectiveDepth(SkDoubleToMScalar(100.0));
412 transform.RotateAboutZAxis(75.0);
413 transform.RotateAboutXAxis(32.2);
414 transform.RotateAboutZAxis(-75.0);
415 transform.Translate3d(SkDoubleToMScalar(50.5),
416 SkDoubleToMScalar(42.42),
417 SkDoubleToMScalar(-100.25));
419 layer->SetTransform(transform);
420 EXPECT_TRUE(layer->transform().IsInvertible());
421 EXPECT_TRUE(layer->transform_is_invertible());
424 class LayerImplScrollTest : public testing::Test {
425 public:
426 LayerImplScrollTest()
427 : host_impl_(settings(),
428 &proxy_,
429 &shared_bitmap_manager_,
430 &task_graph_runner_),
431 root_id_(7) {
432 host_impl_.active_tree()->SetRootLayer(
433 LayerImpl::Create(host_impl_.active_tree(), root_id_));
434 host_impl_.active_tree()->root_layer()->AddChild(
435 LayerImpl::Create(host_impl_.active_tree(), root_id_ + 1));
436 layer()->SetScrollClipLayer(root_id_);
437 // Set the max scroll offset by noting that the root layer has bounds (1,1),
438 // thus whatever bounds are set for the layer will be the max scroll
439 // offset plus 1 in each direction.
440 host_impl_.active_tree()->root_layer()->SetBounds(gfx::Size(1, 1));
441 gfx::Vector2d max_scroll_offset(51, 81);
442 layer()->SetBounds(gfx::Size(max_scroll_offset.x(), max_scroll_offset.y()));
445 LayerImpl* layer() {
446 return host_impl_.active_tree()->root_layer()->children()[0];
449 LayerTreeHostImpl& host_impl() { return host_impl_; }
451 LayerTreeImpl* tree() { return host_impl_.active_tree(); }
453 LayerTreeSettings settings() {
454 LayerTreeSettings settings;
455 return settings;
458 private:
459 FakeImplProxy proxy_;
460 TestSharedBitmapManager shared_bitmap_manager_;
461 TestTaskGraphRunner task_graph_runner_;
462 FakeLayerTreeHostImpl host_impl_;
463 int root_id_;
466 TEST_F(LayerImplScrollTest, ScrollByWithZeroOffset) {
467 // Test that LayerImpl::ScrollBy only affects ScrollDelta and total scroll
468 // offset is bounded by the range [0, max scroll offset].
470 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->CurrentScrollOffset());
471 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->BaseScrollOffset());
472 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
474 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
475 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->CurrentScrollOffset());
477 EXPECT_VECTOR_EQ(layer()->ScrollDelta(), layer()->CurrentScrollOffset());
478 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->BaseScrollOffset());
480 layer()->ScrollBy(gfx::Vector2dF(100, -100));
481 EXPECT_VECTOR_EQ(gfx::Vector2dF(50, 0), layer()->CurrentScrollOffset());
483 EXPECT_VECTOR_EQ(layer()->ScrollDelta(), layer()->CurrentScrollOffset());
484 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->BaseScrollOffset());
487 TEST_F(LayerImplScrollTest, ScrollByWithNonZeroOffset) {
488 gfx::ScrollOffset scroll_offset(10, 5);
489 layer()->PushScrollOffsetFromMainThread(scroll_offset);
491 EXPECT_VECTOR_EQ(scroll_offset, layer()->CurrentScrollOffset());
492 EXPECT_VECTOR_EQ(scroll_offset, layer()->BaseScrollOffset());
493 EXPECT_VECTOR_EQ(gfx::Vector2dF(), layer()->ScrollDelta());
495 layer()->ScrollBy(gfx::Vector2dF(-100, 100));
496 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 80), layer()->CurrentScrollOffset());
498 EXPECT_VECTOR_EQ(
499 gfx::ScrollOffsetWithDelta(scroll_offset, layer()->ScrollDelta()),
500 layer()->CurrentScrollOffset());
501 EXPECT_VECTOR_EQ(scroll_offset, layer()->BaseScrollOffset());
503 layer()->ScrollBy(gfx::Vector2dF(100, -100));
504 EXPECT_VECTOR_EQ(gfx::Vector2dF(50, 0), layer()->CurrentScrollOffset());
506 EXPECT_VECTOR_EQ(
507 gfx::ScrollOffsetWithDelta(scroll_offset, layer()->ScrollDelta()),
508 layer()->CurrentScrollOffset());
509 EXPECT_VECTOR_EQ(scroll_offset, layer()->BaseScrollOffset());
512 TEST_F(LayerImplScrollTest, ApplySentScrollsNoListener) {
513 gfx::ScrollOffset scroll_offset(10, 5);
514 gfx::Vector2dF scroll_delta(20.5f, 8.5f);
515 gfx::Vector2d sent_scroll_delta(12, -3);
517 layer()->PushScrollOffsetFromMainThread(scroll_offset);
518 layer()->ScrollBy(sent_scroll_delta);
519 layer()->PullDeltaForMainThread();
520 layer()->SetCurrentScrollOffset(scroll_offset +
521 gfx::ScrollOffset(scroll_delta));
523 EXPECT_VECTOR_EQ(gfx::ScrollOffsetWithDelta(scroll_offset, scroll_delta),
524 layer()->CurrentScrollOffset());
525 EXPECT_VECTOR_EQ(scroll_delta, layer()->ScrollDelta());
526 EXPECT_VECTOR_EQ(scroll_offset, layer()->BaseScrollOffset());
528 layer()->ApplySentScrollDeltasFromAbortedCommit();
530 EXPECT_VECTOR_EQ(gfx::ScrollOffsetWithDelta(scroll_offset, scroll_delta),
531 layer()->CurrentScrollOffset());
532 EXPECT_VECTOR_EQ(scroll_delta - sent_scroll_delta, layer()->ScrollDelta());
533 EXPECT_VECTOR_EQ(gfx::ScrollOffsetWithDelta(scroll_offset, sent_scroll_delta),
534 layer()->BaseScrollOffset());
537 TEST_F(LayerImplScrollTest, ScrollUserUnscrollableLayer) {
538 gfx::ScrollOffset scroll_offset(10, 5);
539 gfx::Vector2dF scroll_delta(20.5f, 8.5f);
541 layer()->set_user_scrollable_vertical(false);
542 layer()->PushScrollOffsetFromMainThread(scroll_offset);
543 gfx::Vector2dF unscrolled = layer()->ScrollBy(scroll_delta);
545 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 8.5f), unscrolled);
546 EXPECT_VECTOR_EQ(gfx::Vector2dF(30.5f, 5), layer()->CurrentScrollOffset());
549 TEST_F(LayerImplScrollTest, PushPropertiesToMirrorsCurrentScrollOffset) {
550 gfx::ScrollOffset scroll_offset(10, 5);
551 gfx::Vector2dF scroll_delta(12, 18);
553 host_impl().CreatePendingTree();
555 layer()->PushScrollOffsetFromMainThread(scroll_offset);
556 gfx::Vector2dF unscrolled = layer()->ScrollBy(scroll_delta);
558 EXPECT_VECTOR_EQ(gfx::Vector2dF(0, 0), unscrolled);
559 EXPECT_VECTOR_EQ(gfx::Vector2dF(22, 23), layer()->CurrentScrollOffset());
561 layer()->PullDeltaForMainThread();
563 scoped_ptr<LayerImpl> pending_layer = LayerImpl::Create(
564 host_impl().sync_tree(), layer()->id(), layer()->synced_scroll_offset());
565 pending_layer->PushScrollOffsetFromMainThread(layer()->CurrentScrollOffset());
567 pending_layer->PushPropertiesTo(layer());
569 EXPECT_VECTOR_EQ(gfx::Vector2dF(22, 23), layer()->CurrentScrollOffset());
570 EXPECT_VECTOR_EQ(layer()->CurrentScrollOffset(),
571 pending_layer->CurrentScrollOffset());
574 TEST_F(LayerImplScrollTest, SetNewScrollbarParameters) {
575 gfx::ScrollOffset scroll_offset(10, 5);
576 layer()->PushScrollOffsetFromMainThread(scroll_offset);
578 scoped_ptr<PaintedScrollbarLayerImpl> vertical_scrollbar(
579 PaintedScrollbarLayerImpl::Create(tree(), 100, VERTICAL));
580 vertical_scrollbar->SetScrollLayerAndClipLayerByIds(
581 layer()->id(), tree()->root_layer()->id());
583 int expected_vertical_maximum =
584 layer()->bounds().height() - tree()->root_layer()->bounds().height();
585 EXPECT_EQ(expected_vertical_maximum, vertical_scrollbar->maximum());
586 EXPECT_EQ(scroll_offset.y(), vertical_scrollbar->current_pos());
588 scoped_ptr<PaintedScrollbarLayerImpl> horizontal_scrollbar(
589 PaintedScrollbarLayerImpl::Create(tree(), 101, HORIZONTAL));
590 horizontal_scrollbar->SetScrollLayerAndClipLayerByIds(
591 layer()->id(), tree()->root_layer()->id());
593 int expected_horizontal_maximum =
594 layer()->bounds().width() - tree()->root_layer()->bounds().width();
595 EXPECT_EQ(expected_horizontal_maximum, horizontal_scrollbar->maximum());
596 EXPECT_EQ(scroll_offset.x(), horizontal_scrollbar->current_pos());
599 class LayerImplScrollbarSyncTest : public testing::Test {
600 public:
601 enum {
602 ROOT = 1,
603 IV_CLIP = 2,
604 PAGE = 3,
605 IV_SCROLL = 4,
606 SCROLLBAR = 5,
607 OLD_ROOT = 6,
608 OV_CLIP = 7,
609 OV_SCROLL = 8,
611 enum TreeID {
612 PENDING,
613 ACTIVE
616 LayerImplScrollbarSyncTest()
617 : host_impl_(settings(),
618 &proxy_,
619 &shared_bitmap_manager_,
620 &task_graph_runner_) {
621 host_impl_.CreatePendingTree();
623 CreateLayers(host_impl_.pending_tree());
624 CreateLayers(host_impl_.active_tree());
627 void CreateLayers(LayerTreeImpl * tree) {
628 tree->SetRootLayer(LayerImpl::Create(tree, ROOT));
629 LayerImpl * root = tree->root_layer();
630 ASSERT_TRUE(root != nullptr);
632 int hierarchy[] = {IV_CLIP, PAGE, IV_SCROLL, OLD_ROOT, OV_CLIP, OV_SCROLL};
633 LayerImpl * parent = root;
634 for (int child_id : hierarchy) {
635 parent->AddChild(LayerImpl::Create(tree, child_id));
636 parent = tree->LayerById(child_id);
637 ASSERT_TRUE(parent != nullptr);
640 root->AddChild(
641 SolidColorScrollbarLayerImpl::Create(tree, SCROLLBAR, HORIZONTAL,
642 5, 5, false, true));
645 LayerImpl* layer(int id, TreeID tree_id) {
646 LayerTreeImpl* tree =
647 ((tree_id == PENDING) ?
648 host_impl_.pending_tree() : host_impl_.active_tree());
650 assert(tree);
651 return tree->LayerById(id);
654 bool LayerHasScrollbar(int id, TreeID tree_id) {
655 return layer(id, tree_id)->HasScrollbar(HORIZONTAL);
658 ScrollbarLayerImplBase* pending_scrollbar() {
659 LayerImpl* layer_impl = layer(SCROLLBAR, PENDING);
660 assert(layer_impl);
661 return layer_impl->ToScrollbarLayer();
664 LayerImpl* pending_root() {
665 LayerImpl * result = layer(ROOT, PENDING);
666 assert(result);
667 return result;
670 LayerImpl* active_root() {
671 LayerImpl * result = layer(ROOT, ACTIVE);
672 assert(result);
673 return result;
676 LayerTreeSettings settings() {
677 LayerTreeSettings settings;
678 return settings;
681 private:
682 FakeImplProxy proxy_;
683 TestSharedBitmapManager shared_bitmap_manager_;
684 TestTaskGraphRunner task_graph_runner_;
685 FakeLayerTreeHostImpl host_impl_;
688 TEST_F(LayerImplScrollbarSyncTest, LayerImplBecomesScrollable) {
689 // In the beginning IV_SCROLL layer is not scrollable.
690 ASSERT_FALSE(layer(IV_SCROLL, PENDING)->scrollable());
692 // For pinch virtual viewport the clip layer is the inner viewport
693 // clip layer (IV_CLIP) and the scroll one is the outer viewport
694 // scroll layer (OV_SCROLL).
695 pending_scrollbar()->SetScrollLayerAndClipLayerByIds(OV_SCROLL, IV_CLIP);
697 ASSERT_TRUE(LayerHasScrollbar(OV_SCROLL, PENDING));
698 ASSERT_TRUE(LayerHasScrollbar(IV_CLIP, PENDING));
700 // Synchronize with the active tree.
701 TreeSynchronizer::PushProperties(pending_root(), active_root());
703 ASSERT_TRUE(LayerHasScrollbar(OV_SCROLL, ACTIVE));
704 ASSERT_TRUE(LayerHasScrollbar(IV_CLIP, ACTIVE));
706 // Make IV_SCROLL layer scrollable.
707 layer(IV_SCROLL, PENDING)->SetScrollClipLayer(IV_CLIP);
708 layer(IV_SCROLL, PENDING)->SetNeedsPushProperties();
709 ASSERT_TRUE(layer(IV_SCROLL, PENDING)->scrollable());
711 pending_scrollbar()->SetScrollLayerAndClipLayerByIds(OV_SCROLL, IV_CLIP);
713 // Now IV_CLIP layer should also receive the scrollbar.
714 ASSERT_TRUE(LayerHasScrollbar(OV_SCROLL, PENDING));
715 ASSERT_TRUE(LayerHasScrollbar(IV_CLIP, PENDING));
716 ASSERT_TRUE(LayerHasScrollbar(IV_SCROLL, PENDING));
718 // Synchronize with the active tree.
719 TreeSynchronizer::PushProperties(pending_root(), active_root());
721 ASSERT_TRUE(layer(IV_SCROLL, ACTIVE)->scrollable());
723 ASSERT_TRUE(LayerHasScrollbar(OV_SCROLL, ACTIVE));
724 ASSERT_TRUE(LayerHasScrollbar(IV_CLIP, ACTIVE));
725 ASSERT_TRUE(LayerHasScrollbar(IV_SCROLL, ACTIVE));
728 } // namespace
729 } // namespace cc