Revert "Reland c91b178b07b0d - Delete dead signin code (SigninGlobalError)"
[chromium-blink-merge.git] / cc / layers / render_surface_unittest.cc
blob5ecf2f7bac8f1c3993b1af81027a1e69609e0c70
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_occlusion_tracker.h"
15 #include "cc/test/test_shared_bitmap_manager.h"
16 #include "cc/test/test_task_graph_runner.h"
17 #include "cc/trees/single_thread_proxy.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "ui/gfx/transform.h"
22 namespace cc {
23 namespace {
25 #define EXECUTE_AND_VERIFY_SURFACE_CHANGED(code_to_test) \
26 render_surface->ResetPropertyChangedFlag(); \
27 code_to_test; \
28 EXPECT_TRUE(render_surface->SurfacePropertyChanged())
30 #define EXECUTE_AND_VERIFY_SURFACE_DID_NOT_CHANGE(code_to_test) \
31 render_surface->ResetPropertyChangedFlag(); \
32 code_to_test; \
33 EXPECT_FALSE(render_surface->SurfacePropertyChanged())
35 TEST(RenderSurfaceTest, VerifySurfaceChangesAreTrackedProperly) {
37 // This test checks that SurfacePropertyChanged() has the correct behavior.
40 FakeImplProxy proxy;
41 TestSharedBitmapManager shared_bitmap_manager;
42 TestTaskGraphRunner task_graph_runner;
43 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
44 &task_graph_runner);
45 scoped_ptr<LayerImpl> owning_layer =
46 LayerImpl::Create(host_impl.active_tree(), 1);
47 owning_layer->SetHasRenderSurface(true);
48 ASSERT_TRUE(owning_layer->render_surface());
49 RenderSurfaceImpl* render_surface = owning_layer->render_surface();
50 gfx::Rect test_rect(3, 4, 5, 6);
51 owning_layer->ResetAllChangeTrackingForSubtree();
53 // Currently, the content_rect, clip_rect, and
54 // owning_layer->layerPropertyChanged() are the only sources of change.
55 EXECUTE_AND_VERIFY_SURFACE_CHANGED(render_surface->SetClipRect(test_rect));
56 EXECUTE_AND_VERIFY_SURFACE_CHANGED(render_surface->SetContentRect(test_rect));
58 owning_layer->SetOpacity(0.5f);
59 EXPECT_TRUE(render_surface->SurfacePropertyChanged());
60 owning_layer->ResetAllChangeTrackingForSubtree();
62 // Setting the surface properties to the same values again should not be
63 // considered "change".
64 EXECUTE_AND_VERIFY_SURFACE_DID_NOT_CHANGE(
65 render_surface->SetClipRect(test_rect));
66 EXECUTE_AND_VERIFY_SURFACE_DID_NOT_CHANGE(
67 render_surface->SetContentRect(test_rect));
69 scoped_ptr<LayerImpl> dummy_mask =
70 LayerImpl::Create(host_impl.active_tree(), 2);
71 gfx::Transform dummy_matrix;
72 dummy_matrix.Translate(1.0, 2.0);
74 // The rest of the surface properties are either internal and should not cause
75 // change, or they are already accounted for by the
76 // owninglayer->layerPropertyChanged().
77 EXECUTE_AND_VERIFY_SURFACE_DID_NOT_CHANGE(
78 render_surface->SetDrawOpacity(0.5f));
79 EXECUTE_AND_VERIFY_SURFACE_DID_NOT_CHANGE(
80 render_surface->SetDrawTransform(dummy_matrix));
81 EXECUTE_AND_VERIFY_SURFACE_DID_NOT_CHANGE(
82 render_surface->SetReplicaDrawTransform(dummy_matrix));
83 EXECUTE_AND_VERIFY_SURFACE_DID_NOT_CHANGE(render_surface->ClearLayerLists());
86 TEST(RenderSurfaceTest, SanityCheckSurfaceCreatesCorrectSharedQuadState) {
87 FakeImplProxy proxy;
88 TestSharedBitmapManager shared_bitmap_manager;
89 TestTaskGraphRunner task_graph_runner;
90 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
91 &task_graph_runner);
92 scoped_ptr<LayerImpl> root_layer =
93 LayerImpl::Create(host_impl.active_tree(), 1);
95 scoped_ptr<LayerImpl> owning_layer =
96 LayerImpl::Create(host_impl.active_tree(), 2);
97 owning_layer->SetHasRenderSurface(true);
98 ASSERT_TRUE(owning_layer->render_surface());
99 owning_layer->draw_properties().render_target = owning_layer.get();
101 SkXfermode::Mode blend_mode = SkXfermode::kSoftLight_Mode;
102 owning_layer->SetBlendMode(blend_mode);
103 RenderSurfaceImpl* render_surface = owning_layer->render_surface();
105 root_layer->AddChild(owning_layer.Pass());
107 gfx::Rect content_rect(0, 0, 50, 50);
108 gfx::Rect clip_rect(5, 5, 40, 40);
109 gfx::Transform origin;
110 origin.Translate(30, 40);
112 render_surface->SetContentRect(content_rect);
113 render_surface->SetClipRect(clip_rect);
114 render_surface->SetDrawOpacity(1.f);
116 scoped_ptr<RenderPass> render_pass = RenderPass::Create();
117 AppendQuadsData append_quads_data;
119 render_surface->AppendQuads(render_pass.get(), origin, Occlusion(),
120 SK_ColorBLACK, 1.f, nullptr, &append_quads_data,
121 RenderPassId(2, 0));
123 ASSERT_EQ(1u, render_pass->shared_quad_state_list.size());
124 SharedQuadState* shared_quad_state =
125 render_pass->shared_quad_state_list.front();
127 EXPECT_EQ(
128 30.0,
129 shared_quad_state->quad_to_target_transform.matrix().getDouble(0, 3));
130 EXPECT_EQ(
131 40.0,
132 shared_quad_state->quad_to_target_transform.matrix().getDouble(1, 3));
133 EXPECT_EQ(content_rect,
134 gfx::Rect(shared_quad_state->visible_quad_layer_rect));
135 EXPECT_EQ(1.f, shared_quad_state->opacity);
136 EXPECT_EQ(blend_mode, shared_quad_state->blend_mode);
139 class TestRenderPassSink : public RenderPassSink {
140 public:
141 void AppendRenderPass(scoped_ptr<RenderPass> render_pass) override {
142 render_passes_.push_back(render_pass.Pass());
145 const RenderPassList& RenderPasses() const {
146 return render_passes_;
149 private:
150 RenderPassList render_passes_;
153 TEST(RenderSurfaceTest, SanityCheckSurfaceCreatesCorrectRenderPass) {
154 FakeImplProxy proxy;
155 TestSharedBitmapManager shared_bitmap_manager;
156 TestTaskGraphRunner task_graph_runner;
157 FakeLayerTreeHostImpl host_impl(&proxy, &shared_bitmap_manager,
158 &task_graph_runner);
159 scoped_ptr<LayerImpl> root_layer =
160 LayerImpl::Create(host_impl.active_tree(), 1);
162 scoped_ptr<LayerImpl> owning_layer =
163 LayerImpl::Create(host_impl.active_tree(), 2);
164 owning_layer->SetHasRenderSurface(true);
165 ASSERT_TRUE(owning_layer->render_surface());
166 owning_layer->draw_properties().render_target = owning_layer.get();
167 RenderSurfaceImpl* render_surface = owning_layer->render_surface();
169 root_layer->AddChild(owning_layer.Pass());
171 gfx::Rect content_rect(0, 0, 50, 50);
172 gfx::Transform origin;
173 origin.Translate(30.0, 40.0);
175 render_surface->SetScreenSpaceTransform(origin);
176 render_surface->SetContentRect(content_rect);
178 TestRenderPassSink pass_sink;
180 render_surface->AppendRenderPasses(&pass_sink);
182 ASSERT_EQ(1u, pass_sink.RenderPasses().size());
183 RenderPass* pass = pass_sink.RenderPasses()[0];
185 EXPECT_EQ(RenderPassId(2, 0), pass->id);
186 EXPECT_EQ(content_rect, pass->output_rect);
187 EXPECT_EQ(origin, pass->transform_to_root_target);
190 } // namespace
191 } // namespace cc