Skip to content

Commit

Permalink
Add java api to access the OpenXR config preferences.
Browse files Browse the repository at this point in the history
  • Loading branch information
m4gr3d committed Sep 30, 2021
1 parent 10963ec commit edbe9e2
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 0 deletions.
39 changes: 39 additions & 0 deletions android/src/main/cpp/jni/jni_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
#define JNI_UTIL_H

#include <jni.h>
#include <core/Array.hpp>
#include <core/String.hpp>
#include <core/Variant.hpp>
#include <vector>

/** Auxiliary macros */
#define __JNI_METHOD_BUILD(package, class_name, method) \
Expand Down Expand Up @@ -76,4 +79,40 @@ static jstring string_to_jstring(JNIEnv *env, const godot::String &source) {
return nullptr;
}

static jintArray array_to_jintArray(JNIEnv *env, const godot::Array &array) {
const int count = array.size();
std::vector<int> values;
for (int i = 0; i < count; i++) {
values.push_back(array[i]);
}

jintArray result = env->NewIntArray(count);
env->SetIntArrayRegion(result, 0, count, values.data());
return result;
}

static jfloatArray array_to_jfloatArray(JNIEnv *env, const godot::Array &array) {
const int count = array.size();
std::vector<float> values;
for (int i = 0; i < count; i++) {
values.push_back(array[i]);
}

jfloatArray result = env->NewFloatArray(count);
env->SetFloatArrayRegion(result, 0, count, values.data());
return result;
}

static jdoubleArray array_to_jdoubleArray(JNIEnv *env, const godot::Array &array) {
const int count = array.size();
std::vector<double> values;
for (int i = 0; i < count; i++) {
values.push_back(array[i]);
}

jdoubleArray result = env->NewDoubleArray(count);
env->SetDoubleArrayRegion(result, 0, count, values.data());
return result;
}

#endif // JNI_UTIL_H
42 changes: 42 additions & 0 deletions android/src/main/cpp/jni/openxr_config_jni.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include <jni.h>

#include "jni/jni_util.h"
#include "openxr/extensions/xr_fb_color_space_extension_wrapper.h"
#include "openxr/extensions/xr_fb_display_refresh_rate_extension_wrapper.h"
#include <core/Array.hpp>
#include <core/Dictionary.hpp>

#undef JNI_PACKAGE_NAME
#define JNI_PACKAGE_NAME org_godotengine_plugin_vr_openxr_api

#undef JNI_CLASS_NAME
#define JNI_CLASS_NAME OpenXRConfig

