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

Commit 19aa4e7

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

File tree

5 files changed

+60
-49
lines changed

5 files changed

+60
-49
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: 29 additions & 25 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 {
@@ -886,37 +889,35 @@ void PlatformViewAndroidJNIImpl::FlutterViewOnPreEngineRestart() {
886889
FML_CHECK(CheckException(env));
887890
}
888891

889-
void PlatformViewAndroidJNIImpl::SetCurrentSurfaceTexture(
890-
fml::jni::JavaObjectWeakGlobalRef& surface_texture) {
891-
surface_texture_ = surface_texture;
892-
}
893-
894892
void PlatformViewAndroidJNIImpl::SurfaceTextureAttachToGLContext(
893+
std::any surface_texture,
895894
int textureId) {
896895
JNIEnv* env = fml::jni::AttachCurrentThread();
897896

898-
fml::jni::ScopedJavaLocalRef<jobject> surface_texture =
899-
surface_texture_.get(env);
900-
if (surface_texture.is_null()) {
897+
fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref =
898+
SURFACE_TEXTURE_LOCAL_REF.get(env);
899+
if (surface_texture_local_ref.is_null()) {
901900
return;
902901
}
903902

904-
env->CallVoidMethod(surface_texture.obj(), g_attach_to_gl_context_method,
905-
textureId);
903+
env->CallVoidMethod(surface_texture_local_ref.obj(),
904+
g_attach_to_gl_context_method, textureId);
906905

907906
FML_CHECK(CheckException(env));
908907
}
909908

910-
void PlatformViewAndroidJNIImpl::SurfaceTextureUpdateTexImage() {
909+
void PlatformViewAndroidJNIImpl::SurfaceTextureUpdateTexImage(
910+
std::any surface_texture) {
911911
JNIEnv* env = fml::jni::AttachCurrentThread();
912912

913-
fml::jni::ScopedJavaLocalRef<jobject> surface_texture =
914-
surface_texture_.get(env);
915-
if (surface_texture.is_null()) {
913+
fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref =
914+
SURFACE_TEXTURE_LOCAL_REF.get(env);
915+
if (surface_texture_local_ref.is_null()) {
916916
return;
917917
}
918918

919-
env->CallVoidMethod(surface_texture.obj(), g_update_tex_image_method);
919+
env->CallVoidMethod(surface_texture_local_ref.obj(),
920+
g_update_tex_image_method);
920921

921922
FML_CHECK(CheckException(env));
922923
}
@@ -934,20 +935,21 @@ SkSize ScaleToFill(float scaleX, float scaleY) {
934935
}
935936

936937
void PlatformViewAndroidJNIImpl::SurfaceTextureGetTransformMatrix(
938+
std::any surface_texture,
937939
SkMatrix& transform) {
938940
JNIEnv* env = fml::jni::AttachCurrentThread();
939941

940-
fml::jni::ScopedJavaLocalRef<jobject> surface_texture =
941-
surface_texture_.get(env);
942-
if (surface_texture.is_null()) {
942+
fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref =
943+
SURFACE_TEXTURE_LOCAL_REF.get(env);
944+
if (surface_texture_local_ref.is_null()) {
943945
return;
944946
}
945947

946948
fml::jni::ScopedJavaLocalRef<jfloatArray> transformMatrix(
947949
env, env->NewFloatArray(16));
948950

949-
env->CallVoidMethod(surface_texture.obj(), g_get_transform_matrix_method,
950-
transformMatrix.obj());
951+
env->CallVoidMethod(surface_texture_local_ref.obj(),
952+
g_get_transform_matrix_method, transformMatrix.obj());
951953
FML_CHECK(CheckException(env));
952954

953955
float* m = env->GetFloatArrayElements(transformMatrix.obj(), nullptr);
@@ -962,16 +964,18 @@ void PlatformViewAndroidJNIImpl::SurfaceTextureGetTransformMatrix(
962964
transform.set9(matrix3);
963965
}
964966

965-
void PlatformViewAndroidJNIImpl::SurfaceTextureDetachFromGLContext() {
967+
void PlatformViewAndroidJNIImpl::SurfaceTextureDetachFromGLContext(
968+
std::any surface_texture) {
966969
JNIEnv* env = fml::jni::AttachCurrentThread();
967970

968-
fml::jni::ScopedJavaLocalRef<jobject> surface_texture =
969-
surface_texture_.get(env);
970-
if (surface_texture.is_null()) {
971+
fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref =
972+
SURFACE_TEXTURE_LOCAL_REF.get(env);
973+
if (surface_texture_local_ref.is_null()) {
971974
return;
972975
}
973976

974-
env->CallVoidMethod(surface_texture.obj(), g_detach_from_gl_context_method);
977+
env->CallVoidMethod(surface_texture_local_ref.obj(),
978+
g_detach_from_gl_context_method);
975979

976980
FML_CHECK(CheckException(env));
977981
}

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)