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

Commit de06351

Browse files
authored
Add createOverlaySurface JNI (#19040)
1 parent 1ec2a98 commit de06351

File tree

9 files changed

+104
-0
lines changed

9 files changed

+104
-0
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,7 @@ FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/Flutte
702702
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEngineCache.java
703703
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/FlutterEnginePluginRegistry.java
704704
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/FlutterJNI.java
705+
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/FlutterOverlaySurface.java
705706
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/FlutterShellArgs.java
706707
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/dart/DartExecutor.java
707708
FILE: ../../../flutter/shell/platform/android/io/flutter/embedding/engine/dart/DartMessenger.java

shell/platform/android/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ android_java_sources = [
150150
"io/flutter/embedding/engine/FlutterEngineCache.java",
151151
"io/flutter/embedding/engine/FlutterEnginePluginRegistry.java",
152152
"io/flutter/embedding/engine/FlutterJNI.java",
153+
"io/flutter/embedding/engine/FlutterOverlaySurface.java",
153154
"io/flutter/embedding/engine/FlutterShellArgs.java",
154155
"io/flutter/embedding/engine/dart/DartExecutor.java",
155156
"io/flutter/embedding/engine/dart/DartMessenger.java",

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,17 @@ public void onEndFrame() {
821821
}
822822
platformViewsController.onEndFrame();
823823
}
824+
825+
@SuppressWarnings("unused")
826+
@UiThread
827+
public FlutterOverlaySurface createOverlaySurface() {
828+
ensureRunningOnMainThread();
829+
if (platformViewsController == null) {
830+
throw new RuntimeException(
831+
"platformViewsController must be set before attempting to position an overlay surface");
832+
}
833+
return platformViewsController.createOverlaySurface();
834+
}
824835
// ----- End Engine Lifecycle Support ----
825836

826837
// @SuppressWarnings("unused")
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
package io.flutter.embedding.engine;
6+
7+
import android.view.Surface;
8+
import androidx.annotation.Keep;
9+
import androidx.annotation.NonNull;
10+
11+
public class FlutterOverlaySurface {
12+
@NonNull private final Surface surface;
13+
14+
private final long id;
15+
16+
@Keep
17+
public FlutterOverlaySurface(long id, @NonNull Surface surface) {
18+
this.id = id;
19+
this.surface = surface;
20+
}
21+
22+
public long getId() {
23+
return id;
24+
}
25+
26+
public Surface getSurface() {
27+
return surface;
28+
}
29+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import androidx.annotation.NonNull;
1818
import androidx.annotation.UiThread;
1919
import androidx.annotation.VisibleForTesting;
20+
import io.flutter.embedding.engine.FlutterOverlaySurface;
2021
import io.flutter.embedding.engine.dart.DartExecutor;
2122
import io.flutter.embedding.engine.systemchannels.PlatformViewsChannel;
2223
import io.flutter.plugin.editing.TextInputPlugin;
@@ -549,4 +550,9 @@ public void onBeginFrame() {
549550
public void onEndFrame() {
550551
// TODO: Implement this method. https://github.com/flutter/flutter/issues/58288
551552
}
553+
554+
public FlutterOverlaySurface createOverlaySurface() {
555+
// TODO: Implement this method. https://github.com/flutter/flutter/issues/58288
556+
return null;
557+
}
552558
}

shell/platform/android/jni/platform_view_android_jni.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ class PlatformViewAndroidJNI {
130130
int y,
131131
int width,
132132
int height) = 0;
133+
133134
//----------------------------------------------------------------------------
134135
/// @brief Initiates a frame if using hybrid composition.
135136
///
@@ -145,6 +146,13 @@ class PlatformViewAndroidJNI {
145146
/// @note Must be called from the platform thread.
146147
///
147148
virtual void FlutterViewEndFrame() = 0;
149+
150+
//----------------------------------------------------------------------------
151+
/// @brief Instantiates an overlay surface in hybrid composition.
152+
///
153+
/// @note Must be called from the platform thread.
154+
///
155+
virtual void FlutterViewCreateOverlaySurface() = 0;
148156
};
149157

150158
} // namespace flutter

shell/platform/android/platform_view_android_jni_impl.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "flutter/shell/platform/android/android_shell_holder.h"
2525
#include "flutter/shell/platform/android/apk_asset_provider.h"
2626
#include "flutter/shell/platform/android/flutter_main.h"
27+
#include "flutter/shell/platform/android/jni/platform_view_android_jni.h"
2728
#include "flutter/shell/platform/android/platform_view_android.h"
2829

2930
#define ANDROID_SHELL_HOLDER \
@@ -80,6 +81,8 @@ static jmethodID g_on_first_frame_method = nullptr;
8081

8182
static jmethodID g_on_engine_restart_method = nullptr;
8283

84+
static jmethodID g_create_overlay_surface_method = nullptr;
85+
8386
static jmethodID g_on_begin_frame_method = nullptr;
8487

8588
static jmethodID g_on_end_frame_method = nullptr;
@@ -683,6 +686,10 @@ bool RegisterApi(JNIEnv* env) {
683686
return false;
684687
}
685688

689+
g_create_overlay_surface_method =
690+
env->GetMethodID(g_flutter_jni_class->obj(), "createOverlaySurface",
691+
"()Lio/flutter/embedding/engine/FlutterOverlaySurface;");
692+
686693
return true;
687694
}
688695

