Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
flutter::VsyncWaiter Class Referenceabstract

#include <vsync_waiter.h>

Inheritance diagram for flutter::VsyncWaiter:
flutter::VsyncWaiterAndroid flutter::VsyncWaiterEmbedder flutter::VsyncWaiterFallback flutter::VsyncWaiterIOS flutter::testing::ConstantFiringVsyncWaiter flutter::testing::ShellTestVsyncWaiter flutter::testing::TestVsyncWaiter 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 (const 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.

See also
VsyncWaiterAndroid
VsyncWaiterEmbedder

Definition at line 24 of file vsync_waiter.h.

Member Typedef Documentation

◆ Callback

using flutter::VsyncWaiter::Callback = std::function<void(std::unique_ptr<FrameTimingsRecorder>)>

Definition at line 26 of file vsync_waiter.h.

Constructor & Destructor Documentation

◆ ~VsyncWaiter()

flutter::VsyncWaiter::~VsyncWaiter ( )
virtualdefault

Reimplemented in flutter_runner::VsyncWaiter.

◆ VsyncWaiter()

flutter::VsyncWaiter::VsyncWaiter ( const TaskRunners task_runners)
explicitprotected

Definition at line 21 of file vsync_waiter.cc.

22 : task_runners_(task_runners) {}
const TaskRunners task_runners_

Member Function Documentation

◆ AsyncWaitForVsync()

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

Definition at line 27 of file vsync_waiter.cc.

27 {
28 if (!callback) {
29 return;
30 }
31
32 TRACE_EVENT0("flutter", "AsyncWaitForVsync");
33
34 {
35 std::scoped_lock lock(callback_mutex_);
36 if (callback_) {
37 // The animator may request a frame more than once within a frame
38 // interval. Multiple calls to request frame must result in a single
39 // callback per frame interval.
40 TRACE_EVENT_INSTANT0("flutter", "MultipleCallsToVsyncInFrameInterval");
41 return;
42 }
43 callback_ = callback;
44 if (!secondary_callbacks_.empty()) {
45 // Return directly as `AwaitVSync` is already called by
46 // `ScheduleSecondaryCallback`.
47 return;
48 }
49 }
50 AwaitVSync();
51}
virtual void AwaitVSync()=0
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
#define TRACE_EVENT0(category_group, name)
#define TRACE_EVENT_INSTANT0(category_group, name)

◆ AwaitVSync()

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

◆ AwaitVSyncForSecondaryCallback()

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

Reimplemented in flutter_runner::VsyncWaiter.

Definition at line 69 of file vsync_waiter.h.

69{ AwaitVSync(); }

◆ FireCallback()

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

Definition at line 87 of file vsync_waiter.cc.

89 {
90 FML_DCHECK(fml::TimePoint::Now() >= frame_start_time);
91
93 std::vector<fml::closure> secondary_callbacks;
94
95 {
96 std::scoped_lock lock(callback_mutex_);
97 callback = std::move(callback_);
98 for (auto& pair : secondary_callbacks_) {
99 secondary_callbacks.push_back(std::move(pair.second));
100 }
101 secondary_callbacks_.clear();
102 }
103
104 if (!callback && secondary_callbacks.empty()) {
105 // This means that the vsync waiter implementation fired a callback for a
106 // request we did not make. This is a paranoid check but we still want to
107 // make sure we catch misbehaving vsync implementations.
108 TRACE_EVENT_INSTANT0("flutter", "MismatchedFrameCallback");
109 return;
110 }
111
112 if (callback) {
113 const uint64_t flow_identifier = fml::tracing::TraceNonce();
114 if (pause_secondary_tasks) {
115 PauseDartEventLoopTasks();
116 }
117
118 // The base trace ensures that flows have a root to begin from if one does
119 // not exist. The trace viewer will ignore traces that have no base event
120 // trace. While all our message loops insert a base trace trace
121 // (MessageLoop::RunExpiredTasks), embedders may not.
122 TRACE_EVENT0_WITH_FLOW_IDS("flutter", "VsyncFireCallback",
123 /*flow_id_count=*/1,
124 /*flow_ids=*/&flow_identifier);
125
126 TRACE_FLOW_BEGIN("flutter", kVsyncFlowName, flow_identifier);
127
128 fml::TaskQueueId ui_task_queue_id =
130
132 [ui_task_queue_id, callback, flow_identifier, frame_start_time,
133 frame_target_time, pause_secondary_tasks]() {
135 "flutter", kVsyncTraceName, /*flow_id_count=*/1,
136 /*flow_ids=*/&flow_identifier, "StartTime", frame_start_time,
137 "TargetTime", frame_target_time);
138 std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder =
139 std::make_unique<FrameTimingsRecorder>();
140 frame_timings_recorder->RecordVsync(frame_start_time,
141 frame_target_time);
142 callback(std::move(frame_timings_recorder));
143 TRACE_FLOW_END("flutter", kVsyncFlowName, flow_identifier);
144 if (pause_secondary_tasks) {
145 ResumeDartEventLoopTasks(ui_task_queue_id);
146 }
147 });
148 }
149
150 for (auto& secondary_callback : secondary_callbacks) {
151 task_runners_.GetUITaskRunner()->PostTask(secondary_callback);
152 }
153}
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
std::function< void(std::unique_ptr< FrameTimingsRecorder >)> Callback
virtual void PostTask(const fml::closure &task) override
virtual TaskQueueId GetTaskQueueId()
static TimePoint Now()
Definition time_point.cc:49
#define FML_DCHECK(condition)
Definition logging.h:103
static constexpr const char * kVsyncFlowName
static constexpr const char * kVsyncTraceName
size_t TraceNonce()
#define TRACE_FLOW_BEGIN(category, name, id)
#define FML_TRACE_EVENT_WITH_FLOW_IDS(category_group, name, flow_id_count, flow_ids,...)
#define TRACE_FLOW_END(category, name, id)
#define TRACE_EVENT0_WITH_FLOW_IDS(category_group, name, flow_id_count, flow_ids)

◆ 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 53 of file vsync_waiter.cc.

54 {
56
57 if (!callback) {
58 return;
59 }
60
61 TRACE_EVENT0("flutter", "ScheduleSecondaryCallback");
62
63 {
64 std::scoped_lock lock(callback_mutex_);
65 bool secondary_callbacks_originally_empty = secondary_callbacks_.empty();
66 auto [_, inserted] = secondary_callbacks_.emplace(id, callback);
67 if (!inserted) {
68 // Multiple schedules must result in a single callback per frame interval.
69 TRACE_EVENT_INSTANT0("flutter",
70 "MultipleCallsToSecondaryVsyncInFrameInterval");
71 return;
72 }
73 if (callback_) {
74 // Return directly as `AwaitVSync` is already called by
75 // `AsyncWaitForVsync`.
76 return;
77 }
78 if (!secondary_callbacks_originally_empty) {
79 // Return directly as `AwaitVSync` is already called by
80 // `ScheduleSecondaryCallback`.
81 return;
82 }
83 }
85}
virtual void AwaitVSyncForSecondaryCallback()
virtual bool RunsTasksOnCurrentThread()

Friends And Related Symbol Documentation

◆ VsyncWaiterAndroid

friend class VsyncWaiterAndroid
friend

Definition at line 41 of file vsync_waiter.h.

◆ VsyncWaiterEmbedder

friend class VsyncWaiterEmbedder
friend

Definition at line 42 of file vsync_waiter.h.

Member Data Documentation

◆ task_runners_

const TaskRunners flutter::VsyncWaiter::task_runners_
protected

Definition at line 44 of file vsync_waiter.h.


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