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