• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  * Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
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 <string.h>
26 #include "gstd3d11memory.h"
27 #include "gstd3d11device.h"
28 #include "gstd3d11utils.h"
29 #include "gstd3d11_private.h"
30 
31 GST_DEBUG_CATEGORY_STATIC (gst_d3d11_allocator_debug);
32 #define GST_CAT_DEFAULT gst_d3d11_allocator_debug
33 
34 static GstAllocator *_d3d11_memory_allocator;
35 
36 /* GstD3D11AllocationParams */
37 static void gst_d3d11_allocation_params_init (GType type);
38 G_DEFINE_BOXED_TYPE_WITH_CODE (GstD3D11AllocationParams,
39     gst_d3d11_allocation_params,
40     (GBoxedCopyFunc) gst_d3d11_allocation_params_copy,
41     (GBoxedFreeFunc) gst_d3d11_allocation_params_free,
42     gst_d3d11_allocation_params_init (g_define_type_id));
43 
44 /**
45  * gst_d3d11_allocation_params_new:
46  * @device: a #GstD3D11Device
47  * @info: a #GstVideoInfo
48  * @flags: a #GstD3D11AllocationFlags
49  * @bind_flags: D3D11_BIND_FLAG value used for creating Direct3D11 texture
50  *
51  * Create #GstD3D11AllocationParams object which is used by #GstD3D11BufferPool
52  * and #GstD3D11Allocator in order to allocate new ID3D11Texture2D
53  * object with given configuration
54  *
55  * Returns: a #GstD3D11AllocationParams or %NULL if @info is not supported
56  *
57  * Since: 1.20
58  */
59 GstD3D11AllocationParams *
gst_d3d11_allocation_params_new(GstD3D11Device * device,GstVideoInfo * info,GstD3D11AllocationFlags flags,guint bind_flags)60 gst_d3d11_allocation_params_new (GstD3D11Device * device, GstVideoInfo * info,
61     GstD3D11AllocationFlags flags, guint bind_flags)
62 {
63   GstD3D11AllocationParams *ret;
64   const GstD3D11Format *d3d11_format;
65   guint i;
66 
67   g_return_val_if_fail (info != NULL, NULL);
68 
69   d3d11_format = gst_d3d11_device_format_from_gst (device,
70       GST_VIDEO_INFO_FORMAT (info));
71   if (!d3d11_format) {
72     GST_WARNING ("Couldn't get d3d11 format");
73     return NULL;
74   }
75 
76   ret = g_new0 (GstD3D11AllocationParams, 1);
77 
78   ret->info = *info;
79   ret->aligned_info = *info;
80   ret->d3d11_format = d3d11_format;
81 
82   /* Usage Flag
83    * https://docs.microsoft.com/en-us/windows/win32/api/d3d11/ne-d3d11-d3d11_usage
84    *
85    * +----------------------------------------------------------+
86    * | Resource Usage | Default | Dynamic | Immutable | Staging |
87    * +----------------+---------+---------+-----------+---------+
88    * | GPU-Read       | Yes     | Yes     | Yes       | Yes     |
89    * | GPU-Write      | Yes     |         |           | Yes     |
90    * | CPU-Read       |         |         |           | Yes     |
91    * | CPU-Write      |         | Yes     |           | Yes     |
92    * +----------------------------------------------------------+
93    */
94 
95   /* If corresponding dxgi format is undefined, use resource format instead */
96   if (d3d11_format->dxgi_format == DXGI_FORMAT_UNKNOWN) {
97     for (i = 0; i < GST_VIDEO_INFO_N_PLANES (info); i++) {
98       g_assert (d3d11_format->resource_format[i] != DXGI_FORMAT_UNKNOWN);
99 
100       ret->desc[i].Width = GST_VIDEO_INFO_COMP_WIDTH (info, i);
101       ret->desc[i].Height = GST_VIDEO_INFO_COMP_HEIGHT (info, i);
102       ret->desc[i].MipLevels = 1;
103       ret->desc[i].ArraySize = 1;
104       ret->desc[i].Format = d3d11_format->resource_format[i];
105       ret->desc[i].SampleDesc.Count = 1;
106       ret->desc[i].SampleDesc.Quality = 0;
107       ret->desc[i].Usage = D3D11_USAGE_DEFAULT;
108       ret->desc[i].BindFlags = bind_flags;
109     }
110   } else {
111     ret->desc[0].Width = GST_VIDEO_INFO_WIDTH (info);
112     ret->desc[0].Height = GST_VIDEO_INFO_HEIGHT (info);
113     ret->desc[0].MipLevels = 1;
114     ret->desc[0].ArraySize = 1;
115     ret->desc[0].Format = d3d11_format->dxgi_format;
116     ret->desc[0].SampleDesc.Count = 1;
117     ret->desc[0].SampleDesc.Quality = 0;
118     ret->desc[0].Usage = D3D11_USAGE_DEFAULT;
119     ret->desc[0].BindFlags = bind_flags;
120   }
121 
122   ret->flags = flags;
123 
124   return ret;
125 }
126 
127 /**
128  * gst_d3d11_allocation_params_alignment:
129  * @params: a #GstD3D11AllocationParams
130  * @align: a #GstVideoAlignment
131  *
132  * Adjust Width and Height fields of D3D11_TEXTURE2D_DESC with given
133  * @align
134  *
135  * Returns: %TRUE if alignment could be applied
136  *
137  * Since: 1.20
138  */
139 gboolean
gst_d3d11_allocation_params_alignment(GstD3D11AllocationParams * params,GstVideoAlignment * align)140 gst_d3d11_allocation_params_alignment (GstD3D11AllocationParams * params,
141     GstVideoAlignment * align)
142 {
143   guint i;
144   guint padding_width, padding_height;
145   GstVideoInfo *info;
146   GstVideoInfo new_info;
147 
148   g_return_val_if_fail (params != NULL, FALSE);
149   g_return_val_if_fail (align != NULL, FALSE);
150 
151   /* d3d11 does not support stride align. Consider padding only */
152   padding_width = align->padding_left + align->padding_right;
153   padding_height = align->padding_top + align->padding_bottom;
154 
155   info = &params->info;
156 
157   if (!gst_video_info_set_format (&new_info, GST_VIDEO_INFO_FORMAT (info),
158           GST_VIDEO_INFO_WIDTH (info) + padding_width,
159           GST_VIDEO_INFO_HEIGHT (info) + padding_height)) {
160     GST_WARNING ("Set format fail");
161     return FALSE;
162   }
163 
164   params->aligned_info = new_info;
165 
166   for (i = 0; i < GST_VIDEO_INFO_N_PLANES (info); i++) {
167     params->desc[i].Width = GST_VIDEO_INFO_COMP_WIDTH (&new_info, i);
168     params->desc[i].Height = GST_VIDEO_INFO_COMP_HEIGHT (&new_info, i);
169   }
170 
171   return TRUE;
172 }
173 
174 /**
175  * gst_d3d11_allocation_params_copy:
176  * @src: a #GstD3D11AllocationParams
177  *
178  * Returns: a copy of @src
179  *
180  * Since: 1.20
181  */
182 GstD3D11AllocationParams *
gst_d3d11_allocation_params_copy(GstD3D11AllocationParams * src)183 gst_d3d11_allocation_params_copy (GstD3D11AllocationParams * src)
184 {
185   GstD3D11AllocationParams *dst;
186 
187   g_return_val_if_fail (src != NULL, NULL);
188 
189   dst = g_new0 (GstD3D11AllocationParams, 1);
190   memcpy (dst, src, sizeof (GstD3D11AllocationParams));
191 
192   return dst;
193 }
194 
195 /**
196  * gst_d3d11_allocation_params_free:
197  * @params: a #GstD3D11AllocationParams
198  *
199  * Free @params
200  *
201  * Since: 1.20
202  */
203 void
gst_d3d11_allocation_params_free(GstD3D11AllocationParams * params)204 gst_d3d11_allocation_params_free (GstD3D11AllocationParams * params)
205 {
206   g_free (params);
207 }
208 
209 static gint
gst_d3d11_allocation_params_compare(const GstD3D11AllocationParams * p1,const GstD3D11AllocationParams * p2)210 gst_d3d11_allocation_params_compare (const GstD3D11AllocationParams * p1,
211     const GstD3D11AllocationParams * p2)
212 {
213   g_return_val_if_fail (p1 != NULL, -1);
214   g_return_val_if_fail (p2 != NULL, -1);
215 
216   if (p1 == p2)
217     return 0;
218 
219   return -1;
220 }
221 
222 static void
gst_d3d11_allocation_params_init(GType type)223 gst_d3d11_allocation_params_init (GType type)
224 {
225   static GstValueTable table = {
226     0, (GstValueCompareFunc) gst_d3d11_allocation_params_compare,
227     NULL, NULL
228   };
229 
230   table.type = type;
231   gst_value_register (&table);
232 }
233 
234 /* GstD3D11Memory */
235 #define GST_D3D11_MEMORY_GET_LOCK(m) (&(GST_D3D11_MEMORY_CAST(m)->priv->lock))
236 #define GST_D3D11_MEMORY_LOCK(m) G_STMT_START { \
237   GST_TRACE("Locking %p from thread %p", (m), g_thread_self()); \
238   g_mutex_lock(GST_D3D11_MEMORY_GET_LOCK(m)); \
239   GST_TRACE("Locked %p from thread %p", (m), g_thread_self()); \
240 } G_STMT_END
241 
242 #define GST_D3D11_MEMORY_UNLOCK(m) G_STMT_START { \
243   GST_TRACE("Unlocking %p from thread %p", (m), g_thread_self()); \
244   g_mutex_unlock(GST_D3D11_MEMORY_GET_LOCK(m)); \
245 } G_STMT_END
246 
247 struct _GstD3D11MemoryPrivate
248 {
249   ID3D11Texture2D *texture;
250   ID3D11Texture2D *staging;
251 
252   D3D11_TEXTURE2D_DESC desc;
253 
254   guint subresource_index;
255 
256   ID3D11ShaderResourceView *shader_resource_view[GST_VIDEO_MAX_PLANES];
257   guint num_shader_resource_views;
258 
259   ID3D11RenderTargetView *render_target_view[GST_VIDEO_MAX_PLANES];
260   guint num_render_target_views;
261 
262   ID3D11VideoDecoderOutputView *decoder_output_view;
263   ID3D11VideoDecoder *decoder_handle;
264 
265   ID3D11VideoProcessorInputView *processor_input_view;
266   ID3D11VideoProcessorOutputView *processor_output_view;
267 
268   D3D11_MAPPED_SUBRESOURCE map;
269 
270 
271   GMutex lock;
272   gint cpu_map_count;
273 };
274 
275 GST_DEFINE_MINI_OBJECT_TYPE (GstD3D11Memory, gst_d3d11_memory);
276 
277 static inline D3D11_MAP
gst_d3d11_map_flags_to_d3d11(GstMapFlags flags)278 gst_d3d11_map_flags_to_d3d11 (GstMapFlags flags)
279 {
280   if ((flags & GST_MAP_READWRITE) == GST_MAP_READWRITE)
281     return D3D11_MAP_READ_WRITE;
282   else if ((flags & GST_MAP_WRITE) == GST_MAP_WRITE)
283     return D3D11_MAP_WRITE;
284   else if ((flags & GST_MAP_READ) == GST_MAP_READ)
285     return D3D11_MAP_READ;
286   else
287     g_assert_not_reached ();
288 
289   return D3D11_MAP_READ;
290 }
291 
292 static ID3D11Texture2D *
gst_d3d11_allocate_staging_texture(GstD3D11Device * device,const D3D11_TEXTURE2D_DESC * ref)293 gst_d3d11_allocate_staging_texture (GstD3D11Device * device,
294     const D3D11_TEXTURE2D_DESC * ref)
295 {
296   D3D11_TEXTURE2D_DESC desc = { 0, };
297   ID3D11Texture2D *texture = NULL;
298   ID3D11Device *device_handle = gst_d3d11_device_get_device_handle (device);
299   HRESULT hr;
300 
301   desc.Width = ref->Width;
302   desc.Height = ref->Height;
303   desc.MipLevels = 1;
304   desc.Format = ref->Format;
305   desc.SampleDesc.Count = 1;
306   desc.ArraySize = 1;
307   desc.Usage = D3D11_USAGE_STAGING;
308   desc.CPUAccessFlags = (D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE);
309 
310   hr = device_handle->CreateTexture2D (&desc, NULL, &texture);
311   if (!gst_d3d11_result (hr, device)) {
312     GST_ERROR_OBJECT (device, "Failed to create texture");
313     return NULL;
314   }
315 
316   return texture;
317 }
318 
319 /* Must be called with d3d11 device lock */
320 static gboolean
gst_d3d11_memory_map_cpu_access(GstD3D11Memory * dmem,D3D11_MAP map_type)321 gst_d3d11_memory_map_cpu_access (GstD3D11Memory * dmem, D3D11_MAP map_type)
322 {
323   GstD3D11MemoryPrivate *priv = dmem->priv;
324   HRESULT hr;
325   ID3D11Resource *staging = (ID3D11Resource *) priv->staging;
326   ID3D11DeviceContext *device_context =
327       gst_d3d11_device_get_device_context_handle (dmem->device);
328 
329   hr = device_context->Map (staging, 0, map_type, 0, &priv->map);
330 
331   if (!gst_d3d11_result (hr, dmem->device)) {
332     GST_ERROR_OBJECT (GST_MEMORY_CAST (dmem)->allocator,
333         "Failed to map staging texture (0x%x)", (guint) hr);
334     return FALSE;
335   }
336 
337   return TRUE;
338 }
339 
340 /* Must be called with d3d11 device lock */
341 static void
gst_d3d11_memory_upload(GstD3D11Memory * dmem)342 gst_d3d11_memory_upload (GstD3D11Memory * dmem)
343 {
344   GstD3D11MemoryPrivate *priv = dmem->priv;
345   ID3D11DeviceContext *device_context;
346 
347   if (!priv->staging || priv->staging == priv->texture ||
348       !GST_MEMORY_FLAG_IS_SET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD))
349     return;
350 
351   device_context = gst_d3d11_device_get_device_context_handle (dmem->device);
352   device_context->CopySubresourceRegion (priv->texture, priv->subresource_index,
353       0, 0, 0, priv->staging, 0, NULL);
354 }
355 
356 /* Must be called with d3d11 device lock */
357 static void
gst_d3d11_memory_download(GstD3D11Memory * dmem)358 gst_d3d11_memory_download (GstD3D11Memory * dmem)
359 {
360   GstD3D11MemoryPrivate *priv = dmem->priv;
361   ID3D11DeviceContext *device_context;
362 
363   if (!priv->staging || priv->staging == priv->texture ||
364       !GST_MEMORY_FLAG_IS_SET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD))
365     return;
366 
367   device_context = gst_d3d11_device_get_device_context_handle (dmem->device);
368   device_context->CopySubresourceRegion (priv->staging, 0, 0, 0, 0,
369       priv->texture, priv->subresource_index, NULL);
370 }
371 
372 static gpointer
gst_d3d11_memory_map_full(GstMemory * mem,GstMapInfo * info,gsize maxsize)373 gst_d3d11_memory_map_full (GstMemory * mem, GstMapInfo * info, gsize maxsize)
374 {
375   GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
376   GstD3D11MemoryPrivate *priv = dmem->priv;
377   GstMapFlags flags = info->flags;
378   gpointer ret = NULL;
379 
380   gst_d3d11_device_lock (dmem->device);
381   GST_D3D11_MEMORY_LOCK (dmem);
382 
383   if ((flags & GST_MAP_D3D11) == GST_MAP_D3D11) {
384     gst_d3d11_memory_upload (dmem);
385     GST_MEMORY_FLAG_UNSET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD);
386 
387     if ((flags & GST_MAP_WRITE) == GST_MAP_WRITE)
388       GST_MINI_OBJECT_FLAG_SET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
389 
390     g_assert (priv->texture != NULL);
391     ret = priv->texture;
392     goto out;
393   }
394 
395   if (priv->cpu_map_count == 0) {
396     D3D11_MAP map_type;
397 
398     /* Allocate staging texture for CPU access */
399     if (!priv->staging) {
400       priv->staging = gst_d3d11_allocate_staging_texture (dmem->device,
401           &priv->desc);
402       if (!priv->staging) {
403         GST_ERROR_OBJECT (mem->allocator, "Couldn't create staging texture");
404         goto out;
405       }
406 
407       /* first memory, always need download to staging */
408       GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
409     }
410 
411     gst_d3d11_memory_download (dmem);
412     map_type = gst_d3d11_map_flags_to_d3d11 (flags);
413 
414     if (!gst_d3d11_memory_map_cpu_access (dmem, map_type)) {
415       GST_ERROR_OBJECT (mem->allocator, "Couldn't map staging texture");
416       goto out;
417     }
418   }
419 
420   if ((flags & GST_MAP_WRITE) == GST_MAP_WRITE) {
421     GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD);
422   }
423 
424   GST_MEMORY_FLAG_UNSET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
425 
426   priv->cpu_map_count++;
427   ret = dmem->priv->map.pData;
428 
429 out:
430   GST_D3D11_MEMORY_UNLOCK (dmem);
431   gst_d3d11_device_unlock (dmem->device);
432 
433   return ret;
434 }
435 
436 /* Must be called with d3d11 device lock */
437 static void
gst_d3d11_memory_unmap_cpu_access(GstD3D11Memory * dmem)438 gst_d3d11_memory_unmap_cpu_access (GstD3D11Memory * dmem)
439 {
440   GstD3D11MemoryPrivate *priv = dmem->priv;
441   ID3D11Resource *staging = (ID3D11Resource *) priv->staging;
442   ID3D11DeviceContext *device_context =
443       gst_d3d11_device_get_device_context_handle (dmem->device);
444 
445   device_context->Unmap (staging, 0);
446 }
447 
448 static void
gst_d3d11_memory_unmap_full(GstMemory * mem,GstMapInfo * info)449 gst_d3d11_memory_unmap_full (GstMemory * mem, GstMapInfo * info)
450 {
451   GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
452   GstD3D11MemoryPrivate *priv = dmem->priv;
453 
454   gst_d3d11_device_lock (dmem->device);
455   GST_D3D11_MEMORY_LOCK (dmem);
456 
457   if ((info->flags & GST_MAP_D3D11) == GST_MAP_D3D11) {
458     if ((info->flags & GST_MAP_WRITE) == GST_MAP_WRITE)
459       GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
460 
461     goto out;
462   }
463 
464   if ((info->flags & GST_MAP_WRITE) == GST_MAP_WRITE)
465     GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD);
466 
467   priv->cpu_map_count--;
468   if (priv->cpu_map_count > 0)
469     goto out;
470 
471   gst_d3d11_memory_unmap_cpu_access (dmem);
472 
473 out:
474   GST_D3D11_MEMORY_UNLOCK (dmem);
475   gst_d3d11_device_unlock (dmem->device);
476 }
477 
478 static GstMemory *
gst_d3d11_memory_share(GstMemory * mem,gssize offset,gssize size)479 gst_d3d11_memory_share (GstMemory * mem, gssize offset, gssize size)
480 {
481   /* TODO: impl. */
482   return NULL;
483 }
484 
485 static gboolean
gst_d3d11_memory_update_size(GstMemory * mem)486 gst_d3d11_memory_update_size (GstMemory * mem)
487 {
488   GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
489   GstD3D11MemoryPrivate *priv = dmem->priv;
490   gsize offset[GST_VIDEO_MAX_PLANES];
491   gint stride[GST_VIDEO_MAX_PLANES];
492   gsize size;
493   D3D11_TEXTURE2D_DESC *desc = &priv->desc;
494   gboolean ret = FALSE;
495 
496   if (!priv->staging) {
497     priv->staging = gst_d3d11_allocate_staging_texture (dmem->device,
498         &priv->desc);
499     if (!priv->staging) {
500       GST_ERROR_OBJECT (mem->allocator, "Couldn't create staging texture");
501       return FALSE;
502     }
503 
504     GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
505   }
506 
507   gst_d3d11_device_lock (dmem->device);
508   if (!gst_d3d11_memory_map_cpu_access (dmem, D3D11_MAP_READ_WRITE)) {
509     GST_ERROR_OBJECT (mem->allocator, "Couldn't map staging texture");
510     return FALSE;
511   }
512 
513   gst_d3d11_memory_unmap_cpu_access (dmem);
514 
515   if (!gst_d3d11_dxgi_format_get_size (desc->Format, desc->Width, desc->Height,
516           priv->map.RowPitch, offset, stride, &size)) {
517     GST_ERROR_OBJECT (mem->allocator, "Couldn't calculate memory size");
518     goto out;
519   }
520 
521   mem->maxsize = mem->size = size;
522   ret = TRUE;
523 
524 out:
525   gst_d3d11_device_unlock (dmem->device);
526   return ret;
527 }
528 
529 /**
530  * gst_is_d3d11_memory:
531  * @mem: a #GstMemory
532  *
533  * Returns: whether @mem is a #GstD3D11Memory
534  *
535  * Since: 1.20
536  */
537 gboolean
gst_is_d3d11_memory(GstMemory * mem)538 gst_is_d3d11_memory (GstMemory * mem)
539 {
540   return mem != NULL && mem->allocator != NULL &&
541       (GST_IS_D3D11_ALLOCATOR (mem->allocator) ||
542       GST_IS_D3D11_POOL_ALLOCATOR (mem->allocator));
543 }
544 
545 /**
546  * gst_d3d11_memory_init_once:
547  *
548  * Initializes the Direct3D11 Texture allocator. It is safe to call
549  * this function multiple times. This must be called before any other
550  * GstD3D11Memory operation.
551  *
552  * Since: 1.20
553  */
554 void
gst_d3d11_memory_init_once(void)555 gst_d3d11_memory_init_once (void)
556 {
557   static gsize _init = 0;
558 
559   if (g_once_init_enter (&_init)) {
560 
561     GST_DEBUG_CATEGORY_INIT (gst_d3d11_allocator_debug, "d3d11allocator", 0,
562         "Direct3D11 Texture Allocator");
563 
564     _d3d11_memory_allocator =
565         (GstAllocator *) g_object_new (GST_TYPE_D3D11_ALLOCATOR, NULL);
566     gst_object_ref_sink (_d3d11_memory_allocator);
567 
568     gst_allocator_register (GST_D3D11_MEMORY_NAME, _d3d11_memory_allocator);
569     g_once_init_leave (&_init, 1);
570   }
571 }
572 
573 /**
574  * gst_d3d11_memory_get_texture_handle:
575  * @mem: a #GstD3D11Memory
576  *
577  * Returns: (transfer none): a ID3D11Texture2D handle. Caller must not release
578  * returned handle.
579  *
580  * Since: 1.20
581  */
582 ID3D11Texture2D *
gst_d3d11_memory_get_texture_handle(GstD3D11Memory * mem)583 gst_d3d11_memory_get_texture_handle (GstD3D11Memory * mem)
584 {
585   g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
586 
587   return mem->priv->texture;
588 }
589 
590 /**
591  * gst_d3d11_memory_get_subresource_index:
592  * @mem: a #GstD3D11Memory
593  *
594  * Returns: subresource index corresponding to @mem.
595  *
596  * Since: 1.20
597  */
598 guint
gst_d3d11_memory_get_subresource_index(GstD3D11Memory * mem)599 gst_d3d11_memory_get_subresource_index (GstD3D11Memory * mem)
600 {
601   g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), 0);
602 
603   return mem->priv->subresource_index;
604 }
605 
606 /**
607  * gst_d3d11_memory_get_texture_desc:
608  * @mem: a #GstD3D11Memory
609  * @desc: (out): a D3D11_TEXTURE2D_DESC
610  *
611  * Fill @desc with D3D11_TEXTURE2D_DESC for ID3D11Texture2D
612  *
613  * Returns: %TRUE if successeed
614  *
615  * Since: 1.20
616  */
617 gboolean
gst_d3d11_memory_get_texture_desc(GstD3D11Memory * mem,D3D11_TEXTURE2D_DESC * desc)618 gst_d3d11_memory_get_texture_desc (GstD3D11Memory * mem,
619     D3D11_TEXTURE2D_DESC * desc)
620 {
621   g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), FALSE);
622   g_return_val_if_fail (desc != NULL, FALSE);
623 
624   *desc = mem->priv->desc;
625 
626   return TRUE;
627 }
628 
629 gboolean
gst_d3d11_memory_get_texture_stride(GstD3D11Memory * mem,guint * stride)630 gst_d3d11_memory_get_texture_stride (GstD3D11Memory * mem, guint * stride)
631 {
632   g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), FALSE);
633   g_return_val_if_fail (stride != NULL, FALSE);
634 
635   *stride = mem->priv->map.RowPitch;
636 
637   return TRUE;
638 }
639 
640 static gboolean
create_shader_resource_views(GstD3D11Memory * mem)641 create_shader_resource_views (GstD3D11Memory * mem)
642 {
643   GstD3D11MemoryPrivate *priv = mem->priv;
644   guint i;
645   HRESULT hr;
646   guint num_views = 0;
647   ID3D11Device *device_handle;
648   D3D11_SHADER_RESOURCE_VIEW_DESC resource_desc;
649   DXGI_FORMAT formats[GST_VIDEO_MAX_PLANES] = { DXGI_FORMAT_UNKNOWN, };
650 
651   memset (&resource_desc, 0, sizeof (D3D11_SHADER_RESOURCE_VIEW_DESC));
652 
653   device_handle = gst_d3d11_device_get_device_handle (mem->device);
654 
655   switch (priv->desc.Format) {
656     case DXGI_FORMAT_B8G8R8A8_UNORM:
657     case DXGI_FORMAT_R8G8B8A8_UNORM:
658     case DXGI_FORMAT_R10G10B10A2_UNORM:
659     case DXGI_FORMAT_R8_UNORM:
660     case DXGI_FORMAT_R8G8_UNORM:
661     case DXGI_FORMAT_R16_UNORM:
662     case DXGI_FORMAT_R16G16_UNORM:
663     case DXGI_FORMAT_G8R8_G8B8_UNORM:
664     case DXGI_FORMAT_R8G8_B8G8_UNORM:
665     case DXGI_FORMAT_R16G16B16A16_UNORM:
666       num_views = 1;
667       formats[0] = priv->desc.Format;
668       break;
669     case DXGI_FORMAT_AYUV:
670     case DXGI_FORMAT_YUY2:
671       num_views = 1;
672       formats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
673       break;
674     case DXGI_FORMAT_NV12:
675       num_views = 2;
676       formats[0] = DXGI_FORMAT_R8_UNORM;
677       formats[1] = DXGI_FORMAT_R8G8_UNORM;
678       break;
679     case DXGI_FORMAT_P010:
680     case DXGI_FORMAT_P016:
681       num_views = 2;
682       formats[0] = DXGI_FORMAT_R16_UNORM;
683       formats[1] = DXGI_FORMAT_R16G16_UNORM;
684       break;
685     case DXGI_FORMAT_Y210:
686       num_views = 1;
687       formats[0] = DXGI_FORMAT_R16G16B16A16_UNORM;
688       break;
689     case DXGI_FORMAT_Y410:
690       num_views = 1;
691       formats[0] = DXGI_FORMAT_R10G10B10A2_UNORM;
692       break;
693     default:
694       g_assert_not_reached ();
695       return FALSE;
696   }
697 
698   if ((priv->desc.BindFlags & D3D11_BIND_SHADER_RESOURCE) ==
699       D3D11_BIND_SHADER_RESOURCE) {
700     resource_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
701     resource_desc.Texture2D.MipLevels = 1;
702 
703     for (i = 0; i < num_views; i++) {
704       resource_desc.Format = formats[i];
705       hr = device_handle->CreateShaderResourceView (priv->texture,
706           &resource_desc, &priv->shader_resource_view[i]);
707 
708       if (!gst_d3d11_result (hr, mem->device)) {
709         GST_ERROR_OBJECT (GST_MEMORY_CAST (mem)->allocator,
710             "Failed to create %dth resource view (0x%x)", i, (guint) hr);
711         goto error;
712       }
713     }
714 
715     priv->num_shader_resource_views = num_views;
716 
717     return TRUE;
718   }
719 
720   return FALSE;
721 
722 error:
723   for (i = 0; i < num_views; i++)
724     GST_D3D11_CLEAR_COM (priv->shader_resource_view[i]);
725 
726   priv->num_shader_resource_views = 0;
727 
728   return FALSE;
729 }
730 
731 static gboolean
gst_d3d11_memory_ensure_shader_resource_view(GstD3D11Memory * mem)732 gst_d3d11_memory_ensure_shader_resource_view (GstD3D11Memory * mem)
733 {
734   GstD3D11MemoryPrivate *priv = mem->priv;
735   gboolean ret = FALSE;
736 
737   if (!(priv->desc.BindFlags & D3D11_BIND_SHADER_RESOURCE)) {
738     GST_LOG_OBJECT (GST_MEMORY_CAST (mem)->allocator,
739         "Need BindFlags, current flag 0x%x", priv->desc.BindFlags);
740     return FALSE;
741   }
742 
743   GST_D3D11_MEMORY_LOCK (mem);
744   if (priv->num_shader_resource_views) {
745     ret = TRUE;
746     goto done;
747   }
748 
749   ret = create_shader_resource_views (mem);
750 
751 done:
752   GST_D3D11_MEMORY_UNLOCK (mem);
753 
754   return ret;
755 }
756 
757 /**
758  * gst_d3d11_memory_get_shader_resource_view_size:
759  * @mem: a #GstD3D11Memory
760  *
761  * Returns: the number of ID3D11ShaderResourceView that can be used
762  * for processing GPU operation with @mem
763  *
764  * Since: 1.20
765  */
766 guint
gst_d3d11_memory_get_shader_resource_view_size(GstD3D11Memory * mem)767 gst_d3d11_memory_get_shader_resource_view_size (GstD3D11Memory * mem)
768 {
769   g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), 0);
770 
771   if (!gst_d3d11_memory_ensure_shader_resource_view (mem))
772     return 0;
773 
774   return mem->priv->num_shader_resource_views;
775 }
776 
777 /**
778  * gst_d3d11_memory_get_shader_resource_view:
779  * @mem: a #GstD3D11Memory
780  * @index: the index of the ID3D11ShaderResourceView
781  *
782  * Returns: (transfer none) (nullable): a pointer to the
783  * ID3D11ShaderResourceView or %NULL if ID3D11ShaderResourceView is unavailable
784  * for @index
785  *
786  * Since: 1.20
787  */
788 ID3D11ShaderResourceView *
gst_d3d11_memory_get_shader_resource_view(GstD3D11Memory * mem,guint index)789 gst_d3d11_memory_get_shader_resource_view (GstD3D11Memory * mem, guint index)
790 {
791   GstD3D11MemoryPrivate *priv;
792 
793   g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
794 
795   if (!gst_d3d11_memory_ensure_shader_resource_view (mem))
796     return NULL;
797 
798   priv = mem->priv;
799 
800   if (index >= priv->num_shader_resource_views) {
801     GST_ERROR ("Invalid SRV index %d", index);
802     return NULL;
803   }
804 
805   return priv->shader_resource_view[index];
806 }
807 
808 static gboolean
create_render_target_views(GstD3D11Memory * mem)809 create_render_target_views (GstD3D11Memory * mem)
810 {
811   GstD3D11MemoryPrivate *priv = mem->priv;
812   guint i;
813   HRESULT hr;
814   guint num_views = 0;
815   ID3D11Device *device_handle;
816   D3D11_RENDER_TARGET_VIEW_DESC render_desc;
817   DXGI_FORMAT formats[GST_VIDEO_MAX_PLANES] = { DXGI_FORMAT_UNKNOWN, };
818 
819   memset (&render_desc, 0, sizeof (D3D11_RENDER_TARGET_VIEW_DESC));
820 
821   device_handle = gst_d3d11_device_get_device_handle (mem->device);
822 
823   switch (priv->desc.Format) {
824     case DXGI_FORMAT_B8G8R8A8_UNORM:
825     case DXGI_FORMAT_R8G8B8A8_UNORM:
826     case DXGI_FORMAT_R10G10B10A2_UNORM:
827     case DXGI_FORMAT_R8_UNORM:
828     case DXGI_FORMAT_R8G8_UNORM:
829     case DXGI_FORMAT_R16_UNORM:
830     case DXGI_FORMAT_R16G16_UNORM:
831       num_views = 1;
832       formats[0] = priv->desc.Format;
833       break;
834     case DXGI_FORMAT_AYUV:
835       num_views = 1;
836       formats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
837       break;
838     case DXGI_FORMAT_NV12:
839       num_views = 2;
840       formats[0] = DXGI_FORMAT_R8_UNORM;
841       formats[1] = DXGI_FORMAT_R8G8_UNORM;
842       break;
843     case DXGI_FORMAT_P010:
844     case DXGI_FORMAT_P016:
845       num_views = 2;
846       formats[0] = DXGI_FORMAT_R16_UNORM;
847       formats[1] = DXGI_FORMAT_R16G16_UNORM;
848       break;
849     default:
850       g_assert_not_reached ();
851       return FALSE;
852   }
853 
854   if ((priv->desc.BindFlags & D3D11_BIND_RENDER_TARGET) ==
855       D3D11_BIND_RENDER_TARGET) {
856     render_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
857     render_desc.Texture2D.MipSlice = 0;
858 
859     for (i = 0; i < num_views; i++) {
860       render_desc.Format = formats[i];
861 
862       hr = device_handle->CreateRenderTargetView (priv->texture, &render_desc,
863           &priv->render_target_view[i]);
864       if (!gst_d3d11_result (hr, mem->device)) {
865         GST_ERROR_OBJECT (GST_MEMORY_CAST (mem)->allocator,
866             "Failed to create %dth render target view (0x%x)", i, (guint) hr);
867         goto error;
868       }
869     }
870 
871     priv->num_render_target_views = num_views;
872 
873     return TRUE;
874   }
875 
876   return FALSE;
877 
878 error:
879   for (i = 0; i < num_views; i++)
880     GST_D3D11_CLEAR_COM (priv->render_target_view[i]);
881 
882   priv->num_render_target_views = 0;
883 
884   return FALSE;
885 }
886 
887 static gboolean
gst_d3d11_memory_ensure_render_target_view(GstD3D11Memory * mem)888 gst_d3d11_memory_ensure_render_target_view (GstD3D11Memory * mem)
889 {
890   GstD3D11MemoryPrivate *priv = mem->priv;
891   gboolean ret = FALSE;
892 
893   if (!(priv->desc.BindFlags & D3D11_BIND_RENDER_TARGET)) {
894     GST_WARNING_OBJECT (GST_MEMORY_CAST (mem)->allocator,
895         "Need BindFlags, current flag 0x%x", priv->desc.BindFlags);
896     return FALSE;
897   }
898 
899   GST_D3D11_MEMORY_LOCK (mem);
900   if (priv->num_render_target_views) {
901     ret = TRUE;
902     goto done;
903   }
904 
905   ret = create_render_target_views (mem);
906 
907 done:
908   GST_D3D11_MEMORY_UNLOCK (mem);
909 
910   return ret;
911 }
912 
913 /**
914  * gst_d3d11_memory_get_render_target_view_size:
915  * @mem: a #GstD3D11Memory
916  *
917  * Returns: the number of ID3D11RenderTargetView that can be used
918  * for processing GPU operation with @mem
919  *
920  * Since: 1.20
921  */
922 guint
gst_d3d11_memory_get_render_target_view_size(GstD3D11Memory * mem)923 gst_d3d11_memory_get_render_target_view_size (GstD3D11Memory * mem)
924 {
925   g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), 0);
926 
927   if (!gst_d3d11_memory_ensure_render_target_view (mem))
928     return 0;
929 
930   return mem->priv->num_render_target_views;
931 }
932 
933 /**
934  * gst_d3d11_memory_get_render_target_view:
935  * @mem: a #GstD3D11Memory
936  * @index: the index of the ID3D11RenderTargetView
937  *
938  * Returns: (transfer none) (nullable): a pointer to the
939  * ID3D11RenderTargetView or %NULL if ID3D11RenderTargetView is unavailable
940  * for @index
941  *
942  * Since: 1.20
943  */
944 ID3D11RenderTargetView *
gst_d3d11_memory_get_render_target_view(GstD3D11Memory * mem,guint index)945 gst_d3d11_memory_get_render_target_view (GstD3D11Memory * mem, guint index)
946 {
947   GstD3D11MemoryPrivate *priv;
948 
949   g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
950 
951   if (!gst_d3d11_memory_ensure_render_target_view (mem))
952     return NULL;
953 
954   priv = mem->priv;
955 
956   if (index >= priv->num_render_target_views) {
957     GST_ERROR ("Invalid RTV index %d", index);
958     return NULL;
959   }
960 
961   return priv->render_target_view[index];
962 }
963 
964 static gboolean
gst_d3d11_memory_ensure_decoder_output_view(GstD3D11Memory * mem,ID3D11VideoDevice * video_device,ID3D11VideoDecoder * decoder,const GUID * decoder_profile)965 gst_d3d11_memory_ensure_decoder_output_view (GstD3D11Memory * mem,
966     ID3D11VideoDevice * video_device, ID3D11VideoDecoder * decoder,
967     const GUID * decoder_profile)
968 {
969   GstD3D11MemoryPrivate *dmem_priv = mem->priv;
970   GstD3D11Allocator *allocator;
971   D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC desc;
972   HRESULT hr;
973   gboolean ret = FALSE;
974 
975   allocator = GST_D3D11_ALLOCATOR (GST_MEMORY_CAST (mem)->allocator);
976 
977   if (!(dmem_priv->desc.BindFlags & D3D11_BIND_DECODER)) {
978     GST_LOG_OBJECT (allocator,
979         "Need BindFlags, current flag 0x%x", dmem_priv->desc.BindFlags);
980     return FALSE;
981   }
982 
983   GST_D3D11_MEMORY_LOCK (mem);
984   if (dmem_priv->decoder_output_view) {
985     dmem_priv->decoder_output_view->GetDesc (&desc);
986     if (IsEqualGUID (desc.DecodeProfile, *decoder_profile) &&
987         dmem_priv->decoder_handle == decoder) {
988       goto succeeded;
989     } else {
990       /* Shouldn't happen, but try again anyway */
991       GST_WARNING_OBJECT (allocator,
992           "Existing view has different decoder profile");
993       GST_D3D11_CLEAR_COM (dmem_priv->decoder_output_view);
994       GST_D3D11_CLEAR_COM (dmem_priv->decoder_handle);
995     }
996   }
997 
998   if (dmem_priv->decoder_output_view)
999     goto succeeded;
1000 
1001   desc.DecodeProfile = *decoder_profile;
1002   desc.ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D;
1003   desc.Texture2D.ArraySlice = dmem_priv->subresource_index;
1004 
1005   hr = video_device->CreateVideoDecoderOutputView (dmem_priv->texture, &desc,
1006       &dmem_priv->decoder_output_view);
1007   if (!gst_d3d11_result (hr, mem->device)) {
1008     GST_ERROR_OBJECT (allocator,
1009         "Could not create decoder output view, hr: 0x%x", (guint) hr);
1010     goto done;
1011   }
1012 
1013   /* XXX: decoder output view is bound to video device, not decoder handle
1014    * from API point of view. But some driver seems to be unhappy
1015    * when decoder handle is released while there are outstanding view objects */
1016   dmem_priv->decoder_handle = decoder;
1017   decoder->AddRef ();
1018 
1019 succeeded:
1020   ret = TRUE;
1021 
1022 done:
1023   GST_D3D11_MEMORY_UNLOCK (mem);
1024 
1025   return ret;
1026 }
1027 
1028 /**
1029  * gst_d3d11_memory_get_decoder_output_view:
1030  * @mem: a #GstD3D11Memory
1031  * @video_device: (transfer none): a ID3D11VideoDevice handle
1032  * @decoder: (transfer none): a ID3D11VideoDecoder handle
1033  * @decoder_profile: a DXVA decoder profile GUID
1034  *
1035  * Returns: (transfer none) (nullable): a pointer to the
1036  * ID3D11VideoDecoderOutputView or %NULL if ID3D11VideoDecoderOutputView is
1037  * unavailable
1038  *
1039  * Since: 1.20
1040  */
1041 ID3D11VideoDecoderOutputView *
gst_d3d11_memory_get_decoder_output_view(GstD3D11Memory * mem,ID3D11VideoDevice * video_device,ID3D11VideoDecoder * decoder,const GUID * decoder_profile)1042 gst_d3d11_memory_get_decoder_output_view (GstD3D11Memory * mem,
1043     ID3D11VideoDevice * video_device, ID3D11VideoDecoder * decoder,
1044     const GUID * decoder_profile)
1045 {
1046   g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1047   g_return_val_if_fail (video_device != NULL, NULL);
1048   g_return_val_if_fail (decoder != NULL, NULL);
1049   g_return_val_if_fail (decoder_profile != NULL, NULL);
1050 
1051   if (!gst_d3d11_memory_ensure_decoder_output_view (mem,
1052           video_device, decoder, decoder_profile))
1053     return NULL;
1054 
1055   return mem->priv->decoder_output_view;
1056 }
1057 
1058 static gboolean
check_bind_flags_for_processor_input_view(guint bind_flags)1059 check_bind_flags_for_processor_input_view (guint bind_flags)
1060 {
1061   static const guint compatible_flags = (D3D11_BIND_DECODER |
1062       D3D11_BIND_VIDEO_ENCODER | D3D11_BIND_RENDER_TARGET |
1063       D3D11_BIND_UNORDERED_ACCESS);
1064 
1065   if (bind_flags == 0)
1066     return TRUE;
1067 
1068   if ((bind_flags & compatible_flags) != 0)
1069     return TRUE;
1070 
1071   return FALSE;
1072 }
1073 
1074 static gboolean
gst_d3d11_memory_ensure_processor_input_view(GstD3D11Memory * mem,ID3D11VideoDevice * video_device,ID3D11VideoProcessorEnumerator * enumerator)1075 gst_d3d11_memory_ensure_processor_input_view (GstD3D11Memory * mem,
1076     ID3D11VideoDevice * video_device,
1077     ID3D11VideoProcessorEnumerator * enumerator)
1078 {
1079   GstD3D11MemoryPrivate *dmem_priv = mem->priv;
1080   GstD3D11Allocator *allocator;
1081   D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC desc;
1082   HRESULT hr;
1083   gboolean ret = FALSE;
1084 
1085   allocator = GST_D3D11_ALLOCATOR (GST_MEMORY_CAST (mem)->allocator);
1086 
1087   if (!check_bind_flags_for_processor_input_view (dmem_priv->desc.BindFlags)) {
1088     GST_LOG_OBJECT (allocator,
1089         "Need BindFlags, current flag 0x%x", dmem_priv->desc.BindFlags);
1090     return FALSE;
1091   }
1092 
1093   GST_D3D11_MEMORY_LOCK (mem);
1094   if (dmem_priv->processor_input_view)
1095     goto succeeded;
1096 
1097   desc.FourCC = 0;
1098   desc.ViewDimension = D3D11_VPIV_DIMENSION_TEXTURE2D;
1099   desc.Texture2D.MipSlice = 0;
1100   desc.Texture2D.ArraySlice = dmem_priv->subresource_index;
1101 
1102   hr = video_device->CreateVideoProcessorInputView (dmem_priv->texture,
1103       enumerator, &desc, &dmem_priv->processor_input_view);
1104   if (!gst_d3d11_result (hr, mem->device)) {
1105     GST_ERROR_OBJECT (allocator,
1106         "Could not create processor input view, hr: 0x%x", (guint) hr);
1107     goto done;
1108   }
1109 
1110 succeeded:
1111   ret = TRUE;
1112 
1113 done:
1114   GST_D3D11_MEMORY_UNLOCK (mem);
1115 
1116   return ret;
1117 }
1118 
1119 /**
1120  * gst_d3d11_memory_get_processor_input_view:
1121  * @mem: a #GstD3D11Memory
1122  * @video_device: a #ID3D11VideoDevice
1123  * @enumerator: a #ID3D11VideoProcessorEnumerator
1124  *
1125  * Returns: (transfer none) (nullable): a pointer to the
1126  * ID3D11VideoProcessorInputView or %NULL if ID3D11VideoProcessorInputView is
1127  * unavailable
1128  *
1129  * Since: 1.20
1130  */
1131 ID3D11VideoProcessorInputView *
gst_d3d11_memory_get_processor_input_view(GstD3D11Memory * mem,ID3D11VideoDevice * video_device,ID3D11VideoProcessorEnumerator * enumerator)1132 gst_d3d11_memory_get_processor_input_view (GstD3D11Memory * mem,
1133     ID3D11VideoDevice * video_device,
1134     ID3D11VideoProcessorEnumerator * enumerator)
1135 {
1136   g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1137   g_return_val_if_fail (video_device != NULL, NULL);
1138   g_return_val_if_fail (enumerator != NULL, NULL);
1139 
1140   if (!gst_d3d11_memory_ensure_processor_input_view (mem, video_device,
1141           enumerator))
1142     return NULL;
1143 
1144   return mem->priv->processor_input_view;
1145 }
1146 
1147 static gboolean
gst_d3d11_memory_ensure_processor_output_view(GstD3D11Memory * mem,ID3D11VideoDevice * video_device,ID3D11VideoProcessorEnumerator * enumerator)1148 gst_d3d11_memory_ensure_processor_output_view (GstD3D11Memory * mem,
1149     ID3D11VideoDevice * video_device,
1150     ID3D11VideoProcessorEnumerator * enumerator)
1151 {
1152   GstD3D11MemoryPrivate *priv = mem->priv;
1153   GstD3D11Allocator *allocator;
1154   D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC desc;
1155   HRESULT hr;
1156   gboolean ret;
1157 
1158   memset (&desc, 0, sizeof (D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC));
1159 
1160   allocator = GST_D3D11_ALLOCATOR (GST_MEMORY_CAST (mem)->allocator);
1161 
1162   if (!(priv->desc.BindFlags & D3D11_BIND_RENDER_TARGET)) {
1163     GST_LOG_OBJECT (allocator,
1164         "Need BindFlags, current flag 0x%x", priv->desc.BindFlags);
1165     return FALSE;
1166   }
1167 
1168   /* FIXME: texture array should be supported at some point */
1169   if (priv->subresource_index != 0) {
1170     GST_FIXME_OBJECT (allocator,
1171         "Texture array is not suppoted for processor output view");
1172     return FALSE;
1173   }
1174 
1175   GST_D3D11_MEMORY_LOCK (mem);
1176   if (priv->processor_output_view)
1177     goto succeeded;
1178 
1179   desc.ViewDimension = D3D11_VPOV_DIMENSION_TEXTURE2D;
1180   desc.Texture2D.MipSlice = 0;
1181 
1182   hr = video_device->CreateVideoProcessorOutputView (priv->texture,
1183       enumerator, &desc, &priv->processor_output_view);
1184   if (!gst_d3d11_result (hr, mem->device)) {
1185     GST_ERROR_OBJECT (allocator,
1186         "Could not create processor input view, hr: 0x%x", (guint) hr);
1187     goto done;
1188   }
1189 
1190 succeeded:
1191   ret = TRUE;
1192 
1193 done:
1194   GST_D3D11_MEMORY_UNLOCK (mem);
1195 
1196   return ret;
1197 }
1198 
1199 /**
1200  * gst_d3d11_memory_get_processor_output_view:
1201  * @mem: a #GstD3D11Memory
1202  * @video_device: a #ID3D11VideoDevice
1203  * @enumerator: a #ID3D11VideoProcessorEnumerator
1204  *
1205  * Returns: (transfer none) (nullable): a pointer to the
1206  * ID3D11VideoProcessorOutputView or %NULL if ID3D11VideoProcessorOutputView is
1207  * unavailable
1208  *
1209  * Since: 1.20
1210  */
1211 ID3D11VideoProcessorOutputView *
gst_d3d11_memory_get_processor_output_view(GstD3D11Memory * mem,ID3D11VideoDevice * video_device,ID3D11VideoProcessorEnumerator * enumerator)1212 gst_d3d11_memory_get_processor_output_view (GstD3D11Memory * mem,
1213     ID3D11VideoDevice * video_device,
1214     ID3D11VideoProcessorEnumerator * enumerator)
1215 {
1216   g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1217   g_return_val_if_fail (video_device != NULL, NULL);
1218   g_return_val_if_fail (enumerator != NULL, NULL);
1219 
1220   if (!gst_d3d11_memory_ensure_processor_output_view (mem, video_device,
1221           enumerator))
1222     return NULL;
1223 
1224   return mem->priv->processor_output_view;
1225 }
1226 
1227 /* GstD3D11Allocator */
1228 struct _GstD3D11AllocatorPrivate
1229 {
1230   GstMemoryCopyFunction fallback_copy;
1231 };
1232 
1233 #define gst_d3d11_allocator_parent_class alloc_parent_class
1234 G_DEFINE_TYPE_WITH_PRIVATE (GstD3D11Allocator,
1235     gst_d3d11_allocator, GST_TYPE_ALLOCATOR);
1236 
1237 static GstMemory *gst_d3d11_allocator_dummy_alloc (GstAllocator * allocator,
1238     gsize size, GstAllocationParams * params);
1239 static GstMemory *gst_d3d11_allocator_alloc_internal (GstD3D11Allocator * self,
1240     GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc);
1241 static void gst_d3d11_allocator_free (GstAllocator * allocator,
1242     GstMemory * mem);
1243 
1244 static void
gst_d3d11_allocator_class_init(GstD3D11AllocatorClass * klass)1245 gst_d3d11_allocator_class_init (GstD3D11AllocatorClass * klass)
1246 {
1247   GstAllocatorClass *allocator_class = GST_ALLOCATOR_CLASS (klass);
1248 
1249   allocator_class->alloc = gst_d3d11_allocator_dummy_alloc;
1250   allocator_class->free = gst_d3d11_allocator_free;
1251 }
1252 
1253 static GstMemory *
gst_d3d11_memory_copy(GstMemory * mem,gssize offset,gssize size)1254 gst_d3d11_memory_copy (GstMemory * mem, gssize offset, gssize size)
1255 {
1256   GstD3D11Allocator *alloc = GST_D3D11_ALLOCATOR (mem->allocator);
1257   GstD3D11AllocatorPrivate *priv = alloc->priv;
1258   GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
1259   GstD3D11Memory *copy_dmem;
1260   GstD3D11Device *device = dmem->device;
1261   ID3D11Device *device_handle = gst_d3d11_device_get_device_handle (device);
1262   ID3D11DeviceContext *device_context =
1263       gst_d3d11_device_get_device_context_handle (device);
1264   D3D11_TEXTURE2D_DESC dst_desc = { 0, };
1265   D3D11_TEXTURE2D_DESC src_desc = { 0, };
1266   GstMemory *copy = NULL;
1267   GstMapInfo info;
1268   HRESULT hr;
1269   UINT bind_flags = 0;
1270   UINT supported_flags = 0;
1271 
1272   /* non-zero offset or different size is not supported */
1273   if (offset != 0 || (size != -1 && (gsize) size != mem->size)) {
1274     GST_DEBUG_OBJECT (alloc, "Different size/offset, try fallback copy");
1275     return priv->fallback_copy (mem, offset, size);
1276   }
1277 
1278   gst_d3d11_device_lock (device);
1279   if (!gst_memory_map (mem, &info,
1280           (GstMapFlags) (GST_MAP_READ | GST_MAP_D3D11))) {
1281     gst_d3d11_device_unlock (device);
1282 
1283     GST_WARNING_OBJECT (alloc, "Failed to map memory, try fallback copy");
1284 
1285     return priv->fallback_copy (mem, offset, size);
1286   }
1287 
1288   dmem->priv->texture->GetDesc (&src_desc);
1289   dst_desc.Width = src_desc.Width;
1290   dst_desc.Height = src_desc.Height;
1291   dst_desc.MipLevels = 1;
1292   dst_desc.Format = src_desc.Format;
1293   dst_desc.SampleDesc.Count = 1;
1294   dst_desc.ArraySize = 1;
1295   dst_desc.Usage = D3D11_USAGE_DEFAULT;
1296 
1297   /* If supported, use bind flags for SRV/RTV */
1298   hr = device_handle->CheckFormatSupport (src_desc.Format, &supported_flags);
1299   if (gst_d3d11_result (hr, device)) {
1300     if ((supported_flags & D3D11_FORMAT_SUPPORT_SHADER_SAMPLE) ==
1301         D3D11_FORMAT_SUPPORT_SHADER_SAMPLE) {
1302       bind_flags |= D3D11_BIND_SHADER_RESOURCE;
1303     }
1304 
1305     if ((supported_flags & D3D11_FORMAT_SUPPORT_RENDER_TARGET) ==
1306         D3D11_FORMAT_SUPPORT_RENDER_TARGET) {
1307       bind_flags |= D3D11_BIND_RENDER_TARGET;
1308     }
1309   }
1310 
1311   copy = gst_d3d11_allocator_alloc_internal (alloc, device, &dst_desc);
1312   if (!copy) {
1313     gst_memory_unmap (mem, &info);
1314     gst_d3d11_device_unlock (device);
1315 
1316     GST_WARNING_OBJECT (alloc,
1317         "Failed to allocate new d3d11 map memory, try fallback copy");
1318 
1319     return priv->fallback_copy (mem, offset, size);
1320   }
1321 
1322   copy_dmem = GST_D3D11_MEMORY_CAST (copy);
1323   device_context->CopySubresourceRegion (copy_dmem->priv->texture, 0, 0, 0, 0,
1324       dmem->priv->texture, dmem->priv->subresource_index, NULL);
1325   copy->maxsize = copy->size = mem->maxsize;
1326   gst_memory_unmap (mem, &info);
1327   gst_d3d11_device_unlock (device);
1328 
1329   /* newly allocated memory holds valid image data. We need download this
1330    * pixel data into staging memory for CPU access */
1331   GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
1332 
1333   return copy;
1334 }
1335 
1336 static void
gst_d3d11_allocator_init(GstD3D11Allocator * allocator)1337 gst_d3d11_allocator_init (GstD3D11Allocator * allocator)
1338 {
1339   GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
1340   GstD3D11AllocatorPrivate *priv;
1341 
1342   priv = allocator->priv = (GstD3D11AllocatorPrivate *)
1343       gst_d3d11_allocator_get_instance_private (allocator);
1344 
1345   alloc->mem_type = GST_D3D11_MEMORY_NAME;
1346   alloc->mem_map_full = gst_d3d11_memory_map_full;
1347   alloc->mem_unmap_full = gst_d3d11_memory_unmap_full;
1348   alloc->mem_share = gst_d3d11_memory_share;
1349 
1350   /* Store pointer to default mem_copy method for fallback copy */
1351   priv->fallback_copy = alloc->mem_copy;
1352   alloc->mem_copy = gst_d3d11_memory_copy;
1353 
1354   GST_OBJECT_FLAG_SET (alloc, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
1355 }
1356 
1357 static GstMemory *
gst_d3d11_allocator_dummy_alloc(GstAllocator * allocator,gsize size,GstAllocationParams * params)1358 gst_d3d11_allocator_dummy_alloc (GstAllocator * allocator, gsize size,
1359     GstAllocationParams * params)
1360 {
1361   g_return_val_if_reached (NULL);
1362 }
1363 
1364 static void
gst_d3d11_allocator_free(GstAllocator * allocator,GstMemory * mem)1365 gst_d3d11_allocator_free (GstAllocator * allocator, GstMemory * mem)
1366 {
1367   GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
1368   GstD3D11MemoryPrivate *dmem_priv = dmem->priv;
1369   gint i;
1370 
1371   GST_LOG_OBJECT (allocator, "Free memory %p", mem);
1372 
1373   for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
1374     GST_D3D11_CLEAR_COM (dmem_priv->render_target_view[i]);
1375     GST_D3D11_CLEAR_COM (dmem_priv->shader_resource_view[i]);
1376   }
1377 
1378   GST_D3D11_CLEAR_COM (dmem_priv->decoder_output_view);
1379   GST_D3D11_CLEAR_COM (dmem_priv->processor_input_view);
1380   GST_D3D11_CLEAR_COM (dmem_priv->processor_output_view);
1381   GST_D3D11_CLEAR_COM (dmem_priv->texture);
1382   GST_D3D11_CLEAR_COM (dmem_priv->staging);
1383 
1384   GST_D3D11_CLEAR_COM (dmem_priv->decoder_handle);
1385 
1386   gst_clear_object (&dmem->device);
1387   g_mutex_clear (&dmem_priv->lock);
1388   g_free (dmem->priv);
1389   g_free (dmem);
1390 }
1391 
1392 static GstMemory *
gst_d3d11_allocator_alloc_wrapped(GstD3D11Allocator * self,GstD3D11Device * device,const D3D11_TEXTURE2D_DESC * desc,ID3D11Texture2D * texture)1393 gst_d3d11_allocator_alloc_wrapped (GstD3D11Allocator * self,
1394     GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc,
1395     ID3D11Texture2D * texture)
1396 {
1397   GstD3D11Memory *mem;
1398 
1399   mem = g_new0 (GstD3D11Memory, 1);
1400   mem->priv = g_new0 (GstD3D11MemoryPrivate, 1);
1401 
1402   gst_memory_init (GST_MEMORY_CAST (mem),
1403       (GstMemoryFlags) 0, GST_ALLOCATOR_CAST (self), NULL, 0, 0, 0, 0);
1404   g_mutex_init (&mem->priv->lock);
1405   mem->priv->texture = texture;
1406   mem->priv->desc = *desc;
1407   mem->device = (GstD3D11Device *) gst_object_ref (device);
1408 
1409   /* This is staging texture as well */
1410   if (desc->Usage == D3D11_USAGE_STAGING) {
1411     mem->priv->staging = texture;
1412     texture->AddRef ();
1413   }
1414 
1415   return GST_MEMORY_CAST (mem);
1416 }
1417 
1418 static GstMemory *
gst_d3d11_allocator_alloc_internal(GstD3D11Allocator * self,GstD3D11Device * device,const D3D11_TEXTURE2D_DESC * desc)1419 gst_d3d11_allocator_alloc_internal (GstD3D11Allocator * self,
1420     GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc)
1421 {
1422   ID3D11Texture2D *texture = NULL;
1423   ID3D11Device *device_handle;
1424   HRESULT hr;
1425 
1426   device_handle = gst_d3d11_device_get_device_handle (device);
1427 
1428   hr = device_handle->CreateTexture2D (desc, NULL, &texture);
1429   if (!gst_d3d11_result (hr, device)) {
1430     GST_ERROR_OBJECT (self, "Couldn't create texture");
1431     return NULL;
1432   }
1433 
1434   return gst_d3d11_allocator_alloc_wrapped (self, device, desc, texture);
1435 }
1436 
1437 /**
1438  * gst_d3d11_allocator_alloc:
1439  * @allocator: a #GstD3D11Allocator
1440  * @device: a #GstD3D11Device
1441  * @desc: a D3D11_TEXTURE2D_DESC struct
1442  *
1443  * Returns: a newly allocated #GstD3D11Memory with given parameters.
1444  *
1445  * Since: 1.20
1446  */
1447 GstMemory *
gst_d3d11_allocator_alloc(GstD3D11Allocator * allocator,GstD3D11Device * device,const D3D11_TEXTURE2D_DESC * desc)1448 gst_d3d11_allocator_alloc (GstD3D11Allocator * allocator,
1449     GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc)
1450 {
1451   GstMemory *mem;
1452 
1453   g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), NULL);
1454   g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), NULL);
1455   g_return_val_if_fail (desc != NULL, NULL);
1456 
1457   mem = gst_d3d11_allocator_alloc_internal (allocator, device, desc);
1458   if (!mem)
1459     return NULL;
1460 
1461   if (!gst_d3d11_memory_update_size (mem)) {
1462     GST_ERROR_OBJECT (allocator, "Failed to calculate size");
1463     gst_memory_unref (mem);
1464     return NULL;
1465   }
1466 
1467   return mem;
1468 }
1469 
1470 gboolean
gst_d3d11_allocator_set_active(GstD3D11Allocator * allocator,gboolean active)1471 gst_d3d11_allocator_set_active (GstD3D11Allocator * allocator, gboolean active)
1472 {
1473   GstD3D11AllocatorClass *klass;
1474 
1475   g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), FALSE);
1476 
1477   klass = GST_D3D11_ALLOCATOR_GET_CLASS (allocator);
1478   if (klass->set_actvie)
1479     return klass->set_actvie (allocator, active);
1480 
1481   return TRUE;
1482 }
1483 
1484 /* GstD3D11PoolAllocator */
1485 #define GST_D3D11_POOL_ALLOCATOR_LOCK(alloc)   (g_rec_mutex_lock(&alloc->priv->lock))
1486 #define GST_D3D11_POOL_ALLOCATOR_UNLOCK(alloc) (g_rec_mutex_unlock(&alloc->priv->lock))
1487 #define GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING(alloc)  (g_atomic_int_get (&alloc->priv->flushing))
1488 
1489 struct _GstD3D11PoolAllocatorPrivate
1490 {
1491   /* parent texture when array typed memory is used */
1492   ID3D11Texture2D *texture;
1493   D3D11_TEXTURE2D_DESC desc;
1494 
1495   /* All below member variables are analogous to that of GstBufferPool */
1496   GstAtomicQueue *queue;
1497   GstPoll *poll;
1498 
1499   /* This lock will protect all below variables apart from atomic ones
1500    * (identical to GstBufferPool::priv::rec_lock) */
1501   GRecMutex lock;
1502   gboolean started;
1503   gboolean active;
1504 
1505   /* atomic */
1506   gint outstanding;
1507   guint max_mems;
1508   guint cur_mems;
1509   gboolean flushing;
1510 
1511   /* Calculated memory size, based on Direct3D11 staging texture map.
1512    * Note that, we cannot know the actually staging texture memory size prior
1513    * to map the staging texture because driver will likely require padding */
1514   gsize mem_size;
1515 };
1516 
1517 static void gst_d3d11_pool_allocator_dispose (GObject * object);
1518 static void gst_d3d11_pool_allocator_finalize (GObject * object);
1519 
1520 static gboolean
1521 gst_d3d11_pool_allocator_set_active (GstD3D11Allocator * allocator,
1522     gboolean active);
1523 
1524 static gboolean gst_d3d11_pool_allocator_start (GstD3D11PoolAllocator * self);
1525 static gboolean gst_d3d11_pool_allocator_stop (GstD3D11PoolAllocator * self);
1526 static gboolean gst_d3d11_memory_release (GstMiniObject * mini_object);
1527 
1528 #define gst_d3d11_pool_allocator_parent_class pool_alloc_parent_class
1529 G_DEFINE_TYPE_WITH_PRIVATE (GstD3D11PoolAllocator,
1530     gst_d3d11_pool_allocator, GST_TYPE_D3D11_ALLOCATOR);
1531 
1532 static void
gst_d3d11_pool_allocator_class_init(GstD3D11PoolAllocatorClass * klass)1533 gst_d3d11_pool_allocator_class_init (GstD3D11PoolAllocatorClass * klass)
1534 {
1535   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1536   GstD3D11AllocatorClass *d3d11alloc_class = GST_D3D11_ALLOCATOR_CLASS (klass);
1537 
1538   gobject_class->dispose = gst_d3d11_pool_allocator_dispose;
1539   gobject_class->finalize = gst_d3d11_pool_allocator_finalize;
1540 
1541   d3d11alloc_class->set_actvie = gst_d3d11_pool_allocator_set_active;
1542 }
1543 
1544 static void
gst_d3d11_pool_allocator_init(GstD3D11PoolAllocator * allocator)1545 gst_d3d11_pool_allocator_init (GstD3D11PoolAllocator * allocator)
1546 {
1547   GstD3D11PoolAllocatorPrivate *priv;
1548 
1549   priv = allocator->priv = (GstD3D11PoolAllocatorPrivate *)
1550       gst_d3d11_pool_allocator_get_instance_private (allocator);
1551   g_rec_mutex_init (&priv->lock);
1552 
1553   priv->poll = gst_poll_new_timer ();
1554   priv->queue = gst_atomic_queue_new (16);
1555   priv->flushing = 1;
1556   priv->active = FALSE;
1557   priv->started = FALSE;
1558 
1559   /* 1 control write for flushing - the flush token */
1560   gst_poll_write_control (priv->poll);
1561   /* 1 control write for marking that we are not waiting for poll - the wait token */
1562   gst_poll_write_control (priv->poll);
1563 }
1564 
1565 static void
gst_d3d11_pool_allocator_dispose(GObject * object)1566 gst_d3d11_pool_allocator_dispose (GObject * object)
1567 {
1568   GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (object);
1569 
1570   gst_clear_object (&self->device);
1571 
1572   G_OBJECT_CLASS (pool_alloc_parent_class)->dispose (object);
1573 }
1574 
1575 static void
gst_d3d11_pool_allocator_finalize(GObject * object)1576 gst_d3d11_pool_allocator_finalize (GObject * object)
1577 {
1578   GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (object);
1579   GstD3D11PoolAllocatorPrivate *priv = self->priv;
1580 
1581   GST_DEBUG_OBJECT (self, "Finalize");
1582 
1583   gst_d3d11_pool_allocator_stop (self);
1584   gst_atomic_queue_unref (priv->queue);
1585   gst_poll_free (priv->poll);
1586   g_rec_mutex_clear (&priv->lock);
1587 
1588   GST_D3D11_CLEAR_COM (priv->texture);
1589 
1590   G_OBJECT_CLASS (pool_alloc_parent_class)->finalize (object);
1591 }
1592 
1593 static gboolean
gst_d3d11_pool_allocator_start(GstD3D11PoolAllocator * self)1594 gst_d3d11_pool_allocator_start (GstD3D11PoolAllocator * self)
1595 {
1596   GstD3D11PoolAllocatorPrivate *priv = self->priv;
1597   ID3D11Device *device_handle;
1598   HRESULT hr;
1599   guint i;
1600 
1601   if (priv->started)
1602     return TRUE;
1603 
1604   /* Nothing to do */
1605   if (priv->desc.ArraySize == 1) {
1606     priv->started = TRUE;
1607     return TRUE;
1608   }
1609 
1610   device_handle = gst_d3d11_device_get_device_handle (self->device);
1611 
1612   if (!priv->texture) {
1613     hr = device_handle->CreateTexture2D (&priv->desc, NULL, &priv->texture);
1614     if (!gst_d3d11_result (hr, self->device)) {
1615       GST_ERROR_OBJECT (self, "Failed to allocate texture");
1616       return FALSE;
1617     }
1618   }
1619 
1620   /* Pre-allocate memory objects */
1621   for (i = 0; i < priv->desc.ArraySize; i++) {
1622     GstMemory *mem;
1623 
1624     priv->texture->AddRef ();
1625     mem =
1626         gst_d3d11_allocator_alloc_wrapped (GST_D3D11_ALLOCATOR_CAST
1627         (_d3d11_memory_allocator), self->device, &priv->desc, priv->texture);
1628 
1629     if (i == 0) {
1630       if (!gst_d3d11_memory_update_size (mem)) {
1631         GST_ERROR_OBJECT (self, "Failed to calculate memory size");
1632         gst_memory_unref (mem);
1633         return FALSE;
1634       }
1635 
1636       priv->mem_size = mem->size;
1637     } else {
1638       mem->size = mem->maxsize = priv->mem_size;
1639     }
1640 
1641     GST_D3D11_MEMORY_CAST (mem)->priv->subresource_index = i;
1642 
1643     g_atomic_int_add (&priv->cur_mems, 1);
1644     gst_atomic_queue_push (priv->queue, mem);
1645     gst_poll_write_control (priv->poll);
1646   }
1647 
1648   priv->started = TRUE;
1649 
1650   return TRUE;
1651 }
1652 
1653 static void
gst_d3d11_pool_allocator_do_set_flushing(GstD3D11PoolAllocator * self,gboolean flushing)1654 gst_d3d11_pool_allocator_do_set_flushing (GstD3D11PoolAllocator * self,
1655     gboolean flushing)
1656 {
1657   GstD3D11PoolAllocatorPrivate *priv = self->priv;
1658 
1659   if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self) == flushing)
1660     return;
1661 
1662   if (flushing) {
1663     g_atomic_int_set (&priv->flushing, 1);
1664     /* Write the flush token to wake up any waiters */
1665     gst_poll_write_control (priv->poll);
1666   } else {
1667     while (!gst_poll_read_control (priv->poll)) {
1668       if (errno == EWOULDBLOCK) {
1669         /* This should not really happen unless flushing and unflushing
1670          * happens on different threads. Let's wait a bit to get back flush
1671          * token from the thread that was setting it to flushing */
1672         g_thread_yield ();
1673         continue;
1674       } else {
1675         /* Critical error but GstPoll already complained */
1676         break;
1677       }
1678     }
1679 
1680     g_atomic_int_set (&priv->flushing, 0);
1681   }
1682 }
1683 
1684 static gboolean
gst_d3d11_pool_allocator_set_active(GstD3D11Allocator * allocator,gboolean active)1685 gst_d3d11_pool_allocator_set_active (GstD3D11Allocator * allocator,
1686     gboolean active)
1687 {
1688   GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (allocator);
1689   GstD3D11PoolAllocatorPrivate *priv = self->priv;
1690 
1691   GST_LOG_OBJECT (self, "active %d", active);
1692 
1693   GST_D3D11_POOL_ALLOCATOR_LOCK (self);
1694   /* just return if we are already in the right state */
1695   if (priv->active == active)
1696     goto was_ok;
1697 
1698   if (active) {
1699     if (!gst_d3d11_pool_allocator_start (self))
1700       goto start_failed;
1701 
1702     /* flush_stop may release memory objects, setting to active to avoid running
1703      * do_stop while activating the pool */
1704     priv->active = TRUE;
1705 
1706     gst_d3d11_pool_allocator_do_set_flushing (self, FALSE);
1707   } else {
1708     gint outstanding;
1709 
1710     /* set to flushing first */
1711     gst_d3d11_pool_allocator_do_set_flushing (self, TRUE);
1712 
1713     /* when all memory objects are in the pool, free them. Else they will be
1714      * freed when they are released */
1715     outstanding = g_atomic_int_get (&priv->outstanding);
1716     GST_LOG_OBJECT (self, "outstanding memories %d, (in queue %d)",
1717         outstanding, gst_atomic_queue_length (priv->queue));
1718     if (outstanding == 0) {
1719       if (!gst_d3d11_pool_allocator_stop (self))
1720         goto stop_failed;
1721     }
1722 
1723     priv->active = FALSE;
1724   }
1725 
1726   GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1727 
1728   return TRUE;
1729 
1730 was_ok:
1731   {
1732     GST_DEBUG_OBJECT (self, "allocator was in the right state");
1733     GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1734     return TRUE;
1735   }
1736 start_failed:
1737   {
1738     GST_ERROR_OBJECT (self, "start failed");
1739     GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1740     return FALSE;
1741   }
1742 stop_failed:
1743   {
1744     GST_ERROR_OBJECT (self, "stop failed");
1745     GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1746     return FALSE;
1747   }
1748 }
1749 
1750 static void
gst_d3d11_pool_allocator_free_memory(GstD3D11PoolAllocator * self,GstMemory * mem)1751 gst_d3d11_pool_allocator_free_memory (GstD3D11PoolAllocator * self,
1752     GstMemory * mem)
1753 {
1754   GstD3D11PoolAllocatorPrivate *priv = self->priv;
1755 
1756   g_atomic_int_add (&priv->cur_mems, -1);
1757   GST_LOG_OBJECT (self, "freeing memory %p (%u left)", mem, priv->cur_mems);
1758 
1759   GST_MINI_OBJECT_CAST (mem)->dispose = NULL;
1760   gst_memory_unref (mem);
1761 }
1762 
1763 /* must be called with the lock */
1764 static gboolean
gst_d3d11_pool_allocator_clear_queue(GstD3D11PoolAllocator * self)1765 gst_d3d11_pool_allocator_clear_queue (GstD3D11PoolAllocator * self)
1766 {
1767   GstD3D11PoolAllocatorPrivate *priv = self->priv;
1768   GstMemory *memory;
1769 
1770   GST_LOG_OBJECT (self, "Clearing queue");
1771 
1772   /* clear the pool */
1773   while ((memory = (GstMemory *) gst_atomic_queue_pop (priv->queue))) {
1774     while (!gst_poll_read_control (priv->poll)) {
1775       if (errno == EWOULDBLOCK) {
1776         /* We put the memory into the queue but did not finish writing control
1777          * yet, let's wait a bit and retry */
1778         g_thread_yield ();
1779         continue;
1780       } else {
1781         /* Critical error but GstPoll already complained */
1782         break;
1783       }
1784     }
1785     gst_d3d11_pool_allocator_free_memory (self, memory);
1786   }
1787 
1788   GST_LOG_OBJECT (self, "Clear done");
1789 
1790   return priv->cur_mems == 0;
1791 }
1792 
1793 /* must be called with the lock */
1794 static gboolean
gst_d3d11_pool_allocator_stop(GstD3D11PoolAllocator * self)1795 gst_d3d11_pool_allocator_stop (GstD3D11PoolAllocator * self)
1796 {
1797   GstD3D11PoolAllocatorPrivate *priv = self->priv;
1798 
1799   GST_DEBUG_OBJECT (self, "Stop");
1800 
1801   if (priv->started) {
1802     if (!gst_d3d11_pool_allocator_clear_queue (self))
1803       return FALSE;
1804 
1805     priv->started = FALSE;
1806   } else {
1807     GST_DEBUG_OBJECT (self, "Wasn't started");
1808   }
1809 
1810   return TRUE;
1811 }
1812 
1813 static inline void
dec_outstanding(GstD3D11PoolAllocator * self)1814 dec_outstanding (GstD3D11PoolAllocator * self)
1815 {
1816   if (g_atomic_int_dec_and_test (&self->priv->outstanding)) {
1817     /* all memory objects are returned to the pool, see if we need to free them */
1818     if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)) {
1819       /* take the lock so that set_active is not run concurrently */
1820       GST_D3D11_POOL_ALLOCATOR_LOCK (self);
1821       /* now that we have the lock, check if we have been de-activated with
1822        * outstanding buffers */
1823       if (!self->priv->active)
1824         gst_d3d11_pool_allocator_stop (self);
1825 
1826       GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1827     }
1828   }
1829 }
1830 
1831 static void
gst_d3d11_pool_allocator_release_memory(GstD3D11PoolAllocator * self,GstMemory * mem)1832 gst_d3d11_pool_allocator_release_memory (GstD3D11PoolAllocator * self,
1833     GstMemory * mem)
1834 {
1835   GST_LOG_OBJECT (self, "Released memory %p", mem);
1836 
1837   GST_MINI_OBJECT_CAST (mem)->dispose = NULL;
1838   mem->allocator = (GstAllocator *) gst_object_ref (_d3d11_memory_allocator);
1839   gst_object_unref (self);
1840 
1841   /* keep it around in our queue */
1842   gst_atomic_queue_push (self->priv->queue, mem);
1843   gst_poll_write_control (self->priv->poll);
1844   dec_outstanding (self);
1845 }
1846 
1847 static gboolean
gst_d3d11_memory_release(GstMiniObject * mini_object)1848 gst_d3d11_memory_release (GstMiniObject * mini_object)
1849 {
1850   GstMemory *mem = GST_MEMORY_CAST (mini_object);
1851   GstD3D11PoolAllocator *alloc;
1852 
1853   g_assert (mem->allocator != NULL);
1854 
1855   if (!GST_IS_D3D11_POOL_ALLOCATOR (mem->allocator)) {
1856     GST_LOG_OBJECT (mem->allocator, "Not our memory, free");
1857     return TRUE;
1858   }
1859 
1860   alloc = GST_D3D11_POOL_ALLOCATOR (mem->allocator);
1861   /* if flushing, free this memory */
1862   if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (alloc)) {
1863     GST_LOG_OBJECT (alloc, "allocator is flushing, free %p", mem);
1864     return TRUE;
1865   }
1866 
1867   /* return the memory to the allocator */
1868   gst_memory_ref (mem);
1869   gst_d3d11_pool_allocator_release_memory (alloc, mem);
1870 
1871   return FALSE;
1872 }
1873 
1874 static GstFlowReturn
gst_d3d11_pool_allocator_alloc(GstD3D11PoolAllocator * self,GstMemory ** mem)1875 gst_d3d11_pool_allocator_alloc (GstD3D11PoolAllocator * self, GstMemory ** mem)
1876 {
1877   GstD3D11PoolAllocatorPrivate *priv = self->priv;
1878   GstMemory *new_mem;
1879 
1880   /* we allcates texture array during start */
1881   if (priv->desc.ArraySize > 1)
1882     return GST_FLOW_EOS;
1883 
1884   /* increment the allocation counter */
1885   g_atomic_int_add (&priv->cur_mems, 1);
1886   new_mem =
1887       gst_d3d11_allocator_alloc_internal (GST_D3D11_ALLOCATOR_CAST
1888       (_d3d11_memory_allocator), self->device, &priv->desc);
1889   if (!new_mem) {
1890     GST_ERROR_OBJECT (self, "Failed to allocate new memory");
1891     g_atomic_int_add (&priv->cur_mems, -1);
1892     return GST_FLOW_ERROR;
1893   }
1894 
1895   if (!priv->mem_size) {
1896     if (!gst_d3d11_memory_update_size (new_mem)) {
1897       GST_ERROR_OBJECT (self, "Failed to calculate size");
1898       gst_memory_unref (new_mem);
1899       g_atomic_int_add (&priv->cur_mems, -1);
1900 
1901       return GST_FLOW_ERROR;
1902     }
1903 
1904     priv->mem_size = new_mem->size;
1905   }
1906 
1907   new_mem->size = new_mem->maxsize = priv->mem_size;
1908 
1909   *mem = new_mem;
1910 
1911   return GST_FLOW_OK;
1912 }
1913 
1914 static GstFlowReturn
gst_d3d11_pool_allocator_acquire_memory_internal(GstD3D11PoolAllocator * self,GstMemory ** memory)1915 gst_d3d11_pool_allocator_acquire_memory_internal (GstD3D11PoolAllocator * self,
1916     GstMemory ** memory)
1917 {
1918   GstFlowReturn result;
1919   GstD3D11PoolAllocatorPrivate *priv = self->priv;
1920 
1921   while (TRUE) {
1922     if (G_UNLIKELY (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)))
1923       goto flushing;
1924 
1925     /* try to get a memory from the queue */
1926     *memory = (GstMemory *) gst_atomic_queue_pop (priv->queue);
1927     if (G_LIKELY (*memory)) {
1928       while (!gst_poll_read_control (priv->poll)) {
1929         if (errno == EWOULDBLOCK) {
1930           /* We put the memory into the queue but did not finish writing control
1931            * yet, let's wait a bit and retry */
1932           g_thread_yield ();
1933           continue;
1934         } else {
1935           /* Critical error but GstPoll already complained */
1936           break;
1937         }
1938       }
1939       result = GST_FLOW_OK;
1940       GST_LOG_OBJECT (self, "acquired memory %p", *memory);
1941       break;
1942     }
1943 
1944     /* no memory, try to allocate some more */
1945     GST_LOG_OBJECT (self, "no memory, trying to allocate");
1946     result = gst_d3d11_pool_allocator_alloc (self, memory);
1947     if (G_LIKELY (result == GST_FLOW_OK))
1948       /* we have a memory, return it */
1949       break;
1950 
1951     if (G_UNLIKELY (result != GST_FLOW_EOS))
1952       /* something went wrong, return error */
1953       break;
1954 
1955     /* now we release the control socket, we wait for a memory release or
1956      * flushing */
1957     if (!gst_poll_read_control (priv->poll)) {
1958       if (errno == EWOULDBLOCK) {
1959         /* This means that we have two threads trying to allocate memory
1960          * already, and the other one already got the wait token. This
1961          * means that we only have to wait for the poll now and not write the
1962          * token afterwards: we will be woken up once the other thread is
1963          * woken up and that one will write the wait token it removed */
1964         GST_LOG_OBJECT (self, "waiting for free memory or flushing");
1965         gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
1966       } else {
1967         /* This is a critical error, GstPoll already gave a warning */
1968         result = GST_FLOW_ERROR;
1969         break;
1970       }
1971     } else {
1972       /* We're the first thread waiting, we got the wait token and have to
1973        * write it again later
1974        * OR
1975        * We're a second thread and just consumed the flush token and block all
1976        * other threads, in which case we must not wait and give it back
1977        * immediately */
1978       if (!GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)) {
1979         GST_LOG_OBJECT (self, "waiting for free memory or flushing");
1980         gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
1981       }
1982       gst_poll_write_control (priv->poll);
1983     }
1984   }
1985 
1986   return result;
1987 
1988   /* ERRORS */
1989 flushing:
1990   {
1991     GST_DEBUG_OBJECT (self, "we are flushing");
1992     return GST_FLOW_FLUSHING;
1993   }
1994 }
1995 
1996 /**
1997  * gst_d3d11_pool_allocator_new:
1998  * @device: a #GstD3D11Device
1999  * @desc: a D3D11_TEXTURE2D_DESC for texture allocation
2000  *
2001  * Creates a new #GstD3D11PoolAllocator instance.
2002  *
2003  * Returns: (transfer full): a new #GstD3D11PoolAllocator instance
2004  */
2005 GstD3D11PoolAllocator *
gst_d3d11_pool_allocator_new(GstD3D11Device * device,const D3D11_TEXTURE2D_DESC * desc)2006 gst_d3d11_pool_allocator_new (GstD3D11Device * device,
2007     const D3D11_TEXTURE2D_DESC * desc)
2008 {
2009   GstD3D11PoolAllocator *self;
2010 
2011   g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), NULL);
2012   g_return_val_if_fail (desc != NULL, NULL);
2013 
2014   gst_d3d11_memory_init_once ();
2015 
2016   self = (GstD3D11PoolAllocator *)
2017       g_object_new (GST_TYPE_D3D11_POOL_ALLOCATOR, NULL);
2018   gst_object_ref_sink (self);
2019 
2020   self->device = (GstD3D11Device *) gst_object_ref (device);
2021   self->priv->desc = *desc;
2022 
2023   return self;
2024 }
2025 
2026 /**
2027  * gst_d3d11_pool_allocator_acquire_memory:
2028  * @allocator: a #GstD3D11PoolAllocator
2029  * @memory: (transfer full): a #GstMemory
2030  *
2031  * Acquires a #GstMemory from @allocator. @memory should point to a memory
2032  * location that can hold a pointer to the new #GstMemory.
2033  *
2034  * Returns: a #GstFlowReturn such as %GST_FLOW_FLUSHING when the allocator is
2035  * inactive.
2036  */
2037 GstFlowReturn
gst_d3d11_pool_allocator_acquire_memory(GstD3D11PoolAllocator * allocator,GstMemory ** memory)2038 gst_d3d11_pool_allocator_acquire_memory (GstD3D11PoolAllocator * allocator,
2039     GstMemory ** memory)
2040 {
2041   GstD3D11PoolAllocatorPrivate *priv;
2042   GstFlowReturn result;
2043 
2044   g_return_val_if_fail (GST_IS_D3D11_POOL_ALLOCATOR (allocator),
2045       GST_FLOW_ERROR);
2046   g_return_val_if_fail (memory != NULL, GST_FLOW_ERROR);
2047 
2048   priv = allocator->priv;
2049 
2050   /* assume we'll have one more outstanding buffer we need to do that so
2051    * that concurrent set_active doesn't clear the buffers */
2052   g_atomic_int_inc (&priv->outstanding);
2053   result = gst_d3d11_pool_allocator_acquire_memory_internal (allocator, memory);
2054 
2055   if (result == GST_FLOW_OK) {
2056     GstMemory *mem = *memory;
2057     /* Replace default allocator with ours */
2058     gst_object_unref (mem->allocator);
2059     mem->allocator = (GstAllocator *) gst_object_ref (allocator);
2060     GST_MINI_OBJECT_CAST (mem)->dispose = gst_d3d11_memory_release;
2061   } else {
2062     dec_outstanding (allocator);
2063   }
2064 
2065   return result;
2066 }
2067 
2068 /**
2069  * gst_d3d11_pool_allocator_get_pool_size:
2070  * @allocator: a #GstD3D11PoolAllocator
2071  * @max_size: (out) (optional): the max size of pool
2072  * @outstanding_size: (out) (optional): the number of outstanding memory
2073  *
2074  * Returns: %TRUE if the size of memory pool is known
2075  *
2076  * Since: 1.20
2077  */
2078 gboolean
gst_d3d11_pool_allocator_get_pool_size(GstD3D11PoolAllocator * allocator,guint * max_size,guint * outstanding_size)2079 gst_d3d11_pool_allocator_get_pool_size (GstD3D11PoolAllocator * allocator,
2080     guint * max_size, guint * outstanding_size)
2081 {
2082   GstD3D11PoolAllocatorPrivate *priv;
2083 
2084   g_return_val_if_fail (GST_IS_D3D11_POOL_ALLOCATOR (allocator), FALSE);
2085 
2086   priv = allocator->priv;
2087 
2088   if (max_size) {
2089     if (priv->desc.ArraySize > 1) {
2090       *max_size = priv->desc.ArraySize;
2091     } else {
2092       /* For non-texture-array memory, we don't have any limit yet */
2093       *max_size = 0;
2094     }
2095   }
2096 
2097   if (outstanding_size)
2098     *outstanding_size = g_atomic_int_get (&priv->outstanding);
2099 
2100   return TRUE;
2101 }
2102