single-pixel-buffer: Add try_from_buffer() function

Add wlr_single_pixel_buffer_v1_try_from_buffer() and move `struct
wlr_single_pixel_buffer_v1` to wlr_buffer.h. This allows other code to
find out if a wlr_buffer is a single-pixel buffer and, if so, find out
what color it is.
This commit is contained in:
David Turner 2025-04-07 13:25:42 +01:00
parent 709fc8fd8e
commit 5563d23b81
2 changed files with 36 additions and 9 deletions

View file

@ -181,4 +181,29 @@ struct wlr_client_buffer {
*/
struct wlr_client_buffer *wlr_client_buffer_get(struct wlr_buffer *buffer);
/**
* A single-pixel buffer. Used by clients to draw solid-color rectangles.
*/
struct wlr_single_pixel_buffer_v1 {
struct wlr_buffer base;
// Full-scale for each component is UINT32_MAX
uint32_t r, g, b, a;
struct {
struct wl_resource *resource;
struct wl_listener release;
// Packed little-endian DRM_FORMAT_ARGB8888. Used for data_ptr_access
uint8_t argb8888[4];
} WLR_PRIVATE;
};
/**
* If the wlr_buffer is a wlr_single_pixel_buffer_v1 then unwrap it.
* Otherwise, returns NULL.
*/
struct wlr_single_pixel_buffer_v1 *wlr_single_pixel_buffer_v1_try_from_buffer(
struct wlr_buffer *buffer);
#endif

View file

@ -8,15 +8,6 @@
#define SINGLE_PIXEL_MANAGER_VERSION 1
struct wlr_single_pixel_buffer_v1 {
struct wlr_buffer base;
struct wl_resource *resource;
uint32_t r, g, b, a;
uint8_t argb8888[4]; // packed little-endian DRM_FORMAT_ARGB8888
struct wl_listener release;
};
static void destroy_resource(struct wl_client *client,
struct wl_resource *resource) {
wl_resource_destroy(resource);
@ -180,3 +171,14 @@ struct wlr_single_pixel_buffer_manager_v1 *wlr_single_pixel_buffer_manager_v1_cr
return manager;
}
struct wlr_single_pixel_buffer_v1 *wlr_single_pixel_buffer_v1_try_from_buffer(
struct wlr_buffer *buffer) {
if (buffer->impl != &buffer_impl) {
return NULL;
}
return wl_container_of(buffer,
(struct wlr_single_pixel_buffer_v1 *)NULL, base);
}