Fully functional JNI UI based app

This commit is contained in:
Alejandro Saucedo 2020-10-03 19:27:54 +01:00
parent a73f82e191
commit 652e010895
6 changed files with 89 additions and 87 deletions

View file

@ -2,17 +2,17 @@ apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
Properties properties = new Properties()
properties.load(project.rootProject.file('local.properties').newDataInputStream())
def ndkDir = properties.getProperty('ndk.dir')
def valLayerBinDir = "${ndkDir}/sources/third_party/vulkan/src/build-android/jniLibs"
//Properties properties = new Properties()
//properties.load(project.rootProject.file('local.properties').newDataInputStream())
//def ndkDir = properties.getProperty('ndk.dir')
//def valLayerBinDir = "${ndkDir}/sources/third_party/vulkan/src/build-android/jniLibs"
android {
compileSdkVersion 29
ndkVersion '21.2.6472646'
defaultConfig {
applicationId "com.ethicalml.kompute.examples.android"
applicationId "com.ethicalml.kompute"
minSdkVersion 26
targetSdkVersion 29
versionCode = 1
@ -46,14 +46,14 @@ android {
}
}
sourceSets {
main {
jniLibs {
// Must have ndk-r12 or better which including layer binaries
srcDirs = ["${valLayerBinDir}"]
}
}
}
// sourceSets {
// main {
// jniLibs {
// // Must have ndk-r12 or better which including layer binaries
// srcDirs = ["${valLayerBinDir}"]
// }
// }
// }
flavorDimensions 'cpuArch'
productFlavors {
@ -81,5 +81,5 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation fileTree(dir: 'libs', include: ['*.jar'])
// implementation fileTree(dir: 'libs', include: ['*.jar'])
}

View file

@ -1,9 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- BEGIN_INCLUDE(manifest) -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ethicalml.kompute.examples.android"
android:versionCode="1"
android:versionName="1.0">
package="com.ethicalml.komputejni">
<!-- <application android:label="@string/app_name"-->
<!-- android:theme="@android:style/Theme.NoTitleBar.Fullscreen">-->

View file

@ -12,8 +12,8 @@ set(VK_ANDROID_INCLUDE_DIR ${ANDROID_NDK}/sources/third_party/vulkan/src/include
# ${ANDROID_APP_GLUE_DIR}/android_native_app_glue.c)
# build vulkan app
add_library(kompute_android SHARED
KomputeJniNative.cpp)
add_library(kompute-jni SHARED
kompute-jni.cpp)
include_directories(
${VK_ANDROID_COMMON_DIR}
@ -24,10 +24,10 @@ include_directories(
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 \
-DVK_USE_PLATFORM_ANDROID_KHR=1 -DKOMPUTE_DISABLE_VK_DEBUG_LAYERS=1")
set(CMAKE_SHARED_LINKER_FLAGS
"${CMAKE_SHARED_LINKER_FLAGS} -u ANativeActivity_onCreate")
#set(CMAKE_SHARED_LINKER_FLAGS
# "${CMAKE_SHARED_LINKER_FLAGS} -u ANativeActivity_onCreate")
target_link_libraries(kompute_android
target_link_libraries(kompute-jni
kompute
# app-glue
log

View file

@ -12,30 +12,30 @@
// See the License for the specific language governing permissions and
// limitations under the License.
//#undef DEBUG
//#define RELEASE 1
#undef DEBUG
#define RELEASE 1
#include <android/log.h>
//#include <android_native_app_glue.h>
//#include <cassert>
//#include <memory>
//#include <vector>
//#include <unistd.h>
#include <unistd.h>
#include <string.h>
#include <jni.h>
//#include "kompute/Kompute.hpp"
#include "kompute/Kompute.hpp"
// Functions interacting with Android native activity
void android_main(struct android_app* state);
void terminate(void);
void handle_cmd(android_app* app, int32_t cmd);
//void android_main(struct android_app* state);
//void terminate(void);
//void handle_cmd(android_app* app, int32_t cmd);
// Android log function wrappers
static const char* kTAG = "Vulkan-Tutorial01";
static const char* kTAG = "KomputeJni";
#define LOGI(...) \
((void)__android_log_print(ANDROID_LOG_INFO, kTAG, __VA_ARGS__))
#define LOGW(...) \
@ -43,27 +43,27 @@ static const char* kTAG = "Vulkan-Tutorial01";
#define LOGE(...) \
((void)__android_log_print(ANDROID_LOG_ERROR, kTAG, __VA_ARGS__))
extern "C" {
JNIEXPORT jstring JNICALL
Java_com_ethicalml_kompute_examples_android_KomputeJni_stringFromJNI(JNIEnv *env, jobject thiz) {
Java_com_ethicalml_komputejni_KomputeJni_stringFromJNI(JNIEnv *env, jobject thiz) {
#if defined(__arm__)
#if defined(__ARM_ARCH_7A__)
#if defined(__ARM_NEON__)
#if defined(__ARM_PCS_VFP)
#define ABI "armeabi-v7a/NEON (hard-float)"
#else
#define ABI "armeabi-v7a/NEON"
#endif
#else
#if defined(__ARM_PCS_VFP)
#define ABI "armeabi-v7a (hard-float)"
#else
#define ABI "armeabi-v7a"
#endif
#endif
#else
#define ABI "armeabi"
#endif
#if defined(__ARM_ARCH_7A__)
#if defined(__ARM_NEON__)
#if defined(__ARM_PCS_VFP)
#define ABI "armeabi-v7a/NEON (hard-float)"
#else
#define ABI "armeabi-v7a/NEON"
#endif
#else
#if defined(__ARM_PCS_VFP)
#define ABI "armeabi-v7a (hard-float)"
#else
#define ABI "armeabi-v7a"
#endif
#endif
#else
#define ABI "armeabi"
#endif
#elif defined(__i386__)
#define ABI "x86"
#elif defined(__x86_64__)
@ -80,32 +80,36 @@ Java_com_ethicalml_kompute_examples_android_KomputeJni_stringFromJNI(JNIEnv *env
LOGI("Initialising vulkan");
// if (!InitVulkan()) {
// LOGE("Vulkan is unavailable, install vulkan and re-start");
// return false;
// }
//
// LOGI("Creating manager");
//
// kp::Manager mgr;
//
// auto tensorA = mgr.buildTensor({0,1,2});
// auto tensorB = mgr.buildTensor({0,1,2});
// auto tensorC = mgr.buildTensor({1,2,3});
//
// LOGI("Result before:");
// for(const float & i : tensorC->data()) {
// LOGI("%f ", i);
// }
//
// mgr.evalOpDefault<kp::OpMult<>>({tensorA, tensorB, tensorC});
// mgr.evalOpDefault<kp::OpTensorSyncLocal>({tensorC});
//
// LOGI("Result after:");
// for(const float & i : tensorC->data()) {
// LOGI("%f ", i);
// }
// TODO: This optionally to be initialized from Kompute
if(!InitVulkan()) {
sleep(1);
if (!InitVulkan()) {
LOGE("Vulkan is unavailable, install vulkan and re-start");
return (*env).NewStringUTF("ERROR");
}
}
return (*env).NewStringUTF("Hello from JNI ! Compiled with ABI ");
LOGI("Creating manager");
kp::Manager mgr;
auto tensorA = mgr.buildTensor({0,1,2});
auto tensorB = mgr.buildTensor({0,1,2});
auto tensorC = mgr.buildTensor({1,2,3});
LOGI("Result before:");
for(const float & i : tensorC->data()) {
LOGI("%f ", i);
}
mgr.evalOpDefault<kp::OpMult<>>({tensorA, tensorB, tensorC});
mgr.evalOpDefault<kp::OpTensorSyncLocal>({tensorC});
LOGI("Result after:");
for(const float & i : tensorC->data()) {
LOGI("%f ", i);
}
return (*env).NewStringUTF("SUCCESS");
}
}

View file

@ -13,11 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ethicalml.kompute.examples.android
package com.ethicalml.komputejni
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.ethicalml.kompute.examples.android.databinding.ActivityHelloJniBinding
import com.ethicalml.komputejni.databinding.ActivityKomputeJniBinding
class KomputeJni : AppCompatActivity() {
@ -28,9 +28,9 @@ class KomputeJni : AppCompatActivity() {
* the text is retrieved by calling a native
* function.
*/
val binding = ActivityHelloJniBinding.inflate(layoutInflater)
val binding = ActivityKomputeJniBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.helloTextview.text = stringFromJNI()
binding.komputeJniTextview.text = stringFromJNI()
}
/*
@ -61,7 +61,7 @@ class KomputeJni : AppCompatActivity() {
* at the installation time by the package manager.
*/
init {
System.loadLibrary("kompute_android")
System.loadLibrary("kompute-jni")
}
}
}

View file

@ -2,19 +2,19 @@
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_hello_jni"
android:id="@+id/activity_kompute_jni"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.hellojni.HelloJni">
<TextView
android:id="@+id/hello_textview"
android:id="@+id/kompute_jni_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="@+id/activity_hello_jni"
app:layout_constraintLeft_toLeftOf="@+id/activity_hello_jni"
app:layout_constraintRight_toRightOf="@+id/activity_hello_jni"
app:layout_constraintTop_toTopOf="@+id/activity_hello_jni" />
app:layout_constraintBottom_toBottomOf="@+id/activity_kompute_jni"
app:layout_constraintLeft_toLeftOf="@+id/activity_kompute_jni"
app:layout_constraintRight_toRightOf="@+id/activity_kompute_jni"
app:layout_constraintTop_toTopOf="@+id/activity_kompute_jni" />
</androidx.constraintlayout.widget.ConstraintLayout>