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>
31 GST_DEBUG_CATEGORY_STATIC (GST_CAT_CONTEXT);
32 #ifndef GST_DISABLE_GST_DEBUG
33 #define GST_CAT_DEFAULT ensure_debug_category()
34 static GstDebugCategory *
35 ensure_debug_category (void)
37 static gsize cat_gonce = 0;
39 if (g_once_init_enter (&cat_gonce)) {
42 cat_done = (gsize) _gst_debug_category_new ("d3d11utils", 0,
43 "d3d11 utility functions");
45 g_once_init_leave (&cat_gonce, cat_done);
48 return (GstDebugCategory *) cat_gonce;
51 #define ensure_debug_category() /* NOOP */
52 #endif /* GST_DISABLE_GST_DEBUG */
55 _init_context_debug (void)
57 static gsize _init = 0;
59 if (g_once_init_enter (&_init)) {
60 GST_DEBUG_CATEGORY_GET (GST_CAT_CONTEXT, "GST_CONTEXT");
61 g_once_init_leave (&_init, 1);
66 * gst_d3d11_handle_set_context:
67 * @element: a #GstElement
68 * @context: a #GstContext
69 * @device: (inout) (transfer full): location of a #GstD3D11Device
71 * Helper function for implementing #GstElementClass.set_context() in
72 * D3D11 capable elements.
74 * Retrieve's the #GstD3D11Device in @context and places the result in @device.
76 * Returns: whether the @device could be set successfully
81 gst_d3d11_handle_set_context (GstElement * element, GstContext * context,
82 gint adapter, GstD3D11Device ** device)
84 const gchar *context_type;
86 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
87 g_return_val_if_fail (device != NULL, FALSE);
89 _init_context_debug ();
94 context_type = gst_context_get_context_type (context);
95 if (g_strcmp0 (context_type, GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE) == 0) {
96 const GstStructure *str;
97 GstD3D11Device *other_device = NULL;
98 guint other_adapter = 0;
100 /* If we had device already, will not replace it */
104 str = gst_context_get_structure (context);
106 if (gst_structure_get (str, "device", GST_TYPE_D3D11_DEVICE,
107 &other_device, "adapter", G_TYPE_UINT, &other_adapter, NULL)) {
108 if (adapter == -1 || (guint) adapter == other_adapter) {
109 GST_CAT_DEBUG_OBJECT (GST_CAT_CONTEXT,
110 element, "Found D3D11 device context");
111 *device = other_device;
116 gst_object_unref (other_device);
124 context_set_d3d11_device (GstContext * context, GstD3D11Device * device)
130 gboolean hardware = FALSE;
132 gint64 adapter_luid = 0;
134 g_return_if_fail (context != NULL);
136 g_object_get (G_OBJECT (device), "adapter", &adapter, "device-id", &device_id,
137 "vendor-id", &vendor_id, "hardware", &hardware, "description", &desc,
138 "adapter-luid", &adapter_luid, NULL);
140 GST_CAT_LOG (GST_CAT_CONTEXT,
141 "setting GstD3D11Device(%" GST_PTR_FORMAT
142 ") with adapter %d on context(%" GST_PTR_FORMAT ")",
143 device, adapter, context);
145 s = gst_context_writable_structure (context);
146 gst_structure_set (s, "device", GST_TYPE_D3D11_DEVICE, device,
147 "adapter", G_TYPE_UINT, adapter,
148 "adapter-luid", G_TYPE_INT64, adapter_luid,
149 "device-id", G_TYPE_UINT, device_id,
150 "vendor-id", G_TYPE_UINT, vendor_id,
151 "hardware", G_TYPE_BOOLEAN, hardware,
152 "description", G_TYPE_STRING, GST_STR_NULL (desc), NULL);
157 * gst_d3d11_handle_context_query:
158 * @element: a #GstElement
159 * @query: a #GstQuery of type %GST_QUERY_CONTEXT
160 * @device: (transfer none) (nullable): a #GstD3D11Device
162 * Returns: Whether the @query was successfully responded to from the passed
168 gst_d3d11_handle_context_query (GstElement * element, GstQuery * query,
169 GstD3D11Device * device)
171 const gchar *context_type;
172 GstContext *context, *old_context;
174 g_return_val_if_fail (GST_IS_ELEMENT (element), FALSE);
175 g_return_val_if_fail (GST_IS_QUERY (query), FALSE);
177 _init_context_debug ();
179 GST_LOG_OBJECT (element, "handle context query %" GST_PTR_FORMAT, query);
184 gst_query_parse_context_type (query, &context_type);
185 if (g_strcmp0 (context_type, GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE) != 0)
188 gst_query_parse_context (query, &old_context);
190 context = gst_context_copy (old_context);
192 context = gst_context_new (GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE, TRUE);
194 context_set_d3d11_device (context, device);
195 gst_query_set_context (query, context);
196 gst_context_unref (context);
198 GST_DEBUG_OBJECT (element, "successfully set %" GST_PTR_FORMAT
199 " on %" GST_PTR_FORMAT, device, query);
205 pad_query (const GValue * item, GValue * value, gpointer user_data)
207 GstPad *pad = (GstPad *) g_value_get_object (item);
208 GstQuery *query = (GstQuery *) user_data;
211 res = gst_pad_peer_query (pad, query);
214 g_value_set_boolean (value, TRUE);
218 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, pad, "pad peer query failed");
223 run_query (GstElement * element, GstQuery * query, GstPadDirection direction)
226 GstIteratorFoldFunction func = pad_query;
229 g_value_init (&res, G_TYPE_BOOLEAN);
230 g_value_set_boolean (&res, FALSE);
233 if (direction == GST_PAD_SRC)
234 it = gst_element_iterate_src_pads (element);
236 it = gst_element_iterate_sink_pads (element);
238 while (gst_iterator_fold (it, func, &res, query) == GST_ITERATOR_RESYNC)
239 gst_iterator_resync (it);
241 gst_iterator_free (it);
243 return g_value_get_boolean (&res);
247 run_d3d11_context_query (GstElement * element, GstD3D11Device ** device)
250 GstContext *ctxt = NULL;
252 /* 1) Query downstream with GST_QUERY_CONTEXT for the context and
253 * check if downstream already has a context of the specific type
255 query = gst_query_new_context (GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE);
256 if (run_query (element, query, GST_PAD_SRC)) {
257 gst_query_parse_context (query, &ctxt);
259 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
260 "found context (%" GST_PTR_FORMAT ") in downstream query", ctxt);
261 gst_element_set_context (element, ctxt);
265 /* 2) although we found d3d11 device context above, the element does not want
266 * to use the context. Then try to find from the other direction */
267 if (*device == NULL && run_query (element, query, GST_PAD_SINK)) {
268 gst_query_parse_context (query, &ctxt);
270 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
271 "found context (%" GST_PTR_FORMAT ") in upstream query", ctxt);
272 gst_element_set_context (element, ctxt);
276 if (*device == NULL) {
277 /* 3) Post a GST_MESSAGE_NEED_CONTEXT message on the bus with
278 * the required context type and afterwards check if a
279 * usable context was set now as in 1). The message could
280 * be handled by the parent bins of the element and the
285 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
286 "posting need context message");
287 msg = gst_message_new_need_context (GST_OBJECT_CAST (element),
288 GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE);
289 gst_element_post_message (element, msg);
293 * Whomever responds to the need-context message performs a
294 * GstElement::set_context() with the required context in which the element
295 * is required to update the display_ptr or call gst_gl_handle_set_context().
298 gst_query_unref (query);
302 * gst_d3d11_ensure_element_data:
303 * @element: the #GstElement running the query
304 * @adapter: preferred adapter index, pass adapter >=0 when
305 * the adapter explicitly required. Otherwise, set -1.
306 * @device: (inout): the resulting #GstD3D11Device
308 * Perform the steps necessary for retrieving a #GstD3D11Device
309 * from the surrounding elements or from the application using the #GstContext mechanism.
311 * If the contents of @device is not %NULL, then no #GstContext query is
312 * necessary for #GstD3D11Device retrieval is performed.
314 * Returns: whether a #GstD3D11Device exists in @device
319 gst_d3d11_ensure_element_data (GstElement * element, gint adapter,
320 GstD3D11Device ** device)
322 guint target_adapter = 0;
324 g_return_val_if_fail (element != NULL, FALSE);
325 g_return_val_if_fail (device != NULL, FALSE);
327 _init_context_debug ();
330 GST_LOG_OBJECT (element, "already have a device %" GST_PTR_FORMAT, *device);
334 run_d3d11_context_query (element, device);
339 target_adapter = adapter;
341 /* Needs D3D11_CREATE_DEVICE_BGRA_SUPPORT flag for Direct2D interop */
342 *device = gst_d3d11_device_new (target_adapter,
343 D3D11_CREATE_DEVICE_BGRA_SUPPORT);
345 if (*device == NULL) {
346 GST_ERROR_OBJECT (element,
347 "Couldn't create new device with adapter index %d", target_adapter);
353 /* Propagate new D3D11 device context */
355 context = gst_context_new (GST_D3D11_DEVICE_HANDLE_CONTEXT_TYPE, TRUE);
356 context_set_d3d11_device (context, *device);
358 gst_element_set_context (element, context);
360 GST_CAT_INFO_OBJECT (GST_CAT_CONTEXT, element,
361 "posting have context (%p) message with D3D11 device context (%p)",
363 msg = gst_message_new_have_context (GST_OBJECT_CAST (element), context);
364 gst_element_post_message (GST_ELEMENT_CAST (element), msg);
371 _gst_d3d11_result (HRESULT hr, GstD3D11Device * device, GstDebugCategory * cat,
372 const gchar * file, const gchar * function, gint line)
374 #ifndef GST_DISABLE_GST_DEBUG
378 gchar *error_text = NULL;
380 error_text = g_win32_error_message ((guint) hr);
381 /* g_win32_error_message() doesn't cover all HERESULT return code,
382 * so it could be empty string, or null if there was an error
383 * in g_utf16_to_utf8() */
384 gst_debug_log (cat, GST_LEVEL_WARNING, file, function, line,
385 NULL, "D3D11 call failed: 0x%x, %s", (guint) hr,
386 GST_STR_NULL (error_text));
391 #if (HAVE_D3D11SDKLAYERS_H || HAVE_DXGIDEBUG_H)
393 gst_d3d11_device_d3d11_debug (device, file, function, line);
394 gst_d3d11_device_dxgi_debug (device, file, function, line);
400 return SUCCEEDED (hr);