1 /* GStreamer Wayland video sink
3 * Copyright (C) 2012 Intel Corporation
4 * Copyright (C) 2012 Sreerenj Balachandran <sreerenj.balachandran@intel.com>
5 * Copyright (C) 2014 Collabora Ltd.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
23 #include "wlshmallocator.h"
24 #include "wlvideoformat.h"
32 #include <sys/types.h>
34 GST_DEBUG_CATEGORY_EXTERN (gstwayland_debug);
35 #define GST_CAT_DEFAULT gstwayland_debug
37 G_DEFINE_TYPE (GstWlShmAllocator, gst_wl_shm_allocator, GST_TYPE_FD_ALLOCATOR);
40 gst_wl_shm_allocator_alloc (GstAllocator * allocator, gsize size,
41 GstAllocationParams * params)
43 GstWlShmAllocator *self = GST_WL_SHM_ALLOCATOR (allocator);
50 /* TODO: make use of the allocation params, if necessary */
52 #ifdef HAVE_MEMFD_CREATE
53 fd = memfd_create ("gst-wayland-shm", MFD_CLOEXEC | MFD_ALLOW_SEALING);
55 /* We can add this seal before calling posix_fallocate(), as
56 * the file is currently zero-sized anyway.
58 * There is also no need to check for the return value, we
59 * couldn't do anything with it anyway.
61 fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK);
65 /* allocate shm pool */
66 snprintf (filename, 1024, "%s/%s-%d-%s", g_get_user_runtime_dir (),
67 "wayland-shm", init++, "XXXXXX");
69 fd = g_mkstemp (filename);
71 GST_ERROR_OBJECT (self, "opening temp file %s failed: %s", filename,
79 if (ftruncate (fd, size) < 0) {
80 GST_ERROR_OBJECT (self, "ftruncate failed: %s", strerror (errno));
85 mem = gst_fd_allocator_alloc (allocator, fd, size,
86 GST_FD_MEMORY_FLAG_KEEP_MAPPED);
87 if (G_UNLIKELY (!mem)) {
88 GST_ERROR_OBJECT (self, "GstFdMemory allocation failed");
93 /* we need to map the memory in order to unlink the file without losing it */
94 if (!gst_memory_map (mem, &info, GST_MAP_READWRITE)) {
95 GST_ERROR_OBJECT (self, "GstFdMemory map failed");
100 /* unmap will not really munmap(), we just
101 * need it to release the miniobject lock */
102 gst_memory_unmap (mem, &info);
108 gst_wl_shm_allocator_class_init (GstWlShmAllocatorClass * klass)
110 GstAllocatorClass *alloc_class = (GstAllocatorClass *) klass;
112 alloc_class->alloc = GST_DEBUG_FUNCPTR (gst_wl_shm_allocator_alloc);
116 gst_wl_shm_allocator_init (GstWlShmAllocator * self)
118 GstAllocator *alloc = GST_ALLOCATOR_CAST (self);
120 alloc->mem_type = GST_ALLOCATOR_WL_SHM;
122 GST_OBJECT_FLAG_UNSET (self, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
126 gst_wl_shm_allocator_register (void)
130 alloc = g_object_new (GST_TYPE_WL_SHM_ALLOCATOR, NULL);
131 gst_object_ref_sink (alloc);
132 gst_allocator_register (GST_ALLOCATOR_WL_SHM, alloc);
136 gst_wl_shm_allocator_get (void)
138 return gst_allocator_find (GST_ALLOCATOR_WL_SHM);
142 gst_is_wl_shm_memory (GstMemory * mem)
144 return gst_memory_is_type (mem, GST_ALLOCATOR_WL_SHM);
147 /* Copied from gst_v4l2_object_extrapolate_stride() */
149 gst_wl_shm_extrapolate_stride (const GstVideoFormatInfo * finfo, gint plane,
154 switch (finfo->format) {
155 case GST_VIDEO_FORMAT_NV12:
156 case GST_VIDEO_FORMAT_NV12_64Z32:
157 case GST_VIDEO_FORMAT_NV21:
158 case GST_VIDEO_FORMAT_NV16:
159 case GST_VIDEO_FORMAT_NV61:
160 case GST_VIDEO_FORMAT_NV24:
161 estride = (plane == 0 ? 1 : 2) *
162 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
165 estride = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
173 gst_wl_shm_validate_video_info (const GstVideoInfo * vinfo)
175 gint height = GST_VIDEO_INFO_HEIGHT (vinfo);
176 gint base_stride = GST_VIDEO_INFO_PLANE_STRIDE (vinfo, 0);
177 gsize base_offs = GST_VIDEO_INFO_PLANE_OFFSET (vinfo, 0);
181 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (vinfo); i++) {
184 /* Overwrite the video info's stride and offset using the pitch calculcated
185 * by the kms driver. */
186 estride = gst_wl_shm_extrapolate_stride (vinfo->finfo, i, base_stride);
188 if (estride != GST_VIDEO_INFO_PLANE_STRIDE (vinfo, i))
191 if (GST_VIDEO_INFO_PLANE_OFFSET (vinfo, i) - base_offs != offs)
194 /* Note that we cannot negotiate special padding betweem each planes,
195 * hence using the display height here. */
197 estride * GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (vinfo->finfo, i, height);
200 if (vinfo->size < offs)
207 gst_wl_shm_memory_construct_wl_buffer (GstMemory * mem, GstWlDisplay * display,
208 const GstVideoInfo * info)
210 gint width, height, stride;
211 gsize offset, size, memsize, maxsize;
212 enum wl_shm_format format;
213 struct wl_shm_pool *wl_pool;
214 struct wl_buffer *wbuffer;
216 if (!gst_wl_shm_validate_video_info (info)) {
217 GST_DEBUG_OBJECT (display, "Unsupported strides and offsets.");
221 width = GST_VIDEO_INFO_WIDTH (info);
222 height = GST_VIDEO_INFO_HEIGHT (info);
223 stride = GST_VIDEO_INFO_PLANE_STRIDE (info, 0);
224 size = GST_VIDEO_INFO_SIZE (info);
225 format = gst_video_format_to_wl_shm_format (GST_VIDEO_INFO_FORMAT (info));
227 memsize = gst_memory_get_sizes (mem, &offset, &maxsize);
228 offset += GST_VIDEO_INFO_PLANE_OFFSET (info, 0);
230 g_return_val_if_fail (gst_is_fd_memory (mem), NULL);
231 g_return_val_if_fail (size <= memsize, NULL);
232 g_return_val_if_fail (gst_wl_display_check_format_for_shm (display,
233 GST_VIDEO_INFO_FORMAT (info)), NULL);
235 GST_DEBUG_OBJECT (display, "Creating wl_buffer from SHM of size %"
236 G_GSSIZE_FORMAT " (%d x %d, stride %d), format %s", size, width, height,
237 stride, gst_wl_shm_format_to_string (format));
239 wl_pool = wl_shm_create_pool (display->shm, gst_fd_memory_get_fd (mem),
241 wbuffer = wl_shm_pool_create_buffer (wl_pool, offset, width, height, stride,
243 wl_shm_pool_destroy (wl_pool);