Remove need for context switching

Call wl_egl_window_resize just buffer swapping buffers at which point
the context is already correct.

Also might workaround bugs in the NVIDIA driver: https://github.com/NVIDIA/egl-wayland/issues/52
This commit is contained in:
Kovid Goyal
2024-04-24 09:27:02 +05:30
parent b7e22a357f
commit 72272ab4fe
4 changed files with 15 additions and 9 deletions

3
glfw/context.c vendored
View File

@@ -476,6 +476,9 @@ GLFWAPI void glfwSwapBuffers(GLFWwindow* handle)
return;
}
#ifdef _GLFW_WAYLAND
_glfwWaylandBeforeBufferSwap(window);
#endif
window->context.swapBuffers(window);
#ifdef _GLFW_WAYLAND
_glfwWaylandAfterBufferSwap(window);

4
glfw/wl_platform.h vendored
View File

@@ -185,6 +185,9 @@ typedef struct _GLFWwindowWayland
struct zwlr_layer_surface_v1* zwlr_layer_surface_v1;
} layer_shell;
struct {
int width, height;
} framebuffer_size_at_last_resize;
/* information about axis events on current frame */
struct
{
@@ -420,6 +423,7 @@ typedef struct _GLFWcursorWayland
void _glfwAddOutputWayland(uint32_t name, uint32_t version);
void _glfwWaylandBeforeBufferSwap(_GLFWwindow *window);
void _glfwWaylandAfterBufferSwap(_GLFWwindow *window);
void _glfwSetupWaylandDataDevice(void);
void _glfwSetupWaylandPrimarySelectionDevice(void);

13
glfw/wl_window.c vendored
View File

@@ -356,24 +356,25 @@ _glfwWaylandWindowScale(_GLFWwindow *window) {
return ans;
}
typedef struct with_context { _GLFWwindow *current, *new; } with_context;
static inline void with_context_release(with_context *c) { if (c->current != c->new) glfwMakeContextCurrent((GLFWwindow*)c->current); }
#define RAII_window_context(window) with_context __wc __attribute__((cleanup(with_context_release))) = { .current=(_GLFWwindow*)glfwGetCurrentContext(), .new=window }; if (__wc.current != __wc.new) glfwMakeContextCurrent((GLFWwindow*)__wc.new);
static void
resizeFramebuffer(_GLFWwindow* window) {
RAII_window_context(window); // ensure wl_egl_window_resize called with correct context
double scale = _glfwWaylandWindowScale(window);
int scaled_width = (int)round(window->wl.width * scale);
int scaled_height = (int)round(window->wl.height * scale);
debug("Resizing framebuffer of window: %llu to: %dx%d window size: %dx%d at scale: %.3f\n",
window->id, scaled_width, scaled_height, window->wl.width, window->wl.height, scale);
wl_egl_window_resize(window->wl.native, scaled_width, scaled_height, 0, 0);
update_regions(window);
window->wl.waiting_for_swap_to_commit = true;
window->wl.framebuffer_size_at_last_resize.width = scaled_width;
window->wl.framebuffer_size_at_last_resize.height = scaled_height;
_glfwInputFramebufferSize(window, scaled_width, scaled_height);
}
void
_glfwWaylandBeforeBufferSwap(_GLFWwindow* window) {
wl_egl_window_resize(window->wl.native, window->wl.framebuffer_size_at_last_resize.width, window->wl.framebuffer_size_at_last_resize.height, 0, 0);
}
void
_glfwWaylandAfterBufferSwap(_GLFWwindow* window) {
if (window->wl.temp_buffer_used_during_window_creation) {

View File

@@ -1191,9 +1191,7 @@ create_os_window(PyObject UNUSED *self, PyObject *args, PyObject *kw) {
if (temp_window) { glfwDestroyWindow(temp_window); temp_window = NULL; }
if (glfw_window == NULL) { PyErr_SetString(PyExc_ValueError, "Failed to create GLFWwindow"); return NULL; }
glfwMakeContextCurrent(glfw_window);
if (is_first_window) {
gl_init();
}
if (is_first_window) gl_init();
// Will make the GPU automatically apply SRGB gamma curve on the resulting framebuffer
glEnable(GL_FRAMEBUFFER_SRGB);
bool is_semi_transparent = glfwGetWindowAttrib(glfw_window, GLFW_TRANSPARENT_FRAMEBUFFER);