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 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::debug::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 CC_EXPORT 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 virtual void OnBeginFrame(const BeginFrameArgs& args) OVERRIDE; |
| 75 virtual const BeginFrameArgs LastUsedBeginFrameArgs() const OVERRIDE; |
| 76 |
| 77 // Outputs last_begin_frame_args_ |
| 78 virtual void AsValueInto(base::debug::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 CC_EXPORT 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 // Tracing support - Recommend (but not required) to call this implementation |
| 121 // in any override. |
| 122 virtual void AsValueInto(base::debug::TracedValue* dict) const = 0; |
| 123 }; |
| 124 |
| 125 // Simple mix in which implements a BeginFrameSource. |
| 126 // Implementation classes should: |
| 127 // - Implement the pure virtual (Set)NeedsBeginFrames methods from |
| 128 // BeginFrameSource. |
| 129 // - Use the CallOnBeginFrame method to call to the observer(s). |
| 130 // - Recommended (but not required) to call BeginFrameSourceMixIn::AsValueInto |
| 131 // in their own AsValueInto implementation. |
| 132 class CC_EXPORT BeginFrameSourceMixIn : public BeginFrameSource { |
| 133 public: |
| 134 virtual ~BeginFrameSourceMixIn() {} |
| 135 |
| 136 // BeginFrameSource |
| 137 virtual bool NeedsBeginFrames() const OVERRIDE; |
| 138 virtual void SetNeedsBeginFrames(bool needs_begin_frames) OVERRIDE; |
| 139 virtual void DidFinishFrame(size_t remaining_frames) OVERRIDE {} |
| 140 virtual void AddObserver(BeginFrameObserver* obs) OVERRIDE; |
| 141 virtual void RemoveObserver(BeginFrameObserver* obs) OVERRIDE; |
| 142 |
| 143 // Tracing support - Recommend (but not required) to call this implementation |
| 144 // in any override. |
| 145 virtual void AsValueInto(base::debug::TracedValue* dict) const OVERRIDE; |
| 146 |
| 147 protected: |
| 148 BeginFrameSourceMixIn(); |
| 149 |
| 150 // These methods should be used by subclasses to make the call to the |
| 151 // observers. |
| 152 void CallOnBeginFrame(const BeginFrameArgs& args); |
| 153 |
| 154 // This method should be overridden if you want to change some behaviour on |
| 155 // needs_begin_frames change. |
| 156 virtual void OnNeedsBeginFramesChange(bool needs_begin_frames) {} |
| 157 |
| 158 BeginFrameObserver* observer_; |
| 159 bool needs_begin_frames_; |
| 160 |
| 161 private: |
| 162 bool inside_as_value_into_; |
| 163 }; |
| 164 |
| 165 // A frame source which calls BeginFrame (at the next possible time) as soon as |
| 166 // remaining frames reaches zero. |
| 167 class CC_EXPORT BackToBackBeginFrameSource : public BeginFrameSourceMixIn { |
| 168 public: |
| 169 static scoped_ptr<BackToBackBeginFrameSource> Create( |
| 170 base::SingleThreadTaskRunner* task_runner); |
| 171 virtual ~BackToBackBeginFrameSource(); |
| 172 |
| 173 // BeginFrameSource |
| 174 virtual void DidFinishFrame(size_t remaining_frames) OVERRIDE; |
| 175 |
| 176 // Tracing |
| 177 virtual void AsValueInto(base::debug::TracedValue* dict) const OVERRIDE; |
| 178 |
| 179 protected: |
| 180 explicit BackToBackBeginFrameSource( |
| 181 base::SingleThreadTaskRunner* task_runner); |
| 182 virtual base::TimeTicks Now(); // Now overridable for testing |
| 183 |
| 184 base::WeakPtrFactory<BackToBackBeginFrameSource> weak_factory_; |
| 185 base::SingleThreadTaskRunner* task_runner_; |
| 186 |
| 187 bool send_begin_frame_posted_; |
| 188 |
| 189 // BeginFrameSourceMixIn |
| 190 virtual void OnNeedsBeginFramesChange(bool needs_begin_frames) OVERRIDE; |
| 191 |
| 192 void BeginFrame(); |
| 193 }; |
| 194 |
| 195 // A frame source which is locked to an external parameters provides from a |
| 196 // vsync source and generates BeginFrameArgs for it. |
| 197 class CC_EXPORT SyntheticBeginFrameSource : public BeginFrameSourceMixIn, |
| 198 public VSyncParameterObserver, |
| 199 public TimeSourceClient { |
| 200 public: |
| 201 static scoped_ptr<SyntheticBeginFrameSource> Create( |
| 202 base::SingleThreadTaskRunner* task_runner, |
| 203 base::TimeTicks initial_vsync_timebase, |
| 204 base::TimeDelta initial_vsync_interval); |
| 205 virtual ~SyntheticBeginFrameSource(); |
| 206 |
| 207 // BeginFrameSource |
| 208 virtual bool NeedsBeginFrames() const OVERRIDE; |
| 209 |
| 210 // Tracing |
| 211 virtual void AsValueInto(base::debug::TracedValue* dict) const OVERRIDE; |
| 212 |
| 213 // VSyncParameterObserver |
| 214 virtual void OnUpdateVSyncParameters( |
| 215 base::TimeTicks new_vsync_timebase, |
| 216 base::TimeDelta new_vsync_interval) OVERRIDE; |
| 217 |
| 218 // TimeSourceClient |
| 219 virtual void OnTimerTick() OVERRIDE; |
| 220 |
| 221 protected: |
| 222 explicit SyntheticBeginFrameSource( |
| 223 scoped_refptr<DelayBasedTimeSource> time_source); |
| 224 |
| 225 BeginFrameArgs CreateBeginFrameArgs(base::TimeTicks frame_time, |
| 226 BeginFrameArgs::BeginFrameArgsType type); |
| 227 |
| 228 // BeginFrameSourceMixIn |
| 229 virtual void OnNeedsBeginFramesChange(bool needs_begin_frames) OVERRIDE; |
| 230 |
| 231 scoped_refptr<DelayBasedTimeSource> time_source_; |
| 232 }; |
| 233 |
| 234 // A "virtual" frame source which lets you switch between multiple other frame |
| 235 // sources while making sure the BeginFrameArgs stays increasing (possibly |
| 236 // enforcing minimum boundry between BeginFrameArgs messages). |
| 237 class CC_EXPORT BeginFrameSourceMultiplexer : public BeginFrameSourceMixIn, |
| 238 public BeginFrameObserver { |
| 239 public: |
| 240 static scoped_ptr<BeginFrameSourceMultiplexer> Create(); |
| 241 virtual ~BeginFrameSourceMultiplexer(); |
| 242 |
| 243 void SetMinimumInterval(base::TimeDelta new_minimum_interval); |
| 244 |
| 245 void AddSource(BeginFrameSource* new_source); |
| 246 void RemoveSource(BeginFrameSource* existing_source); |
| 247 void SetActiveSource(BeginFrameSource* new_source); |
| 248 const BeginFrameSource* ActiveSource(); |
| 249 |
| 250 // BeginFrameObserver |
| 251 // The mux is an BeginFrameObserver as it needs to proxy the OnBeginFrame |
| 252 // calls to preserve the monotonicity of the BeginFrameArgs when switching |
| 253 // sources. |
| 254 virtual void OnBeginFrame(const BeginFrameArgs& args) OVERRIDE; |
| 255 virtual const BeginFrameArgs LastUsedBeginFrameArgs() const OVERRIDE; |
| 256 |
| 257 // BeginFrameSource |
| 258 virtual bool NeedsBeginFrames() const OVERRIDE; |
| 259 virtual void SetNeedsBeginFrames(bool needs_begin_frames) OVERRIDE; |
| 260 virtual void DidFinishFrame(size_t remaining_frames) OVERRIDE; |
| 261 |
| 262 // Tracing |
| 263 virtual void AsValueInto(base::debug::TracedValue* dict) const OVERRIDE; |
| 264 |
| 265 protected: |
| 266 BeginFrameSourceMultiplexer(); |
| 267 explicit BeginFrameSourceMultiplexer(base::TimeDelta minimum_interval); |
| 268 |
| 269 bool HasSource(BeginFrameSource* source); |
| 270 bool IsIncreasing(const BeginFrameArgs& args); |
| 271 |
| 272 base::TimeDelta minimum_interval_; |
| 273 |
| 274 BeginFrameSource* active_source_; |
| 275 std::set<BeginFrameSource*> source_list_; |
| 276 }; |
| 277 |
| 278 } // namespace cc |
| 279 |
| 280 #endif // CC_SCHEDULER_BEGIN_FRAME_SOURCE_H_ |
OLD | NEW |