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