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
,
37 LAYER_TEXTURE_STATE_UNLOCKED
,
38 LAYER_TEXTURE_STATE_ACQUIRED_BY_MAIN_THREAD
,
39 LAYER_TEXTURE_STATE_ACQUIRED_BY_IMPL_THREAD
,
48 bool commitPending() const
50 return m_commitState
!= COMMIT_STATE_IDLE
;
53 bool redrawPending() const { return m_needsRedraw
; }
59 ACTION_DRAW_IF_POSSIBLE
,
61 ACTION_BEGIN_CONTEXT_RECREATION
,
62 ACTION_ACQUIRE_LAYER_TEXTURES_FOR_MAIN_THREAD
,
64 Action
nextAction() const;
65 void updateState(Action
);
67 // Indicates whether the scheduler needs a vsync callback in order to make
69 bool vsyncCallbackNeeded() const;
71 // Indicates that the system has entered and left a vsync callback.
72 // The scheduler will not draw more than once in a given vsync callback.
76 // Indicates whether the LayerTreeHostImpl is visible.
77 void setVisible(bool);
79 // Indicates that a redraw is required, either due to the impl tree changing
80 // or the screen being damaged and simply needing redisplay.
81 void setNeedsRedraw();
83 // As setNeedsRedraw(), but ensures the draw will definitely happen even if
84 // we are not visible.
85 void setNeedsForcedRedraw();
87 // Indicates whether ACTION_DRAW_IF_POSSIBLE drew to the screen or not.
88 void didDrawIfPossibleCompleted(bool success
);
90 // Indicates that a new commit flow needs to be performed, either to pull
91 // updates from the main thread to the impl, or to push deltas from the impl
93 void setNeedsCommit();
95 // As setNeedsCommit(), but ensures the beginFrame will definitely happen even if
96 // we are not visible.
97 // After this call we expect to go through the forced commit flow and then return
98 // to waiting for a non-forced beginFrame to finish.
99 void setNeedsForcedCommit();
101 // Call this only in response to receiving an ACTION_BEGIN_FRAME
102 // from nextState. Indicates that all painting is complete.
103 void beginFrameComplete();
105 // Call this only in response to receiving an ACTION_BEGIN_FRAME
106 // from nextState if the client rejects the beginFrame message.
107 void beginFrameAborted();
109 // Request exclusive access to the textures that back single buffered
110 // layers on behalf of the main thread. Upon acqusition,
111 // ACTION_DRAW_IF_POSSIBLE will not draw until the main thread releases the
112 // textures to the impl thread by committing the layers.
113 void setMainThreadNeedsLayerTextures();
115 // Indicates whether we can successfully begin a frame at this time.
116 void setCanBeginFrame(bool can
) { m_canBeginFrame
= can
; }
118 // Indicates whether drawing would, at this time, make sense.
119 // canDraw can be used to supress flashes or checkerboarding
120 // when such behavior would be undesirable.
121 void setCanDraw(bool can
) { m_canDraw
= can
; }
123 void didLoseContext();
124 void didRecreateContext();
126 // Exposed for testing purposes.
127 void setMaximumNumberOfFailedDrawsBeforeDrawIsForced(int);
129 std::string
toString();
132 bool shouldDrawForced() const;
133 bool drawSuspendedUntilCommit() const;
134 bool scheduledToDraw() const;
135 bool shouldDraw() const;
136 bool shouldAcquireLayerTexturesForMainThread() const;
137 bool hasDrawnThisFrame() const;
139 CommitState m_commitState
;
141 int m_currentFrameNumber
;
142 int m_lastFrameNumberWhereDrawWasCalled
;
143 int m_consecutiveFailedDraws
;
144 int m_maximumNumberOfFailedDrawsBeforeDrawIsForced
;
146 bool m_needsForcedRedraw
;
147 bool m_needsForcedRedrawAfterNextCommit
;
149 bool m_needsForcedCommit
;
150 bool m_expectImmediateBeginFrame
;
151 bool m_mainThreadNeedsLayerTextures
;
154 bool m_canBeginFrame
;
156 bool m_drawIfPossibleFailed
;
157 TextureState m_textureState
;
158 ContextState m_contextState
;
160 DISALLOW_COPY_AND_ASSIGN(SchedulerStateMachine
);
165 #endif // CC_SCHEDULER_STATE_MACHINE_H_