xcursor: introduce wlr_xcursor_image_get_buffer()

This makes it so callers no longer need to juggle with raw pixel
pointers anymore, and only need a single wlr_buffer-based codepath.

Additionally, cursors can be unloaded without risking use-after-free.
This commit is contained in:
Simon Ser 2026-01-04 18:24:18 +01:00 committed by Simon Zeni
parent 98733c91b4
commit d7f7b68f49
2 changed files with 31 additions and 3 deletions

View file

@ -56,6 +56,10 @@ struct wlr_xcursor_image {
uint32_t hotspot_y; /* hot-spot y (must be inside image) */ uint32_t hotspot_y; /* hot-spot y (must be inside image) */
uint32_t delay; /* animation delay to next frame (ms) */ uint32_t delay; /* animation delay to next frame (ms) */
uint8_t *buffer; /* pixel data */ uint8_t *buffer; /* pixel data */
struct {
struct wlr_readonly_data_buffer *readonly_buffer;
} WLR_PRIVATE;
}; };
/** /**
@ -119,6 +123,11 @@ struct wlr_xcursor *wlr_xcursor_theme_get_cursor(
*/ */
int wlr_xcursor_frame(struct wlr_xcursor *cursor, uint32_t time); int wlr_xcursor_frame(struct wlr_xcursor *cursor, uint32_t time);
/**
* Get a struct wlr_buffer from a cursor image.
*/
struct wlr_buffer *wlr_xcursor_image_get_buffer(struct wlr_xcursor_image *image);
/** /**
* Get the name of the resize cursor for the given edges. * Get the name of the resize cursor for the given edges.
*/ */

View file

@ -23,18 +23,21 @@
* SOFTWARE. * SOFTWARE.
*/ */
#include <drm_fourcc.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <wlr/util/log.h> #include <wlr/util/log.h>
#include <wlr/xcursor.h> #include <wlr/xcursor.h>
#include "types/wlr_buffer.h"
#include "xcursor/xcursor.h" #include "xcursor/xcursor.h"
#include "xcursor/cursor_data.h" #include "xcursor/cursor_data.h"
static void xcursor_destroy(struct wlr_xcursor *cursor) { static void xcursor_destroy(struct wlr_xcursor *cursor) {
for (size_t i = 0; i < cursor->image_count; i++) { for (size_t i = 0; i < cursor->image_count; i++) {
readonly_data_buffer_drop(cursor->images[i]->readonly_buffer);
free(cursor->images[i]->buffer); free(cursor->images[i]->buffer);
free(cursor->images[i]); free(cursor->images[i]);
} }
@ -57,16 +60,28 @@ static struct wlr_xcursor_image *xcursor_image_create(uint32_t width, uint32_t h
image->hotspot_y = hotspot_y; image->hotspot_y = hotspot_y;
image->delay = delay; image->delay = delay;
size_t size = width * height * sizeof(uint32_t); size_t stride = width * sizeof(uint32_t);
size_t size = stride * height;
image->buffer = malloc(size); image->buffer = malloc(size);
if (image->buffer == NULL) { if (image->buffer == NULL) {
free(image); goto err_image;
return NULL;
} }
memcpy(image->buffer, buffer, size); memcpy(image->buffer, buffer, size);
image->readonly_buffer = readonly_data_buffer_create(DRM_FORMAT_ARGB8888,
stride, width, height, image->buffer);
if (image->readonly_buffer == NULL) {
goto err_buffer;
}
return image; return image;
err_buffer:
free(image->buffer);
err_image:
free(image);
return NULL;
} }
static struct wlr_xcursor *xcursor_create_from_data( static struct wlr_xcursor *xcursor_create_from_data(
@ -330,6 +345,10 @@ int wlr_xcursor_frame(struct wlr_xcursor *_cursor, uint32_t time) {
return xcursor_frame_and_duration(_cursor, time, NULL); return xcursor_frame_and_duration(_cursor, time, NULL);
} }
struct wlr_buffer *wlr_xcursor_image_get_buffer(struct wlr_xcursor_image *image) {
return &image->readonly_buffer->base;
}
const char *wlr_xcursor_get_resize_name(enum wlr_edges edges) { const char *wlr_xcursor_get_resize_name(enum wlr_edges edges) {
if (edges & WLR_EDGE_TOP) { if (edges & WLR_EDGE_TOP) {
if (edges & WLR_EDGE_RIGHT) { if (edges & WLR_EDGE_RIGHT) {