Skip to content

armgit / godot

Public Imported from GitHub
0

Godot Engine – Multi-platform 2D and 3D game engine

5.8 KB
Raw Blame
/**************************************************************************/
/*  display_server_visionos.mm                                            */
/**************************************************************************/
/*                         This file is part of:                          */
/*                             GODOT ENGINE                               */
/*                        https://godotengine.org                         */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur.                  */
/*                                                                        */
/* 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.                 */
/**************************************************************************/

#import "display_server_visionos.h"

#import "gc_keyboard_handler.h"
#import "godot_app_delegate_service_visionos.h"

#import "core/os/os.h"

DisplayServerVisionOS *DisplayServerVisionOS::get_singleton() {
	return (DisplayServerVisionOS *)DisplayServerAppleEmbedded::get_singleton();
}

DisplayServerVisionOS::DisplayServerVisionOS(const String &p_rendering_driver, DisplayServerEnums::WindowMode p_mode, DisplayServerEnums::VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, DisplayServerEnums::Context p_context, int64_t p_parent_window, Error &r_error) :
		DisplayServerAppleEmbedded(p_rendering_driver, p_mode, p_vsync_mode, p_flags, p_position, p_resolution, p_screen, p_context, p_parent_window, r_error) {
	String rendering_method = OS::get_singleton()->get_current_rendering_method();
	GDTRenderMode app_delegate_render_mode = GDTAppDelegateServiceVisionOS.renderMode;
	if (app_delegate_render_mode == GDTRenderModeCompositorServices && rendering_method == "forward_plus") {
		WARN_PRINT_ONCE("visionOS in immersive mode doesn't support the Forward+ renderer, switching to the Mobile renderer.");
		OS::get_singleton()->set_current_rendering_method("mobile", OS::RENDERING_SOURCE_FALLBACK);
	}

	// In Compositor Services mode, there is no UIViewController to receive UIPress
	// events, so we use the Game Controller framework's GCKeyboard API instead.
	if (app_delegate_render_mode == GDTRenderModeCompositorServices) {
		gc_keyboard = memnew(GCKeyboardHandler);
		gc_keyboard->start();
	}
}

DisplayServerVisionOS::~DisplayServerVisionOS() {
	if (gc_keyboard) {
		memdelete(gc_keyboard);
		gc_keyboard = nullptr;
	}
}

DisplayServer *DisplayServerVisionOS::create_func(const String &p_rendering_driver, DisplayServerEnums::WindowMode p_mode, DisplayServerEnums::VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, DisplayServerEnums::Context p_context, int64_t p_parent_window, Error &r_error) {
	return memnew(DisplayServerVisionOS(p_rendering_driver, p_mode, p_vsync_mode, p_flags, p_position, p_resolution, p_screen, p_context, p_parent_window, r_error));
}

void DisplayServerVisionOS::register_visionos_driver() {
	register_create_function("visionOS", create_func, get_rendering_drivers_func);
}

String DisplayServerVisionOS::get_name() const {
	return "visionOS";
}

int DisplayServerVisionOS::screen_get_dpi(int p_screen) const {
	p_screen = _get_screen_index(p_screen);
	int screen_count = get_screen_count();
	ERR_FAIL_INDEX_V(p_screen, screen_count, 72);

	// TODO(Apple): Compute this properly from SwiftUI Metric APIs
	return 72;
}

float DisplayServerVisionOS::screen_get_refresh_rate(int p_screen) const {
	p_screen = _get_screen_index(p_screen);
	int screen_count = get_screen_count();
	ERR_FAIL_INDEX_V(p_screen, screen_count, SCREEN_REFRESH_RATE_FALLBACK);

	return 90;
}

float DisplayServerVisionOS::screen_get_scale(int p_screen) const {
	p_screen = _get_screen_index(p_screen);
	int screen_count = get_screen_count();
	ERR_FAIL_INDEX_V(p_screen, screen_count, 1.0f);

	return 1;
}

bool DisplayServerVisionOS::_screen_hdr_is_supported() const {
	return true;
}

float DisplayServerVisionOS::_screen_potential_edr_headroom() const {
	// Current Apple Vision Pro hardware supports an EDR headroom of 2.0 (two times the SDR range).
	// See https://developer.apple.com/videos/play/wwdc2023/10089?time=603
	return 2.0f;
}

float DisplayServerVisionOS::_screen_current_edr_headroom() const {
	// Current Apple Vision Pro hardware  supports an EDR headroom of 2.0 (two times the SDR range).
	// See https://developer.apple.com/videos/play/wwdc2023/10089?time=603
	return 2.0f;
}