| 1 |
/**************************************************************************/ |
| 2 |
/* display_server_embedded.mm */ |
| 3 |
/**************************************************************************/ |
| 4 |
/* This file is part of: */ |
| 5 |
/* GODOT ENGINE */ |
| 6 |
/* https://godotengine.org */ |
| 7 |
/**************************************************************************/ |
| 8 |
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 |
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 |
/* */ |
| 11 |
/* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 |
/* a copy of this software and associated documentation files (the */ |
| 13 |
/* "Software"), to deal in the Software without restriction, including */ |
| 14 |
/* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 |
/* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 |
/* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 |
/* the following conditions: */ |
| 18 |
/* */ |
| 19 |
/* The above copyright notice and this permission notice shall be */ |
| 20 |
/* included in all copies or substantial portions of the Software. */ |
| 21 |
/* */ |
| 22 |
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 |
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 |
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 |
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 |
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 |
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 |
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 |
/**************************************************************************/ |
| 30 |
|
| 31 |
#import "display_server_embedded.h" |
| 32 |
|
| 33 |
#if defined(GLES3_ENABLED) |
| 34 |
#import "embedded_gl_manager.h" |
| 35 |
#import "platform_gl.h" |
| 36 |
|
| 37 |
#import "drivers/gles3/rasterizer_gles3.h" |
| 38 |
#endif |
| 39 |
|
| 40 |
#if defined(RD_ENABLED) |
| 41 |
#import "servers/rendering/renderer_rd/renderer_compositor_rd.h" |
| 42 |
#import "servers/rendering/rendering_device.h" |
| 43 |
|
| 44 |
#if defined(VULKAN_ENABLED) |
| 45 |
#import "rendering_context_driver_vulkan_macos.h" |
| 46 |
#endif // VULKAN_ENABLED |
| 47 |
#if defined(METAL_ENABLED) |
| 48 |
#import "drivers/metal/rendering_context_driver_metal.h" |
| 49 |
#endif |
| 50 |
#endif // RD_ENABLED |
| 51 |
|
| 52 |
#import "embedded_debugger.h" |
| 53 |
#import "macos_quartz_core_spi.h" |
| 54 |
|
| 55 |
#import "core/config/project_settings.h" |
| 56 |
#import "core/debugger/engine_debugger.h" |
| 57 |
#import "core/io/marshalls.h" |
| 58 |
#import "core/os/main_loop.h" |
| 59 |
|
| 60 |
DisplayServerEmbedded::DisplayServerEmbedded(const String &p_rendering_driver, WindowMode p_mode, DisplayServer::VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Context p_context, Error &r_error) { |
| 61 |
EmbeddedDebugger::initialize(this); |
| 62 |
|
| 63 |
r_error = OK; // default to OK |
| 64 |
|
| 65 |
native_menu = memnew(NativeMenu); |
| 66 |
|
| 67 |
Input::get_singleton()->set_event_dispatch_function(_dispatch_input_events); |
| 68 |
|
| 69 |
rendering_driver = p_rendering_driver; |
| 70 |
|
| 71 |
#if defined(RD_ENABLED) |
| 72 |
#if defined(VULKAN_ENABLED) |
| 73 |
#if defined(__x86_64__) |
| 74 |
bool fallback_to_vulkan = GLOBAL_GET("rendering/rendering_device/fallback_to_vulkan"); |
| 75 |
if (!fallback_to_vulkan) { |
| 76 |
WARN_PRINT("Metal is not supported on Intel Macs, switching to Vulkan."); |
| 77 |
} |
| 78 |
// Metal rendering driver not available on Intel. |
| 79 |
if (rendering_driver == "metal") { |
| 80 |
rendering_driver = "vulkan"; |
| 81 |
OS::get_singleton()->set_current_rendering_driver_name(rendering_driver); |
| 82 |
} |
| 83 |
#endif |
| 84 |
if (rendering_driver == "vulkan") { |
| 85 |
rendering_context = memnew(RenderingContextDriverVulkanMacOS); |
| 86 |
} |
| 87 |
#endif |
| 88 |
#if defined(METAL_ENABLED) |
| 89 |
if (rendering_driver == "metal") { |
| 90 |
rendering_context = memnew(RenderingContextDriverMetal); |
| 91 |
} |
| 92 |
#endif |
| 93 |
|
| 94 |
if (rendering_context) { |
| 95 |
if (rendering_context->initialize() != OK) { |
| 96 |
memdelete(rendering_context); |
| 97 |
rendering_context = nullptr; |
| 98 |
#if defined(GLES3_ENABLED) |
| 99 |
bool fallback_to_opengl3 = GLOBAL_GET("rendering/rendering_device/fallback_to_opengl3"); |
| 100 |
if (fallback_to_opengl3 && rendering_driver != "opengl3") { |
| 101 |
WARN_PRINT("Your device does not seem to support MoltenVK or Metal, switching to OpenGL 3."); |
| 102 |
rendering_driver = "opengl3"; |
| 103 |
OS::get_singleton()->set_current_rendering_method("gl_compatibility"); |
| 104 |
OS::get_singleton()->set_current_rendering_driver_name(rendering_driver); |
| 105 |
} else |
| 106 |
#endif |
| 107 |
{ |
| 108 |
r_error = ERR_CANT_CREATE; |
| 109 |
ERR_FAIL_MSG("Could not initialize " + rendering_driver); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
#endif |
| 114 |
|
| 115 |
#if defined(GLES3_ENABLED) |
| 116 |
if (rendering_driver == "opengl3_angle") { |
| 117 |
WARN_PRINT("ANGLE not supported for embedded display, switching to native OpenGL."); |
| 118 |
rendering_driver = "opengl3"; |
| 119 |
OS::get_singleton()->set_current_rendering_driver_name(rendering_driver); |
| 120 |
} |
| 121 |
|
| 122 |
if (rendering_driver == "opengl3") { |
| 123 |
gl_manager = memnew(GLManagerEmbedded); |
| 124 |
if (gl_manager->initialize() != OK) { |
| 125 |
memdelete(gl_manager); |
| 126 |
gl_manager = nullptr; |
| 127 |
r_error = ERR_UNAVAILABLE; |
| 128 |
ERR_FAIL_MSG("Could not initialize native OpenGL."); |
| 129 |
} |
| 130 |
layer = [CALayer new]; |
| 131 |
// OpenGL content is flipped, so it must be transformed. |
| 132 |
layer.anchorPoint = CGPointMake(0, 0); |
| 133 |
layer.transform = CATransform3DMakeScale(1.0, -1.0, 1.0); |
| 134 |
|
| 135 |
Error err = gl_manager->window_create(window_id_counter, layer, p_resolution.width, p_resolution.height); |
| 136 |
if (err != OK) { |
| 137 |
ERR_FAIL_MSG("Could not create OpenGL context."); |
| 138 |
} |
| 139 |
gl_manager->set_vsync_enabled(p_vsync_mode != DisplayServer::VSYNC_DISABLED); |
| 140 |
} |
| 141 |
#endif |
| 142 |
|
| 143 |
#if defined(RD_ENABLED) |
| 144 |
if (rendering_context) { |
| 145 |
layer = [CAMetalLayer new]; |
| 146 |
layer.anchorPoint = CGPointMake(0, 1); |
| 147 |
|
| 148 |
union { |
| 149 |
#ifdef VULKAN_ENABLED |
| 150 |
RenderingContextDriverVulkanMacOS::WindowPlatformData vulkan; |
| 151 |
#endif |
| 152 |
#ifdef METAL_ENABLED |
| 153 |
RenderingContextDriverMetal::WindowPlatformData metal; |
| 154 |
#endif |
| 155 |
} wpd; |
| 156 |
#ifdef VULKAN_ENABLED |
| 157 |
if (rendering_driver == "vulkan") { |
| 158 |
wpd.vulkan.layer_ptr = (CAMetalLayer *const *)&layer; |
| 159 |
} |
| 160 |
#endif |
| 161 |
#ifdef METAL_ENABLED |
| 162 |
if (rendering_driver == "metal") { |
| 163 |
wpd.metal.layer = (CAMetalLayer *)layer; |
| 164 |
} |
| 165 |
#endif |
| 166 |
Error err = rendering_context->window_create(window_id_counter, &wpd); |
| 167 |
ERR_FAIL_COND_MSG(err != OK, vformat("Can't create a %s context", rendering_driver)); |
| 168 |
|
| 169 |
// The rendering context is always in pixels |
| 170 |
rendering_context->window_set_size(window_id_counter, p_resolution.width, p_resolution.height); |
| 171 |
rendering_context->window_set_vsync_mode(window_id_counter, p_vsync_mode); |
| 172 |
} |
| 173 |
#endif |
| 174 |
|
| 175 |
#if defined(GLES3_ENABLED) |
| 176 |
if (rendering_driver == "opengl3") { |
| 177 |
RasterizerGLES3::make_current(true); |
| 178 |
} |
| 179 |
if (rendering_driver == "opengl3_angle") { |
| 180 |
RasterizerGLES3::make_current(false); |
| 181 |
} |
| 182 |
#endif |
| 183 |
#if defined(RD_ENABLED) |
| 184 |
if (rendering_context) { |
| 185 |
rendering_device = memnew(RenderingDevice); |
| 186 |
rendering_device->initialize(rendering_context, MAIN_WINDOW_ID); |
| 187 |
rendering_device->screen_create(MAIN_WINDOW_ID); |
| 188 |
|
| 189 |
RendererCompositorRD::make_current(); |
| 190 |
} |
| 191 |
#endif |
| 192 |
|
| 193 |
CGFloat scale = screen_get_max_scale(); |
| 194 |
layer.contentsScale = scale; |
| 195 |
layer.magnificationFilter = kCAFilterNearest; |
| 196 |
layer.minificationFilter = kCAFilterNearest; |
| 197 |
transparent = ((p_flags & WINDOW_FLAG_TRANSPARENT_BIT) == WINDOW_FLAG_TRANSPARENT_BIT); |
| 198 |
layer.opaque = !(OS::get_singleton()->is_layered_allowed() && transparent); |
| 199 |
layer.actions = @{ @"contents" : [NSNull null] }; // Disable implicit animations for contents. |
| 200 |
// AppKit frames, bounds and positions are always in points. |
| 201 |
CGRect bounds = CGRectMake(0, 0, p_resolution.width, p_resolution.height); |
| 202 |
bounds = CGRectApplyAffineTransform(bounds, CGAffineTransformInvert(CGAffineTransformMakeScale(scale, scale))); |
| 203 |
layer.bounds = bounds; |
| 204 |
|
| 205 |
CGSConnectionID connection_id = CGSMainConnectionID(); |
| 206 |
ca_context = [CAContext contextWithCGSConnection:connection_id options:@{ kCAContextCIFilterBehavior : @"ignore" }]; |
| 207 |
ca_context.layer = layer; |
| 208 |
|
| 209 |
{ |
| 210 |
Array arr = { ca_context.contextId }; |
| 211 |
EngineDebugger::get_singleton()->send_message("game_view:set_context_id", arr); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
DisplayServerEmbedded::~DisplayServerEmbedded() { |
| 216 |
if (native_menu) { |
| 217 |
memdelete(native_menu); |
| 218 |
native_menu = nullptr; |
| 219 |
} |
| 220 |
|
| 221 |
EmbeddedDebugger::deinitialize(); |
| 222 |
|
| 223 |
#if defined(GLES3_ENABLED) |
| 224 |
if (gl_manager) { |
| 225 |
memdelete(gl_manager); |
| 226 |
gl_manager = nullptr; |
| 227 |
} |
| 228 |
#endif |
| 229 |
|
| 230 |
#if defined(RD_ENABLED) |
| 231 |
if (rendering_device) { |
| 232 |
memdelete(rendering_device); |
| 233 |
rendering_device = nullptr; |
| 234 |
} |
| 235 |
|
| 236 |
if (rendering_context) { |
| 237 |
memdelete(rendering_context); |
| 238 |
rendering_context = nullptr; |
| 239 |
} |
| 240 |
#endif |
| 241 |
} |
| 242 |
|
| 243 |
DisplayServer *DisplayServerEmbedded::create_func(const String &p_rendering_driver, WindowMode p_mode, DisplayServer::VSyncMode p_vsync_mode, uint32_t p_flags, const Vector2i *p_position, const Vector2i &p_resolution, int p_screen, Context p_context, int64_t /* p_parent_window */, Error &r_error) { |
| 244 |
return memnew(DisplayServerEmbedded(p_rendering_driver, p_mode, p_vsync_mode, p_flags, p_position, p_resolution, p_screen, p_context, r_error)); |
| 245 |
} |
| 246 |
|
| 247 |
Vector<String> DisplayServerEmbedded::get_rendering_drivers_func() { |
| 248 |
Vector<String> drivers; |
| 249 |
|
| 250 |
#if defined(VULKAN_ENABLED) |
| 251 |
drivers.push_back("vulkan"); |
| 252 |
#endif |
| 253 |
#if defined(METAL_ENABLED) |
| 254 |
drivers.push_back("metal"); |
| 255 |
#endif |
| 256 |
#if defined(GLES3_ENABLED) |
| 257 |
drivers.push_back("opengl3"); |
| 258 |
#endif |
| 259 |
|
| 260 |
return drivers; |
| 261 |
} |
| 262 |
|
| 263 |
void DisplayServerEmbedded::register_embedded_driver() { |
| 264 |
register_create_function("embedded", create_func, get_rendering_drivers_func); |
| 265 |
} |
| 266 |
|
| 267 |
void DisplayServerEmbedded::beep() const { |
| 268 |
NSBeep(); |
| 269 |
} |
| 270 |
|
| 271 |
// MARK: - Mouse |
| 272 |
|
| 273 |
void DisplayServerEmbedded::_mouse_update_mode() { |
| 274 |
MouseMode wanted_mouse_mode = mouse_mode_override_enabled |
| 275 |
? mouse_mode_override |
| 276 |
: mouse_mode_base; |
| 277 |
|
| 278 |
if (wanted_mouse_mode == mouse_mode) { |
| 279 |
return; |
| 280 |
} |
| 281 |
|
| 282 |
EngineDebugger::get_singleton()->send_message("game_view:mouse_set_mode", { wanted_mouse_mode }); |
| 283 |
|
| 284 |
mouse_mode = wanted_mouse_mode; |
| 285 |
} |
| 286 |
|
| 287 |
void DisplayServerEmbedded::mouse_set_mode(MouseMode p_mode) { |
| 288 |
if (p_mode == mouse_mode_base) { |
| 289 |
return; |
| 290 |
} |
| 291 |
mouse_mode_base = p_mode; |
| 292 |
_mouse_update_mode(); |
| 293 |
} |
| 294 |
|
| 295 |
DisplayServerEmbedded::MouseMode DisplayServerEmbedded::mouse_get_mode() const { |
| 296 |
return mouse_mode; |
| 297 |
} |
| 298 |
|
| 299 |
void DisplayServerEmbedded::mouse_set_mode_override(MouseMode p_mode) { |
| 300 |
ERR_FAIL_INDEX(p_mode, MouseMode::MOUSE_MODE_MAX); |
| 301 |
if (p_mode == mouse_mode_override) { |
| 302 |
return; |
| 303 |
} |
| 304 |
mouse_mode_override = p_mode; |
| 305 |
_mouse_update_mode(); |
| 306 |
} |
| 307 |
|
| 308 |
DisplayServer::MouseMode DisplayServerEmbedded::mouse_get_mode_override() const { |
| 309 |
return mouse_mode_override; |
| 310 |
} |
| 311 |
|
| 312 |
void DisplayServerEmbedded::mouse_set_mode_override_enabled(bool p_override_enabled) { |
| 313 |
if (p_override_enabled == mouse_mode_override_enabled) { |
| 314 |
return; |
| 315 |
} |
| 316 |
mouse_mode_override_enabled = p_override_enabled; |
| 317 |
_mouse_update_mode(); |
| 318 |
} |
| 319 |
|
| 320 |
bool DisplayServerEmbedded::mouse_is_mode_override_enabled() const { |
| 321 |
return mouse_mode_override_enabled; |
| 322 |
} |
| 323 |
|
| 324 |
void DisplayServerEmbedded::warp_mouse(const Point2i &p_position) { |
| 325 |
_THREAD_SAFE_METHOD_ |
| 326 |
Input::get_singleton()->set_mouse_position(p_position); |
| 327 |
EngineDebugger::get_singleton()->send_message("game_view:warp_mouse", { p_position }); |
| 328 |
} |
| 329 |
|
| 330 |
Point2i DisplayServerEmbedded::mouse_get_position() const { |
| 331 |
_THREAD_SAFE_METHOD_ |
| 332 |
|
| 333 |
const NSPoint mouse_pos = [NSEvent mouseLocation]; |
| 334 |
const float scale = screen_get_max_scale(); |
| 335 |
|
| 336 |
for (NSScreen *screen in [NSScreen screens]) { |
| 337 |
NSRect frame = [screen frame]; |
| 338 |
if (NSMouseInRect(mouse_pos, frame, NO)) { |
| 339 |
Vector2i pos = Vector2i((int)mouse_pos.x, (int)mouse_pos.y); |
| 340 |
pos *= scale; |
| 341 |
// TODO(sgc): fix this |
| 342 |
// pos -= _get_screens_origin(); |
| 343 |
pos.y *= -1; |
| 344 |
return pos; |
| 345 |
} |
| 346 |
} |
| 347 |
return Vector2i(); |
| 348 |
} |
| 349 |
|
| 350 |
BitField<MouseButtonMask> DisplayServerEmbedded::mouse_get_button_state() const { |
| 351 |
BitField<MouseButtonMask> last_button_state = MouseButtonMask::NONE; |
| 352 |
|
| 353 |
NSUInteger buttons = [NSEvent pressedMouseButtons]; |
| 354 |
if (buttons & (1 << 0)) { |
| 355 |
last_button_state.set_flag(MouseButtonMask::LEFT); |
| 356 |
} |
| 357 |
if (buttons & (1 << 1)) { |
| 358 |
last_button_state.set_flag(MouseButtonMask::RIGHT); |
| 359 |
} |
| 360 |
if (buttons & (1 << 2)) { |
| 361 |
last_button_state.set_flag(MouseButtonMask::MIDDLE); |
| 362 |
} |
| 363 |
if (buttons & (1 << 3)) { |
| 364 |
last_button_state.set_flag(MouseButtonMask::MB_XBUTTON1); |
| 365 |
} |
| 366 |
if (buttons & (1 << 4)) { |
| 367 |
last_button_state.set_flag(MouseButtonMask::MB_XBUTTON2); |
| 368 |
} |
| 369 |
return last_button_state; |
| 370 |
} |
| 371 |
|
| 372 |
// MARK: Events |
| 373 |
|
| 374 |
void DisplayServerEmbedded::window_set_rect_changed_callback(const Callable &p_callable, WindowID p_window) { |
| 375 |
window_resize_callbacks[p_window] = p_callable; |
| 376 |
} |
| 377 |
|
| 378 |
void DisplayServerEmbedded::window_set_window_event_callback(const Callable &p_callable, WindowID p_window) { |
| 379 |
window_event_callbacks[p_window] = p_callable; |
| 380 |
} |
| 381 |
void DisplayServerEmbedded::window_set_input_event_callback(const Callable &p_callable, WindowID p_window) { |
| 382 |
input_event_callbacks[p_window] = p_callable; |
| 383 |
} |
| 384 |
|
| 385 |
void DisplayServerEmbedded::window_set_input_text_callback(const Callable &p_callable, WindowID p_window) { |
| 386 |
input_text_callbacks[p_window] = p_callable; |
| 387 |
} |
| 388 |
|
| 389 |
void DisplayServerEmbedded::window_set_drop_files_callback(const Callable &p_callable, WindowID p_window) { |
| 390 |
// Not supported |
| 391 |
} |
| 392 |
|
| 393 |
void DisplayServerEmbedded::process_events() { |
| 394 |
Input *input = Input::get_singleton(); |
| 395 |
input->flush_buffered_events(); |
| 396 |
} |
| 397 |
|
| 398 |
void DisplayServerEmbedded::_dispatch_input_events(const Ref<InputEvent> &p_event) { |
| 399 |
Ref<InputEventFromWindow> event_from_window = p_event; |
| 400 |
WindowID window_id = INVALID_WINDOW_ID; |
| 401 |
if (event_from_window.is_valid()) { |
| 402 |
window_id = event_from_window->get_window_id(); |
| 403 |
} |
| 404 |
DisplayServerEmbedded *ds = (DisplayServerEmbedded *)DisplayServer::get_singleton(); |
| 405 |
ds->send_input_event(p_event, window_id); |
| 406 |
} |
| 407 |
|
| 408 |
void DisplayServerEmbedded::send_input_event(const Ref<InputEvent> &p_event, WindowID p_id) const { |
| 409 |
if (p_id != INVALID_WINDOW_ID) { |
| 410 |
const Callable *cb = input_event_callbacks.getptr(p_id); |
| 411 |
if (cb) { |
| 412 |
_window_callback(*cb, p_event); |
| 413 |
} |
| 414 |
} else { |
| 415 |
for (const KeyValue<WindowID, Callable> &E : input_event_callbacks) { |
| 416 |
_window_callback(E.value, p_event); |
| 417 |
} |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
void DisplayServerEmbedded::send_input_text(const String &p_text, WindowID p_id) const { |
| 422 |
const Callable *cb = input_text_callbacks.getptr(p_id); |
| 423 |
if (cb) { |
| 424 |
_window_callback(*cb, p_text); |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
void DisplayServerEmbedded::send_window_event(DisplayServer::WindowEvent p_event, WindowID p_id) const { |
| 429 |
const Callable *cb = window_event_callbacks.getptr(p_id); |
| 430 |
if (cb) { |
| 431 |
_window_callback(*cb, int(p_event)); |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
void DisplayServerEmbedded::_window_callback(const Callable &p_callable, const Variant &p_arg) const { |
| 436 |
if (p_callable.is_valid()) { |
| 437 |
p_callable.call(p_arg); |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
// MARK: - |
| 442 |
|
| 443 |
bool DisplayServerEmbedded::has_feature(Feature p_feature) const { |
| 444 |
switch (p_feature) { |
| 445 |
#ifndef DISABLE_DEPRECATED |
| 446 |
case FEATURE_GLOBAL_MENU: { |
| 447 |
return (native_menu && native_menu->has_feature(NativeMenu::FEATURE_GLOBAL_MENU)); |
| 448 |
} break; |
| 449 |
#endif |
| 450 |
case FEATURE_CURSOR_SHAPE: |
| 451 |
case FEATURE_IME: |
| 452 |
case FEATURE_CUSTOM_CURSOR_SHAPE: |
| 453 |
// case FEATURE_HIDPI: |
| 454 |
// case FEATURE_ICON: |
| 455 |
// case FEATURE_MOUSE: |
| 456 |
case FEATURE_MOUSE_WARP: |
| 457 |
// case FEATURE_NATIVE_DIALOG: |
| 458 |
// case FEATURE_NATIVE_ICON: |
| 459 |
// case FEATURE_WINDOW_TRANSPARENCY: |
| 460 |
case FEATURE_CLIPBOARD: |
| 461 |
// case FEATURE_KEEP_SCREEN_ON: |
| 462 |
// case FEATURE_ORIENTATION: |
| 463 |
// case FEATURE_VIRTUAL_KEYBOARD: |
| 464 |
case FEATURE_TEXT_TO_SPEECH: |
| 465 |
// case FEATURE_TOUCHSCREEN: |
| 466 |
return true; |
| 467 |
default: |
| 468 |
return false; |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
String DisplayServerEmbedded::get_name() const { |
| 473 |
return "embedded"; |
| 474 |
} |
| 475 |
|
| 476 |
int DisplayServerEmbedded::get_screen_count() const { |
| 477 |
return 1; |
| 478 |
} |
| 479 |
|
| 480 |
int DisplayServerEmbedded::get_primary_screen() const { |
| 481 |
return 0; |
| 482 |
} |
| 483 |
|
| 484 |
Point2i DisplayServerEmbedded::screen_get_position(int p_screen) const { |
| 485 |
_THREAD_SAFE_METHOD_ |
| 486 |
|
| 487 |
p_screen = _get_screen_index(p_screen); |
| 488 |
int screen_count = get_screen_count(); |
| 489 |
ERR_FAIL_INDEX_V(p_screen, screen_count, Point2i()); |
| 490 |
|
| 491 |
return Point2i(0, 0); |
| 492 |
} |
| 493 |
|
| 494 |
Size2i DisplayServerEmbedded::screen_get_size(int p_screen) const { |
| 495 |
_THREAD_SAFE_METHOD_ |
| 496 |
|
| 497 |
p_screen = _get_screen_index(p_screen); |
| 498 |
int screen_count = get_screen_count(); |
| 499 |
ERR_FAIL_INDEX_V(p_screen, screen_count, Size2i()); |
| 500 |
|
| 501 |
return window_get_size(MAIN_WINDOW_ID); |
| 502 |
} |
| 503 |
|
| 504 |
Rect2i DisplayServerEmbedded::screen_get_usable_rect(int p_screen) const { |
| 505 |
_THREAD_SAFE_METHOD_ |
| 506 |
|
| 507 |
p_screen = _get_screen_index(p_screen); |
| 508 |
int screen_count = get_screen_count(); |
| 509 |
ERR_FAIL_INDEX_V(p_screen, screen_count, Rect2i()); |
| 510 |
|
| 511 |
return Rect2i(screen_get_position(p_screen), screen_get_size(p_screen)); |
| 512 |
} |
| 513 |
|
| 514 |
int DisplayServerEmbedded::screen_get_dpi(int p_screen) const { |
| 515 |
_THREAD_SAFE_METHOD_ |
| 516 |
|
| 517 |
p_screen = _get_screen_index(p_screen); |
| 518 |
int screen_count = get_screen_count(); |
| 519 |
ERR_FAIL_INDEX_V(p_screen, screen_count, 72); |
| 520 |
|
| 521 |
return 96; |
| 522 |
} |
| 523 |
|
| 524 |
float DisplayServerEmbedded::screen_get_scale(int p_screen) const { |
| 525 |
_THREAD_SAFE_METHOD_ |
| 526 |
|
| 527 |
switch (p_screen) { |
| 528 |
case SCREEN_WITH_MOUSE_FOCUS: |
| 529 |
case SCREEN_WITH_KEYBOARD_FOCUS: |
| 530 |
case SCREEN_PRIMARY: |
| 531 |
case SCREEN_OF_MAIN_WINDOW: |
| 532 |
case 0: |
| 533 |
return state.screen_window_scale; |
| 534 |
default: |
| 535 |
return 1.0; |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
float DisplayServerEmbedded::screen_get_refresh_rate(int p_screen) const { |
| 540 |
_THREAD_SAFE_METHOD_ |
| 541 |
|
| 542 |
p_screen = _get_screen_index(p_screen); |
| 543 |
int screen_count = get_screen_count(); |
| 544 |
ERR_FAIL_INDEX_V(p_screen, screen_count, SCREEN_REFRESH_RATE_FALLBACK); |
| 545 |
|
| 546 |
p_screen = _get_screen_index(p_screen); |
| 547 |
NSArray *screenArray = [NSScreen screens]; |
| 548 |
if ((NSUInteger)p_screen < [screenArray count]) { |
| 549 |
NSDictionary *description = [[screenArray objectAtIndex:p_screen] deviceDescription]; |
| 550 |
const CGDisplayModeRef displayMode = CGDisplayCopyDisplayMode([[description objectForKey:@"NSScreenNumber"] unsignedIntValue]); |
| 551 |
const double displayRefreshRate = CGDisplayModeGetRefreshRate(displayMode); |
| 552 |
return (float)displayRefreshRate; |
| 553 |
} |
| 554 |
ERR_PRINT("An error occurred while trying to get the screen refresh rate."); |
| 555 |
return SCREEN_REFRESH_RATE_FALLBACK; |
| 556 |
} |
| 557 |
|
| 558 |
Vector<DisplayServer::WindowID> DisplayServerEmbedded::get_window_list() const { |
| 559 |
Vector<DisplayServer::WindowID> list; |
| 560 |
list.push_back(MAIN_WINDOW_ID); |
| 561 |
return list; |
| 562 |
} |
| 563 |
|
| 564 |
DisplayServer::WindowID DisplayServerEmbedded::get_window_at_screen_position(const Point2i &p_position) const { |
| 565 |
return MAIN_WINDOW_ID; |
| 566 |
} |
| 567 |
|
| 568 |
void DisplayServerEmbedded::window_attach_instance_id(ObjectID p_instance, WindowID p_window) { |
| 569 |
window_attached_instance_id[p_window] = p_instance; |
| 570 |
} |
| 571 |
|
| 572 |
ObjectID DisplayServerEmbedded::window_get_attached_instance_id(WindowID p_window) const { |
| 573 |
return window_attached_instance_id[p_window]; |
| 574 |
} |
| 575 |
|
| 576 |
void DisplayServerEmbedded::window_set_title(const String &p_title, WindowID p_window) { |
| 577 |
// Not supported |
| 578 |
} |
| 579 |
|
| 580 |
int DisplayServerEmbedded::window_get_current_screen(WindowID p_window) const { |
| 581 |
_THREAD_SAFE_METHOD_ |
| 582 |
ERR_FAIL_COND_V(p_window != MAIN_WINDOW_ID, INVALID_SCREEN); |
| 583 |
|
| 584 |
return 0; |
| 585 |
} |
| 586 |
|
| 587 |
void DisplayServerEmbedded::window_set_current_screen(int p_screen, WindowID p_window) { |
| 588 |
// Not supported |
| 589 |
} |
| 590 |
|
| 591 |
Point2i DisplayServerEmbedded::window_get_position(WindowID p_window) const { |
| 592 |
return Point2i(); |
| 593 |
} |
| 594 |
|
| 595 |
Point2i DisplayServerEmbedded::window_get_position_with_decorations(WindowID p_window) const { |
| 596 |
return Point2i(); |
| 597 |
} |
| 598 |
|
| 599 |
void DisplayServerEmbedded::window_set_position(const Point2i &p_position, WindowID p_window) { |
| 600 |
// Probably not supported for single window iOS app |
| 601 |
} |
| 602 |
|
| 603 |
void DisplayServerEmbedded::window_set_transient(WindowID p_window, WindowID p_parent) { |
| 604 |
// Not supported |
| 605 |
} |
| 606 |
|
| 607 |
void DisplayServerEmbedded::window_set_max_size(const Size2i p_size, WindowID p_window) { |
| 608 |
// Not supported |
| 609 |
} |
| 610 |
|
| 611 |
Size2i DisplayServerEmbedded::window_get_max_size(WindowID p_window) const { |
| 612 |
return Size2i(); |
| 613 |
} |
| 614 |
|
| 615 |
void DisplayServerEmbedded::window_set_min_size(const Size2i p_size, WindowID p_window) { |
| 616 |
// Not supported |
| 617 |
} |
| 618 |
|
| 619 |
Size2i DisplayServerEmbedded::window_get_min_size(WindowID p_window) const { |
| 620 |
return Size2i(); |
| 621 |
} |
| 622 |
|
| 623 |
void DisplayServerEmbedded::window_set_size(const Size2i p_size, WindowID p_window) { |
| 624 |
print_line("Embedded window can't be resized."); |
| 625 |
} |
| 626 |
|
| 627 |
void DisplayServerEmbedded::_window_set_size(const Size2i p_size, WindowID p_window) { |
| 628 |
[CATransaction begin]; |
| 629 |
[CATransaction setDisableActions:YES]; |
| 630 |
|
| 631 |
CGFloat scale = screen_get_max_scale(); |
| 632 |
CGRect bounds = CGRectMake(0, 0, p_size.width, p_size.height); |
| 633 |
bounds = CGRectApplyAffineTransform(bounds, CGAffineTransformInvert(CGAffineTransformMakeScale(scale, scale))); |
| 634 |
layer.bounds = bounds; |
| 635 |
layer.contentsScale = scale; |
| 636 |
|
| 637 |
#if defined(RD_ENABLED) |
| 638 |
if (rendering_context) { |
| 639 |
rendering_context->window_set_size(p_window, p_size.width, p_size.height); |
| 640 |
} |
| 641 |
#endif |
| 642 |
#if defined(GLES3_ENABLED) |
| 643 |
if (gl_manager) { |
| 644 |
gl_manager->window_resize(p_window, p_size.width, p_size.height); |
| 645 |
} |
| 646 |
#endif |
| 647 |
[CATransaction commit]; |
| 648 |
|
| 649 |
Callable *cb = window_resize_callbacks.getptr(p_window); |
| 650 |
if (cb) { |
| 651 |
Variant resize_rect = Rect2i(Point2i(), p_size); |
| 652 |
_window_callback(window_resize_callbacks[p_window], resize_rect); |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
Size2i DisplayServerEmbedded::window_get_size(WindowID p_window) const { |
| 657 |
#if defined(RD_ENABLED) |
| 658 |
if (rendering_context) { |
| 659 |
RenderingContextDriver::SurfaceID surface = rendering_context->surface_get_from_window(p_window); |
| 660 |
ERR_FAIL_COND_V_MSG(surface == 0, Size2i(), "Invalid window ID"); |
| 661 |
uint32_t width = rendering_context->surface_get_width(surface); |
| 662 |
uint32_t height = rendering_context->surface_get_height(surface); |
| 663 |
return Size2i(width, height); |
| 664 |
} |
| 665 |
#endif |
| 666 |
#ifdef GLES3_ENABLED |
| 667 |
if (gl_manager) { |
| 668 |
return gl_manager->window_get_size(p_window); |
| 669 |
} |
| 670 |
#endif |
| 671 |
return Size2i(); |
| 672 |
} |
| 673 |
|
| 674 |
Size2i DisplayServerEmbedded::window_get_size_with_decorations(WindowID p_window) const { |
| 675 |
return window_get_size(p_window); |
| 676 |
} |
| 677 |
|
| 678 |
void DisplayServerEmbedded::window_set_mode(WindowMode p_mode, WindowID p_window) { |
| 679 |
// Not supported |
| 680 |
} |
| 681 |
|
| 682 |
DisplayServer::WindowMode DisplayServerEmbedded::window_get_mode(WindowID p_window) const { |
| 683 |
return WindowMode::WINDOW_MODE_WINDOWED; |
| 684 |
} |
| 685 |
|
| 686 |
bool DisplayServerEmbedded::window_is_maximize_allowed(WindowID p_window) const { |
| 687 |
return false; |
| 688 |
} |
| 689 |
|
| 690 |
void DisplayServerEmbedded::window_set_flag(WindowFlags p_flag, bool p_enabled, WindowID p_window) { |
| 691 |
if (p_flag == WINDOW_FLAG_TRANSPARENT && p_window == MAIN_WINDOW_ID) { |
| 692 |
transparent = p_enabled; |
| 693 |
layer.opaque = !(OS::get_singleton()->is_layered_allowed() && transparent); |
| 694 |
} |
| 695 |
} |
| 696 |
|
| 697 |
bool DisplayServerEmbedded::window_get_flag(WindowFlags p_flag, WindowID p_window) const { |
| 698 |
if (p_flag == WINDOW_FLAG_TRANSPARENT && p_window == MAIN_WINDOW_ID) { |
| 699 |
return transparent; |
| 700 |
} |
| 701 |
return false; |
| 702 |
} |
| 703 |
|
| 704 |
void DisplayServerEmbedded::window_request_attention(WindowID p_window) { |
| 705 |
// Not supported |
| 706 |
} |
| 707 |
|
| 708 |
void DisplayServerEmbedded::window_move_to_foreground(WindowID p_window) { |
| 709 |
// Not supported |
| 710 |
} |
| 711 |
|
| 712 |
bool DisplayServerEmbedded::window_is_focused(WindowID p_window) const { |
| 713 |
return true; |
| 714 |
} |
| 715 |
|
| 716 |
float DisplayServerEmbedded::screen_get_max_scale() const { |
| 717 |
return state.screen_max_scale; |
| 718 |
} |
| 719 |
|
| 720 |
bool DisplayServerEmbedded::window_can_draw(WindowID p_window) const { |
| 721 |
return true; |
| 722 |
} |
| 723 |
|
| 724 |
bool DisplayServerEmbedded::can_any_window_draw() const { |
| 725 |
return true; |
| 726 |
} |
| 727 |
|
| 728 |
void DisplayServerEmbedded::window_set_ime_active(const bool p_active, WindowID p_window) { |
| 729 |
EngineDebugger::get_singleton()->send_message("game_view:window_set_ime_active", { p_active }); |
| 730 |
} |
| 731 |
|
| 732 |
void DisplayServerEmbedded::window_set_ime_position(const Point2i &p_pos, WindowID p_window) { |
| 733 |
if (p_pos == ime_last_position) { |
| 734 |
return; |
| 735 |
} |
| 736 |
EngineDebugger::get_singleton()->send_message("game_view:window_set_ime_position", { p_pos }); |
| 737 |
ime_last_position = p_pos; |
| 738 |
} |
| 739 |
|
| 740 |
void DisplayServerEmbedded::set_state(const DisplayServerEmbeddedState &p_state) { |
| 741 |
if (state == p_state) { |
| 742 |
return; |
| 743 |
} |
| 744 |
|
| 745 |
uint32_t old_display_id = state.display_id; |
| 746 |
|
| 747 |
state = p_state; |
| 748 |
|
| 749 |
if (state.display_id != old_display_id) { |
| 750 |
#if defined(GLES3_ENABLED) |
| 751 |
if (gl_manager) { |
| 752 |
gl_manager->set_display_id(state.display_id); |
| 753 |
} |
| 754 |
#endif |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
void DisplayServerEmbedded::window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window) { |
| 759 |
#if defined(GLES3_ENABLED) |
| 760 |
if (gl_manager) { |
| 761 |
gl_manager->set_vsync_enabled(p_vsync_mode != DisplayServer::VSYNC_DISABLED); |
| 762 |
} |
| 763 |
#endif |
| 764 |
|
| 765 |
#if defined(RD_ENABLED) |
| 766 |
if (rendering_context) { |
| 767 |
rendering_context->window_set_vsync_mode(p_window, p_vsync_mode); |
| 768 |
} |
| 769 |
#endif |
| 770 |
} |
| 771 |
|
| 772 |
DisplayServer::VSyncMode DisplayServerEmbedded::window_get_vsync_mode(WindowID p_window) const { |
| 773 |
_THREAD_SAFE_METHOD_ |
| 774 |
#if defined(GLES3_ENABLED) |
| 775 |
if (gl_manager) { |
| 776 |
return (gl_manager->is_vsync_enabled() ? DisplayServer::VSyncMode::VSYNC_ENABLED : DisplayServer::VSyncMode::VSYNC_DISABLED); |
| 777 |
} |
| 778 |
#endif |
| 779 |
#if defined(RD_ENABLED) |
| 780 |
if (rendering_context) { |
| 781 |
return rendering_context->window_get_vsync_mode(p_window); |
| 782 |
} |
| 783 |
#endif |
| 784 |
return DisplayServer::VSYNC_ENABLED; |
| 785 |
} |
| 786 |
|
| 787 |
void DisplayServerEmbedded::update_im_text(const Point2i &p_selection, const String &p_text) { |
| 788 |
im_selection = p_selection; |
| 789 |
im_text = p_text; |
| 790 |
|
| 791 |
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_OS_IME_UPDATE); |
| 792 |
} |
| 793 |
|
| 794 |
Point2i DisplayServerEmbedded::ime_get_selection() const { |
| 795 |
return im_selection; |
| 796 |
} |
| 797 |
|
| 798 |
String DisplayServerEmbedded::ime_get_text() const { |
| 799 |
return im_text; |
| 800 |
} |
| 801 |
|
| 802 |
void DisplayServerEmbedded::cursor_set_shape(CursorShape p_shape) { |
| 803 |
cursor_shape = p_shape; |
| 804 |
EngineDebugger::get_singleton()->send_message("game_view:cursor_set_shape", { p_shape }); |
| 805 |
} |
| 806 |
|
| 807 |
DisplayServer::CursorShape DisplayServerEmbedded::cursor_get_shape() const { |
| 808 |
return cursor_shape; |
| 809 |
} |
| 810 |
|
| 811 |
void DisplayServerEmbedded::cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) { |
| 812 |
PackedByteArray data; |
| 813 |
if (p_cursor.is_valid()) { |
| 814 |
Ref<Image> image = _get_cursor_image_from_resource(p_cursor, p_hotspot); |
| 815 |
if (image.is_valid()) { |
| 816 |
data = image->save_png_to_buffer(); |
| 817 |
} |
| 818 |
} |
| 819 |
EngineDebugger::get_singleton()->send_message("game_view:cursor_set_custom_image", { data, p_shape, p_hotspot }); |
| 820 |
} |
| 821 |
|
| 822 |
void DisplayServerEmbedded::swap_buffers() { |
| 823 |
#ifdef GLES3_ENABLED |
| 824 |
if (gl_manager) { |
| 825 |
gl_manager->swap_buffers(); |
| 826 |
} |
| 827 |
#endif |
| 828 |
} |
| 829 |
|
| 830 |
void DisplayServerEmbeddedState::serialize(PackedByteArray &r_data) { |
| 831 |
r_data.resize(16); |
| 832 |
|
| 833 |
uint8_t *data = r_data.ptrw(); |
| 834 |
data += encode_float(screen_max_scale, data); |
| 835 |
data += encode_float(screen_dpi, data); |
| 836 |
data += encode_float(screen_window_scale, data); |
| 837 |
data += encode_uint32(display_id, data); |
| 838 |
|
| 839 |
// Assert we had enough space. |
| 840 |
DEV_ASSERT(r_data.size() >= (data - r_data.ptrw())); |
| 841 |
} |
| 842 |
|
| 843 |
Error DisplayServerEmbeddedState::deserialize(const PackedByteArray &p_data) { |
| 844 |
const uint8_t *data = p_data.ptr(); |
| 845 |
|
| 846 |
screen_max_scale = decode_float(data); |
| 847 |
data += sizeof(float); |
| 848 |
screen_dpi = decode_float(data); |
| 849 |
data += sizeof(float); |
| 850 |
screen_window_scale = decode_float(data); |
| 851 |
data += sizeof(float); |
| 852 |
display_id = decode_uint32(data); |
| 853 |
|
| 854 |
return OK; |
| 855 |
} |