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/fml/memory/ref_ptr.h"
12 #include "flutter/fml/memory/weak_ptr.h"
13 #include "flutter/fml/synchronization/semaphore.h"
14 #include "flutter/fml/time/time_point.h"
15 #include "flutter/shell/common/pipeline.h"
16 #include "flutter/shell/common/rasterizer.h"
17 #include "flutter/shell/common/vsync_waiter.h"
18 
19 namespace flutter {
20 
21 namespace testing {
22 class ShellTest;
23 }
24 
25 /// Executor of animations.
26 ///
27 /// In conjunction with the |VsyncWaiter| it allows callers (typically Dart
28 /// code) to schedule work that ends up generating a |LayerTree|.
29 class Animator final {
30  public:
31  class Delegate {
32  public:
33  virtual void OnAnimatorBeginFrame(fml::TimePoint frame_target_time) = 0;
34 
35  virtual void OnAnimatorNotifyIdle(int64_t deadline) = 0;
36 
37  virtual void OnAnimatorDraw(
39  fml::TimePoint frame_target_time) = 0;
40 
41  virtual void OnAnimatorDrawLastLayerTree() = 0;
42  };
43 
44  Animator(Delegate& delegate,
45  TaskRunners task_runners,
46  std::unique_ptr<VsyncWaiter> waiter);
47 
48  ~Animator();
49 
50  void RequestFrame(bool regenerate_layer_tree = true);
51 
52  void Render(std::unique_ptr<flutter::LayerTree> layer_tree);
53 
54  //--------------------------------------------------------------------------
55  /// @brief Schedule a secondary callback to be executed right after the
56  /// main `VsyncWaiter::AsyncWaitForVsync` callback (which is added
57  /// by `Animator::RequestFrame`).
58  ///
59  /// Like the callback in `AsyncWaitForVsync`, this callback is
60  /// only scheduled to be called once, and it's supposed to be
61  /// called in the UI thread. If there is no AsyncWaitForVsync
62  /// callback (`Animator::RequestFrame` is not called), this
63  /// secondary callback will still be executed at vsync.
64  ///
65  /// This callback is used to provide the vsync signal needed by
66  /// `SmoothPointerDataDispatcher`.
67  ///
68  /// @see `PointerDataDispatcher::ScheduleSecondaryVsyncCallback`.
69  void ScheduleSecondaryVsyncCallback(const fml::closure& callback);
70 
71  void Start();
72 
73  void Stop();
74 
75  void SetDimensionChangePending();
76 
77  // Enqueue |trace_flow_id| into |trace_flow_ids_|. The corresponding flow
78  // will be ended during the next |BeginFrame|.
79  void EnqueueTraceFlowId(uint64_t trace_flow_id);
80 
81  private:
83 
84  void BeginFrame(fml::TimePoint frame_start_time,
85  fml::TimePoint frame_target_time);
86 
87  bool CanReuseLastLayerTree();
88  void DrawLastLayerTree();
89 
90  void AwaitVSync();
91 
92  const char* FrameParity();
93 
96  std::shared_ptr<VsyncWaiter> waiter_;
97 
98  fml::TimePoint last_frame_begin_time_;
99  fml::TimePoint last_vsync_start_time_;
100  fml::TimePoint last_frame_target_time_;
101  int64_t dart_frame_deadline_;
102  fml::RefPtr<LayerTreePipeline> layer_tree_pipeline_;
103  fml::Semaphore pending_frame_semaphore_;
104  LayerTreePipeline::ProducerContinuation producer_continuation_;
105  int64_t frame_number_;
106  bool paused_;
107  bool regenerate_layer_tree_;
108  bool frame_scheduled_;
109  int notify_idle_task_id_;
110  bool dimension_change_pending_;
111  SkISize last_layer_tree_size_;
112  std::deque<uint64_t> trace_flow_ids_;
113 
114  fml::WeakPtrFactory<Animator> weak_factory_;
115 
116  friend class testing::ShellTest;
117 
119 };
120 
121 } // namespace flutter
122 
123 #endif // FLUTTER_SHELL_COMMON_ANIMATOR_H_
TaskRunners task_runners_
MockDelegate delegate_
std::function< void()> closure
Definition: closure.h:14
#define FML_DISALLOW_COPY_AND_ASSIGN(TypeName)
Definition: macros.h:27