[email protected] | 666d7cf | 2013-10-12 01:30:29 | [diff] [blame] | 1 | // Copyright 2013 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 "base/callback.h" |
| 6 | #include "base/memory/scoped_ptr.h" |
| 7 | #include "cc/debug/micro_benchmark.h" |
| 8 | #include "cc/debug/micro_benchmark_controller.h" |
| 9 | #include "cc/layers/layer.h" |
| 10 | #include "cc/resources/resource_update_queue.h" |
| 11 | #include "cc/test/fake_layer_tree_host.h" |
[email protected] | 5e5648a | 2013-11-18 00:39:33 | [diff] [blame] | 12 | #include "cc/test/fake_layer_tree_host_impl.h" |
[email protected] | 666d7cf | 2013-10-12 01:30:29 | [diff] [blame] | 13 | #include "cc/test/fake_proxy.h" |
| 14 | #include "testing/gtest/include/gtest/gtest.h" |
| 15 | |
| 16 | namespace cc { |
| 17 | namespace { |
| 18 | |
| 19 | class MicroBenchmarkControllerTest : public testing::Test { |
| 20 | public: |
enne | 2097cab | 2014-09-25 20:16:31 | [diff] [blame] | 21 | MicroBenchmarkControllerTest() |
| 22 | : layer_tree_host_client_(FakeLayerTreeHostClient::DIRECT_3D) {} |
| 23 | |
mostynb | f68776d8 | 2014-10-06 18:07:37 | [diff] [blame^] | 24 | virtual void SetUp() override { |
[email protected] | 5e5648a | 2013-11-18 00:39:33 | [diff] [blame] | 25 | impl_proxy_ = make_scoped_ptr(new FakeImplProxy); |
[email protected] | 4e2eb35 | 2014-03-20 17:25:45 | [diff] [blame] | 26 | shared_bitmap_manager_.reset(new TestSharedBitmapManager()); |
| 27 | layer_tree_host_impl_ = make_scoped_ptr(new FakeLayerTreeHostImpl( |
| 28 | impl_proxy_.get(), shared_bitmap_manager_.get())); |
[email protected] | 5e5648a | 2013-11-18 00:39:33 | [diff] [blame] | 29 | |
enne | 2097cab | 2014-09-25 20:16:31 | [diff] [blame] | 30 | layer_tree_host_ = FakeLayerTreeHost::Create(&layer_tree_host_client_); |
[email protected] | 666d7cf | 2013-10-12 01:30:29 | [diff] [blame] | 31 | layer_tree_host_->SetRootLayer(Layer::Create()); |
[email protected] | 5e5648a | 2013-11-18 00:39:33 | [diff] [blame] | 32 | layer_tree_host_->InitializeForTesting(scoped_ptr<Proxy>(new FakeProxy)); |
| 33 | } |
| 34 | |
mostynb | f68776d8 | 2014-10-06 18:07:37 | [diff] [blame^] | 35 | virtual void TearDown() override { |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 36 | layer_tree_host_impl_ = nullptr; |
| 37 | layer_tree_host_ = nullptr; |
| 38 | impl_proxy_ = nullptr; |
[email protected] | 666d7cf | 2013-10-12 01:30:29 | [diff] [blame] | 39 | } |
| 40 | |
enne | 2097cab | 2014-09-25 20:16:31 | [diff] [blame] | 41 | FakeLayerTreeHostClient layer_tree_host_client_; |
[email protected] | 666d7cf | 2013-10-12 01:30:29 | [diff] [blame] | 42 | scoped_ptr<FakeLayerTreeHost> layer_tree_host_; |
[email protected] | 4e2eb35 | 2014-03-20 17:25:45 | [diff] [blame] | 43 | scoped_ptr<SharedBitmapManager> shared_bitmap_manager_; |
[email protected] | 5e5648a | 2013-11-18 00:39:33 | [diff] [blame] | 44 | scoped_ptr<FakeLayerTreeHostImpl> layer_tree_host_impl_; |
| 45 | scoped_ptr<FakeImplProxy> impl_proxy_; |
[email protected] | 666d7cf | 2013-10-12 01:30:29 | [diff] [blame] | 46 | }; |
| 47 | |
| 48 | void Noop(scoped_ptr<base::Value> value) { |
| 49 | } |
| 50 | |
| 51 | void IncrementCallCount(int* count, scoped_ptr<base::Value> value) { |
| 52 | ++(*count); |
| 53 | } |
| 54 | |
| 55 | TEST_F(MicroBenchmarkControllerTest, ScheduleFail) { |
[email protected] | 67e703e1 | 2014-05-30 05:31:51 | [diff] [blame] | 56 | int id = layer_tree_host_->ScheduleMicroBenchmark( |
[email protected] | 752e2cf | 2013-10-21 21:41:15 | [diff] [blame] | 57 | "non_existant_benchmark", scoped_ptr<base::Value>(), base::Bind(&Noop)); |
[email protected] | 67e703e1 | 2014-05-30 05:31:51 | [diff] [blame] | 58 | EXPECT_EQ(id, 0); |
[email protected] | 666d7cf | 2013-10-12 01:30:29 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | TEST_F(MicroBenchmarkControllerTest, CommitScheduled) { |
| 62 | EXPECT_FALSE(layer_tree_host_->needs_commit()); |
[email protected] | 67e703e1 | 2014-05-30 05:31:51 | [diff] [blame] | 63 | int id = layer_tree_host_->ScheduleMicroBenchmark( |
[email protected] | 752e2cf | 2013-10-21 21:41:15 | [diff] [blame] | 64 | "unittest_only_benchmark", scoped_ptr<base::Value>(), base::Bind(&Noop)); |
[email protected] | 67e703e1 | 2014-05-30 05:31:51 | [diff] [blame] | 65 | EXPECT_GT(id, 0); |
[email protected] | 666d7cf | 2013-10-12 01:30:29 | [diff] [blame] | 66 | EXPECT_TRUE(layer_tree_host_->needs_commit()); |
| 67 | } |
| 68 | |
| 69 | TEST_F(MicroBenchmarkControllerTest, BenchmarkRan) { |
| 70 | int run_count = 0; |
[email protected] | 67e703e1 | 2014-05-30 05:31:51 | [diff] [blame] | 71 | int id = layer_tree_host_->ScheduleMicroBenchmark( |
[email protected] | 666d7cf | 2013-10-12 01:30:29 | [diff] [blame] | 72 | "unittest_only_benchmark", |
[email protected] | 752e2cf | 2013-10-21 21:41:15 | [diff] [blame] | 73 | scoped_ptr<base::Value>(), |
[email protected] | 666d7cf | 2013-10-12 01:30:29 | [diff] [blame] | 74 | base::Bind(&IncrementCallCount, base::Unretained(&run_count))); |
[email protected] | 67e703e1 | 2014-05-30 05:31:51 | [diff] [blame] | 75 | EXPECT_GT(id, 0); |
[email protected] | 666d7cf | 2013-10-12 01:30:29 | [diff] [blame] | 76 | |
| 77 | scoped_ptr<ResourceUpdateQueue> queue(new ResourceUpdateQueue); |
| 78 | layer_tree_host_->SetOutputSurfaceLostForTesting(false); |
| 79 | layer_tree_host_->UpdateLayers(queue.get()); |
| 80 | |
| 81 | EXPECT_EQ(1, run_count); |
| 82 | } |
| 83 | |
| 84 | TEST_F(MicroBenchmarkControllerTest, MultipleBenchmarkRan) { |
| 85 | int run_count = 0; |
[email protected] | 67e703e1 | 2014-05-30 05:31:51 | [diff] [blame] | 86 | int id = layer_tree_host_->ScheduleMicroBenchmark( |
[email protected] | 666d7cf | 2013-10-12 01:30:29 | [diff] [blame] | 87 | "unittest_only_benchmark", |
[email protected] | 752e2cf | 2013-10-21 21:41:15 | [diff] [blame] | 88 | scoped_ptr<base::Value>(), |
[email protected] | 666d7cf | 2013-10-12 01:30:29 | [diff] [blame] | 89 | base::Bind(&IncrementCallCount, base::Unretained(&run_count))); |
[email protected] | 67e703e1 | 2014-05-30 05:31:51 | [diff] [blame] | 90 | EXPECT_GT(id, 0); |
| 91 | id = layer_tree_host_->ScheduleMicroBenchmark( |
[email protected] | 666d7cf | 2013-10-12 01:30:29 | [diff] [blame] | 92 | "unittest_only_benchmark", |
[email protected] | 752e2cf | 2013-10-21 21:41:15 | [diff] [blame] | 93 | scoped_ptr<base::Value>(), |
[email protected] | 666d7cf | 2013-10-12 01:30:29 | [diff] [blame] | 94 | base::Bind(&IncrementCallCount, base::Unretained(&run_count))); |
[email protected] | 67e703e1 | 2014-05-30 05:31:51 | [diff] [blame] | 95 | EXPECT_GT(id, 0); |
[email protected] | 666d7cf | 2013-10-12 01:30:29 | [diff] [blame] | 96 | |
| 97 | scoped_ptr<ResourceUpdateQueue> queue(new ResourceUpdateQueue); |
| 98 | layer_tree_host_->SetOutputSurfaceLostForTesting(false); |
| 99 | layer_tree_host_->UpdateLayers(queue.get()); |
| 100 | |
| 101 | EXPECT_EQ(2, run_count); |
| 102 | |
[email protected] | 67e703e1 | 2014-05-30 05:31:51 | [diff] [blame] | 103 | id = layer_tree_host_->ScheduleMicroBenchmark( |
[email protected] | 666d7cf | 2013-10-12 01:30:29 | [diff] [blame] | 104 | "unittest_only_benchmark", |
[email protected] | 752e2cf | 2013-10-21 21:41:15 | [diff] [blame] | 105 | scoped_ptr<base::Value>(), |
[email protected] | 666d7cf | 2013-10-12 01:30:29 | [diff] [blame] | 106 | base::Bind(&IncrementCallCount, base::Unretained(&run_count))); |
[email protected] | 67e703e1 | 2014-05-30 05:31:51 | [diff] [blame] | 107 | EXPECT_GT(id, 0); |
| 108 | id = layer_tree_host_->ScheduleMicroBenchmark( |
[email protected] | 666d7cf | 2013-10-12 01:30:29 | [diff] [blame] | 109 | "unittest_only_benchmark", |
[email protected] | 752e2cf | 2013-10-21 21:41:15 | [diff] [blame] | 110 | scoped_ptr<base::Value>(), |
[email protected] | 666d7cf | 2013-10-12 01:30:29 | [diff] [blame] | 111 | base::Bind(&IncrementCallCount, base::Unretained(&run_count))); |
[email protected] | 67e703e1 | 2014-05-30 05:31:51 | [diff] [blame] | 112 | EXPECT_GT(id, 0); |
[email protected] | 666d7cf | 2013-10-12 01:30:29 | [diff] [blame] | 113 | |
| 114 | layer_tree_host_->UpdateLayers(queue.get()); |
| 115 | EXPECT_EQ(4, run_count); |
| 116 | |
| 117 | layer_tree_host_->UpdateLayers(queue.get()); |
| 118 | EXPECT_EQ(4, run_count); |
| 119 | } |
| 120 | |
[email protected] | 5e5648a | 2013-11-18 00:39:33 | [diff] [blame] | 121 | TEST_F(MicroBenchmarkControllerTest, BenchmarkImplRan) { |
| 122 | int run_count = 0; |
| 123 | scoped_ptr<base::DictionaryValue> settings(new base::DictionaryValue); |
| 124 | settings->SetBoolean("run_benchmark_impl", true); |
| 125 | |
| 126 | // Schedule a main thread benchmark. |
[email protected] | 67e703e1 | 2014-05-30 05:31:51 | [diff] [blame] | 127 | int id = layer_tree_host_->ScheduleMicroBenchmark( |
[email protected] | 5e5648a | 2013-11-18 00:39:33 | [diff] [blame] | 128 | "unittest_only_benchmark", |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 129 | settings.Pass(), |
[email protected] | 5e5648a | 2013-11-18 00:39:33 | [diff] [blame] | 130 | base::Bind(&IncrementCallCount, base::Unretained(&run_count))); |
[email protected] | 67e703e1 | 2014-05-30 05:31:51 | [diff] [blame] | 131 | EXPECT_GT(id, 0); |
[email protected] | 5e5648a | 2013-11-18 00:39:33 | [diff] [blame] | 132 | |
| 133 | // Schedule impl benchmarks. In production code, this is run in commit. |
| 134 | layer_tree_host_->GetMicroBenchmarkController()->ScheduleImplBenchmarks( |
| 135 | layer_tree_host_impl_.get()); |
| 136 | |
| 137 | // Now complete the commit (as if on the impl thread). |
| 138 | layer_tree_host_impl_->CommitComplete(); |
| 139 | |
| 140 | // Make sure all posted messages run. |
| 141 | base::MessageLoop::current()->RunUntilIdle(); |
| 142 | |
| 143 | EXPECT_EQ(1, run_count); |
| 144 | } |
| 145 | |
[email protected] | 67e703e1 | 2014-05-30 05:31:51 | [diff] [blame] | 146 | TEST_F(MicroBenchmarkControllerTest, SendMessage) { |
| 147 | // Send valid message to invalid benchmark (id = 0) |
| 148 | scoped_ptr<base::DictionaryValue> message(new base::DictionaryValue); |
| 149 | message->SetBoolean("can_handle", true); |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 150 | bool message_handled = |
| 151 | layer_tree_host_->SendMessageToMicroBenchmark(0, message.Pass()); |
[email protected] | 67e703e1 | 2014-05-30 05:31:51 | [diff] [blame] | 152 | EXPECT_FALSE(message_handled); |
| 153 | |
| 154 | // Schedule a benchmark |
| 155 | int run_count = 0; |
| 156 | int id = layer_tree_host_->ScheduleMicroBenchmark( |
| 157 | "unittest_only_benchmark", |
| 158 | scoped_ptr<base::Value>(), |
| 159 | base::Bind(&IncrementCallCount, base::Unretained(&run_count))); |
| 160 | EXPECT_GT(id, 0); |
| 161 | |
| 162 | // Send valid message to valid benchmark |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 163 | message = make_scoped_ptr(new base::DictionaryValue); |
[email protected] | 67e703e1 | 2014-05-30 05:31:51 | [diff] [blame] | 164 | message->SetBoolean("can_handle", true); |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 165 | message_handled = |
| 166 | layer_tree_host_->SendMessageToMicroBenchmark(id, message.Pass()); |
[email protected] | 67e703e1 | 2014-05-30 05:31:51 | [diff] [blame] | 167 | EXPECT_TRUE(message_handled); |
| 168 | |
| 169 | // Send invalid message to valid benchmark |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 170 | message = make_scoped_ptr(new base::DictionaryValue); |
[email protected] | 67e703e1 | 2014-05-30 05:31:51 | [diff] [blame] | 171 | message->SetBoolean("can_handle", false); |
danakj | f446a07 | 2014-09-27 21:55:48 | [diff] [blame] | 172 | message_handled = |
| 173 | layer_tree_host_->SendMessageToMicroBenchmark(id, message.Pass()); |
[email protected] | 67e703e1 | 2014-05-30 05:31:51 | [diff] [blame] | 174 | EXPECT_FALSE(message_handled); |
| 175 | } |
| 176 | |
[email protected] | 666d7cf | 2013-10-12 01:30:29 | [diff] [blame] | 177 | } // namespace |
| 178 | } // namespace cc |