Skip to content

Commit

Permalink
wayland: HiDPI support
Browse files Browse the repository at this point in the history
  • Loading branch information
dos1 committed Jun 11, 2019
1 parent 09142eb commit 797b281
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 21 deletions.
39 changes: 32 additions & 7 deletions src/video/wayland/SDL_waylandopengles.c
Expand Up @@ -71,16 +71,24 @@ Wayland_GLES_SwapWindow(_THIS, SDL_Window *window)

// Wayland-EGL forbids drawing calls in-between SwapBuffers and wl_egl_window_resize
if (data->resize.pending) {
if (data->scale_factor != data->resize.scale_factor) {
window->w = 0;
window->h = 0;
}
SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, data->resize.width, data->resize.height);
window->w = data->resize.width;
window->h = data->resize.height;

WAYLAND_wl_egl_window_resize(data->egl_window, window->w, window->h, 0, 0);

if (data->waylandData->shell.xdg) {
xdg_surface_ack_configure(data->shell_surface.xdg.surface, data->resize.serial);
} else if (data->waylandData->shell.zxdg) {
zxdg_surface_v6_ack_configure(data->shell_surface.zxdg.surface, data->resize.serial);
data->scale_factor = data->resize.scale_factor;
wl_surface_set_buffer_scale(data->surface, data->scale_factor);
WAYLAND_wl_egl_window_resize(data->egl_window, window->w * data->scale_factor, window->h * data->scale_factor, 0, 0);

if (data->resize.configure) {
if (data->waylandData->shell.xdg) {
xdg_surface_ack_configure(data->shell_surface.xdg.surface, data->resize.serial);
} else if (data->waylandData->shell.zxdg) {
zxdg_surface_v6_ack_configure(data->shell_surface.zxdg.surface, data->resize.serial);
}
data->resize.configure = SDL_FALSE;
}

region = wl_compositor_create_region(data->waylandData->compositor);
Expand Down Expand Up @@ -113,6 +121,23 @@ Wayland_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context)
return ret;
}

void
Wayland_GLES_GetDrawableSize(_THIS, SDL_Window * window, int * w, int * h)
{
SDL_WindowData *data;
if (window->driverdata) {
data = (SDL_WindowData *) window->driverdata;

if (w) {
*w = window->w * data->scale_factor;
}

if (h) {
*h = window->h * data->scale_factor;
}
}
}

