Flutter Engine
The Flutter Engine
Classes | Public Member Functions | Friends | List of all members
flutter::Animator Class Referencefinal

#include <animator.h>

Classes

class  Delegate
 

Public Member Functions

 Animator (Delegate &delegate, const TaskRunners &task_runners, std::unique_ptr< VsyncWaiter > waiter)
 
 ~Animator ()
 
void RequestFrame (bool regenerate_layer_trees=true)
 
void OnAllViewsRendered ()
 Tells the Animator that all views that should render for this frame have been rendered. More...
 
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. More...
 
const std::weak_ptr< VsyncWaiterGetVsyncWaiter () const
 
void ScheduleSecondaryVsyncCallback (uintptr_t id, const fml::closure &callback)
 Schedule a secondary callback to be executed right after the main VsyncWaiter::AsyncWaitForVsync callback (which is added by Animator::RequestFrame). More...
 
void EnqueueTraceFlowId (uint64_t trace_flow_id)
 

Friends

class testing::ShellTest
 

Detailed Description

Executor of animations.

In conjunction with the |VsyncWaiter| it allows callers (typically Dart code) to schedule work that ends up generating a |LayerTree|.

Definition at line 30 of file animator.h.

Constructor & Destructor Documentation

◆ Animator()

flutter::Animator::Animator ( Delegate delegate,
const TaskRunners task_runners,
std::unique_ptr< VsyncWaiter waiter 
)

Definition at line 25 of file animator.cc.

28 : delegate_(delegate),
29 task_runners_(task_runners),
30 waiter_(std::move(waiter)),
31#if SHELL_ENABLE_METAL
32 layer_tree_pipeline_(std::make_shared<FramePipeline>(2)),
33#else // SHELL_ENABLE_METAL
34 // TODO(dnfield): We should remove this logic and set the pipeline depth
35 // back to 2 in this case. See
36 // https://github.com/flutter/engine/pull/9132 for discussion.
37 layer_tree_pipeline_(std::make_shared<FramePipeline>(
38 task_runners.GetPlatformTaskRunner() ==
39 task_runners.GetRasterTaskRunner()
40 ? 1
41 : 2)),
42#endif // SHELL_ENABLE_METAL
43 pending_frame_semaphore_(1),
44 weak_factory_(this) {
45}

◆ ~Animator()

flutter::Animator::~Animator ( )
default

Member Function Documentation

◆ EnqueueTraceFlowId()

void flutter::Animator::EnqueueTraceFlowId ( uint64_t  trace_flow_id)

Definition at line 49 of file animator.cc.

49 {
51 task_runners_.GetUITaskRunner(),
52 [self = weak_factory_.GetWeakPtr(), trace_flow_id] {
53 if (!self) {
54 return;
55 }
56 self->trace_flow_ids_.push_back(trace_flow_id);
57 self->ScheduleMaybeClearTraceFlowIds();
58 });
59}
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
Definition: task_runners.cc:34
static void RunNowOrPostTask(const fml::RefPtr< fml::TaskRunner > &runner, const fml::closure &task)
Definition: task_runner.cc:55

◆ GetVsyncWaiter()

const std::weak_ptr< VsyncWaiter > flutter::Animator::GetVsyncWaiter ( ) const

Definition at line 213 of file animator.cc.

213 {
214 std::weak_ptr<VsyncWaiter> weak = waiter_;
215 return weak;
216}

◆ OnAllViewsRendered()

void flutter::Animator::OnAllViewsRendered ( )

Tells the Animator that all views that should render for this frame have been rendered.

In regular frames, since all Render calls must take place during a vsync task, the Animator knows that all views have been rendered at the end of the vsync task, therefore calling this method is not needed.

However, the engine might decide to start it a bit earlier, for example, if the engine decides that no more views can be rendered, so that the rasterization can start a bit earlier.

This method is also useful in warm-up frames. In a warm up frame, Animator::Render is called out of vsync tasks, and Animator requires an explicit end-of-frame call to know when to send the layer trees to the pipeline.

For more about warm up frames, see PlatformDispatcher.scheduleWarmUpFrame.

Definition at line 291 of file animator.cc.

291 {
292 if (!layer_trees_tasks_.empty()) {
293 EndFrame();
294 }
295}

◆ Render()

