Path: blob/master/modules/openxr/extensions/openxr_future_extension.cpp
10278 views
/**************************************************************************/1/* openxr_future_extension.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "openxr_future_extension.h"3132#include "../openxr_api.h"3334////////////////////////////////////////////////////////////////////////////35// OpenXRFutureResult3637void OpenXRFutureResult::_bind_methods() {38ClassDB::bind_method(D_METHOD("get_status"), &OpenXRFutureResult::get_status);39ClassDB::bind_method(D_METHOD("get_future"), &OpenXRFutureResult::_get_future);40ClassDB::bind_method(D_METHOD("cancel_future"), &OpenXRFutureResult::cancel_future);4142ClassDB::bind_method(D_METHOD("set_result_value", "result_value"), &OpenXRFutureResult::set_result_value);43ClassDB::bind_method(D_METHOD("get_result_value"), &OpenXRFutureResult::get_result_value);4445ADD_SIGNAL(MethodInfo("completed", PropertyInfo(Variant::OBJECT, "result", PROPERTY_HINT_RESOURCE_TYPE, "OpenXRFutureResult")));4647BIND_ENUM_CONSTANT(RESULT_RUNNING);48BIND_ENUM_CONSTANT(RESULT_FINISHED);49BIND_ENUM_CONSTANT(RESULT_CANCELLED);50}5152void OpenXRFutureResult::_mark_as_finished() {53// Update our status54status = RESULT_FINISHED;5556// Perform our callback57on_success_callback.call(this); // Note, `this` will be converted to a variant that will be refcounted!5859// Emit our signal, we assume our callback has provided us with the correct result value by calling set_result_value.60emit_signal(SNAME("completed"), result_value);61}6263void OpenXRFutureResult::_mark_as_cancelled() {64// Update our status65status = RESULT_CANCELLED;6667// There is no point in doing a callback for cancellation as its always user invoked.6869// But we do emit our signal to make sure any await finishes.70Variant no_result;71emit_signal(SNAME("completed"), no_result);72}7374OpenXRFutureResult::ResultStatus OpenXRFutureResult::get_status() const {75return status;76}7778XrFutureEXT OpenXRFutureResult::get_future() const {79return future;80}8182uint64_t OpenXRFutureResult::_get_future() const {83return (uint64_t)future;84}8586void OpenXRFutureResult::set_result_value(const Variant &p_result_value) {87result_value = p_result_value;88}8990Variant OpenXRFutureResult::get_result_value() const {91return result_value;92}9394void OpenXRFutureResult::cancel_future() {95ERR_FAIL_COND(status != RESULT_RUNNING);9697OpenXRFutureExtension *future_extension = OpenXRFutureExtension::get_singleton();98ERR_FAIL_NULL(future_extension);99100future_extension->cancel_future(future);101}102103OpenXRFutureResult::OpenXRFutureResult(XrFutureEXT p_future, const Callable &p_on_success) {104future = p_future;105on_success_callback = p_on_success;106}107108////////////////////////////////////////////////////////////////////////////109// OpenXRFutureExtension110111OpenXRFutureExtension *OpenXRFutureExtension::singleton = nullptr;112113OpenXRFutureExtension *OpenXRFutureExtension::get_singleton() {114return singleton;115}116117OpenXRFutureExtension::OpenXRFutureExtension() {118singleton = this;119}120121OpenXRFutureExtension::~OpenXRFutureExtension() {122singleton = nullptr;123}124125void OpenXRFutureExtension::_bind_methods() {126ClassDB::bind_method(D_METHOD("is_active"), &OpenXRFutureExtension::is_active);127ClassDB::bind_method(D_METHOD("register_future", "future", "on_success"), &OpenXRFutureExtension::_register_future, DEFVAL(Callable()));128ClassDB::bind_method(D_METHOD("cancel_future", "future"), &OpenXRFutureExtension::_cancel_future);129}130131HashMap<String, bool *> OpenXRFutureExtension::get_requested_extensions() {132HashMap<String, bool *> request_extensions;133134request_extensions[XR_EXT_FUTURE_EXTENSION_NAME] = &future_ext;135136return request_extensions;137}138139void OpenXRFutureExtension::on_instance_created(const XrInstance p_instance) {140if (future_ext) {141EXT_INIT_XR_FUNC(xrPollFutureEXT);142EXT_INIT_XR_FUNC(xrCancelFutureEXT);143144future_ext = xrPollFutureEXT_ptr && xrCancelFutureEXT_ptr;145}146}147148void OpenXRFutureExtension::on_instance_destroyed() {149xrPollFutureEXT_ptr = nullptr;150xrCancelFutureEXT_ptr = nullptr;151}152153void OpenXRFutureExtension::on_session_destroyed() {154if (!is_active()) {155return;156}157158OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();159ERR_FAIL_NULL(openxr_api);160161// Cancel any running futures.162for (const KeyValue<XrFutureEXT, Ref<OpenXRFutureResult>> &element : futures) {163XrFutureCancelInfoEXT cancel_info = {164XR_TYPE_FUTURE_CANCEL_INFO_EXT, // type165nullptr, // next166element.key // future167};168XrResult result = xrCancelFutureEXT_ptr(openxr_api->get_instance(), &cancel_info);169if (XR_FAILED(result)) {170WARN_PRINT("OpenXR: Failed to cancel future [" + openxr_api->get_error_string(result) + "]");171}172173// Make sure we mark our future result as cancelled174element.value->_mark_as_cancelled();175}176futures.clear();177}178179bool OpenXRFutureExtension::is_active() const {180return future_ext;181}182183Ref<OpenXRFutureResult> OpenXRFutureExtension::register_future(XrFutureEXT p_future, const Callable &p_on_success) {184ERR_FAIL_COND_V(futures.has(p_future), nullptr);185186Ref<OpenXRFutureResult> future_result;187future_result.instantiate(p_future, p_on_success);188189futures[p_future] = future_result;190191return future_result;192}193194Ref<OpenXRFutureResult> OpenXRFutureExtension::_register_future(uint64_t p_future, const Callable &p_on_success) {195return register_future((XrFutureEXT)p_future, p_on_success);196}197198void OpenXRFutureExtension::cancel_future(XrFutureEXT p_future) {199ERR_FAIL_COND(!futures.has(p_future));200201OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();202ERR_FAIL_NULL(openxr_api);203204XrFutureCancelInfoEXT cancel_info = {205XR_TYPE_FUTURE_CANCEL_INFO_EXT, // type206nullptr, // next207p_future // future208};209XrResult result = xrCancelFutureEXT_ptr(openxr_api->get_instance(), &cancel_info);210if (XR_FAILED(result)) {211WARN_PRINT("OpenXR: Failed to cancel future [" + openxr_api->get_error_string(result) + "]");212}213214// Make sure we mark our future result as cancelled215futures[p_future]->_mark_as_cancelled();216217// And erase it from the futures we track218futures.erase(p_future);219}220221void OpenXRFutureExtension::_cancel_future(uint64_t p_future) {222cancel_future((XrFutureEXT)p_future);223}224225void OpenXRFutureExtension::on_process() {226if (!is_active()) {227return;228}229230OpenXRAPI *openxr_api = OpenXRAPI::get_singleton();231ERR_FAIL_NULL(openxr_api);232233// Process futures234Vector<XrFutureEXT> completed;235for (const KeyValue<XrFutureEXT, Ref<OpenXRFutureResult>> &element : futures) {236XrFuturePollInfoEXT poll_info = {237XR_TYPE_FUTURE_POLL_INFO_EXT, // type238nullptr, // next239element.key // future240};241XrFuturePollResultEXT poll_result = {242XR_TYPE_FUTURE_POLL_RESULT_EXT, // type243nullptr, // next244XR_FUTURE_STATE_MAX_ENUM_EXT // state245};246XrResult result = xrPollFutureEXT_ptr(openxr_api->get_instance(), &poll_info, &poll_result);247if (XR_FAILED(result)) {248ERR_PRINT("OpenXR: Failed to obtain future status [" + openxr_api->get_error_string(result) + "]");249// Maybe remove this depending on the error?250continue;251}252if (poll_result.state == XR_FUTURE_STATE_READY_EXT) {253// Mark our future result as finished (this will invoke our callback).254element.value->_mark_as_finished();255256// Queue removing this257completed.push_back(element.key);258}259}260261// Now clean up completed futures.262for (const XrFutureEXT &future : completed) {263futures.erase(future);264}265}266267268