Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
animator.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_ANIMATOR_H_
6#define FLUTTER_SHELL_COMMON_ANIMATOR_H_
7
8#include <deque>
9
10#include "flutter/common/task_runners.h"
11#include "flutter/flow/frame_timings.h"
12#include "flutter/fml/memory/ref_ptr.h"
13#include "flutter/fml/memory/weak_ptr.h"
14#include "flutter/fml/synchronization/semaphore.h"
15#include "flutter/fml/time/time_point.h"
16#include "flutter/shell/common/pipeline.h"
17#include "flutter/shell/common/rasterizer.h"
18#include "flutter/shell/common/vsync_waiter.h"
19
20namespace flutter {
21
22namespace testing {
23class ShellTest;
24}
25
26/// Executor of animations.
27///
28/// In conjunction with the |VsyncWaiter| it allows callers (typically Dart
29/// code) to schedule work that ends up generating a |LayerTree|.
30class Animator final {
31 public:
32 class Delegate {
33 public:
34 virtual void OnAnimatorBeginFrame(fml::TimePoint frame_target_time,
35 uint64_t frame_number) = 0;
36
37 virtual void OnAnimatorNotifyIdle(fml::TimeDelta deadline) = 0;
38
40 fml::TimePoint frame_target_time) = 0;
41
42 virtual void OnAnimatorDraw(std::shared_ptr<FramePipeline> pipeline) = 0;
43
45 std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder) = 0;
46 };
47
48 Animator(Delegate& delegate,
49 const TaskRunners& task_runners,
50 std::unique_ptr<VsyncWaiter> waiter);
51
53
54 void RequestFrame(bool regenerate_layer_trees = true);
55
56 //--------------------------------------------------------------------------
57 /// @brief Tells the Animator that all views that should render for this
58 /// frame have been rendered.
59 ///
60 /// In regular frames, since all `Render` calls must take place
61 /// during a vsync task, the Animator knows that all views have
62 /// been rendered at the end of the vsync task, therefore calling
63 /// this method is not needed.
64 ///
65 /// However, the engine might decide to start it a bit earlier, for
66 /// example, if the engine decides that no more views can be
67 /// rendered, so that the rasterization can start a bit earlier.
68 ///
69 /// This method is also useful in warm-up frames. In a warm up
70 /// frame, `Animator::Render` is called out of vsync tasks, and
71 /// Animator requires an explicit end-of-frame call to know when to
72 /// send the layer trees to the pipeline.
73 ///
74 /// For more about warm up frames, see
75 /// `PlatformDispatcher.scheduleWarmUpFrame`.
76 ///
77 void OnAllViewsRendered();
78
79 //--------------------------------------------------------------------------
80 /// @brief Tells the Animator that this frame needs to render another view.
81 ///
82 /// This method must be called during a vsync callback, or
83 /// technically, between Animator::BeginFrame and Animator::EndFrame
84 /// (both private methods). Otherwise, this call will be ignored.
85 ///
86 void Render(int64_t view_id,
87 std::unique_ptr<flutter::LayerTree> layer_tree,
88 float device_pixel_ratio);
89
90 const std::weak_ptr<VsyncWaiter> GetVsyncWaiter() const;
91
92 //--------------------------------------------------------------------------
93 /// @brief Schedule a secondary callback to be executed right after the
94 /// main `VsyncWaiter::AsyncWaitForVsync` callback (which is added
95 /// by `Animator::RequestFrame`).
96 ///
97 /// Like the callback in `AsyncWaitForVsync`, this callback is
98 /// only scheduled to be called once, and it's supposed to be
99 /// called in the UI thread. If there is no AsyncWaitForVsync
100 /// callback (`Animator::RequestFrame` is not called), this
101 /// secondary callback will still be executed at vsync.
102 ///
103 /// This callback is used to provide the vsync signal needed by
104 /// `SmoothPointerDataDispatcher`, and for our own flow events.
105 ///
106 /// @see `PointerDataDispatcher::ScheduleSecondaryVsyncCallback`.
107 void ScheduleSecondaryVsyncCallback(uintptr_t id,
108 const fml::closure& callback);
109
110 // Enqueue |trace_flow_id| into |trace_flow_ids_|. The flow event will be
111 // ended at either the next frame, or the next vsync interval with no active
112 // rendering.
113 void EnqueueTraceFlowId(uint64_t trace_flow_id);
114
115 private:
116 // Animator's work during a vsync is split into two methods, BeginFrame and
117 // EndFrame. The two methods should be called synchronously back-to-back to
118 // avoid being interrupted by a regular vsync. The reason to split them is to
119 // allow ShellTest::PumpOneFrame to insert a Render in between.
120
121 void BeginFrame(std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder);
122 void EndFrame();
123
124 bool CanReuseLastLayerTrees();
125
126 void DrawLastLayerTrees(
127 std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder);
128
129 void AwaitVSync();
130
131 // Clear |trace_flow_ids_| if |frame_scheduled_| is false.
132 void ScheduleMaybeClearTraceFlowIds();
133
134 Delegate& delegate_;
135 TaskRunners task_runners_;
136 std::shared_ptr<VsyncWaiter> waiter_;
137
138 std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder_;
139 std::unordered_map<int64_t, std::unique_ptr<LayerTreeTask>>
140 layer_trees_tasks_;
141 uint64_t frame_request_number_ = 1;
142 fml::TimeDelta dart_frame_deadline_;
143 std::shared_ptr<FramePipeline> layer_tree_pipeline_;
144 fml::Semaphore pending_frame_semaphore_;
145 FramePipeline::ProducerContinuation producer_continuation_;
146 bool regenerate_layer_trees_ = false;
147 bool frame_scheduled_ = false;
148 std::deque<uint64_t> trace_flow_ids_;
149 bool has_rendered_ = false;
150
151 fml::WeakPtrFactory<Animator> weak_factory_;
152
153 friend class testing::ShellTest;
154
156};
157
158} // namespace flutter
159
160#endif // FLUTTER_SHELL_COMMON_ANIMATOR_H_
virtual void OnAnimatorNotifyIdle(fml::TimeDelta deadline)=0
virtual void OnAnimatorBeginFrame(fml::TimePoint frame_target_time, uint64_t frame_number)=0
virtual void OnAnimatorDrawLastLayerTrees(std::unique_ptr< FrameTimingsRecorder > frame_timings_recorder)=0
virtual void OnAnimatorUpdateLatestFrameTargetTime(fml::TimePoint frame_target_time)=0
virtual void OnAnimatorDraw(std::shared_ptr< FramePipeline > pipeline)=0
void OnAllViewsRendered()
Tells the Animator that all views that should render for this frame have been rendered.
Definition animator.cc:291
const std::weak_ptr< VsyncWaiter > GetVsyncWaiter() const
Definition animator.cc:213
void ScheduleSecondaryVsyncCallback(uintptr_t id, const fml::closure &callback)
Schedule a secondary callback to be executed right after the main VsyncWaiter::AsyncWaitForVsync call...
Definition animator.cc:297
void EnqueueTraceFlowId(uint64_t trace_flow_id)
Definition animator.cc:49
void Render(int64_t view_id, std::unique_ptr< flutter::LayerTree > layer_tree, float device_pixel_ratio)
Tells the Animator that this frame needs to render another view.
Definition animator.cc:188
void RequestFrame(bool regenerate_layer_trees=true)
Definition animator.cc:239
A traditional counting semaphore. Waits decrement the counter and Signal increments it.
Definition semaphore.h:26
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition macros.h:27
std::function< void()> closure
Definition closure.h:14