Bitmap: implement pixel buffer access

For GPU textures, the GdkTextureDownloader will take care of format
conversions, so the application never sees the actual format.

If the application calls AndroidBitmap_unlockPixels(), the texture is
converted into a GdkMemoryTexture and can be accessed zero copy.
This commit is contained in:
Julian Winkler
2024-12-22 10:20:50 +01:00
parent 7695aadf91
commit 260821d68c
4 changed files with 130 additions and 21 deletions

View File

@@ -71,6 +71,14 @@ JNIEXPORT jlong JNICALL Java_android_graphics_Bitmap_native_1ref_1texture
JNIEXPORT void JNICALL Java_android_graphics_Bitmap_native_1get_1pixels
(JNIEnv *, jclass, jlong, jintArray, jint, jint, jint, jint, jint, jint);
/*
* Class: android_graphics_Bitmap
* Method: native_copy_to_buffer
* Signature: (JLjava/nio/Buffer;II)V
*/
JNIEXPORT void JNICALL Java_android_graphics_Bitmap_native_1copy_1to_1buffer
(JNIEnv *, jclass, jlong, jobject, jint, jint);
#ifdef __cplusplus
}
#endif

View File

@@ -1,6 +1,7 @@
#include <gtk/gtk.h>
#include "../defines.h"
#include "../util.h"
#include "../generated_headers/android_graphics_Bitmap.h"
@@ -90,3 +91,16 @@ JNIEXPORT void JNICALL Java_android_graphics_Bitmap_native_1get_1pixels(JNIEnv *
gdk_texture_download(texture, (guchar *)(array + offset), stride*4);
(*env)->ReleaseIntArrayElements(env, pixels, array, 0);
}
JNIEXPORT void JNICALL Java_android_graphics_Bitmap_native_1copy_1to_1buffer(JNIEnv *env, jclass class, jlong texture_ptr, jobject buffer, jint memory_format, jint stride)
{
GdkTexture *texture = GDK_TEXTURE(_PTR(texture_ptr));
GdkTextureDownloader *downloader = gdk_texture_downloader_new(texture);
gdk_texture_downloader_set_format(downloader, memory_format);
jarray array_ref;
jbyte *array;
guchar *data = get_nio_buffer(env, buffer, &array_ref, &array);
gdk_texture_downloader_download_into(downloader, data, stride);
release_nio_buffer(env, array_ref, array);
gdk_texture_downloader_free(downloader);
}