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(std::unique_ptr< FrameTimingsRecorder >)>
 

Public Member Functions

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

Protected Member Functions

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

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 21 of file vsync_waiter.h.

Member Typedef Documentation

◆ Callback

Definition at line 23 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)
explicitprotected

Definition at line 37 of file vsync_waiter.cc.

References ~VsyncWaiter().

38  : task_runners_(std::move(task_runners)) {}
const TaskRunners task_runners_
Definition: vsync_waiter.h:41

Member Function Documentation

◆ AsyncWaitForVsync()

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

Definition at line 43 of file vsync_waiter.cc.

References AwaitVSync(), TRACE_EVENT0, and TRACE_EVENT_INSTANT0.

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

◆ AwaitVSync()

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

◆ AwaitVSyncForSecondaryCallback()

virtual void flutter::VsyncWaiter::AwaitVSyncForSecondaryCallback ( )
inlineprotectedvirtual

Definition at line 66 of file vsync_waiter.h.

References AwaitVSync(), FireCallback(), and FML_DISALLOW_COPY_AND_ASSIGN.

Referenced by ScheduleSecondaryCallback().

66 { AwaitVSync(); }
virtual void AwaitVSync()=0

◆ FireCallback()

void flutter::VsyncWaiter::FireCallback ( fml::TimePoint  frame_start_time,
fml::TimePoint  frame_target_time,
bool  pause_secondary_tasks = true 
)
protected

Definition at line 97 of file vsync_waiter.cc.

References callback, FML_DCHECK, FML_TRACE_EVENT, fml::MessageLoopTaskQueues::GetInstance(), fml::TaskRunner::GetTaskQueueId(), flutter::TaskRunners::GetUITaskRunner(), fml::TimePoint::Now(), fml::TaskRunner::PostTask(), fml::TaskRunner::PostTaskForTime(), task_runners_, TRACE_EVENT0, TRACE_EVENT_INSTANT0, TRACE_FLOW_BEGIN, TRACE_FLOW_END, and fml::tracing::TraceNonce().

Referenced by AwaitVSyncForSecondaryCallback(), and flutter::VsyncWaiterIOS::VsyncWaiterIOS.

