1 // Copyright 2012 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. 4 5 #ifndef CC_TEST_LAYER_TREE_IMPL_TEST_BASE_H_ 6 #define CC_TEST_LAYER_TREE_IMPL_TEST_BASE_H_ 7 8 #include <stddef.h> 9 10 #include <memory> 11 #include <utility> 12 13 #include "cc/animation/animation_timeline.h" 14 #include "cc/test/fake_layer_tree_host.h" 15 #include "cc/test/fake_layer_tree_host_client.h" 16 #include "cc/test/layer_test_common.h" 17 #include "cc/test/property_tree_test_utils.h" 18 #include "cc/test/test_task_graph_runner.h" 19 #include "cc/trees/effect_node.h" 20 #include "cc/trees/layer_tree_host_impl.h" 21 #include "cc/trees/layer_tree_settings.h" 22 #include "components/viz/common/quads/compositor_render_pass.h" 23 24 namespace cc { 25 26 class LayerImpl; 27 class LayerTreeFrameSink; 28 class RenderSurfaceImpl; 29 30 class LayerTreeImplTestBase { 31 public: 32 LayerTreeImplTestBase(); 33 explicit LayerTreeImplTestBase( 34 std::unique_ptr<LayerTreeFrameSink> layer_tree_frame_sink); 35 explicit LayerTreeImplTestBase(const LayerTreeSettings& settings); 36 LayerTreeImplTestBase( 37 const LayerTreeSettings& settings, 38 std::unique_ptr<LayerTreeFrameSink> layer_tree_frame_sink); 39 ~LayerTreeImplTestBase(); 40 41 template <typename T, typename... Args> AddLayer(Args &&...args)42 T* AddLayer(Args&&... args) { 43 return AddLayerInternal<T>(host_impl()->active_tree(), 44 std::forward<Args>(args)...); 45 } 46 47 LayerImpl* EnsureRootLayerInPendingTree(); 48 49 template <typename T, typename... Args> AddLayerInPendingTree(Args &&...args)50 T* AddLayerInPendingTree(Args&&... args) { 51 return AddLayerInternal<T>(host_impl()->pending_tree(), 52 std::forward<Args>(args)...); 53 } 54 55 void CalcDrawProps(const gfx::Size& viewport_size); 56 void AppendQuadsWithOcclusion(LayerImpl* layer_impl, 57 const gfx::Rect& occluded); 58 void AppendQuadsForPassWithOcclusion( 59 LayerImpl* layer_impl, 60 viz::CompositorRenderPass* given_render_pass, 61 const gfx::Rect& occluded); 62 void AppendSurfaceQuadsWithOcclusion(RenderSurfaceImpl* surface_impl, 63 const gfx::Rect& occluded); 64 layer_tree_frame_sink()65 LayerTreeFrameSink* layer_tree_frame_sink() const { 66 return host_impl()->layer_tree_frame_sink(); 67 } resource_provider()68 viz::ClientResourceProvider* resource_provider() const { 69 return host_impl()->resource_provider(); 70 } root_layer()71 LayerImpl* root_layer() const { 72 return host_impl()->active_tree()->root_layer(); 73 } host()74 FakeLayerTreeHost* host() { return host_.get(); } host_impl()75 FakeLayerTreeHostImpl* host_impl() const { return host_->host_impl(); } task_runner_provider()76 TaskRunnerProvider* task_runner_provider() const { 77 return host_impl()->task_runner_provider(); 78 } quad_list()79 const viz::QuadList& quad_list() const { return render_pass_->quad_list; } timeline()80 scoped_refptr<AnimationTimeline> timeline() { return timeline_; } timeline_impl()81 scoped_refptr<AnimationTimeline> timeline_impl() { return timeline_impl_; } 82 SetElementIdsForTesting()83 void SetElementIdsForTesting() { 84 host_impl()->active_tree()->SetElementIdsForTesting(); 85 } 86 InnerViewportScrollLayer()87 LayerImpl* InnerViewportScrollLayer() { 88 return host_impl()->active_tree()->InnerViewportScrollLayerForTesting(); 89 } OuterViewportScrollLayer()90 LayerImpl* OuterViewportScrollLayer() { 91 return host_impl()->active_tree()->OuterViewportScrollLayerForTesting(); 92 } 93 94 // These functions sets device scale factor and update device viewport rect 95 // before calling the global UpdateDrawProperties() with 96 // update_layer_impl_list_. 97 void UpdateActiveTreeDrawProperties(float device_scale_factor = 1.0f); 98 void UpdatePendingTreeDrawProperties(float device_scale_factor = 1.0f); 99 UpdateLayerImplListContains(int id)100 bool UpdateLayerImplListContains(int id) const { 101 for (const auto* layer : update_layer_impl_list_) { 102 if (layer->id() == id) 103 return true; 104 } 105 return false; 106 } 107 update_layer_impl_list()108 const LayerImplList& update_layer_impl_list() const { 109 return update_layer_impl_list_; 110 } 111 112 private: 113 template <typename T, typename... Args> AddLayerInternal(LayerTreeImpl * tree,Args &&...args)114 T* AddLayerInternal(LayerTreeImpl* tree, Args&&... args) { 115 std::unique_ptr<T> layer = 116 T::Create(tree, layer_impl_id_++, std::forward<Args>(args)...); 117 T* ptr = layer.get(); 118 tree->AddLayer(std::move(layer)); 119 return ptr; 120 } 121 122 FakeLayerTreeHostClient client_; 123 TestTaskGraphRunner task_graph_runner_; 124 std::unique_ptr<LayerTreeFrameSink> layer_tree_frame_sink_; 125 std::unique_ptr<AnimationHost> animation_host_; 126 std::unique_ptr<FakeLayerTreeHost> host_; 127 std::unique_ptr<viz::CompositorRenderPass> render_pass_; 128 scoped_refptr<AnimationTimeline> timeline_; 129 scoped_refptr<AnimationTimeline> timeline_impl_; 130 int layer_impl_id_; 131 LayerImplList update_layer_impl_list_; 132 }; 133 134 } // namespace cc 135 136 #endif // CC_TEST_LAYER_TREE_IMPL_TEST_BASE_H_ 137