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 "wrap/android.app.h"
#include "wrap/android.content.h"
#include "wrap/android.view.h"
#include "org.freedesktop.monado.auxiliary.hpp"
@ -22,6 +23,7 @@
using wrap::android::app::Activity;
using wrap::android::content::Context;
using wrap::android::view::SurfaceHolder;
using wrap::org::freedesktop::monado::auxiliary::MonadoView;
using xrt::auxiliary::android::loadClassFromRuntimeApk;
@ -140,12 +142,13 @@ android_custom_surface_wait_get_surface(struct android_custom_surface *custom_su
bool
android_custom_surface_get_display_metrics(struct _JavaVM *vm,
void *activity,
void *context,
struct xrt_android_display_metrics *out_metrics)
{
jni::init(vm);
try {
auto clazz = loadClassFromRuntimeApk((jobject)activity, MonadoView::getFullyQualifiedTypeName());
auto clazz = loadClassFromRuntimeApk((jobject)context, MonadoView::getFullyQualifiedTypeName());
if (clazz.isNull()) {
U_LOG_E("Could not load class '%s' from package '%s'", MonadoView::getFullyQualifiedTypeName(),
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.
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+
float displayRefreshRate = MonadoView::getDisplayRefreshRate(Activity((jobject)activity));
float displayRefreshRate = MonadoView::getDisplayRefreshRate(Context((jobject)context));
if (displayRefreshRate == 0.0) {
displayRefreshRate = 60.0f;
}
@ -171,7 +174,6 @@ android_custom_surface_get_display_metrics(struct _JavaVM *vm,
.ydpi = displayMetrics.get<float>("scaledDensity"),
.refresh_rate = displayRefreshRate};
return true;
} catch (std::exception const &e) {
U_LOG_E("Could not get display metrics: %s", e.what());
return false;

View file

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

View file

@ -17,6 +17,10 @@ namespace android::app {
class Activity;
} // namespace android::app
namespace android::content {
class Context;
} // namespace android::content
namespace android::view {
class SurfaceHolder;
} // namespace android::view
@ -78,25 +82,25 @@ namespace org::freedesktop::monado::auxiliary {
* Wrapper for the getDisplayMetrics static method
*
* 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
getDisplayMetrics(android::app::Activity const &activity);
getDisplayMetrics(android::content::Context const &context);
/*!
* Wrapper for the getDisplayRefreshRate static method
*
* 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
getDisplayRefreshRate(android::app::Activity const &activity);
getDisplayRefreshRate(android::content::Context const &context);
/*!
* Wrapper for the getNativePointer method

View file

@ -11,6 +11,7 @@
#pragma once
#include "wrap/android.app.h"
#include "wrap/android.content.h"
#include "wrap/android.view.h"
@ -32,15 +33,15 @@ namespace org::freedesktop::monado::auxiliary {
}
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
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 *

View file

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

View file

@ -235,7 +235,7 @@ android_device_create()
}
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)) {
U_LOG_E("Could not get Android display metrics.");
/* Fallback to default values (Pixel 3) */