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. 4 5 #include "cc/base/scoped_ptr_vector.h" 6 #include "cc/layers/append_quads_data.h" 7 #include "cc/layers/layer_impl.h" 8 #include "cc/layers/render_pass_sink.h" 9 #include "cc/layers/render_surface_impl.h" 10 #include "cc/quads/shared_quad_state.h" 11 #include "cc/test/fake_impl_proxy.h" 12 #include "cc/test/fake_layer_tree_host_impl.h" 13 #include "cc/test/geometry_test_utils.h" 14 #include "cc/test/mock_quad_culler.h" 15 #include "cc/trees/single_thread_proxy.h" 16 #include "testing/gmock/include/gmock/gmock.h" 17 #include "testing/gtest/include/gtest/gtest.h" 18 #include "ui/gfx/transform.h" 19 20 namespace cc { 21 namespace { 22 23 #define EXECUTE_AND_VERIFY_SURFACE_CHANGED(code_to_test) \ 24 render_surface->ResetPropertyChangedFlag(); \ 25 code_to_test; \ 26 EXPECT_TRUE(render_surface->SurfacePropertyChanged()) 27 28 #define EXECUTE_AND_VERIFY_SURFACE_DID_NOT_CHANGE(code_to_test) \ 29 render_surface->ResetPropertyChangedFlag(); \ 30 code_to_test; \ 31 EXPECT_FALSE(render_surface->SurfacePropertyChanged()) 32 33 TEST(RenderSurfaceTest, VerifySurfaceChangesAreTrackedProperly) { 34 // 35 // This test checks that SurfacePropertyChanged() has the correct behavior. 36 // 37 38 FakeImplProxy proxy; 39 FakeLayerTreeHostImpl host_impl(&proxy); 40 scoped_ptr<LayerImpl> owning_layer = 41 LayerImpl::Create(host_impl.active_tree(), 1); 42 owning_layer->CreateRenderSurface(); 43 ASSERT_TRUE(owning_layer->render_surface()); 44 RenderSurfaceImpl* render_surface = owning_layer->render_surface(); 45 gfx::Rect test_rect(3, 4, 5, 6); 46 owning_layer->ResetAllChangeTrackingForSubtree(); 47 48 // Currently, the content_rect, clip_rect, and 49 // owning_layer->layerPropertyChanged() are the only sources of change. 50 EXECUTE_AND_VERIFY_SURFACE_CHANGED(render_surface->SetClipRect(test_rect)); 51 EXECUTE_AND_VERIFY_SURFACE_CHANGED(render_surface->SetContentRect(test_rect)); 52 53 owning_layer->SetOpacity(0.5f); 54 EXPECT_TRUE(render_surface->SurfacePropertyChanged()); 55 owning_layer->ResetAllChangeTrackingForSubtree(); 56 57 // Setting the surface properties to the same values again should not be 58 // considered "change". 59 EXECUTE_AND_VERIFY_SURFACE_DID_NOT_CHANGE( 60 render_surface->SetClipRect(test_rect)); 61 EXECUTE_AND_VERIFY_SURFACE_DID_NOT_CHANGE( 62 render_surface->SetContentRect(test_rect)); 63 64 scoped_ptr<LayerImpl> dummy_mask = 65 LayerImpl::Create(host_impl.active_tree(), 2); 66 gfx::Transform dummy_matrix; 67 dummy_matrix.Translate(1.0, 2.0); 68 69 // The rest of the surface properties are either internal and should not cause 70 // change, or they are already accounted for by the 71 // owninglayer->layerPropertyChanged(). 72 EXECUTE_AND_VERIFY_SURFACE_DID_NOT_CHANGE( 73 render_surface->SetDrawOpacity(0.5f)); 74 EXECUTE_AND_VERIFY_SURFACE_DID_NOT_CHANGE( 75 render_surface->SetDrawTransform(dummy_matrix)); 76 EXECUTE_AND_VERIFY_SURFACE_DID_NOT_CHANGE( 77 render_surface->SetReplicaDrawTransform(dummy_matrix)); 78 EXECUTE_AND_VERIFY_SURFACE_DID_NOT_CHANGE(render_surface->ClearLayerLists()); 79 } 80 81 TEST(RenderSurfaceTest, SanityCheckSurfaceCreatesCorrectSharedQuadState) { 82 FakeImplProxy proxy; 83 FakeLayerTreeHostImpl host_impl(&proxy); 84 scoped_ptr<LayerImpl> root_layer = 85 LayerImpl::Create(host_impl.active_tree(), 1); 86 87 scoped_ptr<LayerImpl> owning_layer = 88 LayerImpl::Create(host_impl.active_tree(), 2); 89 owning_layer->CreateRenderSurface(); 90 ASSERT_TRUE(owning_layer->render_surface()); 91 owning_layer->draw_properties().render_target = owning_layer.get(); 92 RenderSurfaceImpl* render_surface = owning_layer->render_surface(); 93 94 root_layer->AddChild(owning_layer.Pass()); 95 96 gfx::Rect content_rect(0, 0, 50, 50); 97 gfx::Rect clip_rect(5, 5, 40, 40); 98 gfx::Transform origin; 99 100 origin.Translate(30, 40); 101 102 render_surface->SetDrawTransform(origin); 103 render_surface->SetContentRect(content_rect); 104 render_surface->SetClipRect(clip_rect); 105 render_surface->SetDrawOpacity(1.f); 106 107 QuadList quad_list; 108 SharedQuadStateList shared_state_list; 109 MockQuadCuller mock_quad_culler(&quad_list, &shared_state_list); 110 AppendQuadsData append_quads_data; 111 112 bool for_replica = false; 113 render_surface->AppendQuads( 114 &mock_quad_culler, &append_quads_data, for_replica, RenderPass::Id(2, 0)); 115 116 ASSERT_EQ(1u, shared_state_list.size()); 117 SharedQuadState* shared_quad_state = shared_state_list[0]; 118 119 EXPECT_EQ( 120 30.0, 121 shared_quad_state->content_to_target_transform.matrix().getDouble(0, 3)); 122 EXPECT_EQ( 123 40.0, 124 shared_quad_state->content_to_target_transform.matrix().getDouble(1, 3)); 125 EXPECT_RECT_EQ(content_rect, 126 gfx::Rect(shared_quad_state->visible_content_rect)); 127 EXPECT_EQ(1.f, shared_quad_state->opacity); 128 } 129 130 class TestRenderPassSink : public RenderPassSink { 131 public: 132 virtual void AppendRenderPass(scoped_ptr<RenderPass> render_pass) OVERRIDE { 133 render_passes_.push_back(render_pass.Pass()); 134 } 135 136 const ScopedPtrVector<RenderPass>& RenderPasses() const { 137 return render_passes_; 138 } 139 140 private: 141 ScopedPtrVector<RenderPass> render_passes_; 142 }; 143 144 TEST(RenderSurfaceTest, SanityCheckSurfaceCreatesCorrectRenderPass) { 145 FakeImplProxy proxy; 146 FakeLayerTreeHostImpl host_impl(&proxy); 147 scoped_ptr<LayerImpl> root_layer = 148 LayerImpl::Create(host_impl.active_tree(), 1); 149 150 scoped_ptr<LayerImpl> owning_layer = 151 LayerImpl::Create(host_impl.active_tree(), 2); 152 owning_layer->CreateRenderSurface(); 153 ASSERT_TRUE(owning_layer->render_surface()); 154 owning_layer->draw_properties().render_target = owning_layer.get(); 155 RenderSurfaceImpl* render_surface = owning_layer->render_surface(); 156 157 root_layer->AddChild(owning_layer.Pass()); 158 159 gfx::Rect content_rect(0, 0, 50, 50); 160 gfx::Transform origin; 161 origin.Translate(30.0, 40.0); 162 163 render_surface->SetScreenSpaceTransform(origin); 164 render_surface->SetContentRect(content_rect); 165 166 TestRenderPassSink pass_sink; 167 168 render_surface->AppendRenderPasses(&pass_sink); 169 170 ASSERT_EQ(1u, pass_sink.RenderPasses().size()); 171 RenderPass* pass = pass_sink.RenderPasses()[0]; 172 173 EXPECT_EQ(RenderPass::Id(2, 0), pass->id); 174 EXPECT_RECT_EQ(content_rect, pass->output_rect); 175 EXPECT_EQ(origin, pass->transform_to_root_target); 176 } 177 178 } // namespace 179 } // namespace cc 180