Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions shell/platform/android/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ shared_library("flutter_shell_native") {
"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 Down
66 changes: 15 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::unique_ptr<PlatformViewAndroidJni> jni_facade)
: Texture(id), surface_texture_(surface_texture), transform(SkMatrix::I()) {
jni_facade_ = std::unique_ptr<PlatformViewAndroidJniImpl>(
static_cast<PlatformViewAndroidJniImpl*>(jni_facade.release()));
}

AndroidExternalTextureGL::~AndroidExternalTextureGL() {
if (state_ == AttachmentState::attached) {
Expand Down Expand Up @@ -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<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_->SetCurrentSurfaceTexture(surface_texture_);
jni_facade_->SurfaceTextureGetTransformMatrix(transform);
}

void AndroidExternalTextureGL::OnGrContextDestroyed() {
Expand All @@ -108,31 +84,19 @@ 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_->SetCurrentSurfaceTexture(surface_texture_);
jni_facade_->SurfaceTextureAttachToGLContext(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_->SetCurrentSurfaceTexture(surface_texture_);
jni_facade_->SurfaceTextureUpdateTexImage();
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_->SetCurrentSurfaceTexture(surface_texture_);
jni_facade_->SurfaceTextureDetachFromGLContext();
}

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::unique_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::unique_ptr<PlatformViewAndroidJniImpl> 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::unique_ptr<PlatformViewAndroidJni> 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++);

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
std::move(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
std::move(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/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<PlatformViewAndroidJni> jni_facade,
bool is_background_view);

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

private:
const flutter::Settings settings_;
const fml::jni::JavaObjectWeakGlobalRef java_object_;
const std::unique_ptr<PlatformViewAndroidJni> jni_facade_;
fml::WeakPtr<PlatformViewAndroid> platform_view_;
ThreadHost thread_host_;
std::unique_ptr<Shell> shell_;
Expand Down
57 changes: 15 additions & 42 deletions shell/platform/android/platform_message_response_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,65 +11,38 @@ namespace flutter {

PlatformMessageResponseAndroid::PlatformMessageResponseAndroid(
int response_id,
fml::jni::JavaObjectWeakGlobalRef weak_java_object,
std::unique_ptr<PlatformViewAndroidJni> jni_facade,
fml::RefPtr<fml::TaskRunner> 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;

// |flutter::PlatformMessageResponse|
void PlatformMessageResponseAndroid::Complete(
std::unique_ptr<fml::Mapping> 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<jbyteArray> data_array(
env, env->NewByteArray(data->GetSize()));
env->SetByteArrayRegion(
data_array.obj(), 0, data->GetSize(),
reinterpret_cast<const jbyte*>(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
4 changes: 3 additions & 1 deletion shell/platform/android/platform_message_response_android.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -23,13 +24,14 @@ class PlatformMessageResponseAndroid : public flutter::PlatformMessageResponse {
private:
PlatformMessageResponseAndroid(
int response_id,
fml::jni::JavaObjectWeakGlobalRef weak_java_object,
std::unique_ptr<PlatformViewAndroidJni> jni_facade,
fml::RefPtr<fml::TaskRunner> platform_task_runner);

~PlatformMessageResponseAndroid() override;

int response_id_;
fml::jni::JavaObjectWeakGlobalRef weak_java_object_;
std::unique_ptr<PlatformViewAndroidJni> jni_facade_;
fml::RefPtr<fml::TaskRunner> platform_task_runner_;

FML_FRIEND_MAKE_REF_COUNTED(PlatformMessageResponseAndroid);
Expand Down
Loading