1 /*
2 * GStreamer
3 * Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
4 *
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.
9 *
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.
14 *
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.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include "gstd3d11window_dummy.h"
26 #include "gstd3d11pluginutils.h"
27 #include <wrl.h>
28
29 /* *INDENT-OFF* */
30 using namespace Microsoft::WRL;
31 /* *INDENT-ON* */
32
33 GST_DEBUG_CATEGORY_EXTERN (gst_d3d11_window_debug);
34 #define GST_CAT_DEFAULT gst_d3d11_window_debug
35
36 struct _GstD3D11WindowDummy
37 {
38 GstD3D11Window parent;
39
40 ID3D11Texture2D *fallback_texture;
41 ID3D11VideoProcessorOutputView *fallback_pov;
42 ID3D11RenderTargetView *fallback_rtv;
43 };
44
45 #define gst_d3d11_window_dummy_parent_class parent_class
46 G_DEFINE_TYPE (GstD3D11WindowDummy, gst_d3d11_window_dummy,
47 GST_TYPE_D3D11_WINDOW);
48
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);
55 static gboolean
56 gst_d3d11_window_dummy_open_shared_handle (GstD3D11Window * window,
57 GstD3D11WindowSharedHandleData * data);
58 static gboolean
59 gst_d3d11_window_dummy_release_shared_handle (GstD3D11Window * window,
60 GstD3D11WindowSharedHandleData * data);
61
62 static void
gst_d3d11_window_dummy_class_init(GstD3D11WindowDummyClass * klass)63 gst_d3d11_window_dummy_class_init (GstD3D11WindowDummyClass * klass)
64 {
65 GstD3D11WindowClass *window_class = GST_D3D11_WINDOW_CLASS (klass);
66
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);
76 }
77
78 static void
gst_d3d11_window_dummy_init(GstD3D11WindowDummy * self)79 gst_d3d11_window_dummy_init (GstD3D11WindowDummy * self)
80 {
81 }
82
83 static gboolean
gst_d3d11_window_dummy_prepare(GstD3D11Window * window,guint display_width,guint display_height,GstCaps * caps,gboolean * video_processor_available,GError ** error)84 gst_d3d11_window_dummy_prepare (GstD3D11Window * window,
85 guint display_width, guint display_height, GstCaps * caps,
86 gboolean * video_processor_available, GError ** error)
87 {
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);
91
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 */
94
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;
100
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);
105
106 gst_video_info_set_format (&window->render_info,
107 GST_VIDEO_FORMAT_BGRA, display_width, display_height);
108
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;
114
115 gst_d3d11_device_lock (window->device);
116
117 #if (GST_D3D11_DXGI_HEADER_VERSION >= 4)
118 {
119 const GstDxgiColorSpace *in_color_space =
120 gst_d3d11_video_info_to_dxgi_color_space (&window->info);
121 const GstD3D11Format *in_format =
122 gst_d3d11_device_format_from_gst (window->device,
123 GST_VIDEO_INFO_FORMAT (&window->info));
124 gboolean hardware = FALSE;
125 GstD3D11VideoProcessor *processor = NULL;
126 guint i;
127 DXGI_FORMAT formats_to_check[] = {
128 DXGI_FORMAT_R8G8B8A8_UNORM,
129 DXGI_FORMAT_B8G8R8A8_UNORM,
130 DXGI_FORMAT_R10G10B10A2_UNORM
131 };
132
133 if (in_color_space && in_format &&
134 in_format->dxgi_format != DXGI_FORMAT_UNKNOWN) {
135 g_object_get (window->device, "hardware", &hardware, NULL);
136 }
137
138 if (hardware) {
139 processor =
140 gst_d3d11_video_processor_new (window->device,
141 GST_VIDEO_INFO_WIDTH (&window->info),
142 GST_VIDEO_INFO_HEIGHT (&window->info), display_width, display_height);
143 }
144
145 /* Check if video processor can support all possible output dxgi formats */
146 for (i = 0; i < G_N_ELEMENTS (formats_to_check) && processor; i++) {
147 DXGI_FORMAT in_dxgi_format = in_format->dxgi_format;
148 DXGI_FORMAT out_dxgi_format = formats_to_check[i];
149 DXGI_COLOR_SPACE_TYPE in_dxgi_color_space =
150 (DXGI_COLOR_SPACE_TYPE) in_color_space->dxgi_color_space_type;
151
152 if (!gst_d3d11_video_processor_check_format_conversion (processor,
153 in_dxgi_format, in_dxgi_color_space, out_dxgi_format,
154 DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709)) {
155 GST_DEBUG_OBJECT (window, "Conversion is not supported by device");
156 g_clear_pointer (&processor, gst_d3d11_video_processor_free);
157 break;
158 }
159 }
160
161 if (processor) {
162 gst_d3d11_video_processor_set_input_dxgi_color_space (processor,
163 (DXGI_COLOR_SPACE_TYPE) in_color_space->dxgi_color_space_type);
164 gst_d3d11_video_processor_set_output_dxgi_color_space (processor,
165 DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709);
166 }
167
168 window->processor = processor;
169 }
170 #endif
171 *video_processor_available = !!window->processor;
172
173 window->converter =
174 gst_d3d11_converter_new (window->device, &window->info,
175 &window->render_info, nullptr);
176
177 if (!window->converter) {
178 GST_ERROR_OBJECT (window, "Cannot create converter");
179 g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_FAILED,
180 "Cannot create converter");
181 goto error;
182 }
183
184 window->compositor =
185 gst_d3d11_overlay_compositor_new (window->device, &window->render_info);
186 if (!window->compositor) {
187 GST_ERROR_OBJECT (window, "Cannot create overlay compositor");
188 g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_FAILED,
189 "Cannot create overlay compositor");
190 goto error;
191 }
192
193 gst_d3d11_device_unlock (window->device);
194
195 return TRUE;
196
197 error:
198 gst_d3d11_device_unlock (window->device);
199
200 return FALSE;
201 }
202
203 static void
gst_d3d11_window_dummy_clear_resources(GstD3D11WindowDummy * self)204 gst_d3d11_window_dummy_clear_resources (GstD3D11WindowDummy * self)
205 {
206 GST_D3D11_CLEAR_COM (self->fallback_pov);
207 GST_D3D11_CLEAR_COM (self->fallback_rtv);
208 GST_D3D11_CLEAR_COM (self->fallback_texture);
209 }
210
211 static void
gst_d3d11_window_dummy_unprepare(GstD3D11Window * window)212 gst_d3d11_window_dummy_unprepare (GstD3D11Window * window)
213 {
214 GstD3D11WindowDummy *self = GST_D3D11_WINDOW_DUMMY (window);
215
216 gst_d3d11_window_dummy_clear_resources (self);
217 }
218
219 static void
gst_d3d11_window_dummy_on_resize(GstD3D11Window * window,guint width,guint height)220 gst_d3d11_window_dummy_on_resize (GstD3D11Window * window,
221 guint width, guint height)
222 {
223 GstVideoRectangle src_rect, dst_rect, rst_rect;
224
225 dst_rect.x = 0;
226 dst_rect.y = 0;
227 dst_rect.w = width;
228 dst_rect.h = height;
229
230 if (window->force_aspect_ratio) {
231 src_rect.x = 0;
232 src_rect.y = 0;
233 src_rect.w = GST_VIDEO_INFO_WIDTH (&window->render_info);
234 src_rect.h = GST_VIDEO_INFO_HEIGHT (&window->render_info);
235
236 gst_video_sink_center_rect (src_rect, dst_rect, &rst_rect, TRUE);
237 } else {
238 rst_rect = dst_rect;
239 }
240
241 window->render_rect.left = rst_rect.x;
242 window->render_rect.top = rst_rect.y;
243 window->render_rect.right = rst_rect.x + rst_rect.w;
244 window->render_rect.bottom = rst_rect.y + rst_rect.h;
245
246 window->first_present = TRUE;
247 }
248
249 static gboolean
gst_d3d11_window_dummy_setup_fallback_texture(GstD3D11Window * window,D3D11_TEXTURE2D_DESC * shared_desc)250 gst_d3d11_window_dummy_setup_fallback_texture (GstD3D11Window * window,
251 D3D11_TEXTURE2D_DESC * shared_desc)
252 {
253 GstD3D11WindowDummy *self = GST_D3D11_WINDOW_DUMMY (window);
254 D3D11_TEXTURE2D_DESC desc = { 0, };
255 D3D11_RENDER_TARGET_VIEW_DESC rtv_desc;
256 ID3D11Device *device_handle =
257 gst_d3d11_device_get_device_handle (window->device);
258 gboolean need_new_texture = FALSE;
259 HRESULT hr;
260
261 if (!self->fallback_texture) {
262 GST_DEBUG_OBJECT (self,
263 "We have no configured fallback texture, create new one");
264 need_new_texture = TRUE;
265 } else {
266 self->fallback_texture->GetDesc (&desc);
267 if (shared_desc->Format != desc.Format) {
268 GST_DEBUG_OBJECT (self, "Texture formats are different, create new one");
269 need_new_texture = TRUE;
270 } else if (shared_desc->Width > desc.Width ||
271 shared_desc->Height > desc.Height) {
272 GST_DEBUG_OBJECT (self, "Needs larger size of fallback texture");
273 need_new_texture = TRUE;
274 }
275 }
276
277 if (!need_new_texture)
278 return TRUE;
279
280 gst_d3d11_window_dummy_clear_resources (self);
281
282 desc.Width = shared_desc->Width;
283 desc.Height = shared_desc->Height;
284 desc.MipLevels = 1;
285 desc.ArraySize = 1;
286 desc.Format = shared_desc->Format;
287 desc.SampleDesc.Count = 1;
288 desc.SampleDesc.Quality = 0;
289 desc.Usage = D3D11_USAGE_DEFAULT;
290 desc.BindFlags = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_RENDER_TARGET;
291
292 hr = device_handle->CreateTexture2D (&desc, NULL, &self->fallback_texture);
293 if (!gst_d3d11_result (hr, window->device)) {
294 GST_ERROR_OBJECT (self, "Couldn't create fallback texture");
295 return FALSE;
296 }
297
298 rtv_desc.Format = DXGI_FORMAT_UNKNOWN;
299 rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
300 rtv_desc.Texture2D.MipSlice = 0;
301
302 hr = device_handle->CreateRenderTargetView (self->fallback_texture, &rtv_desc,
303 &self->fallback_rtv);
304 if (!gst_d3d11_result (hr, window->device)) {
305 GST_ERROR_OBJECT (self,
306 "Couldn't get render target view from fallback texture");
307 gst_d3d11_window_dummy_clear_resources (self);
308 return FALSE;
309 }
310
311 if (window->processor) {
312 D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC pov_desc;
313
314 pov_desc.ViewDimension = D3D11_VPOV_DIMENSION_TEXTURE2D;
315 pov_desc.Texture2D.MipSlice = 0;
316
317 if (!gst_d3d11_video_processor_create_output_view (window->processor,
318 &pov_desc, self->fallback_texture, &self->fallback_pov)) {
319 GST_ERROR_OBJECT (window,
320 "ID3D11VideoProcessorOutputView is unavailable");
321 gst_d3d11_window_dummy_clear_resources (self);
322 return FALSE;
323 }
324 }
325
326 return TRUE;
327 }
328
329 /* *INDENT-OFF* */
330 static gboolean
gst_d3d11_window_dummy_open_shared_handle(GstD3D11Window * window,GstD3D11WindowSharedHandleData * data)331 gst_d3d11_window_dummy_open_shared_handle (GstD3D11Window * window,
332 GstD3D11WindowSharedHandleData * data)
333 {
334 GstD3D11WindowDummy *self = GST_D3D11_WINDOW_DUMMY (window);
335 GstD3D11Device *device = window->device;
336 ID3D11Device *device_handle;
337 HRESULT hr;
338 ID3D11Texture2D *texture = NULL;
339 IDXGIKeyedMutex *keyed_mutex = NULL;
340 ID3D11VideoProcessorOutputView *pov = NULL;
341 ID3D11RenderTargetView *rtv = NULL;
342 D3D11_TEXTURE2D_DESC desc;
343 gboolean use_keyed_mutex = FALSE;
344 gboolean need_fallback_texture = FALSE;
345
346 device_handle = gst_d3d11_device_get_device_handle (device);
347
348 if ((data->texture_misc_flags & D3D11_RESOURCE_MISC_SHARED_NTHANDLE) ==
349 D3D11_RESOURCE_MISC_SHARED_NTHANDLE) {
350 ComPtr<ID3D11Device1> device1_handle;
351
352 hr = device_handle->QueryInterface (IID_PPV_ARGS (&device1_handle));
353 if (!gst_d3d11_result (hr, device))
354 return FALSE;
355
356 hr = device1_handle->OpenSharedResource1 (data->shared_handle,
357 IID_PPV_ARGS (&texture));
358 } else {
359 hr = device_handle->OpenSharedResource (data->shared_handle,
360 IID_PPV_ARGS (&texture));
361 }
362
363 if (!gst_d3d11_result (hr, device))
364 return FALSE;
365
366 texture->GetDesc (&desc);
367 use_keyed_mutex = (desc.MiscFlags & D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX) ==
368 D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
369
370 if (use_keyed_mutex) {
371 hr = texture->QueryInterface (IID_PPV_ARGS (&keyed_mutex));
372 if (!gst_d3d11_result (hr, device))
373 goto out;
374 }
375
376 if (window->processor) {
377 if (use_keyed_mutex) {
378 D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC pov_desc;
379
380 pov_desc.ViewDimension = D3D11_VPOV_DIMENSION_TEXTURE2D;
381 pov_desc.Texture2D.MipSlice = 0;
382
383 if (!gst_d3d11_video_processor_create_output_view (window->processor,
384 &pov_desc, texture, &pov)) {
385 GST_WARNING_OBJECT (window,
386 "ID3D11VideoProcessorOutputView is unavailable");
387 }
388 } else {
389 /* HACK: If external texture was created without keyed mutext
390 * and we need to used videoprocessor to convert decoder output texture
391 * to external texture, converted texture by videoprocessor seems to be broken
392 * Probably that's because of missing flush/sync API around videoprocessor.
393 * (e.g., ID3D11VideoContext and ID3D11VideoProcessor have no
394 * flushing api such as ID3D11DeviceContext::Flush).
395 * To workaround the case, we need to use fallback texture and copy back
396 * to external texture
397 */
398
399 need_fallback_texture = TRUE;
400
401 GST_TRACE_OBJECT (window,
402 "We are using video processor but keyed mutex is unavailable");
403 if (!gst_d3d11_window_dummy_setup_fallback_texture (window, &desc)) {
404 goto out;
405 }
406 }
407 }
408
409 hr = device_handle->CreateRenderTargetView ((ID3D11Resource *) texture,
410 NULL, &rtv);
411 if (!gst_d3d11_result (hr, device))
412 goto out;
413
414 if (keyed_mutex) {
415 hr = keyed_mutex->AcquireSync(data->acquire_key, INFINITE);
416 if (!gst_d3d11_result (hr, device))
417 goto out;
418 }
419
420 /* Everything is prepared now */
421 gst_d3d11_window_dummy_on_resize (window, desc.Width, desc.Height);
422
423 /* Move owned resources */
424 data->texture = texture;
425 data->keyed_mutex = keyed_mutex;
426 data->pov = pov;
427 data->rtv = rtv;
428
429 if (need_fallback_texture) {
430 data->fallback_pov = self->fallback_pov;
431 data->fallback_rtv = self->fallback_rtv;
432 } else {
433 data->fallback_pov = nullptr;
434 data->fallback_rtv = nullptr;
435 }
436
437 return TRUE;
438
439 out:
440 GST_D3D11_CLEAR_COM (texture);
441 GST_D3D11_CLEAR_COM (keyed_mutex);
442 GST_D3D11_CLEAR_COM (pov);
443 GST_D3D11_CLEAR_COM (rtv);
444
445 return FALSE;
446 }
447 /* *INDENT-ON* */
448
449 static gboolean
gst_d3d11_window_dummy_release_shared_handle(GstD3D11Window * window,GstD3D11WindowSharedHandleData * data)450 gst_d3d11_window_dummy_release_shared_handle (GstD3D11Window * window,
451 GstD3D11WindowSharedHandleData * data)
452 {
453 GstD3D11WindowDummy *self = GST_D3D11_WINDOW_DUMMY (window);
454 GstD3D11Device *device = window->device;
455 HRESULT hr;
456
457 /* TODO: cache owned resource for the later reuse? */
458 if (data->keyed_mutex) {
459 hr = data->keyed_mutex->ReleaseSync (data->release_key);
460 gst_d3d11_result (hr, device);
461
462 data->keyed_mutex->Release ();
463 } else {
464 /* *INDENT-OFF* */
465 ComPtr<ID3D11Query> query;
466 /* *INDENT-ON* */
467 D3D11_QUERY_DESC query_desc;
468 ID3D11Device *device_handle = gst_d3d11_device_get_device_handle (device);
469 ID3D11DeviceContext *context_handle =
470 gst_d3d11_device_get_device_context_handle (device);
471 BOOL sync_done = FALSE;
472
473 /* If keyed mutex is not used, let's handle sync manually by using
474 * ID3D11Query. Issued GPU commands might not be finished yet */
475 query_desc.Query = D3D11_QUERY_EVENT;
476 query_desc.MiscFlags = 0;
477
478 hr = device_handle->CreateQuery (&query_desc, &query);
479 if (!gst_d3d11_result (hr, device)) {
480 GST_ERROR_OBJECT (self, "Couldn't Create event query");
481 return FALSE;
482 }
483
484 /* Copy from fallback texture to user's texture */
485 if (data->fallback_rtv) {
486 D3D11_BOX src_box;
487 D3D11_TEXTURE2D_DESC desc;
488 ID3D11DeviceContext *context_handle =
489 gst_d3d11_device_get_device_context_handle (device);
490
491 data->texture->GetDesc (&desc);
492
493 src_box.left = 0;
494 src_box.top = 0;
495 src_box.front = 0;
496 src_box.back = 1;
497 src_box.right = desc.Width;
498 src_box.bottom = desc.Height;
499
500 context_handle->CopySubresourceRegion (data->texture, 0, 0, 0, 0,
501 self->fallback_texture, 0, &src_box);
502 }
503 context_handle->End (query.Get ());
504
505 /* Wait until all issued GPU commands are finished */
506 do {
507 context_handle->GetData (query.Get (), &sync_done, sizeof (BOOL), 0);
508 } while (!sync_done && (hr == S_OK || hr == S_FALSE));
509
510 if (!gst_d3d11_result (hr, device)) {
511 GST_ERROR_OBJECT (self, "Couldn't sync GPU operation");
512 return FALSE;
513 }
514 }
515
516 GST_D3D11_CLEAR_COM (data->rtv);
517 GST_D3D11_CLEAR_COM (data->pov);
518 GST_D3D11_CLEAR_COM (data->texture);
519
520 return TRUE;
521 }
522
523 GstD3D11Window *
gst_d3d11_window_dummy_new(GstD3D11Device * device)524 gst_d3d11_window_dummy_new (GstD3D11Device * device)
525 {
526 GstD3D11Window *window;
527
528 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), NULL);
529
530 window = (GstD3D11Window *)
531 g_object_new (GST_TYPE_D3D11_WINDOW_DUMMY, "d3d11device", device, NULL);
532
533 window->initialized = TRUE;
534 g_object_ref_sink (window);
535
536 return window;
537 }
538