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