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"
32 * SECTION:gstd3d11memory
33 * @title: GstD3D11Memory
34 * @short_description: Direct3D11 memory abstraction layer
39 GST_DEBUG_CATEGORY_STATIC (gst_d3d11_allocator_debug);
40 #define GST_CAT_DEFAULT gst_d3d11_allocator_debug
42 static GstD3D11Allocator *_d3d11_memory_allocator;
45 gst_d3d11_allocation_flags_get_type (void)
47 static GType type = 0;
48 static const GFlagsValue values[] = {
49 {GST_D3D11_ALLOCATION_FLAG_DEFAULT, "GST_D3D11_ALLOCATION_FLAG_DEFAULT",
51 {GST_D3D11_ALLOCATION_FLAG_TEXTURE_ARRAY,
52 "GST_D3D11_ALLOCATION_FLAG_TEXTURE_ARRAY", "texture-array"},
56 GST_D3D11_CALL_ONCE_BEGIN {
57 type = g_flags_register_static ("GstD3D11AllocationFlags", values);
58 } GST_D3D11_CALL_ONCE_END;
64 gst_d3d11_memory_transfer_get_type (void)
66 static GType type = 0;
67 static const GFlagsValue values[] = {
68 {GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD,
69 "GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD", "need-download"},
70 {GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD,
71 "GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD", "need-upload"},
75 GST_D3D11_CALL_ONCE_BEGIN {
76 type = g_flags_register_static ("GstD3D11MemoryTransfer", values);
77 } GST_D3D11_CALL_ONCE_END;
83 gst_d3d11_memory_native_type_get_type (void)
85 static GType type = 0;
86 static const GEnumValue values[] = {
87 {GST_D3D11_MEMORY_NATIVE_TYPE_INVALID,
88 "GST_D3D11_MEMORY_NATIVE_TYPE_INVALID", "invalid"},
89 {GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER, "GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER",
91 {GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D,
92 "GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D", "texture-2d"},
96 GST_D3D11_CALL_ONCE_BEGIN {
97 type = g_enum_register_static ("GstD3D11MemoryNativeType", values);
98 } GST_D3D11_CALL_ONCE_END;
103 /* GstD3D11AllocationParams */
104 static void gst_d3d11_allocation_params_init (GType type);
105 G_DEFINE_BOXED_TYPE_WITH_CODE (GstD3D11AllocationParams,
106 gst_d3d11_allocation_params,
107 (GBoxedCopyFunc) gst_d3d11_allocation_params_copy,
108 (GBoxedFreeFunc) gst_d3d11_allocation_params_free,
109 gst_d3d11_allocation_params_init (g_define_type_id));
112 * gst_d3d11_allocation_params_new:
113 * @device: a #GstD3D11Device
114 * @info: a #GstVideoInfo
115 * @flags: a #GstD3D11AllocationFlags
116 * @bind_flags: D3D11_BIND_FLAG value used for creating texture
117 * @misc_flags: D3D11_RESOURCE_MISC_FLAG value used for creating texture
119 * Create #GstD3D11AllocationParams object which is used by #GstD3D11BufferPool
120 * and #GstD3D11Allocator in order to allocate new ID3D11Texture2D
121 * object with given configuration
123 * Returns: (transfer full) (nullable): a #GstD3D11AllocationParams or %NULL if @info is not supported
127 GstD3D11AllocationParams *
128 gst_d3d11_allocation_params_new (GstD3D11Device * device,
129 const GstVideoInfo * info, GstD3D11AllocationFlags flags, guint bind_flags,
132 GstD3D11AllocationParams *ret;
133 GstD3D11Format d3d11_format;
136 g_return_val_if_fail (info != NULL, NULL);
138 if (!gst_d3d11_device_get_format (device, GST_VIDEO_INFO_FORMAT (info),
140 GST_WARNING ("Couldn't get d3d11 format");
144 ret = g_new0 (GstD3D11AllocationParams, 1);
147 ret->aligned_info = *info;
148 ret->d3d11_format = d3d11_format;
151 * https://docs.microsoft.com/en-us/windows/win32/api/d3d11/ne-d3d11-d3d11_usage
153 * +----------------------------------------------------------+
154 * | Resource Usage | Default | Dynamic | Immutable | Staging |
155 * +----------------+---------+---------+-----------+---------+
156 * | GPU-Read | Yes | Yes | Yes | Yes |
157 * | GPU-Write | Yes | | | Yes |
158 * | CPU-Read | | | | Yes |
159 * | CPU-Write | | Yes | | Yes |
160 * +----------------------------------------------------------+
163 /* If corresponding dxgi format is undefined, use resource format instead */
164 if (d3d11_format.dxgi_format == DXGI_FORMAT_UNKNOWN) {
165 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (info); i++) {
166 g_assert (d3d11_format.resource_format[i] != DXGI_FORMAT_UNKNOWN);
168 ret->desc[i].Width = GST_VIDEO_INFO_COMP_WIDTH (info, i);
169 ret->desc[i].Height = GST_VIDEO_INFO_COMP_HEIGHT (info, i);
170 ret->desc[i].MipLevels = 1;
171 ret->desc[i].ArraySize = 1;
172 ret->desc[i].Format = d3d11_format.resource_format[i];
173 ret->desc[i].SampleDesc.Count = 1;
174 ret->desc[i].SampleDesc.Quality = 0;
175 ret->desc[i].Usage = D3D11_USAGE_DEFAULT;
176 ret->desc[i].BindFlags = bind_flags;
177 ret->desc[i].MiscFlags = misc_flags;
180 ret->desc[0].Width = GST_VIDEO_INFO_WIDTH (info);
181 ret->desc[0].Height = GST_VIDEO_INFO_HEIGHT (info);
182 ret->desc[0].MipLevels = 1;
183 ret->desc[0].ArraySize = 1;
184 ret->desc[0].Format = d3d11_format.dxgi_format;
185 ret->desc[0].SampleDesc.Count = 1;
186 ret->desc[0].SampleDesc.Quality = 0;
187 ret->desc[0].Usage = D3D11_USAGE_DEFAULT;
188 ret->desc[0].BindFlags = bind_flags;
189 ret->desc[0].MiscFlags = misc_flags;
198 * gst_d3d11_allocation_params_alignment:
199 * @params: a #GstD3D11AllocationParams
200 * @align: a #GstVideoAlignment
202 * Adjust Width and Height fields of D3D11_TEXTURE2D_DESC with given
205 * Returns: %TRUE if alignment could be applied
210 gst_d3d11_allocation_params_alignment (GstD3D11AllocationParams * params,
211 const GstVideoAlignment * align)
214 guint padding_width, padding_height;
216 GstVideoInfo new_info;
218 g_return_val_if_fail (params != NULL, FALSE);
219 g_return_val_if_fail (align != NULL, FALSE);
221 /* d3d11 does not support stride align. Consider padding only */
222 padding_width = align->padding_left + align->padding_right;
223 padding_height = align->padding_top + align->padding_bottom;
225 info = ¶ms->info;
227 if (!gst_video_info_set_format (&new_info, GST_VIDEO_INFO_FORMAT (info),
228 GST_VIDEO_INFO_WIDTH (info) + padding_width,
229 GST_VIDEO_INFO_HEIGHT (info) + padding_height)) {
230 GST_WARNING ("Set format fail");
234 params->aligned_info = new_info;
236 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (info); i++) {
237 params->desc[i].Width = GST_VIDEO_INFO_COMP_WIDTH (&new_info, i);
238 params->desc[i].Height = GST_VIDEO_INFO_COMP_HEIGHT (&new_info, i);
245 * gst_d3d11_allocation_params_copy:
246 * @src: a #GstD3D11AllocationParams
248 * Returns: (transfer full): a copy of @src
252 GstD3D11AllocationParams *
253 gst_d3d11_allocation_params_copy (GstD3D11AllocationParams * src)
255 GstD3D11AllocationParams *dst;
257 g_return_val_if_fail (src != NULL, NULL);
259 dst = g_new0 (GstD3D11AllocationParams, 1);
260 memcpy (dst, src, sizeof (GstD3D11AllocationParams));
266 * gst_d3d11_allocation_params_free:
267 * @params: a #GstD3D11AllocationParams
274 gst_d3d11_allocation_params_free (GstD3D11AllocationParams * params)
280 gst_d3d11_allocation_params_compare (const GstD3D11AllocationParams * p1,
281 const GstD3D11AllocationParams * p2)
283 g_return_val_if_fail (p1 != NULL, -1);
284 g_return_val_if_fail (p2 != NULL, -1);
293 gst_d3d11_allocation_params_init (GType type)
295 static GstValueTable table = {
296 0, (GstValueCompareFunc) gst_d3d11_allocation_params_compare,
301 gst_value_register (&table);
305 #define GST_D3D11_MEMORY_GET_LOCK(m) (&(GST_D3D11_MEMORY_CAST(m)->priv->lock))
307 struct _GstD3D11MemoryPrivate
309 ID3D11Texture2D *texture;
310 ID3D11Buffer *buffer;
312 GstD3D11MemoryNativeType native_type;
314 D3D11_TEXTURE2D_DESC desc;
315 D3D11_BUFFER_DESC buffer_desc;
317 guint subresource_index;
319 /* protected by device lock */
320 ID3D11Resource *staging;
321 D3D11_MAPPED_SUBRESOURCE map;
324 /* protects resource objects */
326 ID3D11ShaderResourceView *shader_resource_view[GST_VIDEO_MAX_PLANES];
327 guint num_shader_resource_views;
329 ID3D11RenderTargetView *render_target_view[GST_VIDEO_MAX_PLANES];
330 guint num_render_target_views;
332 ID3D11VideoDecoderOutputView *decoder_output_view;
333 ID3D11VideoDecoder *decoder_handle;
335 ID3D11VideoProcessorInputView *processor_input_view;
336 ID3D11VideoProcessorOutputView *processor_output_view;
338 GDestroyNotify notify;
342 static inline D3D11_MAP
343 gst_d3d11_map_flags_to_d3d11 (GstMapFlags flags)
345 if ((flags & GST_MAP_READWRITE) == GST_MAP_READWRITE)
346 return D3D11_MAP_READ_WRITE;
347 else if ((flags & GST_MAP_WRITE) == GST_MAP_WRITE)
348 return D3D11_MAP_WRITE;
349 else if ((flags & GST_MAP_READ) == GST_MAP_READ)
350 return D3D11_MAP_READ;
352 g_assert_not_reached ();
354 return D3D11_MAP_READ;
357 static ID3D11Texture2D *
358 gst_d3d11_allocate_staging_texture (GstD3D11Device * device,
359 const D3D11_TEXTURE2D_DESC * ref)
361 D3D11_TEXTURE2D_DESC desc = { 0, };
362 ID3D11Texture2D *texture = NULL;
363 ID3D11Device *device_handle = gst_d3d11_device_get_device_handle (device);
366 desc.Width = ref->Width;
367 desc.Height = ref->Height;
369 desc.Format = ref->Format;
370 desc.SampleDesc.Count = 1;
372 desc.Usage = D3D11_USAGE_STAGING;
373 desc.CPUAccessFlags = (D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE);
375 hr = device_handle->CreateTexture2D (&desc, NULL, &texture);
376 if (!gst_d3d11_result (hr, device)) {
377 GST_ERROR_OBJECT (device, "Failed to create texture");
384 /* Must be called with d3d11 device lock */
386 gst_d3d11_memory_map_cpu_access (GstD3D11Memory * dmem, D3D11_MAP map_type)
388 GstD3D11MemoryPrivate *priv = dmem->priv;
390 ID3D11DeviceContext *device_context =
391 gst_d3d11_device_get_device_context_handle (dmem->device);
393 hr = device_context->Map (priv->staging, 0, map_type, 0, &priv->map);
395 if (!gst_d3d11_result (hr, dmem->device)) {
396 GST_ERROR_OBJECT (GST_MEMORY_CAST (dmem)->allocator,
397 "Failed to map staging texture (0x%x)", (guint) hr);
404 /* Must be called with d3d11 device lock */
406 gst_d3d11_memory_upload (GstD3D11Memory * dmem)
408 GstD3D11MemoryPrivate *priv = dmem->priv;
409 ID3D11DeviceContext *device_context;
411 if (!priv->staging ||
412 !GST_MEMORY_FLAG_IS_SET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD))
415 device_context = gst_d3d11_device_get_device_context_handle (dmem->device);
416 device_context->CopySubresourceRegion (priv->texture, priv->subresource_index,
417 0, 0, 0, priv->staging, 0, NULL);
420 /* Must be called with d3d11 device lock */
422 gst_d3d11_memory_download (GstD3D11Memory * dmem)
424 GstD3D11MemoryPrivate *priv = dmem->priv;
425 ID3D11DeviceContext *device_context;
427 if (!priv->staging ||
428 !GST_MEMORY_FLAG_IS_SET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD))
431 device_context = gst_d3d11_device_get_device_context_handle (dmem->device);
432 device_context->CopySubresourceRegion (priv->staging, 0, 0, 0, 0,
433 priv->texture, priv->subresource_index, NULL);
437 gst_d3d11_memory_map_full (GstMemory * mem, GstMapInfo * info, gsize maxsize)
439 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
440 GstD3D11MemoryPrivate *priv = dmem->priv;
441 GstMapFlags flags = info->flags;
442 GstD3D11DeviceLockGuard lk (dmem->device);
444 memset (info->user_data, 0, sizeof (info->user_data));
445 info->user_data[0] = GUINT_TO_POINTER (dmem->priv->subresource_index);
447 if ((flags & GST_MAP_D3D11) == GST_MAP_D3D11) {
448 if (priv->native_type == GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER) {
449 /* FIXME: handle non-staging buffer */
450 g_assert (priv->buffer != nullptr);
453 gst_d3d11_memory_upload (dmem);
454 GST_MEMORY_FLAG_UNSET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD);
456 if ((flags & GST_MAP_WRITE) == GST_MAP_WRITE)
457 GST_MINI_OBJECT_FLAG_SET (dmem,
458 GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
460 g_assert (priv->texture != NULL);
461 return priv->texture;
465 if (priv->cpu_map_count == 0) {
468 /* FIXME: handle non-staging buffer */
469 if (priv->native_type == GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D) {
470 /* Allocate staging texture for CPU access */
471 if (!priv->staging) {
472 priv->staging = gst_d3d11_allocate_staging_texture (dmem->device,
474 if (!priv->staging) {
475 GST_ERROR_OBJECT (mem->allocator, "Couldn't create staging texture");
479 /* first memory, always need download to staging */
480 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
483 gst_d3d11_memory_download (dmem);
486 map_type = gst_d3d11_map_flags_to_d3d11 (flags);
487 if (!gst_d3d11_memory_map_cpu_access (dmem, map_type)) {
488 GST_ERROR_OBJECT (mem->allocator, "Couldn't map staging texture");
493 if ((flags & GST_MAP_WRITE) == GST_MAP_WRITE) {
494 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD);
497 GST_MEMORY_FLAG_UNSET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
499 priv->cpu_map_count++;
500 return dmem->priv->map.pData;
503 /* Must be called with d3d11 device lock */
505 gst_d3d11_memory_unmap_cpu_access (GstD3D11Memory * dmem)
507 GstD3D11MemoryPrivate *priv = dmem->priv;
508 ID3D11DeviceContext *device_context =
509 gst_d3d11_device_get_device_context_handle (dmem->device);
511 device_context->Unmap (priv->staging, 0);
515 gst_d3d11_memory_unmap_full (GstMemory * mem, GstMapInfo * info)
517 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
518 GstD3D11MemoryPrivate *priv = dmem->priv;
519 GstD3D11DeviceLockGuard lk (dmem->device);
521 if ((info->flags & GST_MAP_D3D11) == GST_MAP_D3D11) {
522 if ((info->flags & GST_MAP_WRITE) == GST_MAP_WRITE)
523 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
528 if ((info->flags & GST_MAP_WRITE) == GST_MAP_WRITE)
529 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD);
531 priv->cpu_map_count--;
532 if (priv->cpu_map_count > 0)
535 gst_d3d11_memory_unmap_cpu_access (dmem);
539 gst_d3d11_memory_share (GstMemory * mem, gssize offset, gssize size)
546 gst_d3d11_memory_update_size (GstMemory * mem)
548 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
549 GstD3D11MemoryPrivate *priv = dmem->priv;
550 gsize offset[GST_VIDEO_MAX_PLANES];
551 gint stride[GST_VIDEO_MAX_PLANES];
553 D3D11_TEXTURE2D_DESC *desc = &priv->desc;
555 if (!priv->staging) {
556 priv->staging = gst_d3d11_allocate_staging_texture (dmem->device,
558 if (!priv->staging) {
559 GST_ERROR_OBJECT (mem->allocator, "Couldn't create staging texture");
564 GstD3D11DeviceLockGuard lk (dmem->device);
565 if (!gst_d3d11_memory_map_cpu_access (dmem, D3D11_MAP_READ_WRITE)) {
566 GST_ERROR_OBJECT (mem->allocator, "Couldn't map staging texture");
570 gst_d3d11_memory_unmap_cpu_access (dmem);
572 if (!gst_d3d11_dxgi_format_get_size (desc->Format, desc->Width, desc->Height,
573 priv->map.RowPitch, offset, stride, &size)) {
574 GST_ERROR_OBJECT (mem->allocator, "Couldn't calculate memory size");
575 GST_D3D11_CLEAR_COM (priv->staging);
579 GST_D3D11_CLEAR_COM (priv->staging);
580 mem->maxsize = mem->size = size;
586 * gst_is_d3d11_memory:
589 * Returns: whether @mem is a #GstD3D11Memory
594 gst_is_d3d11_memory (GstMemory * mem)
596 return mem != NULL && mem->allocator != NULL &&
597 (GST_IS_D3D11_ALLOCATOR (mem->allocator) ||
598 GST_IS_D3D11_POOL_ALLOCATOR (mem->allocator));
602 * gst_d3d11_memory_get_native_type:
603 * @mem: a #GstD3D11Memory
605 * Returns: a #GstD3D11MemoryNativeType
609 GstD3D11MemoryNativeType
610 gst_d3d11_memory_get_native_type (GstD3D11Memory * mem)
612 if (!gst_is_d3d11_memory (GST_MEMORY_CAST (mem)))
613 return GST_D3D11_MEMORY_NATIVE_TYPE_INVALID;
615 return mem->priv->native_type;
619 * gst_d3d11_memory_init_once:
621 * Initializes the Direct3D11 Texture allocator. It is safe to call
622 * this function multiple times. This must be called before any other
623 * GstD3D11Memory operation.
628 gst_d3d11_memory_init_once (void)
630 GST_D3D11_CALL_ONCE_BEGIN {
631 GST_DEBUG_CATEGORY_INIT (gst_d3d11_allocator_debug, "d3d11allocator", 0,
632 "Direct3D11 Texture Allocator");
634 _d3d11_memory_allocator =
635 (GstD3D11Allocator *) g_object_new (GST_TYPE_D3D11_ALLOCATOR, NULL);
636 gst_object_ref_sink (_d3d11_memory_allocator);
637 gst_object_ref (_d3d11_memory_allocator);
639 gst_allocator_register (GST_D3D11_MEMORY_NAME,
640 GST_ALLOCATOR_CAST (_d3d11_memory_allocator));
641 } GST_D3D11_CALL_ONCE_END;
645 * gst_d3d11_memory_get_resource_handle:
646 * @mem: a #GstD3D11Memory
648 * Returns: (transfer none) (nullable): a ID3D11Resource handle. Caller must not release
654 gst_d3d11_memory_get_resource_handle (GstD3D11Memory * mem)
656 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
658 switch (mem->priv->native_type) {
659 case GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER:
660 return mem->priv->buffer;
661 case GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D:
662 return mem->priv->texture;
671 * gst_d3d11_memory_get_subresource_index:
672 * @mem: a #GstD3D11Memory
674 * Returns: subresource index corresponding to @mem.
679 gst_d3d11_memory_get_subresource_index (GstD3D11Memory * mem)
681 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), 0);
683 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
686 return mem->priv->subresource_index;
690 * gst_d3d11_memory_get_texture_desc:
691 * @mem: a #GstD3D11Memory
692 * @desc: (out): a D3D11_TEXTURE2D_DESC
694 * Fill @desc with D3D11_TEXTURE2D_DESC for ID3D11Texture2D
696 * Returns: %TRUE if successeed
701 gst_d3d11_memory_get_texture_desc (GstD3D11Memory * mem,
702 D3D11_TEXTURE2D_DESC * desc)
704 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), FALSE);
705 g_return_val_if_fail (desc != NULL, FALSE);
707 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
710 *desc = mem->priv->desc;
716 * gst_d3d11_memory_get_buffer_desc:
717 * @mem: a #GstD3D11Memory
718 * @desc: (out): a D3D11_BUFFER_DESC
720 * Fill @desc with D3D11_BUFFER_DESC for ID3D11Buffer
722 * Returns: %TRUE if successeed
727 gst_d3d11_memory_get_buffer_desc (GstD3D11Memory * mem,
728 D3D11_BUFFER_DESC * desc)
730 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), FALSE);
731 g_return_val_if_fail (desc != NULL, FALSE);
733 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER)
736 *desc = mem->priv->buffer_desc;
742 * gst_d3d11_memory_get_resource_stride:
743 * @mem: a #GstD3D11Memory
744 * @stride: (out): stride of resource
746 * Returns: %TRUE if successeed
751 gst_d3d11_memory_get_resource_stride (GstD3D11Memory * mem, guint * stride)
753 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), FALSE);
754 g_return_val_if_fail (stride != NULL, FALSE);
756 *stride = mem->priv->map.RowPitch;
762 create_shader_resource_views (GstD3D11Memory * mem)
764 GstD3D11MemoryPrivate *priv = mem->priv;
768 ID3D11Device *device_handle;
769 D3D11_SHADER_RESOURCE_VIEW_DESC resource_desc;
770 DXGI_FORMAT formats[GST_VIDEO_MAX_PLANES] = { DXGI_FORMAT_UNKNOWN, };
772 memset (&resource_desc, 0, sizeof (D3D11_SHADER_RESOURCE_VIEW_DESC));
774 device_handle = gst_d3d11_device_get_device_handle (mem->device);
776 num_views = gst_d3d11_dxgi_format_get_resource_format (priv->desc.Format,
779 GST_ERROR_OBJECT (GST_MEMORY_CAST (mem)->allocator,
780 "Unknown resource formats for DXGI format %s (%d)",
781 gst_d3d11_dxgi_format_to_string (priv->desc.Format), priv->desc.Format);
785 if ((priv->desc.BindFlags & D3D11_BIND_SHADER_RESOURCE) != 0) {
786 resource_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
787 resource_desc.Texture2D.MipLevels = 1;
789 for (i = 0; i < num_views; i++) {
790 resource_desc.Format = formats[i];
791 hr = device_handle->CreateShaderResourceView (priv->texture,
792 &resource_desc, &priv->shader_resource_view[i]);
794 if (!gst_d3d11_result (hr, mem->device)) {
795 GST_ERROR_OBJECT (GST_MEMORY_CAST (mem)->allocator,
796 "Failed to create resource DXGI format %s (%d) for plane %d"
797 " view (0x%x)", gst_d3d11_dxgi_format_to_string (formats[i]),
798 formats[i], i, (guint) hr);
803 priv->num_shader_resource_views = num_views;
811 for (i = 0; i < num_views; i++)
812 GST_D3D11_CLEAR_COM (priv->shader_resource_view[i]);
814 priv->num_shader_resource_views = 0;
820 gst_d3d11_memory_ensure_shader_resource_view (GstD3D11Memory * mem)
822 GstD3D11MemoryPrivate *priv = mem->priv;
824 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
827 if (!(priv->desc.BindFlags & D3D11_BIND_SHADER_RESOURCE)) {
828 GST_LOG_OBJECT (GST_MEMORY_CAST (mem)->allocator,
829 "Need BindFlags, current flag 0x%x", priv->desc.BindFlags);
833 GstD3D11SRWLockGuard lk (GST_D3D11_MEMORY_GET_LOCK (mem));
834 if (priv->num_shader_resource_views)
837 return create_shader_resource_views (mem);
841 * gst_d3d11_memory_get_shader_resource_view_size:
842 * @mem: a #GstD3D11Memory
844 * Returns: the number of ID3D11ShaderResourceView that can be used
845 * for processing GPU operation with @mem
850 gst_d3d11_memory_get_shader_resource_view_size (GstD3D11Memory * mem)
852 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), 0);
854 if (!gst_d3d11_memory_ensure_shader_resource_view (mem))
857 return mem->priv->num_shader_resource_views;
861 * gst_d3d11_memory_get_shader_resource_view:
862 * @mem: a #GstD3D11Memory
863 * @index: the index of the ID3D11ShaderResourceView
865 * Returns: (transfer none) (nullable): a pointer to the
866 * ID3D11ShaderResourceView or %NULL if ID3D11ShaderResourceView is unavailable
871 ID3D11ShaderResourceView *
872 gst_d3d11_memory_get_shader_resource_view (GstD3D11Memory * mem, guint index)
874 GstD3D11MemoryPrivate *priv;
876 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
878 if (!gst_d3d11_memory_ensure_shader_resource_view (mem))
883 if (index >= priv->num_shader_resource_views) {
884 GST_ERROR ("Invalid SRV index %d", index);
888 return priv->shader_resource_view[index];
892 create_render_target_views (GstD3D11Memory * mem)
894 GstD3D11MemoryPrivate *priv = mem->priv;
898 ID3D11Device *device_handle;
899 D3D11_RENDER_TARGET_VIEW_DESC render_desc;
900 DXGI_FORMAT formats[GST_VIDEO_MAX_PLANES] = { DXGI_FORMAT_UNKNOWN, };
902 memset (&render_desc, 0, sizeof (D3D11_RENDER_TARGET_VIEW_DESC));
904 device_handle = gst_d3d11_device_get_device_handle (mem->device);
906 num_views = gst_d3d11_dxgi_format_get_resource_format (priv->desc.Format,
909 GST_ERROR_OBJECT (GST_MEMORY_CAST (mem)->allocator,
910 "Unknown resource formats for DXGI format %s (%d)",
911 gst_d3d11_dxgi_format_to_string (priv->desc.Format), priv->desc.Format);
915 if ((priv->desc.BindFlags & D3D11_BIND_RENDER_TARGET) != 0) {
916 render_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
917 render_desc.Texture2D.MipSlice = 0;
919 for (i = 0; i < num_views; i++) {
920 render_desc.Format = formats[i];
922 hr = device_handle->CreateRenderTargetView (priv->texture, &render_desc,
923 &priv->render_target_view[i]);
924 if (!gst_d3d11_result (hr, mem->device)) {
925 GST_ERROR_OBJECT (GST_MEMORY_CAST (mem)->allocator,
926 "Failed to create resource DXGI format %s (%d) for plane %d"
927 " view (0x%x)", gst_d3d11_dxgi_format_to_string (formats[i]),
928 formats[i], i, (guint) hr);
933 priv->num_render_target_views = num_views;
941 for (i = 0; i < num_views; i++)
942 GST_D3D11_CLEAR_COM (priv->render_target_view[i]);
944 priv->num_render_target_views = 0;
950 gst_d3d11_memory_ensure_render_target_view (GstD3D11Memory * mem)
952 GstD3D11MemoryPrivate *priv = mem->priv;
954 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
957 if (!(priv->desc.BindFlags & D3D11_BIND_RENDER_TARGET)) {
958 GST_WARNING_OBJECT (GST_MEMORY_CAST (mem)->allocator,
959 "Need BindFlags, current flag 0x%x", priv->desc.BindFlags);
963 GstD3D11SRWLockGuard lk (GST_D3D11_MEMORY_GET_LOCK (mem));
964 if (priv->num_render_target_views)
967 return create_render_target_views (mem);
971 * gst_d3d11_memory_get_render_target_view_size:
972 * @mem: a #GstD3D11Memory
974 * Returns: the number of ID3D11RenderTargetView that can be used
975 * for processing GPU operation with @mem
980 gst_d3d11_memory_get_render_target_view_size (GstD3D11Memory * mem)
982 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), 0);
984 if (!gst_d3d11_memory_ensure_render_target_view (mem))
987 return mem->priv->num_render_target_views;
991 * gst_d3d11_memory_get_render_target_view:
992 * @mem: a #GstD3D11Memory
993 * @index: the index of the ID3D11RenderTargetView
995 * Returns: (transfer none) (nullable): a pointer to the
996 * ID3D11RenderTargetView or %NULL if ID3D11RenderTargetView is unavailable
1001 ID3D11RenderTargetView *
1002 gst_d3d11_memory_get_render_target_view (GstD3D11Memory * mem, guint index)
1004 GstD3D11MemoryPrivate *priv;
1006 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1008 if (!gst_d3d11_memory_ensure_render_target_view (mem))
1013 if (index >= priv->num_render_target_views) {
1014 GST_ERROR ("Invalid RTV index %d", index);
1018 return priv->render_target_view[index];
1022 gst_d3d11_memory_ensure_decoder_output_view (GstD3D11Memory * mem,
1023 ID3D11VideoDevice * video_device, ID3D11VideoDecoder * decoder,
1024 const GUID * decoder_profile)
1026 GstD3D11MemoryPrivate *dmem_priv = mem->priv;
1027 GstD3D11Allocator *allocator;
1028 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC desc;
1031 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
1034 allocator = GST_D3D11_ALLOCATOR (GST_MEMORY_CAST (mem)->allocator);
1036 if (!(dmem_priv->desc.BindFlags & D3D11_BIND_DECODER)) {
1037 GST_LOG_OBJECT (allocator,
1038 "Need BindFlags, current flag 0x%x", dmem_priv->desc.BindFlags);
1042 GstD3D11SRWLockGuard lk (GST_D3D11_MEMORY_GET_LOCK (mem));
1043 if (dmem_priv->decoder_output_view) {
1044 dmem_priv->decoder_output_view->GetDesc (&desc);
1045 if (IsEqualGUID (desc.DecodeProfile, *decoder_profile) &&
1046 dmem_priv->decoder_handle == decoder) {
1049 /* Shouldn't happen, but try again anyway */
1050 GST_WARNING_OBJECT (allocator,
1051 "Existing view has different decoder profile");
1052 GST_D3D11_CLEAR_COM (dmem_priv->decoder_output_view);
1053 GST_D3D11_CLEAR_COM (dmem_priv->decoder_handle);
1057 desc.DecodeProfile = *decoder_profile;
1058 desc.ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D;
1059 desc.Texture2D.ArraySlice = dmem_priv->subresource_index;
1061 hr = video_device->CreateVideoDecoderOutputView (dmem_priv->texture, &desc,
1062 &dmem_priv->decoder_output_view);
1063 if (!gst_d3d11_result (hr, mem->device)) {
1064 GST_ERROR_OBJECT (allocator,
1065 "Could not create decoder output view, hr: 0x%x", (guint) hr);
1069 /* XXX: decoder output view is bound to video device, not decoder handle
1070 * from API point of view. But some driver seems to be unhappy
1071 * when decoder handle is released while there are outstanding view objects */
1072 dmem_priv->decoder_handle = decoder;
1079 * gst_d3d11_memory_get_decoder_output_view:
1080 * @mem: a #GstD3D11Memory
1081 * @video_device: (transfer none): a ID3D11VideoDevice handle
1082 * @decoder: (transfer none): a ID3D11VideoDecoder handle
1083 * @decoder_profile: a DXVA decoder profile GUID
1085 * Returns: (transfer none) (nullable): a pointer to the
1086 * ID3D11VideoDecoderOutputView or %NULL if ID3D11VideoDecoderOutputView is
1091 ID3D11VideoDecoderOutputView *
1092 gst_d3d11_memory_get_decoder_output_view (GstD3D11Memory * mem,
1093 ID3D11VideoDevice * video_device, ID3D11VideoDecoder * decoder,
1094 const GUID * decoder_profile)
1096 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1097 g_return_val_if_fail (video_device != NULL, NULL);
1098 g_return_val_if_fail (decoder != NULL, NULL);
1099 g_return_val_if_fail (decoder_profile != NULL, NULL);
1101 if (!gst_d3d11_memory_ensure_decoder_output_view (mem,
1102 video_device, decoder, decoder_profile))
1105 return mem->priv->decoder_output_view;
1109 check_bind_flags_for_processor_input_view (guint bind_flags)
1111 static const guint compatible_flags = (D3D11_BIND_DECODER |
1112 D3D11_BIND_VIDEO_ENCODER | D3D11_BIND_RENDER_TARGET |
1113 D3D11_BIND_UNORDERED_ACCESS);
1115 if (bind_flags == 0)
1118 if ((bind_flags & compatible_flags) != 0)
1125 gst_d3d11_memory_ensure_processor_input_view (GstD3D11Memory * mem,
1126 ID3D11VideoDevice * video_device,
1127 ID3D11VideoProcessorEnumerator * enumerator)
1129 GstD3D11MemoryPrivate *dmem_priv = mem->priv;
1130 GstD3D11Allocator *allocator;
1131 D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC desc;
1134 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
1137 allocator = GST_D3D11_ALLOCATOR (GST_MEMORY_CAST (mem)->allocator);
1139 if (!check_bind_flags_for_processor_input_view (dmem_priv->desc.BindFlags)) {
1140 GST_LOG_OBJECT (allocator,
1141 "Need BindFlags, current flag 0x%x", dmem_priv->desc.BindFlags);
1145 GstD3D11SRWLockGuard lk (GST_D3D11_MEMORY_GET_LOCK (mem));
1146 if (dmem_priv->processor_input_view)
1150 desc.ViewDimension = D3D11_VPIV_DIMENSION_TEXTURE2D;
1151 desc.Texture2D.MipSlice = 0;
1152 desc.Texture2D.ArraySlice = dmem_priv->subresource_index;
1154 hr = video_device->CreateVideoProcessorInputView (dmem_priv->texture,
1155 enumerator, &desc, &dmem_priv->processor_input_view);
1156 if (!gst_d3d11_result (hr, mem->device)) {
1157 GST_ERROR_OBJECT (allocator,
1158 "Could not create processor input view, hr: 0x%x", (guint) hr);
1166 * gst_d3d11_memory_get_processor_input_view:
1167 * @mem: a #GstD3D11Memory
1168 * @video_device: a #ID3D11VideoDevice
1169 * @enumerator: a #ID3D11VideoProcessorEnumerator
1171 * Returns: (transfer none) (nullable): a pointer to the
1172 * ID3D11VideoProcessorInputView or %NULL if ID3D11VideoProcessorInputView is
1177 ID3D11VideoProcessorInputView *
1178 gst_d3d11_memory_get_processor_input_view (GstD3D11Memory * mem,
1179 ID3D11VideoDevice * video_device,
1180 ID3D11VideoProcessorEnumerator * enumerator)
1182 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1183 g_return_val_if_fail (video_device != NULL, NULL);
1184 g_return_val_if_fail (enumerator != NULL, NULL);
1186 if (!gst_d3d11_memory_ensure_processor_input_view (mem, video_device,
1190 return mem->priv->processor_input_view;
1194 gst_d3d11_memory_ensure_processor_output_view (GstD3D11Memory * mem,
1195 ID3D11VideoDevice * video_device,
1196 ID3D11VideoProcessorEnumerator * enumerator)
1198 GstD3D11MemoryPrivate *priv = mem->priv;
1199 GstD3D11Allocator *allocator;
1200 D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC desc;
1203 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
1206 memset (&desc, 0, sizeof (D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC));
1208 allocator = GST_D3D11_ALLOCATOR (GST_MEMORY_CAST (mem)->allocator);
1210 if (!(priv->desc.BindFlags & D3D11_BIND_RENDER_TARGET)) {
1211 GST_LOG_OBJECT (allocator,
1212 "Need BindFlags, current flag 0x%x", priv->desc.BindFlags);
1216 /* FIXME: texture array should be supported at some point */
1217 if (priv->subresource_index != 0) {
1218 GST_FIXME_OBJECT (allocator,
1219 "Texture array is not suppoted for processor output view");
1223 GstD3D11SRWLockGuard lk (GST_D3D11_MEMORY_GET_LOCK (mem));
1224 if (priv->processor_output_view)
1227 desc.ViewDimension = D3D11_VPOV_DIMENSION_TEXTURE2D;
1228 desc.Texture2D.MipSlice = 0;
1230 hr = video_device->CreateVideoProcessorOutputView (priv->texture,
1231 enumerator, &desc, &priv->processor_output_view);
1232 if (!gst_d3d11_result (hr, mem->device)) {
1233 GST_ERROR_OBJECT (allocator,
1234 "Could not create processor input view, hr: 0x%x", (guint) hr);
1242 * gst_d3d11_memory_get_processor_output_view:
1243 * @mem: a #GstD3D11Memory
1244 * @video_device: a #ID3D11VideoDevice
1245 * @enumerator: a #ID3D11VideoProcessorEnumerator
1247 * Returns: (transfer none) (nullable): a pointer to the
1248 * ID3D11VideoProcessorOutputView or %NULL if ID3D11VideoProcessorOutputView is
1253 ID3D11VideoProcessorOutputView *
1254 gst_d3d11_memory_get_processor_output_view (GstD3D11Memory * mem,
1255 ID3D11VideoDevice * video_device,
1256 ID3D11VideoProcessorEnumerator * enumerator)
1258 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1259 g_return_val_if_fail (video_device != NULL, NULL);
1260 g_return_val_if_fail (enumerator != NULL, NULL);
1262 if (!gst_d3d11_memory_ensure_processor_output_view (mem, video_device,
1266 return mem->priv->processor_output_view;
1269 /* GstD3D11Allocator */
1270 struct _GstD3D11AllocatorPrivate
1272 GstMemoryCopyFunction fallback_copy;
1275 #define gst_d3d11_allocator_parent_class alloc_parent_class
1276 G_DEFINE_TYPE_WITH_PRIVATE (GstD3D11Allocator,
1277 gst_d3d11_allocator, GST_TYPE_ALLOCATOR);
1279 static GstMemory *gst_d3d11_allocator_dummy_alloc (GstAllocator * allocator,
1280 gsize size, GstAllocationParams * params);
1281 static GstMemory *gst_d3d11_allocator_alloc_internal (GstD3D11Allocator * self,
1282 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc,
1283 ID3D11Texture2D * texture);
1284 static void gst_d3d11_allocator_free (GstAllocator * allocator,
1288 gst_d3d11_allocator_class_init (GstD3D11AllocatorClass * klass)
1290 GstAllocatorClass *allocator_class = GST_ALLOCATOR_CLASS (klass);
1292 allocator_class->alloc = gst_d3d11_allocator_dummy_alloc;
1293 allocator_class->free = gst_d3d11_allocator_free;
1297 gst_d3d11_memory_copy (GstMemory * mem, gssize offset, gssize size)
1299 GstD3D11Allocator *alloc = GST_D3D11_ALLOCATOR (mem->allocator);
1300 GstD3D11AllocatorPrivate *priv = alloc->priv;
1301 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
1302 GstD3D11Memory *copy_dmem;
1303 GstD3D11Device *device = dmem->device;
1304 ID3D11DeviceContext *device_context =
1305 gst_d3d11_device_get_device_context_handle (device);
1306 D3D11_TEXTURE2D_DESC dst_desc = { 0, };
1307 D3D11_TEXTURE2D_DESC src_desc = { 0, };
1308 GstMemory *copy = NULL;
1311 if (dmem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
1312 return priv->fallback_copy (mem, offset, size);
1314 /* non-zero offset or different size is not supported */
1315 if (offset != 0 || (size != -1 && (gsize) size != mem->size)) {
1316 GST_DEBUG_OBJECT (alloc, "Different size/offset, try fallback copy");
1317 return priv->fallback_copy (mem, offset, size);
1320 GstD3D11DeviceLockGuard lk (device);
1322 if (!gst_memory_map (mem, &info,
1323 (GstMapFlags) (GST_MAP_READ | GST_MAP_D3D11))) {
1325 GST_WARNING_OBJECT (alloc, "Failed to map memory, try fallback copy");
1327 return priv->fallback_copy (mem, offset, size);
1330 dmem->priv->texture->GetDesc (&src_desc);
1331 dst_desc.Width = src_desc.Width;
1332 dst_desc.Height = src_desc.Height;
1333 dst_desc.MipLevels = 1;
1334 dst_desc.Format = src_desc.Format;
1335 dst_desc.SampleDesc.Count = 1;
1336 dst_desc.ArraySize = 1;
1337 dst_desc.Usage = D3D11_USAGE_DEFAULT;
1338 dst_desc.BindFlags = src_desc.BindFlags;
1340 copy = gst_d3d11_allocator_alloc_internal (alloc, device, &dst_desc, nullptr);
1342 gst_memory_unmap (mem, &info);
1344 GST_WARNING_OBJECT (alloc,
1345 "Failed to allocate new d3d11 map memory, try fallback copy");
1347 return priv->fallback_copy (mem, offset, size);
1350 copy_dmem = GST_D3D11_MEMORY_CAST (copy);
1351 device_context->CopySubresourceRegion (copy_dmem->priv->texture, 0, 0, 0, 0,
1352 dmem->priv->texture, dmem->priv->subresource_index, NULL);
1353 copy->maxsize = copy->size = mem->maxsize;
1354 gst_memory_unmap (mem, &info);
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_D3D11_CLEAR_COM (dmem_priv->decoder_handle);
1414 gst_clear_object (&dmem->device);
1416 if (dmem_priv->notify)
1417 dmem_priv->notify (dmem_priv->user_data);
1419 g_free (dmem->priv);
1424 gst_d3d11_allocator_alloc_wrapped_internal (GstD3D11Allocator * self,
1425 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc,
1426 ID3D11Texture2D * texture)
1428 GstD3D11Memory *mem;
1430 mem = g_new0 (GstD3D11Memory, 1);
1431 mem->priv = g_new0 (GstD3D11MemoryPrivate, 1);
1433 gst_memory_init (GST_MEMORY_CAST (mem),
1434 (GstMemoryFlags) 0, GST_ALLOCATOR_CAST (self), NULL, 0, 0, 0, 0);
1435 mem->priv->texture = texture;
1436 mem->priv->desc = *desc;
1437 mem->priv->native_type = GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D;
1438 mem->device = (GstD3D11Device *) gst_object_ref (device);
1440 return GST_MEMORY_CAST (mem);
1443 typedef void (*GstD3D11ClearRTVFunc) (ID3D11DeviceContext * context_handle,
1444 ID3D11RenderTargetView * rtv);
1447 clear_rtv_chroma (ID3D11DeviceContext * context_handle,
1448 ID3D11RenderTargetView * rtv)
1450 const FLOAT clear_color[4] = { 0.5f, 0.5f, 0.5f, 1.0f };
1452 context_handle->ClearRenderTargetView (rtv, clear_color);
1456 clear_rtv_vuya (ID3D11DeviceContext * context_handle,
1457 ID3D11RenderTargetView * rtv)
1459 const FLOAT clear_color[4] = { 0.5f, 0.5f, 0.0f, 1.0f };
1461 context_handle->ClearRenderTargetView (rtv, clear_color);
1465 gst_d3d11_allocator_alloc_internal (GstD3D11Allocator * self,
1466 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc,
1467 ID3D11Texture2D * texture)
1469 ID3D11Device *device_handle;
1470 ID3D11DeviceContext *context_handle;
1473 GstD3D11Memory *dmem;
1474 ID3D11RenderTargetView *rtv = nullptr;
1475 GstD3D11ClearRTVFunc clear_func = nullptr;
1476 gboolean is_new_texture = TRUE;
1478 device_handle = gst_d3d11_device_get_device_handle (device);
1481 hr = device_handle->CreateTexture2D (desc, nullptr, &texture);
1482 if (!gst_d3d11_result (hr, device)) {
1483 GST_ERROR_OBJECT (self, "Couldn't create texture");
1487 is_new_texture = FALSE;
1491 gst_d3d11_allocator_alloc_wrapped_internal (self, device, desc, texture);
1495 /* Don't clear external texture */
1496 if (!is_new_texture)
1499 /* Clear with YUV black if needed and possible
1500 * TODO: do this using UAV if RTV is not allowed (e.g., packed YUV formats) */
1501 if ((desc->BindFlags & D3D11_BIND_RENDER_TARGET) == 0)
1504 dmem = GST_D3D11_MEMORY_CAST (mem);
1505 switch (desc->Format) {
1506 case DXGI_FORMAT_NV12:
1507 case DXGI_FORMAT_P010:
1508 case DXGI_FORMAT_P016:
1509 /* Y component will be zero already */
1510 rtv = gst_d3d11_memory_get_render_target_view (dmem, 1);
1511 clear_func = (GstD3D11ClearRTVFunc) clear_rtv_chroma;
1513 case DXGI_FORMAT_AYUV:
1514 rtv = gst_d3d11_memory_get_render_target_view (dmem, 0);
1515 clear_func = (GstD3D11ClearRTVFunc) clear_rtv_vuya;
1524 context_handle = gst_d3d11_device_get_device_context_handle (device);
1525 GstD3D11DeviceLockGuard lk (device);
1526 clear_func (context_handle, rtv);
1532 * gst_d3d11_allocator_alloc:
1533 * @allocator: (transfer none) (allow-none): a #GstD3D11Allocator
1534 * @device: (transfer none): a #GstD3D11Device
1535 * @desc: a D3D11_TEXTURE2D_DESC struct
1537 * Returns: (transfer full) (nullable): a newly allocated #GstD3D11Memory with given parameters.
1542 gst_d3d11_allocator_alloc (GstD3D11Allocator * allocator,
1543 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc)
1548 gst_d3d11_memory_init_once ();
1549 allocator = _d3d11_memory_allocator;
1552 g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), NULL);
1553 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), NULL);
1554 g_return_val_if_fail (desc != NULL, NULL);
1556 mem = gst_d3d11_allocator_alloc_internal (allocator, device, desc, nullptr);
1560 if (!gst_d3d11_memory_update_size (mem)) {
1561 GST_ERROR_OBJECT (allocator, "Failed to calculate size");
1562 gst_memory_unref (mem);
1570 * gst_d3d11_allocator_alloc_buffer:
1571 * @allocator: (transfer none) (allow-none): a #GstD3D11Allocator
1572 * @device: (transfer none): a #GstD3D11Device
1573 * @desc: a D3D11_BUFFER_DESC struct
1575 * Returns: (transfer full) (nullable): a newly allocated #GstD3D11Memory with given parameters.
1580 gst_d3d11_allocator_alloc_buffer (GstD3D11Allocator * allocator,
1581 GstD3D11Device * device, const D3D11_BUFFER_DESC * desc)
1583 GstD3D11Memory *mem;
1584 ID3D11Buffer *buffer;
1585 ID3D11Device *device_handle;
1589 gst_d3d11_memory_init_once ();
1590 allocator = _d3d11_memory_allocator;
1593 g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), nullptr);
1594 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), nullptr);
1595 g_return_val_if_fail (desc != nullptr, nullptr);
1597 if (desc->Usage != D3D11_USAGE_STAGING) {
1598 GST_FIXME_OBJECT (allocator, "Non staging buffer is not supported");
1602 device_handle = gst_d3d11_device_get_device_handle (device);
1604 hr = device_handle->CreateBuffer (desc, nullptr, &buffer);
1605 if (!gst_d3d11_result (hr, device)) {
1606 GST_ERROR_OBJECT (allocator, "Couldn't create buffer");
1610 mem = g_new0 (GstD3D11Memory, 1);
1611 mem->priv = g_new0 (GstD3D11MemoryPrivate, 1);
1613 gst_memory_init (GST_MEMORY_CAST (mem),
1614 (GstMemoryFlags) 0, GST_ALLOCATOR_CAST (allocator), nullptr, 0, 0, 0, 0);
1615 mem->priv->buffer = buffer;
1616 mem->priv->buffer_desc = *desc;
1617 mem->priv->native_type = GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER;
1618 mem->device = (GstD3D11Device *) gst_object_ref (device);
1620 GST_MEMORY_CAST (mem)->maxsize = GST_MEMORY_CAST (mem)->size =
1623 return GST_MEMORY_CAST (mem);
1627 * gst_d3d11_allocator_alloc_wrapped:
1628 * @allocator: (transfer none) (allow-none): a #GstD3D11Allocator
1629 * @device: (transfer none): a #GstD3D11Device
1630 * @texture: a ID3D11Texture2D
1631 * @size: CPU accessible memory size
1632 * @user_data: (allow-none): user data
1633 * @notify: (allow-none): called with @user_data when the memory is freed
1635 * Allocates memory object with @texture. The refcount of @texture
1636 * will be increased by one.
1638 * Caller should set valid CPU acessible memory value to @size
1639 * (which is typically calculated by using staging texture and Map/Unmap)
1640 * or zero is allowed. In that case, allocator will create a temporary staging
1641 * texture to get the size and the temporary staging texture will be released.
1643 * Caller must not be confused that @size is CPU accessible size, not raw
1646 * Returns: (transfer full) (nullable): a newly allocated #GstD3D11Memory with given @texture
1647 * if successful, or %NULL if @texture is not a valid handle or configuration
1653 gst_d3d11_allocator_alloc_wrapped (GstD3D11Allocator * allocator,
1654 GstD3D11Device * device, ID3D11Texture2D * texture, gsize size,
1655 gpointer user_data, GDestroyNotify notify)
1658 GstD3D11Memory *dmem;
1659 D3D11_TEXTURE2D_DESC desc = { 0, };
1660 ID3D11Texture2D *tex = nullptr;
1664 gst_d3d11_memory_init_once ();
1665 allocator = _d3d11_memory_allocator;
1668 g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), nullptr);
1669 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), nullptr);
1670 g_return_val_if_fail (texture != nullptr, nullptr);
1672 hr = texture->QueryInterface (IID_PPV_ARGS (&tex));
1674 GST_WARNING_OBJECT (allocator, "Not a valid texture handle");
1678 tex->GetDesc (&desc);
1679 mem = gst_d3d11_allocator_alloc_internal (allocator, device, &desc, tex);
1685 if (!gst_d3d11_memory_update_size (mem)) {
1686 GST_ERROR_OBJECT (allocator, "Failed to calculate size");
1687 gst_memory_unref (mem);
1691 mem->maxsize = mem->size = size;
1694 dmem = GST_D3D11_MEMORY_CAST (mem);
1696 dmem->priv->user_data = user_data;
1697 dmem->priv->notify = notify;
1703 * gst_d3d11_allocator_set_active:
1704 * @allocator: a #GstD3D11Allocator
1705 * @active: the new active state
1707 * Controls the active state of @allocator. Default #GstD3D11Allocator is
1708 * stateless and therefore active state is ignored, but subclass implementation
1709 * (e.g., #GstD3D11PoolAllocator) will require explicit active state control
1710 * for its internal resource management.
1712 * This method is conceptually identical to gst_buffer_pool_set_active method.
1714 * Returns: %TRUE if active state of @allocator was successfully updated.
1719 gst_d3d11_allocator_set_active (GstD3D11Allocator * allocator, gboolean active)
1721 GstD3D11AllocatorClass *klass;
1723 g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), FALSE);
1725 klass = GST_D3D11_ALLOCATOR_GET_CLASS (allocator);
1726 if (klass->set_active)
1727 return klass->set_active (allocator, active);
1732 /* GstD3D11PoolAllocator */
1733 #define GST_D3D11_POOL_ALLOCATOR_GET_LOCK(alloc) \
1734 (&(GST_D3D11_POOL_ALLOCATOR_CAST(alloc)->priv->lock))
1735 #define GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING(alloc) (g_atomic_int_get (&alloc->priv->flushing))
1737 struct _GstD3D11PoolAllocatorPrivate
1739 /* parent texture when array typed memory is used */
1740 ID3D11Texture2D *texture;
1741 D3D11_TEXTURE2D_DESC desc;
1743 /* All below member variables are analogous to that of GstBufferPool */
1744 GstAtomicQueue *queue;
1747 /* This lock will protect all below variables apart from atomic ones
1748 * (identical to GstBufferPool::priv::rec_lock) */
1749 CRITICAL_SECTION lock;
1759 /* Calculated memory size, based on Direct3D11 staging texture map.
1760 * Note that, we cannot know the actually staging texture memory size prior
1761 * to map the staging texture because driver will likely require padding */
1765 static void gst_d3d11_pool_allocator_finalize (GObject * object);
1768 gst_d3d11_pool_allocator_set_active (GstD3D11Allocator * allocator,
1771 static gboolean gst_d3d11_pool_allocator_start (GstD3D11PoolAllocator * self);
1772 static gboolean gst_d3d11_pool_allocator_stop (GstD3D11PoolAllocator * self);
1773 static gboolean gst_d3d11_memory_release (GstMiniObject * mini_object);
1775 #define gst_d3d11_pool_allocator_parent_class pool_alloc_parent_class
1776 G_DEFINE_TYPE_WITH_PRIVATE (GstD3D11PoolAllocator,
1777 gst_d3d11_pool_allocator, GST_TYPE_D3D11_ALLOCATOR);
1780 gst_d3d11_pool_allocator_class_init (GstD3D11PoolAllocatorClass * klass)
1782 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1783 GstD3D11AllocatorClass *d3d11alloc_class = GST_D3D11_ALLOCATOR_CLASS (klass);
1785 gobject_class->finalize = gst_d3d11_pool_allocator_finalize;
1787 d3d11alloc_class->set_active = gst_d3d11_pool_allocator_set_active;
1791 gst_d3d11_pool_allocator_init (GstD3D11PoolAllocator * allocator)
1793 GstD3D11PoolAllocatorPrivate *priv;
1795 priv = allocator->priv = (GstD3D11PoolAllocatorPrivate *)
1796 gst_d3d11_pool_allocator_get_instance_private (allocator);
1798 InitializeCriticalSection (&priv->lock);
1800 priv->poll = gst_poll_new_timer ();
1801 priv->queue = gst_atomic_queue_new (16);
1803 priv->active = FALSE;
1804 priv->started = FALSE;
1806 /* 1 control write for flushing - the flush token */
1807 gst_poll_write_control (priv->poll);
1808 /* 1 control write for marking that we are not waiting for poll - the wait token */
1809 gst_poll_write_control (priv->poll);
1813 gst_d3d11_pool_allocator_finalize (GObject * object)
1815 GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (object);
1816 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1818 GST_DEBUG_OBJECT (self, "Finalize");
1820 gst_d3d11_pool_allocator_stop (self);
1821 gst_atomic_queue_unref (priv->queue);
1822 gst_poll_free (priv->poll);
1823 DeleteCriticalSection (&priv->lock);
1825 GST_D3D11_CLEAR_COM (priv->texture);
1827 gst_clear_object (&self->device);
1829 G_OBJECT_CLASS (pool_alloc_parent_class)->finalize (object);
1833 gst_d3d11_pool_allocator_start (GstD3D11PoolAllocator * self)
1835 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1836 ID3D11Device *device_handle;
1844 if (priv->desc.ArraySize == 1) {
1845 priv->started = TRUE;
1849 device_handle = gst_d3d11_device_get_device_handle (self->device);
1851 if (!priv->texture) {
1852 hr = device_handle->CreateTexture2D (&priv->desc, NULL, &priv->texture);
1853 if (!gst_d3d11_result (hr, self->device)) {
1854 GST_ERROR_OBJECT (self, "Failed to allocate texture");
1859 /* Pre-allocate memory objects */
1860 for (i = 0; i < priv->desc.ArraySize; i++) {
1863 priv->texture->AddRef ();
1864 mem = gst_d3d11_allocator_alloc_wrapped_internal (_d3d11_memory_allocator,
1865 self->device, &priv->desc, priv->texture);
1868 if (!gst_d3d11_memory_update_size (mem)) {
1869 GST_ERROR_OBJECT (self, "Failed to calculate memory size");
1870 gst_memory_unref (mem);
1874 priv->mem_size = mem->size;
1876 mem->size = mem->maxsize = priv->mem_size;
1879 GST_D3D11_MEMORY_CAST (mem)->priv->subresource_index = i;
1881 g_atomic_int_add (&priv->cur_mems, 1);
1882 gst_atomic_queue_push (priv->queue, mem);
1883 gst_poll_write_control (priv->poll);
1886 priv->started = TRUE;
1892 gst_d3d11_pool_allocator_do_set_flushing (GstD3D11PoolAllocator * self,
1895 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1897 if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self) == flushing)
1901 g_atomic_int_set (&priv->flushing, 1);
1902 /* Write the flush token to wake up any waiters */
1903 gst_poll_write_control (priv->poll);
1905 while (!gst_poll_read_control (priv->poll)) {
1906 if (errno == EWOULDBLOCK) {
1907 /* This should not really happen unless flushing and unflushing
1908 * happens on different threads. Let's wait a bit to get back flush
1909 * token from the thread that was setting it to flushing */
1913 /* Critical error but GstPoll already complained */
1918 g_atomic_int_set (&priv->flushing, 0);
1923 gst_d3d11_pool_allocator_set_active (GstD3D11Allocator * allocator,
1926 GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (allocator);
1927 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1929 GST_LOG_OBJECT (self, "active %d", active);
1931 GstD3D11CSLockGuard lk (GST_D3D11_POOL_ALLOCATOR_GET_LOCK (self));
1932 /* just return if we are already in the right state */
1933 if (priv->active == active)
1937 if (!gst_d3d11_pool_allocator_start (self)) {
1938 GST_ERROR_OBJECT (self, "start failed");
1942 /* flush_stop may release memory objects, setting to active to avoid running
1943 * do_stop while activating the pool */
1944 priv->active = TRUE;
1946 gst_d3d11_pool_allocator_do_set_flushing (self, FALSE);
1950 /* set to flushing first */
1951 gst_d3d11_pool_allocator_do_set_flushing (self, TRUE);
1953 /* when all memory objects are in the pool, free them. Else they will be
1954 * freed when they are released */
1955 outstanding = g_atomic_int_get (&priv->outstanding);
1956 GST_LOG_OBJECT (self, "outstanding memories %d, (in queue %d)",
1957 outstanding, gst_atomic_queue_length (priv->queue));
1958 if (outstanding == 0) {
1959 if (!gst_d3d11_pool_allocator_stop (self)) {
1960 GST_ERROR_OBJECT (self, "stop failed");
1965 priv->active = FALSE;
1972 gst_d3d11_pool_allocator_free_memory (GstD3D11PoolAllocator * self,
1975 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1977 g_atomic_int_add (&priv->cur_mems, -1);
1978 GST_LOG_OBJECT (self, "freeing memory %p (%u left)", mem, priv->cur_mems);
1980 GST_MINI_OBJECT_CAST (mem)->dispose = NULL;
1981 gst_memory_unref (mem);
1984 /* must be called with the lock */
1986 gst_d3d11_pool_allocator_clear_queue (GstD3D11PoolAllocator * self)
1988 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1991 GST_LOG_OBJECT (self, "Clearing queue");
1993 /* clear the pool */
1994 while ((memory = (GstMemory *) gst_atomic_queue_pop (priv->queue))) {
1995 while (!gst_poll_read_control (priv->poll)) {
1996 if (errno == EWOULDBLOCK) {
1997 /* We put the memory into the queue but did not finish writing control
1998 * yet, let's wait a bit and retry */
2002 /* Critical error but GstPoll already complained */
2006 gst_d3d11_pool_allocator_free_memory (self, memory);
2009 GST_LOG_OBJECT (self, "Clear done");
2011 return priv->cur_mems == 0;
2014 /* must be called with the lock */
2016 gst_d3d11_pool_allocator_stop (GstD3D11PoolAllocator * self)
2018 GstD3D11PoolAllocatorPrivate *priv = self->priv;
2020 GST_DEBUG_OBJECT (self, "Stop");
2022 if (priv->started) {
2023 if (!gst_d3d11_pool_allocator_clear_queue (self))
2026 priv->started = FALSE;
2028 GST_DEBUG_OBJECT (self, "Wasn't started");
2035 dec_outstanding (GstD3D11PoolAllocator * self)
2037 if (g_atomic_int_dec_and_test (&self->priv->outstanding)) {
2038 /* all memory objects are returned to the pool, see if we need to free them */
2039 if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)) {
2040 /* take the lock so that set_active is not run concurrently */
2041 GstD3D11CSLockGuard lk (GST_D3D11_POOL_ALLOCATOR_GET_LOCK (self));
2042 /* now that we have the lock, check if we have been de-activated with
2043 * outstanding buffers */
2044 if (!self->priv->active)
2045 gst_d3d11_pool_allocator_stop (self);
2051 gst_d3d11_pool_allocator_release_memory (GstD3D11PoolAllocator * self,
2054 GST_LOG_OBJECT (self, "Released memory %p", mem);
2056 GST_MINI_OBJECT_CAST (mem)->dispose = NULL;
2057 mem->allocator = (GstAllocator *) gst_object_ref (_d3d11_memory_allocator);
2059 /* keep it around in our queue */
2060 gst_atomic_queue_push (self->priv->queue, mem);
2061 gst_poll_write_control (self->priv->poll);
2062 dec_outstanding (self);
2064 gst_object_unref (self);
2068 gst_d3d11_memory_release (GstMiniObject * mini_object)
2070 GstMemory *mem = GST_MEMORY_CAST (mini_object);
2071 GstD3D11PoolAllocator *alloc;
2073 g_assert (mem->allocator != NULL);
2075 if (!GST_IS_D3D11_POOL_ALLOCATOR (mem->allocator)) {
2076 GST_LOG_OBJECT (mem->allocator, "Not our memory, free");
2080 alloc = GST_D3D11_POOL_ALLOCATOR (mem->allocator);
2081 /* if flushing, free this memory */
2082 if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (alloc)) {
2083 GST_LOG_OBJECT (alloc, "allocator is flushing, free %p", mem);
2087 /* return the memory to the allocator */
2088 gst_memory_ref (mem);
2089 gst_d3d11_pool_allocator_release_memory (alloc, mem);
2094 static GstFlowReturn
2095 gst_d3d11_pool_allocator_alloc (GstD3D11PoolAllocator * self, GstMemory ** mem)
2097 GstD3D11PoolAllocatorPrivate *priv = self->priv;
2100 /* we allcates texture array during start */
2101 if (priv->desc.ArraySize > 1)
2102 return GST_FLOW_EOS;
2104 /* increment the allocation counter */
2105 g_atomic_int_add (&priv->cur_mems, 1);
2106 new_mem = gst_d3d11_allocator_alloc_internal (_d3d11_memory_allocator,
2107 self->device, &priv->desc, nullptr);
2109 GST_ERROR_OBJECT (self, "Failed to allocate new memory");
2110 g_atomic_int_add (&priv->cur_mems, -1);
2111 return GST_FLOW_ERROR;
2114 if (!priv->mem_size) {
2115 if (!gst_d3d11_memory_update_size (new_mem)) {
2116 GST_ERROR_OBJECT (self, "Failed to calculate size");
2117 gst_memory_unref (new_mem);
2118 g_atomic_int_add (&priv->cur_mems, -1);
2120 return GST_FLOW_ERROR;
2123 priv->mem_size = new_mem->size;
2126 new_mem->size = new_mem->maxsize = priv->mem_size;
2133 static GstFlowReturn
2134 gst_d3d11_pool_allocator_acquire_memory_internal (GstD3D11PoolAllocator * self,
2135 GstMemory ** memory)
2137 GstFlowReturn result;
2138 GstD3D11PoolAllocatorPrivate *priv = self->priv;
2141 if (G_UNLIKELY (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)))
2144 /* try to get a memory from the queue */
2145 *memory = (GstMemory *) gst_atomic_queue_pop (priv->queue);
2146 if (G_LIKELY (*memory)) {
2147 while (!gst_poll_read_control (priv->poll)) {
2148 if (errno == EWOULDBLOCK) {
2149 /* We put the memory into the queue but did not finish writing control
2150 * yet, let's wait a bit and retry */
2154 /* Critical error but GstPoll already complained */
2158 result = GST_FLOW_OK;
2159 GST_LOG_OBJECT (self, "acquired memory %p", *memory);
2163 /* no memory, try to allocate some more */
2164 GST_LOG_OBJECT (self, "no memory, trying to allocate");
2165 result = gst_d3d11_pool_allocator_alloc (self, memory);
2166 if (G_LIKELY (result == GST_FLOW_OK))
2167 /* we have a memory, return it */
2170 if (G_UNLIKELY (result != GST_FLOW_EOS))
2171 /* something went wrong, return error */
2174 /* now we release the control socket, we wait for a memory release or
2176 if (!gst_poll_read_control (priv->poll)) {
2177 if (errno == EWOULDBLOCK) {
2178 /* This means that we have two threads trying to allocate memory
2179 * already, and the other one already got the wait token. This
2180 * means that we only have to wait for the poll now and not write the
2181 * token afterwards: we will be woken up once the other thread is
2182 * woken up and that one will write the wait token it removed */
2183 GST_LOG_OBJECT (self, "waiting for free memory or flushing");
2184 gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
2186 /* This is a critical error, GstPoll already gave a warning */
2187 result = GST_FLOW_ERROR;
2191 /* We're the first thread waiting, we got the wait token and have to
2192 * write it again later
2194 * We're a second thread and just consumed the flush token and block all
2195 * other threads, in which case we must not wait and give it back
2197 if (!GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)) {
2198 GST_LOG_OBJECT (self, "waiting for free memory or flushing");
2199 gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
2201 gst_poll_write_control (priv->poll);
2210 GST_DEBUG_OBJECT (self, "we are flushing");
2211 return GST_FLOW_FLUSHING;
2216 * gst_d3d11_pool_allocator_new:
2217 * @device: a #GstD3D11Device
2218 * @desc: a D3D11_TEXTURE2D_DESC for texture allocation
2220 * Creates a new #GstD3D11PoolAllocator instance.
2222 * Returns: (transfer full): a new #GstD3D11PoolAllocator instance
2226 GstD3D11PoolAllocator *
2227 gst_d3d11_pool_allocator_new (GstD3D11Device * device,
2228 const D3D11_TEXTURE2D_DESC * desc)
2230 GstD3D11PoolAllocator *self;
2232 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), NULL);
2233 g_return_val_if_fail (desc != NULL, NULL);
2235 gst_d3d11_memory_init_once ();
2237 self = (GstD3D11PoolAllocator *)
2238 g_object_new (GST_TYPE_D3D11_POOL_ALLOCATOR, NULL);
2239 gst_object_ref_sink (self);
2241 self->device = (GstD3D11Device *) gst_object_ref (device);
2242 self->priv->desc = *desc;
2248 * gst_d3d11_pool_allocator_acquire_memory:
2249 * @allocator: a #GstD3D11PoolAllocator
2250 * @memory: (out): a #GstMemory
2252 * Acquires a #GstMemory from @allocator. @memory should point to a memory
2253 * location that can hold a pointer to the new #GstMemory.
2255 * Returns: a #GstFlowReturn such as %GST_FLOW_FLUSHING when the allocator is
2259 gst_d3d11_pool_allocator_acquire_memory (GstD3D11PoolAllocator * allocator,
2260 GstMemory ** memory)
2262 GstD3D11PoolAllocatorPrivate *priv;
2263 GstFlowReturn result;
2265 g_return_val_if_fail (GST_IS_D3D11_POOL_ALLOCATOR (allocator),
2267 g_return_val_if_fail (memory != NULL, GST_FLOW_ERROR);
2269 priv = allocator->priv;
2271 /* assume we'll have one more outstanding buffer we need to do that so
2272 * that concurrent set_active doesn't clear the buffers */
2273 g_atomic_int_inc (&priv->outstanding);
2274 result = gst_d3d11_pool_allocator_acquire_memory_internal (allocator, memory);
2276 if (result == GST_FLOW_OK) {
2277 GstMemory *mem = *memory;
2278 /* Replace default allocator with ours */
2279 gst_object_unref (mem->allocator);
2280 mem->allocator = (GstAllocator *) gst_object_ref (allocator);
2281 GST_MINI_OBJECT_CAST (mem)->dispose = gst_d3d11_memory_release;
2283 dec_outstanding (allocator);
2290 * gst_d3d11_pool_allocator_get_pool_size:
2291 * @allocator: a #GstD3D11PoolAllocator
2292 * @max_size: (out) (optional): the max size of pool
2293 * @outstanding_size: (out) (optional): the number of outstanding memory
2295 * Returns: %TRUE if the size of memory pool is known
2300 gst_d3d11_pool_allocator_get_pool_size (GstD3D11PoolAllocator * allocator,
2301 guint * max_size, guint * outstanding_size)
2303 GstD3D11PoolAllocatorPrivate *priv;
2305 g_return_val_if_fail (GST_IS_D3D11_POOL_ALLOCATOR (allocator), FALSE);
2307 priv = allocator->priv;
2310 if (priv->desc.ArraySize > 1) {
2311 *max_size = priv->desc.ArraySize;
2313 /* For non-texture-array memory, we don't have any limit yet */
2318 if (outstanding_size)
2319 *outstanding_size = g_atomic_int_get (&priv->outstanding);