Skip to content

Commit ac809b4

Browse files
authored
onBeginFrame JNI (flutter#18866)
1 parent 5f37029 commit ac809b4

6 files changed

Lines changed: 62 additions & 0 deletions

File tree

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -799,6 +799,17 @@ public void onDisplayOverlaySurface(int id, int x, int y, int width, int height)
799799
}
800800
platformViewsController.onDisplayOverlaySurface(id, x, y, width, height);
801801
}
802+
803+
@SuppressWarnings("unused")
804+
@UiThread
805+
public void onBeginFrame() {
806+
ensureRunningOnMainThread();
807+
if (platformViewsController == null) {
808+
throw new RuntimeException(
809+
"platformViewsController must be set before attempting to begin the frame");
810+
}
811+
platformViewsController.onBeginFrame();
812+
}
802813
// ----- End Engine Lifecycle Support ----
803814

804815
// @SuppressWarnings("unused")

shell/platform/android/io/flutter/plugin/platform/PlatformViewsController.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,4 +541,8 @@ public void onDisplayPlatformView(int viewId, int x, int y, int width, int heigh
541541
public void onDisplayOverlaySurface(int id, int x, int y, int width, int height) {
542542
// TODO: Implement this method. https://github.com/flutter/flutter/issues/58288
543543
}
544+
545+
public void onBeginFrame() {
546+
// TODO: Implement this method. https://github.com/flutter/flutter/issues/58288
547+
}
544548
}

shell/platform/android/jni/platform_view_android_jni.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,13 @@ class PlatformViewAndroidJNI {
130130
int y,
131131
int width,
132132
int height) = 0;
133+
//----------------------------------------------------------------------------
134+
/// @brief Initiates a frame if using hybrid composition.
135+
///
136+
///
137+
/// @note Must be called from the platform thread.
138+
///
139+
virtual void FlutterViewBeginFrame() = 0;
133140
};
134141

135142
} // namespace flutter

shell/platform/android/platform_view_android_jni_impl.cc

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ static jmethodID g_on_first_frame_method = nullptr;
8080

8181
static jmethodID g_on_engine_restart_method = nullptr;
8282

83+
static jmethodID g_on_begin_frame_method = nullptr;
84+
8385
static jmethodID g_attach_to_gl_context_method = nullptr;
8486

8587
static jmethodID g_update_tex_image_method = nullptr;
@@ -718,6 +720,14 @@ bool PlatformViewAndroid::Register(JNIEnv* env) {
718720
return false;
719721
}
720722

723+
g_on_begin_frame_method =
724+
env->GetMethodID(g_flutter_jni_class->obj(), "onBeginFrame", "()V");
725+
726+
if (g_on_begin_frame_method == nullptr) {
727+
FML_LOG(ERROR) << "Could not locate onBeginFrame method";
728+
return false;
729+
}
730+
721731
g_on_display_overlay_surface_method = env->GetMethodID(
722732
g_flutter_jni_class->obj(), "onDisplayOverlaySurface", "(IIIII)V");
723733

@@ -1034,4 +1044,17 @@ void PlatformViewAndroidJNIImpl::FlutterViewDisplayOverlaySurface(
10341044
FML_CHECK(CheckException(env));
10351045
}
10361046

1047+
void PlatformViewAndroidJNIImpl::FlutterViewBeginFrame() {
1048+
JNIEnv* env = fml::jni::AttachCurrentThread();
1049+
1050+
auto java_object = java_object_.get(env);
1051+
if (java_object.is_null()) {
1052+
return;
1053+
}
1054+
1055+
env->CallVoidMethod(java_object.obj(), g_on_begin_frame_method);
1056+
1057+
FML_CHECK(CheckException(env));
1058+
}
1059+
10371060
} // namespace flutter

shell/platform/android/platform_view_android_jni_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class PlatformViewAndroidJNIImpl final : public PlatformViewAndroidJNI {
6262
int width,
6363
int height) override;
6464

65+
void FlutterViewBeginFrame() override;
66+
6567
private:
6668
// Reference to FlutterJNI object.
6769
const fml::jni::JavaObjectWeakGlobalRef java_object_;

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,19 @@ public void onDisplayOverlaySurface__callsPlatformViewsController() {
8181
verify(platformViewsController, times(1))
8282
.onDisplayOverlaySurface(/*id=*/ 1, /*x=*/ 10, /*y=*/ 20, /*width=*/ 100, /*height=*/ 200);
8383
}
84+
85+
@Test
86+
public void onBeginFrame__callsPlatformViewsController() {
87+
PlatformViewsController platformViewsController = mock(PlatformViewsController.class);
88+
89+
// --- Test Setup ---
90+
FlutterJNI flutterJNI = new FlutterJNI();
91+
flutterJNI.setPlatformViewsController(platformViewsController);
92+
93+
// --- Execute Test ---
94+
flutterJNI.onBeginFrame();
95+
96+
// --- Verify Results ---
97+
verify(platformViewsController, times(1)).onBeginFrame();
98+
}
8499
}

0 commit comments

Comments
 (0)