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(settings
,
43 shared_bitmap_manager_
.get(),
46 host_impl_
->InitializeRenderer(FakeOutputSurface::Create3d());
48 scoped_ptr
<LayerImpl
> root_layer
= LayerImpl::Create(active_tree(), 1);
49 active_tree()->SetRootLayer(root_layer
.Pass());
52 LayerTreeImpl
* active_tree() { return host_impl_
->active_tree(); }
54 void SetTestName(const std::string
& name
) { test_name_
= name
; }
57 CHECK(!test_name_
.empty()) << "Must SetTestName() before AfterTest().";
58 perf_test::PrintResult("occlusion_tracker_time",
61 1000 * timer_
.MsPerLap(),
68 std::string test_name_
;
69 FakeLayerTreeHostImplClient client_
;
71 DebugScopedSetImplThread impl_
;
72 FakeRenderingStatsInstrumentation stats_
;
73 scoped_ptr
<SharedBitmapManager
> shared_bitmap_manager_
;
74 scoped_ptr
<LayerTreeHostImpl
> host_impl_
;
77 TEST_F(OcclusionTrackerPerfTest
, UnoccludedContentRect_FullyOccluded
) {
78 SetTestName("unoccluded_content_rect_fully_occluded");
80 gfx::Rect
viewport_rect(768, 1038);
81 OcclusionTracker
<LayerImpl
> tracker(viewport_rect
);
84 host_impl_
->SetViewportSize(viewport_rect
.size());
86 scoped_ptr
<SolidColorLayerImpl
> opaque_layer
=
87 SolidColorLayerImpl::Create(active_tree(), 2);
88 opaque_layer
->SetBackgroundColor(SK_ColorRED
);
89 opaque_layer
->SetContentsOpaque(true);
90 opaque_layer
->SetDrawsContent(true);
91 opaque_layer
->SetBounds(viewport_rect
.size());
92 opaque_layer
->SetContentBounds(viewport_rect
.size());
93 active_tree()->root_layer()->AddChild(opaque_layer
.Pass());
95 active_tree()->UpdateDrawProperties();
96 const LayerImplList
& rsll
= active_tree()->RenderSurfaceLayerList();
97 ASSERT_EQ(1u, rsll
.size());
98 EXPECT_EQ(1u, rsll
[0]->render_surface()->layer_list().size());
100 LayerIterator
<LayerImpl
> begin
= LayerIterator
<LayerImpl
>::Begin(&rsll
);
101 LayerIterator
<LayerImpl
> end
= LayerIterator
<LayerImpl
>::End(&rsll
);
103 LayerIteratorPosition
<LayerImpl
> pos
= begin
;
105 // The opaque_layer adds occlusion over the whole viewport.
106 tracker
.EnterLayer(pos
);
107 tracker
.LeaveLayer(pos
);
109 gfx::Transform transform_to_target
;
110 transform_to_target
.Translate(0, 96);
113 Occlusion occlusion
=
114 tracker
.GetCurrentOcclusionForLayer(transform_to_target
);
115 for (int x
= 0; x
< viewport_rect
.width(); x
+= 256) {
116 for (int y
= 0; y
< viewport_rect
.height(); y
+= 256) {
117 gfx::Rect
query_content_rect(x
, y
, 256, 256);
118 gfx::Rect unoccluded
=
119 occlusion
.GetUnoccludedContentRect(query_content_rect
);
120 // Sanity test that we're not hitting early outs.
122 query_content_rect
.right() <= viewport_rect
.width() &&
123 query_content_rect
.bottom() + 96 <= viewport_rect
.height();
124 CHECK_EQ(expect_empty
, unoccluded
.IsEmpty())
125 << query_content_rect
.ToString();
130 } while (!timer_
.HasTimeLimitExpired());
133 LayerIteratorPosition
<LayerImpl
> next
= begin
;
134 EXPECT_EQ(active_tree()->root_layer(), next
.current_layer
);
137 EXPECT_EQ(end
, begin
);
142 TEST_F(OcclusionTrackerPerfTest
, UnoccludedContentRect_10OpaqueLayers
) {
143 static const int kNumOpaqueLayers
= 10;
144 SetTestName("unoccluded_content_rect_10_opaque_layers");
146 gfx::Rect
viewport_rect(768, 1038);
147 OcclusionTracker
<LayerImpl
> tracker(viewport_rect
);
150 host_impl_
->SetViewportSize(viewport_rect
.size());
152 for (int i
= 0; i
< kNumOpaqueLayers
; ++i
) {
153 scoped_ptr
<SolidColorLayerImpl
> opaque_layer
=
154 SolidColorLayerImpl::Create(active_tree(), 2 + i
);
155 opaque_layer
->SetBackgroundColor(SK_ColorRED
);
156 opaque_layer
->SetContentsOpaque(true);
157 opaque_layer
->SetDrawsContent(true);
158 opaque_layer
->SetBounds(
159 gfx::Size(viewport_rect
.width() / 2, viewport_rect
.height() / 2));
160 opaque_layer
->SetContentBounds(
161 gfx::Size(viewport_rect
.width() / 2, viewport_rect
.height() / 2));
162 opaque_layer
->SetPosition(gfx::Point(i
, i
));
163 active_tree()->root_layer()->AddChild(opaque_layer
.Pass());
166 active_tree()->UpdateDrawProperties();
167 const LayerImplList
& rsll
= active_tree()->RenderSurfaceLayerList();
168 ASSERT_EQ(1u, rsll
.size());
169 EXPECT_EQ(static_cast<size_t>(kNumOpaqueLayers
),
170 rsll
[0]->render_surface()->layer_list().size());
172 LayerIterator
<LayerImpl
> begin
= LayerIterator
<LayerImpl
>::Begin(&rsll
);
173 LayerIterator
<LayerImpl
> end
= LayerIterator
<LayerImpl
>::End(&rsll
);
175 // The opaque_layers add occlusion.
176 for (int i
= 0; i
< kNumOpaqueLayers
- 1; ++i
) {
177 LayerIteratorPosition
<LayerImpl
> pos
= begin
;
178 tracker
.EnterLayer(pos
);
179 tracker
.LeaveLayer(pos
);
182 LayerIteratorPosition
<LayerImpl
> pos
= begin
;
183 tracker
.EnterLayer(pos
);
184 tracker
.LeaveLayer(pos
);
186 gfx::Transform transform_to_target
;
187 transform_to_target
.Translate(0, 96);
190 Occlusion occlusion
=
191 tracker
.GetCurrentOcclusionForLayer(transform_to_target
);
192 for (int x
= 0; x
< viewport_rect
.width(); x
+= 256) {
193 for (int y
= 0; y
< viewport_rect
.height(); y
+= 256) {
194 gfx::Rect
query_content_rect(x
, y
, 256, 256);
195 gfx::Rect unoccluded
=
196 occlusion
.GetUnoccludedContentRect(query_content_rect
);
201 } while (!timer_
.HasTimeLimitExpired());
204 LayerIteratorPosition
<LayerImpl
> next
= begin
;
205 EXPECT_EQ(active_tree()->root_layer(), next
.current_layer
);
208 EXPECT_EQ(end
, begin
);