Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
platform_view_android_jni_impl.cc File Reference

Go to the source code of this file.

Namespaces

namespace  flutter
 

Macros

#define ANDROID_SHELL_HOLDER    (reinterpret_cast<AndroidShellHolder*>(shell_holder))
 
#define FLUTTER_FOR_EACH_JNI_METHOD(V)
 
#define FLUTTER_DECLARE_JNI(global_field, jni_name, jni_arg)    static jmethodID global_field = nullptr;
 *end#*‍/
 
#define FLUTTER_BIND_JNI(global_field, jni_name, jni_arg)
 

Functions

static jlong flutter::AttachJNI (JNIEnv *env, jclass clazz, jobject flutterJNI)
 
static void flutter::DestroyJNI (JNIEnv *env, jobject jcaller, jlong shell_holder)
 
static jobject flutter::SpawnJNI (JNIEnv *env, jobject jcaller, jlong shell_holder, jstring jEntrypoint, jstring jLibraryUrl, jstring jInitialRoute, jobject jEntrypointArgs, jlong engineId)
 
static void flutter::SurfaceCreated (JNIEnv *env, jobject jcaller, jlong shell_holder, jobject jsurface)
 
static void flutter::SurfaceWindowChanged (JNIEnv *env, jobject jcaller, jlong shell_holder, jobject jsurface)
 
static void flutter::SurfaceChanged (JNIEnv *env, jobject jcaller, jlong shell_holder, jint width, jint height)
 
static void flutter::SurfaceDestroyed (JNIEnv *env, jobject jcaller, jlong shell_holder)
 
static void flutter::RunBundleAndSnapshotFromLibrary (JNIEnv *env, jobject jcaller, jlong shell_holder, jstring jBundlePath, jstring jEntrypoint, jstring jLibraryUrl, jobject jAssetManager, jobject jEntrypointArgs, jlong engineId)
 
static jobject flutter::LookupCallbackInformation (JNIEnv *env, jobject, jlong handle)
 
static void flutter::SetViewportMetrics (JNIEnv *env, jobject jcaller, jlong shell_holder, jfloat devicePixelRatio, jint physicalWidth, jint physicalHeight, jint physicalPaddingTop, jint physicalPaddingRight, jint physicalPaddingBottom, jint physicalPaddingLeft, jint physicalViewInsetTop, jint physicalViewInsetRight, jint physicalViewInsetBottom, jint physicalViewInsetLeft, jint systemGestureInsetTop, jint systemGestureInsetRight, jint systemGestureInsetBottom, jint systemGestureInsetLeft, jint physicalTouchSlop, jintArray javaDisplayFeaturesBounds, jintArray javaDisplayFeaturesType, jintArray javaDisplayFeaturesState, jint physicalMinWidth, jint physicalMaxWidth, jint physicalMinHeight, jint physicalMaxHeight, jint physicalDisplayCornerRadiusTopLeft, jint physicalDisplayCornerRadiusTopRight, jint physicalDisplayCornerRadiusBottomRight, jint physicalDisplayCornerRadiusBottomLeft)
 
static void flutter::UpdateDisplayMetrics (JNIEnv *env, jobject jcaller, jlong shell_holder)
 
static bool flutter::IsSurfaceControlEnabled (JNIEnv *env, jobject jcaller, jlong shell_holder)
 
static jobject flutter::GetBitmap (JNIEnv *env, jobject jcaller, jlong shell_holder)
 
static void flutter::DispatchPlatformMessage (JNIEnv *env, jobject jcaller, jlong shell_holder, jstring channel, jobject message, jint position, jint responseId)
 
static void flutter::DispatchEmptyPlatformMessage (JNIEnv *env, jobject jcaller, jlong shell_holder, jstring channel, jint responseId)
 
static void flutter::CleanupMessageData (JNIEnv *env, jobject jcaller, jlong message_data)
 
static void flutter::DispatchPointerDataPacket (JNIEnv *env, jobject jcaller, jlong shell_holder, jobject buffer, jint position)
 
static void flutter::DispatchSemanticsAction (JNIEnv *env, jobject jcaller, jlong shell_holder, jint id, jint action, jobject args, jint args_position)
 
static void flutter::SetSemanticsEnabled (JNIEnv *env, jobject jcaller, jlong shell_holder, jboolean enabled)
 
static void flutter::SetAccessibilityFeatures (JNIEnv *env, jobject jcaller, jlong shell_holder, jint flags)
 
static jboolean flutter::GetIsSoftwareRendering (JNIEnv *env, jobject jcaller)
 
static void flutter::RegisterTexture (JNIEnv *env, jobject jcaller, jlong shell_holder, jlong texture_id, jobject surface_texture)
 
static void flutter::RegisterImageTexture (JNIEnv *env, jobject jcaller, jlong shell_holder, jlong texture_id, jobject image_texture_entry, jboolean reset_on_background)
 
static void flutter::UnregisterTexture (JNIEnv *env, jobject jcaller, jlong shell_holder, jlong texture_id)
 
static void flutter::MarkTextureFrameAvailable (JNIEnv *env, jobject jcaller, jlong shell_holder, jlong texture_id)
 
static void flutter::ScheduleFrame (JNIEnv *env, jobject jcaller, jlong shell_holder)
 
static void flutter::InvokePlatformMessageResponseCallback (JNIEnv *env, jobject jcaller, jlong shell_holder, jint responseId, jobject message, jint position)
 
static void flutter::InvokePlatformMessageEmptyResponseCallback (JNIEnv *env, jobject jcaller, jlong shell_holder, jint responseId)
 
static void flutter::NotifyLowMemoryWarning (JNIEnv *env, jobject obj, jlong shell_holder)
 
static jboolean flutter::FlutterTextUtilsIsEmoji (JNIEnv *env, jobject obj, jint codePoint)
 
static jboolean flutter::FlutterTextUtilsIsEmojiModifier (JNIEnv *env, jobject obj, jint codePoint)
 
static jboolean flutter::FlutterTextUtilsIsEmojiModifierBase (JNIEnv *env, jobject obj, jint codePoint)
 
static jboolean flutter::FlutterTextUtilsIsVariationSelector (JNIEnv *env, jobject obj, jint codePoint)
 
static jboolean flutter::FlutterTextUtilsIsRegionalIndicator (JNIEnv *env, jobject obj, jint codePoint)
 
static void flutter::LoadLoadingUnitFailure (intptr_t loading_unit_id, const std::string &message, bool transient)
 
static void flutter::DeferredComponentInstallFailure (JNIEnv *env, jobject obj, jint jLoadingUnitId, jstring jError, jboolean jTransient)
 
static void flutter::LoadDartDeferredLibrary (JNIEnv *env, jobject obj, jlong shell_holder, jint jLoadingUnitId, jobjectArray jSearchPaths)
 
static void flutter::UpdateJavaAssetManager (JNIEnv *env, jobject obj, jlong shell_holder, jobject jAssetManager, jstring jAssetBundlePath)
 
bool flutter::RegisterApi (JNIEnv *env)
 

Variables

static fml::jni::ScopedJavaGlobalRef< jclass > * flutter::g_flutter_callback_info_class
 
static fml::jni::ScopedJavaGlobalRef< jclass > * flutter::g_flutter_jni_class = nullptr
 
static fml::jni::ScopedJavaGlobalRef< jclass > * flutter::g_java_weak_reference_class
 
static fml::jni::ScopedJavaGlobalRef< jclass > * flutter::g_texture_wrapper_class = nullptr
 
static fml::jni::ScopedJavaGlobalRef< jclass > * flutter::g_image_consumer_texture_registry_interface = nullptr
 
static fml::jni::ScopedJavaGlobalRef< jclass > * flutter::g_image_class = nullptr
 
static fml::jni::ScopedJavaGlobalRef< jclass > * flutter::g_hardware_buffer_class = nullptr
 
static fml::jni::ScopedJavaGlobalRef< jclass > * flutter::g_java_long_class = nullptr
 
static fml::jni::ScopedJavaGlobalRef< jclass > * flutter::g_bitmap_class = nullptr
 
static fml::jni::ScopedJavaGlobalRef< jclass > * flutter::g_bitmap_config_class = nullptr
 
static jmethodID flutter::g_flutter_callback_info_constructor = nullptr
 
static jfieldID flutter::g_jni_shell_holder_field = nullptr
 
static jmethodID flutter::g_jni_constructor = nullptr
 
static jmethodID flutter::g_long_constructor = nullptr
 
static jmethodID flutter::g_java_weak_reference_get_method = nullptr
 
static jmethodID flutter::g_attach_to_gl_context_method = nullptr
 
static jmethodID flutter::g_surface_texture_wrapper_should_update = nullptr
 
static jmethodID flutter::g_update_tex_image_method = nullptr
 
static jmethodID flutter::g_get_transform_matrix_method = nullptr
 
static jmethodID flutter::g_detach_from_gl_context_method = nullptr
 
static jmethodID flutter::g_acquire_latest_image_method = nullptr
 
static jmethodID flutter::g_image_get_hardware_buffer_method = nullptr
 
static jmethodID flutter::g_image_close_method = nullptr
 
static jmethodID flutter::g_hardware_buffer_close_method = nullptr
 
static jmethodID flutter::g_compute_platform_resolved_locale_method = nullptr
 
static jmethodID flutter::g_request_dart_deferred_library_method = nullptr
 
static jmethodID flutter::g_overlay_surface_id_method = nullptr
 
static jmethodID flutter::g_overlay_surface_surface_method = nullptr
 
static jmethodID flutter::g_bitmap_create_bitmap_method = nullptr
 
static jmethodID flutter::g_bitmap_copy_pixels_from_buffer_method = nullptr
 
static jmethodID flutter::g_bitmap_config_value_of = nullptr
 
static fml::jni::ScopedJavaGlobalRef< jclass > * flutter::g_mutators_stack_class = nullptr
 
static jmethodID flutter::g_mutators_stack_init_method = nullptr
 
static jmethodID flutter::g_mutators_stack_push_transform_method = nullptr
 
static jmethodID flutter::g_mutators_stack_push_cliprect_method = nullptr
 
static jmethodID flutter::g_mutators_stack_push_cliprrect_method = nullptr
 
static jmethodID flutter::g_mutators_stack_push_opacity_method = nullptr
 
static jmethodID flutter::g_mutators_stack_push_clippath_method = nullptr
 
static fml::jni::ScopedJavaGlobalRef< jclass > * flutter::path_class = nullptr
 
static jmethodID flutter::path_constructor = nullptr
 
static jmethodID flutter::path_move_to_method = nullptr
 
static jmethodID flutter::path_line_to_method = nullptr
 
static jmethodID flutter::path_quad_to_method = nullptr
 
static jmethodID flutter::path_cubic_to_method = nullptr
 
static jmethodID flutter::path_conic_to_method = nullptr
 
static jmethodID flutter::path_close_method = nullptr
 
static jmethodID flutter::path_set_fill_type_method = nullptr
 
static fml::jni::ScopedJavaGlobalRef< jclass > * flutter::g_path_fill_type_class = nullptr
 
static jfieldID flutter::g_path_fill_type_winding_field = nullptr
 
static jfieldID flutter::g_path_fill_type_even_odd_field = nullptr
 

Macro Definition Documentation

◆ ANDROID_SHELL_HOLDER

#define ANDROID_SHELL_HOLDER    (reinterpret_cast<AndroidShellHolder*>(shell_holder))

Definition at line 31 of file platform_view_android_jni_impl.cc.

