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 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "gstcudaloader.h"
25 #include "gstcudacontext.h"
26 #include "gstcudautils.h"
27
28 GST_DEBUG_CATEGORY_STATIC (gst_cuda_context_debug);
29 #define GST_CAT_DEFAULT gst_cuda_context_debug
30
31 /* store all context object with weak ref */
32 static GList *context_list = NULL;
33 G_LOCK_DEFINE_STATIC (list_lock);
34
35 enum
36 {
37 PROP_0,
38 PROP_DEVICE_ID
39 };
40
41 #define DEFAULT_DEVICE_ID -1
42
43 struct _GstCudaContextPrivate
44 {
45 CUcontext context;
46 CUdevice device;
47 gint device_id;
48
49 gint tex_align;
50
51 GHashTable *accessible_peer;
52 };
53
54 #define gst_cuda_context_parent_class parent_class
55 G_DEFINE_TYPE_WITH_PRIVATE (GstCudaContext, gst_cuda_context, GST_TYPE_OBJECT);
56
57 static void gst_cuda_context_set_property (GObject * object, guint prop_id,
58 const GValue * value, GParamSpec * pspec);
59 static void gst_cuda_context_get_property (GObject * object, guint prop_id,
60 GValue * value, GParamSpec * pspec);
61 static void gst_cuda_context_constructed (GObject * object);
62 static void gst_cuda_context_finalize (GObject * object);
63 static void gst_cuda_context_weak_ref_notify (gpointer data,
64 GstCudaContext * context);
65 static void gst_cuda_context_enable_peer_access (GstCudaContext * context,
66 GstCudaContext * peer);
67
68 static void
gst_cuda_context_class_init(GstCudaContextClass * klass)69 gst_cuda_context_class_init (GstCudaContextClass * klass)
70 {
71 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
72
73 gobject_class->set_property = gst_cuda_context_set_property;
74 gobject_class->get_property = gst_cuda_context_get_property;
75 gobject_class->constructed = gst_cuda_context_constructed;
76 gobject_class->finalize = gst_cuda_context_finalize;
77
78 g_object_class_install_property (gobject_class, PROP_DEVICE_ID,
79 g_param_spec_int ("cuda-device-id", "Cuda Device ID",
80 "Set the GPU device to use for operations (-1 = auto)",
81 -1, G_MAXINT, DEFAULT_DEVICE_ID,
82 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
83
84 GST_DEBUG_CATEGORY_INIT (gst_cuda_context_debug,
85 "cudacontext", 0, "CUDA Context");
86 }
87
88 static void
gst_cuda_context_init(GstCudaContext * context)89 gst_cuda_context_init (GstCudaContext * context)
90 {
91 GstCudaContextPrivate *priv = gst_cuda_context_get_instance_private (context);
92
93 priv->context = NULL;
94 priv->device_id = DEFAULT_DEVICE_ID;
95 priv->accessible_peer = g_hash_table_new (g_direct_hash, g_direct_equal);
96
97 context->priv = priv;
98 }
99
100 static void
gst_cuda_context_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)101 gst_cuda_context_set_property (GObject * object, guint prop_id,
102 const GValue * value, GParamSpec * pspec)
103 {
104 GstCudaContext *context = GST_CUDA_CONTEXT (object);
105 GstCudaContextPrivate *priv = context->priv;
106
107 switch (prop_id) {
108 case PROP_DEVICE_ID:
109 priv->device_id = g_value_get_int (value);
110 break;
111 default:
112 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
113 break;
114 }
115 }
116
117 static void
gst_cuda_context_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)118 gst_cuda_context_get_property (GObject * object, guint prop_id,
119 GValue * value, GParamSpec * pspec)
120 {
121 GstCudaContext *context = GST_CUDA_CONTEXT (object);
122 GstCudaContextPrivate *priv = context->priv;
123
124 switch (prop_id) {
125 case PROP_DEVICE_ID:
126 g_value_set_int (value, priv->device_id);
127 break;
128 default:
129 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
130 break;
131 }
132 }
133
134 static void
gst_cuda_context_constructed(GObject * object)135 gst_cuda_context_constructed (GObject * object)
136 {
137 static gsize once = 0;
138 GstCudaContext *context = GST_CUDA_CONTEXT (object);
139 GstCudaContextPrivate *priv = context->priv;
140 CUcontext cuda_ctx, old_ctx;
141 gboolean ret = TRUE;
142 CUdevice cdev = 0, cuda_dev = -1;
143 gint dev_count = 0;
144 gchar name[256];
145 gint min = 0, maj = 0;
146 gint i;
147 gint tex_align = 0;
148 GList *iter;
149
150 if (g_once_init_enter (&once)) {
151 if (CuInit (0) != CUDA_SUCCESS) {
152 GST_ERROR_OBJECT (context, "Failed to cuInit");
153 ret = FALSE;
154 }
155 g_once_init_leave (&once, ret);
156
157 if (!ret)
158 return;
159 }
160
161 if (!gst_cuda_result (CuDeviceGetCount (&dev_count)) || dev_count == 0) {
162 GST_WARNING ("No CUDA devices detected");
163 return;
164 }
165
166 for (i = 0; i < dev_count; ++i) {
167 if (gst_cuda_result (CuDeviceGet (&cdev, i)) &&
168 gst_cuda_result (CuDeviceGetName (name, sizeof (name), cdev)) &&
169 gst_cuda_result (CuDeviceGetAttribute (&maj,
170 CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, cdev)) &&
171 gst_cuda_result (CuDeviceGetAttribute (&min,
172 CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, cdev)) &&
173 gst_cuda_result (CuDeviceGetAttribute (&tex_align,
174 CU_DEVICE_ATTRIBUTE_TEXTURE_ALIGNMENT, cdev))) {
175 GST_INFO ("GPU #%d supports NVENC: %s (%s) (Compute SM %d.%d)", i,
176 (((maj << 4) + min) >= 0x30) ? "yes" : "no", name, maj, min);
177 if (priv->device_id == -1 || priv->device_id == cdev) {
178 priv->device_id = cuda_dev = cdev;
179 priv->tex_align = tex_align;
180 break;
181 }
182 }
183 }
184
185 if (cuda_dev == -1) {
186 GST_WARNING ("Device with id %d does not exist", priv->device_id);
187 return;
188 }
189
190 GST_DEBUG ("Creating cuda context for device index %d", cuda_dev);
191
192 if (!gst_cuda_result (CuCtxCreate (&cuda_ctx, 0, cuda_dev))) {
193 GST_WARNING ("Failed to create CUDA context for cuda device %d", cuda_dev);
194 return;
195 }
196
197 if (!gst_cuda_result (CuCtxPopCurrent (&old_ctx))) {
198 return;
199 }
200
201 GST_INFO ("Created CUDA context %p with device-id %d", cuda_ctx, cuda_dev);
202
203 priv->context = cuda_ctx;
204 priv->device = cuda_dev;
205
206 G_LOCK (list_lock);
207 g_object_weak_ref (G_OBJECT (object),
208 (GWeakNotify) gst_cuda_context_weak_ref_notify, NULL);
209 for (iter = context_list; iter; iter = g_list_next (iter)) {
210 GstCudaContext *peer = (GstCudaContext *) iter->data;
211
212 /* EnablePeerAccess is unidirectional */
213 gst_cuda_context_enable_peer_access (context, peer);
214 gst_cuda_context_enable_peer_access (peer, context);
215 }
216
217 context_list = g_list_append (context_list, context);
218 G_UNLOCK (list_lock);
219 }
220
221 /* must be called with list_lock taken */
222 static void
gst_cuda_context_enable_peer_access(GstCudaContext * context,GstCudaContext * peer)223 gst_cuda_context_enable_peer_access (GstCudaContext * context,
224 GstCudaContext * peer)
225 {
226 GstCudaContextPrivate *priv = context->priv;
227 GstCudaContextPrivate *peer_priv = peer->priv;
228 CUdevice device = priv->device;
229 CUdevice other_dev = peer_priv->device;
230 CUresult cuda_ret;
231 gint can_access = 0;
232
233 cuda_ret = CuDeviceCanAccessPeer (&can_access, device, other_dev);
234
235 if (!gst_cuda_result (cuda_ret) || !can_access) {
236 GST_DEBUG_OBJECT (context,
237 "Peer access to %" GST_PTR_FORMAT " is not allowed", peer);
238 return;
239 }
240
241 gst_cuda_context_push (context);
242 if (gst_cuda_result (CuCtxEnablePeerAccess (peer_priv->context, 0))) {
243 GST_DEBUG_OBJECT (context, "Enable peer access to %" GST_PTR_FORMAT, peer);
244 g_hash_table_add (priv->accessible_peer, peer);
245 }
246
247 gst_cuda_context_pop (NULL);
248 }
249
250 static void
gst_cuda_context_weak_ref_notify(gpointer data,GstCudaContext * context)251 gst_cuda_context_weak_ref_notify (gpointer data, GstCudaContext * context)
252 {
253 GList *iter;
254
255 G_LOCK (list_lock);
256 context_list = g_list_remove (context_list, context);
257
258 /* disable self -> peer access */
259 if (context->priv->accessible_peer) {
260 GHashTableIter iter;
261 gpointer key;
262 g_hash_table_iter_init (&iter, context->priv->accessible_peer);
263 if (gst_cuda_context_push (context)) {
264 while (g_hash_table_iter_next (&iter, &key, NULL)) {
265 GstCudaContext *peer = GST_CUDA_CONTEXT (key);
266 CUcontext peer_handle = gst_cuda_context_get_handle (peer);
267 GST_DEBUG_OBJECT (context,
268 "Disable peer access to %" GST_PTR_FORMAT, peer);
269 gst_cuda_result (CuCtxDisablePeerAccess (peer_handle));
270 }
271 gst_cuda_context_pop (NULL);
272 }
273
274 g_hash_table_destroy (context->priv->accessible_peer);
275 context->priv->accessible_peer = NULL;
276 }
277
278 /* disable peer -> self access */
279 for (iter = context_list; iter; iter = g_list_next (iter)) {
280 GstCudaContext *other = (GstCudaContext *) iter->data;
281 GstCudaContextPrivate *other_priv = other->priv;
282 CUcontext self_handle;
283
284 if (!other_priv->accessible_peer)
285 continue;
286
287 if (g_hash_table_lookup (other_priv->accessible_peer, context)) {
288 if (gst_cuda_context_push (other)) {
289 self_handle = gst_cuda_context_get_handle (context);
290 GST_DEBUG_OBJECT (other,
291 "Disable peer access to %" GST_PTR_FORMAT, context);
292 gst_cuda_result (CuCtxDisablePeerAccess (self_handle));
293 gst_cuda_context_pop (NULL);
294 }
295
296 g_hash_table_remove (other_priv->accessible_peer, context);
297 }
298 }
299 G_UNLOCK (list_lock);
300 }
301
302 static void
gst_cuda_context_finalize(GObject * object)303 gst_cuda_context_finalize (GObject * object)
304 {
305 GstCudaContext *context = GST_CUDA_CONTEXT_CAST (object);
306 GstCudaContextPrivate *priv = context->priv;
307
308 if (priv->context) {
309 GST_DEBUG_OBJECT (context, "Destroying CUDA context %p", priv->context);
310 gst_cuda_result (CuCtxDestroy (priv->context));
311 }
312
313 G_OBJECT_CLASS (parent_class)->finalize (object);
314 }
315
316 /**
317 * gst_cuda_context_new:
318 * @device_id: device-id for creating #GstCudaContext or -1 for auto selection
319 *
320 * Create #GstCudaContext with given device_id. If the @device_id was not -1
321 * but was out of range (e.g., exceed the number of device),
322 * #GstCudaContext will not be created.
323 *
324 * Returns: a new #GstCudaContext or %NULL on failure
325 */
326 GstCudaContext *
gst_cuda_context_new(gint device_id)327 gst_cuda_context_new (gint device_id)
328 {
329 GstCudaContext *self =
330 g_object_new (GST_TYPE_CUDA_CONTEXT, "cuda-device-id", device_id, NULL);
331
332 gst_object_ref_sink (self);
333
334 if (!self->priv->context) {
335 GST_ERROR ("Failed to create CUDA context");
336 gst_clear_object (&self);
337 }
338
339 return self;
340 }
341
342 /**
343 * gst_cuda_context_push:
344 * @ctx: a #GstCudaContext to push current thread
345 *
346 * Pushes the given @ctx onto the CPU thread's stack of current contexts.
347 * The specified context becomes the CPU thread's current context,
348 * so all CUDA functions that operate on the current context are affected.
349 *
350 * Returns: %TRUE if @ctx was pushed without error.
351 */
352 gboolean
gst_cuda_context_push(GstCudaContext * ctx)353 gst_cuda_context_push (GstCudaContext * ctx)
354 {
355 g_return_val_if_fail (ctx, FALSE);
356 g_return_val_if_fail (GST_IS_CUDA_CONTEXT (ctx), FALSE);
357
358 return gst_cuda_result (CuCtxPushCurrent (ctx->priv->context));
359 }
360
361 /**
362 * gst_cuda_context_pop:
363 *
364 * Pops the current CUDA context from CPU thread
365 *
366 * Returns: %TRUE if @ctx was pushed without error.
367 */
368 gboolean
gst_cuda_context_pop(CUcontext * cuda_ctx)369 gst_cuda_context_pop (CUcontext * cuda_ctx)
370 {
371 return gst_cuda_result (CuCtxPopCurrent (cuda_ctx));
372 }
373
374 /**
375 * gst_cuda_context_get_handle:
376 * @ctx: a #GstCudaContext
377 *
378 * Get CUDA device context. Caller must not modify and/or destroy
379 * returned device context.
380 *
381 * Returns: the #CUcontext of @ctx
382 */
383 gpointer
gst_cuda_context_get_handle(GstCudaContext * ctx)384 gst_cuda_context_get_handle (GstCudaContext * ctx)
385 {
386 g_return_val_if_fail (ctx, NULL);
387 g_return_val_if_fail (GST_IS_CUDA_CONTEXT (ctx), NULL);
388
389 return ctx->priv->context;
390 }
391
392 /**
393 * gst_cuda_context_get_texture_alignment:
394 * @ctx: a #GstCudaContext
395 *
396 * Get required texture alignment by device
397 *
398 * Returns: the #CUcontext of @ctx
399 */
400 gint
gst_cuda_context_get_texture_alignment(GstCudaContext * ctx)401 gst_cuda_context_get_texture_alignment (GstCudaContext * ctx)
402 {
403 g_return_val_if_fail (ctx, 0);
404 g_return_val_if_fail (GST_IS_CUDA_CONTEXT (ctx), 0);
405
406 return ctx->priv->tex_align;
407 }
408
409 /**
410 * gst_cuda_context_can_access_peer:
411 * @ctx: a #GstCudaContext
412 * @peer: a #GstCudaContext
413 *
414 * Query whether @ctx can access any memory which belongs to @peer directly.
415
416 * Returns: %TRUE if @ctx can access @peer directly
417 */
418 gboolean
gst_cuda_context_can_access_peer(GstCudaContext * ctx,GstCudaContext * peer)419 gst_cuda_context_can_access_peer (GstCudaContext * ctx, GstCudaContext * peer)
420 {
421 gboolean ret = FALSE;
422
423 g_return_val_if_fail (GST_IS_CUDA_CONTEXT (ctx), FALSE);
424 g_return_val_if_fail (GST_IS_CUDA_CONTEXT (peer), FALSE);
425
426 G_LOCK (list_lock);
427 if (ctx->priv->accessible_peer &&
428 g_hash_table_lookup (ctx->priv->accessible_peer, peer)) {
429 ret = TRUE;
430 }
431 G_UNLOCK (list_lock);
432
433 return ret;
434 }
435