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 "flutter/fml/task_runner.h"
8 #include "flutter/fml/trace_event.h"
9
10 namespace flutter {
11
12 #if defined(OS_FUCHSIA)
13 // In general, traces on Fuchsia are recorded across the whole system.
14 // Because of this, emitting a "VSYNC" event per flutter process is
15 // undesirable, as the events will collide with each other. We
16 // instead let another area of the system emit them.
17 static constexpr const char* kVsyncTraceName = "vsync callback";
18 #else // defined(OS_FUCHSIA)
19 // Note: The tag name must be "VSYNC" (it is special) so that the
20 // "Highlight Vsync" checkbox in the timeline can be enabled.
21 static constexpr const char* kVsyncTraceName = "VSYNC";
22 #endif // defined(OS_FUCHSIA)
23
24 static constexpr const char* kVsyncFlowName = "VsyncFlow";
25
VsyncWaiter(TaskRunners task_runners)26 VsyncWaiter::VsyncWaiter(TaskRunners task_runners)
27 : task_runners_(std::move(task_runners)) {}
28
29 VsyncWaiter::~VsyncWaiter() = default;
30
31 // Public method invoked by the animator.
AsyncWaitForVsync(Callback callback)32 void VsyncWaiter::AsyncWaitForVsync(Callback callback) {
33 if (!callback) {
34 return;
35 }
36
37 TRACE_EVENT0("flutter", "AsyncWaitForVsync");
38
39 {
40 std::scoped_lock lock(callback_mutex_);
41 if (callback_) {
42 // The animator may request a frame more than once within a frame
43 // interval. Multiple calls to request frame must result in a single
44 // callback per frame interval.
45 TRACE_EVENT_INSTANT0("flutter", "MultipleCallsToVsyncInFrameInterval");
46 return;
47 }
48 callback_ = std::move(callback);
49 }
50 AwaitVSync();
51 }
52
FireCallback(fml::TimePoint frame_start_time,fml::TimePoint frame_target_time)53 void VsyncWaiter::FireCallback(fml::TimePoint frame_start_time,
54 fml::TimePoint frame_target_time) {
55 Callback callback;
56
57 {
58 std::scoped_lock lock(callback_mutex_);
59 callback = std::move(callback_);
60 }
61
62 if (!callback) {
63 // This means that the vsync waiter implementation fired a callback for a
64 // request we did not make. This is a paranoid check but we still want to
65 // make sure we catch misbehaving vsync implementations.
66 TRACE_EVENT_INSTANT0("flutter", "MismatchedFrameCallback");
67 return;
68 }
69
70 auto flow_identifier = fml::tracing::TraceNonce();
71
72 // The base trace ensures that flows have a root to begin from if one does not
73 // exist. The trace viewer will ignore traces that have no base event trace.
74 // While all our message loops insert a base trace trace
75 // (MessageLoop::RunExpiredTasks), embedders may not.
76 TRACE_EVENT0("flutter", "VsyncFireCallback");
77
78 TRACE_FLOW_BEGIN("flutter", kVsyncFlowName, flow_identifier);
79
80 task_runners_.GetUITaskRunner()->PostTaskForTime(
81 [callback, flow_identifier, frame_start_time, frame_target_time]() {
82 FML_TRACE_EVENT("flutter", kVsyncTraceName, "StartTime",
83 frame_start_time, "TargetTime", frame_target_time);
84 fml::tracing::TraceEventAsyncComplete(
85 "flutter", "VsyncSchedulingOverhead", fml::TimePoint::Now(),
86 frame_start_time);
87 callback(frame_start_time, frame_target_time);
88 TRACE_FLOW_END("flutter", kVsyncFlowName, flow_identifier);
89 },
90 frame_start_time);
91 }
92
GetDisplayRefreshRate() const93 float VsyncWaiter::GetDisplayRefreshRate() const {
94 return kUnknownRefreshRateFPS;
95 }
96
97 } // namespace flutter
98