2 * Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
3 * Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
26 #include "gstd3d11memory.h"
27 #include "gstd3d11device.h"
28 #include "gstd3d11utils.h"
29 #include "gstd3d11_private.h"
31 GST_DEBUG_CATEGORY_STATIC (gst_d3d11_allocator_debug);
32 #define GST_CAT_DEFAULT gst_d3d11_allocator_debug
34 static GstAllocator *_d3d11_memory_allocator;
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));
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
51 * Create #GstD3D11AllocationParams object which is used by #GstD3D11BufferPool
52 * and #GstD3D11Allocator in order to allocate new ID3D11Texture2D
53 * object with given configuration
55 * Returns: a #GstD3D11AllocationParams or %NULL if @info is not supported
59 GstD3D11AllocationParams *
60 gst_d3d11_allocation_params_new (GstD3D11Device * device, GstVideoInfo * info,
61 GstD3D11AllocationFlags flags, guint bind_flags)
63 GstD3D11AllocationParams *ret;
64 GstD3D11Format d3d11_format;
67 g_return_val_if_fail (info != NULL, NULL);
69 if (!gst_d3d11_device_get_format (device, GST_VIDEO_INFO_FORMAT (info),
71 GST_WARNING ("Couldn't get d3d11 format");
75 ret = g_new0 (GstD3D11AllocationParams, 1);
78 ret->aligned_info = *info;
79 ret->d3d11_format = d3d11_format;
82 * https://docs.microsoft.com/en-us/windows/win32/api/d3d11/ne-d3d11-d3d11_usage
84 * +----------------------------------------------------------+
85 * | Resource Usage | Default | Dynamic | Immutable | Staging |
86 * +----------------+---------+---------+-----------+---------+
87 * | GPU-Read | Yes | Yes | Yes | Yes |
88 * | GPU-Write | Yes | | | Yes |
89 * | CPU-Read | | | | Yes |
90 * | CPU-Write | | Yes | | Yes |
91 * +----------------------------------------------------------+
94 /* If corresponding dxgi format is undefined, use resource format instead */
95 if (d3d11_format.dxgi_format == DXGI_FORMAT_UNKNOWN) {
96 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (info); i++) {
97 g_assert (d3d11_format.resource_format[i] != DXGI_FORMAT_UNKNOWN);
99 ret->desc[i].Width = GST_VIDEO_INFO_COMP_WIDTH (info, i);
100 ret->desc[i].Height = GST_VIDEO_INFO_COMP_HEIGHT (info, i);
101 ret->desc[i].MipLevels = 1;
102 ret->desc[i].ArraySize = 1;
103 ret->desc[i].Format = d3d11_format.resource_format[i];
104 ret->desc[i].SampleDesc.Count = 1;
105 ret->desc[i].SampleDesc.Quality = 0;
106 ret->desc[i].Usage = D3D11_USAGE_DEFAULT;
107 ret->desc[i].BindFlags = bind_flags;
110 ret->desc[0].Width = GST_VIDEO_INFO_WIDTH (info);
111 ret->desc[0].Height = GST_VIDEO_INFO_HEIGHT (info);
112 ret->desc[0].MipLevels = 1;
113 ret->desc[0].ArraySize = 1;
114 ret->desc[0].Format = d3d11_format.dxgi_format;
115 ret->desc[0].SampleDesc.Count = 1;
116 ret->desc[0].SampleDesc.Quality = 0;
117 ret->desc[0].Usage = D3D11_USAGE_DEFAULT;
118 ret->desc[0].BindFlags = bind_flags;
127 * gst_d3d11_allocation_params_alignment:
128 * @params: a #GstD3D11AllocationParams
129 * @align: a #GstVideoAlignment
131 * Adjust Width and Height fields of D3D11_TEXTURE2D_DESC with given
134 * Returns: %TRUE if alignment could be applied
139 gst_d3d11_allocation_params_alignment (GstD3D11AllocationParams * params,
140 GstVideoAlignment * align)
143 guint padding_width, padding_height;
145 GstVideoInfo new_info;
147 g_return_val_if_fail (params != NULL, FALSE);
148 g_return_val_if_fail (align != NULL, FALSE);
150 /* d3d11 does not support stride align. Consider padding only */
151 padding_width = align->padding_left + align->padding_right;
152 padding_height = align->padding_top + align->padding_bottom;
154 info = ¶ms->info;
156 if (!gst_video_info_set_format (&new_info, GST_VIDEO_INFO_FORMAT (info),
157 GST_VIDEO_INFO_WIDTH (info) + padding_width,
158 GST_VIDEO_INFO_HEIGHT (info) + padding_height)) {
159 GST_WARNING ("Set format fail");
163 params->aligned_info = new_info;
165 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (info); i++) {
166 params->desc[i].Width = GST_VIDEO_INFO_COMP_WIDTH (&new_info, i);
167 params->desc[i].Height = GST_VIDEO_INFO_COMP_HEIGHT (&new_info, i);
174 * gst_d3d11_allocation_params_copy:
175 * @src: a #GstD3D11AllocationParams
177 * Returns: a copy of @src
181 GstD3D11AllocationParams *
182 gst_d3d11_allocation_params_copy (GstD3D11AllocationParams * src)
184 GstD3D11AllocationParams *dst;
186 g_return_val_if_fail (src != NULL, NULL);
188 dst = g_new0 (GstD3D11AllocationParams, 1);
189 memcpy (dst, src, sizeof (GstD3D11AllocationParams));
195 * gst_d3d11_allocation_params_free:
196 * @params: a #GstD3D11AllocationParams
203 gst_d3d11_allocation_params_free (GstD3D11AllocationParams * params)
209 gst_d3d11_allocation_params_compare (const GstD3D11AllocationParams * p1,
210 const GstD3D11AllocationParams * p2)
212 g_return_val_if_fail (p1 != NULL, -1);
213 g_return_val_if_fail (p2 != NULL, -1);
222 gst_d3d11_allocation_params_init (GType type)
224 static GstValueTable table = {
225 0, (GstValueCompareFunc) gst_d3d11_allocation_params_compare,
230 gst_value_register (&table);
234 #define GST_D3D11_MEMORY_GET_LOCK(m) (&(GST_D3D11_MEMORY_CAST(m)->priv->lock))
235 #define GST_D3D11_MEMORY_LOCK(m) G_STMT_START { \
236 GST_TRACE("Locking %p from thread %p", (m), g_thread_self()); \
237 g_mutex_lock(GST_D3D11_MEMORY_GET_LOCK(m)); \
238 GST_TRACE("Locked %p from thread %p", (m), g_thread_self()); \
241 #define GST_D3D11_MEMORY_UNLOCK(m) G_STMT_START { \
242 GST_TRACE("Unlocking %p from thread %p", (m), g_thread_self()); \
243 g_mutex_unlock(GST_D3D11_MEMORY_GET_LOCK(m)); \
246 struct _GstD3D11MemoryPrivate
248 ID3D11Texture2D *texture;
249 ID3D11Buffer *buffer;
251 ID3D11Resource *staging;
253 D3D11_TEXTURE2D_DESC desc;
254 D3D11_BUFFER_DESC buffer_desc;
256 guint subresource_index;
258 ID3D11ShaderResourceView *shader_resource_view[GST_VIDEO_MAX_PLANES];
259 guint num_shader_resource_views;
261 ID3D11RenderTargetView *render_target_view[GST_VIDEO_MAX_PLANES];
262 guint num_render_target_views;
264 ID3D11VideoDecoderOutputView *decoder_output_view;
265 ID3D11VideoProcessorInputView *processor_input_view;
266 ID3D11VideoProcessorOutputView *processor_output_view;
268 D3D11_MAPPED_SUBRESOURCE map;
270 GstD3D11MemoryNativeType native_type;
276 GST_DEFINE_MINI_OBJECT_TYPE (GstD3D11Memory, gst_d3d11_memory);
278 static inline D3D11_MAP
279 gst_d3d11_map_flags_to_d3d11 (GstMapFlags flags)
281 if ((flags & GST_MAP_READWRITE) == GST_MAP_READWRITE)
282 return D3D11_MAP_READ_WRITE;
283 else if ((flags & GST_MAP_WRITE) == GST_MAP_WRITE)
284 return D3D11_MAP_WRITE;
285 else if ((flags & GST_MAP_READ) == GST_MAP_READ)
286 return D3D11_MAP_READ;
288 g_assert_not_reached ();
290 return D3D11_MAP_READ;
293 static ID3D11Texture2D *
294 gst_d3d11_allocate_staging_texture (GstD3D11Device * device,
295 const D3D11_TEXTURE2D_DESC * ref)
297 D3D11_TEXTURE2D_DESC desc = { 0, };
298 ID3D11Texture2D *texture = NULL;
299 ID3D11Device *device_handle = gst_d3d11_device_get_device_handle (device);
302 desc.Width = ref->Width;
303 desc.Height = ref->Height;
305 desc.Format = ref->Format;
306 desc.SampleDesc.Count = 1;
308 desc.Usage = D3D11_USAGE_STAGING;
309 desc.CPUAccessFlags = (D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE);
311 hr = device_handle->CreateTexture2D (&desc, NULL, &texture);
312 if (!gst_d3d11_result (hr, device)) {
313 GST_ERROR_OBJECT (device, "Failed to create texture");
320 /* Must be called with d3d11 device lock */
322 gst_d3d11_memory_map_cpu_access (GstD3D11Memory * dmem, D3D11_MAP map_type)
324 GstD3D11MemoryPrivate *priv = dmem->priv;
326 ID3D11DeviceContext *device_context =
327 gst_d3d11_device_get_device_context_handle (dmem->device);
329 hr = device_context->Map (priv->staging, 0, map_type, 0, &priv->map);
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);
340 /* Must be called with d3d11 device lock */
342 gst_d3d11_memory_upload (GstD3D11Memory * dmem)
344 GstD3D11MemoryPrivate *priv = dmem->priv;
345 ID3D11DeviceContext *device_context;
347 if (!priv->staging || priv->staging == priv->texture ||
348 !GST_MEMORY_FLAG_IS_SET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD))
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);
356 /* Must be called with d3d11 device lock */
358 gst_d3d11_memory_download (GstD3D11Memory * dmem)
360 GstD3D11MemoryPrivate *priv = dmem->priv;
361 ID3D11DeviceContext *device_context;
363 if (!priv->staging || priv->staging == priv->texture ||
364 !GST_MEMORY_FLAG_IS_SET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD))
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);
373 gst_d3d11_memory_map_full (GstMemory * mem, GstMapInfo * info, gsize maxsize)
375 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
376 GstD3D11MemoryPrivate *priv = dmem->priv;
377 GstMapFlags flags = info->flags;
380 gst_d3d11_device_lock (dmem->device);
381 GST_D3D11_MEMORY_LOCK (dmem);
383 memset (info->user_data, 0, sizeof (info->user_data));
384 info->user_data[0] = GUINT_TO_POINTER (dmem->priv->subresource_index);
386 if ((flags & GST_MAP_D3D11) == GST_MAP_D3D11) {
387 if (priv->native_type == GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER) {
388 /* FIXME: handle non-staging buffer */
389 g_assert (priv->buffer != nullptr);
392 gst_d3d11_memory_upload (dmem);
393 GST_MEMORY_FLAG_UNSET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD);
395 if ((flags & GST_MAP_WRITE) == GST_MAP_WRITE)
396 GST_MINI_OBJECT_FLAG_SET (dmem,
397 GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
399 g_assert (priv->texture != NULL);
405 if (priv->cpu_map_count == 0) {
408 /* FIXME: handle non-staging buffer */
409 if (priv->native_type == GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D) {
410 /* Allocate staging texture for CPU access */
411 if (!priv->staging) {
412 priv->staging = gst_d3d11_allocate_staging_texture (dmem->device,
414 if (!priv->staging) {
415 GST_ERROR_OBJECT (mem->allocator, "Couldn't create staging texture");
419 /* first memory, always need download to staging */
420 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
423 gst_d3d11_memory_download (dmem);
426 map_type = gst_d3d11_map_flags_to_d3d11 (flags);
427 if (!gst_d3d11_memory_map_cpu_access (dmem, map_type)) {
428 GST_ERROR_OBJECT (mem->allocator, "Couldn't map staging texture");
433 if ((flags & GST_MAP_WRITE) == GST_MAP_WRITE) {
434 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD);
437 GST_MEMORY_FLAG_UNSET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
439 priv->cpu_map_count++;
440 ret = dmem->priv->map.pData;
443 GST_D3D11_MEMORY_UNLOCK (dmem);
444 gst_d3d11_device_unlock (dmem->device);
449 /* Must be called with d3d11 device lock */
451 gst_d3d11_memory_unmap_cpu_access (GstD3D11Memory * dmem)
453 GstD3D11MemoryPrivate *priv = dmem->priv;
454 ID3D11DeviceContext *device_context =
455 gst_d3d11_device_get_device_context_handle (dmem->device);
457 device_context->Unmap (priv->staging, 0);
461 gst_d3d11_memory_unmap_full (GstMemory * mem, GstMapInfo * info)
463 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
464 GstD3D11MemoryPrivate *priv = dmem->priv;
466 gst_d3d11_device_lock (dmem->device);
467 GST_D3D11_MEMORY_LOCK (dmem);
469 if ((info->flags & GST_MAP_D3D11) == GST_MAP_D3D11) {
470 if ((info->flags & GST_MAP_WRITE) == GST_MAP_WRITE)
471 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
476 if ((info->flags & GST_MAP_WRITE) == GST_MAP_WRITE)
477 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD);
479 priv->cpu_map_count--;
480 if (priv->cpu_map_count > 0)
483 gst_d3d11_memory_unmap_cpu_access (dmem);
486 GST_D3D11_MEMORY_UNLOCK (dmem);
487 gst_d3d11_device_unlock (dmem->device);
491 gst_d3d11_memory_share (GstMemory * mem, gssize offset, gssize size)
498 gst_d3d11_memory_update_size (GstMemory * mem)
500 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
501 GstD3D11MemoryPrivate *priv = dmem->priv;
502 gsize offset[GST_VIDEO_MAX_PLANES];
503 gint stride[GST_VIDEO_MAX_PLANES];
505 D3D11_TEXTURE2D_DESC *desc = &priv->desc;
506 gboolean ret = FALSE;
508 if (!priv->staging) {
509 priv->staging = gst_d3d11_allocate_staging_texture (dmem->device,
511 if (!priv->staging) {
512 GST_ERROR_OBJECT (mem->allocator, "Couldn't create staging texture");
516 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
519 gst_d3d11_device_lock (dmem->device);
520 if (!gst_d3d11_memory_map_cpu_access (dmem, D3D11_MAP_READ_WRITE)) {
521 GST_ERROR_OBJECT (mem->allocator, "Couldn't map staging texture");
525 gst_d3d11_memory_unmap_cpu_access (dmem);
527 if (!gst_d3d11_dxgi_format_get_size (desc->Format, desc->Width, desc->Height,
528 priv->map.RowPitch, offset, stride, &size)) {
529 GST_ERROR_OBJECT (mem->allocator, "Couldn't calculate memory size");
533 mem->maxsize = mem->size = size;
537 gst_d3d11_device_unlock (dmem->device);
542 * gst_is_d3d11_memory:
545 * Returns: whether @mem is a #GstD3D11Memory
550 gst_is_d3d11_memory (GstMemory * mem)
552 return mem != NULL && mem->allocator != NULL &&
553 (GST_IS_D3D11_ALLOCATOR (mem->allocator) ||
554 GST_IS_D3D11_POOL_ALLOCATOR (mem->allocator));
558 * gst_d3d11_memory_get_native_type:
559 * @mem: a #GstD3D11Memory
561 * Returns: a #GstD3D11MemoryNativeType
565 GstD3D11MemoryNativeType
566 gst_d3d11_memory_get_native_type (GstD3D11Memory * mem)
568 if (!gst_is_d3d11_memory (GST_MEMORY_CAST (mem)))
569 return GST_D3D11_MEMORY_NATIVE_TYPE_INVALID;
571 return mem->priv->native_type;
575 * gst_d3d11_memory_init_once:
577 * Initializes the Direct3D11 Texture allocator. It is safe to call
578 * this function multiple times. This must be called before any other
579 * GstD3D11Memory operation.
584 gst_d3d11_memory_init_once (void)
586 static gsize _init = 0;
588 if (g_once_init_enter (&_init)) {
590 GST_DEBUG_CATEGORY_INIT (gst_d3d11_allocator_debug, "d3d11allocator", 0,
591 "Direct3D11 Texture Allocator");
593 _d3d11_memory_allocator =
594 (GstAllocator *) g_object_new (GST_TYPE_D3D11_ALLOCATOR, NULL);
595 gst_object_ref_sink (_d3d11_memory_allocator);
597 gst_allocator_register (GST_D3D11_MEMORY_NAME, _d3d11_memory_allocator);
598 g_once_init_leave (&_init, 1);
603 * gst_d3d11_memory_get_resource_handle:
604 * @mem: a #GstD3D11Memory
606 * Returns: (transfer none): a ID3D11Resource handle. Caller must not release
612 gst_d3d11_memory_get_resource_handle (GstD3D11Memory * mem)
614 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
616 switch (mem->priv->native_type) {
617 case GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER:
618 return mem->priv->buffer;
619 case GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D:
620 return mem->priv->texture;
629 * gst_d3d11_memory_get_subresource_index:
630 * @mem: a #GstD3D11Memory
632 * Returns: subresource index corresponding to @mem.
637 gst_d3d11_memory_get_subresource_index (GstD3D11Memory * mem)
639 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), 0);
641 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
644 return mem->priv->subresource_index;
648 * gst_d3d11_memory_get_texture_desc:
649 * @mem: a #GstD3D11Memory
650 * @desc: (out): a D3D11_TEXTURE2D_DESC
652 * Fill @desc with D3D11_TEXTURE2D_DESC for ID3D11Texture2D
654 * Returns: %TRUE if successeed
659 gst_d3d11_memory_get_texture_desc (GstD3D11Memory * mem,
660 D3D11_TEXTURE2D_DESC * desc)
662 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), FALSE);
663 g_return_val_if_fail (desc != NULL, FALSE);
665 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
668 *desc = mem->priv->desc;
674 * gst_d3d11_memory_get_buffer_desc:
675 * @mem: a #GstD3D11Memory
676 * @desc: (out): a D3D11_BUFFER_DESC
678 * Fill @desc with D3D11_BUFFER_DESC for ID3D11Buffer
680 * Returns: %TRUE if successeed
685 gst_d3d11_memory_get_buffer_desc (GstD3D11Memory * mem,
686 D3D11_BUFFER_DESC * desc)
688 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), FALSE);
689 g_return_val_if_fail (desc != NULL, FALSE);
691 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER)
694 *desc = mem->priv->buffer_desc;
700 * gst_d3d11_memory_get_resource_stride:
701 * @mem: a #GstD3D11Memory
702 * @stride: (out): stride of resource
704 * Returns: %TRUE if successeed
709 gst_d3d11_memory_get_resource_stride (GstD3D11Memory * mem, guint * stride)
711 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), FALSE);
712 g_return_val_if_fail (stride != NULL, FALSE);
714 *stride = mem->priv->map.RowPitch;
720 create_shader_resource_views (GstD3D11Memory * mem)
722 GstD3D11MemoryPrivate *priv = mem->priv;
726 ID3D11Device *device_handle;
727 D3D11_SHADER_RESOURCE_VIEW_DESC resource_desc;
728 DXGI_FORMAT formats[GST_VIDEO_MAX_PLANES] = { DXGI_FORMAT_UNKNOWN, };
730 memset (&resource_desc, 0, sizeof (D3D11_SHADER_RESOURCE_VIEW_DESC));
732 device_handle = gst_d3d11_device_get_device_handle (mem->device);
734 switch (priv->desc.Format) {
735 case DXGI_FORMAT_B8G8R8A8_UNORM:
736 case DXGI_FORMAT_R8G8B8A8_UNORM:
737 case DXGI_FORMAT_R10G10B10A2_UNORM:
738 case DXGI_FORMAT_R8_UNORM:
739 case DXGI_FORMAT_R8G8_UNORM:
740 case DXGI_FORMAT_R16_UNORM:
741 case DXGI_FORMAT_R16G16_UNORM:
742 case DXGI_FORMAT_G8R8_G8B8_UNORM:
743 case DXGI_FORMAT_R8G8_B8G8_UNORM:
744 case DXGI_FORMAT_R16G16B16A16_UNORM:
746 formats[0] = priv->desc.Format;
748 case DXGI_FORMAT_AYUV:
749 case DXGI_FORMAT_YUY2:
751 formats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
753 case DXGI_FORMAT_NV12:
755 formats[0] = DXGI_FORMAT_R8_UNORM;
756 formats[1] = DXGI_FORMAT_R8G8_UNORM;
758 case DXGI_FORMAT_P010:
759 case DXGI_FORMAT_P016:
761 formats[0] = DXGI_FORMAT_R16_UNORM;
762 formats[1] = DXGI_FORMAT_R16G16_UNORM;
764 case DXGI_FORMAT_Y210:
766 formats[0] = DXGI_FORMAT_R16G16B16A16_UNORM;
768 case DXGI_FORMAT_Y410:
770 formats[0] = DXGI_FORMAT_R10G10B10A2_UNORM;
773 g_assert_not_reached ();
777 if ((priv->desc.BindFlags & D3D11_BIND_SHADER_RESOURCE) ==
778 D3D11_BIND_SHADER_RESOURCE) {
779 resource_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
780 resource_desc.Texture2D.MipLevels = 1;
782 for (i = 0; i < num_views; i++) {
783 resource_desc.Format = formats[i];
784 hr = device_handle->CreateShaderResourceView (priv->texture,
785 &resource_desc, &priv->shader_resource_view[i]);
787 if (!gst_d3d11_result (hr, mem->device)) {
788 GST_ERROR_OBJECT (GST_MEMORY_CAST (mem)->allocator,
789 "Failed to create %dth resource view (0x%x)", i, (guint) hr);
794 priv->num_shader_resource_views = num_views;
802 for (i = 0; i < num_views; i++)
803 GST_D3D11_CLEAR_COM (priv->shader_resource_view[i]);
805 priv->num_shader_resource_views = 0;
811 gst_d3d11_memory_ensure_shader_resource_view (GstD3D11Memory * mem)
813 GstD3D11MemoryPrivate *priv = mem->priv;
814 gboolean ret = FALSE;
816 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
819 if (!(priv->desc.BindFlags & D3D11_BIND_SHADER_RESOURCE)) {
820 GST_LOG_OBJECT (GST_MEMORY_CAST (mem)->allocator,
821 "Need BindFlags, current flag 0x%x", priv->desc.BindFlags);
825 GST_D3D11_MEMORY_LOCK (mem);
826 if (priv->num_shader_resource_views) {
831 ret = create_shader_resource_views (mem);
834 GST_D3D11_MEMORY_UNLOCK (mem);
840 * gst_d3d11_memory_get_shader_resource_view_size:
841 * @mem: a #GstD3D11Memory
843 * Returns: the number of ID3D11ShaderResourceView that can be used
844 * for processing GPU operation with @mem
849 gst_d3d11_memory_get_shader_resource_view_size (GstD3D11Memory * mem)
851 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), 0);
853 if (!gst_d3d11_memory_ensure_shader_resource_view (mem))
856 return mem->priv->num_shader_resource_views;
860 * gst_d3d11_memory_get_shader_resource_view:
861 * @mem: a #GstD3D11Memory
862 * @index: the index of the ID3D11ShaderResourceView
864 * Returns: (transfer none) (nullable): a pointer to the
865 * ID3D11ShaderResourceView or %NULL if ID3D11ShaderResourceView is unavailable
870 ID3D11ShaderResourceView *
871 gst_d3d11_memory_get_shader_resource_view (GstD3D11Memory * mem, guint index)
873 GstD3D11MemoryPrivate *priv;
875 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
877 if (!gst_d3d11_memory_ensure_shader_resource_view (mem))
882 if (index >= priv->num_shader_resource_views) {
883 GST_ERROR ("Invalid SRV index %d", index);
887 return priv->shader_resource_view[index];
891 create_render_target_views (GstD3D11Memory * mem)
893 GstD3D11MemoryPrivate *priv = mem->priv;
897 ID3D11Device *device_handle;
898 D3D11_RENDER_TARGET_VIEW_DESC render_desc;
899 DXGI_FORMAT formats[GST_VIDEO_MAX_PLANES] = { DXGI_FORMAT_UNKNOWN, };
901 memset (&render_desc, 0, sizeof (D3D11_RENDER_TARGET_VIEW_DESC));
903 device_handle = gst_d3d11_device_get_device_handle (mem->device);
905 switch (priv->desc.Format) {
906 case DXGI_FORMAT_B8G8R8A8_UNORM:
907 case DXGI_FORMAT_R8G8B8A8_UNORM:
908 case DXGI_FORMAT_R10G10B10A2_UNORM:
909 case DXGI_FORMAT_R8_UNORM:
910 case DXGI_FORMAT_R8G8_UNORM:
911 case DXGI_FORMAT_R16_UNORM:
912 case DXGI_FORMAT_R16G16_UNORM:
914 formats[0] = priv->desc.Format;
916 case DXGI_FORMAT_AYUV:
918 formats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
920 case DXGI_FORMAT_NV12:
922 formats[0] = DXGI_FORMAT_R8_UNORM;
923 formats[1] = DXGI_FORMAT_R8G8_UNORM;
925 case DXGI_FORMAT_P010:
926 case DXGI_FORMAT_P016:
928 formats[0] = DXGI_FORMAT_R16_UNORM;
929 formats[1] = DXGI_FORMAT_R16G16_UNORM;
932 g_assert_not_reached ();
936 if ((priv->desc.BindFlags & D3D11_BIND_RENDER_TARGET) ==
937 D3D11_BIND_RENDER_TARGET) {
938 render_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
939 render_desc.Texture2D.MipSlice = 0;
941 for (i = 0; i < num_views; i++) {
942 render_desc.Format = formats[i];
944 hr = device_handle->CreateRenderTargetView (priv->texture, &render_desc,
945 &priv->render_target_view[i]);
946 if (!gst_d3d11_result (hr, mem->device)) {
947 GST_ERROR_OBJECT (GST_MEMORY_CAST (mem)->allocator,
948 "Failed to create %dth render target view (0x%x)", i, (guint) hr);
953 priv->num_render_target_views = num_views;
961 for (i = 0; i < num_views; i++)
962 GST_D3D11_CLEAR_COM (priv->render_target_view[i]);
964 priv->num_render_target_views = 0;
970 gst_d3d11_memory_ensure_render_target_view (GstD3D11Memory * mem)
972 GstD3D11MemoryPrivate *priv = mem->priv;
973 gboolean ret = FALSE;
975 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
978 if (!(priv->desc.BindFlags & D3D11_BIND_RENDER_TARGET)) {
979 GST_WARNING_OBJECT (GST_MEMORY_CAST (mem)->allocator,
980 "Need BindFlags, current flag 0x%x", priv->desc.BindFlags);
984 GST_D3D11_MEMORY_LOCK (mem);
985 if (priv->num_render_target_views) {
990 ret = create_render_target_views (mem);
993 GST_D3D11_MEMORY_UNLOCK (mem);
999 * gst_d3d11_memory_get_render_target_view_size:
1000 * @mem: a #GstD3D11Memory
1002 * Returns: the number of ID3D11RenderTargetView that can be used
1003 * for processing GPU operation with @mem
1008 gst_d3d11_memory_get_render_target_view_size (GstD3D11Memory * mem)
1010 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), 0);
1012 if (!gst_d3d11_memory_ensure_render_target_view (mem))
1015 return mem->priv->num_render_target_views;
1019 * gst_d3d11_memory_get_render_target_view:
1020 * @mem: a #GstD3D11Memory
1021 * @index: the index of the ID3D11RenderTargetView
1023 * Returns: (transfer none) (nullable): a pointer to the
1024 * ID3D11RenderTargetView or %NULL if ID3D11RenderTargetView is unavailable
1029 ID3D11RenderTargetView *
1030 gst_d3d11_memory_get_render_target_view (GstD3D11Memory * mem, guint index)
1032 GstD3D11MemoryPrivate *priv;
1034 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1036 if (!gst_d3d11_memory_ensure_render_target_view (mem))
1041 if (index >= priv->num_render_target_views) {
1042 GST_ERROR ("Invalid RTV index %d", index);
1046 return priv->render_target_view[index];
1050 gst_d3d11_memory_ensure_decoder_output_view (GstD3D11Memory * mem,
1051 ID3D11VideoDevice * video_device, GUID * decoder_profile)
1053 GstD3D11MemoryPrivate *dmem_priv = mem->priv;
1054 GstD3D11Allocator *allocator;
1055 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC desc;
1057 gboolean ret = FALSE;
1059 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
1062 allocator = GST_D3D11_ALLOCATOR (GST_MEMORY_CAST (mem)->allocator);
1064 if (!(dmem_priv->desc.BindFlags & D3D11_BIND_DECODER)) {
1065 GST_LOG_OBJECT (allocator,
1066 "Need BindFlags, current flag 0x%x", dmem_priv->desc.BindFlags);
1070 GST_D3D11_MEMORY_LOCK (mem);
1071 if (dmem_priv->decoder_output_view) {
1072 dmem_priv->decoder_output_view->GetDesc (&desc);
1073 if (IsEqualGUID (desc.DecodeProfile, *decoder_profile)) {
1076 /* Shouldn't happen, but try again anyway */
1077 GST_WARNING_OBJECT (allocator,
1078 "Existing view has different decoder profile");
1079 GST_D3D11_CLEAR_COM (dmem_priv->decoder_output_view);
1083 if (dmem_priv->decoder_output_view)
1086 desc.DecodeProfile = *decoder_profile;
1087 desc.ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D;
1088 desc.Texture2D.ArraySlice = dmem_priv->subresource_index;
1090 hr = video_device->CreateVideoDecoderOutputView (dmem_priv->texture, &desc,
1091 &dmem_priv->decoder_output_view);
1092 if (!gst_d3d11_result (hr, mem->device)) {
1093 GST_ERROR_OBJECT (allocator,
1094 "Could not create decoder output view, hr: 0x%x", (guint) hr);
1102 GST_D3D11_MEMORY_UNLOCK (mem);
1108 * gst_d3d11_memory_get_decoder_output_view:
1109 * @mem: a #GstD3D11Memory
1111 * Returns: (transfer none) (nullable): a pointer to the
1112 * ID3D11VideoDecoderOutputView or %NULL if ID3D11VideoDecoderOutputView is
1117 ID3D11VideoDecoderOutputView *
1118 gst_d3d11_memory_get_decoder_output_view (GstD3D11Memory * mem,
1119 ID3D11VideoDevice * video_device, GUID * decoder_profile)
1121 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1122 g_return_val_if_fail (video_device != NULL, NULL);
1123 g_return_val_if_fail (decoder_profile != NULL, NULL);
1125 if (!gst_d3d11_memory_ensure_decoder_output_view (mem,
1126 video_device, decoder_profile))
1129 return mem->priv->decoder_output_view;
1133 check_bind_flags_for_processor_input_view (guint bind_flags)
1135 static const guint compatible_flags = (D3D11_BIND_DECODER |
1136 D3D11_BIND_VIDEO_ENCODER | D3D11_BIND_RENDER_TARGET |
1137 D3D11_BIND_UNORDERED_ACCESS);
1139 if (bind_flags == 0)
1142 if ((bind_flags & compatible_flags) != 0)
1149 gst_d3d11_memory_ensure_processor_input_view (GstD3D11Memory * mem,
1150 ID3D11VideoDevice * video_device,
1151 ID3D11VideoProcessorEnumerator * enumerator)
1153 GstD3D11MemoryPrivate *dmem_priv = mem->priv;
1154 GstD3D11Allocator *allocator;
1155 D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC desc;
1157 gboolean ret = FALSE;
1159 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
1162 allocator = GST_D3D11_ALLOCATOR (GST_MEMORY_CAST (mem)->allocator);
1164 if (!check_bind_flags_for_processor_input_view (dmem_priv->desc.BindFlags)) {
1165 GST_LOG_OBJECT (allocator,
1166 "Need BindFlags, current flag 0x%x", dmem_priv->desc.BindFlags);
1170 GST_D3D11_MEMORY_LOCK (mem);
1171 if (dmem_priv->processor_input_view)
1175 desc.ViewDimension = D3D11_VPIV_DIMENSION_TEXTURE2D;
1176 desc.Texture2D.MipSlice = 0;
1177 desc.Texture2D.ArraySlice = dmem_priv->subresource_index;
1179 hr = video_device->CreateVideoProcessorInputView (dmem_priv->texture,
1180 enumerator, &desc, &dmem_priv->processor_input_view);
1181 if (!gst_d3d11_result (hr, mem->device)) {
1182 GST_ERROR_OBJECT (allocator,
1183 "Could not create processor input view, hr: 0x%x", (guint) hr);
1191 GST_D3D11_MEMORY_UNLOCK (mem);
1197 * gst_d3d11_memory_get_processor_input_view:
1198 * @mem: a #GstD3D11Memory
1199 * @video_device: a #ID3D11VideoDevice
1200 * @enumerator: a #ID3D11VideoProcessorEnumerator
1202 * Returns: (transfer none) (nullable): a pointer to the
1203 * ID3D11VideoProcessorInputView or %NULL if ID3D11VideoProcessorInputView is
1208 ID3D11VideoProcessorInputView *
1209 gst_d3d11_memory_get_processor_input_view (GstD3D11Memory * mem,
1210 ID3D11VideoDevice * video_device,
1211 ID3D11VideoProcessorEnumerator * enumerator)
1213 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1214 g_return_val_if_fail (video_device != NULL, NULL);
1215 g_return_val_if_fail (enumerator != NULL, NULL);
1217 if (!gst_d3d11_memory_ensure_processor_input_view (mem, video_device,
1221 return mem->priv->processor_input_view;
1225 gst_d3d11_memory_ensure_processor_output_view (GstD3D11Memory * mem,
1226 ID3D11VideoDevice * video_device,
1227 ID3D11VideoProcessorEnumerator * enumerator)
1229 GstD3D11MemoryPrivate *priv = mem->priv;
1230 GstD3D11Allocator *allocator;
1231 D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC desc;
1235 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
1238 memset (&desc, 0, sizeof (D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC));
1240 allocator = GST_D3D11_ALLOCATOR (GST_MEMORY_CAST (mem)->allocator);
1242 if (!(priv->desc.BindFlags & D3D11_BIND_RENDER_TARGET)) {
1243 GST_LOG_OBJECT (allocator,
1244 "Need BindFlags, current flag 0x%x", priv->desc.BindFlags);
1248 /* FIXME: texture array should be supported at some point */
1249 if (priv->subresource_index != 0) {
1250 GST_FIXME_OBJECT (allocator,
1251 "Texture array is not suppoted for processor output view");
1255 GST_D3D11_MEMORY_LOCK (mem);
1256 if (priv->processor_output_view)
1259 desc.ViewDimension = D3D11_VPOV_DIMENSION_TEXTURE2D;
1260 desc.Texture2D.MipSlice = 0;
1262 hr = video_device->CreateVideoProcessorOutputView (priv->texture,
1263 enumerator, &desc, &priv->processor_output_view);
1264 if (!gst_d3d11_result (hr, mem->device)) {
1265 GST_ERROR_OBJECT (allocator,
1266 "Could not create processor input view, hr: 0x%x", (guint) hr);
1274 GST_D3D11_MEMORY_UNLOCK (mem);
1280 * gst_d3d11_memory_get_processor_output_view:
1281 * @mem: a #GstD3D11Memory
1282 * @video_device: a #ID3D11VideoDevice
1283 * @enumerator: a #ID3D11VideoProcessorEnumerator
1285 * Returns: (transfer none) (nullable): a pointer to the
1286 * ID3D11VideoProcessorOutputView or %NULL if ID3D11VideoProcessorOutputView is
1291 ID3D11VideoProcessorOutputView *
1292 gst_d3d11_memory_get_processor_output_view (GstD3D11Memory * mem,
1293 ID3D11VideoDevice * video_device,
1294 ID3D11VideoProcessorEnumerator * enumerator)
1296 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1297 g_return_val_if_fail (video_device != NULL, NULL);
1298 g_return_val_if_fail (enumerator != NULL, NULL);
1300 if (!gst_d3d11_memory_ensure_processor_output_view (mem, video_device,
1304 return mem->priv->processor_output_view;
1307 /* GstD3D11Allocator */
1308 struct _GstD3D11AllocatorPrivate
1310 GstMemoryCopyFunction fallback_copy;
1313 #define gst_d3d11_allocator_parent_class alloc_parent_class
1314 G_DEFINE_TYPE_WITH_PRIVATE (GstD3D11Allocator,
1315 gst_d3d11_allocator, GST_TYPE_ALLOCATOR);
1317 static GstMemory *gst_d3d11_allocator_dummy_alloc (GstAllocator * allocator,
1318 gsize size, GstAllocationParams * params);
1319 static GstMemory *gst_d3d11_allocator_alloc_internal (GstD3D11Allocator * self,
1320 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc);
1321 static void gst_d3d11_allocator_free (GstAllocator * allocator,
1325 gst_d3d11_allocator_class_init (GstD3D11AllocatorClass * klass)
1327 GstAllocatorClass *allocator_class = GST_ALLOCATOR_CLASS (klass);
1329 allocator_class->alloc = gst_d3d11_allocator_dummy_alloc;
1330 allocator_class->free = gst_d3d11_allocator_free;
1334 gst_d3d11_memory_copy (GstMemory * mem, gssize offset, gssize size)
1336 GstD3D11Allocator *alloc = GST_D3D11_ALLOCATOR (mem->allocator);
1337 GstD3D11AllocatorPrivate *priv = alloc->priv;
1338 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
1339 GstD3D11Memory *copy_dmem;
1340 GstD3D11Device *device = dmem->device;
1341 ID3D11Device *device_handle = gst_d3d11_device_get_device_handle (device);
1342 ID3D11DeviceContext *device_context =
1343 gst_d3d11_device_get_device_context_handle (device);
1344 D3D11_TEXTURE2D_DESC dst_desc = { 0, };
1345 D3D11_TEXTURE2D_DESC src_desc = { 0, };
1346 GstMemory *copy = NULL;
1349 UINT bind_flags = 0;
1350 UINT supported_flags = 0;
1352 if (dmem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
1353 return priv->fallback_copy (mem, offset, size);
1355 /* non-zero offset or different size is not supported */
1356 if (offset != 0 || (size != -1 && (gsize) size != mem->size)) {
1357 GST_DEBUG_OBJECT (alloc, "Different size/offset, try fallback copy");
1358 return priv->fallback_copy (mem, offset, size);
1361 gst_d3d11_device_lock (device);
1362 if (!gst_memory_map (mem, &info,
1363 (GstMapFlags) (GST_MAP_READ | GST_MAP_D3D11))) {
1364 gst_d3d11_device_unlock (device);
1366 GST_WARNING_OBJECT (alloc, "Failed to map memory, try fallback copy");
1368 return priv->fallback_copy (mem, offset, size);
1371 dmem->priv->texture->GetDesc (&src_desc);
1372 dst_desc.Width = src_desc.Width;
1373 dst_desc.Height = src_desc.Height;
1374 dst_desc.MipLevels = 1;
1375 dst_desc.Format = src_desc.Format;
1376 dst_desc.SampleDesc.Count = 1;
1377 dst_desc.ArraySize = 1;
1378 dst_desc.Usage = D3D11_USAGE_DEFAULT;
1380 /* If supported, use bind flags for SRV/RTV */
1381 hr = device_handle->CheckFormatSupport (src_desc.Format, &supported_flags);
1382 if (gst_d3d11_result (hr, device)) {
1383 if ((supported_flags & D3D11_FORMAT_SUPPORT_SHADER_SAMPLE) ==
1384 D3D11_FORMAT_SUPPORT_SHADER_SAMPLE) {
1385 bind_flags |= D3D11_BIND_SHADER_RESOURCE;
1388 if ((supported_flags & D3D11_FORMAT_SUPPORT_RENDER_TARGET) ==
1389 D3D11_FORMAT_SUPPORT_RENDER_TARGET) {
1390 bind_flags |= D3D11_BIND_RENDER_TARGET;
1394 copy = gst_d3d11_allocator_alloc_internal (alloc, device, &dst_desc);
1396 gst_memory_unmap (mem, &info);
1397 gst_d3d11_device_unlock (device);
1399 GST_WARNING_OBJECT (alloc,
1400 "Failed to allocate new d3d11 map memory, try fallback copy");
1402 return priv->fallback_copy (mem, offset, size);
1405 copy_dmem = GST_D3D11_MEMORY_CAST (copy);
1406 device_context->CopySubresourceRegion (copy_dmem->priv->texture, 0, 0, 0, 0,
1407 dmem->priv->texture, dmem->priv->subresource_index, NULL);
1408 copy->maxsize = copy->size = mem->maxsize;
1409 gst_memory_unmap (mem, &info);
1410 gst_d3d11_device_unlock (device);
1412 /* newly allocated memory holds valid image data. We need download this
1413 * pixel data into staging memory for CPU access */
1414 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
1420 gst_d3d11_allocator_init (GstD3D11Allocator * allocator)
1422 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
1423 GstD3D11AllocatorPrivate *priv;
1425 priv = allocator->priv = (GstD3D11AllocatorPrivate *)
1426 gst_d3d11_allocator_get_instance_private (allocator);
1428 alloc->mem_type = GST_D3D11_MEMORY_NAME;
1429 alloc->mem_map_full = gst_d3d11_memory_map_full;
1430 alloc->mem_unmap_full = gst_d3d11_memory_unmap_full;
1431 alloc->mem_share = gst_d3d11_memory_share;
1433 /* Store pointer to default mem_copy method for fallback copy */
1434 priv->fallback_copy = alloc->mem_copy;
1435 alloc->mem_copy = gst_d3d11_memory_copy;
1437 GST_OBJECT_FLAG_SET (alloc, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
1441 gst_d3d11_allocator_dummy_alloc (GstAllocator * allocator, gsize size,
1442 GstAllocationParams * params)
1444 g_return_val_if_reached (NULL);
1448 gst_d3d11_allocator_free (GstAllocator * allocator, GstMemory * mem)
1450 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
1451 GstD3D11MemoryPrivate *dmem_priv = dmem->priv;
1454 GST_LOG_OBJECT (allocator, "Free memory %p", mem);
1456 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
1457 GST_D3D11_CLEAR_COM (dmem_priv->render_target_view[i]);
1458 GST_D3D11_CLEAR_COM (dmem_priv->shader_resource_view[i]);
1461 GST_D3D11_CLEAR_COM (dmem_priv->decoder_output_view);
1462 GST_D3D11_CLEAR_COM (dmem_priv->processor_input_view);
1463 GST_D3D11_CLEAR_COM (dmem_priv->processor_output_view);
1464 GST_D3D11_CLEAR_COM (dmem_priv->texture);
1465 GST_D3D11_CLEAR_COM (dmem_priv->staging);
1466 GST_D3D11_CLEAR_COM (dmem_priv->buffer);
1468 gst_clear_object (&dmem->device);
1469 g_mutex_clear (&dmem_priv->lock);
1470 g_free (dmem->priv);
1475 gst_d3d11_allocator_alloc_wrapped (GstD3D11Allocator * self,
1476 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc,
1477 ID3D11Texture2D * texture)
1479 GstD3D11Memory *mem;
1481 mem = g_new0 (GstD3D11Memory, 1);
1482 mem->priv = g_new0 (GstD3D11MemoryPrivate, 1);
1484 gst_memory_init (GST_MEMORY_CAST (mem),
1485 (GstMemoryFlags) 0, GST_ALLOCATOR_CAST (self), NULL, 0, 0, 0, 0);
1486 g_mutex_init (&mem->priv->lock);
1487 mem->priv->texture = texture;
1488 mem->priv->desc = *desc;
1489 mem->priv->native_type = GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D;
1490 mem->device = (GstD3D11Device *) gst_object_ref (device);
1492 /* This is staging texture as well */
1493 if (desc->Usage == D3D11_USAGE_STAGING) {
1494 mem->priv->staging = texture;
1498 return GST_MEMORY_CAST (mem);
1502 gst_d3d11_allocator_alloc_internal (GstD3D11Allocator * self,
1503 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc)
1505 ID3D11Texture2D *texture = NULL;
1506 ID3D11Device *device_handle;
1509 device_handle = gst_d3d11_device_get_device_handle (device);
1511 hr = device_handle->CreateTexture2D (desc, NULL, &texture);
1512 if (!gst_d3d11_result (hr, device)) {
1513 GST_ERROR_OBJECT (self, "Couldn't create texture");
1517 return gst_d3d11_allocator_alloc_wrapped (self, device, desc, texture);
1521 * gst_d3d11_allocator_alloc:
1522 * @allocator: a #GstD3D11Allocator
1523 * @device: a #GstD3D11Device
1524 * @desc: a D3D11_TEXTURE2D_DESC struct
1526 * Returns: a newly allocated #GstD3D11Memory with given parameters.
1531 gst_d3d11_allocator_alloc (GstD3D11Allocator * allocator,
1532 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc)
1536 g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), NULL);
1537 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), NULL);
1538 g_return_val_if_fail (desc != NULL, NULL);
1540 mem = gst_d3d11_allocator_alloc_internal (allocator, device, desc);
1544 if (!gst_d3d11_memory_update_size (mem)) {
1545 GST_ERROR_OBJECT (allocator, "Failed to calculate size");
1546 gst_memory_unref (mem);
1554 * gst_d3d11_allocator_alloc_buffer:
1555 * @allocator: a #GstD3D11Allocator
1556 * @device: a #GstD3D11Device
1557 * @desc: a D3D11_BUFFER_DESC struct
1559 * Returns: a newly allocated #GstD3D11Memory with given parameters.
1564 gst_d3d11_allocator_alloc_buffer (GstD3D11Allocator * allocator,
1565 GstD3D11Device * device, const D3D11_BUFFER_DESC * desc)
1567 GstD3D11Memory *mem;
1568 ID3D11Buffer *buffer;
1569 ID3D11Device *device_handle;
1572 g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), nullptr);
1573 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), nullptr);
1574 g_return_val_if_fail (desc != nullptr, nullptr);
1576 if (desc->Usage != D3D11_USAGE_STAGING) {
1577 GST_FIXME_OBJECT (allocator, "Non staging buffer is not supported");
1581 device_handle = gst_d3d11_device_get_device_handle (device);
1583 hr = device_handle->CreateBuffer (desc, nullptr, &buffer);
1584 if (!gst_d3d11_result (hr, device)) {
1585 GST_ERROR_OBJECT (allocator, "Couldn't create buffer");
1589 mem = g_new0 (GstD3D11Memory, 1);
1590 mem->priv = g_new0 (GstD3D11MemoryPrivate, 1);
1592 gst_memory_init (GST_MEMORY_CAST (mem),
1593 (GstMemoryFlags) 0, GST_ALLOCATOR_CAST (allocator), nullptr, 0, 0, 0, 0);
1594 g_mutex_init (&mem->priv->lock);
1595 mem->priv->buffer = buffer;
1596 mem->priv->buffer_desc = *desc;
1597 mem->priv->native_type = GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER;
1598 mem->device = (GstD3D11Device *) gst_object_ref (device);
1600 GST_MEMORY_CAST (mem)->maxsize = GST_MEMORY_CAST (mem)->size =
1603 return GST_MEMORY_CAST (mem);
1607 gst_d3d11_allocator_set_active (GstD3D11Allocator * allocator, gboolean active)
1609 GstD3D11AllocatorClass *klass;
1611 g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), FALSE);
1613 klass = GST_D3D11_ALLOCATOR_GET_CLASS (allocator);
1614 if (klass->set_actvie)
1615 return klass->set_actvie (allocator, active);
1620 /* GstD3D11PoolAllocator */
1621 #define GST_D3D11_POOL_ALLOCATOR_LOCK(alloc) (g_rec_mutex_lock(&alloc->priv->lock))
1622 #define GST_D3D11_POOL_ALLOCATOR_UNLOCK(alloc) (g_rec_mutex_unlock(&alloc->priv->lock))
1623 #define GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING(alloc) (g_atomic_int_get (&alloc->priv->flushing))
1625 struct _GstD3D11PoolAllocatorPrivate
1627 /* parent texture when array typed memory is used */
1628 ID3D11Texture2D *texture;
1629 D3D11_TEXTURE2D_DESC desc;
1631 /* All below member variables are analogous to that of GstBufferPool */
1632 GstAtomicQueue *queue;
1635 /* This lock will protect all below variables apart from atomic ones
1636 * (identical to GstBufferPool::priv::rec_lock) */
1647 /* Calculated memory size, based on Direct3D11 staging texture map.
1648 * Note that, we cannot know the actually staging texture memory size prior
1649 * to map the staging texture because driver will likely require padding */
1653 static void gst_d3d11_pool_allocator_dispose (GObject * object);
1654 static void gst_d3d11_pool_allocator_finalize (GObject * object);
1657 gst_d3d11_pool_allocator_set_active (GstD3D11Allocator * allocator,
1660 static gboolean gst_d3d11_pool_allocator_start (GstD3D11PoolAllocator * self);
1661 static gboolean gst_d3d11_pool_allocator_stop (GstD3D11PoolAllocator * self);
1662 static gboolean gst_d3d11_memory_release (GstMiniObject * mini_object);
1664 #define gst_d3d11_pool_allocator_parent_class pool_alloc_parent_class
1665 G_DEFINE_TYPE_WITH_PRIVATE (GstD3D11PoolAllocator,
1666 gst_d3d11_pool_allocator, GST_TYPE_D3D11_ALLOCATOR);
1669 gst_d3d11_pool_allocator_class_init (GstD3D11PoolAllocatorClass * klass)
1671 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1672 GstD3D11AllocatorClass *d3d11alloc_class = GST_D3D11_ALLOCATOR_CLASS (klass);
1674 gobject_class->dispose = gst_d3d11_pool_allocator_dispose;
1675 gobject_class->finalize = gst_d3d11_pool_allocator_finalize;
1677 d3d11alloc_class->set_actvie = gst_d3d11_pool_allocator_set_active;
1681 gst_d3d11_pool_allocator_init (GstD3D11PoolAllocator * allocator)
1683 GstD3D11PoolAllocatorPrivate *priv;
1685 priv = allocator->priv = (GstD3D11PoolAllocatorPrivate *)
1686 gst_d3d11_pool_allocator_get_instance_private (allocator);
1687 g_rec_mutex_init (&priv->lock);
1689 priv->poll = gst_poll_new_timer ();
1690 priv->queue = gst_atomic_queue_new (16);
1692 priv->active = FALSE;
1693 priv->started = FALSE;
1695 /* 1 control write for flushing - the flush token */
1696 gst_poll_write_control (priv->poll);
1697 /* 1 control write for marking that we are not waiting for poll - the wait token */
1698 gst_poll_write_control (priv->poll);
1702 gst_d3d11_pool_allocator_dispose (GObject * object)
1704 GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (object);
1706 gst_clear_object (&self->device);
1708 G_OBJECT_CLASS (pool_alloc_parent_class)->dispose (object);
1712 gst_d3d11_pool_allocator_finalize (GObject * object)
1714 GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (object);
1715 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1717 GST_DEBUG_OBJECT (self, "Finalize");
1719 gst_d3d11_pool_allocator_stop (self);
1720 gst_atomic_queue_unref (priv->queue);
1721 gst_poll_free (priv->poll);
1722 g_rec_mutex_clear (&priv->lock);
1724 GST_D3D11_CLEAR_COM (priv->texture);
1726 G_OBJECT_CLASS (pool_alloc_parent_class)->finalize (object);
1730 gst_d3d11_pool_allocator_start (GstD3D11PoolAllocator * self)
1732 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1733 ID3D11Device *device_handle;
1741 if (priv->desc.ArraySize == 1) {
1742 priv->started = TRUE;
1746 device_handle = gst_d3d11_device_get_device_handle (self->device);
1748 if (!priv->texture) {
1749 hr = device_handle->CreateTexture2D (&priv->desc, NULL, &priv->texture);
1750 if (!gst_d3d11_result (hr, self->device)) {
1751 GST_ERROR_OBJECT (self, "Failed to allocate texture");
1756 /* Pre-allocate memory objects */
1757 for (i = 0; i < priv->desc.ArraySize; i++) {
1760 priv->texture->AddRef ();
1762 gst_d3d11_allocator_alloc_wrapped (GST_D3D11_ALLOCATOR_CAST
1763 (_d3d11_memory_allocator), self->device, &priv->desc, priv->texture);
1766 if (!gst_d3d11_memory_update_size (mem)) {
1767 GST_ERROR_OBJECT (self, "Failed to calculate memory size");
1768 gst_memory_unref (mem);
1772 priv->mem_size = mem->size;
1774 mem->size = mem->maxsize = priv->mem_size;
1777 GST_D3D11_MEMORY_CAST (mem)->priv->subresource_index = i;
1779 g_atomic_int_add (&priv->cur_mems, 1);
1780 gst_atomic_queue_push (priv->queue, mem);
1781 gst_poll_write_control (priv->poll);
1784 priv->started = TRUE;
1790 gst_d3d11_pool_allocator_do_set_flushing (GstD3D11PoolAllocator * self,
1793 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1795 if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self) == flushing)
1799 g_atomic_int_set (&priv->flushing, 1);
1800 /* Write the flush token to wake up any waiters */
1801 gst_poll_write_control (priv->poll);
1803 while (!gst_poll_read_control (priv->poll)) {
1804 if (errno == EWOULDBLOCK) {
1805 /* This should not really happen unless flushing and unflushing
1806 * happens on different threads. Let's wait a bit to get back flush
1807 * token from the thread that was setting it to flushing */
1811 /* Critical error but GstPoll already complained */
1816 g_atomic_int_set (&priv->flushing, 0);
1821 gst_d3d11_pool_allocator_set_active (GstD3D11Allocator * allocator,
1824 GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (allocator);
1825 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1827 GST_LOG_OBJECT (self, "active %d", active);
1829 GST_D3D11_POOL_ALLOCATOR_LOCK (self);
1830 /* just return if we are already in the right state */
1831 if (priv->active == active)
1835 if (!gst_d3d11_pool_allocator_start (self))
1838 /* flush_stop may release memory objects, setting to active to avoid running
1839 * do_stop while activating the pool */
1840 priv->active = TRUE;
1842 gst_d3d11_pool_allocator_do_set_flushing (self, FALSE);
1846 /* set to flushing first */
1847 gst_d3d11_pool_allocator_do_set_flushing (self, TRUE);
1849 /* when all memory objects are in the pool, free them. Else they will be
1850 * freed when they are released */
1851 outstanding = g_atomic_int_get (&priv->outstanding);
1852 GST_LOG_OBJECT (self, "outstanding memories %d, (in queue %d)",
1853 outstanding, gst_atomic_queue_length (priv->queue));
1854 if (outstanding == 0) {
1855 if (!gst_d3d11_pool_allocator_stop (self))
1859 priv->active = FALSE;
1862 GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1868 GST_DEBUG_OBJECT (self, "allocator was in the right state");
1869 GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1874 GST_ERROR_OBJECT (self, "start failed");
1875 GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1880 GST_ERROR_OBJECT (self, "stop failed");
1881 GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1887 gst_d3d11_pool_allocator_free_memory (GstD3D11PoolAllocator * self,
1890 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1892 g_atomic_int_add (&priv->cur_mems, -1);
1893 GST_LOG_OBJECT (self, "freeing memory %p (%u left)", mem, priv->cur_mems);
1895 GST_MINI_OBJECT_CAST (mem)->dispose = NULL;
1896 gst_memory_unref (mem);
1899 /* must be called with the lock */
1901 gst_d3d11_pool_allocator_clear_queue (GstD3D11PoolAllocator * self)
1903 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1906 GST_LOG_OBJECT (self, "Clearing queue");
1908 /* clear the pool */
1909 while ((memory = (GstMemory *) gst_atomic_queue_pop (priv->queue))) {
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 */
1917 /* Critical error but GstPoll already complained */
1921 gst_d3d11_pool_allocator_free_memory (self, memory);
1924 GST_LOG_OBJECT (self, "Clear done");
1926 return priv->cur_mems == 0;
1929 /* must be called with the lock */
1931 gst_d3d11_pool_allocator_stop (GstD3D11PoolAllocator * self)
1933 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1935 GST_DEBUG_OBJECT (self, "Stop");
1937 if (priv->started) {
1938 if (!gst_d3d11_pool_allocator_clear_queue (self))
1941 priv->started = FALSE;
1943 GST_DEBUG_OBJECT (self, "Wasn't started");
1950 dec_outstanding (GstD3D11PoolAllocator * self)
1952 if (g_atomic_int_dec_and_test (&self->priv->outstanding)) {
1953 /* all memory objects are returned to the pool, see if we need to free them */
1954 if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)) {
1955 /* take the lock so that set_active is not run concurrently */
1956 GST_D3D11_POOL_ALLOCATOR_LOCK (self);
1957 /* now that we have the lock, check if we have been de-activated with
1958 * outstanding buffers */
1959 if (!self->priv->active)
1960 gst_d3d11_pool_allocator_stop (self);
1962 GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1968 gst_d3d11_pool_allocator_release_memory (GstD3D11PoolAllocator * self,
1971 GST_LOG_OBJECT (self, "Released memory %p", mem);
1973 GST_MINI_OBJECT_CAST (mem)->dispose = NULL;
1974 mem->allocator = (GstAllocator *) gst_object_ref (_d3d11_memory_allocator);
1975 gst_object_unref (self);
1977 /* keep it around in our queue */
1978 gst_atomic_queue_push (self->priv->queue, mem);
1979 gst_poll_write_control (self->priv->poll);
1980 dec_outstanding (self);
1984 gst_d3d11_memory_release (GstMiniObject * mini_object)
1986 GstMemory *mem = GST_MEMORY_CAST (mini_object);
1987 GstD3D11PoolAllocator *alloc;
1989 g_assert (mem->allocator != NULL);
1991 if (!GST_IS_D3D11_POOL_ALLOCATOR (mem->allocator)) {
1992 GST_LOG_OBJECT (mem->allocator, "Not our memory, free");
1996 alloc = GST_D3D11_POOL_ALLOCATOR (mem->allocator);
1997 /* if flushing, free this memory */
1998 if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (alloc)) {
1999 GST_LOG_OBJECT (alloc, "allocator is flushing, free %p", mem);
2003 /* return the memory to the allocator */
2004 gst_memory_ref (mem);
2005 gst_d3d11_pool_allocator_release_memory (alloc, mem);
2010 static GstFlowReturn
2011 gst_d3d11_pool_allocator_alloc (GstD3D11PoolAllocator * self, GstMemory ** mem)
2013 GstD3D11PoolAllocatorPrivate *priv = self->priv;
2016 /* we allcates texture array during start */
2017 if (priv->desc.ArraySize > 1)
2018 return GST_FLOW_EOS;
2020 /* increment the allocation counter */
2021 g_atomic_int_add (&priv->cur_mems, 1);
2023 gst_d3d11_allocator_alloc_internal (GST_D3D11_ALLOCATOR_CAST
2024 (_d3d11_memory_allocator), self->device, &priv->desc);
2026 GST_ERROR_OBJECT (self, "Failed to allocate new memory");
2027 g_atomic_int_add (&priv->cur_mems, -1);
2028 return GST_FLOW_ERROR;
2031 if (!priv->mem_size) {
2032 if (!gst_d3d11_memory_update_size (new_mem)) {
2033 GST_ERROR_OBJECT (self, "Failed to calculate size");
2034 gst_memory_unref (new_mem);
2035 g_atomic_int_add (&priv->cur_mems, -1);
2037 return GST_FLOW_ERROR;
2040 priv->mem_size = new_mem->size;
2043 new_mem->size = new_mem->maxsize = priv->mem_size;
2050 static GstFlowReturn
2051 gst_d3d11_pool_allocator_acquire_memory_internal (GstD3D11PoolAllocator * self,
2052 GstMemory ** memory)
2054 GstFlowReturn result;
2055 GstD3D11PoolAllocatorPrivate *priv = self->priv;
2058 if (G_UNLIKELY (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)))
2061 /* try to get a memory from the queue */
2062 *memory = (GstMemory *) gst_atomic_queue_pop (priv->queue);
2063 if (G_LIKELY (*memory)) {
2064 while (!gst_poll_read_control (priv->poll)) {
2065 if (errno == EWOULDBLOCK) {
2066 /* We put the memory into the queue but did not finish writing control
2067 * yet, let's wait a bit and retry */
2071 /* Critical error but GstPoll already complained */
2075 result = GST_FLOW_OK;
2076 GST_LOG_OBJECT (self, "acquired memory %p", *memory);
2080 /* no memory, try to allocate some more */
2081 GST_LOG_OBJECT (self, "no memory, trying to allocate");
2082 result = gst_d3d11_pool_allocator_alloc (self, memory);
2083 if (G_LIKELY (result == GST_FLOW_OK))
2084 /* we have a memory, return it */
2087 if (G_UNLIKELY (result != GST_FLOW_EOS))
2088 /* something went wrong, return error */
2091 /* now we release the control socket, we wait for a memory release or
2093 if (!gst_poll_read_control (priv->poll)) {
2094 if (errno == EWOULDBLOCK) {
2095 /* This means that we have two threads trying to allocate memory
2096 * already, and the other one already got the wait token. This
2097 * means that we only have to wait for the poll now and not write the
2098 * token afterwards: we will be woken up once the other thread is
2099 * woken up and that one will write the wait token it removed */
2100 GST_LOG_OBJECT (self, "waiting for free memory or flushing");
2101 gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
2103 /* This is a critical error, GstPoll already gave a warning */
2104 result = GST_FLOW_ERROR;
2108 /* We're the first thread waiting, we got the wait token and have to
2109 * write it again later
2111 * We're a second thread and just consumed the flush token and block all
2112 * other threads, in which case we must not wait and give it back
2114 if (!GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)) {
2115 GST_LOG_OBJECT (self, "waiting for free memory or flushing");
2116 gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
2118 gst_poll_write_control (priv->poll);
2127 GST_DEBUG_OBJECT (self, "we are flushing");
2128 return GST_FLOW_FLUSHING;
2133 * gst_d3d11_pool_allocator_new:
2134 * @device: a #GstD3D11Device
2135 * @desc: a D3D11_TEXTURE2D_DESC for texture allocation
2137 * Creates a new #GstD3D11PoolAllocator instance.
2139 * Returns: (transfer full): a new #GstD3D11PoolAllocator instance
2141 GstD3D11PoolAllocator *
2142 gst_d3d11_pool_allocator_new (GstD3D11Device * device,
2143 const D3D11_TEXTURE2D_DESC * desc)
2145 GstD3D11PoolAllocator *self;
2147 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), NULL);
2148 g_return_val_if_fail (desc != NULL, NULL);
2150 gst_d3d11_memory_init_once ();
2152 self = (GstD3D11PoolAllocator *)
2153 g_object_new (GST_TYPE_D3D11_POOL_ALLOCATOR, NULL);
2154 gst_object_ref_sink (self);
2156 self->device = (GstD3D11Device *) gst_object_ref (device);
2157 self->priv->desc = *desc;
2163 * gst_d3d11_pool_allocator_acquire_memory:
2164 * @allocator: a #GstD3D11PoolAllocator
2165 * @memory: (transfer full): a #GstMemory
2167 * Acquires a #GstMemory from @allocator. @memory should point to a memory
2168 * location that can hold a pointer to the new #GstMemory.
2170 * Returns: a #GstFlowReturn such as %GST_FLOW_FLUSHING when the allocator is
2174 gst_d3d11_pool_allocator_acquire_memory (GstD3D11PoolAllocator * allocator,
2175 GstMemory ** memory)
2177 GstD3D11PoolAllocatorPrivate *priv;
2178 GstFlowReturn result;
2180 g_return_val_if_fail (GST_IS_D3D11_POOL_ALLOCATOR (allocator),
2182 g_return_val_if_fail (memory != NULL, GST_FLOW_ERROR);
2184 priv = allocator->priv;
2186 /* assume we'll have one more outstanding buffer we need to do that so
2187 * that concurrent set_active doesn't clear the buffers */
2188 g_atomic_int_inc (&priv->outstanding);
2189 result = gst_d3d11_pool_allocator_acquire_memory_internal (allocator, memory);
2191 if (result == GST_FLOW_OK) {
2192 GstMemory *mem = *memory;
2193 /* Replace default allocator with ours */
2194 gst_object_unref (mem->allocator);
2195 mem->allocator = (GstAllocator *) gst_object_ref (allocator);
2196 GST_MINI_OBJECT_CAST (mem)->dispose = gst_d3d11_memory_release;
2198 dec_outstanding (allocator);
2205 * gst_d3d11_pool_allocator_get_pool_size:
2206 * @allocator: a #GstD3D11PoolAllocator
2207 * @max_size: (out) (optional): the max size of pool
2208 * @outstanding_size: (out) (optional): the number of outstanding memory
2210 * Returns: %TRUE if the size of memory pool is known
2215 gst_d3d11_pool_allocator_get_pool_size (GstD3D11PoolAllocator * allocator,
2216 guint * max_size, guint * outstanding_size)
2218 GstD3D11PoolAllocatorPrivate *priv;
2220 g_return_val_if_fail (GST_IS_D3D11_POOL_ALLOCATOR (allocator), FALSE);
2222 priv = allocator->priv;
2225 if (priv->desc.ArraySize > 1) {
2226 *max_size = priv->desc.ArraySize;
2228 /* For non-texture-array memory, we don't have any limit yet */
2233 if (outstanding_size)
2234 *outstanding_size = g_atomic_int_get (&priv->outstanding);