Unverified Commit e46eb30c authored by alexander-gorshenev's avatar alexander-gorshenev Committed by GitHub

toInterop/fromInterop for Int, Long and Double (#192)

* toInterop/fromInterop for Int, Long and Double
* toInterop/fromInterop for ShortArrays
* jvm NativePointerArray return
* native toInterop(Array<String>)
parent 60191b83
...@@ -24,10 +24,20 @@ expect class InteropScope() { ...@@ -24,10 +24,20 @@ expect class InteropScope() {
fun InteropPointer.fromInterop(result: CharArray) fun InteropPointer.fromInterop(result: CharArray)
fun toInterop(array: ByteArray?): InteropPointer fun toInterop(array: ByteArray?): InteropPointer
fun InteropPointer.fromInterop(result: ByteArray) fun InteropPointer.fromInterop(result: ByteArray)
fun toInterop(array: ShortArray?): InteropPointer
fun InteropPointer.fromInterop(result: ShortArray)
fun toInterop(array: IntArray?): InteropPointer
fun InteropPointer.fromInterop(result: IntArray)
fun toInterop(array: LongArray?): InteropPointer
fun InteropPointer.fromInterop(result: LongArray)
fun toInterop(array: FloatArray?): InteropPointer fun toInterop(array: FloatArray?): InteropPointer
fun InteropPointer.fromInterop(result: FloatArray) fun InteropPointer.fromInterop(result: FloatArray)
fun toInterop(array: DoubleArray?): InteropPointer
fun InteropPointer.fromInterop(result: DoubleArray)
fun toInterop(array: NativePointerArray?): InteropPointer fun toInterop(array: NativePointerArray?): InteropPointer
fun InteropPointer.fromInterop(result: NativePointerArray)
fun toInterop(stringArray: Array<String>?): InteropPointer
fun InteropPointer.fromInteropNativePointerArray(): NativePointerArray
fun release() fun release()
} }
...@@ -54,3 +64,9 @@ inline fun withResult(result: FloatArray, block: (InteropPointer) -> Unit): Floa ...@@ -54,3 +64,9 @@ inline fun withResult(result: FloatArray, block: (InteropPointer) -> Unit): Floa
result result
} }
inline fun withResult(result: NativePointerArray, block: (InteropPointer) -> Unit): NativePointerArray = interopScope {
val handle = toInterop(result)
block(handle)
handle.fromInterop(result)
result
}
...@@ -2,14 +2,9 @@ ...@@ -2,14 +2,9 @@
package org.jetbrains.skia.paragraph package org.jetbrains.skia.paragraph
import org.jetbrains.skia.impl.Library.Companion.staticLoad import org.jetbrains.skia.impl.Library.Companion.staticLoad
import org.jetbrains.skia.impl.RefCnt
import org.jetbrains.skia.* import org.jetbrains.skia.*
import org.jetbrains.skia.impl.Stats
import org.jetbrains.skia.impl.reachabilityBarrier
import org.jetbrains.skia.ExternalSymbolName import org.jetbrains.skia.ExternalSymbolName
import org.jetbrains.skia.impl.NativePointer import org.jetbrains.skia.impl.*
import org.jetbrains.skia.impl.NativePointerArray
import org.jetbrains.skia.impl.getPtr
class FontCollection internal constructor(ptr: NativePointer) : RefCnt(ptr) { class FontCollection internal constructor(ptr: NativePointer) : RefCnt(ptr) {
companion object { companion object {
...@@ -90,10 +85,12 @@ class FontCollection internal constructor(ptr: NativePointer) : RefCnt(ptr) { ...@@ -90,10 +85,12 @@ class FontCollection internal constructor(ptr: NativePointer) : RefCnt(ptr) {
reachabilityBarrier(this) reachabilityBarrier(this)
} }
fun findTypefaces(familyNames: Array<String?>?, style: FontStyle): Array<Typeface?> { fun findTypefaces(familyNames: Array<String>?, style: FontStyle): Array<Typeface?> {
return try { return try {
Stats.onNativeCall() Stats.onNativeCall()
val ptrs = _nFindTypefaces(_ptr, familyNames, style._value) val ptrs = interopScope {
_nFindTypefaces(_ptr, toInterop(familyNames), style._value).fromInteropNativePointerArray()
}
val res = arrayOfNulls<Typeface>(ptrs.size) val res = arrayOfNulls<Typeface>(ptrs.size)
for (i in 0 until ptrs.size) res[i] = Typeface(ptrs[i]) for (i in 0 until ptrs.size) res[i] = Typeface(ptrs[i])
res res
...@@ -160,8 +157,7 @@ private external fun _nSetDefaultFontManager(ptr: NativePointer, fontManagerPtr: ...@@ -160,8 +157,7 @@ private external fun _nSetDefaultFontManager(ptr: NativePointer, fontManagerPtr:
private external fun _nGetFallbackManager(ptr: NativePointer): NativePointer private external fun _nGetFallbackManager(ptr: NativePointer): NativePointer
@ExternalSymbolName("org_jetbrains_skia_paragraph_FontCollection__1nFindTypefaces") @ExternalSymbolName("org_jetbrains_skia_paragraph_FontCollection__1nFindTypefaces")
// TODO: fix me, must pass value to store result! private external fun _nFindTypefaces(ptr: NativePointer, familyNames: InteropPointer, fontStyle: Int): InteropPointer
private external fun _nFindTypefaces(ptr: NativePointer, familyNames: Array<String?>?, fontStyle: Int): NativePointerArray
@ExternalSymbolName("org_jetbrains_skia_paragraph_FontCollection__1nDefaultFallbackChar") @ExternalSymbolName("org_jetbrains_skia_paragraph_FontCollection__1nDefaultFallbackChar")
private external fun _nDefaultFallbackChar(ptr: NativePointer, unicode: Int, fontStyle: Int, locale: String?): NativePointer private external fun _nDefaultFallbackChar(ptr: NativePointer, unicode: Int, fontStyle: Int, locale: String?): NativePointer
......
...@@ -25,7 +25,6 @@ actual fun reachabilityBarrier(obj: Any?) { ...@@ -25,7 +25,6 @@ actual fun reachabilityBarrier(obj: Any?) {
} }
actual typealias NativePointer = Int actual typealias NativePointer = Int
actual typealias NativePointerArray = IntArray
actual typealias InteropPointer = Int actual typealias InteropPointer = Int
actual class InteropScope actual constructor() { actual class InteropScope actual constructor() {
...@@ -56,6 +55,44 @@ actual class InteropScope actual constructor() { ...@@ -56,6 +55,44 @@ actual class InteropScope actual constructor() {
0 0
} }
} }
actual fun InteropPointer.fromInterop(result: ShortArray) {
fromWasm(this@fromInterop, result)
}
actual fun toInterop(array: ShortArray?): InteropPointer {
return if (array != null) {
val data = _malloc(array.size * 2)
elements.add(data)
toWasm(data, array)
data
} else {
0
}
}
actual fun InteropPointer.fromInterop(result: IntArray) {
fromWasm(this@fromInterop, result)
}
actual fun toInterop(array: IntArray?): InteropPointer {
return if (array != null) {
val data = _malloc(array.size * 4)
elements.add(data)
toWasm(data, array)
data
} else {
0
}
}
actual fun InteropPointer.fromInterop(result: LongArray) {
TODO("implement wasm fromInterop(LongArray)")
}
actual fun toInterop(array: LongArray?): InteropPointer {
TODO("implement wasm toInterop(LongArray)")
}
actual fun InteropPointer.fromInterop(result: FloatArray) { actual fun InteropPointer.fromInterop(result: FloatArray) {
fromWasm(this@fromInterop, result) fromWasm(this@fromInterop, result)
} }
...@@ -70,6 +107,15 @@ actual class InteropScope actual constructor() { ...@@ -70,6 +107,15 @@ actual class InteropScope actual constructor() {
0 0
} }
} }
actual fun InteropPointer.fromInterop(result: DoubleArray) {
TODO("implement wasm fromInterop(DoubleArray)")
}
actual fun toInterop(array: DoubleArray?): InteropPointer {
TODO("implement wasm toInterop(DoubleArray)")
}
actual fun InteropPointer.fromInterop(result: ByteArray) { actual fun InteropPointer.fromInterop(result: ByteArray) {
fromWasm(this@fromInterop, result) fromWasm(this@fromInterop, result)
} }
...@@ -78,13 +124,24 @@ actual class InteropScope actual constructor() { ...@@ -78,13 +124,24 @@ actual class InteropScope actual constructor() {
return if (array != null) { return if (array != null) {
val data = _malloc(array.size * 4) val data = _malloc(array.size * 4)
elements.add(data) elements.add(data)
toWasm(data, array) toWasm(data, array.backing)
data data
} else { } else {
0 0
} }
} }
actual fun InteropPointer.fromInterop(result: NativePointerArray) {
TODO("implement wasm fromInterop(NativePointerArray)")
}
actual fun toInterop(stringArray: Array<String>?): InteropPointer =
TODO("implement wasm toInterop(Array<String>)")
actual fun InteropPointer.fromInteropNativePointerArray(): NativePointerArray {
TODO("implement wasm fromInteropNativePointerArray")
}
actual fun release() { actual fun release() {
elements.forEach { elements.forEach {
_free(it) _free(it)
...@@ -111,6 +168,10 @@ private fun toWasm(dest: NativePointer, src: CharArray) { ...@@ -111,6 +168,10 @@ private fun toWasm(dest: NativePointer, src: CharArray) {
js("HEAPU16.set(src, dest)") js("HEAPU16.set(src, dest)")
} }
private fun toWasm(dest: NativePointer, src: ShortArray) {
js("HEAPU16.set(src, dest)")
}
private fun toWasm(dest: NativePointer, src: FloatArray) { private fun toWasm(dest: NativePointer, src: FloatArray) {
js("HEAPU32.set(src, dest)") js("HEAPU32.set(src, dest)")
} }
...@@ -127,8 +188,28 @@ private fun fromWasm(src: NativePointer, result: CharArray) { ...@@ -127,8 +188,28 @@ private fun fromWasm(src: NativePointer, result: CharArray) {
js("result.set(HEAPU16.subarray(src, result.size))") js("result.set(HEAPU16.subarray(src, result.size))")
} }
private fun fromWasm(src: NativePointer, result: ShortArray) {
js("result.set(HEAPU16.subarray(src, result.size))")
}
private fun fromWasm(src: NativePointer, result: IntArray) {
js("result.set(HEAPU32.subarray(src, result.size))")
}
private fun fromWasm(src: NativePointer, result: FloatArray) { private fun fromWasm(src: NativePointer, result: FloatArray) {
js("result.set(HEAPU32.subarray(src, result.size))") js("result.set(HEAPU32.subarray(src, result.size))")
} }
actual class NativePointerArray actual constructor(size: Int) {
internal val backing = IntArray(size)
actual operator fun get(index: Int): NativePointer {
return backing[index]
}
actual operator fun set(index: Int, value: NativePointer) {
backing[index] = value
}
actual val size: Int
get() = backing.size
}
...@@ -47,7 +47,6 @@ actual fun reachabilityBarrier(obj: Any?) { ...@@ -47,7 +47,6 @@ actual fun reachabilityBarrier(obj: Any?) {
} }
actual typealias NativePointer = Long actual typealias NativePointer = Long
actual typealias NativePointerArray = LongArray
actual typealias InteropPointer = Any? actual typealias InteropPointer = Any?
...@@ -56,8 +55,36 @@ actual class InteropScope actual constructor() { ...@@ -56,8 +55,36 @@ actual class InteropScope actual constructor() {
actual fun InteropPointer.fromInterop(result: CharArray) {} actual fun InteropPointer.fromInterop(result: CharArray) {}
actual fun toInterop(array: ByteArray?): InteropPointer = array actual fun toInterop(array: ByteArray?): InteropPointer = array
actual fun InteropPointer.fromInterop(result: ByteArray) {} actual fun InteropPointer.fromInterop(result: ByteArray) {}
actual fun toInterop(array: ShortArray?): InteropPointer = array
actual fun InteropPointer.fromInterop(result: ShortArray) {}
actual fun toInterop(array: IntArray?): InteropPointer = array
actual fun InteropPointer.fromInterop(result: IntArray) {}
actual fun toInterop(array: LongArray?): InteropPointer = array
actual fun InteropPointer.fromInterop(result: LongArray) {}
actual fun toInterop(array: FloatArray?): InteropPointer = array actual fun toInterop(array: FloatArray?): InteropPointer = array
actual fun InteropPointer.fromInterop(result: FloatArray) {} actual fun InteropPointer.fromInterop(result: FloatArray) {}
actual fun toInterop(array: NativePointerArray?): InteropPointer = array actual fun toInterop(array: DoubleArray?): InteropPointer = array
actual fun InteropPointer.fromInterop(result: DoubleArray) {}
actual fun toInterop(array: NativePointerArray?): InteropPointer = array?.backing
actual fun InteropPointer.fromInterop(result: NativePointerArray) {}
actual fun toInterop(stringArray: Array<String>?): InteropPointer = stringArray
actual fun InteropPointer.fromInteropNativePointerArray(): NativePointerArray =
NativePointerArray((this as LongArray).size, this)
actual fun release() {} actual fun release() {}
} }
actual class NativePointerArray constructor(size: Int, internal val backing: LongArray) {
actual constructor(size: Int) : this(size, LongArray(size))
actual operator fun get(index: Int): NativePointer {
return backing[index]
}
actual operator fun set(index: Int, value: NativePointer) {
backing[index] = value
}
actual val size: Int
get() = backing.size
}
\ No newline at end of file
...@@ -59,6 +59,45 @@ actual class InteropScope actual constructor() { ...@@ -59,6 +59,45 @@ actual class InteropScope actual constructor() {
actual fun InteropPointer.fromInterop(result: ByteArray) {} actual fun InteropPointer.fromInterop(result: ByteArray) {}
actual fun toInterop(array: ShortArray?): InteropPointer {
return if (array != null) {
val pinned = array.pin()
elements.add(pinned)
val result = pinned.addressOf(0).rawValue
result
} else {
NativePtr.NULL
}
}
actual fun InteropPointer.fromInterop(result: ShortArray) {}
actual fun toInterop(array: IntArray?): InteropPointer {
return if (array != null) {
val pinned = array.pin()
elements.add(pinned)
val result = pinned.addressOf(0).rawValue
result
} else {
NativePtr.NULL
}
}
actual fun InteropPointer.fromInterop(result: IntArray) {}
actual fun toInterop(array: LongArray?): InteropPointer {
return if (array != null) {
val pinned = array.pin()
elements.add(pinned)
val result = pinned.addressOf(0).rawValue
result
} else {
NativePtr.NULL
}
}
actual fun InteropPointer.fromInterop(result: LongArray) {}
actual fun toInterop(array: FloatArray?): InteropPointer { actual fun toInterop(array: FloatArray?): InteropPointer {
return if (array != null) { return if (array != null) {
val pinned = array.pin() val pinned = array.pin()
...@@ -70,6 +109,21 @@ actual class InteropScope actual constructor() { ...@@ -70,6 +109,21 @@ actual class InteropScope actual constructor() {
} }
} }
actual fun InteropPointer.fromInterop(result: FloatArray) {}
actual fun toInterop(array: DoubleArray?): InteropPointer {
return if (array != null) {
val pinned = array.pin()
elements.add(pinned)
val result = pinned.addressOf(0).rawValue
result
} else {
NativePtr.NULL
}
}
actual fun InteropPointer.fromInterop(result: DoubleArray) {}
actual fun toInterop(array: NativePointerArray?): InteropPointer { actual fun toInterop(array: NativePointerArray?): InteropPointer {
return if (array != null) { return if (array != null) {
// We pass it as LongArray via boundary. // We pass it as LongArray via boundary.
...@@ -82,7 +136,25 @@ actual class InteropScope actual constructor() { ...@@ -82,7 +136,25 @@ actual class InteropScope actual constructor() {
} }
} }
actual fun InteropPointer.fromInterop(result: FloatArray) {} actual fun InteropPointer.fromInterop(result: NativePointerArray) {}
actual fun toInterop(stringArray: Array<String>?): InteropPointer {
if (stringArray == null) return NativePtr.NULL
val pins = stringArray.toList()
.map { it.encodeToByteArray().pin() }
val nativePointerArray = NativePointerArray(stringArray.size)
pins.forEachIndexed { index, pin ->
elements.add(pin)
nativePointerArray[index] = pin.addressOf(0).rawValue
}
return toInterop(nativePointerArray)
}
actual fun InteropPointer.fromInteropNativePointerArray(): NativePointerArray {
TODO("implement native fromInteropNativePointerArray")
}
actual fun release() { actual fun release() {
elements.forEach { elements.forEach {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment