d3d11: Update build-time dependency
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / sys / d3d11 / gstd3d11window_dummy.cpp
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
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
79 gst_d3d11_window_dummy_init (GstD3D11WindowDummy * self)
80 {
81 }
82
83 static gboolean
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   {
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;
125     guint i;
126     DXGI_FORMAT formats_to_check[] = {
127       DXGI_FORMAT_R8G8B8A8_UNORM,
128       DXGI_FORMAT_B8G8R8A8_UNORM,
129       DXGI_FORMAT_R10G10B10A2_UNORM
130     };
131
132     if (in_color_space && in_format &&
133         in_format->dxgi_format != DXGI_FORMAT_UNKNOWN) {
134       g_object_get (window->device, "hardware", &hardware, NULL);
135     }
136
137     if (hardware) {
138       processor =
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);
142     }
143
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;
150
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);
156         break;
157       }
158     }
159
160     if (processor) {
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);
165     }
166
167     window->processor = processor;
168   }
169
170   *video_processor_available = !!window->processor;
171
172   window->converter =
173       gst_d3d11_converter_new (window->device, &window->info,
174       &window->render_info, nullptr);
175
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");
180     goto error;
181   }
182
183   window->compositor =
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");
189     goto error;
190   }
191
192   gst_d3d11_device_unlock (window->device);
193
194   return TRUE;
195
196 error:
197   gst_d3d11_device_unlock (window->device);
198
199   return FALSE;
200 }
201
202 static void
203 gst_d3d11_window_dummy_clear_resources (GstD3D11WindowDummy * self)
204 {
205   GST_D3D11_CLEAR_COM (self->fallback_pov);
206   GST_D3D11_CLEAR_COM (self->fallback_rtv);
207   GST_D3D11_CLEAR_COM (self->fallback_texture);
208 }
209
210 static void
211 gst_d3d11_window_dummy_unprepare (GstD3D11Window * window)
212 {
213   GstD3D11WindowDummy *self = GST_D3D11_WINDOW_DUMMY (window);
214
215   gst_d3d11_window_dummy_clear_resources (self);
216 }
217
218 static void
219 gst_d3d11_window_dummy_on_resize (GstD3D11Window * window,
220     guint width, guint height)
221 {
222   GstVideoRectangle src_rect, dst_rect, rst_rect;
223
224   dst_rect.x = 0;
225   dst_rect.y = 0;
226   dst_rect.w = width;
227   dst_rect.h = height;
228
229   if (window->force_aspect_ratio) {
230     src_rect.x = 0;
231     src_rect.y = 0;
232     src_rect.w = GST_VIDEO_INFO_WIDTH (&window->render_info);
233     src_rect.h = GST_VIDEO_INFO_HEIGHT (&window->render_info);
234
235     gst_video_sink_center_rect (src_rect, dst_rect, &rst_rect, TRUE);
236   } else {
237     rst_rect = dst_rect;
238   }
239
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;
244
245   window->first_present = TRUE;
246 }
247
248 static gboolean
249 gst_d3d11_window_dummy_setup_fallback_texture (GstD3D11Window * window,
250     D3D11_TEXTURE2D_DESC * shared_desc)
251 {
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;
258   HRESULT hr;
259
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;
264   } else {
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;
273     }
274   }
275
276   if (!need_new_texture)
277     return TRUE;
278
279   gst_d3d11_window_dummy_clear_resources (self);
280
281   desc.Width = shared_desc->Width;
282   desc.Height = shared_desc->Height;
283   desc.MipLevels = 1;
284   desc.ArraySize = 1;
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;
290
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");
294     return FALSE;
295   }
296
297   rtv_desc.Format = DXGI_FORMAT_UNKNOWN;
298   rtv_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
299   rtv_desc.Texture2D.MipSlice = 0;
300
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);
307     return FALSE;
308   }
309
310   if (window->processor) {
311     D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC pov_desc;
312
313     pov_desc.ViewDimension = D3D11_VPOV_DIMENSION_TEXTURE2D;
314     pov_desc.Texture2D.MipSlice = 0;
315
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);
321       return FALSE;
322     }
323   }
324
325   return TRUE;
326 }
327
328 /* *INDENT-OFF* */
329 static gboolean
330 gst_d3d11_window_dummy_open_shared_handle (GstD3D11Window * window,
331     GstD3D11WindowSharedHandleData * data)
332 {
333   GstD3D11WindowDummy *self = GST_D3D11_WINDOW_DUMMY (window);
334   GstD3D11Device *device = window->device;
335   ID3D11Device *device_handle;
336   HRESULT hr;
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;
344
345   device_handle = gst_d3d11_device_get_device_handle (device);
346
347   if ((data->texture_misc_flags & D3D11_RESOURCE_MISC_SHARED_NTHANDLE) ==
348       D3D11_RESOURCE_MISC_SHARED_NTHANDLE) {
349     ComPtr<ID3D11Device1> device1_handle;
350
351     hr = device_handle->QueryInterface (IID_PPV_ARGS (&device1_handle));
352     if (!gst_d3d11_result (hr, device))
353       return FALSE;
354
355     hr = device1_handle->OpenSharedResource1 (data->shared_handle,
356         IID_PPV_ARGS (&texture));
357   } else {
358     hr = device_handle->OpenSharedResource (data->shared_handle,
359         IID_PPV_ARGS (&texture));
360   }
361
362   if (!gst_d3d11_result (hr, device))
363     return FALSE;
364
365   texture->GetDesc (&desc);
366   use_keyed_mutex = (desc.MiscFlags & D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX) ==
367       D3D11_RESOURCE_MISC_SHARED_KEYEDMUTEX;
368
369   if (use_keyed_mutex) {
370     hr = texture->QueryInterface (IID_PPV_ARGS (&keyed_mutex));
371     if (!gst_d3d11_result (hr, device))
372       goto out;
373   }
374
375   if (window->processor) {
376     if (use_keyed_mutex) {
377       D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC pov_desc;
378
379       pov_desc.ViewDimension = D3D11_VPOV_DIMENSION_TEXTURE2D;
380       pov_desc.Texture2D.MipSlice = 0;
381
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");
386       }
387     } else {
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
396        */
397
398       need_fallback_texture = TRUE;
399
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)) {
403         goto out;
404       }
405     }
406   }
407
408   hr = device_handle->CreateRenderTargetView ((ID3D11Resource *) texture,
409       NULL, &rtv);
410   if (!gst_d3d11_result (hr, device))
411     goto out;
412
413   if (keyed_mutex) {
414     hr = keyed_mutex->AcquireSync(data->acquire_key, INFINITE);
415     if (!gst_d3d11_result (hr, device))
416       goto out;
417   }
418
419   /* Everything is prepared now */
420   gst_d3d11_window_dummy_on_resize (window, desc.Width, desc.Height);
421
422   /* Move owned resources */
423   data->texture = texture;
424   data->keyed_mutex = keyed_mutex;
425   data->pov = pov;
426   data->rtv = rtv;
427
428   if (need_fallback_texture) {
429     data->fallback_pov = self->fallback_pov;
430     data->fallback_rtv = self->fallback_rtv;
431   } else {
432     data->fallback_pov = nullptr;
433     data->fallback_rtv = nullptr;
434   }
435
436   return TRUE;
437
438 out:
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);
443
444   return FALSE;
445 }
446 /* *INDENT-ON* */
447
448 static gboolean
449 gst_d3d11_window_dummy_release_shared_handle (GstD3D11Window * window,
450     GstD3D11WindowSharedHandleData * data)
451 {
452   GstD3D11WindowDummy *self = GST_D3D11_WINDOW_DUMMY (window);
453   GstD3D11Device *device = window->device;
454   HRESULT hr;
455
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);
460
461     data->keyed_mutex->Release ();
462   } else {
463     /* *INDENT-OFF* */
464     ComPtr<ID3D11Query> query;
465     /* *INDENT-ON* */
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;
471
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;
476
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");
480       return FALSE;
481     }
482
483     /* Copy from fallback texture to user's texture */
484     if (data->fallback_rtv) {
485       D3D11_BOX src_box;
486       D3D11_TEXTURE2D_DESC desc;
487       ID3D11DeviceContext *context_handle =
488           gst_d3d11_device_get_device_context_handle (device);
489
490       data->texture->GetDesc (&desc);
491
492       src_box.left = 0;
493       src_box.top = 0;
494       src_box.front = 0;
495       src_box.back = 1;
496       src_box.right = desc.Width;
497       src_box.bottom = desc.Height;
498
499       context_handle->CopySubresourceRegion (data->texture, 0, 0, 0, 0,
500           self->fallback_texture, 0, &src_box);
501     }
502     context_handle->End (query.Get ());
503
504     /* Wait until all issued GPU commands are finished */
505     do {
506       context_handle->GetData (query.Get (), &sync_done, sizeof (BOOL), 0);
507     } while (!sync_done && (hr == S_OK || hr == S_FALSE));
508
509     if (!gst_d3d11_result (hr, device)) {
510       GST_ERROR_OBJECT (self, "Couldn't sync GPU operation");
511       return FALSE;
512     }
513   }
514
515   GST_D3D11_CLEAR_COM (data->rtv);
516   GST_D3D11_CLEAR_COM (data->pov);
517   GST_D3D11_CLEAR_COM (data->texture);
518
519   return TRUE;
520 }
521
522 GstD3D11Window *
523 gst_d3d11_window_dummy_new (GstD3D11Device * device)
524 {
525   GstD3D11Window *window;
526
527   g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), NULL);
528
529   window = (GstD3D11Window *)
530       g_object_new (GST_TYPE_D3D11_WINDOW_DUMMY, "d3d11device", device, NULL);
531
532   window->initialized = TRUE;
533   g_object_ref_sink (window);
534
535   return window;
536 }