summaryrefslogtreecommitdiffstats
path: root/cc/scheduler/scheduler.h
blob: 93d5b4ec55f9b06f3a2518c9ce9ee367f2369188 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CC_SCHEDULER_SCHEDULER_H_
#define CC_SCHEDULER_SCHEDULER_H_

#include <string>

#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "base/time.h"
#include "cc/base/cc_export.h"
#include "cc/output/begin_frame_args.h"
#include "cc/scheduler/scheduler_settings.h"
#include "cc/scheduler/scheduler_state_machine.h"
#include "cc/trees/layer_tree_host.h"

namespace cc {

class Thread;

struct ScheduledActionDrawAndSwapResult {
  ScheduledActionDrawAndSwapResult()
      : did_draw(false),
        did_swap(false) {}
  ScheduledActionDrawAndSwapResult(bool did_draw, bool did_swap)
      : did_draw(did_draw),
        did_swap(did_swap) {}
  bool did_draw;
  bool did_swap;
};

class SchedulerClient {
 public:
  virtual void SetNeedsBeginFrameOnImplThread(bool enable) = 0;
  virtual void ScheduledActionSendBeginFrameToMainThread() = 0;
  virtual ScheduledActionDrawAndSwapResult
  ScheduledActionDrawAndSwapIfPossible() = 0;
  virtual ScheduledActionDrawAndSwapResult
  ScheduledActionDrawAndSwapForced() = 0;
  virtual void ScheduledActionCommit() = 0;
  virtual void ScheduledActionCheckForCompletedTileUploads() = 0;
  virtual void ScheduledActionActivatePendingTreeIfNeeded() = 0;
  virtual void ScheduledActionBeginOutputSurfaceCreation() = 0;
  virtual void ScheduledActionAcquireLayerTexturesForMainThread() = 0;
  virtual void DidAnticipatedDrawTimeChange(base::TimeTicks time) = 0;
  virtual base::TimeDelta DrawDurationEstimate() = 0;

 protected:
  virtual ~SchedulerClient() {}
};

class CC_EXPORT Scheduler {
 public:
  static scoped_ptr<Scheduler> Create(
      SchedulerClient* client,
      const SchedulerSettings& scheduler_settings) {
    return make_scoped_ptr(new Scheduler(client,  scheduler_settings));
  }

  virtual ~Scheduler();

  void SetCanStart();

  void SetVisible(bool visible);
  void SetCanDraw(bool can_draw);
  void SetHasPendingTree(bool has_pending_tree);

  void SetNeedsCommit();

  // Like SetNeedsCommit(), but ensures a commit will definitely happen even if
  // we are not visible.
  void SetNeedsForcedCommit();

  void SetNeedsRedraw();

  void SetMainThreadNeedsLayerTextures();

  // Like SetNeedsRedraw(), but ensures the draw will definitely happen even if
  // we are not visible.
  void SetNeedsForcedRedraw();

  void DidSwapUseIncompleteTile();

  void FinishCommit();
  void BeginFrameAbortedByMainThread();

  void DidLoseOutputSurface();
  void DidCreateAndInitializeOutputSurface();
  bool HasInitializedOutputSurface() const {
    return state_machine_.HasInitializedOutputSurface();
  }

  bool CommitPending() const { return state_machine_.CommitPending(); }
  bool RedrawPending() const { return state_machine_.RedrawPending(); }

  bool WillDrawIfNeeded() const;

  base::TimeTicks AnticipatedDrawTime();

  base::TimeTicks LastBeginFrameOnImplThreadTime();

  void BeginFrame(const BeginFrameArgs& args);

  std::string StateAsStringForTesting() { return state_machine_.ToString(); }

 private:
  Scheduler(SchedulerClient* client,
            const SchedulerSettings& scheduler_settings);

  void SetupNextBeginFrameIfNeeded();
  void DrawAndSwapIfPossible();
  void DrawAndSwapForced();
  void ProcessScheduledActions();

  const SchedulerSettings settings_;
  SchedulerClient* client_;

  base::WeakPtrFactory<Scheduler> weak_factory_;
  bool last_set_needs_begin_frame_;
  bool has_pending_begin_frame_;
  // TODO(brianderson): crbug.com/249806 : Remove safe_to_expect_begin_frame_
  // workaround.
  bool safe_to_expect_begin_frame_;
  BeginFrameArgs last_begin_frame_args_;

  SchedulerStateMachine state_machine_;
  bool inside_process_scheduled_actions_;

  DISALLOW_COPY_AND_ASSIGN(Scheduler);
};

}  // namespace cc

#endif  // CC_SCHEDULER_SCHEDULER_H_