1 /* GStreamer Wayland video sink
2 *
3 * Copyright (C) 2012 Intel Corporation
4 * Copyright (C) 2012 Sreerenj Balachandran <sreerenj.balachandran@intel.com>
5 * Copyright (C) 2014 Collabora Ltd.
6 *
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.
11 *
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.
16 *
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.
21 */
22
23 #include "wlshmallocator.h"
24 #include "wlvideoformat.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <sys/mman.h>
32 #include <sys/types.h>
33
34 GST_DEBUG_CATEGORY_EXTERN (gstwayland_debug);
35 #define GST_CAT_DEFAULT gstwayland_debug
36
37 G_DEFINE_TYPE (GstWlShmAllocator, gst_wl_shm_allocator, GST_TYPE_FD_ALLOCATOR);
38
39 static GstMemory *
gst_wl_shm_allocator_alloc(GstAllocator * allocator,gsize size,GstAllocationParams * params)40 gst_wl_shm_allocator_alloc (GstAllocator * allocator, gsize size,
41 GstAllocationParams * params)
42 {
43 GstWlShmAllocator *self = GST_WL_SHM_ALLOCATOR (allocator);
44 char filename[1024];
45 static int init = 0;
46 int fd;
47 GstMemory *mem;
48 GstMapInfo info;
49
50 /* TODO: make use of the allocation params, if necessary */
51
52 #ifdef HAVE_MEMFD_CREATE
53 fd = memfd_create ("gst-wayland-shm", MFD_CLOEXEC | MFD_ALLOW_SEALING);
54 if (fd >= 0) {
55 /* We can add this seal before calling posix_fallocate(), as
56 * the file is currently zero-sized anyway.
57 *
58 * There is also no need to check for the return value, we
59 * couldn't do anything with it anyway.
60 */
61 fcntl (fd, F_ADD_SEALS, F_SEAL_SHRINK);
62 } else
63 #endif
64 {
65 /* allocate shm pool */
66 snprintf (filename, 1024, "%s/%s-%d-%s", g_get_user_runtime_dir (),
67 "wayland-shm", init++, "XXXXXX");
68
69 fd = g_mkstemp (filename);
70 if (fd < 0) {
71 GST_ERROR_OBJECT (self, "opening temp file %s failed: %s", filename,
72 strerror (errno));
73 return NULL;
74 }
75
76 unlink (filename);
77 }
78
79 if (ftruncate (fd, size) < 0) {
80 GST_ERROR_OBJECT (self, "ftruncate failed: %s", strerror (errno));
81 close (fd);
82 return NULL;
83 }
84
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");
89 close (fd);
90 return NULL;
91 }
92
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");
96 close (fd);
97 return NULL;
98 }
99
100 /* unmap will not really munmap(), we just
101 * need it to release the miniobject lock */
102 gst_memory_unmap (mem, &info);
103
104 return mem;
105 }
106
107 static void
gst_wl_shm_allocator_class_init(GstWlShmAllocatorClass * klass)108 gst_wl_shm_allocator_class_init (GstWlShmAllocatorClass * klass)
109 {
110 GstAllocatorClass *alloc_class = (GstAllocatorClass *) klass;
111
112 alloc_class->alloc = GST_DEBUG_FUNCPTR (gst_wl_shm_allocator_alloc);
113 }
114
115 static void
gst_wl_shm_allocator_init(GstWlShmAllocator * self)116 gst_wl_shm_allocator_init (GstWlShmAllocator * self)
117 {
118 GstAllocator *alloc = GST_ALLOCATOR_CAST (self);
119
120 alloc->mem_type = GST_ALLOCATOR_WL_SHM;
121
122 GST_OBJECT_FLAG_UNSET (self, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
123 }
124
125 void
gst_wl_shm_allocator_register(void)126 gst_wl_shm_allocator_register (void)
127 {
128 GstAllocator *alloc;
129
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);
133 }
134
135 GstAllocator *
gst_wl_shm_allocator_get(void)136 gst_wl_shm_allocator_get (void)
137 {
138 return gst_allocator_find (GST_ALLOCATOR_WL_SHM);
139 }
140
141 gboolean
gst_is_wl_shm_memory(GstMemory * mem)142 gst_is_wl_shm_memory (GstMemory * mem)
143 {
144 return gst_memory_is_type (mem, GST_ALLOCATOR_WL_SHM);
145 }
146
147 /* Copied from gst_v4l2_object_extrapolate_stride() */
148 static gint
gst_wl_shm_extrapolate_stride(const GstVideoFormatInfo * finfo,gint plane,gint stride)149 gst_wl_shm_extrapolate_stride (const GstVideoFormatInfo * finfo, gint plane,
150 gint stride)
151 {
152 gint estride;
153
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);
163 break;
164 default:
165 estride = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
166 break;
167 }
168
169 return estride;
170 }
171
172 static gboolean
gst_wl_shm_validate_video_info(const GstVideoInfo * vinfo)173 gst_wl_shm_validate_video_info (const GstVideoInfo * vinfo)
174 {
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);
178 gint i;
179 gsize offs = 0;
180
181 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (vinfo); i++) {
182 guint32 estride;
183
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);
187
188 if (estride != GST_VIDEO_INFO_PLANE_STRIDE (vinfo, i))
189 return FALSE;
190
191 if (GST_VIDEO_INFO_PLANE_OFFSET (vinfo, i) - base_offs != offs)
192 return FALSE;
193
194 /* Note that we cannot negotiate special padding betweem each planes,
195 * hence using the display height here. */
196 offs +=
197 estride * GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (vinfo->finfo, i, height);
198 }
199
200 if (vinfo->size < offs)
201 return FALSE;
202
203 return TRUE;
204 }
205
206 struct wl_buffer *
gst_wl_shm_memory_construct_wl_buffer(GstMemory * mem,GstWlDisplay * display,const GstVideoInfo * info)207 gst_wl_shm_memory_construct_wl_buffer (GstMemory * mem, GstWlDisplay * display,
208 const GstVideoInfo * info)
209 {
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;
215
216 if (!gst_wl_shm_validate_video_info (info)) {
217 GST_DEBUG_OBJECT (display, "Unsupported strides and offsets.");
218 return NULL;
219 }
220
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));
226
227 memsize = gst_memory_get_sizes (mem, &offset, &maxsize);
228 offset += GST_VIDEO_INFO_PLANE_OFFSET (info, 0);
229
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);
234
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));
238
239 wl_pool = wl_shm_create_pool (display->shm, gst_fd_memory_get_fd (mem),
240 memsize);
241 wbuffer = wl_shm_pool_create_buffer (wl_pool, offset, width, height, stride,
242 format);
243 wl_shm_pool_destroy (wl_pool);
244
245 return wbuffer;
246 }
247