• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "cc/trees/occlusion_tracker.h"
6 
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"
21 
22 namespace cc {
23 namespace {
24 
25 static const int kTimeLimitMillis = 2000;
26 static const int kWarmupRuns = 5;
27 static const int kTimeCheckInterval = 10;
28 
29 class OcclusionTrackerPerfTest : public testing::Test {
30  public:
OcclusionTrackerPerfTest()31   OcclusionTrackerPerfTest()
32       : timer_(kWarmupRuns,
33                base::TimeDelta::FromMilliseconds(kTimeLimitMillis),
34                kTimeCheckInterval),
35         impl_(&proxy_) {}
CreateHost()36   void CreateHost() {
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>());
43 
44     scoped_ptr<LayerImpl> root_layer = LayerImpl::Create(active_tree(), 1);
45     active_tree()->SetRootLayer(root_layer.Pass());
46   }
47 
active_tree()48   LayerTreeImpl* active_tree() { return host_impl_->active_tree(); }
49 
SetTestName(const std::string & name)50   void SetTestName(const std::string& name) { test_name_ = name; }
51 
PrintResults()52   void PrintResults() {
53     CHECK(!test_name_.empty()) << "Must SetTestName() before AfterTest().";
54     perf_test::PrintResult("occlusion_tracker_time",
55                            "",
56                            test_name_,
57                            1000 * timer_.MsPerLap(),
58                            "us",
59                            true);
60   }
61 
62  protected:
63   LapTimer timer_;
64   std::string test_name_;
65   FakeLayerTreeHostImplClient client_;
66   FakeProxy proxy_;
67   DebugScopedSetImplThread impl_;
68   FakeRenderingStatsInstrumentation stats_;
69   scoped_ptr<SharedBitmapManager> shared_bitmap_manager_;
70   scoped_ptr<LayerTreeHostImpl> host_impl_;
71 };
72 
TEST_F(OcclusionTrackerPerfTest,UnoccludedContentRect_FullyOccluded)73 TEST_F(OcclusionTrackerPerfTest, UnoccludedContentRect_FullyOccluded) {
74   SetTestName("unoccluded_content_rect_fully_occluded");
75 
76   gfx::Rect viewport_rect(768, 1038);
77   OcclusionTracker<LayerImpl> tracker(viewport_rect);
78 
79   CreateHost();
80   host_impl_->SetViewportSize(viewport_rect.size());
81 
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>());
90 
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());
95 
96   LayerIterator<LayerImpl> begin = LayerIterator<LayerImpl>::Begin(&rsll);
97   LayerIterator<LayerImpl> end = LayerIterator<LayerImpl>::End(&rsll);
98 
99   LayerIteratorPosition<LayerImpl> pos = begin;
100 
101   // The opaque_layer adds occlusion over the whole viewport.
102   tracker.EnterLayer(pos);
103   tracker.LeaveLayer(pos);
104 
105   gfx::Transform transform_to_target;
106   transform_to_target.Translate(0, 96);
107 
108   do {
109     Occlusion occlusion =
110         tracker.GetCurrentOcclusionForLayer(transform_to_target);
111     for (int x = 0; x < viewport_rect.width(); x += 256) {
112       for (int y = 0; y < viewport_rect.height(); y += 256) {
113         gfx::Rect query_content_rect(x, y, 256, 256);
114         gfx::Rect unoccluded =
115             occlusion.GetUnoccludedContentRect(query_content_rect);
116         // Sanity test that we're not hitting early outs.
117         bool expect_empty =
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();
122       }
123     }
124 
125     timer_.NextLap();
126   } while (!timer_.HasTimeLimitExpired());
127 
128   ++begin;
129   LayerIteratorPosition<LayerImpl> next = begin;
130   EXPECT_EQ(active_tree()->root_layer(), next.current_layer);
131 
132   ++begin;
133   EXPECT_EQ(end, begin);
134 
135   PrintResults();
136 }
137 
TEST_F(OcclusionTrackerPerfTest,UnoccludedContentRect_10OpaqueLayers)138 TEST_F(OcclusionTrackerPerfTest, UnoccludedContentRect_10OpaqueLayers) {
139   static const int kNumOpaqueLayers = 10;
140   SetTestName("unoccluded_content_rect_10_opaque_layers");
141 
142   gfx::Rect viewport_rect(768, 1038);
143   OcclusionTracker<LayerImpl> tracker(viewport_rect);
144 
145   CreateHost();
146   host_impl_->SetViewportSize(viewport_rect.size());
147 
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>());
160   }
161 
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());
167 
168   LayerIterator<LayerImpl> begin = LayerIterator<LayerImpl>::Begin(&rsll);
169   LayerIterator<LayerImpl> end = LayerIterator<LayerImpl>::End(&rsll);
170 
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);
176     ++begin;
177   }
178   LayerIteratorPosition<LayerImpl> pos = begin;
179   tracker.EnterLayer(pos);
180   tracker.LeaveLayer(pos);
181 
182   gfx::Transform transform_to_target;
183   transform_to_target.Translate(0, 96);
184 
185   do {
186     Occlusion occlusion =
187         tracker.GetCurrentOcclusionForLayer(transform_to_target);
188     for (int x = 0; x < viewport_rect.width(); x += 256) {
189       for (int y = 0; y < viewport_rect.height(); y += 256) {
190         gfx::Rect query_content_rect(x, y, 256, 256);
191         gfx::Rect unoccluded =
192             occlusion.GetUnoccludedContentRect(query_content_rect);
193       }
194     }
195 
196     timer_.NextLap();
197   } while (!timer_.HasTimeLimitExpired());
198 
199   ++begin;
200   LayerIteratorPosition<LayerImpl> next = begin;
201   EXPECT_EQ(active_tree()->root_layer(), next.current_layer);
202 
203   ++begin;
204   EXPECT_EQ(end, begin);
205 
206   PrintResults();
207 }
208 
209 }  // namespace
210 }  // namespace cc
211