Flutter Engine
flutter::VsyncWaiter Class Referenceabstract

#include <vsync_waiter.h>

Inheritance diagram for flutter::VsyncWaiter:
flutter::testing::ConstantFiringVsyncWaiter flutter::testing::ShellTestVsyncWaiter flutter::VsyncWaiterAndroid flutter::VsyncWaiterEmbedder flutter::VsyncWaiterFallback flutter::VsyncWaiterIOS flutter_runner::VsyncWaiter

Public Types

using Callback = std::function< void(fml::TimePoint frame_start_time, fml::TimePoint frame_target_time)>
 

Public Member Functions

virtual ~VsyncWaiter ()
 
void AsyncWaitForVsync (const Callback &callback)
 
void ScheduleSecondaryCallback (const fml::closure &callback)
 

Protected Member Functions

 VsyncWaiter (TaskRunners task_runners)
 
virtual void AwaitVSync ()=0
 
void FireCallback (fml::TimePoint frame_start_time, fml::TimePoint frame_target_time)
 

Protected Attributes

const TaskRunners task_runners_
 

Friends

class VsyncWaiterAndroid
 
class VsyncWaiterEmbedder
 

Detailed Description

Abstract Base Class that represents a platform specific mechanism for getting callbacks when a vsync event happens.

Definition at line 19 of file vsync_waiter.h.

Member Typedef Documentation

◆ Callback

using flutter::VsyncWaiter::Callback = std::function<void(fml::TimePoint frame_start_time, fml::TimePoint frame_target_time)>

Definition at line 22 of file vsync_waiter.h.

Constructor & Destructor Documentation

◆ ~VsyncWaiter()

flutter::VsyncWaiter::~VsyncWaiter ( )
virtualdefault

Reimplemented in flutter_runner::VsyncWaiter.

Referenced by VsyncWaiter().

◆ VsyncWaiter()

flutter::VsyncWaiter::VsyncWaiter ( TaskRunners  task_runners)
protected

Definition at line 32 of file vsync_waiter.cc.

References ~VsyncWaiter().

33  : task_runners_(std::move(task_runners)) {}
const TaskRunners task_runners_
Definition: vsync_waiter.h:39

Member Function Documentation

◆ AsyncWaitForVsync()

void flutter::VsyncWaiter::AsyncWaitForVsync ( const Callback callback)

Definition at line 38 of file vsync_waiter.cc.

References AwaitVSync(), TRACE_EVENT0, and TRACE_EVENT_INSTANT0.

38  {
39  if (!callback) {
40  return;
41  }
42 
43  TRACE_EVENT0("flutter", "AsyncWaitForVsync");
44 
45  {
46  std::scoped_lock lock(callback_mutex_);
47  if (callback_) {
48  // The animator may request a frame more than once within a frame
49  // interval. Multiple calls to request frame must result in a single
50  // callback per frame interval.
51  TRACE_EVENT_INSTANT0("flutter", "MultipleCallsToVsyncInFrameInterval");
52  return;
53  }
54  callback_ = std::move(callback);
55  if (secondary_callback_) {
56  // Return directly as `AwaitVSync` is already called by
57  // `ScheduleSecondaryCallback`.
58  return;
59  }
60  }
61  AwaitVSync();
62 }
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:75
#define TRACE_EVENT_INSTANT0(category_group, name)
Definition: trace_event.h:104
virtual void AwaitVSync()=0

◆ AwaitVSync()

virtual void flutter::VsyncWaiter::AwaitVSync ( )
protectedpure virtual

◆ FireCallback()

void flutter::VsyncWaiter::FireCallback ( fml::TimePoint  frame_start_time,
fml::TimePoint  frame_target_time 
)
protected

Definition at line 91 of file vsync_waiter.cc.

References FML_TRACE_EVENT, flutter::TaskRunners::GetUITaskRunner(), fml::TaskRunner::PostTaskForTime(), task_runners_, TRACE_EVENT0, TRACE_EVENT_INSTANT0, TRACE_FLOW_BEGIN, TRACE_FLOW_END, and fml::tracing::TraceNonce().

Referenced by flutter_runner::VsyncWaiter::SnapToNextPhase(), and flutter::VsyncWaiterFallback::VsyncWaiterFallback().

