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/memory/scoped_ptr.h"
12#include "base/time/time.h"
13#include "cc/base/cc_export.h"
14#include "cc/output/begin_frame_args.h"
15#include "cc/scheduler/scheduler_settings.h"
16#include "cc/scheduler/scheduler_state_machine.h"
17#include "cc/trees/layer_tree_host.h"
18
19namespace cc {
20
21class Thread;
22
23struct ScheduledActionDrawAndSwapResult {
24  ScheduledActionDrawAndSwapResult()
25      : did_draw(false),
26        did_swap(false) {}
27  ScheduledActionDrawAndSwapResult(bool did_draw, bool did_swap)
28      : did_draw(did_draw),
29        did_swap(did_swap) {}
30  bool did_draw;
31  bool did_swap;
32};
33
34class SchedulerClient {
35 public:
36  virtual void SetNeedsBeginFrameOnImplThread(bool enable) = 0;
37  virtual void ScheduledActionSendBeginFrameToMainThread() = 0;
38  virtual ScheduledActionDrawAndSwapResult
39  ScheduledActionDrawAndSwapIfPossible() = 0;
40  virtual ScheduledActionDrawAndSwapResult
41  ScheduledActionDrawAndSwapForced() = 0;
42  virtual void ScheduledActionCommit() = 0;
43  virtual void ScheduledActionUpdateVisibleTiles() = 0;
44  virtual void ScheduledActionActivatePendingTreeIfNeeded() = 0;
45  virtual void ScheduledActionBeginOutputSurfaceCreation() = 0;
46  virtual void ScheduledActionAcquireLayerTexturesForMainThread() = 0;
47  virtual void DidAnticipatedDrawTimeChange(base::TimeTicks time) = 0;
48  virtual base::TimeDelta DrawDurationEstimate() = 0;
49  virtual base::TimeDelta BeginFrameToCommitDurationEstimate() = 0;
50  virtual base::TimeDelta CommitToActivateDurationEstimate() = 0;
51
52 protected:
53  virtual ~SchedulerClient() {}
54};
55
56class CC_EXPORT Scheduler {
57 public:
58  static scoped_ptr<Scheduler> Create(
59      SchedulerClient* client,
60      const SchedulerSettings& scheduler_settings) {
61    return make_scoped_ptr(new Scheduler(client,  scheduler_settings));
62  }
63
64  virtual ~Scheduler();
65
66  void SetCanStart();
67
68  void SetVisible(bool visible);
69  void SetCanDraw(bool can_draw);
70  void SetHasPendingTree(bool has_pending_tree);
71
72  void SetNeedsCommit();
73
74  // Like SetNeedsCommit(), but ensures a commit will definitely happen even if
75  // we are not visible.
76  void SetNeedsForcedCommit();
77
78  void SetNeedsRedraw();
79
80  void SetMainThreadNeedsLayerTextures();
81
82  // Like SetNeedsRedraw(), but ensures the draw will definitely happen even if
83  // we are not visible.
84  void SetNeedsForcedRedraw();
85
86  void DidSwapUseIncompleteTile();
87
88  void FinishCommit();
89  void BeginFrameAbortedByMainThread(bool did_handle);
90
91  void DidLoseOutputSurface();
92  void DidCreateAndInitializeOutputSurface();
93  bool HasInitializedOutputSurface() const {
94    return state_machine_.HasInitializedOutputSurface();
95  }
96
97  bool CommitPending() const { return state_machine_.CommitPending(); }
98  bool RedrawPending() const { return state_machine_.RedrawPending(); }
99
100  bool WillDrawIfNeeded() const;
101
102  base::TimeTicks AnticipatedDrawTime();
103
104  base::TimeTicks LastBeginFrameOnImplThreadTime();
105
106  void BeginFrame(const BeginFrameArgs& args);
107  void PollForAnticipatedDrawTriggers();
108
109  std::string StateAsStringForTesting() { return state_machine_.ToString(); }
110
111 private:
112  Scheduler(SchedulerClient* client,
113            const SchedulerSettings& scheduler_settings);
114
115  void SetupNextBeginFrameIfNeeded();
116  void DrawAndSwapIfPossible();
117  void DrawAndSwapForced();
118  void ProcessScheduledActions();
119
120  const SchedulerSettings settings_;
121  SchedulerClient* client_;
122
123  base::WeakPtrFactory<Scheduler> weak_factory_;
124  bool last_set_needs_begin_frame_;
125  bool has_pending_begin_frame_;
126  // TODO(brianderson): crbug.com/249806 : Remove safe_to_expect_begin_frame_
127  // workaround.
128  bool safe_to_expect_begin_frame_;
129  BeginFrameArgs last_begin_frame_args_;
130  base::CancelableClosure poll_for_draw_triggers_closure_;
131
132  SchedulerStateMachine state_machine_;
133  bool inside_process_scheduled_actions_;
134
135  DISALLOW_COPY_AND_ASSIGN(Scheduler);
136};
137
138}  // namespace cc
139
140#endif  // CC_SCHEDULER_SCHEDULER_H_
141