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
= tracker
.UnoccludedContentRect(
113 query_content_rect
, transform_to_target
);
114 // Sanity test that we're not hitting early outs.
116 query_content_rect
.right() <= viewport_rect
.width() &&
117 query_content_rect
.bottom() + 96 <= viewport_rect
.height();
118 CHECK_EQ(expect_empty
, unoccluded
.IsEmpty())
119 << query_content_rect
.ToString();
124 } while (!timer_
.HasTimeLimitExpired());
127 LayerIteratorPosition
<LayerImpl
> next
= begin
;
128 EXPECT_EQ(active_tree()->root_layer(), next
.current_layer
);
131 EXPECT_EQ(end
, begin
);
136 TEST_F(OcclusionTrackerPerfTest
, UnoccludedContentRect_10OpaqueLayers
) {
137 static const int kNumOpaqueLayers
= 10;
138 SetTestName("unoccluded_content_rect_10_opaque_layers");
140 gfx::Rect
viewport_rect(768, 1038);
141 OcclusionTracker
<LayerImpl
> tracker(viewport_rect
);
144 host_impl_
->SetViewportSize(viewport_rect
.size());
146 for (int i
= 0; i
< kNumOpaqueLayers
; ++i
) {
147 scoped_ptr
<SolidColorLayerImpl
> opaque_layer
=
148 SolidColorLayerImpl::Create(active_tree(), 2 + i
);
149 opaque_layer
->SetBackgroundColor(SK_ColorRED
);
150 opaque_layer
->SetContentsOpaque(true);
151 opaque_layer
->SetDrawsContent(true);
152 opaque_layer
->SetBounds(
153 gfx::Size(viewport_rect
.width() / 2, viewport_rect
.height() / 2));
154 opaque_layer
->SetContentBounds(
155 gfx::Size(viewport_rect
.width() / 2, viewport_rect
.height() / 2));
156 opaque_layer
->SetPosition(gfx::Point(i
, i
));
157 active_tree()->root_layer()->AddChild(opaque_layer
.PassAs
<LayerImpl
>());
160 active_tree()->UpdateDrawProperties();
161 const LayerImplList
& rsll
= active_tree()->RenderSurfaceLayerList();
162 ASSERT_EQ(1u, rsll
.size());
163 EXPECT_EQ(static_cast<size_t>(kNumOpaqueLayers
),
164 rsll
[0]->render_surface()->layer_list().size());
166 LayerIterator
<LayerImpl
> begin
= LayerIterator
<LayerImpl
>::Begin(&rsll
);
167 LayerIterator
<LayerImpl
> end
= LayerIterator
<LayerImpl
>::End(&rsll
);
169 // The opaque_layers add occlusion.
170 for (int i
= 0; i
< kNumOpaqueLayers
- 1; ++i
) {
171 LayerIteratorPosition
<LayerImpl
> pos
= begin
;
172 tracker
.EnterLayer(pos
);
173 tracker
.LeaveLayer(pos
);
176 LayerIteratorPosition
<LayerImpl
> pos
= begin
;
177 tracker
.EnterLayer(pos
);
178 tracker
.LeaveLayer(pos
);
180 gfx::Transform transform_to_target
;
181 transform_to_target
.Translate(0, 96);
184 for (int x
= 0; x
< viewport_rect
.width(); x
+= 256) {
185 for (int y
= 0; y
< viewport_rect
.height(); y
+= 256) {
186 gfx::Rect
query_content_rect(x
, y
, 256, 256);
187 gfx::Rect unoccluded
= tracker
.UnoccludedContentRect(
188 query_content_rect
, transform_to_target
);
193 } while (!timer_
.HasTimeLimitExpired());
196 LayerIteratorPosition
<LayerImpl
> next
= begin
;
197 EXPECT_EQ(active_tree()->root_layer(), next
.current_layer
);
200 EXPECT_EQ(end
, begin
);