a/android: Add supported display refresh rates in xrt_android_display_metrics

Part-of: <https://gitlab.freedesktop.org/monado/monado/-/merge_requests/1786>
This commit is contained in:
Zhongwang Zhang 2023-04-18 14:26:29 +08:00 committed by Rylie Pavlik
parent bb2b9e64c2
commit 4029233069
6 changed files with 57 additions and 0 deletions

View file

@ -218,6 +218,8 @@ android_custom_surface_get_display_metrics(struct _JavaVM *vm,
U_LOG_W("Could not get refresh rate, returning 60hz");
displayRefreshRate = 60.0f;
}
std::vector<float> supported_refresh_rates =
MonadoView::getSupportedRefreshRates(Context((jobject)context));
struct xrt_android_display_metrics metrics = {
.width_pixels = displayMetrics.get<int>("widthPixels"),
@ -228,7 +230,11 @@ android_custom_surface_get_display_metrics(struct _JavaVM *vm,
.xdpi = displayMetrics.get<float>("density"),
.ydpi = displayMetrics.get<float>("scaledDensity"),
.refresh_rate = displayRefreshRate,
.refresh_rate_count = (uint32_t)supported_refresh_rates.size(),
};
for (int i = 0; i < (int)metrics.refresh_rate_count; ++i) {
metrics.refresh_rates[i] = supported_refresh_rates[i];
}
*out_metrics = metrics;

View file

@ -11,6 +11,7 @@
#pragma once
#include <xrt/xrt_config_os.h>
#include <xrt/xrt_limits.h>
#ifdef XRT_OS_ANDROID
@ -33,6 +34,8 @@ struct xrt_android_display_metrics
float xdpi;
float ydpi;
float refresh_rate;
float refresh_rates[XRT_MAX_SUPPORTED_REFRESH_RATES];
uint32_t refresh_rate_count;
};
/*!

View file

@ -24,6 +24,8 @@ namespace org::freedesktop::monado::auxiliary {
"(Landroid/content/Context;)Landroid/util/DisplayMetrics;")),
getDisplayRefreshRate(
classRef().getStaticMethod("getDisplayRefreshRate", "(Landroid/content/Context;)F")),
getSupportedRefreshRates(
classRef().getStaticMethod("getSupportedRefreshRates", "(Landroid/content/Context;)[F")),
getNativePointer(classRef().getMethod("getNativePointer", "()J")),
markAsDiscardedByNative(classRef().getMethod("markAsDiscardedByNative", "()V")),
waitGetSurfaceHolder(classRef().getMethod("waitGetSurfaceHolder", "(I)Landroid/view/SurfaceHolder;")),

View file

@ -137,6 +137,18 @@ namespace org::freedesktop::monado::auxiliary {
int32_t displayId,
int32_t displayModeId);
/*!
* Wrapper for the getSupportedRefreshRates static method
*
* Java prototype:
* `public static float[] getSupportedRefreshRates(android.content.Context);`
*
* JNI signature: (Landroid/content/Context;)[F;
*
*/
static std::vector<float>
getSupportedRefreshRates(android::content::Context const &context);
/*!
* Wrapper for the getNativePointer method
*
@ -192,6 +204,7 @@ namespace org::freedesktop::monado::auxiliary {
jni::method_t removeFromWindow;
jni::method_t getDisplayMetrics;
jni::method_t getDisplayRefreshRate;
jni::method_t getSupportedRefreshRates;
jni::method_t getNativePointer;
jni::method_t markAsDiscardedByNative;
jni::method_t waitGetSurfaceHolder;

View file

@ -13,6 +13,7 @@
#include "wrap/android.app.h"
#include "wrap/android.content.h"
#include "wrap/android.view.h"
#include <vector>
namespace wrap {
@ -63,6 +64,21 @@ namespace org::freedesktop::monado::auxiliary {
displayId, displayModeId);
}
inline std::vector<float>
MonadoView::getSupportedRefreshRates(android::content::Context const &context)
{
jni::Object refreshRateArray =
Meta::data().clazz().call<jni::Object>(Meta::data().getSupportedRefreshRates, context.object());
jfloat *refreshRates =
(jfloat *)jni::env()->GetFloatArrayElements((jfloatArray)refreshRateArray.getHandle(), 0);
jsize length = jni::env()->GetArrayLength((jfloatArray)refreshRateArray.getHandle());
std::vector<float> refreshRateVector;
for (int i = 0; i < length; i++) {
refreshRateVector.push_back(refreshRates[i]);
}
return refreshRateVector;
}
inline void *
MonadoView::getNativePointer()
{

View file

@ -25,6 +25,7 @@ import androidx.annotation.GuardedBy;
import androidx.annotation.Keep;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.TreeSet;
@Keep
public class MonadoView extends SurfaceView
@ -194,6 +195,22 @@ public class MonadoView extends SurfaceView
return 0;
}
@Keep
public static float[] getSupportedRefreshRates(@NonNull Context context) {
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
TreeSet<Float> rateSet = new TreeSet<>();
Display.Mode[] modes = wm.getDefaultDisplay().getSupportedModes();
for (Display.Mode mode : modes) {
rateSet.add(mode.getRefreshRate());
}
float[] rates = new float[rateSet.size()];
int i = 0;
for (Float f : rateSet) {
rates[i++] = f;
}
return rates;
}
@Keep
public long getNativePointer() {
if (nativeCounterpart == null) {