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