Flutter Engine
The Flutter Engine
vsync_waiter.h
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#ifndef FLUTTER_SHELL_COMMON_VSYNC_WAITER_H_
6#define FLUTTER_SHELL_COMMON_VSYNC_WAITER_H_
7
8#include <functional>
9#include <memory>
10#include <mutex>
11#include <unordered_map>
12
13#include "flutter/common/task_runners.h"
14#include "flutter/flow/frame_timings.h"
15#include "flutter/fml/time/time_point.h"
16
17namespace flutter {
18
19/// Abstract Base Class that represents a platform specific mechanism for
20/// getting callbacks when a vsync event happens.
21///
22/// @see VsyncWaiterAndroid
23/// @see VsyncWaiterEmbedder
24class VsyncWaiter : public std::enable_shared_from_this<VsyncWaiter> {
25 public:
26 using Callback = std::function<void(std::unique_ptr<FrameTimingsRecorder>)>;
27
28 virtual ~VsyncWaiter();
29
31
32 /// Add a secondary callback for key |id| for the next vsync.
33 ///
34 /// See also |PointerDataDispatcher::ScheduleSecondaryVsyncCallback| and
35 /// |Animator::ScheduleMaybeClearTraceFlowIds|.
36 void ScheduleSecondaryCallback(uintptr_t id, const fml::closure& callback);
37
38 protected:
39 // On some backends, the |FireCallback| needs to be made from a static C
40 // method.
41 friend class VsyncWaiterAndroid;
42 friend class VsyncWaiterEmbedder;
43
45
46 explicit VsyncWaiter(const TaskRunners& task_runners);
47
48 // There are two distinct situations where VsyncWaiter wishes to awaken at
49 // the next vsync. Although the functionality can be the same, the intent is
50 // different, therefore it makes sense to have a method for each intent.
51
52 // The intent of AwaitVSync() is that the Animator wishes to produce a frame.
53 // The underlying implementation can choose to be aware of this intent when
54 // it comes to implementing backpressure and other scheduling invariants.
55 //
56 // Implementations are meant to override this method and arm their vsync
57 // latches when in response to this invocation. On vsync, they are meant to
58 // invoke the |FireCallback| method once (and only once) with the appropriate
59 // arguments. This method should not block the current thread.
60 virtual void AwaitVSync() = 0;
61
62 // The intent of AwaitVSyncForSecondaryCallback() is simply to wake up at the
63 // next vsync.
64 //
65 // Because there is no association with frame scheduling, underlying
66 // implementations do not need to worry about maintaining invariants or
67 // backpressure. The default implementation is to simply follow the same logic
68 // as AwaitVSync().
70
71 // Schedules the callback on the UI task runner. Needs to be invoked as close
72 // to the `frame_start_time` as possible.
73 void FireCallback(fml::TimePoint frame_start_time,
74 fml::TimePoint frame_target_time,
75 bool pause_secondary_tasks = true);
76
77 private:
78 std::mutex callback_mutex_;
79 Callback callback_;
80 std::unordered_map<uintptr_t, fml::closure> secondary_callbacks_;
81
82 void PauseDartEventLoopTasks();
83 static void ResumeDartEventLoopTasks(fml::TaskQueueId ui_task_queue_id);
84
86};
87
88} // namespace flutter
89
90#endif // FLUTTER_SHELL_COMMON_VSYNC_WAITER_H_
void ScheduleSecondaryCallback(uintptr_t id, const fml::closure &callback)
Definition: vsync_waiter.cc:53
std::function< void(std::unique_ptr< FrameTimingsRecorder >)> Callback
Definition: vsync_waiter.h:26
VsyncWaiter(const TaskRunners &task_runners)
Definition: vsync_waiter.cc:21
virtual void AwaitVSync()=0
void AsyncWaitForVsync(const Callback &callback)
Definition: vsync_waiter.cc:27
void FireCallback(fml::TimePoint frame_start_time, fml::TimePoint frame_target_time, bool pause_secondary_tasks=true)
Definition: vsync_waiter.cc:87
virtual void AwaitVSyncForSecondaryCallback()
Definition: vsync_waiter.h:69
const TaskRunners task_runners_
Definition: vsync_waiter.h:44
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27
Dart_NativeFunction function
Definition: fuchsia.cc:51
std::function< void()> closure
Definition: closure.h:14