Path: blob/master/modules/openxr/extensions/openxr_android_thread_settings_extension.cpp
20938 views
/**************************************************************************/1/* openxr_android_thread_settings_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_android_thread_settings_extension.h"3132#include "core/object/callable_method_pointer.h"33#include "core/string/print_string.h"34#include "servers/rendering/rendering_server.h"3536#ifdef XR_USE_PLATFORM_ANDROID37#include "../openxr_api.h"38#include <unistd.h>39#endif4041OpenXRAndroidThreadSettingsExtension *OpenXRAndroidThreadSettingsExtension::singleton = nullptr;4243OpenXRAndroidThreadSettingsExtension *OpenXRAndroidThreadSettingsExtension::get_singleton() {44return singleton;45}4647OpenXRAndroidThreadSettingsExtension::OpenXRAndroidThreadSettingsExtension() {48singleton = this;49}5051OpenXRAndroidThreadSettingsExtension::~OpenXRAndroidThreadSettingsExtension() {52singleton = nullptr;53}5455void OpenXRAndroidThreadSettingsExtension::_bind_methods() {56ClassDB::bind_method(D_METHOD("set_application_thread_type", "thread_type", "thread_id"), &OpenXRAndroidThreadSettingsExtension::set_application_thread_type, DEFVAL(0));5758BIND_ENUM_CONSTANT(THREAD_TYPE_APPLICATION_MAIN);59BIND_ENUM_CONSTANT(THREAD_TYPE_APPLICATION_WORKER);60BIND_ENUM_CONSTANT(THREAD_TYPE_RENDERER_MAIN);61BIND_ENUM_CONSTANT(THREAD_TYPE_RENDERER_WORKER);62}6364HashMap<String, bool *> OpenXRAndroidThreadSettingsExtension::get_requested_extensions(XrVersion p_version) {65HashMap<String, bool *> request_extensions;6667#ifdef XR_USE_PLATFORM_ANDROID68request_extensions[XR_KHR_ANDROID_THREAD_SETTINGS_EXTENSION_NAME] = &available;69#endif7071return request_extensions;72}7374void OpenXRAndroidThreadSettingsExtension::on_instance_created(XrInstance p_instance) {75if (!available) {76return;77}7879if (!_initialize_openxr_android_thread_settings_extension()) {80print_error("OpenXR: Failed to initialize android thread settings extension");81available = false;82}83}8485void OpenXRAndroidThreadSettingsExtension::on_session_created(XrSession p_session) {86if (!available) {87return;88}8990// Attempt to mark this thread as the "main thread".91set_application_thread_type(THREAD_TYPE_APPLICATION_MAIN);9293// Attempt to mark the render thread too.94RenderingServer *rendering_server = RenderingServer::get_singleton();95ERR_FAIL_NULL(rendering_server);96rendering_server->call_on_render_thread(callable_mp(this, &OpenXRAndroidThreadSettingsExtension::_set_render_thread_type));97}9899bool OpenXRAndroidThreadSettingsExtension::set_application_thread_type(ThreadType p_thread_type, uint32_t p_thread_id) {100if (!available) {101return false;102}103104#ifdef XR_USE_PLATFORM_ANDROID105XrAndroidThreadTypeKHR thread_type{};106switch (p_thread_type) {107case THREAD_TYPE_APPLICATION_MAIN:108thread_type = XR_ANDROID_THREAD_TYPE_APPLICATION_MAIN_KHR;109break;110case THREAD_TYPE_APPLICATION_WORKER:111thread_type = XR_ANDROID_THREAD_TYPE_APPLICATION_WORKER_KHR;112break;113case THREAD_TYPE_RENDERER_MAIN:114thread_type = XR_ANDROID_THREAD_TYPE_RENDERER_MAIN_KHR;115break;116case THREAD_TYPE_RENDERER_WORKER:117thread_type = XR_ANDROID_THREAD_TYPE_RENDERER_WORKER_KHR;118break;119default:120print_error(vformat("OpenXR: Failed to set android application thread; invalid thread type %d", p_thread_type));121return false;122}123124XrResult result = xrSetAndroidApplicationThreadKHR(OpenXRAPI::get_singleton()->get_session(), thread_type, p_thread_id == 0 ? gettid() : p_thread_id);125if (result != XR_SUCCESS) {126print_error(vformat("OpenXR: Failed to set android application thread; %s", OpenXRAPI::get_singleton()->get_error_string(result)));127return false;128}129#endif130131return true;132}133134bool OpenXRAndroidThreadSettingsExtension::_initialize_openxr_android_thread_settings_extension() {135#ifdef XR_USE_PLATFORM_ANDROID136EXT_INIT_XR_FUNC_V(xrSetAndroidApplicationThreadKHR);137#endif138return true;139}140141void OpenXRAndroidThreadSettingsExtension::_set_render_thread_type() {142// Skip when the render thread == the main thread143if (Thread::is_main_thread()) {144return;145}146147set_application_thread_type(THREAD_TYPE_RENDERER_MAIN);148}149150151