| 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 : compositor_frame_sink_(FakeCompositorFrameSink::Create3d()) { | |
| 36 LayerTreeSettings settings; | |
| 37 settings.layer_transforms_should_scale_layer_contents = true; | |
| 38 host_impl_.reset(new FakeLayerTreeHostImpl(settings, &task_runner_provider_, | |
| 39 &task_graph_runner_)); | |
| 40 host_impl_->SetVisible(true); | |
| 41 EXPECT_TRUE(host_impl_->InitializeRenderer(compositor_frame_sink_.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 *host_impl_; } | |
| 52 | |
| 53 LayerImpl* RootLayer() { | |
| 54 return host_impl_->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 message_loop_; | |
| 62 TestTaskGraphRunner task_graph_runner_; | |
| 63 FakeImplTaskRunnerProvider task_runner_provider_; | |
| 64 std::unique_ptr<FakeCompositorFrameSink> compositor_frame_sink_; | |
| 65 std::unique_ptr<FakeLayerTreeHostImpl> host_impl_; | |
| 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 empty_map; |
| 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(&empty_map, &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 proxy_id = 12; |
| 91 LayerImpl::Create(HostImpl().active_tree(), 42); | 32 int element_id = 15; |
| 92 | 33 |
| 93 std::unique_ptr<LayerImpl> scoped_layer = | 34 ProxyCompositorMutablePropertiesMap input_properties; |
| 94 LayerImpl::Create(HostImpl().active_tree(), 11); | 35 input_properties[proxy_id].element_id = element_id; |
| 95 LayerImpl* layer = scoped_layer.get(); | 36 input_properties[proxy_id].opacity = 1.0; |
| 96 layer->SetScrollClipLayer(root->id()); | 37 input_properties[proxy_id].transform = SkMatrix44::I(); |
| 97 | 38 input_properties[proxy_id].scroll_left = 0; |
| 98 root->test_properties()->AddChild(std::move(scoped_layer)); | 39 input_properties[proxy_id].scroll_top = 0; |
| 99 | |
| 100 SetLayerPropertiesForTesting(layer); | |
| 101 | |
| 102 int primary_id = 12; | |
| 103 root->SetElementId( | |
| 104 CreateCompositorElementId(primary_id, CompositorSubElementId::kPrimary)); | |
| 105 layer->SetElementId( | |
| 106 CreateCompositorElementId(primary_id, CompositorSubElementId::kScroll)); | |
| 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(&input_properties, &mutations); |
| 119 | 43 |
| 120 std::unique_ptr<CompositorMutableState> state( | 44 std::unique_ptr<CompositorMutableState> state( |
| 121 provider.GetMutableStateFor(primary_id)); | 45 provider.GetMutableStateFor(proxy_id)); |
| 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(primary_id)->value; | 67 const CompositorMutation& mutation = *mutations.map.Find(element_id)->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 |