extern "C" {
JNIEXPORT jint JNICALL JNI_METHOD(nativeGetXrColorSpace)(JNIEnv *, jclass) {
return XRFbColorSpaceExtensionWrapper::get_singleton()->get_color_space();
}

JNIEXPORT void JNICALL JNI_METHOD(nativeSetXrColorSpace)(JNIEnv *, jclass, jint color_space) {
XRFbColorSpaceExtensionWrapper::get_singleton()->set_color_space(color_space);
}

JNIEXPORT jintArray JNICALL JNI_METHOD(nativeGetAvailableXrColorSpaces)(JNIEnv *env, jclass) {
godot::Dictionary color_spaces = XRFbColorSpaceExtensionWrapper::get_singleton()->get_available_color_spaces();
godot::Array color_spaces_values = color_spaces.keys();
return array_to_jintArray(env, color_spaces_values);
}

JNIEXPORT jdouble JNICALL JNI_METHOD(getRefreshRate)(JNIEnv *, jclass, jobject) {
return XRFbDisplayRefreshRateExtensionWrapper::get_singleton()->get_refresh_rate();
}

JNIEXPORT void JNICALL JNI_METHOD(setRefreshRate)(JNIEnv *, jclass, jobject, jdouble refresh_rate) {
XRFbDisplayRefreshRateExtensionWrapper::get_singleton()->set_refresh_rate(refresh_rate);
}

JNIEXPORT jdoubleArray JNICALL JNI_METHOD(getAvailableRefreshRates)(JNIEnv *env, jclass, jobject) {
godot::Array refresh_rates = XRFbDisplayRefreshRateExtensionWrapper::get_singleton()->get_available_refresh_rates();
return array_to_jdoubleArray(env, refresh_rates);
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
@file:JvmName("OpenXRConfig")

package org.godotengine.plugin.vr.openxr.api

import org.godotengine.plugin.vr.openxr.OpenXRPlugin

/**
* See https://www.khronos.org/registry/OpenXR/specs/1.0/html/xrspec.html#XrColorSpaceFB for reference.
*/
enum class XrColorSpace(internal val value: Int) {
/**
* No color correction, not recommended for production use.
*/
XR_COLOR_SPACE_UNMANAGED(0),

/**
* Standard Rec. 2020 chromacities.
* This is the preferred color space for standardized color across all Oculus HMDs
* with D65 white point.
*/
XR_COLOR_SPACE_REC2020(1),

/**
* Standard Rec. 709 chromaticities, similar to sRGB.
*/
XR_COLOR_SPACE_REC709(2),

/**
* Unique color space, between P3 and Adobe RGB using D75 white point.
*
* Color Space Details with Chromacity Primaries in CIE 1931 xy:
* Red: (0.666, 0.334)
* Green: (0.238, 0.714)
* Blue: (0.139, 0.053)
* White: (0.298, 0.318)
*/
XR_COLOR_SPACE_RIFT_CV1(3),

/**
* Unique color space. Similar to Rec 709 using D75.
*
* Color Space Details with Chromacity Primaries in CIE 1931 xy:
* Red: (0.640, 0.330)
* Green: (0.292, 0.586)
* Blue: (0.156, 0.058)
* White: (0.298, 0.318)
*/
XR_COLOR_SPACE_RIFT_S(4),

/**
* Unique color space. Similar to Rift CV1 using D75 white point
*
* Color Space Details with Chromacity Primaries in CIE 1931 xy:
* Red: (0.661, 0.338)
* Green: (0.228, 0.718)
* Blue: (0.142, 0.042)
* White: (0.298, 0.318)
*/
XR_COLOR_SPACE_QUEST(5),

/**
* Similar to DCI-P3, but uses D65 white point instead.
*
* Color Space Details with Chromacity Primaries in CIE 1931 xy:
* Red: (0.680, 0.320)
* Green: (0.265, 0.690)
* Blue: (0.150, 0.060)
* White: (0.313, 0.329)
*/
XR_COLOR_SPACE_P3(6),

/**
* Standard Adobe chromacities.
*/
XR_COLOR_SPACE_ADOBE_RGB(7);

companion object {
fun toXrColorSpace(value: Int): XrColorSpace {
for (colorSpace in values()) {
if (colorSpace.value == value) {
return colorSpace
}
}
return XR_COLOR_SPACE_UNMANAGED
}
}
}

/**
* Used to retrieve the device color space.
*/
fun OpenXRPlugin.getColorSpace() = XrColorSpace.toXrColorSpace(nativeGetXrColorSpace())

private external fun nativeGetXrColorSpace(): Int

/**
* Update the device color space.
*/
fun OpenXRPlugin.setColorSpace(colorSpace: XrColorSpace) = nativeSetXrColorSpace(colorSpace.value)

private external fun nativeSetXrColorSpace(colorSpace: Int)

/**
* Retrieve the list of color spaces supported on this device.
*/
fun OpenXRPlugin.getAvailableColorSpaces() : Array<XrColorSpace> {
val colorSpaces = nativeGetAvailableXrColorSpaces()
return Array(colorSpaces.size) { XrColorSpace.toXrColorSpace(colorSpaces[it]) }
}

private external fun nativeGetAvailableXrColorSpaces(): IntArray

/**
* Retrieve the device current refresh rate.
*/
external fun OpenXRPlugin.getRefreshRate(): Double

/**
* Update the device's refresh rate.
*/
external fun OpenXRPlugin.setRefreshRate(refreshRate: Double)

/**
* Retrieve the list of refresh rates supported by this device.
*/
external fun OpenXRPlugin.getAvailableRefreshRates(): DoubleArray

0 comments on commit edbe9e2

Please sign in to comment.