From 727ce8fc7a5fe13cec0aa4994625bc53dd4d480e Mon Sep 17 00:00:00 2001 From: Emmanuel Garcia Date: Sun, 7 Jun 2020 20:29:24 -0700 Subject: [PATCH 01/16] Make interface for JNI calls --- ci/licenses_golden/licenses_flutter | 3 +- shell/platform/android/BUILD.gn | 3 +- .../android/platform_view_android_jni.h | 50 ++++++++--------- ...i.cc => platform_view_android_jni_impl.cc} | 43 ++++++++++++++- .../android/platform_view_android_jni_impl.h | 53 +++++++++++++++++++ 5 files changed, 121 insertions(+), 31 deletions(-) rename shell/platform/android/{platform_view_android_jni.cc => platform_view_android_jni_impl.cc} (95%) create mode 100644 shell/platform/android/platform_view_android_jni_impl.h diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index 2854e04c45e07..3e1e4e3221368 100755 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -788,8 +788,9 @@ FILE: ../../../flutter/shell/platform/android/platform_message_response_android. FILE: ../../../flutter/shell/platform/android/platform_message_response_android.h FILE: ../../../flutter/shell/platform/android/platform_view_android.cc FILE: ../../../flutter/shell/platform/android/platform_view_android.h -FILE: ../../../flutter/shell/platform/android/platform_view_android_jni.cc FILE: ../../../flutter/shell/platform/android/platform_view_android_jni.h +FILE: ../../../flutter/shell/platform/android/platform_view_android_jni_impl.cc +FILE: ../../../flutter/shell/platform/android/platform_view_android_jni_impl.h FILE: ../../../flutter/shell/platform/android/robolectric.properties FILE: ../../../flutter/shell/platform/android/vsync_waiter_android.cc FILE: ../../../flutter/shell/platform/android/vsync_waiter_android.h diff --git a/shell/platform/android/BUILD.gn b/shell/platform/android/BUILD.gn index 77a9b63c4ea0e..5d7c7cf1929fc 100644 --- a/shell/platform/android/BUILD.gn +++ b/shell/platform/android/BUILD.gn @@ -50,8 +50,9 @@ shared_library("flutter_shell_native") { "platform_message_response_android.h", "platform_view_android.cc", "platform_view_android.h", - "platform_view_android_jni.cc", "platform_view_android_jni.h", + "platform_view_android_jni_impl.cc", + "platform_view_android_jni_impl.h", "vsync_waiter_android.cc", "vsync_waiter_android.h", ] diff --git a/shell/platform/android/platform_view_android_jni.h b/shell/platform/android/platform_view_android_jni.h index 07c70b2a45b50..f535284ce856d 100644 --- a/shell/platform/android/platform_view_android_jni.h +++ b/shell/platform/android/platform_view_android_jni.h @@ -5,46 +5,42 @@ #ifndef FLUTTER_SHELL_PLATFORM_ANDROID_PLATFORM_VIEW_ANDROID_JNI_H_ #define FLUTTER_SHELL_PLATFORM_ANDROID_PLATFORM_VIEW_ANDROID_JNI_H_ -#include #include "flutter/fml/macros.h" -#include "flutter/shell/platform/android/platform_view_android.h" namespace flutter { -void FlutterViewHandlePlatformMessage(JNIEnv* env, - jobject obj, - jstring channel, - jobject message, - jint responseId); +class PlatformViewAndroidJni { + public: + virtual void FlutterViewHandlePlatformMessage( + const std::string& channel, + fml::RefPtr message, + int responseId); -void FlutterViewHandlePlatformMessageResponse(JNIEnv* env, - jobject obj, - jint responseId, - jobject response); + virtual void FlutterViewHandlePlatformMessageResponse( + int responseId, + std::unique_ptr data); -void FlutterViewUpdateSemantics(JNIEnv* env, - jobject obj, - jobject buffer, - jobjectArray strings); + virtual void FlutterViewUpdateSemantics(std::vector buffer, + std::vector strings); -void FlutterViewUpdateCustomAccessibilityActions(JNIEnv* env, - jobject obj, - jobject buffer, - jobjectArray strings); + virtual void FlutterViewUpdateCustomAccessibilityActions( + std::vector actions_buffer, + std::vector strings); -void FlutterViewOnFirstFrame(JNIEnv* env, jobject obj); + virtual void FlutterViewOnFirstFrame(); -void FlutterViewOnPreEngineRestart(JNIEnv* env, jobject obj); + virtual void FlutterViewOnPreEngineRestart(); -void SurfaceTextureAttachToGLContext(JNIEnv* env, jobject obj, jint textureId); + virtual void SurfaceTextureAttachToGLContext(int textureId); -void SurfaceTextureUpdateTexImage(JNIEnv* env, jobject obj); + virtual void SurfaceTextureUpdateTexImage(); -void SurfaceTextureGetTransformMatrix(JNIEnv* env, - jobject obj, - jfloatArray result); + virtual void SurfaceTextureGetTransformMatrix(SkMatrix transform); -void SurfaceTextureDetachFromGLContext(JNIEnv* env, jobject obj); + virtual void SurfaceTextureDetachFromGLContext(); + + FML_DISALLOW_COPY_AND_ASSIGN(PlatformViewAndroidJni); +} } // namespace flutter diff --git a/shell/platform/android/platform_view_android_jni.cc b/shell/platform/android/platform_view_android_jni_impl.cc similarity index 95% rename from shell/platform/android/platform_view_android_jni.cc rename to shell/platform/android/platform_view_android_jni_impl.cc index 4f6f9258b3e11..9b61f84f3bb3b 100644 --- a/shell/platform/android/platform_view_android_jni.cc +++ b/shell/platform/android/platform_view_android_jni_impl.cc @@ -2,11 +2,12 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/shell/platform/android/platform_view_android_jni.h" +#include "flutter/shell/platform/android/platform_view_android_jni_impl.h" #include - +#include #include + #include "unicode/uchar.h" #include "flutter/assets/directory_asset_bundle.h" @@ -23,6 +24,7 @@ #include "flutter/shell/platform/android/android_shell_holder.h" #include "flutter/shell/platform/android/apk_asset_provider.h" #include "flutter/shell/platform/android/flutter_main.h" +#include "flutter/shell/platform/android/platform_view_android.h" #define ANDROID_SHELL_HOLDER \ (reinterpret_cast(shell_holder)) @@ -792,4 +794,41 @@ bool PlatformViewAndroid::Register(JNIEnv* env) { return RegisterApi(env); } +PlatformViewAndroidJniImpl::PlatformViewAndroidJniImpl( + fml::jni::JavaObjectWeakGlobalRef java_object) + : java_object_(java_object) {} + +PlatformViewAndroidJniImpl::~PlatformViewAndroidJniImpl() = default; + +void PlatformViewAndroidJniImpl::FlutterViewHandlePlatformMessage( + const std::string& channel, + fml::RefPtr message, + int responseId) {} + +void PlatformViewAndroidJniImpl::FlutterViewHandlePlatformMessageResponse( + int responseId, + std::unique_ptr data) {} + +void PlatformViewAndroidJniImpl::FlutterViewUpdateSemantics( + std::vector buffer, + std::vector strings) {} + +void PlatformViewAndroidJniImpl::FlutterViewUpdateCustomAccessibilityActions( + std::vector actions_buffer, + std::vector strings) {} + +void PlatformViewAndroidJniImpl::FlutterViewOnFirstFrame() {} + +void PlatformViewAndroidJniImpl::FlutterViewOnPreEngineRestart() {} + +void PlatformViewAndroidJniImpl::SurfaceTextureAttachToGLContext( + int textureId) {} + +void PlatformViewAndroidJniImpl::SurfaceTextureUpdateTexImage() {} + +void PlatformViewAndroidJniImpl::SurfaceTextureGetTransformMatrix( + SkMatrix transform) {} + +void PlatformViewAndroidJniImpl::SurfaceTextureDetachFromGLContext() {} + } // namespace flutter diff --git a/shell/platform/android/platform_view_android_jni_impl.h b/shell/platform/android/platform_view_android_jni_impl.h new file mode 100644 index 0000000000000..0e0a616606fa3 --- /dev/null +++ b/shell/platform/android/platform_view_android_jni_impl.h @@ -0,0 +1,53 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef FLUTTER_SHELL_PLATFORM_ANDROID_PLATFORM_VIEW_ANDROID_JNI_IMPL_H_ +#define FLUTTER_SHELL_PLATFORM_ANDROID_PLATFORM_VIEW_ANDROID_JNI_IMPL_H_ + +#include "flutter/shell/platform/android/platform_view_android_jni.h" + +namespace flutter { + +class PlatformViewAndroidJniImpl : public PlatformViewAndroidJni { + public: + PlatformViewAndroidJniImpl(); + + ~PlatformViewAndroidJniImpl(); + + void FlutterViewHandlePlatformMessage( + const std::string& channel, + fml::RefPtr message, + int responseId) override; + + void FlutterViewHandlePlatformMessageResponse( + int responseId, + std::unique_ptr data) override; + + void FlutterViewUpdateSemantics(std::vector buffer, + std::vector strings) override; + + void FlutterViewUpdateCustomAccessibilityActions( + std::vector actions_buffer, + std::vector strings) override; + + void FlutterViewOnFirstFrame() override; + + void FlutterViewOnPreEngineRestart() override; + + void SurfaceTextureAttachToGLContext(int textureId) override; + + void SurfaceTextureUpdateTexImage() override; + + void SurfaceTextureGetTransformMatrix(SkMatrix transform) override; + + void SurfaceTextureDetachFromGLContext() override; + + private: + // Reference to FlutterJNI. + const fml::jni::JavaObjectWeakGlobalRef java_object_; +} + +} // namespace flutter + +#endif // FLUTTER_SHELL_PLATFORM_ANDROID_PLATFORM_VIEW_ANDROID_JNI_IMPL_H_ From 4236ef76547dad7e602647cc3a6855fb177c0f08 Mon Sep 17 00:00:00 2001 From: Emmanuel Garcia Date: Mon, 8 Jun 2020 14:27:11 -0700 Subject: [PATCH 02/16] Migrate rest of methods --- shell/platform/android/BUILD.gn | 1 + .../android/android_external_texture_gl.cc | 66 ++---- .../android/android_external_texture_gl.h | 6 +- .../platform/android/android_shell_holder.cc | 11 +- shell/platform/android/android_shell_holder.h | 6 +- .../platform_message_response_android.cc | 57 ++--- .../platform_message_response_android.h | 4 +- .../platform/android/platform_view_android.cc | 81 ++----- .../platform/android/platform_view_android.h | 7 +- .../android/platform_view_android_jni.cc | 11 + .../android/platform_view_android_jni.h | 31 +-- .../android/platform_view_android_jni_impl.cc | 218 +++++++++++++++++- .../android/platform_view_android_jni_impl.h | 20 +- 13 files changed, 317 insertions(+), 202 deletions(-) create mode 100644 shell/platform/android/platform_view_android_jni.cc diff --git a/shell/platform/android/BUILD.gn b/shell/platform/android/BUILD.gn index 5d7c7cf1929fc..22f00ac02c89e 100644 --- a/shell/platform/android/BUILD.gn +++ b/shell/platform/android/BUILD.gn @@ -50,6 +50,7 @@ shared_library("flutter_shell_native") { "platform_message_response_android.h", "platform_view_android.cc", "platform_view_android.h", + "platform_view_android_jni.cc", "platform_view_android_jni.h", "platform_view_android_jni_impl.cc", "platform_view_android_jni_impl.h", diff --git a/shell/platform/android/android_external_texture_gl.cc b/shell/platform/android/android_external_texture_gl.cc index 8c4f960b0f232..42b9b15d5c40e 100644 --- a/shell/platform/android/android_external_texture_gl.cc +++ b/shell/platform/android/android_external_texture_gl.cc @@ -6,15 +6,18 @@ #include -#include "flutter/shell/platform/android/platform_view_android_jni.h" #include "third_party/skia/include/gpu/GrBackendSurface.h" namespace flutter { AndroidExternalTextureGL::AndroidExternalTextureGL( int64_t id, - const fml::jni::JavaObjectWeakGlobalRef& surfaceTexture) - : Texture(id), surface_texture_(surfaceTexture), transform(SkMatrix::I()) {} + const fml::jni::JavaObjectWeakGlobalRef& surface_texture, + std::unique_ptr jni_facade) + : Texture(id), surface_texture_(surface_texture), transform(SkMatrix::I()) { + jni_facade_ = std::unique_ptr( + static_cast(jni_facade.release())); +} AndroidExternalTextureGL::~AndroidExternalTextureGL() { if (state_ == AttachmentState::attached) { @@ -68,36 +71,9 @@ void AndroidExternalTextureGL::Paint(SkCanvas& canvas, } } -// The bounds we set for the canvas are post composition. -// To fill the canvas we need to ensure that the transformation matrix -// on the `SurfaceTexture` will be scaled to fill. We rescale and preseve -// the scaled aspect ratio. -SkSize ScaleToFill(float scaleX, float scaleY) { - const double epsilon = std::numeric_limits::epsilon(); - // scaleY is negative. - const double minScale = fmin(scaleX, fabs(scaleY)); - const double rescale = 1.0f / (minScale + epsilon); - return SkSize::Make(scaleX * rescale, scaleY * rescale); -} - void AndroidExternalTextureGL::UpdateTransform() { - JNIEnv* env = fml::jni::AttachCurrentThread(); - fml::jni::ScopedJavaLocalRef surfaceTexture = - surface_texture_.get(env); - fml::jni::ScopedJavaLocalRef transformMatrix( - env, env->NewFloatArray(16)); - SurfaceTextureGetTransformMatrix(env, surfaceTexture.obj(), - transformMatrix.obj()); - float* m = env->GetFloatArrayElements(transformMatrix.obj(), nullptr); - float scaleX = m[0], scaleY = m[5]; - const SkSize scaled = ScaleToFill(scaleX, scaleY); - SkScalar matrix3[] = { - scaled.fWidth, m[1], m[2], // - m[4], scaled.fHeight, m[6], // - m[8], m[9], m[10], // - }; - env->ReleaseFloatArrayElements(transformMatrix.obj(), m, JNI_ABORT); - transform.set9(matrix3); + jni_facade_->SetCurrentSurfaceTexture(surface_texture_); + jni_facade_->SurfaceTextureGetTransformMatrix(transform); } void AndroidExternalTextureGL::OnGrContextDestroyed() { @@ -108,31 +84,19 @@ void AndroidExternalTextureGL::OnGrContextDestroyed() { } void AndroidExternalTextureGL::Attach(jint textureName) { - JNIEnv* env = fml::jni::AttachCurrentThread(); - fml::jni::ScopedJavaLocalRef surfaceTexture = - surface_texture_.get(env); - if (!surfaceTexture.is_null()) { - SurfaceTextureAttachToGLContext(env, surfaceTexture.obj(), textureName); - } + jni_facade_->SetCurrentSurfaceTexture(surface_texture_); + jni_facade_->SurfaceTextureAttachToGLContext(textureName); } void AndroidExternalTextureGL::Update() { - JNIEnv* env = fml::jni::AttachCurrentThread(); - fml::jni::ScopedJavaLocalRef surfaceTexture = - surface_texture_.get(env); - if (!surfaceTexture.is_null()) { - SurfaceTextureUpdateTexImage(env, surfaceTexture.obj()); - UpdateTransform(); - } + jni_facade_->SetCurrentSurfaceTexture(surface_texture_); + jni_facade_->SurfaceTextureUpdateTexImage(); + UpdateTransform(); } void AndroidExternalTextureGL::Detach() { - JNIEnv* env = fml::jni::AttachCurrentThread(); - fml::jni::ScopedJavaLocalRef surfaceTexture = - surface_texture_.get(env); - if (!surfaceTexture.is_null()) { - SurfaceTextureDetachFromGLContext(env, surfaceTexture.obj()); - } + jni_facade_->SetCurrentSurfaceTexture(surface_texture_); + jni_facade_->SurfaceTextureDetachFromGLContext(); } void AndroidExternalTextureGL::OnTextureUnregistered() {} diff --git a/shell/platform/android/android_external_texture_gl.h b/shell/platform/android/android_external_texture_gl.h index 4e2b187eb3f61..6c0845cf10e5b 100644 --- a/shell/platform/android/android_external_texture_gl.h +++ b/shell/platform/android/android_external_texture_gl.h @@ -8,6 +8,7 @@ #include #include "flutter/flow/texture.h" #include "flutter/fml/platform/android/jni_weak_ref.h" +#include "flutter/shell/platform/android/platform_view_android_jni_impl.h" namespace flutter { @@ -15,7 +16,8 @@ class AndroidExternalTextureGL : public flutter::Texture { public: AndroidExternalTextureGL( int64_t id, - const fml::jni::JavaObjectWeakGlobalRef& surfaceTexture); + const fml::jni::JavaObjectWeakGlobalRef& surface_texture, + std::unique_ptr jni_facade); ~AndroidExternalTextureGL() override; @@ -43,6 +45,8 @@ class AndroidExternalTextureGL : public flutter::Texture { enum class AttachmentState { uninitialized, attached, detached }; + std::unique_ptr jni_facade_; + fml::jni::JavaObjectWeakGlobalRef surface_texture_; AttachmentState state_ = AttachmentState::uninitialized; diff --git a/shell/platform/android/android_shell_holder.cc b/shell/platform/android/android_shell_holder.cc index bde98f83630c6..2af12e3801e49 100644 --- a/shell/platform/android/android_shell_holder.cc +++ b/shell/platform/android/android_shell_holder.cc @@ -29,9 +29,9 @@ static WindowData GetDefaultWindowData() { AndroidShellHolder::AndroidShellHolder( flutter::Settings settings, - fml::jni::JavaObjectWeakGlobalRef java_object, + std::unique_ptr jni_facade, bool is_background_view) - : settings_(std::move(settings)), java_object_(java_object) { + : settings_(std::move(settings)), jni_facade_(std::move(jni_facade)) { static size_t shell_count = 1; auto thread_label = std::to_string(shell_count++); @@ -56,20 +56,19 @@ AndroidShellHolder::AndroidShellHolder( fml::WeakPtr weak_platform_view; Shell::CreateCallback on_create_platform_view = - [is_background_view, java_object, &weak_platform_view](Shell& shell) { + [is_background_view, &jni_facade, &weak_platform_view](Shell& shell) { std::unique_ptr platform_view_android; if (is_background_view) { platform_view_android = std::make_unique( shell, // delegate shell.GetTaskRunners(), // task runners - java_object // java object handle for JNI interop + std::move(jni_facade) // JNI interop ); - } else { platform_view_android = std::make_unique( shell, // delegate shell.GetTaskRunners(), // task runners - java_object, // java object handle for JNI interop + std::move(jni_facade), // JNI interop shell.GetSettings() .enable_software_rendering // use software rendering ); diff --git a/shell/platform/android/android_shell_holder.h b/shell/platform/android/android_shell_holder.h index 9be7f35df0bb8..7bd8bcbc0ebaf 100644 --- a/shell/platform/android/android_shell_holder.h +++ b/shell/platform/android/android_shell_holder.h @@ -8,7 +8,6 @@ #include #include "flutter/fml/macros.h" -#include "flutter/fml/platform/android/jni_weak_ref.h" #include "flutter/fml/unique_fd.h" #include "flutter/lib/ui/window/viewport_metrics.h" #include "flutter/runtime/window_data.h" @@ -16,13 +15,14 @@ #include "flutter/shell/common/shell.h" #include "flutter/shell/common/thread_host.h" #include "flutter/shell/platform/android/platform_view_android.h" +#include "flutter/shell/platform/android/platform_view_android_jni.h" namespace flutter { class AndroidShellHolder { public: AndroidShellHolder(flutter::Settings settings, - fml::jni::JavaObjectWeakGlobalRef java_object, + std::unique_ptr jni_facade, bool is_background_view); ~AndroidShellHolder(); @@ -42,7 +42,7 @@ class AndroidShellHolder { private: const flutter::Settings settings_; - const fml::jni::JavaObjectWeakGlobalRef java_object_; + const std::unique_ptr jni_facade_; fml::WeakPtr platform_view_; ThreadHost thread_host_; std::unique_ptr shell_; diff --git a/shell/platform/android/platform_message_response_android.cc b/shell/platform/android/platform_message_response_android.cc index f79f2b8c155a7..7c75fa2d15ff3 100644 --- a/shell/platform/android/platform_message_response_android.cc +++ b/shell/platform/android/platform_message_response_android.cc @@ -11,10 +11,10 @@ namespace flutter { PlatformMessageResponseAndroid::PlatformMessageResponseAndroid( int response_id, - fml::jni::JavaObjectWeakGlobalRef weak_java_object, + std::unique_ptr jni_facade, fml::RefPtr platform_task_runner) : response_id_(response_id), - weak_java_object_(weak_java_object), + jni_facade_(std::move(jni_facade)), platform_task_runner_(std::move(platform_task_runner)) {} PlatformMessageResponseAndroid::~PlatformMessageResponseAndroid() = default; @@ -22,54 +22,27 @@ PlatformMessageResponseAndroid::~PlatformMessageResponseAndroid() = default; // |flutter::PlatformMessageResponse| void PlatformMessageResponseAndroid::Complete( std::unique_ptr data) { - platform_task_runner_->PostTask( - fml::MakeCopyable([response = response_id_, // - weak_java_object = weak_java_object_, // - data = std::move(data) // - ]() { - // We are on the platform thread. Attempt to get the strong reference to - // the Java object. - auto* env = fml::jni::AttachCurrentThread(); - auto java_object = weak_java_object.get(env); - - if (java_object.is_null()) { - // The Java object was collected before this message response got to - // it. Drop the response on the floor. - return; - } - - // Convert the vector to a Java byte array. - fml::jni::ScopedJavaLocalRef data_array( - env, env->NewByteArray(data->GetSize())); - env->SetByteArrayRegion( - data_array.obj(), 0, data->GetSize(), - reinterpret_cast(data->GetMapping())); - - // Make the response call into Java. - FlutterViewHandlePlatformMessageResponse(env, java_object.obj(), - response, data_array.obj()); - })); + // platform_task_runner_->PostTask( + // fml::MakeCopyable([response_id = response_id_, // + // jni_facade = std::move(jni_facade_), // + // data = std::move(data) // + // ]() { + // // Make the response call into Java. + // jni_facade->FlutterViewHandlePlatformMessageResponse(response_id, + // std::move(data)); + // })); } // |flutter::PlatformMessageResponse| void PlatformMessageResponseAndroid::CompleteEmpty() { platform_task_runner_->PostTask( - fml::MakeCopyable([response = response_id_, // + fml::MakeCopyable([response_id = response_id_, // + jni_facade = std::move(jni_facade_), // weak_java_object = weak_java_object_ // ]() { - // We are on the platform thread. Attempt to get the strong reference to - // the Java object. - auto* env = fml::jni::AttachCurrentThread(); - auto java_object = weak_java_object.get(env); - - if (java_object.is_null()) { - // The Java object was collected before this message response got to - // it. Drop the response on the floor. - return; - } // Make the response call into Java. - FlutterViewHandlePlatformMessageResponse(env, java_object.obj(), - response, nullptr); + jni_facade->FlutterViewHandlePlatformMessageResponse(response_id, + nullptr); })); } } // namespace flutter diff --git a/shell/platform/android/platform_message_response_android.h b/shell/platform/android/platform_message_response_android.h index a93b4c802efe4..c60a71b9fe255 100644 --- a/shell/platform/android/platform_message_response_android.h +++ b/shell/platform/android/platform_message_response_android.h @@ -9,6 +9,7 @@ #include "flutter/fml/platform/android/jni_weak_ref.h" #include "flutter/fml/task_runner.h" #include "flutter/lib/ui/window/platform_message_response.h" +#include "flutter/shell/platform/android/platform_view_android_jni.h" namespace flutter { @@ -23,13 +24,14 @@ class PlatformMessageResponseAndroid : public flutter::PlatformMessageResponse { private: PlatformMessageResponseAndroid( int response_id, - fml::jni::JavaObjectWeakGlobalRef weak_java_object, + std::unique_ptr jni_facade, fml::RefPtr platform_task_runner); ~PlatformMessageResponseAndroid() override; int response_id_; fml::jni::JavaObjectWeakGlobalRef weak_java_object_; + std::unique_ptr jni_facade_; fml::RefPtr platform_task_runner_; FML_FRIEND_MAKE_REF_COUNTED(PlatformMessageResponseAndroid); diff --git a/shell/platform/android/platform_view_android.cc b/shell/platform/android/platform_view_android.cc index c1e396d043e02..e4383a986d25c 100644 --- a/shell/platform/android/platform_view_android.cc +++ b/shell/platform/android/platform_view_android.cc @@ -22,10 +22,10 @@ namespace flutter { PlatformViewAndroid::PlatformViewAndroid( PlatformView::Delegate& delegate, flutter::TaskRunners task_runners, - fml::jni::JavaObjectWeakGlobalRef java_object, + std::unique_ptr jni_facade, bool use_software_rendering) : PlatformView(delegate, std::move(task_runners)), - java_object_(java_object) { + jni_facade_(std::move(jni_facade)) { std::shared_ptr android_context; if (use_software_rendering) { android_context = AndroidContext::Create(AndroidRenderingAPI::kSoftware); @@ -45,10 +45,9 @@ PlatformViewAndroid::PlatformViewAndroid( PlatformViewAndroid::PlatformViewAndroid( PlatformView::Delegate& delegate, flutter::TaskRunners task_runners, - fml::jni::JavaObjectWeakGlobalRef java_object) + std::unique_ptr jni_facade) : PlatformView(delegate, std::move(task_runners)), - java_object_(java_object), - android_surface_(nullptr) {} + jni_facade_(std::move(jni_facade)) {} PlatformViewAndroid::~PlatformViewAndroid() = default; @@ -113,7 +112,8 @@ void PlatformViewAndroid::DispatchPlatformMessage(JNIEnv* env, fml::RefPtr response; if (response_id) { response = fml::MakeRefCounted( - response_id, java_object_, task_runners_.GetPlatformTaskRunner()); + response_id, std::move(jni_facade_), + task_runners_.GetPlatformTaskRunner()); } PlatformView::DispatchPlatformMessage( @@ -127,7 +127,8 @@ void PlatformViewAndroid::DispatchEmptyPlatformMessage(JNIEnv* env, fml::RefPtr response; if (response_id) { response = fml::MakeRefCounted( - response_id, java_object_, task_runners_.GetPlatformTaskRunner()); + response_id, std::move(jni_facade_), + task_runners_.GetPlatformTaskRunner()); } PlatformView::DispatchPlatformMessage( @@ -171,46 +172,19 @@ void PlatformViewAndroid::InvokePlatformMessageEmptyResponseCallback( // |PlatformView| void PlatformViewAndroid::HandlePlatformMessage( fml::RefPtr message) { - JNIEnv* env = fml::jni::AttachCurrentThread(); - fml::jni::ScopedJavaLocalRef view = java_object_.get(env); - if (view.is_null()) - return; - int response_id = 0; if (auto response = message->response()) { response_id = next_response_id_++; pending_responses_[response_id] = response; } - auto java_channel = fml::jni::StringToJavaString(env, message->channel()); - if (message->hasData()) { - fml::jni::ScopedJavaLocalRef message_array( - env, env->NewByteArray(message->data().size())); - env->SetByteArrayRegion( - message_array.obj(), 0, message->data().size(), - reinterpret_cast(message->data().data())); - message = nullptr; - - // This call can re-enter in InvokePlatformMessageXxxResponseCallback. - FlutterViewHandlePlatformMessage(env, view.obj(), java_channel.obj(), - message_array.obj(), response_id); - } else { - message = nullptr; - - // This call can re-enter in InvokePlatformMessageXxxResponseCallback. - FlutterViewHandlePlatformMessage(env, view.obj(), java_channel.obj(), - nullptr, response_id); - } + // This call can re-enter in InvokePlatformMessageXxxResponseCallback. + jni_facade_->FlutterViewHandlePlatformMessage(message, response_id); + message = nullptr; } // |PlatformView| void PlatformViewAndroid::OnPreEngineRestart() const { - JNIEnv* env = fml::jni::AttachCurrentThread(); - fml::jni::ScopedJavaLocalRef view = java_object_.get(env); - if (view.is_null()) { - // The Java object died. - return; - } - FlutterViewOnPreEngineRestart(fml::jni::AttachCurrentThread(), view.obj()); + jni_facade_->FlutterViewOnPreEngineRestart(); } void PlatformViewAndroid::DispatchSemanticsAction(JNIEnv* env, @@ -242,12 +216,7 @@ void PlatformViewAndroid::UpdateSemantics( constexpr size_t kBytesPerChild = sizeof(int32_t); constexpr size_t kBytesPerAction = 4 * sizeof(int32_t); - JNIEnv* env = fml::jni::AttachCurrentThread(); { - fml::jni::ScopedJavaLocalRef view = java_object_.get(env); - if (view.is_null()) - return; - size_t num_bytes = 0; for (const auto& value : update) { num_bytes += kBytesPerNode; @@ -372,20 +341,12 @@ void PlatformViewAndroid::UpdateSemantics( // Calling NewDirectByteBuffer in API level 22 and below with a size of zero // will cause a JNI crash. if (actions_buffer.size() > 0) { - fml::jni::ScopedJavaLocalRef direct_actions_buffer( - env, env->NewDirectByteBuffer(actions_buffer.data(), - actions_buffer.size())); - FlutterViewUpdateCustomAccessibilityActions( - env, view.obj(), direct_actions_buffer.obj(), - fml::jni::VectorToStringArray(env, action_strings).obj()); + jni_facade_->FlutterViewUpdateCustomAccessibilityActions(actions_buffer, + strings); } if (buffer.size() > 0) { - fml::jni::ScopedJavaLocalRef direct_buffer( - env, env->NewDirectByteBuffer(buffer.data(), buffer.size())); - FlutterViewUpdateSemantics( - env, view.obj(), direct_buffer.obj(), - fml::jni::VectorToStringArray(env, strings).obj()); + jni_facade_->FlutterViewUpdateSemantics(buffer, strings); } } } @@ -393,8 +354,8 @@ void PlatformViewAndroid::UpdateSemantics( void PlatformViewAndroid::RegisterExternalTexture( int64_t texture_id, const fml::jni::JavaObjectWeakGlobalRef& surface_texture) { - RegisterTexture( - std::make_shared(texture_id, surface_texture)); + RegisterTexture(std::make_shared( + texture_id, surface_texture, std::move(jni_facade_))); } // |PlatformView| @@ -454,13 +415,7 @@ void PlatformViewAndroid::InstallFirstFrameCallback() { } void PlatformViewAndroid::FireFirstFrameCallback() { - JNIEnv* env = fml::jni::AttachCurrentThread(); - fml::jni::ScopedJavaLocalRef view = java_object_.get(env); - if (view.is_null()) { - // The Java object died. - return; - } - FlutterViewOnFirstFrame(fml::jni::AttachCurrentThread(), view.obj()); + jni_facade_->FlutterViewOnFirstFrame(); } } // namespace flutter diff --git a/shell/platform/android/platform_view_android.h b/shell/platform/android/platform_view_android.h index d517e7fda2970..2be1cc4f9efae 100644 --- a/shell/platform/android/platform_view_android.h +++ b/shell/platform/android/platform_view_android.h @@ -17,6 +17,7 @@ #include "flutter/shell/common/platform_view.h" #include "flutter/shell/platform/android/android_native_window.h" #include "flutter/shell/platform/android/android_surface.h" +#include "flutter/shell/platform/android/platform_view_android_jni.h" namespace flutter { @@ -28,12 +29,12 @@ class PlatformViewAndroid final : public PlatformView { // background execution. PlatformViewAndroid(PlatformView::Delegate& delegate, flutter::TaskRunners task_runners, - fml::jni::JavaObjectWeakGlobalRef java_object); + std::unique_ptr jni_facade); // Creates a PlatformViewAndroid with a rendering surface. PlatformViewAndroid(PlatformView::Delegate& delegate, flutter::TaskRunners task_runners, - fml::jni::JavaObjectWeakGlobalRef java_object, + std::unique_ptr jni_facade, bool use_software_rendering); ~PlatformViewAndroid() override; @@ -74,7 +75,7 @@ class PlatformViewAndroid final : public PlatformView { const fml::jni::JavaObjectWeakGlobalRef& surface_texture); private: - const fml::jni::JavaObjectWeakGlobalRef java_object_; + std::unique_ptr jni_facade_; std::unique_ptr android_surface_; // We use id 0 to mean that no response is expected. diff --git a/shell/platform/android/platform_view_android_jni.cc b/shell/platform/android/platform_view_android_jni.cc new file mode 100644 index 0000000000000..d81a613b01c37 --- /dev/null +++ b/shell/platform/android/platform_view_android_jni.cc @@ -0,0 +1,11 @@ +// Copyright 2013 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "flutter/shell/platform/android/platform_view_android_jni.h" + +namespace flutter { + +PlatformViewAndroidJni::~PlatformViewAndroidJni() = default; + +} // namespace flutter diff --git a/shell/platform/android/platform_view_android_jni.h b/shell/platform/android/platform_view_android_jni.h index f535284ce856d..eeb8c6d2fa99d 100644 --- a/shell/platform/android/platform_view_android_jni.h +++ b/shell/platform/android/platform_view_android_jni.h @@ -6,41 +6,44 @@ #define FLUTTER_SHELL_PLATFORM_ANDROID_PLATFORM_VIEW_ANDROID_JNI_H_ #include "flutter/fml/macros.h" +#include "flutter/fml/mapping.h" +#include "flutter/lib/ui/window/platform_message.h" + +#include "third_party/skia/include/core/SkMatrix.h" namespace flutter { class PlatformViewAndroidJni { public: + virtual ~PlatformViewAndroidJni(); + virtual void FlutterViewHandlePlatformMessage( - const std::string& channel, fml::RefPtr message, - int responseId); + int responseId) = 0; virtual void FlutterViewHandlePlatformMessageResponse( int responseId, - std::unique_ptr data); + std::unique_ptr data) = 0; virtual void FlutterViewUpdateSemantics(std::vector buffer, - std::vector strings); + std::vector strings) = 0; virtual void FlutterViewUpdateCustomAccessibilityActions( std::vector actions_buffer, - std::vector strings); - - virtual void FlutterViewOnFirstFrame(); + std::vector strings) = 0; - virtual void FlutterViewOnPreEngineRestart(); + virtual void FlutterViewOnFirstFrame() = 0; - virtual void SurfaceTextureAttachToGLContext(int textureId); + virtual void FlutterViewOnPreEngineRestart() = 0; - virtual void SurfaceTextureUpdateTexImage(); + virtual void SurfaceTextureAttachToGLContext(int textureId) = 0; - virtual void SurfaceTextureGetTransformMatrix(SkMatrix transform); + virtual void SurfaceTextureUpdateTexImage() = 0; - virtual void SurfaceTextureDetachFromGLContext(); + virtual void SurfaceTextureGetTransformMatrix(SkMatrix& transform) = 0; - FML_DISALLOW_COPY_AND_ASSIGN(PlatformViewAndroidJni); -} + virtual void SurfaceTextureDetachFromGLContext() = 0; +}; } // namespace flutter diff --git a/shell/platform/android/platform_view_android_jni_impl.cc b/shell/platform/android/platform_view_android_jni_impl.cc index 9b61f84f3bb3b..d0395e0a02830 100644 --- a/shell/platform/android/platform_view_android_jni_impl.cc +++ b/shell/platform/android/platform_view_android_jni_impl.cc @@ -153,8 +153,11 @@ static jlong AttachJNI(JNIEnv* env, jobject flutterJNI, jboolean is_background_view) { fml::jni::JavaObjectWeakGlobalRef java_object(env, flutterJNI); + std::unique_ptr jni_facade = + std::make_unique(java_object); auto shell_holder = std::make_unique( - FlutterMain::Get().GetSettings(), java_object, is_background_view); + FlutterMain::Get().GetSettings(), std::move(jni_facade), + is_background_view); if (shell_holder->IsValid()) { return reinterpret_cast(shell_holder.release()); } else { @@ -801,34 +804,225 @@ PlatformViewAndroidJniImpl::PlatformViewAndroidJniImpl( PlatformViewAndroidJniImpl::~PlatformViewAndroidJniImpl() = default; void PlatformViewAndroidJniImpl::FlutterViewHandlePlatformMessage( - const std::string& channel, fml::RefPtr message, - int responseId) {} + int responseId) { + JNIEnv* env = fml::jni::AttachCurrentThread(); + + auto java_object = java_object_.get(env); + if (java_object.is_null()) { + return; + } + + fml::jni::ScopedJavaLocalRef java_channel = + fml::jni::StringToJavaString(env, message->channel()); + + if (message->hasData()) { + fml::jni::ScopedJavaLocalRef message_array( + env, env->NewByteArray(message->data().size())); + env->SetByteArrayRegion( + message_array.obj(), 0, message->data().size(), + reinterpret_cast(message->data().data())); + env->CallVoidMethod(java_object.obj(), g_handle_platform_message_method, + java_channel.obj(), message_array.obj(), responseId); + } else { + env->CallVoidMethod(java_object.obj(), g_handle_platform_message_method, + java_channel.obj(), nullptr, responseId); + } + + FML_CHECK(CheckException(env)); +} void PlatformViewAndroidJniImpl::FlutterViewHandlePlatformMessageResponse( int responseId, - std::unique_ptr data) {} + std::unique_ptr data) { + // We are on the platform thread. Attempt to get the strong reference to + // the Java object. + JNIEnv* env = fml::jni::AttachCurrentThread(); + + auto java_object = java_object_.get(env); + if (java_object.is_null()) { + // The Java object was collected before this message response got to + // it. Drop the response on the floor. + return; + } + if (data == nullptr) { // Empty response. + env->CallVoidMethod(java_object.obj(), + g_handle_platform_message_response_method, responseId, + nullptr); + } else { + // Convert the vector to a Java byte array. + fml::jni::ScopedJavaLocalRef data_array( + env, env->NewByteArray(data->GetSize())); + env->SetByteArrayRegion(data_array.obj(), 0, data->GetSize(), + reinterpret_cast(data->GetMapping())); + + env->CallVoidMethod(java_object.obj(), + g_handle_platform_message_response_method, responseId, + data_array.obj()); + } + + FML_CHECK(CheckException(env)); +} void PlatformViewAndroidJniImpl::FlutterViewUpdateSemantics( std::vector buffer, - std::vector strings) {} + std::vector strings) { + JNIEnv* env = fml::jni::AttachCurrentThread(); + + auto java_object = java_object_.get(env); + if (java_object.is_null()) { + return; + } + + fml::jni::ScopedJavaLocalRef direct_buffer( + env, env->NewDirectByteBuffer(buffer.data(), buffer.size())); + fml::jni::ScopedJavaLocalRef jstrings = + fml::jni::VectorToStringArray(env, strings); + + env->CallVoidMethod(java_object.obj(), g_update_semantics_method, + direct_buffer.obj(), jstrings.obj()); + + FML_CHECK(CheckException(env)); +} void PlatformViewAndroidJniImpl::FlutterViewUpdateCustomAccessibilityActions( std::vector actions_buffer, - std::vector strings) {} + std::vector strings) { + JNIEnv* env = fml::jni::AttachCurrentThread(); + + auto java_object = java_object_.get(env); + if (java_object.is_null()) { + return; + } + + fml::jni::ScopedJavaLocalRef direct_actions_buffer( + env, + env->NewDirectByteBuffer(actions_buffer.data(), actions_buffer.size())); + + fml::jni::ScopedJavaLocalRef jstrings = + fml::jni::VectorToStringArray(env, strings); + + env->CallVoidMethod(java_object.obj(), + g_update_custom_accessibility_actions_method, + direct_actions_buffer.obj(), jstrings.obj()); + + FML_CHECK(CheckException(env)); +} -void PlatformViewAndroidJniImpl::FlutterViewOnFirstFrame() {} +void PlatformViewAndroidJniImpl::FlutterViewOnFirstFrame() { + JNIEnv* env = fml::jni::AttachCurrentThread(); -void PlatformViewAndroidJniImpl::FlutterViewOnPreEngineRestart() {} + auto java_object = java_object_.get(env); + if (java_object.is_null()) { + return; + } + + env->CallVoidMethod(java_object.obj(), g_on_first_frame_method); + + FML_CHECK(CheckException(env)); +} + +void PlatformViewAndroidJniImpl::FlutterViewOnPreEngineRestart() { + JNIEnv* env = fml::jni::AttachCurrentThread(); + + auto java_object = java_object_.get(env); + if (java_object.is_null()) { + return; + } + + env->CallVoidMethod(java_object.obj(), g_on_engine_restart_method); + + FML_CHECK(CheckException(env)); +} + +void PlatformViewAndroidJniImpl::SetCurrentSurfaceTexture( + fml::jni::JavaObjectWeakGlobalRef& surface_texture) { + surface_texture_ = surface_texture; +} void PlatformViewAndroidJniImpl::SurfaceTextureAttachToGLContext( - int textureId) {} + int textureId) { + JNIEnv* env = fml::jni::AttachCurrentThread(); + + fml::jni::ScopedJavaLocalRef surface_texture = + surface_texture_.get(env); + if (surface_texture.is_null()) { + return; + } -void PlatformViewAndroidJniImpl::SurfaceTextureUpdateTexImage() {} + env->CallVoidMethod(surface_texture.obj(), g_attach_to_gl_context_method, + textureId); + + FML_CHECK(CheckException(env)); +} + +void PlatformViewAndroidJniImpl::SurfaceTextureUpdateTexImage() { + JNIEnv* env = fml::jni::AttachCurrentThread(); + + fml::jni::ScopedJavaLocalRef surface_texture = + surface_texture_.get(env); + if (surface_texture.is_null()) { + return; + } + + env->CallVoidMethod(surface_texture.obj(), g_update_tex_image_method); + + FML_CHECK(CheckException(env)); +} + +// The bounds we set for the canvas are post composition. +// To fill the canvas we need to ensure that the transformation matrix +// on the `SurfaceTexture` will be scaled to fill. We rescale and preseve +// the scaled aspect ratio. +SkSize ScaleToFill(float scaleX, float scaleY) { + const double epsilon = std::numeric_limits::epsilon(); + // scaleY is negative. + const double minScale = fmin(scaleX, fabs(scaleY)); + const double rescale = 1.0f / (minScale + epsilon); + return SkSize::Make(scaleX * rescale, scaleY * rescale); +} void PlatformViewAndroidJniImpl::SurfaceTextureGetTransformMatrix( - SkMatrix transform) {} + SkMatrix& transform) { + JNIEnv* env = fml::jni::AttachCurrentThread(); + + fml::jni::ScopedJavaLocalRef surface_texture = + surface_texture_.get(env); + if (surface_texture.is_null()) { + return; + } -void PlatformViewAndroidJniImpl::SurfaceTextureDetachFromGLContext() {} + fml::jni::ScopedJavaLocalRef transformMatrix( + env, env->NewFloatArray(16)); + + env->CallVoidMethod(surface_texture.obj(), g_get_transform_matrix_method, + transformMatrix.obj()); + FML_CHECK(CheckException(env)); + + float* m = env->GetFloatArrayElements(transformMatrix.obj(), nullptr); + float scaleX = m[0], scaleY = m[5]; + const SkSize scaled = ScaleToFill(scaleX, scaleY); + SkScalar matrix3[] = { + scaled.fWidth, m[1], m[2], // + m[4], scaled.fHeight, m[6], // + m[8], m[9], m[10], // + }; + env->ReleaseFloatArrayElements(transformMatrix.obj(), m, JNI_ABORT); + transform.set9(matrix3); +} + +void PlatformViewAndroidJniImpl::SurfaceTextureDetachFromGLContext() { + JNIEnv* env = fml::jni::AttachCurrentThread(); + + fml::jni::ScopedJavaLocalRef surface_texture = + surface_texture_.get(env); + if (surface_texture.is_null()) { + return; + } + + env->CallVoidMethod(surface_texture.obj(), g_detach_from_gl_context_method); + + FML_CHECK(CheckException(env)); +} } // namespace flutter diff --git a/shell/platform/android/platform_view_android_jni_impl.h b/shell/platform/android/platform_view_android_jni_impl.h index 0e0a616606fa3..6ad3c05f23b7e 100644 --- a/shell/platform/android/platform_view_android_jni_impl.h +++ b/shell/platform/android/platform_view_android_jni_impl.h @@ -5,18 +5,18 @@ #ifndef FLUTTER_SHELL_PLATFORM_ANDROID_PLATFORM_VIEW_ANDROID_JNI_IMPL_H_ #define FLUTTER_SHELL_PLATFORM_ANDROID_PLATFORM_VIEW_ANDROID_JNI_IMPL_H_ +#include "flutter/fml/platform/android/jni_weak_ref.h" #include "flutter/shell/platform/android/platform_view_android_jni.h" namespace flutter { -class PlatformViewAndroidJniImpl : public PlatformViewAndroidJni { +class PlatformViewAndroidJniImpl final : public PlatformViewAndroidJni { public: - PlatformViewAndroidJniImpl(); + PlatformViewAndroidJniImpl(fml::jni::JavaObjectWeakGlobalRef java_object); - ~PlatformViewAndroidJniImpl(); + ~PlatformViewAndroidJniImpl() override; void FlutterViewHandlePlatformMessage( - const std::string& channel, fml::RefPtr message, int responseId) override; @@ -35,18 +35,26 @@ class PlatformViewAndroidJniImpl : public PlatformViewAndroidJni { void FlutterViewOnPreEngineRestart() override; + void SetCurrentSurfaceTexture( + fml::jni::JavaObjectWeakGlobalRef& surface_texture); + void SurfaceTextureAttachToGLContext(int textureId) override; void SurfaceTextureUpdateTexImage() override; - void SurfaceTextureGetTransformMatrix(SkMatrix transform) override; + void SurfaceTextureGetTransformMatrix(SkMatrix& transform) override; void SurfaceTextureDetachFromGLContext() override; private: // Reference to FlutterJNI. const fml::jni::JavaObjectWeakGlobalRef java_object_; -} + + // Reference to the current Surface texture object. + fml::jni::JavaObjectWeakGlobalRef surface_texture_; + + FML_DISALLOW_COPY_AND_ASSIGN(PlatformViewAndroidJniImpl); +}; } // namespace flutter From a4944fb2f872b4dba599f5b75a87fd2ad3df31bc Mon Sep 17 00:00:00 2001 From: Emmanuel Garcia Date: Mon, 8 Jun 2020 14:31:30 -0700 Subject: [PATCH 03/16] Remove existing static methods --- .../android/platform_view_android_jni_impl.cc | 59 ------------------- 1 file changed, 59 deletions(-) diff --git a/shell/platform/android/platform_view_android_jni_impl.cc b/shell/platform/android/platform_view_android_jni_impl.cc index d0395e0a02830..17a4e3faf956f 100644 --- a/shell/platform/android/platform_view_android_jni_impl.cc +++ b/shell/platform/android/platform_view_android_jni_impl.cc @@ -69,85 +69,26 @@ jobject CreateFlutterCallbackInformation( } static jmethodID g_handle_platform_message_method = nullptr; -void FlutterViewHandlePlatformMessage(JNIEnv* env, - jobject obj, - jstring channel, - jobject message, - jint responseId) { - env->CallVoidMethod(obj, g_handle_platform_message_method, channel, message, - responseId); - FML_CHECK(CheckException(env)); -} static jmethodID g_handle_platform_message_response_method = nullptr; -void FlutterViewHandlePlatformMessageResponse(JNIEnv* env, - jobject obj, - jint responseId, - jobject response) { - env->CallVoidMethod(obj, g_handle_platform_message_response_method, - responseId, response); - FML_CHECK(CheckException(env)); -} static jmethodID g_update_semantics_method = nullptr; -void FlutterViewUpdateSemantics(JNIEnv* env, - jobject obj, - jobject buffer, - jobjectArray strings) { - env->CallVoidMethod(obj, g_update_semantics_method, buffer, strings); - FML_CHECK(CheckException(env)); -} static jmethodID g_update_custom_accessibility_actions_method = nullptr; -void FlutterViewUpdateCustomAccessibilityActions(JNIEnv* env, - jobject obj, - jobject buffer, - jobjectArray strings) { - env->CallVoidMethod(obj, g_update_custom_accessibility_actions_method, buffer, - strings); - FML_CHECK(CheckException(env)); -} static jmethodID g_on_first_frame_method = nullptr; -void FlutterViewOnFirstFrame(JNIEnv* env, jobject obj) { - env->CallVoidMethod(obj, g_on_first_frame_method); - FML_CHECK(CheckException(env)); -} static jmethodID g_on_engine_restart_method = nullptr; -void FlutterViewOnPreEngineRestart(JNIEnv* env, jobject obj) { - env->CallVoidMethod(obj, g_on_engine_restart_method); - FML_CHECK(CheckException(env)); -} static jmethodID g_attach_to_gl_context_method = nullptr; -void SurfaceTextureAttachToGLContext(JNIEnv* env, jobject obj, jint textureId) { - env->CallVoidMethod(obj, g_attach_to_gl_context_method, textureId); - FML_CHECK(CheckException(env)); -} static jmethodID g_update_tex_image_method = nullptr; -void SurfaceTextureUpdateTexImage(JNIEnv* env, jobject obj) { - env->CallVoidMethod(obj, g_update_tex_image_method); - FML_CHECK(CheckException(env)); -} static jmethodID g_get_transform_matrix_method = nullptr; -void SurfaceTextureGetTransformMatrix(JNIEnv* env, - jobject obj, - jfloatArray result) { - env->CallVoidMethod(obj, g_get_transform_matrix_method, result); - FML_CHECK(CheckException(env)); -} static jmethodID g_detach_from_gl_context_method = nullptr; -void SurfaceTextureDetachFromGLContext(JNIEnv* env, jobject obj) { - env->CallVoidMethod(obj, g_detach_from_gl_context_method); - FML_CHECK(CheckException(env)); -} // Called By Java - static jlong AttachJNI(JNIEnv* env, jclass clazz, jobject flutterJNI, From b52e79e1c252a45a5b8d9181d67fe97126311d1e Mon Sep 17 00:00:00 2001 From: Emmanuel Garcia Date: Mon, 8 Jun 2020 14:41:23 -0700 Subject: [PATCH 04/16] Update license --- ci/licenses_golden/licenses_flutter | 1 + 1 file changed, 1 insertion(+) diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index 3e1e4e3221368..737848571736c 100755 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -788,6 +788,7 @@ FILE: ../../../flutter/shell/platform/android/platform_message_response_android. FILE: ../../../flutter/shell/platform/android/platform_message_response_android.h FILE: ../../../flutter/shell/platform/android/platform_view_android.cc FILE: ../../../flutter/shell/platform/android/platform_view_android.h +FILE: ../../../flutter/shell/platform/android/platform_view_android_jni.cc FILE: ../../../flutter/shell/platform/android/platform_view_android_jni.h FILE: ../../../flutter/shell/platform/android/platform_view_android_jni_impl.cc FILE: ../../../flutter/shell/platform/android/platform_view_android_jni_impl.h From 9bc03a90be998eed7e2aba43cc2c105497686e8a Mon Sep 17 00:00:00 2001 From: Emmanuel Garcia Date: Mon, 8 Jun 2020 14:43:24 -0700 Subject: [PATCH 05/16] Separate build rules --- shell/platform/android/BUILD.gn | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/shell/platform/android/BUILD.gn b/shell/platform/android/BUILD.gn index 22f00ac02c89e..627ccf645ec61 100644 --- a/shell/platform/android/BUILD.gn +++ b/shell/platform/android/BUILD.gn @@ -50,8 +50,6 @@ shared_library("flutter_shell_native") { "platform_message_response_android.h", "platform_view_android.cc", "platform_view_android.h", - "platform_view_android_jni.cc", - "platform_view_android_jni.h", "platform_view_android_jni_impl.cc", "platform_view_android_jni_impl.h", "vsync_waiter_android.cc", @@ -60,6 +58,7 @@ shared_library("flutter_shell_native") { deps = [ ":android_gpu_configuration", + ":android_jni", ":icudtl_object", "//flutter/assets", "//flutter/common", @@ -97,6 +96,21 @@ shared_library("flutter_shell_native") { ldflags = [ "-Wl,--version-script=" + rebase_path("android_exports.lst") ] } +source_set("android_jni") { + sources = [ + "platform_view_android_jni.cc", + "platform_view_android_jni.h", + ] + + public_configs = [ "//flutter:config" ] + + deps = [ + "//flutter/fml", + "//flutter/lib/ui", + "//third_party/skia", + ] +} + action("gen_android_build_config_java") { script = "//flutter/tools/gen_android_buildconfig.py" From e2ff6251e06c660a8afc6d0503a5f29fe16d5b75 Mon Sep 17 00:00:00 2001 From: Emmanuel Garcia Date: Mon, 8 Jun 2020 15:19:11 -0700 Subject: [PATCH 06/16] Pass JNI facade to Surfaces and External View Embedder --- shell/platform/android/android_surface.cc | 10 ++++++---- shell/platform/android/android_surface.h | 4 +++- shell/platform/android/android_surface_gl.cc | 6 ++++-- shell/platform/android/android_surface_gl.h | 4 +++- shell/platform/android/android_surface_software.cc | 6 ++++-- shell/platform/android/android_surface_software.h | 3 ++- shell/platform/android/android_surface_vulkan.cc | 6 ++++-- shell/platform/android/android_surface_vulkan.h | 1 + shell/platform/android/external_view_embedder/BUILD.gn | 1 + .../external_view_embedder/external_view_embedder.cc | 4 ++++ .../external_view_embedder/external_view_embedder.h | 9 ++++++++- .../external_view_embedder_unittests.cc | 6 +++--- shell/platform/android/platform_view_android.cc | 3 ++- 13 files changed, 45 insertions(+), 18 deletions(-) diff --git a/shell/platform/android/android_surface.cc b/shell/platform/android/android_surface.cc index 711a1e0571002..0738b354e3923 100644 --- a/shell/platform/android/android_surface.cc +++ b/shell/platform/android/android_surface.cc @@ -15,18 +15,20 @@ namespace flutter { std::unique_ptr AndroidSurface::Create( - std::shared_ptr android_context) { + std::shared_ptr android_context, + std::unique_ptr jni_facade) { std::unique_ptr surface; switch (android_context->RenderingApi()) { case AndroidRenderingAPI::kSoftware: - surface = std::make_unique(); + surface = std::make_unique(std::move(jni_facade)); break; case AndroidRenderingAPI::kOpenGLES: - surface = std::make_unique(android_context); + surface = std::make_unique(android_context, + std::move(jni_facade)); break; case AndroidRenderingAPI::kVulkan: #if SHELL_ENABLE_VULKAN - surface = std::make_unique(); + surface = std::make_unique(std::move(jni_facade)); #endif // SHELL_ENABLE_VULKAN break; } diff --git a/shell/platform/android/android_surface.h b/shell/platform/android/android_surface.h index 9c463c9907325..dd26f0a9ef4ea 100644 --- a/shell/platform/android/android_surface.h +++ b/shell/platform/android/android_surface.h @@ -14,6 +14,7 @@ #include "flutter/shell/common/surface.h" #include "flutter/shell/platform/android/android_context.h" #include "flutter/shell/platform/android/android_native_window.h" +#include "flutter/shell/platform/android/platform_view_android_jni.h" #include "third_party/skia/include/core/SkSize.h" namespace flutter { @@ -21,7 +22,8 @@ namespace flutter { class AndroidSurface { public: static std::unique_ptr Create( - std::shared_ptr android_context); + std::shared_ptr android_context, + std::unique_ptr jni_facade); virtual ~AndroidSurface(); diff --git a/shell/platform/android/android_surface_gl.cc b/shell/platform/android/android_surface_gl.cc index 59e5eacffac2b..e7ad8cb6de026 100644 --- a/shell/platform/android/android_surface_gl.cc +++ b/shell/platform/android/android_surface_gl.cc @@ -12,7 +12,8 @@ namespace flutter { AndroidSurfaceGL::AndroidSurfaceGL( - std::shared_ptr android_context) + std::shared_ptr android_context, + std::unique_ptr jni_facade) : native_window_(nullptr), onscreen_surface_(nullptr), offscreen_surface_(nullptr) { @@ -23,7 +24,8 @@ AndroidSurfaceGL::AndroidSurfaceGL( if (offscreen_surface_ == EGL_NO_SURFACE) { offscreen_surface_ = nullptr; } - external_view_embedder_ = std::make_unique(); + external_view_embedder_ = + std::make_unique(std::move(jni_facade)); } AndroidSurfaceGL::~AndroidSurfaceGL() { diff --git a/shell/platform/android/android_surface_gl.h b/shell/platform/android/android_surface_gl.h index 58188630fb178..d610bdad5df8c 100644 --- a/shell/platform/android/android_surface_gl.h +++ b/shell/platform/android/android_surface_gl.h @@ -14,13 +14,15 @@ #include "flutter/shell/platform/android/android_environment_gl.h" #include "flutter/shell/platform/android/android_surface.h" #include "flutter/shell/platform/android/external_view_embedder/external_view_embedder.h" +#include "flutter/shell/platform/android/platform_view_android_jni.h" namespace flutter { class AndroidSurfaceGL final : public GPUSurfaceGLDelegate, public AndroidSurface { public: - AndroidSurfaceGL(std::shared_ptr android_context); + AndroidSurfaceGL(std::shared_ptr android_context, + std::unique_ptr jni_facade); ~AndroidSurfaceGL() override; diff --git a/shell/platform/android/android_surface_software.cc b/shell/platform/android/android_surface_software.cc index 562bd0937f10b..10d8c4a58b195 100644 --- a/shell/platform/android/android_surface_software.cc +++ b/shell/platform/android/android_surface_software.cc @@ -36,10 +36,12 @@ bool GetSkColorType(int32_t buffer_format, } // anonymous namespace -AndroidSurfaceSoftware::AndroidSurfaceSoftware() { +AndroidSurfaceSoftware::AndroidSurfaceSoftware( + std::unique_ptr jni_facade) { GetSkColorType(WINDOW_FORMAT_RGBA_8888, &target_color_type_, &target_alpha_type_); - external_view_embedder_ = std::make_unique(); + external_view_embedder_ = + std::make_unique(std::move(jni_facade)); } AndroidSurfaceSoftware::~AndroidSurfaceSoftware() = default; diff --git a/shell/platform/android/android_surface_software.h b/shell/platform/android/android_surface_software.h index dfe3de9fb6738..af0611f4b5198 100644 --- a/shell/platform/android/android_surface_software.h +++ b/shell/platform/android/android_surface_software.h @@ -11,13 +11,14 @@ #include "flutter/shell/gpu/gpu_surface_software.h" #include "flutter/shell/platform/android/android_surface.h" #include "flutter/shell/platform/android/external_view_embedder/external_view_embedder.h" +#include "flutter/shell/platform/android/platform_view_android_jni.h" namespace flutter { class AndroidSurfaceSoftware final : public AndroidSurface, public GPUSurfaceSoftwareDelegate { public: - AndroidSurfaceSoftware(); + AndroidSurfaceSoftware(std::unique_ptr jni_facade); ~AndroidSurfaceSoftware() override; diff --git a/shell/platform/android/android_surface_vulkan.cc b/shell/platform/android/android_surface_vulkan.cc index 1ed814ff539a4..85b93fa14fb5b 100644 --- a/shell/platform/android/android_surface_vulkan.cc +++ b/shell/platform/android/android_surface_vulkan.cc @@ -12,9 +12,11 @@ namespace flutter { -AndroidSurfaceVulkan::AndroidSurfaceVulkan() +AndroidSurfaceVulkan::AndroidSurfaceVulkan( + std::unique_ptr jni_facade) : proc_table_(fml::MakeRefCounted()) { - external_view_embedder_ = std::make_unique(); + external_view_embedder_ = + std::make_unique(std::move(jni_facade)); } AndroidSurfaceVulkan::~AndroidSurfaceVulkan() = default; diff --git a/shell/platform/android/android_surface_vulkan.h b/shell/platform/android/android_surface_vulkan.h index d73addf5bdf8e..30ac7f1201dd4 100644 --- a/shell/platform/android/android_surface_vulkan.h +++ b/shell/platform/android/android_surface_vulkan.h @@ -12,6 +12,7 @@ #include "flutter/shell/platform/android/android_native_window.h" #include "flutter/shell/platform/android/android_surface.h" #include "flutter/shell/platform/android/external_view_embedder/external_view_embedder.h" +#include "flutter/shell/platform/android/platform_view_android_jni.h" #include "flutter/vulkan/vulkan_window.h" namespace flutter { diff --git a/shell/platform/android/external_view_embedder/BUILD.gn b/shell/platform/android/external_view_embedder/BUILD.gn index a3ff4df4ec47e..8afbda9dd3277 100644 --- a/shell/platform/android/external_view_embedder/BUILD.gn +++ b/shell/platform/android/external_view_embedder/BUILD.gn @@ -20,6 +20,7 @@ source_set("external_view_embedder") { deps = [ "//flutter/common", + "//flutter/shell/platform/android:android_jni", "//flutter/flow", "//flutter/fml", "//third_party/skia", diff --git a/shell/platform/android/external_view_embedder/external_view_embedder.cc b/shell/platform/android/external_view_embedder/external_view_embedder.cc index 8a057cbab62a0..d071e92794a99 100644 --- a/shell/platform/android/external_view_embedder/external_view_embedder.cc +++ b/shell/platform/android/external_view_embedder/external_view_embedder.cc @@ -8,6 +8,10 @@ namespace flutter { +AndroidExternalViewEmbedder::AndroidExternalViewEmbedder( + std::unique_ptr jni_facade) + : ExternalViewEmbedder(), jni_facade_(std::move(jni_facade)) {} + // |ExternalViewEmbedder| void AndroidExternalViewEmbedder::PrerollCompositeEmbeddedView( int view_id, diff --git a/shell/platform/android/external_view_embedder/external_view_embedder.h b/shell/platform/android/external_view_embedder/external_view_embedder.h index b7a0954b755b3..ba37ba2f0cbfe 100644 --- a/shell/platform/android/external_view_embedder/external_view_embedder.h +++ b/shell/platform/android/external_view_embedder/external_view_embedder.h @@ -7,12 +7,16 @@ #include "flutter/flow/embedded_views.h" +#include "flutter/shell/platform/android/platform_view_android_jni.h" #include "third_party/skia/include/core/SkPictureRecorder.h" namespace flutter { -class AndroidExternalViewEmbedder : public ExternalViewEmbedder { +class AndroidExternalViewEmbedder final : public ExternalViewEmbedder { public: + AndroidExternalViewEmbedder( + std::unique_ptr jni_facade); + // |ExternalViewEmbedder| void PrerollCompositeEmbeddedView( int view_id, @@ -48,6 +52,9 @@ class AndroidExternalViewEmbedder : public ExternalViewEmbedder { fml::RefPtr raster_thread_merger) override; private: + // Allows to call methods in Java. + std::unique_ptr jni_facade_; + // The size of the background canvas. SkISize frame_size_; diff --git a/shell/platform/android/external_view_embedder/external_view_embedder_unittests.cc b/shell/platform/android/external_view_embedder/external_view_embedder_unittests.cc index 856c6e0e4893b..e3f7cbef16230 100644 --- a/shell/platform/android/external_view_embedder/external_view_embedder_unittests.cc +++ b/shell/platform/android/external_view_embedder/external_view_embedder_unittests.cc @@ -10,7 +10,7 @@ namespace flutter { namespace testing { TEST(AndroidExternalViewEmbedder, GetCurrentCanvases) { - auto embedder = new AndroidExternalViewEmbedder(); + auto embedder = new AndroidExternalViewEmbedder(nullptr); embedder->BeginFrame(SkISize::Make(10, 20), nullptr, 1.0); @@ -26,7 +26,7 @@ TEST(AndroidExternalViewEmbedder, GetCurrentCanvases) { } TEST(AndroidExternalViewEmbedder, CompositeEmbeddedView) { - auto embedder = new AndroidExternalViewEmbedder(); + auto embedder = new AndroidExternalViewEmbedder(nullptr); embedder->PrerollCompositeEmbeddedView( 0, std::make_unique()); @@ -38,7 +38,7 @@ TEST(AndroidExternalViewEmbedder, CompositeEmbeddedView) { } TEST(AndroidExternalViewEmbedder, CancelFrame) { - auto embedder = new AndroidExternalViewEmbedder(); + auto embedder = new AndroidExternalViewEmbedder(nullptr); embedder->PrerollCompositeEmbeddedView( 0, std::make_unique()); diff --git a/shell/platform/android/platform_view_android.cc b/shell/platform/android/platform_view_android.cc index e4383a986d25c..b8b8f701e5235 100644 --- a/shell/platform/android/platform_view_android.cc +++ b/shell/platform/android/platform_view_android.cc @@ -36,7 +36,8 @@ PlatformViewAndroid::PlatformViewAndroid( android_context = AndroidContext::Create(AndroidRenderingAPI::kOpenGLES); #endif // SHELL_ENABLE_VULKAN } - android_surface_ = AndroidSurface::Create(android_context); + android_surface_ = + AndroidSurface::Create(android_context, std::move(jni_facade)); FML_CHECK(android_surface_) << "Could not create an OpenGL, Vulkan or Software surface to setup " "rendering."; From 8ae4953755603c2092029e5f3c000251326b85d9 Mon Sep 17 00:00:00 2001 From: Emmanuel Garcia Date: Mon, 8 Jun 2020 15:36:27 -0700 Subject: [PATCH 07/16] Move interface to shell/platform/android/jni/ --- ci/licenses_golden/licenses_flutter | 4 ++-- shell/platform/android/BUILD.gn | 17 +--------------- shell/platform/android/android_shell_holder.h | 2 +- shell/platform/android/android_surface.h | 2 +- shell/platform/android/android_surface_gl.h | 2 +- .../android/android_surface_software.cc | 2 +- .../android/android_surface_software.h | 2 +- .../platform/android/android_surface_vulkan.h | 2 +- .../android/external_view_embedder/BUILD.gn | 2 +- .../external_view_embedder.h | 2 +- shell/platform/android/jni/BUILD.gn | 20 +++++++++++++++++++ .../{ => jni}/platform_view_android_jni.cc | 2 +- .../{ => jni}/platform_view_android_jni.h | 0 .../platform_message_response_android.cc | 20 +++++++++---------- .../platform_message_response_android.h | 2 +- .../platform/android/platform_view_android.cc | 2 +- .../platform/android/platform_view_android.h | 2 +- .../android/platform_view_android_jni_impl.h | 2 +- 18 files changed, 46 insertions(+), 41 deletions(-) create mode 100644 shell/platform/android/jni/BUILD.gn rename shell/platform/android/{ => jni}/platform_view_android_jni.cc (78%) rename shell/platform/android/{ => jni}/platform_view_android_jni.h (100%) diff --git a/ci/licenses_golden/licenses_flutter b/ci/licenses_golden/licenses_flutter index 737848571736c..f4aef530a5434 100755 --- a/ci/licenses_golden/licenses_flutter +++ b/ci/licenses_golden/licenses_flutter @@ -783,13 +783,13 @@ FILE: ../../../flutter/shell/platform/android/io/flutter/view/FlutterRunArgument FILE: ../../../flutter/shell/platform/android/io/flutter/view/FlutterView.java FILE: ../../../flutter/shell/platform/android/io/flutter/view/TextureRegistry.java FILE: ../../../flutter/shell/platform/android/io/flutter/view/VsyncWaiter.java +FILE: ../../../flutter/shell/platform/android/jni/platform_view_android_jni.cc +FILE: ../../../flutter/shell/platform/android/jni/platform_view_android_jni.h FILE: ../../../flutter/shell/platform/android/library_loader.cc FILE: ../../../flutter/shell/platform/android/platform_message_response_android.cc FILE: ../../../flutter/shell/platform/android/platform_message_response_android.h FILE: ../../../flutter/shell/platform/android/platform_view_android.cc FILE: ../../../flutter/shell/platform/android/platform_view_android.h -FILE: ../../../flutter/shell/platform/android/platform_view_android_jni.cc -FILE: ../../../flutter/shell/platform/android/platform_view_android_jni.h FILE: ../../../flutter/shell/platform/android/platform_view_android_jni_impl.cc FILE: ../../../flutter/shell/platform/android/platform_view_android_jni_impl.h FILE: ../../../flutter/shell/platform/android/robolectric.properties diff --git a/shell/platform/android/BUILD.gn b/shell/platform/android/BUILD.gn index 627ccf645ec61..00122513236ab 100644 --- a/shell/platform/android/BUILD.gn +++ b/shell/platform/android/BUILD.gn @@ -58,7 +58,6 @@ shared_library("flutter_shell_native") { deps = [ ":android_gpu_configuration", - ":android_jni", ":icudtl_object", "//flutter/assets", "//flutter/common", @@ -69,6 +68,7 @@ shared_library("flutter_shell_native") { "//flutter/runtime:libdart", "//flutter/shell/common", "//flutter/shell/platform/android/external_view_embedder", + "//flutter/shell/platform/android/jni", "//third_party/skia", ] @@ -96,21 +96,6 @@ shared_library("flutter_shell_native") { ldflags = [ "-Wl,--version-script=" + rebase_path("android_exports.lst") ] } -source_set("android_jni") { - sources = [ - "platform_view_android_jni.cc", - "platform_view_android_jni.h", - ] - - public_configs = [ "//flutter:config" ] - - deps = [ - "//flutter/fml", - "//flutter/lib/ui", - "//third_party/skia", - ] -} - action("gen_android_build_config_java") { script = "//flutter/tools/gen_android_buildconfig.py" diff --git a/shell/platform/android/android_shell_holder.h b/shell/platform/android/android_shell_holder.h index 7bd8bcbc0ebaf..4b2b74d654d16 100644 --- a/shell/platform/android/android_shell_holder.h +++ b/shell/platform/android/android_shell_holder.h @@ -14,8 +14,8 @@ #include "flutter/shell/common/run_configuration.h" #include "flutter/shell/common/shell.h" #include "flutter/shell/common/thread_host.h" +#include "flutter/shell/platform/android/jni/platform_view_android_jni.h" #include "flutter/shell/platform/android/platform_view_android.h" -#include "flutter/shell/platform/android/platform_view_android_jni.h" namespace flutter { diff --git a/shell/platform/android/android_surface.h b/shell/platform/android/android_surface.h index dd26f0a9ef4ea..7a2de3d82cf90 100644 --- a/shell/platform/android/android_surface.h +++ b/shell/platform/android/android_surface.h @@ -14,7 +14,7 @@ #include "flutter/shell/common/surface.h" #include "flutter/shell/platform/android/android_context.h" #include "flutter/shell/platform/android/android_native_window.h" -#include "flutter/shell/platform/android/platform_view_android_jni.h" +#include "flutter/shell/platform/android/jni/platform_view_android_jni.h" #include "third_party/skia/include/core/SkSize.h" namespace flutter { diff --git a/shell/platform/android/android_surface_gl.h b/shell/platform/android/android_surface_gl.h index d610bdad5df8c..35f8ea906684e 100644 --- a/shell/platform/android/android_surface_gl.h +++ b/shell/platform/android/android_surface_gl.h @@ -14,7 +14,7 @@ #include "flutter/shell/platform/android/android_environment_gl.h" #include "flutter/shell/platform/android/android_surface.h" #include "flutter/shell/platform/android/external_view_embedder/external_view_embedder.h" -#include "flutter/shell/platform/android/platform_view_android_jni.h" +#include "flutter/shell/platform/android/jni/platform_view_android_jni.h" namespace flutter { diff --git a/shell/platform/android/android_surface_software.cc b/shell/platform/android/android_surface_software.cc index 10d8c4a58b195..0c2c85e4786e1 100644 --- a/shell/platform/android/android_surface_software.cc +++ b/shell/platform/android/android_surface_software.cc @@ -11,7 +11,7 @@ #include "flutter/fml/platform/android/jni_weak_ref.h" #include "flutter/fml/platform/android/scoped_java_ref.h" #include "flutter/fml/trace_event.h" -#include "flutter/shell/platform/android/platform_view_android_jni.h" +#include "flutter/shell/platform/android/jni/platform_view_android_jni.h" namespace flutter { diff --git a/shell/platform/android/android_surface_software.h b/shell/platform/android/android_surface_software.h index af0611f4b5198..3d2c48cdb9e50 100644 --- a/shell/platform/android/android_surface_software.h +++ b/shell/platform/android/android_surface_software.h @@ -11,7 +11,7 @@ #include "flutter/shell/gpu/gpu_surface_software.h" #include "flutter/shell/platform/android/android_surface.h" #include "flutter/shell/platform/android/external_view_embedder/external_view_embedder.h" -#include "flutter/shell/platform/android/platform_view_android_jni.h" +#include "flutter/shell/platform/android/jni/platform_view_android_jni.h" namespace flutter { diff --git a/shell/platform/android/android_surface_vulkan.h b/shell/platform/android/android_surface_vulkan.h index 30ac7f1201dd4..f3ea5b2310a19 100644 --- a/shell/platform/android/android_surface_vulkan.h +++ b/shell/platform/android/android_surface_vulkan.h @@ -12,7 +12,7 @@ #include "flutter/shell/platform/android/android_native_window.h" #include "flutter/shell/platform/android/android_surface.h" #include "flutter/shell/platform/android/external_view_embedder/external_view_embedder.h" -#include "flutter/shell/platform/android/platform_view_android_jni.h" +#include "flutter/shell/platform/android/jni/platform_view_android_jni.h" #include "flutter/vulkan/vulkan_window.h" namespace flutter { diff --git a/shell/platform/android/external_view_embedder/BUILD.gn b/shell/platform/android/external_view_embedder/BUILD.gn index 8afbda9dd3277..6ef3a967088fc 100644 --- a/shell/platform/android/external_view_embedder/BUILD.gn +++ b/shell/platform/android/external_view_embedder/BUILD.gn @@ -20,7 +20,7 @@ source_set("external_view_embedder") { deps = [ "//flutter/common", - "//flutter/shell/platform/android:android_jni", + "//flutter/shell/platform/android/jni", "//flutter/flow", "//flutter/fml", "//third_party/skia", diff --git a/shell/platform/android/external_view_embedder/external_view_embedder.h b/shell/platform/android/external_view_embedder/external_view_embedder.h index ba37ba2f0cbfe..379087e457562 100644 --- a/shell/platform/android/external_view_embedder/external_view_embedder.h +++ b/shell/platform/android/external_view_embedder/external_view_embedder.h @@ -7,7 +7,7 @@ #include "flutter/flow/embedded_views.h" -#include "flutter/shell/platform/android/platform_view_android_jni.h" +#include "flutter/shell/platform/android/jni/platform_view_android_jni.h" #include "third_party/skia/include/core/SkPictureRecorder.h" namespace flutter { diff --git a/shell/platform/android/jni/BUILD.gn b/shell/platform/android/jni/BUILD.gn new file mode 100644 index 0000000000000..700dfb5c6676e --- /dev/null +++ b/shell/platform/android/jni/BUILD.gn @@ -0,0 +1,20 @@ +# Copyright 2013 The Flutter Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +import("//flutter/common/config.gni") + +source_set("jni") { + sources = [ + "platform_view_android_jni.cc", + "platform_view_android_jni.h", + ] + + public_configs = [ "//flutter:config" ] + + deps = [ + "//flutter/fml", + "//flutter/lib/ui", + "//third_party/skia", + ] +} diff --git a/shell/platform/android/platform_view_android_jni.cc b/shell/platform/android/jni/platform_view_android_jni.cc similarity index 78% rename from shell/platform/android/platform_view_android_jni.cc rename to shell/platform/android/jni/platform_view_android_jni.cc index d81a613b01c37..3d9db80cb896f 100644 --- a/shell/platform/android/platform_view_android_jni.cc +++ b/shell/platform/android/jni/platform_view_android_jni.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "flutter/shell/platform/android/platform_view_android_jni.h" +#include "flutter/shell/platform/android/jni/platform_view_android_jni.h" namespace flutter { diff --git a/shell/platform/android/platform_view_android_jni.h b/shell/platform/android/jni/platform_view_android_jni.h similarity index 100% rename from shell/platform/android/platform_view_android_jni.h rename to shell/platform/android/jni/platform_view_android_jni.h diff --git a/shell/platform/android/platform_message_response_android.cc b/shell/platform/android/platform_message_response_android.cc index 7c75fa2d15ff3..79e30aaca211f 100644 --- a/shell/platform/android/platform_message_response_android.cc +++ b/shell/platform/android/platform_message_response_android.cc @@ -5,7 +5,7 @@ #include "flutter/shell/platform/android/platform_message_response_android.h" #include "flutter/fml/make_copyable.h" -#include "flutter/shell/platform/android/platform_view_android_jni.h" +#include "flutter/shell/platform/android/jni/platform_view_android_jni.h" namespace flutter { @@ -22,15 +22,15 @@ PlatformMessageResponseAndroid::~PlatformMessageResponseAndroid() = default; // |flutter::PlatformMessageResponse| void PlatformMessageResponseAndroid::Complete( std::unique_ptr data) { - // platform_task_runner_->PostTask( - // fml::MakeCopyable([response_id = response_id_, // - // jni_facade = std::move(jni_facade_), // - // data = std::move(data) // - // ]() { - // // Make the response call into Java. - // jni_facade->FlutterViewHandlePlatformMessageResponse(response_id, - // std::move(data)); - // })); + platform_task_runner_->PostTask( + fml::MakeCopyable([response_id = response_id_, // + jni_facade = std::move(jni_facade_), // + data = std::move(data) // + ]() { + // Make the response call into Java. + jni_facade->FlutterViewHandlePlatformMessageResponse(response_id, + std::move(data)); + })); } // |flutter::PlatformMessageResponse| diff --git a/shell/platform/android/platform_message_response_android.h b/shell/platform/android/platform_message_response_android.h index c60a71b9fe255..65e4c13ca4dc3 100644 --- a/shell/platform/android/platform_message_response_android.h +++ b/shell/platform/android/platform_message_response_android.h @@ -9,7 +9,7 @@ #include "flutter/fml/platform/android/jni_weak_ref.h" #include "flutter/fml/task_runner.h" #include "flutter/lib/ui/window/platform_message_response.h" -#include "flutter/shell/platform/android/platform_view_android_jni.h" +#include "flutter/shell/platform/android/jni/platform_view_android_jni.h" namespace flutter { diff --git a/shell/platform/android/platform_view_android.cc b/shell/platform/android/platform_view_android.cc index b8b8f701e5235..3d260af807433 100644 --- a/shell/platform/android/platform_view_android.cc +++ b/shell/platform/android/platform_view_android.cc @@ -13,8 +13,8 @@ #include "flutter/shell/platform/android/android_context.h" #include "flutter/shell/platform/android/android_external_texture_gl.h" #include "flutter/shell/platform/android/android_surface_gl.h" +#include "flutter/shell/platform/android/jni/platform_view_android_jni.h" #include "flutter/shell/platform/android/platform_message_response_android.h" -#include "flutter/shell/platform/android/platform_view_android_jni.h" #include "flutter/shell/platform/android/vsync_waiter_android.h" namespace flutter { diff --git a/shell/platform/android/platform_view_android.h b/shell/platform/android/platform_view_android.h index 2be1cc4f9efae..5432969a3d103 100644 --- a/shell/platform/android/platform_view_android.h +++ b/shell/platform/android/platform_view_android.h @@ -17,7 +17,7 @@ #include "flutter/shell/common/platform_view.h" #include "flutter/shell/platform/android/android_native_window.h" #include "flutter/shell/platform/android/android_surface.h" -#include "flutter/shell/platform/android/platform_view_android_jni.h" +#include "flutter/shell/platform/android/jni/platform_view_android_jni.h" namespace flutter { diff --git a/shell/platform/android/platform_view_android_jni_impl.h b/shell/platform/android/platform_view_android_jni_impl.h index 6ad3c05f23b7e..1e6119044bfe0 100644 --- a/shell/platform/android/platform_view_android_jni_impl.h +++ b/shell/platform/android/platform_view_android_jni_impl.h @@ -6,7 +6,7 @@ #define FLUTTER_SHELL_PLATFORM_ANDROID_PLATFORM_VIEW_ANDROID_JNI_IMPL_H_ #include "flutter/fml/platform/android/jni_weak_ref.h" -#include "flutter/shell/platform/android/platform_view_android_jni.h" +#include "flutter/shell/platform/android/jni/platform_view_android_jni.h" namespace flutter { From efb8f46f15426308342b7fefb9b6d59545f84cbd Mon Sep 17 00:00:00 2001 From: Emmanuel Garcia Date: Mon, 8 Jun 2020 16:53:27 -0700 Subject: [PATCH 08/16] PlatformViewAndroidJni -> PlatformViewAndroidJNI --- .../android/android_external_texture_gl.cc | 6 +- .../android/android_external_texture_gl.h | 4 +- .../platform/android/android_shell_holder.cc | 2 +- shell/platform/android/android_shell_holder.h | 4 +- shell/platform/android/android_surface.cc | 2 +- shell/platform/android/android_surface.h | 2 +- shell/platform/android/android_surface_gl.cc | 2 +- shell/platform/android/android_surface_gl.h | 2 +- .../android/android_surface_software.cc | 2 +- .../android/android_surface_software.h | 2 +- .../android/android_surface_vulkan.cc | 2 +- .../external_view_embedder.cc | 2 +- .../external_view_embedder.h | 4 +- .../android/jni/platform_view_android_jni.cc | 2 +- .../android/jni/platform_view_android_jni.h | 58 ++++++++++++++++++- .../platform_message_response_android.cc | 15 ++--- .../platform_message_response_android.h | 4 +- .../platform/android/platform_view_android.cc | 4 +- .../platform/android/platform_view_android.h | 6 +- .../android/platform_view_android_jni_impl.cc | 32 +++++----- .../android/platform_view_android_jni_impl.h | 14 +++-- 21 files changed, 113 insertions(+), 58 deletions(-) diff --git a/shell/platform/android/android_external_texture_gl.cc b/shell/platform/android/android_external_texture_gl.cc index 42b9b15d5c40e..e632e3645b39f 100644 --- a/shell/platform/android/android_external_texture_gl.cc +++ b/shell/platform/android/android_external_texture_gl.cc @@ -13,10 +13,10 @@ namespace flutter { AndroidExternalTextureGL::AndroidExternalTextureGL( int64_t id, const fml::jni::JavaObjectWeakGlobalRef& surface_texture, - std::unique_ptr jni_facade) + std::unique_ptr jni_facade) : Texture(id), surface_texture_(surface_texture), transform(SkMatrix::I()) { - jni_facade_ = std::unique_ptr( - static_cast(jni_facade.release())); + jni_facade_ = std::unique_ptr( + static_cast(jni_facade.release())); } AndroidExternalTextureGL::~AndroidExternalTextureGL() { diff --git a/shell/platform/android/android_external_texture_gl.h b/shell/platform/android/android_external_texture_gl.h index 6c0845cf10e5b..9d9733eb20f0f 100644 --- a/shell/platform/android/android_external_texture_gl.h +++ b/shell/platform/android/android_external_texture_gl.h @@ -17,7 +17,7 @@ class AndroidExternalTextureGL : public flutter::Texture { AndroidExternalTextureGL( int64_t id, const fml::jni::JavaObjectWeakGlobalRef& surface_texture, - std::unique_ptr jni_facade); + std::unique_ptr jni_facade); ~AndroidExternalTextureGL() override; @@ -45,7 +45,7 @@ class AndroidExternalTextureGL : public flutter::Texture { enum class AttachmentState { uninitialized, attached, detached }; - std::unique_ptr jni_facade_; + std::unique_ptr jni_facade_; fml::jni::JavaObjectWeakGlobalRef surface_texture_; diff --git a/shell/platform/android/android_shell_holder.cc b/shell/platform/android/android_shell_holder.cc index 2af12e3801e49..134be4c74f2ae 100644 --- a/shell/platform/android/android_shell_holder.cc +++ b/shell/platform/android/android_shell_holder.cc @@ -29,7 +29,7 @@ static WindowData GetDefaultWindowData() { AndroidShellHolder::AndroidShellHolder( flutter::Settings settings, - std::unique_ptr jni_facade, + std::unique_ptr jni_facade, bool is_background_view) : settings_(std::move(settings)), jni_facade_(std::move(jni_facade)) { static size_t shell_count = 1; diff --git a/shell/platform/android/android_shell_holder.h b/shell/platform/android/android_shell_holder.h index 4b2b74d654d16..437dfcdcfe29b 100644 --- a/shell/platform/android/android_shell_holder.h +++ b/shell/platform/android/android_shell_holder.h @@ -22,7 +22,7 @@ namespace flutter { class AndroidShellHolder { public: AndroidShellHolder(flutter::Settings settings, - std::unique_ptr jni_facade, + std::unique_ptr jni_facade, bool is_background_view); ~AndroidShellHolder(); @@ -42,7 +42,7 @@ class AndroidShellHolder { private: const flutter::Settings settings_; - const std::unique_ptr jni_facade_; + const std::unique_ptr jni_facade_; fml::WeakPtr platform_view_; ThreadHost thread_host_; std::unique_ptr shell_; diff --git a/shell/platform/android/android_surface.cc b/shell/platform/android/android_surface.cc index 0738b354e3923..920ecb6e43017 100644 --- a/shell/platform/android/android_surface.cc +++ b/shell/platform/android/android_surface.cc @@ -16,7 +16,7 @@ namespace flutter { std::unique_ptr AndroidSurface::Create( std::shared_ptr android_context, - std::unique_ptr jni_facade) { + std::unique_ptr jni_facade) { std::unique_ptr surface; switch (android_context->RenderingApi()) { case AndroidRenderingAPI::kSoftware: diff --git a/shell/platform/android/android_surface.h b/shell/platform/android/android_surface.h index 7a2de3d82cf90..8ce6db0594b0f 100644 --- a/shell/platform/android/android_surface.h +++ b/shell/platform/android/android_surface.h @@ -23,7 +23,7 @@ class AndroidSurface { public: static std::unique_ptr Create( std::shared_ptr android_context, - std::unique_ptr jni_facade); + std::unique_ptr jni_facade); virtual ~AndroidSurface(); diff --git a/shell/platform/android/android_surface_gl.cc b/shell/platform/android/android_surface_gl.cc index e7ad8cb6de026..780ca8c102db8 100644 --- a/shell/platform/android/android_surface_gl.cc +++ b/shell/platform/android/android_surface_gl.cc @@ -13,7 +13,7 @@ namespace flutter { AndroidSurfaceGL::AndroidSurfaceGL( std::shared_ptr android_context, - std::unique_ptr jni_facade) + std::unique_ptr jni_facade) : native_window_(nullptr), onscreen_surface_(nullptr), offscreen_surface_(nullptr) { diff --git a/shell/platform/android/android_surface_gl.h b/shell/platform/android/android_surface_gl.h index 35f8ea906684e..8679270a641eb 100644 --- a/shell/platform/android/android_surface_gl.h +++ b/shell/platform/android/android_surface_gl.h @@ -22,7 +22,7 @@ class AndroidSurfaceGL final : public GPUSurfaceGLDelegate, public AndroidSurface { public: AndroidSurfaceGL(std::shared_ptr android_context, - std::unique_ptr jni_facade); + std::unique_ptr jni_facade); ~AndroidSurfaceGL() override; diff --git a/shell/platform/android/android_surface_software.cc b/shell/platform/android/android_surface_software.cc index 0c2c85e4786e1..c59db100f2f4e 100644 --- a/shell/platform/android/android_surface_software.cc +++ b/shell/platform/android/android_surface_software.cc @@ -37,7 +37,7 @@ bool GetSkColorType(int32_t buffer_format, } // anonymous namespace AndroidSurfaceSoftware::AndroidSurfaceSoftware( - std::unique_ptr jni_facade) { + std::unique_ptr jni_facade) { GetSkColorType(WINDOW_FORMAT_RGBA_8888, &target_color_type_, &target_alpha_type_); external_view_embedder_ = diff --git a/shell/platform/android/android_surface_software.h b/shell/platform/android/android_surface_software.h index 3d2c48cdb9e50..d698010178d29 100644 --- a/shell/platform/android/android_surface_software.h +++ b/shell/platform/android/android_surface_software.h @@ -18,7 +18,7 @@ namespace flutter { class AndroidSurfaceSoftware final : public AndroidSurface, public GPUSurfaceSoftwareDelegate { public: - AndroidSurfaceSoftware(std::unique_ptr jni_facade); + AndroidSurfaceSoftware(std::unique_ptr jni_facade); ~AndroidSurfaceSoftware() override; diff --git a/shell/platform/android/android_surface_vulkan.cc b/shell/platform/android/android_surface_vulkan.cc index 85b93fa14fb5b..c7f9ade534004 100644 --- a/shell/platform/android/android_surface_vulkan.cc +++ b/shell/platform/android/android_surface_vulkan.cc @@ -13,7 +13,7 @@ namespace flutter { AndroidSurfaceVulkan::AndroidSurfaceVulkan( - std::unique_ptr jni_facade) + std::unique_ptr jni_facade) : proc_table_(fml::MakeRefCounted()) { external_view_embedder_ = std::make_unique(std::move(jni_facade)); diff --git a/shell/platform/android/external_view_embedder/external_view_embedder.cc b/shell/platform/android/external_view_embedder/external_view_embedder.cc index d071e92794a99..e326a85117ab5 100644 --- a/shell/platform/android/external_view_embedder/external_view_embedder.cc +++ b/shell/platform/android/external_view_embedder/external_view_embedder.cc @@ -9,7 +9,7 @@ namespace flutter { AndroidExternalViewEmbedder::AndroidExternalViewEmbedder( - std::unique_ptr jni_facade) + std::unique_ptr jni_facade) : ExternalViewEmbedder(), jni_facade_(std::move(jni_facade)) {} // |ExternalViewEmbedder| diff --git a/shell/platform/android/external_view_embedder/external_view_embedder.h b/shell/platform/android/external_view_embedder/external_view_embedder.h index 379087e457562..29d2bd38786f9 100644 --- a/shell/platform/android/external_view_embedder/external_view_embedder.h +++ b/shell/platform/android/external_view_embedder/external_view_embedder.h @@ -15,7 +15,7 @@ namespace flutter { class AndroidExternalViewEmbedder final : public ExternalViewEmbedder { public: AndroidExternalViewEmbedder( - std::unique_ptr jni_facade); + std::unique_ptr jni_facade); // |ExternalViewEmbedder| void PrerollCompositeEmbeddedView( @@ -53,7 +53,7 @@ class AndroidExternalViewEmbedder final : public ExternalViewEmbedder { private: // Allows to call methods in Java. - std::unique_ptr jni_facade_; + std::unique_ptr jni_facade_; // The size of the background canvas. SkISize frame_size_; diff --git a/shell/platform/android/jni/platform_view_android_jni.cc b/shell/platform/android/jni/platform_view_android_jni.cc index 3d9db80cb896f..b00a39be3bae0 100644 --- a/shell/platform/android/jni/platform_view_android_jni.cc +++ b/shell/platform/android/jni/platform_view_android_jni.cc @@ -6,6 +6,6 @@ namespace flutter { -PlatformViewAndroidJni::~PlatformViewAndroidJni() = default; +PlatformViewAndroidJNI::~PlatformViewAndroidJNI() = default; } // namespace flutter diff --git a/shell/platform/android/jni/platform_view_android_jni.h b/shell/platform/android/jni/platform_view_android_jni.h index e3e310c44e284..2d720c29a2378 100644 --- a/shell/platform/android/jni/platform_view_android_jni.h +++ b/shell/platform/android/jni/platform_view_android_jni.h @@ -13,37 +13,91 @@ namespace flutter { -class PlatformViewAndroidJni { +//------------------------------------------------------------------------------ +/// Allows to call Java code running in the JVM from any thread. However, most +/// methods can only be called from the platform thread as that is where the +/// Java code runs. +/// +/// This interface must not depend on the Android toolchain directly, so it can +/// be used in unit tests compiled with the host toolchain. +/// +class PlatformViewAndroidJNI { public: - virtual ~PlatformViewAndroidJni(); + virtual ~PlatformViewAndroidJNI(); + //---------------------------------------------------------------------------- + /// @brief Sends a platform message. The message may be empty. + /// virtual void FlutterViewHandlePlatformMessage( fml::RefPtr message, int responseId) = 0; + //---------------------------------------------------------------------------- + /// @brief Responds to a platform message. The data may be a `nullptr`. + /// virtual void FlutterViewHandlePlatformMessageResponse( int responseId, std::unique_ptr data) = 0; + //---------------------------------------------------------------------------- + /// @brief Sends semantics tree updates. + /// + /// @note Must be called from the platform thread. + /// virtual void FlutterViewUpdateSemantics(std::vector buffer, std::vector strings) = 0; + //---------------------------------------------------------------------------- + /// @brief Sends new custom accessibility events. + /// + /// @note Must be called from the platform thread. + /// virtual void FlutterViewUpdateCustomAccessibilityActions( std::vector actions_buffer, std::vector strings) = 0; + //---------------------------------------------------------------------------- + /// @brief Indicates that FlutterView should start painting pixels. + /// + /// @note Must be called from the platform thread. + /// virtual void FlutterViewOnFirstFrame() = 0; + //---------------------------------------------------------------------------- + /// @brief Indicates that a hot restart is about to happen. + /// virtual void FlutterViewOnPreEngineRestart() = 0; + //---------------------------------------------------------------------------- + /// @brief Attach the SurfaceTexture to the OpenGL ES context that is + /// current on the calling thread. + /// virtual void SurfaceTextureAttachToGLContext(int textureId) = 0; + //---------------------------------------------------------------------------- + /// @brief Updates the texture image to the most recent frame from the + /// image stream. + /// virtual void SurfaceTextureUpdateTexImage() = 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(SkMatrix& transform) = 0; + //---------------------------------------------------------------------------- + /// @brief Detaches a SurfaceTexture from the OpenGL ES context. + /// virtual void SurfaceTextureDetachFromGLContext() = 0; + //---------------------------------------------------------------------------- + /// @brief Positions and sizes a platform view if using hybrid + /// composition. + /// + /// @note Must be called from the platform thread. + /// virtual void FlutterViewOnDisplayPlatformView(int view_id, int x, int y, diff --git a/shell/platform/android/platform_message_response_android.cc b/shell/platform/android/platform_message_response_android.cc index 79e30aaca211f..b44303cd36da1 100644 --- a/shell/platform/android/platform_message_response_android.cc +++ b/shell/platform/android/platform_message_response_android.cc @@ -11,7 +11,7 @@ namespace flutter { PlatformMessageResponseAndroid::PlatformMessageResponseAndroid( int response_id, - std::unique_ptr jni_facade, + std::unique_ptr jni_facade, fml::RefPtr platform_task_runner) : response_id_(response_id), jni_facade_(std::move(jni_facade)), @@ -23,11 +23,9 @@ PlatformMessageResponseAndroid::~PlatformMessageResponseAndroid() = default; void PlatformMessageResponseAndroid::Complete( std::unique_ptr data) { platform_task_runner_->PostTask( - fml::MakeCopyable([response_id = response_id_, // - jni_facade = std::move(jni_facade_), // - data = std::move(data) // - ]() { - // Make the response call into Java. + fml::MakeCopyable([response_id = response_id_, // + data = std::move(data), // + jni_facade = std::move(jni_facade_)]() mutable { jni_facade->FlutterViewHandlePlatformMessageResponse(response_id, std::move(data)); })); @@ -36,9 +34,8 @@ void PlatformMessageResponseAndroid::Complete( // |flutter::PlatformMessageResponse| void PlatformMessageResponseAndroid::CompleteEmpty() { platform_task_runner_->PostTask( - fml::MakeCopyable([response_id = response_id_, // - jni_facade = std::move(jni_facade_), // - weak_java_object = weak_java_object_ // + fml::MakeCopyable([response_id = response_id_, // + jni_facade = std::move(jni_facade_) // ]() { // Make the response call into Java. jni_facade->FlutterViewHandlePlatformMessageResponse(response_id, diff --git a/shell/platform/android/platform_message_response_android.h b/shell/platform/android/platform_message_response_android.h index 65e4c13ca4dc3..4af2fb2b97c46 100644 --- a/shell/platform/android/platform_message_response_android.h +++ b/shell/platform/android/platform_message_response_android.h @@ -24,14 +24,14 @@ class PlatformMessageResponseAndroid : public flutter::PlatformMessageResponse { private: PlatformMessageResponseAndroid( int response_id, - std::unique_ptr jni_facade, + std::unique_ptr jni_facade, fml::RefPtr platform_task_runner); ~PlatformMessageResponseAndroid() override; int response_id_; fml::jni::JavaObjectWeakGlobalRef weak_java_object_; - std::unique_ptr jni_facade_; + std::unique_ptr jni_facade_; fml::RefPtr platform_task_runner_; FML_FRIEND_MAKE_REF_COUNTED(PlatformMessageResponseAndroid); diff --git a/shell/platform/android/platform_view_android.cc b/shell/platform/android/platform_view_android.cc index 3d260af807433..7dce84c1ecb69 100644 --- a/shell/platform/android/platform_view_android.cc +++ b/shell/platform/android/platform_view_android.cc @@ -22,7 +22,7 @@ namespace flutter { PlatformViewAndroid::PlatformViewAndroid( PlatformView::Delegate& delegate, flutter::TaskRunners task_runners, - std::unique_ptr jni_facade, + std::unique_ptr jni_facade, bool use_software_rendering) : PlatformView(delegate, std::move(task_runners)), jni_facade_(std::move(jni_facade)) { @@ -46,7 +46,7 @@ PlatformViewAndroid::PlatformViewAndroid( PlatformViewAndroid::PlatformViewAndroid( PlatformView::Delegate& delegate, flutter::TaskRunners task_runners, - std::unique_ptr jni_facade) + std::unique_ptr jni_facade) : PlatformView(delegate, std::move(task_runners)), jni_facade_(std::move(jni_facade)) {} diff --git a/shell/platform/android/platform_view_android.h b/shell/platform/android/platform_view_android.h index 5432969a3d103..28c407894cef2 100644 --- a/shell/platform/android/platform_view_android.h +++ b/shell/platform/android/platform_view_android.h @@ -29,12 +29,12 @@ class PlatformViewAndroid final : public PlatformView { // background execution. PlatformViewAndroid(PlatformView::Delegate& delegate, flutter::TaskRunners task_runners, - std::unique_ptr jni_facade); + std::unique_ptr jni_facade); // Creates a PlatformViewAndroid with a rendering surface. PlatformViewAndroid(PlatformView::Delegate& delegate, flutter::TaskRunners task_runners, - std::unique_ptr jni_facade, + std::unique_ptr jni_facade, bool use_software_rendering); ~PlatformViewAndroid() override; @@ -75,7 +75,7 @@ class PlatformViewAndroid final : public PlatformView { const fml::jni::JavaObjectWeakGlobalRef& surface_texture); private: - std::unique_ptr jni_facade_; + std::unique_ptr jni_facade_; std::unique_ptr android_surface_; // We use id 0 to mean that no response is expected. diff --git a/shell/platform/android/platform_view_android_jni_impl.cc b/shell/platform/android/platform_view_android_jni_impl.cc index 595cabc936bac..66d6caf1b26c4 100644 --- a/shell/platform/android/platform_view_android_jni_impl.cc +++ b/shell/platform/android/platform_view_android_jni_impl.cc @@ -96,8 +96,8 @@ static jlong AttachJNI(JNIEnv* env, jobject flutterJNI, jboolean is_background_view) { fml::jni::JavaObjectWeakGlobalRef java_object(env, flutterJNI); - std::unique_ptr jni_facade = - std::make_unique(java_object); + std::unique_ptr jni_facade = + std::make_unique(java_object); auto shell_holder = std::make_unique( FlutterMain::Get().GetSettings(), std::move(jni_facade), is_background_view); @@ -748,13 +748,13 @@ bool PlatformViewAndroid::Register(JNIEnv* env) { return RegisterApi(env); } -PlatformViewAndroidJniImpl::PlatformViewAndroidJniImpl( +PlatformViewAndroidJNIImpl::PlatformViewAndroidJNIImpl( fml::jni::JavaObjectWeakGlobalRef java_object) : java_object_(java_object) {} -PlatformViewAndroidJniImpl::~PlatformViewAndroidJniImpl() = default; +PlatformViewAndroidJNIImpl::~PlatformViewAndroidJNIImpl() = default; -void PlatformViewAndroidJniImpl::FlutterViewHandlePlatformMessage( +void PlatformViewAndroidJNIImpl::FlutterViewHandlePlatformMessage( fml::RefPtr message, int responseId) { JNIEnv* env = fml::jni::AttachCurrentThread(); @@ -783,7 +783,7 @@ void PlatformViewAndroidJniImpl::FlutterViewHandlePlatformMessage( FML_CHECK(CheckException(env)); } -void PlatformViewAndroidJniImpl::FlutterViewHandlePlatformMessageResponse( +void PlatformViewAndroidJNIImpl::FlutterViewHandlePlatformMessageResponse( int responseId, std::unique_ptr data) { // We are on the platform thread. Attempt to get the strong reference to @@ -815,7 +815,7 @@ void PlatformViewAndroidJniImpl::FlutterViewHandlePlatformMessageResponse( FML_CHECK(CheckException(env)); } -void PlatformViewAndroidJniImpl::FlutterViewUpdateSemantics( +void PlatformViewAndroidJNIImpl::FlutterViewUpdateSemantics( std::vector buffer, std::vector strings) { JNIEnv* env = fml::jni::AttachCurrentThread(); @@ -836,7 +836,7 @@ void PlatformViewAndroidJniImpl::FlutterViewUpdateSemantics( FML_CHECK(CheckException(env)); } -void PlatformViewAndroidJniImpl::FlutterViewUpdateCustomAccessibilityActions( +void PlatformViewAndroidJNIImpl::FlutterViewUpdateCustomAccessibilityActions( std::vector actions_buffer, std::vector strings) { JNIEnv* env = fml::jni::AttachCurrentThread(); @@ -860,7 +860,7 @@ void PlatformViewAndroidJniImpl::FlutterViewUpdateCustomAccessibilityActions( FML_CHECK(CheckException(env)); } -void PlatformViewAndroidJniImpl::FlutterViewOnFirstFrame() { +void PlatformViewAndroidJNIImpl::FlutterViewOnFirstFrame() { JNIEnv* env = fml::jni::AttachCurrentThread(); auto java_object = java_object_.get(env); @@ -873,7 +873,7 @@ void PlatformViewAndroidJniImpl::FlutterViewOnFirstFrame() { FML_CHECK(CheckException(env)); } -void PlatformViewAndroidJniImpl::FlutterViewOnPreEngineRestart() { +void PlatformViewAndroidJNIImpl::FlutterViewOnPreEngineRestart() { JNIEnv* env = fml::jni::AttachCurrentThread(); auto java_object = java_object_.get(env); @@ -886,12 +886,12 @@ void PlatformViewAndroidJniImpl::FlutterViewOnPreEngineRestart() { FML_CHECK(CheckException(env)); } -void PlatformViewAndroidJniImpl::SetCurrentSurfaceTexture( +void PlatformViewAndroidJNIImpl::SetCurrentSurfaceTexture( fml::jni::JavaObjectWeakGlobalRef& surface_texture) { surface_texture_ = surface_texture; } -void PlatformViewAndroidJniImpl::SurfaceTextureAttachToGLContext( +void PlatformViewAndroidJNIImpl::SurfaceTextureAttachToGLContext( int textureId) { JNIEnv* env = fml::jni::AttachCurrentThread(); @@ -907,7 +907,7 @@ void PlatformViewAndroidJniImpl::SurfaceTextureAttachToGLContext( FML_CHECK(CheckException(env)); } -void PlatformViewAndroidJniImpl::SurfaceTextureUpdateTexImage() { +void PlatformViewAndroidJNIImpl::SurfaceTextureUpdateTexImage() { JNIEnv* env = fml::jni::AttachCurrentThread(); fml::jni::ScopedJavaLocalRef surface_texture = @@ -933,7 +933,7 @@ SkSize ScaleToFill(float scaleX, float scaleY) { return SkSize::Make(scaleX * rescale, scaleY * rescale); } -void PlatformViewAndroidJniImpl::SurfaceTextureGetTransformMatrix( +void PlatformViewAndroidJNIImpl::SurfaceTextureGetTransformMatrix( SkMatrix& transform) { JNIEnv* env = fml::jni::AttachCurrentThread(); @@ -962,7 +962,7 @@ void PlatformViewAndroidJniImpl::SurfaceTextureGetTransformMatrix( transform.set9(matrix3); } -void PlatformViewAndroidJniImpl::SurfaceTextureDetachFromGLContext() { +void PlatformViewAndroidJNIImpl::SurfaceTextureDetachFromGLContext() { JNIEnv* env = fml::jni::AttachCurrentThread(); fml::jni::ScopedJavaLocalRef surface_texture = @@ -976,7 +976,7 @@ void PlatformViewAndroidJniImpl::SurfaceTextureDetachFromGLContext() { FML_CHECK(CheckException(env)); } -void PlatformViewAndroidJniImpl::FlutterViewOnDisplayPlatformView(int view_id, +void PlatformViewAndroidJNIImpl::FlutterViewOnDisplayPlatformView(int view_id, int x, int y, int width, diff --git a/shell/platform/android/platform_view_android_jni_impl.h b/shell/platform/android/platform_view_android_jni_impl.h index cf289e9530aea..d988ac1c2500e 100644 --- a/shell/platform/android/platform_view_android_jni_impl.h +++ b/shell/platform/android/platform_view_android_jni_impl.h @@ -10,11 +10,15 @@ namespace flutter { -class PlatformViewAndroidJniImpl final : public PlatformViewAndroidJni { +//------------------------------------------------------------------------------ +/// @brief Concrete implementation of `PlatformViewAndroidJNI` that is +/// compiled with the Android toolchain. +/// +class PlatformViewAndroidJNIImpl final : public PlatformViewAndroidJNI { public: - PlatformViewAndroidJniImpl(fml::jni::JavaObjectWeakGlobalRef java_object); + PlatformViewAndroidJNIImpl(fml::jni::JavaObjectWeakGlobalRef java_object); - ~PlatformViewAndroidJniImpl() override; + ~PlatformViewAndroidJNIImpl() override; void FlutterViewHandlePlatformMessage( fml::RefPtr message, @@ -53,13 +57,13 @@ class PlatformViewAndroidJniImpl final : public PlatformViewAndroidJni { int height) override; private: - // Reference to FlutterJNI. + // Reference to FlutterJNI object. const fml::jni::JavaObjectWeakGlobalRef java_object_; // Reference to the current Surface texture object. fml::jni::JavaObjectWeakGlobalRef surface_texture_; - FML_DISALLOW_COPY_AND_ASSIGN(PlatformViewAndroidJniImpl); + FML_DISALLOW_COPY_AND_ASSIGN(PlatformViewAndroidJNIImpl); }; } // namespace flutter From a5bc36d7a600070bf18cdbfc30a0b92b1f9fd3d3 Mon Sep 17 00:00:00 2001 From: Emmanuel Garcia Date: Mon, 8 Jun 2020 16:59:10 -0700 Subject: [PATCH 09/16] gn format --- shell/platform/android/external_view_embedder/BUILD.gn | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/platform/android/external_view_embedder/BUILD.gn b/shell/platform/android/external_view_embedder/BUILD.gn index 6ef3a967088fc..5145e00fa8972 100644 --- a/shell/platform/android/external_view_embedder/BUILD.gn +++ b/shell/platform/android/external_view_embedder/BUILD.gn @@ -20,9 +20,9 @@ source_set("external_view_embedder") { deps = [ "//flutter/common", - "//flutter/shell/platform/android/jni", "//flutter/flow", "//flutter/fml", + "//flutter/shell/platform/android/jni", "//third_party/skia", ] } From 24ec465b035de323deef4edb9156f8f9c461b3a9 Mon Sep 17 00:00:00 2001 From: Emmanuel Garcia Date: Mon, 8 Jun 2020 17:33:53 -0700 Subject: [PATCH 10/16] Typo --- shell/platform/android/android_surface_vulkan.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/platform/android/android_surface_vulkan.h b/shell/platform/android/android_surface_vulkan.h index f3ea5b2310a19..9a25315d4d0ee 100644 --- a/shell/platform/android/android_surface_vulkan.h +++ b/shell/platform/android/android_surface_vulkan.h @@ -20,7 +20,7 @@ namespace flutter { class AndroidSurfaceVulkan : public AndroidSurface, public GPUSurfaceVulkanDelegate { public: - AndroidSurfaceVulkan(); + AndroidSurfaceVulkan(std::unique_ptr jni_facade); ~AndroidSurfaceVulkan() override; From 19aa4e77534794db4ba6405d56ea5d08d7f810bd Mon Sep 17 00:00:00 2001 From: Emmanuel Garcia Date: Mon, 8 Jun 2020 22:41:23 -0700 Subject: [PATCH 11/16] Pass surface_texture to JNI methods --- .../android/android_external_texture_gl.cc | 20 +++---- .../android/android_external_texture_gl.h | 2 +- .../android/jni/platform_view_android_jni.h | 20 +++++-- .../android/platform_view_android_jni_impl.cc | 54 ++++++++++--------- .../android/platform_view_android_jni_impl.h | 13 +++-- 5 files changed, 60 insertions(+), 49 deletions(-) diff --git a/shell/platform/android/android_external_texture_gl.cc b/shell/platform/android/android_external_texture_gl.cc index e632e3645b39f..d28980f09b9f8 100644 --- a/shell/platform/android/android_external_texture_gl.cc +++ b/shell/platform/android/android_external_texture_gl.cc @@ -14,10 +14,10 @@ AndroidExternalTextureGL::AndroidExternalTextureGL( int64_t id, const fml::jni::JavaObjectWeakGlobalRef& surface_texture, std::unique_ptr jni_facade) - : Texture(id), surface_texture_(surface_texture), transform(SkMatrix::I()) { - jni_facade_ = std::unique_ptr( - static_cast(jni_facade.release())); -} + : Texture(id), + jni_facade_(std::move(jni_facade)), + surface_texture_(surface_texture), + transform(SkMatrix::I()) {} AndroidExternalTextureGL::~AndroidExternalTextureGL() { if (state_ == AttachmentState::attached) { @@ -72,8 +72,7 @@ void AndroidExternalTextureGL::Paint(SkCanvas& canvas, } void AndroidExternalTextureGL::UpdateTransform() { - jni_facade_->SetCurrentSurfaceTexture(surface_texture_); - jni_facade_->SurfaceTextureGetTransformMatrix(transform); + jni_facade_->SurfaceTextureGetTransformMatrix(surface_texture_, transform); } void AndroidExternalTextureGL::OnGrContextDestroyed() { @@ -84,19 +83,16 @@ void AndroidExternalTextureGL::OnGrContextDestroyed() { } void AndroidExternalTextureGL::Attach(jint textureName) { - jni_facade_->SetCurrentSurfaceTexture(surface_texture_); - jni_facade_->SurfaceTextureAttachToGLContext(textureName); + jni_facade_->SurfaceTextureAttachToGLContext(surface_texture_, textureName); } void AndroidExternalTextureGL::Update() { - jni_facade_->SetCurrentSurfaceTexture(surface_texture_); - jni_facade_->SurfaceTextureUpdateTexImage(); + jni_facade_->SurfaceTextureUpdateTexImage(surface_texture_); UpdateTransform(); } void AndroidExternalTextureGL::Detach() { - jni_facade_->SetCurrentSurfaceTexture(surface_texture_); - jni_facade_->SurfaceTextureDetachFromGLContext(); + jni_facade_->SurfaceTextureDetachFromGLContext(surface_texture_); } void AndroidExternalTextureGL::OnTextureUnregistered() {} diff --git a/shell/platform/android/android_external_texture_gl.h b/shell/platform/android/android_external_texture_gl.h index 9d9733eb20f0f..5507cb20f1c72 100644 --- a/shell/platform/android/android_external_texture_gl.h +++ b/shell/platform/android/android_external_texture_gl.h @@ -45,7 +45,7 @@ class AndroidExternalTextureGL : public flutter::Texture { enum class AttachmentState { uninitialized, attached, detached }; - std::unique_ptr jni_facade_; + std::unique_ptr jni_facade_; fml::jni::JavaObjectWeakGlobalRef surface_texture_; diff --git a/shell/platform/android/jni/platform_view_android_jni.h b/shell/platform/android/jni/platform_view_android_jni.h index 2d720c29a2378..25e235964e291 100644 --- a/shell/platform/android/jni/platform_view_android_jni.h +++ b/shell/platform/android/jni/platform_view_android_jni.h @@ -5,6 +5,8 @@ #ifndef FLUTTER_SHELL_PLATFORM_ANDROID_PLATFORM_VIEW_ANDROID_JNI_H_ #define FLUTTER_SHELL_PLATFORM_ANDROID_PLATFORM_VIEW_ANDROID_JNI_H_ +#include + #include "flutter/fml/macros.h" #include "flutter/fml/mapping.h" #include "flutter/lib/ui/window/platform_message.h" @@ -72,25 +74,35 @@ class PlatformViewAndroidJNI { /// @brief Attach the SurfaceTexture to the OpenGL ES context that is /// current on the calling thread. /// - virtual void SurfaceTextureAttachToGLContext(int textureId) = 0; + /// @note `surface_texture` must be `fml::jni::JavaObjectWeakGlobalRef` + /// + virtual void SurfaceTextureAttachToGLContext(std::any surface_texture, + int textureId) = 0; //---------------------------------------------------------------------------- /// @brief Updates the texture image to the most recent frame from the /// image stream. /// - virtual void SurfaceTextureUpdateTexImage() = 0; + /// @note `surface_texture` must be `fml::jni::JavaObjectWeakGlobalRef` + /// + virtual void SurfaceTextureUpdateTexImage(std::any 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(SkMatrix& transform) = 0; + /// @note `surface_texture` must be `fml::jni::JavaObjectWeakGlobalRef` + /// + virtual void SurfaceTextureGetTransformMatrix(std::any surface_texture, + SkMatrix& transform) = 0; //---------------------------------------------------------------------------- /// @brief Detaches a SurfaceTexture from the OpenGL ES context. /// - virtual void SurfaceTextureDetachFromGLContext() = 0; + /// @note `surface_texture` must be `fml::jni::JavaObjectWeakGlobalRef` + /// + virtual void SurfaceTextureDetachFromGLContext(std::any surface_texture) = 0; //---------------------------------------------------------------------------- /// @brief Positions and sizes a platform view if using hybrid diff --git a/shell/platform/android/platform_view_android_jni_impl.cc b/shell/platform/android/platform_view_android_jni_impl.cc index 66d6caf1b26c4..bbd494557b314 100644 --- a/shell/platform/android/platform_view_android_jni_impl.cc +++ b/shell/platform/android/platform_view_android_jni_impl.cc @@ -29,6 +29,9 @@ #define ANDROID_SHELL_HOLDER \ (reinterpret_cast(shell_holder)) +#define SURFACE_TEXTURE_LOCAL_REF \ + (std::any_cast(surface_texture)) + namespace flutter { namespace { @@ -886,37 +889,35 @@ void PlatformViewAndroidJNIImpl::FlutterViewOnPreEngineRestart() { FML_CHECK(CheckException(env)); } -void PlatformViewAndroidJNIImpl::SetCurrentSurfaceTexture( - fml::jni::JavaObjectWeakGlobalRef& surface_texture) { - surface_texture_ = surface_texture; -} - void PlatformViewAndroidJNIImpl::SurfaceTextureAttachToGLContext( + std::any surface_texture, int textureId) { JNIEnv* env = fml::jni::AttachCurrentThread(); - fml::jni::ScopedJavaLocalRef surface_texture = - surface_texture_.get(env); - if (surface_texture.is_null()) { + fml::jni::ScopedJavaLocalRef surface_texture_local_ref = + SURFACE_TEXTURE_LOCAL_REF.get(env); + if (surface_texture_local_ref.is_null()) { return; } - env->CallVoidMethod(surface_texture.obj(), g_attach_to_gl_context_method, - textureId); + env->CallVoidMethod(surface_texture_local_ref.obj(), + g_attach_to_gl_context_method, textureId); FML_CHECK(CheckException(env)); } -void PlatformViewAndroidJNIImpl::SurfaceTextureUpdateTexImage() { +void PlatformViewAndroidJNIImpl::SurfaceTextureUpdateTexImage( + std::any surface_texture) { JNIEnv* env = fml::jni::AttachCurrentThread(); - fml::jni::ScopedJavaLocalRef surface_texture = - surface_texture_.get(env); - if (surface_texture.is_null()) { + fml::jni::ScopedJavaLocalRef surface_texture_local_ref = + SURFACE_TEXTURE_LOCAL_REF.get(env); + if (surface_texture_local_ref.is_null()) { return; } - env->CallVoidMethod(surface_texture.obj(), g_update_tex_image_method); + env->CallVoidMethod(surface_texture_local_ref.obj(), + g_update_tex_image_method); FML_CHECK(CheckException(env)); } @@ -934,20 +935,21 @@ SkSize ScaleToFill(float scaleX, float scaleY) { } void PlatformViewAndroidJNIImpl::SurfaceTextureGetTransformMatrix( + std::any surface_texture, SkMatrix& transform) { JNIEnv* env = fml::jni::AttachCurrentThread(); - fml::jni::ScopedJavaLocalRef surface_texture = - surface_texture_.get(env); - if (surface_texture.is_null()) { + fml::jni::ScopedJavaLocalRef surface_texture_local_ref = + SURFACE_TEXTURE_LOCAL_REF.get(env); + if (surface_texture_local_ref.is_null()) { return; } fml::jni::ScopedJavaLocalRef transformMatrix( env, env->NewFloatArray(16)); - env->CallVoidMethod(surface_texture.obj(), g_get_transform_matrix_method, - transformMatrix.obj()); + env->CallVoidMethod(surface_texture_local_ref.obj(), + g_get_transform_matrix_method, transformMatrix.obj()); FML_CHECK(CheckException(env)); float* m = env->GetFloatArrayElements(transformMatrix.obj(), nullptr); @@ -962,16 +964,18 @@ void PlatformViewAndroidJNIImpl::SurfaceTextureGetTransformMatrix( transform.set9(matrix3); } -void PlatformViewAndroidJNIImpl::SurfaceTextureDetachFromGLContext() { +void PlatformViewAndroidJNIImpl::SurfaceTextureDetachFromGLContext( + std::any surface_texture) { JNIEnv* env = fml::jni::AttachCurrentThread(); - fml::jni::ScopedJavaLocalRef surface_texture = - surface_texture_.get(env); - if (surface_texture.is_null()) { + fml::jni::ScopedJavaLocalRef surface_texture_local_ref = + SURFACE_TEXTURE_LOCAL_REF.get(env); + if (surface_texture_local_ref.is_null()) { return; } - env->CallVoidMethod(surface_texture.obj(), g_detach_from_gl_context_method); + env->CallVoidMethod(surface_texture_local_ref.obj(), + g_detach_from_gl_context_method); FML_CHECK(CheckException(env)); } diff --git a/shell/platform/android/platform_view_android_jni_impl.h b/shell/platform/android/platform_view_android_jni_impl.h index d988ac1c2500e..b0432ab22c80b 100644 --- a/shell/platform/android/platform_view_android_jni_impl.h +++ b/shell/platform/android/platform_view_android_jni_impl.h @@ -39,16 +39,15 @@ class PlatformViewAndroidJNIImpl final : public PlatformViewAndroidJNI { void FlutterViewOnPreEngineRestart() override; - void SetCurrentSurfaceTexture( - fml::jni::JavaObjectWeakGlobalRef& surface_texture); + void SurfaceTextureAttachToGLContext(std::any surface_texture, + int textureId) override; - void SurfaceTextureAttachToGLContext(int textureId) override; + void SurfaceTextureUpdateTexImage(std::any surface_texture) override; - void SurfaceTextureUpdateTexImage() override; + void SurfaceTextureGetTransformMatrix(std::any surface_texture, + SkMatrix& transform) override; - void SurfaceTextureGetTransformMatrix(SkMatrix& transform) override; - - void SurfaceTextureDetachFromGLContext() override; + void SurfaceTextureDetachFromGLContext(std::any surface_texture) override; void FlutterViewOnDisplayPlatformView(int view_id, int x, From 8d9d48adbaef79c0936de582803a7fa72193f023 Mon Sep 17 00:00:00 2001 From: Emmanuel Garcia Date: Tue, 9 Jun 2020 14:23:13 -0700 Subject: [PATCH 12/16] Chinmay suggestion --- .../android/jni/platform_view_android_jni.h | 34 +++++++++++-------- .../android/platform_view_android_jni_impl.cc | 19 +++++------ .../android/platform_view_android_jni_impl.h | 9 ++--- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/shell/platform/android/jni/platform_view_android_jni.h b/shell/platform/android/jni/platform_view_android_jni.h index 25e235964e291..5542784bc9cc4 100644 --- a/shell/platform/android/jni/platform_view_android_jni.h +++ b/shell/platform/android/jni/platform_view_android_jni.h @@ -5,7 +5,9 @@ #ifndef FLUTTER_SHELL_PLATFORM_ANDROID_PLATFORM_VIEW_ANDROID_JNI_H_ #define FLUTTER_SHELL_PLATFORM_ANDROID_PLATFORM_VIEW_ANDROID_JNI_H_ -#include +#if OS_ANDROID +#include "flutter/fml/platform/android/jni_weak_ref.h" +#endif #include "flutter/fml/macros.h" #include "flutter/fml/mapping.h" @@ -15,6 +17,12 @@ namespace flutter { +#if OS_ANDROID +using JavaWeakGlobalRef = fml::jni::JavaObjectWeakGlobalRef; +#else +using JavaWeakGlobalRef = std::nullptr_t; +#endif + //------------------------------------------------------------------------------ /// Allows to call Java code running in the JVM from any thread. However, most /// methods can only be called from the platform thread as that is where the @@ -74,35 +82,31 @@ class PlatformViewAndroidJNI { /// @brief Attach the SurfaceTexture to the OpenGL ES context that is /// current on the calling thread. /// - /// @note `surface_texture` must be `fml::jni::JavaObjectWeakGlobalRef` - /// - virtual void SurfaceTextureAttachToGLContext(std::any surface_texture, - int textureId) = 0; + virtual void SurfaceTextureAttachToGLContext( + JavaWeakGlobalRef surface_texture, + int textureId) = 0; //---------------------------------------------------------------------------- /// @brief Updates the texture image to the most recent frame from the /// image stream. /// - /// @note `surface_texture` must be `fml::jni::JavaObjectWeakGlobalRef` - /// - virtual void SurfaceTextureUpdateTexImage(std::any surface_texture) = 0; + virtual void SurfaceTextureUpdateTexImage( + JavaWeakGlobalRef 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. /// - /// @note `surface_texture` must be `fml::jni::JavaObjectWeakGlobalRef` - /// - virtual void SurfaceTextureGetTransformMatrix(std::any surface_texture, - SkMatrix& transform) = 0; + virtual void SurfaceTextureGetTransformMatrix( + JavaWeakGlobalRef surface_texture, + SkMatrix& transform) = 0; //---------------------------------------------------------------------------- /// @brief Detaches a SurfaceTexture from the OpenGL ES context. /// - /// @note `surface_texture` must be `fml::jni::JavaObjectWeakGlobalRef` - /// - virtual void SurfaceTextureDetachFromGLContext(std::any surface_texture) = 0; + virtual void SurfaceTextureDetachFromGLContext( + JavaWeakGlobalRef surface_texture) = 0; //---------------------------------------------------------------------------- /// @brief Positions and sizes a platform view if using hybrid diff --git a/shell/platform/android/platform_view_android_jni_impl.cc b/shell/platform/android/platform_view_android_jni_impl.cc index bbd494557b314..59f7797e21bd2 100644 --- a/shell/platform/android/platform_view_android_jni_impl.cc +++ b/shell/platform/android/platform_view_android_jni_impl.cc @@ -29,9 +29,6 @@ #define ANDROID_SHELL_HOLDER \ (reinterpret_cast(shell_holder)) -#define SURFACE_TEXTURE_LOCAL_REF \ - (std::any_cast(surface_texture)) - namespace flutter { namespace { @@ -890,12 +887,12 @@ void PlatformViewAndroidJNIImpl::FlutterViewOnPreEngineRestart() { } void PlatformViewAndroidJNIImpl::SurfaceTextureAttachToGLContext( - std::any surface_texture, + JavaWeakGlobalRef surface_texture, int textureId) { JNIEnv* env = fml::jni::AttachCurrentThread(); fml::jni::ScopedJavaLocalRef surface_texture_local_ref = - SURFACE_TEXTURE_LOCAL_REF.get(env); + surface_texture.get(env); if (surface_texture_local_ref.is_null()) { return; } @@ -907,11 +904,11 @@ void PlatformViewAndroidJNIImpl::SurfaceTextureAttachToGLContext( } void PlatformViewAndroidJNIImpl::SurfaceTextureUpdateTexImage( - std::any surface_texture) { + JavaWeakGlobalRef surface_texture) { JNIEnv* env = fml::jni::AttachCurrentThread(); fml::jni::ScopedJavaLocalRef surface_texture_local_ref = - SURFACE_TEXTURE_LOCAL_REF.get(env); + surface_texture.get(env); if (surface_texture_local_ref.is_null()) { return; } @@ -935,12 +932,12 @@ SkSize ScaleToFill(float scaleX, float scaleY) { } void PlatformViewAndroidJNIImpl::SurfaceTextureGetTransformMatrix( - std::any surface_texture, + JavaWeakGlobalRef surface_texture, SkMatrix& transform) { JNIEnv* env = fml::jni::AttachCurrentThread(); fml::jni::ScopedJavaLocalRef surface_texture_local_ref = - SURFACE_TEXTURE_LOCAL_REF.get(env); + surface_texture.get(env); if (surface_texture_local_ref.is_null()) { return; } @@ -965,11 +962,11 @@ void PlatformViewAndroidJNIImpl::SurfaceTextureGetTransformMatrix( } void PlatformViewAndroidJNIImpl::SurfaceTextureDetachFromGLContext( - std::any surface_texture) { + JavaWeakGlobalRef surface_texture) { JNIEnv* env = fml::jni::AttachCurrentThread(); fml::jni::ScopedJavaLocalRef surface_texture_local_ref = - SURFACE_TEXTURE_LOCAL_REF.get(env); + surface_texture.get(env); if (surface_texture_local_ref.is_null()) { return; } diff --git a/shell/platform/android/platform_view_android_jni_impl.h b/shell/platform/android/platform_view_android_jni_impl.h index b0432ab22c80b..fe5116ffb783b 100644 --- a/shell/platform/android/platform_view_android_jni_impl.h +++ b/shell/platform/android/platform_view_android_jni_impl.h @@ -39,15 +39,16 @@ class PlatformViewAndroidJNIImpl final : public PlatformViewAndroidJNI { void FlutterViewOnPreEngineRestart() override; - void SurfaceTextureAttachToGLContext(std::any surface_texture, + void SurfaceTextureAttachToGLContext(JavaWeakGlobalRef surface_texture, int textureId) override; - void SurfaceTextureUpdateTexImage(std::any surface_texture) override; + void SurfaceTextureUpdateTexImage(JavaWeakGlobalRef surface_texture) override; - void SurfaceTextureGetTransformMatrix(std::any surface_texture, + void SurfaceTextureGetTransformMatrix(JavaWeakGlobalRef surface_texture, SkMatrix& transform) override; - void SurfaceTextureDetachFromGLContext(std::any surface_texture) override; + void SurfaceTextureDetachFromGLContext( + JavaWeakGlobalRef surface_texture) override; void FlutterViewOnDisplayPlatformView(int view_id, int x, From 12642bca32c0654bac1edf3c456e2043eb89b06e Mon Sep 17 00:00:00 2001 From: Emmanuel Garcia Date: Tue, 9 Jun 2020 16:03:36 -0700 Subject: [PATCH 13/16] Tweaks --- shell/platform/android/jni/platform_view_android_jni.h | 6 +++--- shell/platform/android/platform_view_android_jni_impl.h | 3 --- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/shell/platform/android/jni/platform_view_android_jni.h b/shell/platform/android/jni/platform_view_android_jni.h index 5542784bc9cc4..90317cd5d930b 100644 --- a/shell/platform/android/jni/platform_view_android_jni.h +++ b/shell/platform/android/jni/platform_view_android_jni.h @@ -5,14 +5,14 @@ #ifndef FLUTTER_SHELL_PLATFORM_ANDROID_PLATFORM_VIEW_ANDROID_JNI_H_ #define FLUTTER_SHELL_PLATFORM_ANDROID_PLATFORM_VIEW_ANDROID_JNI_H_ +#include "flutter/fml/macros.h" +#include "flutter/fml/mapping.h" + #if OS_ANDROID #include "flutter/fml/platform/android/jni_weak_ref.h" #endif -#include "flutter/fml/macros.h" -#include "flutter/fml/mapping.h" #include "flutter/lib/ui/window/platform_message.h" - #include "third_party/skia/include/core/SkMatrix.h" namespace flutter { diff --git a/shell/platform/android/platform_view_android_jni_impl.h b/shell/platform/android/platform_view_android_jni_impl.h index fe5116ffb783b..4b8312d9c0c26 100644 --- a/shell/platform/android/platform_view_android_jni_impl.h +++ b/shell/platform/android/platform_view_android_jni_impl.h @@ -60,9 +60,6 @@ class PlatformViewAndroidJNIImpl final : public PlatformViewAndroidJNI { // Reference to FlutterJNI object. const fml::jni::JavaObjectWeakGlobalRef java_object_; - // Reference to the current Surface texture object. - fml::jni::JavaObjectWeakGlobalRef surface_texture_; - FML_DISALLOW_COPY_AND_ASSIGN(PlatformViewAndroidJNIImpl); }; From d017a993d98d3ecc7d1c505bd8b62afd4ded00f6 Mon Sep 17 00:00:00 2001 From: Emmanuel Garcia Date: Tue, 9 Jun 2020 17:15:33 -0700 Subject: [PATCH 14/16] Fix tests --- .../external_view_embedder_unittests.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shell/platform/android/external_view_embedder/external_view_embedder_unittests.cc b/shell/platform/android/external_view_embedder/external_view_embedder_unittests.cc index 392458dce1270..d0ef824cb6724 100644 --- a/shell/platform/android/external_view_embedder/external_view_embedder_unittests.cc +++ b/shell/platform/android/external_view_embedder/external_view_embedder_unittests.cc @@ -50,7 +50,7 @@ TEST(AndroidExternalViewEmbedder, CancelFrame) { } TEST(AndroidExternalViewEmbedder, RasterizerRunsOnPlatformThread) { - auto embedder = new AndroidExternalViewEmbedder(); + auto embedder = new AndroidExternalViewEmbedder(nullptr); auto platform_thread = new fml::Thread("platform"); auto rasterizer_thread = new fml::Thread("rasterizer"); auto platform_queue_id = platform_thread->GetTaskRunner()->GetTaskQueueId(); @@ -82,7 +82,7 @@ TEST(AndroidExternalViewEmbedder, RasterizerRunsOnPlatformThread) { } TEST(AndroidExternalViewEmbedder, RasterizerRunsOnRasterizerThread) { - auto embedder = new AndroidExternalViewEmbedder(); + auto embedder = new AndroidExternalViewEmbedder(nullptr); auto platform_thread = new fml::Thread("platform"); auto rasterizer_thread = new fml::Thread("rasterizer"); auto platform_queue_id = platform_thread->GetTaskRunner()->GetTaskQueueId(); From c7c9e44fc592659c237ee38af9f226bcff1c8b75 Mon Sep 17 00:00:00 2001 From: Emmanuel Garcia Date: Wed, 10 Jun 2020 12:28:13 -0700 Subject: [PATCH 15/16] Trigger presubmit checks From aa641bc9949c6a34e7e4455b452eb46896288207 Mon Sep 17 00:00:00 2001 From: Emmanuel Garcia Date: Wed, 10 Jun 2020 16:40:56 -0700 Subject: [PATCH 16/16] unique_ptr -> shared_ptr --- .../android/android_external_texture_gl.cc | 4 ++-- .../android/android_external_texture_gl.h | 4 ++-- shell/platform/android/android_shell_holder.cc | 8 ++++---- shell/platform/android/android_shell_holder.h | 4 ++-- shell/platform/android/android_surface.cc | 9 ++++----- shell/platform/android/android_surface.h | 2 +- shell/platform/android/android_surface_gl.cc | 4 ++-- shell/platform/android/android_surface_gl.h | 2 +- .../android/android_surface_software.cc | 4 ++-- .../android/android_surface_software.h | 2 +- .../platform/android/android_surface_vulkan.cc | 4 ++-- .../platform/android/android_surface_vulkan.h | 2 +- .../external_view_embedder.cc | 4 ++-- .../external_view_embedder.h | 4 ++-- .../platform_message_response_android.cc | 10 +++++----- .../platform_message_response_android.h | 9 ++++----- .../platform/android/platform_view_android.cc | 18 +++++++----------- shell/platform/android/platform_view_android.h | 6 +++--- .../android/platform_view_android_jni_impl.cc | 7 +++---- 19 files changed, 50 insertions(+), 57 deletions(-) diff --git a/shell/platform/android/android_external_texture_gl.cc b/shell/platform/android/android_external_texture_gl.cc index d28980f09b9f8..01822b2329bff 100644 --- a/shell/platform/android/android_external_texture_gl.cc +++ b/shell/platform/android/android_external_texture_gl.cc @@ -13,9 +13,9 @@ namespace flutter { AndroidExternalTextureGL::AndroidExternalTextureGL( int64_t id, const fml::jni::JavaObjectWeakGlobalRef& surface_texture, - std::unique_ptr jni_facade) + std::shared_ptr jni_facade) : Texture(id), - jni_facade_(std::move(jni_facade)), + jni_facade_(jni_facade), surface_texture_(surface_texture), transform(SkMatrix::I()) {} diff --git a/shell/platform/android/android_external_texture_gl.h b/shell/platform/android/android_external_texture_gl.h index 5507cb20f1c72..c8268be8a4689 100644 --- a/shell/platform/android/android_external_texture_gl.h +++ b/shell/platform/android/android_external_texture_gl.h @@ -17,7 +17,7 @@ class AndroidExternalTextureGL : public flutter::Texture { AndroidExternalTextureGL( int64_t id, const fml::jni::JavaObjectWeakGlobalRef& surface_texture, - std::unique_ptr jni_facade); + std::shared_ptr jni_facade); ~AndroidExternalTextureGL() override; @@ -45,7 +45,7 @@ class AndroidExternalTextureGL : public flutter::Texture { enum class AttachmentState { uninitialized, attached, detached }; - std::unique_ptr jni_facade_; + std::shared_ptr jni_facade_; fml::jni::JavaObjectWeakGlobalRef surface_texture_; diff --git a/shell/platform/android/android_shell_holder.cc b/shell/platform/android/android_shell_holder.cc index 134be4c74f2ae..f2f39d4ca390c 100644 --- a/shell/platform/android/android_shell_holder.cc +++ b/shell/platform/android/android_shell_holder.cc @@ -29,9 +29,9 @@ static WindowData GetDefaultWindowData() { AndroidShellHolder::AndroidShellHolder( flutter::Settings settings, - std::unique_ptr jni_facade, + std::shared_ptr jni_facade, bool is_background_view) - : settings_(std::move(settings)), jni_facade_(std::move(jni_facade)) { + : settings_(std::move(settings)), jni_facade_(jni_facade) { static size_t shell_count = 1; auto thread_label = std::to_string(shell_count++); @@ -62,13 +62,13 @@ AndroidShellHolder::AndroidShellHolder( platform_view_android = std::make_unique( shell, // delegate shell.GetTaskRunners(), // task runners - std::move(jni_facade) // JNI interop + jni_facade // JNI interop ); } else { platform_view_android = std::make_unique( shell, // delegate shell.GetTaskRunners(), // task runners - std::move(jni_facade), // JNI interop + jni_facade, // JNI interop shell.GetSettings() .enable_software_rendering // use software rendering ); diff --git a/shell/platform/android/android_shell_holder.h b/shell/platform/android/android_shell_holder.h index 437dfcdcfe29b..9ebd3c7fec62b 100644 --- a/shell/platform/android/android_shell_holder.h +++ b/shell/platform/android/android_shell_holder.h @@ -22,7 +22,7 @@ namespace flutter { class AndroidShellHolder { public: AndroidShellHolder(flutter::Settings settings, - std::unique_ptr jni_facade, + std::shared_ptr jni_facade, bool is_background_view); ~AndroidShellHolder(); @@ -42,7 +42,7 @@ class AndroidShellHolder { private: const flutter::Settings settings_; - const std::unique_ptr jni_facade_; + const std::shared_ptr jni_facade_; fml::WeakPtr platform_view_; ThreadHost thread_host_; std::unique_ptr shell_; diff --git a/shell/platform/android/android_surface.cc b/shell/platform/android/android_surface.cc index 920ecb6e43017..e50f79180b78b 100644 --- a/shell/platform/android/android_surface.cc +++ b/shell/platform/android/android_surface.cc @@ -16,19 +16,18 @@ namespace flutter { std::unique_ptr AndroidSurface::Create( std::shared_ptr android_context, - std::unique_ptr jni_facade) { + std::shared_ptr jni_facade) { std::unique_ptr surface; switch (android_context->RenderingApi()) { case AndroidRenderingAPI::kSoftware: - surface = std::make_unique(std::move(jni_facade)); + surface = std::make_unique(jni_facade); break; case AndroidRenderingAPI::kOpenGLES: - surface = std::make_unique(android_context, - std::move(jni_facade)); + surface = std::make_unique(android_context, jni_facade); break; case AndroidRenderingAPI::kVulkan: #if SHELL_ENABLE_VULKAN - surface = std::make_unique(std::move(jni_facade)); + surface = std::make_unique(jni_facade); #endif // SHELL_ENABLE_VULKAN break; } diff --git a/shell/platform/android/android_surface.h b/shell/platform/android/android_surface.h index 9c021ca528fd4..4997588b6a649 100644 --- a/shell/platform/android/android_surface.h +++ b/shell/platform/android/android_surface.h @@ -23,7 +23,7 @@ class AndroidSurface { public: static std::unique_ptr Create( std::shared_ptr android_context, - std::unique_ptr jni_facade); + std::shared_ptr jni_facade); virtual ~AndroidSurface(); diff --git a/shell/platform/android/android_surface_gl.cc b/shell/platform/android/android_surface_gl.cc index 780ca8c102db8..521f06995cdd1 100644 --- a/shell/platform/android/android_surface_gl.cc +++ b/shell/platform/android/android_surface_gl.cc @@ -13,7 +13,7 @@ namespace flutter { AndroidSurfaceGL::AndroidSurfaceGL( std::shared_ptr android_context, - std::unique_ptr jni_facade) + std::shared_ptr jni_facade) : native_window_(nullptr), onscreen_surface_(nullptr), offscreen_surface_(nullptr) { @@ -25,7 +25,7 @@ AndroidSurfaceGL::AndroidSurfaceGL( offscreen_surface_ = nullptr; } external_view_embedder_ = - std::make_unique(std::move(jni_facade)); + std::make_unique(jni_facade); } AndroidSurfaceGL::~AndroidSurfaceGL() { diff --git a/shell/platform/android/android_surface_gl.h b/shell/platform/android/android_surface_gl.h index 8679270a641eb..baa0d451d94c3 100644 --- a/shell/platform/android/android_surface_gl.h +++ b/shell/platform/android/android_surface_gl.h @@ -22,7 +22,7 @@ class AndroidSurfaceGL final : public GPUSurfaceGLDelegate, public AndroidSurface { public: AndroidSurfaceGL(std::shared_ptr android_context, - std::unique_ptr jni_facade); + std::shared_ptr jni_facade); ~AndroidSurfaceGL() override; diff --git a/shell/platform/android/android_surface_software.cc b/shell/platform/android/android_surface_software.cc index c59db100f2f4e..56d9e3bc542fd 100644 --- a/shell/platform/android/android_surface_software.cc +++ b/shell/platform/android/android_surface_software.cc @@ -37,11 +37,11 @@ bool GetSkColorType(int32_t buffer_format, } // anonymous namespace AndroidSurfaceSoftware::AndroidSurfaceSoftware( - std::unique_ptr jni_facade) { + std::shared_ptr jni_facade) { GetSkColorType(WINDOW_FORMAT_RGBA_8888, &target_color_type_, &target_alpha_type_); external_view_embedder_ = - std::make_unique(std::move(jni_facade)); + std::make_unique(jni_facade); } AndroidSurfaceSoftware::~AndroidSurfaceSoftware() = default; diff --git a/shell/platform/android/android_surface_software.h b/shell/platform/android/android_surface_software.h index d698010178d29..00a4b245af7d2 100644 --- a/shell/platform/android/android_surface_software.h +++ b/shell/platform/android/android_surface_software.h @@ -18,7 +18,7 @@ namespace flutter { class AndroidSurfaceSoftware final : public AndroidSurface, public GPUSurfaceSoftwareDelegate { public: - AndroidSurfaceSoftware(std::unique_ptr jni_facade); + AndroidSurfaceSoftware(std::shared_ptr jni_facade); ~AndroidSurfaceSoftware() override; diff --git a/shell/platform/android/android_surface_vulkan.cc b/shell/platform/android/android_surface_vulkan.cc index c7f9ade534004..3d802a1f1ba42 100644 --- a/shell/platform/android/android_surface_vulkan.cc +++ b/shell/platform/android/android_surface_vulkan.cc @@ -13,10 +13,10 @@ namespace flutter { AndroidSurfaceVulkan::AndroidSurfaceVulkan( - std::unique_ptr jni_facade) + std::shared_ptr jni_facade) : proc_table_(fml::MakeRefCounted()) { external_view_embedder_ = - std::make_unique(std::move(jni_facade)); + std::make_unique(jni_facade); } AndroidSurfaceVulkan::~AndroidSurfaceVulkan() = default; diff --git a/shell/platform/android/android_surface_vulkan.h b/shell/platform/android/android_surface_vulkan.h index 9a25315d4d0ee..2778a7ec10003 100644 --- a/shell/platform/android/android_surface_vulkan.h +++ b/shell/platform/android/android_surface_vulkan.h @@ -20,7 +20,7 @@ namespace flutter { class AndroidSurfaceVulkan : public AndroidSurface, public GPUSurfaceVulkanDelegate { public: - AndroidSurfaceVulkan(std::unique_ptr jni_facade); + AndroidSurfaceVulkan(std::shared_ptr jni_facade); ~AndroidSurfaceVulkan() override; diff --git a/shell/platform/android/external_view_embedder/external_view_embedder.cc b/shell/platform/android/external_view_embedder/external_view_embedder.cc index 29ff3c42b153a..4e42ac662f77f 100644 --- a/shell/platform/android/external_view_embedder/external_view_embedder.cc +++ b/shell/platform/android/external_view_embedder/external_view_embedder.cc @@ -9,8 +9,8 @@ namespace flutter { AndroidExternalViewEmbedder::AndroidExternalViewEmbedder( - std::unique_ptr jni_facade) - : ExternalViewEmbedder(), jni_facade_(std::move(jni_facade)) {} + std::shared_ptr jni_facade) + : ExternalViewEmbedder(), jni_facade_(jni_facade) {} // |ExternalViewEmbedder| void AndroidExternalViewEmbedder::PrerollCompositeEmbeddedView( diff --git a/shell/platform/android/external_view_embedder/external_view_embedder.h b/shell/platform/android/external_view_embedder/external_view_embedder.h index 8955dd04e78df..6cfa82a8d58e1 100644 --- a/shell/platform/android/external_view_embedder/external_view_embedder.h +++ b/shell/platform/android/external_view_embedder/external_view_embedder.h @@ -15,7 +15,7 @@ namespace flutter { class AndroidExternalViewEmbedder final : public ExternalViewEmbedder { public: AndroidExternalViewEmbedder( - std::unique_ptr jni_facade); + std::shared_ptr jni_facade); // |ExternalViewEmbedder| void PrerollCompositeEmbeddedView( @@ -53,7 +53,7 @@ class AndroidExternalViewEmbedder final : public ExternalViewEmbedder { private: // Allows to call methods in Java. - std::unique_ptr jni_facade_; + const std::shared_ptr jni_facade_; // The number of frames the rasterizer task runner will continue // to run on the platform thread after no platform view is rendered. diff --git a/shell/platform/android/platform_message_response_android.cc b/shell/platform/android/platform_message_response_android.cc index b44303cd36da1..36950caf43942 100644 --- a/shell/platform/android/platform_message_response_android.cc +++ b/shell/platform/android/platform_message_response_android.cc @@ -11,10 +11,10 @@ namespace flutter { PlatformMessageResponseAndroid::PlatformMessageResponseAndroid( int response_id, - std::unique_ptr jni_facade, + std::shared_ptr jni_facade, fml::RefPtr platform_task_runner) : response_id_(response_id), - jni_facade_(std::move(jni_facade)), + jni_facade_(jni_facade), platform_task_runner_(std::move(platform_task_runner)) {} PlatformMessageResponseAndroid::~PlatformMessageResponseAndroid() = default; @@ -25,7 +25,7 @@ void PlatformMessageResponseAndroid::Complete( platform_task_runner_->PostTask( fml::MakeCopyable([response_id = response_id_, // data = std::move(data), // - jni_facade = std::move(jni_facade_)]() mutable { + jni_facade = jni_facade_]() mutable { jni_facade->FlutterViewHandlePlatformMessageResponse(response_id, std::move(data)); })); @@ -34,8 +34,8 @@ void PlatformMessageResponseAndroid::Complete( // |flutter::PlatformMessageResponse| void PlatformMessageResponseAndroid::CompleteEmpty() { platform_task_runner_->PostTask( - fml::MakeCopyable([response_id = response_id_, // - jni_facade = std::move(jni_facade_) // + fml::MakeCopyable([response_id = response_id_, // + jni_facade = jni_facade_ // ]() { // Make the response call into Java. jni_facade->FlutterViewHandlePlatformMessageResponse(response_id, diff --git a/shell/platform/android/platform_message_response_android.h b/shell/platform/android/platform_message_response_android.h index 4af2fb2b97c46..5b13e4bd268ae 100644 --- a/shell/platform/android/platform_message_response_android.h +++ b/shell/platform/android/platform_message_response_android.h @@ -24,15 +24,14 @@ class PlatformMessageResponseAndroid : public flutter::PlatformMessageResponse { private: PlatformMessageResponseAndroid( int response_id, - std::unique_ptr jni_facade, + std::shared_ptr jni_facade, fml::RefPtr platform_task_runner); ~PlatformMessageResponseAndroid() override; - int response_id_; - fml::jni::JavaObjectWeakGlobalRef weak_java_object_; - std::unique_ptr jni_facade_; - fml::RefPtr platform_task_runner_; + const int response_id_; + const std::shared_ptr jni_facade_; + const fml::RefPtr platform_task_runner_; FML_FRIEND_MAKE_REF_COUNTED(PlatformMessageResponseAndroid); FML_DISALLOW_COPY_AND_ASSIGN(PlatformMessageResponseAndroid); diff --git a/shell/platform/android/platform_view_android.cc b/shell/platform/android/platform_view_android.cc index 7dce84c1ecb69..0cae52e370c4d 100644 --- a/shell/platform/android/platform_view_android.cc +++ b/shell/platform/android/platform_view_android.cc @@ -22,10 +22,9 @@ namespace flutter { PlatformViewAndroid::PlatformViewAndroid( PlatformView::Delegate& delegate, flutter::TaskRunners task_runners, - std::unique_ptr jni_facade, + std::shared_ptr jni_facade, bool use_software_rendering) - : PlatformView(delegate, std::move(task_runners)), - jni_facade_(std::move(jni_facade)) { + : PlatformView(delegate, std::move(task_runners)), jni_facade_(jni_facade) { std::shared_ptr android_context; if (use_software_rendering) { android_context = AndroidContext::Create(AndroidRenderingAPI::kSoftware); @@ -36,8 +35,7 @@ PlatformViewAndroid::PlatformViewAndroid( android_context = AndroidContext::Create(AndroidRenderingAPI::kOpenGLES); #endif // SHELL_ENABLE_VULKAN } - android_surface_ = - AndroidSurface::Create(android_context, std::move(jni_facade)); + android_surface_ = AndroidSurface::Create(android_context, jni_facade); FML_CHECK(android_surface_) << "Could not create an OpenGL, Vulkan or Software surface to setup " "rendering."; @@ -46,9 +44,9 @@ PlatformViewAndroid::PlatformViewAndroid( PlatformViewAndroid::PlatformViewAndroid( PlatformView::Delegate& delegate, flutter::TaskRunners task_runners, - std::unique_ptr jni_facade) + std::shared_ptr jni_facade) : PlatformView(delegate, std::move(task_runners)), - jni_facade_(std::move(jni_facade)) {} + jni_facade_(jni_facade) {} PlatformViewAndroid::~PlatformViewAndroid() = default; @@ -113,8 +111,7 @@ void PlatformViewAndroid::DispatchPlatformMessage(JNIEnv* env, fml::RefPtr response; if (response_id) { response = fml::MakeRefCounted( - response_id, std::move(jni_facade_), - task_runners_.GetPlatformTaskRunner()); + response_id, jni_facade_, task_runners_.GetPlatformTaskRunner()); } PlatformView::DispatchPlatformMessage( @@ -128,8 +125,7 @@ void PlatformViewAndroid::DispatchEmptyPlatformMessage(JNIEnv* env, fml::RefPtr response; if (response_id) { response = fml::MakeRefCounted( - response_id, std::move(jni_facade_), - task_runners_.GetPlatformTaskRunner()); + response_id, jni_facade_, task_runners_.GetPlatformTaskRunner()); } PlatformView::DispatchPlatformMessage( diff --git a/shell/platform/android/platform_view_android.h b/shell/platform/android/platform_view_android.h index 28c407894cef2..9f859a1dcdaf5 100644 --- a/shell/platform/android/platform_view_android.h +++ b/shell/platform/android/platform_view_android.h @@ -29,12 +29,12 @@ class PlatformViewAndroid final : public PlatformView { // background execution. PlatformViewAndroid(PlatformView::Delegate& delegate, flutter::TaskRunners task_runners, - std::unique_ptr jni_facade); + std::shared_ptr jni_facade); // Creates a PlatformViewAndroid with a rendering surface. PlatformViewAndroid(PlatformView::Delegate& delegate, flutter::TaskRunners task_runners, - std::unique_ptr jni_facade, + std::shared_ptr jni_facade, bool use_software_rendering); ~PlatformViewAndroid() override; @@ -75,7 +75,7 @@ class PlatformViewAndroid final : public PlatformView { const fml::jni::JavaObjectWeakGlobalRef& surface_texture); private: - std::unique_ptr jni_facade_; + const std::shared_ptr jni_facade_; std::unique_ptr android_surface_; // We use id 0 to mean that no response is expected. diff --git a/shell/platform/android/platform_view_android_jni_impl.cc b/shell/platform/android/platform_view_android_jni_impl.cc index 5cc6b0c9a05c8..5d3a34b00139b 100644 --- a/shell/platform/android/platform_view_android_jni_impl.cc +++ b/shell/platform/android/platform_view_android_jni_impl.cc @@ -98,11 +98,10 @@ static jlong AttachJNI(JNIEnv* env, jobject flutterJNI, jboolean is_background_view) { fml::jni::JavaObjectWeakGlobalRef java_object(env, flutterJNI); - std::unique_ptr jni_facade = - std::make_unique(java_object); + std::shared_ptr jni_facade = + std::make_shared(java_object); auto shell_holder = std::make_unique( - FlutterMain::Get().GetSettings(), std::move(jni_facade), - is_background_view); + FlutterMain::Get().GetSettings(), jni_facade, is_background_view); if (shell_holder->IsValid()) { return reinterpret_cast(shell_holder.release()); } else {