2 * Copyright (C) <2018-2019> Seungha Yang <seungha.yang@navercorp.com>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
24 #include "gstcudautils.h"
25 #include "gstcudacontext.h"
27 #ifdef HAVE_NVCODEC_GST_GL
28 #include <gst/gl/gl.h>
29 #include <gst/gl/gstglfuncs.h>
32 GST_DEBUG_CATEGORY_STATIC (gst_cuda_utils_debug);
33 #define GST_CAT_DEFAULT gst_cuda_utils_debug
34 GST_DEBUG_CATEGORY_STATIC (GST_CAT_CONTEXT);
39 static volatile gsize once_init = 0;
41 if (g_once_init_enter (&once_init)) {
43 GST_DEBUG_CATEGORY_INIT (gst_cuda_utils_debug, "cudautils", 0,
45 GST_DEBUG_CATEGORY_GET (GST_CAT_CONTEXT, "GST_CONTEXT");
46 g_once_init_leave (&once_init, 1);
51 pad_query (const GValue * item, GValue * value, gpointer user_data)
53 GstPad *pad = g_value_get_object (item);
54 GstQuery *query = user_data;
57 res = gst_pad_peer_query (pad, query);
60 g_value_set_boolean (value, TRUE);
64 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, pad, "pad peer query failed");
69 run_query (GstElement * element, GstQuery * query, GstPadDirection direction)
72 GstIteratorFoldFunction func = pad_query;
75 g_value_init (&res, G_TYPE_BOOLEAN);
76 g_value_set_boolean (&res, FALSE);
79 if (direction == GST_PAD_SRC)
80 it = gst_element_iterate_src_pads (element);
82 it = gst_element_iterate_sink_pads (element);
84 while (gst_iterator_fold (it, func, &res, query) == GST_ITERATOR_RESYNC)
85 gst_iterator_resync (it);
87 gst_iterator_free (it);
89 return g_value_get_boolean (&res);
93 find_cuda_context (GstElement * element, GstCudaContext ** cuda_ctx)
98 /* 1) Query downstream with GST_QUERY_CONTEXT for the context and
99 * check if upstream already has a context of the specific type
100 * 2) Query upstream as above.
102 query = gst_query_new_context (GST_CUDA_CONTEXT_TYPE);
103 if (run_query (element, query, GST_PAD_SRC)) {
104 gst_query_parse_context (query, &ctxt);
105 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
106 "found context (%p) in downstream query", ctxt);
107 gst_element_set_context (element, ctxt);
110 /* although we found cuda context above, the element does not want
111 * to use the context. Then try to find from the other direction */
112 if (*cuda_ctx == NULL && run_query (element, query, GST_PAD_SINK)) {
113 gst_query_parse_context (query, &ctxt);
114 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
115 "found context (%p) in upstream query", ctxt);
116 gst_element_set_context (element, ctxt);
119 if (*cuda_ctx == NULL) {
120 /* 3) Post a GST_MESSAGE_NEED_CONTEXT message on the bus with
121 * the required context type and afterwards check if a
122 * usable context was set now. The message could
123 * be handled by the parent bins of the element and the
128 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
129 "posting need context message");
130 msg = gst_message_new_need_context (GST_OBJECT_CAST (element),
131 GST_CUDA_CONTEXT_TYPE);
132 gst_element_post_message (element, msg);
136 * Whomever responds to the need-context message performs a
137 * GstElement::set_context() with the required context in which the element
138 * is required to update the cuda_ctx or call gst_cuda_handle_set_context().
141 gst_query_unref (query);
145 context_set_cuda_context (GstContext * context, GstCudaContext * cuda_ctx)
150 g_return_if_fail (context != NULL);
152 g_object_get (G_OBJECT (cuda_ctx), "cuda-device-id", &device_id, NULL);
154 GST_CAT_LOG (GST_CAT_CONTEXT,
155 "setting GstCudaContext(%" GST_PTR_FORMAT
156 ") with cuda-device-id %d on context(%" GST_PTR_FORMAT ")",
157 cuda_ctx, device_id, context);
159 s = gst_context_writable_structure (context);
160 gst_structure_set (s, GST_CUDA_CONTEXT_TYPE, GST_TYPE_CUDA_CONTEXT,
161 cuda_ctx, "cuda-device-id", G_TYPE_INT, device_id, NULL);
165 * gst_cuda_ensure_element_context:
166 * @element: the #GstElement running the query
167 * @device_id: preferred device-id, pass device_id >=0 when
168 * the device_id explicitly required. Otherwise, set -1.
169 * @cuda_ctx: (inout): the resulting #GstCudaContext
171 * Perform the steps necessary for retrieving a #GstCudaContext from the
172 * surrounding elements or from the application using the #GstContext mechanism.
174 * If the content of @cuda_ctx is not %NULL, then no #GstContext query is
175 * necessary for #GstCudaContext.
177 * Returns: whether a #GstCudaContext exists in @cuda_ctx
180 gst_cuda_ensure_element_context (GstElement * element, gint device_id,
181 GstCudaContext ** cuda_ctx)
183 g_return_val_if_fail (element != NULL, FALSE);
184 g_return_val_if_fail (cuda_ctx != NULL, FALSE);
191 find_cuda_context (element, cuda_ctx);
195 /* No available CUDA context in pipeline, create new one here */
196 *cuda_ctx = gst_cuda_context_new (device_id);
198 if (*cuda_ctx == NULL) {
199 GST_CAT_ERROR_OBJECT (GST_CAT_CONTEXT, element,
200 "Failed to create CUDA context with device-id %d", device_id);
206 /* Propagate new CUDA context */
208 context = gst_context_new (GST_CUDA_CONTEXT_TYPE, TRUE);
209 context_set_cuda_context (context, *cuda_ctx);
211 gst_element_set_context (element, context);
213 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
214 "posting have context (%p) message with CUDA context (%p)",
216 msg = gst_message_new_have_context (GST_OBJECT_CAST (element), context);
217 gst_element_post_message (GST_ELEMENT_CAST (element), msg);
224 * gst_cuda_handle_set_context:
225 * @element: a #GstElement
226 * @context: a #GstContext
227 * @device_id: preferred device-id, pass device_id >=0 when
228 * the device_id explicitly required. Otherwise, set -1.
229 * @cuda_ctx: (inout) (transfer full): location of a #GstCudaContext
231 * Helper function for implementing #GstElementClass.set_context() in
232 * CUDA capable elements.
234 * Retrieves the #GstCudaContext in @context and places the result in @cuda_ctx.
236 * Returns: whether the @cuda_ctx could be set successfully
239 gst_cuda_handle_set_context (GstElement * element,
240 GstContext * context, gint device_id, GstCudaContext ** cuda_ctx)
242 const gchar *context_type;
244 g_return_val_if_fail (element != NULL, FALSE);
245 g_return_val_if_fail (cuda_ctx != NULL, FALSE);
252 context_type = gst_context_get_context_type (context);
253 if (g_strcmp0 (context_type, GST_CUDA_CONTEXT_TYPE) == 0) {
254 const GstStructure *str;
255 GstCudaContext *other_ctx = NULL;
256 gint other_device_id = 0;
258 /* If we had context already, will not replace it */
262 str = gst_context_get_structure (context);
263 if (gst_structure_get (str, GST_CUDA_CONTEXT_TYPE, GST_TYPE_CUDA_CONTEXT,
265 g_object_get (other_ctx, "cuda-device-id", &other_device_id, NULL);
267 if (device_id == -1 || other_device_id == device_id) {
268 GST_CAT_DEBUG_OBJECT (GST_CAT_CONTEXT, element, "Found CUDA context");
269 *cuda_ctx = other_ctx;
274 gst_object_unref (other_ctx);
282 * gst_cuda_handle_context_query:
283 * @element: a #GstElement
284 * @query: a #GstQuery of type %GST_QUERY_CONTEXT
285 * @cuda_ctx: (transfer none) (nullable): a #GstCudaContext
287 * Returns: Whether the @query was successfully responded to from the passed
291 gst_cuda_handle_context_query (GstElement * element,
292 GstQuery * query, GstCudaContext * cuda_ctx)
294 const gchar *context_type;
295 GstContext *context, *old_context;
297 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
298 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
299 g_return_val_if_fail (cuda_ctx == NULL
300 || GST_IS_CUDA_CONTEXT (cuda_ctx), FALSE);
304 GST_CAT_LOG_OBJECT (GST_CAT_CONTEXT, element,
305 "handle context query %" GST_PTR_FORMAT, query);
306 gst_query_parse_context_type (query, &context_type);
308 if (cuda_ctx && g_strcmp0 (context_type, GST_CUDA_CONTEXT_TYPE) == 0) {
309 gst_query_parse_context (query, &old_context);
312 context = gst_context_copy (old_context);
314 context = gst_context_new (GST_CUDA_CONTEXT_TYPE, TRUE);
316 context_set_cuda_context (context, cuda_ctx);
317 gst_query_set_context (query, context);
318 gst_context_unref (context);
319 GST_CAT_DEBUG_OBJECT (GST_CAT_CONTEXT, element,
320 "successfully set %" GST_PTR_FORMAT " on %" GST_PTR_FORMAT, cuda_ctx,
330 * gst_context_new_cuda_context:
331 * @cuda_ctx: (transfer none) a #GstCudaContext
333 * Returns: (transfer full) (nullable): a new #GstContext embedding the @cuda_ctx
337 gst_context_new_cuda_context (GstCudaContext * cuda_ctx)
341 g_return_val_if_fail (GST_IS_CUDA_CONTEXT (cuda_ctx), NULL);
343 context = gst_context_new (GST_CUDA_CONTEXT_TYPE, TRUE);
344 context_set_cuda_context (context, cuda_ctx);
349 static const gchar *gst_cuda_quark_strings[] =
350 { "GstCudaQuarkGraphicsResource" };
352 static GQuark gst_cuda_quark_table[GST_CUDA_QUARK_MAX];
355 init_cuda_quark_once (void)
357 static volatile gsize once_init = 0;
359 if (g_once_init_enter (&once_init)) {
362 for (i = 0; i < GST_CUDA_QUARK_MAX; i++) {
363 gst_cuda_quark_table[i] =
364 g_quark_from_static_string (gst_cuda_quark_strings[i]);
366 g_once_init_leave (&once_init, 1);
372 * gst_cuda_quark_from_id: (skip)
373 * @id: a #GstCudaQuarkId
375 * Returns: the GQuark for given @id or 0 if @id is unknown value
378 gst_cuda_quark_from_id (GstCudaQuarkId id)
380 g_return_val_if_fail (id < GST_CUDA_QUARK_MAX, 0);
382 init_cuda_quark_once ();
385 return gst_cuda_quark_table[id];
389 * gst_cuda_graphics_resource_new: (skip)
390 * @context: (transfer none): a #GstCudaContext
391 * @graphics_context: (transfer none) (nullable): a graphics API specific context object
392 * @type: a #GstCudaGraphicsResourceType of resource registration
394 * Create new #GstCudaGraphicsResource with given @context and @type
396 * Returns: a new #GstCudaGraphicsResource.
397 * Free with gst_cuda_graphics_resource_free
399 GstCudaGraphicsResource *
400 gst_cuda_graphics_resource_new (GstCudaContext *
401 context, GstObject * graphics_context, GstCudaGraphicsResourceType type)
403 GstCudaGraphicsResource *resource;
405 g_return_val_if_fail (GST_IS_CUDA_CONTEXT (context), NULL);
409 resource = g_new0 (GstCudaGraphicsResource, 1);
410 resource->cuda_context = gst_object_ref (context);
411 if (graphics_context)
412 resource->graphics_context = gst_object_ref (graphics_context);
418 * gst_cuda_graphics_resource_register_gl_buffer: (skip)
419 * @resource a #GstCudaGraphicsResource
420 * @buffer: a GL buffer object
421 * @flags: a #CUgraphicsRegisterFlags
423 * Register the @buffer for access by CUDA.
424 * Must be called from the gl context thread with current cuda context was
425 * pushed on the current thread
427 * Returns: whether @buffer was registered or not
430 gst_cuda_graphics_resource_register_gl_buffer (GstCudaGraphicsResource *
431 resource, guint buffer, CUgraphicsRegisterFlags flags)
435 g_return_val_if_fail (resource != NULL, FALSE);
436 g_return_val_if_fail (resource->registered == FALSE, FALSE);
440 cuda_ret = CuGraphicsGLRegisterBuffer (&resource->resource, buffer, flags);
442 if (!gst_cuda_result (cuda_ret))
445 resource->registered = TRUE;
446 resource->type = GST_CUDA_GRAPHICS_RESOURCE_GL_BUFFER;
447 resource->flags = flags;
453 * gst_cuda_graphics_resource_unregister: (skip)
454 * @resource: a #GstCudaGraphicsResource
456 * Unregister previously registered resource.
457 * For GL resource, this method must be called from gl context thread.
458 * Also, current cuda context should be pushed on the current thread
459 * before calling this method.
462 gst_cuda_graphics_resource_unregister (GstCudaGraphicsResource * resource)
464 g_return_if_fail (resource != NULL);
468 if (!resource->registered)
471 gst_cuda_result (CuGraphicsUnregisterResource (resource->resource));
472 resource->resource = NULL;
473 resource->registered = FALSE;
479 * gst_cuda_graphics_resource_map: (skip)
480 * @resource: a #GstCudaGraphicsResource
481 * @stream: a #CUstream
482 * @flags: a #CUgraphicsMapResourceFlags
484 * Map previously registered resource with map flags
486 * Returns: the #CUgraphicsResource if successful or %NULL when failed
489 gst_cuda_graphics_resource_map (GstCudaGraphicsResource * resource,
490 CUstream stream, CUgraphicsMapResourceFlags flags)
494 g_return_val_if_fail (resource != NULL, NULL);
495 g_return_val_if_fail (resource->registered != FALSE, NULL);
499 cuda_ret = CuGraphicsResourceSetMapFlags (resource->resource, flags);
500 if (!gst_cuda_result (cuda_ret))
503 cuda_ret = CuGraphicsMapResources (1, &resource->resource, stream);
504 if (!gst_cuda_result (cuda_ret))
507 resource->mapped = TRUE;
509 return resource->resource;
513 * gst_cuda_graphics_resource_unmap: (skip)
514 * @resource: a #GstCudaGraphicsResource
515 * @stream: a #CUstream
517 * Unmap previously mapped resource
520 gst_cuda_graphics_resource_unmap (GstCudaGraphicsResource * resource,
523 g_return_if_fail (resource != NULL);
524 g_return_if_fail (resource->registered != FALSE);
528 if (!resource->mapped)
531 gst_cuda_result (CuGraphicsUnmapResources (1, &resource->resource, stream));
533 resource->mapped = FALSE;
536 #ifdef HAVE_NVCODEC_GST_GL
538 unregister_resource_from_gl_thread (GstGLContext * gl_context,
539 GstCudaGraphicsResource * resource)
541 GstCudaContext *cuda_context = resource->cuda_context;
543 if (!gst_cuda_context_push (cuda_context)) {
544 GST_WARNING_OBJECT (cuda_context, "failed to push CUDA context");
548 gst_cuda_graphics_resource_unregister (resource);
550 if (!gst_cuda_context_pop (NULL)) {
551 GST_WARNING_OBJECT (cuda_context, "failed to pop CUDA context");
557 * gst_cuda_graphics_resource_free: (skip)
558 * @resource: a #GstCudaGraphicsResource
563 gst_cuda_graphics_resource_free (GstCudaGraphicsResource * resource)
565 g_return_if_fail (resource != NULL);
567 if (resource->registered) {
568 #ifdef HAVE_NVCODEC_GST_GL
569 if (resource->type == GST_CUDA_GRAPHICS_RESOURCE_GL_BUFFER) {
570 gst_gl_context_thread_add ((GstGLContext *) resource->graphics_context,
571 (GstGLContextThreadFunc) unregister_resource_from_gl_thread,
576 /* FIXME: currently opengl only */
577 g_assert_not_reached ();
581 gst_object_unref (resource->cuda_context);
582 if (resource->graphics_context)
583 gst_object_unref (resource->graphics_context);