Skip to content

Commit

Permalink
Added Android Surface swapchain support
Browse files Browse the repository at this point in the history
  • Loading branch information
maunvz committed Mar 21, 2024
1 parent 7e7fe08 commit ee59ac0
Show file tree
Hide file tree
Showing 3 changed files with 224 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/**************************************************************************/
/* openxr_khr_android_surface_swapchain_extension_wrapper.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT XR */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2022-present Godot XR contributors (see CONTRIBUTORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#ifdef ANDROID

#include "extensions/openxr_khr_android_surface_swapchain_extension_wrapper.h"
#include <godot_cpp/classes/open_xrapi_extension.hpp>

using namespace godot;

OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper *OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper::singleton = nullptr;

OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper *OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper::get_singleton() {
if (singleton == nullptr) {
singleton = memnew(OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper());
}
return singleton;
}

OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper::OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper() :
OpenXRExtensionWrapperExtension() {
ERR_FAIL_COND_MSG(singleton != nullptr, "An OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper singleton already exists.");

request_extensions[XR_KHR_ANDROID_SURFACE_SWAPCHAIN_EXTENSION_NAME] = &khr_android_surface_swapchain_ext;
request_extensions[XR_FB_ANDROID_SURFACE_SWAPCHAIN_CREATE_EXTENSION_NAME] = &fb_android_surface_swapchain_create_ext;
singleton = this;
}

OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper::~OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper() {
cleanup();
}

void OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper::_bind_methods() {
// ClassDB::bind_method(D_METHOD("is_spatial_entity_query_supported"), &OpenXRFbSpatialEntityQueryExtensionWrapper::is_spatial_entity_query_supported);
// ClassDB::bind_method(D_METHOD("query_persisted_anchors"), &OpenXRFbSpatialEntityQueryExtensionWrapper::query_persisted_anchors);
// ADD_SIGNAL(MethodInfo("persisted_anchors_found", PropertyInfo(Variant::PACKED_STRING_ARRAY, "name")));
}

void OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper::cleanup() {
khr_android_surface_swapchain_ext = false;
fb_android_surface_swapchain_create_ext = false;
}

Dictionary OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper::_get_requested_extensions() {
Dictionary result;
for (auto ext : request_extensions) {
uint64_t value = reinterpret_cast<uint64_t>(ext.value);
result[ext.key] = (Variant)value;
}
return result;
}

void OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper::_on_instance_created(uint64_t instance) {
if (khr_android_surface_swapchain_ext && fb_android_surface_swapchain_create_ext) {
bool result = initialize_khr_android_surface_swapchain_extension((XrInstance)instance);
if (!result) {
UtilityFunctions::print("Failed to initialize khr_android_surface_swapchain_ext extension");
khr_android_surface_swapchain_ext = false;
fb_android_surface_swapchain_create_ext = false;
}
}
}

void OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper::_on_session_created(uint64_t p_session) {
if (khr_android_surface_swapchain_ext && fb_android_surface_swapchain_create_ext) {
get_openxr_api()->register_composition_layer_provider(this);
}
};

void OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper::_on_session_destroyed() {
if (khr_android_surface_swapchain_ext && fb_android_surface_swapchain_create_ext) {
get_openxr_api()->unregister_composition_layer_provider(this);
}
};

void OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper::_on_instance_destroyed() {
cleanup();
}

int OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper::_get_composition_layer_count() {
return 0;
};

uint64_t OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper::_get_composition_layer(int p_index) {
return 0;
};

int OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper::_get_composition_layer_order(int p_index) {
return 0;
};

bool OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper::initialize_khr_android_surface_swapchain_extension(const XrInstance &p_instance) {
GDEXTENSION_INIT_XR_FUNC_V(xrCreateSwapchainAndroidSurfaceKHR);

return true;
}

#endif // ANDROID
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**************************************************************************/
/* openxr_khr_android_surface_swapchain_extension_wrapper.h */
/**************************************************************************/
/* This file is part of: */
/* GODOT XR */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2022-present Godot XR contributors (see CONTRIBUTORS.md) */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/

#pragma once

#define XR_USE_PLATFORM_ANDROID
#include <jni.h>
#include <openxr/openxr.h>
#include <openxr/openxr_platform.h>

#include <godot_cpp/classes/open_xr_extension_wrapper_extension.hpp>
#include <godot_cpp/templates/hash_map.hpp>
#include <godot_cpp/variant/utility_functions.hpp>

#include "util.h"

using namespace godot;

class OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper : public OpenXRExtensionWrapperExtension {
GDCLASS(OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper, OpenXRExtensionWrapperExtension);

public:
Dictionary _get_requested_extensions() override;

void _on_instance_created(uint64_t p_instance) override;
void _on_session_created(uint64_t p_session) override;
void _on_session_destroyed() override;
void _on_instance_destroyed() override;

int _get_composition_layer_count() override;
uint64_t _get_composition_layer(int p_index) override;
int _get_composition_layer_order(int p_index) override;

bool is_android_surface_swapchain_supported() {
return khr_android_surface_swapchain_ext && fb_android_surface_swapchain_create_ext;
}

static OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper *get_singleton();

OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper();
~OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper();

protected:
static void _bind_methods();

private:
EXT_PROTO_XRRESULT_FUNC4(xrCreateSwapchainAndroidSurfaceKHR,
(XrSession), session,
(const XrSwapchainCreateInfo *), info,
(XrSwapchain *), swapchain,
(jobject *), surface);

bool initialize_khr_android_surface_swapchain_extension(const XrInstance &instance);
void cleanup();

static OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper *singleton;

HashMap<String, bool *> request_extensions;
bool khr_android_surface_swapchain_ext = false;
bool fb_android_surface_swapchain_create_ext = false;
};
13 changes: 13 additions & 0 deletions common/src/main/cpp/register_types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@
#include "extensions/openxr_fb_spatial_entity_query_extension_wrapper.h"
#include "extensions/openxr_fb_spatial_entity_storage_extension_wrapper.h"

#ifdef ANDROID
#include "extensions/openxr_khr_android_surface_swapchain_extension_wrapper.h"
#endif

#include "classes/openxr_fb_hand_tracking_mesh.h"
#include "classes/openxr_fb_render_model.h"
#include "scene/xr_scene_object.h"
Expand Down Expand Up @@ -99,6 +103,11 @@ void initialize_plugin_module(ModuleInitializationLevel p_level) {
ClassDB::register_class<OpenXRFbHandTrackingAimExtensionWrapper>();
OpenXRFbHandTrackingAimExtensionWrapper::get_singleton()->register_extension_wrapper();

#ifdef ANDROID
ClassDB::register_class<OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper>();
OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper::get_singleton()->register_extension_wrapper();
#endif

ClassDB::register_class<XrSceneObject>();
ClassDB::register_class<XrSceneManager>();
ClassDB::register_class<XrSceneProviderOpenXR>();
Expand All @@ -120,6 +129,10 @@ void initialize_plugin_module(ModuleInitializationLevel p_level) {
Engine::get_singleton()->register_singleton("OpenXRFbFaceTrackingExtensionWrapper", OpenXRFbFaceTrackingExtensionWrapper::get_singleton());
Engine::get_singleton()->register_singleton("OpenXRFbHandTrackingAimExtensionWrapper", OpenXRFbHandTrackingAimExtensionWrapper::get_singleton());

#ifdef ANDROID
Engine::get_singleton()->register_singleton("OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper", OpenXRKhrAndroidSurfaceSwapchainExtensionWrapper::get_singleton());
#endif

ClassDB::register_class<OpenXRFbRenderModel>();
ClassDB::register_class<OpenXRFbHandTrackingMesh>();

Expand Down

0 comments on commit ee59ac0

Please sign in to comment.