99  {
100  FML_DCHECK(fml::TimePoint::Now() >= frame_start_time);
101 
103  std::vector<fml::closure> secondary_callbacks;
104 
105  {
106  std::scoped_lock lock(callback_mutex_);
107  callback = std::move(callback_);
108  for (auto& pair : secondary_callbacks_) {
109  secondary_callbacks.push_back(std::move(pair.second));
110  }
111  secondary_callbacks_.clear();
112  }
113 
114  if (!callback && secondary_callbacks.empty()) {
115  // This means that the vsync waiter implementation fired a callback for a
116  // request we did not make. This is a paranoid check but we still want to
117  // make sure we catch misbehaving vsync implementations.
118  TRACE_EVENT_INSTANT0("flutter", "MismatchedFrameCallback");
119  return;
120  }
121 
122  if (callback) {
123  auto flow_identifier = fml::tracing::TraceNonce();
124  if (pause_secondary_tasks) {
125  PauseDartMicroTasks();
126  }
127 
128  // The base trace ensures that flows have a root to begin from if one does
129  // not exist. The trace viewer will ignore traces that have no base event
130  // trace. While all our message loops insert a base trace trace
131  // (MessageLoop::RunExpiredTasks), embedders may not.
132  TRACE_EVENT0("flutter", "VsyncFireCallback");
133 
134  TRACE_FLOW_BEGIN("flutter", kVsyncFlowName, flow_identifier);
135 
136  fml::TaskQueueId ui_task_queue_id =
138 
140  [ui_task_queue_id, callback, flow_identifier, frame_start_time,
141  frame_target_time, pause_secondary_tasks]() {
142  FML_TRACE_EVENT("flutter", kVsyncTraceName, "StartTime",
143  frame_start_time, "TargetTime", frame_target_time);
144  std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder =
145  std::make_unique<FrameTimingsRecorder>();
146  frame_timings_recorder->RecordVsync(frame_start_time,
147  frame_target_time);
148  callback(std::move(frame_timings_recorder));
149  TRACE_FLOW_END("flutter", kVsyncFlowName, flow_identifier);
150  if (pause_secondary_tasks) {
151  ResumeDartMicroTasks(ui_task_queue_id);
152  }
153  });
154  }
155 
156  for (auto& secondary_callback : secondary_callbacks) {
158  std::move(secondary_callback), frame_start_time);
159  }
160 }
virtual TaskQueueId GetTaskQueueId()
Definition: task_runner.cc:38
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:90
#define FML_DCHECK(condition)
Definition: logging.h:86
#define TRACE_EVENT_INSTANT0(category_group, name)
Definition: trace_event.h:119
#define TRACE_FLOW_END(category, name, id)
Definition: trace_event.h:136
virtual void PostTaskForTime(const fml::closure &task, fml::TimePoint target_time)
Definition: task_runner.cc:28
FlKeyEvent FlKeyResponderAsyncCallback callback
static constexpr const char * kVsyncFlowName
Definition: vsync_waiter.cc:17
#define TRACE_FLOW_BEGIN(category, name, id)
Definition: trace_event.h:130
static constexpr const char * kVsyncTraceName
Definition: vsync_waiter.cc:34
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
Definition: task_runners.cc:34
const TaskRunners task_runners_
Definition: vsync_waiter.h:41
std::function< void(std::unique_ptr< FrameTimingsRecorder >)> Callback
Definition: vsync_waiter.h:23
size_t TraceNonce()
Definition: trace_event.cc:298
#define FML_TRACE_EVENT(category_group, name,...)
Definition: trace_event.h:86
virtual void PostTask(const fml::closure &task) override
Definition: task_runner.cc:24
static TimePoint Now()
Definition: time_point.cc:39

◆ ScheduleSecondaryCallback()

void flutter::VsyncWaiter::ScheduleSecondaryCallback ( uintptr_t  id,
const fml::closure callback 
)

Add a secondary callback for key |id| for the next vsync.

See also |PointerDataDispatcher::ScheduleSecondaryVsyncCallback| and |Animator::ScheduleMaybeClearTraceFlowIds|.

Definition at line 69 of file vsync_waiter.cc.

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

70  {
72 
73  if (!callback) {
74  return;
75  }
76 
77  TRACE_EVENT0("flutter", "ScheduleSecondaryCallback");
78 
79  {
80  std::scoped_lock lock(callback_mutex_);
81  auto [_, inserted] = secondary_callbacks_.emplace(id, std::move(callback));
82  if (!inserted) {
83  // Multiple schedules must result in a single callback per frame interval.
84  TRACE_EVENT_INSTANT0("flutter",
85  "MultipleCallsToSecondaryVsyncInFrameInterval");
86  return;
87  }
88  if (callback_) {
89  // Return directly as `AwaitVSync` is already called by
90  // `AsyncWaitForVsync`.
91  return;
92  }
93  }
95 }
#define TRACE_EVENT0(category_group, name)
Definition: trace_event.h:90
#define FML_DCHECK(condition)
Definition: logging.h:86
#define TRACE_EVENT_INSTANT0(category_group, name)
Definition: trace_event.h:119
virtual void AwaitVSyncForSecondaryCallback()
Definition: vsync_waiter.h:66
FlKeyEvent FlKeyResponderAsyncCallback callback
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
Definition: task_runners.cc:34
const TaskRunners task_runners_
Definition: vsync_waiter.h:41
virtual bool RunsTasksOnCurrentThread()
Definition: task_runner.cc:43

Friends And Related Function Documentation

◆ VsyncWaiterAndroid

friend class VsyncWaiterAndroid
friend

Definition at line 38 of file vsync_waiter.h.

◆ VsyncWaiterEmbedder

friend class VsyncWaiterEmbedder
friend

Definition at line 39 of file vsync_waiter.h.

Member Data Documentation

◆ task_runners_


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