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

Put JNI functions under an interface #18903

Merged
merged 21 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -782,13 +782,15 @@ 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
FILE: ../../../flutter/shell/platform/android/vsync_waiter_android.cc
FILE: ../../../flutter/shell/platform/android/vsync_waiter_android.h
Expand Down
5 changes: 3 additions & 2 deletions shell/platform/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ 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",
]
Expand All @@ -68,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",
]

Expand Down
62 changes: 11 additions & 51 deletions shell/platform/android/android_external_texture_gl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@

#include <GLES/glext.h>

#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::shared_ptr<PlatformViewAndroidJNI> jni_facade)
: Texture(id),
jni_facade_(jni_facade),
surface_texture_(surface_texture),
transform(SkMatrix::I()) {}

AndroidExternalTextureGL::~AndroidExternalTextureGL() {
if (state_ == AttachmentState::attached) {
Expand Down Expand Up @@ -68,36 +71,8 @@ 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<double>::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<jobject> surfaceTexture =
surface_texture_.get(env);
fml::jni::ScopedJavaLocalRef<jfloatArray> 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_->SurfaceTextureGetTransformMatrix(surface_texture_, transform);
}

void AndroidExternalTextureGL::OnGrContextDestroyed() {
Expand All @@ -108,31 +83,16 @@ void AndroidExternalTextureGL::OnGrContextDestroyed() {
}

void AndroidExternalTextureGL::Attach(jint textureName) {
JNIEnv* env = fml::jni::AttachCurrentThread();
fml::jni::ScopedJavaLocalRef<jobject> surfaceTexture =
surface_texture_.get(env);
if (!surfaceTexture.is_null()) {
SurfaceTextureAttachToGLContext(env, surfaceTexture.obj(), textureName);
}
jni_facade_->SurfaceTextureAttachToGLContext(surface_texture_, textureName);
}

void AndroidExternalTextureGL::Update() {
JNIEnv* env = fml::jni::AttachCurrentThread();
fml::jni::ScopedJavaLocalRef<jobject> surfaceTexture =
surface_texture_.get(env);
if (!surfaceTexture.is_null()) {
SurfaceTextureUpdateTexImage(env, surfaceTexture.obj());
UpdateTransform();
}
jni_facade_->SurfaceTextureUpdateTexImage(surface_texture_);
UpdateTransform();
}

void AndroidExternalTextureGL::Detach() {
JNIEnv* env = fml::jni::AttachCurrentThread();
fml::jni::ScopedJavaLocalRef<jobject> surfaceTexture =
surface_texture_.get(env);
if (!surfaceTexture.is_null()) {
SurfaceTextureDetachFromGLContext(env, surfaceTexture.obj());
}
jni_facade_->SurfaceTextureDetachFromGLContext(surface_texture_);
}

void AndroidExternalTextureGL::OnTextureUnregistered() {}
Expand Down
6 changes: 5 additions & 1 deletion shell/platform/android/android_external_texture_gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
#include <GLES/gl.h>
#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 {

class AndroidExternalTextureGL : public flutter::Texture {
public:
AndroidExternalTextureGL(
int64_t id,
const fml::jni::JavaObjectWeakGlobalRef& surfaceTexture);
const fml::jni::JavaObjectWeakGlobalRef& surface_texture,
std::shared_ptr<PlatformViewAndroidJNI> jni_facade);

~AndroidExternalTextureGL() override;

Expand Down Expand Up @@ -43,6 +45,8 @@ class AndroidExternalTextureGL : public flutter::Texture {

enum class AttachmentState { uninitialized, attached, detached };

std::shared_ptr<PlatformViewAndroidJNI> jni_facade_;

fml::jni::JavaObjectWeakGlobalRef surface_texture_;

AttachmentState state_ = AttachmentState::uninitialized;
Expand Down
11 changes: 5 additions & 6 deletions shell/platform/android/android_shell_holder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ static WindowData GetDefaultWindowData() {

AndroidShellHolder::AndroidShellHolder(
flutter::Settings settings,
fml::jni::JavaObjectWeakGlobalRef java_object,
std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
bool is_background_view)
: settings_(std::move(settings)), java_object_(java_object) {
: settings_(std::move(settings)), jni_facade_(jni_facade) {
static size_t shell_count = 1;
auto thread_label = std::to_string(shell_count++);

Expand All @@ -56,20 +56,19 @@ AndroidShellHolder::AndroidShellHolder(

fml::WeakPtr<PlatformViewAndroid> weak_platform_view;
Shell::CreateCallback<PlatformView> 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<PlatformViewAndroid> platform_view_android;
if (is_background_view) {
platform_view_android = std::make_unique<PlatformViewAndroid>(
shell, // delegate
shell.GetTaskRunners(), // task runners
java_object // java object handle for JNI interop
jni_facade // JNI interop
);

} else {
platform_view_android = std::make_unique<PlatformViewAndroid>(
shell, // delegate
shell.GetTaskRunners(), // task runners
java_object, // java object handle for JNI interop
jni_facade, // JNI interop
shell.GetSettings()
.enable_software_rendering // use software rendering
);
Expand Down
6 changes: 3 additions & 3 deletions shell/platform/android/android_shell_holder.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,21 @@
#include <memory>

#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"
#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"

namespace flutter {

class AndroidShellHolder {
public:
AndroidShellHolder(flutter::Settings settings,
fml::jni::JavaObjectWeakGlobalRef java_object,
std::shared_ptr<PlatformViewAndroidJNI> jni_facade,
bool is_background_view);

~AndroidShellHolder();
Expand All @@ -44,7 +44,7 @@ class AndroidShellHolder {

private:
const flutter::Settings settings_;
const fml::jni::JavaObjectWeakGlobalRef java_object_;
const std::shared_ptr<PlatformViewAndroidJNI> jni_facade_;
fml::WeakPtr<PlatformViewAndroid> platform_view_;
ThreadHost thread_host_;
std::unique_ptr<Shell> shell_;
Expand Down
9 changes: 5 additions & 4 deletions shell/platform/android/android_surface.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@
namespace flutter {

std::unique_ptr<AndroidSurface> AndroidSurface::Create(
std::shared_ptr<AndroidContext> android_context) {
std::shared_ptr<AndroidContext> android_context,
std::shared_ptr<PlatformViewAndroidJNI> jni_facade) {
std::unique_ptr<AndroidSurface> surface;
switch (android_context->RenderingApi()) {
case AndroidRenderingAPI::kSoftware:
surface = std::make_unique<AndroidSurfaceSoftware>();
surface = std::make_unique<AndroidSurfaceSoftware>(jni_facade);
break;
case AndroidRenderingAPI::kOpenGLES:
surface = std::make_unique<AndroidSurfaceGL>(android_context);
surface = std::make_unique<AndroidSurfaceGL>(android_context, jni_facade);
break;
case AndroidRenderingAPI::kVulkan:
#if SHELL_ENABLE_VULKAN
surface = std::make_unique<AndroidSurfaceVulkan>();
surface = std::make_unique<AndroidSurfaceVulkan>(jni_facade);
#endif // SHELL_ENABLE_VULKAN
break;
}
Expand Down
4 changes: 3 additions & 1 deletion shell/platform/android/android_surface.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
#include "flutter/shell/common/platform_view.h"
#include "flutter/shell/platform/android/android_context.h"
#include "flutter/shell/platform/android/android_native_window.h"
#include "flutter/shell/platform/android/jni/platform_view_android_jni.h"
#include "third_party/skia/include/core/SkSize.h"

namespace flutter {

class AndroidSurface {
public:
static std::unique_ptr<AndroidSurface> Create(
std::shared_ptr<AndroidContext> android_context);
std::shared_ptr<AndroidContext> android_context,
std::shared_ptr<PlatformViewAndroidJNI> jni_facade);

virtual ~AndroidSurface();

Expand Down
6 changes: 4 additions & 2 deletions shell/platform/android/android_surface_gl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
namespace flutter {

AndroidSurfaceGL::AndroidSurfaceGL(
std::shared_ptr<AndroidContext> android_context)
std::shared_ptr<AndroidContext> android_context,
std::shared_ptr<PlatformViewAndroidJNI> jni_facade)
: native_window_(nullptr),
onscreen_surface_(nullptr),
offscreen_surface_(nullptr) {
Expand All @@ -23,7 +24,8 @@ AndroidSurfaceGL::AndroidSurfaceGL(
if (!offscreen_surface_->IsValid()) {
offscreen_surface_ = nullptr;
}
external_view_embedder_ = std::make_unique<AndroidExternalViewEmbedder>();
external_view_embedder_ =
std::make_unique<AndroidExternalViewEmbedder>(jni_facade);
}

AndroidSurfaceGL::~AndroidSurfaceGL() = default;
Expand Down
4 changes: 3 additions & 1 deletion shell/platform/android/android_surface_gl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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/jni/platform_view_android_jni.h"

namespace flutter {

class AndroidSurfaceGL final : public GPUSurfaceGLDelegate,
public AndroidSurface {
public:
AndroidSurfaceGL(std::shared_ptr<AndroidContext> android_context);
AndroidSurfaceGL(std::shared_ptr<AndroidContext> android_context,
std::shared_ptr<PlatformViewAndroidJNI> jni_facade);

~AndroidSurfaceGL() override;

Expand Down
8 changes: 5 additions & 3 deletions shell/platform/android/android_surface_software.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -36,10 +36,12 @@ bool GetSkColorType(int32_t buffer_format,

} // anonymous namespace

AndroidSurfaceSoftware::AndroidSurfaceSoftware() {
AndroidSurfaceSoftware::AndroidSurfaceSoftware(
std::shared_ptr<PlatformViewAndroidJNI> jni_facade) {
GetSkColorType(WINDOW_FORMAT_RGBA_8888, &target_color_type_,
&target_alpha_type_);
external_view_embedder_ = std::make_unique<AndroidExternalViewEmbedder>();
external_view_embedder_ =
std::make_unique<AndroidExternalViewEmbedder>(jni_facade);
}

AndroidSurfaceSoftware::~AndroidSurfaceSoftware() = default;
Expand Down
3 changes: 2 additions & 1 deletion shell/platform/android/android_surface_software.h
Original file line number Diff line number Diff line change
Expand Up @@ -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/jni/platform_view_android_jni.h"

namespace flutter {

class AndroidSurfaceSoftware final : public AndroidSurface,
public GPUSurfaceSoftwareDelegate {
public:
AndroidSurfaceSoftware();
AndroidSurfaceSoftware(std::shared_ptr<PlatformViewAndroidJNI> jni_facade);

~AndroidSurfaceSoftware() override;

Expand Down
6 changes: 4 additions & 2 deletions shell/platform/android/android_surface_vulkan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@

namespace flutter {

AndroidSurfaceVulkan::AndroidSurfaceVulkan()
AndroidSurfaceVulkan::AndroidSurfaceVulkan(
std::shared_ptr<PlatformViewAndroidJNI> jni_facade)
: proc_table_(fml::MakeRefCounted<vulkan::VulkanProcTable>()) {
external_view_embedder_ = std::make_unique<AndroidExternalViewEmbedder>();
external_view_embedder_ =
std::make_unique<AndroidExternalViewEmbedder>(jni_facade);
}

AndroidSurfaceVulkan::~AndroidSurfaceVulkan() = default;
Expand Down
3 changes: 2 additions & 1 deletion shell/platform/android/android_surface_vulkan.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
#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/jni/platform_view_android_jni.h"
#include "flutter/vulkan/vulkan_window.h"

namespace flutter {

class AndroidSurfaceVulkan : public AndroidSurface,
public GPUSurfaceVulkanDelegate {
public:
AndroidSurfaceVulkan();
AndroidSurfaceVulkan(std::shared_ptr<PlatformViewAndroidJNI> jni_facade);

~AndroidSurfaceVulkan() override;

Expand Down
1 change: 1 addition & 0 deletions shell/platform/android/external_view_embedder/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ source_set("external_view_embedder") {
"//flutter/common",
"//flutter/flow",
"//flutter/fml",
"//flutter/shell/platform/android/jni",
"//third_party/skia",
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

namespace flutter {

AndroidExternalViewEmbedder::AndroidExternalViewEmbedder(
std::shared_ptr<PlatformViewAndroidJNI> jni_facade)
: ExternalViewEmbedder(), jni_facade_(jni_facade) {}

// |ExternalViewEmbedder|
void AndroidExternalViewEmbedder::PrerollCompositeEmbeddedView(
int view_id,
Expand Down
Loading