Unverified Commit 8dee13dc authored by Shagen Ogandzhanian's avatar Shagen Ogandzhanian Committed by GitHub

withResult functions accept InteropScopeContext functions (#354)

* All withResult family of functions accept InteropScope handlers

* Introduce InteropScopeContext

* Fix after rebase

* Get rid of InteropScopeContext alias
parent ff381625
package org.jetbrains.skia package org.jetbrains.skia
import org.jetbrains.skia.impl.InteropPointer import org.jetbrains.skia.impl.InteropPointer
import org.jetbrains.skia.impl.InteropScope
import org.jetbrains.skia.impl.withResult import org.jetbrains.skia.impl.withResult
/** /**
...@@ -86,15 +87,16 @@ class AnimationFrameInfo( ...@@ -86,15 +87,16 @@ class AnimationFrameInfo(
) )
} }
internal fun fromInteropPointer(block: (InteropPointer) -> Unit): AnimationFrameInfo { internal fun fromInteropPointer(block: InteropScope.(InteropPointer) -> Unit): AnimationFrameInfo {
return fromIntArray(withResult(IntArray(REPR_SIZE), block)) return fromIntArray(withResult(IntArray(REPR_SIZE), block))
} }
internal fun fromInteropArrayPointer(size: Int, block: (InteropPointer) -> Unit): Array<AnimationFrameInfo> { internal fun fromInteropArrayPointer(size: Int, block: InteropScope.(InteropPointer) -> Unit): Array<AnimationFrameInfo> {
val repr = withResult(IntArray(REPR_SIZE * size), block) val repr = withResult(IntArray(REPR_SIZE * size), block)
return Array(size) { fromIntArray(repr, it) } return Array(size) { fromIntArray(repr, it) }
} }
} }
internal constructor( internal constructor(
requiredFrame: Int, requiredFrame: Int,
duration: Int, duration: Int,
...@@ -114,6 +116,7 @@ class AnimationFrameInfo( ...@@ -114,6 +116,7 @@ class AnimationFrameInfo(
BlendMode.values()[blendModeOrdinal], BlendMode.values()[blendModeOrdinal],
frameRect frameRect
) )
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
if (other === this) return true if (other === this) return true
if (other !is AnimationFrameInfo) return false if (other !is AnimationFrameInfo) return false
......
package org.jetbrains.skia package org.jetbrains.skia
import org.jetbrains.skia.impl.InteropPointer import org.jetbrains.skia.impl.InteropPointer
import org.jetbrains.skia.impl.InteropScope
import org.jetbrains.skia.impl.withResult import org.jetbrains.skia.impl.withResult
/** /**
...@@ -57,7 +58,7 @@ class Matrix44(vararg mat: Float) { ...@@ -57,7 +58,7 @@ class Matrix44(vararg mat: Float) {
companion object { companion object {
val IDENTITY = Matrix44(1f, 0f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 1f) val IDENTITY = Matrix44(1f, 0f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 1f, 0f, 0f, 0f, 0f, 1f)
internal fun fromInteropPointer(block: (InteropPointer) -> Unit): Matrix44 { internal fun fromInteropPointer(block: InteropScope.(InteropPointer) -> Unit): Matrix44 {
val result = withResult(FloatArray(16), block) val result = withResult(FloatArray(16), block)
return Matrix44(*result) return Matrix44(*result)
} }
......
...@@ -57,7 +57,7 @@ class PathSegmentIterator internal constructor(val _path: Path?, ptr: NativePoin ...@@ -57,7 +57,7 @@ class PathSegmentIterator internal constructor(val _path: Path?, ptr: NativePoin
} }
private fun nextSegment() = pathSegmentFromIntArray(withResult(IntArray(10)) { private fun nextSegment() = pathSegmentFromIntArray(withResult(IntArray(10)) {
PathSegmentIterator_nNext(this._ptr, it) PathSegmentIterator_nNext(this@PathSegmentIterator._ptr, it)
}) })
} }
......
package org.jetbrains.skia package org.jetbrains.skia
import org.jetbrains.skia.impl.InteropPointer import org.jetbrains.skia.impl.InteropPointer
import org.jetbrains.skia.impl.InteropScope
import org.jetbrains.skia.impl.withResult import org.jetbrains.skia.impl.withResult
class Point(val x: Float, val y: Float) { class Point(val x: Float, val y: Float) {
...@@ -63,7 +64,7 @@ class Point(val x: Float, val y: Float) { ...@@ -63,7 +64,7 @@ class Point(val x: Float, val y: Float) {
return arr return arr
} }
internal fun fromInteropPointer(block: (InteropPointer) -> Unit): Point { internal fun fromInteropPointer(block: InteropScope.(InteropPointer) -> Unit): Point {
val result = withResult(FloatArray(2), block) val result = withResult(FloatArray(2), block)
return Point(result[0], result[1]) return Point(result[0], result[1])
} }
......
package org.jetbrains.skia package org.jetbrains.skia
import org.jetbrains.skia.impl.InteropPointer import org.jetbrains.skia.impl.InteropPointer
import org.jetbrains.skia.impl.InteropScope
import org.jetbrains.skia.impl.withResult import org.jetbrains.skia.impl.withResult
import kotlin.jvm.JvmStatic import kotlin.jvm.JvmStatic
import kotlin.math.abs import kotlin.math.abs
...@@ -153,7 +154,7 @@ class RRect internal constructor(l: Float, t: Float, r: Float, b: Float, val rad ...@@ -153,7 +154,7 @@ class RRect internal constructor(l: Float, t: Float, r: Float, b: Float, val rad
return RRect(l, t, l + w, t + h, floatArrayOf(minOf(w, h) / 2.0f)) return RRect(l, t, l + w, t + h, floatArrayOf(minOf(w, h) / 2.0f))
} }
internal fun fromInteropPointer(block: (InteropPointer) -> Unit): RRect { internal fun fromInteropPointer(block: InteropScope.(InteropPointer) -> Unit): RRect {
val result = withResult(FloatArray(12 /* 4 dimensions + 4 radii * 2 */), block) val result = withResult(FloatArray(12 /* 4 dimensions + 4 radii * 2 */), block)
return RRect(result[0], result[1], result[2], result[3], result.copyOfRange(4, 12)) return RRect(result[0], result[1], result[2], result[3], result.copyOfRange(4, 12))
} }
......
package org.jetbrains.skia package org.jetbrains.skia
import org.jetbrains.skia.ExternalSymbolName
import org.jetbrains.skia.impl.InteropPointer import org.jetbrains.skia.impl.InteropPointer
import org.jetbrains.skia.impl.InteropScope
import org.jetbrains.skia.impl.withResult import org.jetbrains.skia.impl.withResult
import kotlin.jvm.JvmStatic import kotlin.jvm.JvmStatic
...@@ -106,7 +106,7 @@ open class Rect constructor(val left: Float, val top: Float, val right: Float, v ...@@ -106,7 +106,7 @@ open class Rect constructor(val left: Float, val top: Float, val right: Float, v
return Rect(l, t, l + w, t + h) return Rect(l, t, l + w, t + h)
} }
internal fun fromInteropPointer(block: (InteropPointer) -> Unit): Rect { internal fun fromInteropPointer(block: InteropScope.(InteropPointer) -> Unit): Rect {
val result = withResult(FloatArray(4), block) val result = withResult(FloatArray(4), block)
return Rect(result[0], result[1], result[2], result[3]) return Rect(result[0], result[1], result[2], result[3])
} }
......
...@@ -47,14 +47,14 @@ expect class InteropScope() { ...@@ -47,14 +47,14 @@ expect class InteropScope() {
expect inline fun <T> interopScope(block: InteropScope.() -> T): T expect inline fun <T> interopScope(block: InteropScope.() -> T): T
inline fun withResult(result: ByteArray, block: (InteropPointer) -> Unit): ByteArray = interopScope { inline fun withResult(result: ByteArray, block: InteropScope.(InteropPointer) -> Unit): ByteArray = interopScope {
val handle = toInterop(result) val handle = toInterop(result)
block(handle) block(handle)
handle.fromInterop(result) handle.fromInterop(result)
result result
} }
inline fun withNullableResult(result: ByteArray, block: (InteropPointer) -> Boolean): ByteArray? = interopScope { inline fun withNullableResult(result: ByteArray, block: InteropScope.(InteropPointer) -> Boolean): ByteArray? = interopScope {
val handle = toInterop(result) val handle = toInterop(result)
return if (block(handle)) { return if (block(handle)) {
handle.fromInterop(result) handle.fromInterop(result)
...@@ -64,21 +64,21 @@ inline fun withNullableResult(result: ByteArray, block: (InteropPointer) -> Bool ...@@ -64,21 +64,21 @@ inline fun withNullableResult(result: ByteArray, block: (InteropPointer) -> Bool
} }
} }
inline fun withResult(result: FloatArray, block: (InteropPointer) -> Unit): FloatArray = interopScope { inline fun withResult(result: FloatArray, block: InteropScope.(InteropPointer) -> Unit): FloatArray = interopScope {
val handle = toInterop(result) val handle = toInterop(result)
block(handle) block(handle)
handle.fromInterop(result) handle.fromInterop(result)
result result
} }
inline fun withResult(result: IntArray, block: (InteropPointer) -> Unit): IntArray = interopScope { inline fun withResult(result: IntArray, block: InteropScope.(InteropPointer) -> Unit): IntArray = interopScope {
val handle = toInterop(result) val handle = toInterop(result)
block(handle) block(handle)
handle.fromInterop(result) handle.fromInterop(result)
result result
} }
inline fun withNullableResult(result: IntArray, block: (InteropPointer) -> Boolean): IntArray? = interopScope { inline fun withNullableResult(result: IntArray, block: InteropScope.(InteropPointer) -> Boolean): IntArray? = interopScope {
val handle = toInterop(result) val handle = toInterop(result)
return if (block(handle)) { return if (block(handle)) {
handle.fromInterop(result) handle.fromInterop(result)
...@@ -88,21 +88,21 @@ inline fun withNullableResult(result: IntArray, block: (InteropPointer) -> Boole ...@@ -88,21 +88,21 @@ inline fun withNullableResult(result: IntArray, block: (InteropPointer) -> Boole
} }
} }
inline fun withResult(result: ShortArray, block: (InteropPointer) -> Unit): ShortArray = interopScope { inline fun withResult(result: ShortArray, block: InteropScope.(InteropPointer) -> Unit): ShortArray = interopScope {
val handle = toInterop(result) val handle = toInterop(result)
block(handle) block(handle)
handle.fromInterop(result) handle.fromInterop(result)
result result
} }
inline fun withResult(result: DoubleArray, block: (InteropPointer) -> Unit): DoubleArray = interopScope { inline fun withResult(result: DoubleArray, block: InteropScope.(InteropPointer) -> Unit): DoubleArray = interopScope {
val handle = toInterop(result) val handle = toInterop(result)
block(handle) block(handle)
handle.fromInterop(result) handle.fromInterop(result)
result result
} }
inline fun withResult(result: NativePointerArray, block: (InteropPointer) -> Unit): NativePointerArray = interopScope { inline fun withResult(result: NativePointerArray, block: InteropScope.(InteropPointer) -> Unit): NativePointerArray = interopScope {
val handle = toInterop(result) val handle = toInterop(result)
block(handle) block(handle)
handle.fromInterop(result) handle.fromInterop(result)
......
package org.jetbrains.skia.svg package org.jetbrains.skia.svg
import org.jetbrains.skia.impl.InteropPointer import org.jetbrains.skia.impl.InteropPointer
import org.jetbrains.skia.impl.InteropScope
import org.jetbrains.skia.impl.withResult import org.jetbrains.skia.impl.withResult
class SVGLength(val value: Float, val unit: SVGLengthUnit) { class SVGLength(val value: Float, val unit: SVGLengthUnit) {
companion object { companion object {
internal fun fromInterop(block: (InteropPointer) -> Unit) internal fun fromInterop(block: InteropScope.(InteropPointer) -> Unit)
= withResult(IntArray(2), block).let { = withResult(IntArray(2), block).let {
SVGLength(Float.fromBits(it[0]), it[1]) SVGLength(Float.fromBits(it[0]), it[1])
} }
......
package org.jetbrains.skia.svg package org.jetbrains.skia.svg
import org.jetbrains.skia.impl.InteropPointer import org.jetbrains.skia.impl.InteropPointer
import org.jetbrains.skia.impl.InteropScope
import org.jetbrains.skia.impl.withResult import org.jetbrains.skia.impl.withResult
class SVGPreserveAspectRatio(align: SVGPreserveAspectRatioAlign, scale: SVGPreserveAspectRatioScale) { class SVGPreserveAspectRatio(align: SVGPreserveAspectRatioAlign, scale: SVGPreserveAspectRatioScale) {
...@@ -9,7 +10,7 @@ class SVGPreserveAspectRatio(align: SVGPreserveAspectRatioAlign, scale: SVGPrese ...@@ -9,7 +10,7 @@ class SVGPreserveAspectRatio(align: SVGPreserveAspectRatioAlign, scale: SVGPrese
internal val _scale: SVGPreserveAspectRatioScale internal val _scale: SVGPreserveAspectRatioScale
companion object { companion object {
internal fun fromInterop(block: (InteropPointer) -> Unit) = internal fun fromInterop(block: InteropScope.(InteropPointer) -> Unit) =
withResult(IntArray(2), block).let { withResult(IntArray(2), block).let {
SVGPreserveAspectRatio(it[0], it[1]) SVGPreserveAspectRatio(it[0], it[1])
} }
......
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