1 // Copyright 2014 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/trees/occlusion_tracker.h"
7 #include "base/time/time.h"
8 #include "cc/debug/lap_timer.h"
9 #include "cc/layers/layer_iterator.h"
10 #include "cc/layers/solid_color_layer_impl.h"
11 #include "cc/test/fake_layer_tree_host_impl_client.h"
12 #include "cc/test/fake_output_surface.h"
13 #include "cc/test/fake_proxy.h"
14 #include "cc/test/fake_rendering_stats_instrumentation.h"
15 #include "cc/test/test_shared_bitmap_manager.h"
16 #include "cc/trees/layer_tree_host_impl.h"
17 #include "cc/trees/layer_tree_impl.h"
18 #include "cc/trees/single_thread_proxy.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 #include "testing/perf/perf_test.h"
25 static const int kTimeLimitMillis
= 2000;
26 static const int kWarmupRuns
= 5;
27 static const int kTimeCheckInterval
= 10;
29 class OcclusionTrackerPerfTest
: public testing::Test
{
31 OcclusionTrackerPerfTest()
33 base::TimeDelta::FromMilliseconds(kTimeLimitMillis
),
37 LayerTreeSettings settings
;
38 shared_bitmap_manager_
.reset(new TestSharedBitmapManager());
39 host_impl_
= LayerTreeHostImpl::Create(
40 settings
, &client_
, &proxy_
, &stats_
, shared_bitmap_manager_
.get(), 1);
41 host_impl_
->InitializeRenderer(
42 FakeOutputSurface::Create3d().PassAs
<OutputSurface
>());
44 scoped_ptr
<LayerImpl
> root_layer
= LayerImpl::Create(active_tree(), 1);
45 active_tree()->SetRootLayer(root_layer
.Pass());
48 LayerTreeImpl
* active_tree() { return host_impl_
->active_tree(); }
50 void SetTestName(const std::string
& name
) { test_name_
= name
; }
53 CHECK(!test_name_
.empty()) << "Must SetTestName() before AfterTest().";
54 perf_test::PrintResult("occlusion_tracker_time",
57 1000 * timer_
.MsPerLap(),
64 std::string test_name_
;
65 FakeLayerTreeHostImplClient client_
;
67 DebugScopedSetImplThread impl_
;
68 FakeRenderingStatsInstrumentation stats_
;
69 scoped_ptr
<SharedBitmapManager
> shared_bitmap_manager_
;
70 scoped_ptr
<LayerTreeHostImpl
> host_impl_
;
73 TEST_F(OcclusionTrackerPerfTest
, UnoccludedContentRect_FullyOccluded
) {
74 SetTestName("unoccluded_content_rect_fully_occluded");
76 gfx::Rect
viewport_rect(768, 1038);
77 OcclusionTracker
<LayerImpl
> tracker(viewport_rect
);
80 host_impl_
->SetViewportSize(viewport_rect
.size());
82 scoped_ptr
<SolidColorLayerImpl
> opaque_layer
=
83 SolidColorLayerImpl::Create(active_tree(), 2);
84 opaque_layer
->SetBackgroundColor(SK_ColorRED
);
85 opaque_layer
->SetContentsOpaque(true);
86 opaque_layer
->SetDrawsContent(true);
87 opaque_layer
->SetBounds(viewport_rect
.size());
88 opaque_layer
->SetContentBounds(viewport_rect
.size());
89 active_tree()->root_layer()->AddChild(opaque_layer
.PassAs
<LayerImpl
>());
91 active_tree()->UpdateDrawProperties();
92 const LayerImplList
& rsll
= active_tree()->RenderSurfaceLayerList();
93 ASSERT_EQ(1u, rsll
.size());
94 EXPECT_EQ(1u, rsll
[0]->render_surface()->layer_list().size());
96 LayerIterator
<LayerImpl
> begin
= LayerIterator
<LayerImpl
>::Begin(&rsll
);
97 LayerIterator
<LayerImpl
> end
= LayerIterator
<LayerImpl
>::End(&rsll
);
99 LayerIteratorPosition
<LayerImpl
> pos
= begin
;
101 // The opaque_layer adds occlusion over the whole viewport.
102 tracker
.EnterLayer(pos
);
103 tracker
.LeaveLayer(pos
);
105 gfx::Transform transform_to_target
;
106 transform_to_target
.Translate(0, 96);
109 for (int x
= 0; x
< viewport_rect
.width(); x
+= 256) {
110 for (int y
= 0; y
< viewport_rect
.height(); y
+= 256) {
111 gfx::Rect
query_content_rect(x
, y
, 256, 256);
112 gfx::Rect unoccluded
=
113 tracker
.UnoccludedContentRect(pos
.target_render_surface_layer
,
115 transform_to_target
);
116 // Sanity test that we're not hitting early outs.
118 query_content_rect
.right() <= viewport_rect
.width() &&
119 query_content_rect
.bottom() + 96 <= viewport_rect
.height();
120 CHECK_EQ(expect_empty
, unoccluded
.IsEmpty())
121 << query_content_rect
.ToString();
126 } while (!timer_
.HasTimeLimitExpired());
129 LayerIteratorPosition
<LayerImpl
> next
= begin
;
130 EXPECT_EQ(active_tree()->root_layer(), next
.current_layer
);
133 EXPECT_EQ(end
, begin
);
138 TEST_F(OcclusionTrackerPerfTest
, UnoccludedContentRect_10OpaqueLayers
) {
139 static const int kNumOpaqueLayers
= 10;
140 SetTestName("unoccluded_content_rect_10_opaque_layers");
142 gfx::Rect
viewport_rect(768, 1038);
143 OcclusionTracker
<LayerImpl
> tracker(viewport_rect
);
146 host_impl_
->SetViewportSize(viewport_rect
.size());
148 for (int i
= 0; i
< kNumOpaqueLayers
; ++i
) {
149 scoped_ptr
<SolidColorLayerImpl
> opaque_layer
=
150 SolidColorLayerImpl::Create(active_tree(), 2 + i
);
151 opaque_layer
->SetBackgroundColor(SK_ColorRED
);
152 opaque_layer
->SetContentsOpaque(true);
153 opaque_layer
->SetDrawsContent(true);
154 opaque_layer
->SetBounds(
155 gfx::Size(viewport_rect
.width() / 2, viewport_rect
.height() / 2));
156 opaque_layer
->SetContentBounds(
157 gfx::Size(viewport_rect
.width() / 2, viewport_rect
.height() / 2));
158 opaque_layer
->SetPosition(gfx::Point(i
, i
));
159 active_tree()->root_layer()->AddChild(opaque_layer
.PassAs
<LayerImpl
>());
162 active_tree()->UpdateDrawProperties();
163 const LayerImplList
& rsll
= active_tree()->RenderSurfaceLayerList();
164 ASSERT_EQ(1u, rsll
.size());
165 EXPECT_EQ(static_cast<size_t>(kNumOpaqueLayers
),
166 rsll
[0]->render_surface()->layer_list().size());
168 LayerIterator
<LayerImpl
> begin
= LayerIterator
<LayerImpl
>::Begin(&rsll
);
169 LayerIterator
<LayerImpl
> end
= LayerIterator
<LayerImpl
>::End(&rsll
);
171 // The opaque_layers add occlusion.
172 for (int i
= 0; i
< kNumOpaqueLayers
- 1; ++i
) {
173 LayerIteratorPosition
<LayerImpl
> pos
= begin
;
174 tracker
.EnterLayer(pos
);
175 tracker
.LeaveLayer(pos
);
178 LayerIteratorPosition
<LayerImpl
> pos
= begin
;
179 tracker
.EnterLayer(pos
);
180 tracker
.LeaveLayer(pos
);
182 gfx::Transform transform_to_target
;
183 transform_to_target
.Translate(0, 96);
186 for (int x
= 0; x
< viewport_rect
.width(); x
+= 256) {
187 for (int y
= 0; y
< viewport_rect
.height(); y
+= 256) {
188 gfx::Rect
query_content_rect(x
, y
, 256, 256);
189 gfx::Rect unoccluded
=
190 tracker
.UnoccludedContentRect(pos
.target_render_surface_layer
,
192 transform_to_target
);
197 } while (!timer_
.HasTimeLimitExpired());
200 LayerIteratorPosition
<LayerImpl
> next
= begin
;
201 EXPECT_EQ(active_tree()->root_layer(), next
.current_layer
);
204 EXPECT_EQ(end
, begin
);