From 1430bd9e8800f4c5fda1e0202aad371d93a882ce Mon Sep 17 00:00:00 2001 From: tarsin Date: Thu, 2 Feb 2023 19:37:49 +0800 Subject: [PATCH] Rewrite mmap operations with KNI --- app/src/main/cpp/fdutils.c | 11 -------- .../moe/tarsin/ehviewer/PlatformTypes.kt | 7 ++++++ .../moe/tarsin/ehviewer/PlatformTypes.kt | 7 ++++++ .../moe/tarsin/ehviewer/PlatformTypes.kt | 7 ++++++ .../moe/tarsin/ehviewer/PlatformTypes.kt | 7 ++++++ .../kotlin/moe/tarsin/ehviewer/KNIUtils.kt | 25 +++++++++++++++++++ .../moe/tarsin/ehviewer/PlatformTypes.kt | 5 ++++ .../kotlin/moe/tarsin/ehviewer/RewriteGif.kt | 8 +++--- .../kotlin/moe/tarsin/ehviewer/mmap.kt | 22 ++++++++++++++++ 9 files changed, 84 insertions(+), 15 deletions(-) create mode 100644 native/src/androidNativeArm32Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt create mode 100644 native/src/androidNativeArm64Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt create mode 100644 native/src/androidNativeX64Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt create mode 100644 native/src/androidNativeX86Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt create mode 100644 native/src/nativeMain/kotlin/moe/tarsin/ehviewer/KNIUtils.kt create mode 100644 native/src/nativeMain/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt create mode 100644 native/src/nativeMain/kotlin/moe/tarsin/ehviewer/mmap.kt diff --git a/app/src/main/cpp/fdutils.c b/app/src/main/cpp/fdutils.c index 641f35ada..b40ee5bf9 100644 --- a/app/src/main/cpp/fdutils.c +++ b/app/src/main/cpp/fdutils.c @@ -24,17 +24,6 @@ Java_com_hippo_Native_getFd(JNIEnv *env, jclass clazz, jobject fileDescriptor) { return fd; } -JNIEXPORT jobject JNICALL -Java_com_hippo_Native_mapFd(JNIEnv *env, jclass clazz, jint fd, jlong capability) { - void *addr = mmap(0, capability, PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0); - if (addr != MAP_FAILED) { - return (*env)->NewDirectByteBuffer(env, addr, capability); - } else { - LOGE("%s%s", "mmap failed with error ", strerror(errno)); - return NULL; - } -} - JNIEXPORT void JNICALL Java_com_hippo_Native_unmapDirectByteBuffer(JNIEnv *env, jclass clazz, jobject buffer) { void *addr = (*env)->GetDirectBufferAddress(env, buffer); diff --git a/native/src/androidNativeArm32Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt b/native/src/androidNativeArm32Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt new file mode 100644 index 000000000..f0d19844f --- /dev/null +++ b/native/src/androidNativeArm32Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt @@ -0,0 +1,7 @@ +package moe.tarsin.ehviewer + +import platform.posix.size_t + +actual fun Long.tosize_t(): size_t { + return toUInt() +} diff --git a/native/src/androidNativeArm64Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt b/native/src/androidNativeArm64Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt new file mode 100644 index 000000000..a627f3b21 --- /dev/null +++ b/native/src/androidNativeArm64Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt @@ -0,0 +1,7 @@ +package moe.tarsin.ehviewer + +import platform.posix.size_t + +actual fun Long.tosize_t(): size_t { + return toULong() +} diff --git a/native/src/androidNativeX64Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt b/native/src/androidNativeX64Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt new file mode 100644 index 000000000..a627f3b21 --- /dev/null +++ b/native/src/androidNativeX64Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt @@ -0,0 +1,7 @@ +package moe.tarsin.ehviewer + +import platform.posix.size_t + +actual fun Long.tosize_t(): size_t { + return toULong() +} diff --git a/native/src/androidNativeX86Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt b/native/src/androidNativeX86Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt new file mode 100644 index 000000000..f0d19844f --- /dev/null +++ b/native/src/androidNativeX86Main/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt @@ -0,0 +1,7 @@ +package moe.tarsin.ehviewer + +import platform.posix.size_t + +actual fun Long.tosize_t(): size_t { + return toUInt() +} diff --git a/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/KNIUtils.kt b/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/KNIUtils.kt new file mode 100644 index 000000000..dea930010 --- /dev/null +++ b/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/KNIUtils.kt @@ -0,0 +1,25 @@ +package moe.tarsin.ehviewer + +import kotlinx.cinterop.COpaquePointer +import kotlinx.cinterop.CPointer +import kotlinx.cinterop.invoke +import kotlinx.cinterop.pointed +import platform.android.JNIEnvVar +import platform.android.JNINativeInterface +import platform.android.jobject + +fun CPointer.getNativeInterface(): JNINativeInterface { + return pointed.pointed!! +} + +fun CPointer.getDirectBufferAddress(buffer: jobject): COpaquePointer { + return getNativeInterface().GetDirectBufferAddress!!.invoke(this, buffer)!! +} + +fun CPointer.getDirectBufferCapacity(buffer: jobject): Long { + return getNativeInterface().GetDirectBufferCapacity!!.invoke(this, buffer) +} + +fun CPointer.newDirectByteBuffer(addr: COpaquePointer, capability: Long): jobject { + return getNativeInterface().NewDirectByteBuffer!!.invoke(this, addr, capability)!! +} diff --git a/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt b/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt new file mode 100644 index 000000000..db57e9267 --- /dev/null +++ b/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/PlatformTypes.kt @@ -0,0 +1,5 @@ +package moe.tarsin.ehviewer + +import platform.posix.size_t + +expect fun Long.tosize_t(): size_t diff --git a/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/RewriteGif.kt b/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/RewriteGif.kt index dfc76278a..8772877f3 100644 --- a/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/RewriteGif.kt +++ b/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/RewriteGif.kt @@ -1,13 +1,13 @@ package moe.tarsin.ehviewer import kotlinx.cinterop.CPointer -import kotlinx.cinterop.invoke -import kotlinx.cinterop.pointed import platform.android.JNIEnvVar import platform.android.jclass import platform.android.jobject +@Suppress("UNUSED_PARAMETER") @CName("Java_com_hippo_Native_rewriteGif") -fun rewriteGif(env: CPointer, thiz: jclass, buffer: jobject) { - val ptr = env.pointed.pointed!!.GetDirectBufferAddress!!.invoke(env, buffer) +fun rewriteGif(env: CPointer, clazz: jclass, buffer: jobject) { + val ptr = env.getDirectBufferAddress(buffer) + val size = env.getDirectBufferCapacity(buffer) } diff --git a/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/mmap.kt b/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/mmap.kt new file mode 100644 index 000000000..8b0231245 --- /dev/null +++ b/native/src/nativeMain/kotlin/moe/tarsin/ehviewer/mmap.kt @@ -0,0 +1,22 @@ +package moe.tarsin.ehviewer + +import kotlinx.cinterop.CPointer +import kotlinx.cinterop.UnsafeNumber +import kotlinx.cinterop.value +import platform.android.JNIEnvVar +import platform.android.jclass +import platform.android.jint +import platform.android.jlong +import platform.android.jobject +import platform.posix.MAP_PRIVATE +import platform.posix.PROT_READ +import platform.posix.PROT_WRITE +import platform.posix.mmap + +@OptIn(UnsafeNumber::class) +@Suppress("UNUSED_PARAMETER") +@CName("Java_com_hippo_Native_mapFd") +fun mapFd(env: CPointer, clazz: jclass, fd: jint, capability: jlong): jobject { + val result = mmap(null, capability.tosize_t(), PROT_READ or PROT_WRITE, MAP_PRIVATE, fd, 0) + return env.newDirectByteBuffer(result!!, capability) +}