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 static inline D3D11_MAP
277 gst_d3d11_map_flags_to_d3d11 (GstMapFlags flags)
279 if ((flags & GST_MAP_READWRITE) == GST_MAP_READWRITE)
280 return D3D11_MAP_READ_WRITE;
281 else if ((flags & GST_MAP_WRITE) == GST_MAP_WRITE)
282 return D3D11_MAP_WRITE;
283 else if ((flags & GST_MAP_READ) == GST_MAP_READ)
284 return D3D11_MAP_READ;
286 g_assert_not_reached ();
288 return D3D11_MAP_READ;
291 static ID3D11Texture2D *
292 gst_d3d11_allocate_staging_texture (GstD3D11Device * device,
293 const D3D11_TEXTURE2D_DESC * ref)
295 D3D11_TEXTURE2D_DESC desc = { 0, };
296 ID3D11Texture2D *texture = NULL;
297 ID3D11Device *device_handle = gst_d3d11_device_get_device_handle (device);
300 desc.Width = ref->Width;
301 desc.Height = ref->Height;
303 desc.Format = ref->Format;
304 desc.SampleDesc.Count = 1;
306 desc.Usage = D3D11_USAGE_STAGING;
307 desc.CPUAccessFlags = (D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE);
309 hr = device_handle->CreateTexture2D (&desc, NULL, &texture);
310 if (!gst_d3d11_result (hr, device)) {
311 GST_ERROR_OBJECT (device, "Failed to create texture");
318 /* Must be called with d3d11 device lock */
320 gst_d3d11_memory_map_cpu_access (GstD3D11Memory * dmem, D3D11_MAP map_type)
322 GstD3D11MemoryPrivate *priv = dmem->priv;
324 ID3D11DeviceContext *device_context =
325 gst_d3d11_device_get_device_context_handle (dmem->device);
327 hr = device_context->Map (priv->staging, 0, map_type, 0, &priv->map);
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);
338 /* Must be called with d3d11 device lock */
340 gst_d3d11_memory_upload (GstD3D11Memory * dmem)
342 GstD3D11MemoryPrivate *priv = dmem->priv;
343 ID3D11DeviceContext *device_context;
345 if (!priv->staging ||
346 !GST_MEMORY_FLAG_IS_SET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD))
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);
354 /* Must be called with d3d11 device lock */
356 gst_d3d11_memory_download (GstD3D11Memory * dmem)
358 GstD3D11MemoryPrivate *priv = dmem->priv;
359 ID3D11DeviceContext *device_context;
361 if (!priv->staging ||
362 !GST_MEMORY_FLAG_IS_SET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD))
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);
371 gst_d3d11_memory_map_full (GstMemory * mem, GstMapInfo * info, gsize maxsize)
373 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
374 GstD3D11MemoryPrivate *priv = dmem->priv;
375 GstMapFlags flags = info->flags;
378 gst_d3d11_device_lock (dmem->device);
379 GST_D3D11_MEMORY_LOCK (dmem);
381 memset (info->user_data, 0, sizeof (info->user_data));
382 info->user_data[0] = GUINT_TO_POINTER (dmem->priv->subresource_index);
384 if ((flags & GST_MAP_D3D11) == GST_MAP_D3D11) {
385 if (priv->native_type == GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER) {
386 /* FIXME: handle non-staging buffer */
387 g_assert (priv->buffer != nullptr);
390 gst_d3d11_memory_upload (dmem);
391 GST_MEMORY_FLAG_UNSET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD);
393 if ((flags & GST_MAP_WRITE) == GST_MAP_WRITE)
394 GST_MINI_OBJECT_FLAG_SET (dmem,
395 GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
397 g_assert (priv->texture != NULL);
403 if (priv->cpu_map_count == 0) {
406 /* FIXME: handle non-staging buffer */
407 if (priv->native_type == GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D) {
408 /* Allocate staging texture for CPU access */
409 if (!priv->staging) {
410 priv->staging = gst_d3d11_allocate_staging_texture (dmem->device,
412 if (!priv->staging) {
413 GST_ERROR_OBJECT (mem->allocator, "Couldn't create staging texture");
417 /* first memory, always need download to staging */
418 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
421 gst_d3d11_memory_download (dmem);
424 map_type = gst_d3d11_map_flags_to_d3d11 (flags);
425 if (!gst_d3d11_memory_map_cpu_access (dmem, map_type)) {
426 GST_ERROR_OBJECT (mem->allocator, "Couldn't map staging texture");
431 if ((flags & GST_MAP_WRITE) == GST_MAP_WRITE) {
432 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD);
435 GST_MEMORY_FLAG_UNSET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
437 priv->cpu_map_count++;
438 ret = dmem->priv->map.pData;
441 GST_D3D11_MEMORY_UNLOCK (dmem);
442 gst_d3d11_device_unlock (dmem->device);
447 /* Must be called with d3d11 device lock */
449 gst_d3d11_memory_unmap_cpu_access (GstD3D11Memory * dmem)
451 GstD3D11MemoryPrivate *priv = dmem->priv;
452 ID3D11DeviceContext *device_context =
453 gst_d3d11_device_get_device_context_handle (dmem->device);
455 device_context->Unmap (priv->staging, 0);
459 gst_d3d11_memory_unmap_full (GstMemory * mem, GstMapInfo * info)
461 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
462 GstD3D11MemoryPrivate *priv = dmem->priv;
464 gst_d3d11_device_lock (dmem->device);
465 GST_D3D11_MEMORY_LOCK (dmem);
467 if ((info->flags & GST_MAP_D3D11) == GST_MAP_D3D11) {
468 if ((info->flags & GST_MAP_WRITE) == GST_MAP_WRITE)
469 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
474 if ((info->flags & GST_MAP_WRITE) == GST_MAP_WRITE)
475 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD);
477 priv->cpu_map_count--;
478 if (priv->cpu_map_count > 0)
481 gst_d3d11_memory_unmap_cpu_access (dmem);
484 GST_D3D11_MEMORY_UNLOCK (dmem);
485 gst_d3d11_device_unlock (dmem->device);
489 gst_d3d11_memory_share (GstMemory * mem, gssize offset, gssize size)
496 gst_d3d11_memory_update_size (GstMemory * mem)
498 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
499 GstD3D11MemoryPrivate *priv = dmem->priv;
500 gsize offset[GST_VIDEO_MAX_PLANES];
501 gint stride[GST_VIDEO_MAX_PLANES];
503 D3D11_TEXTURE2D_DESC *desc = &priv->desc;
504 gboolean ret = FALSE;
506 if (!priv->staging) {
507 priv->staging = gst_d3d11_allocate_staging_texture (dmem->device,
509 if (!priv->staging) {
510 GST_ERROR_OBJECT (mem->allocator, "Couldn't create staging texture");
514 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
517 gst_d3d11_device_lock (dmem->device);
518 if (!gst_d3d11_memory_map_cpu_access (dmem, D3D11_MAP_READ_WRITE)) {
519 GST_ERROR_OBJECT (mem->allocator, "Couldn't map staging texture");
523 gst_d3d11_memory_unmap_cpu_access (dmem);
525 if (!gst_d3d11_dxgi_format_get_size (desc->Format, desc->Width, desc->Height,
526 priv->map.RowPitch, offset, stride, &size)) {
527 GST_ERROR_OBJECT (mem->allocator, "Couldn't calculate memory size");
531 mem->maxsize = mem->size = size;
535 gst_d3d11_device_unlock (dmem->device);
540 * gst_is_d3d11_memory:
543 * Returns: whether @mem is a #GstD3D11Memory
548 gst_is_d3d11_memory (GstMemory * mem)
550 return mem != NULL && mem->allocator != NULL &&
551 (GST_IS_D3D11_ALLOCATOR (mem->allocator) ||
552 GST_IS_D3D11_POOL_ALLOCATOR (mem->allocator));
556 * gst_d3d11_memory_get_native_type:
557 * @mem: a #GstD3D11Memory
559 * Returns: a #GstD3D11MemoryNativeType
563 GstD3D11MemoryNativeType
564 gst_d3d11_memory_get_native_type (GstD3D11Memory * mem)
566 if (!gst_is_d3d11_memory (GST_MEMORY_CAST (mem)))
567 return GST_D3D11_MEMORY_NATIVE_TYPE_INVALID;
569 return mem->priv->native_type;
573 * gst_d3d11_memory_init_once:
575 * Initializes the Direct3D11 Texture allocator. It is safe to call
576 * this function multiple times. This must be called before any other
577 * GstD3D11Memory operation.
582 gst_d3d11_memory_init_once (void)
584 static gsize _init = 0;
586 if (g_once_init_enter (&_init)) {
588 GST_DEBUG_CATEGORY_INIT (gst_d3d11_allocator_debug, "d3d11allocator", 0,
589 "Direct3D11 Texture Allocator");
591 _d3d11_memory_allocator =
592 (GstAllocator *) g_object_new (GST_TYPE_D3D11_ALLOCATOR, NULL);
593 gst_object_ref_sink (_d3d11_memory_allocator);
595 gst_allocator_register (GST_D3D11_MEMORY_NAME, _d3d11_memory_allocator);
596 g_once_init_leave (&_init, 1);
601 * gst_d3d11_memory_get_resource_handle:
602 * @mem: a #GstD3D11Memory
604 * Returns: (transfer none): a ID3D11Resource handle. Caller must not release
610 gst_d3d11_memory_get_resource_handle (GstD3D11Memory * mem)
612 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
614 switch (mem->priv->native_type) {
615 case GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER:
616 return mem->priv->buffer;
617 case GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D:
618 return mem->priv->texture;
627 * gst_d3d11_memory_get_subresource_index:
628 * @mem: a #GstD3D11Memory
630 * Returns: subresource index corresponding to @mem.
635 gst_d3d11_memory_get_subresource_index (GstD3D11Memory * mem)
637 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), 0);
639 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
642 return mem->priv->subresource_index;
646 * gst_d3d11_memory_get_texture_desc:
647 * @mem: a #GstD3D11Memory
648 * @desc: (out): a D3D11_TEXTURE2D_DESC
650 * Fill @desc with D3D11_TEXTURE2D_DESC for ID3D11Texture2D
652 * Returns: %TRUE if successeed
657 gst_d3d11_memory_get_texture_desc (GstD3D11Memory * mem,
658 D3D11_TEXTURE2D_DESC * desc)
660 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), FALSE);
661 g_return_val_if_fail (desc != NULL, FALSE);
663 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
666 *desc = mem->priv->desc;
672 * gst_d3d11_memory_get_buffer_desc:
673 * @mem: a #GstD3D11Memory
674 * @desc: (out): a D3D11_BUFFER_DESC
676 * Fill @desc with D3D11_BUFFER_DESC for ID3D11Buffer
678 * Returns: %TRUE if successeed
683 gst_d3d11_memory_get_buffer_desc (GstD3D11Memory * mem,
684 D3D11_BUFFER_DESC * desc)
686 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), FALSE);
687 g_return_val_if_fail (desc != NULL, FALSE);
689 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER)
692 *desc = mem->priv->buffer_desc;
698 * gst_d3d11_memory_get_resource_stride:
699 * @mem: a #GstD3D11Memory
700 * @stride: (out): stride of resource
702 * Returns: %TRUE if successeed
707 gst_d3d11_memory_get_resource_stride (GstD3D11Memory * mem, guint * stride)
709 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), FALSE);
710 g_return_val_if_fail (stride != NULL, FALSE);
712 *stride = mem->priv->map.RowPitch;
718 create_shader_resource_views (GstD3D11Memory * mem)
720 GstD3D11MemoryPrivate *priv = mem->priv;
724 ID3D11Device *device_handle;
725 D3D11_SHADER_RESOURCE_VIEW_DESC resource_desc;
726 DXGI_FORMAT formats[GST_VIDEO_MAX_PLANES] = { DXGI_FORMAT_UNKNOWN, };
728 memset (&resource_desc, 0, sizeof (D3D11_SHADER_RESOURCE_VIEW_DESC));
730 device_handle = gst_d3d11_device_get_device_handle (mem->device);
732 num_views = gst_d3d11_dxgi_format_get_resource_format (priv->desc.Format,
735 GST_ERROR_OBJECT (GST_MEMORY_CAST (mem)->allocator,
736 "Unknown resource formats for DXGI format %s (%d)",
737 gst_d3d11_dxgi_format_to_string (priv->desc.Format), priv->desc.Format);
741 if ((priv->desc.BindFlags & D3D11_BIND_SHADER_RESOURCE) != 0) {
742 resource_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
743 resource_desc.Texture2D.MipLevels = 1;
745 for (i = 0; i < num_views; i++) {
746 resource_desc.Format = formats[i];
747 hr = device_handle->CreateShaderResourceView (priv->texture,
748 &resource_desc, &priv->shader_resource_view[i]);
750 if (!gst_d3d11_result (hr, mem->device)) {
751 GST_ERROR_OBJECT (GST_MEMORY_CAST (mem)->allocator,
752 "Failed to create resource DXGI format %s (%d) for plane %d"
753 " view (0x%x)", gst_d3d11_dxgi_format_to_string (formats[i]),
754 formats[i], i, (guint) hr);
759 priv->num_shader_resource_views = num_views;
767 for (i = 0; i < num_views; i++)
768 GST_D3D11_CLEAR_COM (priv->shader_resource_view[i]);
770 priv->num_shader_resource_views = 0;
776 gst_d3d11_memory_ensure_shader_resource_view (GstD3D11Memory * mem)
778 GstD3D11MemoryPrivate *priv = mem->priv;
779 gboolean ret = FALSE;
781 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
784 if (!(priv->desc.BindFlags & D3D11_BIND_SHADER_RESOURCE)) {
785 GST_LOG_OBJECT (GST_MEMORY_CAST (mem)->allocator,
786 "Need BindFlags, current flag 0x%x", priv->desc.BindFlags);
790 GST_D3D11_MEMORY_LOCK (mem);
791 if (priv->num_shader_resource_views) {
796 ret = create_shader_resource_views (mem);
799 GST_D3D11_MEMORY_UNLOCK (mem);
805 * gst_d3d11_memory_get_shader_resource_view_size:
806 * @mem: a #GstD3D11Memory
808 * Returns: the number of ID3D11ShaderResourceView that can be used
809 * for processing GPU operation with @mem
814 gst_d3d11_memory_get_shader_resource_view_size (GstD3D11Memory * mem)
816 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), 0);
818 if (!gst_d3d11_memory_ensure_shader_resource_view (mem))
821 return mem->priv->num_shader_resource_views;
825 * gst_d3d11_memory_get_shader_resource_view:
826 * @mem: a #GstD3D11Memory
827 * @index: the index of the ID3D11ShaderResourceView
829 * Returns: (transfer none) (nullable): a pointer to the
830 * ID3D11ShaderResourceView or %NULL if ID3D11ShaderResourceView is unavailable
835 ID3D11ShaderResourceView *
836 gst_d3d11_memory_get_shader_resource_view (GstD3D11Memory * mem, guint index)
838 GstD3D11MemoryPrivate *priv;
840 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
842 if (!gst_d3d11_memory_ensure_shader_resource_view (mem))
847 if (index >= priv->num_shader_resource_views) {
848 GST_ERROR ("Invalid SRV index %d", index);
852 return priv->shader_resource_view[index];
856 create_render_target_views (GstD3D11Memory * mem)
858 GstD3D11MemoryPrivate *priv = mem->priv;
862 ID3D11Device *device_handle;
863 D3D11_RENDER_TARGET_VIEW_DESC render_desc;
864 DXGI_FORMAT formats[GST_VIDEO_MAX_PLANES] = { DXGI_FORMAT_UNKNOWN, };
866 memset (&render_desc, 0, sizeof (D3D11_RENDER_TARGET_VIEW_DESC));
868 device_handle = gst_d3d11_device_get_device_handle (mem->device);
870 num_views = gst_d3d11_dxgi_format_get_resource_format (priv->desc.Format,
873 GST_ERROR_OBJECT (GST_MEMORY_CAST (mem)->allocator,
874 "Unknown resource formats for DXGI format %s (%d)",
875 gst_d3d11_dxgi_format_to_string (priv->desc.Format), priv->desc.Format);
879 if ((priv->desc.BindFlags & D3D11_BIND_RENDER_TARGET) != 0) {
880 render_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
881 render_desc.Texture2D.MipSlice = 0;
883 for (i = 0; i < num_views; i++) {
884 render_desc.Format = formats[i];
886 hr = device_handle->CreateRenderTargetView (priv->texture, &render_desc,
887 &priv->render_target_view[i]);
888 if (!gst_d3d11_result (hr, mem->device)) {
889 GST_ERROR_OBJECT (GST_MEMORY_CAST (mem)->allocator,
890 "Failed to create resource DXGI format %s (%d) for plane %d"
891 " view (0x%x)", gst_d3d11_dxgi_format_to_string (formats[i]),
892 formats[i], i, (guint) hr);
897 priv->num_render_target_views = num_views;
905 for (i = 0; i < num_views; i++)
906 GST_D3D11_CLEAR_COM (priv->render_target_view[i]);
908 priv->num_render_target_views = 0;
914 gst_d3d11_memory_ensure_render_target_view (GstD3D11Memory * mem)
916 GstD3D11MemoryPrivate *priv = mem->priv;
917 gboolean ret = FALSE;
919 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
922 if (!(priv->desc.BindFlags & D3D11_BIND_RENDER_TARGET)) {
923 GST_WARNING_OBJECT (GST_MEMORY_CAST (mem)->allocator,
924 "Need BindFlags, current flag 0x%x", priv->desc.BindFlags);
928 GST_D3D11_MEMORY_LOCK (mem);
929 if (priv->num_render_target_views) {
934 ret = create_render_target_views (mem);
937 GST_D3D11_MEMORY_UNLOCK (mem);
943 * gst_d3d11_memory_get_render_target_view_size:
944 * @mem: a #GstD3D11Memory
946 * Returns: the number of ID3D11RenderTargetView that can be used
947 * for processing GPU operation with @mem
952 gst_d3d11_memory_get_render_target_view_size (GstD3D11Memory * mem)
954 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), 0);
956 if (!gst_d3d11_memory_ensure_render_target_view (mem))
959 return mem->priv->num_render_target_views;
963 * gst_d3d11_memory_get_render_target_view:
964 * @mem: a #GstD3D11Memory
965 * @index: the index of the ID3D11RenderTargetView
967 * Returns: (transfer none) (nullable): a pointer to the
968 * ID3D11RenderTargetView or %NULL if ID3D11RenderTargetView is unavailable
973 ID3D11RenderTargetView *
974 gst_d3d11_memory_get_render_target_view (GstD3D11Memory * mem, guint index)
976 GstD3D11MemoryPrivate *priv;
978 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
980 if (!gst_d3d11_memory_ensure_render_target_view (mem))
985 if (index >= priv->num_render_target_views) {
986 GST_ERROR ("Invalid RTV index %d", index);
990 return priv->render_target_view[index];
994 gst_d3d11_memory_ensure_decoder_output_view (GstD3D11Memory * mem,
995 ID3D11VideoDevice * video_device, GUID * decoder_profile)
997 GstD3D11MemoryPrivate *dmem_priv = mem->priv;
998 GstD3D11Allocator *allocator;
999 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC desc;
1001 gboolean ret = FALSE;
1003 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
1006 allocator = GST_D3D11_ALLOCATOR (GST_MEMORY_CAST (mem)->allocator);
1008 if (!(dmem_priv->desc.BindFlags & D3D11_BIND_DECODER)) {
1009 GST_LOG_OBJECT (allocator,
1010 "Need BindFlags, current flag 0x%x", dmem_priv->desc.BindFlags);
1014 GST_D3D11_MEMORY_LOCK (mem);
1015 if (dmem_priv->decoder_output_view) {
1016 dmem_priv->decoder_output_view->GetDesc (&desc);
1017 if (IsEqualGUID (desc.DecodeProfile, *decoder_profile)) {
1020 /* Shouldn't happen, but try again anyway */
1021 GST_WARNING_OBJECT (allocator,
1022 "Existing view has different decoder profile");
1023 GST_D3D11_CLEAR_COM (dmem_priv->decoder_output_view);
1027 if (dmem_priv->decoder_output_view)
1030 desc.DecodeProfile = *decoder_profile;
1031 desc.ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D;
1032 desc.Texture2D.ArraySlice = dmem_priv->subresource_index;
1034 hr = video_device->CreateVideoDecoderOutputView (dmem_priv->texture, &desc,
1035 &dmem_priv->decoder_output_view);
1036 if (!gst_d3d11_result (hr, mem->device)) {
1037 GST_ERROR_OBJECT (allocator,
1038 "Could not create decoder output view, hr: 0x%x", (guint) hr);
1046 GST_D3D11_MEMORY_UNLOCK (mem);
1052 * gst_d3d11_memory_get_decoder_output_view:
1053 * @mem: a #GstD3D11Memory
1055 * Returns: (transfer none) (nullable): a pointer to the
1056 * ID3D11VideoDecoderOutputView or %NULL if ID3D11VideoDecoderOutputView is
1061 ID3D11VideoDecoderOutputView *
1062 gst_d3d11_memory_get_decoder_output_view (GstD3D11Memory * mem,
1063 ID3D11VideoDevice * video_device, GUID * decoder_profile)
1065 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1066 g_return_val_if_fail (video_device != NULL, NULL);
1067 g_return_val_if_fail (decoder_profile != NULL, NULL);
1069 if (!gst_d3d11_memory_ensure_decoder_output_view (mem,
1070 video_device, decoder_profile))
1073 return mem->priv->decoder_output_view;
1077 check_bind_flags_for_processor_input_view (guint bind_flags)
1079 static const guint compatible_flags = (D3D11_BIND_DECODER |
1080 D3D11_BIND_VIDEO_ENCODER | D3D11_BIND_RENDER_TARGET |
1081 D3D11_BIND_UNORDERED_ACCESS);
1083 if (bind_flags == 0)
1086 if ((bind_flags & compatible_flags) != 0)
1093 gst_d3d11_memory_ensure_processor_input_view (GstD3D11Memory * mem,
1094 ID3D11VideoDevice * video_device,
1095 ID3D11VideoProcessorEnumerator * enumerator)
1097 GstD3D11MemoryPrivate *dmem_priv = mem->priv;
1098 GstD3D11Allocator *allocator;
1099 D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC desc;
1101 gboolean ret = FALSE;
1103 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
1106 allocator = GST_D3D11_ALLOCATOR (GST_MEMORY_CAST (mem)->allocator);
1108 if (!check_bind_flags_for_processor_input_view (dmem_priv->desc.BindFlags)) {
1109 GST_LOG_OBJECT (allocator,
1110 "Need BindFlags, current flag 0x%x", dmem_priv->desc.BindFlags);
1114 GST_D3D11_MEMORY_LOCK (mem);
1115 if (dmem_priv->processor_input_view)
1119 desc.ViewDimension = D3D11_VPIV_DIMENSION_TEXTURE2D;
1120 desc.Texture2D.MipSlice = 0;
1121 desc.Texture2D.ArraySlice = dmem_priv->subresource_index;
1123 hr = video_device->CreateVideoProcessorInputView (dmem_priv->texture,
1124 enumerator, &desc, &dmem_priv->processor_input_view);
1125 if (!gst_d3d11_result (hr, mem->device)) {
1126 GST_ERROR_OBJECT (allocator,
1127 "Could not create processor input view, hr: 0x%x", (guint) hr);
1135 GST_D3D11_MEMORY_UNLOCK (mem);
1141 * gst_d3d11_memory_get_processor_input_view:
1142 * @mem: a #GstD3D11Memory
1143 * @video_device: a #ID3D11VideoDevice
1144 * @enumerator: a #ID3D11VideoProcessorEnumerator
1146 * Returns: (transfer none) (nullable): a pointer to the
1147 * ID3D11VideoProcessorInputView or %NULL if ID3D11VideoProcessorInputView is
1152 ID3D11VideoProcessorInputView *
1153 gst_d3d11_memory_get_processor_input_view (GstD3D11Memory * mem,
1154 ID3D11VideoDevice * video_device,
1155 ID3D11VideoProcessorEnumerator * enumerator)
1157 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1158 g_return_val_if_fail (video_device != NULL, NULL);
1159 g_return_val_if_fail (enumerator != NULL, NULL);
1161 if (!gst_d3d11_memory_ensure_processor_input_view (mem, video_device,
1165 return mem->priv->processor_input_view;
1169 gst_d3d11_memory_ensure_processor_output_view (GstD3D11Memory * mem,
1170 ID3D11VideoDevice * video_device,
1171 ID3D11VideoProcessorEnumerator * enumerator)
1173 GstD3D11MemoryPrivate *priv = mem->priv;
1174 GstD3D11Allocator *allocator;
1175 D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC desc;
1179 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
1182 memset (&desc, 0, sizeof (D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC));
1184 allocator = GST_D3D11_ALLOCATOR (GST_MEMORY_CAST (mem)->allocator);
1186 if (!(priv->desc.BindFlags & D3D11_BIND_RENDER_TARGET)) {
1187 GST_LOG_OBJECT (allocator,
1188 "Need BindFlags, current flag 0x%x", priv->desc.BindFlags);
1192 /* FIXME: texture array should be supported at some point */
1193 if (priv->subresource_index != 0) {
1194 GST_FIXME_OBJECT (allocator,
1195 "Texture array is not suppoted for processor output view");
1199 GST_D3D11_MEMORY_LOCK (mem);
1200 if (priv->processor_output_view)
1203 desc.ViewDimension = D3D11_VPOV_DIMENSION_TEXTURE2D;
1204 desc.Texture2D.MipSlice = 0;
1206 hr = video_device->CreateVideoProcessorOutputView (priv->texture,
1207 enumerator, &desc, &priv->processor_output_view);
1208 if (!gst_d3d11_result (hr, mem->device)) {
1209 GST_ERROR_OBJECT (allocator,
1210 "Could not create processor input view, hr: 0x%x", (guint) hr);
1218 GST_D3D11_MEMORY_UNLOCK (mem);
1224 * gst_d3d11_memory_get_processor_output_view:
1225 * @mem: a #GstD3D11Memory
1226 * @video_device: a #ID3D11VideoDevice
1227 * @enumerator: a #ID3D11VideoProcessorEnumerator
1229 * Returns: (transfer none) (nullable): a pointer to the
1230 * ID3D11VideoProcessorOutputView or %NULL if ID3D11VideoProcessorOutputView is
1235 ID3D11VideoProcessorOutputView *
1236 gst_d3d11_memory_get_processor_output_view (GstD3D11Memory * mem,
1237 ID3D11VideoDevice * video_device,
1238 ID3D11VideoProcessorEnumerator * enumerator)
1240 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1241 g_return_val_if_fail (video_device != NULL, NULL);
1242 g_return_val_if_fail (enumerator != NULL, NULL);
1244 if (!gst_d3d11_memory_ensure_processor_output_view (mem, video_device,
1248 return mem->priv->processor_output_view;
1251 /* GstD3D11Allocator */
1252 struct _GstD3D11AllocatorPrivate
1254 GstMemoryCopyFunction fallback_copy;
1257 #define gst_d3d11_allocator_parent_class alloc_parent_class
1258 G_DEFINE_TYPE_WITH_PRIVATE (GstD3D11Allocator,
1259 gst_d3d11_allocator, GST_TYPE_ALLOCATOR);
1261 static GstMemory *gst_d3d11_allocator_dummy_alloc (GstAllocator * allocator,
1262 gsize size, GstAllocationParams * params);
1263 static GstMemory *gst_d3d11_allocator_alloc_internal (GstD3D11Allocator * self,
1264 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc);
1265 static void gst_d3d11_allocator_free (GstAllocator * allocator,
1269 gst_d3d11_allocator_class_init (GstD3D11AllocatorClass * klass)
1271 GstAllocatorClass *allocator_class = GST_ALLOCATOR_CLASS (klass);
1273 allocator_class->alloc = gst_d3d11_allocator_dummy_alloc;
1274 allocator_class->free = gst_d3d11_allocator_free;
1278 gst_d3d11_memory_copy (GstMemory * mem, gssize offset, gssize size)
1280 GstD3D11Allocator *alloc = GST_D3D11_ALLOCATOR (mem->allocator);
1281 GstD3D11AllocatorPrivate *priv = alloc->priv;
1282 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
1283 GstD3D11Memory *copy_dmem;
1284 GstD3D11Device *device = dmem->device;
1285 ID3D11Device *device_handle = gst_d3d11_device_get_device_handle (device);
1286 ID3D11DeviceContext *device_context =
1287 gst_d3d11_device_get_device_context_handle (device);
1288 D3D11_TEXTURE2D_DESC dst_desc = { 0, };
1289 D3D11_TEXTURE2D_DESC src_desc = { 0, };
1290 GstMemory *copy = NULL;
1293 UINT bind_flags = 0;
1294 UINT supported_flags = 0;
1296 if (dmem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
1297 return priv->fallback_copy (mem, offset, size);
1299 /* non-zero offset or different size is not supported */
1300 if (offset != 0 || (size != -1 && (gsize) size != mem->size)) {
1301 GST_DEBUG_OBJECT (alloc, "Different size/offset, try fallback copy");
1302 return priv->fallback_copy (mem, offset, size);
1305 gst_d3d11_device_lock (device);
1306 if (!gst_memory_map (mem, &info,
1307 (GstMapFlags) (GST_MAP_READ | GST_MAP_D3D11))) {
1308 gst_d3d11_device_unlock (device);
1310 GST_WARNING_OBJECT (alloc, "Failed to map memory, try fallback copy");
1312 return priv->fallback_copy (mem, offset, size);
1315 dmem->priv->texture->GetDesc (&src_desc);
1316 dst_desc.Width = src_desc.Width;
1317 dst_desc.Height = src_desc.Height;
1318 dst_desc.MipLevels = 1;
1319 dst_desc.Format = src_desc.Format;
1320 dst_desc.SampleDesc.Count = 1;
1321 dst_desc.ArraySize = 1;
1322 dst_desc.Usage = D3D11_USAGE_DEFAULT;
1324 /* If supported, use bind flags for SRV/RTV */
1325 hr = device_handle->CheckFormatSupport (src_desc.Format, &supported_flags);
1326 if (gst_d3d11_result (hr, device)) {
1327 if ((supported_flags & D3D11_FORMAT_SUPPORT_SHADER_SAMPLE) ==
1328 D3D11_FORMAT_SUPPORT_SHADER_SAMPLE) {
1329 bind_flags |= D3D11_BIND_SHADER_RESOURCE;
1332 if ((supported_flags & D3D11_FORMAT_SUPPORT_RENDER_TARGET) ==
1333 D3D11_FORMAT_SUPPORT_RENDER_TARGET) {
1334 bind_flags |= D3D11_BIND_RENDER_TARGET;
1338 copy = gst_d3d11_allocator_alloc_internal (alloc, device, &dst_desc);
1340 gst_memory_unmap (mem, &info);
1341 gst_d3d11_device_unlock (device);
1343 GST_WARNING_OBJECT (alloc,
1344 "Failed to allocate new d3d11 map memory, try fallback copy");
1346 return priv->fallback_copy (mem, offset, size);
1349 copy_dmem = GST_D3D11_MEMORY_CAST (copy);
1350 device_context->CopySubresourceRegion (copy_dmem->priv->texture, 0, 0, 0, 0,
1351 dmem->priv->texture, dmem->priv->subresource_index, NULL);
1352 copy->maxsize = copy->size = mem->maxsize;
1353 gst_memory_unmap (mem, &info);
1354 gst_d3d11_device_unlock (device);
1356 /* newly allocated memory holds valid image data. We need download this
1357 * pixel data into staging memory for CPU access */
1358 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
1364 gst_d3d11_allocator_init (GstD3D11Allocator * allocator)
1366 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
1367 GstD3D11AllocatorPrivate *priv;
1369 priv = allocator->priv = (GstD3D11AllocatorPrivate *)
1370 gst_d3d11_allocator_get_instance_private (allocator);
1372 alloc->mem_type = GST_D3D11_MEMORY_NAME;
1373 alloc->mem_map_full = gst_d3d11_memory_map_full;
1374 alloc->mem_unmap_full = gst_d3d11_memory_unmap_full;
1375 alloc->mem_share = gst_d3d11_memory_share;
1377 /* Store pointer to default mem_copy method for fallback copy */
1378 priv->fallback_copy = alloc->mem_copy;
1379 alloc->mem_copy = gst_d3d11_memory_copy;
1381 GST_OBJECT_FLAG_SET (alloc, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
1385 gst_d3d11_allocator_dummy_alloc (GstAllocator * allocator, gsize size,
1386 GstAllocationParams * params)
1388 g_return_val_if_reached (NULL);
1392 gst_d3d11_allocator_free (GstAllocator * allocator, GstMemory * mem)
1394 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
1395 GstD3D11MemoryPrivate *dmem_priv = dmem->priv;
1398 GST_LOG_OBJECT (allocator, "Free memory %p", mem);
1400 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
1401 GST_D3D11_CLEAR_COM (dmem_priv->render_target_view[i]);
1402 GST_D3D11_CLEAR_COM (dmem_priv->shader_resource_view[i]);
1405 GST_D3D11_CLEAR_COM (dmem_priv->decoder_output_view);
1406 GST_D3D11_CLEAR_COM (dmem_priv->processor_input_view);
1407 GST_D3D11_CLEAR_COM (dmem_priv->processor_output_view);
1408 GST_D3D11_CLEAR_COM (dmem_priv->texture);
1409 GST_D3D11_CLEAR_COM (dmem_priv->staging);
1410 GST_D3D11_CLEAR_COM (dmem_priv->buffer);
1412 gst_clear_object (&dmem->device);
1413 g_mutex_clear (&dmem_priv->lock);
1414 g_free (dmem->priv);
1419 gst_d3d11_allocator_alloc_wrapped (GstD3D11Allocator * self,
1420 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc,
1421 ID3D11Texture2D * texture)
1423 GstD3D11Memory *mem;
1425 mem = g_new0 (GstD3D11Memory, 1);
1426 mem->priv = g_new0 (GstD3D11MemoryPrivate, 1);
1428 gst_memory_init (GST_MEMORY_CAST (mem),
1429 (GstMemoryFlags) 0, GST_ALLOCATOR_CAST (self), NULL, 0, 0, 0, 0);
1430 g_mutex_init (&mem->priv->lock);
1431 mem->priv->texture = texture;
1432 mem->priv->desc = *desc;
1433 mem->priv->native_type = GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D;
1434 mem->device = (GstD3D11Device *) gst_object_ref (device);
1436 return GST_MEMORY_CAST (mem);
1439 typedef void (*GstD3D11ClearRTVFunc) (ID3D11DeviceContext * context_handle,
1440 ID3D11RenderTargetView * rtv);
1443 clear_rtv_chroma (ID3D11DeviceContext * context_handle,
1444 ID3D11RenderTargetView * rtv)
1446 const FLOAT clear_color[4] = { 0.5f, 0.5f, 0.5f, 1.0f };
1448 context_handle->ClearRenderTargetView (rtv, clear_color);
1452 clear_rtv_vuya (ID3D11DeviceContext * context_handle,
1453 ID3D11RenderTargetView * rtv)
1455 const FLOAT clear_color[4] = { 0.5f, 0.5f, 0.0f, 1.0f };
1457 context_handle->ClearRenderTargetView (rtv, clear_color);
1461 gst_d3d11_allocator_alloc_internal (GstD3D11Allocator * self,
1462 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc)
1464 ID3D11Texture2D *texture = NULL;
1465 ID3D11Device *device_handle;
1466 ID3D11DeviceContext *context_handle;
1469 GstD3D11Memory *dmem;
1470 ID3D11RenderTargetView *rtv = nullptr;
1471 GstD3D11ClearRTVFunc clear_func = nullptr;
1473 device_handle = gst_d3d11_device_get_device_handle (device);
1475 hr = device_handle->CreateTexture2D (desc, NULL, &texture);
1476 if (!gst_d3d11_result (hr, device)) {
1477 GST_ERROR_OBJECT (self, "Couldn't create texture");
1481 mem = gst_d3d11_allocator_alloc_wrapped (self, device, desc, texture);
1485 /* Clear with YUV black if needed and possible
1486 * TODO: do this using UAV if RTV is not allowed (e.g., packed YUV formats) */
1487 if ((desc->BindFlags & D3D11_BIND_RENDER_TARGET) == 0)
1490 dmem = GST_D3D11_MEMORY_CAST (mem);
1491 switch (desc->Format) {
1492 case DXGI_FORMAT_NV12:
1493 case DXGI_FORMAT_P010:
1494 case DXGI_FORMAT_P016:
1495 /* Y component will be zero already */
1496 rtv = gst_d3d11_memory_get_render_target_view (dmem, 1);
1497 clear_func = (GstD3D11ClearRTVFunc) clear_rtv_chroma;
1499 case DXGI_FORMAT_AYUV:
1500 rtv = gst_d3d11_memory_get_render_target_view (dmem, 0);
1501 clear_func = (GstD3D11ClearRTVFunc) clear_rtv_vuya;
1510 context_handle = gst_d3d11_device_get_device_context_handle (device);
1511 gst_d3d11_device_lock (device);
1512 clear_func (context_handle, rtv);
1513 gst_d3d11_device_unlock (device);
1519 * gst_d3d11_allocator_alloc:
1520 * @allocator: a #GstD3D11Allocator
1521 * @device: a #GstD3D11Device
1522 * @desc: a D3D11_TEXTURE2D_DESC struct
1524 * Returns: a newly allocated #GstD3D11Memory with given parameters.
1529 gst_d3d11_allocator_alloc (GstD3D11Allocator * allocator,
1530 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc)
1534 g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), NULL);
1535 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), NULL);
1536 g_return_val_if_fail (desc != NULL, NULL);
1538 mem = gst_d3d11_allocator_alloc_internal (allocator, device, desc);
1542 if (!gst_d3d11_memory_update_size (mem)) {
1543 GST_ERROR_OBJECT (allocator, "Failed to calculate size");
1544 gst_memory_unref (mem);
1552 * gst_d3d11_allocator_alloc_buffer:
1553 * @allocator: a #GstD3D11Allocator
1554 * @device: a #GstD3D11Device
1555 * @desc: a D3D11_BUFFER_DESC struct
1557 * Returns: a newly allocated #GstD3D11Memory with given parameters.
1562 gst_d3d11_allocator_alloc_buffer (GstD3D11Allocator * allocator,
1563 GstD3D11Device * device, const D3D11_BUFFER_DESC * desc)
1565 GstD3D11Memory *mem;
1566 ID3D11Buffer *buffer;
1567 ID3D11Device *device_handle;
1570 g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), nullptr);
1571 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), nullptr);
1572 g_return_val_if_fail (desc != nullptr, nullptr);
1574 if (desc->Usage != D3D11_USAGE_STAGING) {
1575 GST_FIXME_OBJECT (allocator, "Non staging buffer is not supported");
1579 device_handle = gst_d3d11_device_get_device_handle (device);
1581 hr = device_handle->CreateBuffer (desc, nullptr, &buffer);
1582 if (!gst_d3d11_result (hr, device)) {
1583 GST_ERROR_OBJECT (allocator, "Couldn't create buffer");
1587 mem = g_new0 (GstD3D11Memory, 1);
1588 mem->priv = g_new0 (GstD3D11MemoryPrivate, 1);
1590 gst_memory_init (GST_MEMORY_CAST (mem),
1591 (GstMemoryFlags) 0, GST_ALLOCATOR_CAST (allocator), nullptr, 0, 0, 0, 0);
1592 g_mutex_init (&mem->priv->lock);
1593 mem->priv->buffer = buffer;
1594 mem->priv->buffer_desc = *desc;
1595 mem->priv->native_type = GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER;
1596 mem->device = (GstD3D11Device *) gst_object_ref (device);
1598 GST_MEMORY_CAST (mem)->maxsize = GST_MEMORY_CAST (mem)->size =
1601 return GST_MEMORY_CAST (mem);
1605 gst_d3d11_allocator_set_active (GstD3D11Allocator * allocator, gboolean active)
1607 GstD3D11AllocatorClass *klass;
1609 g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), FALSE);
1611 klass = GST_D3D11_ALLOCATOR_GET_CLASS (allocator);
1612 if (klass->set_actvie)
1613 return klass->set_actvie (allocator, active);
1618 /* GstD3D11PoolAllocator */
1619 #define GST_D3D11_POOL_ALLOCATOR_LOCK(alloc) (g_rec_mutex_lock(&alloc->priv->lock))
1620 #define GST_D3D11_POOL_ALLOCATOR_UNLOCK(alloc) (g_rec_mutex_unlock(&alloc->priv->lock))
1621 #define GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING(alloc) (g_atomic_int_get (&alloc->priv->flushing))
1623 struct _GstD3D11PoolAllocatorPrivate
1625 /* parent texture when array typed memory is used */
1626 ID3D11Texture2D *texture;
1627 D3D11_TEXTURE2D_DESC desc;
1629 /* All below member variables are analogous to that of GstBufferPool */
1630 GstAtomicQueue *queue;
1633 /* This lock will protect all below variables apart from atomic ones
1634 * (identical to GstBufferPool::priv::rec_lock) */
1645 /* Calculated memory size, based on Direct3D11 staging texture map.
1646 * Note that, we cannot know the actually staging texture memory size prior
1647 * to map the staging texture because driver will likely require padding */
1651 static void gst_d3d11_pool_allocator_dispose (GObject * object);
1652 static void gst_d3d11_pool_allocator_finalize (GObject * object);
1655 gst_d3d11_pool_allocator_set_active (GstD3D11Allocator * allocator,
1658 static gboolean gst_d3d11_pool_allocator_start (GstD3D11PoolAllocator * self);
1659 static gboolean gst_d3d11_pool_allocator_stop (GstD3D11PoolAllocator * self);
1660 static gboolean gst_d3d11_memory_release (GstMiniObject * mini_object);
1662 #define gst_d3d11_pool_allocator_parent_class pool_alloc_parent_class
1663 G_DEFINE_TYPE_WITH_PRIVATE (GstD3D11PoolAllocator,
1664 gst_d3d11_pool_allocator, GST_TYPE_D3D11_ALLOCATOR);
1667 gst_d3d11_pool_allocator_class_init (GstD3D11PoolAllocatorClass * klass)
1669 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1670 GstD3D11AllocatorClass *d3d11alloc_class = GST_D3D11_ALLOCATOR_CLASS (klass);
1672 gobject_class->dispose = gst_d3d11_pool_allocator_dispose;
1673 gobject_class->finalize = gst_d3d11_pool_allocator_finalize;
1675 d3d11alloc_class->set_actvie = gst_d3d11_pool_allocator_set_active;
1679 gst_d3d11_pool_allocator_init (GstD3D11PoolAllocator * allocator)
1681 GstD3D11PoolAllocatorPrivate *priv;
1683 priv = allocator->priv = (GstD3D11PoolAllocatorPrivate *)
1684 gst_d3d11_pool_allocator_get_instance_private (allocator);
1685 g_rec_mutex_init (&priv->lock);
1687 priv->poll = gst_poll_new_timer ();
1688 priv->queue = gst_atomic_queue_new (16);
1690 priv->active = FALSE;
1691 priv->started = FALSE;
1693 /* 1 control write for flushing - the flush token */
1694 gst_poll_write_control (priv->poll);
1695 /* 1 control write for marking that we are not waiting for poll - the wait token */
1696 gst_poll_write_control (priv->poll);
1700 gst_d3d11_pool_allocator_dispose (GObject * object)
1702 GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (object);
1704 gst_clear_object (&self->device);
1706 G_OBJECT_CLASS (pool_alloc_parent_class)->dispose (object);
1710 gst_d3d11_pool_allocator_finalize (GObject * object)
1712 GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (object);
1713 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1715 GST_DEBUG_OBJECT (self, "Finalize");
1717 gst_d3d11_pool_allocator_stop (self);
1718 gst_atomic_queue_unref (priv->queue);
1719 gst_poll_free (priv->poll);
1720 g_rec_mutex_clear (&priv->lock);
1722 GST_D3D11_CLEAR_COM (priv->texture);
1724 G_OBJECT_CLASS (pool_alloc_parent_class)->finalize (object);
1728 gst_d3d11_pool_allocator_start (GstD3D11PoolAllocator * self)
1730 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1731 ID3D11Device *device_handle;
1739 if (priv->desc.ArraySize == 1) {
1740 priv->started = TRUE;
1744 device_handle = gst_d3d11_device_get_device_handle (self->device);
1746 if (!priv->texture) {
1747 hr = device_handle->CreateTexture2D (&priv->desc, NULL, &priv->texture);
1748 if (!gst_d3d11_result (hr, self->device)) {
1749 GST_ERROR_OBJECT (self, "Failed to allocate texture");
1754 /* Pre-allocate memory objects */
1755 for (i = 0; i < priv->desc.ArraySize; i++) {
1758 priv->texture->AddRef ();
1760 gst_d3d11_allocator_alloc_wrapped (GST_D3D11_ALLOCATOR_CAST
1761 (_d3d11_memory_allocator), self->device, &priv->desc, priv->texture);
1764 if (!gst_d3d11_memory_update_size (mem)) {
1765 GST_ERROR_OBJECT (self, "Failed to calculate memory size");
1766 gst_memory_unref (mem);
1770 priv->mem_size = mem->size;
1772 mem->size = mem->maxsize = priv->mem_size;
1775 GST_D3D11_MEMORY_CAST (mem)->priv->subresource_index = i;
1777 g_atomic_int_add (&priv->cur_mems, 1);
1778 gst_atomic_queue_push (priv->queue, mem);
1779 gst_poll_write_control (priv->poll);
1782 priv->started = TRUE;
1788 gst_d3d11_pool_allocator_do_set_flushing (GstD3D11PoolAllocator * self,
1791 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1793 if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self) == flushing)
1797 g_atomic_int_set (&priv->flushing, 1);
1798 /* Write the flush token to wake up any waiters */
1799 gst_poll_write_control (priv->poll);
1801 while (!gst_poll_read_control (priv->poll)) {
1802 if (errno == EWOULDBLOCK) {
1803 /* This should not really happen unless flushing and unflushing
1804 * happens on different threads. Let's wait a bit to get back flush
1805 * token from the thread that was setting it to flushing */
1809 /* Critical error but GstPoll already complained */
1814 g_atomic_int_set (&priv->flushing, 0);
1819 gst_d3d11_pool_allocator_set_active (GstD3D11Allocator * allocator,
1822 GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (allocator);
1823 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1825 GST_LOG_OBJECT (self, "active %d", active);
1827 GST_D3D11_POOL_ALLOCATOR_LOCK (self);
1828 /* just return if we are already in the right state */
1829 if (priv->active == active)
1833 if (!gst_d3d11_pool_allocator_start (self))
1836 /* flush_stop may release memory objects, setting to active to avoid running
1837 * do_stop while activating the pool */
1838 priv->active = TRUE;
1840 gst_d3d11_pool_allocator_do_set_flushing (self, FALSE);
1844 /* set to flushing first */
1845 gst_d3d11_pool_allocator_do_set_flushing (self, TRUE);
1847 /* when all memory objects are in the pool, free them. Else they will be
1848 * freed when they are released */
1849 outstanding = g_atomic_int_get (&priv->outstanding);
1850 GST_LOG_OBJECT (self, "outstanding memories %d, (in queue %d)",
1851 outstanding, gst_atomic_queue_length (priv->queue));
1852 if (outstanding == 0) {
1853 if (!gst_d3d11_pool_allocator_stop (self))
1857 priv->active = FALSE;
1860 GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1866 GST_DEBUG_OBJECT (self, "allocator was in the right state");
1867 GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1872 GST_ERROR_OBJECT (self, "start failed");
1873 GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1878 GST_ERROR_OBJECT (self, "stop failed");
1879 GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1885 gst_d3d11_pool_allocator_free_memory (GstD3D11PoolAllocator * self,
1888 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1890 g_atomic_int_add (&priv->cur_mems, -1);
1891 GST_LOG_OBJECT (self, "freeing memory %p (%u left)", mem, priv->cur_mems);
1893 GST_MINI_OBJECT_CAST (mem)->dispose = NULL;
1894 gst_memory_unref (mem);
1897 /* must be called with the lock */
1899 gst_d3d11_pool_allocator_clear_queue (GstD3D11PoolAllocator * self)
1901 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1904 GST_LOG_OBJECT (self, "Clearing queue");
1906 /* clear the pool */
1907 while ((memory = (GstMemory *) gst_atomic_queue_pop (priv->queue))) {
1908 while (!gst_poll_read_control (priv->poll)) {
1909 if (errno == EWOULDBLOCK) {
1910 /* We put the memory into the queue but did not finish writing control
1911 * yet, let's wait a bit and retry */
1915 /* Critical error but GstPoll already complained */
1919 gst_d3d11_pool_allocator_free_memory (self, memory);
1922 GST_LOG_OBJECT (self, "Clear done");
1924 return priv->cur_mems == 0;
1927 /* must be called with the lock */
1929 gst_d3d11_pool_allocator_stop (GstD3D11PoolAllocator * self)
1931 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1933 GST_DEBUG_OBJECT (self, "Stop");
1935 if (priv->started) {
1936 if (!gst_d3d11_pool_allocator_clear_queue (self))
1939 priv->started = FALSE;
1941 GST_DEBUG_OBJECT (self, "Wasn't started");
1948 dec_outstanding (GstD3D11PoolAllocator * self)
1950 if (g_atomic_int_dec_and_test (&self->priv->outstanding)) {
1951 /* all memory objects are returned to the pool, see if we need to free them */
1952 if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)) {
1953 /* take the lock so that set_active is not run concurrently */
1954 GST_D3D11_POOL_ALLOCATOR_LOCK (self);
1955 /* now that we have the lock, check if we have been de-activated with
1956 * outstanding buffers */
1957 if (!self->priv->active)
1958 gst_d3d11_pool_allocator_stop (self);
1960 GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1966 gst_d3d11_pool_allocator_release_memory (GstD3D11PoolAllocator * self,
1969 GST_LOG_OBJECT (self, "Released memory %p", mem);
1971 GST_MINI_OBJECT_CAST (mem)->dispose = NULL;
1972 mem->allocator = (GstAllocator *) gst_object_ref (_d3d11_memory_allocator);
1973 gst_object_unref (self);
1975 /* keep it around in our queue */
1976 gst_atomic_queue_push (self->priv->queue, mem);
1977 gst_poll_write_control (self->priv->poll);
1978 dec_outstanding (self);
1982 gst_d3d11_memory_release (GstMiniObject * mini_object)
1984 GstMemory *mem = GST_MEMORY_CAST (mini_object);
1985 GstD3D11PoolAllocator *alloc;
1987 g_assert (mem->allocator != NULL);
1989 if (!GST_IS_D3D11_POOL_ALLOCATOR (mem->allocator)) {
1990 GST_LOG_OBJECT (mem->allocator, "Not our memory, free");
1994 alloc = GST_D3D11_POOL_ALLOCATOR (mem->allocator);
1995 /* if flushing, free this memory */
1996 if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (alloc)) {
1997 GST_LOG_OBJECT (alloc, "allocator is flushing, free %p", mem);
2001 /* return the memory to the allocator */
2002 gst_memory_ref (mem);
2003 gst_d3d11_pool_allocator_release_memory (alloc, mem);
2008 static GstFlowReturn
2009 gst_d3d11_pool_allocator_alloc (GstD3D11PoolAllocator * self, GstMemory ** mem)
2011 GstD3D11PoolAllocatorPrivate *priv = self->priv;
2014 /* we allcates texture array during start */
2015 if (priv->desc.ArraySize > 1)
2016 return GST_FLOW_EOS;
2018 /* increment the allocation counter */
2019 g_atomic_int_add (&priv->cur_mems, 1);
2021 gst_d3d11_allocator_alloc_internal (GST_D3D11_ALLOCATOR_CAST
2022 (_d3d11_memory_allocator), self->device, &priv->desc);
2024 GST_ERROR_OBJECT (self, "Failed to allocate new memory");
2025 g_atomic_int_add (&priv->cur_mems, -1);
2026 return GST_FLOW_ERROR;
2029 if (!priv->mem_size) {
2030 if (!gst_d3d11_memory_update_size (new_mem)) {
2031 GST_ERROR_OBJECT (self, "Failed to calculate size");
2032 gst_memory_unref (new_mem);
2033 g_atomic_int_add (&priv->cur_mems, -1);
2035 return GST_FLOW_ERROR;
2038 priv->mem_size = new_mem->size;
2041 new_mem->size = new_mem->maxsize = priv->mem_size;
2048 static GstFlowReturn
2049 gst_d3d11_pool_allocator_acquire_memory_internal (GstD3D11PoolAllocator * self,
2050 GstMemory ** memory)
2052 GstFlowReturn result;
2053 GstD3D11PoolAllocatorPrivate *priv = self->priv;
2056 if (G_UNLIKELY (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)))
2059 /* try to get a memory from the queue */
2060 *memory = (GstMemory *) gst_atomic_queue_pop (priv->queue);
2061 if (G_LIKELY (*memory)) {
2062 while (!gst_poll_read_control (priv->poll)) {
2063 if (errno == EWOULDBLOCK) {
2064 /* We put the memory into the queue but did not finish writing control
2065 * yet, let's wait a bit and retry */
2069 /* Critical error but GstPoll already complained */
2073 result = GST_FLOW_OK;
2074 GST_LOG_OBJECT (self, "acquired memory %p", *memory);
2078 /* no memory, try to allocate some more */
2079 GST_LOG_OBJECT (self, "no memory, trying to allocate");
2080 result = gst_d3d11_pool_allocator_alloc (self, memory);
2081 if (G_LIKELY (result == GST_FLOW_OK))
2082 /* we have a memory, return it */
2085 if (G_UNLIKELY (result != GST_FLOW_EOS))
2086 /* something went wrong, return error */
2089 /* now we release the control socket, we wait for a memory release or
2091 if (!gst_poll_read_control (priv->poll)) {
2092 if (errno == EWOULDBLOCK) {
2093 /* This means that we have two threads trying to allocate memory
2094 * already, and the other one already got the wait token. This
2095 * means that we only have to wait for the poll now and not write the
2096 * token afterwards: we will be woken up once the other thread is
2097 * woken up and that one will write the wait token it removed */
2098 GST_LOG_OBJECT (self, "waiting for free memory or flushing");
2099 gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
2101 /* This is a critical error, GstPoll already gave a warning */
2102 result = GST_FLOW_ERROR;
2106 /* We're the first thread waiting, we got the wait token and have to
2107 * write it again later
2109 * We're a second thread and just consumed the flush token and block all
2110 * other threads, in which case we must not wait and give it back
2112 if (!GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)) {
2113 GST_LOG_OBJECT (self, "waiting for free memory or flushing");
2114 gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
2116 gst_poll_write_control (priv->poll);
2125 GST_DEBUG_OBJECT (self, "we are flushing");
2126 return GST_FLOW_FLUSHING;
2131 * gst_d3d11_pool_allocator_new:
2132 * @device: a #GstD3D11Device
2133 * @desc: a D3D11_TEXTURE2D_DESC for texture allocation
2135 * Creates a new #GstD3D11PoolAllocator instance.
2137 * Returns: (transfer full): a new #GstD3D11PoolAllocator instance
2139 GstD3D11PoolAllocator *
2140 gst_d3d11_pool_allocator_new (GstD3D11Device * device,
2141 const D3D11_TEXTURE2D_DESC * desc)
2143 GstD3D11PoolAllocator *self;
2145 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), NULL);
2146 g_return_val_if_fail (desc != NULL, NULL);
2148 gst_d3d11_memory_init_once ();
2150 self = (GstD3D11PoolAllocator *)
2151 g_object_new (GST_TYPE_D3D11_POOL_ALLOCATOR, NULL);
2152 gst_object_ref_sink (self);
2154 self->device = (GstD3D11Device *) gst_object_ref (device);
2155 self->priv->desc = *desc;
2161 * gst_d3d11_pool_allocator_acquire_memory:
2162 * @allocator: a #GstD3D11PoolAllocator
2163 * @memory: (transfer full): a #GstMemory
2165 * Acquires a #GstMemory from @allocator. @memory should point to a memory
2166 * location that can hold a pointer to the new #GstMemory.
2168 * Returns: a #GstFlowReturn such as %GST_FLOW_FLUSHING when the allocator is
2172 gst_d3d11_pool_allocator_acquire_memory (GstD3D11PoolAllocator * allocator,
2173 GstMemory ** memory)
2175 GstD3D11PoolAllocatorPrivate *priv;
2176 GstFlowReturn result;
2178 g_return_val_if_fail (GST_IS_D3D11_POOL_ALLOCATOR (allocator),
2180 g_return_val_if_fail (memory != NULL, GST_FLOW_ERROR);
2182 priv = allocator->priv;
2184 /* assume we'll have one more outstanding buffer we need to do that so
2185 * that concurrent set_active doesn't clear the buffers */
2186 g_atomic_int_inc (&priv->outstanding);
2187 result = gst_d3d11_pool_allocator_acquire_memory_internal (allocator, memory);
2189 if (result == GST_FLOW_OK) {
2190 GstMemory *mem = *memory;
2191 /* Replace default allocator with ours */
2192 gst_object_unref (mem->allocator);
2193 mem->allocator = (GstAllocator *) gst_object_ref (allocator);
2194 GST_MINI_OBJECT_CAST (mem)->dispose = gst_d3d11_memory_release;
2196 dec_outstanding (allocator);
2203 * gst_d3d11_pool_allocator_get_pool_size:
2204 * @allocator: a #GstD3D11PoolAllocator
2205 * @max_size: (out) (optional): the max size of pool
2206 * @outstanding_size: (out) (optional): the number of outstanding memory
2208 * Returns: %TRUE if the size of memory pool is known
2213 gst_d3d11_pool_allocator_get_pool_size (GstD3D11PoolAllocator * allocator,
2214 guint * max_size, guint * outstanding_size)
2216 GstD3D11PoolAllocatorPrivate *priv;
2218 g_return_val_if_fail (GST_IS_D3D11_POOL_ALLOCATOR (allocator), FALSE);
2220 priv = allocator->priv;
2223 if (priv->desc.ArraySize > 1) {
2224 *max_size = priv->desc.ArraySize;
2226 /* For non-texture-array memory, we don't have any limit yet */
2231 if (outstanding_size)
2232 *outstanding_size = g_atomic_int_get (&priv->outstanding);