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 GstD3D11Allocator *_d3d11_memory_allocator;
37 gst_d3d11_allocation_flags_get_type (void)
39 static GType type = 0;
40 static const GFlagsValue values[] = {
41 {GST_D3D11_ALLOCATION_FLAG_DEFAULT, "GST_D3D11_ALLOCATION_FLAG_DEFAULT",
43 {GST_D3D11_ALLOCATION_FLAG_TEXTURE_ARRAY,
44 "GST_D3D11_ALLOCATION_FLAG_TEXTURE_ARRAY", "texture-array"},
48 GST_D3D11_CALL_ONCE_BEGIN {
49 type = g_flags_register_static ("GstD3D11AllocationFlags", values);
50 } GST_D3D11_CALL_ONCE_END;
56 gst_d3d11_memory_transfer_get_type (void)
58 static GType type = 0;
59 static const GFlagsValue values[] = {
60 {GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD,
61 "GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD", "need-download"},
62 {GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD,
63 "GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD", "need-upload"},
67 GST_D3D11_CALL_ONCE_BEGIN {
68 type = g_flags_register_static ("GstD3D11MemoryTransfer", values);
69 } GST_D3D11_CALL_ONCE_END;
75 gst_d3d11_memory_native_type_get_type (void)
77 static GType type = 0;
78 static const GEnumValue values[] = {
79 {GST_D3D11_MEMORY_NATIVE_TYPE_INVALID,
80 "GST_D3D11_MEMORY_NATIVE_TYPE_INVALID", "invalid"},
81 {GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER, "GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER",
83 {GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D,
84 "GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D", "texture-2d"},
88 GST_D3D11_CALL_ONCE_BEGIN {
89 type = g_enum_register_static ("GstD3D11MemoryNativeType", values);
90 } GST_D3D11_CALL_ONCE_END;
95 /* GstD3D11AllocationParams */
96 static void gst_d3d11_allocation_params_init (GType type);
97 G_DEFINE_BOXED_TYPE_WITH_CODE (GstD3D11AllocationParams,
98 gst_d3d11_allocation_params,
99 (GBoxedCopyFunc) gst_d3d11_allocation_params_copy,
100 (GBoxedFreeFunc) gst_d3d11_allocation_params_free,
101 gst_d3d11_allocation_params_init (g_define_type_id));
104 * gst_d3d11_allocation_params_new:
105 * @device: a #GstD3D11Device
106 * @info: a #GstVideoInfo
107 * @flags: a #GstD3D11AllocationFlags
108 * @bind_flags: D3D11_BIND_FLAG value used for creating texture
109 * @misc_flags: D3D11_RESOURCE_MISC_FLAG value used for creating texture
111 * Create #GstD3D11AllocationParams object which is used by #GstD3D11BufferPool
112 * and #GstD3D11Allocator in order to allocate new ID3D11Texture2D
113 * object with given configuration
115 * Returns: a #GstD3D11AllocationParams or %NULL if @info is not supported
119 GstD3D11AllocationParams *
120 gst_d3d11_allocation_params_new (GstD3D11Device * device,
121 const GstVideoInfo * info, GstD3D11AllocationFlags flags, guint bind_flags,
124 GstD3D11AllocationParams *ret;
125 GstD3D11Format d3d11_format;
128 g_return_val_if_fail (info != NULL, NULL);
130 if (!gst_d3d11_device_get_format (device, GST_VIDEO_INFO_FORMAT (info),
132 GST_WARNING ("Couldn't get d3d11 format");
136 ret = g_new0 (GstD3D11AllocationParams, 1);
139 ret->aligned_info = *info;
140 ret->d3d11_format = d3d11_format;
143 * https://docs.microsoft.com/en-us/windows/win32/api/d3d11/ne-d3d11-d3d11_usage
145 * +----------------------------------------------------------+
146 * | Resource Usage | Default | Dynamic | Immutable | Staging |
147 * +----------------+---------+---------+-----------+---------+
148 * | GPU-Read | Yes | Yes | Yes | Yes |
149 * | GPU-Write | Yes | | | Yes |
150 * | CPU-Read | | | | Yes |
151 * | CPU-Write | | Yes | | Yes |
152 * +----------------------------------------------------------+
155 /* If corresponding dxgi format is undefined, use resource format instead */
156 if (d3d11_format.dxgi_format == DXGI_FORMAT_UNKNOWN) {
157 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (info); i++) {
158 g_assert (d3d11_format.resource_format[i] != DXGI_FORMAT_UNKNOWN);
160 ret->desc[i].Width = GST_VIDEO_INFO_COMP_WIDTH (info, i);
161 ret->desc[i].Height = GST_VIDEO_INFO_COMP_HEIGHT (info, i);
162 ret->desc[i].MipLevels = 1;
163 ret->desc[i].ArraySize = 1;
164 ret->desc[i].Format = d3d11_format.resource_format[i];
165 ret->desc[i].SampleDesc.Count = 1;
166 ret->desc[i].SampleDesc.Quality = 0;
167 ret->desc[i].Usage = D3D11_USAGE_DEFAULT;
168 ret->desc[i].BindFlags = bind_flags;
169 ret->desc[i].MiscFlags = misc_flags;
172 ret->desc[0].Width = GST_VIDEO_INFO_WIDTH (info);
173 ret->desc[0].Height = GST_VIDEO_INFO_HEIGHT (info);
174 ret->desc[0].MipLevels = 1;
175 ret->desc[0].ArraySize = 1;
176 ret->desc[0].Format = d3d11_format.dxgi_format;
177 ret->desc[0].SampleDesc.Count = 1;
178 ret->desc[0].SampleDesc.Quality = 0;
179 ret->desc[0].Usage = D3D11_USAGE_DEFAULT;
180 ret->desc[0].BindFlags = bind_flags;
181 ret->desc[0].MiscFlags = misc_flags;
190 * gst_d3d11_allocation_params_alignment:
191 * @params: a #GstD3D11AllocationParams
192 * @align: a #GstVideoAlignment
194 * Adjust Width and Height fields of D3D11_TEXTURE2D_DESC with given
197 * Returns: %TRUE if alignment could be applied
202 gst_d3d11_allocation_params_alignment (GstD3D11AllocationParams * params,
203 const GstVideoAlignment * align)
206 guint padding_width, padding_height;
208 GstVideoInfo new_info;
210 g_return_val_if_fail (params != NULL, FALSE);
211 g_return_val_if_fail (align != NULL, FALSE);
213 /* d3d11 does not support stride align. Consider padding only */
214 padding_width = align->padding_left + align->padding_right;
215 padding_height = align->padding_top + align->padding_bottom;
217 info = ¶ms->info;
219 if (!gst_video_info_set_format (&new_info, GST_VIDEO_INFO_FORMAT (info),
220 GST_VIDEO_INFO_WIDTH (info) + padding_width,
221 GST_VIDEO_INFO_HEIGHT (info) + padding_height)) {
222 GST_WARNING ("Set format fail");
226 params->aligned_info = new_info;
228 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (info); i++) {
229 params->desc[i].Width = GST_VIDEO_INFO_COMP_WIDTH (&new_info, i);
230 params->desc[i].Height = GST_VIDEO_INFO_COMP_HEIGHT (&new_info, i);
237 * gst_d3d11_allocation_params_copy:
238 * @src: a #GstD3D11AllocationParams
240 * Returns: a copy of @src
244 GstD3D11AllocationParams *
245 gst_d3d11_allocation_params_copy (GstD3D11AllocationParams * src)
247 GstD3D11AllocationParams *dst;
249 g_return_val_if_fail (src != NULL, NULL);
251 dst = g_new0 (GstD3D11AllocationParams, 1);
252 memcpy (dst, src, sizeof (GstD3D11AllocationParams));
258 * gst_d3d11_allocation_params_free:
259 * @params: a #GstD3D11AllocationParams
266 gst_d3d11_allocation_params_free (GstD3D11AllocationParams * params)
272 gst_d3d11_allocation_params_compare (const GstD3D11AllocationParams * p1,
273 const GstD3D11AllocationParams * p2)
275 g_return_val_if_fail (p1 != NULL, -1);
276 g_return_val_if_fail (p2 != NULL, -1);
285 gst_d3d11_allocation_params_init (GType type)
287 static GstValueTable table = {
288 0, (GstValueCompareFunc) gst_d3d11_allocation_params_compare,
293 gst_value_register (&table);
297 #define GST_D3D11_MEMORY_GET_LOCK(m) (&(GST_D3D11_MEMORY_CAST(m)->priv->lock))
299 struct _GstD3D11MemoryPrivate
301 ID3D11Texture2D *texture;
302 ID3D11Buffer *buffer;
304 GstD3D11MemoryNativeType native_type;
306 D3D11_TEXTURE2D_DESC desc;
307 D3D11_BUFFER_DESC buffer_desc;
309 guint subresource_index;
311 /* protected by device lock */
312 ID3D11Resource *staging;
313 D3D11_MAPPED_SUBRESOURCE map;
316 /* protects resource objects */
318 ID3D11ShaderResourceView *shader_resource_view[GST_VIDEO_MAX_PLANES];
319 guint num_shader_resource_views;
321 ID3D11RenderTargetView *render_target_view[GST_VIDEO_MAX_PLANES];
322 guint num_render_target_views;
324 ID3D11VideoDecoderOutputView *decoder_output_view;
325 ID3D11VideoDecoder *decoder_handle;
327 ID3D11VideoProcessorInputView *processor_input_view;
328 ID3D11VideoProcessorOutputView *processor_output_view;
330 GDestroyNotify notify;
334 static inline D3D11_MAP
335 gst_d3d11_map_flags_to_d3d11 (GstMapFlags flags)
337 if ((flags & GST_MAP_READWRITE) == GST_MAP_READWRITE)
338 return D3D11_MAP_READ_WRITE;
339 else if ((flags & GST_MAP_WRITE) == GST_MAP_WRITE)
340 return D3D11_MAP_WRITE;
341 else if ((flags & GST_MAP_READ) == GST_MAP_READ)
342 return D3D11_MAP_READ;
344 g_assert_not_reached ();
346 return D3D11_MAP_READ;
349 static ID3D11Texture2D *
350 gst_d3d11_allocate_staging_texture (GstD3D11Device * device,
351 const D3D11_TEXTURE2D_DESC * ref)
353 D3D11_TEXTURE2D_DESC desc = { 0, };
354 ID3D11Texture2D *texture = NULL;
355 ID3D11Device *device_handle = gst_d3d11_device_get_device_handle (device);
358 desc.Width = ref->Width;
359 desc.Height = ref->Height;
361 desc.Format = ref->Format;
362 desc.SampleDesc.Count = 1;
364 desc.Usage = D3D11_USAGE_STAGING;
365 desc.CPUAccessFlags = (D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE);
367 hr = device_handle->CreateTexture2D (&desc, NULL, &texture);
368 if (!gst_d3d11_result (hr, device)) {
369 GST_ERROR_OBJECT (device, "Failed to create texture");
376 /* Must be called with d3d11 device lock */
378 gst_d3d11_memory_map_cpu_access (GstD3D11Memory * dmem, D3D11_MAP map_type)
380 GstD3D11MemoryPrivate *priv = dmem->priv;
382 ID3D11DeviceContext *device_context =
383 gst_d3d11_device_get_device_context_handle (dmem->device);
385 hr = device_context->Map (priv->staging, 0, map_type, 0, &priv->map);
387 if (!gst_d3d11_result (hr, dmem->device)) {
388 GST_ERROR_OBJECT (GST_MEMORY_CAST (dmem)->allocator,
389 "Failed to map staging texture (0x%x)", (guint) hr);
396 /* Must be called with d3d11 device lock */
398 gst_d3d11_memory_upload (GstD3D11Memory * dmem)
400 GstD3D11MemoryPrivate *priv = dmem->priv;
401 ID3D11DeviceContext *device_context;
403 if (!priv->staging ||
404 !GST_MEMORY_FLAG_IS_SET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD))
407 device_context = gst_d3d11_device_get_device_context_handle (dmem->device);
408 device_context->CopySubresourceRegion (priv->texture, priv->subresource_index,
409 0, 0, 0, priv->staging, 0, NULL);
412 /* Must be called with d3d11 device lock */
414 gst_d3d11_memory_download (GstD3D11Memory * dmem)
416 GstD3D11MemoryPrivate *priv = dmem->priv;
417 ID3D11DeviceContext *device_context;
419 if (!priv->staging ||
420 !GST_MEMORY_FLAG_IS_SET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD))
423 device_context = gst_d3d11_device_get_device_context_handle (dmem->device);
424 device_context->CopySubresourceRegion (priv->staging, 0, 0, 0, 0,
425 priv->texture, priv->subresource_index, NULL);
429 gst_d3d11_memory_map_full (GstMemory * mem, GstMapInfo * info, gsize maxsize)
431 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
432 GstD3D11MemoryPrivate *priv = dmem->priv;
433 GstMapFlags flags = info->flags;
434 GstD3D11DeviceLockGuard lk (dmem->device);
436 memset (info->user_data, 0, sizeof (info->user_data));
437 info->user_data[0] = GUINT_TO_POINTER (dmem->priv->subresource_index);
439 if ((flags & GST_MAP_D3D11) == GST_MAP_D3D11) {
440 if (priv->native_type == GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER) {
441 /* FIXME: handle non-staging buffer */
442 g_assert (priv->buffer != nullptr);
445 gst_d3d11_memory_upload (dmem);
446 GST_MEMORY_FLAG_UNSET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD);
448 if ((flags & GST_MAP_WRITE) == GST_MAP_WRITE)
449 GST_MINI_OBJECT_FLAG_SET (dmem,
450 GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
452 g_assert (priv->texture != NULL);
453 return priv->texture;
457 if (priv->cpu_map_count == 0) {
460 /* FIXME: handle non-staging buffer */
461 if (priv->native_type == GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D) {
462 /* Allocate staging texture for CPU access */
463 if (!priv->staging) {
464 priv->staging = gst_d3d11_allocate_staging_texture (dmem->device,
466 if (!priv->staging) {
467 GST_ERROR_OBJECT (mem->allocator, "Couldn't create staging texture");
471 /* first memory, always need download to staging */
472 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
475 gst_d3d11_memory_download (dmem);
478 map_type = gst_d3d11_map_flags_to_d3d11 (flags);
479 if (!gst_d3d11_memory_map_cpu_access (dmem, map_type)) {
480 GST_ERROR_OBJECT (mem->allocator, "Couldn't map staging texture");
485 if ((flags & GST_MAP_WRITE) == GST_MAP_WRITE) {
486 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD);
489 GST_MEMORY_FLAG_UNSET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
491 priv->cpu_map_count++;
492 return dmem->priv->map.pData;
495 /* Must be called with d3d11 device lock */
497 gst_d3d11_memory_unmap_cpu_access (GstD3D11Memory * dmem)
499 GstD3D11MemoryPrivate *priv = dmem->priv;
500 ID3D11DeviceContext *device_context =
501 gst_d3d11_device_get_device_context_handle (dmem->device);
503 device_context->Unmap (priv->staging, 0);
507 gst_d3d11_memory_unmap_full (GstMemory * mem, GstMapInfo * info)
509 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
510 GstD3D11MemoryPrivate *priv = dmem->priv;
511 GstD3D11DeviceLockGuard lk (dmem->device);
513 if ((info->flags & GST_MAP_D3D11) == GST_MAP_D3D11) {
514 if ((info->flags & GST_MAP_WRITE) == GST_MAP_WRITE)
515 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
520 if ((info->flags & GST_MAP_WRITE) == GST_MAP_WRITE)
521 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD);
523 priv->cpu_map_count--;
524 if (priv->cpu_map_count > 0)
527 gst_d3d11_memory_unmap_cpu_access (dmem);
531 gst_d3d11_memory_share (GstMemory * mem, gssize offset, gssize size)
538 gst_d3d11_memory_update_size (GstMemory * mem)
540 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
541 GstD3D11MemoryPrivate *priv = dmem->priv;
542 gsize offset[GST_VIDEO_MAX_PLANES];
543 gint stride[GST_VIDEO_MAX_PLANES];
545 D3D11_TEXTURE2D_DESC *desc = &priv->desc;
547 if (!priv->staging) {
548 priv->staging = gst_d3d11_allocate_staging_texture (dmem->device,
550 if (!priv->staging) {
551 GST_ERROR_OBJECT (mem->allocator, "Couldn't create staging texture");
556 GstD3D11DeviceLockGuard lk (dmem->device);
557 if (!gst_d3d11_memory_map_cpu_access (dmem, D3D11_MAP_READ_WRITE)) {
558 GST_ERROR_OBJECT (mem->allocator, "Couldn't map staging texture");
562 gst_d3d11_memory_unmap_cpu_access (dmem);
564 if (!gst_d3d11_dxgi_format_get_size (desc->Format, desc->Width, desc->Height,
565 priv->map.RowPitch, offset, stride, &size)) {
566 GST_ERROR_OBJECT (mem->allocator, "Couldn't calculate memory size");
567 GST_D3D11_CLEAR_COM (priv->staging);
571 GST_D3D11_CLEAR_COM (priv->staging);
572 mem->maxsize = mem->size = size;
578 * gst_is_d3d11_memory:
581 * Returns: whether @mem is a #GstD3D11Memory
586 gst_is_d3d11_memory (GstMemory * mem)
588 return mem != NULL && mem->allocator != NULL &&
589 (GST_IS_D3D11_ALLOCATOR (mem->allocator) ||
590 GST_IS_D3D11_POOL_ALLOCATOR (mem->allocator));
594 * gst_d3d11_memory_get_native_type:
595 * @mem: a #GstD3D11Memory
597 * Returns: a #GstD3D11MemoryNativeType
601 GstD3D11MemoryNativeType
602 gst_d3d11_memory_get_native_type (GstD3D11Memory * mem)
604 if (!gst_is_d3d11_memory (GST_MEMORY_CAST (mem)))
605 return GST_D3D11_MEMORY_NATIVE_TYPE_INVALID;
607 return mem->priv->native_type;
611 * gst_d3d11_memory_init_once:
613 * Initializes the Direct3D11 Texture allocator. It is safe to call
614 * this function multiple times. This must be called before any other
615 * GstD3D11Memory operation.
620 gst_d3d11_memory_init_once (void)
622 GST_D3D11_CALL_ONCE_BEGIN {
623 GST_DEBUG_CATEGORY_INIT (gst_d3d11_allocator_debug, "d3d11allocator", 0,
624 "Direct3D11 Texture Allocator");
626 _d3d11_memory_allocator =
627 (GstD3D11Allocator *) g_object_new (GST_TYPE_D3D11_ALLOCATOR, NULL);
628 gst_object_ref_sink (_d3d11_memory_allocator);
629 gst_object_ref (_d3d11_memory_allocator);
631 gst_allocator_register (GST_D3D11_MEMORY_NAME,
632 GST_ALLOCATOR_CAST (_d3d11_memory_allocator));
633 } GST_D3D11_CALL_ONCE_END;
637 * gst_d3d11_memory_get_resource_handle:
638 * @mem: a #GstD3D11Memory
640 * Returns: (transfer none): a ID3D11Resource handle. Caller must not release
646 gst_d3d11_memory_get_resource_handle (GstD3D11Memory * mem)
648 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
650 switch (mem->priv->native_type) {
651 case GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER:
652 return mem->priv->buffer;
653 case GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D:
654 return mem->priv->texture;
663 * gst_d3d11_memory_get_subresource_index:
664 * @mem: a #GstD3D11Memory
666 * Returns: subresource index corresponding to @mem.
671 gst_d3d11_memory_get_subresource_index (GstD3D11Memory * mem)
673 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), 0);
675 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
678 return mem->priv->subresource_index;
682 * gst_d3d11_memory_get_texture_desc:
683 * @mem: a #GstD3D11Memory
684 * @desc: (out): a D3D11_TEXTURE2D_DESC
686 * Fill @desc with D3D11_TEXTURE2D_DESC for ID3D11Texture2D
688 * Returns: %TRUE if successeed
693 gst_d3d11_memory_get_texture_desc (GstD3D11Memory * mem,
694 D3D11_TEXTURE2D_DESC * desc)
696 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), FALSE);
697 g_return_val_if_fail (desc != NULL, FALSE);
699 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
702 *desc = mem->priv->desc;
708 * gst_d3d11_memory_get_buffer_desc:
709 * @mem: a #GstD3D11Memory
710 * @desc: (out): a D3D11_BUFFER_DESC
712 * Fill @desc with D3D11_BUFFER_DESC for ID3D11Buffer
714 * Returns: %TRUE if successeed
719 gst_d3d11_memory_get_buffer_desc (GstD3D11Memory * mem,
720 D3D11_BUFFER_DESC * desc)
722 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), FALSE);
723 g_return_val_if_fail (desc != NULL, FALSE);
725 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER)
728 *desc = mem->priv->buffer_desc;
734 * gst_d3d11_memory_get_resource_stride:
735 * @mem: a #GstD3D11Memory
736 * @stride: (out): stride of resource
738 * Returns: %TRUE if successeed
743 gst_d3d11_memory_get_resource_stride (GstD3D11Memory * mem, guint * stride)
745 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), FALSE);
746 g_return_val_if_fail (stride != NULL, FALSE);
748 *stride = mem->priv->map.RowPitch;
754 create_shader_resource_views (GstD3D11Memory * mem)
756 GstD3D11MemoryPrivate *priv = mem->priv;
760 ID3D11Device *device_handle;
761 D3D11_SHADER_RESOURCE_VIEW_DESC resource_desc;
762 DXGI_FORMAT formats[GST_VIDEO_MAX_PLANES] = { DXGI_FORMAT_UNKNOWN, };
764 memset (&resource_desc, 0, sizeof (D3D11_SHADER_RESOURCE_VIEW_DESC));
766 device_handle = gst_d3d11_device_get_device_handle (mem->device);
768 num_views = gst_d3d11_dxgi_format_get_resource_format (priv->desc.Format,
771 GST_ERROR_OBJECT (GST_MEMORY_CAST (mem)->allocator,
772 "Unknown resource formats for DXGI format %s (%d)",
773 gst_d3d11_dxgi_format_to_string (priv->desc.Format), priv->desc.Format);
777 if ((priv->desc.BindFlags & D3D11_BIND_SHADER_RESOURCE) != 0) {
778 resource_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
779 resource_desc.Texture2D.MipLevels = 1;
781 for (i = 0; i < num_views; i++) {
782 resource_desc.Format = formats[i];
783 hr = device_handle->CreateShaderResourceView (priv->texture,
784 &resource_desc, &priv->shader_resource_view[i]);
786 if (!gst_d3d11_result (hr, mem->device)) {
787 GST_ERROR_OBJECT (GST_MEMORY_CAST (mem)->allocator,
788 "Failed to create resource DXGI format %s (%d) for plane %d"
789 " view (0x%x)", gst_d3d11_dxgi_format_to_string (formats[i]),
790 formats[i], i, (guint) hr);
795 priv->num_shader_resource_views = num_views;
803 for (i = 0; i < num_views; i++)
804 GST_D3D11_CLEAR_COM (priv->shader_resource_view[i]);
806 priv->num_shader_resource_views = 0;
812 gst_d3d11_memory_ensure_shader_resource_view (GstD3D11Memory * mem)
814 GstD3D11MemoryPrivate *priv = mem->priv;
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 GstD3D11SRWLockGuard lk (GST_D3D11_MEMORY_GET_LOCK (mem));
826 if (priv->num_shader_resource_views)
829 return create_shader_resource_views (mem);
833 * gst_d3d11_memory_get_shader_resource_view_size:
834 * @mem: a #GstD3D11Memory
836 * Returns: the number of ID3D11ShaderResourceView that can be used
837 * for processing GPU operation with @mem
842 gst_d3d11_memory_get_shader_resource_view_size (GstD3D11Memory * mem)
844 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), 0);
846 if (!gst_d3d11_memory_ensure_shader_resource_view (mem))
849 return mem->priv->num_shader_resource_views;
853 * gst_d3d11_memory_get_shader_resource_view:
854 * @mem: a #GstD3D11Memory
855 * @index: the index of the ID3D11ShaderResourceView
857 * Returns: (transfer none) (nullable): a pointer to the
858 * ID3D11ShaderResourceView or %NULL if ID3D11ShaderResourceView is unavailable
863 ID3D11ShaderResourceView *
864 gst_d3d11_memory_get_shader_resource_view (GstD3D11Memory * mem, guint index)
866 GstD3D11MemoryPrivate *priv;
868 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
870 if (!gst_d3d11_memory_ensure_shader_resource_view (mem))
875 if (index >= priv->num_shader_resource_views) {
876 GST_ERROR ("Invalid SRV index %d", index);
880 return priv->shader_resource_view[index];
884 create_render_target_views (GstD3D11Memory * mem)
886 GstD3D11MemoryPrivate *priv = mem->priv;
890 ID3D11Device *device_handle;
891 D3D11_RENDER_TARGET_VIEW_DESC render_desc;
892 DXGI_FORMAT formats[GST_VIDEO_MAX_PLANES] = { DXGI_FORMAT_UNKNOWN, };
894 memset (&render_desc, 0, sizeof (D3D11_RENDER_TARGET_VIEW_DESC));
896 device_handle = gst_d3d11_device_get_device_handle (mem->device);
898 num_views = gst_d3d11_dxgi_format_get_resource_format (priv->desc.Format,
901 GST_ERROR_OBJECT (GST_MEMORY_CAST (mem)->allocator,
902 "Unknown resource formats for DXGI format %s (%d)",
903 gst_d3d11_dxgi_format_to_string (priv->desc.Format), priv->desc.Format);
907 if ((priv->desc.BindFlags & D3D11_BIND_RENDER_TARGET) != 0) {
908 render_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
909 render_desc.Texture2D.MipSlice = 0;
911 for (i = 0; i < num_views; i++) {
912 render_desc.Format = formats[i];
914 hr = device_handle->CreateRenderTargetView (priv->texture, &render_desc,
915 &priv->render_target_view[i]);
916 if (!gst_d3d11_result (hr, mem->device)) {
917 GST_ERROR_OBJECT (GST_MEMORY_CAST (mem)->allocator,
918 "Failed to create resource DXGI format %s (%d) for plane %d"
919 " view (0x%x)", gst_d3d11_dxgi_format_to_string (formats[i]),
920 formats[i], i, (guint) hr);
925 priv->num_render_target_views = num_views;
933 for (i = 0; i < num_views; i++)
934 GST_D3D11_CLEAR_COM (priv->render_target_view[i]);
936 priv->num_render_target_views = 0;
942 gst_d3d11_memory_ensure_render_target_view (GstD3D11Memory * mem)
944 GstD3D11MemoryPrivate *priv = mem->priv;
946 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
949 if (!(priv->desc.BindFlags & D3D11_BIND_RENDER_TARGET)) {
950 GST_WARNING_OBJECT (GST_MEMORY_CAST (mem)->allocator,
951 "Need BindFlags, current flag 0x%x", priv->desc.BindFlags);
955 GstD3D11SRWLockGuard lk (GST_D3D11_MEMORY_GET_LOCK (mem));
956 if (priv->num_render_target_views)
959 return create_render_target_views (mem);
963 * gst_d3d11_memory_get_render_target_view_size:
964 * @mem: a #GstD3D11Memory
966 * Returns: the number of ID3D11RenderTargetView that can be used
967 * for processing GPU operation with @mem
972 gst_d3d11_memory_get_render_target_view_size (GstD3D11Memory * mem)
974 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), 0);
976 if (!gst_d3d11_memory_ensure_render_target_view (mem))
979 return mem->priv->num_render_target_views;
983 * gst_d3d11_memory_get_render_target_view:
984 * @mem: a #GstD3D11Memory
985 * @index: the index of the ID3D11RenderTargetView
987 * Returns: (transfer none) (nullable): a pointer to the
988 * ID3D11RenderTargetView or %NULL if ID3D11RenderTargetView is unavailable
993 ID3D11RenderTargetView *
994 gst_d3d11_memory_get_render_target_view (GstD3D11Memory * mem, guint index)
996 GstD3D11MemoryPrivate *priv;
998 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1000 if (!gst_d3d11_memory_ensure_render_target_view (mem))
1005 if (index >= priv->num_render_target_views) {
1006 GST_ERROR ("Invalid RTV index %d", index);
1010 return priv->render_target_view[index];
1014 gst_d3d11_memory_ensure_decoder_output_view (GstD3D11Memory * mem,
1015 ID3D11VideoDevice * video_device, ID3D11VideoDecoder * decoder,
1016 const GUID * decoder_profile)
1018 GstD3D11MemoryPrivate *dmem_priv = mem->priv;
1019 GstD3D11Allocator *allocator;
1020 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC desc;
1023 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
1026 allocator = GST_D3D11_ALLOCATOR (GST_MEMORY_CAST (mem)->allocator);
1028 if (!(dmem_priv->desc.BindFlags & D3D11_BIND_DECODER)) {
1029 GST_LOG_OBJECT (allocator,
1030 "Need BindFlags, current flag 0x%x", dmem_priv->desc.BindFlags);
1034 GstD3D11SRWLockGuard lk (GST_D3D11_MEMORY_GET_LOCK (mem));
1035 if (dmem_priv->decoder_output_view) {
1036 dmem_priv->decoder_output_view->GetDesc (&desc);
1037 if (IsEqualGUID (desc.DecodeProfile, *decoder_profile) &&
1038 dmem_priv->decoder_handle == decoder) {
1041 /* Shouldn't happen, but try again anyway */
1042 GST_WARNING_OBJECT (allocator,
1043 "Existing view has different decoder profile");
1044 GST_D3D11_CLEAR_COM (dmem_priv->decoder_output_view);
1045 GST_D3D11_CLEAR_COM (dmem_priv->decoder_handle);
1049 desc.DecodeProfile = *decoder_profile;
1050 desc.ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D;
1051 desc.Texture2D.ArraySlice = dmem_priv->subresource_index;
1053 hr = video_device->CreateVideoDecoderOutputView (dmem_priv->texture, &desc,
1054 &dmem_priv->decoder_output_view);
1055 if (!gst_d3d11_result (hr, mem->device)) {
1056 GST_ERROR_OBJECT (allocator,
1057 "Could not create decoder output view, hr: 0x%x", (guint) hr);
1061 /* XXX: decoder output view is bound to video device, not decoder handle
1062 * from API point of view. But some driver seems to be unhappy
1063 * when decoder handle is released while there are outstanding view objects */
1064 dmem_priv->decoder_handle = decoder;
1071 * gst_d3d11_memory_get_decoder_output_view:
1072 * @mem: a #GstD3D11Memory
1073 * @video_device: (transfer none): a ID3D11VideoDevice handle
1074 * @decoder: (transfer none): a ID3D11VideoDecoder handle
1075 * @decoder_profile: a DXVA decoder profile GUID
1077 * Returns: (transfer none) (nullable): a pointer to the
1078 * ID3D11VideoDecoderOutputView or %NULL if ID3D11VideoDecoderOutputView is
1083 ID3D11VideoDecoderOutputView *
1084 gst_d3d11_memory_get_decoder_output_view (GstD3D11Memory * mem,
1085 ID3D11VideoDevice * video_device, ID3D11VideoDecoder * decoder,
1086 const GUID * decoder_profile)
1088 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1089 g_return_val_if_fail (video_device != NULL, NULL);
1090 g_return_val_if_fail (decoder != NULL, NULL);
1091 g_return_val_if_fail (decoder_profile != NULL, NULL);
1093 if (!gst_d3d11_memory_ensure_decoder_output_view (mem,
1094 video_device, decoder, decoder_profile))
1097 return mem->priv->decoder_output_view;
1101 check_bind_flags_for_processor_input_view (guint bind_flags)
1103 static const guint compatible_flags = (D3D11_BIND_DECODER |
1104 D3D11_BIND_VIDEO_ENCODER | D3D11_BIND_RENDER_TARGET |
1105 D3D11_BIND_UNORDERED_ACCESS);
1107 if (bind_flags == 0)
1110 if ((bind_flags & compatible_flags) != 0)
1117 gst_d3d11_memory_ensure_processor_input_view (GstD3D11Memory * mem,
1118 ID3D11VideoDevice * video_device,
1119 ID3D11VideoProcessorEnumerator * enumerator)
1121 GstD3D11MemoryPrivate *dmem_priv = mem->priv;
1122 GstD3D11Allocator *allocator;
1123 D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC desc;
1126 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
1129 allocator = GST_D3D11_ALLOCATOR (GST_MEMORY_CAST (mem)->allocator);
1131 if (!check_bind_flags_for_processor_input_view (dmem_priv->desc.BindFlags)) {
1132 GST_LOG_OBJECT (allocator,
1133 "Need BindFlags, current flag 0x%x", dmem_priv->desc.BindFlags);
1137 GstD3D11SRWLockGuard lk (GST_D3D11_MEMORY_GET_LOCK (mem));
1138 if (dmem_priv->processor_input_view)
1142 desc.ViewDimension = D3D11_VPIV_DIMENSION_TEXTURE2D;
1143 desc.Texture2D.MipSlice = 0;
1144 desc.Texture2D.ArraySlice = dmem_priv->subresource_index;
1146 hr = video_device->CreateVideoProcessorInputView (dmem_priv->texture,
1147 enumerator, &desc, &dmem_priv->processor_input_view);
1148 if (!gst_d3d11_result (hr, mem->device)) {
1149 GST_ERROR_OBJECT (allocator,
1150 "Could not create processor input view, hr: 0x%x", (guint) hr);
1158 * gst_d3d11_memory_get_processor_input_view:
1159 * @mem: a #GstD3D11Memory
1160 * @video_device: a #ID3D11VideoDevice
1161 * @enumerator: a #ID3D11VideoProcessorEnumerator
1163 * Returns: (transfer none) (nullable): a pointer to the
1164 * ID3D11VideoProcessorInputView or %NULL if ID3D11VideoProcessorInputView is
1169 ID3D11VideoProcessorInputView *
1170 gst_d3d11_memory_get_processor_input_view (GstD3D11Memory * mem,
1171 ID3D11VideoDevice * video_device,
1172 ID3D11VideoProcessorEnumerator * enumerator)
1174 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1175 g_return_val_if_fail (video_device != NULL, NULL);
1176 g_return_val_if_fail (enumerator != NULL, NULL);
1178 if (!gst_d3d11_memory_ensure_processor_input_view (mem, video_device,
1182 return mem->priv->processor_input_view;
1186 gst_d3d11_memory_ensure_processor_output_view (GstD3D11Memory * mem,
1187 ID3D11VideoDevice * video_device,
1188 ID3D11VideoProcessorEnumerator * enumerator)
1190 GstD3D11MemoryPrivate *priv = mem->priv;
1191 GstD3D11Allocator *allocator;
1192 D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC desc;
1195 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
1198 memset (&desc, 0, sizeof (D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC));
1200 allocator = GST_D3D11_ALLOCATOR (GST_MEMORY_CAST (mem)->allocator);
1202 if (!(priv->desc.BindFlags & D3D11_BIND_RENDER_TARGET)) {
1203 GST_LOG_OBJECT (allocator,
1204 "Need BindFlags, current flag 0x%x", priv->desc.BindFlags);
1208 /* FIXME: texture array should be supported at some point */
1209 if (priv->subresource_index != 0) {
1210 GST_FIXME_OBJECT (allocator,
1211 "Texture array is not suppoted for processor output view");
1215 GstD3D11SRWLockGuard lk (GST_D3D11_MEMORY_GET_LOCK (mem));
1216 if (priv->processor_output_view)
1219 desc.ViewDimension = D3D11_VPOV_DIMENSION_TEXTURE2D;
1220 desc.Texture2D.MipSlice = 0;
1222 hr = video_device->CreateVideoProcessorOutputView (priv->texture,
1223 enumerator, &desc, &priv->processor_output_view);
1224 if (!gst_d3d11_result (hr, mem->device)) {
1225 GST_ERROR_OBJECT (allocator,
1226 "Could not create processor input view, hr: 0x%x", (guint) hr);
1234 * gst_d3d11_memory_get_processor_output_view:
1235 * @mem: a #GstD3D11Memory
1236 * @video_device: a #ID3D11VideoDevice
1237 * @enumerator: a #ID3D11VideoProcessorEnumerator
1239 * Returns: (transfer none) (nullable): a pointer to the
1240 * ID3D11VideoProcessorOutputView or %NULL if ID3D11VideoProcessorOutputView is
1245 ID3D11VideoProcessorOutputView *
1246 gst_d3d11_memory_get_processor_output_view (GstD3D11Memory * mem,
1247 ID3D11VideoDevice * video_device,
1248 ID3D11VideoProcessorEnumerator * enumerator)
1250 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1251 g_return_val_if_fail (video_device != NULL, NULL);
1252 g_return_val_if_fail (enumerator != NULL, NULL);
1254 if (!gst_d3d11_memory_ensure_processor_output_view (mem, video_device,
1258 return mem->priv->processor_output_view;
1261 /* GstD3D11Allocator */
1262 struct _GstD3D11AllocatorPrivate
1264 GstMemoryCopyFunction fallback_copy;
1267 #define gst_d3d11_allocator_parent_class alloc_parent_class
1268 G_DEFINE_TYPE_WITH_PRIVATE (GstD3D11Allocator,
1269 gst_d3d11_allocator, GST_TYPE_ALLOCATOR);
1271 static GstMemory *gst_d3d11_allocator_dummy_alloc (GstAllocator * allocator,
1272 gsize size, GstAllocationParams * params);
1273 static GstMemory *gst_d3d11_allocator_alloc_internal (GstD3D11Allocator * self,
1274 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc,
1275 ID3D11Texture2D * texture);
1276 static void gst_d3d11_allocator_free (GstAllocator * allocator,
1280 gst_d3d11_allocator_class_init (GstD3D11AllocatorClass * klass)
1282 GstAllocatorClass *allocator_class = GST_ALLOCATOR_CLASS (klass);
1284 allocator_class->alloc = gst_d3d11_allocator_dummy_alloc;
1285 allocator_class->free = gst_d3d11_allocator_free;
1289 gst_d3d11_memory_copy (GstMemory * mem, gssize offset, gssize size)
1291 GstD3D11Allocator *alloc = GST_D3D11_ALLOCATOR (mem->allocator);
1292 GstD3D11AllocatorPrivate *priv = alloc->priv;
1293 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
1294 GstD3D11Memory *copy_dmem;
1295 GstD3D11Device *device = dmem->device;
1296 ID3D11Device *device_handle = gst_d3d11_device_get_device_handle (device);
1297 ID3D11DeviceContext *device_context =
1298 gst_d3d11_device_get_device_context_handle (device);
1299 D3D11_TEXTURE2D_DESC dst_desc = { 0, };
1300 D3D11_TEXTURE2D_DESC src_desc = { 0, };
1301 GstMemory *copy = NULL;
1304 UINT bind_flags = 0;
1305 UINT supported_flags = 0;
1307 if (dmem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
1308 return priv->fallback_copy (mem, offset, size);
1310 /* non-zero offset or different size is not supported */
1311 if (offset != 0 || (size != -1 && (gsize) size != mem->size)) {
1312 GST_DEBUG_OBJECT (alloc, "Different size/offset, try fallback copy");
1313 return priv->fallback_copy (mem, offset, size);
1316 GstD3D11DeviceLockGuard lk (device);
1318 if (!gst_memory_map (mem, &info,
1319 (GstMapFlags) (GST_MAP_READ | GST_MAP_D3D11))) {
1321 GST_WARNING_OBJECT (alloc, "Failed to map memory, try fallback copy");
1323 return priv->fallback_copy (mem, offset, size);
1326 dmem->priv->texture->GetDesc (&src_desc);
1327 dst_desc.Width = src_desc.Width;
1328 dst_desc.Height = src_desc.Height;
1329 dst_desc.MipLevels = 1;
1330 dst_desc.Format = src_desc.Format;
1331 dst_desc.SampleDesc.Count = 1;
1332 dst_desc.ArraySize = 1;
1333 dst_desc.Usage = D3D11_USAGE_DEFAULT;
1335 /* If supported, use bind flags for SRV/RTV */
1336 hr = device_handle->CheckFormatSupport (src_desc.Format, &supported_flags);
1337 if (gst_d3d11_result (hr, device)) {
1338 if ((supported_flags & D3D11_FORMAT_SUPPORT_SHADER_SAMPLE) ==
1339 D3D11_FORMAT_SUPPORT_SHADER_SAMPLE) {
1340 bind_flags |= D3D11_BIND_SHADER_RESOURCE;
1343 if ((supported_flags & D3D11_FORMAT_SUPPORT_RENDER_TARGET) ==
1344 D3D11_FORMAT_SUPPORT_RENDER_TARGET) {
1345 bind_flags |= D3D11_BIND_RENDER_TARGET;
1349 copy = gst_d3d11_allocator_alloc_internal (alloc, device, &dst_desc, nullptr);
1351 gst_memory_unmap (mem, &info);
1353 GST_WARNING_OBJECT (alloc,
1354 "Failed to allocate new d3d11 map memory, try fallback copy");
1356 return priv->fallback_copy (mem, offset, size);
1359 copy_dmem = GST_D3D11_MEMORY_CAST (copy);
1360 device_context->CopySubresourceRegion (copy_dmem->priv->texture, 0, 0, 0, 0,
1361 dmem->priv->texture, dmem->priv->subresource_index, NULL);
1362 copy->maxsize = copy->size = mem->maxsize;
1363 gst_memory_unmap (mem, &info);
1365 /* newly allocated memory holds valid image data. We need download this
1366 * pixel data into staging memory for CPU access */
1367 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
1373 gst_d3d11_allocator_init (GstD3D11Allocator * allocator)
1375 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
1376 GstD3D11AllocatorPrivate *priv;
1378 priv = allocator->priv = (GstD3D11AllocatorPrivate *)
1379 gst_d3d11_allocator_get_instance_private (allocator);
1381 alloc->mem_type = GST_D3D11_MEMORY_NAME;
1382 alloc->mem_map_full = gst_d3d11_memory_map_full;
1383 alloc->mem_unmap_full = gst_d3d11_memory_unmap_full;
1384 alloc->mem_share = gst_d3d11_memory_share;
1386 /* Store pointer to default mem_copy method for fallback copy */
1387 priv->fallback_copy = alloc->mem_copy;
1388 alloc->mem_copy = gst_d3d11_memory_copy;
1390 GST_OBJECT_FLAG_SET (alloc, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
1394 gst_d3d11_allocator_dummy_alloc (GstAllocator * allocator, gsize size,
1395 GstAllocationParams * params)
1397 g_return_val_if_reached (NULL);
1401 gst_d3d11_allocator_free (GstAllocator * allocator, GstMemory * mem)
1403 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
1404 GstD3D11MemoryPrivate *dmem_priv = dmem->priv;
1407 GST_LOG_OBJECT (allocator, "Free memory %p", mem);
1409 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
1410 GST_D3D11_CLEAR_COM (dmem_priv->render_target_view[i]);
1411 GST_D3D11_CLEAR_COM (dmem_priv->shader_resource_view[i]);
1414 GST_D3D11_CLEAR_COM (dmem_priv->decoder_output_view);
1415 GST_D3D11_CLEAR_COM (dmem_priv->processor_input_view);
1416 GST_D3D11_CLEAR_COM (dmem_priv->processor_output_view);
1417 GST_D3D11_CLEAR_COM (dmem_priv->texture);
1418 GST_D3D11_CLEAR_COM (dmem_priv->staging);
1419 GST_D3D11_CLEAR_COM (dmem_priv->buffer);
1421 GST_D3D11_CLEAR_COM (dmem_priv->decoder_handle);
1423 gst_clear_object (&dmem->device);
1425 if (dmem_priv->notify)
1426 dmem_priv->notify (dmem_priv->user_data);
1428 g_free (dmem->priv);
1433 gst_d3d11_allocator_alloc_wrapped_internal (GstD3D11Allocator * self,
1434 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc,
1435 ID3D11Texture2D * texture)
1437 GstD3D11Memory *mem;
1439 mem = g_new0 (GstD3D11Memory, 1);
1440 mem->priv = g_new0 (GstD3D11MemoryPrivate, 1);
1442 gst_memory_init (GST_MEMORY_CAST (mem),
1443 (GstMemoryFlags) 0, GST_ALLOCATOR_CAST (self), NULL, 0, 0, 0, 0);
1444 mem->priv->texture = texture;
1445 mem->priv->desc = *desc;
1446 mem->priv->native_type = GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D;
1447 mem->device = (GstD3D11Device *) gst_object_ref (device);
1449 return GST_MEMORY_CAST (mem);
1452 typedef void (*GstD3D11ClearRTVFunc) (ID3D11DeviceContext * context_handle,
1453 ID3D11RenderTargetView * rtv);
1456 clear_rtv_chroma (ID3D11DeviceContext * context_handle,
1457 ID3D11RenderTargetView * rtv)
1459 const FLOAT clear_color[4] = { 0.5f, 0.5f, 0.5f, 1.0f };
1461 context_handle->ClearRenderTargetView (rtv, clear_color);
1465 clear_rtv_vuya (ID3D11DeviceContext * context_handle,
1466 ID3D11RenderTargetView * rtv)
1468 const FLOAT clear_color[4] = { 0.5f, 0.5f, 0.0f, 1.0f };
1470 context_handle->ClearRenderTargetView (rtv, clear_color);
1474 gst_d3d11_allocator_alloc_internal (GstD3D11Allocator * self,
1475 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc,
1476 ID3D11Texture2D * texture)
1478 ID3D11Device *device_handle;
1479 ID3D11DeviceContext *context_handle;
1482 GstD3D11Memory *dmem;
1483 ID3D11RenderTargetView *rtv = nullptr;
1484 GstD3D11ClearRTVFunc clear_func = nullptr;
1486 device_handle = gst_d3d11_device_get_device_handle (device);
1489 hr = device_handle->CreateTexture2D (desc, nullptr, &texture);
1490 if (!gst_d3d11_result (hr, device)) {
1491 GST_ERROR_OBJECT (self, "Couldn't create texture");
1497 gst_d3d11_allocator_alloc_wrapped_internal (self, device, desc, texture);
1501 /* Clear with YUV black if needed and possible
1502 * TODO: do this using UAV if RTV is not allowed (e.g., packed YUV formats) */
1503 if ((desc->BindFlags & D3D11_BIND_RENDER_TARGET) == 0)
1506 dmem = GST_D3D11_MEMORY_CAST (mem);
1507 switch (desc->Format) {
1508 case DXGI_FORMAT_NV12:
1509 case DXGI_FORMAT_P010:
1510 case DXGI_FORMAT_P016:
1511 /* Y component will be zero already */
1512 rtv = gst_d3d11_memory_get_render_target_view (dmem, 1);
1513 clear_func = (GstD3D11ClearRTVFunc) clear_rtv_chroma;
1515 case DXGI_FORMAT_AYUV:
1516 rtv = gst_d3d11_memory_get_render_target_view (dmem, 0);
1517 clear_func = (GstD3D11ClearRTVFunc) clear_rtv_vuya;
1526 context_handle = gst_d3d11_device_get_device_context_handle (device);
1527 GstD3D11DeviceLockGuard lk (device);
1528 clear_func (context_handle, rtv);
1534 * gst_d3d11_allocator_alloc:
1535 * @allocator: (transfer none) (allow-none): a #GstD3D11Allocator
1536 * @device: (transfer none): a #GstD3D11Device
1537 * @desc: a D3D11_TEXTURE2D_DESC struct
1539 * Returns: a newly allocated #GstD3D11Memory with given parameters.
1544 gst_d3d11_allocator_alloc (GstD3D11Allocator * allocator,
1545 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc)
1550 gst_d3d11_memory_init_once ();
1551 allocator = _d3d11_memory_allocator;
1554 g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), NULL);
1555 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), NULL);
1556 g_return_val_if_fail (desc != NULL, NULL);
1558 mem = gst_d3d11_allocator_alloc_internal (allocator, device, desc, nullptr);
1562 if (!gst_d3d11_memory_update_size (mem)) {
1563 GST_ERROR_OBJECT (allocator, "Failed to calculate size");
1564 gst_memory_unref (mem);
1572 * gst_d3d11_allocator_alloc_buffer:
1573 * @allocator: (transfer none) (allow-none): a #GstD3D11Allocator
1574 * @device: (transfer none): a #GstD3D11Device
1575 * @desc: a D3D11_BUFFER_DESC struct
1577 * Returns: a newly allocated #GstD3D11Memory with given parameters.
1582 gst_d3d11_allocator_alloc_buffer (GstD3D11Allocator * allocator,
1583 GstD3D11Device * device, const D3D11_BUFFER_DESC * desc)
1585 GstD3D11Memory *mem;
1586 ID3D11Buffer *buffer;
1587 ID3D11Device *device_handle;
1591 gst_d3d11_memory_init_once ();
1592 allocator = _d3d11_memory_allocator;
1595 g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), nullptr);
1596 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), nullptr);
1597 g_return_val_if_fail (desc != nullptr, nullptr);
1599 if (desc->Usage != D3D11_USAGE_STAGING) {
1600 GST_FIXME_OBJECT (allocator, "Non staging buffer is not supported");
1604 device_handle = gst_d3d11_device_get_device_handle (device);
1606 hr = device_handle->CreateBuffer (desc, nullptr, &buffer);
1607 if (!gst_d3d11_result (hr, device)) {
1608 GST_ERROR_OBJECT (allocator, "Couldn't create buffer");
1612 mem = g_new0 (GstD3D11Memory, 1);
1613 mem->priv = g_new0 (GstD3D11MemoryPrivate, 1);
1615 gst_memory_init (GST_MEMORY_CAST (mem),
1616 (GstMemoryFlags) 0, GST_ALLOCATOR_CAST (allocator), nullptr, 0, 0, 0, 0);
1617 mem->priv->buffer = buffer;
1618 mem->priv->buffer_desc = *desc;
1619 mem->priv->native_type = GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER;
1620 mem->device = (GstD3D11Device *) gst_object_ref (device);
1622 GST_MEMORY_CAST (mem)->maxsize = GST_MEMORY_CAST (mem)->size =
1625 return GST_MEMORY_CAST (mem);
1629 * gst_d3d11_allocator_alloc_wrapped:
1630 * @allocator: (transfer none) (allow-none): a #GstD3D11Allocator
1631 * @device: (transfer none): a #GstD3D11Device
1632 * @texture: a ID3D11Texture2D
1633 * @size: CPU accessible memory size
1634 * @user_data: (allow-none): user data
1635 * @notify: (allow-none): called with @user_data when the memory is freed
1637 * Allocates memory object with @texture. The refcount of @texture
1638 * will be increased by one.
1640 * Caller should set valid CPU acessible memory value to @size
1641 * (which is typically calculated by using staging texture and Map/Unmap)
1642 * or zero is allowed. In that case, allocator will create a temporary staging
1643 * texture to get the size and the temporary staging texture will be released.
1645 * Caller must not be confused that @size is CPU accessible size, not raw
1648 * Returns: a newly allocated #GstD3D11Memory with given @texture
1649 * if successful, or %NULL if @texture is not a valid handle or configuration
1655 gst_d3d11_allocator_alloc_wrapped (GstD3D11Allocator * allocator,
1656 GstD3D11Device * device, ID3D11Texture2D * texture, gsize size,
1657 gpointer user_data, GDestroyNotify notify)
1660 GstD3D11Memory *dmem;
1661 D3D11_TEXTURE2D_DESC desc = { 0, };
1662 ID3D11Texture2D *tex = nullptr;
1666 gst_d3d11_memory_init_once ();
1667 allocator = _d3d11_memory_allocator;
1670 g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), nullptr);
1671 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), nullptr);
1672 g_return_val_if_fail (texture != nullptr, nullptr);
1674 hr = texture->QueryInterface (IID_PPV_ARGS (&tex));
1676 GST_WARNING_OBJECT (allocator, "Not a valid texture handle");
1680 tex->GetDesc (&desc);
1681 mem = gst_d3d11_allocator_alloc_internal (allocator, device, &desc, tex);
1687 if (!gst_d3d11_memory_update_size (mem)) {
1688 GST_ERROR_OBJECT (allocator, "Failed to calculate size");
1689 gst_memory_unref (mem);
1693 mem->maxsize = mem->size = size;
1696 dmem = GST_D3D11_MEMORY_CAST (mem);
1698 dmem->priv->user_data = user_data;
1699 dmem->priv->notify = notify;
1705 * gst_d3d11_allocator_set_active:
1706 * @allocator: a #GstD3D11Allocator
1707 * @active: the new active state
1709 * Controls the active state of @allocator. Default #GstD3D11Allocator is
1710 * stateless and therefore active state is ignored, but subclass implementation
1711 * (e.g., #GstD3D11PoolAllocator) will require explicit active state control
1712 * for its internal resource management.
1714 * This method is conceptually identical to gst_buffer_pool_set_active method.
1716 * Returns: %TRUE if active state of @allocator was successfully updated.
1721 gst_d3d11_allocator_set_active (GstD3D11Allocator * allocator, gboolean active)
1723 GstD3D11AllocatorClass *klass;
1725 g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), FALSE);
1727 klass = GST_D3D11_ALLOCATOR_GET_CLASS (allocator);
1728 if (klass->set_actvie)
1729 return klass->set_actvie (allocator, active);
1734 /* GstD3D11PoolAllocator */
1735 #define GST_D3D11_POOL_ALLOCATOR_GET_LOCK(alloc) \
1736 (&(GST_D3D11_POOL_ALLOCATOR_CAST(alloc)->priv->lock))
1737 #define GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING(alloc) (g_atomic_int_get (&alloc->priv->flushing))
1739 struct _GstD3D11PoolAllocatorPrivate
1741 /* parent texture when array typed memory is used */
1742 ID3D11Texture2D *texture;
1743 D3D11_TEXTURE2D_DESC desc;
1745 /* All below member variables are analogous to that of GstBufferPool */
1746 GstAtomicQueue *queue;
1749 /* This lock will protect all below variables apart from atomic ones
1750 * (identical to GstBufferPool::priv::rec_lock) */
1751 CRITICAL_SECTION lock;
1761 /* Calculated memory size, based on Direct3D11 staging texture map.
1762 * Note that, we cannot know the actually staging texture memory size prior
1763 * to map the staging texture because driver will likely require padding */
1767 static void gst_d3d11_pool_allocator_dispose (GObject * object);
1768 static void gst_d3d11_pool_allocator_finalize (GObject * object);
1771 gst_d3d11_pool_allocator_set_active (GstD3D11Allocator * allocator,
1774 static gboolean gst_d3d11_pool_allocator_start (GstD3D11PoolAllocator * self);
1775 static gboolean gst_d3d11_pool_allocator_stop (GstD3D11PoolAllocator * self);
1776 static gboolean gst_d3d11_memory_release (GstMiniObject * mini_object);
1778 #define gst_d3d11_pool_allocator_parent_class pool_alloc_parent_class
1779 G_DEFINE_TYPE_WITH_PRIVATE (GstD3D11PoolAllocator,
1780 gst_d3d11_pool_allocator, GST_TYPE_D3D11_ALLOCATOR);
1783 gst_d3d11_pool_allocator_class_init (GstD3D11PoolAllocatorClass * klass)
1785 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1786 GstD3D11AllocatorClass *d3d11alloc_class = GST_D3D11_ALLOCATOR_CLASS (klass);
1788 gobject_class->dispose = gst_d3d11_pool_allocator_dispose;
1789 gobject_class->finalize = gst_d3d11_pool_allocator_finalize;
1791 d3d11alloc_class->set_actvie = gst_d3d11_pool_allocator_set_active;
1795 gst_d3d11_pool_allocator_init (GstD3D11PoolAllocator * allocator)
1797 GstD3D11PoolAllocatorPrivate *priv;
1799 priv = allocator->priv = (GstD3D11PoolAllocatorPrivate *)
1800 gst_d3d11_pool_allocator_get_instance_private (allocator);
1802 InitializeCriticalSection (&priv->lock);
1804 priv->poll = gst_poll_new_timer ();
1805 priv->queue = gst_atomic_queue_new (16);
1807 priv->active = FALSE;
1808 priv->started = FALSE;
1810 /* 1 control write for flushing - the flush token */
1811 gst_poll_write_control (priv->poll);
1812 /* 1 control write for marking that we are not waiting for poll - the wait token */
1813 gst_poll_write_control (priv->poll);
1817 gst_d3d11_pool_allocator_dispose (GObject * object)
1819 GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (object);
1821 gst_clear_object (&self->device);
1823 G_OBJECT_CLASS (pool_alloc_parent_class)->dispose (object);
1827 gst_d3d11_pool_allocator_finalize (GObject * object)
1829 GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (object);
1830 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1832 GST_DEBUG_OBJECT (self, "Finalize");
1834 gst_d3d11_pool_allocator_stop (self);
1835 gst_atomic_queue_unref (priv->queue);
1836 gst_poll_free (priv->poll);
1837 DeleteCriticalSection (&priv->lock);
1839 GST_D3D11_CLEAR_COM (priv->texture);
1841 G_OBJECT_CLASS (pool_alloc_parent_class)->finalize (object);
1845 gst_d3d11_pool_allocator_start (GstD3D11PoolAllocator * self)
1847 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1848 ID3D11Device *device_handle;
1856 if (priv->desc.ArraySize == 1) {
1857 priv->started = TRUE;
1861 device_handle = gst_d3d11_device_get_device_handle (self->device);
1863 if (!priv->texture) {
1864 hr = device_handle->CreateTexture2D (&priv->desc, NULL, &priv->texture);
1865 if (!gst_d3d11_result (hr, self->device)) {
1866 GST_ERROR_OBJECT (self, "Failed to allocate texture");
1871 /* Pre-allocate memory objects */
1872 for (i = 0; i < priv->desc.ArraySize; i++) {
1875 priv->texture->AddRef ();
1876 mem = gst_d3d11_allocator_alloc_wrapped_internal (_d3d11_memory_allocator,
1877 self->device, &priv->desc, priv->texture);
1880 if (!gst_d3d11_memory_update_size (mem)) {
1881 GST_ERROR_OBJECT (self, "Failed to calculate memory size");
1882 gst_memory_unref (mem);
1886 priv->mem_size = mem->size;
1888 mem->size = mem->maxsize = priv->mem_size;
1891 GST_D3D11_MEMORY_CAST (mem)->priv->subresource_index = i;
1893 g_atomic_int_add (&priv->cur_mems, 1);
1894 gst_atomic_queue_push (priv->queue, mem);
1895 gst_poll_write_control (priv->poll);
1898 priv->started = TRUE;
1904 gst_d3d11_pool_allocator_do_set_flushing (GstD3D11PoolAllocator * self,
1907 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1909 if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self) == flushing)
1913 g_atomic_int_set (&priv->flushing, 1);
1914 /* Write the flush token to wake up any waiters */
1915 gst_poll_write_control (priv->poll);
1917 while (!gst_poll_read_control (priv->poll)) {
1918 if (errno == EWOULDBLOCK) {
1919 /* This should not really happen unless flushing and unflushing
1920 * happens on different threads. Let's wait a bit to get back flush
1921 * token from the thread that was setting it to flushing */
1925 /* Critical error but GstPoll already complained */
1930 g_atomic_int_set (&priv->flushing, 0);
1935 gst_d3d11_pool_allocator_set_active (GstD3D11Allocator * allocator,
1938 GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (allocator);
1939 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1941 GST_LOG_OBJECT (self, "active %d", active);
1943 GstD3D11CSLockGuard lk (GST_D3D11_POOL_ALLOCATOR_GET_LOCK (self));
1944 /* just return if we are already in the right state */
1945 if (priv->active == active)
1949 if (!gst_d3d11_pool_allocator_start (self)) {
1950 GST_ERROR_OBJECT (self, "start failed");
1954 /* flush_stop may release memory objects, setting to active to avoid running
1955 * do_stop while activating the pool */
1956 priv->active = TRUE;
1958 gst_d3d11_pool_allocator_do_set_flushing (self, FALSE);
1962 /* set to flushing first */
1963 gst_d3d11_pool_allocator_do_set_flushing (self, TRUE);
1965 /* when all memory objects are in the pool, free them. Else they will be
1966 * freed when they are released */
1967 outstanding = g_atomic_int_get (&priv->outstanding);
1968 GST_LOG_OBJECT (self, "outstanding memories %d, (in queue %d)",
1969 outstanding, gst_atomic_queue_length (priv->queue));
1970 if (outstanding == 0) {
1971 if (!gst_d3d11_pool_allocator_stop (self)) {
1972 GST_ERROR_OBJECT (self, "stop failed");
1977 priv->active = FALSE;
1984 gst_d3d11_pool_allocator_free_memory (GstD3D11PoolAllocator * self,
1987 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1989 g_atomic_int_add (&priv->cur_mems, -1);
1990 GST_LOG_OBJECT (self, "freeing memory %p (%u left)", mem, priv->cur_mems);
1992 GST_MINI_OBJECT_CAST (mem)->dispose = NULL;
1993 gst_memory_unref (mem);
1996 /* must be called with the lock */
1998 gst_d3d11_pool_allocator_clear_queue (GstD3D11PoolAllocator * self)
2000 GstD3D11PoolAllocatorPrivate *priv = self->priv;
2003 GST_LOG_OBJECT (self, "Clearing queue");
2005 /* clear the pool */
2006 while ((memory = (GstMemory *) gst_atomic_queue_pop (priv->queue))) {
2007 while (!gst_poll_read_control (priv->poll)) {
2008 if (errno == EWOULDBLOCK) {
2009 /* We put the memory into the queue but did not finish writing control
2010 * yet, let's wait a bit and retry */
2014 /* Critical error but GstPoll already complained */
2018 gst_d3d11_pool_allocator_free_memory (self, memory);
2021 GST_LOG_OBJECT (self, "Clear done");
2023 return priv->cur_mems == 0;
2026 /* must be called with the lock */
2028 gst_d3d11_pool_allocator_stop (GstD3D11PoolAllocator * self)
2030 GstD3D11PoolAllocatorPrivate *priv = self->priv;
2032 GST_DEBUG_OBJECT (self, "Stop");
2034 if (priv->started) {
2035 if (!gst_d3d11_pool_allocator_clear_queue (self))
2038 priv->started = FALSE;
2040 GST_DEBUG_OBJECT (self, "Wasn't started");
2047 dec_outstanding (GstD3D11PoolAllocator * self)
2049 if (g_atomic_int_dec_and_test (&self->priv->outstanding)) {
2050 /* all memory objects are returned to the pool, see if we need to free them */
2051 if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)) {
2052 /* take the lock so that set_active is not run concurrently */
2053 GstD3D11CSLockGuard lk (GST_D3D11_POOL_ALLOCATOR_GET_LOCK (self));
2054 /* now that we have the lock, check if we have been de-activated with
2055 * outstanding buffers */
2056 if (!self->priv->active)
2057 gst_d3d11_pool_allocator_stop (self);
2063 gst_d3d11_pool_allocator_release_memory (GstD3D11PoolAllocator * self,
2066 GST_LOG_OBJECT (self, "Released memory %p", mem);
2068 GST_MINI_OBJECT_CAST (mem)->dispose = NULL;
2069 mem->allocator = (GstAllocator *) gst_object_ref (_d3d11_memory_allocator);
2070 gst_object_unref (self);
2072 /* keep it around in our queue */
2073 gst_atomic_queue_push (self->priv->queue, mem);
2074 gst_poll_write_control (self->priv->poll);
2075 dec_outstanding (self);
2079 gst_d3d11_memory_release (GstMiniObject * mini_object)
2081 GstMemory *mem = GST_MEMORY_CAST (mini_object);
2082 GstD3D11PoolAllocator *alloc;
2084 g_assert (mem->allocator != NULL);
2086 if (!GST_IS_D3D11_POOL_ALLOCATOR (mem->allocator)) {
2087 GST_LOG_OBJECT (mem->allocator, "Not our memory, free");
2091 alloc = GST_D3D11_POOL_ALLOCATOR (mem->allocator);
2092 /* if flushing, free this memory */
2093 if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (alloc)) {
2094 GST_LOG_OBJECT (alloc, "allocator is flushing, free %p", mem);
2098 /* return the memory to the allocator */
2099 gst_memory_ref (mem);
2100 gst_d3d11_pool_allocator_release_memory (alloc, mem);
2105 static GstFlowReturn
2106 gst_d3d11_pool_allocator_alloc (GstD3D11PoolAllocator * self, GstMemory ** mem)
2108 GstD3D11PoolAllocatorPrivate *priv = self->priv;
2111 /* we allcates texture array during start */
2112 if (priv->desc.ArraySize > 1)
2113 return GST_FLOW_EOS;
2115 /* increment the allocation counter */
2116 g_atomic_int_add (&priv->cur_mems, 1);
2117 new_mem = gst_d3d11_allocator_alloc_internal (_d3d11_memory_allocator,
2118 self->device, &priv->desc, nullptr);
2120 GST_ERROR_OBJECT (self, "Failed to allocate new memory");
2121 g_atomic_int_add (&priv->cur_mems, -1);
2122 return GST_FLOW_ERROR;
2125 if (!priv->mem_size) {
2126 if (!gst_d3d11_memory_update_size (new_mem)) {
2127 GST_ERROR_OBJECT (self, "Failed to calculate size");
2128 gst_memory_unref (new_mem);
2129 g_atomic_int_add (&priv->cur_mems, -1);
2131 return GST_FLOW_ERROR;
2134 priv->mem_size = new_mem->size;
2137 new_mem->size = new_mem->maxsize = priv->mem_size;
2144 static GstFlowReturn
2145 gst_d3d11_pool_allocator_acquire_memory_internal (GstD3D11PoolAllocator * self,
2146 GstMemory ** memory)
2148 GstFlowReturn result;
2149 GstD3D11PoolAllocatorPrivate *priv = self->priv;
2152 if (G_UNLIKELY (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)))
2155 /* try to get a memory from the queue */
2156 *memory = (GstMemory *) gst_atomic_queue_pop (priv->queue);
2157 if (G_LIKELY (*memory)) {
2158 while (!gst_poll_read_control (priv->poll)) {
2159 if (errno == EWOULDBLOCK) {
2160 /* We put the memory into the queue but did not finish writing control
2161 * yet, let's wait a bit and retry */
2165 /* Critical error but GstPoll already complained */
2169 result = GST_FLOW_OK;
2170 GST_LOG_OBJECT (self, "acquired memory %p", *memory);
2174 /* no memory, try to allocate some more */
2175 GST_LOG_OBJECT (self, "no memory, trying to allocate");
2176 result = gst_d3d11_pool_allocator_alloc (self, memory);
2177 if (G_LIKELY (result == GST_FLOW_OK))
2178 /* we have a memory, return it */
2181 if (G_UNLIKELY (result != GST_FLOW_EOS))
2182 /* something went wrong, return error */
2185 /* now we release the control socket, we wait for a memory release or
2187 if (!gst_poll_read_control (priv->poll)) {
2188 if (errno == EWOULDBLOCK) {
2189 /* This means that we have two threads trying to allocate memory
2190 * already, and the other one already got the wait token. This
2191 * means that we only have to wait for the poll now and not write the
2192 * token afterwards: we will be woken up once the other thread is
2193 * woken up and that one will write the wait token it removed */
2194 GST_LOG_OBJECT (self, "waiting for free memory or flushing");
2195 gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
2197 /* This is a critical error, GstPoll already gave a warning */
2198 result = GST_FLOW_ERROR;
2202 /* We're the first thread waiting, we got the wait token and have to
2203 * write it again later
2205 * We're a second thread and just consumed the flush token and block all
2206 * other threads, in which case we must not wait and give it back
2208 if (!GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)) {
2209 GST_LOG_OBJECT (self, "waiting for free memory or flushing");
2210 gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
2212 gst_poll_write_control (priv->poll);
2221 GST_DEBUG_OBJECT (self, "we are flushing");
2222 return GST_FLOW_FLUSHING;
2227 * gst_d3d11_pool_allocator_new:
2228 * @device: a #GstD3D11Device
2229 * @desc: a D3D11_TEXTURE2D_DESC for texture allocation
2231 * Creates a new #GstD3D11PoolAllocator instance.
2233 * Returns: (transfer full): a new #GstD3D11PoolAllocator instance
2237 GstD3D11PoolAllocator *
2238 gst_d3d11_pool_allocator_new (GstD3D11Device * device,
2239 const D3D11_TEXTURE2D_DESC * desc)
2241 GstD3D11PoolAllocator *self;
2243 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), NULL);
2244 g_return_val_if_fail (desc != NULL, NULL);
2246 gst_d3d11_memory_init_once ();
2248 self = (GstD3D11PoolAllocator *)
2249 g_object_new (GST_TYPE_D3D11_POOL_ALLOCATOR, NULL);
2250 gst_object_ref_sink (self);
2252 self->device = (GstD3D11Device *) gst_object_ref (device);
2253 self->priv->desc = *desc;
2259 * gst_d3d11_pool_allocator_acquire_memory:
2260 * @allocator: a #GstD3D11PoolAllocator
2261 * @memory: (out): a #GstMemory
2263 * Acquires a #GstMemory from @allocator. @memory should point to a memory
2264 * location that can hold a pointer to the new #GstMemory.
2266 * Returns: a #GstFlowReturn such as %GST_FLOW_FLUSHING when the allocator is
2270 gst_d3d11_pool_allocator_acquire_memory (GstD3D11PoolAllocator * allocator,
2271 GstMemory ** memory)
2273 GstD3D11PoolAllocatorPrivate *priv;
2274 GstFlowReturn result;
2276 g_return_val_if_fail (GST_IS_D3D11_POOL_ALLOCATOR (allocator),
2278 g_return_val_if_fail (memory != NULL, GST_FLOW_ERROR);
2280 priv = allocator->priv;
2282 /* assume we'll have one more outstanding buffer we need to do that so
2283 * that concurrent set_active doesn't clear the buffers */
2284 g_atomic_int_inc (&priv->outstanding);
2285 result = gst_d3d11_pool_allocator_acquire_memory_internal (allocator, memory);
2287 if (result == GST_FLOW_OK) {
2288 GstMemory *mem = *memory;
2289 /* Replace default allocator with ours */
2290 gst_object_unref (mem->allocator);
2291 mem->allocator = (GstAllocator *) gst_object_ref (allocator);
2292 GST_MINI_OBJECT_CAST (mem)->dispose = gst_d3d11_memory_release;
2294 dec_outstanding (allocator);
2301 * gst_d3d11_pool_allocator_get_pool_size:
2302 * @allocator: a #GstD3D11PoolAllocator
2303 * @max_size: (out) (optional): the max size of pool
2304 * @outstanding_size: (out) (optional): the number of outstanding memory
2306 * Returns: %TRUE if the size of memory pool is known
2311 gst_d3d11_pool_allocator_get_pool_size (GstD3D11PoolAllocator * allocator,
2312 guint * max_size, guint * outstanding_size)
2314 GstD3D11PoolAllocatorPrivate *priv;
2316 g_return_val_if_fail (GST_IS_D3D11_POOL_ALLOCATOR (allocator), FALSE);
2318 priv = allocator->priv;
2321 if (priv->desc.ArraySize > 1) {
2322 *max_size = priv->desc.ArraySize;
2324 /* For non-texture-array memory, we don't have any limit yet */
2329 if (outstanding_size)
2330 *outstanding_size = g_atomic_int_get (&priv->outstanding);