d/android: Fix errors while getting display metrics

This commit is contained in:
Jarvis Huang 2022-08-31 16:59:30 +08:00 committed by Ryan Pavlik
parent 52c790d1a9
commit 7be4163b91
6 changed files with 32 additions and 27 deletions

View file

@ -15,6 +15,7 @@
#include "util/u_logging.h" #include "util/u_logging.h"
#include "wrap/android.app.h" #include "wrap/android.app.h"
#include "wrap/android.content.h"
#include "wrap/android.view.h" #include "wrap/android.view.h"
#include "org.freedesktop.monado.auxiliary.hpp" #include "org.freedesktop.monado.auxiliary.hpp"
@ -22,6 +23,7 @@
using wrap::android::app::Activity; using wrap::android::app::Activity;
using wrap::android::content::Context;
using wrap::android::view::SurfaceHolder; using wrap::android::view::SurfaceHolder;
using wrap::org::freedesktop::monado::auxiliary::MonadoView; using wrap::org::freedesktop::monado::auxiliary::MonadoView;
using xrt::auxiliary::android::loadClassFromRuntimeApk; using xrt::auxiliary::android::loadClassFromRuntimeApk;
@ -140,12 +142,13 @@ android_custom_surface_wait_get_surface(struct android_custom_surface *custom_su
bool bool
android_custom_surface_get_display_metrics(struct _JavaVM *vm, android_custom_surface_get_display_metrics(struct _JavaVM *vm,
void *activity, void *context,
struct xrt_android_display_metrics *out_metrics) struct xrt_android_display_metrics *out_metrics)
{ {
jni::init(vm); jni::init(vm);
try { try {
auto clazz = loadClassFromRuntimeApk((jobject)activity, MonadoView::getFullyQualifiedTypeName()); auto clazz = loadClassFromRuntimeApk((jobject)context, MonadoView::getFullyQualifiedTypeName());
if (clazz.isNull()) { if (clazz.isNull()) {
U_LOG_E("Could not load class '%s' from package '%s'", MonadoView::getFullyQualifiedTypeName(), U_LOG_E("Could not load class '%s' from package '%s'", MonadoView::getFullyQualifiedTypeName(),
XRT_ANDROID_PACKAGE); XRT_ANDROID_PACKAGE);
@ -155,9 +158,9 @@ android_custom_surface_get_display_metrics(struct _JavaVM *vm,
// Teach the wrapper our class before we start to use it. // Teach the wrapper our class before we start to use it.
MonadoView::staticInitClass((jclass)clazz.object().getHandle()); MonadoView::staticInitClass((jclass)clazz.object().getHandle());
jni::Object displayMetrics = MonadoView::getDisplayMetrics(Activity((jobject)activity)); jni::Object displayMetrics = MonadoView::getDisplayMetrics(Context((jobject)context));
//! @todo implement non-deprecated codepath for api 30+ //! @todo implement non-deprecated codepath for api 30+
float displayRefreshRate = MonadoView::getDisplayRefreshRate(Activity((jobject)activity)); float displayRefreshRate = MonadoView::getDisplayRefreshRate(Context((jobject)context));
if (displayRefreshRate == 0.0) { if (displayRefreshRate == 0.0) {
displayRefreshRate = 60.0f; displayRefreshRate = 60.0f;
} }
@ -171,7 +174,6 @@ android_custom_surface_get_display_metrics(struct _JavaVM *vm,
.ydpi = displayMetrics.get<float>("scaledDensity"), .ydpi = displayMetrics.get<float>("scaledDensity"),
.refresh_rate = displayRefreshRate}; .refresh_rate = displayRefreshRate};
return true; return true;
} catch (std::exception const &e) { } catch (std::exception const &e) {
U_LOG_E("Could not get display metrics: %s", e.what()); U_LOG_E("Could not get display metrics: %s", e.what());
return false; return false;

View file

@ -19,8 +19,9 @@ namespace org::freedesktop::monado::auxiliary {
attachToActivity1(classRef().getStaticMethod( attachToActivity1(classRef().getStaticMethod(
"attachToActivity", "(Landroid/app/Activity;)Lorg/freedesktop/monado/auxiliary/MonadoView;")), "attachToActivity", "(Landroid/app/Activity;)Lorg/freedesktop/monado/auxiliary/MonadoView;")),
getDisplayMetrics(classRef().getStaticMethod("getDisplayMetrics", getDisplayMetrics(classRef().getStaticMethod("getDisplayMetrics",
"(Landroid/app/Activity;)Landroid/util/DisplayMetrics;")), "(Landroid/content/Context;)Landroid/util/DisplayMetrics;")),
getDisplayRefreshRate(classRef().getStaticMethod("getDisplayRefreshRate", "(Landroid/app/Activity;)F")), getDisplayRefreshRate(
classRef().getStaticMethod("getDisplayRefreshRate", "(Landroid/content/Context;)F")),
getNativePointer(classRef().getMethod("getNativePointer", "()J")), getNativePointer(classRef().getMethod("getNativePointer", "()J")),
markAsDiscardedByNative(classRef().getMethod("markAsDiscardedByNative", "()V")), markAsDiscardedByNative(classRef().getMethod("markAsDiscardedByNative", "()V")),
waitGetSurfaceHolder(classRef().getMethod("waitGetSurfaceHolder", "(I)Landroid/view/SurfaceHolder;")) waitGetSurfaceHolder(classRef().getMethod("waitGetSurfaceHolder", "(I)Landroid/view/SurfaceHolder;"))

View file

@ -17,6 +17,10 @@ namespace android::app {
class Activity; class Activity;
} // namespace android::app } // namespace android::app
namespace android::content {
class Context;
} // namespace android::content
namespace android::view { namespace android::view {
class SurfaceHolder; class SurfaceHolder;
} // namespace android::view } // namespace android::view
@ -78,25 +82,25 @@ namespace org::freedesktop::monado::auxiliary {
* Wrapper for the getDisplayMetrics static method * Wrapper for the getDisplayMetrics static method
* *
* Java prototype: * Java prototype:
* `public static android.util.DisplayMetrics getDisplayMetrics(android.app.Activity);` * `public static android.util.DisplayMetrics getDisplayMetrics(android.content.Context);`
* *
* JNI signature: (Landroid/app/Activity;)Landroid/util/DisplayMetrics; * JNI signature: (Landroid/content/Context;)Landroid/util/DisplayMetrics;
* *
*/ */
static jni::Object static jni::Object
getDisplayMetrics(android::app::Activity const &activity); getDisplayMetrics(android::content::Context const &context);
/*! /*!
* Wrapper for the getDisplayRefreshRate static method * Wrapper for the getDisplayRefreshRate static method
* *
* Java prototype: * Java prototype:
* `public static float getDisplayRefreshRate(android.app.Activity);` * `public static float getDisplayRefreshRate(android.content.Context);`
* *
* JNI signature: (Landroid/app/Activity;)F; * JNI signature: (Landroid/content/Context;)F;
* *
*/ */
static float static float
getDisplayRefreshRate(android::app::Activity const &activity); getDisplayRefreshRate(android::content::Context const &context);
/*! /*!
* Wrapper for the getNativePointer method * Wrapper for the getNativePointer method

View file

@ -11,6 +11,7 @@
#pragma once #pragma once
#include "wrap/android.app.h" #include "wrap/android.app.h"
#include "wrap/android.content.h"
#include "wrap/android.view.h" #include "wrap/android.view.h"
@ -32,15 +33,15 @@ namespace org::freedesktop::monado::auxiliary {
} }
inline jni::Object inline jni::Object
MonadoView::getDisplayMetrics(android::app::Activity const &activity) MonadoView::getDisplayMetrics(android::content::Context const &context)
{ {
return Meta::data().clazz().call<jni::Object>(Meta::data().getDisplayMetrics, activity.object()); return Meta::data().clazz().call<jni::Object>(Meta::data().getDisplayMetrics, context.object());
} }
inline float inline float
MonadoView::getDisplayRefreshRate(android::app::Activity const &activity) MonadoView::getDisplayRefreshRate(android::content::Context const &context)
{ {
return Meta::data().clazz().call<float>(Meta::data().getDisplayRefreshRate, activity.object()); return Meta::data().clazz().call<float>(Meta::data().getDisplayRefreshRate, context.object());
} }
inline void * inline void *

View file

@ -11,22 +11,17 @@ package org.freedesktop.monado.auxiliary;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.os.Build;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.util.Log; import android.util.Log;
import android.view.SurfaceHolder; import android.view.SurfaceHolder;
import android.view.SurfaceView; import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager; import android.view.WindowManager;
import androidx.annotation.GuardedBy; import androidx.annotation.GuardedBy;
import androidx.annotation.Keep; import androidx.annotation.Keep;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Calendar; import java.util.Calendar;
@Keep @Keep
@ -100,15 +95,17 @@ public class MonadoView extends SurfaceView implements SurfaceHolder.Callback, S
@NonNull @NonNull
@Keep @Keep
public static DisplayMetrics getDisplayMetrics(Activity activity) { public static DisplayMetrics getDisplayMetrics(@NonNull Context context) {
DisplayMetrics displayMetrics = new DisplayMetrics(); DisplayMetrics displayMetrics = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(displayMetrics);
return displayMetrics; return displayMetrics;
} }
@Keep @Keep
public static float getDisplayRefreshRate(Activity activity) { public static float getDisplayRefreshRate(@NonNull Context context) {
return activity.getWindowManager().getDefaultDisplay().getRefreshRate(); WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
return wm.getDefaultDisplay().getRefreshRate();
} }
@Keep @Keep

View file

@ -235,7 +235,7 @@ android_device_create()
} }
struct xrt_android_display_metrics metrics; struct xrt_android_display_metrics metrics;
if (!android_custom_surface_get_display_metrics(android_globals_get_vm(), android_globals_get_activity(), if (!android_custom_surface_get_display_metrics(android_globals_get_vm(), android_globals_get_context(),
&metrics)) { &metrics)) {
U_LOG_E("Could not get Android display metrics."); U_LOG_E("Could not get Android display metrics.");
/* Fallback to default values (Pixel 3) */ /* Fallback to default values (Pixel 3) */