Flutter Engine
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 
7 #include "flow/frame_timings.h"
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 
15 namespace flutter {
16 
17 static constexpr const char* kVsyncFlowName = "VsyncFlow";
18 
19 #if defined(OS_FUCHSIA)
20 // ________ _________ ________ ________
21 // |\ ____\|\___ ___\\ __ \|\ __ \
22 // \ \ \___|\|___ \ \_\ \ \|\ \ \ \|\ \
23 // \ \_____ \ \ \ \ \ \ \\\ \ \ ____\
24 // \|____|\ \ \ \ \ \ \ \\\ \ \ \___|
25 // ____\_\ \ \ \__\ \ \_______\ \__\
26 // |\_________\ \|__| \|_______|\|__|
27 // \|_________|
28 //
29 // Fuchsia benchmarks depend on this trace event's name. Please do not change
30 // it without checking that the changes are compatible with Fuchsia benchmarks
31 // first!
32 static constexpr const char* kVsyncTraceName = "vsync callback";
33 #else
34 static constexpr const char* kVsyncTraceName = "VsyncProcessCallback";
35 #endif
36 
38  : task_runners_(std::move(task_runners)) {}
39 
40 VsyncWaiter::~VsyncWaiter() = default;
41 
42 // Public method invoked by the animator.
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 }
68 
70  const fml::closure& callback) {
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 }
96 
98  fml::TimePoint frame_target_time,
99  bool pause_secondary_tasks) {
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 }
161 
162 void VsyncWaiter::PauseDartMicroTasks() {
163  auto ui_task_queue_id = task_runners_.GetUITaskRunner()->GetTaskQueueId();
164  auto task_queues = fml::MessageLoopTaskQueues::GetInstance();
165  task_queues->PauseSecondarySource(ui_task_queue_id);
166 }
167 
168 void VsyncWaiter::ResumeDartMicroTasks(fml::TaskQueueId ui_task_queue_id) {
169  auto task_queues = fml::MessageLoopTaskQueues::GetInstance();
170  task_queues->ResumeSecondarySource(ui_task_queue_id);
171 }
172 
173 } // namespace flutter
VsyncWaiter(TaskRunners task_runners)
Definition: vsync_waiter.cc:37
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 AwaitVSyncForSecondaryCallback()
Definition: vsync_waiter.h:66
Definition: ref_ptr.h:252
virtual void PostTaskForTime(const fml::closure &task, fml::TimePoint target_time)
Definition: task_runner.cc:28
void FireCallback(fml::TimePoint frame_start_time, fml::TimePoint frame_target_time, bool pause_secondary_tasks=true)
Definition: vsync_waiter.cc:97
FlKeyEvent FlKeyResponderAsyncCallback callback
void ScheduleSecondaryCallback(uintptr_t id, const fml::closure &callback)
Definition: vsync_waiter.cc:69
static constexpr const char * kVsyncFlowName
Definition: vsync_waiter.cc:17
std::function< void()> closure
Definition: closure.h:14
#define TRACE_FLOW_BEGIN(category, name, id)
Definition: trace_event.h:130
static fml::RefPtr< MessageLoopTaskQueues > GetInstance()
static constexpr const char * kVsyncTraceName
Definition: vsync_waiter.cc:34
virtual void AwaitVSync()=0
void AsyncWaitForVsync(const Callback &callback)
Definition: vsync_waiter.cc:43
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
Definition: task_runners.cc:34
const TaskRunners task_runners_
Definition: vsync_waiter.h:41
TaskRunners task_runners_
virtual bool RunsTasksOnCurrentThread()
Definition: task_runner.cc:43
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