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