Roll src/third_party/skia 4cd6713:0edd965
[chromium-blink-merge.git] / cc / trees / occlusion_tracker_perftest.cc
blob854dacdb5f0a330c330aa1eda12af9860f05efc3
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"
22 namespace cc {
23 namespace {
25 static const int kTimeLimitMillis = 2000;
26 static const int kWarmupRuns = 5;
27 static const int kTimeCheckInterval = 10;
29 class OcclusionTrackerPerfTest : public testing::Test {
30 public:
31 OcclusionTrackerPerfTest()
32 : timer_(kWarmupRuns,
33 base::TimeDelta::FromMilliseconds(kTimeLimitMillis),
34 kTimeCheckInterval),
35 impl_(&proxy_) {}
36 void CreateHost() {
37 LayerTreeSettings settings;
38 shared_bitmap_manager_.reset(new TestSharedBitmapManager());
39 host_impl_ =
40 LayerTreeHostImpl::Create(settings, &client_, &proxy_, &stats_,
41 shared_bitmap_manager_.get(), NULL, NULL, 1);
42 host_impl_->InitializeRenderer(FakeOutputSurface::Create3d());
44 scoped_ptr<LayerImpl> root_layer = LayerImpl::Create(active_tree(), 1);
45 root_layer->SetHasRenderSurface(true);
46 active_tree()->SetRootLayer(root_layer.Pass());
49 LayerTreeImpl* active_tree() { return host_impl_->active_tree(); }
51 void SetTestName(const std::string& name) { test_name_ = name; }
53 void PrintResults() {
54 CHECK(!test_name_.empty()) << "Must SetTestName() before AfterTest().";
55 perf_test::PrintResult("occlusion_tracker_time",
56 "",
57 test_name_,
58 1000 * timer_.MsPerLap(),
59 "us",
60 true);
63 protected:
64 LapTimer timer_;
65 std::string test_name_;
66 FakeLayerTreeHostImplClient client_;
67 FakeProxy proxy_;
68 DebugScopedSetImplThread impl_;
69 FakeRenderingStatsInstrumentation stats_;
70 scoped_ptr<SharedBitmapManager> shared_bitmap_manager_;
71 scoped_ptr<LayerTreeHostImpl> host_impl_;
74 TEST_F(OcclusionTrackerPerfTest, UnoccludedContentRect_FullyOccluded) {
75 SetTestName("unoccluded_content_rect_fully_occluded");
77 gfx::Rect viewport_rect(768, 1038);
78 OcclusionTracker<LayerImpl> tracker(viewport_rect);
80 CreateHost();
81 host_impl_->SetViewportSize(viewport_rect.size());
83 scoped_ptr<SolidColorLayerImpl> opaque_layer =
84 SolidColorLayerImpl::Create(active_tree(), 2);
85 opaque_layer->SetBackgroundColor(SK_ColorRED);
86 opaque_layer->SetContentsOpaque(true);
87 opaque_layer->SetDrawsContent(true);
88 opaque_layer->SetBounds(viewport_rect.size());
89 opaque_layer->SetContentBounds(viewport_rect.size());
90 active_tree()->root_layer()->AddChild(opaque_layer.Pass());
92 bool update_lcd_text = false;
93 active_tree()->UpdateDrawProperties(update_lcd_text);
94 const LayerImplList& rsll = active_tree()->RenderSurfaceLayerList();
95 ASSERT_EQ(1u, rsll.size());
96 EXPECT_EQ(1u, rsll[0]->render_surface()->layer_list().size());
98 LayerIterator<LayerImpl> begin = LayerIterator<LayerImpl>::Begin(&rsll);
99 LayerIterator<LayerImpl> end = LayerIterator<LayerImpl>::End(&rsll);
101 LayerIteratorPosition<LayerImpl> pos = begin;
103 // The opaque_layer adds occlusion over the whole viewport.
104 tracker.EnterLayer(pos);
105 tracker.LeaveLayer(pos);
107 gfx::Transform transform_to_target;
108 transform_to_target.Translate(0, 96);
110 do {
111 Occlusion occlusion =
112 tracker.GetCurrentOcclusionForLayer(transform_to_target);
113 for (int x = 0; x < viewport_rect.width(); x += 256) {
114 for (int y = 0; y < viewport_rect.height(); y += 256) {
115 gfx::Rect query_content_rect(x, y, 256, 256);
116 gfx::Rect unoccluded =
117 occlusion.GetUnoccludedContentRect(query_content_rect);
118 // Sanity test that we're not hitting early outs.
119 bool expect_empty =
120 query_content_rect.right() <= viewport_rect.width() &&
121 query_content_rect.bottom() + 96 <= viewport_rect.height();
122 CHECK_EQ(expect_empty, unoccluded.IsEmpty())
123 << query_content_rect.ToString();
127 timer_.NextLap();
128 } while (!timer_.HasTimeLimitExpired());
130 ++begin;
131 LayerIteratorPosition<LayerImpl> next = begin;
132 EXPECT_EQ(active_tree()->root_layer(), next.current_layer);
134 ++begin;
135 EXPECT_EQ(end, begin);
137 PrintResults();
140 TEST_F(OcclusionTrackerPerfTest, UnoccludedContentRect_10OpaqueLayers) {
141 static const int kNumOpaqueLayers = 10;
142 SetTestName("unoccluded_content_rect_10_opaque_layers");
144 gfx::Rect viewport_rect(768, 1038);
145 OcclusionTracker<LayerImpl> tracker(viewport_rect);
147 CreateHost();
148 host_impl_->SetViewportSize(viewport_rect.size());
150 for (int i = 0; i < kNumOpaqueLayers; ++i) {
151 scoped_ptr<SolidColorLayerImpl> opaque_layer =
152 SolidColorLayerImpl::Create(active_tree(), 2 + i);
153 opaque_layer->SetBackgroundColor(SK_ColorRED);
154 opaque_layer->SetContentsOpaque(true);
155 opaque_layer->SetDrawsContent(true);
156 opaque_layer->SetBounds(
157 gfx::Size(viewport_rect.width() / 2, viewport_rect.height() / 2));
158 opaque_layer->SetContentBounds(
159 gfx::Size(viewport_rect.width() / 2, viewport_rect.height() / 2));
160 opaque_layer->SetPosition(gfx::Point(i, i));
161 active_tree()->root_layer()->AddChild(opaque_layer.Pass());
164 bool update_lcd_text = false;
165 active_tree()->UpdateDrawProperties(update_lcd_text);
166 const LayerImplList& rsll = active_tree()->RenderSurfaceLayerList();
167 ASSERT_EQ(1u, rsll.size());
168 EXPECT_EQ(static_cast<size_t>(kNumOpaqueLayers),
169 rsll[0]->render_surface()->layer_list().size());
171 LayerIterator<LayerImpl> begin = LayerIterator<LayerImpl>::Begin(&rsll);
172 LayerIterator<LayerImpl> end = LayerIterator<LayerImpl>::End(&rsll);
174 // The opaque_layers add occlusion.
175 for (int i = 0; i < kNumOpaqueLayers - 1; ++i) {
176 LayerIteratorPosition<LayerImpl> pos = begin;
177 tracker.EnterLayer(pos);
178 tracker.LeaveLayer(pos);
179 ++begin;
181 LayerIteratorPosition<LayerImpl> pos = begin;
182 tracker.EnterLayer(pos);
183 tracker.LeaveLayer(pos);
185 gfx::Transform transform_to_target;
186 transform_to_target.Translate(0, 96);
188 do {
189 Occlusion occlusion =
190 tracker.GetCurrentOcclusionForLayer(transform_to_target);
191 for (int x = 0; x < viewport_rect.width(); x += 256) {
192 for (int y = 0; y < viewport_rect.height(); y += 256) {
193 gfx::Rect query_content_rect(x, y, 256, 256);
194 gfx::Rect unoccluded =
195 occlusion.GetUnoccludedContentRect(query_content_rect);
199 timer_.NextLap();
200 } while (!timer_.HasTimeLimitExpired());
202 ++begin;
203 LayerIteratorPosition<LayerImpl> next = begin;
204 EXPECT_EQ(active_tree()->root_layer(), next.current_layer);
206 ++begin;
207 EXPECT_EQ(end, begin);
209 PrintResults();
212 } // namespace
213 } // namespace cc