buffer: replace get_data_ptr with {begin,end}_data_ptr_access

This new API allows buffer implementations to know when a user is
actively accessing the buffer's underlying storage. This is
important for the upcoming client-backed wlr_buffer implementation.
This commit is contained in:
Simon Ser 2021-06-02 17:02:28 +02:00 committed by Simon Zeni
parent 9e58301df7
commit 38ba5881a0
7 changed files with 79 additions and 18 deletions

View File

@ -16,13 +16,16 @@ enum wlr_buffer_cap {
}; };
/** /**
* Access a pointer to the allocated data from the underlying implementation, * Get a pointer to a region of memory referring to the buffer's underlying
* its format and its stride. * storage. The format and stride can be used to interpret the memory region
* contents.
* *
* The returned pointer should be pointing to a valid memory location for read * The returned pointer should be pointing to a valid memory region for read
* and write operations. * and write operations. The returned pointer is only valid up to the next
* buffer_end_data_ptr_access call.
*/ */
bool buffer_get_data_ptr(struct wlr_buffer *buffer, void **data, bool buffer_begin_data_ptr_access(struct wlr_buffer *buffer, void **data,
uint32_t *format, size_t *stride); uint32_t *format, size_t *stride);
void buffer_end_data_ptr_access(struct wlr_buffer *buffer);
#endif #endif

View File

