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.
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"
23 #define EXECUTE_AND_VERIFY_SURFACE_CHANGED(code_to_test) \
24 render_surface->ResetPropertyChangedFlag(); \
26 EXPECT_TRUE(render_surface->SurfacePropertyChanged())
28 #define EXECUTE_AND_VERIFY_SURFACE_DID_NOT_CHANGE(code_to_test) \
29 render_surface->ResetPropertyChangedFlag(); \
31 EXPECT_FALSE(render_surface->SurfacePropertyChanged())
33 TEST(RenderSurfaceTest
, VerifySurfaceChangesAreTrackedProperly
) {
35 // This test checks that SurfacePropertyChanged() has the correct behavior.
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();
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
));
53 owning_layer
->SetOpacity(0.5f
);
54 EXPECT_TRUE(render_surface
->SurfacePropertyChanged());
55 owning_layer
->ResetAllChangeTrackingForSubtree();
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
));
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);
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());
81 TEST(RenderSurfaceTest
, SanityCheckSurfaceCreatesCorrectSharedQuadState
) {
83 FakeLayerTreeHostImpl
host_impl(&proxy
);
84 scoped_ptr
<LayerImpl
> root_layer
=
85 LayerImpl::Create(host_impl
.active_tree(), 1);
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();
94 root_layer
->AddChild(owning_layer
.Pass());
96 gfx::Rect
content_rect(0, 0, 50, 50);
97 gfx::Rect
clip_rect(5, 5, 40, 40);
98 gfx::Transform origin
;
100 origin
.Translate(30, 40);
102 render_surface
->SetDrawTransform(origin
);
103 render_surface
->SetContentRect(content_rect
);
104 render_surface
->SetClipRect(clip_rect
);
105 render_surface
->SetDrawOpacity(1.f
);
108 SharedQuadStateList shared_state_list
;
109 MockQuadCuller
mock_quad_culler(&quad_list
, &shared_state_list
);
110 AppendQuadsData append_quads_data
;
112 bool for_replica
= false;
113 render_surface
->AppendQuads(
114 &mock_quad_culler
, &append_quads_data
, for_replica
, RenderPass::Id(2, 0));
116 ASSERT_EQ(1u, shared_state_list
.size());
117 SharedQuadState
* shared_quad_state
= shared_state_list
[0];
121 shared_quad_state
->content_to_target_transform
.matrix().getDouble(0, 3));
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
);
130 class TestRenderPassSink
: public RenderPassSink
{
132 virtual void AppendRenderPass(scoped_ptr
<RenderPass
> render_pass
) OVERRIDE
{
133 render_passes_
.push_back(render_pass
.Pass());
136 const ScopedPtrVector
<RenderPass
>& RenderPasses() const {
137 return render_passes_
;
141 ScopedPtrVector
<RenderPass
> render_passes_
;
144 TEST(RenderSurfaceTest
, SanityCheckSurfaceCreatesCorrectRenderPass
) {
146 FakeLayerTreeHostImpl
host_impl(&proxy
);
147 scoped_ptr
<LayerImpl
> root_layer
=
148 LayerImpl::Create(host_impl
.active_tree(), 1);
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();
157 root_layer
->AddChild(owning_layer
.Pass());
159 gfx::Rect
content_rect(0, 0, 50, 50);
160 gfx::Transform origin
;
161 origin
.Translate(30.0, 40.0);
163 render_surface
->SetScreenSpaceTransform(origin
);
164 render_surface
->SetContentRect(content_rect
);
166 TestRenderPassSink pass_sink
;
168 render_surface
->AppendRenderPasses(&pass_sink
);
170 ASSERT_EQ(1u, pass_sink
.RenderPasses().size());
171 RenderPass
* pass
= pass_sink
.RenderPasses()[0];
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
);