You've already forked android_translation_layer
mirror of
https://gitlab.com/android_translation_layer/android_translation_layer.git
synced 2025-10-27 11:48:10 -07:00
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:
@@ -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);
|
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);
|
||||||
|
}
|
||||||
|
|||||||
@@ -23,6 +23,46 @@ JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawBitmap
|
|||||||
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawRect
|
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1drawRect
|
||||||
(JNIEnv *, jobject, jlong, jfloat, jfloat, jfloat, jfloat, jint);
|
(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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -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
|
JNIEXPORT jfloat JNICALL Java_android_graphics_Paint_native_1measure_1text
|
||||||
(JNIEnv *, jclass, jlong, jobject, jint, jint, jlong);
|
(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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -23,6 +23,14 @@ JNIEXPORT jlong JNICALL Java_android_graphics_drawable_Drawable_native_1paintabl
|
|||||||
JNIEXPORT jlong JNICALL Java_android_graphics_drawable_Drawable_native_1constructor
|
JNIEXPORT jlong JNICALL Java_android_graphics_drawable_Drawable_native_1constructor
|
||||||
(JNIEnv *, jobject);
|
(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
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
#include <gtk/gtk.h>
|
#include <gtk/gtk.h>
|
||||||
#include <graphene.h>
|
#include <graphene.h>
|
||||||
|
|
||||||
|
#include "include/c/sk_paint.h"
|
||||||
|
#include "include/c/sk_path.h"
|
||||||
|
|
||||||
#include "../defines.h"
|
#include "../defines.h"
|
||||||
#include "../util.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);
|
graphene_rect_t bounds = GRAPHENE_RECT_INIT(left, top, right - left, bottom - top);
|
||||||
gtk_snapshot_append_color(snapshot, &gdk_color, &bounds);
|
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);
|
||||||
|
}
|
||||||
|
|||||||
@@ -224,3 +224,12 @@ JNIEXPORT jboolean JNICALL Java_android_graphics_Matrix_native_1preScale__JFF(JN
|
|||||||
graphene_matrix_multiply(&scale, matrix, matrix);
|
graphene_matrix_multiply(&scale, matrix, matrix);
|
||||||
return true;
|
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -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);
|
graphene_matrix_t *matrix = (graphene_matrix_t *)_PTR(matrix_ptr);
|
||||||
float v[16];
|
float v[16];
|
||||||
graphene_matrix_to_float(matrix, v);
|
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);
|
sk_path_transform(path, &m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,31 +6,39 @@ package android.graphics;
|
|||||||
*/
|
*/
|
||||||
public class GskCanvas extends Canvas {
|
public class GskCanvas extends Canvas {
|
||||||
private long snapshot;
|
private long snapshot;
|
||||||
|
private int save_count = 0;
|
||||||
|
|
||||||
public GskCanvas(long snapshot) {
|
public GskCanvas(long snapshot) {
|
||||||
System.out.println("GskCanvas(" + snapshot + ")");
|
|
||||||
this.snapshot = snapshot;
|
this.snapshot = snapshot;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int save() {
|
public int save() {
|
||||||
System.out.println("GskCanvas.save()");
|
native_save(snapshot);
|
||||||
return -1;
|
return save_count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void restore() {
|
public void restore() {
|
||||||
System.out.println("GskCanvas.restore()");
|
save_count--;
|
||||||
|
native_restore(snapshot);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void restoreToCount(int count) {
|
||||||
|
while (save_count > count) {
|
||||||
|
restore();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void translate(float dx, float dy) {
|
public void translate(float dx, float dy) {
|
||||||
System.out.println("GskCanvas.translate(" + dx + ", " + dy + ")");
|
native_translate(snapshot, dx, dy);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void rotate(float degrees) {
|
public void rotate(float degrees) {
|
||||||
System.out.println("GskCanvas.rotate(" + degrees + ")");
|
native_rotate(snapshot, degrees);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -44,7 +52,7 @@ public class GskCanvas extends Canvas {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drawPath(Path path, Paint paint) {
|
public void drawPath(Path path, Paint paint) {
|
||||||
System.out.println("GskCanvas.drawPath(" + path + ", " + paint + ")");
|
native_drawPath(snapshot, path.mNativePath, paint.skia_paint);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -76,4 +84,9 @@ public class GskCanvas extends Canvas {
|
|||||||
|
|
||||||
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 pixbuf, 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_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);
|
||||||
|
protected native void native_rotate(long snapshot, float degrees);
|
||||||
|
protected native void native_save(long snapshot);
|
||||||
|
protected native void native_restore(long snapshot);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -323,8 +323,7 @@ public class Matrix {
|
|||||||
* M' = M * T(dx, dy)
|
* M' = M * T(dx, dy)
|
||||||
*/
|
*/
|
||||||
public boolean preTranslate(float dx, float dy) {
|
public boolean preTranslate(float dx, float dy) {
|
||||||
// return native_preTranslate(native_instance, dx, dy);
|
return native_preTranslate(native_instance, dx, dy);
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Preconcats the matrix with the specified scale.
|
* Preconcats the matrix with the specified scale.
|
||||||
|
|||||||
@@ -30,7 +30,9 @@ public class Paint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void setAntiAlias(boolean aa) {}
|
public void setAntiAlias(boolean aa) {}
|
||||||
public void setStrokeWidth(float width) {}
|
public void setStrokeWidth(float width) {
|
||||||
|
native_set_stroke_width(skia_paint, width);
|
||||||
|
}
|
||||||
public void setTextSize(float size) {
|
public void setTextSize(float size) {
|
||||||
if(skia_font == 0)
|
if(skia_font == 0)
|
||||||
skia_font = native_create_font();
|
skia_font = native_create_font();
|
||||||
@@ -51,7 +53,9 @@ public class Paint {
|
|||||||
public void getTextBounds(char[] text, int index, int count, Rect bounds) {}
|
public void getTextBounds(char[] text, int index, int count, Rect bounds) {}
|
||||||
public void setFlags(int flags) {}
|
public void setFlags(int flags) {}
|
||||||
public void setFilterBitmap(boolean filter) {}
|
public void setFilterBitmap(boolean filter) {}
|
||||||
public void setStyle(Style style) {}
|
public void setStyle(Style style) {
|
||||||
|
native_set_style(skia_paint, style.nativeInt);
|
||||||
|
}
|
||||||
public float ascent() {
|
public float ascent() {
|
||||||
if(skia_font == 0)
|
if(skia_font == 0)
|
||||||
return 0;
|
return 0;
|
||||||
@@ -237,4 +241,6 @@ public class Paint {
|
|||||||
private static native void native_set_typeface(long skia_font, long skia_typeface);
|
private static native void native_set_typeface(long skia_font, long skia_typeface);
|
||||||
private static native void native_set_text_size(long skia_font, float size);
|
private static native void native_set_text_size(long skia_font, float size);
|
||||||
private static native float native_measure_text(long skia_font, CharSequence text, int start, int end, long skia_paint);
|
private static native float native_measure_text(long skia_font, CharSequence text, int start, int end, long skia_paint);
|
||||||
|
private static native void native_set_stroke_width(long skia_font, float width);
|
||||||
|
private static native void native_set_style(long skia_paint, int style);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user