wayland: Add dedupe logic to window geometry configuration

Adds deduplication logic to ConfigureWindowGeometry() to avoid setting redundant backbuffer, viewport and surface opaque region dimensions. State is now only set when the window and/or backbuffer dimensions change.

This repurposes the viewport rect to always hold the actual size of the window, which can differ from the SDL size if things are being scaled. The SDL_Rect was removed in favor of two ints, as the x/y members of the struct were never used, so they just wasted space.

Since the internal variables always have the true window size, the width/height getter functions are no longer required and can be removed.
main
Frank Praznik 2022-10-01 11:31:26 -04:00 committed by Ethan Lee
parent f687cbd4c6
commit a8cb7bbe2f
2 changed files with 61 additions and 78 deletions

View File

@ -164,21 +164,6 @@ NeedViewport(SDL_Window *window)
return SDL_FALSE; return SDL_FALSE;
} }
/* If a viewport is active, use the width and height for the window dimensions. */
SDL_FORCE_INLINE int
GetWindowWidth(SDL_Window *window)
{
SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
return SDL_RectEmpty(&data->viewport_rect) ? window->w : data->viewport_rect.w;
}
SDL_FORCE_INLINE int
GetWindowHeight(SDL_Window *window)
{
SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
return SDL_RectEmpty(&data->viewport_rect) ? window->h : data->viewport_rect.h;
}
static void static void
GetBufferSize(SDL_Window *window, int *width, int *height) GetBufferSize(SDL_Window *window, int *width, int *height)
{ {
@ -247,11 +232,16 @@ ConfigureWindowGeometry(SDL_Window *window)
SDL_VideoData *viddata = data->waylandData; SDL_VideoData *viddata = data->waylandData;
SDL_WaylandOutputData *output = (SDL_WaylandOutputData *)SDL_GetDisplayForWindow(window)->driverdata; SDL_WaylandOutputData *output = (SDL_WaylandOutputData *)SDL_GetDisplayForWindow(window)->driverdata;
struct wl_region *region; struct wl_region *region;
const int old_dw = data->drawable_width;
const int old_dh = data->drawable_height;
SDL_bool window_size_changed;
SDL_bool drawable_size_changed;
/* Set the drawable backbuffer size. */ /* Set the drawable backbuffer size. */
GetBufferSize(window, &data->drawable_width, &data->drawable_height); GetBufferSize(window, &data->drawable_width, &data->drawable_height);
drawable_size_changed = data->drawable_width != old_dw || data->drawable_height != old_dh;
if (data->egl_window) { if (data->egl_window && drawable_size_changed) {
WAYLAND_wl_egl_window_resize(data->egl_window, WAYLAND_wl_egl_window_resize(data->egl_window,
data->drawable_width, data->drawable_width,
data->drawable_height, data->drawable_height,
@ -260,58 +250,63 @@ ConfigureWindowGeometry(SDL_Window *window)
if (FullscreenModeEmulation(window) && NeedViewport(window)) { if (FullscreenModeEmulation(window) && NeedViewport(window)) {
int fs_width, fs_height; int fs_width, fs_height;
int src_width, src_height;
const int output_width = data->fs_output_width ? data->fs_output_width : output->width; const int output_width = data->fs_output_width ? data->fs_output_width : output->width;
const int output_height = data->fs_output_height ? data->fs_output_height : output->height; const int output_height = data->fs_output_height ? data->fs_output_height : output->height;
GetFullScreenDimensions(window, &fs_width, &fs_height, &src_width, &src_height); window_size_changed = data->window_width != output_width || data->window_height != output_height;
/* Set the buffer scale to 1 since a viewport will be used. */ if (window_size_changed || drawable_size_changed) {
wl_surface_set_buffer_scale(data->surface, 1); GetFullScreenDimensions(window, &fs_width, &fs_height, NULL, NULL);
SetDrawSurfaceViewport(window, src_width, src_height, output_width, output_height);
data->viewport_rect.x = 0; /* Set the buffer scale to 1 since a viewport will be used. */
data->viewport_rect.y = 0; wl_surface_set_buffer_scale(data->surface, 1);
data->viewport_rect.w = output_width; SetDrawSurfaceViewport(window, data->drawable_width, data->drawable_height,
data->viewport_rect.h = output_height; output_width, output_height);
data->pointer_scale_x = (float)fs_width / (float)output_width; data->window_width = output_width;
data->pointer_scale_y = (float)fs_height / (float)output_height; data->window_height = output_height;
if (!EGLTransparencyEnabled()) { data->pointer_scale_x = (float) fs_width / (float) output_width;
region = wl_compositor_create_region(viddata->compositor); data->pointer_scale_y = (float) fs_height / (float) output_height;
wl_region_add(region, data->viewport_rect.x, data->viewport_rect.y,
data->viewport_rect.w, data->viewport_rect.h);
wl_surface_set_opaque_region(data->surface, region);
wl_region_destroy(region);
} }
} else { } else {
if (NeedViewport(window)) { window_size_changed = data->window_width != window->w || data->window_height != window->h;
wl_surface_set_buffer_scale(data->surface, 1);
SetDrawSurfaceViewport(window, data->drawable_width, data->drawable_height, window->w, window->h);
} else {
UnsetDrawSurfaceViewport(window);
/* Round to the next integer in case of a fractional value. */ if (window_size_changed || drawable_size_changed) {
wl_surface_set_buffer_scale(data->surface, (int32_t)SDL_ceilf(data->scale_factor)); if (NeedViewport(window)) {
} wl_surface_set_buffer_scale(data->surface, 1);
SetDrawSurfaceViewport(window, data->drawable_width, data->drawable_height, window->w, window->h);
} else {
UnsetDrawSurfaceViewport(window);
SDL_zero(data->viewport_rect); /* Round to the next integer in case of a fractional value. */
wl_surface_set_buffer_scale(data->surface, (int32_t) SDL_ceilf(data->scale_factor));
}
data->pointer_scale_x = 1.0f; data->window_width = window->w;
data->pointer_scale_y = 1.0f; data->window_height = window->h;
if (!EGLTransparencyEnabled()) { data->pointer_scale_x = 1.0f;
region = wl_compositor_create_region(viddata->compositor); data->pointer_scale_y = 1.0f;
wl_region_add(region, 0, 0, window->w, window->h);
wl_surface_set_opaque_region(data->surface, region);
wl_region_destroy(region);
} }
} }
/* Recreate the pointer confinement region when the window geometry changes. */ /*
if (data->confined_pointer) { * The opaque and pointer confinement regions only need to be recalculated
Wayland_input_confine_pointer(viddata->input, window); * if the output size has changed.
*/
if (window_size_changed) {
if (!EGLTransparencyEnabled()) {
region = wl_compositor_create_region(viddata->compositor);
wl_region_add(region, 0, 0,
data->window_width, data->window_height);
wl_surface_set_opaque_region(data->surface, region);
wl_region_destroy(region);
}
if (data->confined_pointer) {
Wayland_input_confine_pointer(viddata->input, window);
}
} }
} }
@ -361,7 +356,7 @@ SetMinMaxDimensions(SDL_Window *window, SDL_bool commit)
max_height); max_height);
if (commit) { if (commit) {
struct libdecor_state *state = libdecor_state_new(GetWindowWidth(window), GetWindowHeight(window)); struct libdecor_state *state = libdecor_state_new(wind->window_width, wind->window_height);
libdecor_frame_commit(wind->shell_surface.libdecor.frame, state, NULL); libdecor_frame_commit(wind->shell_surface.libdecor.frame, state, NULL);
libdecor_state_free(state); libdecor_state_free(state);
wl_surface_commit(wind->surface); wl_surface_commit(wind->surface);
@ -415,7 +410,7 @@ SetFullscreen(SDL_Window *window, struct wl_output *output)
libdecor_frame_set_capabilities(wind->shell_surface.libdecor.frame, LIBDECOR_ACTION_RESIZE); libdecor_frame_set_capabilities(wind->shell_surface.libdecor.frame, LIBDECOR_ACTION_RESIZE);
wl_surface_commit(wind->surface); wl_surface_commit(wind->surface);
} else { } else {
struct libdecor_state *state = libdecor_state_new(GetWindowWidth(window), GetWindowHeight(window)); struct libdecor_state *state = libdecor_state_new(wind->window_width, wind->window_height);
libdecor_frame_commit(wind->shell_surface.libdecor.frame, state, NULL); libdecor_frame_commit(wind->shell_surface.libdecor.frame, state, NULL);
libdecor_state_free(state); libdecor_state_free(state);
wl_surface_commit(wind->surface); wl_surface_commit(wind->surface);
@ -430,7 +425,7 @@ SetFullscreen(SDL_Window *window, struct wl_output *output)
libdecor_frame_unset_capabilities(wind->shell_surface.libdecor.frame, LIBDECOR_ACTION_RESIZE); libdecor_frame_unset_capabilities(wind->shell_surface.libdecor.frame, LIBDECOR_ACTION_RESIZE);
wl_surface_commit(wind->surface); wl_surface_commit(wind->surface);
} else { } else {
struct libdecor_state *state = libdecor_state_new(GetWindowWidth(window), GetWindowHeight(window)); struct libdecor_state *state = libdecor_state_new(wind->window_width, wind->window_height);
libdecor_frame_commit(wind->shell_surface.libdecor.frame, state, NULL); libdecor_frame_commit(wind->shell_surface.libdecor.frame, state, NULL);
libdecor_state_free(state); libdecor_state_free(state);
wl_surface_commit(wind->surface); wl_surface_commit(wind->surface);
@ -509,14 +504,14 @@ CommitWindowGeometry(SDL_Window *window)
#ifdef HAVE_LIBDECOR_H #ifdef HAVE_LIBDECOR_H
if (WINDOW_IS_LIBDECOR(data, window) && wind->shell_surface.libdecor.frame) { if (WINDOW_IS_LIBDECOR(data, window) && wind->shell_surface.libdecor.frame) {
struct libdecor_state *state = libdecor_state_new(GetWindowWidth(window), GetWindowHeight(window)); struct libdecor_state *state = libdecor_state_new(wind->window_width, wind->window_height);
libdecor_frame_commit(wind->shell_surface.libdecor.frame, state, NULL); libdecor_frame_commit(wind->shell_surface.libdecor.frame, state, NULL);
libdecor_state_free(state); libdecor_state_free(state);
} else } else
#endif #endif
if (viddata->shell.xdg && wind->shell_surface.xdg.surface) { if (viddata->shell.xdg && wind->shell_surface.xdg.surface) {
xdg_surface_set_window_geometry(wind->shell_surface.xdg.surface, 0, 0, xdg_surface_set_window_geometry(wind->shell_surface.xdg.surface, 0, 0,
GetWindowWidth(window), GetWindowHeight(window)); wind->window_width, wind->window_height);
} }
} }
@ -527,11 +522,9 @@ surface_damage_frame_done(void *data, struct wl_callback *cb, uint32_t time)
{ {
SDL_WindowData *wind = (SDL_WindowData *)data; SDL_WindowData *wind = (SDL_WindowData *)data;
/* Manually set the damage region when using a viewport. */ /* Set the damage region. */
if (!SDL_RectEmpty(&wind->viewport_rect)) { wl_surface_damage(wind->surface, 0, 0,
wl_surface_damage(wind->surface, wind->viewport_rect.x, wind->viewport_rect.y, wind->window_width, wind->window_height);
wind->viewport_rect.w, wind->viewport_rect.h);
}
wl_callback_destroy(cb); wl_callback_destroy(cb);
wind->surface_damage_frame_callback = wl_surface_frame(wind->surface); wind->surface_damage_frame_callback = wl_surface_frame(wind->surface);
@ -933,7 +926,7 @@ decoration_frame_configure(struct libdecor_frame *frame,
wind->shell_surface.libdecor.initial_configure_seen = SDL_TRUE; wind->shell_surface.libdecor.initial_configure_seen = SDL_TRUE;
/* ... then commit the changes on the libdecor side. */ /* ... then commit the changes on the libdecor side. */
state = libdecor_state_new(GetWindowWidth(window), GetWindowHeight(window)); state = libdecor_state_new(wind->window_width, wind->window_height);
libdecor_frame_commit(frame, state, configuration); libdecor_frame_commit(frame, state, configuration);
libdecor_state_free(state); libdecor_state_free(state);
@ -1971,7 +1964,6 @@ int Wayland_CreateWindow(_THIS, SDL_Window *window)
{ {
SDL_WindowData *data; SDL_WindowData *data;
SDL_VideoData *c; SDL_VideoData *c;
struct wl_region *region;
data = SDL_calloc(1, sizeof *data); data = SDL_calloc(1, sizeof *data);
if (data == NULL) if (data == NULL)
@ -1998,8 +1990,6 @@ int Wayland_CreateWindow(_THIS, SDL_Window *window)
data->sdlwindow = window; data->sdlwindow = window;
data->scale_factor = 1.0f; data->scale_factor = 1.0f;
data->pointer_scale_x = 1.0f;
data->pointer_scale_y = 1.0f;
if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) { if (window->flags & SDL_WINDOW_ALLOW_HIGHDPI) {
int i; int i;
@ -2021,6 +2011,9 @@ int Wayland_CreateWindow(_THIS, SDL_Window *window)
SDL_WAYLAND_register_surface(data->surface); SDL_WAYLAND_register_surface(data->surface);
/* Must be called before EGL configuration to set the drawable backbuffer size. */
ConfigureWindowGeometry(window);
/* Fire a callback when the compositor wants a new frame rendered. /* Fire a callback when the compositor wants a new frame rendered.
* Right now this only matters for OpenGL; we use this callback to add a * Right now this only matters for OpenGL; we use this callback to add a
* wait timeout that avoids getting deadlocked by the compositor when the * wait timeout that avoids getting deadlocked by the compositor when the
@ -2048,9 +2041,6 @@ int Wayland_CreateWindow(_THIS, SDL_Window *window)
} }
#endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */ #endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */
data->drawable_width = SDL_lroundf(window->w * data->scale_factor);
data->drawable_height = SDL_lroundf(window->h * data->scale_factor);
if (window->flags & SDL_WINDOW_OPENGL) { if (window->flags & SDL_WINDOW_OPENGL) {
data->egl_window = WAYLAND_wl_egl_window_create(data->surface, data->drawable_width, data->drawable_height); data->egl_window = WAYLAND_wl_egl_window_create(data->surface, data->drawable_width, data->drawable_height);
@ -2072,13 +2062,6 @@ int Wayland_CreateWindow(_THIS, SDL_Window *window)
} }
#endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */ #endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */
if (!EGLTransparencyEnabled()) {
region = wl_compositor_create_region(c->compositor);
wl_region_add(region, 0, 0, window->w, window->h);
wl_surface_set_opaque_region(data->surface, region);
wl_region_destroy(region);
}
if (c->relative_mouse_mode) { if (c->relative_mouse_mode) {
Wayland_input_lock_pointer(c->input); Wayland_input_lock_pointer(c->input);
} }
@ -2149,7 +2132,7 @@ Wayland_HandleResize(SDL_Window *window, int width, int height, float scale)
viddata->shell.xdg && viddata->shell.xdg &&
data->shell_surface.xdg.surface) { data->shell_surface.xdg.surface) {
xdg_surface_set_window_geometry(data->shell_surface.xdg.surface, 0, 0, xdg_surface_set_window_geometry(data->shell_surface.xdg.surface, 0, 0,
GetWindowWidth(window), GetWindowHeight(window)); data->window_width, data->window_height);
} }
} }

View File

@ -105,7 +105,7 @@ typedef struct {
float pointer_scale_y; float pointer_scale_y;
int drawable_width, drawable_height; int drawable_width, drawable_height;
int fs_output_width, fs_output_height; int fs_output_width, fs_output_height;
SDL_Rect viewport_rect; int window_width, window_height;
SDL_bool needs_resize_event; SDL_bool needs_resize_event;
SDL_bool floating_resize_pending; SDL_bool floating_resize_pending;
SDL_bool is_fullscreen; SDL_bool is_fullscreen;