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