GskCanvas: implement drawPath, rotate, translate, save, restore

This is enough to make DrawerArrowDrawable functional.

drawPath() only draws line segments for now.
This commit is contained in:
Julian Winkler
2024-04-08 23:15:05 +02:00
parent 74bedcc93c
commit 3d9eb9611f
10 changed files with 168 additions and 12 deletions

View File

@@ -67,3 +67,13 @@ JNIEXPORT jfloat JNICALL Java_android_graphics_Paint_native_1measure_1text(JNIEn
return sk_font_measure_text(font, text + start, end - start, UTF8_SK_TEXT_ENCODING, NULL, paint);
}
JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1stroke_1width(JNIEnv *env, jclass this, jlong skia_paint, jfloat width)
{
sk_paint_set_stroke_width(_PTR(skia_paint), width);
}
JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1style(JNIEnv *env, jclass this, jlong skia_paint, jint style)
{
sk_paint_set_style(_PTR(skia_paint), (sk_paint_style_t)style);
}

View File

@@ -23,6 +23,46 @@ JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawBitmap
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawRect
(JNIEnv *, jobject, jlong, jfloat, jfloat, jfloat, jfloat, jint);
/*
* Class: android_graphics_GskCanvas
* Method: native_drawPath
* Signature: (JJJ)V
*/
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawPath
(JNIEnv *, jobject, jlong, jlong, jlong);
/*
* Class: android_graphics_GskCanvas
* Method: native_translate
* Signature: (JFF)V
*/
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1translate
(JNIEnv *, jobject, jlong, jfloat, jfloat);
/*
* Class: android_graphics_GskCanvas
* Method: native_rotate
* Signature: (JF)V
*/
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1rotate
(JNIEnv *, jobject, jlong, jfloat);
/*
* Class: android_graphics_GskCanvas
* Method: native_save
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1save
(JNIEnv *, jobject, jlong);
/*
* Class: android_graphics_GskCanvas
* Method: native_restore
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1restore
(JNIEnv *, jobject, jlong);
#ifdef __cplusplus
}
#endif

View File

@@ -71,6 +71,22 @@ JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1text_1size
JNIEXPORT jfloat JNICALL Java_android_graphics_Paint_native_1measure_1text
(JNIEnv *, jclass, jlong, jobject, jint, jint, jlong);
/*
* Class: android_graphics_Paint
* Method: native_set_stroke_width
* Signature: (JF)V
*/
JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1stroke_1width
(JNIEnv *, jclass, jlong, jfloat);
/*
* Class: android_graphics_Paint
* Method: native_set_style
* Signature: (JI)V
*/
JNIEXPORT void JNICALL Java_android_graphics_Paint_native_1set_1style
(JNIEnv *, jclass, jlong, jint);
#ifdef __cplusplus
}
#endif

View File

