Flutter Engine
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 
20 namespace flutter {
21 
22 namespace testing {
23 class 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|.
30 class 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(int64_t deadline) = 0;
38 
39  virtual void OnAnimatorDraw(
40  std::shared_ptr<Pipeline<flutter::LayerTree>> pipeline,
41  std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder) = 0;
42 
43  virtual void OnAnimatorDrawLastLayerTree(
44  std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder) = 0;
45  };
46 
47  Animator(Delegate& delegate,
48  TaskRunners task_runners,
49  std::unique_ptr<VsyncWaiter> waiter);
50 
51  ~Animator();
52 
53  void RequestFrame(bool regenerate_layer_tree = true);
54 
55  void Render(std::unique_ptr<flutter::LayerTree> layer_tree);
56 
57  //--------------------------------------------------------------------------
58  /// @brief Schedule a secondary callback to be executed right after the
59  /// main `VsyncWaiter::AsyncWaitForVsync` callback (which is added
60  /// by `Animator::RequestFrame`).
61  ///
62  /// Like the callback in `AsyncWaitForVsync`, this callback is
63  /// only scheduled to be called once, and it's supposed to be
64  /// called in the UI thread. If there is no AsyncWaitForVsync
65  /// callback (`Animator::RequestFrame` is not called), this
66  /// secondary callback will still be executed at vsync.
67  ///
68  /// This callback is used to provide the vsync signal needed by
69  /// `SmoothPointerDataDispatcher`, and for our own flow events.
70  ///
71  /// @see `PointerDataDispatcher::ScheduleSecondaryVsyncCallback`.
72  void ScheduleSecondaryVsyncCallback(uintptr_t id,
73  const fml::closure& callback);
74 
75  void Start();
76 
77  void Stop();
78 
79  void SetDimensionChangePending();
80 
81  // Enqueue |trace_flow_id| into |trace_flow_ids_|. The flow event will be
82  // ended at either the next frame, or the next vsync interval with no active
83  // active rendering.
84  void EnqueueTraceFlowId(uint64_t trace_flow_id);
85 
86  private:
88 
89  void BeginFrame(std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder);
90 
91  bool CanReuseLastLayerTree();
92 
93  void DrawLastLayerTree(
94  std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder);
95 
96  void AwaitVSync();
97 
98  const char* FrameParity();
99 
100  // Clear |trace_flow_ids_| if |frame_scheduled_| is false.
101  void ScheduleMaybeClearTraceFlowIds();
102 
105  std::shared_ptr<VsyncWaiter> waiter_;
106 
107  std::unique_ptr<FrameTimingsRecorder> frame_timings_recorder_;
108  uint64_t frame_request_number_ = 1;
109  fml::TimePoint dart_frame_deadline_;
110  std::shared_ptr<LayerTreePipeline> layer_tree_pipeline_;
111  fml::Semaphore pending_frame_semaphore_;
112  LayerTreePipeline::ProducerContinuation producer_continuation_;
113  bool paused_ = true;
114  bool regenerate_layer_tree_ = false;
115  bool frame_scheduled_ = false;
116  int notify_idle_task_id_ = 0;
117  bool dimension_change_pending_ = false;
118  SkISize last_layer_tree_size_ = {0, 0};
119  std::deque<uint64_t> trace_flow_ids_;
120  bool has_rendered_ = false;
121 
122  fml::WeakPtrFactory<Animator> weak_factory_;
123 
124  friend class testing::ShellTest;
125 
127 };
128 
129 } // namespace flutter
130 
131 #endif // FLUTTER_SHELL_COMMON_ANIMATOR_H_
MockDelegate delegate_
FlKeyEvent FlKeyResponderAsyncCallback callback
std::function< void()> closure
Definition: closure.h:14
TaskRunners task_runners_
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27