92  {
93  Callback callback;
94  fml::closure secondary_callback;
95 
96  {
97  std::scoped_lock lock(callback_mutex_);
98  callback = std::move(callback_);
99  secondary_callback = std::move(secondary_callback_);
100  }
101 
102  if (!callback && !secondary_callback) {
103  // This means that the vsync waiter implementation fired a callback for a
104  // request we did not make. This is a paranoid check but we still want to
105  // make sure we catch misbehaving vsync implementations.
106  TRACE_EVENT_INSTANT0("flutter", "MismatchedFrameCallback");
107  return;
108  }
109 
110  if (callback) {
111  auto flow_identifier = fml::tracing::TraceNonce();
112 
113  // The base trace ensures that flows have a root to begin from if one does
114  // not exist. The trace viewer will ignore traces that have no base event
115  // trace. While all our message loops insert a base trace trace
116  // (MessageLoop::RunExpiredTasks), embedders may not.
117  TRACE_EVENT0("flutter", "VsyncFireCallback");
118 
119  TRACE_FLOW_BEGIN("flutter", kVsyncFlowName, flow_identifier);
120 
122  [callback, flow_identifier, frame_start_time, frame_target_time]() {
123  FML_TRACE_EVENT("flutter", kVsyncTraceName, "StartTime",
124  frame_start_time, "TargetTime", frame_target_time);
125  callback(frame_start_time, frame_target_time);
126  TRACE_FLOW_END("flutter", kVsyncFlowName, flow_identifier);
127  },
128  frame_start_time);
129  }
130 
131  if (secondary_callback) {
133  std::move(secondary_callback), frame_start_time);
134  }
135 }
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:75
#define TRACE_EVENT_INSTANT0(category_group, name)
Definition: trace_event.h:104
#define TRACE_FLOW_END(category, name, id)
Definition: trace_event.h:121
std::function< void(fml::TimePoint frame_start_time, fml::TimePoint frame_target_time)> Callback
Definition: vsync_waiter.h:22
virtual void PostTaskForTime(const fml::closure &task, fml::TimePoint target_time)
Definition: task_runner.cc:28
static constexpr const char * kVsyncFlowName
Definition: vsync_waiter.cc:12
std::function< void()> closure
Definition: closure.h:14
#define TRACE_FLOW_BEGIN(category, name, id)
Definition: trace_event.h:115
static constexpr const char * kVsyncTraceName
Definition: vsync_waiter.cc:29
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
Definition: task_runners.cc:34
const TaskRunners task_runners_
Definition: vsync_waiter.h:39
size_t TraceNonce()
Definition: trace_event.cc:291
#define FML_TRACE_EVENT(category_group, name,...)
Definition: trace_event.h:71

◆ ScheduleSecondaryCallback()

void flutter::VsyncWaiter::ScheduleSecondaryCallback ( const fml::closure callback)

Add a secondary callback for the next vsync.

See also |PointerDataDispatcher::ScheduleSecondaryVsyncCallback|.

Definition at line 64 of file vsync_waiter.cc.

References AwaitVSync(), FML_DCHECK, flutter::TaskRunners::GetUITaskRunner(), fml::TaskRunner::RunsTasksOnCurrentThread(), task_runners_, TRACE_EVENT0, and TRACE_EVENT_INSTANT0.

64  {
66 
67  if (!callback) {
68  return;
69  }
70 
71  TRACE_EVENT0("flutter", "ScheduleSecondaryCallback");
72 
73  {
74  std::scoped_lock lock(callback_mutex_);
75  if (secondary_callback_) {
76  // Multiple schedules must result in a single callback per frame interval.
77  TRACE_EVENT_INSTANT0("flutter",
78  "MultipleCallsToSecondaryVsyncInFrameInterval");
79  return;
80  }
81  secondary_callback_ = std::move(callback);
82  if (callback_) {
83  // Return directly as `AwaitVSync` is already called by
84  // `AsyncWaitForVsync`.
85  return;
86  }
87  }
88  AwaitVSync();
89 }
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:75
#define FML_DCHECK(condition)
Definition: logging.h:86
#define TRACE_EVENT_INSTANT0(category_group, name)
Definition: trace_event.h:104
virtual void AwaitVSync()=0
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
Definition: task_runners.cc:34
const TaskRunners task_runners_
Definition: vsync_waiter.h:39
virtual bool RunsTasksOnCurrentThread()
Definition: task_runner.cc:43

Friends And Related Function Documentation

◆ VsyncWaiterAndroid

friend class VsyncWaiterAndroid
friend

Definition at line 36 of file vsync_waiter.h.

◆ VsyncWaiterEmbedder

friend class VsyncWaiterEmbedder
friend

Definition at line 37 of file vsync_waiter.h.

Member Data Documentation

◆ task_runners_


The documentation for this class was generated from the following files: