OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "platform/graphics/CompositorMutableState.h" | 5 #include "platform/graphics/CompositorMutableState.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "cc/test/fake_compositor_frame_sink.h" | |
9 #include "cc/test/fake_impl_task_runner_provider.h" | |
10 #include "cc/test/fake_layer_tree_host_impl.h" | |
11 #include "cc/test/test_shared_bitmap_manager.h" | |
12 #include "cc/test/test_task_graph_runner.h" | |
13 #include "cc/trees/layer_tree_host_impl.h" | |
14 #include "cc/trees/layer_tree_impl.h" | |
15 #include "platform/graphics/CompositorElementId.h" | |
16 #include "platform/graphics/CompositorMutableProperties.h" | 8 #include "platform/graphics/CompositorMutableProperties.h" |
17 #include "platform/graphics/CompositorMutableStateProvider.h" | 9 #include "platform/graphics/CompositorMutableStateProvider.h" |
18 #include "platform/graphics/CompositorMutation.h" | 10 #include "platform/graphics/CompositorMutation.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
20 #include <memory> | 12 #include <memory> |
21 | 13 |
22 namespace blink { | 14 namespace blink { |
23 | 15 |
24 using cc::FakeImplTaskRunnerProvider; | 16 TEST(CompositorMutableStateTest, NoMutableState) { |
25 using cc::FakeLayerTreeHostImpl; | 17 // In this test, there are no proxies with either an element id or mutable |
26 using cc::FakeCompositorFrameSink; | |
27 using cc::LayerImpl; | |
28 using cc::LayerTreeSettings; | |
29 using cc::TestTaskGraphRunner; | |
30 using cc::TestSharedBitmapManager; | |
31 | |
32 class CompositorMutableStateTest : public testing::Test { | |
33 public: | |
34 CompositorMutableStateTest() | |
35 : m_compositorFrameSink(FakeCompositorFrameSink::Create3d()) { | |
36 LayerTreeSettings settings; | |
37 settings.layer_transforms_should_scale_layer_contents = true; | |
38 m_hostImpl.reset(new FakeLayerTreeHostImpl(settings, &m_taskRunnerProvider, | |
39 &m_taskGraphRunner)); | |
40 m_hostImpl->SetVisible(true); | |
41 EXPECT_TRUE(m_hostImpl->InitializeRenderer(m_compositorFrameSink.get())); | |
42 } | |
43 | |
44 void SetLayerPropertiesForTesting(LayerImpl* layer) { | |
45 layer->test_properties()->transform = gfx::Transform(); | |
46 layer->SetPosition(gfx::PointF()); | |
47 layer->SetBounds(gfx::Size(100, 100)); | |
48 layer->SetDrawsContent(true); | |
49 } | |
50 | |
51 FakeLayerTreeHostImpl& hostImpl() { return *m_hostImpl; } | |
52 | |
53 LayerImpl* rootLayer() { | |
54 return m_hostImpl->active_tree()->root_layer_for_testing(); | |
55 } | |
56 | |
57 private: | |
58 // The cc testing machinery has fairly deep dependency on having a main | |
59 // message loop (one example is the task runner provider). We construct one | |
60 // here so that it's installed in TLA and can be found by other cc classes. | |
61 base::MessageLoop m_messageLoop; | |
62 TestTaskGraphRunner m_taskGraphRunner; | |
63 FakeImplTaskRunnerProvider m_taskRunnerProvider; | |
64 std::unique_ptr<FakeCompositorFrameSink> m_compositorFrameSink; | |
65 std::unique_ptr<FakeLayerTreeHostImpl> m_hostImpl; | |
66 }; | |
67 | |
68 TEST_F(CompositorMutableStateTest, NoMutableState) { | |
69 // In this test, there are no layers with either an element id or mutable | |
70 // properties. We should not be able to get any mutable state. | 18 // properties. We should not be able to get any mutable state. |
71 std::unique_ptr<LayerImpl> root = | 19 ProxyCompositorMutablePropertiesMap emptyMap; |
72 LayerImpl::Create(hostImpl().active_tree(), 42); | |
73 SetLayerPropertiesForTesting(root.get()); | |
74 | |
75 hostImpl().SetViewportSize(root->bounds()); | |
76 hostImpl().active_tree()->SetRootLayerForTesting(std::move(root)); | |
77 hostImpl().UpdateNumChildrenAndDrawPropertiesForActiveTree(); | |
78 | |
79 CompositorMutations mutations; | 20 CompositorMutations mutations; |
80 CompositorMutableStateProvider provider(hostImpl().active_tree(), &mutations); | 21 CompositorMutableStateProvider provider(&emptyMap, &mutations); |
81 std::unique_ptr<CompositorMutableState> state( | 22 std::unique_ptr<CompositorMutableState> state( |
82 provider.getMutableStateFor(42)); | 23 provider.getMutableStateFor(42)); |
83 EXPECT_FALSE(state); | 24 EXPECT_FALSE(state); |
84 } | 25 } |
85 | 26 |
86 TEST_F(CompositorMutableStateTest, MutableStateMutableProperties) { | 27 TEST(CompositorMutableStateTest, MutableStateMutableProperties) { |
87 // In this test, there is a layer with an element id and mutable properties. | 28 // In this test, there is a proxy with an element id and mutable properties. |
88 // In this case, we should get a valid mutable state for this element id that | 29 // In this case, we should get a valid mutable state for this element id and |
89 // has a real effect on the corresponding layer. | 30 // mutations should be updated accordingly. |
90 std::unique_ptr<LayerImpl> root = | 31 int proxyId = 12; |
91 LayerImpl::Create(hostImpl().active_tree(), 42); | 32 int elementId = 15; |
92 | 33 |
93 std::unique_ptr<LayerImpl> scopedLayer = | 34 ProxyCompositorMutablePropertiesMap inputProperties; |
94 LayerImpl::Create(hostImpl().active_tree(), 11); | 35 inputProperties[proxyId].elementId = elementId; |
95 LayerImpl* layer = scopedLayer.get(); | 36 inputProperties[proxyId].opacity = 1.0; |
96 layer->SetScrollClipLayer(root->id()); | 37 inputProperties[proxyId].transform = SkMatrix44::I(); |
97 | 38 inputProperties[proxyId].scrollLeft = 0; |
98 root->test_properties()->AddChild(std::move(scopedLayer)); | 39 inputProperties[proxyId].scrollTop = 0; |
99 | |
100 SetLayerPropertiesForTesting(layer); | |
101 | |
102 int primaryId = 12; | |
103 root->SetElementId( | |
104 createCompositorElementId(primaryId, CompositorSubElementId::Primary)); | |
105 layer->SetElementId( | |
106 createCompositorElementId(primaryId, CompositorSubElementId::Scroll)); | |
107 | |
108 root->SetMutableProperties(CompositorMutableProperty::kOpacity | | |
109 CompositorMutableProperty::kTransform); | |
110 layer->SetMutableProperties(CompositorMutableProperty::kScrollLeft | | |
111 CompositorMutableProperty::kScrollTop); | |
112 | |
113 hostImpl().SetViewportSize(layer->bounds()); | |
114 hostImpl().active_tree()->SetRootLayerForTesting(std::move(root)); | |
115 hostImpl().UpdateNumChildrenAndDrawPropertiesForActiveTree(); | |
116 | 40 |
117 CompositorMutations mutations; | 41 CompositorMutations mutations; |
118 CompositorMutableStateProvider provider(hostImpl().active_tree(), &mutations); | 42 CompositorMutableStateProvider provider(&inputProperties, &mutations); |
119 | 43 |
120 std::unique_ptr<CompositorMutableState> state( | 44 std::unique_ptr<CompositorMutableState> state( |
121 provider.getMutableStateFor(primaryId)); | 45 provider.getMutableStateFor(proxyId)); |
122 EXPECT_TRUE(state.get()); | 46 EXPECT_TRUE(state.get()); |
123 | 47 |
124 EXPECT_EQ(1.0, rootLayer()->Opacity()); | 48 EXPECT_EQ(1.0, state->opacity()); |
125 EXPECT_EQ(gfx::Transform().ToString(), rootLayer()->Transform().ToString()); | 49 EXPECT_EQ(SkMatrix44::I(), state->transform()); |
126 EXPECT_EQ(0.0, layer->CurrentScrollOffset().x()); | 50 EXPECT_EQ(0.0, state->scrollTop()); |
127 EXPECT_EQ(0.0, layer->CurrentScrollOffset().y()); | 51 EXPECT_EQ(0.0, state->scrollLeft()); |
128 | 52 |
129 gfx::Transform zero(0, 0, 0, 0, 0, 0); | 53 SkMatrix44 zero; |
130 state->setOpacity(0.5); | 54 state->setOpacity(0.5); |
131 state->setTransform(zero.matrix()); | 55 state->setTransform(zero); |
132 state->setScrollLeft(1.0); | 56 state->setScrollLeft(0.75); |
133 state->setScrollTop(1.0); | 57 state->setScrollTop(0.25); |
134 | 58 |
135 EXPECT_EQ(0.5, rootLayer()->Opacity()); | 59 EXPECT_EQ(0.5, state->opacity()); |
136 EXPECT_EQ(zero.ToString(), rootLayer()->Transform().ToString()); | 60 EXPECT_EQ(zero, state->transform()); |
137 EXPECT_EQ(1.0, layer->CurrentScrollOffset().x()); | 61 EXPECT_EQ(0.25, state->scrollTop()); |
138 EXPECT_EQ(1.0, layer->CurrentScrollOffset().y()); | 62 EXPECT_EQ(0.75, state->scrollLeft()); |
139 | 63 |
140 // The corresponding mutation should reflect the changed values. | 64 // The corresponding mutation should reflect the changed values. |
141 EXPECT_EQ(1ul, mutations.map.size()); | 65 EXPECT_EQ(1ul, mutations.map.size()); |
142 | 66 |
143 const CompositorMutation& mutation = *mutations.map.find(primaryId)->value; | 67 const CompositorMutation& mutation = *mutations.map.find(elementId)->value; |
144 EXPECT_TRUE(mutation.isOpacityMutated()); | 68 EXPECT_TRUE(mutation.isOpacityMutated()); |
145 EXPECT_TRUE(mutation.isTransformMutated()); | 69 EXPECT_TRUE(mutation.isTransformMutated()); |
146 EXPECT_TRUE(mutation.isScrollLeftMutated()); | 70 EXPECT_TRUE(mutation.isScrollLeftMutated()); |
147 EXPECT_TRUE(mutation.isScrollTopMutated()); | 71 EXPECT_TRUE(mutation.isScrollTopMutated()); |
148 | 72 |
149 EXPECT_EQ(0.5, mutation.opacity()); | 73 EXPECT_EQ(0.5, mutation.opacity()); |
150 EXPECT_EQ(zero.ToString(), gfx::Transform(mutation.transform()).ToString()); | 74 EXPECT_EQ(zero, mutation.transform()); |
151 EXPECT_EQ(1.0, mutation.scrollLeft()); | 75 EXPECT_EQ(0.75, mutation.scrollLeft()); |
152 EXPECT_EQ(1.0, mutation.scrollTop()); | 76 EXPECT_EQ(0.25, mutation.scrollTop()); |
153 } | 77 } |
154 | 78 |
155 } // namespace blink | 79 } // namespace blink |
OLD | NEW |