void
Wayland_GLES_DeleteContext(_THIS, SDL_GLContext context)
{
Expand Down
1 change: 1 addition & 0 deletions src/video/wayland/SDL_waylandopengles.h
Expand Up @@ -42,6 +42,7 @@ extern int Wayland_GLES_LoadLibrary(_THIS, const char *path);
extern SDL_GLContext Wayland_GLES_CreateContext(_THIS, SDL_Window * window);
extern int Wayland_GLES_SwapWindow(_THIS, SDL_Window * window);
extern int Wayland_GLES_MakeCurrent(_THIS, SDL_Window * window, SDL_GLContext context);
extern void Wayland_GLES_GetDrawableSize(_THIS, SDL_Window * window, int * w, int * h);
extern void Wayland_GLES_DeleteContext(_THIS, SDL_GLContext context);

#endif /* SDL_waylandopengles_h_ */
Expand Down
23 changes: 17 additions & 6 deletions src/video/wayland/SDL_waylandvideo.c
Expand Up @@ -172,6 +172,7 @@ Wayland_CreateDevice(int devindex)
device->GL_SwapWindow = Wayland_GLES_SwapWindow;
device->GL_GetSwapInterval = Wayland_GLES_GetSwapInterval;
device->GL_SetSwapInterval = Wayland_GLES_SetSwapInterval;
device->GL_GetDrawableSize = Wayland_GLES_GetDrawableSize;
device->GL_MakeCurrent = Wayland_GLES_MakeCurrent;
device->GL_CreateContext = Wayland_GLES_CreateContext;
device->GL_LoadLibrary = Wayland_GLES_LoadLibrary;
Expand Down Expand Up @@ -199,6 +200,7 @@ Wayland_CreateDevice(int devindex)
device->Vulkan_UnloadLibrary = Wayland_Vulkan_UnloadLibrary;
device->Vulkan_GetInstanceExtensions = Wayland_Vulkan_GetInstanceExtensions;
device->Vulkan_CreateSurface = Wayland_Vulkan_CreateSurface;
device->Vulkan_GetDrawableSize = Wayland_Vulkan_GetDrawableSize;
#endif

device->free = Wayland_DeleteDevice;
Expand Down Expand Up @@ -226,7 +228,6 @@ display_handle_geometry(void *data,
SDL_VideoDisplay *display = data;

display->name = SDL_strdup(model);
display->driverdata = output;
}

static void
Expand All @@ -237,15 +238,15 @@ display_handle_mode(void *data,
int height,
int refresh)
{
SDL_VideoDisplay *display = data;
SDL_DisplayMode mode;
SDL_VideoDisplay *display = data;

SDL_zero(mode);
mode.format = SDL_PIXELFORMAT_RGB888;
mode.w = width;
mode.h = height;
mode.refresh_rate = refresh / 1000; // mHz to Hz
mode.driverdata = display->driverdata;
mode.driverdata = ((SDL_WaylandOutputData*)display->driverdata)->output;
SDL_AddDisplayMode(display, &mode);

if (flags & WL_OUTPUT_MODE_CURRENT) {
Expand All @@ -258,8 +259,10 @@ static void
display_handle_done(void *data,
struct wl_output *output)
{
/* !!! FIXME: this will fail on any further property changes! */
SDL_VideoDisplay *display = data;
SDL_AddVideoDisplay(display);
wl_output_set_user_data(output, display->driverdata);
SDL_free(display->name);
SDL_free(display);
}
Expand All @@ -269,7 +272,8 @@ display_handle_scale(void *data,
struct wl_output *output,
int32_t factor)
{
// TODO: do HiDPI stuff.
SDL_VideoDisplay *display = data;
((SDL_WaylandOutputData*)display->driverdata)->scale_factor = factor;
}

static const struct wl_output_listener output_listener = {
Expand All @@ -283,6 +287,7 @@ static void
Wayland_add_display(SDL_VideoData *d, uint32_t id)
{
struct wl_output *output;
SDL_WaylandOutputData *data;
SDL_VideoDisplay *display = SDL_malloc(sizeof *display);
if (!display) {
SDL_OutOfMemory();
Expand All @@ -296,6 +301,10 @@ Wayland_add_display(SDL_VideoData *d, uint32_t id)
SDL_free(display);
return;
}
data = SDL_malloc(sizeof *data);
data->output = output;
data->scale_factor = 1.0;
display->driverdata = data;

wl_output_add_listener(output, &output_listener, display);
}
Expand Down Expand Up @@ -351,7 +360,7 @@ display_handle_global(void *data, struct wl_registry *registry, uint32_t id,
/*printf("WAYLAND INTERFACE: %s\n", interface);*/

if (strcmp(interface, "wl_compositor") == 0) {
d->compositor = wl_registry_bind(d->registry, id, &wl_compositor_interface, 1);
d->compositor = wl_registry_bind(d->registry, id, &wl_compositor_interface, SDL_min(3, version));
} else if (strcmp(interface, "wl_output") == 0) {
Wayland_add_display(d, id);
} else if (strcmp(interface, "wl_seat") == 0) {
Expand Down Expand Up @@ -466,7 +475,9 @@ Wayland_VideoQuit(_THIS)

for (i = 0; i < _this->num_displays; ++i) {
SDL_VideoDisplay *display = &_this->displays[i];
wl_output_destroy(display->driverdata);

wl_output_destroy(((SDL_WaylandOutputData*)display->driverdata)->output);
SDL_free(display->driverdata);
display->driverdata = NULL;

for (j = display->num_display_modes; j--;) {
Expand Down
5 changes: 5 additions & 0 deletions src/video/wayland/SDL_waylandvideo.h
Expand Up @@ -82,6 +82,11 @@ typedef struct {
int relative_mouse_mode;
} SDL_VideoData;

typedef struct {
struct wl_output *output;
float scale_factor;
} SDL_WaylandOutputData;

#endif /* SDL_waylandvideo_h_ */

/* vi: set ts=4 sw=4 expandtab: */
16 changes: 16 additions & 0 deletions src/video/wayland/SDL_waylandvulkan.c
Expand Up @@ -127,6 +127,22 @@ SDL_bool Wayland_Vulkan_GetInstanceExtensions(_THIS,
extensionsForWayland);
}

void Wayland_Vulkan_GetDrawableSize(_THIS, SDL_Window *window, int *w, int *h)
{
SDL_WindowData *data;
if (window->driverdata) {
data = (SDL_WindowData *) window->driverdata;

if (w) {
*w = window->w * data->scale_factor;
}

if (h) {
*h = window->h * data->scale_factor;
}
}
}

SDL_bool Wayland_Vulkan_CreateSurface(_THIS,
SDL_Window *window,
VkInstance instance,
Expand Down
1 change: 1 addition & 0 deletions src/video/wayland/SDL_waylandvulkan.h
Expand Up @@ -40,6 +40,7 @@ SDL_bool Wayland_Vulkan_GetInstanceExtensions(_THIS,
SDL_Window *window,
unsigned *count,
const char **names);
void Wayland_Vulkan_GetDrawableSize(_THIS, SDL_Window *window, int *w, int *h);
SDL_bool Wayland_Vulkan_CreateSurface(_THIS,
SDL_Window *window,
VkInstance instance,
Expand Down

0 comments on commit 797b281

Please sign in to comment.