Unverified Commit 839ae5e2 authored by Oleksandr Karpovich's avatar Oleksandr Karpovich Committed by GitHub

add more Surface Tests (#264)

* add more Surface Tests

* fix org_jetbrains_skia_Pixmap__1nMake
Co-authored-by: 's avatarOleksandr Karpovich <oleksandr.karpovich@jetbrains.com>
parent 71e2477d
......@@ -59,6 +59,10 @@ namespace skija {
namespace ImageInfo {
void writeImageInfoForInterop(SkImageInfo imageInfo, KInt* imageInfoResult, KNativePointer* colorSpacePtrsArray);
}
namespace SurfaceProps {
std::unique_ptr<SkSurfaceProps> toSkSurfaceProps(KInt* surfacePropsInts);
}
}
std::unique_ptr<SkMatrix> skMatrix(KFloat* matrixArray);
......
......@@ -157,4 +157,15 @@ namespace skija {
colorSpacePtrsArray[0] = imageInfo.refColorSpace().release();
}
}
namespace SurfaceProps {
std::unique_ptr<SkSurfaceProps> toSkSurfaceProps(KInt* surfacePropsInts) {
if (surfacePropsInts == nullptr) {
return std::unique_ptr<SkSurfaceProps>(nullptr);
}
int flags = surfacePropsInts[0];
SkPixelGeometry geom = static_cast<SkPixelGeometry>(surfacePropsInts[1]);
return std::make_unique<SkSurfaceProps>(flags, geom);
}
}
}
......@@ -19,8 +19,7 @@ SKIKO_EXPORT KNativePointer org_jetbrains_skia_Pixmap__1nMakeNull
}
SKIKO_EXPORT KNativePointer org_jetbrains_skia_Pixmap__1nMake
(KInteropPointer __Kinstance,
KInt width, KInt height, KInt colorType, KInt alphaType, KNativePointer colorSpacePtr, KNativePointer pixelsPtr, KInt rowBytes) {
(KInt width, KInt height, KInt colorType, KInt alphaType, KNativePointer colorSpacePtr, KNativePointer pixelsPtr, KInt rowBytes) {
SkColorSpace* colorSpace = interopToPtr<SkColorSpace*>(colorSpacePtr);
SkImageInfo imageInfo = SkImageInfo::Make(width,
height,
......
......@@ -8,85 +8,52 @@
SKIKO_EXPORT KNativePointer org_jetbrains_skia_Surface__1nMakeRasterDirect
(KInteropPointer __Kinstance,
KInt width, KInt height, KInt colorType, KInt alphaType, KNativePointer colorSpacePtr,
KNativePointer pixelsPtr, KNativePointer rowBytes,
KInteropPointer surfacePropsObj)
{
TODO("implement org_jetbrains_skia_Surface__1nMakeRasterDirect");
}
#if 0
SKIKO_EXPORT KNativePointer org_jetbrains_skia_Surface__1nMakeRasterDirect
(KInteropPointer __Kinstance,
KInt width, KInt height, KInt colorType, KInt alphaType, KNativePointer colorSpacePtr,
KNativePointer pixelsPtr, KNativePointer rowBytes,
KInteropPointer surfacePropsObj)
(KInt width, KInt height, KInt colorType, KInt alphaType, KNativePointer colorSpacePtr,
KNativePointer pixelsPtr, KInt rowBytes,
KInt* surfacePropsInts)
{
SkColorSpace* colorSpace = reinterpret_cast<SkColorSpace*>((colorSpacePtr));
SkColorSpace* colorSpace = reinterpret_cast<SkColorSpace*>(colorSpacePtr);
SkImageInfo imageInfo = SkImageInfo::Make(width,
height,
static_cast<SkColorType>(colorType),
static_cast<SkAlphaType>(alphaType),
sk_ref_sp<SkColorSpace>(colorSpace));
std::unique_ptr<SkSurfaceProps> surfaceProps = skija::SurfaceProps::toSkSurfaceProps(env, surfacePropsObj);
std::unique_ptr<SkSurfaceProps> surfaceProps = skija::SurfaceProps::toSkSurfaceProps(surfacePropsInts);
sk_sp<SkSurface> instance = SkSurface::MakeRasterDirect(
imageInfo,
reinterpret_cast<void*>((pixelsPtr)),
reinterpret_cast<void*>(pixelsPtr),
rowBytes,
surfaceProps.get());
return reinterpret_cast<KNativePointer>(instance.release());
}
#endif
SKIKO_EXPORT KNativePointer org_jetbrains_skia_Surface__1nMakeRasterDirectWithPixmap
(KInteropPointer __Kinstance,
KNativePointer pixmapPtr, KInteropPointer surfacePropsObj)
{
TODO("implement org_jetbrains_skia_Surface__1nMakeRasterDirectWithPixmap");
}
#if 0
SKIKO_EXPORT KNativePointer org_jetbrains_skia_Surface__1nMakeRasterDirectWithPixmap
(KInteropPointer __Kinstance,
KNativePointer pixmapPtr, KInteropPointer surfacePropsObj)
(KNativePointer pixmapPtr, KInt* surfacePropsInts)
{
SkPixmap* pixmap = reinterpret_cast<SkPixmap*>((pixmapPtr));
std::unique_ptr<SkSurfaceProps> surfaceProps = skija::SurfaceProps::toSkSurfaceProps(env, surfacePropsObj);
SkPixmap* pixmap = reinterpret_cast<SkPixmap*>(pixmapPtr);
std::unique_ptr<SkSurfaceProps> surfaceProps = skija::SurfaceProps::toSkSurfaceProps(surfacePropsInts);
sk_sp<SkSurface> instance = SkSurface::MakeRasterDirect(*pixmap, surfaceProps.get());
return reinterpret_cast<KNativePointer>(instance.release());
}
#endif
SKIKO_EXPORT KNativePointer org_jetbrains_skia_Surface__1nMakeRaster
(KInteropPointer __Kinstance,
KInt width, KInt height, KInt colorType, KInt alphaType, KNativePointer colorSpacePtr,
KNativePointer rowBytes,
KInteropPointer surfacePropsObj)
{
TODO("implement org_jetbrains_skia_Surface__1nMakeRaster");
}
#if 0
SKIKO_EXPORT KNativePointer org_jetbrains_skia_Surface__1nMakeRaster
(KInteropPointer __Kinstance,
KInt width, KInt height, KInt colorType, KInt alphaType, KNativePointer colorSpacePtr,
KNativePointer rowBytes,
KInteropPointer surfacePropsObj)
(KInt width, KInt height, KInt colorType, KInt alphaType, KNativePointer colorSpacePtr,
KInt rowBytes,
KInt* surfacePropsInts)
{
SkColorSpace* colorSpace = reinterpret_cast<SkColorSpace*>((colorSpacePtr));
SkColorSpace* colorSpace = reinterpret_cast<SkColorSpace*>(colorSpacePtr);
SkImageInfo imageInfo = SkImageInfo::Make(width,
height,
static_cast<SkColorType>(colorType),
static_cast<SkAlphaType>(alphaType),
sk_ref_sp<SkColorSpace>(colorSpace));
std::unique_ptr<SkSurfaceProps> surfaceProps = skija::SurfaceProps::toSkSurfaceProps(env, surfacePropsObj);
std::unique_ptr<SkSurfaceProps> surfaceProps = skija::SurfaceProps::toSkSurfaceProps(surfacePropsInts);
sk_sp<SkSurface> instance = SkSurface::MakeRaster(
imageInfo,
......@@ -94,7 +61,6 @@ SKIKO_EXPORT KNativePointer org_jetbrains_skia_Surface__1nMakeRaster
surfaceProps.get());
return reinterpret_cast<KNativePointer>(instance.release());
}
#endif
SKIKO_EXPORT KNativePointer org_jetbrains_skia_Surface__1nMakeRasterN32Premul
......
......@@ -39,7 +39,7 @@ class Surface : RefCnt {
fun makeRasterDirect(
imageInfo: ImageInfo,
pixelsPtr: NativePointer,
rowBytes: NativePointer
rowBytes: Int
): Surface {
return makeRasterDirect(imageInfo, pixelsPtr, rowBytes, null)
}
......@@ -50,9 +50,11 @@ class Surface : RefCnt {
): Surface {
return try {
Stats.onNativeCall()
val ptr = _nMakeRasterDirectWithPixmap(
getPtr(pixmap), surfaceProps
)
val ptr = interopScope {
_nMakeRasterDirectWithPixmap(
getPtr(pixmap), toInterop(surfaceProps?.packToIntArray())
)
}
require(ptr != NullPointer) {
"Failed Surface.makeRasterDirect($pixmap, $surfaceProps)"
}
......@@ -94,21 +96,23 @@ class Surface : RefCnt {
fun makeRasterDirect(
imageInfo: ImageInfo,
pixelsPtr: NativePointer,
rowBytes: NativePointer,
rowBytes: Int,
surfaceProps: SurfaceProps?
): Surface {
return try {
Stats.onNativeCall()
val ptr = _nMakeRasterDirect(
imageInfo.width,
imageInfo.height,
imageInfo.colorInfo.colorType.ordinal,
imageInfo.colorInfo.alphaType.ordinal,
getPtr(imageInfo.colorInfo.colorSpace),
pixelsPtr,
rowBytes,
surfaceProps
)
val ptr = interopScope {
_nMakeRasterDirect(
imageInfo.width,
imageInfo.height,
imageInfo.colorInfo.colorType.ordinal,
imageInfo.colorInfo.alphaType.ordinal,
getPtr(imageInfo.colorInfo.colorSpace),
pixelsPtr,
rowBytes,
toInterop(surfaceProps?.packToIntArray())
)
}
require(ptr != NullPointer) {
"Failed Surface.makeRasterDirect($imageInfo, $pixelsPtr, $rowBytes, $surfaceProps)"
}
......@@ -135,7 +139,7 @@ class Surface : RefCnt {
* @return new Surface
*/
fun makeRaster(imageInfo: ImageInfo): Surface {
return makeRaster(imageInfo, NullPointer, null)
return makeRaster(imageInfo, imageInfo.minRowBytes, null)
}
/**
......@@ -162,7 +166,7 @@ class Surface : RefCnt {
*/
fun makeRaster(
imageInfo: ImageInfo,
rowBytes: NativePointer
rowBytes: Int
): Surface {
return makeRaster(imageInfo, rowBytes, null)
}
......@@ -193,20 +197,22 @@ class Surface : RefCnt {
*/
fun makeRaster(
imageInfo: ImageInfo,
rowBytes: NativePointer,
rowBytes: Int,
surfaceProps: SurfaceProps?
): Surface {
return try {
Stats.onNativeCall()
val ptr = _nMakeRaster(
imageInfo.width,
imageInfo.height,
imageInfo.colorInfo.colorType.ordinal,
imageInfo.colorInfo.alphaType.ordinal,
getPtr(imageInfo.colorInfo.colorSpace),
rowBytes,
surfaceProps
)
val ptr = interopScope {
_nMakeRaster(
imageInfo.width,
imageInfo.height,
imageInfo.colorInfo.colorType.ordinal,
imageInfo.colorInfo.alphaType.ordinal,
getPtr(imageInfo.colorInfo.colorSpace),
rowBytes,
toInterop(surfaceProps?.packToIntArray())
)
}
require(ptr != NullPointer) {
"Failed Surface.makeRaster($imageInfo, $rowBytes, $surfaceProps)"
}
......@@ -1011,13 +1017,13 @@ private external fun _nMakeRasterDirect(
alphaType: Int,
colorSpacePtr: NativePointer,
pixelsPtr: NativePointer,
rowBytes: NativePointer,
surfaceProps: SurfaceProps?
rowBytes: Int,
surfaceProps: InteropPointer
): NativePointer
@ExternalSymbolName("org_jetbrains_skia_Surface__1nMakeRasterDirectWithPixmap")
private external fun _nMakeRasterDirectWithPixmap(pixmapPtr: NativePointer, surfaceProps: SurfaceProps?): NativePointer
private external fun _nMakeRasterDirectWithPixmap(pixmapPtr: NativePointer, surfaceProps: InteropPointer): NativePointer
@ExternalSymbolName("org_jetbrains_skia_Surface__1nMakeRaster")
private external fun _nMakeRaster(
......@@ -1026,8 +1032,8 @@ private external fun _nMakeRaster(
colorType: Int,
alphaType: Int,
colorSpacePtr: NativePointer,
rowBytes: NativePointer,
surfaceProps: SurfaceProps?
rowBytes: Int,
surfaceProps: InteropPointer
): NativePointer
......
......@@ -44,4 +44,8 @@ class SurfaceProps constructor(
fun withPixelGeometry(_pixelGeometry: PixelGeometry): SurfaceProps {
return if (pixelGeometry == _pixelGeometry) this else SurfaceProps(isDeviceIndependentFonts, _pixelGeometry)
}
}
\ No newline at end of file
internal fun packToIntArray(): IntArray {
return intArrayOf(_getFlags(), _getPixelGeometryOrdinal())
}
}
package org.jetbrains.skia
import org.jetbrains.skia.impl.interopScope
import org.jetbrains.skia.impl.use
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertNotEquals
import kotlin.test.assertTrue
import org.jetbrains.skiko.tests.allocateBytesForPixels
import org.jetbrains.skiko.tests.runTest
import kotlin.test.*
class SurfaceTest {
......@@ -57,4 +56,49 @@ class SurfaceTest {
assertEquals(context, null)
}
}
@Test
fun canMakeRaster() = runTest {
val imageInfo = ImageInfo.makeN32Premul(100, 100)
val surface1 = Surface.makeRaster(
imageInfo, imageInfo.minRowBytes, SurfaceProps()
)
assertEquals(100, surface1.width)
assertEquals(100, surface1.height)
val surface2 = Surface.makeRaster(
imageInfo, imageInfo.minRowBytes, null
)
}
@Test
fun canMakeRasterDirect() = runTest {
interopScope {
val imageInfo = ImageInfo.makeN32Premul(25, 25)
val addr = allocateBytesForPixels(25 * imageInfo.minRowBytes)
val surface = Surface.makeRasterDirect(imageInfo, addr, imageInfo.minRowBytes)
val writePixelsBitmap = Bitmap()
writePixelsBitmap.setImageInfo(ImageInfo.makeN32Premul(10, 20))
writePixelsBitmap.allocPixels()
surface.writePixels(writePixelsBitmap, 0, 0)
}
}
@Test
fun canMakeRasterDirectUsingPixmap() = runTest {
interopScope {
val imageInfo = ImageInfo.makeN32Premul(20, 20)
val addr = allocateBytesForPixels(20 * imageInfo.minRowBytes)
val pixmap = Pixmap.make(imageInfo, addr, imageInfo.minRowBytes)
val surface = Surface.makeRasterDirect(pixmap)
val writePixelsBitmap = Bitmap()
writePixelsBitmap.setImageInfo(ImageInfo.makeN32Premul(10, 10))
writePixelsBitmap.allocPixels()
surface.writePixels(writePixelsBitmap, 0, 0)
}
}
}
package org.jetbrains.skiko.tests
import org.jetbrains.skia.ByteBuffer
import org.jetbrains.skia.ExternalSymbolName
import org.jetbrains.skia.impl.InteropPointer
import org.jetbrains.skia.impl.Library
import org.jetbrains.skia.impl.withResult
import org.jetbrains.skia.impl.*
class TestHelpers {
......@@ -37,6 +36,12 @@ class TestHelpers {
}
}
fun getPointerFromByteBuffer(buffer: ByteBuffer): NativePointer {
val result = _nGetPointerFromByteBuffer(buffer)
require(result != Native.NullPointer) { "The given buffer " + buffer + "is not a direct buffer or current JVM doesn't support JNI direct buffer access!" }
return result
}
init {
Library.staticLoad()
}
......@@ -56,3 +61,6 @@ private external fun _nFillIntArrayOf5(interopPointer: InteropPointer)
@ExternalSymbolName("org_jetbrains_skiko_tests_TestHelpers__1nFillDoubleArrayOf5")
private external fun _nFillDoubleArrayOf5(interopPointer: InteropPointer)
@ExternalSymbolName("org_jetbrains_skiko_tests_TestHelpers__1nGetPointerFromByteBuffer")
private external fun _nGetPointerFromByteBuffer(buffer: ByteBuffer?): NativePointer
package org.jetbrains.skiko.tests
import org.jetbrains.skia.impl.InteropScope
import org.jetbrains.skia.impl.NativePointer
expect annotation class IgnoreTestOnJvm()
expect fun runTest(block: suspend () -> Unit)
expect fun InteropScope.allocateBytesForPixels(size: Int): NativePointer
......@@ -2,6 +2,8 @@ package org.jetbrains.skiko.tests
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.promise
import org.jetbrains.skia.impl.InteropScope
import org.jetbrains.skia.impl.NativePointer
import org.jetbrains.skiko.wasm.await
import org.jetbrains.skiko.wasm.wasmSetup
......@@ -12,3 +14,7 @@ actual fun runTest(block: suspend () -> Unit): dynamic = GlobalScope.promise {
wasmSetup.await()
block()
}
actual fun InteropScope.allocateBytesForPixels(size: Int): NativePointer {
return toInterop(ByteArray(size))
}
......@@ -7,8 +7,8 @@
extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skia_SurfaceKt__1nMakeRasterDirect
(JNIEnv* env, jclass jclass,
jint width, jint height, jint colorType, jint alphaType, jlong colorSpacePtr,
jlong pixelsPtr, jlong rowBytes,
jobject surfacePropsObj)
jlong pixelsPtr, jint rowBytes,
jintArray surfacePropsInts)
{
SkColorSpace* colorSpace = reinterpret_cast<SkColorSpace*>(static_cast<uintptr_t>(colorSpacePtr));
SkImageInfo imageInfo = SkImageInfo::Make(width,
......@@ -16,7 +16,7 @@ extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skia_SurfaceKt__1nMakeRast
static_cast<SkColorType>(colorType),
static_cast<SkAlphaType>(alphaType),
sk_ref_sp<SkColorSpace>(colorSpace));
std::unique_ptr<SkSurfaceProps> surfaceProps = skija::SurfaceProps::toSkSurfaceProps(env, surfacePropsObj);
std::unique_ptr<SkSurfaceProps> surfaceProps = skija::SurfaceProps::toSkSurfaceProps(env, surfacePropsInts);
sk_sp<SkSurface> instance = SkSurface::MakeRasterDirect(
imageInfo,
......@@ -28,10 +28,10 @@ extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skia_SurfaceKt__1nMakeRast
extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skia_SurfaceKt__1nMakeRasterDirectWithPixmap
(JNIEnv* env, jclass jclass,
jlong pixmapPtr, jobject surfacePropsObj)
jlong pixmapPtr, jintArray surfacePropsInts)
{
SkPixmap* pixmap = reinterpret_cast<SkPixmap*>(static_cast<uintptr_t>(pixmapPtr));
std::unique_ptr<SkSurfaceProps> surfaceProps = skija::SurfaceProps::toSkSurfaceProps(env, surfacePropsObj);
std::unique_ptr<SkSurfaceProps> surfaceProps = skija::SurfaceProps::toSkSurfaceProps(env, surfacePropsInts);
sk_sp<SkSurface> instance = SkSurface::MakeRasterDirect(*pixmap, surfaceProps.get());
return reinterpret_cast<jlong>(instance.release());
......@@ -40,8 +40,8 @@ extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skia_SurfaceKt__1nMakeRast
extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skia_SurfaceKt__1nMakeRaster
(JNIEnv* env, jclass jclass,
jint width, jint height, jint colorType, jint alphaType, jlong colorSpacePtr,
jlong rowBytes,
jobject surfacePropsObj)
jint rowBytes,
jintArray surfacePropsInts)
{
SkColorSpace* colorSpace = reinterpret_cast<SkColorSpace*>(static_cast<uintptr_t>(colorSpacePtr));
SkImageInfo imageInfo = SkImageInfo::Make(width,
......@@ -49,7 +49,7 @@ extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skia_SurfaceKt__1nMakeRast
static_cast<SkColorType>(colorType),
static_cast<SkAlphaType>(alphaType),
sk_ref_sp<SkColorSpace>(colorSpace));
std::unique_ptr<SkSurfaceProps> surfaceProps = skija::SurfaceProps::toSkSurfaceProps(env, surfacePropsObj);
std::unique_ptr<SkSurfaceProps> surfaceProps = skija::SurfaceProps::toSkSurfaceProps(env, surfacePropsInts);
sk_sp<SkSurface> instance = SkSurface::MakeRaster(
imageInfo,
......
......@@ -748,6 +748,17 @@ namespace skija {
std::unique_ptr<SkSurfaceProps>(nullptr);
return std::make_unique<SkSurfaceProps>(flags, geom);
}
std::unique_ptr<SkSurfaceProps> toSkSurfaceProps(JNIEnv* env, jintArray surfacePropsInts) {
if (surfacePropsInts == nullptr) {
return std::unique_ptr<SkSurfaceProps>(nullptr);
}
jint *ints = env->GetIntArrayElements(surfacePropsInts, NULL);
uint32_t flags = ints[0];
SkPixelGeometry geom = static_cast<SkPixelGeometry>(ints[1]);
env->ReleaseIntArrayElements(surfacePropsInts, ints, 0);
return std::make_unique<SkSurfaceProps>(flags, geom);
}
}
namespace impl {
......
......@@ -294,6 +294,7 @@ namespace skija {
extern jmethodID _getPixelGeometryOrdinal;
void onLoad(JNIEnv* env);
std::unique_ptr<SkSurfaceProps> toSkSurfaceProps(JNIEnv* env, jobject surfacePropsObj);
std::unique_ptr<SkSurfaceProps> toSkSurfaceProps(JNIEnv* env, jintArray surfacePropsInts);
}
namespace SamplingMode {
......
......@@ -55,3 +55,9 @@ extern "C" JNIEXPORT void JNICALL Java_org_jetbrains_skiko_tests_TestHelpersKt__
result_double[4] = 10000000.9991;
env->ReleaseDoubleArrayElements(jdarray, result_double, 0);
}
extern "C" JNIEXPORT jlong JNICALL Java_org_jetbrains_skiko_tests_TestHelpersKt__1nGetPointerFromByteBuffer
(JNIEnv *env, jclass, jobject buffer) {
return reinterpret_cast<jlong>(env->GetDirectBufferAddress(buffer));
}
package org.jetbrains.skiko.tests
import kotlinx.coroutines.runBlocking
import org.jetbrains.skia.impl.InteropScope
import org.jetbrains.skia.impl.NativePointer
import java.nio.ByteBuffer
actual typealias IgnoreTestOnJvm = org.junit.Ignore
actual fun runTest(block: suspend () -> Unit) {
runBlocking { block() }
}
actual fun InteropScope.allocateBytesForPixels(size: Int): NativePointer {
return TestHelpers().getPointerFromByteBuffer(ByteBuffer.allocateDirect(size))
}
package org.jetbrains.skiko.tests
import kotlinx.coroutines.runBlocking
import org.jetbrains.skia.impl.InteropScope
import org.jetbrains.skia.impl.NativePointer
annotation class DoNothing
actual typealias IgnoreTestOnJvm = DoNothing
......@@ -8,3 +10,7 @@ actual typealias IgnoreTestOnJvm = DoNothing
actual fun runTest(block: suspend () -> Unit) {
runBlocking { block() }
}
actual fun InteropScope.allocateBytesForPixels(size: Int): NativePointer {
return toInterop(ByteArray(size))
}
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