Initial Android build support

This commit is contained in:
Syoyo Fujita
2021-08-02 22:10:01 +09:00
parent 63221af44e
commit 52dfbf04ed
9 changed files with 137 additions and 37 deletions

3
android/.idea/.gitignore generated vendored Normal file
View File

@@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

View File

@@ -13,6 +13,8 @@ arm64-v8a arch only.
Open this folder in Android Studio and build it.
When you open the folder in the first time, it will take time(~20 mins) and many file downloads(e.g. gradle) will happen.
### Command line build
T.B.W.

View File

@@ -17,17 +17,21 @@ set(TINYUSDZ_DEP_SOURCES
# Build the libhello-oboe library
add_library(hello-tinyusdz SHARED
hello-tinyusdz.cc
jni-tinyusdz.cc
${TINYUSDZ_SOURCES}
${TINYUSDZ_DEP_SOURCES}
)
target_link_libraries(hello-tinyusdz android log)
target_include_directories(hello-tinyusdz PRIVATE
${PROJECT_SOURCE_DIR}/../../../../../src/
${PROJECT_SOURCE_DIR}/../../../../../src/
${PROJECT_SOURCE_DIR}/../../../../../src/external/ryu
)
# Required to load .usd files from Android asset for demo purpose
# When you embed TinyUSDZ to your own app, you are better to load .usd files from a memory and turn this define off.
target_compile_definitions(hello-tinyusdz PRIVATE "TINYUSDZ_ANDROID_LOAD_FROM_ASSETS")
# Enable optimization flags: if having problems with source level debugging,
# disable -Ofast ( and debug ), re-enable after done debugging.
target_compile_options(hello-tinyusdz PRIVATE -Wall -Werror "$<$<CONFIG:RELEASE>:-Ofast>")

View File

@@ -1,18 +0,0 @@
#include <jni.h>
#include "tinyusdz.hh"
extern "C" {
/*
* Returns: 0 - success
* -1 - failed
*/
JNIEXPORT jint JNICALL
Java_com_example_hellotinyusdz_MainActivity_createStream(
JNIEnv * /* env */,
jobject /* this */) {
/* TODO: Implement */
return -1;
}
}

View File

@@ -0,0 +1,56 @@
#include <jni.h>
#include <android/log.h>
#include <android/asset_manager.h>
#include <android/asset_manager_jni.h>
#include "tinyusdz.hh"
// global
tinyusdz::Scene g_scene;
#ifndef TINYUSDZ_ANDROID_LOAD_FROM_ASSETS
#error "This demo requires to load .usd file from Android Assets"
#else
#endif
extern "C" {
/*
* Returns: 0 - success
* -1 - failed
*/
JNIEXPORT jint JNICALL
Java_com_example_hellotinyusdz_MainActivity_createStream(
JNIEnv *env,
jobject obj,
jobject assetManager) {
tinyusdz::asset_manager = AAssetManager_fromJava(env, assetManager);
tinyusdz::USDLoadOptions options;
// load from Android asset folder
std::string warn, err;
bool ret = LoadUSDCFromFile("suzanne.usdc", &g_scene, &warn, &err, options);
if (warn.size()) {
__android_log_print(ANDROID_LOG_WARN, "tinyusdz", "USD load warning: %s", warn.c_str());
}
if (!ret) {
if (err.size()) {
__android_log_print(ANDROID_LOG_ERROR, "tinyusdz", "USD load error: %s", err.c_str());
}
}
if (ret) {
__android_log_print(ANDROID_LOG_INFO, "tinyusdz", "USD loaded. #of geom_meshes: %d", int(g_scene.geom_meshes.size()));
return int(g_scene.geom_meshes.size());
}
// err
return -1;
}
}

View File

@@ -17,6 +17,7 @@
package com.example.hellotinyusdz
import android.content.res.AssetManager
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.MotionEvent
@@ -46,10 +47,18 @@ class MainActivity : AppCompatActivity() {
override fun onResume() {
super.onResume()
if (createStream() != 0) {
val errorString : String = "ERROR"
var n = createStream(getAssets());
if (n <= 0) {
val errorString : String = "Failed to load USD file"
Toast.makeText(applicationContext, errorString,Toast.LENGTH_LONG).show()
sample_text.text = errorString
} else {
val s : String = "Loaded USD. # of geom_meshes " + n
Toast.makeText(applicationContext, s,Toast.LENGTH_LONG).show()
sample_text.text = s
}
}
@@ -59,7 +68,7 @@ class MainActivity : AppCompatActivity() {
// Creates and starts Oboe stream to play audio
private external fun createStream() : Int
private external fun createStream(mgr: AssetManager) : Int
companion object {
// Used to load native code calling oboe on app startup.