| 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 "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
| 8 #include "base/debug/trace_event.h" | 8 #include "base/debug/trace_event.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "cc/base/thread.h" | |
| 11 | 10 |
| 12 namespace cc { | 11 namespace cc { |
| 13 | 12 |
| 14 Scheduler::Scheduler(SchedulerClient* client, | 13 Scheduler::Scheduler(SchedulerClient* client, |
| 14 scoped_ptr<FrameRateController> frame_rate_controller, |
| 15 const SchedulerSettings& scheduler_settings) | 15 const SchedulerSettings& scheduler_settings) |
| 16 : settings_(scheduler_settings), | 16 : settings_(scheduler_settings), |
| 17 client_(client), | 17 client_(client), |
| 18 weak_factory_(this), | 18 frame_rate_controller_(frame_rate_controller.Pass()), |
| 19 last_set_needs_begin_frame_(false), | |
| 20 has_pending_begin_frame_(false), | |
| 21 last_begin_frame_time_(base::TimeTicks()), | |
| 22 // TODO(brianderson): Pass with BeginFrame in the near future. | |
| 23 interval_(base::TimeDelta::FromMicroseconds(16666)), | |
| 24 state_machine_(scheduler_settings), | 19 state_machine_(scheduler_settings), |
| 25 inside_process_scheduled_actions_(false) { | 20 inside_process_scheduled_actions_(false) { |
| 26 DCHECK(client_); | 21 DCHECK(client_); |
| 22 frame_rate_controller_->SetClient(this); |
| 27 DCHECK(!state_machine_.BeginFrameNeededByImplThread()); | 23 DCHECK(!state_machine_.BeginFrameNeededByImplThread()); |
| 28 } | 24 } |
| 29 | 25 |
| 30 Scheduler::~Scheduler() { | 26 Scheduler::~Scheduler() { frame_rate_controller_->SetActive(false); } |
| 31 client_->SetNeedsBeginFrameOnImplThread(false); | |
| 32 } | |
| 33 | 27 |
| 34 void Scheduler::SetCanStart() { | 28 void Scheduler::SetCanStart() { |
| 35 state_machine_.SetCanStart(); | 29 state_machine_.SetCanStart(); |
| 36 ProcessScheduledActions(); | 30 ProcessScheduledActions(); |
| 37 } | 31 } |
| 38 | 32 |
| 39 void Scheduler::SetVisible(bool visible) { | 33 void Scheduler::SetVisible(bool visible) { |
| 40 state_machine_.SetVisible(visible); | 34 state_machine_.SetVisible(visible); |
| 41 ProcessScheduledActions(); | 35 ProcessScheduledActions(); |
| 42 } | 36 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 state_machine_.FinishCommit(); | 81 state_machine_.FinishCommit(); |
| 88 ProcessScheduledActions(); | 82 ProcessScheduledActions(); |
| 89 } | 83 } |
| 90 | 84 |
| 91 void Scheduler::BeginFrameAbortedByMainThread() { | 85 void Scheduler::BeginFrameAbortedByMainThread() { |
| 92 TRACE_EVENT0("cc", "Scheduler::BeginFrameAbortedByMainThread"); | 86 TRACE_EVENT0("cc", "Scheduler::BeginFrameAbortedByMainThread"); |
| 93 state_machine_.BeginFrameAbortedByMainThread(); | 87 state_machine_.BeginFrameAbortedByMainThread(); |
| 94 ProcessScheduledActions(); | 88 ProcessScheduledActions(); |
| 95 } | 89 } |
| 96 | 90 |
| 91 void Scheduler::SetMaxFramesPending(int max_frames_pending) { |
| 92 frame_rate_controller_->SetMaxFramesPending(max_frames_pending); |
| 93 } |
| 94 |
| 95 int Scheduler::MaxFramesPending() const { |
| 96 return frame_rate_controller_->MaxFramesPending(); |
| 97 } |
| 98 |
| 99 int Scheduler::NumFramesPendingForTesting() const { |
| 100 return frame_rate_controller_->NumFramesPendingForTesting(); |
| 101 } |
| 102 |
| 103 void Scheduler::DidSwapBuffersComplete() { |
| 104 TRACE_EVENT0("cc", "Scheduler::DidSwapBuffersComplete"); |
| 105 frame_rate_controller_->DidSwapBuffersComplete(); |
| 106 } |
| 107 |
| 97 void Scheduler::DidLoseOutputSurface() { | 108 void Scheduler::DidLoseOutputSurface() { |
| 98 TRACE_EVENT0("cc", "Scheduler::DidLoseOutputSurface"); | 109 TRACE_EVENT0("cc", "Scheduler::DidLoseOutputSurface"); |
| 99 state_machine_.DidLoseOutputSurface(); | 110 state_machine_.DidLoseOutputSurface(); |
| 100 ProcessScheduledActions(); | 111 ProcessScheduledActions(); |
| 101 } | 112 } |
| 102 | 113 |
| 103 void Scheduler::DidCreateAndInitializeOutputSurface() { | 114 void Scheduler::DidCreateAndInitializeOutputSurface() { |
| 104 TRACE_EVENT0("cc", "Scheduler::DidCreateAndInitializeOutputSurface"); | 115 TRACE_EVENT0("cc", "Scheduler::DidCreateAndInitializeOutputSurface"); |
| 116 frame_rate_controller_->DidAbortAllPendingFrames(); |
| 105 state_machine_.DidCreateAndInitializeOutputSurface(); | 117 state_machine_.DidCreateAndInitializeOutputSurface(); |
| 106 has_pending_begin_frame_ = false; | |
| 107 last_set_needs_begin_frame_ = false; | |
| 108 ProcessScheduledActions(); | 118 ProcessScheduledActions(); |
| 109 } | 119 } |
| 110 | 120 |
| 121 void Scheduler::SetTimebaseAndInterval(base::TimeTicks timebase, |
| 122 base::TimeDelta interval) { |
| 123 frame_rate_controller_->SetTimebaseAndInterval(timebase, interval); |
| 124 } |
| 125 |
| 111 base::TimeTicks Scheduler::AnticipatedDrawTime() { | 126 base::TimeTicks Scheduler::AnticipatedDrawTime() { |
| 112 TRACE_EVENT0("cc", "Scheduler::AnticipatedDrawTime"); | 127 return frame_rate_controller_->NextTickTime(); |
| 113 base::TimeTicks now = base::TimeTicks::Now(); | |
| 114 int64 intervals = ((now - last_begin_frame_time_) / interval_) + 1; | |
| 115 return last_begin_frame_time_ + (interval_ * intervals); | |
| 116 } | 128 } |
| 117 | 129 |
| 118 base::TimeTicks Scheduler::LastBeginFrameOnImplThreadTime() { | 130 base::TimeTicks Scheduler::LastBeginFrameOnImplThreadTime() { |
| 119 return last_begin_frame_time_; | 131 return frame_rate_controller_->LastTickTime(); |
| 120 } | 132 } |
| 121 | 133 |
| 122 void Scheduler::SetupNextBeginFrameIfNeeded() { | 134 void Scheduler::BeginFrame(bool throttled) { |
| 123 // Determine if we need BeginFrame notifications. | 135 TRACE_EVENT1("cc", "Scheduler::BeginFrame", "throttled", throttled); |
| 124 // If we do, always request the BeginFrame immediately. | 136 if (!throttled) |
| 125 // If not, only disable on the next BeginFrame to avoid unnecessary toggles. | 137 state_machine_.DidEnterBeginFrame(); |
| 126 // The synchronous renderer compositor requires immediate disables though. | |
| 127 bool needs_begin_frame = state_machine_.BeginFrameNeededByImplThread(); | |
| 128 if ((needs_begin_frame || | |
| 129 state_machine_.inside_begin_frame() || | |
| 130 settings_.using_synchronous_renderer_compositor) && | |
| 131 (needs_begin_frame != last_set_needs_begin_frame_)) { | |
| 132 client_->SetNeedsBeginFrameOnImplThread(needs_begin_frame); | |
| 133 last_set_needs_begin_frame_ = needs_begin_frame; | |
| 134 } | |
| 135 | |
| 136 // Request another BeginFrame if we haven't drawn for now until we have | |
| 137 // deadlines implemented. | |
| 138 if (state_machine_.inside_begin_frame() && has_pending_begin_frame_) { | |
| 139 has_pending_begin_frame_ = false; | |
| 140 client_->SetNeedsBeginFrameOnImplThread(true); | |
| 141 } | |
| 142 } | |
| 143 | |
| 144 void Scheduler::BeginFrame(base::TimeTicks frame_time) { | |
| 145 TRACE_EVENT0("cc", "Scheduler::BeginFrame"); | |
| 146 DCHECK(!has_pending_begin_frame_); | |
| 147 has_pending_begin_frame_ = true; | |
| 148 last_begin_frame_time_ = frame_time; | |
| 149 state_machine_.DidEnterBeginFrame(); | |
| 150 state_machine_.SetFrameTime(frame_time); | |
| 151 ProcessScheduledActions(); | 138 ProcessScheduledActions(); |
| 152 state_machine_.DidLeaveBeginFrame(); | 139 if (!throttled) |
| 153 } | 140 state_machine_.DidLeaveBeginFrame(); |
| 154 | |
| 155 void Scheduler::DrawAndSwapIfPossible() { | |
| 156 ScheduledActionDrawAndSwapResult result = | |
| 157 client_->ScheduledActionDrawAndSwapIfPossible(); | |
| 158 state_machine_.DidDrawIfPossibleCompleted(result.did_draw); | |
| 159 if (result.did_swap) | |
| 160 has_pending_begin_frame_ = false; | |
| 161 } | |
| 162 | |
| 163 void Scheduler::DrawAndSwapForced() { | |
| 164 ScheduledActionDrawAndSwapResult result = | |
| 165 client_->ScheduledActionDrawAndSwapForced(); | |
| 166 if (result.did_swap) | |
| 167 has_pending_begin_frame_ = false; | |
| 168 } | 141 } |
| 169 | 142 |
| 170 void Scheduler::ProcessScheduledActions() { | 143 void Scheduler::ProcessScheduledActions() { |
| 171 // We do not allow ProcessScheduledActions to be recursive. | 144 // We do not allow ProcessScheduledActions to be recursive. |
| 172 // The top-level call will iteratively execute the next action for us anyway. | 145 // The top-level call will iteratively execute the next action for us anyway. |
| 173 if (inside_process_scheduled_actions_) | 146 if (inside_process_scheduled_actions_) |
| 174 return; | 147 return; |
| 175 | 148 |
| 176 base::AutoReset<bool> mark_inside(&inside_process_scheduled_actions_, true); | 149 base::AutoReset<bool> mark_inside(&inside_process_scheduled_actions_, true); |
| 177 | 150 |
| 178 SchedulerStateMachine::Action action = state_machine_.NextAction(); | 151 SchedulerStateMachine::Action action = state_machine_.NextAction(); |
| 179 while (action != SchedulerStateMachine::ACTION_NONE) { | 152 while (action != SchedulerStateMachine::ACTION_NONE) { |
| 180 state_machine_.UpdateState(action); | 153 state_machine_.UpdateState(action); |
| 154 TRACE_EVENT1( |
| 155 "cc", "Scheduler::ProcessScheduledActions()", "action", action); |
| 156 |
| 181 switch (action) { | 157 switch (action) { |
| 182 case SchedulerStateMachine::ACTION_NONE: | 158 case SchedulerStateMachine::ACTION_NONE: |
| 183 break; | 159 break; |
| 184 case SchedulerStateMachine::ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD: | 160 case SchedulerStateMachine::ACTION_SEND_BEGIN_FRAME_TO_MAIN_THREAD: |
| 185 client_->ScheduledActionSendBeginFrameToMainThread(); | 161 client_->ScheduledActionSendBeginFrameToMainThread(); |
| 186 break; | 162 break; |
| 187 case SchedulerStateMachine::ACTION_COMMIT: | 163 case SchedulerStateMachine::ACTION_COMMIT: |
| 188 client_->ScheduledActionCommit(); | 164 client_->ScheduledActionCommit(); |
| 189 break; | 165 break; |
| 190 case SchedulerStateMachine::ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS: | 166 case SchedulerStateMachine::ACTION_CHECK_FOR_COMPLETED_TILE_UPLOADS: |
| 191 client_->ScheduledActionCheckForCompletedTileUploads(); | 167 client_->ScheduledActionCheckForCompletedTileUploads(); |
| 192 break; | 168 break; |
| 193 case SchedulerStateMachine::ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED: | 169 case SchedulerStateMachine::ACTION_ACTIVATE_PENDING_TREE_IF_NEEDED: |
| 194 client_->ScheduledActionActivatePendingTreeIfNeeded(); | 170 client_->ScheduledActionActivatePendingTreeIfNeeded(); |
| 195 break; | 171 break; |
| 196 case SchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE: | 172 case SchedulerStateMachine::ACTION_DRAW_IF_POSSIBLE: { |
| 197 DrawAndSwapIfPossible(); | 173 frame_rate_controller_->DidSwapBuffers(); |
| 174 ScheduledActionDrawAndSwapResult result = |
| 175 client_->ScheduledActionDrawAndSwapIfPossible(); |
| 176 state_machine_.DidDrawIfPossibleCompleted(result.did_draw); |
| 177 if (!result.did_swap) |
| 178 frame_rate_controller_->DidSwapBuffersComplete(); |
| 198 break; | 179 break; |
| 199 case SchedulerStateMachine::ACTION_DRAW_FORCED: | 180 } |
| 200 DrawAndSwapForced(); | 181 case SchedulerStateMachine::ACTION_DRAW_FORCED: { |
| 182 frame_rate_controller_->DidSwapBuffers(); |
| 183 ScheduledActionDrawAndSwapResult result = |
| 184 client_->ScheduledActionDrawAndSwapForced(); |
| 185 if (!result.did_swap) |
| 186 frame_rate_controller_->DidSwapBuffersComplete(); |
| 201 break; | 187 break; |
| 188 } |
| 202 case SchedulerStateMachine::ACTION_BEGIN_OUTPUT_SURFACE_CREATION: | 189 case SchedulerStateMachine::ACTION_BEGIN_OUTPUT_SURFACE_CREATION: |
| 203 client_->ScheduledActionBeginOutputSurfaceCreation(); | 190 client_->ScheduledActionBeginOutputSurfaceCreation(); |
| 204 break; | 191 break; |
| 205 case SchedulerStateMachine::ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD: | 192 case SchedulerStateMachine::ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD: |
| 206 client_->ScheduledActionAcquireLayerTexturesForMainThread(); | 193 client_->ScheduledActionAcquireLayerTexturesForMainThread(); |
| 207 break; | 194 break; |
| 208 } | 195 } |
| 209 action = state_machine_.NextAction(); | 196 action = state_machine_.NextAction(); |
| 210 } | 197 } |
| 211 | 198 |
| 212 SetupNextBeginFrameIfNeeded(); | 199 // Activate or deactivate the frame rate controller. |
| 213 client_->DidAnticipatedDrawTimeChange(AnticipatedDrawTime()); | 200 frame_rate_controller_->SetActive( |
| 201 state_machine_.BeginFrameNeededByImplThread()); |
| 202 client_->DidAnticipatedDrawTimeChange(frame_rate_controller_->NextTickTime()); |
| 214 } | 203 } |
| 215 | 204 |
| 216 bool Scheduler::WillDrawIfNeeded() const { | 205 bool Scheduler::WillDrawIfNeeded() const { |
| 217 return !state_machine_.DrawSuspendedUntilCommit(); | 206 return !state_machine_.DrawSuspendedUntilCommit(); |
| 218 } | 207 } |
| 219 | 208 |
| 220 } // namespace cc | 209 } // namespace cc |
| OLD | NEW |