diff --git a/meson.build b/meson.build index b3bd30c6..6661add0 100644 --- a/meson.build +++ b/meson.build @@ -101,7 +101,6 @@ libtranslationlayer_so = shared_library('translation_layer_main', [ 'src/api-impl-jni/graphics/android_graphics_Path.c', 'src/api-impl-jni/graphics/android_graphics_Typeface.c', 'src/api-impl-jni/graphics/android_graphics_Typeface.c', - 'src/api-impl-jni/graphics/android_graphics_drawable_BitmapDrawable.c', 'src/api-impl-jni/graphics/android_graphics_drawable_Drawable.c', 'src/api-impl-jni/graphics/android_graphics_drawable_DrawableContainer.c', 'src/api-impl-jni/location/android_location_LocationManager.c', diff --git a/src/api-impl-jni/android_graphics_Bitmap.c b/src/api-impl-jni/android_graphics_Bitmap.c index 495ea13d..64f15b93 100644 --- a/src/api-impl-jni/android_graphics_Bitmap.c +++ b/src/api-impl-jni/android_graphics_Bitmap.c @@ -99,15 +99,18 @@ JNIEXPORT void JNICALL Java_android_graphics_Bitmap_nativeGetPixels(JNIEnv *env, (*env)->ReleaseIntArrayElements(env, pixelArray, dst, 0); } -JNIEXPORT jboolean JNICALL Java_android_graphics_Bitmap_nativeRecycle(JNIEnv *env, jclass, jlong bitmapHandle) +JNIEXPORT jboolean JNICALL Java_android_graphics_Bitmap_nativeRecycle(JNIEnv *env, jclass, jlong bitmapHandle, jlong textureHandle) { GdkPixbuf *pixbuf = _PTR(bitmapHandle); + GdkTexture *texture = _PTR(textureHandle); sk_image_t *image = g_object_get_data(G_OBJECT(pixbuf), "sk_image"); if(image) sk_image_unref(image); else fprintf(stderr, "nativeRecycle: pixbuf doesn't have a skia image associated: %p\n", pixbuf); g_object_unref(pixbuf); + if (texture) + g_object_unref(texture); return true; } @@ -137,3 +140,9 @@ JNIEXPORT jint JNICALL Java_android_graphics_Bitmap_nativeRowBytes(JNIEnv *env, return gdk_pixbuf_get_rowstride(pixbuf); } + +JNIEXPORT jlong JNICALL Java_android_graphics_Bitmap_native_1paintable_1from_1pixbuf(JNIEnv *env, jclass, jlong pixbuf_ptr) +{ + GdkPixbuf *pixbuf = _PTR(pixbuf_ptr); + return _INTPTR(gdk_texture_new_for_pixbuf(pixbuf)); +} 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 6e6d8769..86227c49 100644 --- a/src/api-impl-jni/generated_headers/android_graphics_Bitmap.h +++ b/src/api-impl-jni/generated_headers/android_graphics_Bitmap.h @@ -59,6 +59,14 @@ JNIEXPORT jlong JNICALL Java_android_graphics_Bitmap_native_1subpixbuf JNIEXPORT jlong JNICALL Java_android_graphics_Bitmap_native_1create (JNIEnv *, jclass, jint, jint); +/* + * Class: android_graphics_Bitmap + * Method: native_paintable_from_pixbuf + * Signature: (J)J + */ +JNIEXPORT jlong JNICALL Java_android_graphics_Bitmap_native_1paintable_1from_1pixbuf + (JNIEnv *, jclass, jlong); + /* * Class: android_graphics_Bitmap * Method: nativeCopy @@ -78,10 +86,10 @@ JNIEXPORT void JNICALL Java_android_graphics_Bitmap_nativeDestructor /* * Class: android_graphics_Bitmap * Method: nativeRecycle - * Signature: (J)Z + * Signature: (JJ)Z */ JNIEXPORT jboolean JNICALL Java_android_graphics_Bitmap_nativeRecycle - (JNIEnv *, jclass, jlong); + (JNIEnv *, jclass, jlong, jlong); /* * Class: android_graphics_Bitmap diff --git a/src/api-impl-jni/generated_headers/android_graphics_drawable_BitmapDrawable.h b/src/api-impl-jni/generated_headers/android_graphics_drawable_BitmapDrawable.h deleted file mode 100644 index d2d8c00e..00000000 --- a/src/api-impl-jni/generated_headers/android_graphics_drawable_BitmapDrawable.h +++ /dev/null @@ -1,21 +0,0 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -/* Header for class android_graphics_drawable_BitmapDrawable */ - -#ifndef _Included_android_graphics_drawable_BitmapDrawable -#define _Included_android_graphics_drawable_BitmapDrawable -#ifdef __cplusplus -extern "C" { -#endif -/* - * Class: android_graphics_drawable_BitmapDrawable - * Method: native_paintable_from_pixbuf - * Signature: (J)J - */ -JNIEXPORT jlong JNICALL Java_android_graphics_drawable_BitmapDrawable_native_1paintable_1from_1pixbuf - (JNIEnv *, jclass, jlong); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/api-impl-jni/graphics/android_graphics_GskCanvas.c b/src/api-impl-jni/graphics/android_graphics_GskCanvas.c index f27e221f..b19b67bd 100644 --- a/src/api-impl-jni/graphics/android_graphics_GskCanvas.c +++ b/src/api-impl-jni/graphics/android_graphics_GskCanvas.c @@ -10,11 +10,10 @@ #include "../generated_headers/android_graphics_GskCanvas.h" -JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawBitmap(JNIEnv *env, jclass this_class, jlong snapshot_ptr, jlong pixbuf_ptr, jint x, jint y, jint width, jint height, jint color) +JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawBitmap(JNIEnv *env, jclass this_class, jlong snapshot_ptr, jlong texture_ptr, jint x, jint y, jint width, jint height, jint color) { GdkSnapshot *snapshot = (GdkSnapshot *)_PTR(snapshot_ptr); - GdkPixbuf *pixbuf = (GdkPixbuf *)_PTR(pixbuf_ptr); - GdkTexture *texture = gdk_texture_new_for_pixbuf(pixbuf); + GdkTexture *texture = GDK_TEXTURE(_PTR(texture_ptr)); if (color) { // use only alpha from pixbuf, color is fixed graphene_matrix_t color_matrix; graphene_vec4_t color_offset; @@ -30,7 +29,6 @@ JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawBitmap(JNIEnv gtk_snapshot_append_texture(snapshot, texture, &GRAPHENE_RECT_INIT(x, y, width, height)); if (color) gtk_snapshot_pop(snapshot); - g_object_unref(texture); } JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawRect(JNIEnv *env, jclass this_class, jlong snapshot_ptr, jfloat left, jfloat top, jfloat right, jfloat bottom, jint color) diff --git a/src/api-impl-jni/graphics/android_graphics_drawable_BitmapDrawable.c b/src/api-impl-jni/graphics/android_graphics_drawable_BitmapDrawable.c deleted file mode 100644 index 86731ac5..00000000 --- a/src/api-impl-jni/graphics/android_graphics_drawable_BitmapDrawable.c +++ /dev/null @@ -1,11 +0,0 @@ -#include - -#include "../defines.h" -#include "../util.h" - -JNIEXPORT jlong JNICALL Java_android_graphics_drawable_BitmapDrawable_native_1paintable_1from_1pixbuf(JNIEnv *env, jclass this, jlong _pixbuf) -{ - GdkPixbuf *pixbuf = (GdkPixbuf *)_PTR(_pixbuf); - - return _INTPTR(gdk_texture_new_for_pixbuf(pixbuf)); -} diff --git a/src/api-impl-jni/graphics/android_graphics_drawable_Drawable.c b/src/api-impl-jni/graphics/android_graphics_drawable_Drawable.c index 3cf4c314..071b31b5 100644 --- a/src/api-impl-jni/graphics/android_graphics_drawable_Drawable.c +++ b/src/api-impl-jni/graphics/android_graphics_drawable_Drawable.c @@ -93,5 +93,6 @@ JNIEXPORT void JNICALL Java_android_graphics_drawable_Drawable_native_1invalidat JNIEXPORT void JNICALL Java_android_graphics_drawable_Drawable_native_1draw(JNIEnv *env, jobject this, jlong paintable_ptr, jlong snapshot_ptr, jint width, jint height) { GdkSnapshot *snapshot = (GdkSnapshot *)_PTR(snapshot_ptr); GdkPaintable *paintable = GDK_PAINTABLE(_PTR(paintable_ptr)); - gdk_paintable_snapshot(paintable, snapshot, width, height); + if (!JAVA_IS_PAINTABLE(paintable)) + gdk_paintable_snapshot(paintable, snapshot, width, height); } diff --git a/src/api-impl/android/graphics/Bitmap.java b/src/api-impl/android/graphics/Bitmap.java index d49a610a..094eb257 100644 --- a/src/api-impl/android/graphics/Bitmap.java +++ b/src/api-impl/android/graphics/Bitmap.java @@ -16,6 +16,7 @@ package android.graphics; +import android.graphics.drawable.BitmapDrawable; import android.util.DisplayMetrics; import java.io.IOException; import java.io.InputStream; @@ -102,6 +103,7 @@ public final class Bitmap { } public long pixbuf = 0; + private long texture = 0; Bitmap() { mIsMutable = false; @@ -352,7 +354,7 @@ public final class Bitmap { */ public void recycle() { if (!mRecycled) { - if (nativeRecycle(pixbuf)) { + if (nativeRecycle(pixbuf, texture)) { // return value indicates whether native pixel object was actually recycled. // false indicates that it is still in use at the native level and these // objects should not be collected now. They will be collected later when the @@ -1594,17 +1596,29 @@ public final class Bitmap { } } + /** + * internal ATL method to create or get a GdkTexture for the pixbuf + * @return pointer to the GdkTexture + */ + public long getTexture() { + if (texture == 0) { + texture = native_paintable_from_pixbuf(pixbuf); + } + return texture; + } + //////////// native methods private native long native_bitmap_from_path(CharSequence path); static native long native_copy(long src); static native long native_subpixbuf(long src, int x, int y, int width, int height); private static native long native_create(int width, int height); + public static native long native_paintable_from_pixbuf(long pixbuf); private static native Bitmap nativeCopy(int srcBitmap, int nativeConfig, boolean isMutable); private static native void nativeDestructor(int nativeBitmap); - private static native boolean nativeRecycle(long nativeBitmap); + private static native boolean nativeRecycle(long nativeBitmap, long texture); private static native void nativeReconfigure(int nativeBitmap, int width, int height, int config, int allocSize); diff --git a/src/api-impl/android/graphics/GskCanvas.java b/src/api-impl/android/graphics/GskCanvas.java index d06128c6..82ee93b2 100644 --- a/src/api-impl/android/graphics/GskCanvas.java +++ b/src/api-impl/android/graphics/GskCanvas.java @@ -47,7 +47,7 @@ public class GskCanvas extends Canvas { if (paint != null && paint.colorFilter instanceof PorterDuffColorFilter) { color = ((PorterDuffColorFilter) paint.colorFilter).getColor(); } - native_drawBitmap(snapshot, bitmap.pixbuf, dst.left, dst.top, dst.width(), dst.height(), color); + native_drawBitmap(snapshot, bitmap.getTexture(), dst.left, dst.top, dst.width(), dst.height(), color); } @Override @@ -89,7 +89,7 @@ public class GskCanvas extends Canvas { drawBitmap(bitmap, src, new Rect((int)dst.left, (int)dst.top, (int)dst.right, (int)dst.bottom), paint); } - protected native void native_drawBitmap(long snapshot, long pixbuf, int x, int y, int width, int height, int color); + protected native void native_drawBitmap(long snapshot, long texture, int x, int y, int width, int height, int color); protected native void native_drawRect(long snapshot, float left, float top, float right, float bottom, int color); protected native void native_drawPath(long snapshot, long path, long paint); protected native void native_translate(long snapshot, float dx, float dy); diff --git a/src/api-impl/android/graphics/drawable/BitmapDrawable.java b/src/api-impl/android/graphics/drawable/BitmapDrawable.java index ffb74f5e..37b023b1 100644 --- a/src/api-impl/android/graphics/drawable/BitmapDrawable.java +++ b/src/api-impl/android/graphics/drawable/BitmapDrawable.java @@ -25,7 +25,7 @@ public class BitmapDrawable extends Drawable { public BitmapDrawable(Resources res, Bitmap bitmap) { this.bitmap = bitmap; - this.paintable = native_paintable_from_pixbuf(bitmap.pixbuf); + this.paintable = bitmap.getTexture(); } public Bitmap getBitmap() { @@ -40,6 +40,4 @@ public class BitmapDrawable extends Drawable { } a.recycle(); } - - static native long native_paintable_from_pixbuf(long pixbuf); } diff --git a/src/api-impl/android/graphics/drawable/VectorDrawable.java b/src/api-impl/android/graphics/drawable/VectorDrawable.java index 7177ea5f..e1f65add 100644 --- a/src/api-impl/android/graphics/drawable/VectorDrawable.java +++ b/src/api-impl/android/graphics/drawable/VectorDrawable.java @@ -50,7 +50,7 @@ public class VectorDrawable extends Drawable { String svg = sb.toString(); byte[] bytes = svg.getBytes(); Bitmap bm = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); - this.paintable = BitmapDrawable.native_paintable_from_pixbuf(bm.pixbuf); + this.paintable = bm.getTexture(); } }