Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 83e2a97

Browse files
author
Emmanuel Garcia
authored
Revert "Use AChoreographer methods to await vsync when available (#31859)"
This reverts commit 266d336.
1 parent ca2f95c commit 83e2a97

File tree

10 files changed

+32
-198
lines changed

10 files changed

+32
-198
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,8 +1146,6 @@ FILE: ../../../flutter/shell/gpu/gpu_surface_vulkan.h
11461146
FILE: ../../../flutter/shell/gpu/gpu_surface_vulkan_delegate.cc
11471147
FILE: ../../../flutter/shell/gpu/gpu_surface_vulkan_delegate.h
11481148
FILE: ../../../flutter/shell/platform/android/AndroidManifest.xml
1149-
FILE: ../../../flutter/shell/platform/android/android_choreographer.cc
1150-
FILE: ../../../flutter/shell/platform/android/android_choreographer.h
11511149
FILE: ../../../flutter/shell/platform/android/android_context_gl.cc
11521150
FILE: ../../../flutter/shell/platform/android/android_context_gl.h
11531151
FILE: ../../../flutter/shell/platform/android/android_context_gl_unittests.cc

shell/platform/android/BUILD.gn

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@ source_set("flutter_shell_native_src") {
6262

6363
sources = [
6464
"$root_build_dir/flutter_icu/icudtl.o",
65-
"android_choreographer.cc",
66-
"android_choreographer.h",
6765
"android_context_gl.cc",
6866
"android_context_gl.h",
6967
"android_display.cc",

shell/platform/android/android_choreographer.cc

Lines changed: 0 additions & 61 deletions
This file was deleted.

shell/platform/android/android_choreographer.h

Lines changed: 0 additions & 30 deletions
This file was deleted.

shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -257,18 +257,8 @@ public void setRefreshRateFPS(float refreshRateFPS) {
257257
// on Android we will need to refactor this. Static lookup makes things a
258258
// bit easier on the C++ side.
259259
FlutterJNI.refreshRateFPS = refreshRateFPS;
260-
updateRefreshRate();
261260
}
262261

263-
public void updateRefreshRate() {
264-
if (!FlutterJNI.loadLibraryCalled) {
265-
return;
266-
}
267-
nativeUpdateRefreshRate(refreshRateFPS);
268-
}
269-
270-
private native void nativeUpdateRefreshRate(float refreshRateFPS);
271-
272262
/**
273263
* The Android vsync waiter implementation in C++ needs to know when a vsync signal arrives, which
274264
* is obtained via Java API. The delegate set here is called on the C++ side when the engine is

shell/platform/android/io/flutter/embedding/engine/loader/FlutterLoader.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ public InitResult call() {
183183
ResourceExtractor resourceExtractor = initResources(appContext);
184184

185185
flutterJNI.loadLibrary();
186-
flutterJNI.updateRefreshRate();
187186

188187
// Prefetch the default font manager as soon as possible on a background thread.
189188
// It helps to reduce time cost of engine setup that blocks the platform thread.

shell/platform/android/test/io/flutter/embedding/engine/FlutterJNITest.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import static org.junit.Assert.assertEquals;
44
import static org.mockito.Mockito.mock;
5-
import static org.mockito.Mockito.spy;
65
import static org.mockito.Mockito.times;
76
import static org.mockito.Mockito.verify;
87
import static org.mockito.Mockito.when;
@@ -246,13 +245,4 @@ public void invokePlatformMessageResponseCallback__wantsDirectBuffer() {
246245
ByteBuffer buffer = ByteBuffer.allocate(4);
247246
flutterJNI.invokePlatformMessageResponseCallback(0, buffer, buffer.position());
248247
}
249-
250-
@Test
251-
public void setRefreshRateFPS__callsUpdateRefreshRate() {
252-
FlutterJNI flutterJNI = spy(new FlutterJNI());
253-
// --- Execute Test ---
254-
flutterJNI.setRefreshRateFPS(120.0f);
255-
// --- Verify Results ---
256-
verify(flutterJNI, times(1)).updateRefreshRate();
257-
}
258248
}

shell/platform/android/test/io/flutter/embedding/engine/loader/FlutterLoaderTest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public void itReportsInitializedAfterInitializing() {
5555
shadowOf(getMainLooper()).idle();
5656
assertTrue(flutterLoader.initialized());
5757
verify(mockFlutterJNI, times(1)).loadLibrary();
58-
verify(mockFlutterJNI, times(1)).updateRefreshRate();
5958
}
6059

6160
@Test

shell/platform/android/vsync_waiter_android.cc

Lines changed: 26 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -13,81 +13,53 @@
1313
#include "flutter/fml/platform/android/scoped_java_ref.h"
1414
#include "flutter/fml/size.h"
1515
#include "flutter/fml/trace_event.h"
16-
#include "flutter/shell/platform/android/android_choreographer.h"
1716

1817
namespace flutter {
1918

2019
static fml::jni::ScopedJavaGlobalRef<jclass>* g_vsync_waiter_class = nullptr;
2120
static jmethodID g_async_wait_for_vsync_method_ = nullptr;
22-
static std::atomic_uint g_refresh_rate_ = 60;
2321

2422
VsyncWaiterAndroid::VsyncWaiterAndroid(flutter::TaskRunners task_runners)
25-
: VsyncWaiter(std::move(task_runners)),
26-
use_ndk_choreographer_(
27-
AndroidChoreographer::ShouldUseNDKChoreographer()) {}
23+
: VsyncWaiter(std::move(task_runners)) {}
2824

2925
VsyncWaiterAndroid::~VsyncWaiterAndroid() = default;
3026

3127
// |VsyncWaiter|
3228
void VsyncWaiterAndroid::AwaitVSync() {
33-
if (use_ndk_choreographer_) {
34-
auto* weak_this = new std::weak_ptr<VsyncWaiter>(shared_from_this());
35-
fml::TaskRunner::RunNowOrPostTask(
36-
task_runners_.GetUITaskRunner(), [weak_this]() {
37-
AndroidChoreographer::PostFrameCallback(&OnVsyncFromNDK, weak_this);
38-
});
39-
} else {
40-
// TODO(99798): Remove it when we drop support for API level < 29.
41-
auto* weak_this = new std::weak_ptr<VsyncWaiter>(shared_from_this());
42-
jlong java_baton = reinterpret_cast<jlong>(weak_this);
43-
task_runners_.GetPlatformTaskRunner()->PostTask([java_baton]() {
44-
JNIEnv* env = fml::jni::AttachCurrentThread();
45-
env->CallStaticVoidMethod(g_vsync_waiter_class->obj(), //
46-
g_async_wait_for_vsync_method_, //
47-
java_baton //
48-
);
49-
});
50-
}
51-
}
52-
53-
// static
54-
void VsyncWaiterAndroid::OnVsyncFromNDK(int64_t frame_nanos, void* data) {
55-
TRACE_EVENT0("flutter", "VSYNC");
56-
57-
auto frame_time = fml::TimePoint::FromEpochDelta(
58-
fml::TimeDelta::FromNanoseconds(frame_nanos));
59-
auto now = fml::TimePoint::Now();
60-
if (frame_time > now) {
61-
frame_time = now;
62-
}
63-
auto target_time = frame_time + fml::TimeDelta::FromNanoseconds(
64-
1000000000.0 / g_refresh_rate_);
65-
auto* weak_this = reinterpret_cast<std::weak_ptr<VsyncWaiter>*>(data);
66-
ConsumePendingCallback(weak_this, frame_time, target_time);
29+
auto* weak_this = new std::weak_ptr<VsyncWaiter>(shared_from_this());
30+
jlong java_baton = reinterpret_cast<jlong>(weak_this);
31+
32+
task_runners_.GetPlatformTaskRunner()->PostTask([java_baton]() {
33+
JNIEnv* env = fml::jni::AttachCurrentThread();
34+
env->CallStaticVoidMethod(g_vsync_waiter_class->obj(), //
35+
g_async_wait_for_vsync_method_, //
36+
java_baton //
37+
);
38+
});
6739
}
6840

6941
// static
70-
void VsyncWaiterAndroid::OnVsyncFromJava(JNIEnv* env,
71-
jclass jcaller,
72-
jlong frameDelayNanos,
73-
jlong refreshPeriodNanos,
74-
jlong java_baton) {
42+
void VsyncWaiterAndroid::OnNativeVsync(JNIEnv* env,
43+
jclass jcaller,
44+
jlong frameDelayNanos,
45+
jlong refreshPeriodNanos,
46+
jlong java_baton) {
7547
TRACE_EVENT0("flutter", "VSYNC");
7648

7749
auto frame_time =
7850
fml::TimePoint::Now() - fml::TimeDelta::FromNanoseconds(frameDelayNanos);
7951
auto target_time =
8052
frame_time + fml::TimeDelta::FromNanoseconds(refreshPeriodNanos);
8153

82-
auto* weak_this = reinterpret_cast<std::weak_ptr<VsyncWaiter>*>(java_baton);
83-
ConsumePendingCallback(weak_this, frame_time, target_time);
54+
ConsumePendingCallback(java_baton, frame_time, target_time);
8455
}
8556

8657
// static
8758
void VsyncWaiterAndroid::ConsumePendingCallback(
88-
std::weak_ptr<VsyncWaiter>* weak_this,
59+
jlong java_baton,
8960
fml::TimePoint frame_start_time,
9061
fml::TimePoint frame_target_time) {
62+
auto* weak_this = reinterpret_cast<std::weak_ptr<VsyncWaiter>*>(java_baton);
9163
auto shared_this = weak_this->lock();
9264
delete weak_this;
9365

@@ -96,27 +68,13 @@ void VsyncWaiterAndroid::ConsumePendingCallback(
9668
}
9769
}
9870

99-
// static
100-
void VsyncWaiterAndroid::OnUpdateRefreshRate(JNIEnv* env,
101-
jclass jcaller,
102-
jfloat refresh_rate) {
103-
FML_DCHECK(refresh_rate > 0);
104-
g_refresh_rate_ = static_cast<uint>(refresh_rate);
105-
}
106-
10771
// static
10872
bool VsyncWaiterAndroid::Register(JNIEnv* env) {
109-
static const JNINativeMethod methods[] = {
110-
{
111-
.name = "nativeOnVsync",
112-
.signature = "(JJJ)V",
113-
.fnPtr = reinterpret_cast<void*>(&OnVsyncFromJava),
114-
},
115-
{
116-
.name = "nativeUpdateRefreshRate",
117-
.signature = "(F)V",
118-
.fnPtr = reinterpret_cast<void*>(&OnUpdateRefreshRate),
119-
}};
73+
static const JNINativeMethod methods[] = {{
74+
.name = "nativeOnVsync",
75+
.signature = "(JJJ)V",
76+
.fnPtr = reinterpret_cast<void*>(&OnNativeVsync),
77+
}};
12078

12179
jclass clazz = env->FindClass("io/flutter/embedding/engine/FlutterJNI");
12280

@@ -125,10 +83,12 @@ bool VsyncWaiterAndroid::Register(JNIEnv* env) {
12583
}
12684

12785
g_vsync_waiter_class = new fml::jni::ScopedJavaGlobalRef<jclass>(env, clazz);
86+
12887
FML_CHECK(!g_vsync_waiter_class->is_null());
12988

13089
g_async_wait_for_vsync_method_ = env->GetStaticMethodID(
13190
g_vsync_waiter_class->obj(), "asyncWaitForVsync", "(J)V");
91+
13292
FML_CHECK(g_async_wait_for_vsync_method_ != nullptr);
13393

13494
return env->RegisterNatives(clazz, methods, fml::size(methods)) == 0;

shell/platform/android/vsync_waiter_android.h

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414

1515
namespace flutter {
1616

17-
class AndroidChoreographer;
18-
1917
class VsyncWaiterAndroid final : public VsyncWaiter {
2018
public:
2119
static bool Register(JNIEnv* env);
@@ -28,23 +26,16 @@ class VsyncWaiterAndroid final : public VsyncWaiter {
2826
// |VsyncWaiter|
2927
void AwaitVSync() override;
3028

31-
static void OnVsyncFromNDK(int64_t frame_nanos, void* data);
32-
33-
static void OnVsyncFromJava(JNIEnv* env,
34-
jclass jcaller,
35-
jlong frameDelayNanos,
36-
jlong refreshPeriodNanos,
37-
jlong java_baton);
29+
static void OnNativeVsync(JNIEnv* env,
30+
jclass jcaller,
31+
jlong frameDelayNanos,
32+
jlong refreshPeriodNanos,
33+
jlong java_baton);
3834

39-
static void ConsumePendingCallback(std::weak_ptr<VsyncWaiter>* weak_this,
35+
static void ConsumePendingCallback(jlong java_baton,
4036
fml::TimePoint frame_start_time,
4137
fml::TimePoint frame_target_time);
4238

43-
static void OnUpdateRefreshRate(JNIEnv* env,
44-
jclass jcaller,
45-
jfloat refresh_rate);
46-
47-
const bool use_ndk_choreographer_;
4839
FML_DISALLOW_COPY_AND_ASSIGN(VsyncWaiterAndroid);
4940
};
5041

0 commit comments

Comments
 (0)