From 88640622ebff372cff112c479abb0708eb69b6d2 Mon Sep 17 00:00:00 2001 From: Sunderland93 Date: Sat, 28 Dec 2024 15:33:21 +0400 Subject: [PATCH] wayland: add support for single-pixel-buffer-v1 protocol --- .gitignore | 2 + Makefile.common | 3 +- .../staging/single-pixel-buffer/README | 4 + .../single-pixel-buffer-v1.xml | 69 ++++++++++++++++ gfx/common/wayland/generate_wayland_protos.sh | 3 +- gfx/common/wayland_common.c | 82 ++++++++++++------- input/common/wayland_common.c | 4 + input/common/wayland_common.h | 8 +- 8 files changed, 139 insertions(+), 36 deletions(-) create mode 100644 deps/wayland-protocols/staging/single-pixel-buffer/README create mode 100644 deps/wayland-protocols/staging/single-pixel-buffer/single-pixel-buffer-v1.xml diff --git a/.gitignore b/.gitignore index 84cfed500c9e..d3470d8fa617 100644 --- a/.gitignore +++ b/.gitignore @@ -237,6 +237,8 @@ gfx/common/wayland/tablet-unstable-v2.h gfx/common/wayland/tablet-unstable-v2.c gfx/common/wayland/content-type-v1.h gfx/common/wayland/content-type-v1.c +gfx/common/wayland/single-pixel-buffer-v1.h +gfx/common/wayland/single-pixel-buffer-v1.c # libretro-common samples libretro-common/samples/streams/rzip/rzip diff --git a/Makefile.common b/Makefile.common index 70914d4b5cce..01bae782ee5a 100644 --- a/Makefile.common +++ b/Makefile.common @@ -1278,7 +1278,8 @@ ifeq ($(HAVE_WAYLAND), 1) gfx/common/wayland/relative-pointer-unstable-v1.o \ gfx/common/wayland/cursor-shape-v1.o \ gfx/common/wayland/tablet-unstable-v2.o \ - gfx/common/wayland/content-type-v1.o + gfx/common/wayland/content-type-v1.o \ + gfx/common/wayland/single-pixel-buffer-v1.o ifeq ($(HAVE_VULKAN), 1) OBJ += gfx/drivers_context/wayland_vk_ctx.o diff --git a/deps/wayland-protocols/staging/single-pixel-buffer/README b/deps/wayland-protocols/staging/single-pixel-buffer/README new file mode 100644 index 000000000000..b3a1b8687289 --- /dev/null +++ b/deps/wayland-protocols/staging/single-pixel-buffer/README @@ -0,0 +1,4 @@ +Single-pixel buffer protocol + +Maintainers: +Simon Ser (@emersion) diff --git a/deps/wayland-protocols/staging/single-pixel-buffer/single-pixel-buffer-v1.xml b/deps/wayland-protocols/staging/single-pixel-buffer/single-pixel-buffer-v1.xml new file mode 100644 index 000000000000..b15715578145 --- /dev/null +++ b/deps/wayland-protocols/staging/single-pixel-buffer/single-pixel-buffer-v1.xml @@ -0,0 +1,69 @@ + + + + Copyright © 2022 Simon Ser + + Permission is hereby granted, free of charge, to any person obtaining a + copy of this software and associated documentation files (the "Software"), + to deal in the Software without restriction, including without limitation + the rights to use, copy, modify, merge, publish, distribute, sublicense, + and/or sell copies of the Software, and to permit persons to whom the + Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice (including the next + paragraph) shall be included in all copies or substantial portions of the + Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + DEALINGS IN THE SOFTWARE. + + + + This protocol extension allows clients to create single-pixel buffers. + + Compositors supporting this protocol extension should also support the + viewporter protocol extension. Clients may use viewporter to scale a + single-pixel buffer to a desired size. + + Warning! The protocol described in this file is currently in the testing + phase. Backward compatible changes may be added together with the + corresponding interface version bump. Backward incompatible changes can + only be done by creating a new major version of the extension. + + + + + The wp_single_pixel_buffer_manager_v1 interface is a factory for + single-pixel buffers. + + + + + Destroy the wp_single_pixel_buffer_manager_v1 object. + + The child objects created via this interface are unaffected. + + + + + + Create a single-pixel buffer from four 32-bit RGBA values. + + Unless specified in another protocol extension, the RGBA values use + pre-multiplied alpha. + + The width and height of the buffer are 1. + + + + + + + + + diff --git a/gfx/common/wayland/generate_wayland_protos.sh b/gfx/common/wayland/generate_wayland_protos.sh index a0fc857d424d..c26b3cbbc672 100755 --- a/gfx/common/wayland/generate_wayland_protos.sh +++ b/gfx/common/wayland/generate_wayland_protos.sh @@ -69,6 +69,7 @@ generate_source 'unstable/pointer-constraints' 'pointer-constraints-unstable-v1' generate_source 'unstable/relative-pointer' 'relative-pointer-unstable-v1' generate_source 'staging/fractional-scale' 'fractional-scale-v1' generate_source 'staging/cursor-shape' 'cursor-shape-v1' -# Required by cursor-shape-v1 +# tablet-unstable-v1 is required by cursor-shape-v1 generate_source 'unstable/tablet' 'tablet-unstable-v2' generate_source 'staging/content-type' 'content-type-v1' +generate_source 'staging/single-pixel-buffer' 'single-pixel-buffer-v1' diff --git a/gfx/common/wayland_common.c b/gfx/common/wayland_common.c index e98eed58be8d..8669629d1a1a 100644 --- a/gfx/common/wayland_common.c +++ b/gfx/common/wayland_common.c @@ -319,6 +319,8 @@ void gfx_ctx_wl_destroy_resources_common(gfx_ctx_wayland_data_t *wl) wp_cursor_shape_manager_v1_destroy (wl->cursor_shape_manager); if (wl->cursor_shape_device) wp_cursor_shape_device_v1_destroy (wl->cursor_shape_device); + if (wl->single_pixel_manager) + wp_single_pixel_buffer_manager_v1_destroy (wl->single_pixel_manager); if (wl->seat) wl_seat_destroy(wl->seat); if (wl->xdg_shell) @@ -372,6 +374,7 @@ void gfx_ctx_wl_destroy_resources_common(gfx_ctx_wayland_data_t *wl) wl->cursor_shape_device = NULL; wl->idle_inhibit_manager = NULL; wl->deco_manager = NULL; + wl->single_pixel_manager = NULL; wl->surface = NULL; wl->xdg_surface = NULL; wl->xdg_toplevel = NULL; @@ -505,46 +508,58 @@ static shm_buffer_t *create_shm_buffer(gfx_ctx_wayland_data_t *wl, int width, int height, uint32_t format) { - int fd, ofd; - struct wl_shm_pool *pool = NULL; - void *data = NULL; - shm_buffer_t *buffer = NULL; - int stride = width * 4; - int size = stride * height; + shm_buffer_t *buffer = NULL; - if (size <= 0) - return NULL; - - if ((fd = create_shm_file(size)) < 0) + if (wl->single_pixel_manager) { - RARCH_ERR("[Wayland] [SHM]: Creating a buffer file for %d B failed\n", - size); - return NULL; + buffer = calloc(1, sizeof(*buffer)); + buffer->wl_buffer = wp_single_pixel_buffer_manager_v1_create_u32_rgba_buffer( + wl->single_pixel_manager, 0, 0, 0, UINT32_MAX); /* R, G, B, A */ + return buffer; } - - data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); - if (data == MAP_FAILED) + else { - RARCH_ERR("[Wayland] [SHM]: mmap failed\n"); - close(fd); - return NULL; - } + int fd, ofd; + struct wl_shm_pool *pool = NULL; + void *data = NULL; + shm_buffer_t *buffer = NULL; + int stride = width * 4; + int size = stride * height; + + if (size <= 0) + return NULL; + + if ((fd = create_shm_file(size)) < 0) + { + RARCH_ERR("[Wayland] [SHM]: Creating a buffer file for %d B failed\n", + size); + return NULL; + } - buffer = calloc(1, sizeof *buffer); + data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + if (data == MAP_FAILED) + { + RARCH_ERR("[Wayland] [SHM]: mmap failed\n"); + close(fd); + return NULL; + } - pool = wl_shm_create_pool(wl->shm, fd, size); - buffer->wl_buffer = wl_shm_pool_create_buffer(pool, 0, - width, height, - stride, format); - wl_buffer_add_listener(buffer->wl_buffer, &shm_buffer_listener, buffer); - wl_shm_pool_destroy(pool); + buffer = calloc(1, sizeof *buffer); - close(fd); + pool = wl_shm_create_pool(wl->shm, fd, size); + buffer->wl_buffer = wl_shm_pool_create_buffer(pool, 0, + width, height, + stride, format); + wl_buffer_add_listener(buffer->wl_buffer, &shm_buffer_listener, buffer); + wl_shm_pool_destroy(pool); + + close(fd); - buffer->data = data; - buffer->data_size = size; + buffer->data = data; + buffer->data_size = size; - return buffer; + return buffer; + } } static void shm_buffer_paint_icon( @@ -746,6 +761,11 @@ bool gfx_ctx_wl_init_common( RARCH_LOG("[Wayland]: Compositor doesn't support the %s protocol!\n", zwp_relative_pointer_manager_v1_interface.name); } + if (!wl->single_pixel_manager) + { + RARCH_LOG("[Wayland]: Compositor doesn't support the %s protocol!\n", wp_single_pixel_buffer_manager_v1_interface.name); + } + wl->surface = wl_compositor_create_surface(wl->compositor); if (wl->viewporter) wl->viewport = wp_viewporter_get_viewport(wl->viewporter, wl->surface); diff --git a/input/common/wayland_common.c b/input/common/wayland_common.c index 54893943615d..9f5d27373580 100644 --- a/input/common/wayland_common.c +++ b/input/common/wayland_common.c @@ -802,6 +802,10 @@ static void wl_registry_handle_global(void *data, struct wl_registry *reg, wl->content_type_manager = (struct wp_content_type_manager_v1*) wl_registry_bind( reg, id, &wp_content_type_manager_v1_interface, MIN(version, 1)); + else if (string_is_equal(interface, wp_single_pixel_buffer_manager_v1_interface.name) && found++) + wl->single_pixel_manager = (struct wp_single_pixel_buffer_manager_v1*) + wl_registry_bind( + reg, id, &wp_single_pixel_buffer_manager_v1_interface, MIN(version, 1)); if (found > 1) RARCH_LOG("[Wayland]: Registered interface %s at version %u\n", diff --git a/input/common/wayland_common.h b/input/common/wayland_common.h index 9b7a143f0b13..8ccae5f2fda2 100644 --- a/input/common/wayland_common.h +++ b/input/common/wayland_common.h @@ -37,12 +37,13 @@ #include "../../gfx/common/wayland/content-type-v1.h" #include "../../gfx/common/wayland/cursor-shape-v1.h" #include "../../gfx/common/wayland/fractional-scale-v1.h" -#include "../../gfx/common/wayland/viewporter.h" #include "../../gfx/common/wayland/idle-inhibit-unstable-v1.h" -#include "../../gfx/common/wayland/xdg-shell.h" -#include "../../gfx/common/wayland/xdg-decoration-unstable-v1.h" #include "../../gfx/common/wayland/pointer-constraints-unstable-v1.h" #include "../../gfx/common/wayland/relative-pointer-unstable-v1.h" +#include "../../gfx/common/wayland/single-pixel-buffer-v1.h" +#include "../../gfx/common/wayland/viewporter.h" +#include "../../gfx/common/wayland/xdg-decoration-unstable-v1.h" +#include "../../gfx/common/wayland/xdg-shell.h" #define FRACTIONAL_SCALE_V1_DEN 120 #define FRACTIONAL_SCALE_MULT(v, scale_num) \ @@ -187,6 +188,7 @@ typedef struct gfx_ctx_wayland_data struct wp_cursor_shape_device_v1 *cursor_shape_device; struct wp_content_type_manager_v1 *content_type_manager; struct wp_content_type_v1 *content_type; + struct wp_single_pixel_buffer_manager_v1 *single_pixel_manager; output_info_t *current_output; #ifdef HAVE_VULKAN gfx_ctx_vulkan_data_t vk;