Flutter Impeller
choreographer.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_IMPELLER_TOOLKIT_ANDROID_CHOREOGRAPHER_H_
6 #define FLUTTER_IMPELLER_TOOLKIT_ANDROID_CHOREOGRAPHER_H_
7 
9 
10 #include <chrono>
11 #include <memory>
12 
13 namespace impeller::android {
14 
15 //------------------------------------------------------------------------------
16 /// @brief This class describes access to the choreographer instance for
17 /// the current thread. Choreographers are only available on API
18 /// levels above 24. On levels below 24, an invalid choreographer
19 /// will be returned.
20 ///
21 /// Since choreographer need an event loop on the current thread,
22 /// one will be setup if it doesn't already exist.
23 ///
25  public:
26  static bool IsAvailableOnPlatform();
27 
28  //----------------------------------------------------------------------------
29  /// @brief Create or get the thread local instance of a choreographer. A
30  /// message loop will be setup on the calling thread if none
31  /// exists.
32  ///
33  /// @warning Choreographers are only available on API levels 24 and above.
34  /// Below this level, this will return an invalid instance.
35  /// Availability can also be checked via the
36  /// `IsAvailableOnPlatform` call.
37  ///
38  /// @return The thread local choreographer instance. If none can be setup,
39  /// an invalid object reference will be returned. See `IsValid`.
40  ///
41  static Choreographer& GetInstance();
42 
44 
45  Choreographer(const Choreographer&) = delete;
46 
48 
49  bool IsValid() const;
50 
51  //----------------------------------------------------------------------------
52  /// A monotonic system clock.
53  ///
54  using FrameClock = std::chrono::steady_clock;
55 
56  //----------------------------------------------------------------------------
57  /// A timepoint on a monotonic system clock.
58  ///
59  using FrameTimePoint = std::chrono::time_point<FrameClock>;
60  using FrameCallback = std::function<void(FrameTimePoint)>;
61 
62  //----------------------------------------------------------------------------
63  /// @brief Posts a frame callback. The time that the frame is being
64  /// rendered will be available in the callback as an argument.
65  /// Multiple frame callbacks within the same frame interval will
66  /// receive the same argument.
67  ///
68  /// @param[in] callback The callback
69  ///
70  /// @return `true` if the frame callback could be posted. This may return
71  /// `false` if choreographers are not available on the platform.
72  /// See `IsAvailableOnPlatform`.
73  ///
74  bool PostFrameCallback(FrameCallback callback) const;
75 
76  private:
77  AChoreographer* instance_ = nullptr;
78 
79  explicit Choreographer();
80 };
81 
82 } // namespace impeller::android
83 
84 #endif // FLUTTER_IMPELLER_TOOLKIT_ANDROID_CHOREOGRAPHER_H_
This class describes access to the choreographer instance for the current thread. Choreographers are ...
Definition: choreographer.h:24
std::chrono::time_point< FrameClock > FrameTimePoint
Definition: choreographer.h:59
std::function< void(FrameTimePoint)> FrameCallback
Definition: choreographer.h:60
bool PostFrameCallback(FrameCallback callback) const
Posts a frame callback. The time that the frame is being rendered will be available in the callback a...
std::chrono::steady_clock FrameClock
Definition: choreographer.h:54
Choreographer(const Choreographer &)=delete
Choreographer & operator=(const Choreographer &)=delete
static Choreographer & GetInstance()
Create or get the thread local instance of a choreographer. A message loop will be setup on the calli...