diff --git a/src/api-impl-jni/generated_headers/android_graphics_Bitmap.h b/src/api-impl-jni/generated_headers/android_graphics_Bitmap.h index cdcbb302..1e5bab00 100644 --- a/src/api-impl-jni/generated_headers/android_graphics_Bitmap.h +++ b/src/api-impl-jni/generated_headers/android_graphics_Bitmap.h @@ -18,10 +18,10 @@ JNIEXPORT jlong JNICALL Java_android_graphics_Bitmap_native_1create_1snapshot /* * Class: android_graphics_Bitmap * Method: native_create_texture - * Signature: (JII)J + * Signature: (JIIII)J */ JNIEXPORT jlong JNICALL Java_android_graphics_Bitmap_native_1create_1texture - (JNIEnv *, jclass, jlong, jint, jint); + (JNIEnv *, jclass, jlong, jint, jint, jint, jint); /* * Class: android_graphics_Bitmap diff --git a/src/api-impl-jni/graphics/android_graphics_Bitmap.c b/src/api-impl-jni/graphics/android_graphics_Bitmap.c index 35031e5a..f099e760 100644 --- a/src/api-impl-jni/graphics/android_graphics_Bitmap.c +++ b/src/api-impl-jni/graphics/android_graphics_Bitmap.c @@ -16,29 +16,38 @@ JNIEXPORT jlong JNICALL Java_android_graphics_Bitmap_native_1create_1snapshot(JN return _INTPTR(snapshot); } -JNIEXPORT jlong JNICALL Java_android_graphics_Bitmap_native_1create_1texture(JNIEnv *env, jclass class, jlong snapshot_ptr, jint width, jint height) +JNIEXPORT jlong JNICALL Java_android_graphics_Bitmap_native_1create_1texture(JNIEnv *env, jclass class, jlong snapshot_ptr, jint width, jint height, jint stride, jint format) { - GtkSnapshot *snapshot = _PTR(snapshot_ptr); static GType renderer_type = 0; - if (!renderer_type) { - // Use same renderer type as for onscreen rendering. - GdkSurface *surface = gdk_surface_new_toplevel(gdk_display_get_default()); - GskRenderer *renderer = gsk_renderer_new_for_surface(surface); - renderer_type = G_OBJECT_TYPE(renderer); + GtkSnapshot *snapshot = _PTR(snapshot_ptr); + GskRenderNode *node = snapshot ? gtk_snapshot_free_to_node(snapshot) : NULL; + GdkTexture *texture = NULL; + if (node) { + graphene_rect_t bounds = GRAPHENE_RECT_INIT(0, 0, width, height); + if (!renderer_type) { + // Use same renderer type as for onscreen rendering. + GdkSurface *surface = gdk_surface_new_toplevel(gdk_display_get_default()); + GskRenderer *renderer = gsk_renderer_new_for_surface(surface); + renderer_type = G_OBJECT_TYPE(renderer); + gsk_renderer_unrealize(renderer); + g_object_unref(renderer); + gdk_surface_destroy(surface); + } + GskRenderer *renderer = g_object_new(renderer_type, NULL); + gsk_renderer_realize(renderer, NULL, NULL); + texture = gsk_renderer_render_texture(renderer, node, &bounds); + gsk_render_node_unref(node); gsk_renderer_unrealize(renderer); g_object_unref(renderer); - gdk_surface_destroy(surface); + } else { + if (format == -1) { + format = GDK_MEMORY_R8G8B8A8; + stride = width * 4; + } + GBytes *bytes = g_bytes_new_take(g_malloc0(height * stride), height * stride); + texture = gdk_memory_texture_new(width, height, format, bytes, stride); + g_bytes_unref(bytes); } - GskRenderer *renderer = g_object_new(renderer_type, NULL); - gsk_renderer_realize(renderer, NULL, NULL); - GskRenderNode *node = snapshot ? gtk_snapshot_free_to_node(snapshot) : NULL; - graphene_rect_t bounds = GRAPHENE_RECT_INIT(0, 0, width, height); - if (!node) - node = gsk_color_node_new(&(GdkRGBA){.alpha = 0}, &bounds); - GdkTexture *texture = gsk_renderer_render_texture(renderer, node, &bounds); - gsk_render_node_unref(node); - gsk_renderer_unrealize(renderer); - g_object_unref(renderer); return _INTPTR(texture); } diff --git a/src/api-impl/android/graphics/Bitmap.java b/src/api-impl/android/graphics/Bitmap.java index a4b0edc9..15e54177 100644 --- a/src/api-impl/android/graphics/Bitmap.java +++ b/src/api-impl/android/graphics/Bitmap.java @@ -101,7 +101,7 @@ public final class Bitmap { public synchronized long getTexture() { if (texture == 0) { - texture = native_create_texture(snapshot, width, height); + texture = native_create_texture(snapshot, width, height, stride, config.gdk_memory_format); snapshot = 0; } return texture; @@ -191,7 +191,7 @@ public final class Bitmap { } private static native long native_create_snapshot(long texture); - private static native long native_create_texture(long snapshot, int width, int height); + private static native long native_create_texture(long snapshot, int width, int height, int stride, int format); private static native int native_get_width(long texture); private static native int native_get_height(long texture); private static native long native_erase_color(int color, int width, int height);