Skip to content

Commit

Permalink
Polish interaction between windowing, input and rendering
Browse files Browse the repository at this point in the history
- Adapt GL make/release API to the current architecture.
- Fix DisplayServer being locked while dispatching input (prevent deadlocks).
  • Loading branch information
RandomShaper committed Apr 10, 2024
1 parent 1b104ff commit c28f590
Show file tree
Hide file tree
Showing 22 changed files with 34 additions and 112 deletions.
15 changes: 0 additions & 15 deletions drivers/egl/egl_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,21 +260,6 @@ void EGLManager::release_current() {
eglMakeCurrent(current_display.egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
}

void EGLManager::make_current() {
if (!current_window) {
return;
}

if (!current_window->initialized) {
WARN_PRINT("Current OpenGL window is uninitialized!");
return;
}

GLDisplay &current_display = displays[current_window->gldisplay_id];

eglMakeCurrent(current_display.egl_display, current_window->egl_surface, current_window->egl_surface, current_display.egl_context);
}

void EGLManager::swap_buffers() {
if (!current_window) {
return;
Expand Down
1 change: 0 additions & 1 deletion drivers/egl/egl_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ class EGLManager {
void window_destroy(DisplayServer::WindowID p_window_id);

void release_current();
void make_current();
void swap_buffers();

void window_make_current(DisplayServer::WindowID p_window_id);
Expand Down
8 changes: 0 additions & 8 deletions platform/linuxbsd/wayland/display_server_wayland.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1174,14 +1174,6 @@ void DisplayServerWayland::release_rendering_thread() {
#endif
}

void DisplayServerWayland::make_rendering_thread() {
#ifdef GLES3_ENABLED
if (egl_manager) {
egl_manager->make_current();
}
#endif
}

void DisplayServerWayland::swap_buffers() {
#ifdef GLES3_ENABLED
if (egl_manager) {
Expand Down
1 change: 0 additions & 1 deletion platform/linuxbsd/wayland/display_server_wayland.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ class DisplayServerWayland : public DisplayServer {
virtual void process_events() override;

virtual void release_rendering_thread() override;
virtual void make_rendering_thread() override;
virtual void swap_buffers() override;

virtual void set_context(Context p_context) override;
Expand Down
15 changes: 3 additions & 12 deletions platform/linuxbsd/x11/display_server_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4268,7 +4268,7 @@ bool DisplayServerX11::_window_focus_check() {
}

void DisplayServerX11::process_events() {
_THREAD_SAFE_METHOD_
_THREAD_SAFE_LOCK_

#ifdef DISPLAY_SERVER_X11_DEBUG_LOGS_ENABLED
static int frame = 0;
Expand Down Expand Up @@ -5097,6 +5097,8 @@ void DisplayServerX11::process_events() {
*/
}

_THREAD_SAFE_UNLOCK_

Input::get_singleton()->flush_buffered_events();
}

Expand All @@ -5111,17 +5113,6 @@ void DisplayServerX11::release_rendering_thread() {
#endif
}

void DisplayServerX11::make_rendering_thread() {
#if defined(GLES3_ENABLED)
if (gl_manager) {
gl_manager->make_current();
}
if (gl_manager_egl) {
gl_manager_egl->make_current();
}
#endif
}

void DisplayServerX11::swap_buffers() {
#if defined(GLES3_ENABLED)
if (gl_manager) {
Expand Down
1 change: 0 additions & 1 deletion platform/linuxbsd/x11/display_server_x11.h
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,6 @@ class DisplayServerX11 : public DisplayServer {
virtual void process_events() override;

virtual void release_rendering_thread() override;
virtual void make_rendering_thread() override;
virtual void swap_buffers() override;

virtual void set_context(Context p_context) override;
Expand Down
14 changes: 0 additions & 14 deletions platform/linuxbsd/x11/gl_manager_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,20 +311,6 @@ void GLManager_X11::window_make_current(DisplayServer::WindowID p_window_id) {
_internal_set_current_window(&win);
}

void GLManager_X11::make_current() {
if (!_current_window) {
return;
}
if (!_current_window->in_use) {
WARN_PRINT("current window not in use!");
return;
}
const GLDisplay &disp = get_current_display();
if (!glXMakeCurrent(_x_windisp.x11_display, _x_windisp.x11_window, disp.context->glx_context)) {
ERR_PRINT("glXMakeCurrent failed");
}
}

void GLManager_X11::swap_buffers() {
if (!_current_window) {
return;
Expand Down
1 change: 0 additions & 1 deletion platform/linuxbsd/x11/gl_manager_x11.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,6 @@ class GLManager_X11 {
void window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height);

void release_current();
void make_current();
void swap_buffers();

void window_make_current(DisplayServer::WindowID p_window_id);
Expand Down
1 change: 0 additions & 1 deletion platform/macos/display_server_macos.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,6 @@ class DisplayServerMacOS : public DisplayServer {
virtual void force_process_and_drop_events() override;

virtual void release_rendering_thread() override;
virtual void make_rendering_thread() override;
virtual void swap_buffers() override;

virtual void set_native_icon(const String &p_filename) override;
Expand Down
18 changes: 13 additions & 5 deletions platform/macos/display_server_macos.mm
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,6 @@
}

void DisplayServerMacOS::_dispatch_input_event(const Ref<InputEvent> &p_event) {
_THREAD_SAFE_METHOD_
if (!in_dispatch_input_event) {
in_dispatch_input_event = true;

Expand Down Expand Up @@ -2986,7 +2985,7 @@
}

void DisplayServerMacOS::process_events() {
_THREAD_SAFE_METHOD_
_THREAD_SAFE_LOCK_

while (true) {
NSEvent *event = [NSApp
Expand Down Expand Up @@ -3019,7 +3018,9 @@

if (!drop_events) {
_process_key_events();
_THREAD_SAFE_UNLOCK_
Input::get_singleton()->flush_buffered_events();
_THREAD_SAFE_LOCK_
}

for (KeyValue<WindowID, WindowData> &E : windows) {
Expand All @@ -3045,6 +3046,8 @@
}
}
}

_THREAD_SAFE_UNLOCK_
}

void DisplayServerMacOS::force_process_and_drop_events() {
Expand All @@ -3056,9 +3059,14 @@
}

void DisplayServerMacOS::release_rendering_thread() {
}

void DisplayServerMacOS::make_rendering_thread() {
#if defined(GLES3_ENABLED)
if (gl_manager_angle) {
gl_manager_angle->release_current();
}
if (gl_manager_legacy) {
gl_manager_legacy->release_current();
}
#endif
}

void DisplayServerMacOS::swap_buffers() {
Expand Down
1 change: 0 additions & 1 deletion platform/macos/gl_manager_macos_legacy.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ class GLManagerLegacy_MacOS {
void window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height);

void release_current();
void make_current();
void swap_buffers();

void window_make_current(DisplayServer::WindowID p_window_id);
Expand Down
13 changes: 1 addition & 12 deletions platform/macos/gl_manager_macos_legacy.mm
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
}

[NSOpenGLContext clearCurrentContext];
current_window = DisplayServer::INVALID_WINDOW_ID;
}

void GLManagerLegacy_MacOS::window_make_current(DisplayServer::WindowID p_window_id) {
Expand All @@ -133,18 +134,6 @@
current_window = p_window_id;
}

void GLManagerLegacy_MacOS::make_current() {
if (current_window == DisplayServer::INVALID_WINDOW_ID) {
return;
}
if (!windows.has(current_window)) {
return;
}

GLWindow &win = windows[current_window];
[win.context makeCurrentContext];
}

void GLManagerLegacy_MacOS::swap_buffers() {
GLWindow &win = windows[current_window];
[win.context flushBuffer];
Expand Down
17 changes: 12 additions & 5 deletions platform/windows/display_server_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2962,7 +2962,7 @@ String DisplayServerWindows::keyboard_get_layout_name(int p_index) const {
}

void DisplayServerWindows::process_events() {
_THREAD_SAFE_METHOD_
_THREAD_SAFE_LOCK_

MSG msg;

Expand All @@ -2977,7 +2977,10 @@ void DisplayServerWindows::process_events() {

if (!drop_events) {
_process_key_events();
_THREAD_SAFE_UNLOCK_
Input::get_singleton()->flush_buffered_events();
} else {
_THREAD_SAFE_UNLOCK_
}
}

Expand All @@ -2990,9 +2993,14 @@ void DisplayServerWindows::force_process_and_drop_events() {
}

void DisplayServerWindows::release_rendering_thread() {
}

void DisplayServerWindows::make_rendering_thread() {
#if defined(GLES3_ENABLED)
if (gl_manager_angle) {
gl_manager_angle->release_current();
}
if (gl_manager_native) {
gl_manager_native->release_current();
}
#endif
}

void DisplayServerWindows::swap_buffers() {
Expand Down Expand Up @@ -3433,7 +3441,6 @@ void DisplayServerWindows::_dispatch_input_events(const Ref<InputEvent> &p_event
}

void DisplayServerWindows::_dispatch_input_event(const Ref<InputEvent> &p_event) {
_THREAD_SAFE_METHOD_
if (in_dispatch_input_event) {
return;
}
Expand Down
1 change: 0 additions & 1 deletion platform/windows/display_server_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,6 @@ class DisplayServerWindows : public DisplayServer {
virtual void force_process_and_drop_events() override;

virtual void release_rendering_thread() override;
virtual void make_rendering_thread() override;
virtual void swap_buffers() override;

virtual void set_native_icon(const String &p_filename) override;
Expand Down
24 changes: 3 additions & 21 deletions platform/windows/gl_manager_windows_native.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,10 +427,6 @@ Error GLManagerNative_Windows::window_create(DisplayServer::WindowID p_window_id
return OK;
}

void GLManagerNative_Windows::_internal_set_current_window(GLWindow *p_win) {
_current_window = p_win;
}

void GLManagerNative_Windows::window_destroy(DisplayServer::WindowID p_window_id) {
GLWindow &win = get_window(p_window_id);
if (_current_window == &win) {
Expand All @@ -447,6 +443,8 @@ void GLManagerNative_Windows::release_current() {
if (!gd_wglMakeCurrent(_current_window->hDC, nullptr)) {
ERR_PRINT("Could not detach OpenGL context from window marked current: " + format_error_message(GetLastError()));
}

_current_window = nullptr;
}

void GLManagerNative_Windows::window_make_current(DisplayServer::WindowID p_window_id) {
Expand All @@ -467,17 +465,7 @@ void GLManagerNative_Windows::window_make_current(DisplayServer::WindowID p_wind
ERR_PRINT("Could not switch OpenGL context to other window: " + format_error_message(GetLastError()));
}

_internal_set_current_window(&win);
}

void GLManagerNative_Windows::make_current() {
if (!_current_window) {
return;
}
const GLDisplay &disp = get_current_display();
if (!gd_wglMakeCurrent(_current_window->hDC, disp.hRC)) {
ERR_PRINT("Could not switch OpenGL context to window marked current: " + format_error_message(GetLastError()));
}
_current_window = &win;
}

void GLManagerNative_Windows::swap_buffers() {
Expand All @@ -491,7 +479,6 @@ Error GLManagerNative_Windows::initialize() {

void GLManagerNative_Windows::set_use_vsync(DisplayServer::WindowID p_window_id, bool p_use) {
GLWindow &win = get_window(p_window_id);
GLWindow *current = _current_window;

if (&win != _current_window) {
window_make_current(p_window_id);
Expand All @@ -506,11 +493,6 @@ void GLManagerNative_Windows::set_use_vsync(DisplayServer::WindowID p_window_id,
} else {
WARN_PRINT("Could not set V-Sync mode. V-Sync is not supported.");
}

if (current != _current_window) {
_current_window = current;
make_current();
}
}

bool GLManagerNative_Windows::is_using_vsync(DisplayServer::WindowID p_window_id) const {
Expand Down
4 changes: 0 additions & 4 deletions platform/windows/gl_manager_windows_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,6 @@ class GLManagerNative_Windows {

PFNWGLSWAPINTERVALEXTPROC wglSwapIntervalEXT = nullptr;

// funcs
void _internal_set_current_window(GLWindow *p_win);

GLWindow &get_window(unsigned int id) { return _windows[id]; }
const GLWindow &get_window(unsigned int id) const { return _windows[id]; }

Expand All @@ -91,7 +88,6 @@ class GLManagerNative_Windows {
void window_resize(DisplayServer::WindowID p_window_id, int p_width, int p_height) {}

void release_current();
void make_current();
void swap_buffers();

void window_make_current(DisplayServer::WindowID p_window_id);
Expand Down
1 change: 1 addition & 0 deletions scene/main/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1579,6 +1579,7 @@ bool Window::_can_consume_input_events() const {
}

void Window::_window_input(const Ref<InputEvent> &p_ev) {
ERR_MAIN_THREAD_GUARD;
if (EngineDebugger::is_active()) {
// Quit from game window using the stop shortcut (F8 by default).
// The custom shortcut is provided via environment variable when running from the editor.
Expand Down
4 changes: 0 additions & 4 deletions servers/display_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,10 +697,6 @@ void DisplayServer::release_rendering_thread() {
WARN_PRINT("Rendering thread not supported by this display server.");
}

void DisplayServer::make_rendering_thread() {
WARN_PRINT("Rendering thread not supported by this display server.");
}

void DisplayServer::swap_buffers() {
WARN_PRINT("Swap buffers not supported by this display server.");
}
Expand Down
1 change: 0 additions & 1 deletion servers/display_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,6 @@ class DisplayServer : public Object {
virtual void force_process_and_drop_events();

virtual void release_rendering_thread();
virtual void make_rendering_thread();
virtual void swap_buffers();

virtual void set_native_icon(const String &p_filename);
Expand Down
1 change: 0 additions & 1 deletion servers/physics_server_2d_wrap_mt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ void PhysicsServer2DWrapMT::end_sync() {

void PhysicsServer2DWrapMT::init() {
if (create_thread) {
//OS::get_singleton()->release_rendering_thread();
thread.start(_thread_callback, this);
while (!step_thread_up.is_set()) {
OS::get_singleton()->delay_usec(1000);
Expand Down
1 change: 0 additions & 1 deletion servers/physics_server_3d_wrap_mt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ void PhysicsServer3DWrapMT::end_sync() {

void PhysicsServer3DWrapMT::init() {
if (create_thread) {
//OS::get_singleton()->release_rendering_thread();
thread.start(_thread_callback, this);
while (!step_thread_up) {
OS::get_singleton()->delay_usec(1000);
Expand Down
Loading

0 comments on commit c28f590

Please sign in to comment.