@@ -23,6 +23,14 @@ JNIEXPORT jlong JNICALL Java_android_graphics_drawable_Drawable_native_1paintabl
JNIEXPORT jlong JNICALL Java_android_graphics_drawable_Drawable_native_1constructor
(JNIEnv *, jobject);
/*
* Class: android_graphics_drawable_Drawable
* Method: native_invalidate
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_android_graphics_drawable_Drawable_native_1invalidate
(JNIEnv *, jobject, jlong);
#ifdef __cplusplus
}
#endif

View File

@@ -1,6 +1,9 @@
#include <gtk/gtk.h>
#include <graphene.h>
#include "include/c/sk_paint.h"
#include "include/c/sk_path.h"
#include "../defines.h"
#include "../util.h"
@@ -41,3 +44,53 @@ JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawRect(JNIEnv *
graphene_rect_t bounds = GRAPHENE_RECT_INIT(left, top, right - left, bottom - top);
gtk_snapshot_append_color(snapshot, &gdk_color, &bounds);
}
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawPath(JNIEnv *env, jclass this_class, jlong snapshot_ptr, jlong path_ptr, jlong paint_ptr)
{
GtkSnapshot *snapshot = GTK_SNAPSHOT(_PTR(snapshot_ptr));
sk_paint_t *paint = (sk_paint_t *)_PTR(paint_ptr);
sk_path_t *path = (sk_path_t *)_PTR(path_ptr);
GdkRGBA gdk_color;
sk_path_iterator_t *iterator = sk_path_create_iter(path, 0);
sk_path_verb_t verb;
sk_point_t line[4];
sk_paint_get_color4f(paint, (sk_color4f_t *)&gdk_color);
float width = sk_paint_get_stroke_width(paint);
while ((verb = sk_path_iter_next(iterator, line)) != DONE_SK_PATH_VERB) {
// TODO: use GskPath to support other verbs
if (verb == LINE_SK_PATH_VERB) {
gtk_snapshot_save(snapshot);
gtk_snapshot_translate(snapshot, &GRAPHENE_POINT_INIT(line[0].x, line[0].y));
float rotation = atan2(line[1].y - line[0].y, line[1].x - line[0].x);
gtk_snapshot_rotate(snapshot, rotation * 180 / M_PI);
float length = sqrt((line[1].x - line[0].x) * (line[1].x - line[0].x) + (line[1].y - line[0].y) * (line[1].y - line[0].y));
gtk_snapshot_append_color(snapshot, &gdk_color, &GRAPHENE_RECT_INIT(0, -width / 2, length, width));
gtk_snapshot_restore(snapshot);
}
}
sk_path_iter_destroy(iterator);
}
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1translate(JNIEnv *env, jclass this_class, jlong snapshot_ptr, jfloat dx, jfloat dy)
{
GtkSnapshot *snapshot = GTK_SNAPSHOT(_PTR(snapshot_ptr));
gtk_snapshot_translate(snapshot, &GRAPHENE_POINT_INIT(dx, dy));
}
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1rotate(JNIEnv *env, jclass this_class, jlong snapshot_ptr, jfloat angle)
{
GtkSnapshot *snapshot = GTK_SNAPSHOT(_PTR(snapshot_ptr));
gtk_snapshot_rotate(snapshot, angle);
}
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1save(JNIEnv *env, jclass this_class, jlong snapshot_ptr)
{
GtkSnapshot *snapshot = GTK_SNAPSHOT(_PTR(snapshot_ptr));
gtk_snapshot_save(snapshot);
}
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1restore(JNIEnv *env, jclass this_class, jlong snapshot_ptr)
{
GtkSnapshot *snapshot = GTK_SNAPSHOT(_PTR(snapshot_ptr));
gtk_snapshot_restore(snapshot);
}

View File

@@ -224,3 +224,12 @@ JNIEXPORT jboolean JNICALL Java_android_graphics_Matrix_native_1preScale__JFF(JN
graphene_matrix_multiply(&scale, matrix, matrix);
return true;
}
JNIEXPORT jboolean JNICALL Java_android_graphics_Matrix_native_1preTranslate(JNIEnv *env, jclass class, jlong matrix_ptr, jfloat x, jfloat y)
{
graphene_matrix_t *matrix = (graphene_matrix_t *)_PTR(matrix_ptr);
graphene_matrix_t translation;
graphene_matrix_init_translate(&translation, &GRAPHENE_POINT3D_INIT(x, y, 0));
graphene_matrix_multiply(&translation, matrix, matrix);
return true;
}

View File

@@ -135,7 +135,9 @@ JNIEXPORT void JNICALL Java_android_graphics_Path_native_1transform__JJ(JNIEnv *
graphene_matrix_t *matrix = (graphene_matrix_t *)_PTR(matrix_ptr);
float v[16];
graphene_matrix_to_float(matrix, v);
sk_matrix_t m = {v[0], v[1], v[3], v[4], v[5], v[7], v[12], v[13], v[15]};
sk_matrix_t m = {v[0], v[4], v[12],
v[1], v[5], v[13],
v[3], v[7], v[15]};
sk_path_transform(path, &m);
}