1 /* GStreamer Wayland video sink
3 * Copyright (C) 2016 STMicroelectronics SA
4 * Copyright (C) 2016 Fabien Dessenne <fabien.dessenne@st.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the Free
18 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301 USA.
26 #include <gst/allocators/gstdmabuf.h>
28 #include "wllinuxdmabuf.h"
29 #include "wlvideoformat.h"
31 GST_DEBUG_CATEGORY_EXTERN (gstwayland_debug);
32 #define GST_CAT_DEFAULT gstwayland_debug
38 struct wl_buffer *wbuf;
39 } ConstructBufferData;
42 create_succeeded (void *data, struct zwp_linux_buffer_params_v1 *params,
43 struct wl_buffer *new_buffer)
45 ConstructBufferData *d = data;
47 g_mutex_lock (&d->lock);
49 zwp_linux_buffer_params_v1_destroy (params);
50 g_cond_signal (&d->cond);
51 g_mutex_unlock (&d->lock);
55 create_failed (void *data, struct zwp_linux_buffer_params_v1 *params)
57 ConstructBufferData *d = data;
59 g_mutex_lock (&d->lock);
61 zwp_linux_buffer_params_v1_destroy (params);
62 g_cond_signal (&d->cond);
63 g_mutex_unlock (&d->lock);
66 static const struct zwp_linux_buffer_params_v1_listener params_listener = {
72 gst_wl_linux_dmabuf_construct_wl_buffer (GstBuffer * buf,
73 GstWlDisplay * display, const GstVideoInfo * info)
77 guint i, width, height;
78 guint nplanes, flags = 0;
79 struct zwp_linux_buffer_params_v1 *params;
81 ConstructBufferData data;
83 g_return_val_if_fail (gst_wl_display_check_format_for_dmabuf (display,
84 GST_VIDEO_INFO_FORMAT (info)), NULL);
86 mem = gst_buffer_peek_memory (buf, 0);
87 format = gst_video_format_to_wl_dmabuf_format (GST_VIDEO_INFO_FORMAT (info));
89 g_cond_init (&data.cond);
90 g_mutex_init (&data.lock);
91 g_mutex_lock (&data.lock);
93 width = GST_VIDEO_INFO_WIDTH (info);
94 height = GST_VIDEO_INFO_HEIGHT (info);
95 nplanes = GST_VIDEO_INFO_N_PLANES (info);
97 GST_DEBUG_OBJECT (display, "Creating wl_buffer from DMABuf of size %"
98 G_GSSIZE_FORMAT " (%d x %d), format %s", info->size, width, height,
99 gst_wl_dmabuf_format_to_string (format));
101 /* Creation and configuration of planes */
102 params = zwp_linux_dmabuf_v1_create_params (display->dmabuf);
104 for (i = 0; i < nplanes; i++) {
105 guint offset, stride, mem_idx, length;
108 offset = GST_VIDEO_INFO_PLANE_OFFSET (info, i);
109 stride = GST_VIDEO_INFO_PLANE_STRIDE (info, i);
110 if (gst_buffer_find_memory (buf, offset, 1, &mem_idx, &length, &skip)) {
111 GstMemory *m = gst_buffer_peek_memory (buf, mem_idx);
112 gint fd = gst_dmabuf_memory_get_fd (m);
113 zwp_linux_buffer_params_v1_add (params, fd, i, m->offset + skip,
116 GST_ERROR_OBJECT (mem->allocator, "memory does not seem to contain "
117 "enough data for the specified format");
118 zwp_linux_buffer_params_v1_destroy (params);
124 if (GST_BUFFER_FLAG_IS_SET (buf, GST_VIDEO_BUFFER_FLAG_INTERLACED)) {
125 GST_DEBUG_OBJECT (mem->allocator, "interlaced buffer");
126 flags = ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_INTERLACED;
128 if (!GST_BUFFER_FLAG_IS_SET (buf, GST_VIDEO_BUFFER_FLAG_TFF)) {
129 GST_DEBUG_OBJECT (mem->allocator, "with bottom field first");
130 flags |= ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_BOTTOM_FIRST;
134 /* Request buffer creation */
135 zwp_linux_buffer_params_v1_add_listener (params, ¶ms_listener, &data);
136 zwp_linux_buffer_params_v1_create (params, width, height, format, flags);
138 /* Wait for the request answer */
139 wl_display_flush (display->display);
140 data.wbuf = (gpointer) 0x1;
141 timeout = g_get_monotonic_time () + G_TIME_SPAN_SECOND;
142 while (data.wbuf == (gpointer) 0x1) {
143 if (!g_cond_wait_until (&data.cond, &data.lock, timeout)) {
144 GST_ERROR_OBJECT (mem->allocator, "zwp_linux_buffer_params_v1 time out");
145 zwp_linux_buffer_params_v1_destroy (params);
152 GST_ERROR_OBJECT (mem->allocator, "can't create linux-dmabuf buffer");
154 GST_DEBUG_OBJECT (mem->allocator, "created linux_dmabuf wl_buffer (%p):"
155 "%dx%d, fmt=%.4s, %d planes",
156 data.wbuf, width, height, (char *) &format, nplanes);
159 g_mutex_unlock (&data.lock);
160 g_mutex_clear (&data.lock);
161 g_cond_clear (&data.cond);