Skip to content

Commit

Permalink
wayland: add support for single-pixel-buffer-v1 protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunderland93 committed Dec 28, 2024
1 parent b8c9665 commit 8864062
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 36 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Makefile.common
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions deps/wayland-protocols/staging/single-pixel-buffer/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Single-pixel buffer protocol

Maintainers:
Simon Ser <[email protected]> (@emersion)
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<protocol name="single_pixel_buffer_v1">
<copyright>
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.
</copyright>

<description summary="single pixel buffer factory">
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.
</description>

<interface name="wp_single_pixel_buffer_manager_v1" version="1">
<description summary="global factory for single-pixel buffers">
The wp_single_pixel_buffer_manager_v1 interface is a factory for
single-pixel buffers.
</description>

<request name="destroy" type="destructor">
<description summary="destroy the manager">
Destroy the wp_single_pixel_buffer_manager_v1 object.

The child objects created via this interface are unaffected.
</description>
</request>

<request name="create_u32_rgba_buffer">
<description summary="create a 1×1 buffer from 32-bit RGBA values">
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.
</description>
<arg name="id" type="new_id" interface="wl_buffer"/>
<arg name="r" type="uint" summary="value of the buffer's red channel"/>
<arg name="g" type="uint" summary="value of the buffer's green channel"/>
<arg name="b" type="uint" summary="value of the buffer's blue channel"/>
<arg name="a" type="uint" summary="value of the buffer's alpha channel"/>
</request>
</interface>
</protocol>
3 changes: 2 additions & 1 deletion gfx/common/wayland/generate_wayland_protos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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'
82 changes: 51 additions & 31 deletions gfx/common/wayland_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions input/common/wayland_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 5 additions & 3 deletions input/common/wayland_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 8864062

Please sign in to comment.