[android] use macro definition to shrink repetitive JNI code size. (#163395)

But less code!
This commit is contained in:
Jonah Williams 2025-02-18 13:31:36 -08:00 committed by GitHub
parent ec01644f96
commit e7d167cca2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -9,16 +9,12 @@
#include <dlfcn.h>
#include <jni.h>
#include <memory>
#include <sstream>
#include <utility>
#include "impeller/toolkit/android/shadow_realm.h"
#include "include/android/SkImageAndroid.h"
#include "unicode/uchar.h"
#include "flutter/assets/directory_asset_bundle.h"
#include "flutter/common/constants.h"
#include "flutter/fml/file.h"
#include "flutter/fml/mapping.h"
#include "flutter/fml/native_library.h"
#include "flutter/fml/platform/android/jni_util.h"
@ -66,45 +62,63 @@ static fml::jni::ScopedJavaGlobalRef<jclass>* g_bitmap_config_class = nullptr;
// Called By Native
static jmethodID g_flutter_callback_info_constructor = nullptr;
jobject CreateFlutterCallbackInformation(
JNIEnv* env,
const std::string& callbackName,
const std::string& callbackClassName,
const std::string& callbackLibraryPath) {
return env->NewObject(g_flutter_callback_info_class->obj(),
g_flutter_callback_info_constructor,
env->NewStringUTF(callbackName.c_str()),
env->NewStringUTF(callbackClassName.c_str()),
env->NewStringUTF(callbackLibraryPath.c_str()));
}
static jfieldID g_jni_shell_holder_field = nullptr;
#define FLUTTER_FOR_EACH_JNI_METHOD(V) \
V(g_handle_platform_message_method, handlePlatformMessage, \
"(Ljava/lang/String;Ljava/nio/ByteBuffer;IJ)V") \
V(g_handle_platform_message_response_method, handlePlatformMessageResponse, \
"(ILjava/nio/ByteBuffer;)V") \
V(g_update_semantics_method, updateSemantics, \
"(Ljava/nio/ByteBuffer;[Ljava/lang/String;[Ljava/nio/ByteBuffer;)V") \
V(g_on_display_platform_view_method, onDisplayPlatformView, \
"(IIIIIIILio/flutter/embedding/engine/mutatorsstack/" \
"FlutterMutatorsStack;)V") \
V(g_on_begin_frame_method, onBeginFrame, "()V") \
V(g_on_end_frame_method, onEndFrame, "()V") \
V(g_on_display_overlay_surface_method, onDisplayOverlaySurface, "(IIIII)V") \
V(g_create_transaction_method, createTransaction, \
"()Landroid/view/SurfaceControl$Transaction;") \
V(g_swap_transaction_method, swapTransactions, "()V") \
V(g_apply_transaction_method, applyTransactions, "()V") \
V(g_create_overlay_surface2_method, createOverlaySurface2, \
"()Lio/flutter/embedding/engine/FlutterOverlaySurface;") \
V(g_destroy_overlay_surface2_method, destroyOverlaySurface2, "()V") \
V(g_on_display_platform_view2_method, onDisplayPlatformView2, \
"(IIIIIIILio/flutter/embedding/engine/mutatorsstack/" \
"FlutterMutatorsStack;)V") \
V(g_on_end_frame2_method, endFrame2, "()V") \
V(g_show_overlay_surface2_method, showOverlaySurface2, "()V") \
V(g_hide_overlay_surface2_method, hideOverlaySurface2, "()V") \
V(g_get_scaled_font_size_method, getScaledFontSize, "(FI)F") \
V(g_update_custom_accessibility_actions_method, \
updateCustomAccessibilityActions, \
"(Ljava/nio/ByteBuffer;[Ljava/lang/String;)V") \
V(g_on_first_frame_method, onFirstFrame, "()V") \
V(g_on_engine_restart_method, onPreEngineRestart, "()V") \
V(g_create_overlay_surface_method, createOverlaySurface, \
"()Lio/flutter/embedding/engine/FlutterOverlaySurface;") \
V(g_destroy_overlay_surfaces_method, destroyOverlaySurfaces, "()V")
//
#define FLUTTER_DECLARE_JNI(global_field, jni_name, jni_arg) \
static jmethodID global_field = nullptr;
#define FLUTTER_BIND_JNI(global_field, jni_name, jni_arg) \
global_field = \
env->GetMethodID(g_flutter_jni_class->obj(), #jni_name, jni_arg); \
if (global_field == nullptr) { \
FML_LOG(ERROR) << "Could not locate " << #jni_name << " method."; \
return false; \
}
static jmethodID g_jni_constructor = nullptr;
static jmethodID g_long_constructor = nullptr;
static jmethodID g_handle_platform_message_method = nullptr;
static jmethodID g_handle_platform_message_response_method = nullptr;
static jmethodID g_update_semantics_method = nullptr;
static jmethodID g_update_custom_accessibility_actions_method = nullptr;
static jmethodID g_get_scaled_font_size_method = nullptr;
static jmethodID g_on_first_frame_method = nullptr;
static jmethodID g_on_engine_restart_method = nullptr;
static jmethodID g_create_overlay_surface_method = nullptr;
static jmethodID g_destroy_overlay_surfaces_method = nullptr;
static jmethodID g_on_begin_frame_method = nullptr;
static jmethodID g_on_end_frame_method = nullptr;
FLUTTER_FOR_EACH_JNI_METHOD(FLUTTER_DECLARE_JNI)
static jmethodID g_java_weak_reference_get_method = nullptr;
@ -131,9 +145,6 @@ static jmethodID g_compute_platform_resolved_locale_method = nullptr;
static jmethodID g_request_dart_deferred_library_method = nullptr;
// Called By Java
static jmethodID g_on_display_platform_view_method = nullptr;
static jmethodID g_on_display_overlay_surface_method = nullptr;
static jmethodID g_overlay_surface_id_method = nullptr;
@ -145,25 +156,6 @@ static jmethodID g_bitmap_copy_pixels_from_buffer_method = nullptr;
static jmethodID g_bitmap_config_value_of = nullptr;
// New platform Views
static jmethodID g_create_transaction_method = nullptr;
static jmethodID g_swap_transaction_method = nullptr;
static jmethodID g_apply_transaction_method = nullptr;
static jmethodID g_create_overlay_surface2_method = nullptr;
static jmethodID g_destroy_overlay_surface2_method = nullptr;
static jmethodID g_on_display_platform_view2_method = nullptr;
static jmethodID g_on_end_frame2_method = nullptr;
static jmethodID g_show_overlay_surface2_method = nullptr;
static jmethodID g_hide_overlay_surface2_method = nullptr;
// Mutators
static fml::jni::ScopedJavaGlobalRef<jclass>* g_mutators_stack_class = nullptr;
static jmethodID g_mutators_stack_init_method = nullptr;
@ -310,8 +302,11 @@ static jobject LookupCallbackInformation(JNIEnv* env,
if (cbInfo == nullptr) {
return nullptr;
}
return CreateFlutterCallbackInformation(env, cbInfo->name, cbInfo->class_name,
cbInfo->library_path);
return env->NewObject(g_flutter_callback_info_class->obj(),
g_flutter_callback_info_constructor,
env->NewStringUTF(cbInfo->name.c_str()),
env->NewStringUTF(cbInfo->class_name.c_str()),
env->NewStringUTF(cbInfo->library_path.c_str()));
}
static void SetViewportMetrics(JNIEnv* env,
@ -919,158 +914,7 @@ bool RegisterApi(JNIEnv* env) {
return false;
}
g_handle_platform_message_method =
env->GetMethodID(g_flutter_jni_class->obj(), "handlePlatformMessage",
"(Ljava/lang/String;Ljava/nio/ByteBuffer;IJ)V");
if (g_handle_platform_message_method == nullptr) {
FML_LOG(ERROR) << "Could not locate handlePlatformMessage method";
return false;
}
g_handle_platform_message_response_method = env->GetMethodID(
g_flutter_jni_class->obj(), "handlePlatformMessageResponse",
"(ILjava/nio/ByteBuffer;)V");
if (g_handle_platform_message_response_method == nullptr) {
FML_LOG(ERROR) << "Could not locate handlePlatformMessageResponse method";
return false;
}
g_get_scaled_font_size_method = env->GetMethodID(
g_flutter_jni_class->obj(), "getScaledFontSize", "(FI)F");
if (g_get_scaled_font_size_method == nullptr) {
FML_LOG(ERROR) << "Could not locate FlutterJNI#getScaledFontSize method";
return false;
}
g_update_semantics_method = env->GetMethodID(
g_flutter_jni_class->obj(), "updateSemantics",
"(Ljava/nio/ByteBuffer;[Ljava/lang/String;[Ljava/nio/ByteBuffer;)V");
if (g_update_semantics_method == nullptr) {
FML_LOG(ERROR) << "Could not locate updateSemantics method";
return false;
}
g_update_custom_accessibility_actions_method = env->GetMethodID(
g_flutter_jni_class->obj(), "updateCustomAccessibilityActions",
"(Ljava/nio/ByteBuffer;[Ljava/lang/String;)V");
if (g_update_custom_accessibility_actions_method == nullptr) {
FML_LOG(ERROR)
<< "Could not locate updateCustomAccessibilityActions method";
return false;
}
g_on_first_frame_method =
env->GetMethodID(g_flutter_jni_class->obj(), "onFirstFrame", "()V");
if (g_on_first_frame_method == nullptr) {
FML_LOG(ERROR) << "Could not locate onFirstFrame method";
return false;
}
g_on_engine_restart_method =
env->GetMethodID(g_flutter_jni_class->obj(), "onPreEngineRestart", "()V");
if (g_on_engine_restart_method == nullptr) {
FML_LOG(ERROR) << "Could not locate onEngineRestart method";
return false;
}
g_create_overlay_surface_method =
env->GetMethodID(g_flutter_jni_class->obj(), "createOverlaySurface",
"()Lio/flutter/embedding/engine/FlutterOverlaySurface;");
if (g_create_overlay_surface_method == nullptr) {
FML_LOG(ERROR) << "Could not locate createOverlaySurface method";
return false;
}
g_destroy_overlay_surfaces_method = env->GetMethodID(
g_flutter_jni_class->obj(), "destroyOverlaySurfaces", "()V");
if (g_destroy_overlay_surfaces_method == nullptr) {
FML_LOG(ERROR) << "Could not locate destroyOverlaySurfaces method";
return false;
}
// new platform views
g_create_transaction_method =
env->GetMethodID(g_flutter_jni_class->obj(), "createTransaction",
"()Landroid/view/SurfaceControl$Transaction;");
if (g_create_transaction_method == nullptr) {
FML_LOG(ERROR) << "Could not locate createTransaction method";
return false;
}
g_swap_transaction_method =
env->GetMethodID(g_flutter_jni_class->obj(), "swapTransactions", "()V");
if (g_swap_transaction_method == nullptr) {
FML_LOG(ERROR) << "Could not locate swapTransactions method";
return false;
}
g_apply_transaction_method =
env->GetMethodID(g_flutter_jni_class->obj(), "applyTransactions", "()V");
if (g_apply_transaction_method == nullptr) {
FML_LOG(ERROR) << "Could not locate applyTransactions method";
return false;
}
g_create_overlay_surface2_method =
env->GetMethodID(g_flutter_jni_class->obj(), "createOverlaySurface2",
"()Lio/flutter/embedding/engine/FlutterOverlaySurface;");
if (g_create_overlay_surface2_method == nullptr) {
FML_LOG(ERROR) << "Could not locate createOverlaySurface2 method";
return false;
}
g_destroy_overlay_surface2_method = env->GetMethodID(
g_flutter_jni_class->obj(), "destroyOverlaySurface2", "()V");
if (g_destroy_overlay_surface2_method == nullptr) {
FML_LOG(ERROR) << "Could not locate destroyOverlaySurface2 method";
return false;
}
g_on_display_platform_view2_method =
env->GetMethodID(g_flutter_jni_class->obj(), "onDisplayPlatformView2",
"(IIIIIIILio/flutter/embedding/engine/mutatorsstack/"
"FlutterMutatorsStack;)V");
if (g_on_display_platform_view2_method == nullptr) {
FML_LOG(ERROR) << "Could not locate onDisplayPlatformView2 method";
return false;
}
g_on_end_frame2_method =
env->GetMethodID(g_flutter_jni_class->obj(), "endFrame2", "()V");
if (g_on_end_frame2_method == nullptr) {
FML_LOG(ERROR) << "Could not locate onEndFrame2 method";
return false;
}
g_show_overlay_surface2_method = env->GetMethodID(
g_flutter_jni_class->obj(), "showOverlaySurface2", "()V");
if (g_on_end_frame2_method == nullptr) {
FML_LOG(ERROR) << "Could not locate showOverlaySurface2 method";
return false;
}
g_hide_overlay_surface2_method = env->GetMethodID(
g_flutter_jni_class->obj(), "hideOverlaySurface2", "()V");
if (g_on_end_frame2_method == nullptr) {
FML_LOG(ERROR) << "Could not locate hideOverlaySurface2 method";
return false;
}
//
FLUTTER_FOR_EACH_JNI_METHOD(FLUTTER_BIND_JNI)
fml::jni::ScopedJavaLocalRef<jclass> overlay_surface_class(
env, env->FindClass("io/flutter/embedding/engine/FlutterOverlaySurface"));
@ -1200,40 +1044,6 @@ bool PlatformViewAndroid::Register(JNIEnv* env) {
return false;
}
g_on_display_platform_view_method =
env->GetMethodID(g_flutter_jni_class->obj(), "onDisplayPlatformView",
"(IIIIIIILio/flutter/embedding/engine/mutatorsstack/"
"FlutterMutatorsStack;)V");
if (g_on_display_platform_view_method == nullptr) {
FML_LOG(ERROR) << "Could not locate onDisplayPlatformView method";
return false;
}
g_on_begin_frame_method =
env->GetMethodID(g_flutter_jni_class->obj(), "onBeginFrame", "()V");
if (g_on_begin_frame_method == nullptr) {
FML_LOG(ERROR) << "Could not locate onBeginFrame method";
return false;
}
g_on_end_frame_method =
env->GetMethodID(g_flutter_jni_class->obj(), "onEndFrame", "()V");
if (g_on_end_frame_method == nullptr) {
FML_LOG(ERROR) << "Could not locate onEndFrame method";
return false;
}
g_on_display_overlay_surface_method = env->GetMethodID(
g_flutter_jni_class->obj(), "onDisplayOverlaySurface", "(IIIII)V");
if (g_on_display_overlay_surface_method == nullptr) {
FML_LOG(ERROR) << "Could not locate onDisplayOverlaySurface method";
return false;
}
g_java_weak_reference_class = new fml::jni::ScopedJavaGlobalRef<jclass>(
env, env->FindClass("java/lang/ref/WeakReference"));
if (g_java_weak_reference_class->is_null()) {