Debian Codegraph @

gst-libs/gst/va/gstvautils.c

raw symbols SCIP
1 /* GStreamer
2 * Copyright (C) 2020 Igalia, S.L.
3 * Author: Víctor Jáquez <[email protected]m>
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 /**
22 * SECTION:gstvautils
23 * @title: GstVaUtils
24 * @short_description: Utility functions for context handling
25 * @sources:
26 * - gstvautils.h
27 */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include "gstvautils.h"
34
35 #ifdef G_OS_WIN32
36 #include "gstvadisplay_win32.h"
37 #else
38 #include "gstvadisplay_drm.h"
39 #endif
40 #include "gstvadisplay_wrapped.h"
41
42 GST_DEBUG_CATEGORY_STATIC (GST_CAT_CONTEXT);
43
44 static void
45 _init_context_debug (void)
46 {
47 #ifndef GST_DISABLE_GST_DEBUG
48 static gsize _init = 0;
49
50 if (g_once_init_enter (&_init)) {
51 GST_DEBUG_CATEGORY_GET (GST_CAT_CONTEXT, "GST_CONTEXT");
52 g_once_init_leave (&_init, 1);
53 }
54 #endif
55 }
56
57 static gboolean
58 gst_va_display_found (GstElement * element, GstVaDisplay * display)
59 {
60 if (display) {
61 GST_CAT_LOG_OBJECT (GST_CAT_CONTEXT, element, "already have a display (%p)",
62 display);
63 return TRUE;
64 }
65
66 return FALSE;
67 }
68
69 static gboolean
70 pad_query (const GValue * item, GValue * value, gpointer user_data)
71 {
72 GstPad *pad = g_value_get_object (item);
73 GstQuery *query = user_data;
74 gboolean res;
75
76 res = gst_pad_peer_query (pad, query);
77
78 if (res) {
79 g_value_set_boolean (value, TRUE);
80 return FALSE;
81 }
82
83 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, pad, "pad peer query failed");
84 return TRUE;
85 }
86
87 static gboolean
88 _gst_va_run_query (GstElement * element, GstQuery * query,
89 GstPadDirection direction)
90 {
91 GstIterator *it;
92 GstIteratorFoldFunction func = pad_query;
93 GValue res = G_VALUE_INIT;
94
95 g_value_init (&res, G_TYPE_BOOLEAN);
96 g_value_set_boolean (&res, FALSE);
97
98 if (direction == GST_PAD_SRC)
99 it = gst_element_iterate_src_pads (element);
100 else
101 it = gst_element_iterate_sink_pads (element);
102
103 while (gst_iterator_fold (it, func, &res, query) == GST_ITERATOR_RESYNC)
104 gst_iterator_resync (it);
105
106 gst_iterator_free (it);
107
108 return g_value_get_boolean (&res);
109 }
110
111 /**
112 * gst_va_context_query:
113 * @element: a #GstElement
114 * @context_type: the #gchar string specify the context type name
115 *
116 * Query the specified context type name.
117 *
118 * Since: 1.22
119 **/
120 void
121 gst_va_context_query (GstElement * element, const gchar * context_type)
122 {
123 GstQuery *query;
124 GstContext *ctxt = NULL;
125
126 _init_context_debug ();
127
128 /* 2a) Query downstream with GST_QUERY_CONTEXT for the context and
129 * check if downstream already has a context of the specific type
130 * 2b) Query upstream as above.
131 */
132 query = gst_query_new_context (context_type);
133 if (_gst_va_run_query (element, query, GST_PAD_SRC)) {
134 gst_query_parse_context (query, &ctxt);
135 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
136 "found context (%p) in downstream query", ctxt);
137 gst_element_set_context (element, ctxt);
138 } else if (_gst_va_run_query (element, query, GST_PAD_SINK)) {
139 gst_query_parse_context (query, &ctxt);
140 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
141 "found context (%p) in upstream query", ctxt);
142 gst_element_set_context (element, ctxt);
143 } else {
144 /* 3) Post a GST_MESSAGE_NEED_CONTEXT message on the bus with
145 * the required context type and afterwards check if a
146 * usable context was set now as in 1). The message could
147 * be handled by the parent bins of the element and the
148 * application.
149 */
150 GstMessage *msg;
151
152 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
153 "posting need context message");
154 msg = gst_message_new_need_context (GST_OBJECT_CAST (element),
155 context_type);
156 gst_element_post_message (element, msg);
157 }
158
159 /*
160 * Whomever responds to the need-context message performs a
161 * GstElement::set_context() with the required context in which the element
162 * is required to update the display_ptr or call gst_va_handle_set_context().
163 */
164
165 gst_query_unref (query);
166 }
167
168 /**
169 * gst_va_element_propagate_display_context:
170 * @element: a #GstElement
171 * @display: the #GstVaDisplay to propagate
172 *
173 * Propagate @display by posting it as #GstContext in the pipeline's bus.
174 *
175 * Since: 1.22
176 **/
177 void
178 gst_va_element_propagate_display_context (GstElement * element,
179 GstVaDisplay * display)
180 {
181 GstContext *ctxt;
182 GstMessage *msg;
183
184 _init_context_debug ();
185
186 if (!display) {
187 GST_ERROR_OBJECT (element, "Could not get VA display connection");
188 return;
189 }
190
191 /* 4) Create a context by itself and post a GST_MESSAGE_HAVE_CONTEXT
192 * message.
193 */
194 ctxt = gst_context_new (GST_VA_DISPLAY_HANDLE_CONTEXT_TYPE_STR, TRUE);
195 gst_context_set_va_display (ctxt, display);
196
197 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
198 "post have context (%p) message with display (%p)", ctxt, display);
199 msg = gst_message_new_have_context (GST_OBJECT_CAST (element), ctxt);
200 gst_element_post_message (element, msg);
201 }
202
203 /**
204 * gst_va_ensure_element_data:
205 * @element: a #GstElement
206 * @render_device_path: the #gchar string of render device path
207 * @display_ptr: (out) (transfer full): The #GstVaDisplay to ensure
208 *
209 * Called by the va element to ensure a valid #GstVaDisplay.
210 *
211 * Returns: whether a #GstVaDisplay exists in @display_ptr
212 *
213 * Since: 1.22
214 **/
215 gboolean
216 gst_va_ensure_element_data (gpointer element, const gchar * render_device_path,
217 GstVaDisplay ** display_ptr)
218 {
219 GstVaDisplay *display;
220
221 _init_context_debug ();
222
223 g_return_val_if_fail (element, FALSE);
224 g_return_val_if_fail (render_device_path, FALSE);
225 g_return_val_if_fail (display_ptr, FALSE);
226
227 /* 1) Check if the element already has a context of the specific
228 * type.
229 */
230 if (gst_va_display_found (element, g_atomic_pointer_get (display_ptr)))
231 goto done;
232
233 gst_va_context_query (element, GST_VA_DISPLAY_HANDLE_CONTEXT_TYPE_STR);
234
235 /* Neighbour found and it updated the display. */
236 if (gst_va_display_found (element, g_atomic_pointer_get (display_ptr)))
237 goto done;
238
239 #ifdef G_OS_WIN32
240 display = gst_va_display_win32_new (render_device_path);
241 #else
242 /* If no neighbor, or application not interested, use drm. */
243 display = gst_va_display_drm_new_from_path (render_device_path);
244 #endif
245
246 gst_object_replace ((GstObject **) display_ptr, (GstObject *) display);
247
248 gst_va_element_propagate_display_context (element, display);
249
250 gst_clear_object (&display);
251
252 done:
253 return g_atomic_pointer_get (display_ptr) != NULL;
254 }
255
256 /**
257 * gst_va_handle_set_context:
258 * @element: a #GstElement
259 * @context: a #GstContext may contain the display
260 * @render_device_path: the #gchar string of render device path
261 * @display_ptr: (out) (transfer full): The #GstVaDisplay to set
262 *
263 * Called by elements in their #GstElementClass::set_context vmethod.
264 * It gets a valid #GstVaDisplay if @context has it.
265 *
266 * Returns: whether the @display_ptr could be successfully set to a
267 * valid #GstVaDisplay in the @context
268 *
269 * Since: 1.22
270 **/
271 gboolean
272 gst_va_handle_set_context (GstElement * element, GstContext * context,
273 const gchar * render_device_path, GstVaDisplay ** display_ptr)
274 {
275 GstVaDisplay *display_replacement = NULL;
276 const gchar *context_type, *type_name;
277
278 _init_context_debug ();
279
280 g_return_val_if_fail (display_ptr, FALSE);
281
282 if (!context)
283 return FALSE;
284
285 context_type = gst_context_get_context_type (context);
286
287 if (g_strcmp0 (context_type, GST_VA_DISPLAY_HANDLE_CONTEXT_TYPE_STR) == 0) {
288 type_name = G_OBJECT_TYPE_NAME (element);
289 if (!gst_context_get_va_display (context, type_name, render_device_path,
290 &display_replacement)) {
291 GST_CAT_WARNING_OBJECT (GST_CAT_CONTEXT, element,
292 "Failed to get display from context");
293 return FALSE;
294 }
295 }
296
297 if (display_replacement) {
298 gst_object_replace ((GstObject **) display_ptr,
299 (GstObject *) display_replacement);
300 gst_object_unref (display_replacement);
301 }
302
303 return TRUE;
304 }
305
306 /**
307 * gst_va_handle_context_query:
308 * @element: a #GstElement
309 * @query: a #GstQuery to query the context
310 * @display: a #GstVaDisplay to answer the query
311 *
312 * Used by elements when processing their pad's queries, propagating
313 * element's #GstVaDisplay if the processed query requests it.
314 *
315 * Returns: whether we can handle the context query successfully
316 *
317 * Since: 1.22
318 **/
319 gboolean
320 gst_va_handle_context_query (GstElement * element, GstQuery * query,
321 GstVaDisplay * display)
322 {
323 const gchar *context_type;
324 GstContext *ctxt, *old_ctxt;
325
326 _init_context_debug ();
327
328 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
329 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
330 g_return_val_if_fail (!display || GST_IS_VA_DISPLAY (display), FALSE);
331
332 GST_CAT_LOG_OBJECT (GST_CAT_CONTEXT, element,
333 "handle context query %" GST_PTR_FORMAT, query);
334 gst_query_parse_context_type (query, &context_type);
335
336 if (!display
337 || g_strcmp0 (context_type, GST_VA_DISPLAY_HANDLE_CONTEXT_TYPE_STR) != 0)
338 return FALSE;
339
340 gst_query_parse_context (query, &old_ctxt);
341
342 if (old_ctxt)
343 ctxt = gst_context_copy (old_ctxt);
344 else
345 ctxt = gst_context_new (GST_VA_DISPLAY_HANDLE_CONTEXT_TYPE_STR, TRUE);
346
347 gst_context_set_va_display (ctxt, display);
348 gst_query_set_context (query, ctxt);
349 gst_context_unref (ctxt);
350 GST_CAT_DEBUG_OBJECT (GST_CAT_CONTEXT, element,
351 "successfully %" GST_PTR_FORMAT " on %" GST_PTR_FORMAT, display, query);
352
353 return TRUE;
354 }
355
356 /**
357 * gst_context_get_va_display:
358 * @context: a #GstContext may contain the display
359 * @type_name: a #gchar string of the element type
360 * @render_device_path: the #gchar string of render device path
361 * @display_ptr: (out) (transfer full): the #GstVaDisplay we get
362 *
363 * Returns: whether we find a valid @display in the @context
364 *
365 * Since: 1.22
366 **/
367 gboolean
368 gst_context_get_va_display (GstContext * context, const gchar * type_name,
369 const gchar * render_device_path, GstVaDisplay ** display_ptr)
370 {
371 const GstStructure *s;
372 GstVaDisplay *display = NULL;
373 gpointer dpy;
374 gboolean is_devnode;
375
376 _init_context_debug ();
377
378 g_return_val_if_fail (display_ptr, FALSE);
379 g_return_val_if_fail (context, FALSE);
380
381 is_devnode = (g_strstr_len (type_name, -1, "renderD") != NULL);
382
383 s = gst_context_get_structure (context);
384 if (gst_structure_get (s, "gst-display", GST_TYPE_OBJECT, &display, NULL)) {
385 gboolean ret;
386 gchar *device_path = NULL;
387 #ifdef G_OS_WIN32
388 if (GST_IS_VA_DISPLAY_WIN32 (display)) {
389 #else
390 if (GST_IS_VA_DISPLAY_DRM (display)) {
391 #endif
392 g_object_get (display, "path", &device_path, NULL);
393 ret = (g_strcmp0 (device_path, render_device_path) == 0);
394 g_free (device_path);
395 if (ret)
396 goto accept;
397 } else if (GST_IS_VA_DISPLAY (display) && !is_devnode) {
398 goto accept;
399 }
400
401 /* let's try other fields */
402 gst_clear_object (&display);
403 }
404
405 /* if element is render device node specific, it doesn't accept
406 * VADisplay from users */
407 if (!is_devnode
408 && gst_structure_get (s, "va-display", G_TYPE_POINTER, &dpy, NULL)) {
409 if ((display = gst_va_display_wrapped_new (dpy)))
410 goto accept;
411 }
412
413 GST_CAT_DEBUG (GST_CAT_CONTEXT, "No valid GstVaDisplay from context (%p)",
414 context);
415 return FALSE;
416
417 accept:
418 {
419 *display_ptr = display;
420
421 GST_CAT_LOG (GST_CAT_CONTEXT, "got GstVaDisplay (%p) from context (%p)",
422 *display_ptr, context);
423 return TRUE;
424 }
425 }
426
427 /**
428 * gst_context_set_va_display:
429 * @context: a #GstContext
430 * @display: the #GstVaDisplay we want to set
431 *
432 * Set the @display in the @context
433 *
434 * Since: 1.22
435 */
436 void
437 gst_context_set_va_display (GstContext * context, GstVaDisplay * display)
438 {
439 GstStructure *s;
440
441 _init_context_debug ();
442
443 g_return_if_fail (context != NULL);
444
445 s = gst_context_writable_structure (context);
446 gst_structure_set (s, "gst-display", GST_TYPE_OBJECT, display, NULL);
447
448 if (display) {
449 GObjectClass *klass = G_OBJECT_GET_CLASS (display);
450 gchar *vendor_desc = NULL;
451 gchar *path = NULL;
452
453 g_object_get (display, "description", &vendor_desc, NULL);
454 if (g_object_class_find_property (klass, "path"))
455 g_object_get (display, "path", &path, NULL);
456
457 GST_CAT_LOG (GST_CAT_CONTEXT,
458 "setting GstVaDisplay (%" GST_PTR_FORMAT ") on context (%"
459 GST_PTR_FORMAT "), description: \"%s\", path: %s", display, context,
460 GST_STR_NULL (vendor_desc), GST_STR_NULL (path));
461
462 if (vendor_desc) {
463 gst_structure_set (s, "description", G_TYPE_STRING, vendor_desc, NULL);
464 g_free (vendor_desc);
465 }
466
467 if (path) {
468 gst_structure_set (s, "path", G_TYPE_STRING, path, NULL);
469 g_free (path);
470 }
471 }
472 }