| Flutter Impeller |

choreographer.cc
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 #include "flutter/impeller/toolkit/android/choreographer.h"
6
7 #include "flutter/fml/message_loop.h"
8
9 namespace impeller::android {
10
11 Choreographer& Choreographer::GetInstance() {
12 static thread_local Choreographer tChoreographer;
13 return tChoreographer;
14 }
15
16 Choreographer::Choreographer() {
17 if (!IsAvailableOnPlatform()) {
18 return;
19 }
20
21 // We need a message loop on the current thread for the choreographer to
22 // schedule callbacks for us on.
23 fml::MessageLoop::EnsureInitializedForCurrentThread();
24 instance_ = GetProcTable().AChoreographer_getInstance();
25 }
26
27 Choreographer::~Choreographer() = default;
28
29 bool Choreographer::IsValid() const {
30 return !!instance_;
31 }
32
33 static Choreographer::FrameTimePoint ClockMonotonicNanosToFrameTimePoint(
34 int64_t p_nanos) {
35 return Choreographer::FrameTimePoint{std::chrono::nanoseconds(p_nanos)};
36 }
37
38 bool Choreographer::PostFrameCallback(FrameCallback callback) const {
39 if (!callback || !IsValid()) {
40 return false;
41 }
42
43 struct InFlightData {
44 FrameCallback callback;
45 };
46
47 auto data = std::make_unique<InFlightData>();
48 data->callback = std::move(callback);
49
50 const auto& table = GetProcTable();
51 if (table.AChoreographer_postFrameCallback64) {
52 table.AChoreographer_postFrameCallback64(
53 const_cast<AChoreographer*>(instance_),
54 [](int64_t nanos, void* p_data) {
55 auto data = reinterpret_cast<InFlightData*>(p_data);
56 data->callback(ClockMonotonicNanosToFrameTimePoint(nanos));
57 delete data;
58 },
59 data.release());
60 return true;
61 } else if (table.AChoreographer_postFrameCallback) {
62 table.AChoreographer_postFrameCallback(
63 const_cast<AChoreographer*>(instance_),
64 [](long /*NOLINT*/ nanos, void* p_data) {
65 auto data = reinterpret_cast<InFlightData*>(p_data);
66 data->callback(ClockMonotonicNanosToFrameTimePoint(nanos));
67 delete data;
68 },
69 data.release());
70 return true;
71 }
72
73 // The validity check should have tripped by now.
74 FML_UNREACHABLE();
75 return false;
76 }
77
78 bool Choreographer::IsAvailableOnPlatform() {
79 return GetProcTable().AChoreographer_getInstance &&
80 (GetProcTable().AChoreographer_postFrameCallback64 ||
81 GetProcTable().AChoreographer_postFrameCallback);
82 }
83
84 } // namespace impeller::android
impeller::android::ClockMonotonicNanosToFrameTimePoint
static Choreographer::FrameTimePoint ClockMonotonicNanosToFrameTimePoint(int64_t p_nanos)
Definition: choreographer.cc:33
data
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:64
impeller::android::Choreographer::IsAvailableOnPlatform
static bool IsAvailableOnPlatform()
Definition: choreographer.cc:78
impeller::android::Choreographer::PostFrameCallback
bool PostFrameCallback(FrameCallback callback) const
Posts a frame callback. The time that the frame is being rendered will be available in the callback a...
Definition: choreographer.cc:38
impeller::android
Definition: choreographer.cc:9
impeller::android::Choreographer::GetInstance
static Choreographer & GetInstance()
Create or get the thread local instance of a choreographer. A message loop will be setup on the calli...
Definition: choreographer.cc:11
impeller::android::Choreographer::FrameTimePoint
std::chrono::time_point< FrameClock > FrameTimePoint
Definition: choreographer.h:59
impeller::android::Choreographer::IsValid
bool IsValid() const
Definition: choreographer.cc:29
impeller::android::Choreographer::~Choreographer
~Choreographer()
choreographer.h
impeller::android::Choreographer
This class describes access to the choreographer instance for the current thread. Choreographers are ...
Definition: choreographer.h:24
impeller::android::Choreographer::FrameCallback
std::function< void(FrameTimePoint)> FrameCallback
Definition: choreographer.h:60
impeller::android::GetProcTable
const ProcTable & GetProcTable()
Definition: proc_table.cc:12