2 * Copyright (C) 2020 Seungha Yang <seungha@centricular.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 "gstd3d11utils.h"
25 #include "gstd3d11device.h"
26 #include "gstd3d11-private.h"
29 #include <versionhelpers.h>
33 * SECTION:gstd3d11utils
34 * @title: GstD3D11Utils
35 * @short_description: Direct3D11 specific utility methods
41 static std::recursive_mutex _context_lock;
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)
50 static GstDebugCategory *cat = nullptr;
52 GST_D3D11_CALL_ONCE_BEGIN {
53 cat = _gst_debug_category_new ("d3d11utils", 0, "d3d11 utility functions");
54 } GST_D3D11_CALL_ONCE_END;
59 #define ensure_debug_category() /* NOOP */
60 #endif /* GST_DISABLE_GST_DEBUG */
63 _init_context_debug (void)
65 GST_D3D11_CALL_ONCE_BEGIN {
66 GST_DEBUG_CATEGORY_GET (GST_CAT_CONTEXT, "GST_CONTEXT");
67 } GST_D3D11_CALL_ONCE_END;
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
77 * Helper function for implementing #GstElementClass.set_context() in
78 * D3D11 capable elements.
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
84 * Returns: whether the @device could be set successfully
89 gst_d3d11_handle_set_context (GstElement * element, GstContext * context,
90 gint adapter_index, GstD3D11Device ** device)
92 const gchar *context_type;
94 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
95 g_return_val_if_fail (device != NULL, FALSE);
97 _init_context_debug ();
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;
108 /* If we had device already, will not replace it */
112 str = gst_context_get_structure (context);
114 if (gst_structure_get (str, "device", GST_TYPE_D3D11_DEVICE,
115 &other_device, "adapter", G_TYPE_UINT, &other_adapter_index,
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;
125 gst_object_unref (other_device);
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
139 * Helper function for implementing #GstElementClass.set_context() in
140 * D3D11 capable elements.
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
145 * Returns: whether the @device could be set successfully
150 gst_d3d11_handle_set_context_for_adapter_luid (GstElement * element,
151 GstContext * context, gint64 adapter_luid, GstD3D11Device ** device)
153 const gchar *context_type;
155 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
156 g_return_val_if_fail (device != NULL, FALSE);
158 _init_context_debug ();
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;
169 /* If we had device already, will not replace it */
173 str = gst_context_get_structure (context);
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;
186 gst_object_unref (other_device);
194 context_set_d3d11_device (GstContext * context, GstD3D11Device * device)
200 gboolean hardware = FALSE;
202 gint64 adapter_luid = 0;
204 g_return_if_fail (context != NULL);
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);
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);
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);
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
232 * Returns: Whether the @query was successfully responded to from the passed
238 gst_d3d11_handle_context_query (GstElement * element, GstQuery * query,
239 GstD3D11Device * device)
241 const gchar *context_type;
242 GstContext *context, *old_context;
244 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
245 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
247 _init_context_debug ();
249 GST_LOG_OBJECT (element, "handle context query %" GST_PTR_FORMAT, query);
254 gst_query_parse_context_type (query, &context_type);
255 if (g_strcmp0 (context_type, GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE) != 0)
258 gst_query_parse_context (query, &old_context);
260 context = gst_context_copy (old_context);
262 context = gst_context_new (GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE, TRUE);
264 context_set_d3d11_device (context, device);
265 gst_query_set_context (query, context);
266 gst_context_unref (context);
268 GST_DEBUG_OBJECT (element, "successfully set %" GST_PTR_FORMAT
269 " on %" GST_PTR_FORMAT, device, query);
275 pad_query (const GValue * item, GValue * value, gpointer user_data)
277 GstPad *pad = (GstPad *) g_value_get_object (item);
278 GstQuery *query = (GstQuery *) user_data;
281 res = gst_pad_peer_query (pad, query);
284 g_value_set_boolean (value, TRUE);
288 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, pad, "pad peer query failed");
293 run_query (GstElement * element, GstQuery * query, GstPadDirection direction)
296 GstIteratorFoldFunction func = pad_query;
299 g_value_init (&res, G_TYPE_BOOLEAN);
300 g_value_set_boolean (&res, FALSE);
303 if (direction == GST_PAD_SRC)
304 it = gst_element_iterate_src_pads (element);
306 it = gst_element_iterate_sink_pads (element);
308 while (gst_iterator_fold (it, func, &res, query) == GST_ITERATOR_RESYNC)
309 gst_iterator_resync (it);
311 gst_iterator_free (it);
313 return g_value_get_boolean (&res);
317 run_d3d11_context_query (GstElement * element, GstD3D11Device ** device)
320 GstContext *ctxt = NULL;
322 /* 1) Query downstream with GST_QUERY_CONTEXT for the context and
323 * check if downstream already has a context of the specific type
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);
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);
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);
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);
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
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);
363 gst_query_unref (query);
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
373 * Perform the steps necessary for retrieving a #GstD3D11Device
374 * from the surrounding elements or from the application using the #GstContext mechanism.
376 * If the contents of @device is not %NULL, then no #GstContext query is
377 * necessary for #GstD3D11Device retrieval is performed.
379 * Returns: whether a #GstD3D11Device exists in @device
384 gst_d3d11_ensure_element_data (GstElement * element, gint adapter,
385 GstD3D11Device ** device)
387 guint target_adapter = 0;
389 std::lock_guard<std::recursive_mutex> lk (_context_lock);
392 g_return_val_if_fail (element != NULL, FALSE);
393 g_return_val_if_fail (device != NULL, FALSE);
395 _init_context_debug ();
398 GST_LOG_OBJECT (element, "already have a device %" GST_PTR_FORMAT, *device);
402 run_d3d11_context_query (element, device);
407 target_adapter = adapter;
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);
413 if (*device == NULL) {
414 GST_ERROR_OBJECT (element,
415 "Couldn't create new device with adapter index %d", target_adapter);
421 /* Propagate new D3D11 device context */
423 context = gst_context_new (GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE, TRUE);
424 context_set_d3d11_device (context, *device);
426 gst_element_set_context (element, context);
428 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
429 "posting have context (%p) message with D3D11 device context (%p)",
431 msg = gst_message_new_have_context (GST_OBJECT_CAST (element), context);
432 gst_element_post_message (GST_ELEMENT_CAST (element), msg);
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
444 * Perform the steps necessary for retrieving a #GstD3D11Device
445 * from the surrounding elements or from the application using the #GstContext mechanism.
447 * If the contents of @device is not %NULL, then no #GstContext query is
448 * necessary for #GstD3D11Device retrieval is performed.
450 * Returns: whether a #GstD3D11Device exists in @device
455 gst_d3d11_ensure_element_data_for_adapter_luid (GstElement * element,
456 gint64 adapter_luid, GstD3D11Device ** device)
459 std::lock_guard<std::recursive_mutex> lk (_context_lock);
462 g_return_val_if_fail (element != NULL, FALSE);
463 g_return_val_if_fail (device != NULL, FALSE);
465 _init_context_debug ();
468 GST_LOG_OBJECT (element, "already have a device %" GST_PTR_FORMAT, *device);
472 run_d3d11_context_query (element, device);
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);
480 if (*device == NULL) {
481 GST_ERROR_OBJECT (element,
482 "Couldn't create new device with adapter luid %" G_GINT64_FORMAT,
489 /* Propagate new D3D11 device context */
491 context = gst_context_new (GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE, TRUE);
492 context_set_d3d11_device (context, *device);
494 gst_element_set_context (element, context);
496 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
497 "posting have context (%p) message with D3D11 device context (%p)",
499 msg = gst_message_new_have_context (GST_OBJECT_CAST (element), context);
500 gst_element_post_message (GST_ELEMENT_CAST (element), msg);
507 * gst_d3d11_context_new:
508 * @device: (transfer none): a #GstD3D11Device
510 * Creates a new #GstContext object with @device
512 * Returns: a #GstContext object
517 gst_d3d11_context_new (GstD3D11Device * device)
521 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), nullptr);
523 context = gst_context_new (GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE, TRUE);
524 context_set_d3d11_device (context, device);
530 * gst_d3d11_luid_to_int64:
531 * @luid: A pointer to LUID struct
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
540 gst_d3d11_luid_to_int64 (const LUID * luid)
544 g_return_val_if_fail (luid != nullptr, 0);
546 val.LowPart = luid->LowPart;
547 val.HighPart = luid->HighPart;
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
561 * Prints debug message if @result code indicates the operation was failed.
563 * Returns: %TRUE if D3D11 API call result is SUCCESS
568 _gst_d3d11_result (HRESULT hr, GstD3D11Device * device, GstDebugCategory * cat,
569 const gchar * file, const gchar * function, gint line)
571 #ifndef GST_DISABLE_GST_DEBUG
575 gchar *error_text = NULL;
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));
588 #if (HAVE_D3D11SDKLAYERS_H || HAVE_DXGIDEBUG_H)
590 gst_d3d11_device_d3d11_debug (device, file, function, line);
591 gst_d3d11_device_dxgi_debug (device, file, function, line);
597 return SUCCEEDED (hr);