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/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" | |
16 | |
17 namespace cc { | |
18 | |
19 // (Pure) Interface for observing BeginFrame messages from BeginFrameSource | |
20 // objects. | |
21 class BeginFrameObserver { | |
22 public: | |
23 virtual ~BeginFrameObserver() {} | |
24 | |
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. | |
28 // | |
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; | |
33 | |
34 // Returns the last BeginFrameArgs used by the observer. This method's return | |
35 // value is affected by the OnBeginFrame method! | |
36 // | |
37 // - Before the first call of OnBeginFrame, this method should return a | |
38 // BeginFrameArgs on which IsValid() returns false. | |
39 // | |
40 // - If the |args| passed to OnBeginFrame is (or *will be*) used, then | |
41 // LastUsedBeginFrameArgs return value should become the |args| given to | |
42 // OnBeginFrame. | |
43 // | |
44 // - If the |args| passed to OnBeginFrame is dropped, then | |
45 // LastUsedBeginFrameArgs return value should *not* change. | |
46 // | |
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; | |
51 | |
52 // Tracing support | |
53 virtual void AsValueInto(base::trace_event::TracedValue* dict) const = 0; | |
54 }; | |
55 | |
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. | |
59 // | |
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 | |
64 // function. | |
65 class BeginFrameObserverMixIn : public BeginFrameObserver { | |
66 public: | |
67 BeginFrameObserverMixIn(); | |
68 | |
69 // BeginFrameObserver | |
70 | |
71 // Traces |args| and DCHECK |args| satisfies pre-conditions then calls | |
72 // OnBeginFrameMixInDelegate and updates the last_begin_frame_args_ value on | |
73 // true. | |
74 void OnBeginFrame(const BeginFrameArgs& args) override; | |
75 const BeginFrameArgs LastUsedBeginFrameArgs() const override; | |
76 | |
77 // Outputs last_begin_frame_args_ | |
78 void AsValueInto(base::trace_event::TracedValue* dict) const override; | |
79 | |
80 protected: | |
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; | |
84 | |
85 BeginFrameArgs last_begin_frame_args_; | |
86 int64_t dropped_begin_frame_args_; | |
87 }; | |
88 | |
89 // Interface for a class which produces BeginFrame calls to a | |
90 // BeginFrameObserver. | |
91 // | |
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 BeginFrameSource { | |
99 public: | |
100 virtual ~BeginFrameSource() {} | |
101 | |
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; | |
106 | |
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; | |
112 | |
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; | |
119 | |
120 // Tells the Source that client is ready to handle BeginFrames messages. | |
121 virtual void SetClientReady() = 0; | |
122 | |
123 // Tracing support - Recommend (but not required) to call this implementation | |
124 // in any override. | |
125 virtual void AsValueInto(base::trace_event::TracedValue* dict) const = 0; | |
126 }; | |
127 | |
128 // Simple mix in which implements a BeginFrameSource. | |
129 // Implementation classes should: | |
130 // - Implement the pure virtual (Set)NeedsBeginFrames methods from | |
131 // BeginFrameSource. | |
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 BeginFrameSourceMixIn : public BeginFrameSource { | |
136 public: | |
137 ~BeginFrameSourceMixIn() override {} | |
138 | |
139 // BeginFrameSource | |
140 bool NeedsBeginFrames() const final; | |
141 void SetNeedsBeginFrames(bool needs_begin_frames) final; | |
142 void DidFinishFrame(size_t remaining_frames) override {} | |
143 void AddObserver(BeginFrameObserver* obs) final; | |
144 void RemoveObserver(BeginFrameObserver* obs) final; | |
145 void SetClientReady() override {} | |
146 | |
147 // Tracing support - Recommend (but not required) to call this implementation | |
148 // in any override. | |
149 void AsValueInto(base::trace_event::TracedValue* dict) const override; | |
150 | |
151 protected: | |
152 BeginFrameSourceMixIn(); | |
153 | |
154 // These methods should be used by subclasses to make the call to the | |
155 // observers. | |
156 void CallOnBeginFrame(const BeginFrameArgs& args); | |
157 | |
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) {} | |
161 | |
162 BeginFrameObserver* observer_; | |
163 bool needs_begin_frames_; | |
164 | |
165 private: | |
166 bool inside_as_value_into_; | |
167 }; | |
168 | |
169 // A frame source which calls BeginFrame (at the next possible time) as soon as | |
170 // remaining frames reaches zero. | |
171 class BackToBackBeginFrameSource : public BeginFrameSourceMixIn { | |
172 public: | |
173 static scoped_ptr<BackToBackBeginFrameSource> Create( | |
174 base::SingleThreadTaskRunner* task_runner); | |
175 ~BackToBackBeginFrameSource() override; | |
176 | |
177 // BeginFrameSource | |
178 void DidFinishFrame(size_t remaining_frames) override; | |
179 | |
180 // Tracing | |
181 void AsValueInto(base::trace_event::TracedValue* dict) const override; | |
182 | |
183 protected: | |
184 explicit BackToBackBeginFrameSource( | |
185 base::SingleThreadTaskRunner* task_runner); | |
186 virtual base::TimeTicks Now(); // Now overridable for testing | |
187 | |
188 base::SingleThreadTaskRunner* task_runner_; | |
189 | |
190 bool send_begin_frame_posted_; | |
191 | |
192 // BeginFrameSourceMixIn | |
193 void OnNeedsBeginFramesChange(bool needs_begin_frames) override; | |
194 | |
195 void BeginFrame(); | |
196 | |
197 private: | |
198 base::WeakPtrFactory<BackToBackBeginFrameSource> weak_factory_; | |
199 }; | |
200 | |
201 // A frame source which is locked to an external parameters provides from a | |
202 // vsync source and generates BeginFrameArgs for it. | |
203 class SyntheticBeginFrameSource : public BeginFrameSourceMixIn, | |
204 public VSyncParameterObserver, | |
205 public TimeSourceClient { | |
206 public: | |
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; | |
212 | |
213 // Tracing | |
214 void AsValueInto(base::trace_event::TracedValue* dict) const override; | |
215 | |
216 // VSyncParameterObserver | |
217 void OnUpdateVSyncParameters(base::TimeTicks new_vsync_timebase, | |
218 base::TimeDelta new_vsync_interval) override; | |
219 | |
220 // TimeSourceClient | |
221 void OnTimerTick() override; | |
222 | |
223 protected: | |
224 explicit SyntheticBeginFrameSource( | |
225 scoped_refptr<DelayBasedTimeSource> time_source); | |
226 | |
227 BeginFrameArgs CreateBeginFrameArgs(base::TimeTicks frame_time, | |
228 BeginFrameArgs::BeginFrameArgsType type); | |
229 | |
230 // BeginFrameSourceMixIn | |
231 void OnNeedsBeginFramesChange(bool needs_begin_frames) override; | |
232 | |
233 scoped_refptr<DelayBasedTimeSource> time_source_; | |
234 }; | |
235 | |
236 // A "virtual" frame source which lets you switch between multiple other frame | |
237 // sources while making sure the BeginFrameArgs stays increasing (possibly | |
238 // enforcing minimum boundry between BeginFrameArgs messages). | |
239 class BeginFrameSourceMultiplexer : public BeginFrameSourceMixIn, | |
240 public BeginFrameObserver { | |
241 public: | |
242 static scoped_ptr<BeginFrameSourceMultiplexer> Create(); | |
243 ~BeginFrameSourceMultiplexer() override; | |
244 | |
245 void SetMinimumInterval(base::TimeDelta new_minimum_interval); | |
246 | |
247 void AddSource(BeginFrameSource* new_source); | |
248 void RemoveSource(BeginFrameSource* existing_source); | |
249 void SetActiveSource(BeginFrameSource* new_source); | |
250 const BeginFrameSource* ActiveSource(); | |
251 | |
252 // BeginFrameObserver | |
253 // The mux is an BeginFrameObserver as it needs to proxy the OnBeginFrame | |
254 // calls to preserve the monotonicity of the BeginFrameArgs when switching | |
255 // sources. | |
256 void OnBeginFrame(const BeginFrameArgs& args) override; | |
257 const BeginFrameArgs LastUsedBeginFrameArgs() const override; | |
258 | |
259 // BeginFrameSource | |
260 void DidFinishFrame(size_t remaining_frames) override; | |
261 | |
262 // BeginFrameSourceMixIn | |
263 void OnNeedsBeginFramesChange(bool needs_begin_frames) override; | |
264 | |
265 // Tracing | |
266 void AsValueInto(base::trace_event::TracedValue* dict) const override; | |
267 | |
268 protected: | |
269 BeginFrameSourceMultiplexer(); | |
270 explicit BeginFrameSourceMultiplexer(base::TimeDelta minimum_interval); | |
271 | |
272 bool HasSource(BeginFrameSource* source); | |
273 bool IsIncreasing(const BeginFrameArgs& args); | |
274 | |
275 base::TimeDelta minimum_interval_; | |
276 | |
277 BeginFrameSource* active_source_; | |
278 std::set<BeginFrameSource*> source_list_; | |
279 }; | |
280 | |
281 } // namespace cc | |
282 | |
283 #endif // CC_SCHEDULER_BEGIN_FRAME_SOURCE_H_ | |
OLD | NEW |