[PATCH 5/5] buffer: add helper accessors.

Gwenole Beauchesne gb.devel at gmail.com
Fri Apr 20 07:39:56 PDT 2012


Signed-off-by: Gwenole Beauchesne <gwenole.beauchesne at intel.com>
---
 src/wayland-server.c |   55 ++++++++++++++++++++++++++++++++++++++++++++++++++
 src/wayland-server.h |    6 +++++
 2 files changed, 61 insertions(+), 0 deletions(-)

diff --git a/src/wayland-server.c b/src/wayland-server.c
index 8ca27bd..d672f83 100644
--- a/src/wayland-server.c
+++ b/src/wayland-server.c
@@ -1072,3 +1072,58 @@ wl_client_new_object(struct wl_client *client,
 				    interface, implementation, id, data);
 
 }
+
+/* XXX: assume commonly used format */
+#define WL_BUFFER_FORMAT_DEFAULT (WL_BUFFER_FORMAT_ARGB32)
+
+static uint32_t
+wl_shm_buffer_get_base_format(struct wl_buffer *buffer)
+{
+	uint32_t format;
+
+	switch (wl_shm_buffer_get_format(buffer)) {
+	case WL_SHM_FORMAT_ARGB8888:
+		format = WL_BUFFER_FORMAT_ARGB32;
+		break;
+	case WL_SHM_FORMAT_XRGB8888:
+		format = WL_BUFFER_FORMAT_XRGB32;
+		break;
+	default:
+		format = WL_BUFFER_FORMAT_DEFAULT;
+		break;
+	}
+	return format;
+}
+
+static inline int
+wl_buffer_has_layout(struct wl_buffer *buffer)
+{
+	return wl_buffer_interface.version >= 2;
+}
+
+WL_EXPORT uint32_t
+wl_buffer_get_format(struct wl_buffer *buffer)
+{
+	if (wl_buffer_has_layout(buffer))
+		return buffer->layout.format;
+	if (wl_buffer_is_shm(buffer))
+		return wl_shm_buffer_get_base_format(buffer);
+	return WL_BUFFER_FORMAT_DEFAULT;
+}
+
+WL_EXPORT void
+wl_buffer_get_layout(struct wl_buffer *buffer, struct wl_buffer_layout *layout)
+{
+	if (wl_buffer_has_layout(buffer))
+		memcpy(layout, &buffer->layout, sizeof(*layout));
+	else {
+		layout->format     = wl_buffer_get_format(buffer);
+		layout->width      = buffer->width;
+		layout->height     = buffer->height;
+		layout->num_planes = 1;
+		layout->offsets[0] = 0;
+		layout->pitches[0] = 4 * buffer->width;
+		if (wl_buffer_is_shm(buffer))
+			layout->pitches[0] = wl_shm_buffer_get_stride(buffer);
+	}
+}
diff --git a/src/wayland-server.h b/src/wayland-server.h
index af949a0..9fdad7d 100644
--- a/src/wayland-server.h
+++ b/src/wayland-server.h
@@ -363,6 +363,12 @@ wl_buffer_is_shm(struct wl_buffer *buffer);
 int
 wl_display_init_shm(struct wl_display *display);
 
+uint32_t
+wl_buffer_get_format(struct wl_buffer *buffer);
+
+void
+wl_buffer_get_layout(struct wl_buffer *buffer, struct wl_buffer_layout *layout);
+
 #ifdef  __cplusplus
 }
 #endif
-- 
1.7.5.4



More information about the wayland-devel mailing list