Unverified Commit d709bbb1 authored by Desmond's avatar Desmond Committed by GitHub

Fix bindings for PixelRef (#1149)

This PR fixes https://youtrack.jetbrains.com/issue/SKIKO-996

The PixelRef bindings have two mistakes in them:

1. `rowBytes` is defined as a `NativePointer`, whereas it is actually an
integer with the number of bytes per row. This is the same as
`Bitmap::rowBytes`.
2. It is missing the `pixels` property that actually returns a pointer
to the pixel data.

See: https://api.skia.org/classSkPixelRef.html
parent d6e53c50
...@@ -27,13 +27,20 @@ class PixelRef internal constructor(ptr: NativePointer) : RefCnt(ptr) { ...@@ -27,13 +27,20 @@ class PixelRef internal constructor(ptr: NativePointer) : RefCnt(ptr) {
} finally { } finally {
reachabilityBarrier(this) reachabilityBarrier(this)
} }
val rowBytes: NativePointer val rowBytes: Int
get() = try { get() = try {
Stats.onNativeCall() Stats.onNativeCall()
PixelRef_nGetRowBytes(_ptr) PixelRef_nGetRowBytes(_ptr)
} finally { } finally {
reachabilityBarrier(this) reachabilityBarrier(this)
} }
val pixels: NativePointer
get() = try {
Stats.onNativeCall()
PixelRef_nGetPixels(_ptr)
} finally {
reachabilityBarrier(this)
}
/** /**
* Returns a non-zero, unique value corresponding to the pixels in this * Returns a non-zero, unique value corresponding to the pixels in this
...@@ -83,8 +90,11 @@ class PixelRef internal constructor(ptr: NativePointer) : RefCnt(ptr) { ...@@ -83,8 +90,11 @@ class PixelRef internal constructor(ptr: NativePointer) : RefCnt(ptr) {
} }
} }
@ExternalSymbolName("org_jetbrains_skia_PixelRef__1nGetPixels")
private external fun PixelRef_nGetPixels(ptr: NativePointer): NativePointer
@ExternalSymbolName("org_jetbrains_skia_PixelRef__1nGetRowBytes") @ExternalSymbolName("org_jetbrains_skia_PixelRef__1nGetRowBytes")
private external fun PixelRef_nGetRowBytes(ptr: NativePointer): NativePointer private external fun PixelRef_nGetRowBytes(ptr: NativePointer): Int
@ExternalSymbolName("org_jetbrains_skia_PixelRef__1nGetGenerationId") @ExternalSymbolName("org_jetbrains_skia_PixelRef__1nGetGenerationId")
private external fun PixelRef_nGetGenerationId(ptr: NativePointer): Int private external fun PixelRef_nGetGenerationId(ptr: NativePointer): Int
......
package org.jetbrains.skia
import org.jetbrains.skia.util.assertIsNotNullPointer
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
class PixelRefTest {
@Test
fun pixelRefTest() {
val bitmap = Bitmap()
bitmap.allocPixels(ImageInfo.makeS32(7, 3, ColorAlphaType.OPAQUE))
val pixelRef = bitmap.pixelRef
assertNotNull(pixelRef)
assertEquals(7, pixelRef.width)
assertEquals(3, pixelRef.height)
assertEquals(7 * 4, pixelRef.rowBytes)
assertIsNotNullPointer(pixelRef.pixels)
}
}
\ No newline at end of file
package org.jetbrains.skia.util
import org.jetbrains.skia.impl.NativePointer
import kotlin.test.assertFalse
import kotlin.test.assertTrue
internal expect val NativePointer.isNullPointer: Boolean
fun assertIsNullPointer(ptr: NativePointer) =
assertTrue(ptr.isNullPointer, message = "Expected a null pointer")
fun assertIsNotNullPointer(ptr: NativePointer) =
assertFalse(ptr.isNullPointer, message = "Expected a non-null pointer")
...@@ -14,7 +14,13 @@ extern "C" JNIEXPORT jint JNICALL Java_org_jetbrains_skia_PixelRefKt__1nGetHeigh ...@@ -14,7 +14,13 @@ extern "C" JNIEXPORT jint JNICALL Java_org_jetbrains_skia_PixelRefKt__1nGetHeigh
return instance->height(); return instance->height();
} }
extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skia_PixelRefKt_PixelRef_1nGetRowBytes extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skia_PixelRefKt_PixelRef_1nGetPixels
(JNIEnv* env, jclass jclass, jlong ptr) {
SkPixelRef* instance = reinterpret_cast<SkPixelRef*>(static_cast<uintptr_t>(ptr));
return reinterpret_cast<jlong>(instance->pixels());
}
extern "C" JNIEXPORT jint JNICALL Java_org_jetbrains_skia_PixelRefKt_PixelRef_1nGetRowBytes
(JNIEnv* env, jclass jclass, jlong ptr) { (JNIEnv* env, jclass jclass, jlong ptr) {
SkPixelRef* instance = reinterpret_cast<SkPixelRef*>(static_cast<uintptr_t>(ptr)); SkPixelRef* instance = reinterpret_cast<SkPixelRef*>(static_cast<uintptr_t>(ptr));
return instance->rowBytes(); return instance->rowBytes();
......
package org.jetbrains.skia.util
import org.jetbrains.skia.impl.NativePointer
internal actual val NativePointer.isNullPointer: Boolean
get() = this == 0L
...@@ -13,6 +13,12 @@ SKIKO_EXPORT KInt org_jetbrains_skia_PixelRef__1nGetHeight ...@@ -13,6 +13,12 @@ SKIKO_EXPORT KInt org_jetbrains_skia_PixelRef__1nGetHeight
return instance->height(); return instance->height();
} }
SKIKO_EXPORT KNativePointer org_jetbrains_skia_PixelRef__1nGetPixels
(KNativePointer ptr) {
SkPixelRef* instance = reinterpret_cast<SkPixelRef*>((ptr));
return instance->pixels();
}
SKIKO_EXPORT KInt org_jetbrains_skia_PixelRef__1nGetRowBytes SKIKO_EXPORT KInt org_jetbrains_skia_PixelRef__1nGetRowBytes
(KNativePointer ptr) { (KNativePointer ptr) {
SkPixelRef* instance = reinterpret_cast<SkPixelRef*>((ptr)); SkPixelRef* instance = reinterpret_cast<SkPixelRef*>((ptr));
......
package org.jetbrains.skia.util
import kotlinx.cinterop.ExperimentalForeignApi
import org.jetbrains.skia.impl.NativePointer
@OptIn(ExperimentalForeignApi::class)
internal actual val NativePointer.isNullPointer: Boolean
get() = this == NativePointer.NULL
package org.jetbrains.skia.util
import org.jetbrains.skia.impl.NativePointer
internal actual val NativePointer.isNullPointer: Boolean
get() = this == 0
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