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
implement SurfaceView.lockCanvas() using GskCanvas
This commit is contained in:
@@ -207,6 +207,22 @@ extern "C" {
|
|||||||
JNIEXPORT jlong JNICALL Java_android_view_SurfaceView_native_1constructor
|
JNIEXPORT jlong JNICALL Java_android_view_SurfaceView_native_1constructor
|
||||||
(JNIEnv *, jobject, jobject, jobject);
|
(JNIEnv *, jobject, jobject, jobject);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Class: android_view_SurfaceView
|
||||||
|
* Method: native_createSnapshot
|
||||||
|
* Signature: ()J
|
||||||
|
*/
|
||||||
|
JNIEXPORT jlong JNICALL Java_android_view_SurfaceView_native_1createSnapshot
|
||||||
|
(JNIEnv *, jobject);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Class: android_view_SurfaceView
|
||||||
|
* Method: native_postSnapshot
|
||||||
|
* Signature: (JJ)V
|
||||||
|
*/
|
||||||
|
JNIEXPORT void JNICALL Java_android_view_SurfaceView_native_1postSnapshot
|
||||||
|
(JNIEnv *, jobject, jlong, jlong);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -103,6 +103,11 @@ JNIEXPORT jlong JNICALL Java_android_view_SurfaceView_native_1constructor(JNIEnv
|
|||||||
// TODO: is this correct for all usecases? how do we know when it's not?
|
// TODO: is this correct for all usecases? how do we know when it's not?
|
||||||
gtk_widget_set_hexpand(wrapper, true);
|
gtk_widget_set_hexpand(wrapper, true);
|
||||||
gtk_widget_set_vexpand(wrapper, true);
|
gtk_widget_set_vexpand(wrapper, true);
|
||||||
|
#if GTK_CHECK_VERSION(4, 14, 0)
|
||||||
|
gtk_widget_insert_after(gtk_graphics_offload_new(gtk_picture_new()), dummy, NULL);
|
||||||
|
#else
|
||||||
|
gtk_widget_insert_after(gtk_picture_new(), dummy, NULL);
|
||||||
|
#endif
|
||||||
|
|
||||||
JavaVM *jvm;
|
JavaVM *jvm;
|
||||||
(*env)->GetJavaVM(env, &jvm);
|
(*env)->GetJavaVM(env, &jvm);
|
||||||
@@ -117,3 +122,30 @@ JNIEXPORT jlong JNICALL Java_android_view_SurfaceView_native_1constructor(JNIEnv
|
|||||||
|
|
||||||
return _INTPTR(dummy);
|
return _INTPTR(dummy);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
JNIEXPORT jlong JNICALL Java_android_view_SurfaceView_native_1createSnapshot(JNIEnv *env, jclass class)
|
||||||
|
{
|
||||||
|
return _INTPTR(gtk_snapshot_new());
|
||||||
|
}
|
||||||
|
|
||||||
|
extern GtkWindow *window;
|
||||||
|
|
||||||
|
JNIEXPORT void JNICALL Java_android_view_SurfaceView_native_1postSnapshot(JNIEnv *env, jclass class, jlong surface_view, jlong snapshot_ptr)
|
||||||
|
{
|
||||||
|
GtkWidget *view = GTK_WIDGET(_PTR(surface_view));
|
||||||
|
#if GTK_CHECK_VERSION(4, 14, 0)
|
||||||
|
GtkPicture *picture = GTK_PICTURE(gtk_widget_get_first_child(gtk_widget_get_first_child(view)));
|
||||||
|
#else
|
||||||
|
GtkPicture *picture = GTK_PICTURE(gtk_widget_get_first_child(view));
|
||||||
|
#endif
|
||||||
|
GtkSnapshot *snapshot = GTK_SNAPSHOT(_PTR(snapshot_ptr));
|
||||||
|
GskRenderer *renderer = gsk_renderer_new_for_surface(gtk_native_get_surface(GTK_NATIVE(window)));
|
||||||
|
GskRenderNode *node = gtk_snapshot_free_to_node(snapshot);
|
||||||
|
GdkTexture *paintable = gsk_renderer_render_texture(renderer, node, NULL);
|
||||||
|
gsk_render_node_unref(node);
|
||||||
|
gsk_renderer_unrealize(renderer);
|
||||||
|
g_object_unref(renderer);
|
||||||
|
|
||||||
|
gtk_picture_set_paintable(picture, GDK_PAINTABLE(paintable));
|
||||||
|
g_object_unref(paintable);
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package android.view;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Canvas;
|
import android.graphics.Canvas;
|
||||||
|
import android.graphics.GskCanvas;
|
||||||
import android.graphics.Rect;
|
import android.graphics.Rect;
|
||||||
import android.util.AttributeSet;
|
import android.util.AttributeSet;
|
||||||
|
|
||||||
@@ -38,6 +39,9 @@ public class SurfaceView extends View {
|
|||||||
@Override
|
@Override
|
||||||
protected native long native_constructor(Context context, AttributeSet attrs);
|
protected native long native_constructor(Context context, AttributeSet attrs);
|
||||||
|
|
||||||
|
protected native long native_createSnapshot();
|
||||||
|
protected native void native_postSnapshot(long surfaceView, long snapshot);
|
||||||
|
|
||||||
public SurfaceHolder getHolder() {
|
public SurfaceHolder getHolder() {
|
||||||
return mSurfaceHolder;
|
return mSurfaceHolder;
|
||||||
}
|
}
|
||||||
@@ -125,8 +129,7 @@ public class SurfaceView extends View {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Canvas lockCanvas() {
|
public Canvas lockCanvas() {
|
||||||
// return internalLockCanvas(null);
|
return internalLockCanvas(null);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -146,8 +149,7 @@ public class SurfaceView extends View {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Canvas lockCanvas(Rect inOutDirty) {
|
public Canvas lockCanvas(Rect inOutDirty) {
|
||||||
// return internalLockCanvas(inOutDirty);
|
return internalLockCanvas(inOutDirty);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private final Canvas internalLockCanvas(Rect dirty) {
|
private final Canvas internalLockCanvas(Rect dirty) {
|
||||||
@@ -186,7 +188,10 @@ public class SurfaceView extends View {
|
|||||||
mLastLockTime = now;
|
mLastLockTime = now;
|
||||||
mSurfaceLock.unlock();
|
mSurfaceLock.unlock();
|
||||||
*/
|
*/
|
||||||
return null;
|
if(getWidth() == 0 || getHeight() == 0)
|
||||||
|
return null;
|
||||||
|
|
||||||
|
return new GskCanvas(native_createSnapshot());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -197,6 +202,7 @@ public class SurfaceView extends View {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void unlockCanvasAndPost(Canvas canvas) {
|
public void unlockCanvasAndPost(Canvas canvas) {
|
||||||
|
native_postSnapshot(widget, ((GskCanvas)canvas).snapshot);
|
||||||
// mSurface.unlockCanvasAndPost(canvas);
|
// mSurface.unlockCanvasAndPost(canvas);
|
||||||
// mSurfaceLock.unlock();
|
// mSurfaceLock.unlock();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user