Debian Codegraph @

gst-libs/gst/wayland/gstwlvideobufferpool.c

raw
1 /* GStreamer Wayland Library
2 *
3 * Copyright (C) 2017 Collabora Ltd.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include "gstwlvideobufferpool.h"
26
27 #include <gst/allocators/allocators.h>
28 #include <drm_fourcc.h>
29
30 GST_DEBUG_CATEGORY (gst_wl_videobufferpool_debug);
31 #define GST_CAT_DEFAULT gst_wl_videobufferpool_debug
32
33 struct _GstWlVideoBufferPool
34 {
35 GstVideoBufferPool parent;
36 GstAllocator *allocator;
37 GstVideoInfo vinfo;
38 };
39
40 #define gst_wl_video_buffer_pool_parent_class parent_class
41 G_DEFINE_TYPE_WITH_CODE (GstWlVideoBufferPool, gst_wl_video_buffer_pool,
42 GST_TYPE_VIDEO_BUFFER_POOL,
43 GST_DEBUG_CATEGORY_INIT (gst_wl_videobufferpool_debug,
44 "wl_videobufferpool", 0, "wl_dmabuf library"));
45
46 static void
47 gst_wl_update_video_info_from_pitch (GstVideoInfo * vinfo, gint n_planes,
48 guint32 pitch)
49 {
50 gint i;
51 gsize offs = 0;
52 guint32 height = GST_VIDEO_INFO_HEIGHT (vinfo);
53
54 if (!pitch)
55 return;
56
57 for (i = 0; i < n_planes; i++) {
58 guint32 plane_pitch;
59
60 /* Overwrite the video info's stride and offset using the pitch calculcated
61 * by the kms driver. */
62 plane_pitch = gst_video_format_info_extrapolate_stride (vinfo->finfo, i,
63 pitch);
64 GST_VIDEO_INFO_PLANE_STRIDE (vinfo, i) = plane_pitch;
65 GST_VIDEO_INFO_PLANE_OFFSET (vinfo, i) = offs;
66
67 /* Note that we cannot negotiate special padding betweem each planes,
68 * hence using the display height here. */
69 offs += plane_pitch
70 * GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (vinfo->finfo, i, height);
71
72 GST_DEBUG ("Updated plane %i with stride %i and "
73 "offset %" G_GSIZE_FORMAT "(from pitch %u)", i,
74 GST_VIDEO_INFO_PLANE_STRIDE (vinfo, i),
75 GST_VIDEO_INFO_PLANE_OFFSET (vinfo, i), pitch);
76 }
77
78 /* Update with the size use for display, excluding any padding at the end */
79 GST_VIDEO_INFO_SIZE (vinfo) = offs;
80 }
81
82 static gboolean
83 gst_wl_video_buffer_pool_start (GstBufferPool * pool)
84 {
85 GstWlVideoBufferPool *self = GST_WL_VIDEO_BUFFER_POOL (pool);
86 GstStructure *config = gst_buffer_pool_get_config (pool);
87 GstCaps *caps;
88 GstAllocator *allocator;
89 gboolean ret = FALSE;
90 GstVideoFormat format;
91
92 if (!gst_buffer_pool_config_get_params (config, &caps, NULL, NULL, NULL))
93 goto wrong_config;
94
95 if (!gst_buffer_pool_config_get_allocator (config, &allocator, NULL))
96 goto wrong_config;
97
98 /* now parse the caps from the config */
99 if (!gst_video_info_from_caps (&self->vinfo, caps))
100 goto wrong_caps;
101
102 format = GST_VIDEO_INFO_FORMAT (&self->vinfo);
103 if (!gst_video_format_to_wl_dmabuf_format (format))
104 goto unsupported_pixel_format;
105
106 if (GST_IS_DRM_DUMB_ALLOCATOR (allocator)) {
107 if (!gst_drm_dumb_allocator_has_prime_export (allocator))
108 goto no_prime_export;
109
110 self->allocator = gst_object_ref (allocator);
111 }
112
113 /* all good */
114 ret = GST_BUFFER_POOL_CLASS (parent_class)->start (pool);
115
116 done:
117 gst_structure_free (config);
118 return ret;
119
120 wrong_config:
121 {
122 GST_WARNING_OBJECT (pool, "invalid config");
123 goto done;
124 }
125 wrong_caps:
126 {
127 GST_WARNING_OBJECT (pool,
128 "failed getting geometry from caps %" GST_PTR_FORMAT, caps);
129 goto done;
130 }
131 unsupported_pixel_format:
132 {
133 GST_WARNING_OBJECT (pool, "no support for %s pixel format",
134 gst_video_format_to_string (format));
135 goto done;
136 }
137 no_prime_export:
138 {
139 GST_WARNING_OBJECT (self,
140 "not using DRM Dumb allocator as it can't export DMABuf.");
141 goto done;
142 }
143 }
144
145 static gboolean
146 gst_wl_video_buffer_pool_stop (GstBufferPool * pool)
147 {
148 GstWlVideoBufferPool *self = GST_WL_VIDEO_BUFFER_POOL (pool);
149 gst_clear_object (&self->allocator);
150 gst_video_info_init (&self->vinfo);
151 return GST_BUFFER_POOL_CLASS (parent_class)->stop (pool);
152 }
153
154 static GstFlowReturn
155 gst_wl_video_buffer_pool_alloc_buffer (GstBufferPool * pool,
156 GstBuffer ** buffer, GstBufferPoolAcquireParams * params)
157 {
158 GstWlVideoBufferPool *self = GST_WL_VIDEO_BUFFER_POOL (pool);
159 guint32 pitch;
160 GstBuffer *buf;
161 GstMemory *drm_mem, *dma_mem;
162 gsize dumbbuf_size;
163 GstVideoFormat format = GST_VIDEO_INFO_FORMAT (&self->vinfo);
164 guint32 drm_fourcc = gst_video_format_to_wl_dmabuf_format (format);
165
166 if (!self->allocator)
167 return GST_BUFFER_POOL_CLASS (parent_class)->alloc_buffer (pool, buffer,
168 params);
169
170 drm_mem = gst_drm_dumb_allocator_alloc (self->allocator,
171 drm_fourcc, GST_VIDEO_INFO_WIDTH (&self->vinfo),
172 GST_VIDEO_INFO_HEIGHT (&self->vinfo), &pitch);
173 if (!drm_mem)
174 goto alloc_failed;
175
176 dma_mem = gst_drm_dumb_memory_export_dmabuf (drm_mem);
177
178 gst_memory_unref (drm_mem);
179 drm_mem = NULL;
180
181 gst_wl_update_video_info_from_pitch (&self->vinfo,
182 GST_VIDEO_INFO_N_PLANES (&self->vinfo), pitch);
183
184 gst_memory_get_sizes (dma_mem, NULL, &dumbbuf_size);
185 if (dumbbuf_size < GST_VIDEO_INFO_SIZE (&self->vinfo)) {
186 GST_ERROR_OBJECT (self,
187 "DUMB buffer has a size of %" G_GSIZE_FORMAT
188 " but we require at least %" G_GSIZE_FORMAT " to hold a frame",
189 dumbbuf_size, GST_VIDEO_INFO_SIZE (&self->vinfo));
190 goto buffer_too_small;
191 }
192
193 gst_memory_resize (dma_mem, 0, GST_VIDEO_INFO_SIZE (&self->vinfo));
194
195 buf = gst_buffer_new ();
196 gst_buffer_append_memory (buf, dma_mem);
197 gst_buffer_add_video_meta_full (buf, GST_VIDEO_FRAME_FLAG_NONE,
198 GST_VIDEO_INFO_FORMAT (&self->vinfo), GST_VIDEO_INFO_WIDTH (&self->vinfo),
199 GST_VIDEO_INFO_HEIGHT (&self->vinfo),
200 GST_VIDEO_INFO_N_PLANES (&self->vinfo),
201 self->vinfo.offset, self->vinfo.stride);
202
203 *buffer = buf;
204
205 return GST_FLOW_OK;
206
207 /* ERRORS */
208 alloc_failed:
209 {
210 GST_ERROR_OBJECT (self, "failed to allocate DRM Dumb buffer.");
211 return GST_FLOW_ERROR;
212 }
213 buffer_too_small:
214 {
215 GST_ERROR_OBJECT (self, "dumb buffer too small to store an image.");
216 return GST_FLOW_ERROR;
217 }
218 }
219
220 static const gchar **
221 gst_wl_video_buffer_pool_get_options (GstBufferPool * pool)
222 {
223 static const gchar *options[] = { GST_BUFFER_POOL_OPTION_VIDEO_META, NULL };
224 return options;
225 }
226
227 static void
228 gst_wl_video_buffer_pool_class_init (GstWlVideoBufferPoolClass * klass)
229 {
230 GstBufferPoolClass *pool_class = GST_BUFFER_POOL_CLASS (klass);
231 pool_class->get_options = gst_wl_video_buffer_pool_get_options;
232 pool_class->start = gst_wl_video_buffer_pool_start;
233 pool_class->stop = gst_wl_video_buffer_pool_stop;
234 pool_class->alloc_buffer = gst_wl_video_buffer_pool_alloc_buffer;
235 }
236
237 static void
238 gst_wl_video_buffer_pool_init (GstWlVideoBufferPool * self)
239 {
240 gst_video_info_init (&self->vinfo);
241 }
242
243 GstBufferPool *
244 gst_wl_video_buffer_pool_new (void)
245 {
246 return (GstBufferPool *) g_object_new (GST_TYPE_WL_VIDEO_BUFFER_POOL, NULL);
247 }