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.

View File

@@ -8,6 +8,7 @@ project(${BUILD_TARGET} CXX)
option(TINYUSDZ_USE_CCACHE "Use ccache for faster recompile." ON)
option(TINYUSDZ_WITH_OPENSUBDIV "Build with OpenSubdiv(osdCPU. if required, set `osd_DIR` to specify the path to your own OpenSubdiv)" ON)
option(USDVIEW_USE_NATIVEFILEDIALOG "Use nativefiledialog. Requires gtk+-3 libs on linux to build" ON)
option(USDVIEW_ENABLE_PHYSICS "Enable Physics" OFF)
if (EMSCRIPTEN)
# Disable nfd
@@ -31,6 +32,10 @@ if (USDVIEW_USE_NATIVEFILEDIALOG)
add_subdirectory(${PROJECT_SOURCE_DIR}/../common/nativefiledialog-extended nfd_build)
endif()
if (USDVIEW_ENABLE_PHYSICS)
add_subdirectory(${PROJECT_SOURCE_DIR}/../common/bullet3 bullet3_build)
endif()
# [ccache]
if(TINYUSDZ_USE_CCACHE)
if(MSVC)

View File

@@ -39,12 +39,6 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <unordered_set>
#include <vector>
#ifdef __ANDROID__
#ifdef TINYUSDZ_ANDROID_LOAD_FROM_ASSETS
#include <android/asset_manager.h>
#endif
#endif
#ifdef _WIN32
#ifndef NOMINMAX
@@ -139,14 +133,11 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma clang diagnostic pop
#endif
namespace tinyusdz {
#ifdef __ANDROID__
#ifdef TINYUSDZ_ANDROID_LOAD_FROM_ASSETS
AAssetManager *asset_manager = nullptr;
#endif
#endif
namespace tinyusdz {
namespace {
@@ -225,12 +216,12 @@ bool ReadWholeFile(std::vector<uint8_t> *out, std::string *err,
(void)userdata;
#ifdef TINYUSDZ_ANDROID_LOAD_FROM_ASSETS
if (asset_manager) {
if (tinyusdz::asset_manager) {
AAsset *asset = AAssetManager_open(asset_manager, filepath.c_str(),
AASSET_MODE_STREAMING);
if (!asset) {
if (err) {
(*err) += "File open error : " + filepath + "\n";
(*err) += "File open error(from AssestManager) : " + filepath + "\n";
}
return false;
}
@@ -5485,9 +5476,28 @@ bool LoadUSDCFromMemory(const uint8_t *addr, const size_t length, Scene *scene,
return true;
}
bool LoadUSDCFromFile(const std::string &filename, Scene *scene,
bool LoadUSDCFromFile(const std::string &_filename, Scene *scene,
std::string *warn, std::string *err,
const USDLoadOptions &options) {
std::string filepath = ExpandFilePath(_filename, /* userdata */nullptr);
std::vector<uint8_t> data;
size_t max_bytes = size_t(1024 * 1024 * options.max_memory_limit_in_mb);
if (!ReadWholeFile(&data, err, filepath, max_bytes, /* userdata */nullptr)) {
return false;
}
if (data.size() < (11 * 8)) {
// ???
if (err) {
(*err) +=
"File size too short. Looks like this file is not a USDC : \"" +
filepath + "\"\n";
}
return false;
}
#if 0
std::vector<uint8_t> data;
{
std::ifstream ifs(filename.c_str(), std::ifstream::binary);
@@ -5535,6 +5545,7 @@ bool LoadUSDCFromFile(const std::string &filename, Scene *scene,
ifs.read(reinterpret_cast<char *>(&data.at(0)),
static_cast<std::streamsize>(sz));
}
#endif
return LoadUSDCFromMemory(data.data(), data.size(), scene, warn, err,
options);
@@ -5560,12 +5571,31 @@ static std::string str_tolower(std::string s) {
} // namespace
bool LoadUSDZFromFile(const std::string &filename, Scene *scene,
bool LoadUSDZFromFile(const std::string &_filename, Scene *scene,
std::string *warn, std::string *err,
const USDLoadOptions &options) {
// <filename, byte_begin, byte_end>
std::vector<std::tuple<std::string, size_t, size_t>> assets;
std::string filepath = ExpandFilePath(_filename, /* userdata */nullptr);
std::vector<uint8_t> data;
size_t max_bytes = size_t(1024 * 1024 * options.max_memory_limit_in_mb);
if (!ReadWholeFile(&data, err, filepath, max_bytes, /* userdata */nullptr)) {
return false;
}
if (data.size() < (11 * 8) + 30) { // 88 for USDC header, 30 for ZIP header
// ???
if (err) {
(*err) +=
"File size too short. Looks like this file is not a USDZ : \"" +
filepath + "\"\n";
}
return false;
}
#if 0
std::vector<uint8_t> data;
{
std::ifstream ifs(filename.c_str(), std::ifstream::binary);
@@ -5603,6 +5633,7 @@ bool LoadUSDZFromFile(const std::string &filename, Scene *scene,
ifs.read(reinterpret_cast<char *>(&data.at(0)),
static_cast<std::streamsize>(sz));
}
#endif
size_t offset = 0;
while ((offset + 30) < data.size()) {

View File

@@ -37,6 +37,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <vector>
#include <cmath>
#ifdef TINYUSDZ_ANDROID_LOAD_FROM_ASSETS
#include <android/asset_manager.h>
#endif
#ifdef TINYUSDZ_LOCAL_DEBUG_PRINT
#include <iostream> // dbg
#endif
@@ -58,6 +62,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
namespace tinyusdz {
#ifdef TINYUSDZ_ANDROID_LOAD_FROM_ASSETS
extern AAssetManager *asset_manager;
#endif
constexpr int version_major = 0;
constexpr int version_minor = 7;
constexpr int version_micro = 0;