Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add binding for SkCanvas::getSurface() #593

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions skiko/src/commonMain/kotlin/org/jetbrains/skia/Canvas.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a simple test to CanvasTest?

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))
Expand Down Expand Up @@ -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)

Expand Down
7 changes: 7 additions & 0 deletions skiko/src/jvmMain/cpp/common/Canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skia_CanvasKt__1nMakeFromB
return reinterpret_cast<jlong>(canvas);
}

extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skia_CanvasKt__1nGetSurface
(JNIEnv* env, jclass jclass, jlong ptr) {
SkCanvas* instance = reinterpret_cast<SkCanvas*>(static_cast<uintptr_t>(ptr));
SkSurface* surface = instance->getSurface();
return reinterpret_cast<jlong>(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<SkCanvas*>(static_cast<uintptr_t>(canvasPtr));
Expand Down
7 changes: 7 additions & 0 deletions skiko/src/nativeJsMain/cpp/Canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ SKIKO_EXPORT KNativePointer org_jetbrains_skia_Canvas__1nMakeFromBitmap
return reinterpret_cast<KNativePointer>(canvas);
}

SKIKO_EXPORT KNativePointer org_jetbrains_skia_CanvasKt__1nGetSurface
(KNativePointer ptr) {
SkCanvas* instance = reinterpret_cast<SkCanvas*>((ptr));
SkSurface* surface = instance->getSurface();
return reinterpret_cast<KNativePointer>(surface);
}

SKIKO_EXPORT void org_jetbrains_skia_Canvas__1nDrawPoint
(KNativePointer canvasPtr, KFloat x, KFloat y, KNativePointer paintPtr) {
SkCanvas* canvas = reinterpret_cast<SkCanvas*>((canvasPtr));
Expand Down