| 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" |
| 11 | #include "fml/message_loop_task_queues.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 | static constexpr const char* kVsyncTraceName = "VsyncProcessCallback" ; |
| 20 | |
| 21 | VsyncWaiter::VsyncWaiter(const TaskRunners& task_runners) |
| 22 | : task_runners_(task_runners) {} |
| 23 | |
| 24 | VsyncWaiter::~VsyncWaiter() = default; |
| 25 | |
| 26 | // Public method invoked by the animator. |
| 27 | void VsyncWaiter::AsyncWaitForVsync(const Callback& callback) { |
| 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 | |
| 53 | void VsyncWaiter::ScheduleSecondaryCallback(uintptr_t id, |
| 54 | const fml::closure& callback) { |
| 55 | FML_DCHECK(task_runners_.GetUITaskRunner()->RunsTasksOnCurrentThread()); |
| 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(args&: id, args: 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 | } |
| 84 | AwaitVSyncForSecondaryCallback(); |
| 85 | } |
| 86 | |
| 87 | void VsyncWaiter::FireCallback(fml::TimePoint frame_start_time, |
| 88 | fml::TimePoint frame_target_time, |
| 89 | bool pause_secondary_tasks) { |
| 90 | FML_DCHECK(fml::TimePoint::Now() >= frame_start_time); |
| 91 | |
| 92 | Callback callback; |
| 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(x: 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 | PauseDartMicroTasks(); |
| 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 = |
| 129 | task_runners_.GetUITaskRunner()->GetTaskQueueId(); |
| 130 | |
| 131 | task_runners_.GetUITaskRunner()->PostTask( |
| 132 | task: [ui_task_queue_id, callback, flow_identifier, frame_start_time, |
| 133 | frame_target_time, pause_secondary_tasks]() { |
| 134 | FML_TRACE_EVENT_WITH_FLOW_IDS( |
| 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(vsync_start: frame_start_time, |
| 141 | vsync_target: frame_target_time); |
| 142 | callback(std::move(frame_timings_recorder)); |
| 143 | TRACE_FLOW_END("flutter" , kVsyncFlowName, flow_identifier); |
| 144 | if (pause_secondary_tasks) { |
| 145 | ResumeDartMicroTasks(ui_task_queue_id); |
| 146 | } |
| 147 | }); |
| 148 | } |
| 149 | |
| 150 | for (auto& secondary_callback : secondary_callbacks) { |
| 151 | task_runners_.GetUITaskRunner()->PostTask(task: secondary_callback); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | void VsyncWaiter::PauseDartMicroTasks() { |
| 156 | auto ui_task_queue_id = task_runners_.GetUITaskRunner()->GetTaskQueueId(); |
| 157 | auto task_queues = fml::MessageLoopTaskQueues::GetInstance(); |
| 158 | task_queues->PauseSecondarySource(queue_id: ui_task_queue_id); |
| 159 | } |
| 160 | |
| 161 | void VsyncWaiter::ResumeDartMicroTasks(fml::TaskQueueId ui_task_queue_id) { |
| 162 | auto task_queues = fml::MessageLoopTaskQueues::GetInstance(); |
| 163 | task_queues->ResumeSecondarySource(queue_id: ui_task_queue_id); |
| 164 | } |
| 165 | |
| 166 | } // namespace flutter |
| 167 | |