Bitmap: create Texture as GdkMemoryTexture if nothing has to be rendered

This improves OsmAnd V1 renderer performance.
This commit is contained in:
Julian Winkler
2024-12-22 12:22:30 +01:00
parent 0dfcb9324f
commit bf923cfcfa
3 changed files with 31 additions and 22 deletions

View File

@@ -18,10 +18,10 @@ JNIEXPORT jlong JNICALL Java_android_graphics_Bitmap_native_1create_1snapshot
/* /*
* Class: android_graphics_Bitmap * Class: android_graphics_Bitmap
* Method: native_create_texture * Method: native_create_texture
* Signature: (JII)J * Signature: (JIIII)J
*/ */
JNIEXPORT jlong JNICALL Java_android_graphics_Bitmap_native_1create_1texture 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 * Class: android_graphics_Bitmap

View File

@@ -16,29 +16,38 @@ JNIEXPORT jlong JNICALL Java_android_graphics_Bitmap_native_1create_1snapshot(JN
return _INTPTR(snapshot); 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; static GType renderer_type = 0;
if (!renderer_type) { GtkSnapshot *snapshot = _PTR(snapshot_ptr);
// Use same renderer type as for onscreen rendering. GskRenderNode *node = snapshot ? gtk_snapshot_free_to_node(snapshot) : NULL;
GdkSurface *surface = gdk_surface_new_toplevel(gdk_display_get_default()); GdkTexture *texture = NULL;
GskRenderer *renderer = gsk_renderer_new_for_surface(surface); if (node) {
renderer_type = G_OBJECT_TYPE(renderer); 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); gsk_renderer_unrealize(renderer);
g_object_unref(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); return _INTPTR(texture);
} }

View File

@@ -101,7 +101,7 @@ public final class Bitmap {
public synchronized long getTexture() { public synchronized long getTexture() {
if (texture == 0) { if (texture == 0) {
texture = native_create_texture(snapshot, width, height); texture = native_create_texture(snapshot, width, height, stride, config.gdk_memory_format);
snapshot = 0; snapshot = 0;
} }
return texture; 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_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_width(long texture);
private static native int native_get_height(long texture); private static native int native_get_height(long texture);
private static native long native_erase_color(int color, int width, int height); private static native long native_erase_color(int color, int width, int height);