mirror of
https://gitlab.freedesktop.org/monado/monado.git
synced 2025-03-03 21:26:36 +00:00
aux/android: Implement display metrics acquisition.
This commit is contained in:
parent
ae76b5037d
commit
c0ab34a355
src/xrt/auxiliary/android
android_custom_surface.cppandroid_custom_surface.horg.freedesktop.monado.auxiliary.cpporg.freedesktop.monado.auxiliary.hpporg.freedesktop.monado.auxiliary.impl.hpp
src/main/java/org/freedesktop/monado/auxiliary
|
@ -157,3 +157,50 @@ android_custom_surface_wait_get_surface(
|
|||
return ANativeWindow_fromSurface(jni::env(),
|
||||
surf.object().makeLocalReference());
|
||||
}
|
||||
|
||||
bool
|
||||
android_custom_surface_get_display_metrics(
|
||||
struct _JavaVM *vm,
|
||||
void *activity,
|
||||
struct xrt_android_display_metrics *out_metrics)
|
||||
{
|
||||
jni::init(vm);
|
||||
try {
|
||||
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 false;
|
||||
}
|
||||
|
||||
auto clazz = loadClassFromPackage(info, (jobject)activity,
|
||||
FULLY_QUALIFIED_CLASSNAME);
|
||||
|
||||
if (clazz.isNull()) {
|
||||
U_LOG_E("Could not load class '%s' from package '%s'",
|
||||
FULLY_QUALIFIED_CLASSNAME, XRT_ANDROID_PACKAGE);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Teach the wrapper our class before we start to use it.
|
||||
MonadoView::staticInitClass((jclass)clazz.object().getHandle());
|
||||
|
||||
jni::Object displayMetrics =
|
||||
MonadoView::getDisplayMetrics(Activity((jobject)activity));
|
||||
|
||||
*out_metrics = {
|
||||
.width_pixels = displayMetrics.get<int>("widthPixels"),
|
||||
.height_pixels = displayMetrics.get<int>("heightPixels"),
|
||||
.density_dpi = displayMetrics.get<int>("densityDpi"),
|
||||
.density = displayMetrics.get<float>("xdpi"),
|
||||
.scaled_density = displayMetrics.get<float>("ydpi"),
|
||||
.xdpi = displayMetrics.get<float>("density"),
|
||||
.ydpi = displayMetrics.get<float>("scaledDensity")};
|
||||
return true;
|
||||
|
||||
} catch (std::exception const &e) {
|
||||
U_LOG_E("Could not get display metrics: %s", e.what());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,6 +23,17 @@ extern "C" {
|
|||
struct _JNIEnv;
|
||||
struct _JavaVM;
|
||||
|
||||
struct xrt_android_display_metrics
|
||||
{
|
||||
int width_pixels;
|
||||
int height_pixels;
|
||||
int density_dpi;
|
||||
float density;
|
||||
float scaled_density;
|
||||
float xdpi;
|
||||
float ydpi;
|
||||
};
|
||||
|
||||
/*!
|
||||
* Opaque type representing a custom surface added to an activity, and the async
|
||||
* operation to perform this adding.
|
||||
|
@ -77,6 +88,13 @@ android_custom_surface_destroy(
|
|||
ANativeWindow *
|
||||
android_custom_surface_wait_get_surface(
|
||||
struct android_custom_surface *custom_surface, uint64_t timeout_ms);
|
||||
|
||||
bool
|
||||
android_custom_surface_get_display_metrics(
|
||||
struct _JavaVM *vm,
|
||||
void *activity,
|
||||
struct xrt_android_display_metrics *out_metrics);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -21,7 +21,10 @@ namespace org::freedesktop::monado::auxiliary {
|
|||
waitGetSurfaceHolder(classRef().getMethod(
|
||||
"waitGetSurfaceHolder", "(I)Landroid/view/SurfaceHolder;")),
|
||||
markAsDiscardedByNative(
|
||||
classRef().getMethod("markAsDiscardedByNative", "()V"))
|
||||
classRef().getMethod("markAsDiscardedByNative", "()V")),
|
||||
getDisplayMetrics(classRef().getStaticMethod(
|
||||
"getDisplayMetrics",
|
||||
"(Landroid/app/Activity;)Landroid/util/DisplayMetrics;"))
|
||||
{}
|
||||
} // namespace org::freedesktop::monado::auxiliary
|
||||
} // namespace wrap
|
||||
|
|
|
@ -83,6 +83,9 @@ namespace org::freedesktop::monado::auxiliary {
|
|||
markAsDiscardedByNative();
|
||||
|
||||
|
||||
static jni::Object
|
||||
getDisplayMetrics(android::app::Activity const &activity);
|
||||
|
||||
/*!
|
||||
* Initialize the static metadata of this wrapper with a known
|
||||
* (non-null) Java class.
|
||||
|
@ -101,6 +104,7 @@ namespace org::freedesktop::monado::auxiliary {
|
|||
jni::method_t attachToActivity;
|
||||
jni::method_t waitGetSurfaceHolder;
|
||||
jni::method_t markAsDiscardedByNative;
|
||||
jni::method_t getDisplayMetrics;
|
||||
|
||||
/*!
|
||||
* Singleton accessor
|
||||
|
|
|
@ -40,5 +40,13 @@ namespace org::freedesktop::monado::auxiliary {
|
|||
return object().call<void>(
|
||||
Meta::data().markAsDiscardedByNative);
|
||||
}
|
||||
|
||||
inline jni::Object
|
||||
MonadoView::getDisplayMetrics(android::app::Activity const &activity)
|
||||
{
|
||||
return Meta::data().clazz().call<jni::Object>(
|
||||
Meta::data().getDisplayMetrics, activity.object());
|
||||
}
|
||||
|
||||
} // namespace org::freedesktop::monado::auxiliary
|
||||
} // namespace wrap
|
||||
|
|
|
@ -11,6 +11,7 @@ package org.freedesktop.monado.auxiliary;
|
|||
|
||||
import android.app.Activity;
|
||||
import android.os.Build;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.util.Log;
|
||||
import android.view.SurfaceHolder;
|
||||
import android.view.SurfaceView;
|
||||
|
@ -233,4 +234,12 @@ public class MonadoView extends SurfaceView implements SurfaceHolder.Callback, S
|
|||
Log.i(TAG, "surfaceRedrawNeeded");
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Keep
|
||||
public static DisplayMetrics getDisplayMetrics(Activity activity) {
|
||||
DisplayMetrics displayMetrics = new DisplayMetrics();
|
||||
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
|
||||
return displayMetrics;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue