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_time) = 0; 34 35 virtual void OnAnimatorNotifyIdle(int64_t deadline) = 0; 36 37 virtual void OnAnimatorDraw( 38 fml::RefPtr<Pipeline<flutter::LayerTree>> pipeline) = 0; 39 40 virtual void OnAnimatorDrawLastLayerTree() = 0; 41 }; 42 43 Animator(Delegate& delegate, 44 TaskRunners task_runners, 45 std::unique_ptr<VsyncWaiter> waiter); 46 47 ~Animator(); 48 49 float GetDisplayRefreshRate() const; 50 51 void RequestFrame(bool regenerate_layer_tree = true); 52 53 void Render(std::unique_ptr<flutter::LayerTree> layer_tree); 54 55 void Start(); 56 57 void Stop(); 58 59 void SetDimensionChangePending(); 60 61 // Enqueue |trace_flow_id| into |trace_flow_ids_|. The corresponding flow 62 // will be ended during the next |BeginFrame|. 63 void EnqueueTraceFlowId(uint64_t trace_flow_id); 64 65 private: 66 using LayerTreePipeline = Pipeline<flutter::LayerTree>; 67 68 void BeginFrame(fml::TimePoint frame_start_time, 69 fml::TimePoint frame_target_time); 70 71 bool CanReuseLastLayerTree(); 72 void DrawLastLayerTree(); 73 74 void AwaitVSync(); 75 76 const char* FrameParity(); 77 78 Delegate& delegate_; 79 TaskRunners task_runners_; 80 std::shared_ptr<VsyncWaiter> waiter_; 81 82 fml::TimePoint last_begin_frame_time_; 83 int64_t dart_frame_deadline_; 84 fml::RefPtr<LayerTreePipeline> layer_tree_pipeline_; 85 fml::Semaphore pending_frame_semaphore_; 86 LayerTreePipeline::ProducerContinuation producer_continuation_; 87 int64_t frame_number_; 88 bool paused_; 89 bool regenerate_layer_tree_; 90 bool frame_scheduled_; 91 int notify_idle_task_id_; 92 bool dimension_change_pending_; 93 SkISize last_layer_tree_size_; 94 std::deque<uint64_t> trace_flow_ids_; 95 96 fml::WeakPtrFactory<Animator> weak_factory_; 97 98 friend class testing::ShellTest; 99 100 FML_DISALLOW_COPY_AND_ASSIGN(Animator); 101 }; 102 103 } // namespace flutter 104 105 #endif // FLUTTER_SHELL_COMMON_ANIMATOR_H_ 106