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 
6 
7 #include "flutter/fml/message_loop.h"
8 
9 namespace impeller::android {
10 
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 
28 
29 bool Choreographer::IsValid() const {
30  return !!instance_;
31 }
32 
34  int64_t p_nanos) {
35  return Choreographer::FrameTimePoint{std::chrono::nanoseconds(p_nanos)};
36 }
37 
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 
79  return GetProcTable().AChoreographer_getInstance &&
80  (GetProcTable().AChoreographer_postFrameCallback64 ||
81  GetProcTable().AChoreographer_postFrameCallback);
82 }
83 
84 } // namespace impeller::android
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...
static Choreographer & GetInstance()
Create or get the thread local instance of a choreographer. A message loop will be setup on the calli...
const ProcTable & GetProcTable()
Definition: proc_table.cc:12
static Choreographer::FrameTimePoint ClockMonotonicNanosToFrameTimePoint(int64_t p_nanos)
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:68