Skip to content

Commit

Permalink
Expose BBHFactory to record bounds in Picture (#889)
Browse files Browse the repository at this point in the history
  • Loading branch information
MatkovIvan authored Mar 11, 2024
1 parent 22d02e6 commit 574e3ee
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 8 deletions.
28 changes: 28 additions & 0 deletions skiko/src/commonMain/kotlin/org/jetbrains/skia/BBHFactory.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.jetbrains.skia

import org.jetbrains.skia.impl.Managed
import org.jetbrains.skia.impl.NativePointer
import org.jetbrains.skia.impl.Stats

// TODO class BBoxHierarchy internal constructor(ptr: NativePointer) : RefCnt(ptr)

abstract class BBHFactory internal constructor(ptr: NativePointer) : Managed(ptr, _FinalizerHolder.PTR) {
private object _FinalizerHolder {
val PTR = BBHFactory_nGetFinalizer()
}
}

class RTreeFactory : BBHFactory {
@Suppress("ConvertSecondaryConstructorToPrimary")
constructor() : super(RTreeFactory_nMake()) {
Stats.onNativeCall()
}
}

@ExternalSymbolName("org_jetbrains_skia_RTreeFactory__1nMake")
@ModuleImport("./skiko.mjs", "org_jetbrains_skia_RTreeFactory__1nMake")
private external fun RTreeFactory_nMake(): NativePointer

@ExternalSymbolName("org_jetbrains_skia_BBHFactory__1nGetFinalizer")
@ModuleImport("./skiko.mjs", "org_jetbrains_skia_BBHFactory_1nGetFinalizer")
private external fun BBHFactory_nGetFinalizer(): NativePointer
18 changes: 14 additions & 4 deletions skiko/src/commonMain/kotlin/org/jetbrains/skia/PictureRecorder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package org.jetbrains.skia

import org.jetbrains.skia.impl.Library.Companion.staticLoad
import org.jetbrains.skia.impl.Managed
import org.jetbrains.skia.impl.NativePointer
import org.jetbrains.skia.impl.Stats
import org.jetbrains.skia.impl.getPtr
import org.jetbrains.skia.impl.reachabilityBarrier
import org.jetbrains.skia.impl.NativePointer

class PictureRecorder internal constructor(ptr: NativePointer) : Managed(ptr, _FinalizerHolder.PTR) {
companion object {
Expand Down Expand Up @@ -44,9 +45,10 @@ class PictureRecorder internal constructor(ptr: NativePointer) : Managed(ptr, _F
*
* @param bounds the cull rect used when recording this picture. Any drawing the falls outside
* of this rect is undefined, and may be drawn or it may not.
* @param bbh optional acceleration structure
* @return the canvas.
*/
fun beginRecording(bounds: Rect): Canvas {
fun beginRecording(bounds: Rect, bbh: BBHFactory? = null): Canvas {
return try {
Stats.onNativeCall()
Canvas(
Expand All @@ -55,7 +57,8 @@ class PictureRecorder internal constructor(ptr: NativePointer) : Managed(ptr, _F
bounds.left,
bounds.top,
bounds.right,
bounds.bottom
bounds.bottom,
getPtr(bbh)
), false, this
)
} finally {
Expand Down Expand Up @@ -134,7 +137,14 @@ private external fun PictureRecorder_nGetFinalizer(): NativePointer

@ExternalSymbolName("org_jetbrains_skia_PictureRecorder__1nBeginRecording")
@ModuleImport("./skiko.mjs", "org_jetbrains_skia_PictureRecorder__1nBeginRecording")
private external fun _nBeginRecording(ptr: NativePointer, left: Float, top: Float, right: Float, bottom: Float): NativePointer
private external fun _nBeginRecording(
ptr: NativePointer,
left: Float,
top: Float,
right: Float,
bottom: Float,
bbh: NativePointer
): NativePointer

@ExternalSymbolName("org_jetbrains_skia_PictureRecorder__1nGetRecordingCanvas")
@ModuleImport("./skiko.mjs", "org_jetbrains_skia_PictureRecorder__1nGetRecordingCanvas")
Expand Down
18 changes: 18 additions & 0 deletions skiko/src/jvmMain/cpp/common/BBHFactory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <jni.h>
#include "interop.hh"
#include "SkBBHFactory.h"

static void deleteSkBBHFactory(SkBBHFactory* bbh) {
delete bbh;
}

extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skia_BBHFactoryKt_RTreeFactory_1nMake
(JNIEnv* env, jclass jclass) {
SkRTreeFactory* instance = new SkRTreeFactory();
return reinterpret_cast<jlong>(instance);
}

extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skia_BBHFactoryKt_BBHFactory_1nGetFinalizer
(JNIEnv* env, jclass jclass) {
return static_cast<jlong>(reinterpret_cast<uintptr_t>(&deleteSkBBHFactory));
}
5 changes: 3 additions & 2 deletions skiko/src/jvmMain/cpp/common/PictureRecorder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skia_PictureRecorderKt_Pic
}

extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skia_PictureRecorderKt__1nBeginRecording
(JNIEnv* env, jclass jclass, jlong ptr, jfloat left, jfloat top, jfloat right, jfloat bottom) {
(JNIEnv* env, jclass jclass, jlong ptr, jfloat left, jfloat top, jfloat right, jfloat bottom, jlong bbh) {
SkPictureRecorder* instance = reinterpret_cast<SkPictureRecorder*>(static_cast<uintptr_t>(ptr));
SkCanvas* canvas = instance->beginRecording(SkRect::MakeLTRB(left, top, right, bottom), nullptr);
SkBBHFactory* factory = reinterpret_cast<SkBBHFactory*>(static_cast<uintptr_t>(bbh));
SkCanvas* canvas = instance->beginRecording(SkRect::MakeLTRB(left, top, right, bottom), factory);
return reinterpret_cast<jlong>(canvas);
}

Expand Down
17 changes: 17 additions & 0 deletions skiko/src/nativeJsMain/cpp/BBHFactory.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "common.h"
#include "SkBBHFactory.h"

static void deleteSkBBHFactory(SkBBHFactory* bbh) {
delete bbh;
}

SKIKO_EXPORT KNativePointer org_jetbrains_skia_RTreeFactory__1nMake
() {
SkRTreeFactory* instance = new SkRTreeFactory();
return reinterpret_cast<KNativePointer>(instance);
}

SKIKO_EXPORT KNativePointer org_jetbrains_skia_BBHFactory__1nGetFinalizer
() {
return reinterpret_cast<KNativePointer>((&deleteSkBBHFactory));
}
5 changes: 3 additions & 2 deletions skiko/src/nativeJsMain/cpp/PictureRecorder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ SKIKO_EXPORT KNativePointer org_jetbrains_skia_PictureRecorder__1nGetFinalizer
}

SKIKO_EXPORT KNativePointer org_jetbrains_skia_PictureRecorder__1nBeginRecording
(KNativePointer ptr, KFloat left, KFloat top, KFloat right, KFloat bottom) {
(KNativePointer ptr, KFloat left, KFloat top, KFloat right, KFloat bottom, KNativePointer bbh) {
SkPictureRecorder* instance = reinterpret_cast<SkPictureRecorder*>((ptr));
SkCanvas* canvas = instance->beginRecording(SkRect::MakeLTRB(left, top, right, bottom), nullptr);
SkBBHFactory* factory = reinterpret_cast<SkBBHFactory*>(bbh);
SkCanvas* canvas = instance->beginRecording(SkRect::MakeLTRB(left, top, right, bottom), factory);
return reinterpret_cast<KNativePointer>(canvas);
}

Expand Down

0 comments on commit 574e3ee

Please sign in to comment.