| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include <config.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include "gstwllinuxdmabuf.h" |
| 28 |
|
| 29 |
#include "linux-dmabuf-unstable-v1-client-protocol.h" |
| 30 |
|
| 31 |
GST_DEBUG_CATEGORY (gst_wl_dmabuf_debug); |
| 32 |
#define GST_CAT_DEFAULT gst_wl_dmabuf_debug |
| 33 |
|
| 34 |
void |
| 35 |
gst_wl_linux_dmabuf_init_once (void) |
| 36 |
{ |
| 37 |
static gsize _init = 0; |
| 38 |
|
| 39 |
if (g_once_init_enter (&_init)) { |
| 40 |
GST_DEBUG_CATEGORY_INIT (gst_wl_dmabuf_debug, "wl_dmabuf", 0, |
| 41 |
"wl_dmabuf library"); |
| 42 |
|
| 43 |
g_once_init_leave (&_init, 1); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
typedef struct |
| 48 |
{ |
| 49 |
GMutex lock; |
| 50 |
GCond cond; |
| 51 |
struct wl_buffer *wbuf; |
| 52 |
} ConstructBufferData; |
| 53 |
|
| 54 |
static void |
| 55 |
create_succeeded (void *data, struct zwp_linux_buffer_params_v1 *params, |
| 56 |
struct wl_buffer *new_buffer) |
| 57 |
{ |
| 58 |
ConstructBufferData *d = data; |
| 59 |
|
| 60 |
g_mutex_lock (&d->lock); |
| 61 |
d->wbuf = new_buffer; |
| 62 |
zwp_linux_buffer_params_v1_destroy (params); |
| 63 |
g_cond_signal (&d->cond); |
| 64 |
g_mutex_unlock (&d->lock); |
| 65 |
} |
| 66 |
|
| 67 |
static void |
| 68 |
create_failed (void *data, struct zwp_linux_buffer_params_v1 *params) |
| 69 |
{ |
| 70 |
ConstructBufferData *d = data; |
| 71 |
|
| 72 |
g_mutex_lock (&d->lock); |
| 73 |
d->wbuf = NULL; |
| 74 |
zwp_linux_buffer_params_v1_destroy (params); |
| 75 |
g_cond_signal (&d->cond); |
| 76 |
g_mutex_unlock (&d->lock); |
| 77 |
} |
| 78 |
|
| 79 |
static const struct zwp_linux_buffer_params_v1_listener params_listener = { |
| 80 |
create_succeeded, |
| 81 |
create_failed |
| 82 |
}; |
| 83 |
|
| 84 |
static gint |
| 85 |
get_drm_stride (const GstVideoFormatInfo * finfo, const gint * strides, |
| 86 |
gint plane) |
| 87 |
{ |
| 88 |
gint stride = strides[plane]; |
| 89 |
|
| 90 |
if (!GST_VIDEO_FORMAT_INFO_IS_TILED (finfo)) |
| 91 |
return stride; |
| 92 |
|
| 93 |
return GST_VIDEO_TILE_X_TILES (stride) * |
| 94 |
GST_VIDEO_FORMAT_INFO_TILE_STRIDE (finfo, plane); |
| 95 |
} |
| 96 |
|
| 97 |
struct wl_buffer * |
| 98 |
gst_wl_linux_dmabuf_construct_wl_buffer (GstBuffer * buf, |
| 99 |
GstWlDisplay * display, const GstVideoInfoDmaDrm * drm_info) |
| 100 |
{ |
| 101 |
GstMemory *mem; |
| 102 |
guint fourcc; |
| 103 |
guint64 modifier; |
| 104 |
guint i, width = 0, height = 0; |
| 105 |
GstVideoInfo info; |
| 106 |
const GstVideoFormatInfo *finfo; |
| 107 |
const gsize *offsets = NULL; |
| 108 |
const gint *strides = NULL; |
| 109 |
GstVideoMeta *vmeta; |
| 110 |
guint nplanes = 0, flags = 0; |
| 111 |
struct zwp_linux_buffer_params_v1 *params; |
| 112 |
gint64 timeout; |
| 113 |
ConstructBufferData data = { 0 }; |
| 114 |
|
| 115 |
g_return_val_if_fail (gst_wl_display_check_format_for_dmabuf (display, |
| 116 |
drm_info), NULL); |
| 117 |
|
| 118 |
mem = gst_buffer_peek_memory (buf, 0); |
| 119 |
fourcc = drm_info->drm_fourcc; |
| 120 |
modifier = drm_info->drm_modifier; |
| 121 |
|
| 122 |
g_cond_init (&data.cond); |
| 123 |
g_mutex_init (&data.lock); |
| 124 |
g_mutex_lock (&data.lock); |
| 125 |
|
| 126 |
vmeta = gst_buffer_get_video_meta (buf); |
| 127 |
if (vmeta) { |
| 128 |
finfo = drm_info->vinfo.finfo; |
| 129 |
width = vmeta->width; |
| 130 |
height = vmeta->height; |
| 131 |
nplanes = vmeta->n_planes; |
| 132 |
offsets = vmeta->offset; |
| 133 |
strides = vmeta->stride; |
| 134 |
} else if (gst_video_info_dma_drm_to_video_info (drm_info, &info)) { |
| 135 |
finfo = info.finfo; |
| 136 |
nplanes = GST_VIDEO_INFO_N_PLANES (&info); |
| 137 |
width = info.width; |
| 138 |
height = info.height; |
| 139 |
offsets = info.offset; |
| 140 |
strides = info.stride; |
| 141 |
} else { |
| 142 |
GST_ERROR_OBJECT (display, "GstVideoMeta is needed to carry DMABuf using " |
| 143 |
"'memory:DMABuf' caps feature."); |
| 144 |
goto out; |
| 145 |
} |
| 146 |
|
| 147 |
GST_DEBUG_OBJECT (display, |
| 148 |
"Creating wl_buffer from DMABuf of size %" G_GSSIZE_FORMAT |
| 149 |
" (%d x %d), DRM fourcc %" GST_FOURCC_FORMAT, gst_buffer_get_size (buf), |
| 150 |
width, height, GST_FOURCC_ARGS (fourcc)); |
| 151 |
|
| 152 |
|
| 153 |
params = zwp_linux_dmabuf_v1_create_params (gst_wl_display_get_dmabuf_v1 |
| 154 |
(display)); |
| 155 |
|
| 156 |
for (i = 0; i < nplanes; i++) { |
| 157 |
guint offset, stride, mem_idx, length; |
| 158 |
gsize skip; |
| 159 |
|
| 160 |
offset = offsets[i]; |
| 161 |
stride = get_drm_stride (finfo, strides, i); |
| 162 |
if (gst_buffer_find_memory (buf, offset, 1, &mem_idx, &length, &skip)) { |
| 163 |
GstMemory *m = gst_buffer_peek_memory (buf, mem_idx); |
| 164 |
gint fd = gst_dmabuf_memory_get_fd (m); |
| 165 |
zwp_linux_buffer_params_v1_add (params, fd, i, m->offset + skip, |
| 166 |
stride, modifier >> 32, modifier & G_GUINT64_CONSTANT (0x0ffffffff)); |
| 167 |
} else { |
| 168 |
GST_ERROR_OBJECT (mem->allocator, "memory does not seem to contain " |
| 169 |
"enough data for the specified format"); |
| 170 |
zwp_linux_buffer_params_v1_destroy (params); |
| 171 |
data.wbuf = NULL; |
| 172 |
goto out; |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
if (GST_BUFFER_FLAG_IS_SET (buf, GST_VIDEO_BUFFER_FLAG_INTERLACED)) { |
| 177 |
GST_DEBUG_OBJECT (mem->allocator, "interlaced buffer"); |
| 178 |
flags = ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_INTERLACED; |
| 179 |
|
| 180 |
if (!GST_BUFFER_FLAG_IS_SET (buf, GST_VIDEO_BUFFER_FLAG_TFF)) { |
| 181 |
GST_DEBUG_OBJECT (mem->allocator, "with bottom field first"); |
| 182 |
flags |= ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_BOTTOM_FIRST; |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
zwp_linux_buffer_params_v1_add_listener (params, ¶ms_listener, &data); |
| 188 |
zwp_linux_buffer_params_v1_create (params, width, height, fourcc, flags); |
| 189 |
|
| 190 |
|
| 191 |
wl_display_flush (gst_wl_display_get_display (display)); |
| 192 |
data.wbuf = (gpointer) 0x1; |
| 193 |
timeout = g_get_monotonic_time () + G_TIME_SPAN_SECOND; |
| 194 |
while (data.wbuf == (gpointer) 0x1) { |
| 195 |
if (!g_cond_wait_until (&data.cond, &data.lock, timeout)) { |
| 196 |
GST_ERROR_OBJECT (mem->allocator, "zwp_linux_buffer_params_v1 time out"); |
| 197 |
zwp_linux_buffer_params_v1_destroy (params); |
| 198 |
data.wbuf = NULL; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
out: |
| 203 |
if (!data.wbuf) { |
| 204 |
GST_ERROR_OBJECT (mem->allocator, "can't create linux-dmabuf buffer"); |
| 205 |
} else { |
| 206 |
GST_DEBUG_OBJECT (mem->allocator, "created linux_dmabuf wl_buffer (%p):" |
| 207 |
"%dx%d, fmt=%" GST_FOURCC_FORMAT ", %d planes", |
| 208 |
data.wbuf, width, height, GST_FOURCC_ARGS (fourcc), nplanes); |
| 209 |
} |
| 210 |
|
| 211 |
g_mutex_unlock (&data.lock); |
| 212 |
g_mutex_clear (&data.lock); |
| 213 |
g_cond_clear (&data.cond); |
| 214 |
|
| 215 |
return data.wbuf; |
| 216 |
} |