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_STATE_MACHINE_H_
6 #define CC_SCHEDULER_STATE_MACHINE_H_
10 #include "base/basictypes.h"
11 #include "cc/cc_export.h"
15 // The SchedulerStateMachine decides how to coordinate main thread activites
16 // like painting/running javascript with rendering and input activities on the
19 // The state machine tracks internal state but is also influenced by external state.
20 // Internal state includes things like whether a frame has been requested, while
21 // external state includes things like the current time being near to the vblank time.
23 // The scheduler seperates "what to do next" from the updating of its internal state to
24 // make testing cleaner.
25 class CC_EXPORT SchedulerStateMachine
{
27 SchedulerStateMachine();
31 COMMIT_STATE_FRAME_IN_PROGRESS
,
32 COMMIT_STATE_READY_TO_COMMIT
,
33 COMMIT_STATE_WAITING_FOR_FIRST_DRAW
,
34 COMMIT_STATE_WAITING_FOR_FIRST_FORCED_DRAW
,
38 LAYER_TEXTURE_STATE_UNLOCKED
,
39 LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD
,
40 LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD
,
43 enum OutputSurfaceState
{
44 OUTPUT_SURFACE_ACTIVE
,
46 OUTPUT_SURFACE_RECREATING
,
49 bool commitPending() const
51 return m_commitState
== COMMIT_STATE_FRAME_IN_PROGRESS
||
52 m_commitState
== COMMIT_STATE_READY_TO_COMMIT
;
55 bool redrawPending() const { return m_needsRedraw
; }
61 ACTION_DRAW_IF_POSSIBLE
,
63 ACTION_BEGIN_OUTPUT_SURFACE_RECREATION
,
64 ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD
,
66 Action
nextAction() const;
67 void updateState(Action
);
69 // Indicates whether the scheduler needs a vsync callback in order to make
71 bool vsyncCallbackNeeded() const;
73 // Indicates that the system has entered and left a vsync callback.
74 // The scheduler will not draw more than once in a given vsync callback.
78 // Indicates whether the LayerTreeHostImpl is visible.
79 void setVisible(bool);
81 // Indicates that a redraw is required, either due to the impl tree changing
82 // or the screen being damaged and simply needing redisplay.
83 void setNeedsRedraw();
85 // As setNeedsRedraw(), but ensures the draw will definitely happen even if
86 // we are not visible.
87 void setNeedsForcedRedraw();
89 // Indicates whether ACTION_DRAW_IF_POSSIBLE drew to the screen or not.
90 void didDrawIfPossibleCompleted(bool success
);
92 // Indicates that a new commit flow needs to be performed, either to pull
93 // updates from the main thread to the impl, or to push deltas from the impl
95 void setNeedsCommit();
97 // As setNeedsCommit(), but ensures the beginFrame will definitely happen even if
98 // we are not visible.
99 // After this call we expect to go through the forced commit flow and then return
100 // to waiting for a non-forced beginFrame to finish.
101 void setNeedsForcedCommit();
103 // Call this only in response to receiving an ACTION_BEGIN_FRAME
104 // from nextState. Indicates that all painting is complete.
105 void beginFrameComplete();
107 // Call this only in response to receiving an ACTION_BEGIN_FRAME
108 // from nextState if the client rejects the beginFrame message.
109 void beginFrameAborted();
111 // Request exclusive access to the textures that back single buffered
112 // layers on behalf of the main thread. Upon acqusition,
113 // ACTION_DRAW_IF_POSSIBLE will not draw until the main thread releases the
114 // textures to the impl thread by committing the layers.
115 void setMainThreadNeedsLayerTextures();
117 // Indicates whether we can successfully begin a frame at this time.
118 void setCanBeginFrame(bool can
) { m_canBeginFrame
= can
; }
120 // Indicates whether drawing would, at this time, make sense.
121 // canDraw can be used to supress flashes or checkerboarding
122 // when such behavior would be undesirable.
123 void setCanDraw(bool can
);
125 // Indicates whether or not there is a pending tree. This influences
126 // whether or not we can succesfully commit at this time. If the
127 // last commit is still being processed (but not blocking), it may not
128 // be possible to take another commit yet. This overrides force commit,
129 // as a commit is already still in flight.
130 void setHasPendingTree(bool hasPendingTree
);
131 bool hasPendingTree() const { return m_hasPendingTree
; }
133 void didLoseOutputSurface();
134 void didRecreateOutputSurface();
136 // Exposed for testing purposes.
137 void setMaximumNumberOfFailedDrawsBeforeDrawIsForced(int);
139 std::string
toString();
142 bool shouldDrawForced() const;
143 bool drawSuspendedUntilCommit() const;
144 bool scheduledToDraw() const;
145 bool shouldDraw() const;
146 bool shouldAcquireLayerTexturesForMainThread() const;
147 bool hasDrawnThisFrame() const;
149 CommitState m_commitState
;
151 int m_currentFrameNumber
;
152 int m_lastFrameNumberWhereDrawWasCalled
;
153 int m_consecutiveFailedDraws
;
154 int m_maximumNumberOfFailedDrawsBeforeDrawIsForced
;
156 bool m_needsForcedRedraw
;
157 bool m_needsForcedRedrawAfterNextCommit
;
159 bool m_needsForcedCommit
;
160 bool m_expectImmediateBeginFrame
;
161 bool m_mainThreadNeedsLayerTextures
;
164 bool m_canBeginFrame
;
166 bool m_hasPendingTree
;
167 bool m_drawIfPossibleFailed
;
168 TextureState m_textureState
;
169 OutputSurfaceState m_outputSurfaceState
;
171 DISALLOW_COPY_AND_ASSIGN(SchedulerStateMachine
);
176 #endif // CC_SCHEDULER_STATE_MACHINE_H_