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 3 // found in the LICENSE file. 4 5 #ifndef CC_SCHEDULER_SCHEDULER_H_ 6 #define CC_SCHEDULER_SCHEDULER_H_ 7 8 #include <string> 9 10 #include "base/basictypes.h" 11 #include "base/cancelable_callback.h" 12 #include "base/memory/scoped_ptr.h" 13 #include "base/time/time.h" 14 #include "cc/base/cc_export.h" 15 #include "cc/output/begin_frame_args.h" 16 #include "cc/scheduler/scheduler_settings.h" 17 #include "cc/scheduler/scheduler_state_machine.h" 18 #include "cc/trees/layer_tree_host.h" 19 20 namespace cc { 21 22 class Thread; 23 24 struct DrawSwapReadbackResult { 25 DrawSwapReadbackResult() 26 : did_draw(false), did_swap(false), did_readback(false) {} 27 DrawSwapReadbackResult(bool did_draw, bool did_swap, bool did_readback) 28 : did_draw(did_draw), did_swap(did_swap), did_readback(did_readback) {} 29 bool did_draw; 30 bool did_swap; 31 bool did_readback; 32 }; 33 34 class SchedulerClient { 35 public: 36 virtual void SetNeedsBeginImplFrame(bool enable) = 0; 37 virtual void ScheduledActionSendBeginMainFrame() = 0; 38 virtual DrawSwapReadbackResult ScheduledActionDrawAndSwapIfPossible() = 0; 39 virtual DrawSwapReadbackResult ScheduledActionDrawAndSwapForced() = 0; 40 virtual DrawSwapReadbackResult ScheduledActionDrawAndReadback() = 0; 41 virtual void ScheduledActionCommit() = 0; 42 virtual void ScheduledActionUpdateVisibleTiles() = 0; 43 virtual void ScheduledActionActivatePendingTree() = 0; 44 virtual void ScheduledActionBeginOutputSurfaceCreation() = 0; 45 virtual void ScheduledActionAcquireLayerTexturesForMainThread() = 0; 46 virtual void ScheduledActionManageTiles() = 0; 47 virtual void DidAnticipatedDrawTimeChange(base::TimeTicks time) = 0; 48 virtual base::TimeDelta DrawDurationEstimate() = 0; 49 virtual base::TimeDelta BeginMainFrameToCommitDurationEstimate() = 0; 50 virtual base::TimeDelta CommitToActivateDurationEstimate() = 0; 51 virtual void PostBeginImplFrameDeadline(const base::Closure& closure, 52 base::TimeTicks deadline) = 0; 53 virtual void DidBeginImplFrameDeadline() = 0; 54 55 protected: 56 virtual ~SchedulerClient() {} 57 }; 58 59 class CC_EXPORT Scheduler { 60 public: 61 static scoped_ptr<Scheduler> Create( 62 SchedulerClient* client, 63 const SchedulerSettings& scheduler_settings, 64 int layer_tree_host_id) { 65 return make_scoped_ptr( 66 new Scheduler(client, scheduler_settings, layer_tree_host_id)); 67 } 68 69 virtual ~Scheduler(); 70 71 void SetCanStart(); 72 73 void SetVisible(bool visible); 74 void SetCanDraw(bool can_draw); 75 void NotifyReadyToActivate(); 76 77 void SetNeedsCommit(); 78 79 // Like SetNeedsCommit(), but ensures a commit will definitely happen even if 80 // we are not visible. Will eventually result in a forced draw internally. 81 void SetNeedsForcedCommitForReadback(); 82 83 void SetNeedsRedraw(); 84 85 void SetNeedsManageTiles(); 86 87 void SetMainThreadNeedsLayerTextures(); 88 89 void SetSwapUsedIncompleteTile(bool used_incomplete_tile); 90 91 void SetSmoothnessTakesPriority(bool smoothness_takes_priority); 92 93 void FinishCommit(); 94 void BeginMainFrameAborted(bool did_handle); 95 96 void DidManageTiles(); 97 void DidLoseOutputSurface(); 98 void DidCreateAndInitializeOutputSurface(); 99 bool HasInitializedOutputSurface() const { 100 return state_machine_.HasInitializedOutputSurface(); 101 } 102 103 bool CommitPending() const { return state_machine_.CommitPending(); } 104 bool RedrawPending() const { return state_machine_.RedrawPending(); } 105 bool ManageTilesPending() const { 106 return state_machine_.ManageTilesPending(); 107 } 108 bool MainThreadIsInHighLatencyMode() const { 109 return state_machine_.MainThreadIsInHighLatencyMode(); 110 } 111 112 bool WillDrawIfNeeded() const; 113 114 base::TimeTicks AnticipatedDrawTime(); 115 116 base::TimeTicks LastBeginImplFrameTime(); 117 118 void BeginImplFrame(const BeginFrameArgs& args); 119 void OnBeginImplFrameDeadline(); 120 void PollForAnticipatedDrawTriggers(); 121 122 scoped_ptr<base::Value> StateAsValue() { 123 return state_machine_.AsValue().Pass(); 124 } 125 126 bool IsInsideAction(SchedulerStateMachine::Action action) { 127 return inside_action_ == action; 128 } 129 130 private: 131 Scheduler(SchedulerClient* client, 132 const SchedulerSettings& scheduler_settings, 133 int layer_tree_host_id); 134 135 void PostBeginImplFrameDeadline(base::TimeTicks deadline); 136 void SetupNextBeginImplFrameIfNeeded(); 137 void ActivatePendingTree(); 138 void DrawAndSwapIfPossible(); 139 void DrawAndSwapForced(); 140 void DrawAndReadback(); 141 void ProcessScheduledActions(); 142 143 bool CanCommitAndActivateBeforeDeadline() const; 144 void AdvanceCommitStateIfPossible(); 145 146 const SchedulerSettings settings_; 147 SchedulerClient* client_; 148 int layer_tree_host_id_; 149 150 bool last_set_needs_begin_impl_frame_; 151 BeginFrameArgs last_begin_impl_frame_args_; 152 base::CancelableClosure begin_impl_frame_deadline_closure_; 153 base::CancelableClosure poll_for_draw_triggers_closure_; 154 base::RepeatingTimer<Scheduler> advance_commit_state_timer_; 155 156 SchedulerStateMachine state_machine_; 157 bool inside_process_scheduled_actions_; 158 SchedulerStateMachine::Action inside_action_; 159 160 base::WeakPtrFactory<Scheduler> weak_factory_; 161 162 DISALLOW_COPY_AND_ASSIGN(Scheduler); 163 }; 164 165 } // namespace cc 166 167 #endif // CC_SCHEDULER_SCHEDULER_H_ 168