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_occlusion_tracker.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 AppendQuadsData append_quads_data;
113
114 bool for_replica = false;
115 render_surface->AppendQuads(render_pass.get(),
116 occlusion_tracker,
117 &append_quads_data,
118 for_replica,
119 RenderPassId(2, 0));
120
121 ASSERT_EQ(1u, render_pass->shared_quad_state_list.size());
122 SharedQuadState* shared_quad_state = render_pass->shared_quad_state_list[0];
123
124 EXPECT_EQ(
125 30.0,
126 shared_quad_state->content_to_target_transform.matrix().getDouble(0, 3));
127 EXPECT_EQ(
128 40.0,
129 shared_quad_state->content_to_target_transform.matrix().getDouble(1, 3));
130 EXPECT_RECT_EQ(content_rect,
131 gfx::Rect(shared_quad_state->visible_content_rect));
132 EXPECT_EQ(1.f, shared_quad_state->opacity);
133 }
134
135 class TestRenderPassSink : public RenderPassSink {
136 public:
AppendRenderPass(scoped_ptr<RenderPass> render_pass)137 virtual void AppendRenderPass(scoped_ptr<RenderPass> render_pass) OVERRIDE {
138 render_passes_.push_back(render_pass.Pass());
139 }
140
RenderPasses() const141 const RenderPassList& RenderPasses() const {
142 return render_passes_;
143 }
144
145 private:
146 RenderPassList render_passes_;
147 };
148
TEST(RenderSurfaceTest,SanityCheckSurfaceCreatesCorrectRenderPass)149 TEST(RenderSurfaceTest, SanityCheckSurfaceCreatesCorrectRenderPass) {
150 FakeImplProxy proxy;
151 TestSharedBitmapManager shared_bitmap_manager;
152 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager);
153 scoped_ptr<LayerImpl> root_layer =
154 LayerImpl::Create(host_impl.active_tree(), 1);
155
156 scoped_ptr<LayerImpl> owning_layer =
157 LayerImpl::Create(host_impl.active_tree(), 2);
158 owning_layer->CreateRenderSurface();
159 ASSERT_TRUE(owning_layer->render_surface());
160 owning_layer->draw_properties().render_target = owning_layer.get();
161 RenderSurfaceImpl* render_surface = owning_layer->render_surface();
162
163 root_layer->AddChild(owning_layer.Pass());
164
165 gfx::Rect content_rect(0, 0, 50, 50);
166 gfx::Transform origin;
167 origin.Translate(30.0, 40.0);
168
169 render_surface->SetScreenSpaceTransform(origin);
170 render_surface->SetContentRect(content_rect);
171
172 TestRenderPassSink pass_sink;
173
174 render_surface->AppendRenderPasses(&pass_sink);
175
176 ASSERT_EQ(1u, pass_sink.RenderPasses().size());
177 RenderPass* pass = pass_sink.RenderPasses()[0];
178
179 EXPECT_EQ(RenderPassId(2, 0), pass->id);
180 EXPECT_RECT_EQ(content_rect, pass->output_rect);
181 EXPECT_EQ(origin, pass->transform_to_root_target);
182 }
183
184 } // namespace
185 } // namespace cc
186