@ -26,10 +26,11 @@ struct wlr_buffer_impl {
void (*destroy)(struct wlr_buffer *buffer); void (*destroy)(struct wlr_buffer *buffer);
bool (*get_dmabuf)(struct wlr_buffer *buffer, bool (*get_dmabuf)(struct wlr_buffer *buffer,
struct wlr_dmabuf_attributes *attribs); struct wlr_dmabuf_attributes *attribs);
bool (*get_data_ptr)(struct wlr_buffer *buffer, void **data,
uint32_t *format, size_t *stride);
bool (*get_shm)(struct wlr_buffer *buffer, bool (*get_shm)(struct wlr_buffer *buffer,
struct wlr_shm_attributes *attribs); struct wlr_shm_attributes *attribs);
bool (*begin_data_ptr_access)(struct wlr_buffer *buffer, void **data,
uint32_t *format, size_t *stride);
void (*end_data_ptr_access)(struct wlr_buffer *buffer);
}; };
/** /**
@ -47,6 +48,7 @@ struct wlr_buffer {
bool dropped; bool dropped;
size_t n_locks; size_t n_locks;
bool accessing_data_ptr;
struct { struct {
struct wl_signal destroy; struct wl_signal destroy;

View File

@ -81,7 +81,8 @@ struct wlr_buffer *wlr_allocator_create_buffer(struct wlr_allocator *alloc,
return NULL; return NULL;
} }
if (alloc->buffer_caps & WLR_BUFFER_CAP_DATA_PTR) { if (alloc->buffer_caps & WLR_BUFFER_CAP_DATA_PTR) {
assert(buffer->impl->get_data_ptr); assert(buffer->impl->begin_data_ptr_access &&
buffer->impl->end_data_ptr_access);
} }
if (alloc->buffer_caps & WLR_BUFFER_CAP_DMABUF) { if (alloc->buffer_caps & WLR_BUFFER_CAP_DMABUF) {
assert(buffer->impl->get_dmabuf); assert(buffer->impl->get_dmabuf);

View File

@ -128,8 +128,8 @@ create_err:
return NULL; return NULL;
} }
static bool drm_dumb_buffer_get_data_ptr(struct wlr_buffer *wlr_buffer, void **data, static bool drm_dumb_buffer_begin_data_ptr_access(struct wlr_buffer *wlr_buffer,
uint32_t *format, size_t *stride) { void **data, uint32_t *format, size_t *stride) {
struct wlr_drm_dumb_buffer *buf = drm_dumb_buffer_from_buffer(wlr_buffer); struct wlr_drm_dumb_buffer *buf = drm_dumb_buffer_from_buffer(wlr_buffer);
*data = buf->data; *data = buf->data;
*stride = buf->stride; *stride = buf->stride;
@ -137,6 +137,10 @@ static bool drm_dumb_buffer_get_data_ptr(struct wlr_buffer *wlr_buffer, void **d
return true; return true;
} }
static void drm_dumb_buffer_end_data_ptr_access(struct wlr_buffer *wlr_buffer) {
// This space is intentionally left blank
}
static bool buffer_get_dmabuf(struct wlr_buffer *wlr_buffer, static bool buffer_get_dmabuf(struct wlr_buffer *wlr_buffer,
struct wlr_dmabuf_attributes *attribs) { struct wlr_dmabuf_attributes *attribs) {
struct wlr_drm_dumb_buffer *buf = drm_dumb_buffer_from_buffer(wlr_buffer); struct wlr_drm_dumb_buffer *buf = drm_dumb_buffer_from_buffer(wlr_buffer);
@ -153,7 +157,8 @@ static void buffer_destroy(struct wlr_buffer *wlr_buffer) {
static const struct wlr_buffer_impl buffer_impl = { static const struct wlr_buffer_impl buffer_impl = {
.destroy = buffer_destroy, .destroy = buffer_destroy,
.get_dmabuf = buffer_get_dmabuf, .get_dmabuf = buffer_get_dmabuf,
.get_data_ptr = drm_dumb_buffer_get_data_ptr, .begin_data_ptr_access = drm_dumb_buffer_begin_data_ptr_access,
.end_data_ptr_access = drm_dumb_buffer_end_data_ptr_access,
}; };
static const struct wlr_allocator_interface allocator_impl; static const struct wlr_allocator_interface allocator_impl;

View File

@ -86,10 +86,11 @@ static struct wlr_pixman_buffer *create_buffer(
void *data = NULL; void *data = NULL;
uint32_t drm_format; uint32_t drm_format;
size_t stride; size_t stride;
if (!buffer_get_data_ptr(wlr_buffer, &data, &drm_format, &stride)) { if (!buffer_begin_data_ptr_access(wlr_buffer, &data, &drm_format, &stride)) {
wlr_log(WLR_ERROR, "Failed to get buffer data"); wlr_log(WLR_ERROR, "Failed to get buffer data");
goto error_buffer; goto error_buffer;
} }
buffer_end_data_ptr_access(wlr_buffer);
pixman_format_code_t format = get_pixman_format_from_drm(drm_format); pixman_format_code_t format = get_pixman_format_from_drm(drm_format);
if (format == 0) { if (format == 0) {
@ -125,6 +126,33 @@ static void pixman_begin(struct wlr_renderer *wlr_renderer, uint32_t width,
struct wlr_pixman_renderer *renderer = get_renderer(wlr_renderer); struct wlr_pixman_renderer *renderer = get_renderer(wlr_renderer);
renderer->width = width; renderer->width = width;
renderer->height = height; renderer->height = height;
struct wlr_pixman_buffer *buffer = renderer->current_buffer;
assert(buffer != NULL);
void *data = NULL;
uint32_t drm_format;
size_t stride;
buffer_begin_data_ptr_access(buffer->buffer, &data, &drm_format, &stride);
// If the data pointer has changed, re-create the Pixman image. This can
// happen if it's a client buffer and the wl_shm_pool has been resized.
if (data != pixman_image_get_data(buffer->image)) {
pixman_format_code_t format = get_pixman_format_from_drm(drm_format);
assert(format != 0);
pixman_image_unref(buffer->image);
buffer->image = pixman_image_create_bits_no_clear(format,
buffer->buffer->width, buffer->buffer->height, data, stride);
}
}
static void pixman_end(struct wlr_renderer *wlr_renderer) {
struct wlr_pixman_renderer *renderer = get_renderer(wlr_renderer);
assert(renderer->current_buffer != NULL);
buffer_end_data_ptr_access(renderer->current_buffer->buffer);
} }
static void pixman_clear(struct wlr_renderer *wlr_renderer, static void pixman_clear(struct wlr_renderer *wlr_renderer,
@ -413,6 +441,7 @@ static uint32_t pixman_get_render_buffer_caps(void) {
static const struct wlr_renderer_impl renderer_impl = { static const struct wlr_renderer_impl renderer_impl = {
.begin = pixman_begin, .begin = pixman_begin,
.end = pixman_end,
.clear = pixman_clear, .clear = pixman_clear,
.scissor = pixman_scissor, .scissor = pixman_scissor,
.render_subtexture_with_matrix = pixman_render_subtexture_with_matrix, .render_subtexture_with_matrix = pixman_render_subtexture_with_matrix,

View File

@ -31,8 +31,8 @@ static bool buffer_get_shm(struct wlr_buffer *wlr_buffer,
return true; return true;
} }
static bool shm_buffer_get_data_ptr(struct wlr_buffer *wlr_buffer, void **data, static bool shm_buffer_begin_data_ptr_access(struct wlr_buffer *wlr_buffer,
uint32_t *format, size_t *stride) { void **data, uint32_t *format, size_t *stride) {
struct wlr_shm_buffer *buffer = shm_buffer_from_buffer(wlr_buffer); struct wlr_shm_buffer *buffer = shm_buffer_from_buffer(wlr_buffer);
*data = buffer->data; *data = buffer->data;
*format = buffer->shm.format; *format = buffer->shm.format;
@ -40,10 +40,15 @@ static bool shm_buffer_get_data_ptr(struct wlr_buffer *wlr_buffer, void **data,
return true; return true;
} }
static void shm_buffer_end_data_ptr_access(struct wlr_buffer *wlr_buffer) {
// This space is intentionally left blank
}
static const struct wlr_buffer_impl buffer_impl = { static const struct wlr_buffer_impl buffer_impl = {
.destroy = buffer_destroy, .destroy = buffer_destroy,
.get_shm = buffer_get_shm, .get_shm = buffer_get_shm,
.get_data_ptr = shm_buffer_get_data_ptr, .begin_data_ptr_access = shm_buffer_begin_data_ptr_access,
.end_data_ptr_access = shm_buffer_end_data_ptr_access,
}; };
static struct wlr_buffer *allocator_create_buffer( static struct wlr_buffer *allocator_create_buffer(

View File

@ -12,6 +12,9 @@
void wlr_buffer_init(struct wlr_buffer *buffer, void wlr_buffer_init(struct wlr_buffer *buffer,
const struct wlr_buffer_impl *impl, int width, int height) { const struct wlr_buffer_impl *impl, int width, int height) {
assert(impl->destroy); assert(impl->destroy);
if (impl->begin_data_ptr_access || impl->end_data_ptr_access) {
assert(impl->begin_data_ptr_access && impl->end_data_ptr_access);
}
buffer->impl = impl; buffer->impl = impl;
buffer->width = width; buffer->width = width;
buffer->height = height; buffer->height = height;
@ -24,6 +27,8 @@ static void buffer_consider_destroy(struct wlr_buffer *buffer) {
return; return;
} }
assert(!buffer->accessing_data_ptr);
wlr_signal_emit_safe(&buffer->events.destroy, NULL); wlr_signal_emit_safe(&buffer->events.destroy, NULL);
buffer->impl->destroy(buffer); buffer->impl->destroy(buffer);
@ -67,12 +72,23 @@ bool wlr_buffer_get_dmabuf(struct wlr_buffer *buffer,
return buffer->impl->get_dmabuf(buffer, attribs); return buffer->impl->get_dmabuf(buffer, attribs);
} }
bool buffer_get_data_ptr(struct wlr_buffer *buffer, void **data, bool buffer_begin_data_ptr_access(struct wlr_buffer *buffer, void **data,
uint32_t *format, size_t *stride) { uint32_t *format, size_t *stride) {
if (!buffer->impl->get_data_ptr) { assert(!buffer->accessing_data_ptr);
if (!buffer->impl->begin_data_ptr_access) {
return false; return false;
} }
return buffer->impl->get_data_ptr(buffer, data, format, stride); if (!buffer->impl->begin_data_ptr_access(buffer, data, format, stride)) {
return false;
}
buffer->accessing_data_ptr = true;
return true;
}
void buffer_end_data_ptr_access(struct wlr_buffer *buffer) {
assert(buffer->accessing_data_ptr);
buffer->impl->end_data_ptr_access(buffer);
buffer->accessing_data_ptr = false;
} }
bool wlr_buffer_get_shm(struct wlr_buffer *buffer, bool wlr_buffer_get_shm(struct wlr_buffer *buffer,