@@ -707,6 +714,10 @@ bool PlatformViewAndroid::Register(JNIEnv* env) {
707714
return false;
708715
}
709716

717+
g_create_overlay_surface_method =
718+
env->GetMethodID(g_flutter_jni_class->obj(), "createOverlaySurface",
719+
"()Lio/flutter/embedding/engine/FlutterOverlaySurface;");
720+
710721
g_flutter_jni_class = new fml::jni::ScopedJavaGlobalRef<jclass>(
711722
env, env->FindClass("io/flutter/embedding/engine/FlutterJNI"));
712723
if (g_flutter_jni_class->is_null()) {
@@ -738,6 +749,14 @@ bool PlatformViewAndroid::Register(JNIEnv* env) {
738749
return false;
739750
}
740751

752+
g_create_overlay_surface_method = env->GetMethodID(
753+
g_flutter_jni_class->obj(), "createOverlaySurface", "()V");
754+
755+
if (g_create_overlay_surface_method == nullptr) {
756+
FML_LOG(ERROR) << "Could not locate createOverlaySurface method";
757+
return false;
758+
}
759+
741760
g_on_display_overlay_surface_method = env->GetMethodID(
742761
g_flutter_jni_class->obj(), "onDisplayOverlaySurface", "(IIIII)V");
743762

@@ -1080,4 +1099,17 @@ void PlatformViewAndroidJNIImpl::FlutterViewEndFrame() {
10801099
FML_CHECK(CheckException(env));
10811100
}
10821101

1102+
void PlatformViewAndroidJNIImpl::FlutterViewCreateOverlaySurface() {
1103+
JNIEnv* env = fml::jni::AttachCurrentThread();
1104+
1105+
auto java_object = java_object_.get(env);
1106+
if (java_object.is_null()) {
1107+
return;
1108+
}
1109+
1110+
env->CallVoidMethod(java_object.obj(), g_create_overlay_surface_method);
1111+
1112+
FML_CHECK(CheckException(env));
1113+
}
1114+
10831115
} // 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
@@ -66,6 +66,8 @@ class PlatformViewAndroidJNIImpl final : public PlatformViewAndroidJNI {
6666

6767
void FlutterViewEndFrame() override;
6868

69+
void FlutterViewCreateOverlaySurface() override;
70+
6971
private:
7072
// Reference to FlutterJNI object.
7173
const fml::jni::JavaObjectWeakGlobalRef java_object_;

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,18 @@ public void onEndFrame__callsPlatformViewsController() {
111111
// --- Verify Results ---
112112
verify(platformViewsController, times(1)).onEndFrame();
113113
}
114+
115+
@Test
116+
public void createOverlaySurface__callsPlatformViewsController() {
117+
PlatformViewsController platformViewsController = mock(PlatformViewsController.class);
118+
119+
FlutterJNI flutterJNI = new FlutterJNI();
120+
flutterJNI.setPlatformViewsController(platformViewsController);
121+
122+
// --- Execute Test ---
123+
flutterJNI.createOverlaySurface();
124+
125+
// --- Verify Results ---
126+
verify(platformViewsController, times(1)).createOverlaySurface();
127+
}
114128
}

0 commit comments

Comments
 (0)