• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) <2018-2019> Seungha Yang <seungha.yang@navercorp.com>
3  *
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.
8  *
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.
13  *
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.
18  */
19 
20 #ifndef __GST_CUDA_UTILS_H__
21 #define __GST_CUDA_UTILS_H__
22 
23 #include <gst/gst.h>
24 #include "gstcudaloader.h"
25 #include "gstcudacontext.h"
26 
27 G_BEGIN_DECLS
28 
29 #ifndef GST_DISABLE_GST_DEBUG
30 static inline gboolean
_gst_cuda_debug(CUresult result,GstDebugCategory * category,const gchar * file,const gchar * function,gint line)31 _gst_cuda_debug(CUresult result, GstDebugCategory * category,
32     const gchar * file, const gchar * function, gint line)
33 {
34   const gchar *_error_name, *_error_text;
35   if (result != CUDA_SUCCESS) {
36     CuGetErrorName (result, &_error_name);
37     CuGetErrorString (result, &_error_text);
38     gst_debug_log (category, GST_LEVEL_WARNING, file, function, line,
39         NULL, "CUDA call failed: %s, %s", _error_name, _error_text);
40 
41     return FALSE;
42   }
43 
44   return TRUE;
45 }
46 
47 /**
48  * gst_cuda_result:
49  * @result: CUDA device API return code #CUresult
50  *
51  * Returns: %TRUE if CUDA device API call result is CUDA_SUCCESS
52  */
53 #define gst_cuda_result(result) \
54   _gst_cuda_debug(result, GST_CAT_DEFAULT, __FILE__, GST_FUNCTION, __LINE__)
55 #else
56 
57 static inline gboolean
58 _gst_cuda_debug(CUresult result, GstDebugCategory * category,
59     const gchar * file, const gchar * function, gint line)
60 {
61   return result == CUDA_SUCCESS;
62 }
63 
64 /**
65  * gst_cuda_result:
66  * @result: CUDA device API return code #CUresult
67  *
68  * Returns: %TRUE if CUDA device API call result is CUDA_SUCCESS
69  */
70 #define gst_cuda_result(result) \
71   _gst_cuda_debug(result, NULL, __FILE__, GST_FUNCTION, __LINE__)
72 #endif
73 
74 typedef enum
75 {
76   GST_CUDA_QUARK_GRAPHICS_RESOURCE = 0,
77 
78   /* end of quark list */
79   GST_CUDA_QUARK_MAX = 1
80 } GstCudaQuarkId;
81 
82 typedef enum
83 {
84   GST_CUDA_GRAPHICS_RESOURCE_NONE = 0,
85   GST_CUDA_GRAPHICS_RESOURCE_GL_BUFFER = 1,
86 } GstCudaGraphicsResourceType;
87 
88 typedef struct _GstCudaGraphicsResource
89 {
90   GstCudaContext *cuda_context;
91   /* GL context (or d3d11 context in the future) */
92   GstObject *graphics_context;
93 
94   GstCudaGraphicsResourceType type;
95   CUgraphicsResource resource;
96   CUgraphicsRegisterFlags flags;
97 
98   gboolean registered;
99   gboolean mapped;
100 } GstCudaGraphicsResource;
101 
102 gboolean        gst_cuda_ensure_element_context (GstElement * element,
103                                                  gint device_id,
104                                                  GstCudaContext ** cuda_ctx);
105 
106 gboolean        gst_cuda_handle_set_context     (GstElement * element,
107                                                  GstContext * context,
108                                                  gint device_id,
109                                                  GstCudaContext ** cuda_ctx);
110 
111 gboolean        gst_cuda_handle_context_query   (GstElement * element,
112                                                  GstQuery * query,
113                                                  GstCudaContext * cuda_ctx);
114 
115 GstContext *    gst_context_new_cuda_context    (GstCudaContext * context);
116 
117 GQuark          gst_cuda_quark_from_id          (GstCudaQuarkId id);
118 
119 GstCudaGraphicsResource * gst_cuda_graphics_resource_new  (GstCudaContext * context,
120                                                            GstObject * graphics_context,
121                                                            GstCudaGraphicsResourceType type);
122 
123 gboolean        gst_cuda_graphics_resource_register_gl_buffer (GstCudaGraphicsResource * resource,
124                                                                guint buffer,
125                                                                CUgraphicsRegisterFlags flags);
126 
127 void            gst_cuda_graphics_resource_unregister (GstCudaGraphicsResource * resource);
128 
129 CUgraphicsResource gst_cuda_graphics_resource_map (GstCudaGraphicsResource * resource,
130                                                    CUstream stream,
131                                                    CUgraphicsMapResourceFlags flags);
132 
133 void            gst_cuda_graphics_resource_unmap (GstCudaGraphicsResource * resource,
134                                                   CUstream stream);
135 
136 void            gst_cuda_graphics_resource_free (GstCudaGraphicsResource * resource);
137 
138 G_END_DECLS
139 
140 #endif /* __GST_CUDA_UTILS_H__ */
141