Currently duration is simply returned or changed without checking whether the stream...
[chromium-blink-merge.git] / cc / scheduler / scheduler.h
blob9209782939068382f106614818d31027b888a26a
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.
5 #ifndef CC_SCHEDULER_SCHEDULER_H_
6 #define CC_SCHEDULER_SCHEDULER_H_
8 #include <string>
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"
20 namespace cc {
22 class Thread;
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;
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;
55 protected:
56 virtual ~SchedulerClient() {}
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));
69 virtual ~Scheduler();
71 void SetCanStart();
73 void SetVisible(bool visible);
74 void SetCanDraw(bool can_draw);
75 void NotifyReadyToActivate();
77 void SetNeedsCommit();
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();
83 void SetNeedsRedraw();
85 void SetNeedsManageTiles();
87 void SetMainThreadNeedsLayerTextures();
89 void SetSwapUsedIncompleteTile(bool used_incomplete_tile);
91 void SetSmoothnessTakesPriority(bool smoothness_takes_priority);
93 void FinishCommit();
94 void BeginMainFrameAborted(bool did_handle);
96 void DidManageTiles();
97 void DidLoseOutputSurface();
98 void DidCreateAndInitializeOutputSurface();
99 bool HasInitializedOutputSurface() const {
100 return state_machine_.HasInitializedOutputSurface();
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();
108 bool MainThreadIsInHighLatencyMode() const {
109 return state_machine_.MainThreadIsInHighLatencyMode();
112 bool WillDrawIfNeeded() const;
114 base::TimeTicks AnticipatedDrawTime();
116 base::TimeTicks LastBeginImplFrameTime();
118 void BeginImplFrame(const BeginFrameArgs& args);
119 void OnBeginImplFrameDeadline();
120 void PollForAnticipatedDrawTriggers();
122 scoped_ptr<base::Value> StateAsValue() {
123 return state_machine_.AsValue().Pass();
126 bool IsInsideAction(SchedulerStateMachine::Action action) {
127 return inside_action_ == action;
130 private:
131 Scheduler(SchedulerClient* client,
132 const SchedulerSettings& scheduler_settings,
133 int layer_tree_host_id);
135 void PostBeginImplFrameDeadline(base::TimeTicks deadline);
136 void SetupNextBeginImplFrameIfNeeded();
137 void ActivatePendingTree();
138 void DrawAndSwapIfPossible();
139 void DrawAndSwapForced();
140 void DrawAndReadback();
141 void ProcessScheduledActions();
143 bool CanCommitAndActivateBeforeDeadline() const;
144 void AdvanceCommitStateIfPossible();
146 const SchedulerSettings settings_;
147 SchedulerClient* client_;
148 int layer_tree_host_id_;
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_;
156 SchedulerStateMachine state_machine_;
157 bool inside_process_scheduled_actions_;
158 SchedulerStateMachine::Action inside_action_;
160 base::WeakPtrFactory<Scheduler> weak_factory_;
162 DISALLOW_COPY_AND_ASSIGN(Scheduler);
165 } // namespace cc
167 #endif // CC_SCHEDULER_SCHEDULER_H_