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 /* allocate shm pool */
53 snprintf (filename, 1024, "%s/%s-%d-%s", g_get_user_runtime_dir (),
54 "wayland-shm", init++, "XXXXXX");
55
56 fd = g_mkstemp (filename);
57 if (fd < 0) {
58 GST_ERROR_OBJECT (self, "opening temp file %s failed: %s", filename,
59 strerror (errno));
60 return NULL;
61 }
62 if (ftruncate (fd, size) < 0) {
63 GST_ERROR_OBJECT (self, "ftruncate failed: %s", strerror (errno));
64 close (fd);
65 return NULL;
66 }
67
68 mem = gst_fd_allocator_alloc (allocator, fd, size,
69 GST_FD_MEMORY_FLAG_KEEP_MAPPED);
70 if (G_UNLIKELY (!mem)) {
71 GST_ERROR_OBJECT (self, "GstFdMemory allocation failed");
72 close (fd);
73 return NULL;
74 }
75
76 /* we need to map the memory in order to unlink the file without losing it */
77 if (!gst_memory_map (mem, &info, GST_MAP_READWRITE)) {
78 GST_ERROR_OBJECT (self, "GstFdMemory map failed");
79 close (fd);
80 return NULL;
81 }
82
83 /* unmap will not really munmap(), we just
84 * need it to release the miniobject lock */
85 gst_memory_unmap (mem, &info);
86
87 unlink (filename);
88
89 return mem;
90 }
91
92 static void
gst_wl_shm_allocator_class_init(GstWlShmAllocatorClass * klass)93 gst_wl_shm_allocator_class_init (GstWlShmAllocatorClass * klass)
94 {
95 GstAllocatorClass *alloc_class = (GstAllocatorClass *) klass;
96
97 alloc_class->alloc = GST_DEBUG_FUNCPTR (gst_wl_shm_allocator_alloc);
98 }
99
100 static void
gst_wl_shm_allocator_init(GstWlShmAllocator * self)101 gst_wl_shm_allocator_init (GstWlShmAllocator * self)
102 {
103 GstAllocator *alloc = GST_ALLOCATOR_CAST (self);
104
105 alloc->mem_type = GST_ALLOCATOR_WL_SHM;
106
107 GST_OBJECT_FLAG_UNSET (self, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
108 }
109
110 void
gst_wl_shm_allocator_register(void)111 gst_wl_shm_allocator_register (void)
112 {
113 GstAllocator *alloc;
114
115 alloc = g_object_new (GST_TYPE_WL_SHM_ALLOCATOR, NULL);
116 gst_object_ref_sink (alloc);
117 gst_allocator_register (GST_ALLOCATOR_WL_SHM, alloc);
118 }
119
120 GstAllocator *
gst_wl_shm_allocator_get(void)121 gst_wl_shm_allocator_get (void)
122 {
123 return gst_allocator_find (GST_ALLOCATOR_WL_SHM);
124 }
125
126 gboolean
gst_is_wl_shm_memory(GstMemory * mem)127 gst_is_wl_shm_memory (GstMemory * mem)
128 {
129 return gst_memory_is_type (mem, GST_ALLOCATOR_WL_SHM);
130 }
131
132 /* Copied from gst_v4l2_object_extrapolate_stride() */
133 static gint
gst_wl_shm_extrapolate_stride(const GstVideoFormatInfo * finfo,gint plane,gint stride)134 gst_wl_shm_extrapolate_stride (const GstVideoFormatInfo * finfo, gint plane,
135 gint stride)
136 {
137 gint estride;
138
139 switch (finfo->format) {
140 case GST_VIDEO_FORMAT_NV12:
141 case GST_VIDEO_FORMAT_NV12_64Z32:
142 case GST_VIDEO_FORMAT_NV21:
143 case GST_VIDEO_FORMAT_NV16:
144 case GST_VIDEO_FORMAT_NV61:
145 case GST_VIDEO_FORMAT_NV24:
146 estride = (plane == 0 ? 1 : 2) *
147 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
148 break;
149 default:
150 estride = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH (finfo, plane, stride);
151 break;
152 }
153
154 return estride;
155 }
156
157 static gboolean
gst_wl_shm_validate_video_info(const GstVideoInfo * vinfo)158 gst_wl_shm_validate_video_info (const GstVideoInfo * vinfo)
159 {
160 gint height = GST_VIDEO_INFO_HEIGHT (vinfo);
161 gint base_stride = GST_VIDEO_INFO_PLANE_STRIDE (vinfo, 0);
162 gsize base_offs = GST_VIDEO_INFO_PLANE_OFFSET (vinfo, 0);
163 gint i;
164 gsize offs = 0;
165
166 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (vinfo); i++) {
167 guint32 estride;
168
169 /* Overwrite the video info's stride and offset using the pitch calculcated
170 * by the kms driver. */
171 estride = gst_wl_shm_extrapolate_stride (vinfo->finfo, i, base_stride);
172
173 if (estride != GST_VIDEO_INFO_PLANE_STRIDE (vinfo, i))
174 return FALSE;
175
176 if (GST_VIDEO_INFO_PLANE_OFFSET (vinfo, i) - base_offs != offs)
177 return FALSE;
178
179 /* Note that we cannot negotiate special padding betweem each planes,
180 * hence using the display height here. */
181 offs +=
182 estride * GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (vinfo->finfo, i, height);
183 }
184
185 if (vinfo->size < offs)
186 return FALSE;
187
188 return TRUE;
189 }
190
191 struct wl_buffer *
gst_wl_shm_memory_construct_wl_buffer(GstMemory * mem,GstWlDisplay * display,const GstVideoInfo * info)192 gst_wl_shm_memory_construct_wl_buffer (GstMemory * mem, GstWlDisplay * display,
193 const GstVideoInfo * info)
194 {
195 gint width, height, stride;
196 gsize offset, size, memsize, maxsize;
197 enum wl_shm_format format;
198 struct wl_shm_pool *wl_pool;
199 struct wl_buffer *wbuffer;
200
201 if (!gst_wl_shm_validate_video_info (info)) {
202 GST_DEBUG_OBJECT (display, "Unsupported strides and offsets.");
203 return NULL;
204 }
205
206 width = GST_VIDEO_INFO_WIDTH (info);
207 height = GST_VIDEO_INFO_HEIGHT (info);
208 stride = GST_VIDEO_INFO_PLANE_STRIDE (info, 0);
209 size = GST_VIDEO_INFO_SIZE (info);
210 format = gst_video_format_to_wl_shm_format (GST_VIDEO_INFO_FORMAT (info));
211
212 memsize = gst_memory_get_sizes (mem, &offset, &maxsize);
213 offset += GST_VIDEO_INFO_PLANE_OFFSET (info, 0);
214
215 g_return_val_if_fail (gst_is_fd_memory (mem), NULL);
216 g_return_val_if_fail (size <= memsize, NULL);
217 g_return_val_if_fail (gst_wl_display_check_format_for_shm (display,
218 GST_VIDEO_INFO_FORMAT (info)), NULL);
219
220 GST_DEBUG_OBJECT (display, "Creating wl_buffer from SHM of size %"
221 G_GSSIZE_FORMAT " (%d x %d, stride %d), format %s", size, width, height,
222 stride, gst_wl_shm_format_to_string (format));
223
224 wl_pool = wl_shm_create_pool (display->shm, gst_fd_memory_get_fd (mem),
225 memsize);
226 wbuffer = wl_shm_pool_create_buffer (wl_pool, offset, width, height, stride,
227 format);
228 wl_shm_pool_destroy (wl_pool);
229
230 return wbuffer;
231 }
232