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
19namespace flutter {
20
21namespace testing {
22class 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|.
29class 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(
38 fml::RefPtr<Pipeline<flutter::LayerTree>> pipeline,
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 float GetDisplayRefreshRate() const;
51
52 void RequestFrame(bool regenerate_layer_tree = true);
53
54 void Render(std::unique_ptr<flutter::LayerTree> layer_tree);
55
56 //--------------------------------------------------------------------------
57 /// @brief Schedule a secondary callback to be executed right after the
58 /// main `VsyncWaiter::AsyncWaitForVsync` callback (which is added
59 /// by `Animator::RequestFrame`).
60 ///
61 /// Like the callback in `AsyncWaitForVsync`, this callback is
62 /// only scheduled to be called once, and it's supposed to be
63 /// called in the UI thread. If there is no AsyncWaitForVsync
64 /// callback (`Animator::RequestFrame` is not called), this
65 /// secondary callback will still be executed at vsync.
66 ///
67 /// This callback is used to provide the vsync signal needed by
68 /// `SmoothPointerDataDispatcher`.
69 ///
70 /// @see `PointerDataDispatcher::ScheduleSecondaryVsyncCallback`.
71 void ScheduleSecondaryVsyncCallback(const fml::closure& callback);
72
73 void Start();
74
75 void Stop();
76
77 void SetDimensionChangePending();
78
79 // Enqueue |trace_flow_id| into |trace_flow_ids_|. The corresponding flow
80 // will be ended during the next |BeginFrame|.
81 void EnqueueTraceFlowId(uint64_t trace_flow_id);
82
83 private:
84 using LayerTreePipeline = Pipeline<flutter::LayerTree>;
85
86 void BeginFrame(fml::TimePoint frame_start_time,
87 fml::TimePoint frame_target_time);
88
89 bool CanReuseLastLayerTree();
90 void DrawLastLayerTree();
91
92 void AwaitVSync();
93
94 const char* FrameParity();
95
96 Delegate& delegate_;
97 TaskRunners task_runners_;
98 std::shared_ptr<VsyncWaiter> waiter_;
99
100 fml::TimePoint last_frame_begin_time_;
101 fml::TimePoint last_vsync_start_time_;
102 fml::TimePoint last_frame_target_time_;
103 int64_t dart_frame_deadline_;
104 fml::RefPtr<LayerTreePipeline> layer_tree_pipeline_;
105 fml::Semaphore pending_frame_semaphore_;
106 LayerTreePipeline::ProducerContinuation producer_continuation_;
107 int64_t frame_number_;
108 bool paused_;
109 bool regenerate_layer_tree_;
110 bool frame_scheduled_;
111 int notify_idle_task_id_;
112 bool dimension_change_pending_;
113 SkISize last_layer_tree_size_;
114 std::deque<uint64_t> trace_flow_ids_;
115
116 fml::WeakPtrFactory<Animator> weak_factory_;
117
118 friend class testing::ShellTest;
119
120 FML_DISALLOW_COPY_AND_ASSIGN(Animator);
121};
122
123} // namespace flutter
124
125#endif // FLUTTER_SHELL_COMMON_ANIMATOR_H_
126