| OLD | NEW |
| 1 // Copyright 2011 The Chromium Authors. All rights reserved. | 1 // Copyright 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "cc/scheduler/scheduler.h" | 5 #include "cc/scheduler/scheduler.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
| 14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
| 15 #include "base/numerics/safe_conversions.h" | 15 #include "base/numerics/safe_conversions.h" |
| 16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 17 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 18 #include "base/trace_event/trace_event.h" | 18 #include "base/trace_event/trace_event.h" |
| 19 #include "cc/test/begin_frame_args_test.h" | 19 #include "cc/test/begin_frame_args_test.h" |
| 20 #include "cc/test/fake_external_begin_frame_source.h" |
| 20 #include "cc/test/ordered_simple_task_runner.h" | 21 #include "cc/test/ordered_simple_task_runner.h" |
| 21 #include "cc/test/scheduler_test_common.h" | 22 #include "cc/test/scheduler_test_common.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 25 |
| 25 #define EXPECT_ACTION(action, client, action_index, expected_num_actions) \ | 26 #define EXPECT_ACTION(action, client, action_index, expected_num_actions) \ |
| 26 do { \ | 27 do { \ |
| 27 EXPECT_EQ(expected_num_actions, client->num_actions_()); \ | 28 EXPECT_EQ(expected_num_actions, client->num_actions_()); \ |
| 28 if (action_index >= 0) { \ | 29 if (action_index >= 0) { \ |
| 29 ASSERT_LT(action_index, client->num_actions_()) << scheduler_.get(); \ | 30 ASSERT_LT(action_index, client->num_actions_()) << scheduler_.get(); \ |
| (...skipping 14 matching lines...) Expand all Loading... |
| 44 SCOPED_TRACE(""); \ | 45 SCOPED_TRACE(""); \ |
| 45 statements; \ | 46 statements; \ |
| 46 } | 47 } |
| 47 | 48 |
| 48 namespace cc { | 49 namespace cc { |
| 49 namespace { | 50 namespace { |
| 50 | 51 |
| 51 base::TimeDelta kSlowDuration = base::TimeDelta::FromSeconds(1); | 52 base::TimeDelta kSlowDuration = base::TimeDelta::FromSeconds(1); |
| 52 base::TimeDelta kFastDuration = base::TimeDelta::FromMilliseconds(1); | 53 base::TimeDelta kFastDuration = base::TimeDelta::FromMilliseconds(1); |
| 53 | 54 |
| 54 class FakeSchedulerClient : public SchedulerClient { | 55 class FakeSchedulerClient : public SchedulerClient, |
| 56 public FakeExternalBeginFrameSource::Client { |
| 55 public: | 57 public: |
| 56 FakeSchedulerClient() | 58 FakeSchedulerClient() |
| 57 : automatic_swap_ack_(true), | 59 : automatic_swap_ack_(true), |
| 58 scheduler_(nullptr) { | 60 scheduler_(nullptr) { |
| 59 Reset(); | 61 Reset(); |
| 60 } | 62 } |
| 61 | 63 |
| 62 void Reset() { | 64 void Reset() { |
| 63 actions_.clear(); | 65 actions_.clear(); |
| 64 states_.clear(); | 66 states_.clear(); |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 173 return base::Bind(&FakeSchedulerClient::ImplFrameDeadlinePendingCallback, | 175 return base::Bind(&FakeSchedulerClient::ImplFrameDeadlinePendingCallback, |
| 174 base::Unretained(this), | 176 base::Unretained(this), |
| 175 state); | 177 state); |
| 176 } | 178 } |
| 177 | 179 |
| 178 void PushAction(const char* description) { | 180 void PushAction(const char* description) { |
| 179 actions_.push_back(description); | 181 actions_.push_back(description); |
| 180 states_.push_back(scheduler_->AsValue()); | 182 states_.push_back(scheduler_->AsValue()); |
| 181 } | 183 } |
| 182 | 184 |
| 185 // FakeExternalBeginFrameSource::Client implementation. |
| 186 void OnAddObserver(BeginFrameObserver* obs) override { |
| 187 PushAction("AddObserver(this)"); |
| 188 } |
| 189 void OnRemoveObserver(BeginFrameObserver* obs) override { |
| 190 PushAction("RemoveObserver(this)"); |
| 191 } |
| 192 |
| 183 protected: | 193 protected: |
| 184 bool ImplFrameDeadlinePendingCallback(bool state) { | 194 bool ImplFrameDeadlinePendingCallback(bool state) { |
| 185 return scheduler_->BeginImplFrameDeadlinePending() == state; | 195 return scheduler_->BeginImplFrameDeadlinePending() == state; |
| 186 } | 196 } |
| 187 | 197 |
| 188 bool will_begin_impl_frame_causes_redraw_; | 198 bool will_begin_impl_frame_causes_redraw_; |
| 189 bool will_begin_impl_frame_requests_one_begin_impl_frame_; | 199 bool will_begin_impl_frame_requests_one_begin_impl_frame_; |
| 190 bool draw_will_happen_; | 200 bool draw_will_happen_; |
| 191 bool swap_will_happen_if_draw_happens_; | 201 bool swap_will_happen_if_draw_happens_; |
| 192 bool automatic_swap_ack_; | 202 bool automatic_swap_ack_; |
| 193 int num_draws_; | 203 int num_draws_; |
| 194 BeginFrameArgs last_begin_main_frame_args_; | 204 BeginFrameArgs last_begin_main_frame_args_; |
| 195 base::TimeTicks posted_begin_impl_frame_deadline_; | 205 base::TimeTicks posted_begin_impl_frame_deadline_; |
| 196 std::vector<const char*> actions_; | 206 std::vector<const char*> actions_; |
| 197 std::vector<std::unique_ptr<base::trace_event::ConvertableToTraceFormat>> | 207 std::vector<std::unique_ptr<base::trace_event::ConvertableToTraceFormat>> |
| 198 states_; | 208 states_; |
| 199 TestScheduler* scheduler_; | 209 TestScheduler* scheduler_; |
| 200 }; | 210 }; |
| 201 | 211 |
| 202 class FakeExternalBeginFrameSource : public BeginFrameSourceBase { | |
| 203 public: | |
| 204 explicit FakeExternalBeginFrameSource(FakeSchedulerClient* client) | |
| 205 : client_(client) {} | |
| 206 ~FakeExternalBeginFrameSource() override {} | |
| 207 | |
| 208 void AddObserver(BeginFrameObserver* obs) override { | |
| 209 client_->PushAction("AddObserver(this)"); | |
| 210 BeginFrameSourceBase::AddObserver(obs); | |
| 211 } | |
| 212 | |
| 213 void RemoveObserver(BeginFrameObserver* obs) override { | |
| 214 client_->PushAction("RemoveObserver(this)"); | |
| 215 BeginFrameSourceBase::RemoveObserver(obs); | |
| 216 } | |
| 217 | |
| 218 // TODO(sunnyps): Use using CallOnBeginFrame, SetBeginFrameSourcePaused. | |
| 219 void TestOnBeginFrame(const BeginFrameArgs& args) { | |
| 220 return CallOnBeginFrame(args); | |
| 221 } | |
| 222 | |
| 223 void SetPaused(bool paused) { SetBeginFrameSourcePaused(paused); } | |
| 224 | |
| 225 private: | |
| 226 FakeSchedulerClient* client_; | |
| 227 }; | |
| 228 | |
| 229 class SchedulerTest : public testing::Test { | 212 class SchedulerTest : public testing::Test { |
| 230 public: | 213 public: |
| 231 SchedulerTest() | 214 SchedulerTest() |
| 232 : now_src_(new base::SimpleTestTickClock()), | 215 : now_src_(new base::SimpleTestTickClock()), |
| 233 task_runner_(new OrderedSimpleTaskRunner(now_src_.get(), true)), | 216 task_runner_(new OrderedSimpleTaskRunner(now_src_.get(), true)), |
| 234 fake_external_begin_frame_source_(nullptr) { | 217 fake_external_begin_frame_source_(nullptr) { |
| 235 now_src_->Advance(base::TimeDelta::FromMicroseconds(10000)); | 218 now_src_->Advance(base::TimeDelta::FromMicroseconds(10000)); |
| 236 // A bunch of tests require NowTicks() | 219 // A bunch of tests require NowTicks() |
| 237 // to be > BeginFrameArgs::DefaultInterval() | 220 // to be > BeginFrameArgs::DefaultInterval() |
| 238 now_src_->Advance(base::TimeDelta::FromMilliseconds(100)); | 221 now_src_->Advance(base::TimeDelta::FromMilliseconds(100)); |
| 239 // Fail if we need to run 100 tasks in a row. | 222 // Fail if we need to run 100 tasks in a row. |
| 240 task_runner_->SetRunTaskLimit(100); | 223 task_runner_->SetRunTaskLimit(100); |
| 241 } | 224 } |
| 242 | 225 |
| 243 ~SchedulerTest() override {} | 226 ~SchedulerTest() override {} |
| 244 | 227 |
| 245 protected: | 228 protected: |
| 246 TestScheduler* CreateScheduler() { | 229 TestScheduler* CreateScheduler() { |
| 247 BeginFrameSource* frame_source; | 230 BeginFrameSource* frame_source; |
| 248 unthrottled_frame_source_.reset( | 231 unthrottled_frame_source_.reset(new BackToBackBeginFrameSource( |
| 249 new TestBackToBackBeginFrameSource(now_src_.get(), task_runner_.get())); | 232 base::MakeUnique<TestDelayBasedTimeSource>(now_src_.get(), |
| 233 task_runner_.get()))); |
| 250 fake_external_begin_frame_source_.reset( | 234 fake_external_begin_frame_source_.reset( |
| 251 new FakeExternalBeginFrameSource(client_.get())); | 235 new FakeExternalBeginFrameSource(0.f, false)); |
| 252 synthetic_frame_source_.reset(new TestSyntheticBeginFrameSource( | 236 fake_external_begin_frame_source_->SetClient(client_.get()); |
| 253 now_src_.get(), task_runner_.get(), BeginFrameArgs::DefaultInterval())); | 237 synthetic_frame_source_.reset(new DelayBasedBeginFrameSource( |
| 238 base::MakeUnique<TestDelayBasedTimeSource>(now_src_.get(), |
| 239 task_runner_.get()))); |
| 254 if (!scheduler_settings_.throttle_frame_production) { | 240 if (!scheduler_settings_.throttle_frame_production) { |
| 255 frame_source = unthrottled_frame_source_.get(); | 241 frame_source = unthrottled_frame_source_.get(); |
| 256 } else if (scheduler_settings_.use_external_begin_frame_source) { | 242 } else if (scheduler_settings_.use_external_begin_frame_source) { |
| 257 frame_source = fake_external_begin_frame_source_.get(); | 243 frame_source = fake_external_begin_frame_source_.get(); |
| 258 } else { | 244 } else { |
| 259 frame_source = synthetic_frame_source_.get(); | 245 frame_source = synthetic_frame_source_.get(); |
| 260 } | 246 } |
| 261 | 247 |
| 262 std::unique_ptr<FakeCompositorTimingHistory> | 248 std::unique_ptr<FakeCompositorTimingHistory> |
| 263 fake_compositor_timing_history = FakeCompositorTimingHistory::Create( | 249 fake_compositor_timing_history = FakeCompositorTimingHistory::Create( |
| (...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 422 bool use_external_begin_frame_source, | 408 bool use_external_begin_frame_source, |
| 423 bool throttle_frame_production); | 409 bool throttle_frame_production); |
| 424 bool BeginMainFrameOnCriticalPath(TreePriority tree_priority, | 410 bool BeginMainFrameOnCriticalPath(TreePriority tree_priority, |
| 425 ScrollHandlerState scroll_handler_state, | 411 ScrollHandlerState scroll_handler_state, |
| 426 base::TimeDelta durations); | 412 base::TimeDelta durations); |
| 427 | 413 |
| 428 std::unique_ptr<base::SimpleTestTickClock> now_src_; | 414 std::unique_ptr<base::SimpleTestTickClock> now_src_; |
| 429 scoped_refptr<OrderedSimpleTaskRunner> task_runner_; | 415 scoped_refptr<OrderedSimpleTaskRunner> task_runner_; |
| 430 std::unique_ptr<FakeExternalBeginFrameSource> | 416 std::unique_ptr<FakeExternalBeginFrameSource> |
| 431 fake_external_begin_frame_source_; | 417 fake_external_begin_frame_source_; |
| 432 std::unique_ptr<TestSyntheticBeginFrameSource> synthetic_frame_source_; | 418 std::unique_ptr<SyntheticBeginFrameSource> synthetic_frame_source_; |
| 433 std::unique_ptr<TestBackToBackBeginFrameSource> unthrottled_frame_source_; | 419 std::unique_ptr<SyntheticBeginFrameSource> unthrottled_frame_source_; |
| 434 SchedulerSettings scheduler_settings_; | 420 SchedulerSettings scheduler_settings_; |
| 435 std::unique_ptr<FakeSchedulerClient> client_; | 421 std::unique_ptr<FakeSchedulerClient> client_; |
| 436 std::unique_ptr<TestScheduler> scheduler_; | 422 std::unique_ptr<TestScheduler> scheduler_; |
| 437 FakeCompositorTimingHistory* fake_compositor_timing_history_; | 423 FakeCompositorTimingHistory* fake_compositor_timing_history_; |
| 438 }; | 424 }; |
| 439 | 425 |
| 440 TEST_F(SchedulerTest, InitializeOutputSurfaceDoesNotBeginImplFrame) { | 426 TEST_F(SchedulerTest, InitializeOutputSurfaceDoesNotBeginImplFrame) { |
| 441 scheduler_settings_.use_external_begin_frame_source = true; | 427 scheduler_settings_.use_external_begin_frame_source = true; |
| 442 SetUpScheduler(false); | 428 SetUpScheduler(false); |
| 443 scheduler_->SetVisible(true); | 429 scheduler_->SetVisible(true); |
| (...skipping 2318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2762 EXPECT_ACTION("SendBeginMainFrameNotExpectedSoon", client_, 2, 3); | 2748 EXPECT_ACTION("SendBeginMainFrameNotExpectedSoon", client_, 2, 3); |
| 2763 EXPECT_FALSE(scheduler_->BeginImplFrameDeadlinePending()); | 2749 EXPECT_FALSE(scheduler_->BeginImplFrameDeadlinePending()); |
| 2764 EXPECT_FALSE(scheduler_->begin_frames_expected()); | 2750 EXPECT_FALSE(scheduler_->begin_frames_expected()); |
| 2765 | 2751 |
| 2766 // No more BeginRetroFrame because BeginRetroFrame queue is cleared. | 2752 // No more BeginRetroFrame because BeginRetroFrame queue is cleared. |
| 2767 client_->Reset(); | 2753 client_->Reset(); |
| 2768 task_runner().RunPendingTasks(); | 2754 task_runner().RunPendingTasks(); |
| 2769 EXPECT_NO_ACTION(client_); | 2755 EXPECT_NO_ACTION(client_); |
| 2770 } | 2756 } |
| 2771 | 2757 |
| 2772 TEST_F(SchedulerTest, DidLoseOutputSurfaceWithSyntheticBeginFrameSource) { | 2758 TEST_F(SchedulerTest, DidLoseOutputSurfaceWithDelayBasedBeginFrameSource) { |
| 2773 SetUpScheduler(true); | 2759 SetUpScheduler(true); |
| 2774 | 2760 |
| 2775 // SetNeedsBeginMainFrame should begin the frame on the next BeginImplFrame. | 2761 // SetNeedsBeginMainFrame should begin the frame on the next BeginImplFrame. |
| 2776 EXPECT_FALSE(scheduler_->begin_frames_expected()); | 2762 EXPECT_FALSE(scheduler_->begin_frames_expected()); |
| 2777 scheduler_->SetNeedsBeginMainFrame(); | 2763 scheduler_->SetNeedsBeginMainFrame(); |
| 2778 EXPECT_TRUE(scheduler_->begin_frames_expected()); | 2764 EXPECT_TRUE(scheduler_->begin_frames_expected()); |
| 2779 | 2765 |
| 2780 client_->Reset(); | 2766 client_->Reset(); |
| 2781 AdvanceFrame(); | 2767 AdvanceFrame(); |
| 2782 EXPECT_ACTION("WillBeginImplFrame", client_, 0, 2); | 2768 EXPECT_ACTION("WillBeginImplFrame", client_, 0, 2); |
| (...skipping 912 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3695 } | 3681 } |
| 3696 | 3682 |
| 3697 TEST_F(SchedulerTest, BeginMainFrameOnCriticalPath_AHS) { | 3683 TEST_F(SchedulerTest, BeginMainFrameOnCriticalPath_AHS) { |
| 3698 EXPECT_FALSE(BeginMainFrameOnCriticalPath( | 3684 EXPECT_FALSE(BeginMainFrameOnCriticalPath( |
| 3699 SMOOTHNESS_TAKES_PRIORITY, | 3685 SMOOTHNESS_TAKES_PRIORITY, |
| 3700 ScrollHandlerState::SCROLL_AFFECTS_SCROLL_HANDLER, kSlowDuration)); | 3686 ScrollHandlerState::SCROLL_AFFECTS_SCROLL_HANDLER, kSlowDuration)); |
| 3701 } | 3687 } |
| 3702 | 3688 |
| 3703 } // namespace | 3689 } // namespace |
| 3704 } // namespace cc | 3690 } // namespace cc |
| OLD | NEW |