5#include "flutter/shell/platform/android/vsync_waiter_android.h"
10#include "flutter/common/task_runners.h"
11#include "flutter/fml/logging.h"
12#include "flutter/fml/platform/android/jni_util.h"
13#include "flutter/fml/platform/android/scoped_java_ref.h"
14#include "flutter/fml/size.h"
15#include "flutter/fml/trace_event.h"
30void VsyncWaiterAndroid::AwaitVSync() {
32 auto* weak_this =
new std::weak_ptr<VsyncWaiter>(shared_from_this());
35 const auto& choreographer =
36 impeller::android::Choreographer::GetInstance();
37 choreographer.PostFrameCallback([weak_this](auto time) {
39 std::chrono::time_point_cast<std::chrono::nanoseconds>(time)
42 OnVsyncFromNDK(time_ns, weak_this);
48 auto* weak_this =
new std::weak_ptr<VsyncWaiter>(shared_from_this());
49 jlong java_baton =
reinterpret_cast<jlong
>(weak_this);
61void VsyncWaiterAndroid::OnVsyncFromNDK(int64_t frame_nanos,
void*
data) {
65 if (frame_time > now) {
72 frame_time.ToEpochDelta().ToMicroseconds(),
74 target_time.ToEpochDelta().ToMicroseconds());
76 auto* weak_this =
reinterpret_cast<std::weak_ptr<VsyncWaiter>*
>(
data);
77 ConsumePendingCallback(weak_this, frame_time, target_time);
81void VsyncWaiterAndroid::OnVsyncFromJava(JNIEnv*
env,
83 jlong frameDelayNanos,
84 jlong refreshPeriodNanos,
92 frame_time.ToEpochDelta().ToMicroseconds(),
94 target_time.ToEpochDelta().ToMicroseconds());
96 auto* weak_this =
reinterpret_cast<std::weak_ptr<VsyncWaiter>*
>(java_baton);
97 ConsumePendingCallback(weak_this, frame_time, target_time);
101void VsyncWaiterAndroid::ConsumePendingCallback(
102 std::weak_ptr<VsyncWaiter>* weak_this,
105 auto shared_this = weak_this->lock();
109 shared_this->FireCallback(frame_start_time, frame_target_time);
114void VsyncWaiterAndroid::OnUpdateRefreshRate(JNIEnv*
env,
116 jfloat refresh_rate) {
123 static const JNINativeMethod methods[] = {
125 .name =
"nativeOnVsync",
126 .signature =
"(JJJ)V",
127 .fnPtr =
reinterpret_cast<void*
>(&OnVsyncFromJava),
130 .name =
"nativeUpdateRefreshRate",
132 .fnPtr =
reinterpret_cast<void*
>(&OnUpdateRefreshRate),
135 jclass clazz =
env->FindClass(
"io/flutter/embedding/engine/FlutterJNI");
137 if (clazz ==
nullptr) {
148 return env->RegisterNatives(clazz, methods,
fml::size(methods)) == 0;
fml::RefPtr< fml::TaskRunner > GetUITaskRunner() const
fml::RefPtr< fml::TaskRunner > GetPlatformTaskRunner() const
~VsyncWaiterAndroid() override
friend class VsyncWaiterAndroid
const TaskRunners task_runners_
static void RunNowOrPostTask(const fml::RefPtr< fml::TaskRunner > &runner, const fml::closure &task)
virtual void PostTask(const fml::closure &task) override
static constexpr TimeDelta FromNanoseconds(int64_t nanos)
static constexpr TimePoint FromEpochDelta(TimeDelta ticks)
static bool IsAvailableOnPlatform()
TaskRunners task_runners_
#define FML_CHECK(condition)
#define FML_DCHECK(condition)
static fml::jni::ScopedJavaGlobalRef< jclass > * g_vsync_waiter_class
static jmethodID g_async_wait_for_vsync_method_
static std::atomic_uint g_refresh_rate_
JNIEnv * AttachCurrentThread()
constexpr std::size_t size(T(&array)[N])
std::shared_ptr< const fml::Mapping > data
#define TRACE_EVENT2_INT(category_group, name, arg1_name, arg1_val, arg2_name, arg2_val)