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

Commit c72c3eb

Browse files
author
Emmanuel Garcia
committed
Pass surface_texture to JNI methods
1 parent 24ec465 commit c72c3eb

File tree

5 files changed

+65
-50
lines changed

5 files changed

+65
-50
lines changed

shell/platform/android/android_external_texture_gl.cc

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ AndroidExternalTextureGL::AndroidExternalTextureGL(
1414
int64_t id,
1515
const fml::jni::JavaObjectWeakGlobalRef& surface_texture,
1616
std::unique_ptr<PlatformViewAndroidJNI> jni_facade)
17-
: Texture(id), surface_texture_(surface_texture), transform(SkMatrix::I()) {
18-
jni_facade_ = std::unique_ptr<PlatformViewAndroidJNIImpl>(
19-
static_cast<PlatformViewAndroidJNIImpl*>(jni_facade.release()));
20-
}
17+
: Texture(id),
18+
jni_facade_(std::move(jni_facade)),
19+
surface_texture_(surface_texture),
20+
transform(SkMatrix::I()) {}
2121

2222
AndroidExternalTextureGL::~AndroidExternalTextureGL() {
2323
if (state_ == AttachmentState::attached) {
@@ -72,8 +72,7 @@ void AndroidExternalTextureGL::Paint(SkCanvas& canvas,
7272
}
7373

7474
void AndroidExternalTextureGL::UpdateTransform() {
75-
jni_facade_->SetCurrentSurfaceTexture(surface_texture_);
76-
jni_facade_->SurfaceTextureGetTransformMatrix(transform);
75+
jni_facade_->SurfaceTextureGetTransformMatrix(surface_texture_, transform);
7776
}
7877

7978
void AndroidExternalTextureGL::OnGrContextDestroyed() {
@@ -84,19 +83,16 @@ void AndroidExternalTextureGL::OnGrContextDestroyed() {
8483
}
8584

8685
void AndroidExternalTextureGL::Attach(jint textureName) {
87-
jni_facade_->SetCurrentSurfaceTexture(surface_texture_);
88-
jni_facade_->SurfaceTextureAttachToGLContext(textureName);
86+
jni_facade_->SurfaceTextureAttachToGLContext(surface_texture_, textureName);
8987
}
9088

9189
void AndroidExternalTextureGL::Update() {
92-
jni_facade_->SetCurrentSurfaceTexture(surface_texture_);
93-
jni_facade_->SurfaceTextureUpdateTexImage();
90+
jni_facade_->SurfaceTextureUpdateTexImage(surface_texture_);
9491
UpdateTransform();
9592
}
9693

9794
void AndroidExternalTextureGL::Detach() {
98-
jni_facade_->SetCurrentSurfaceTexture(surface_texture_);
99-
jni_facade_->SurfaceTextureDetachFromGLContext();
95+
jni_facade_->SurfaceTextureDetachFromGLContext(surface_texture_);
10096
}
10197

10298
void AndroidExternalTextureGL::OnTextureUnregistered() {}

shell/platform/android/android_external_texture_gl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class AndroidExternalTextureGL : public flutter::Texture {
4545

4646
enum class AttachmentState { uninitialized, attached, detached };
4747

48-
std::unique_ptr<PlatformViewAndroidJNIImpl> jni_facade_;
48+
std::unique_ptr<PlatformViewAndroidJNI> jni_facade_;
4949

5050
fml::jni::JavaObjectWeakGlobalRef surface_texture_;
5151

shell/platform/android/jni/platform_view_android_jni.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#ifndef FLUTTER_SHELL_PLATFORM_ANDROID_PLATFORM_VIEW_ANDROID_JNI_H_
66
#define FLUTTER_SHELL_PLATFORM_ANDROID_PLATFORM_VIEW_ANDROID_JNI_H_
77

8+
#include <any>
9+
810
#include "flutter/fml/macros.h"
911
#include "flutter/fml/mapping.h"
1012
#include "flutter/lib/ui/window/platform_message.h"
@@ -72,25 +74,35 @@ class PlatformViewAndroidJNI {
7274
/// @brief Attach the SurfaceTexture to the OpenGL ES context that is
7375
/// current on the calling thread.
7476
///
75-
virtual void SurfaceTextureAttachToGLContext(int textureId) = 0;
77+
/// @note `surface_texture` must be `fml::jni::JavaObjectWeakGlobalRef`
78+
///
79+
virtual void SurfaceTextureAttachToGLContext(std::any surface_texture,
80+
int textureId) = 0;
7681

7782
//----------------------------------------------------------------------------
7883
/// @brief Updates the texture image to the most recent frame from the
7984
/// image stream.
8085
///
81-
virtual void SurfaceTextureUpdateTexImage() = 0;
86+
/// @note `surface_texture` must be `fml::jni::JavaObjectWeakGlobalRef`
87+
///
88+
virtual void SurfaceTextureUpdateTexImage(std::any surface_texture) = 0;
8289

8390
//----------------------------------------------------------------------------
8491
/// @brief Gets the transform matrix from the SurfaceTexture.
8592
/// Then, it updates the `transform` matrix, so it fill the canvas
8693
/// and preserve the aspect ratio.
8794
///
88-
virtual void SurfaceTextureGetTransformMatrix(SkMatrix& transform) = 0;
95+
/// @note `surface_texture` must be `fml::jni::JavaObjectWeakGlobalRef`
96+
///
97+
virtual void SurfaceTextureGetTransformMatrix(std::any surface_texture,
98+
SkMatrix& transform) = 0;
8999

90100
//----------------------------------------------------------------------------
91101
/// @brief Detaches a SurfaceTexture from the OpenGL ES context.
92102
///
93-
virtual void SurfaceTextureDetachFromGLContext() = 0;
103+
/// @note `surface_texture` must be `fml::jni::JavaObjectWeakGlobalRef`
104+
///
105+
virtual void SurfaceTextureDetachFromGLContext(std::any surface_texture) = 0;
94106

95107
//----------------------------------------------------------------------------
96108
/// @brief Positions and sizes a platform view if using hybrid

shell/platform/android/platform_view_android_jni_impl.cc

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
#define ANDROID_SHELL_HOLDER \
3030
(reinterpret_cast<AndroidShellHolder*>(shell_holder))
3131

32+
#define SURFACE_TEXTURE_LOCAL_REF \
33+
(std::any_cast<fml::jni::JavaObjectWeakGlobalRef>(surface_texture))
34+
3235
namespace flutter {
3336

3437
namespace {
@@ -750,7 +753,11 @@ bool PlatformViewAndroid::Register(JNIEnv* env) {
750753

751754
PlatformViewAndroidJNIImpl::PlatformViewAndroidJNIImpl(
752755
fml::jni::JavaObjectWeakGlobalRef java_object)
753-
: java_object_(java_object) {}
756+
: java_object_(java_object) {
757+
// FML_LOG(ERROR) << b;
758+
759+
// surface_texture_ = any_cast<fml::jni::JavaObjectWeakGlobalRef>(test_);
760+
}
754761

755762
PlatformViewAndroidJNIImpl::~PlatformViewAndroidJNIImpl() = default;
756763

@@ -886,37 +893,35 @@ void PlatformViewAndroidJNIImpl::FlutterViewOnPreEngineRestart() {
886893
FML_CHECK(CheckException(env));
887894
}
888895

889-
void PlatformViewAndroidJNIImpl::SetCurrentSurfaceTexture(
890-
fml::jni::JavaObjectWeakGlobalRef& surface_texture) {
891-
surface_texture_ = surface_texture;
892-
}
893-
894896
void PlatformViewAndroidJNIImpl::SurfaceTextureAttachToGLContext(
897+
std::any surface_texture,
895898
int textureId) {
896899
JNIEnv* env = fml::jni::AttachCurrentThread();
897900

898-
fml::jni::ScopedJavaLocalRef<jobject> surface_texture =
899-
surface_texture_.get(env);
900-
if (surface_texture.is_null()) {
901+
fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref =
902+
SURFACE_TEXTURE_LOCAL_REF.get(env);
903+
if (surface_texture_local_ref.is_null()) {
901904
return;
902905
}
903906

904-
env->CallVoidMethod(surface_texture.obj(), g_attach_to_gl_context_method,
905-
textureId);
907+
env->CallVoidMethod(surface_texture_local_ref.obj(),
908+
g_attach_to_gl_context_method, textureId);
906909

907910
FML_CHECK(CheckException(env));
908911
}
909912

910-
void PlatformViewAndroidJNIImpl::SurfaceTextureUpdateTexImage() {
913+
void PlatformViewAndroidJNIImpl::SurfaceTextureUpdateTexImage(
914+
std::any surface_texture) {
911915
JNIEnv* env = fml::jni::AttachCurrentThread();
912916

913-
fml::jni::ScopedJavaLocalRef<jobject> surface_texture =
914-
surface_texture_.get(env);
915-
if (surface_texture.is_null()) {
917+
fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref =
918+
SURFACE_TEXTURE_LOCAL_REF.get(env);
919+
if (surface_texture_local_ref.is_null()) {
916920
return;
917921
}
918922

919-
env->CallVoidMethod(surface_texture.obj(), g_update_tex_image_method);
923+
env->CallVoidMethod(surface_texture_local_ref.obj(),
924+
g_update_tex_image_method);
920925

921926
FML_CHECK(CheckException(env));
922927
}
@@ -934,20 +939,21 @@ SkSize ScaleToFill(float scaleX, float scaleY) {
934939
}
935940

936941
void PlatformViewAndroidJNIImpl::SurfaceTextureGetTransformMatrix(
942+
std::any surface_texture,
937943
SkMatrix& transform) {
938944
JNIEnv* env = fml::jni::AttachCurrentThread();
939945

940-
fml::jni::ScopedJavaLocalRef<jobject> surface_texture =
941-
surface_texture_.get(env);
942-
if (surface_texture.is_null()) {
946+
fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref =
947+
SURFACE_TEXTURE_LOCAL_REF.get(env);
948+
if (surface_texture_local_ref.is_null()) {
943949
return;
944950
}
945951

946952
fml::jni::ScopedJavaLocalRef<jfloatArray> transformMatrix(
947953
env, env->NewFloatArray(16));
948954

949-
env->CallVoidMethod(surface_texture.obj(), g_get_transform_matrix_method,
950-
transformMatrix.obj());
955+
env->CallVoidMethod(surface_texture_local_ref.obj(),
956+
g_get_transform_matrix_method, transformMatrix.obj());
951957
FML_CHECK(CheckException(env));
952958

953959
float* m = env->GetFloatArrayElements(transformMatrix.obj(), nullptr);
@@ -962,16 +968,18 @@ void PlatformViewAndroidJNIImpl::SurfaceTextureGetTransformMatrix(
962968
transform.set9(matrix3);
963969
}
964970

965-
void PlatformViewAndroidJNIImpl::SurfaceTextureDetachFromGLContext() {
971+
void PlatformViewAndroidJNIImpl::SurfaceTextureDetachFromGLContext(
972+
std::any surface_texture) {
966973
JNIEnv* env = fml::jni::AttachCurrentThread();
967974

968-
fml::jni::ScopedJavaLocalRef<jobject> surface_texture =
969-
surface_texture_.get(env);
970-
if (surface_texture.is_null()) {
975+
fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref =
976+
SURFACE_TEXTURE_LOCAL_REF.get(env);
977+
if (surface_texture_local_ref.is_null()) {
971978
return;
972979
}
973980

974-
env->CallVoidMethod(surface_texture.obj(), g_detach_from_gl_context_method);
981+
env->CallVoidMethod(surface_texture_local_ref.obj(),
982+
g_detach_from_gl_context_method);
975983

976984
FML_CHECK(CheckException(env));
977985
}

shell/platform/android/platform_view_android_jni_impl.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,15 @@ class PlatformViewAndroidJNIImpl final : public PlatformViewAndroidJNI {
3939

4040
void FlutterViewOnPreEngineRestart() override;
4141

42-
void SetCurrentSurfaceTexture(
43-
fml::jni::JavaObjectWeakGlobalRef& surface_texture);
42+
void SurfaceTextureAttachToGLContext(std::any surface_texture,
43+
int textureId) override;
4444

45-
void SurfaceTextureAttachToGLContext(int textureId) override;
45+
void SurfaceTextureUpdateTexImage(std::any surface_texture) override;
4646

47-
void SurfaceTextureUpdateTexImage() override;
47+
void SurfaceTextureGetTransformMatrix(std::any surface_texture,
48+
SkMatrix& transform) override;
4849

49-
void SurfaceTextureGetTransformMatrix(SkMatrix& transform) override;
50-
51-
void SurfaceTextureDetachFromGLContext() override;
50+
void SurfaceTextureDetachFromGLContext(std::any surface_texture) override;
5251

5352
void FlutterViewOnDisplayPlatformView(int view_id,
5453
int x,

0 commit comments

Comments
 (0)