]> git.mdlowis.com Git - proto/labwc.git/commitdiff
buffer: add buffer_create_from_wlr_buffer()
authortokyo4j <hrak1529@gmail.com>
Fri, 30 May 2025 11:58:15 +0000 (20:58 +0900)
committerHiroaki Yamamoto <hrak1529@gmail.com>
Fri, 6 Jun 2025 17:12:56 +0000 (02:12 +0900)
include/buffer.h
src/buffer.c

index a756a7b55fa8c24d9046466f526190074cb51883..2771334f9f4ab11e938ad48bf98bd00e935af94e 100644 (file)
@@ -70,6 +70,13 @@ struct lab_data_buffer *buffer_create_cairo(uint32_t logical_width,
 struct lab_data_buffer *buffer_create_from_data(void *pixel_data, uint32_t width,
        uint32_t height, uint32_t stride);
 
+/*
+ * Create a lab_data_buffer from a wlr_buffer by copying its content.
+ * The wlr_buffer must be backed by shm.
+ */
+struct lab_data_buffer *buffer_create_from_wlr_buffer(
+       struct wlr_buffer *wlr_buffer);
+
 /*
  * Resize a buffer to the given size. The source buffer is rendered at the
  * center of the output buffer and shrunk if it overflows from the output buffer.
index 085601168427fa54134a698a92e25d7356e521eb..1faa4620f845abb3000a19a444ec2cfe36024b81 100644 (file)
@@ -28,6 +28,7 @@
 #include <stdlib.h>
 #include <drm_fourcc.h>
 #include <wlr/interfaces/wlr_buffer.h>
+#include <wlr/util/log.h>
 #include "buffer.h"
 #include "common/box.h"
 #include "common/mem.h"
@@ -148,6 +149,32 @@ buffer_create_from_data(void *pixel_data, uint32_t width, uint32_t height,
        return buffer;
 }
 
+struct lab_data_buffer *
+buffer_create_from_wlr_buffer(struct wlr_buffer *wlr_buffer)
+{
+       void *data;
+       uint32_t format;
+       size_t stride;
+       if (!wlr_buffer_begin_data_ptr_access(wlr_buffer,
+                       WLR_BUFFER_DATA_PTR_ACCESS_READ, &data, &format, &stride)) {
+               wlr_log(WLR_ERROR, "failed to access wlr_buffer");
+               return NULL;
+       }
+       if (format != DRM_FORMAT_ARGB8888) {
+               /* TODO: support other formats */
+               wlr_buffer_end_data_ptr_access(wlr_buffer);
+               wlr_log(WLR_ERROR, "cannot create buffer: format=%d", format);
+               return NULL;
+       }
+       size_t buffer_size = stride * wlr_buffer->height;
+       void *copied_data = xmalloc(buffer_size);
+       memcpy(copied_data, data, buffer_size);
+       wlr_buffer_end_data_ptr_access(wlr_buffer);
+
+       return buffer_create_from_data(copied_data,
+               wlr_buffer->width, wlr_buffer->height, stride);
+}
+
 struct lab_data_buffer *
 buffer_resize(struct lab_data_buffer *src_buffer, int width, int height,
                double scale)