OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #ifndef CC_SCHEDULER_BEGIN_FRAME_SOURCE_H_ | |
6 #define CC_SCHEDULER_BEGIN_FRAME_SOURCE_H_ | |
7 | |
8 #include <set> | |
9 #include <string> | |
10 | |
11 #include "base/debug/trace_event.h" | |
12 #include "base/logging.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" | |
16 | |
17 namespace cc { | |
18 | |
19 // (Pure) Interface for observing BeginFrame messages from BeginFrameSource | |
20 // objects. | |
21 class CC_EXPORT BeginFrameObserver { | |
22 public: | |
23 // The |args| given to OnBeginFrame is guaranteed to have | |
24 // |args|.IsValid()==true and have |args|.frame_time | |
25 // field be strictly greater than the previous call. | |
26 // | |
27 // Side effects: This function can (and most of the time *will*) change the | |
28 // return value of the LastUsedBeginFrameArgs method. See the documentation | |
29 // on that method for more information. | |
30 virtual void OnBeginFrame(const BeginFrameArgs& args) = 0; | |
31 | |
32 // Some frame sources *may* provide the last missed begin frame when | |
33 // SetNeedsBeginFrames is set true (such as the SyntheticBeginFrameSource). | |
34 // | |
35 // **Don't** depend on this function ever being called. | |
36 virtual void OnMissedBeginFrame(const BeginFrameArgs& args) = 0; | |
37 | |
38 // Returns the last BeginFrameArgs used by the observer. This method's return | |
39 // value is affected by the OnBeginFrame method! | |
40 // | |
41 // - Before the first call of OnBeginFrame, this method should return a | |
42 // BeginFrameArgs on which IsValid() returns false. | |
43 // | |
44 // - If the |args| passed to OnBeginFrame is (or *will be*) used, then | |
45 // LastUsedBeginFrameArgs return value should become the |args| given to | |
46 // OnBeginFrame. | |
47 // | |
48 // - If the |args| passed to OnBeginFrame is dropped, then | |
49 // LastUsedBeginFrameArgs return value should *not* change. | |
50 // | |
51 // These requirements are designed to allow chaining and nesting of | |
52 // BeginFrameObservers which filter the incoming BeginFrame messages while | |
53 // preventing "double dropping" and other bad side effects. | |
54 virtual const BeginFrameArgs LastUsedBeginFrameArgs() const = 0; | |
55 | |
56 // Tracing support | |
57 virtual void AsValueInto(base::debug::TracedValue* dict) const = 0; | |
58 }; | |
59 | |
60 // Simple mix in which implements a BeginFrameObserver which checks the | |
61 // incoming values meet the BeginFrameObserver requirements and implements the | |
62 // required LastUsedBeginFrameArgs behaviour. | |
63 // | |
64 // Users of this mix in should; | |
65 // - Implement the OnBeginFrameMixInDelegate function. | |
66 // - Recommended (but not required) to call | |
67 // BeginFrameObserverMixIn::OnValueInto in their overridden OnValueInto | |
68 // function. | |
69 // | |
70 // Advance observers may also wish to override the OnMissedBeginFrame method. | |
simonhong
2014/09/25 06:19:46
Advance -> Advanced?
mithro-old
2014/09/25 13:32:19
Done.
| |
71 class CC_EXPORT BeginFrameObserverMixIn : public BeginFrameObserver { | |
72 public: | |
73 // BeginFrameObserver | |
simonhong
2014/09/25 06:19:46
wrong comment.
mithro-old
2014/09/25 13:32:19
Done.
| |
74 BeginFrameObserverMixIn(); | |
75 | |
76 // Traces |args| and DCHECK |args| satisfies pre-conditions then calls | |
77 // OnBeginFrameMixInDelegate and updates the last_begin_frame_args_ value on | |
78 // true. | |
79 virtual void OnBeginFrame(const BeginFrameArgs& args) OVERRIDE; | |
80 | |
81 // Forwards call to the OnBeginFrame callback. | |
82 virtual void OnMissedBeginFrame(const BeginFrameArgs& args) OVERRIDE; | |
83 | |
84 virtual const BeginFrameArgs LastUsedBeginFrameArgs() const OVERRIDE; | |
85 | |
86 // Outputs last_begin_frame_args_ | |
87 virtual void AsValueInto(base::debug::TracedValue* dict) const OVERRIDE; | |
88 | |
89 protected: | |
90 // Subclasses should override this method! | |
91 // Return true if the given argument is (or will be) used. | |
92 virtual bool OnBeginFrameMixInDelegate(const BeginFrameArgs& args) = 0; | |
93 virtual bool OnMissedBeginFrameMixInDelegate(const BeginFrameArgs& args); | |
94 | |
95 BeginFrameArgs last_begin_frame_args_; | |
96 int64_t dropped_begin_frame_args_; | |
97 int64_t dropped_missed_begin_frame_args_; | |
98 }; | |
99 | |
100 // Interface for a class which produces BeginFrame calls to a | |
101 // BeginFrameObserver. | |
102 // | |
103 // BeginFrame calls *normally* occur just after a vsync interrupt when input | |
104 // processing has been finished and provide information about the time values | |
105 // of the vsync times. *However*, these values can be heavily modified or even | |
106 // plain made up (when no vsync signal is available or vsync throttling is | |
107 // turned off). See the BeginFrameObserver for information about the guarantees | |
108 // all BeginFrameSources *must* provide. | |
109 // | |
110 // Implementation classes should: | |
111 // - Use the CallOn(Missed)BeginFrame method to call to the observer(s). | |
112 // - Implement the pure virtual methods. | |
113 // - Recommend (but not required) to call BeginFrameSource::AsValueInto in | |
114 // their own AsValueInto implementation. | |
115 class CC_EXPORT BeginFrameSource { | |
116 public: | |
117 virtual ~BeginFrameSource() {} | |
118 | |
119 // SetNeedsBeginFrames is the on/off "switch" for the BeginFrameSource. When | |
120 // set to false no more BeginFrame messages should be sent to observer. | |
121 virtual bool NeedsBeginFrames() const = 0; | |
122 virtual void SetNeedsBeginFrames(bool needs_begin_frames) = 0; | |
123 | |
124 // DidFinishFrame provides back pressure to a frame source about frame | |
125 // processing (rather than toggling SetNeedsBeginFrames every frame). It is | |
126 // used by systems like the BackToBackFrameSource to make sure only one frame | |
127 // is pending at a time. | |
128 virtual void DidFinishFrame(size_t remaining_frames) = 0; | |
simonhong
2014/09/25 06:19:46
Does we really need this function?
I'm not sure ex
mithro-old
2014/09/25 13:32:19
We went back and forth on this problem and eventua
| |
129 | |
130 // Add/Remove and observer from the source. | |
simonhong
2014/09/25 06:19:46
and -> an.
mithro-old
2014/09/25 13:32:19
Done.
| |
131 // *At the moment* only a single observer can be added to the source, however | |
132 // in the future this may be extended to allow multiple observers. | |
133 // If making this change, please use base::ObserverList to do so. | |
134 void AddObserver(BeginFrameObserver* obs); | |
135 void RemoveObserver(BeginFrameObserver* obs); | |
136 | |
137 // Tracing support - Recommend (but not required) to call this implementation | |
138 // in any override. | |
139 virtual void AsValueInto(base::debug::TracedValue* dict) const; | |
140 | |
141 protected: | |
142 BeginFrameSource(); | |
143 | |
144 // These methods should be used by subclasses to make the call to the | |
145 // observers. | |
146 void CallOnBeginFrame(const BeginFrameArgs& args); | |
147 void CallOnMissedBeginFrame(const BeginFrameArgs& args); | |
148 | |
149 BeginFrameObserver* observer_; | |
150 | |
151 private: | |
152 bool inside_as_value_into_; | |
153 }; | |
154 | |
155 // A frame source which calls BeginFrame (at the next possible time) as soon as | |
156 // remaining frames reaches zero. | |
157 class CC_EXPORT BackToBackBeginFrameSource : public BeginFrameSource { | |
158 public: | |
159 static scoped_ptr<BackToBackBeginFrameSource> Create( | |
160 base::SingleThreadTaskRunner* task_runner); | |
161 virtual ~BackToBackBeginFrameSource(); | |
162 | |
163 // BeginFrameSource | |
164 virtual bool NeedsBeginFrames() const OVERRIDE; | |
165 virtual void SetNeedsBeginFrames(bool needs_begin_frames) OVERRIDE; | |
166 virtual void DidFinishFrame(size_t remaining_frames) OVERRIDE; | |
167 | |
168 // Tracing | |
169 virtual void AsValueInto(base::debug::TracedValue* dict) const OVERRIDE; | |
170 | |
171 protected: | |
172 explicit BackToBackBeginFrameSource( | |
173 base::SingleThreadTaskRunner* task_runner); | |
174 virtual base::TimeTicks Now(); // Now overridable for testing | |
175 | |
176 base::WeakPtrFactory<BackToBackBeginFrameSource> weak_factory_; | |
177 base::SingleThreadTaskRunner* task_runner_; | |
178 | |
179 bool needs_begin_frames_; | |
180 bool send_begin_frame_posted_; | |
181 | |
182 void ScheduleBeginFrame(); | |
183 void BeginFrame(); | |
184 }; | |
185 | |
186 // A frame source which is locked to an external parameters provides from a | |
187 // vsync source and generates BeginFrameArgs for it. | |
188 class CC_EXPORT SyntheticBeginFrameSource : public BeginFrameSource, | |
189 public VSyncParameterObserver, | |
190 public TimeSourceClient { | |
191 public: | |
192 static scoped_ptr<SyntheticBeginFrameSource> Create( | |
193 base::SingleThreadTaskRunner* task_runner, | |
194 base::TimeTicks initial_vsync_timebase, | |
195 base::TimeDelta initial_vsync_interval); | |
196 virtual ~SyntheticBeginFrameSource(); | |
197 | |
198 // BeginFrameSource | |
199 virtual bool NeedsBeginFrames() const OVERRIDE; | |
200 virtual void SetNeedsBeginFrames(bool needs_begin_frames) OVERRIDE; | |
201 virtual void DidFinishFrame(size_t remaining_frames) OVERRIDE{}; | |
202 | |
203 // Tracing | |
204 virtual void AsValueInto(base::debug::TracedValue* dict) const OVERRIDE; | |
205 | |
206 // VSyncParameterObserver | |
207 virtual void OnUpdateVSyncParameters( | |
208 base::TimeTicks new_vsync_timebase, | |
209 base::TimeDelta new_vsync_interval) OVERRIDE; | |
210 | |
211 // TimeSourceClient | |
212 virtual void OnTimerTick() OVERRIDE; | |
213 | |
214 protected: | |
215 explicit SyntheticBeginFrameSource( | |
216 scoped_refptr<DelayBasedTimeSource> time_source); | |
217 | |
218 void SendBeginFrameFromTick(base::TimeTicks frame_time); | |
219 | |
220 scoped_refptr<DelayBasedTimeSource> time_source_; | |
221 }; | |
222 | |
223 // A "virtual" frame source which lets you switch between multiple other frame | |
224 // sources while making sure the BeginFrameArgs stays increasing (possibly | |
225 // enforcing minimum boundry between BeginFrameArgs messages). | |
226 class CC_EXPORT BeginFrameSourceMultiplexer : public BeginFrameSource, | |
227 public BeginFrameObserver { | |
228 public: | |
229 static scoped_ptr<BeginFrameSourceMultiplexer> Create(); | |
230 virtual ~BeginFrameSourceMultiplexer(); | |
231 | |
232 void SetMinimumInterval(base::TimeDelta new_minimum_interval); | |
233 | |
234 void AddSource(BeginFrameSource* new_source); | |
235 void RemoveSource(BeginFrameSource* existing_source); | |
236 void SetActiveSource(BeginFrameSource* new_source); | |
237 const BeginFrameSource* ActiveSource(); | |
238 | |
239 // BeginFrameObserver | |
240 // The mux is an BeginFrameObserver as it needs to proxy the OnBeginFrame | |
241 // calls to preserve the monotonicity of the BeginFrameArgs when switching | |
242 // sources. | |
243 virtual void OnBeginFrame(const BeginFrameArgs& args) OVERRIDE; | |
244 virtual void OnMissedBeginFrame(const BeginFrameArgs& args) OVERRIDE; | |
245 virtual const BeginFrameArgs LastUsedBeginFrameArgs() const OVERRIDE; | |
246 | |
247 // BeginFrameSource | |
248 virtual bool NeedsBeginFrames() const OVERRIDE; | |
249 virtual void SetNeedsBeginFrames(bool needs_begin_frames) OVERRIDE; | |
250 virtual void DidFinishFrame(size_t remaining_frames) OVERRIDE; | |
251 | |
252 // Tracing | |
253 virtual void AsValueInto(base::debug::TracedValue* dict) const OVERRIDE; | |
254 | |
255 protected: | |
256 BeginFrameSourceMultiplexer(); | |
257 explicit BeginFrameSourceMultiplexer(base::TimeDelta minimum_interval); | |
258 | |
259 bool HasSource(BeginFrameSource* source); | |
260 bool IsIncreasing(const BeginFrameArgs& args); | |
261 | |
262 base::TimeDelta minimum_interval_; | |
263 | |
264 BeginFrameSource* active_source_; | |
265 std::set<BeginFrameSource*> source_list_; | |
266 }; | |
267 | |
268 } // namespace cc | |
269 | |
270 #endif // CC_SCHEDULER_BEGIN_FRAME_SOURCE_H_ | |
OLD | NEW |