implement Canvas.clipRect() using gtk_snapshot_push_clip()

This commit is contained in:
Julian Winkler
2025-10-04 19:34:43 +02:00
committed by Mis012
parent e58d8d2065
commit d78fb53ce3
4 changed files with 57 additions and 13 deletions

View File

@@ -113,6 +113,22 @@ JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1scale
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1concat JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1concat
(JNIEnv *, jobject, jlong, jlong); (JNIEnv *, jobject, jlong, jlong);
/*
* Class: android_graphics_GskCanvas
* Method: native_clipRect
* Signature: (JFFFF)V
*/
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1clipRect
(JNIEnv *, jobject, jlong, jfloat, jfloat, jfloat, jfloat);
/*
* Class: android_graphics_GskCanvas
* Method: native_pop
* Signature: (JI)V
*/
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1pop
(JNIEnv *, jobject, jlong, jint);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@@ -154,3 +154,16 @@ JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1concat(JNIEnv *en
graphene_matrix_t *matrix = (graphene_matrix_t *)_PTR(matrix_ptr); graphene_matrix_t *matrix = (graphene_matrix_t *)_PTR(matrix_ptr);
gtk_snapshot_transform_matrix(snapshot, matrix); gtk_snapshot_transform_matrix(snapshot, matrix);
} }
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1clipRect(JNIEnv *env, jclass this_class, jlong snapshot_ptr, jfloat left, jfloat top, jfloat right, jfloat bottom)
{
GdkSnapshot *snapshot = GTK_SNAPSHOT(_PTR(snapshot_ptr));
gtk_snapshot_push_clip(snapshot, &GRAPHENE_RECT_INIT(left, top, right - left, bottom - top));
}
JNIEXPORT void JNICALL Java_android_graphics_GskCanvas_native_1pop(JNIEnv *env, jclass this_class, jlong snapshot_ptr, jint count)
{
GdkSnapshot *snapshot = GTK_SNAPSHOT(_PTR(snapshot_ptr));
while (count--)
gtk_snapshot_pop(snapshot);
}

View File

@@ -488,22 +488,20 @@ public class Canvas {
} }
public boolean clipRect(int left, int top, int right, int bottom) { public boolean clipRect(int left, int top, int right, int bottom) {
Log.w("Canvas", "STUB: clipRect"); return clipRect((float)left, top, right, bottom);
return false;
} }
public boolean clipRect(float left, float top, float right, float bottom) { public boolean clipRect(float left, float top, float right, float bottom) {
return false; gsk_canvas.snapshot = bitmap.getSnapshot();
return gsk_canvas.clipRect(left, top, right, bottom);
} }
public boolean clipRect(Rect rect) { public boolean clipRect(Rect rect) {
Log.w("Canvas", "STUB: clipRect"); return clipRect((float)rect.left, rect.top, rect.right, rect.bottom);
return false;
} }
public boolean clipRect(Rect rect, Region.Op op) { public boolean clipRect(Rect rect, Region.Op op) {
Log.w("Canvas", "STUB: clipRect"); return clipRect((float)rect.left, rect.top, rect.right, rect.bottom);
return false;
} }
public boolean clipPath(Path path) { public boolean clipPath(Path path) {
@@ -521,18 +519,15 @@ public class Canvas {
} }
public boolean clipRect(RectF rect) { public boolean clipRect(RectF rect) {
Log.v("Canvas", "STUB: clipRect"); return clipRect(rect.left, rect.top, rect.right, rect.bottom);
return false;
} }
public boolean clipRect(float left, float top, float right, float bottom, Region.Op op) { public boolean clipRect(float left, float top, float right, float bottom, Region.Op op) {
Log.v("Canvas", "STUB: clipRect"); return clipRect(left, top, right, bottom);
return false;
} }
public boolean clipRect(RectF rect, Region.Op op) { public boolean clipRect(RectF rect, Region.Op op) {
Log.v("Canvas", "STUB: clipRect"); return clipRect(rect.left, rect.top, rect.right, rect.bottom);
return false;
} }
public void drawArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean includeCenter, Paint paint) { public void drawArc(float left, float top, float right, float bottom, float startAngle, float sweepAngle, boolean includeCenter, Paint paint) {

View File

@@ -1,5 +1,7 @@
package android.graphics; package android.graphics;
import java.util.Arrays;
/** /**
* GskCanvas: * GskCanvas:
* - implements Canvas for onscreen rendering inside GTKs snapshot function * - implements Canvas for onscreen rendering inside GTKs snapshot function
@@ -7,6 +9,7 @@ package android.graphics;
public class GskCanvas extends Canvas { public class GskCanvas extends Canvas {
public long snapshot; public long snapshot;
private int save_count = 1; private int save_count = 1;
private int[] push_history = null;
private static Paint default_paint = new Paint(); private static Paint default_paint = new Paint();
@@ -22,6 +25,10 @@ public class GskCanvas extends Canvas {
@Override @Override
public void restore() { public void restore() {
if (push_history != null && push_history.length > save_count && push_history[save_count] > 0) {
native_pop(snapshot, push_history[save_count]);
push_history[save_count] = 0;
}
save_count--; save_count--;
native_restore(snapshot); native_restore(snapshot);
} }
@@ -146,6 +153,17 @@ public class GskCanvas extends Canvas {
drawText(text.toString().substring(start, end), x, y, paint); drawText(text.toString().substring(start, end), x, y, paint);
} }
@Override
public boolean clipRect(float left, float top, float right, float bottom) {
native_clipRect(snapshot, left, top, right, bottom);
if (push_history == null)
push_history = new int[save_count+1];
else if (push_history.length <= save_count)
push_history = Arrays.copyOf(push_history, save_count+1);
push_history[save_count]++;
return right > left && bottom > top;
}
protected native void native_drawBitmap(long snapshot, long texture, int x, int y, int width, int height, long paint); protected native void native_drawBitmap(long snapshot, long texture, int x, int y, int width, int height, long paint);
protected native void native_drawRect(long snapshot, float left, float top, float right, float bottom, long paint); protected native void native_drawRect(long snapshot, float left, float top, float right, float bottom, long paint);
protected native void native_drawPath(long snapshot, long path, long paint); protected native void native_drawPath(long snapshot, long path, long paint);
@@ -159,4 +177,6 @@ public class GskCanvas extends Canvas {
protected native void native_drawRoundRect(long snapshot, float left, float top, float right, float bottom, float rx, float ry, long paint); protected native void native_drawRoundRect(long snapshot, float left, float top, float right, float bottom, float rx, float ry, long paint);
protected native void native_scale(long snapshot, float sx, float sy); protected native void native_scale(long snapshot, float sx, float sy);
protected native void native_concat(long snapshot, long matrix); protected native void native_concat(long snapshot, long matrix);
protected native void native_clipRect(long snapshot, float left, float top, float right, float bottom);
protected native void native_pop(long snapshot, int pop_count);
} }