d3d11: Update library doc
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / gst-libs / gst / d3d11 / gstd3d11utils.cpp
1 /* GStreamer
2  * Copyright (C) 2020 Seungha Yang <seungha@centricular.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 "gstd3d11utils.h"
25 #include "gstd3d11device.h"
26 #include "gstd3d11-private.h"
27
28 #include <windows.h>
29 #include <versionhelpers.h>
30 #include <mutex>
31
32 /**
33  * SECTION:gstd3d11utils
34  * @title: GstD3D11Utils
35  * @short_description: Direct3D11 specific utility methods
36  *
37  * Since: 1.22
38  */
39
40 /* *INDENT-OFF* */
41 static std::recursive_mutex _context_lock;
42 /* *INDENT-ON* */
43
44 GST_DEBUG_CATEGORY_STATIC (GST_CAT_CONTEXT);
45 #ifndef GST_DISABLE_GST_DEBUG
46 #define GST_CAT_DEFAULT ensure_debug_category()
47 static GstDebugCategory *
48 ensure_debug_category (void)
49 {
50   static GstDebugCategory *cat = nullptr;
51
52   GST_D3D11_CALL_ONCE_BEGIN {
53     cat = _gst_debug_category_new ("d3d11utils", 0, "d3d11 utility functions");
54   } GST_D3D11_CALL_ONCE_END;
55
56   return cat;
57 }
58 #else
59 #define ensure_debug_category() /* NOOP */
60 #endif /* GST_DISABLE_GST_DEBUG */
61
62 static void
63 _init_context_debug (void)
64 {
65   GST_D3D11_CALL_ONCE_BEGIN {
66     GST_DEBUG_CATEGORY_GET (GST_CAT_CONTEXT, "GST_CONTEXT");
67   } GST_D3D11_CALL_ONCE_END;
68 }
69
70 /**
71  * gst_d3d11_handle_set_context:
72  * @element: a #GstElement
73  * @context: a #GstContext
74  * @adapter_index: a DXGI adapter index
75  * @device: (inout) (transfer full): location of a #GstD3D11Device
76  *
77  * Helper function for implementing #GstElementClass.set_context() in
78  * D3D11 capable elements.
79  *
80  * Retrieve's the #GstD3D11Device in @context and places the result in @device.
81  * @device is accepted if @adapter_index is equal to -1 (accept any device)
82  * or equal to that of @device
83  *
84  * Returns: whether the @device could be set successfully
85  *
86  * Since: 1.22
87  */
88 gboolean
89 gst_d3d11_handle_set_context (GstElement * element, GstContext * context,
90     gint adapter_index, GstD3D11Device ** device)
91 {
92   const gchar *context_type;
93
94   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
95   g_return_val_if_fail (device != NULL, FALSE);
96
97   _init_context_debug ();
98
99   if (!context)
100     return FALSE;
101
102   context_type = gst_context_get_context_type (context);
103   if (g_strcmp0 (context_type, GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE) == 0) {
104     const GstStructure *str;
105     GstD3D11Device *other_device = NULL;
106     guint other_adapter_index = 0;
107
108     /* If we had device already, will not replace it */
109     if (*device)
110       return TRUE;
111
112     str = gst_context_get_structure (context);
113
114     if (gst_structure_get (str, "device", GST_TYPE_D3D11_DEVICE,
115             &other_device, "adapter", G_TYPE_UINT, &other_adapter_index,
116             NULL)) {
117       if (adapter_index == -1 || (guint) adapter_index == other_adapter_index) {
118         GST_CAT_DEBUG_OBJECT (GST_CAT_CONTEXT,
119             element, "Found D3D11 device context");
120         *device = other_device;
121
122         return TRUE;
123       }
124
125       gst_object_unref (other_device);
126     }
127   }
128
129   return FALSE;
130 }
131
132 /**
133  * gst_d3d11_handle_set_context_for_adapter_luid:
134  * @element: a #GstElement
135  * @context: a #GstContext
136  * @adapter_luid: an int64 representation of DXGI adapter LUID
137  * @device: (inout) (transfer full): location of a #GstD3D11Device
138  *
139  * Helper function for implementing #GstElementClass.set_context() in
140  * D3D11 capable elements.
141  *
142  * Retrieve's the #GstD3D11Device in @context and places the result in @device.
143  * @device is accepted only when @adapter_index is equal to that of @device
144  *
145  * Returns: whether the @device could be set successfully
146  *
147  * Since: 1.22
148  */
149 gboolean
150 gst_d3d11_handle_set_context_for_adapter_luid (GstElement * element,
151     GstContext * context, gint64 adapter_luid, GstD3D11Device ** device)
152 {
153   const gchar *context_type;
154
155   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
156   g_return_val_if_fail (device != NULL, FALSE);
157
158   _init_context_debug ();
159
160   if (!context)
161     return FALSE;
162
163   context_type = gst_context_get_context_type (context);
164   if (g_strcmp0 (context_type, GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE) == 0) {
165     const GstStructure *str;
166     GstD3D11Device *other_device = NULL;
167     gint64 other_adapter_luid = 0;
168
169     /* If we had device already, will not replace it */
170     if (*device)
171       return TRUE;
172
173     str = gst_context_get_structure (context);
174
175     if (gst_structure_get (str, "device", GST_TYPE_D3D11_DEVICE,
176             &other_device, "adapter-luid", G_TYPE_INT64,
177             &other_adapter_luid, NULL)) {
178       if (adapter_luid == other_adapter_luid) {
179         GST_CAT_DEBUG_OBJECT (GST_CAT_CONTEXT,
180             element, "Found D3D11 device context");
181         *device = other_device;
182
183         return TRUE;
184       }
185
186       gst_object_unref (other_device);
187     }
188   }
189
190   return FALSE;
191 }
192
193 static void
194 context_set_d3d11_device (GstContext * context, GstD3D11Device * device)
195 {
196   GstStructure *s;
197   guint adapter = 0;
198   guint device_id = 0;
199   guint vendor_id = 0;
200   gboolean hardware = FALSE;
201   gchar *desc = NULL;
202   gint64 adapter_luid = 0;
203
204   g_return_if_fail (context != NULL);
205
206   g_object_get (G_OBJECT (device), "adapter", &adapter, "device-id", &device_id,
207       "vendor-id", &vendor_id, "hardware", &hardware, "description", &desc,
208       "adapter-luid", &adapter_luid, NULL);
209
210   GST_CAT_LOG (GST_CAT_CONTEXT,
211       "setting GstD3D11Device(%" GST_PTR_FORMAT
212       ") with adapter %d on context(%" GST_PTR_FORMAT ")",
213       device, adapter, context);
214
215   s = gst_context_writable_structure (context);
216   gst_structure_set (s, "device", GST_TYPE_D3D11_DEVICE, device,
217       "adapter", G_TYPE_UINT, adapter,
218       "adapter-luid", G_TYPE_INT64, adapter_luid,
219       "device-id", G_TYPE_UINT, device_id,
220       "vendor-id", G_TYPE_UINT, vendor_id,
221       "hardware", G_TYPE_BOOLEAN, hardware,
222       "description", G_TYPE_STRING, GST_STR_NULL (desc), NULL);
223   g_free (desc);
224 }
225
226 /**
227  * gst_d3d11_handle_context_query:
228  * @element: a #GstElement
229  * @query: a #GstQuery of type %GST_QUERY_CONTEXT
230  * @device: (transfer none) (nullable): a #GstD3D11Device
231  *
232  * Returns: Whether the @query was successfully responded to from the passed
233  *          @device.
234  *
235  * Since: 1.22
236  */
237 gboolean
238 gst_d3d11_handle_context_query (GstElement * element, GstQuery * query,
239     GstD3D11Device * device)
240 {
241   const gchar *context_type;
242   GstContext *context, *old_context;
243
244   g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
245   g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
246
247   _init_context_debug ();
248
249   GST_LOG_OBJECT (element, "handle context query %" GST_PTR_FORMAT, query);
250
251   if (!device)
252     return FALSE;
253
254   gst_query_parse_context_type (query, &context_type);
255   if (g_strcmp0 (context_type, GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE) != 0)
256     return FALSE;
257
258   gst_query_parse_context (query, &old_context);
259   if (old_context)
260     context = gst_context_copy (old_context);
261   else
262     context = gst_context_new (GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE, TRUE);
263
264   context_set_d3d11_device (context, device);
265   gst_query_set_context (query, context);
266   gst_context_unref (context);
267
268   GST_DEBUG_OBJECT (element, "successfully set %" GST_PTR_FORMAT
269       " on %" GST_PTR_FORMAT, device, query);
270
271   return TRUE;
272 }
273
274 static gboolean
275 pad_query (const GValue * item, GValue * value, gpointer user_data)
276 {
277   GstPad *pad = (GstPad *) g_value_get_object (item);
278   GstQuery *query = (GstQuery *) user_data;
279   gboolean res;
280
281   res = gst_pad_peer_query (pad, query);
282
283   if (res) {
284     g_value_set_boolean (value, TRUE);
285     return FALSE;
286   }
287
288   GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, pad, "pad peer query failed");
289   return TRUE;
290 }
291
292 static gboolean
293 run_query (GstElement * element, GstQuery * query, GstPadDirection direction)
294 {
295   GstIterator *it;
296   GstIteratorFoldFunction func = pad_query;
297   GValue res = { 0 };
298
299   g_value_init (&res, G_TYPE_BOOLEAN);
300   g_value_set_boolean (&res, FALSE);
301
302   /* Ask neighbor */
303   if (direction == GST_PAD_SRC)
304     it = gst_element_iterate_src_pads (element);
305   else
306     it = gst_element_iterate_sink_pads (element);
307
308   while (gst_iterator_fold (it, func, &res, query) == GST_ITERATOR_RESYNC)
309     gst_iterator_resync (it);
310
311   gst_iterator_free (it);
312
313   return g_value_get_boolean (&res);
314 }
315
316 static void
317 run_d3d11_context_query (GstElement * element, GstD3D11Device ** device)
318 {
319   GstQuery *query;
320   GstContext *ctxt = NULL;
321
322   /* 1) Query downstream with GST_QUERY_CONTEXT for the context and
323    *    check if downstream already has a context of the specific type
324    */
325   query = gst_query_new_context (GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE);
326   if (run_query (element, query, GST_PAD_SRC)) {
327     gst_query_parse_context (query, &ctxt);
328     if (ctxt) {
329       GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
330           "found context (%" GST_PTR_FORMAT ") in downstream query", ctxt);
331       gst_element_set_context (element, ctxt);
332     }
333   }
334
335   /* 2) although we found d3d11 device context above, the context might not be
336    *    expected/wanted one by the element (e.g., belongs to the other GPU).
337    *    Then try to find it from the other direction */
338   if (*device == NULL && run_query (element, query, GST_PAD_SINK)) {
339     gst_query_parse_context (query, &ctxt);
340     if (ctxt) {
341       GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
342           "found context (%" GST_PTR_FORMAT ") in upstream query", ctxt);
343       gst_element_set_context (element, ctxt);
344     }
345   }
346
347   if (*device == NULL) {
348     /* 3) Post a GST_MESSAGE_NEED_CONTEXT message on the bus with
349      *    the required context type and afterwards check if a
350      *    usable context was set now as in 1). The message could
351      *    be handled by the parent bins of the element and the
352      *    application.
353      */
354     GstMessage *msg;
355
356     GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
357         "posting need context message");
358     msg = gst_message_new_need_context (GST_OBJECT_CAST (element),
359         GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE);
360     gst_element_post_message (element, msg);
361   }
362
363   gst_query_unref (query);
364 }
365
366 /**
367  * gst_d3d11_ensure_element_data:
368  * @element: the #GstElement running the query
369  * @adapter: preferred DXGI adapter index, pass adapter >=0 when
370  *           the adapter explicitly required. Otherwise, set -1.
371  * @device: (inout): the resulting #GstD3D11Device
372  *
373  * Perform the steps necessary for retrieving a #GstD3D11Device
374  * from the surrounding elements or from the application using the #GstContext mechanism.
375  *
376  * If the contents of @device is not %NULL, then no #GstContext query is
377  * necessary for #GstD3D11Device retrieval is performed.
378  *
379  * Returns: whether a #GstD3D11Device exists in @device
380  *
381  * Since: 1.22
382  */
383 gboolean
384 gst_d3d11_ensure_element_data (GstElement * element, gint adapter,
385     GstD3D11Device ** device)
386 {
387   guint target_adapter = 0;
388   /* *INDENT-OFF* */
389   std::lock_guard<std::recursive_mutex> lk (_context_lock);
390   /* *INDENT-ON* */
391
392   g_return_val_if_fail (element != NULL, FALSE);
393   g_return_val_if_fail (device != NULL, FALSE);
394
395   _init_context_debug ();
396
397   if (*device) {
398     GST_LOG_OBJECT (element, "already have a device %" GST_PTR_FORMAT, *device);
399     return TRUE;
400   }
401
402   run_d3d11_context_query (element, device);
403   if (*device)
404     return TRUE;
405
406   if (adapter > 0)
407     target_adapter = adapter;
408
409   /* Needs D3D11_CREATE_DEVICE_BGRA_SUPPORT flag for Direct2D interop */
410   *device = gst_d3d11_device_new (target_adapter,
411       D3D11_CREATE_DEVICE_BGRA_SUPPORT);
412
413   if (*device == NULL) {
414     GST_ERROR_OBJECT (element,
415         "Couldn't create new device with adapter index %d", target_adapter);
416     return FALSE;
417   } else {
418     GstContext *context;
419     GstMessage *msg;
420
421     /* Propagate new D3D11 device context */
422
423     context = gst_context_new (GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE, TRUE);
424     context_set_d3d11_device (context, *device);
425
426     gst_element_set_context (element, context);
427
428     GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
429         "posting have context (%p) message with D3D11 device context (%p)",
430         context, *device);
431     msg = gst_message_new_have_context (GST_OBJECT_CAST (element), context);
432     gst_element_post_message (GST_ELEMENT_CAST (element), msg);
433   }
434
435   return TRUE;
436 }
437
438 /**
439  * gst_d3d11_ensure_element_data_for_adapter_luid:
440  * @element: the #GstElement running the query
441  * @adapter_luid: an int64 representation of DXGI adapter LUID
442  * @device: (inout): the resulting #GstD3D11Device
443  *
444  * Perform the steps necessary for retrieving a #GstD3D11Device
445  * from the surrounding elements or from the application using the #GstContext mechanism.
446  *
447  * If the contents of @device is not %NULL, then no #GstContext query is
448  * necessary for #GstD3D11Device retrieval is performed.
449  *
450  * Returns: whether a #GstD3D11Device exists in @device
451  *
452  * Since: 1.22
453  */
454 gboolean
455 gst_d3d11_ensure_element_data_for_adapter_luid (GstElement * element,
456     gint64 adapter_luid, GstD3D11Device ** device)
457 {
458   /* *INDENT-OFF* */
459   std::lock_guard<std::recursive_mutex> lk (_context_lock);
460   /* *INDENT-ON* */
461
462   g_return_val_if_fail (element != NULL, FALSE);
463   g_return_val_if_fail (device != NULL, FALSE);
464
465   _init_context_debug ();
466
467   if (*device) {
468     GST_LOG_OBJECT (element, "already have a device %" GST_PTR_FORMAT, *device);
469     return TRUE;
470   }
471
472   run_d3d11_context_query (element, device);
473   if (*device)
474     return TRUE;
475
476   /* Needs D3D11_CREATE_DEVICE_BGRA_SUPPORT flag for Direct2D interop */
477   *device = gst_d3d11_device_new_for_adapter_luid (adapter_luid,
478       D3D11_CREATE_DEVICE_BGRA_SUPPORT);
479
480   if (*device == NULL) {
481     GST_ERROR_OBJECT (element,
482         "Couldn't create new device with adapter luid %" G_GINT64_FORMAT,
483         adapter_luid);
484     return FALSE;
485   } else {
486     GstContext *context;
487     GstMessage *msg;
488
489     /* Propagate new D3D11 device context */
490
491     context = gst_context_new (GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE, TRUE);
492     context_set_d3d11_device (context, *device);
493
494     gst_element_set_context (element, context);
495
496     GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
497         "posting have context (%p) message with D3D11 device context (%p)",
498         context, *device);
499     msg = gst_message_new_have_context (GST_OBJECT_CAST (element), context);
500     gst_element_post_message (GST_ELEMENT_CAST (element), msg);
501   }
502
503   return TRUE;
504 }
505
506 /**
507  * gst_d3d11_context_new:
508  * @device: (transfer none): a #GstD3D11Device
509  *
510  * Creates a new #GstContext object with @device
511  *
512  * Returns: a #GstContext object
513  *
514  * Since: 1.22
515  */
516 GstContext *
517 gst_d3d11_context_new (GstD3D11Device * device)
518 {
519   GstContext *context;
520
521   g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), nullptr);
522
523   context = gst_context_new (GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE, TRUE);
524   context_set_d3d11_device (context, device);
525
526   return context;
527 }
528
529 /**
530  * gst_d3d11_luid_to_int64:
531  * @luid: A pointer to LUID struct
532  *
533  * Converts @luid to a 64-bit signed integer.
534  * See also Int64FromLuid method defined in
535  * windows.devices.display.core.interop.h Windows SDK header
536  *
537  * Since: 1.22
538  */
539 gint64
540 gst_d3d11_luid_to_int64 (const LUID * luid)
541 {
542   LARGE_INTEGER val;
543
544   g_return_val_if_fail (luid != nullptr, 0);
545
546   val.LowPart = luid->LowPart;
547   val.HighPart = luid->HighPart;
548
549   return val.QuadPart;
550 }
551
552 /**
553  * _gst_d3d11_result:
554  * @result: HRESULT D3D11 API return code
555  * @device: (nullable): Associated #GstD3D11Device
556  * @cat: a #GstDebugCategory
557  * @file: the file that checking the result code
558  * @function: the function that checking the result code
559  * @line: the line that checking the result code
560  *
561  * Prints debug message if @result code indicates the operation was failed.
562  *
563  * Returns: %TRUE if D3D11 API call result is SUCCESS
564  *
565  * Since: 1.22
566  */
567 gboolean
568 _gst_d3d11_result (HRESULT hr, GstD3D11Device * device, GstDebugCategory * cat,
569     const gchar * file, const gchar * function, gint line)
570 {
571 #ifndef GST_DISABLE_GST_DEBUG
572   gboolean ret = TRUE;
573
574   if (FAILED (hr)) {
575     gchar *error_text = NULL;
576
577     error_text = g_win32_error_message ((guint) hr);
578     /* g_win32_error_message() doesn't cover all HERESULT return code,
579      * so it could be empty string, or null if there was an error
580      * in g_utf16_to_utf8() */
581     gst_debug_log (cat, GST_LEVEL_WARNING, file, function, line,
582         NULL, "D3D11 call failed: 0x%x, %s", (guint) hr,
583         GST_STR_NULL (error_text));
584     g_free (error_text);
585
586     ret = FALSE;
587   }
588 #if (HAVE_D3D11SDKLAYERS_H || HAVE_DXGIDEBUG_H)
589   if (device) {
590     gst_d3d11_device_d3d11_debug (device, file, function, line);
591     gst_d3d11_device_dxgi_debug (device, file, function, line);
592   }
593 #endif
594
595   return ret;
596 #else
597   return SUCCEEDED (hr);
598 #endif
599 }