Skip to content

Commit e7d167c

Browse files
author
Jonah Williams
authored
[android] use macro definition to shrink repetitive JNI code size. (#163395)
But less code!
1 parent ec01644 commit e7d167c

1 file changed

Lines changed: 54 additions & 244 deletions

File tree

engine/src/flutter/shell/platform/android/platform_view_android_jni_impl.cc

Lines changed: 54 additions & 244 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,12 @@
99
#include <dlfcn.h>
1010
#include <jni.h>
1111
#include <memory>
12-
#include <sstream>
1312
#include <utility>
1413

1514
#include "impeller/toolkit/android/shadow_realm.h"
16-
#include "include/android/SkImageAndroid.h"
1715
#include "unicode/uchar.h"
1816

19-
#include "flutter/assets/directory_asset_bundle.h"
2017
#include "flutter/common/constants.h"
21-
#include "flutter/fml/file.h"
2218
#include "flutter/fml/mapping.h"
2319
#include "flutter/fml/native_library.h"
2420
#include "flutter/fml/platform/android/jni_util.h"
@@ -66,45 +62,63 @@ static fml::jni::ScopedJavaGlobalRef<jclass>* g_bitmap_config_class = nullptr;
6662
// Called By Native
6763

6864
static jmethodID g_flutter_callback_info_constructor = nullptr;
69-
jobject CreateFlutterCallbackInformation(
70-
JNIEnv* env,
71-
const std::string& callbackName,
72-
const std::string& callbackClassName,
73-
const std::string& callbackLibraryPath) {
74-
return env->NewObject(g_flutter_callback_info_class->obj(),
75-
g_flutter_callback_info_constructor,
76-
env->NewStringUTF(callbackName.c_str()),
77-
env->NewStringUTF(callbackClassName.c_str()),
78-
env->NewStringUTF(callbackLibraryPath.c_str()));
79-
}
8065

8166
static jfieldID g_jni_shell_holder_field = nullptr;
8267

83-
static jmethodID g_jni_constructor = nullptr;
84-
85-
static jmethodID g_long_constructor = nullptr;
86-
87-
static jmethodID g_handle_platform_message_method = nullptr;
68+
#define FLUTTER_FOR_EACH_JNI_METHOD(V) \
69+
V(g_handle_platform_message_method, handlePlatformMessage, \
70+
"(Ljava/lang/String;Ljava/nio/ByteBuffer;IJ)V") \
71+
V(g_handle_platform_message_response_method, handlePlatformMessageResponse, \
72+
"(ILjava/nio/ByteBuffer;)V") \
73+
V(g_update_semantics_method, updateSemantics, \
74+
"(Ljava/nio/ByteBuffer;[Ljava/lang/String;[Ljava/nio/ByteBuffer;)V") \
75+
V(g_on_display_platform_view_method, onDisplayPlatformView, \
76+
"(IIIIIIILio/flutter/embedding/engine/mutatorsstack/" \
77+
"FlutterMutatorsStack;)V") \
78+
V(g_on_begin_frame_method, onBeginFrame, "()V") \
79+
V(g_on_end_frame_method, onEndFrame, "()V") \
80+
V(g_on_display_overlay_surface_method, onDisplayOverlaySurface, "(IIIII)V") \
81+
V(g_create_transaction_method, createTransaction, \
82+
"()Landroid/view/SurfaceControl$Transaction;") \
83+
V(g_swap_transaction_method, swapTransactions, "()V") \
84+
V(g_apply_transaction_method, applyTransactions, "()V") \
85+
V(g_create_overlay_surface2_method, createOverlaySurface2, \
86+
"()Lio/flutter/embedding/engine/FlutterOverlaySurface;") \
87+
V(g_destroy_overlay_surface2_method, destroyOverlaySurface2, "()V") \
88+
V(g_on_display_platform_view2_method, onDisplayPlatformView2, \
89+
"(IIIIIIILio/flutter/embedding/engine/mutatorsstack/" \
90+
"FlutterMutatorsStack;)V") \
91+
V(g_on_end_frame2_method, endFrame2, "()V") \
92+
V(g_show_overlay_surface2_method, showOverlaySurface2, "()V") \
93+
V(g_hide_overlay_surface2_method, hideOverlaySurface2, "()V") \
94+
V(g_get_scaled_font_size_method, getScaledFontSize, "(FI)F") \
95+
V(g_update_custom_accessibility_actions_method, \
96+
updateCustomAccessibilityActions, \
97+
"(Ljava/nio/ByteBuffer;[Ljava/lang/String;)V") \
98+
V(g_on_first_frame_method, onFirstFrame, "()V") \
99+
V(g_on_engine_restart_method, onPreEngineRestart, "()V") \
100+
V(g_create_overlay_surface_method, createOverlaySurface, \
101+
"()Lio/flutter/embedding/engine/FlutterOverlaySurface;") \
102+
V(g_destroy_overlay_surfaces_method, destroyOverlaySurfaces, "()V")
88103

89-
static jmethodID g_handle_platform_message_response_method = nullptr;
90-
91-
static jmethodID g_update_semantics_method = nullptr;
92-
93-
static jmethodID g_update_custom_accessibility_actions_method = nullptr;
94-
95-
static jmethodID g_get_scaled_font_size_method = nullptr;
96-
97-
static jmethodID g_on_first_frame_method = nullptr;
104+
//
98105

99-
static jmethodID g_on_engine_restart_method = nullptr;
106+
#define FLUTTER_DECLARE_JNI(global_field, jni_name, jni_arg) \
107+
static jmethodID global_field = nullptr;
100108

101-
static jmethodID g_create_overlay_surface_method = nullptr;
109+
#define FLUTTER_BIND_JNI(global_field, jni_name, jni_arg) \
110+
global_field = \
111+
env->GetMethodID(g_flutter_jni_class->obj(), #jni_name, jni_arg); \
112+
if (global_field == nullptr) { \
113+
FML_LOG(ERROR) << "Could not locate " << #jni_name << " method."; \
114+
return false; \
115+
}
102116

103-
static jmethodID g_destroy_overlay_surfaces_method = nullptr;
117+
static jmethodID g_jni_constructor = nullptr;
104118

105-
static jmethodID g_on_begin_frame_method = nullptr;
119+
static jmethodID g_long_constructor = nullptr;
106120

107-
static jmethodID g_on_end_frame_method = nullptr;
121+
FLUTTER_FOR_EACH_JNI_METHOD(FLUTTER_DECLARE_JNI)
108122

109123
static jmethodID g_java_weak_reference_get_method = nullptr;
110124

@@ -131,9 +145,6 @@ static jmethodID g_compute_platform_resolved_locale_method = nullptr;
131145
static jmethodID g_request_dart_deferred_library_method = nullptr;
132146

133147
// Called By Java
134-
static jmethodID g_on_display_platform_view_method = nullptr;
135-
136-
static jmethodID g_on_display_overlay_surface_method = nullptr;
137148

138149
static jmethodID g_overlay_surface_id_method = nullptr;
139150

@@ -145,25 +156,6 @@ static jmethodID g_bitmap_copy_pixels_from_buffer_method = nullptr;
145156

146157
static jmethodID g_bitmap_config_value_of = nullptr;
147158

148-
// New platform Views
149-
static jmethodID g_create_transaction_method = nullptr;
150-
151-
static jmethodID g_swap_transaction_method = nullptr;
152-
153-
static jmethodID g_apply_transaction_method = nullptr;
154-
155-
static jmethodID g_create_overlay_surface2_method = nullptr;
156-
157-
static jmethodID g_destroy_overlay_surface2_method = nullptr;
158-
159-
static jmethodID g_on_display_platform_view2_method = nullptr;
160-
161-
static jmethodID g_on_end_frame2_method = nullptr;
162-
163-
static jmethodID g_show_overlay_surface2_method = nullptr;
164-
165-
static jmethodID g_hide_overlay_surface2_method = nullptr;
166-
167159
// Mutators
168160
static fml::jni::ScopedJavaGlobalRef<jclass>* g_mutators_stack_class = nullptr;
169161
static jmethodID g_mutators_stack_init_method = nullptr;
@@ -310,8 +302,11 @@ static jobject LookupCallbackInformation(JNIEnv* env,
310302
if (cbInfo == nullptr) {
311303
return nullptr;
312304
}
313-
return CreateFlutterCallbackInformation(env, cbInfo->name, cbInfo->class_name,
314-
cbInfo->library_path);
305+
return env->NewObject(g_flutter_callback_info_class->obj(),
306+
g_flutter_callback_info_constructor,
307+
env->NewStringUTF(cbInfo->name.c_str()),
308+
env->NewStringUTF(cbInfo->class_name.c_str()),
309+
env->NewStringUTF(cbInfo->library_path.c_str()));
315310
}
316311

317312
static void SetViewportMetrics(JNIEnv* env,
@@ -919,158 +914,7 @@ bool RegisterApi(JNIEnv* env) {
919914
return false;
920915
}
921916

922-
g_handle_platform_message_method =
923-
env->GetMethodID(g_flutter_jni_class->obj(), "handlePlatformMessage",
924-
"(Ljava/lang/String;Ljava/nio/ByteBuffer;IJ)V");
925-
926-
if (g_handle_platform_message_method == nullptr) {
927-
FML_LOG(ERROR) << "Could not locate handlePlatformMessage method";
928-
return false;
929-
}
930-
931-
g_handle_platform_message_response_method = env->GetMethodID(
932-
g_flutter_jni_class->obj(), "handlePlatformMessageResponse",
933-
"(ILjava/nio/ByteBuffer;)V");
934-
935-
if (g_handle_platform_message_response_method == nullptr) {
936-
FML_LOG(ERROR) << "Could not locate handlePlatformMessageResponse method";
937-
return false;
938-
}
939-
940-
g_get_scaled_font_size_method = env->GetMethodID(
941-
g_flutter_jni_class->obj(), "getScaledFontSize", "(FI)F");
942-
943-
if (g_get_scaled_font_size_method == nullptr) {
944-
FML_LOG(ERROR) << "Could not locate FlutterJNI#getScaledFontSize method";
945-
return false;
946-
}
947-
948-
g_update_semantics_method = env->GetMethodID(
949-
g_flutter_jni_class->obj(), "updateSemantics",
950-
"(Ljava/nio/ByteBuffer;[Ljava/lang/String;[Ljava/nio/ByteBuffer;)V");
951-
952-
if (g_update_semantics_method == nullptr) {
953-
FML_LOG(ERROR) << "Could not locate updateSemantics method";
954-
return false;
955-
}
956-
957-
g_update_custom_accessibility_actions_method = env->GetMethodID(
958-
g_flutter_jni_class->obj(), "updateCustomAccessibilityActions",
959-
"(Ljava/nio/ByteBuffer;[Ljava/lang/String;)V");
960-
961-
if (g_update_custom_accessibility_actions_method == nullptr) {
962-
FML_LOG(ERROR)
963-
<< "Could not locate updateCustomAccessibilityActions method";
964-
return false;
965-
}
966-
967-
g_on_first_frame_method =
968-
env->GetMethodID(g_flutter_jni_class->obj(), "onFirstFrame", "()V");
969-
970-
if (g_on_first_frame_method == nullptr) {
971-
FML_LOG(ERROR) << "Could not locate onFirstFrame method";
972-
return false;
973-
}
974-
975-
g_on_engine_restart_method =
976-
env->GetMethodID(g_flutter_jni_class->obj(), "onPreEngineRestart", "()V");
977-
978-
if (g_on_engine_restart_method == nullptr) {
979-
FML_LOG(ERROR) << "Could not locate onEngineRestart method";
980-
return false;
981-
}
982-
983-
g_create_overlay_surface_method =
984-
env->GetMethodID(g_flutter_jni_class->obj(), "createOverlaySurface",
985-
"()Lio/flutter/embedding/engine/FlutterOverlaySurface;");
986-
987-
if (g_create_overlay_surface_method == nullptr) {
988-
FML_LOG(ERROR) << "Could not locate createOverlaySurface method";
989-
return false;
990-
}
991-
992-
g_destroy_overlay_surfaces_method = env->GetMethodID(
993-
g_flutter_jni_class->obj(), "destroyOverlaySurfaces", "()V");
994-
995-
if (g_destroy_overlay_surfaces_method == nullptr) {
996-
FML_LOG(ERROR) << "Could not locate destroyOverlaySurfaces method";
997-
return false;
998-
}
999-
1000-
// new platform views
1001-
g_create_transaction_method =
1002-
env->GetMethodID(g_flutter_jni_class->obj(), "createTransaction",
1003-
"()Landroid/view/SurfaceControl$Transaction;");
1004-
1005-
if (g_create_transaction_method == nullptr) {
1006-
FML_LOG(ERROR) << "Could not locate createTransaction method";
1007-
return false;
1008-
}
1009-
1010-
g_swap_transaction_method =
1011-
env->GetMethodID(g_flutter_jni_class->obj(), "swapTransactions", "()V");
1012-
1013-
if (g_swap_transaction_method == nullptr) {
1014-
FML_LOG(ERROR) << "Could not locate swapTransactions method";
1015-
return false;
1016-
}
1017-
1018-
g_apply_transaction_method =
1019-
env->GetMethodID(g_flutter_jni_class->obj(), "applyTransactions", "()V");
1020-
1021-
if (g_apply_transaction_method == nullptr) {
1022-
FML_LOG(ERROR) << "Could not locate applyTransactions method";
1023-
return false;
1024-
}
1025-
1026-
g_create_overlay_surface2_method =
1027-
env->GetMethodID(g_flutter_jni_class->obj(), "createOverlaySurface2",
1028-
"()Lio/flutter/embedding/engine/FlutterOverlaySurface;");
1029-
1030-
if (g_create_overlay_surface2_method == nullptr) {
1031-
FML_LOG(ERROR) << "Could not locate createOverlaySurface2 method";
1032-
return false;
1033-
}
1034-
1035-
g_destroy_overlay_surface2_method = env->GetMethodID(
1036-
g_flutter_jni_class->obj(), "destroyOverlaySurface2", "()V");
1037-
1038-
if (g_destroy_overlay_surface2_method == nullptr) {
1039-
FML_LOG(ERROR) << "Could not locate destroyOverlaySurface2 method";
1040-
return false;
1041-
}
1042-
1043-
g_on_display_platform_view2_method =
1044-
env->GetMethodID(g_flutter_jni_class->obj(), "onDisplayPlatformView2",
1045-
"(IIIIIIILio/flutter/embedding/engine/mutatorsstack/"
1046-
"FlutterMutatorsStack;)V");
1047-
1048-
if (g_on_display_platform_view2_method == nullptr) {
1049-
FML_LOG(ERROR) << "Could not locate onDisplayPlatformView2 method";
1050-
return false;
1051-
}
1052-
1053-
g_on_end_frame2_method =
1054-
env->GetMethodID(g_flutter_jni_class->obj(), "endFrame2", "()V");
1055-
if (g_on_end_frame2_method == nullptr) {
1056-
FML_LOG(ERROR) << "Could not locate onEndFrame2 method";
1057-
return false;
1058-
}
1059-
1060-
g_show_overlay_surface2_method = env->GetMethodID(
1061-
g_flutter_jni_class->obj(), "showOverlaySurface2", "()V");
1062-
if (g_on_end_frame2_method == nullptr) {
1063-
FML_LOG(ERROR) << "Could not locate showOverlaySurface2 method";
1064-
return false;
1065-
}
1066-
1067-
g_hide_overlay_surface2_method = env->GetMethodID(
1068-
g_flutter_jni_class->obj(), "hideOverlaySurface2", "()V");
1069-
if (g_on_end_frame2_method == nullptr) {
1070-
FML_LOG(ERROR) << "Could not locate hideOverlaySurface2 method";
1071-
return false;
1072-
}
1073-
//
917+
FLUTTER_FOR_EACH_JNI_METHOD(FLUTTER_BIND_JNI)
1074918

1075919
fml::jni::ScopedJavaLocalRef<jclass> overlay_surface_class(
1076920
env, env->FindClass("io/flutter/embedding/engine/FlutterOverlaySurface"));
@@ -1200,40 +1044,6 @@ bool PlatformViewAndroid::Register(JNIEnv* env) {
12001044
return false;
12011045
}
12021046

1203-
g_on_display_platform_view_method =
1204-
env->GetMethodID(g_flutter_jni_class->obj(), "onDisplayPlatformView",
1205-
"(IIIIIIILio/flutter/embedding/engine/mutatorsstack/"
1206-
"FlutterMutatorsStack;)V");
1207-
1208-
if (g_on_display_platform_view_method == nullptr) {
1209-
FML_LOG(ERROR) << "Could not locate onDisplayPlatformView method";
1210-
return false;
1211-
}
1212-
1213-
g_on_begin_frame_method =
1214-
env->GetMethodID(g_flutter_jni_class->obj(), "onBeginFrame", "()V");
1215-
1216-
if (g_on_begin_frame_method == nullptr) {
1217-
FML_LOG(ERROR) << "Could not locate onBeginFrame method";
1218-
return false;
1219-
}
1220-
1221-
g_on_end_frame_method =
1222-
env->GetMethodID(g_flutter_jni_class->obj(), "onEndFrame", "()V");
1223-
1224-
if (g_on_end_frame_method == nullptr) {
1225-
FML_LOG(ERROR) << "Could not locate onEndFrame method";
1226-
return false;
1227-
}
1228-
1229-
g_on_display_overlay_surface_method = env->GetMethodID(
1230-
g_flutter_jni_class->obj(), "onDisplayOverlaySurface", "(IIIII)V");
1231-
1232-
if (g_on_display_overlay_surface_method == nullptr) {
1233-
FML_LOG(ERROR) << "Could not locate onDisplayOverlaySurface method";
1234-
return false;
1235-
}
1236-
12371047
g_java_weak_reference_class = new fml::jni::ScopedJavaGlobalRef<jclass>(
12381048
env, env->FindClass("java/lang/ref/WeakReference"));
12391049
if (g_java_weak_reference_class->is_null()) {

0 commit comments

Comments
 (0)