Debian Codegraph @

gst-libs/gst/wayland/gstwlshmallocator.c

raw
1 /* GStreamer Wayland Library
2 *
3 * Copyright (C) 2012 Intel Corporation
4 * Copyright (C) 2012 Sreerenj Balachandran <[email protected]>
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 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include "gstwlshmallocator.h"
28
29 GST_DEBUG_CATEGORY (gst_wl_shm_debug);
30 #define GST_CAT_DEFAULT gst_wl_shm_debug
31
32 void
33 gst_wl_shm_init_once (void)
34 {
35 static gsize _init = 0;
36
37 if (g_once_init_enter (&_init)) {
38 GST_DEBUG_CATEGORY_INIT (gst_wl_shm_debug, "wl_shm", 0, "wl_shm library");
39 g_once_init_leave (&_init, 1);
40 }
41 }
42
43 static gboolean
44 gst_wl_shm_validate_video_info (const GstVideoInfo * vinfo)
45 {
46 gint height = GST_VIDEO_INFO_HEIGHT (vinfo);
47 gint base_stride = GST_VIDEO_INFO_PLANE_STRIDE (vinfo, 0);
48 gsize base_offs = GST_VIDEO_INFO_PLANE_OFFSET (vinfo, 0);
49 gint i;
50 gsize offs = 0;
51
52 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (vinfo); i++) {
53 guint32 estride;
54
55 /* Overwrite the video info's stride and offset using the pitch calculcated
56 * by the kms driver. */
57 estride = gst_video_format_info_extrapolate_stride (vinfo->finfo, i,
58 base_stride);
59
60 if (estride != GST_VIDEO_INFO_PLANE_STRIDE (vinfo, i))
61 return FALSE;
62
63 if (GST_VIDEO_INFO_PLANE_OFFSET (vinfo, i) - base_offs != offs)
64 return FALSE;
65
66 /* Note that we cannot negotiate special padding betweem each planes,
67 * hence using the display height here. */
68 offs +=
69 estride * GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (vinfo->finfo, i, height);
70 }
71
72 if (vinfo->size < offs)
73 return FALSE;
74
75 return TRUE;
76 }
77
78 struct wl_buffer *
79 gst_wl_shm_memory_construct_wl_buffer (GstMemory * mem, GstWlDisplay * display,
80 const GstVideoInfo * info)
81 {
82 gint width, height, stride;
83 gsize offset, size, memsize, maxsize;
84 enum wl_shm_format format;
85 struct wl_shm_pool *wl_pool;
86 struct wl_buffer *wbuffer;
87
88 if (!gst_wl_shm_validate_video_info (info)) {
89 GST_DEBUG_OBJECT (display, "Unsupported strides and offsets.");
90 return NULL;
91 }
92
93 width = GST_VIDEO_INFO_WIDTH (info);
94 height = GST_VIDEO_INFO_HEIGHT (info);
95 stride = GST_VIDEO_INFO_PLANE_STRIDE (info, 0);
96 size = GST_VIDEO_INFO_SIZE (info);
97 format = gst_video_format_to_wl_shm_format (GST_VIDEO_INFO_FORMAT (info));
98
99 memsize = gst_memory_get_sizes (mem, &offset, &maxsize);
100 offset += GST_VIDEO_INFO_PLANE_OFFSET (info, 0);
101
102 g_return_val_if_fail (gst_is_fd_memory (mem), NULL);
103 g_return_val_if_fail (size <= memsize, NULL);
104 g_return_val_if_fail (gst_wl_display_check_format_for_shm (display, info),
105 NULL);
106
107 GST_DEBUG_OBJECT (display, "Creating wl_buffer from SHM of size %"
108 G_GSSIZE_FORMAT " (%d x %d, stride %d), format %s", size, width, height,
109 stride, gst_wl_shm_format_to_string (format));
110
111 wl_pool = wl_shm_create_pool (gst_wl_display_get_shm (display),
112 gst_fd_memory_get_fd (mem), memsize);
113 wbuffer = wl_shm_pool_create_buffer (wl_pool, offset, width, height, stride,
114 format);
115 wl_shm_pool_destroy (wl_pool);
116
117 return wbuffer;
118 }