implement ColorFilter in GskCanvas

This is needed to make icons follow night mode setting
This commit is contained in:
Julian Winkler
2024-03-24 21:12:13 +01:00
parent ad266c7821
commit 8eb0c0a3c1
5 changed files with 34 additions and 6 deletions

View File

@@ -10,10 +10,10 @@ extern "C" {
/*
* Class: android_graphics_GskCanvas
* Method: native_drawBitmap
* Signature: (JJIIII)V
* Signature: (JJIIIII)V
*/
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawBitmap
(JNIEnv *, jobject, jlong, jlong, jint, jint, jint, jint);
(JNIEnv *, jobject, jlong, jlong, jint, jint, jint, jint, jint);
#ifdef __cplusplus
}

View File

@@ -6,11 +6,25 @@
#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)
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)
{
GdkSnapshot *snapshot = (GdkSnapshot *)_PTR(snapshot_ptr);
GdkPixbuf *pixbuf = (GdkPixbuf *)_PTR(pixbuf_ptr);
GdkTexture *texture = gdk_texture_new_for_pixbuf(pixbuf);
if (color) { // use only alpha from pixbuf, color is fixed
graphene_matrix_t color_matrix;
graphene_vec4_t color_offset;
graphene_matrix_init_from_float(&color_matrix, (float[]){
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 1,
});
graphene_vec4_init(&color_offset, ((color >> 16) & 0xFF) / 255.f, ((color >> 8) & 0xFF) / 255.f, ((color >> 0) & 0xFF) / 255.f, 0);
gtk_snapshot_push_color_matrix(snapshot, &color_matrix, &color_offset);
}
gtk_snapshot_append_texture(snapshot, texture, &GRAPHENE_RECT_INIT(x, y, width, height));
if (color)
gtk_snapshot_pop(snapshot);
g_object_unref(texture);
}

View File

@@ -35,7 +35,11 @@ public class GskCanvas extends Canvas {
@Override
public void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) {
native_drawBitmap(snapshot, bitmap.pixbuf, dst.left, dst.top, dst.width(), dst.height());
int color = 0;
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);
}
@Override
@@ -70,5 +74,5 @@ public class GskCanvas extends Canvas {
drawBitmap(bitmap, src, dst, paint);
}
protected native void native_drawBitmap(long snapshot, long pixbuf, int x, int y, int width, int height);
protected native void native_drawBitmap(long snapshot, long pixbuf, int x, int y, int width, int height, int color);
}

View File

@@ -4,6 +4,7 @@ public class Paint {
public long skia_paint = 0;
private Typeface typeface = null;
public long skia_font = 0;
ColorFilter colorFilter = null;
public Paint() {
skia_paint = native_constructor();
@@ -64,6 +65,11 @@ public class Paint {
}
public float measureText(CharSequence text, int start, int end) { return 10; }
public ColorFilter setColorFilter(ColorFilter colorFilter) {
this.colorFilter = colorFilter;
return colorFilter;
}
public Shader setShader(Shader shader) { return shader; }
public enum Style {

View File

@@ -92,6 +92,10 @@ public class PorterDuff {
ADD(16),
OVERLAY(17);
Mode(int nativeInt) {}
public int nativeInt;
Mode(int nativeInt) {
this.nativeInt = nativeInt;
}
}
}