3 * Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
25 #include "gstd3d11window_dummy.h"
26 #include "gstd3d11pluginutils.h"
30 using namespace Microsoft::WRL;
33 GST_DEBUG_CATEGORY_EXTERN (gst_d3d11_window_debug);
34 #define GST_CAT_DEFAULT gst_d3d11_window_debug
36 struct _GstD3D11WindowDummy
38 GstD3D11Window parent;
40 ID3D11Texture2D *fallback_texture;
41 ID3D11VideoProcessorOutputView *fallback_pov;
42 ID3D11RenderTargetView *fallback_rtv;
45 #define gst_d3d11_window_dummy_parent_class parent_class
46 G_DEFINE_TYPE (GstD3D11WindowDummy, gst_d3d11_window_dummy,
47 GST_TYPE_D3D11_WINDOW);
49 static void gst_d3d11_window_dummy_on_resize (GstD3D11Window * window,
50 guint width, guint height);
51 static gboolean gst_d3d11_window_dummy_prepare (GstD3D11Window * window,
52 guint display_width, guint display_height, GstCaps * caps,
53 gboolean * video_processor_available, GError ** error);
54 static void gst_d3d11_window_dummy_unprepare (GstD3D11Window * window);
56 gst_d3d11_window_dummy_open_shared_handle (GstD3D11Window * window,
57 GstD3D11WindowSharedHandleData * data);
59 gst_d3d11_window_dummy_release_shared_handle (GstD3D11Window * window,
60 GstD3D11WindowSharedHandleData * data);
63 gst_d3d11_window_dummy_class_init (GstD3D11WindowDummyClass * klass)
65 GstD3D11WindowClass *window_class = GST_D3D11_WINDOW_CLASS (klass);
67 window_class->on_resize =
68 GST_DEBUG_FUNCPTR (gst_d3d11_window_dummy_on_resize);
69 window_class->prepare = GST_DEBUG_FUNCPTR (gst_d3d11_window_dummy_prepare);
70 window_class->unprepare =
71 GST_DEBUG_FUNCPTR (gst_d3d11_window_dummy_unprepare);
72 window_class->open_shared_handle =
73 GST_DEBUG_FUNCPTR (gst_d3d11_window_dummy_open_shared_handle);
74 window_class->release_shared_handle =
75 GST_DEBUG_FUNCPTR (gst_d3d11_window_dummy_release_shared_handle);
79 gst_d3d11_window_dummy_init (GstD3D11WindowDummy * self)
84 gst_d3d11_window_dummy_prepare (GstD3D11Window * window,
85 guint display_width, guint display_height, GstCaps * caps,
86 gboolean * video_processor_available, GError ** error)
88 g_clear_pointer (&window->processor, gst_d3d11_video_processor_free);
89 g_clear_pointer (&window->converter, gst_d3d11_converter_free);
90 g_clear_pointer (&window->compositor, gst_d3d11_overlay_compositor_free);
92 /* We are supporting only RGBA, BGRA or RGB10A2_LE formats but we don't know
93 * which format texture will be used at this moment */
95 gst_video_info_from_caps (&window->info, caps);
96 window->render_rect.left = 0;
97 window->render_rect.top = 0;
98 window->render_rect.right = display_width;
99 window->render_rect.bottom = display_height;
101 window->input_rect.left = 0;
102 window->input_rect.top = 0;
103 window->input_rect.right = GST_VIDEO_INFO_WIDTH (&window->info);
104 window->input_rect.bottom = GST_VIDEO_INFO_HEIGHT (&window->info);
106 gst_video_info_set_format (&window->render_info,
107 GST_VIDEO_FORMAT_BGRA, display_width, display_height);
109 /* TODO: not sure which colorspace should be used, let's use BT709 since
110 * it's default and most common one */
111 window->render_info.colorimetry.primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
112 window->render_info.colorimetry.transfer = GST_VIDEO_TRANSFER_BT709;
113 window->render_info.colorimetry.range = GST_VIDEO_COLOR_RANGE_0_255;
115 gst_d3d11_device_lock (window->device);
118 const GstDxgiColorSpace *in_color_space =
119 gst_d3d11_video_info_to_dxgi_color_space (&window->info);
120 const GstD3D11Format *in_format =
121 gst_d3d11_device_format_from_gst (window->device,
122 GST_VIDEO_INFO_FORMAT (&window->info));
123 gboolean hardware = FALSE;
124 GstD3D11VideoProcessor *processor = NULL;
126 DXGI_FORMAT formats_to_check[] = {
127 DXGI_FORMAT_R8G8B8A8_UNORM,
128 DXGI_FORMAT_B8G8R8A8_UNORM,
129 DXGI_FORMAT_R10G10B10A2_UNORM
132 if (in_color_space && in_format &&
133 in_format->dxgi_format != DXGI_FORMAT_UNKNOWN) {
134 g_object_get (window->device, "hardware", &hardware, NULL);
139 gst_d3d11_video_processor_new (window->device,
140 GST_VIDEO_INFO_WIDTH (&window->info),
141 GST_VIDEO_INFO_HEIGHT (&window->info), display_width, display_height);
144 /* Check if video processor can support all possible output dxgi formats */
145 for (i = 0; i < G_N_ELEMENTS (formats_to_check) && processor; i++) {
146 DXGI_FORMAT in_dxgi_format = in_format->dxgi_format;
147 DXGI_FORMAT out_dxgi_format = formats_to_check[i];
148 DXGI_COLOR_SPACE_TYPE in_dxgi_color_space =
149 (DXGI_COLOR_SPACE_TYPE) in_color_space->dxgi_color_space_type;
151 if (!gst_d3d11_video_processor_check_format_conversion (processor,
152 in_dxgi_format, in_dxgi_color_space, out_dxgi_format,
153 DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709)) {
154 GST_DEBUG_OBJECT (window, "Conversion is not supported by device");
155 g_clear_pointer (&processor, gst_d3d11_video_processor_free);
161 gst_d3d11_video_processor_set_input_dxgi_color_space (processor,
162 (DXGI_COLOR_SPACE_TYPE) in_color_space->dxgi_color_space_type);
163 gst_d3d11_video_processor_set_output_dxgi_color_space (processor,
164 DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709);
167 window->processor = processor;
170 *video_processor_available = !!window->processor;
173 gst_d3d11_converter_new (window->device, &window->info,
174 &window->render_info, nullptr);
176 if (!window->converter) {
177 GST_ERROR_OBJECT (window, "Cannot create converter");
178 g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_FAILED,
179 "Cannot create converter");
184 gst_d3d11_overlay_compositor_new (window->device, &window->render_info);
185 if (!window->compositor) {
186 GST_ERROR_OBJECT (window, "Cannot create overlay compositor");
187 g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_FAILED,
188 "Cannot create overlay compositor");
192 gst_d3d11_device_unlock (window->device);
197 gst_d3d11_device_unlock (window->device);
203 gst_d3d11_window_dummy_clear_resources (GstD3D11WindowDummy * self)
205 GST_D3D11_CLEAR_COM (self->fallback_pov);
206 GST_D3D11_CLEAR_COM (self->fallback_rtv);
207 GST_D3D11_CLEAR_COM (self->fallback_texture);
211 gst_d3d11_window_dummy_unprepare (GstD3D11Window * window)
213 GstD3D11WindowDummy *self = GST_D3D11_WINDOW_DUMMY (window);
215 gst_d3d11_window_dummy_clear_resources (self);
219 gst_d3d11_window_dummy_on_resize (GstD3D11Window * window,
220 guint width, guint height)
222 GstVideoRectangle src_rect, dst_rect, rst_rect;
229 if (window->force_aspect_ratio) {
232 src_rect.w = GST_VIDEO_INFO_WIDTH (&window->render_info);
233 src_rect.h = GST_VIDEO_INFO_HEIGHT (&window->render_info);
235 gst_video_sink_center_rect (src_rect, dst_rect, &rst_rect, TRUE);
240 window->render_rect.left = rst_rect.x;
241 window->render_rect.top = rst_rect.y;
242 window->render_rect.right = rst_rect.x + rst_rect.w;
243 window->render_rect.bottom = rst_rect.y + rst_rect.h;
245 window->first_present = TRUE;
249 gst_d3d11_window_dummy_setup_fallback_texture (GstD3D11Window * window,
250 D3D11_TEXTURE2D_DESC * shared_desc)
252 GstD3D11WindowDummy *self = GST_D3D11_WINDOW_DUMMY (window);
253 D3D11_TEXTURE2D_DESC desc = { 0, };
254 D3D11_RENDER_TARGET_VIEW_DESC rtv_desc;
255 ID3D11Device *device_handle =
256 gst_d3d11_device_get_device_handle (window->device);
257 gboolean need_new_texture = FALSE;
260 if (!self->fallback_texture) {
261 GST_DEBUG_OBJECT (self,
262 "We have no configured fallback texture, create new one");
263 need_new_texture = TRUE;
265 self->fallback_texture->GetDesc (&desc);
266 if (shared_desc->Format != desc.Format) {
267 GST_DEBUG_OBJECT (self, "Texture formats are different, create new one");
268 need_new_texture = TRUE;
269 } else if (shared_desc->Width > desc.Width ||
270 shared_desc->Height > desc.Height) {
271 GST_DEBUG_OBJECT (self, "Needs larger size of fallback texture");
272 need_new_texture = TRUE;
276 if (!need_new_texture)
279 gst_d3d11_window_dummy_clear_resources (self);
281 desc.Width = shared_desc->Width;
282 desc.Height = shared_desc->Height;
285 desc.Format = shared_desc->Format;
286 desc.SampleDesc.Count = 1;
287 desc.SampleDesc.Quality = 0;
288 desc.Usage = D3D11_USAGE_DEFAULT;
289 desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
291 hr = device_handle->CreateTexture2D (&desc, NULL, &self->fallback_texture);
292 if (!gst_d3d11_result (hr, window->device)) {
293 GST_ERROR_OBJECT (self, "Couldn't create fallback texture");
297 rtv_desc.Format = DXGI_FORMAT_UNKNOWN;
298 rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
299 rtv_desc.Texture2D.MipSlice = 0;
301 hr = device_handle->CreateRenderTargetView (self->fallback_texture, &rtv_desc,
302 &self->fallback_rtv);
303 if (!gst_d3d11_result (hr, window->device)) {
304 GST_ERROR_OBJECT (self,
305 "Couldn't get render target view from fallback texture");
306 gst_d3d11_window_dummy_clear_resources (self);
310 if (window->processor) {
311 D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC pov_desc;
313 pov_desc.ViewDimension = D3D11_VPOV_DIMENSION_TEXTURE2D;
314 pov_desc.Texture2D.MipSlice = 0;
316 if (!gst_d3d11_video_processor_create_output_view (window->processor,
317 &pov_desc, self->fallback_texture, &self->fallback_pov)) {
318 GST_ERROR_OBJECT (window,
319 "ID3D11VideoProcessorOutputView is unavailable");
320 gst_d3d11_window_dummy_clear_resources (self);
330 gst_d3d11_window_dummy_open_shared_handle (GstD3D11Window * window,
331 GstD3D11WindowSharedHandleData * data)
333 GstD3D11WindowDummy *self = GST_D3D11_WINDOW_DUMMY (window);
334 GstD3D11Device *device = window->device;
335 ID3D11Device *device_handle;
337 ID3D11Texture2D *texture = NULL;
338 IDXGIKeyedMutex *keyed_mutex = NULL;
339 ID3D11VideoProcessorOutputView *pov = NULL;
340 ID3D11RenderTargetView *rtv = NULL;
341 D3D11_TEXTURE2D_DESC desc;
342 gboolean use_keyed_mutex = FALSE;
343 gboolean need_fallback_texture = FALSE;
345 device_handle = gst_d3d11_device_get_device_handle (device);
347 if ((data->texture_misc_flags & D3D11_RESOURCE_MISC_SHARED_NTHANDLE) ==
348 D3D11_RESOURCE_MISC_SHARED_NTHANDLE) {
349 ComPtr<ID3D11Device1> device1_handle;
351 hr = device_handle->QueryInterface (IID_PPV_ARGS (&device1_handle));
352 if (!gst_d3d11_result (hr, device))
355 hr = device1_handle->OpenSharedResource1 (data->shared_handle,
356 IID_PPV_ARGS (&texture));
358 hr = device_handle->OpenSharedResource (data->shared_handle,
359 IID_PPV_ARGS (&texture));
362 if (!gst_d3d11_result (hr, device))
365 texture->GetDesc (&desc);
366 use_keyed_mutex = (desc.MiscFlags & D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX) ==
367 D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
369 if (use_keyed_mutex) {
370 hr = texture->QueryInterface (IID_PPV_ARGS (&keyed_mutex));
371 if (!gst_d3d11_result (hr, device))
375 if (window->processor) {
376 if (use_keyed_mutex) {
377 D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC pov_desc;
379 pov_desc.ViewDimension = D3D11_VPOV_DIMENSION_TEXTURE2D;
380 pov_desc.Texture2D.MipSlice = 0;
382 if (!gst_d3d11_video_processor_create_output_view (window->processor,
383 &pov_desc, texture, &pov)) {
384 GST_WARNING_OBJECT (window,
385 "ID3D11VideoProcessorOutputView is unavailable");
388 /* HACK: If external texture was created without keyed mutext
389 * and we need to used videoprocessor to convert decoder output texture
390 * to external texture, converted texture by videoprocessor seems to be broken
391 * Probably that's because of missing flush/sync API around videoprocessor.
392 * (e.g., ID3D11VideoContext and ID3D11VideoProcessor have no
393 * flushing api such as ID3D11DeviceContext::Flush).
394 * To workaround the case, we need to use fallback texture and copy back
395 * to external texture
398 need_fallback_texture = TRUE;
400 GST_TRACE_OBJECT (window,
401 "We are using video processor but keyed mutex is unavailable");
402 if (!gst_d3d11_window_dummy_setup_fallback_texture (window, &desc)) {
408 hr = device_handle->CreateRenderTargetView ((ID3D11Resource *) texture,
410 if (!gst_d3d11_result (hr, device))
414 hr = keyed_mutex->AcquireSync(data->acquire_key, INFINITE);
415 if (!gst_d3d11_result (hr, device))
419 /* Everything is prepared now */
420 gst_d3d11_window_dummy_on_resize (window, desc.Width, desc.Height);
422 /* Move owned resources */
423 data->texture = texture;
424 data->keyed_mutex = keyed_mutex;
428 if (need_fallback_texture) {
429 data->fallback_pov = self->fallback_pov;
430 data->fallback_rtv = self->fallback_rtv;
432 data->fallback_pov = nullptr;
433 data->fallback_rtv = nullptr;
439 GST_D3D11_CLEAR_COM (texture);
440 GST_D3D11_CLEAR_COM (keyed_mutex);
441 GST_D3D11_CLEAR_COM (pov);
442 GST_D3D11_CLEAR_COM (rtv);
449 gst_d3d11_window_dummy_release_shared_handle (GstD3D11Window * window,
450 GstD3D11WindowSharedHandleData * data)
452 GstD3D11WindowDummy *self = GST_D3D11_WINDOW_DUMMY (window);
453 GstD3D11Device *device = window->device;
456 /* TODO: cache owned resource for the later reuse? */
457 if (data->keyed_mutex) {
458 hr = data->keyed_mutex->ReleaseSync (data->release_key);
459 gst_d3d11_result (hr, device);
461 data->keyed_mutex->Release ();
464 ComPtr<ID3D11Query> query;
466 D3D11_QUERY_DESC query_desc;
467 ID3D11Device *device_handle = gst_d3d11_device_get_device_handle (device);
468 ID3D11DeviceContext *context_handle =
469 gst_d3d11_device_get_device_context_handle (device);
470 BOOL sync_done = FALSE;
472 /* If keyed mutex is not used, let's handle sync manually by using
473 * ID3D11Query. Issued GPU commands might not be finished yet */
474 query_desc.Query = D3D11_QUERY_EVENT;
475 query_desc.MiscFlags = 0;
477 hr = device_handle->CreateQuery (&query_desc, &query);
478 if (!gst_d3d11_result (hr, device)) {
479 GST_ERROR_OBJECT (self, "Couldn't Create event query");
483 /* Copy from fallback texture to user's texture */
484 if (data->fallback_rtv) {
486 D3D11_TEXTURE2D_DESC desc;
487 ID3D11DeviceContext *context_handle =
488 gst_d3d11_device_get_device_context_handle (device);
490 data->texture->GetDesc (&desc);
496 src_box.right = desc.Width;
497 src_box.bottom = desc.Height;
499 context_handle->CopySubresourceRegion (data->texture, 0, 0, 0, 0,
500 self->fallback_texture, 0, &src_box);
502 context_handle->End (query.Get ());
504 /* Wait until all issued GPU commands are finished */
506 context_handle->GetData (query.Get (), &sync_done, sizeof (BOOL), 0);
507 } while (!sync_done && (hr == S_OK || hr == S_FALSE));
509 if (!gst_d3d11_result (hr, device)) {
510 GST_ERROR_OBJECT (self, "Couldn't sync GPU operation");
515 GST_D3D11_CLEAR_COM (data->rtv);
516 GST_D3D11_CLEAR_COM (data->pov);
517 GST_D3D11_CLEAR_COM (data->texture);
523 gst_d3d11_window_dummy_new (GstD3D11Device * device)
525 GstD3D11Window *window;
527 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), NULL);
529 window = (GstD3D11Window *)
530 g_object_new (GST_TYPE_D3D11_WINDOW_DUMMY, "d3d11device", device, NULL);
532 window->initialized = TRUE;
533 g_object_ref_sink (window);