Flutter Engine
flutter::Animator Class Referencefinal

#include <animator.h>

Classes

class  Delegate
 

Public Member Functions

 Animator (Delegate &delegate, TaskRunners task_runners, std::unique_ptr< VsyncWaiter > waiter)
 
 ~Animator ()
 
void RequestFrame (bool regenerate_layer_tree=true)
 
void Render (std::unique_ptr< flutter::LayerTree > layer_tree)
 
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 Start ()
 
void Stop ()
 
void SetDimensionChangePending ()
 
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,
TaskRunners  task_runners,
std::unique_ptr< VsyncWaiter waiter 
)

Definition at line 24 of file animator.cc.

References ~Animator().

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

◆ ~Animator()

flutter::Animator::~Animator ( )
default

Referenced by Animator().

Member Function Documentation

◆ EnqueueTraceFlowId()

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

Definition at line 67 of file animator.cc.

References flutter::TaskRunners::GetUITaskRunner(), and fml::TaskRunner::RunNowOrPostTask().

67  {
69  task_runners_.GetUITaskRunner(),
70  [self = weak_factory_.GetWeakPtr(), trace_flow_id] {
71  if (!self) {
72  return;
73  }
74  self->trace_flow_ids_.push_back(trace_flow_id);
75  self->ScheduleMaybeClearTraceFlowIds();
76  });
77 }
static void RunNowOrPostTask(fml::RefPtr< fml::TaskRunner > runner, const fml::closure &task)
Definition: task_runner.cc:55
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
Definition: task_runners.cc:34

◆ Render()

void flutter::Animator::Render ( std::unique_ptr< flutter::LayerTree layer_tree)

Definition at line 179 of file animator.cc.

References flutter::Pipeline< R >::ProducerContinuation::Complete(), FML_DLOG, fml::TimePoint::Now(), flutter::Animator::Delegate::OnAnimatorDraw(), flutter::Animator::Delegate::OnAnimatorDrawLastLayerTree(), result, fml::Semaphore::Signal(), and TRACE_EVENT_WITH_FRAME_NUMBER.

179  {
180  has_rendered_ = true;
181  if (dimension_change_pending_ &&
182  layer_tree->frame_size() != last_layer_tree_size_) {
183  dimension_change_pending_ = false;
184  }
185  last_layer_tree_size_ = layer_tree->frame_size();
186 
187  if (!frame_timings_recorder_) {
188  // Framework can directly call render with a built scene.
189  frame_timings_recorder_ = std::make_unique<FrameTimingsRecorder>();
190  const fml::TimePoint placeholder_time = fml::TimePoint::Now();
191  frame_timings_recorder_->RecordVsync(placeholder_time, placeholder_time);
192  frame_timings_recorder_->RecordBuildStart(placeholder_time);
193  }
194 
195  TRACE_EVENT_WITH_FRAME_NUMBER(frame_timings_recorder_, "flutter",
196  "Animator::Render");
197  frame_timings_recorder_->RecordBuildEnd(fml::TimePoint::Now());
198 
199  // Commit the pending continuation.
200  bool result = producer_continuation_.Complete(std::move(layer_tree));
201  if (!result) {
202  FML_DLOG(INFO) << "No pending continuation to commit";
203  }
204 
205  delegate_.OnAnimatorDraw(layer_tree_pipeline_,
206  std::move(frame_timings_recorder_));
207 }
bool Complete(ResourcePtr resource)
Definition: pipeline.h:63
GAsyncResult * result
#define TRACE_EVENT_WITH_FRAME_NUMBER(recorder, category_group, name)
Definition: frame_timings.h:16
#define FML_DLOG(severity)
Definition: logging.h:85
virtual void OnAnimatorDraw(std::shared_ptr< Pipeline< flutter::LayerTree >> pipeline, std::unique_ptr< FrameTimingsRecorder > frame_timings_recorder)=0
static TimePoint Now()
Definition: time_point.cc:39

◆ RequestFrame()

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

Definition at line 226 of file animator.cc.

References flutter::TaskRunners::GetUITaskRunner(), flutter::Animator::Delegate::OnAnimatorNotifyIdle(), fml::TaskRunner::PostTask(), fml::TimePoint::ToEpochDelta(), fml::TimeDelta::ToMicroseconds(), TRACE_EVENT_ASYNC_BEGIN0, and fml::Semaphore::TryWait().

Referenced by flutter::FxlToDartOrEarlier(), and Start().

226  {
227  if (regenerate_layer_tree) {
228  regenerate_layer_tree_ = true;
229  }
230  if (paused_ && !dimension_change_pending_) {
231  return;
232  }
233 
234  if (!pending_frame_semaphore_.TryWait()) {
235  // Multiple calls to Animator::RequestFrame will still result in a
236  // single request to the VsyncWaiter.
237  return;
238  }
239 
240  // The AwaitVSync is going to call us back at the next VSync. However, we want
241  // to be reasonably certain that the UI thread is not in the middle of a
242  // particularly expensive callout. We post the AwaitVSync to run right after
243  // an idle. This does NOT provide a guarantee that the UI thread has not
244  // started an expensive operation right after posting this message however.
245  // To support that, we need edge triggered wakes on VSync.
246 
247  task_runners_.GetUITaskRunner()->PostTask(
248  [self = weak_factory_.GetWeakPtr(),
249  frame_request_number = frame_request_number_]() {
250  if (!self) {
251  return;
252  }
253  TRACE_EVENT_ASYNC_BEGIN0("flutter", "Frame Request Pending",
254  frame_request_number);
255  self->AwaitVSync();
256  });
257  frame_scheduled_ = true;
258 }
#define TRACE_EVENT_ASYNC_BEGIN0(category_group, name, id)
Definition: trace_event.h:104
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
Definition: task_runners.cc:34
bool TryWait()
Definition: semaphore.cc:157
virtual void PostTask(const fml::closure &task) override
Definition: task_runner.cc:24

◆ 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 278 of file animator.cc.

References TRACE_EVENT0, and TRACE_FLOW_END.

279  {
280  waiter_->ScheduleSecondaryCallback(id, callback);
281 }
FlKeyEvent FlKeyResponderAsyncCallback callback

◆ SetDimensionChangePending()

void flutter::Animator::SetDimensionChangePending ( )

Definition at line 63 of file animator.cc.

63  {
64  dimension_change_pending_ = true;
65 }

◆ Start()

void flutter::Animator::Start ( )

Definition at line 52 of file animator.cc.

References RequestFrame().

52  {
53  if (!paused_) {
54  return;
55  }
56 
57  paused_ = false;
58  RequestFrame();
59 }
void RequestFrame(bool regenerate_layer_tree=true)
Definition: animator.cc:226

◆ Stop()

void flutter::Animator::Stop ( )

Definition at line 48 of file animator.cc.

48  {
49  paused_ = true;
50 }

Friends And Related Function Documentation

◆ testing::ShellTest

friend class testing::ShellTest
friend

Definition at line 124 of file animator.h.


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