blob: 93d5b4ec55f9b06f3a2518c9ce9ee367f2369188 [file] [log] [blame]
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +00001// 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
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +01008#include <string>
9
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000010#include "base/basictypes.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/time.h"
13#include "cc/base/cc_export.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010014#include "cc/output/begin_frame_args.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000015#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:
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010036 virtual void SetNeedsBeginFrameOnImplThread(bool enable) = 0;
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010037 virtual void ScheduledActionSendBeginFrameToMainThread() = 0;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000038 virtual ScheduledActionDrawAndSwapResult
39 ScheduledActionDrawAndSwapIfPossible() = 0;
40 virtual ScheduledActionDrawAndSwapResult
41 ScheduledActionDrawAndSwapForced() = 0;
42 virtual void ScheduledActionCommit() = 0;
43 virtual void ScheduledActionCheckForCompletedTileUploads() = 0;
44 virtual void ScheduledActionActivatePendingTreeIfNeeded() = 0;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010045 virtual void ScheduledActionBeginOutputSurfaceCreation() = 0;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000046 virtual void ScheduledActionAcquireLayerTexturesForMainThread() = 0;
47 virtual void DidAnticipatedDrawTimeChange(base::TimeTicks time) = 0;
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010048 virtual base::TimeDelta DrawDurationEstimate() = 0;
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000049
50 protected:
51 virtual ~SchedulerClient() {}
52};
53
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010054class CC_EXPORT Scheduler {
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000055 public:
56 static scoped_ptr<Scheduler> Create(
57 SchedulerClient* client,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000058 const SchedulerSettings& scheduler_settings) {
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010059 return make_scoped_ptr(new Scheduler(client, scheduler_settings));
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000060 }
61
62 virtual ~Scheduler();
63
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010064 void SetCanStart();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000065
66 void SetVisible(bool visible);
67 void SetCanDraw(bool can_draw);
68 void SetHasPendingTree(bool has_pending_tree);
69
70 void SetNeedsCommit();
71
72 // Like SetNeedsCommit(), but ensures a commit will definitely happen even if
73 // we are not visible.
74 void SetNeedsForcedCommit();
75
76 void SetNeedsRedraw();
77
78 void SetMainThreadNeedsLayerTextures();
79
80 // Like SetNeedsRedraw(), but ensures the draw will definitely happen even if
81 // we are not visible.
82 void SetNeedsForcedRedraw();
83
84 void DidSwapUseIncompleteTile();
85
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +010086 void FinishCommit();
87 void BeginFrameAbortedByMainThread();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000088
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000089 void DidLoseOutputSurface();
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010090 void DidCreateAndInitializeOutputSurface();
91 bool HasInitializedOutputSurface() const {
92 return state_machine_.HasInitializedOutputSurface();
93 }
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000094
95 bool CommitPending() const { return state_machine_.CommitPending(); }
96 bool RedrawPending() const { return state_machine_.RedrawPending(); }
97
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010098 bool WillDrawIfNeeded() const;
99
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000100 base::TimeTicks AnticipatedDrawTime();
101
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100102 base::TimeTicks LastBeginFrameOnImplThreadTime();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000103
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100104 void BeginFrame(const BeginFrameArgs& args);
Torne (Richard Coles)90dce4d2013-05-29 14:40:03 +0100105
106 std::string StateAsStringForTesting() { return state_machine_.ToString(); }
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000107
108 private:
109 Scheduler(SchedulerClient* client,
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000110 const SchedulerSettings& scheduler_settings);
111
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100112 void SetupNextBeginFrameIfNeeded();
113 void DrawAndSwapIfPossible();
114 void DrawAndSwapForced();
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000115 void ProcessScheduledActions();
116
117 const SchedulerSettings settings_;
118 SchedulerClient* client_;
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +0100119
120 base::WeakPtrFactory<Scheduler> weak_factory_;
121 bool last_set_needs_begin_frame_;
122 bool has_pending_begin_frame_;
123 // TODO(brianderson): crbug.com/249806 : Remove safe_to_expect_begin_frame_
124 // workaround.
125 bool safe_to_expect_begin_frame_;
126 BeginFrameArgs last_begin_frame_args_;
127
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000128 SchedulerStateMachine state_machine_;
129 bool inside_process_scheduled_actions_;
130
131 DISALLOW_COPY_AND_ASSIGN(Scheduler);
132};
133
134} // namespace cc
135
136#endif // CC_SCHEDULER_SCHEDULER_H_