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: 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_ = 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 48 LayerTreeImpl* active_tree() { return host_impl_->active_tree(); } 49 50 void SetTestName(const std::string& name) { test_name_ = name; } 51 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 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 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. 115 bool expect_empty = 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(); 120 } 121 } 122 123 timer_.NextLap(); 124 } while (!timer_.HasTimeLimitExpired()); 125 126 ++begin; 127 LayerIteratorPosition<LayerImpl> next = begin; 128 EXPECT_EQ(active_tree()->root_layer(), next.current_layer); 129 130 ++begin; 131 EXPECT_EQ(end, begin); 132 133 PrintResults(); 134 } 135 136 TEST_F(OcclusionTrackerPerfTest, UnoccludedContentRect_10OpaqueLayers) { 137 static const int kNumOpaqueLayers = 10; 138 SetTestName("unoccluded_content_rect_10_opaque_layers"); 139 140 gfx::Rect viewport_rect(768, 1038); 141 OcclusionTracker<LayerImpl> tracker(viewport_rect); 142 143 CreateHost(); 144 host_impl_->SetViewportSize(viewport_rect.size()); 145 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>()); 158 } 159 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()); 165 166 LayerIterator<LayerImpl> begin = LayerIterator<LayerImpl>::Begin(&rsll); 167 LayerIterator<LayerImpl> end = LayerIterator<LayerImpl>::End(&rsll); 168 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); 174 ++begin; 175 } 176 LayerIteratorPosition<LayerImpl> pos = begin; 177 tracker.EnterLayer(pos); 178 tracker.LeaveLayer(pos); 179 180 gfx::Transform transform_to_target; 181 transform_to_target.Translate(0, 96); 182 183 do { 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); 189 } 190 } 191 192 timer_.NextLap(); 193 } while (!timer_.HasTimeLimitExpired()); 194 195 ++begin; 196 LayerIteratorPosition<LayerImpl> next = begin; 197 EXPECT_EQ(active_tree()->root_layer(), next.current_layer); 198 199 ++begin; 200 EXPECT_EQ(end, begin); 201 202 PrintResults(); 203 } 204 205 } // namespace 206 } // namespace cc 207