From a9bf3ec4512e5d48ba90547765ae4205b3b5a4cc Mon Sep 17 00:00:00 2001 From: mcpiroman Date: Sat, 10 Sep 2022 20:32:36 +0200 Subject: [PATCH] Add binding for SkCanvas::getSurface() --- .../kotlin/org/jetbrains/skia/Canvas.kt | 18 ++++++++++++++++++ skiko/src/jvmMain/cpp/common/Canvas.cc | 7 +++++++ skiko/src/nativeJsMain/cpp/Canvas.cc | 7 +++++++ 3 files changed, 32 insertions(+) diff --git a/skiko/src/commonMain/kotlin/org/jetbrains/skia/Canvas.kt b/skiko/src/commonMain/kotlin/org/jetbrains/skia/Canvas.kt index dbd74c012..cca1dd3f8 100644 --- a/skiko/src/commonMain/kotlin/org/jetbrains/skia/Canvas.kt +++ b/skiko/src/commonMain/kotlin/org/jetbrains/skia/Canvas.kt @@ -55,6 +55,21 @@ open class Canvas internal constructor(ptr: NativePointer, managed: Boolean, int reachabilityBarrier(bitmap) } + /** + * Sometimes a canvas is owned by a surface. + * If it is, [surface] will return that surface, else this will null. + * + * @return the surface this canvas is owned by, or null if it is not owned by any surface. + */ + val surface: Surface? + get() = try { + Stats.onNativeCall() + val ptr = _nGetSurface(_ptr) + if (ptr == NullPointer) null else Surface(ptr) + } finally { + reachabilityBarrier(this) + } + fun drawPoint(x: Float, y: Float, paint: Paint): Canvas { Stats.onNativeCall() _nDrawPoint(_ptr, x, y, getPtr(paint)) @@ -1241,6 +1256,9 @@ private external fun Canvas_nGetFinalizer(): NativePointer @ExternalSymbolName("org_jetbrains_skia_Canvas__1nMakeFromBitmap") private external fun _nMakeFromBitmap(bitmapPtr: NativePointer, flags: Int, pixelGeometry: Int): NativePointer +@ExternalSymbolName("org_jetbrains_skia_Canvas__1nGetSurface") +private external fun _nGetSurface(ptr: NativePointer): NativePointer + @ExternalSymbolName("org_jetbrains_skia_Canvas__1nDrawPoint") private external fun _nDrawPoint(ptr: NativePointer, x: Float, y: Float, paintPtr: NativePointer) diff --git a/skiko/src/jvmMain/cpp/common/Canvas.cc b/skiko/src/jvmMain/cpp/common/Canvas.cc index 1364ee816..4fa53cbfb 100644 --- a/skiko/src/jvmMain/cpp/common/Canvas.cc +++ b/skiko/src/jvmMain/cpp/common/Canvas.cc @@ -23,6 +23,13 @@ extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skia_CanvasKt__1nMakeFromB return reinterpret_cast(canvas); } +extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skia_CanvasKt__1nGetSurface + (JNIEnv* env, jclass jclass, jlong ptr) { + SkCanvas* instance = reinterpret_cast(static_cast(ptr)); + SkSurface* surface = instance->getSurface(); + return reinterpret_cast(surface); +} + extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skia_CanvasKt__1nDrawPoint (JNIEnv* env, jclass jclass, jlong canvasPtr, jfloat x, jfloat y, jlong paintPtr) { SkCanvas* canvas = reinterpret_cast(static_cast(canvasPtr)); diff --git a/skiko/src/nativeJsMain/cpp/Canvas.cc b/skiko/src/nativeJsMain/cpp/Canvas.cc index 0beab2868..6883a250b 100644 --- a/skiko/src/nativeJsMain/cpp/Canvas.cc +++ b/skiko/src/nativeJsMain/cpp/Canvas.cc @@ -22,6 +22,13 @@ SKIKO_EXPORT KNativePointer org_jetbrains_skia_Canvas__1nMakeFromBitmap return reinterpret_cast(canvas); } +SKIKO_EXPORT KNativePointer org_jetbrains_skia_CanvasKt__1nGetSurface + (KNativePointer ptr) { + SkCanvas* instance = reinterpret_cast((ptr)); + SkSurface* surface = instance->getSurface(); + return reinterpret_cast(surface); +} + SKIKO_EXPORT void org_jetbrains_skia_Canvas__1nDrawPoint (KNativePointer canvasPtr, KFloat x, KFloat y, KNativePointer paintPtr) { SkCanvas* canvas = reinterpret_cast((canvasPtr));