Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions shell/platform/android/android_external_texture_gl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace flutter {

AndroidExternalTextureGL::AndroidExternalTextureGL(
int64_t id,
const fml::jni::JavaObjectWeakGlobalRef& surface_texture,
const fml::jni::ScopedJavaGlobalRef<jobject>& surface_texture,
std::shared_ptr<PlatformViewAndroidJNI> jni_facade)
: Texture(id),
jni_facade_(jni_facade),
Expand Down Expand Up @@ -74,7 +74,8 @@ void AndroidExternalTextureGL::Paint(SkCanvas& canvas,
}

void AndroidExternalTextureGL::UpdateTransform() {
jni_facade_->SurfaceTextureGetTransformMatrix(surface_texture_, transform);
jni_facade_->SurfaceTextureGetTransformMatrix(
fml::jni::ScopedJavaLocalRef<jobject>(surface_texture_), transform);
}

void AndroidExternalTextureGL::OnGrContextDestroyed() {
Expand All @@ -86,16 +87,19 @@ void AndroidExternalTextureGL::OnGrContextDestroyed() {
}

void AndroidExternalTextureGL::Attach(jint textureName) {
jni_facade_->SurfaceTextureAttachToGLContext(surface_texture_, textureName);
jni_facade_->SurfaceTextureAttachToGLContext(
fml::jni::ScopedJavaLocalRef<jobject>(surface_texture_), textureName);
}

void AndroidExternalTextureGL::Update() {
jni_facade_->SurfaceTextureUpdateTexImage(surface_texture_);
jni_facade_->SurfaceTextureUpdateTexImage(
fml::jni::ScopedJavaLocalRef<jobject>(surface_texture_));
UpdateTransform();
}

void AndroidExternalTextureGL::Detach() {
jni_facade_->SurfaceTextureDetachFromGLContext(surface_texture_);
jni_facade_->SurfaceTextureDetachFromGLContext(
fml::jni::ScopedJavaLocalRef<jobject>(surface_texture_));
}

void AndroidExternalTextureGL::OnTextureUnregistered() {}
Expand Down
5 changes: 2 additions & 3 deletions shell/platform/android/android_external_texture_gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include <GLES/gl.h>

#include "flutter/common/graphics/texture.h"
#include "flutter/fml/platform/android/jni_weak_ref.h"
#include "flutter/shell/platform/android/platform_view_android_jni_impl.h"

namespace flutter {
Expand All @@ -17,7 +16,7 @@ class AndroidExternalTextureGL : public flutter::Texture {
public:
AndroidExternalTextureGL(
int64_t id,
const fml::jni::JavaObjectWeakGlobalRef& surface_texture,
const fml::jni::ScopedJavaGlobalRef<jobject>& surface_texture,
std::shared_ptr<PlatformViewAndroidJNI> jni_facade);

~AndroidExternalTextureGL() override;
Expand Down Expand Up @@ -49,7 +48,7 @@ class AndroidExternalTextureGL : public flutter::Texture {

std::shared_ptr<PlatformViewAndroidJNI> jni_facade_;

fml::jni::JavaObjectWeakGlobalRef surface_texture_;
fml::jni::ScopedJavaGlobalRef<jobject> surface_texture_;

AttachmentState state_ = AttachmentState::uninitialized;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import io.flutter.view.AccessibilityBridge;
import io.flutter.view.FlutterCallbackInformation;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -756,11 +757,14 @@ public void setAccessibilityFeatures(int flags) {
public void registerTexture(long textureId, @NonNull SurfaceTextureWrapper textureWrapper) {
ensureRunningOnMainThread();
ensureAttachedToNative();
nativeRegisterTexture(nativeShellHolderId, textureId, textureWrapper);
nativeRegisterTexture(
nativeShellHolderId, textureId, new WeakReference<SurfaceTextureWrapper>(textureWrapper));
}

private native void nativeRegisterTexture(
long nativeShellHolderId, long textureId, @NonNull SurfaceTextureWrapper textureWrapper);
long nativeShellHolderId,
long textureId,
@NonNull WeakReference<SurfaceTextureWrapper> textureWrapper);

/**
* Call this method to inform Flutter that a texture previously registered with {@link
Expand Down
8 changes: 4 additions & 4 deletions shell/platform/android/jni/jni_mock.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,22 @@ class JNIMock final : public PlatformViewAndroidJNI {

MOCK_METHOD(void,
SurfaceTextureAttachToGLContext,
(JavaWeakGlobalRef surface_texture, int textureId),
(JavaLocalRef surface_texture, int textureId),
(override));

MOCK_METHOD(void,
SurfaceTextureUpdateTexImage,
(JavaWeakGlobalRef surface_texture),
(JavaLocalRef surface_texture),
(override));

MOCK_METHOD(void,
SurfaceTextureGetTransformMatrix,
(JavaWeakGlobalRef surface_texture, SkMatrix& transform),
(JavaLocalRef surface_texture, SkMatrix& transform),
(override));

MOCK_METHOD(void,
SurfaceTextureDetachFromGLContext,
(JavaWeakGlobalRef surface_texture),
(JavaLocalRef surface_texture),
(override));

MOCK_METHOD(void,
Expand Down
21 changes: 9 additions & 12 deletions shell/platform/android/jni/platform_view_android_jni.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
#include "third_party/skia/include/core/SkMatrix.h"

#if OS_ANDROID
#include "flutter/fml/platform/android/jni_weak_ref.h"
#include "flutter/fml/platform/android/scoped_java_ref.h"
#endif

namespace flutter {

#if OS_ANDROID
using JavaWeakGlobalRef = fml::jni::JavaObjectWeakGlobalRef;
using JavaLocalRef = fml::jni::ScopedJavaLocalRef<jobject>;
#else
using JavaWeakGlobalRef = std::nullptr_t;
using JavaLocalRef = std::nullptr_t;
#endif

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -86,31 +86,28 @@ class PlatformViewAndroidJNI {
/// @brief Attach the SurfaceTexture to the OpenGL ES context that is
/// current on the calling thread.
///
virtual void SurfaceTextureAttachToGLContext(
JavaWeakGlobalRef surface_texture,
int textureId) = 0;
virtual void SurfaceTextureAttachToGLContext(JavaLocalRef surface_texture,
int textureId) = 0;

//----------------------------------------------------------------------------
/// @brief Updates the texture image to the most recent frame from the
/// image stream.
///
virtual void SurfaceTextureUpdateTexImage(
JavaWeakGlobalRef surface_texture) = 0;
virtual void SurfaceTextureUpdateTexImage(JavaLocalRef surface_texture) = 0;

//----------------------------------------------------------------------------
/// @brief Gets the transform matrix from the SurfaceTexture.
/// Then, it updates the `transform` matrix, so it fill the canvas
/// and preserve the aspect ratio.
///
virtual void SurfaceTextureGetTransformMatrix(
JavaWeakGlobalRef surface_texture,
SkMatrix& transform) = 0;
virtual void SurfaceTextureGetTransformMatrix(JavaLocalRef surface_texture,
SkMatrix& transform) = 0;

//----------------------------------------------------------------------------
/// @brief Detaches a SurfaceTexture from the OpenGL ES context.
///
virtual void SurfaceTextureDetachFromGLContext(
JavaWeakGlobalRef surface_texture) = 0;
JavaLocalRef surface_texture) = 0;

//----------------------------------------------------------------------------
/// @brief Positions and sizes a platform view if using hybrid
Expand Down
2 changes: 1 addition & 1 deletion shell/platform/android/platform_view_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ void PlatformViewAndroid::UpdateSemantics(

void PlatformViewAndroid::RegisterExternalTexture(
int64_t texture_id,
const fml::jni::JavaObjectWeakGlobalRef& surface_texture) {
const fml::jni::ScopedJavaGlobalRef<jobject>& surface_texture) {
RegisterTexture(std::make_shared<AndroidExternalTextureGL>(
texture_id, surface_texture, std::move(jni_facade_)));
}
Expand Down
3 changes: 1 addition & 2 deletions shell/platform/android/platform_view_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
#include <vector>

#include "flutter/fml/memory/weak_ptr.h"
#include "flutter/fml/platform/android/jni_weak_ref.h"
#include "flutter/fml/platform/android/scoped_java_ref.h"
#include "flutter/lib/ui/window/platform_message.h"
#include "flutter/shell/common/platform_view.h"
Expand Down Expand Up @@ -97,7 +96,7 @@ class PlatformViewAndroid final : public PlatformView {

void RegisterExternalTexture(
int64_t texture_id,
const fml::jni::JavaObjectWeakGlobalRef& surface_texture);
const fml::jni::ScopedJavaGlobalRef<jobject>& surface_texture);

// |PlatformView|
void LoadDartDeferredLibrary(
Expand Down
72 changes: 56 additions & 16 deletions shell/platform/android/platform_view_android_jni_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ static fml::jni::ScopedJavaGlobalRef<jclass>* g_flutter_callback_info_class =

static fml::jni::ScopedJavaGlobalRef<jclass>* g_flutter_jni_class = nullptr;

static fml::jni::ScopedJavaGlobalRef<jclass>* g_java_weak_reference_class =
nullptr;

static fml::jni::ScopedJavaGlobalRef<jclass>* g_texture_wrapper_class = nullptr;

static fml::jni::ScopedJavaGlobalRef<jclass>* g_java_long_class = nullptr;
Expand Down Expand Up @@ -87,6 +90,8 @@ static jmethodID g_on_begin_frame_method = nullptr;

static jmethodID g_on_end_frame_method = nullptr;

static jmethodID g_java_weak_reference_get_method = nullptr;

static jmethodID g_attach_to_gl_context_method = nullptr;

static jmethodID g_update_tex_image_method = nullptr;
Expand Down Expand Up @@ -452,8 +457,8 @@ static void RegisterTexture(JNIEnv* env,
jlong texture_id,
jobject surface_texture) {
ANDROID_SHELL_HOLDER->GetPlatformView()->RegisterExternalTexture(
static_cast<int64_t>(texture_id), //
fml::jni::JavaObjectWeakGlobalRef(env, surface_texture) //
static_cast<int64_t>(texture_id), //
fml::jni::ScopedJavaGlobalRef<jobject>(env, surface_texture) //
);
}

Expand Down Expand Up @@ -714,8 +719,8 @@ bool RegisterApi(JNIEnv* env) {
},
{
.name = "nativeRegisterTexture",
.signature = "(JJLio/flutter/embedding/engine/renderer/"
"SurfaceTextureWrapper;)V",
.signature = "(JJLjava/lang/ref/"
"WeakReference;)V",
.fnPtr = reinterpret_cast<void*>(&RegisterTexture),
},
{
Expand Down Expand Up @@ -1007,6 +1012,20 @@ bool PlatformViewAndroid::Register(JNIEnv* env) {
return false;
}

g_java_weak_reference_class = new fml::jni::ScopedJavaGlobalRef<jclass>(
env, env->FindClass("java/lang/ref/WeakReference"));
if (g_java_weak_reference_class->is_null()) {
FML_LOG(ERROR) << "Could not locate WeakReference class";
return false;
}

g_java_weak_reference_get_method = env->GetMethodID(
g_java_weak_reference_class->obj(), "get", "()Ljava/lang/Object;");
if (g_java_weak_reference_get_method == nullptr) {
FML_LOG(ERROR) << "Could not locate WeakReference.get method";
return false;
}

g_texture_wrapper_class = new fml::jni::ScopedJavaGlobalRef<jclass>(
env, env->FindClass(
"io/flutter/embedding/engine/renderer/SurfaceTextureWrapper"));
Expand Down Expand Up @@ -1215,12 +1234,18 @@ void PlatformViewAndroidJNIImpl::FlutterViewOnPreEngineRestart() {
}

void PlatformViewAndroidJNIImpl::SurfaceTextureAttachToGLContext(
JavaWeakGlobalRef surface_texture,
JavaLocalRef surface_texture,
int textureId) {
JNIEnv* env = fml::jni::AttachCurrentThread();

fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref =
surface_texture.get(env);
if (surface_texture.is_null()) {
return;
}

fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref(
env, env->CallObjectMethod(surface_texture.obj(),
g_java_weak_reference_get_method));

if (surface_texture_local_ref.is_null()) {
return;
}
Expand All @@ -1232,11 +1257,16 @@ void PlatformViewAndroidJNIImpl::SurfaceTextureAttachToGLContext(
}

void PlatformViewAndroidJNIImpl::SurfaceTextureUpdateTexImage(
JavaWeakGlobalRef surface_texture) {
JavaLocalRef surface_texture) {
JNIEnv* env = fml::jni::AttachCurrentThread();

fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref =
surface_texture.get(env);
if (surface_texture.is_null()) {
return;
}

fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref(
env, env->CallObjectMethod(surface_texture.obj(),
g_java_weak_reference_get_method));
if (surface_texture_local_ref.is_null()) {
return;
}
Expand All @@ -1260,12 +1290,17 @@ SkSize ScaleToFill(float scaleX, float scaleY) {
}

void PlatformViewAndroidJNIImpl::SurfaceTextureGetTransformMatrix(
JavaWeakGlobalRef surface_texture,
JavaLocalRef surface_texture,
SkMatrix& transform) {
JNIEnv* env = fml::jni::AttachCurrentThread();

fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref =
surface_texture.get(env);
if (surface_texture.is_null()) {
return;
}

fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref(
env, env->CallObjectMethod(surface_texture.obj(),
g_java_weak_reference_get_method));
if (surface_texture_local_ref.is_null()) {
return;
}
Expand All @@ -1290,11 +1325,16 @@ void PlatformViewAndroidJNIImpl::SurfaceTextureGetTransformMatrix(
}

void PlatformViewAndroidJNIImpl::SurfaceTextureDetachFromGLContext(
JavaWeakGlobalRef surface_texture) {
JavaLocalRef surface_texture) {
JNIEnv* env = fml::jni::AttachCurrentThread();

fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref =
surface_texture.get(env);
if (surface_texture.is_null()) {
return;
}

fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref(
env, env->CallObjectMethod(surface_texture.obj(),
g_java_weak_reference_get_method));
if (surface_texture_local_ref.is_null()) {
return;
}
Expand Down
9 changes: 4 additions & 5 deletions shell/platform/android/platform_view_android_jni_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,15 @@ class PlatformViewAndroidJNIImpl final : public PlatformViewAndroidJNI {

void FlutterViewOnPreEngineRestart() override;

void SurfaceTextureAttachToGLContext(JavaWeakGlobalRef surface_texture,
void SurfaceTextureAttachToGLContext(JavaLocalRef surface_texture,
int textureId) override;

void SurfaceTextureUpdateTexImage(JavaWeakGlobalRef surface_texture) override;
void SurfaceTextureUpdateTexImage(JavaLocalRef surface_texture) override;

void SurfaceTextureGetTransformMatrix(JavaWeakGlobalRef surface_texture,
void SurfaceTextureGetTransformMatrix(JavaLocalRef surface_texture,
SkMatrix& transform) override;

void SurfaceTextureDetachFromGLContext(
JavaWeakGlobalRef surface_texture) override;
void SurfaceTextureDetachFromGLContext(JavaLocalRef surface_texture) override;

void FlutterViewOnDisplayPlatformView(int view_id,
int x,
Expand Down