Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
vsync_waiter.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter 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#include "flutter/shell/common/vsync_waiter.h"
6
8#include "flutter/fml/task_runner.h"
9#include "flutter/fml/trace_event.h"
10#include "fml/logging.h"
12#include "fml/task_queue_id.h"
13#include "fml/time/time_point.h"
14
15namespace flutter {
16
17static constexpr const char* kVsyncFlowName = "VsyncFlow";
18
19static constexpr const char* kVsyncTraceName = "VsyncProcessCallback";
20
22 : task_runners_(task_runners) {}
23
25
26// Public method invoked by the animator.
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}
52
54 const fml::closure& callback) {
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}
86
88 fml::TimePoint frame_target_time,
89 bool pause_secondary_tasks) {
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}
154
155void VsyncWaiter::PauseDartEventLoopTasks() {
156 auto ui_task_queue_id = task_runners_.GetUITaskRunner()->GetTaskQueueId();
157 auto task_queues = fml::MessageLoopTaskQueues::GetInstance();
158 task_queues->PauseSecondarySource(ui_task_queue_id);
159}
160
161void VsyncWaiter::ResumeDartEventLoopTasks(fml::TaskQueueId ui_task_queue_id) {
162 auto task_queues = fml::MessageLoopTaskQueues::GetInstance();
163 task_queues->ResumeSecondarySource(ui_task_queue_id);
164}
165
166} // namespace flutter
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
void ScheduleSecondaryCallback(uintptr_t id, const fml::closure &callback)
std::function< void(std::unique_ptr< FrameTimingsRecorder >)> Callback
VsyncWaiter(const TaskRunners &task_runners)
virtual void AwaitVSync()=0
void AsyncWaitForVsync(const Callback &callback)
void FireCallback(fml::TimePoint frame_start_time, fml::TimePoint frame_target_time, bool pause_secondary_tasks=true)
virtual void AwaitVSyncForSecondaryCallback()
const TaskRunners task_runners_
static MessageLoopTaskQueues * GetInstance()
virtual void PostTask(const fml::closure &task) override
virtual bool RunsTasksOnCurrentThread()
virtual TaskQueueId GetTaskQueueId()
static TimePoint Now()
Definition time_point.cc:49
TaskRunners task_runners_
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
#define FML_DCHECK(condition)
Definition logging.h:103
static constexpr const char * kVsyncFlowName
static constexpr const char * kVsyncTraceName
size_t TraceNonce()
std::function< void()> closure
Definition closure.h:14
#define TRACE_FLOW_BEGIN(category, name, id)
#define TRACE_EVENT0(category_group, name)
#define FML_TRACE_EVENT_WITH_FLOW_IDS(category_group, name, flow_id_count, flow_ids,...)
#define TRACE_EVENT_INSTANT0(category_group, name)
#define TRACE_FLOW_END(category, name, id)
#define TRACE_EVENT0_WITH_FLOW_IDS(category_group, name, flow_id_count, flow_ids)