Flutter Engine
The 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 "vsync_waiter.h"
6
7#include <cstdint>
8
9#include <lib/async/default.h>
10
11#include "flutter/fml/logging.h"
12#include "flutter/fml/make_copyable.h"
13#include "flutter/fml/synchronization/waitable_event.h"
14#include "flutter/fml/time/time_delta.h"
15#include "flutter/fml/trace_event.h"
16
18
19namespace flutter_runner {
20
23 await_vsync_for_secondary_callback_callback,
24 flutter::TaskRunners task_runners)
25 : flutter::VsyncWaiter(task_runners),
26 await_vsync_callback_(await_vsync_callback),
27 await_vsync_for_secondary_callback_callback_(
28 await_vsync_for_secondary_callback_callback),
29 weak_factory_ui_(nullptr),
30 weak_factory_(this) {
31 fire_callback_callback_ = [this](fml::TimePoint frame_start,
32 fml::TimePoint frame_end) {
34 [frame_start, frame_end, weak_this = weak_ui_]() {
35 if (weak_this) {
36 // Note: It is VERY important to set |pause_secondary_tasks| to
37 // false, else Animator will almost immediately crash on Fuchsia.
38 // FML_LOG(INFO) << "CRASH:: VsyncWaiter about to FireCallback";
39 weak_this->FireCallback(frame_start, frame_end,
40 /*pause_secondary_tasks*/ false);
41 }
42 },
44 };
45
46 // Generate a WeakPtrFactory for use with the UI thread. This does not need
47 // to wait on a latch because we only ever use the WeakPtrFactory on the UI
48 // thread so we have ordering guarantees (see ::AwaitVSync())
51 weak_factory_ui_ =
52 std::make_unique<fml::WeakPtrFactory<VsyncWaiter>>(this);
53 weak_ui_ = weak_factory_ui_->GetWeakPtr();
54 }));
55}
56
62 [weak_factory_ui = std::move(weak_factory_ui_), &ui_latch]() mutable {
63 weak_factory_ui.reset();
64 ui_latch.Signal();
65 }));
66 ui_latch.Wait();
67}
68
69void VsyncWaiter::AwaitVSync() {
70 await_vsync_callback_(fire_callback_callback_);
71}
72
73void VsyncWaiter::AwaitVSyncForSecondaryCallback() {
74 await_vsync_for_secondary_callback_callback_(fire_callback_callback_);
75}
76
77} // namespace flutter_runner
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
Definition: task_runners.cc:34
const TaskRunners task_runners_
Definition: vsync_waiter.h:44
VsyncWaiter(AwaitVsyncCallback await_vsync_callback, AwaitVsyncForSecondaryCallbackCallback await_vsync_for_secondary_callback_callback, flutter::TaskRunners task_runners)
Definition: vsync_waiter.cc:21
static void RunNowOrPostTask(const fml::RefPtr< fml::TaskRunner > &runner, const fml::closure &task)
Definition: task_runner.cc:55
virtual void PostTaskForTime(const fml::closure &task, fml::TimePoint target_time)
Definition: task_runner.cc:28
std::function< void(FireCallbackCallback)> AwaitVsyncForSecondaryCallbackCallback
Definition: vsync_waiter.h:25
std::function< void(FireCallbackCallback)> AwaitVsyncCallback
Definition: vsync_waiter.h:22
internal::CopyableLambda< T > MakeCopyable(T lambda)
Definition: make_copyable.h:57