void flutter::Animator::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.

      This method must be called during a vsync callback, or
      technically, between Animator::BeginFrame and Animator::EndFrame
      (both private methods). Otherwise, this call will be ignored.

Definition at line 188 of file animator.cc.

190 {
191 has_rendered_ = true;
192
193 if (!frame_timings_recorder_) {
194 // Framework can directly call render with a built scene. A major reason is
195 // to render warm up frames.
196 frame_timings_recorder_ = std::make_unique<FrameTimingsRecorder>();
197 const fml::TimePoint placeholder_time = fml::TimePoint::Now();
198 frame_timings_recorder_->RecordVsync(placeholder_time, placeholder_time);
199 frame_timings_recorder_->RecordBuildStart(placeholder_time);
200 }
201
202 TRACE_EVENT_WITH_FRAME_NUMBER(frame_timings_recorder_, "flutter",
203 "Animator::Render", /*flow_id_count=*/0,
204 /*flow_ids=*/nullptr);
205
206 // Only inserts if the view ID has not been rendered before, ignoring
207 // duplicate Render calls.
208 layer_trees_tasks_.try_emplace(
209 view_id, std::make_unique<LayerTreeTask>(view_id, std::move(layer_tree),
210 device_pixel_ratio));
211}
static TimePoint Now()
Definition: time_point.cc:49
#define TRACE_EVENT_WITH_FRAME_NUMBER(recorder, category_group, name, flow_id_count, flow_ids)
Definition: frame_timings.h:17

◆ RequestFrame()

void flutter::Animator::RequestFrame ( bool  regenerate_layer_trees = true)

Definition at line 239 of file animator.cc.

239 {
240 if (regenerate_layer_trees && !regenerate_layer_trees_) {
241 // This event will be closed by BeginFrame. BeginFrame will only be called
242 // if regenerating the layer trees. If a frame has been requested to update
243 // an external texture, this will be false and no BeginFrame call will
244 // happen.
245 TRACE_EVENT_ASYNC_BEGIN0("flutter", "Frame Request Pending",
246 frame_request_number_);
247 regenerate_layer_trees_ = true;
248 }
249
250 if (!pending_frame_semaphore_.TryWait()) {
251 // Multiple calls to Animator::RequestFrame will still result in a
252 // single request to the VsyncWaiter.
253 return;
254 }
255
256 // The AwaitVSync is going to call us back at the next VSync. However, we want
257 // to be reasonably certain that the UI thread is not in the middle of a
258 // particularly expensive callout. We post the AwaitVSync to run right after
259 // an idle. This does NOT provide a guarantee that the UI thread has not
260 // started an expensive operation right after posting this message however.
261 // To support that, we need edge triggered wakes on VSync.
262
263 task_runners_.GetUITaskRunner()->PostTask(
264 [self = weak_factory_.GetWeakPtr()]() {
265 if (!self) {
266 return;
267 }
268 self->AwaitVSync();
269 });
270 frame_scheduled_ = true;
271}
bool TryWait()
Decrement the counts if it is greater than zero. Returns false if the counter is already at zero.
Definition: semaphore.cc:185
virtual void PostTask(const fml::closure &task) override
Definition: task_runner.cc:24
#define TRACE_EVENT_ASYNC_BEGIN0(category_group, name, id)
Definition: trace_event.h:157

◆ ScheduleSecondaryVsyncCallback()

void flutter::Animator::ScheduleSecondaryVsyncCallback ( uintptr_t  id,
const fml::closure callback 
)

Schedule a secondary callback to be executed right after the main VsyncWaiter::AsyncWaitForVsync callback (which is added by Animator::RequestFrame).

Like the callback in AsyncWaitForVsync, this callback is only scheduled to be called once, and it's supposed to be called in the UI thread. If there is no AsyncWaitForVsync callback (Animator::RequestFrame is not called), this secondary callback will still be executed at vsync.

This callback is used to provide the vsync signal needed by SmoothPointerDataDispatcher, and for our own flow events.

See also
PointerDataDispatcher::ScheduleSecondaryVsyncCallback.

Definition at line 297 of file animator.cc.

298 {
299 waiter_->ScheduleSecondaryCallback(id, callback);
300}
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback

Friends And Related Function Documentation

◆ testing::ShellTest

friend class testing::ShellTest
friend

Definition at line 153 of file animator.h.


The documentation for this class was generated from the following files: