1 // Copyright 2014 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_BEGIN_FRAME_SOURCE_H_
6 #define CC_SCHEDULER_BEGIN_FRAME_SOURCE_H_
11 #include "base/logging.h"
12 #include "base/trace_event/trace_event.h"
13 #include "cc/output/begin_frame_args.h"
14 #include "cc/output/vsync_parameter_observer.h"
15 #include "cc/scheduler/delay_based_time_source.h"
19 // (Pure) Interface for observing BeginFrame messages from BeginFrameSource
21 class CC_EXPORT BeginFrameObserver
{
23 virtual ~BeginFrameObserver() {}
25 // The |args| given to OnBeginFrame is guaranteed to have
26 // |args|.IsValid()==true and have |args|.frame_time
27 // field be strictly greater than the previous call.
29 // Side effects: This function can (and most of the time *will*) change the
30 // return value of the LastUsedBeginFrameArgs method. See the documentation
31 // on that method for more information.
32 virtual void OnBeginFrame(const BeginFrameArgs
& args
) = 0;
34 // Returns the last BeginFrameArgs used by the observer. This method's return
35 // value is affected by the OnBeginFrame method!
37 // - Before the first call of OnBeginFrame, this method should return a
38 // BeginFrameArgs on which IsValid() returns false.
40 // - If the |args| passed to OnBeginFrame is (or *will be*) used, then
41 // LastUsedBeginFrameArgs return value should become the |args| given to
44 // - If the |args| passed to OnBeginFrame is dropped, then
45 // LastUsedBeginFrameArgs return value should *not* change.
47 // These requirements are designed to allow chaining and nesting of
48 // BeginFrameObservers which filter the incoming BeginFrame messages while
49 // preventing "double dropping" and other bad side effects.
50 virtual const BeginFrameArgs
LastUsedBeginFrameArgs() const = 0;
53 virtual void AsValueInto(base::debug::TracedValue
* dict
) const = 0;
56 // Simple mix in which implements a BeginFrameObserver which checks the
57 // incoming values meet the BeginFrameObserver requirements and implements the
58 // required LastUsedBeginFrameArgs behaviour.
60 // Users of this mix in should;
61 // - Implement the OnBeginFrameMixInDelegate function.
62 // - Recommended (but not required) to call
63 // BeginFrameObserverMixIn::OnValueInto in their overridden OnValueInto
65 class CC_EXPORT BeginFrameObserverMixIn
: public BeginFrameObserver
{
67 BeginFrameObserverMixIn();
71 // Traces |args| and DCHECK |args| satisfies pre-conditions then calls
72 // OnBeginFrameMixInDelegate and updates the last_begin_frame_args_ value on
74 void OnBeginFrame(const BeginFrameArgs
& args
) override
;
75 const BeginFrameArgs
LastUsedBeginFrameArgs() const override
;
77 // Outputs last_begin_frame_args_
78 void AsValueInto(base::debug::TracedValue
* dict
) const override
;
81 // Subclasses should override this method!
82 // Return true if the given argument is (or will be) used.
83 virtual bool OnBeginFrameMixInDelegate(const BeginFrameArgs
& args
) = 0;
85 BeginFrameArgs last_begin_frame_args_
;
86 int64_t dropped_begin_frame_args_
;
89 // Interface for a class which produces BeginFrame calls to a
90 // BeginFrameObserver.
92 // BeginFrame calls *normally* occur just after a vsync interrupt when input
93 // processing has been finished and provide information about the time values
94 // of the vsync times. *However*, these values can be heavily modified or even
95 // plain made up (when no vsync signal is available or vsync throttling is
96 // turned off). See the BeginFrameObserver for information about the guarantees
97 // all BeginFrameSources *must* provide.
98 class CC_EXPORT BeginFrameSource
{
100 virtual ~BeginFrameSource() {}
102 // SetNeedsBeginFrames is the on/off "switch" for the BeginFrameSource. When
103 // set to false no more BeginFrame messages should be sent to observer.
104 virtual bool NeedsBeginFrames() const = 0;
105 virtual void SetNeedsBeginFrames(bool needs_begin_frames
) = 0;
107 // DidFinishFrame provides back pressure to a frame source about frame
108 // processing (rather than toggling SetNeedsBeginFrames every frame). It is
109 // used by systems like the BackToBackFrameSource to make sure only one frame
110 // is pending at a time.
111 virtual void DidFinishFrame(size_t remaining_frames
) = 0;
113 // Add/Remove an observer from the source.
114 // *At the moment* only a single observer can be added to the source, however
115 // in the future this may be extended to allow multiple observers.
116 // If making this change, please use base::ObserverList to do so.
117 virtual void AddObserver(BeginFrameObserver
* obs
) = 0;
118 virtual void RemoveObserver(BeginFrameObserver
* obs
) = 0;
120 // Tells the Source that client is ready to handle BeginFrames messages.
121 virtual void SetClientReady() = 0;
123 // Tracing support - Recommend (but not required) to call this implementation
125 virtual void AsValueInto(base::debug::TracedValue
* dict
) const = 0;
128 // Simple mix in which implements a BeginFrameSource.
129 // Implementation classes should:
130 // - Implement the pure virtual (Set)NeedsBeginFrames methods from
132 // - Use the CallOnBeginFrame method to call to the observer(s).
133 // - Recommended (but not required) to call BeginFrameSourceMixIn::AsValueInto
134 // in their own AsValueInto implementation.
135 class CC_EXPORT BeginFrameSourceMixIn
: public BeginFrameSource
{
137 ~BeginFrameSourceMixIn() override
{}
140 bool NeedsBeginFrames() const override
;
141 void SetNeedsBeginFrames(bool needs_begin_frames
) override
;
142 void DidFinishFrame(size_t remaining_frames
) override
{}
143 void AddObserver(BeginFrameObserver
* obs
) final
;
144 void RemoveObserver(BeginFrameObserver
* obs
) final
;
145 void SetClientReady() override
{}
147 // Tracing support - Recommend (but not required) to call this implementation
149 void AsValueInto(base::debug::TracedValue
* dict
) const override
;
152 BeginFrameSourceMixIn();
154 // These methods should be used by subclasses to make the call to the
156 void CallOnBeginFrame(const BeginFrameArgs
& args
);
158 // This method should be overridden if you want to change some behaviour on
159 // needs_begin_frames change.
160 virtual void OnNeedsBeginFramesChange(bool needs_begin_frames
) {}
162 BeginFrameObserver
* observer_
;
163 bool needs_begin_frames_
;
166 bool inside_as_value_into_
;
169 // A frame source which calls BeginFrame (at the next possible time) as soon as
170 // remaining frames reaches zero.
171 class CC_EXPORT BackToBackBeginFrameSource
: public BeginFrameSourceMixIn
{
173 static scoped_ptr
<BackToBackBeginFrameSource
> Create(
174 base::SingleThreadTaskRunner
* task_runner
);
175 ~BackToBackBeginFrameSource() override
;
178 void DidFinishFrame(size_t remaining_frames
) override
;
181 void AsValueInto(base::debug::TracedValue
* dict
) const override
;
184 explicit BackToBackBeginFrameSource(
185 base::SingleThreadTaskRunner
* task_runner
);
186 virtual base::TimeTicks
Now(); // Now overridable for testing
188 base::SingleThreadTaskRunner
* task_runner_
;
190 bool send_begin_frame_posted_
;
192 // BeginFrameSourceMixIn
193 void OnNeedsBeginFramesChange(bool needs_begin_frames
) override
;
198 base::WeakPtrFactory
<BackToBackBeginFrameSource
> weak_factory_
;
201 // A frame source which is locked to an external parameters provides from a
202 // vsync source and generates BeginFrameArgs for it.
203 class CC_EXPORT SyntheticBeginFrameSource
: public BeginFrameSourceMixIn
,
204 public VSyncParameterObserver
,
205 public TimeSourceClient
{
207 static scoped_ptr
<SyntheticBeginFrameSource
> Create(
208 base::SingleThreadTaskRunner
* task_runner
,
209 base::TimeTicks initial_vsync_timebase
,
210 base::TimeDelta initial_vsync_interval
);
211 ~SyntheticBeginFrameSource() override
;
214 bool NeedsBeginFrames() const override
;
217 void AsValueInto(base::debug::TracedValue
* dict
) const override
;
219 // VSyncParameterObserver
220 void OnUpdateVSyncParameters(base::TimeTicks new_vsync_timebase
,
221 base::TimeDelta new_vsync_interval
) override
;
224 void OnTimerTick() override
;
227 explicit SyntheticBeginFrameSource(
228 scoped_refptr
<DelayBasedTimeSource
> time_source
);
230 BeginFrameArgs
CreateBeginFrameArgs(base::TimeTicks frame_time
,
231 BeginFrameArgs::BeginFrameArgsType type
);
233 // BeginFrameSourceMixIn
234 void OnNeedsBeginFramesChange(bool needs_begin_frames
) override
;
236 scoped_refptr
<DelayBasedTimeSource
> time_source_
;
239 // A "virtual" frame source which lets you switch between multiple other frame
240 // sources while making sure the BeginFrameArgs stays increasing (possibly
241 // enforcing minimum boundry between BeginFrameArgs messages).
242 class CC_EXPORT BeginFrameSourceMultiplexer
: public BeginFrameSourceMixIn
,
243 public BeginFrameObserver
{
245 static scoped_ptr
<BeginFrameSourceMultiplexer
> Create();
246 ~BeginFrameSourceMultiplexer() override
;
248 void SetMinimumInterval(base::TimeDelta new_minimum_interval
);
250 void AddSource(BeginFrameSource
* new_source
);
251 void RemoveSource(BeginFrameSource
* existing_source
);
252 void SetActiveSource(BeginFrameSource
* new_source
);
253 const BeginFrameSource
* ActiveSource();
255 // BeginFrameObserver
256 // The mux is an BeginFrameObserver as it needs to proxy the OnBeginFrame
257 // calls to preserve the monotonicity of the BeginFrameArgs when switching
259 void OnBeginFrame(const BeginFrameArgs
& args
) override
;
260 const BeginFrameArgs
LastUsedBeginFrameArgs() const override
;
263 bool NeedsBeginFrames() const override
;
264 void SetNeedsBeginFrames(bool needs_begin_frames
) override
;
265 void DidFinishFrame(size_t remaining_frames
) override
;
268 void AsValueInto(base::debug::TracedValue
* dict
) const override
;
271 BeginFrameSourceMultiplexer();
272 explicit BeginFrameSourceMultiplexer(base::TimeDelta minimum_interval
);
274 bool HasSource(BeginFrameSource
* source
);
275 bool IsIncreasing(const BeginFrameArgs
& args
);
277 base::TimeDelta minimum_interval_
;
279 BeginFrameSource
* active_source_
;
280 std::set
<BeginFrameSource
*> source_list_
;
285 #endif // CC_SCHEDULER_BEGIN_FRAME_SOURCE_H_