2020-09-08 20:09:07 +00:00
|
|
|
// Copyright 2020, Collabora, Ltd.
|
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
2020-10-28 16:29:15 +00:00
|
|
|
/*!
|
|
|
|
* @file
|
|
|
|
* @brief Implementation of native code for Android custom surface.
|
|
|
|
* @author Ryan Pavlik <ryan.pavlik@collabora.com>
|
|
|
|
* @author Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
|
|
|
|
* @ingroup aux_android
|
|
|
|
*/
|
2020-09-08 20:09:07 +00:00
|
|
|
|
|
|
|
#include "android_custom_surface.h"
|
2020-10-28 16:30:44 +00:00
|
|
|
#include "android_load_class.hpp"
|
2020-09-08 20:09:07 +00:00
|
|
|
|
|
|
|
#include "xrt/xrt_config_android.h"
|
|
|
|
#include "util/u_logging.h"
|
|
|
|
|
|
|
|
#include "wrap/android.app.h"
|
|
|
|
#include "wrap/android.view.h"
|
|
|
|
|
|
|
|
#include <android/native_window_jni.h>
|
|
|
|
|
|
|
|
|
|
|
|
using wrap::android::app::Activity;
|
|
|
|
using wrap::android::view::SurfaceHolder;
|
|
|
|
|
2020-09-25 17:59:08 +00:00
|
|
|
|
2020-09-08 20:09:07 +00:00
|
|
|
struct android_custom_surface
|
|
|
|
{
|
2020-10-28 16:33:30 +00:00
|
|
|
explicit android_custom_surface(jobject act);
|
2020-10-28 15:24:28 +00:00
|
|
|
~android_custom_surface();
|
|
|
|
Activity activity{};
|
|
|
|
jni::Class monadoViewClass{};
|
|
|
|
jni::Object monadoView{};
|
|
|
|
jni::method_t waitGetSurfaceHolderMethod = nullptr;
|
|
|
|
jni::method_t markAsDiscardedMethod = nullptr;
|
2020-09-08 20:09:07 +00:00
|
|
|
};
|
|
|
|
|
2020-10-28 16:33:30 +00:00
|
|
|
|
|
|
|
android_custom_surface::android_custom_surface(jobject act) : activity(act) {}
|
|
|
|
|
2020-10-28 15:24:28 +00:00
|
|
|
android_custom_surface::~android_custom_surface()
|
|
|
|
{
|
2020-10-28 16:33:30 +00:00
|
|
|
// Tell Java that native code is done with this.
|
2020-10-28 15:24:28 +00:00
|
|
|
if (!monadoView.isNull() && markAsDiscardedMethod != nullptr) {
|
|
|
|
try {
|
|
|
|
monadoView.call<void>(markAsDiscardedMethod);
|
|
|
|
} catch (std::exception const &e) {
|
|
|
|
U_LOG_E(
|
|
|
|
"Failure while marking MonadoView as discarded: %s",
|
|
|
|
e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-28 16:49:56 +00:00
|
|
|
constexpr auto FULLY_QUALIFIED_CLASSNAME =
|
|
|
|
"org.freedesktop.monado.auxiliary.MonadoView";
|
|
|
|
|
2020-09-08 20:09:07 +00:00
|
|
|
struct android_custom_surface *
|
|
|
|
android_custom_surface_async_start(struct _JavaVM *vm, void *activity)
|
|
|
|
{
|
|
|
|
jni::init(vm);
|
2020-09-25 17:59:08 +00:00
|
|
|
jni::method_t attachToActivity;
|
2020-09-08 20:09:07 +00:00
|
|
|
try {
|
|
|
|
std::unique_ptr<android_custom_surface> ret =
|
2020-10-28 16:33:30 +00:00
|
|
|
std::make_unique<android_custom_surface>((jobject)activity);
|
2020-09-25 17:59:08 +00:00
|
|
|
auto info = getAppInfo(XRT_ANDROID_PACKAGE, (jobject)activity);
|
|
|
|
if (info.isNull()) {
|
|
|
|
U_LOG_E(
|
|
|
|
"Could not get application info for package '%s'",
|
|
|
|
"org.freedesktop.monado.openxr_runtime");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-10-28 16:49:56 +00:00
|
|
|
auto clazz = loadClassFromPackage(info, (jobject)activity,
|
|
|
|
FULLY_QUALIFIED_CLASSNAME);
|
2020-09-25 17:59:08 +00:00
|
|
|
|
|
|
|
if (clazz.isNull()) {
|
|
|
|
U_LOG_E("Could not load class '%s' from package '%s'",
|
2020-10-28 16:49:56 +00:00
|
|
|
FULLY_QUALIFIED_CLASSNAME, XRT_ANDROID_PACKAGE);
|
2020-09-25 17:59:08 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2020-09-08 20:09:07 +00:00
|
|
|
|
2020-10-26 23:22:57 +00:00
|
|
|
// the 0 is to avoid this being considered "temporary" and to
|
|
|
|
// create a global ref.
|
2020-09-08 20:09:07 +00:00
|
|
|
ret->monadoViewClass =
|
2020-10-26 23:22:57 +00:00
|
|
|
jni::Class((jclass)clazz.object().getHandle(), 0);
|
2020-09-25 17:59:08 +00:00
|
|
|
|
|
|
|
if (ret->monadoViewClass.isNull()) {
|
|
|
|
U_LOG_E("monadoViewClass was null");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string clazz_name = ret->monadoViewClass.getName();
|
2020-10-28 16:49:56 +00:00
|
|
|
if (clazz_name != FULLY_QUALIFIED_CLASSNAME) {
|
2020-09-25 17:59:08 +00:00
|
|
|
U_LOG_E("Unexpected class name: %s",
|
|
|
|
clazz_name.c_str());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-09-08 20:09:07 +00:00
|
|
|
if (ret->monadoViewClass.isNull()) {
|
2020-09-25 17:59:08 +00:00
|
|
|
U_LOG_E("MonadoView was null.");
|
2020-09-08 20:09:07 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2020-10-26 23:22:57 +00:00
|
|
|
ret->waitGetSurfaceHolderMethod =
|
|
|
|
ret->monadoViewClass.getMethod(
|
|
|
|
"waitGetSurfaceHolder",
|
|
|
|
"(I)Landroid/view/SurfaceHolder;");
|
2020-10-28 15:24:28 +00:00
|
|
|
ret->markAsDiscardedMethod = ret->monadoViewClass.getMethod(
|
|
|
|
"markAsDiscardedByNative", "()V;");
|
2020-10-26 23:22:57 +00:00
|
|
|
|
2020-09-25 17:59:08 +00:00
|
|
|
attachToActivity = ret->monadoViewClass.getStaticMethod(
|
|
|
|
"attachToActivity",
|
|
|
|
"(Landroid/app/Activity;)Lorg/freedesktop/monado/auxiliary/"
|
|
|
|
"MonadoView;");
|
2020-10-20 21:11:37 +00:00
|
|
|
|
2020-09-08 20:09:07 +00:00
|
|
|
ret->monadoView = ret->monadoViewClass.call<jni::Object>(
|
|
|
|
attachToActivity, ret->activity.object());
|
|
|
|
return ret.release();
|
|
|
|
} catch (std::exception const &e) {
|
|
|
|
|
|
|
|
U_LOG_E(
|
|
|
|
"Could not start attaching our custom surface to activity: "
|
|
|
|
"%s",
|
|
|
|
e.what());
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
android_custom_surface_destroy(
|
|
|
|
struct android_custom_surface **ptr_custom_surface)
|
|
|
|
{
|
|
|
|
if (ptr_custom_surface == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
struct android_custom_surface *custom_surface = *ptr_custom_surface;
|
|
|
|
if (custom_surface == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
delete custom_surface;
|
|
|
|
*ptr_custom_surface = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ANativeWindow *
|
2020-10-26 23:22:57 +00:00
|
|
|
android_custom_surface_wait_get_surface(
|
|
|
|
struct android_custom_surface *custom_surface, uint64_t timeout_ms)
|
2020-09-08 20:09:07 +00:00
|
|
|
{
|
2020-10-20 21:11:37 +00:00
|
|
|
jni::Object holder = nullptr;
|
|
|
|
try {
|
2020-10-26 23:22:57 +00:00
|
|
|
|
|
|
|
holder = custom_surface->monadoView.call<jni::Object>(
|
|
|
|
custom_surface->waitGetSurfaceHolderMethod,
|
|
|
|
(int)timeout_ms);
|
|
|
|
|
2020-10-20 21:11:37 +00:00
|
|
|
} catch (std::exception const &e) {
|
2020-10-26 23:22:57 +00:00
|
|
|
// do nothing right now.
|
|
|
|
U_LOG_E(
|
|
|
|
"Could not wait for our custom surface: "
|
|
|
|
"%s",
|
|
|
|
e.what());
|
2020-09-08 20:09:07 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2020-10-20 21:11:37 +00:00
|
|
|
|
2020-09-08 20:09:07 +00:00
|
|
|
SurfaceHolder surfaceHolder(holder);
|
2020-10-26 23:22:57 +00:00
|
|
|
if (surfaceHolder.isNull()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2020-09-08 20:09:07 +00:00
|
|
|
auto surf = surfaceHolder.getSurface();
|
|
|
|
if (surf.isNull()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return ANativeWindow_fromSurface(jni::env(), surf.object().getHandle());
|
|
|
|
}
|