33 {
34
36 nullptr;
37
39
41 nullptr;
42
44
47
49
51
53
55
57
58// Called By Native
59
60static jmethodID g_flutter_callback_info_constructor = nullptr;
61
62static jfieldID g_jni_shell_holder_field = nullptr;
63
64#define FLUTTER_FOR_EACH_JNI_METHOD(V) \
65 V(g_handle_platform_message_method, handlePlatformMessage, \
66 "(Ljava/lang/String;Ljava/nio/ByteBuffer;IJ)V") \
67 V(g_handle_platform_message_response_method, handlePlatformMessageResponse, \
68 "(ILjava/nio/ByteBuffer;)V") \
69 V(g_update_semantics_method, updateSemantics, \
70 "(Ljava/nio/ByteBuffer;[Ljava/lang/String;[Ljava/nio/ByteBuffer;)V") \
71 V(g_set_application_locale_method, setApplicationLocale, \
72 "(Ljava/lang/String;)V") \
73 V(g_set_semantics_tree_enabled_method, setSemanticsTreeEnabled, "(Z)V") \
74 V(g_on_display_platform_view_method, onDisplayPlatformView, \
75 "(IIIIIIILio/flutter/embedding/engine/mutatorsstack/" \
76 "FlutterMutatorsStack;)V") \
77 V(g_on_begin_frame_method, onBeginFrame, "()V") \
78 V(g_on_end_frame_method, onEndFrame, "()V") \
79 V(g_on_display_overlay_surface_method, onDisplayOverlaySurface, "(IIIII)V") \
80 V(g_create_transaction_method, createTransaction, \
81 "()Landroid/view/SurfaceControl$Transaction;") \
82 V(g_swap_transaction_method, swapTransactions, "()V") \
83 V(g_apply_transaction_method, applyTransactions, "()V") \
84 V(g_create_overlay_surface2_method, createOverlaySurface2, \
85 "()Lio/flutter/embedding/engine/FlutterOverlaySurface;") \
86 V(g_destroy_overlay_surface2_method, destroyOverlaySurface2, "()V") \
87 V(g_on_display_platform_view2_method, onDisplayPlatformView2, \
88 "(IIIIIIILio/flutter/embedding/engine/mutatorsstack/" \
89 "FlutterMutatorsStack;)V") \
90 V(g_hide_platform_view2_method, hidePlatformView2, "(I)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") \
103 V(g_maybe_resize_surface_view, maybeResizeSurfaceView, "(II)V") \
104 //
105
106#define FLUTTER_DECLARE_JNI(global_field, jni_name, jni_arg) \
107 static jmethodID global_field = nullptr;
108
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 }
116
117static jmethodID g_jni_constructor = nullptr;
118
119static jmethodID g_long_constructor = nullptr;
120
122
123static jmethodID g_java_weak_reference_get_method = nullptr;
124
125static jmethodID g_attach_to_gl_context_method = nullptr;
126
127static jmethodID g_surface_texture_wrapper_should_update = nullptr;
128
129static jmethodID g_update_tex_image_method = nullptr;
130
131static jmethodID g_get_transform_matrix_method = nullptr;
132
133static jmethodID g_detach_from_gl_context_method = nullptr;
134
135static jmethodID g_acquire_latest_image_method = nullptr;
136
137static jmethodID g_image_get_hardware_buffer_method = nullptr;
138
139static jmethodID g_image_close_method = nullptr;
140
141static jmethodID g_hardware_buffer_close_method = nullptr;
142
143static jmethodID g_compute_platform_resolved_locale_method = nullptr;
144
145static jmethodID g_request_dart_deferred_library_method = nullptr;
146
147// Called By Java
148
149static jmethodID g_overlay_surface_id_method = nullptr;
150
151static jmethodID g_overlay_surface_surface_method = nullptr;
152
153static jmethodID g_bitmap_create_bitmap_method = nullptr;
154
155static jmethodID g_bitmap_copy_pixels_from_buffer_method = nullptr;
156
157static jmethodID g_bitmap_config_value_of = nullptr;
158
159// Mutators
161static jmethodID g_mutators_stack_init_method = nullptr;
162static jmethodID g_mutators_stack_push_transform_method = nullptr;
163static jmethodID g_mutators_stack_push_cliprect_method = nullptr;
164static jmethodID g_mutators_stack_push_cliprrect_method = nullptr;
165static jmethodID g_mutators_stack_push_opacity_method = nullptr;
166static jmethodID g_mutators_stack_push_clippath_method = nullptr;
167
168// android.graphics.Path class, methods, and nested classes.
170static jmethodID path_constructor = nullptr;
171static jmethodID path_move_to_method = nullptr;
172static jmethodID path_line_to_method = nullptr;
173static jmethodID path_quad_to_method = nullptr;
174static jmethodID path_cubic_to_method = nullptr;
175static jmethodID path_conic_to_method = nullptr;
176static jmethodID path_close_method = nullptr;
177static jmethodID path_set_fill_type_method = nullptr;
178
180static jfieldID g_path_fill_type_winding_field = nullptr;
181static jfieldID g_path_fill_type_even_odd_field = nullptr;
182
183// Called By Java
184static jlong AttachJNI(JNIEnv* env, jclass clazz, jobject flutterJNI) {
185 fml::jni::JavaObjectWeakGlobalRef java_object(env, flutterJNI);
186 std::shared_ptr<PlatformViewAndroidJNI> jni_facade =
187 std::make_shared<PlatformViewAndroidJNIImpl>(java_object);
188 auto shell_holder = std::make_unique<AndroidShellHolder>(
189 FlutterMain::Get().GetSettings(), jni_facade,
190 FlutterMain::Get().GetAndroidRenderingAPI());
191 if (shell_holder->IsValid()) {
192 return reinterpret_cast<jlong>(shell_holder.release());
193 } else {
194 return 0;
195 }
196}
197
198static void DestroyJNI(JNIEnv* env, jobject jcaller, jlong shell_holder) {
200}
201
202// Signature is similar to RunBundleAndSnapshotFromLibrary but it can't change
203// the bundle path or asset manager since we can only spawn with the same
204// AOT.
205//
206// The shell_holder instance must be a pointer address to the current
207// AndroidShellHolder whose Shell will be used to spawn a new Shell.
208//
209// This creates a Java Long that points to the newly created
210// AndroidShellHolder's raw pointer, connects that Long to a newly created
211// FlutterJNI instance, then returns the FlutterJNI instance.
212static jobject SpawnJNI(JNIEnv* env,
213 jobject jcaller,
214 jlong shell_holder,
215 jstring jEntrypoint,
216 jstring jLibraryUrl,
217 jstring jInitialRoute,
218 jobject jEntrypointArgs,
219 jlong engineId) {
220 jobject jni = env->NewObject(g_flutter_jni_class->obj(), g_jni_constructor);
221 if (jni == nullptr) {
222 FML_LOG(ERROR) << "Could not create a FlutterJNI instance";
223 return nullptr;
224 }
225
226 fml::jni::JavaObjectWeakGlobalRef java_jni(env, jni);
227 std::shared_ptr<PlatformViewAndroidJNI> jni_facade =
228 std::make_shared<PlatformViewAndroidJNIImpl>(java_jni);
229
230 auto entrypoint = fml::jni::JavaStringToString(env, jEntrypoint);
231 auto libraryUrl = fml::jni::JavaStringToString(env, jLibraryUrl);
232 auto initial_route = fml::jni::JavaStringToString(env, jInitialRoute);
233 auto entrypoint_args = fml::jni::StringListToVector(env, jEntrypointArgs);
234
235 auto spawned_shell_holder =
236 ANDROID_SHELL_HOLDER->Spawn(jni_facade, entrypoint, libraryUrl,
237 initial_route, entrypoint_args, engineId);
238
239 if (spawned_shell_holder == nullptr || !spawned_shell_holder->IsValid()) {
240 FML_LOG(ERROR) << "Could not spawn Shell";
241 return nullptr;
242 }
243
244 jobject javaLong = env->CallStaticObjectMethod(
245 g_java_long_class->obj(), g_long_constructor,
246 reinterpret_cast<jlong>(spawned_shell_holder.release()));
247 if (javaLong == nullptr) {
248 FML_LOG(ERROR) << "Could not create a Long instance";
249 return nullptr;
250 }
251
252 env->SetObjectField(jni, g_jni_shell_holder_field, javaLong);
253
254 return jni;
255}
256
257static void SurfaceCreated(JNIEnv* env,
258 jobject jcaller,
259 jlong shell_holder,
260 jobject jsurface) {
261 // Note: This frame ensures that any local references used by
262 // ANativeWindow_fromSurface are released immediately. This is needed as a
263 // workaround for https://code.google.com/p/android/issues/detail?id=68174
264 fml::jni::ScopedJavaLocalFrame scoped_local_reference_frame(env);
265 auto window = fml::MakeRefCounted<AndroidNativeWindow>(
266 ANativeWindow_fromSurface(env, jsurface));
267 ANDROID_SHELL_HOLDER->GetPlatformView()->NotifyCreated(std::move(window));
268}
269
270static void SurfaceWindowChanged(JNIEnv* env,
271 jobject jcaller,
272 jlong shell_holder,
273 jobject jsurface) {
274 // Note: This frame ensures that any local references used by
275 // ANativeWindow_fromSurface are released immediately. This is needed as a
276 // workaround for https://code.google.com/p/android/issues/detail?id=68174
277 fml::jni::ScopedJavaLocalFrame scoped_local_reference_frame(env);
278 auto window = fml::MakeRefCounted<AndroidNativeWindow>(
279 ANativeWindow_fromSurface(env, jsurface));
280 ANDROID_SHELL_HOLDER->GetPlatformView()->NotifySurfaceWindowChanged(
281 std::move(window));
282}
283
284static void SurfaceChanged(JNIEnv* env,
285 jobject jcaller,
286 jlong shell_holder,
287 jint width,
288 jint height) {
289 ANDROID_SHELL_HOLDER->GetPlatformView()->NotifyChanged(
291}
292
293static void SurfaceDestroyed(JNIEnv* env, jobject jcaller, jlong shell_holder) {
294 ANDROID_SHELL_HOLDER->GetPlatformView()->NotifyDestroyed();
295}
296
297static void RunBundleAndSnapshotFromLibrary(JNIEnv* env,
298 jobject jcaller,
299 jlong shell_holder,
300 jstring jBundlePath,
301 jstring jEntrypoint,
302 jstring jLibraryUrl,
303 jobject jAssetManager,
304 jobject jEntrypointArgs,
305 jlong engineId) {
306 auto apk_asset_provider = std::make_unique<flutter::APKAssetProvider>(
307 env, // jni environment
308 jAssetManager, // asset manager
309 fml::jni::JavaStringToString(env, jBundlePath) // apk asset dir
310 );
311 auto entrypoint = fml::jni::JavaStringToString(env, jEntrypoint);
312 auto libraryUrl = fml::jni::JavaStringToString(env, jLibraryUrl);
313 auto entrypoint_args = fml::jni::StringListToVector(env, jEntrypointArgs);
314
315 ANDROID_SHELL_HOLDER->Launch(std::move(apk_asset_provider), entrypoint,
316 libraryUrl, entrypoint_args, engineId);
317}
318
319static jobject LookupCallbackInformation(JNIEnv* env,
320 /* unused */ jobject,
321 jlong handle) {
323 if (cbInfo == nullptr) {
324 return nullptr;
325 }
326 return env->NewObject(g_flutter_callback_info_class->obj(),
327 g_flutter_callback_info_constructor,
328 env->NewStringUTF(cbInfo->name.c_str()),
329 env->NewStringUTF(cbInfo->class_name.c_str()),
330 env->NewStringUTF(cbInfo->library_path.c_str()));
331}
332
333static void SetViewportMetrics(JNIEnv* env,
334 jobject jcaller,
335 jlong shell_holder,
336 jfloat devicePixelRatio,
337 jint physicalWidth,
338 jint physicalHeight,
339 jint physicalPaddingTop,
340 jint physicalPaddingRight,
341 jint physicalPaddingBottom,
342 jint physicalPaddingLeft,
343 jint physicalViewInsetTop,
344 jint physicalViewInsetRight,
345 jint physicalViewInsetBottom,
346 jint physicalViewInsetLeft,
347 jint systemGestureInsetTop,
348 jint systemGestureInsetRight,
349 jint systemGestureInsetBottom,
350 jint systemGestureInsetLeft,
351 jint physicalTouchSlop,
352 jintArray javaDisplayFeaturesBounds,
353 jintArray javaDisplayFeaturesType,
354 jintArray javaDisplayFeaturesState,
355 jint physicalMinWidth,
356 jint physicalMaxWidth,
357 jint physicalMinHeight,
358 jint physicalMaxHeight,
359 jint physicalDisplayCornerRadiusTopLeft,
360 jint physicalDisplayCornerRadiusTopRight,
361 jint physicalDisplayCornerRadiusBottomRight,
362 jint physicalDisplayCornerRadiusBottomLeft) {
363 // Convert java->c++. javaDisplayFeaturesBounds, javaDisplayFeaturesType and
364 // javaDisplayFeaturesState cannot be null
365 jsize rectSize = env->GetArrayLength(javaDisplayFeaturesBounds);
366 std::vector<int> boundsIntVector(rectSize);
367 env->GetIntArrayRegion(javaDisplayFeaturesBounds, 0, rectSize,
368 &boundsIntVector[0]);
369 std::vector<double> displayFeaturesBounds(boundsIntVector.begin(),
370 boundsIntVector.end());
371 jsize typeSize = env->GetArrayLength(javaDisplayFeaturesType);
372 std::vector<int> displayFeaturesType(typeSize);
373 env->GetIntArrayRegion(javaDisplayFeaturesType, 0, typeSize,
374 &displayFeaturesType[0]);
375
376 jsize stateSize = env->GetArrayLength(javaDisplayFeaturesState);
377 std::vector<int> displayFeaturesState(stateSize);
378 env->GetIntArrayRegion(javaDisplayFeaturesState, 0, stateSize,
379 &displayFeaturesState[0]);
380
381 // TODO(boetger): update for https://github.com/flutter/flutter/issues/149033
382 const flutter::ViewportMetrics metrics{
383 static_cast<double>(devicePixelRatio), // p_device_pixel_ratio
384 static_cast<double>(physicalWidth), // p_physical_width
385 static_cast<double>(physicalHeight), // p_physical_height
386 static_cast<double>(physicalMinWidth), // p_physical_min_width_constraint
387 static_cast<double>(physicalMaxWidth), // p_physical_max_width_constraint
388 static_cast<double>(
389 physicalMinHeight), // p_physical_min_height_constraint
390 static_cast<double>(
391 physicalMaxHeight), // p_physical_max_height_constraint
392 static_cast<double>(physicalPaddingTop), // p_physical_padding_top
393 static_cast<double>(physicalPaddingRight), // p_physical_padding_right
394 static_cast<double>(physicalPaddingBottom), // p_physical_padding_bottom
395 static_cast<double>(physicalPaddingLeft), // p_physical_padding_left
396 static_cast<double>(physicalViewInsetTop), // p_physical_view_inset_top
397 static_cast<double>(
398 physicalViewInsetRight), // p_physical_view_inset_right
399 static_cast<double>(
400 physicalViewInsetBottom), // p_physical_view_inset_bottom
401 static_cast<double>(physicalViewInsetLeft), // p_physical_view_inset_left
402 static_cast<double>(
403 systemGestureInsetTop), // p_physical_system_gesture_inset_top
404 static_cast<double>(
405 systemGestureInsetRight), // p_physical_system_gesture_inset_right
406 static_cast<double>(
407 systemGestureInsetBottom), // p_physical_system_gesture_inset_bottom
408 static_cast<double>(
409 systemGestureInsetLeft), // p_physical_system_gesture_inset_left
410 static_cast<double>(physicalTouchSlop), // p_physical_touch_slop
411 displayFeaturesBounds, // p_physical_display_features_bounds
412 displayFeaturesType, // p_physical_display_features_type
413 displayFeaturesState, // p_physical_display_features_state
414 0, // p_display_id,
415 static_cast<double>(
416 physicalDisplayCornerRadiusTopLeft), // p_physical_display_corner_radius_top_left
417 static_cast<double>(
418 physicalDisplayCornerRadiusTopRight), // p_physical_display_corner_radius_top_right
419 static_cast<double>(
420 physicalDisplayCornerRadiusBottomRight), // p_physical_display_corner_radius_bottom_right
421 static_cast<double>(
422 physicalDisplayCornerRadiusBottomLeft), // p_physical_display_corner_radius_bottom_left
423 };
424
425 ANDROID_SHELL_HOLDER->GetPlatformView()->SetViewportMetrics(
426 kFlutterImplicitViewId, metrics);
427}
428
429static void UpdateDisplayMetrics(JNIEnv* env,
430 jobject jcaller,
431 jlong shell_holder) {
432 ANDROID_SHELL_HOLDER->UpdateDisplayMetrics();
433}
434
435static bool IsSurfaceControlEnabled(JNIEnv* env,
436 jobject jcaller,
437 jlong shell_holder) {
438 return ANDROID_SHELL_HOLDER->IsSurfaceControlEnabled();
439}
440
441static jobject GetBitmap(JNIEnv* env, jobject jcaller, jlong shell_holder) {
442 auto screenshot = ANDROID_SHELL_HOLDER->Screenshot(
443 Rasterizer::ScreenshotType::UncompressedImage, false);
444 if (screenshot.data == nullptr) {
445 return nullptr;
446 }
447
448 jstring argb = env->NewStringUTF("ARGB_8888");
449 if (argb == nullptr) {
450 return nullptr;
451 }
452
453 jobject bitmap_config = env->CallStaticObjectMethod(
454 g_bitmap_config_class->obj(), g_bitmap_config_value_of, argb);
455 if (bitmap_config == nullptr) {
456 return nullptr;
457 }
458
459 auto bitmap = env->CallStaticObjectMethod(
460 g_bitmap_class->obj(), g_bitmap_create_bitmap_method,
461 screenshot.frame_size.width, screenshot.frame_size.height, bitmap_config);
462
464 env,
465 env->NewDirectByteBuffer(const_cast<uint8_t*>(screenshot.data->bytes()),
466 screenshot.data->size()));
467
468 env->CallVoidMethod(bitmap, g_bitmap_copy_pixels_from_buffer_method,
469 buffer.obj());
470
471 return bitmap;
472}
473
474static void DispatchPlatformMessage(JNIEnv* env,
475 jobject jcaller,
476 jlong shell_holder,
477 jstring channel,
478 jobject message,
479 jint position,
480 jint responseId) {
481 ANDROID_SHELL_HOLDER->GetPlatformView()->DispatchPlatformMessage(
482 env, //
484 message, //
485 position, //
486 responseId //
487 );
488}
489
490static void DispatchEmptyPlatformMessage(JNIEnv* env,
491 jobject jcaller,
492 jlong shell_holder,
493 jstring channel,
494 jint responseId) {
495 ANDROID_SHELL_HOLDER->GetPlatformView()->DispatchEmptyPlatformMessage(
496 env, //
498 responseId //
499 );
500}
501
502static void CleanupMessageData(JNIEnv* env,
503 jobject jcaller,
504 jlong message_data) {
505 // Called from any thread.
506 free(reinterpret_cast<void*>(message_data));
507}
508
509static void DispatchPointerDataPacket(JNIEnv* env,
510 jobject jcaller,
511 jlong shell_holder,
512 jobject buffer,
513 jint position) {
514 uint8_t* data = static_cast<uint8_t*>(env->GetDirectBufferAddress(buffer));
515 auto packet = std::make_unique<flutter::PointerDataPacket>(data, position);
516 ANDROID_SHELL_HOLDER->GetPlatformView()->DispatchPointerDataPacket(
517 std::move(packet));
518}
519
520static void DispatchSemanticsAction(JNIEnv* env,
521 jobject jcaller,
522 jlong shell_holder,
523 jint id,
524 jint action,
525 jobject args,
526 jint args_position) {
527 ANDROID_SHELL_HOLDER->GetPlatformView()->DispatchSemanticsAction(
528 env, //
529 id, //
530 action, //
531 args, //
532 args_position //
533 );
534}
535
536static void SetSemanticsEnabled(JNIEnv* env,
537 jobject jcaller,
538 jlong shell_holder,
539 jboolean enabled) {
540 ANDROID_SHELL_HOLDER->GetPlatformView()->SetSemanticsEnabled(enabled);
541}
542
543static void SetAccessibilityFeatures(JNIEnv* env,
544 jobject jcaller,
545 jlong shell_holder,
546 jint flags) {
547 ANDROID_SHELL_HOLDER->GetPlatformView()->SetAccessibilityFeatures(flags);
548}
549
550static jboolean GetIsSoftwareRendering(JNIEnv* env, jobject jcaller) {
551 return FlutterMain::Get().GetSettings().enable_software_rendering;
552}
553
554static void RegisterTexture(JNIEnv* env,
555 jobject jcaller,
556 jlong shell_holder,
557 jlong texture_id,
558 jobject surface_texture) {
559 ANDROID_SHELL_HOLDER->GetPlatformView()->RegisterExternalTexture(
560 static_cast<int64_t>(texture_id), //
561 fml::jni::ScopedJavaGlobalRef<jobject>(env, surface_texture) //
562 );
563}
564
565static void RegisterImageTexture(JNIEnv* env,
566 jobject jcaller,
567 jlong shell_holder,
568 jlong texture_id,
569 jobject image_texture_entry,
570 jboolean reset_on_background) {
571 ImageExternalTexture::ImageLifecycle lifecycle =
572 reset_on_background ? ImageExternalTexture::ImageLifecycle::kReset
573 : ImageExternalTexture::ImageLifecycle::kKeepAlive;
574
575 ANDROID_SHELL_HOLDER->GetPlatformView()->RegisterImageTexture(
576 static_cast<int64_t>(texture_id), //
577 fml::jni::ScopedJavaGlobalRef<jobject>(env, image_texture_entry), //
578 lifecycle //
579 );
580}
581
582static void UnregisterTexture(JNIEnv* env,
583 jobject jcaller,
584 jlong shell_holder,
585 jlong texture_id) {
586 ANDROID_SHELL_HOLDER->GetPlatformView()->UnregisterTexture(
587 static_cast<int64_t>(texture_id));
588}
589
590static void MarkTextureFrameAvailable(JNIEnv* env,
591 jobject jcaller,
592 jlong shell_holder,
593 jlong texture_id) {
594 ANDROID_SHELL_HOLDER->GetPlatformView()->MarkTextureFrameAvailable(
595 static_cast<int64_t>(texture_id));
596}
597
598static void ScheduleFrame(JNIEnv* env, jobject jcaller, jlong shell_holder) {
599 ANDROID_SHELL_HOLDER->GetPlatformView()->ScheduleFrame();
600}
601
602static void InvokePlatformMessageResponseCallback(JNIEnv* env,
603 jobject jcaller,
604 jlong shell_holder,
605 jint responseId,
606 jobject message,
607 jint position) {
608 uint8_t* response_data =
609 static_cast<uint8_t*>(env->GetDirectBufferAddress(message));
610 FML_DCHECK(response_data != nullptr);
611 auto mapping = std::make_unique<fml::MallocMapping>(
612 fml::MallocMapping::Copy(response_data, response_data + position));
613 ANDROID_SHELL_HOLDER->GetPlatformMessageHandler()
614 ->InvokePlatformMessageResponseCallback(responseId, std::move(mapping));
615}
616
617static void InvokePlatformMessageEmptyResponseCallback(JNIEnv* env,
618 jobject jcaller,
619 jlong shell_holder,
620 jint responseId) {
621 ANDROID_SHELL_HOLDER->GetPlatformMessageHandler()
622 ->InvokePlatformMessageEmptyResponseCallback(responseId);
623}
624
625static void NotifyLowMemoryWarning(JNIEnv* env,
626 jobject obj,
627 jlong shell_holder) {
628 ANDROID_SHELL_HOLDER->NotifyLowMemoryWarning();
629}
630
631static jboolean FlutterTextUtilsIsEmoji(JNIEnv* env,
632 jobject obj,
633 jint codePoint) {
634 return u_hasBinaryProperty(codePoint, UProperty::UCHAR_EMOJI);
635}
636
637static jboolean FlutterTextUtilsIsEmojiModifier(JNIEnv* env,
638 jobject obj,
639 jint codePoint) {
640 return u_hasBinaryProperty(codePoint, UProperty::UCHAR_EMOJI_MODIFIER);
641}
642
643static jboolean FlutterTextUtilsIsEmojiModifierBase(JNIEnv* env,
644 jobject obj,
645 jint codePoint) {
646 return u_hasBinaryProperty(codePoint, UProperty::UCHAR_EMOJI_MODIFIER_BASE);
647}
648
649static jboolean FlutterTextUtilsIsVariationSelector(JNIEnv* env,
650 jobject obj,
651 jint codePoint) {
652 return u_hasBinaryProperty(codePoint, UProperty::UCHAR_VARIATION_SELECTOR);
653}
654
655static jboolean FlutterTextUtilsIsRegionalIndicator(JNIEnv* env,
656 jobject obj,
657 jint codePoint) {
658 return u_hasBinaryProperty(codePoint, UProperty::UCHAR_REGIONAL_INDICATOR);
659}
660
661static void LoadLoadingUnitFailure(intptr_t loading_unit_id,
662 const std::string& message,
663 bool transient) {
664 // TODO(garyq): Implement
665}
666
667static void DeferredComponentInstallFailure(JNIEnv* env,
668 jobject obj,
669 jint jLoadingUnitId,
670 jstring jError,
671 jboolean jTransient) {
672 LoadLoadingUnitFailure(static_cast<intptr_t>(jLoadingUnitId),
673 fml::jni::JavaStringToString(env, jError),
674 static_cast<bool>(jTransient));
675}
676
677static void LoadDartDeferredLibrary(JNIEnv* env,
678 jobject obj,
679 jlong shell_holder,
680 jint jLoadingUnitId,
681 jobjectArray jSearchPaths) {
682 // Convert java->c++
683 intptr_t loading_unit_id = static_cast<intptr_t>(jLoadingUnitId);
684 std::vector<std::string> search_paths =
685 fml::jni::StringArrayToVector(env, jSearchPaths);
686
687 // Use dlopen here to directly check if handle is nullptr before creating a
688 // NativeLibrary.
689 void* handle = nullptr;
690 while (handle == nullptr && !search_paths.empty()) {
691 std::string path = search_paths.back();
692 handle = ::dlopen(path.c_str(), RTLD_NOW);
693 search_paths.pop_back();
694 }
695 if (handle == nullptr) {
696 LoadLoadingUnitFailure(loading_unit_id,
697 "No lib .so found for provided search paths.", true);
698 return;
699 }
702
703 // Resolve symbols.
704 std::unique_ptr<const fml::SymbolMapping> data_mapping =
705 std::make_unique<const fml::SymbolMapping>(
706 native_lib, DartSnapshot::kIsolateDataSymbol);
707 std::unique_ptr<const fml::SymbolMapping> instructions_mapping =
708 std::make_unique<const fml::SymbolMapping>(
709 native_lib, DartSnapshot::kIsolateInstructionsSymbol);
710
711 ANDROID_SHELL_HOLDER->GetPlatformView()->LoadDartDeferredLibrary(
712 loading_unit_id, std::move(data_mapping),
713 std::move(instructions_mapping));
714}
715
716static void UpdateJavaAssetManager(JNIEnv* env,
717 jobject obj,
718 jlong shell_holder,
719 jobject jAssetManager,
720 jstring jAssetBundlePath) {
721 auto asset_resolver = std::make_unique<flutter::APKAssetProvider>(
722 env, // jni environment
723 jAssetManager, // asset manager
724 fml::jni::JavaStringToString(env, jAssetBundlePath)); // apk asset dir
725
726 ANDROID_SHELL_HOLDER->GetPlatformView()->UpdateAssetResolverByType(
727 std::move(asset_resolver),
728 AssetResolver::AssetResolverType::kApkAssetProvider);
729}
730
731bool RegisterApi(JNIEnv* env) {
732 static const JNINativeMethod flutter_jni_methods[] = {
733 // Start of methods from FlutterJNI
734 {
735 .name = "nativeAttach",
736 .signature = "(Lio/flutter/embedding/engine/FlutterJNI;)J",
737 .fnPtr = reinterpret_cast<void*>(&AttachJNI),
738 },
739 {
740 .name = "nativeDestroy",
741 .signature = "(J)V",
742 .fnPtr = reinterpret_cast<void*>(&DestroyJNI),
743 },
744 {
745 .name = "nativeSpawn",
746 .signature = "(JLjava/lang/String;Ljava/lang/String;Ljava/lang/"
747 "String;Ljava/util/List;J)Lio/flutter/"
748 "embedding/engine/FlutterJNI;",
749 .fnPtr = reinterpret_cast<void*>(&SpawnJNI),
750 },
751 {
752 .name = "nativeRunBundleAndSnapshotFromLibrary",
753 .signature = "(JLjava/lang/String;Ljava/lang/String;"
754 "Ljava/lang/String;Landroid/content/res/"
755 "AssetManager;Ljava/util/List;J)V",
756 .fnPtr = reinterpret_cast<void*>(&RunBundleAndSnapshotFromLibrary),
757 },
758 {
759 .name = "nativeDispatchEmptyPlatformMessage",
760 .signature = "(JLjava/lang/String;I)V",
761 .fnPtr = reinterpret_cast<void*>(&DispatchEmptyPlatformMessage),
762 },
763 {
764 .name = "nativeCleanupMessageData",
765 .signature = "(J)V",
766 .fnPtr = reinterpret_cast<void*>(&CleanupMessageData),
767 },
768 {
769 .name = "nativeDispatchPlatformMessage",
770 .signature = "(JLjava/lang/String;Ljava/nio/ByteBuffer;II)V",
771 .fnPtr = reinterpret_cast<void*>(&DispatchPlatformMessage),
772 },
773 {
774 .name = "nativeInvokePlatformMessageResponseCallback",
775 .signature = "(JILjava/nio/ByteBuffer;I)V",
776 .fnPtr =
777 reinterpret_cast<void*>(&InvokePlatformMessageResponseCallback),
778 },
779 {
780 .name = "nativeInvokePlatformMessageEmptyResponseCallback",
781 .signature = "(JI)V",
782 .fnPtr = reinterpret_cast<void*>(
784 },
785 {
786 .name = "nativeNotifyLowMemoryWarning",
787 .signature = "(J)V",
788 .fnPtr = reinterpret_cast<void*>(&NotifyLowMemoryWarning),
789 },
790
791 // Start of methods from FlutterView
792 {
793 .name = "nativeGetBitmap",
794 .signature = "(J)Landroid/graphics/Bitmap;",
795 .fnPtr = reinterpret_cast<void*>(&GetBitmap),
796 },
797 {
798 .name = "nativeSurfaceCreated",
799 .signature = "(JLandroid/view/Surface;)V",
800 .fnPtr = reinterpret_cast<void*>(&SurfaceCreated),
801 },
802 {
803 .name = "nativeSurfaceWindowChanged",
804 .signature = "(JLandroid/view/Surface;)V",
805 .fnPtr = reinterpret_cast<void*>(&SurfaceWindowChanged),
806 },
807 {
808 .name = "nativeSurfaceChanged",
809 .signature = "(JII)V",
810 .fnPtr = reinterpret_cast<void*>(&SurfaceChanged),
811 },
812 {
813 .name = "nativeSurfaceDestroyed",
814 .signature = "(J)V",
815 .fnPtr = reinterpret_cast<void*>(&SurfaceDestroyed),
816 },
817 {
818 .name = "nativeSetViewportMetrics",
819 .signature = "(JFIIIIIIIIIIIIIII[I[I[IIIIIIIII)V",
820 .fnPtr = reinterpret_cast<void*>(&SetViewportMetrics),
821 },
822 {
823 .name = "nativeDispatchPointerDataPacket",
824 .signature = "(JLjava/nio/ByteBuffer;I)V",
825 .fnPtr = reinterpret_cast<void*>(&DispatchPointerDataPacket),
826 },
827 {
828 .name = "nativeDispatchSemanticsAction",
829 .signature = "(JIILjava/nio/ByteBuffer;I)V",
830 .fnPtr = reinterpret_cast<void*>(&DispatchSemanticsAction),
831 },
832 {
833 .name = "nativeSetSemanticsEnabled",
834 .signature = "(JZ)V",
835 .fnPtr = reinterpret_cast<void*>(&SetSemanticsEnabled),
836 },
837 {
838 .name = "nativeSetAccessibilityFeatures",
839 .signature = "(JI)V",
840 .fnPtr = reinterpret_cast<void*>(&SetAccessibilityFeatures),
841 },
842 {
843 .name = "nativeGetIsSoftwareRenderingEnabled",
844 .signature = "()Z",
845 .fnPtr = reinterpret_cast<void*>(&GetIsSoftwareRendering),
846 },
847 {
848 .name = "nativeRegisterTexture",
849 .signature = "(JJLjava/lang/ref/"
850 "WeakReference;)V",
851 .fnPtr = reinterpret_cast<void*>(&RegisterTexture),
852 },
853 {
854 .name = "nativeRegisterImageTexture",
855 .signature = "(JJLjava/lang/ref/"
856 "WeakReference;Z)V",
857 .fnPtr = reinterpret_cast<void*>(&RegisterImageTexture),
858 },
859 {
860 .name = "nativeMarkTextureFrameAvailable",
861 .signature = "(JJ)V",
862 .fnPtr = reinterpret_cast<void*>(&MarkTextureFrameAvailable),
863 },
864 {
865 .name = "nativeScheduleFrame",
866 .signature = "(J)V",
867 .fnPtr = reinterpret_cast<void*>(&ScheduleFrame),
868 },
869 {
870 .name = "nativeUnregisterTexture",
871 .signature = "(JJ)V",
872 .fnPtr = reinterpret_cast<void*>(&UnregisterTexture),
873 },
874 // Methods for Dart callback functionality.
875 {
876 .name = "nativeLookupCallbackInformation",
877 .signature = "(J)Lio/flutter/view/FlutterCallbackInformation;",
878 .fnPtr = reinterpret_cast<void*>(&LookupCallbackInformation),
879 },
880
881 // Start of methods for FlutterTextUtils
882 {
883 .name = "nativeFlutterTextUtilsIsEmoji",
884 .signature = "(I)Z",
885 .fnPtr = reinterpret_cast<void*>(&FlutterTextUtilsIsEmoji),
886 },
887 {
888 .name = "nativeFlutterTextUtilsIsEmojiModifier",
889 .signature = "(I)Z",
890 .fnPtr = reinterpret_cast<void*>(&FlutterTextUtilsIsEmojiModifier),
891 },
892 {
893 .name = "nativeFlutterTextUtilsIsEmojiModifierBase",
894 .signature = "(I)Z",
895 .fnPtr =
896 reinterpret_cast<void*>(&FlutterTextUtilsIsEmojiModifierBase),
897 },
898 {
899 .name = "nativeFlutterTextUtilsIsVariationSelector",
900 .signature = "(I)Z",
901 .fnPtr =
902 reinterpret_cast<void*>(&FlutterTextUtilsIsVariationSelector),
903 },
904 {
905 .name = "nativeFlutterTextUtilsIsRegionalIndicator",
906 .signature = "(I)Z",
907 .fnPtr =
908 reinterpret_cast<void*>(&FlutterTextUtilsIsRegionalIndicator),
909 },
910 {
911 .name = "nativeLoadDartDeferredLibrary",
912 .signature = "(JI[Ljava/lang/String;)V",
913 .fnPtr = reinterpret_cast<void*>(&LoadDartDeferredLibrary),
914 },
915 {
916 .name = "nativeUpdateJavaAssetManager",
917 .signature =
918 "(JLandroid/content/res/AssetManager;Ljava/lang/String;)V",
919 .fnPtr = reinterpret_cast<void*>(&UpdateJavaAssetManager),
920 },
921 {
922 .name = "nativeDeferredComponentInstallFailure",
923 .signature = "(ILjava/lang/String;Z)V",
924 .fnPtr = reinterpret_cast<void*>(&DeferredComponentInstallFailure),
925 },
926 {
927 .name = "nativeUpdateDisplayMetrics",
928 .signature = "(J)V",
929 .fnPtr = reinterpret_cast<void*>(&UpdateDisplayMetrics),
930 },
931 {
932 .name = "nativeIsSurfaceControlEnabled",
933 .signature = "(J)Z",
934 .fnPtr = reinterpret_cast<void*>(&IsSurfaceControlEnabled),
935 }};
936
937 if (env->RegisterNatives(g_flutter_jni_class->obj(), flutter_jni_methods,
938 std::size(flutter_jni_methods)) != 0) {
939 FML_LOG(ERROR) << "Failed to RegisterNatives with FlutterJNI";
940 return false;
941 }
942
943 g_jni_shell_holder_field = env->GetFieldID(
944 g_flutter_jni_class->obj(), "nativeShellHolderId", "Ljava/lang/Long;");
945
946 if (g_jni_shell_holder_field == nullptr) {
947 FML_LOG(ERROR) << "Could not locate FlutterJNI's nativeShellHolderId field";
948 return false;
949 }
950
952 env->GetMethodID(g_flutter_jni_class->obj(), "<init>", "()V");
953
954 if (g_jni_constructor == nullptr) {
955 FML_LOG(ERROR) << "Could not locate FlutterJNI's constructor";
956 return false;
957 }
958
959 g_long_constructor = env->GetStaticMethodID(g_java_long_class->obj(),
960 "valueOf", "(J)Ljava/lang/Long;");
961 if (g_long_constructor == nullptr) {
962 FML_LOG(ERROR) << "Could not locate Long's constructor";
963 return false;
964 }
965
967
968 fml::jni::ScopedJavaLocalRef<jclass> overlay_surface_class(
969 env, env->FindClass("io/flutter/embedding/engine/FlutterOverlaySurface"));
970 if (overlay_surface_class.is_null()) {
971 FML_LOG(ERROR) << "Could not locate FlutterOverlaySurface class";
972 return false;
973 }
975 env->GetMethodID(overlay_surface_class.obj(), "getId", "()I");
976 if (g_overlay_surface_id_method == nullptr) {
977 FML_LOG(ERROR) << "Could not locate FlutterOverlaySurface#getId() method";
978 return false;
979 }
980 g_overlay_surface_surface_method = env->GetMethodID(
981 overlay_surface_class.obj(), "getSurface", "()Landroid/view/Surface;");
982 if (g_overlay_surface_surface_method == nullptr) {
983 FML_LOG(ERROR)
984 << "Could not locate FlutterOverlaySurface#getSurface() method";
985 return false;
986 }
987
989 env, env->FindClass("android/graphics/Bitmap"));
990 if (g_bitmap_class->is_null()) {
991 FML_LOG(ERROR) << "Could not locate Bitmap Class";
992 return false;
993 }
994
995 g_bitmap_create_bitmap_method = env->GetStaticMethodID(
996 g_bitmap_class->obj(), "createBitmap",
997 "(IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;");
998 if (g_bitmap_create_bitmap_method == nullptr) {
999 FML_LOG(ERROR) << "Could not locate Bitmap.createBitmap method";
1000 return false;
1001 }
1002
1003 g_bitmap_copy_pixels_from_buffer_method = env->GetMethodID(
1004 g_bitmap_class->obj(), "copyPixelsFromBuffer", "(Ljava/nio/Buffer;)V");
1005 if (g_bitmap_copy_pixels_from_buffer_method == nullptr) {
1006 FML_LOG(ERROR) << "Could not locate Bitmap.copyPixelsFromBuffer method";
1007 return false;
1008 }
1009
1011 env, env->FindClass("android/graphics/Bitmap$Config"));
1012 if (g_bitmap_config_class->is_null()) {
1013 FML_LOG(ERROR) << "Could not locate Bitmap.Config Class";
1014 return false;
1015 }
1016
1017 g_bitmap_config_value_of = env->GetStaticMethodID(
1018 g_bitmap_config_class->obj(), "valueOf",
1019 "(Ljava/lang/String;)Landroid/graphics/Bitmap$Config;");
1020 if (g_bitmap_config_value_of == nullptr) {
1021 FML_LOG(ERROR) << "Could not locate Bitmap.Config.valueOf method";
1022 return false;
1023 }
1024
1025 return true;
1026}
1027
1028bool PlatformViewAndroid::Register(JNIEnv* env) {
1029 if (env == nullptr) {
1030 FML_LOG(ERROR) << "No JNIEnv provided";
1031 return false;
1032 }
1033
1035 env, env->FindClass("io/flutter/view/FlutterCallbackInformation"));
1036 if (g_flutter_callback_info_class->is_null()) {
1037 FML_LOG(ERROR) << "Could not locate FlutterCallbackInformation class";
1038 return false;
1039 }
1040
1041 g_flutter_callback_info_constructor = env->GetMethodID(
1042 g_flutter_callback_info_class->obj(), "<init>",
1043 "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
1044 if (g_flutter_callback_info_constructor == nullptr) {
1045 FML_LOG(ERROR) << "Could not locate FlutterCallbackInformation constructor";
1046 return false;
1047 }
1048
1050 env, env->FindClass("io/flutter/embedding/engine/FlutterJNI"));
1051 if (g_flutter_jni_class->is_null()) {
1052 FML_LOG(ERROR) << "Failed to find FlutterJNI Class.";
1053 return false;
1054 }
1055
1057 env,
1058 env->FindClass(
1059 "io/flutter/embedding/engine/mutatorsstack/FlutterMutatorsStack"));
1060 if (g_mutators_stack_class == nullptr) {
1061 FML_LOG(ERROR) << "Could not locate FlutterMutatorsStack";
1062 return false;
1063 }
1064
1066 env->GetMethodID(g_mutators_stack_class->obj(), "<init>", "()V");
1067 if (g_mutators_stack_init_method == nullptr) {
1068 FML_LOG(ERROR) << "Could not locate FlutterMutatorsStack.init method";
1069 return false;
1070 }
1071
1073 env->GetMethodID(g_mutators_stack_class->obj(), "pushTransform", "([F)V");
1074 if (g_mutators_stack_push_transform_method == nullptr) {
1075 FML_LOG(ERROR)
1076 << "Could not locate FlutterMutatorsStack.pushTransform method";
1077 return false;
1078 }
1079
1080 g_mutators_stack_push_cliprect_method = env->GetMethodID(
1081 g_mutators_stack_class->obj(), "pushClipRect", "(IIII)V");
1082 if (g_mutators_stack_push_cliprect_method == nullptr) {
1083 FML_LOG(ERROR)
1084 << "Could not locate FlutterMutatorsStack.pushClipRect method";
1085 return false;
1086 }
1087
1088 g_mutators_stack_push_cliprrect_method = env->GetMethodID(
1089 g_mutators_stack_class->obj(), "pushClipRRect", "(IIII[F)V");
1090 if (g_mutators_stack_push_cliprrect_method == nullptr) {
1091 FML_LOG(ERROR)
1092 << "Could not locate FlutterMutatorsStack.pushClipRRect method";
1093 return false;
1094 }
1095
1097 env->GetMethodID(g_mutators_stack_class->obj(), "pushOpacity", "(F)V");
1098 if (g_mutators_stack_push_opacity_method == nullptr) {
1099 FML_LOG(ERROR)
1100 << "Could not locate FlutterMutatorsStack.pushOpacity method";
1101 return false;
1102 }
1103
1105 env->GetMethodID(g_mutators_stack_class->obj(), "pushClipPath",
1106 "(Landroid/graphics/Path;)V");
1107 if (g_mutators_stack_push_clippath_method == nullptr) {
1108 FML_LOG(ERROR)
1109 << "Could not locate FlutterMutatorsStack.pushClipPath method";
1110 return false;
1111 }
1112
1114 env, env->FindClass("java/lang/ref/WeakReference"));
1115 if (g_java_weak_reference_class->is_null()) {
1116 FML_LOG(ERROR) << "Could not locate WeakReference class";
1117 return false;
1118 }
1119
1120 g_java_weak_reference_get_method = env->GetMethodID(
1121 g_java_weak_reference_class->obj(), "get", "()Ljava/lang/Object;");
1122 if (g_java_weak_reference_get_method == nullptr) {
1123 FML_LOG(ERROR) << "Could not locate WeakReference.get method";
1124 return false;
1125 }
1126
1128 env, env->FindClass(
1129 "io/flutter/embedding/engine/renderer/SurfaceTextureWrapper"));
1130 if (g_texture_wrapper_class->is_null()) {
1131 FML_LOG(ERROR) << "Could not locate SurfaceTextureWrapper class";
1132 return false;
1133 }
1134
1135 g_attach_to_gl_context_method = env->GetMethodID(
1136 g_texture_wrapper_class->obj(), "attachToGLContext", "(I)V");
1137
1138 if (g_attach_to_gl_context_method == nullptr) {
1139 FML_LOG(ERROR) << "Could not locate attachToGlContext method";
1140 return false;
1141 }
1142
1144 env->GetMethodID(g_texture_wrapper_class->obj(), "shouldUpdate", "()Z");
1145
1146 if (g_surface_texture_wrapper_should_update == nullptr) {
1147 FML_LOG(ERROR)
1148 << "Could not locate SurfaceTextureWrapper.shouldUpdate method";
1149 return false;
1150 }
1151
1153 env->GetMethodID(g_texture_wrapper_class->obj(), "updateTexImage", "()V");
1154
1155 if (g_update_tex_image_method == nullptr) {
1156 FML_LOG(ERROR) << "Could not locate updateTexImage method";
1157 return false;
1158 }
1159
1160 g_get_transform_matrix_method = env->GetMethodID(
1161 g_texture_wrapper_class->obj(), "getTransformMatrix", "([F)V");
1162
1163 if (g_get_transform_matrix_method == nullptr) {
1164 FML_LOG(ERROR) << "Could not locate getTransformMatrix method";
1165 return false;
1166 }
1167
1168 g_detach_from_gl_context_method = env->GetMethodID(
1169 g_texture_wrapper_class->obj(), "detachFromGLContext", "()V");
1170
1171 if (g_detach_from_gl_context_method == nullptr) {
1172 FML_LOG(ERROR) << "Could not locate detachFromGlContext method";
1173 return false;
1174 }
1177 env, env->FindClass("io/flutter/view/TextureRegistry$ImageConsumer"));
1179 FML_LOG(ERROR) << "Could not locate TextureRegistry.ImageConsumer class";
1180 return false;
1181 }
1182
1184 env->GetMethodID(g_image_consumer_texture_registry_interface->obj(),
1185 "acquireLatestImage", "()Landroid/media/Image;");
1186 if (g_acquire_latest_image_method == nullptr) {
1187 FML_LOG(ERROR) << "Could not locate acquireLatestImage on "
1188 "TextureRegistry.ImageConsumer class";
1189 return false;
1190 }
1191
1193 env, env->FindClass("android/media/Image"));
1194 if (g_image_class->is_null()) {
1195 FML_LOG(ERROR) << "Could not locate Image class";
1196 return false;
1197 }
1198
1199 // Ensure we don't have any pending exceptions.
1201
1203 env->GetMethodID(g_image_class->obj(), "getHardwareBuffer",
1204 "()Landroid/hardware/HardwareBuffer;");
1205
1206 if (g_image_get_hardware_buffer_method == nullptr) {
1207 // Continue on as this method may not exist at API <= 29.
1208 fml::jni::ClearException(env, true);
1209 }
1210
1211 g_image_close_method = env->GetMethodID(g_image_class->obj(), "close", "()V");
1212
1213 if (g_image_close_method == nullptr) {
1214 FML_LOG(ERROR) << "Could not locate close on Image class";
1215 return false;
1216 }
1217
1218 // Ensure we don't have any pending exceptions.
1221 env, env->FindClass("android/hardware/HardwareBuffer"));
1222
1223 if (!g_hardware_buffer_class->is_null()) {
1225 env->GetMethodID(g_hardware_buffer_class->obj(), "close", "()V");
1226 if (g_hardware_buffer_close_method == nullptr) {
1227 // Continue on as this class may not exist at API <= 26.
1228 fml::jni::ClearException(env, true);
1229 }
1230 } else {
1231 // Continue on as this class may not exist at API <= 26.
1232 fml::jni::ClearException(env, true);
1233 }
1234
1236 g_flutter_jni_class->obj(), "computePlatformResolvedLocale",
1237 "([Ljava/lang/String;)[Ljava/lang/String;");
1238
1239 if (g_compute_platform_resolved_locale_method == nullptr) {
1240 FML_LOG(ERROR) << "Could not locate computePlatformResolvedLocale method";
1241 return false;
1242 }
1243
1244 g_request_dart_deferred_library_method = env->GetMethodID(
1245 g_flutter_jni_class->obj(), "requestDartDeferredLibrary", "(I)V");
1246
1247 if (g_request_dart_deferred_library_method == nullptr) {
1248 FML_LOG(ERROR) << "Could not locate requestDartDeferredLibrary method";
1249 return false;
1250 }
1251
1253 env, env->FindClass("java/lang/Long"));
1254 if (g_java_long_class->is_null()) {
1255 FML_LOG(ERROR) << "Could not locate java.lang.Long class";
1256 return false;
1257 }
1258
1259 // Android path class and methods.
1261 env, env->FindClass("android/graphics/Path"));
1262 if (path_class->is_null()) {
1263 FML_LOG(ERROR) << "Could not locate android.graphics.Path class";
1264 return false;
1265 }
1266
1267 path_constructor = env->GetMethodID(path_class->obj(), "<init>", "()V");
1268 if (path_constructor == nullptr) {
1269 FML_LOG(ERROR) << "Could not locate android.graphics.Path constructor";
1270 return false;
1271 }
1272
1273 path_set_fill_type_method = env->GetMethodID(
1274 path_class->obj(), "setFillType", "(Landroid/graphics/Path$FillType;)V");
1275 if (path_set_fill_type_method == nullptr) {
1276 FML_LOG(ERROR)
1277 << "Could not locate android.graphics.Path.setFillType method";
1278 return false;
1279 }
1280
1281 path_move_to_method = env->GetMethodID(path_class->obj(), "moveTo", "(FF)V");
1282 if (path_move_to_method == nullptr) {
1283 FML_LOG(ERROR) << "Could not locate android.graphics.Path.moveTo method";
1284 return false;
1285 }
1286 path_line_to_method = env->GetMethodID(path_class->obj(), "lineTo", "(FF)V");
1287 if (path_line_to_method == nullptr) {
1288 FML_LOG(ERROR) << "Could not locate android.graphics.Path.lineTo method";
1289 return false;
1290 }
1292 env->GetMethodID(path_class->obj(), "quadTo", "(FFFF)V");
1293 if (path_quad_to_method == nullptr) {
1294 FML_LOG(ERROR) << "Could not locate android.graphics.Path.quadTo method";
1295 return false;
1296 }
1298 env->GetMethodID(path_class->obj(), "cubicTo", "(FFFFFF)V");
1299 if (path_cubic_to_method == nullptr) {
1300 FML_LOG(ERROR) << "Could not locate android.graphics.Path.cubicTo method";
1301 return false;
1302 }
1303 // Ensure we don't have any pending exceptions.
1305
1307 env->GetMethodID(path_class->obj(), "conicTo", "(FFFFF)V");
1308 if (path_conic_to_method == nullptr) {
1309 // Continue on as this method may not exist at API <= 34.
1310 fml::jni::ClearException(env, true);
1311 }
1312 path_close_method = env->GetMethodID(path_class->obj(), "close", "()V");
1313 if (path_close_method == nullptr) {
1314 FML_LOG(ERROR) << "Could not locate android.graphics.Path.close method";
1315 return false;
1316 }
1317
1319 env, env->FindClass("android/graphics/Path$FillType"));
1320 if (g_path_fill_type_class->is_null()) {
1321 FML_LOG(ERROR) << "Could not locate android.graphics.Path$FillType class";
1322 return false;
1323 }
1324
1326 env->GetStaticFieldID(g_path_fill_type_class->obj(), "WINDING",
1327 "Landroid/graphics/Path$FillType;");
1328 if (g_path_fill_type_winding_field == nullptr) {
1329 FML_LOG(ERROR) << "Could not locate Path.FillType.WINDING field";
1330 return false;
1331 }
1332
1334 env->GetStaticFieldID(g_path_fill_type_class->obj(), "EVEN_ODD",
1335 "Landroid/graphics/Path$FillType;");
1336 if (g_path_fill_type_even_odd_field == nullptr) {
1337 FML_LOG(ERROR) << "Could not locate Path.FillType.EVEN_ODD field";
1338 return false;
1339 }
1340
1341 return RegisterApi(env);
1342}
1343
1344PlatformViewAndroidJNIImpl::PlatformViewAndroidJNIImpl(
1345 const fml::jni::JavaObjectWeakGlobalRef& java_object)
1346 : java_object_(java_object) {}
1347
1348PlatformViewAndroidJNIImpl::~PlatformViewAndroidJNIImpl() = default;
1349
1350void PlatformViewAndroidJNIImpl::FlutterViewHandlePlatformMessage(
1351 std::unique_ptr<flutter::PlatformMessage> message,
1352 int responseId) {
1353 // Called from any thread.
1354 JNIEnv* env = fml::jni::AttachCurrentThread();
1355
1356 auto java_object = java_object_.get(env);
1357 if (java_object.is_null()) {
1358 return;
1359 }
1360
1362 fml::jni::StringToJavaString(env, message->channel());
1363
1364 if (message->hasData()) {
1366 env, env->NewDirectByteBuffer(
1367 const_cast<uint8_t*>(message->data().GetMapping()),
1368 message->data().GetSize()));
1369 // Message data is deleted in CleanupMessageData.
1370 fml::MallocMapping mapping = message->releaseData();
1371 env->CallVoidMethod(java_object.obj(), g_handle_platform_message_method,
1372 java_channel.obj(), message_array.obj(), responseId,
1373 reinterpret_cast<jlong>(mapping.Release()));
1374 } else {
1375 env->CallVoidMethod(java_object.obj(), g_handle_platform_message_method,
1376 java_channel.obj(), nullptr, responseId, nullptr);
1377 }
1378
1380}
1381
1382void PlatformViewAndroidJNIImpl::FlutterViewSetApplicationLocale(
1383 std::string locale) {
1384 JNIEnv* env = fml::jni::AttachCurrentThread();
1385
1386 auto java_object = java_object_.get(env);
1387 if (java_object.is_null()) {
1388 return;
1389 }
1390
1392 fml::jni::StringToJavaString(env, locale);
1393
1394 env->CallVoidMethod(java_object.obj(), g_set_application_locale_method,
1395 jlocale.obj());
1396
1398}
1399
1400void PlatformViewAndroidJNIImpl::FlutterViewSetSemanticsTreeEnabled(
1401 bool enabled) {
1402 JNIEnv* env = fml::jni::AttachCurrentThread();
1403
1404 auto java_object = java_object_.get(env);
1405 if (java_object.is_null()) {
1406 return;
1407 }
1408
1409 env->CallVoidMethod(java_object.obj(), g_set_semantics_tree_enabled_method,
1410 enabled);
1411
1413}
1414
1415void PlatformViewAndroidJNIImpl::FlutterViewHandlePlatformMessageResponse(
1416 int responseId,
1417 std::unique_ptr<fml::Mapping> data) {
1418 // We are on the platform thread. Attempt to get the strong reference to
1419 // the Java object.
1420 JNIEnv* env = fml::jni::AttachCurrentThread();
1421
1422 auto java_object = java_object_.get(env);
1423 if (java_object.is_null()) {
1424 // The Java object was collected before this message response got to
1425 // it. Drop the response on the floor.
1426 return;
1427 }
1428 if (data == nullptr) { // Empty response.
1429 env->CallVoidMethod(java_object.obj(),
1430 g_handle_platform_message_response_method, responseId,
1431 nullptr);
1432 } else {
1433 // Convert the vector to a Java byte array.
1435 env, env->NewDirectByteBuffer(const_cast<uint8_t*>(data->GetMapping()),
1436 data->GetSize()));
1437
1438 env->CallVoidMethod(java_object.obj(),
1439 g_handle_platform_message_response_method, responseId,
1440 data_array.obj());
1441 }
1442
1444}
1445
1446double PlatformViewAndroidJNIImpl::FlutterViewGetScaledFontSize(
1447 double font_size,
1448 int configuration_id) const {
1449 JNIEnv* env = fml::jni::AttachCurrentThread();
1450
1451 auto java_object = java_object_.get(env);
1452 if (java_object.is_null()) {
1453 return -3;
1454 }
1455
1456 const jfloat scaledSize = env->CallFloatMethod(
1457 java_object.obj(), g_get_scaled_font_size_method,
1458 static_cast<jfloat>(font_size), static_cast<jint>(configuration_id));
1460 return static_cast<double>(scaledSize);
1461}
1462
1463void PlatformViewAndroidJNIImpl::FlutterViewUpdateSemantics(
1464 std::vector<uint8_t> buffer,
1465 std::vector<std::string> strings,
1466 std::vector<std::vector<uint8_t>> string_attribute_args) {
1467 JNIEnv* env = fml::jni::AttachCurrentThread();
1468
1469 auto java_object = java_object_.get(env);
1470 if (java_object.is_null()) {
1471 return;
1472 }
1473
1475 env, env->NewDirectByteBuffer(buffer.data(), buffer.size()));
1477 fml::jni::VectorToStringArray(env, strings);
1478 fml::jni::ScopedJavaLocalRef<jobjectArray> jstring_attribute_args =
1479 fml::jni::VectorToBufferArray(env, string_attribute_args);
1480
1481 env->CallVoidMethod(java_object.obj(), g_update_semantics_method,
1482 direct_buffer.obj(), jstrings.obj(),
1483 jstring_attribute_args.obj());
1484
1486}
1487
1488void PlatformViewAndroidJNIImpl::FlutterViewUpdateCustomAccessibilityActions(
1489 std::vector<uint8_t> actions_buffer,
1490 std::vector<std::string> strings) {
1491 JNIEnv* env = fml::jni::AttachCurrentThread();
1492
1493 auto java_object = java_object_.get(env);
1494 if (java_object.is_null()) {
1495 return;
1496 }
1497
1498 fml::jni::ScopedJavaLocalRef<jobject> direct_actions_buffer(
1499 env,
1500 env->NewDirectByteBuffer(actions_buffer.data(), actions_buffer.size()));
1501
1503 fml::jni::VectorToStringArray(env, strings);
1504
1505 env->CallVoidMethod(java_object.obj(),
1506 g_update_custom_accessibility_actions_method,
1507 direct_actions_buffer.obj(), jstrings.obj());
1508
1510}
1511
1512void PlatformViewAndroidJNIImpl::FlutterViewOnFirstFrame() {
1513 JNIEnv* env = fml::jni::AttachCurrentThread();
1514
1515 auto java_object = java_object_.get(env);
1516 if (java_object.is_null()) {
1517 return;
1518 }
1519
1520 env->CallVoidMethod(java_object.obj(), g_on_first_frame_method);
1521
1523}
1524
1525void PlatformViewAndroidJNIImpl::FlutterViewOnPreEngineRestart() {
1526 JNIEnv* env = fml::jni::AttachCurrentThread();
1527
1528 auto java_object = java_object_.get(env);
1529 if (java_object.is_null()) {
1530 return;
1531 }
1532
1533 env->CallVoidMethod(java_object.obj(), g_on_engine_restart_method);
1534
1536}
1537
1538void PlatformViewAndroidJNIImpl::SurfaceTextureAttachToGLContext(
1539 JavaLocalRef surface_texture,
1540 int textureId) {
1541 JNIEnv* env = fml::jni::AttachCurrentThread();
1542
1543 if (surface_texture.is_null()) {
1544 return;
1545 }
1546
1547 fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref(
1548 env, env->CallObjectMethod(surface_texture.obj(),
1549 g_java_weak_reference_get_method));
1550
1551 if (surface_texture_local_ref.is_null()) {
1552 return;
1553 }
1554
1555 env->CallVoidMethod(surface_texture_local_ref.obj(),
1556 g_attach_to_gl_context_method, textureId);
1557
1559}
1560
1561bool PlatformViewAndroidJNIImpl::SurfaceTextureShouldUpdate(
1562 JavaLocalRef surface_texture) {
1563 JNIEnv* env = fml::jni::AttachCurrentThread();
1564
1565 if (surface_texture.is_null()) {
1566 return false;
1567 }
1568
1569 fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref(
1570 env, env->CallObjectMethod(surface_texture.obj(),
1571 g_java_weak_reference_get_method));
1572 if (surface_texture_local_ref.is_null()) {
1573 return false;
1574 }
1575
1576 jboolean shouldUpdate = env->CallBooleanMethod(
1577 surface_texture_local_ref.obj(), g_surface_texture_wrapper_should_update);
1578
1580
1581 return shouldUpdate;
1582}
1583
1584void PlatformViewAndroidJNIImpl::SurfaceTextureUpdateTexImage(
1585 JavaLocalRef surface_texture) {
1586 JNIEnv* env = fml::jni::AttachCurrentThread();
1587
1588 if (surface_texture.is_null()) {
1589 return;
1590 }
1591
1592 fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref(
1593 env, env->CallObjectMethod(surface_texture.obj(),
1594 g_java_weak_reference_get_method));
1595 if (surface_texture_local_ref.is_null()) {
1596 return;
1597 }
1598
1599 env->CallVoidMethod(surface_texture_local_ref.obj(),
1600 g_update_tex_image_method);
1601
1603}
1604
1605SkM44 PlatformViewAndroidJNIImpl::SurfaceTextureGetTransformMatrix(
1606 JavaLocalRef surface_texture) {
1607 JNIEnv* env = fml::jni::AttachCurrentThread();
1608
1609 if (surface_texture.is_null()) {
1610 return {};
1611 }
1612
1613 fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref(
1614 env, env->CallObjectMethod(surface_texture.obj(),
1615 g_java_weak_reference_get_method));
1616 if (surface_texture_local_ref.is_null()) {
1617 return {};
1618 }
1619
1621 env, env->NewFloatArray(16));
1622
1623 env->CallVoidMethod(surface_texture_local_ref.obj(),
1624 g_get_transform_matrix_method, transformMatrix.obj());
1626
1627 float* m = env->GetFloatArrayElements(transformMatrix.obj(), nullptr);
1628
1629 static_assert(sizeof(SkScalar) == sizeof(float));
1630 const auto transform = SkM44::ColMajor(m);
1631
1632 env->ReleaseFloatArrayElements(transformMatrix.obj(), m, JNI_ABORT);
1633
1634 return transform;
1635}
1636
1637void PlatformViewAndroidJNIImpl::SurfaceTextureDetachFromGLContext(
1638 JavaLocalRef surface_texture) {
1639 JNIEnv* env = fml::jni::AttachCurrentThread();
1640
1641 if (surface_texture.is_null()) {
1642 return;
1643 }
1644
1645 fml::jni::ScopedJavaLocalRef<jobject> surface_texture_local_ref(
1646 env, env->CallObjectMethod(surface_texture.obj(),
1647 g_java_weak_reference_get_method));
1648 if (surface_texture_local_ref.is_null()) {
1649 return;
1650 }
1651
1652 env->CallVoidMethod(surface_texture_local_ref.obj(),
1653 g_detach_from_gl_context_method);
1654
1656}
1657
1659PlatformViewAndroidJNIImpl::ImageProducerTextureEntryAcquireLatestImage(
1660 JavaLocalRef image_producer_texture_entry) {
1661 JNIEnv* env = fml::jni::AttachCurrentThread();
1662
1663 if (image_producer_texture_entry.is_null()) {
1664 // Return null.
1665 return JavaLocalRef();
1666 }
1667
1668 // Convert the weak reference to ImageTextureEntry into a strong local
1669 // reference.
1670 fml::jni::ScopedJavaLocalRef<jobject> image_producer_texture_entry_local_ref(
1671 env, env->CallObjectMethod(image_producer_texture_entry.obj(),
1672 g_java_weak_reference_get_method));
1673
1674 if (image_producer_texture_entry_local_ref.is_null()) {
1675 // Return null.
1676 return JavaLocalRef();
1677 }
1678
1680 env, env->CallObjectMethod(image_producer_texture_entry_local_ref.obj(),
1681 g_acquire_latest_image_method));
1682 if (fml::jni::CheckException(env)) {
1683 return r;
1684 }
1685 // Return null.
1686 return JavaLocalRef();
1687}
1688
1689JavaLocalRef PlatformViewAndroidJNIImpl::ImageGetHardwareBuffer(
1690 JavaLocalRef image) {
1691 FML_CHECK(g_image_get_hardware_buffer_method != nullptr);
1692 JNIEnv* env = fml::jni::AttachCurrentThread();
1693 if (image.is_null()) {
1694 // Return null.
1695 return JavaLocalRef();
1696 }
1698 env,
1699 env->CallObjectMethod(image.obj(), g_image_get_hardware_buffer_method));
1701 return r;
1702}
1703
1704void PlatformViewAndroidJNIImpl::ImageClose(JavaLocalRef image) {
1705 JNIEnv* env = fml::jni::AttachCurrentThread();
1706 if (image.is_null()) {
1707 return;
1708 }
1709 env->CallVoidMethod(image.obj(), g_image_close_method);
1711}
1712
1713void PlatformViewAndroidJNIImpl::HardwareBufferClose(
1714 JavaLocalRef hardware_buffer) {
1715 FML_CHECK(g_hardware_buffer_close_method != nullptr);
1716 JNIEnv* env = fml::jni::AttachCurrentThread();
1717 if (hardware_buffer.is_null()) {
1718 return;
1719 }
1720 env->CallVoidMethod(hardware_buffer.obj(), g_hardware_buffer_close_method);
1722}
1723
1724void PlatformViewAndroidJNIImpl::FlutterViewOnDisplayPlatformView(
1725 int view_id,
1726 int x,
1727 int y,
1728 int width,
1729 int height,
1730 int viewWidth,
1731 int viewHeight,
1732 MutatorsStack mutators_stack) {
1733 JNIEnv* env = fml::jni::AttachCurrentThread();
1734 auto java_object = java_object_.get(env);
1735 if (java_object.is_null()) {
1736 return;
1737 }
1738
1739 jobject mutatorsStack = env->NewObject(g_mutators_stack_class->obj(),
1740 g_mutators_stack_init_method);
1741
1742 std::vector<std::shared_ptr<Mutator>>::const_iterator iter =
1743 mutators_stack.Begin();
1744 while (iter != mutators_stack.End()) {
1745 switch ((*iter)->GetType()) {
1746 case MutatorType::kTransform: {
1747 const DlMatrix& matrix = (*iter)->GetMatrix();
1748 DlScalar matrix_array[9]{
1749 matrix.m[0], matrix.m[4], matrix.m[12], //
1750 matrix.m[1], matrix.m[5], matrix.m[13], //
1751 matrix.m[3], matrix.m[7], matrix.m[15],
1752 };
1754 env, env->NewFloatArray(9));
1755
1756 env->SetFloatArrayRegion(transformMatrix.obj(), 0, 9, matrix_array);
1757 env->CallVoidMethod(mutatorsStack,
1758 g_mutators_stack_push_transform_method,
1759 transformMatrix.obj());
1760 break;
1761 }
1762 case MutatorType::kClipRect: {
1763 const DlRect& rect = (*iter)->GetRect();
1764 env->CallVoidMethod(mutatorsStack,
1765 g_mutators_stack_push_cliprect_method,
1766 static_cast<int>(rect.GetLeft()), //
1767 static_cast<int>(rect.GetTop()), //
1768 static_cast<int>(rect.GetRight()), //
1769 static_cast<int>(rect.GetBottom()));
1770 break;
1771 }
1772 case MutatorType::kClipRRect: {
1773 const DlRoundRect& rrect = (*iter)->GetRRect();
1774 const DlRect& rect = rrect.GetBounds();
1775 const DlRoundingRadii radii = rrect.GetRadii();
1776 SkScalar radiis[8] = {
1777 radii.top_left.width, radii.top_left.height,
1778 radii.top_right.width, radii.top_right.height,
1779 radii.bottom_right.width, radii.bottom_right.height,
1780 radii.bottom_left.width, radii.bottom_left.height,
1781 };
1783 env, env->NewFloatArray(8));
1784 env->SetFloatArrayRegion(radiisArray.obj(), 0, 8, radiis);
1785 env->CallVoidMethod(mutatorsStack,
1786 g_mutators_stack_push_cliprrect_method,
1787 static_cast<int>(rect.GetLeft()), //
1788 static_cast<int>(rect.GetTop()), //
1789 static_cast<int>(rect.GetRight()), //
1790 static_cast<int>(rect.GetBottom()), //
1791 radiisArray.obj());
1792 break;
1793 }
1794 case MutatorType::kClipRSE: {
1795 const DlRoundRect& rrect = (*iter)->GetRSEApproximation();
1796 const DlRect& rect = rrect.GetBounds();
1797 const DlRoundingRadii radii = rrect.GetRadii();
1798 SkScalar radiis[8] = {
1799 radii.top_left.width, radii.top_left.height,
1800 radii.top_right.width, radii.top_right.height,
1801 radii.bottom_right.width, radii.bottom_right.height,
1802 radii.bottom_left.width, radii.bottom_left.height,
1803 };
1805 env, env->NewFloatArray(8));
1806 env->SetFloatArrayRegion(radiisArray.obj(), 0, 8, radiis);
1807 env->CallVoidMethod(mutatorsStack,
1808 g_mutators_stack_push_cliprrect_method,
1809 static_cast<int>(rect.GetLeft()), //
1810 static_cast<int>(rect.GetTop()), //
1811 static_cast<int>(rect.GetRight()), //
1812 static_cast<int>(rect.GetBottom()), //
1813 radiisArray.obj());
1814 break;
1815 }
1816 // TODO(cyanglaz): Implement other mutators.
1817 // https://github.com/flutter/flutter/issues/58426
1818 case MutatorType::kClipPath:
1819 case MutatorType::kOpacity:
1820 case MutatorType::kBackdropFilter:
1821 case MutatorType::kBackdropClipRect:
1822 case MutatorType::kBackdropClipRRect:
1823 case MutatorType::kBackdropClipRSuperellipse:
1824 case MutatorType::kBackdropClipPath:
1825 break;
1826 }
1827 ++iter;
1828 }
1829
1830 env->CallVoidMethod(java_object.obj(), g_on_display_platform_view_method,
1831 view_id, x, y, width, height, viewWidth, viewHeight,
1832 mutatorsStack);
1833
1835}
1836
1837void PlatformViewAndroidJNIImpl::FlutterViewDisplayOverlaySurface(
1838 int surface_id,
1839 int x,
1840 int y,
1841 int width,
1842 int height) {
1843 JNIEnv* env = fml::jni::AttachCurrentThread();
1844
1845 auto java_object = java_object_.get(env);
1846 if (java_object.is_null()) {
1847 return;
1848 }
1849
1850 env->CallVoidMethod(java_object.obj(), g_on_display_overlay_surface_method,
1851 surface_id, x, y, width, height);
1852
1854}
1855
1856void PlatformViewAndroidJNIImpl::FlutterViewBeginFrame() {
1857 JNIEnv* env = fml::jni::AttachCurrentThread();
1858
1859 auto java_object = java_object_.get(env);
1860 if (java_object.is_null()) {
1861 return;
1862 }
1863
1864 env->CallVoidMethod(java_object.obj(), g_on_begin_frame_method);
1865
1867}
1868
1869void PlatformViewAndroidJNIImpl::FlutterViewEndFrame() {
1870 JNIEnv* env = fml::jni::AttachCurrentThread();
1871
1872 auto java_object = java_object_.get(env);
1873 if (java_object.is_null()) {
1874 return;
1875 }
1876
1877 env->CallVoidMethod(java_object.obj(), g_on_end_frame_method);
1878
1880}
1881
1882std::unique_ptr<PlatformViewAndroidJNI::OverlayMetadata>
1883PlatformViewAndroidJNIImpl::FlutterViewCreateOverlaySurface() {
1884 JNIEnv* env = fml::jni::AttachCurrentThread();
1885
1886 auto java_object = java_object_.get(env);
1887 if (java_object.is_null()) {
1888 return nullptr;
1889 }
1890
1892 env, env->CallObjectMethod(java_object.obj(),
1893 g_create_overlay_surface_method));
1895
1896 if (overlay.is_null()) {
1897 return std::make_unique<PlatformViewAndroidJNI::OverlayMetadata>(0,
1898 nullptr);
1899 }
1900
1901 jint overlay_id =
1902 env->CallIntMethod(overlay.obj(), g_overlay_surface_id_method);
1903
1904 jobject overlay_surface =
1905 env->CallObjectMethod(overlay.obj(), g_overlay_surface_surface_method);
1906
1907 auto overlay_window = fml::MakeRefCounted<AndroidNativeWindow>(
1908 ANativeWindow_fromSurface(env, overlay_surface));
1909
1910 return std::make_unique<PlatformViewAndroidJNI::OverlayMetadata>(
1911 overlay_id, std::move(overlay_window));
1912}
1913
1914void PlatformViewAndroidJNIImpl::FlutterViewDestroyOverlaySurfaces() {
1915 JNIEnv* env = fml::jni::AttachCurrentThread();
1916
1917 auto java_object = java_object_.get(env);
1918 if (java_object.is_null()) {
1919 return;
1920 }
1921
1922 env->CallVoidMethod(java_object.obj(), g_destroy_overlay_surfaces_method);
1923
1925}
1926
1927std::unique_ptr<std::vector<std::string>>
1928PlatformViewAndroidJNIImpl::FlutterViewComputePlatformResolvedLocale(
1929 std::vector<std::string> supported_locales_data) {
1930 JNIEnv* env = fml::jni::AttachCurrentThread();
1931
1932 std::unique_ptr<std::vector<std::string>> out =
1933 std::make_unique<std::vector<std::string>>();
1934
1935 auto java_object = java_object_.get(env);
1936 if (java_object.is_null()) {
1937 return out;
1938 }
1940 fml::jni::VectorToStringArray(env, supported_locales_data);
1941 jobjectArray result = static_cast<jobjectArray>(env->CallObjectMethod(
1942 java_object.obj(), g_compute_platform_resolved_locale_method,
1943 j_locales_data.obj()));
1944
1946
1947 int length = env->GetArrayLength(result);
1948 for (int i = 0; i < length; i++) {
1949 out->emplace_back(fml::jni::JavaStringToString(
1950 env, static_cast<jstring>(env->GetObjectArrayElement(result, i))));
1951 }
1952 return out;
1953}
1954
1955double PlatformViewAndroidJNIImpl::GetDisplayRefreshRate() {
1956 JNIEnv* env = fml::jni::AttachCurrentThread();
1957
1958 auto java_object = java_object_.get(env);
1959 if (java_object.is_null()) {
1960 return kUnknownDisplayRefreshRate;
1961 }
1962
1964 env, env->GetObjectClass(java_object.obj()));
1965 if (clazz.is_null()) {
1966 return kUnknownDisplayRefreshRate;
1967 }
1968
1969 jfieldID fid = env->GetStaticFieldID(clazz.obj(), "refreshRateFPS", "F");
1970 return static_cast<double>(env->GetStaticFloatField(clazz.obj(), fid));
1971}
1972
1973double PlatformViewAndroidJNIImpl::GetDisplayWidth() {
1974 JNIEnv* env = fml::jni::AttachCurrentThread();
1975
1976 auto java_object = java_object_.get(env);
1977 if (java_object.is_null()) {
1978 return -1;
1979 }
1980
1982 env, env->GetObjectClass(java_object.obj()));
1983 if (clazz.is_null()) {
1984 return -1;
1985 }
1986
1987 jfieldID fid = env->GetStaticFieldID(clazz.obj(), "displayWidth", "F");
1988 return static_cast<double>(env->GetStaticFloatField(clazz.obj(), fid));
1989}
1990
1991double PlatformViewAndroidJNIImpl::GetDisplayHeight() {
1992 JNIEnv* env = fml::jni::AttachCurrentThread();
1993
1994 auto java_object = java_object_.get(env);
1995 if (java_object.is_null()) {
1996 return -1;
1997 }
1998
2000 env, env->GetObjectClass(java_object.obj()));
2001 if (clazz.is_null()) {
2002 return -1;
2003 }
2004
2005 jfieldID fid = env->GetStaticFieldID(clazz.obj(), "displayHeight", "F");
2006 return static_cast<double>(env->GetStaticFloatField(clazz.obj(), fid));
2007}
2008
2009double PlatformViewAndroidJNIImpl::GetDisplayDensity() {
2010 JNIEnv* env = fml::jni::AttachCurrentThread();
2011
2012 auto java_object = java_object_.get(env);
2013 if (java_object.is_null()) {
2014 return -1;
2015 }
2016
2018 env, env->GetObjectClass(java_object.obj()));
2019 if (clazz.is_null()) {
2020 return -1;
2021 }
2022
2023 jfieldID fid = env->GetStaticFieldID(clazz.obj(), "displayDensity", "F");
2024 return static_cast<double>(env->GetStaticFloatField(clazz.obj(), fid));
2025}
2026
2027bool PlatformViewAndroidJNIImpl::RequestDartDeferredLibrary(
2028 int loading_unit_id) {
2029 JNIEnv* env = fml::jni::AttachCurrentThread();
2030
2031 auto java_object = java_object_.get(env);
2032 if (java_object.is_null()) {
2033 return true;
2034 }
2035
2036 env->CallVoidMethod(java_object.obj(), g_request_dart_deferred_library_method,
2037 loading_unit_id);
2038
2040 return true;
2041}
2042
2043// New Platform View Support.
2044
2045ASurfaceTransaction* PlatformViewAndroidJNIImpl::createTransaction() {
2046 JNIEnv* env = fml::jni::AttachCurrentThread();
2047
2048 auto java_object = java_object_.get(env);
2049 if (java_object.is_null()) {
2050 return nullptr;
2051 }
2052
2054 env,
2055 env->CallObjectMethod(java_object.obj(), g_create_transaction_method));
2056 if (transaction.is_null()) {
2057 return nullptr;
2058 }
2060
2061 return impeller::android::GetProcTable().ASurfaceTransaction_fromJava(
2062 env, transaction.obj());
2063}
2064
2065void PlatformViewAndroidJNIImpl::swapTransaction() {
2066 JNIEnv* env = fml::jni::AttachCurrentThread();
2067
2068 auto java_object = java_object_.get(env);
2069 if (java_object.is_null()) {
2070 return;
2071 }
2072
2073 env->CallVoidMethod(java_object.obj(), g_swap_transaction_method);
2074
2076}
2077
2078void PlatformViewAndroidJNIImpl::applyTransaction() {
2079 JNIEnv* env = fml::jni::AttachCurrentThread();
2080
2081 auto java_object = java_object_.get(env);
2082 if (java_object.is_null()) {
2083 return;
2084 }
2085
2086 env->CallVoidMethod(java_object.obj(), g_apply_transaction_method);
2087
2089}
2090
2091std::unique_ptr<PlatformViewAndroidJNI::OverlayMetadata>
2092PlatformViewAndroidJNIImpl::createOverlaySurface2() {
2093 JNIEnv* env = fml::jni::AttachCurrentThread();
2094
2095 auto java_object = java_object_.get(env);
2096 if (java_object.is_null()) {
2097 return nullptr;
2098 }
2099
2101 env, env->CallObjectMethod(java_object.obj(),
2102 g_create_overlay_surface2_method));
2104
2105 if (overlay.is_null()) {
2106 return std::make_unique<PlatformViewAndroidJNI::OverlayMetadata>(0,
2107 nullptr);
2108 }
2109
2110 jint overlay_id =
2111 env->CallIntMethod(overlay.obj(), g_overlay_surface_id_method);
2112
2113 jobject overlay_surface =
2114 env->CallObjectMethod(overlay.obj(), g_overlay_surface_surface_method);
2115
2116 auto overlay_window = fml::MakeRefCounted<AndroidNativeWindow>(
2117 ANativeWindow_fromSurface(env, overlay_surface));
2118
2119 return std::make_unique<PlatformViewAndroidJNI::OverlayMetadata>(
2120 overlay_id, std::move(overlay_window));
2121}
2122
2123void PlatformViewAndroidJNIImpl::destroyOverlaySurface2() {
2124 JNIEnv* env = fml::jni::AttachCurrentThread();
2125
2126 auto java_object = java_object_.get(env);
2127 if (java_object.is_null()) {
2128 return;
2129 }
2130
2131 env->CallVoidMethod(java_object.obj(), g_destroy_overlay_surface2_method);
2132
2134}
2135
2136namespace {
2137class AndroidPathReceiver final : public DlPathReceiver {
2138 public:
2139 explicit AndroidPathReceiver(JNIEnv* env)
2140 : env_(env),
2141 android_path_(env->NewObject(path_class->obj(), path_constructor)) {}
2142
2143 void SetFillType(DlPathFillType type) {
2144 jfieldID fill_type_field_id;
2145 switch (type) {
2146 case DlPathFillType::kOdd:
2147 fill_type_field_id = g_path_fill_type_even_odd_field;
2148 break;
2149 case DlPathFillType::kNonZero:
2150 fill_type_field_id = g_path_fill_type_winding_field;
2151 break;
2152 default:
2153 // DlPathFillType does not have corresponding kInverseEvenOdd or
2154 // kInverseWinding fill types.
2155 return;
2156 }
2157
2158 // Get the static enum field value (Path.FillType.WINDING or
2159 // Path.FillType.EVEN_ODD)
2162 env_, env_->GetStaticObjectField(g_path_fill_type_class->obj(),
2163 fill_type_field_id));
2165 FML_CHECK(!fill_type_enum.is_null());
2166
2167 // Call Path.setFillType(Path.FillType)
2168 env_->CallVoidMethod(android_path_, path_set_fill_type_method,
2169 fill_type_enum.obj());
2171 }
2172
2173 void MoveTo(const DlPoint& p2, bool will_be_closed) override {
2174 env_->CallVoidMethod(android_path_, path_move_to_method, p2.x, p2.y);
2175 }
2176 void LineTo(const DlPoint& p2) override {
2177 env_->CallVoidMethod(android_path_, path_line_to_method, p2.x, p2.y);
2178 }
2179 void QuadTo(const DlPoint& cp, const DlPoint& p2) override {
2180 env_->CallVoidMethod(android_path_, path_quad_to_method, //
2181 cp.x, cp.y, p2.x, p2.y);
2182 }
2183 bool ConicTo(const DlPoint& cp, const DlPoint& p2, DlScalar weight) override {
2184 if (!path_conic_to_method) {
2185 return false;
2186 }
2187 env_->CallVoidMethod(android_path_, path_conic_to_method, //
2188 cp.x, cp.y, p2.x, p2.y, weight);
2189 return true;
2190 };
2191 void CubicTo(const DlPoint& cp1,
2192 const DlPoint& cp2,
2193 const DlPoint& p2) override {
2194 env_->CallVoidMethod(android_path_, path_cubic_to_method, //
2195 cp1.x, cp1.y, cp2.x, cp2.y, p2.x, p2.y);
2196 }
2197 void Close() override {
2198 env_->CallVoidMethod(android_path_, path_close_method);
2199 }
2200
2201 jobject TakePath() const { return android_path_; }
2202
2203 private:
2204 JNIEnv* env_;
2205 jobject android_path_;
2206};
2207} // namespace
2208
2209void PlatformViewAndroidJNIImpl::onDisplayPlatformView2(
2210 int32_t view_id,
2211 int32_t x,
2212 int32_t y,
2213 int32_t width,
2214 int32_t height,
2215 int32_t viewWidth,
2216 int32_t viewHeight,
2217 MutatorsStack mutators_stack) {
2218 JNIEnv* env = fml::jni::AttachCurrentThread();
2219 auto java_object = java_object_.get(env);
2220 if (java_object.is_null()) {
2221 return;
2222 }
2223
2224 jobject mutatorsStack = env->NewObject(g_mutators_stack_class->obj(),
2225 g_mutators_stack_init_method);
2226
2227 std::vector<std::shared_ptr<Mutator>>::const_iterator iter =
2228 mutators_stack.Begin();
2229 while (iter != mutators_stack.End()) {
2230 switch ((*iter)->GetType()) {
2231 case MutatorType::kTransform: {
2232 const DlMatrix& matrix = (*iter)->GetMatrix();
2233 DlScalar matrix_array[9]{
2234 matrix.m[0], matrix.m[4], matrix.m[12], //
2235 matrix.m[1], matrix.m[5], matrix.m[13], //
2236 matrix.m[3], matrix.m[7], matrix.m[15],
2237 };
2239 env, env->NewFloatArray(9));
2240
2241 env->SetFloatArrayRegion(transformMatrix.obj(), 0, 9, matrix_array);
2242 env->CallVoidMethod(mutatorsStack,
2243 g_mutators_stack_push_transform_method,
2244 transformMatrix.obj());
2245 break;
2246 }
2247 case MutatorType::kClipRect: {
2248 const DlRect& rect = (*iter)->GetRect();
2249 env->CallVoidMethod(mutatorsStack,
2250 g_mutators_stack_push_cliprect_method,
2251 static_cast<int>(rect.GetLeft()), //
2252 static_cast<int>(rect.GetTop()), //
2253 static_cast<int>(rect.GetRight()), //
2254 static_cast<int>(rect.GetBottom()));
2255 break;
2256 }
2257 case MutatorType::kClipRRect: {
2258 const DlRoundRect& rrect = (*iter)->GetRRect();
2259 const DlRect& rect = rrect.GetBounds();
2260 const DlRoundingRadii& radii = rrect.GetRadii();
2261 SkScalar radiis[8] = {
2262 radii.top_left.width, radii.top_left.height,
2263 radii.top_right.width, radii.top_right.height,
2264 radii.bottom_right.width, radii.bottom_right.height,
2265 radii.bottom_left.width, radii.bottom_left.height,
2266 };
2268 env, env->NewFloatArray(8));
2269 env->SetFloatArrayRegion(radiisArray.obj(), 0, 8, radiis);
2270 env->CallVoidMethod(mutatorsStack,
2271 g_mutators_stack_push_cliprrect_method,
2272 static_cast<int>(rect.GetLeft()), //
2273 static_cast<int>(rect.GetTop()), //
2274 static_cast<int>(rect.GetRight()), //
2275 static_cast<int>(rect.GetBottom()), //
2276 radiisArray.obj());
2277 break;
2278 }
2279 case MutatorType::kClipRSE: {
2280 const DlRoundRect& rrect = (*iter)->GetRSEApproximation();
2281 const DlRect& rect = rrect.GetBounds();
2282 const DlRoundingRadii& radii = rrect.GetRadii();
2283 SkScalar radiis[8] = {
2284 radii.top_left.width, radii.top_left.height,
2285 radii.top_right.width, radii.top_right.height,
2286 radii.bottom_right.width, radii.bottom_right.height,
2287 radii.bottom_left.width, radii.bottom_left.height,
2288 };
2290 env, env->NewFloatArray(8));
2291 env->SetFloatArrayRegion(radiisArray.obj(), 0, 8, radiis);
2292 env->CallVoidMethod(mutatorsStack,
2293 g_mutators_stack_push_cliprrect_method,
2294 static_cast<int>(rect.GetLeft()), //
2295 static_cast<int>(rect.GetTop()), //
2296 static_cast<int>(rect.GetRight()), //
2297 static_cast<int>(rect.GetBottom()), //
2298 radiisArray.obj());
2299 break;
2300 }
2301 case MutatorType::kOpacity: {
2302 float opacity = (*iter)->GetAlphaFloat();
2303 env->CallVoidMethod(mutatorsStack, g_mutators_stack_push_opacity_method,
2304 opacity);
2305 break;
2306 }
2307 case MutatorType::kClipPath: {
2308 auto& dlPath = (*iter)->GetPath();
2309 // The layer mutator mechanism should have already caught and
2310 // redirected these simplified path cases, which is important because
2311 // the conics they generate (in the case of oval and rrect) will
2312 // not match the results of an impeller path conversion very closely.
2313 FML_DCHECK(!dlPath.IsRect());
2314 FML_DCHECK(!dlPath.IsOval());
2315 FML_DCHECK(!dlPath.IsRoundRect());
2316
2317 // Define and populate an Android Path with data from the DlPath
2318 AndroidPathReceiver receiver(env);
2319 receiver.SetFillType(dlPath.GetFillType());
2320
2321 // TODO(flar): https://github.com/flutter/flutter/issues/164808
2322 // Need to convert the fill type to the Android enum and
2323 // call setFillType on the path...
2324 dlPath.Dispatch(receiver);
2325
2326 env->CallVoidMethod(mutatorsStack,
2327 g_mutators_stack_push_clippath_method,
2328 receiver.TakePath());
2329 break;
2330 }
2331 // TODO(cyanglaz): Implement other mutators.
2332 // https://github.com/flutter/flutter/issues/58426
2333 case MutatorType::kBackdropFilter:
2334 case MutatorType::kBackdropClipRect:
2335 case MutatorType::kBackdropClipRRect:
2336 case MutatorType::kBackdropClipRSuperellipse:
2337 case MutatorType::kBackdropClipPath:
2338 break;
2339 }
2340 ++iter;
2341 }
2342
2343 env->CallVoidMethod(java_object.obj(), g_on_display_platform_view2_method,
2344 view_id, x, y, width, height, viewWidth, viewHeight,
2345 mutatorsStack);
2346
2348}
2349
2350void PlatformViewAndroidJNIImpl::hidePlatformView2(int32_t view_id) {
2351 JNIEnv* env = fml::jni::AttachCurrentThread();
2352 auto java_object = java_object_.get(env);
2353 if (java_object.is_null()) {
2354 return;
2355 }
2356
2357 env->CallVoidMethod(java_object.obj(), g_hide_platform_view2_method, view_id);
2358}
2359
2360void PlatformViewAndroidJNIImpl::onEndFrame2() {
2361 JNIEnv* env = fml::jni::AttachCurrentThread();
2362
2363 auto java_object = java_object_.get(env);
2364 if (java_object.is_null()) {
2365 return;
2366 }
2367
2368 env->CallVoidMethod(java_object.obj(), g_on_end_frame2_method);
2369
2371}
2372
2373void PlatformViewAndroidJNIImpl::showOverlaySurface2() {
2374 JNIEnv* env = fml::jni::AttachCurrentThread();
2375
2376 auto java_object = java_object_.get(env);
2377 if (java_object.is_null()) {
2378 return;
2379 }
2380
2381 env->CallVoidMethod(java_object.obj(), g_show_overlay_surface2_method);
2383}
2384
2385void PlatformViewAndroidJNIImpl::hideOverlaySurface2() {
2386 JNIEnv* env = fml::jni::AttachCurrentThread();
2387
2388 auto java_object = java_object_.get(env);
2389 if (java_object.is_null()) {
2390 return;
2391 }
2392
2393 env->CallVoidMethod(java_object.obj(), g_hide_overlay_surface2_method);
2395}
2396
2397void PlatformViewAndroidJNIImpl::MaybeResizeSurfaceView(int32_t width,
2398 int32_t height) const {
2399 JNIEnv* env = fml::jni::AttachCurrentThread();
2400
2401 auto java_object = java_object_.get(env);
2402 if (java_object.is_null()) {
2403 return;
2404 }
2405
2406 env->CallVoidMethod(java_object.obj(), g_maybe_resize_surface_view, width,
2407 height);
2409}
2410
2411} // namespace flutter
GLenum type
static std::unique_ptr< DartCallbackRepresentation > GetCallbackInformation(int64_t handle)
A Mapping like NonOwnedMapping, but uses Free as its release proc.
Definition mapping.h:144
static MallocMapping Copy(const T *begin, const T *end)
Definition mapping.h:162
uint8_t * Release()
Definition mapping.cc:155
static fml::RefPtr< NativeLibrary > CreateWithHandle(Handle handle, bool close_handle_when_done)
int32_t x
FlutterVulkanImage * image
GLFWwindow * window
Definition main.cc:60
const char * message
G_BEGIN_DECLS G_MODULE_EXPORT FlValue * args
const gchar * channel
G_BEGIN_DECLS FlutterViewId view_id
#define FML_LOG(severity)
Definition logging.h:101
#define FML_CHECK(condition)
Definition logging.h:104
#define FML_DCHECK(condition)
Definition logging.h:122
std::shared_ptr< SkBitmap > bitmap
size_t length
double y
static jmethodID g_hardware_buffer_close_method
impeller::Scalar DlScalar
static void RunBundleAndSnapshotFromLibrary(JNIEnv *env, jobject jcaller, jlong shell_holder, jstring jBundlePath, jstring jEntrypoint, jstring jLibraryUrl, jobject jAssetManager, jobject jEntrypointArgs, jlong engineId)
impeller::PathReceiver DlPathReceiver
Definition dl_path.h:17
static jmethodID g_mutators_stack_init_method
impeller::RoundingRadii DlRoundingRadii
static jfieldID g_jni_shell_holder_field
static void InvokePlatformMessageResponseCallback(JNIEnv *env, jobject jcaller, jlong shell_holder, jint responseId, jobject message, jint position)
static jmethodID g_request_dart_deferred_library_method
static jboolean GetIsSoftwareRendering(JNIEnv *env, jobject jcaller)
static bool IsSurfaceControlEnabled(JNIEnv *env, jobject jcaller, jlong shell_holder)
static void DispatchPointerDataPacket(JNIEnv *env, jobject jcaller, jlong shell_holder, jobject buffer, jint position)
bool RegisterApi(JNIEnv *env)
impeller::RoundRect DlRoundRect
static jmethodID g_mutators_stack_push_clippath_method
static jboolean FlutterTextUtilsIsEmojiModifier(JNIEnv *env, jobject obj, jint codePoint)
static fml::jni::ScopedJavaGlobalRef< jclass > * path_class
static fml::jni::ScopedJavaGlobalRef< jclass > * g_java_long_class
static void LoadLoadingUnitFailure(intptr_t loading_unit_id, const std::string &message, bool transient)
static void InvokePlatformMessageEmptyResponseCallback(JNIEnv *env, jobject jcaller, jlong shell_holder, jint responseId)
impeller::Matrix DlMatrix
impeller::Rect DlRect
impeller::ISize32 DlISize
static jmethodID g_acquire_latest_image_method
static jmethodID g_mutators_stack_push_transform_method
static fml::jni::ScopedJavaGlobalRef< jclass > * g_bitmap_class
std::nullptr_t JavaLocalRef
static void SurfaceDestroyed(JNIEnv *env, jobject jcaller, jlong shell_holder)
static jmethodID path_line_to_method
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir path
Definition switch_defs.h:52
static jmethodID path_quad_to_method
static jmethodID g_long_constructor
static void DestroyJNI(JNIEnv *env, jobject jcaller, jlong shell_holder)
static void DispatchSemanticsAction(JNIEnv *env, jobject jcaller, jlong shell_holder, jint id, jint action, jobject args, jint args_position)
static jmethodID g_jni_constructor
static void MarkTextureFrameAvailable(JNIEnv *env, jobject jcaller, jlong shell_holder, jlong texture_id)
static jmethodID g_attach_to_gl_context_method
static jfieldID g_path_fill_type_even_odd_field
static void SetSemanticsEnabled(JNIEnv *env, jobject jcaller, jlong shell_holder, jboolean enabled)
static void DeferredComponentInstallFailure(JNIEnv *env, jobject obj, jint jLoadingUnitId, jstring jError, jboolean jTransient)
static jobject SpawnJNI(JNIEnv *env, jobject jcaller, jlong shell_holder, jstring jEntrypoint, jstring jLibraryUrl, jstring jInitialRoute, jobject jEntrypointArgs, jlong engineId)
static jfieldID g_path_fill_type_winding_field
static fml::jni::ScopedJavaGlobalRef< jclass > * g_texture_wrapper_class
static void LoadDartDeferredLibrary(JNIEnv *env, jobject obj, jlong shell_holder, jint jLoadingUnitId, jobjectArray jSearchPaths)
static jmethodID g_java_weak_reference_get_method
static void NotifyLowMemoryWarning(JNIEnv *env, jobject obj, jlong shell_holder)
static jboolean FlutterTextUtilsIsVariationSelector(JNIEnv *env, jobject obj, jint codePoint)
static jmethodID g_image_get_hardware_buffer_method
static void CleanupMessageData(JNIEnv *env, jobject jcaller, jlong message_data)
static jmethodID g_mutators_stack_push_cliprect_method
static jmethodID g_overlay_surface_surface_method
static void SetViewportMetrics(JNIEnv *env, jobject jcaller, jlong shell_holder, jfloat devicePixelRatio, jint physicalWidth, jint physicalHeight, jint physicalPaddingTop, jint physicalPaddingRight, jint physicalPaddingBottom, jint physicalPaddingLeft, jint physicalViewInsetTop, jint physicalViewInsetRight, jint physicalViewInsetBottom, jint physicalViewInsetLeft, jint systemGestureInsetTop, jint systemGestureInsetRight, jint systemGestureInsetBottom, jint systemGestureInsetLeft, jint physicalTouchSlop, jintArray javaDisplayFeaturesBounds, jintArray javaDisplayFeaturesType, jintArray javaDisplayFeaturesState, jint physicalMinWidth, jint physicalMaxWidth, jint physicalMinHeight, jint physicalMaxHeight, jint physicalDisplayCornerRadiusTopLeft, jint physicalDisplayCornerRadiusTopRight, jint physicalDisplayCornerRadiusBottomRight, jint physicalDisplayCornerRadiusBottomLeft)
static void DispatchPlatformMessage(JNIEnv *env, jobject jcaller, jlong shell_holder, jstring channel, jobject message, jint position, jint responseId)
static jmethodID g_overlay_surface_id_method
static jmethodID g_mutators_stack_push_cliprrect_method
static jmethodID g_get_transform_matrix_method
static jmethodID path_cubic_to_method
static jmethodID g_compute_platform_resolved_locale_method
static void SurfaceWindowChanged(JNIEnv *env, jobject jcaller, jlong shell_holder, jobject jsurface)
static jmethodID g_bitmap_copy_pixels_from_buffer_method
static void RegisterImageTexture(JNIEnv *env, jobject jcaller, jlong shell_holder, jlong texture_id, jobject image_texture_entry, jboolean reset_on_background)
static fml::jni::ScopedJavaGlobalRef< jclass > * g_flutter_jni_class
static fml::jni::ScopedJavaGlobalRef< jclass > * g_bitmap_config_class
static jmethodID g_surface_texture_wrapper_should_update
static fml::jni::ScopedJavaGlobalRef< jclass > * g_image_class
static void SurfaceCreated(JNIEnv *env, jobject jcaller, jlong shell_holder, jobject jsurface)
static jmethodID g_flutter_callback_info_constructor
static jmethodID path_set_fill_type_method
static void DispatchEmptyPlatformMessage(JNIEnv *env, jobject jcaller, jlong shell_holder, jstring channel, jint responseId)
static fml::jni::ScopedJavaGlobalRef< jclass > * g_path_fill_type_class
static void ScheduleFrame(JNIEnv *env, jobject jcaller, jlong shell_holder)
static jmethodID path_move_to_method
static jlong AttachJNI(JNIEnv *env, jclass clazz, jobject flutterJNI)
static void SetAccessibilityFeatures(JNIEnv *env, jobject jcaller, jlong shell_holder, jint flags)
static jmethodID g_update_tex_image_method
static fml::jni::ScopedJavaGlobalRef< jclass > * g_java_weak_reference_class
static jmethodID g_image_close_method
static void UpdateDisplayMetrics(JNIEnv *env, jobject jcaller, jlong shell_holder)
static jmethodID g_mutators_stack_push_opacity_method
static void SurfaceChanged(JNIEnv *env, jobject jcaller, jlong shell_holder, jint width, jint height)
static jmethodID g_bitmap_config_value_of
static fml::jni::ScopedJavaGlobalRef< jclass > * g_image_consumer_texture_registry_interface
static void UpdateJavaAssetManager(JNIEnv *env, jobject obj, jlong shell_holder, jobject jAssetManager, jstring jAssetBundlePath)
static jobject GetBitmap(JNIEnv *env, jobject jcaller, jlong shell_holder)
static jmethodID path_constructor
static jmethodID g_bitmap_create_bitmap_method
static void UnregisterTexture(JNIEnv *env, jobject jcaller, jlong shell_holder, jlong texture_id)
static fml::jni::ScopedJavaGlobalRef< jclass > * g_flutter_callback_info_class
static fml::jni::ScopedJavaGlobalRef< jclass > * g_mutators_stack_class
static jmethodID path_close_method
static jboolean FlutterTextUtilsIsEmojiModifierBase(JNIEnv *env, jobject obj, jint codePoint)
static jobject LookupCallbackInformation(JNIEnv *env, jobject, jlong handle)
static jboolean FlutterTextUtilsIsRegionalIndicator(JNIEnv *env, jobject obj, jint codePoint)
static jmethodID g_detach_from_gl_context_method
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set profile Make the profiler discard new samples once the profiler sample buffer is full When this flag is not the profiler sample buffer is used as a ring buffer
Definition switch_defs.h:98
static jmethodID path_conic_to_method
static fml::jni::ScopedJavaGlobalRef< jclass > * g_hardware_buffer_class
static jboolean FlutterTextUtilsIsEmoji(JNIEnv *env, jobject obj, jint codePoint)
static void RegisterTexture(JNIEnv *env, jobject jcaller, jlong shell_holder, jlong texture_id, jobject surface_texture)
JNIEnv * AttachCurrentThread()
Definition jni_util.cc:34
ScopedJavaLocalRef< jobjectArray > VectorToBufferArray(JNIEnv *env, const std::vector< std::vector< uint8_t > > &vector)
Definition jni_util.cc:164
std::string JavaStringToString(JNIEnv *env, jstring str)
Definition jni_util.cc:70
bool ClearException(JNIEnv *env, bool silent)
Definition jni_util.cc:188
bool CheckException(JNIEnv *env)
Definition jni_util.cc:199
ScopedJavaLocalRef< jobjectArray > VectorToStringArray(JNIEnv *env, const std::vector< std::string > &vector)
Definition jni_util.cc:147
std::vector< std::string > StringListToVector(JNIEnv *env, jobject list)
Definition jni_util.cc:118
std::vector< std::string > StringArrayToVector(JNIEnv *env, jobjectArray array)
Definition jni_util.cc:96
ScopedJavaLocalRef< jstring > StringToJavaString(JNIEnv *env, const std::string &u8_string)
Definition jni_util.cc:86
const ProcTable & GetProcTable()
Definition proc_table.cc:12
void MoveTo(PathBuilder *builder, Scalar x, Scalar y)
void LineTo(PathBuilder *builder, Scalar x, Scalar y)
void CubicTo(PathBuilder *builder, Scalar x1, Scalar y1, Scalar x2, Scalar y2, Scalar x3, Scalar y3)
void Close(PathBuilder *builder)
#define ANDROID_SHELL_HOLDER
#define FLUTTER_BIND_JNI(global_field, jni_name, jni_arg)
#define FLUTTER_DECLARE_JNI(global_field, jni_name, jni_arg)
*end#*‍/
#define FLUTTER_FOR_EACH_JNI_METHOD(V)
int32_t height
int32_t width
Type width
Definition size.h:28
Scalar font_size
std::shared_ptr< const fml::Mapping > data
int64_t texture_id

◆ FLUTTER_BIND_JNI

#define FLUTTER_BIND_JNI (   global_field,
  jni_name,
  jni_arg 
)
Value:
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; \
}

Definition at line 110 of file platform_view_android_jni_impl.cc.

113 { \
114 FML_LOG(ERROR) << "Could not locate " << #jni_name << " method."; \
115 return false; \
116 }

◆ FLUTTER_DECLARE_JNI

#define FLUTTER_DECLARE_JNI (   global_field,
  jni_name,
  jni_arg 
)     static jmethodID global_field = nullptr;

*end#*‍/

Definition at line 107 of file platform_view_android_jni_impl.cc.

◆ FLUTTER_FOR_EACH_JNI_METHOD

#define FLUTTER_FOR_EACH_JNI_METHOD (   V)

Definition at line 65 of file platform_view_android_jni_impl.cc.