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_VSYNC_WAITER_H_ 6 #define FLUTTER_SHELL_COMMON_VSYNC_WAITER_H_ 7 8 #include <functional> 9 #include <memory> 10 #include <mutex> 11 12 #include "flutter/common/task_runners.h" 13 #include "flutter/fml/synchronization/thread_annotations.h" 14 #include "flutter/fml/time/time_point.h" 15 16 namespace flutter { 17 18 /// Abstract Base Class that represents a platform specific mechanism for 19 /// getting callbacks when a vsync event happens. 20 class VsyncWaiter : public std::enable_shared_from_this<VsyncWaiter> { 21 public: 22 using Callback = std::function<void(fml::TimePoint frame_start_time, 23 fml::TimePoint frame_target_time)>; 24 25 virtual ~VsyncWaiter(); 26 27 void AsyncWaitForVsync(Callback callback); 28 29 static constexpr float kUnknownRefreshRateFPS = 0.0; 30 31 // Get the display's maximum refresh rate in the unit of frame per second. 32 // Return kUnknownRefreshRateFPS if the refresh rate is unknown. 33 virtual float GetDisplayRefreshRate() const; 34 35 protected: 36 // On some backends, the |FireCallback| needs to be made from a static C 37 // method. 38 friend class VsyncWaiterAndroid; 39 friend class VsyncWaiterEmbedder; 40 41 const TaskRunners task_runners_; 42 43 VsyncWaiter(TaskRunners task_runners); 44 45 // Implementations are meant to override this method and arm their vsync 46 // latches when in response to this invocation. On vsync, they are meant to 47 // invoke the |FireCallback| method once (and only once) with the appropriate 48 // arguments. 49 virtual void AwaitVSync() = 0; 50 51 void FireCallback(fml::TimePoint frame_start_time, 52 fml::TimePoint frame_target_time); 53 54 private: 55 std::mutex callback_mutex_; 56 Callback callback_ FML_GUARDED_BY(callback_mutex_); 57 58 FML_DISALLOW_COPY_AND_ASSIGN(VsyncWaiter); 59 }; 60 61 } // namespace flutter 62 63 #endif // FLUTTER_SHELL_COMMON_VSYNC_WAITER_H_ 64