@@ -49,6 +49,10 @@ static fml::jni::ScopedJavaGlobalRef<jclass>* g_texture_wrapper_class = nullptr;
4949
5050static fml::jni::ScopedJavaGlobalRef<jclass>* g_java_long_class = nullptr ;
5151
52+ static fml::jni::ScopedJavaGlobalRef<jclass>* g_bitmap_class = nullptr ;
53+
54+ static fml::jni::ScopedJavaGlobalRef<jclass>* g_bitmap_config_class = nullptr ;
55+
5256// Called By Native
5357
5458static jmethodID g_flutter_callback_info_constructor = nullptr ;
@@ -115,6 +119,12 @@ static jmethodID g_overlay_surface_id_method = nullptr;
115119
116120static jmethodID g_overlay_surface_surface_method = nullptr ;
117121
122+ static jmethodID g_bitmap_create_bitmap_method = nullptr ;
123+
124+ static jmethodID g_bitmap_copy_pixels_from_buffer_method = nullptr ;
125+
126+ static jmethodID g_bitmap_config_value_of = nullptr ;
127+
118128// Mutators
119129static fml::jni::ScopedJavaGlobalRef<jclass>* g_mutators_stack_class = nullptr ;
120130static jmethodID g_mutators_stack_init_method = nullptr ;
@@ -337,70 +347,31 @@ static jobject GetBitmap(JNIEnv* env, jobject jcaller, jlong shell_holder) {
337347 return nullptr ;
338348 }
339349
340- const SkISize& frame_size = screenshot.frame_size ;
341- jsize pixels_size = frame_size.width () * frame_size.height ();
342- jintArray pixels_array = env->NewIntArray (pixels_size);
343- if (pixels_array == nullptr ) {
344- return nullptr ;
345- }
346-
347- jint* pixels = env->GetIntArrayElements (pixels_array, nullptr );
348- if (pixels == nullptr ) {
349- return nullptr ;
350- }
351-
352- auto * pixels_src = static_cast <const int32_t *>(screenshot.data ->data ());
353-
354- // Our configuration of Skia does not support rendering to the
355- // BitmapConfig.ARGB_8888 format expected by android.graphics.Bitmap.
356- // Convert from kRGBA_8888 to kBGRA_8888 (equivalent to ARGB_8888).
357- for (int i = 0 ; i < pixels_size; i++) {
358- int32_t src_pixel = pixels_src[i];
359- uint8_t * src_bytes = reinterpret_cast <uint8_t *>(&src_pixel);
360- std::swap (src_bytes[0 ], src_bytes[2 ]);
361- pixels[i] = src_pixel;
362- }
363-
364- env->ReleaseIntArrayElements (pixels_array, pixels, 0 );
365-
366- jclass bitmap_class = env->FindClass (" android/graphics/Bitmap" );
367- if (bitmap_class == nullptr ) {
368- return nullptr ;
369- }
370-
371- jmethodID create_bitmap = env->GetStaticMethodID (
372- bitmap_class, " createBitmap" ,
373- " ([IIILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;" );
374- if (create_bitmap == nullptr ) {
375- return nullptr ;
376- }
377-
378- jclass bitmap_config_class = env->FindClass (" android/graphics/Bitmap$Config" );
379- if (bitmap_config_class == nullptr ) {
380- return nullptr ;
381- }
382-
383- jmethodID bitmap_config_value_of = env->GetStaticMethodID (
384- bitmap_config_class, " valueOf" ,
385- " (Ljava/lang/String;)Landroid/graphics/Bitmap$Config;" );
386- if (bitmap_config_value_of == nullptr ) {
387- return nullptr ;
388- }
389-
390350 jstring argb = env->NewStringUTF (" ARGB_8888" );
391351 if (argb == nullptr ) {
392352 return nullptr ;
393353 }
394354
395355 jobject bitmap_config = env->CallStaticObjectMethod (
396- bitmap_config_class, bitmap_config_value_of , argb);
356+ g_bitmap_config_class-> obj (), g_bitmap_config_value_of , argb);
397357 if (bitmap_config == nullptr ) {
398358 return nullptr ;
399359 }
400360
401- return env->CallStaticObjectMethod (bitmap_class, create_bitmap, pixels_array,
402- frame_size.width (), frame_size.height (),
403- bitmap_config);
361+ auto bitmap = env->CallStaticObjectMethod (
362+ g_bitmap_class->obj (), g_bitmap_create_bitmap_method,
363+ screenshot.frame_size .width (), screenshot.frame_size .height (),
364+ bitmap_config);
365+
366+ fml::jni::ScopedJavaLocalRef<jobject> buffer (
367+ env,
368+ env->NewDirectByteBuffer (const_cast <uint8_t *>(screenshot.data ->bytes ()),
369+ screenshot.data ->size ()));
370+
371+ env->CallVoidMethod (bitmap, g_bitmap_copy_pixels_from_buffer_method,
372+ buffer.obj ());
373+
374+ return bitmap;
404375}
405376
406377static void DispatchPlatformMessage (JNIEnv* env,
@@ -945,6 +916,43 @@ bool RegisterApi(JNIEnv* env) {
945916 return false ;
946917 }
947918
919+ g_bitmap_class = new fml::jni::ScopedJavaGlobalRef<jclass>(
920+ env, env->FindClass (" android/graphics/Bitmap" ));
921+ if (g_bitmap_class->is_null ()) {
922+ FML_LOG (ERROR) << " Could not locate Bitmap Class" ;
923+ return false ;
924+ }
925+
926+ g_bitmap_create_bitmap_method = env->GetStaticMethodID (
927+ g_bitmap_class->obj (), " createBitmap" ,
928+ " (IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;" );
929+ if (g_bitmap_create_bitmap_method == nullptr ) {
930+ FML_LOG (ERROR) << " Could not locate Bitmap.createBitmap method" ;
931+ return false ;
932+ }
933+
934+ g_bitmap_copy_pixels_from_buffer_method = env->GetMethodID (
935+ g_bitmap_class->obj (), " copyPixelsFromBuffer" , " (Ljava/nio/Buffer;)V" );
936+ if (g_bitmap_copy_pixels_from_buffer_method == nullptr ) {
937+ FML_LOG (ERROR) << " Could not locate Bitmap.copyPixelsFromBuffer method" ;
938+ return false ;
939+ }
940+
941+ g_bitmap_config_class = new fml::jni::ScopedJavaGlobalRef<jclass>(
942+ env, env->FindClass (" android/graphics/Bitmap$Config" ));
943+ if (g_bitmap_config_class->is_null ()) {
944+ FML_LOG (ERROR) << " Could not locate Bitmap.Config Class" ;
945+ return false ;
946+ }
947+
948+ g_bitmap_config_value_of = env->GetStaticMethodID (
949+ g_bitmap_config_class->obj (), " valueOf" ,
950+ " (Ljava/lang/String;)Landroid/graphics/Bitmap$Config;" );
951+ if (g_bitmap_config_value_of == nullptr ) {
952+ FML_LOG (ERROR) << " Could not locate Bitmap.Config.valueOf method" ;
953+ return false ;
954+ }
955+
948956 return true ;
949957}
950958
0 commit comments