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;
1477 device_handle = gst_d3d11_device_get_device_handle (device);
1480 hr = device_handle->CreateTexture2D (desc, nullptr, &texture);
1481 if (!gst_d3d11_result (hr, device)) {
1482 GST_ERROR_OBJECT (self, "Couldn't create texture");
1488 gst_d3d11_allocator_alloc_wrapped_internal (self, device, desc, texture);
1492 /* Clear with YUV black if needed and possible
1493 * TODO: do this using UAV if RTV is not allowed (e.g., packed YUV formats) */
1494 if ((desc->BindFlags & D3D11_BIND_RENDER_TARGET) == 0)
1497 dmem = GST_D3D11_MEMORY_CAST (mem);
1498 switch (desc->Format) {
1499 case DXGI_FORMAT_NV12:
1500 case DXGI_FORMAT_P010:
1501 case DXGI_FORMAT_P016:
1502 /* Y component will be zero already */
1503 rtv = gst_d3d11_memory_get_render_target_view (dmem, 1);
1504 clear_func = (GstD3D11ClearRTVFunc) clear_rtv_chroma;
1506 case DXGI_FORMAT_AYUV:
1507 rtv = gst_d3d11_memory_get_render_target_view (dmem, 0);
1508 clear_func = (GstD3D11ClearRTVFunc) clear_rtv_vuya;
1517 context_handle = gst_d3d11_device_get_device_context_handle (device);
1518 GstD3D11DeviceLockGuard lk (device);
1519 clear_func (context_handle, rtv);
1525 * gst_d3d11_allocator_alloc:
1526 * @allocator: (transfer none) (allow-none): a #GstD3D11Allocator
1527 * @device: (transfer none): a #GstD3D11Device
1528 * @desc: a D3D11_TEXTURE2D_DESC struct
1530 * Returns: (transfer full) (nullable): a newly allocated #GstD3D11Memory with given parameters.
1535 gst_d3d11_allocator_alloc (GstD3D11Allocator * allocator,
1536 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc)
1541 gst_d3d11_memory_init_once ();
1542 allocator = _d3d11_memory_allocator;
1545 g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), NULL);
1546 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), NULL);
1547 g_return_val_if_fail (desc != NULL, NULL);
1549 mem = gst_d3d11_allocator_alloc_internal (allocator, device, desc, nullptr);
1553 if (!gst_d3d11_memory_update_size (mem)) {
1554 GST_ERROR_OBJECT (allocator, "Failed to calculate size");
1555 gst_memory_unref (mem);
1563 * gst_d3d11_allocator_alloc_buffer:
1564 * @allocator: (transfer none) (allow-none): a #GstD3D11Allocator
1565 * @device: (transfer none): a #GstD3D11Device
1566 * @desc: a D3D11_BUFFER_DESC struct
1568 * Returns: (transfer full) (nullable): a newly allocated #GstD3D11Memory with given parameters.
1573 gst_d3d11_allocator_alloc_buffer (GstD3D11Allocator * allocator,
1574 GstD3D11Device * device, const D3D11_BUFFER_DESC * desc)
1576 GstD3D11Memory *mem;
1577 ID3D11Buffer *buffer;
1578 ID3D11Device *device_handle;
1582 gst_d3d11_memory_init_once ();
1583 allocator = _d3d11_memory_allocator;
1586 g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), nullptr);
1587 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), nullptr);
1588 g_return_val_if_fail (desc != nullptr, nullptr);
1590 if (desc->Usage != D3D11_USAGE_STAGING) {
1591 GST_FIXME_OBJECT (allocator, "Non staging buffer is not supported");
1595 device_handle = gst_d3d11_device_get_device_handle (device);
1597 hr = device_handle->CreateBuffer (desc, nullptr, &buffer);
1598 if (!gst_d3d11_result (hr, device)) {
1599 GST_ERROR_OBJECT (allocator, "Couldn't create buffer");
1603 mem = g_new0 (GstD3D11Memory, 1);
1604 mem->priv = g_new0 (GstD3D11MemoryPrivate, 1);
1606 gst_memory_init (GST_MEMORY_CAST (mem),
1607 (GstMemoryFlags) 0, GST_ALLOCATOR_CAST (allocator), nullptr, 0, 0, 0, 0);
1608 mem->priv->buffer = buffer;
1609 mem->priv->buffer_desc = *desc;
1610 mem->priv->native_type = GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER;
1611 mem->device = (GstD3D11Device *) gst_object_ref (device);
1613 GST_MEMORY_CAST (mem)->maxsize = GST_MEMORY_CAST (mem)->size =
1616 return GST_MEMORY_CAST (mem);
1620 * gst_d3d11_allocator_alloc_wrapped:
1621 * @allocator: (transfer none) (allow-none): a #GstD3D11Allocator
1622 * @device: (transfer none): a #GstD3D11Device
1623 * @texture: a ID3D11Texture2D
1624 * @size: CPU accessible memory size
1625 * @user_data: (allow-none): user data
1626 * @notify: (allow-none): called with @user_data when the memory is freed
1628 * Allocates memory object with @texture. The refcount of @texture
1629 * will be increased by one.
1631 * Caller should set valid CPU acessible memory value to @size
1632 * (which is typically calculated by using staging texture and Map/Unmap)
1633 * or zero is allowed. In that case, allocator will create a temporary staging
1634 * texture to get the size and the temporary staging texture will be released.
1636 * Caller must not be confused that @size is CPU accessible size, not raw
1639 * Returns: (transfer full) (nullable): a newly allocated #GstD3D11Memory with given @texture
1640 * if successful, or %NULL if @texture is not a valid handle or configuration
1646 gst_d3d11_allocator_alloc_wrapped (GstD3D11Allocator * allocator,
1647 GstD3D11Device * device, ID3D11Texture2D * texture, gsize size,
1648 gpointer user_data, GDestroyNotify notify)
1651 GstD3D11Memory *dmem;
1652 D3D11_TEXTURE2D_DESC desc = { 0, };
1653 ID3D11Texture2D *tex = nullptr;
1657 gst_d3d11_memory_init_once ();
1658 allocator = _d3d11_memory_allocator;
1661 g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), nullptr);
1662 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), nullptr);
1663 g_return_val_if_fail (texture != nullptr, nullptr);
1665 hr = texture->QueryInterface (IID_PPV_ARGS (&tex));
1667 GST_WARNING_OBJECT (allocator, "Not a valid texture handle");
1671 tex->GetDesc (&desc);
1672 mem = gst_d3d11_allocator_alloc_internal (allocator, device, &desc, tex);
1678 if (!gst_d3d11_memory_update_size (mem)) {
1679 GST_ERROR_OBJECT (allocator, "Failed to calculate size");
1680 gst_memory_unref (mem);
1684 mem->maxsize = mem->size = size;
1687 dmem = GST_D3D11_MEMORY_CAST (mem);
1689 dmem->priv->user_data = user_data;
1690 dmem->priv->notify = notify;
1696 * gst_d3d11_allocator_set_active:
1697 * @allocator: a #GstD3D11Allocator
1698 * @active: the new active state
1700 * Controls the active state of @allocator. Default #GstD3D11Allocator is
1701 * stateless and therefore active state is ignored, but subclass implementation
1702 * (e.g., #GstD3D11PoolAllocator) will require explicit active state control
1703 * for its internal resource management.
1705 * This method is conceptually identical to gst_buffer_pool_set_active method.
1707 * Returns: %TRUE if active state of @allocator was successfully updated.
1712 gst_d3d11_allocator_set_active (GstD3D11Allocator * allocator, gboolean active)
1714 GstD3D11AllocatorClass *klass;
1716 g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), FALSE);
1718 klass = GST_D3D11_ALLOCATOR_GET_CLASS (allocator);
1719 if (klass->set_active)
1720 return klass->set_active (allocator, active);
1725 /* GstD3D11PoolAllocator */
1726 #define GST_D3D11_POOL_ALLOCATOR_GET_LOCK(alloc) \
1727 (&(GST_D3D11_POOL_ALLOCATOR_CAST(alloc)->priv->lock))
1728 #define GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING(alloc) (g_atomic_int_get (&alloc->priv->flushing))
1730 struct _GstD3D11PoolAllocatorPrivate
1732 /* parent texture when array typed memory is used */
1733 ID3D11Texture2D *texture;
1734 D3D11_TEXTURE2D_DESC desc;
1736 /* All below member variables are analogous to that of GstBufferPool */
1737 GstAtomicQueue *queue;
1740 /* This lock will protect all below variables apart from atomic ones
1741 * (identical to GstBufferPool::priv::rec_lock) */
1742 CRITICAL_SECTION lock;
1752 /* Calculated memory size, based on Direct3D11 staging texture map.
1753 * Note that, we cannot know the actually staging texture memory size prior
1754 * to map the staging texture because driver will likely require padding */
1758 static void gst_d3d11_pool_allocator_finalize (GObject * object);
1761 gst_d3d11_pool_allocator_set_active (GstD3D11Allocator * allocator,
1764 static gboolean gst_d3d11_pool_allocator_start (GstD3D11PoolAllocator * self);
1765 static gboolean gst_d3d11_pool_allocator_stop (GstD3D11PoolAllocator * self);
1766 static gboolean gst_d3d11_memory_release (GstMiniObject * mini_object);
1768 #define gst_d3d11_pool_allocator_parent_class pool_alloc_parent_class
1769 G_DEFINE_TYPE_WITH_PRIVATE (GstD3D11PoolAllocator,
1770 gst_d3d11_pool_allocator, GST_TYPE_D3D11_ALLOCATOR);
1773 gst_d3d11_pool_allocator_class_init (GstD3D11PoolAllocatorClass * klass)
1775 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1776 GstD3D11AllocatorClass *d3d11alloc_class = GST_D3D11_ALLOCATOR_CLASS (klass);
1778 gobject_class->finalize = gst_d3d11_pool_allocator_finalize;
1780 d3d11alloc_class->set_active = gst_d3d11_pool_allocator_set_active;
1784 gst_d3d11_pool_allocator_init (GstD3D11PoolAllocator * allocator)
1786 GstD3D11PoolAllocatorPrivate *priv;
1788 priv = allocator->priv = (GstD3D11PoolAllocatorPrivate *)
1789 gst_d3d11_pool_allocator_get_instance_private (allocator);
1791 InitializeCriticalSection (&priv->lock);
1793 priv->poll = gst_poll_new_timer ();
1794 priv->queue = gst_atomic_queue_new (16);
1796 priv->active = FALSE;
1797 priv->started = FALSE;
1799 /* 1 control write for flushing - the flush token */
1800 gst_poll_write_control (priv->poll);
1801 /* 1 control write for marking that we are not waiting for poll - the wait token */
1802 gst_poll_write_control (priv->poll);
1806 gst_d3d11_pool_allocator_finalize (GObject * object)
1808 GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (object);
1809 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1811 GST_DEBUG_OBJECT (self, "Finalize");
1813 gst_d3d11_pool_allocator_stop (self);
1814 gst_atomic_queue_unref (priv->queue);
1815 gst_poll_free (priv->poll);
1816 DeleteCriticalSection (&priv->lock);
1818 GST_D3D11_CLEAR_COM (priv->texture);
1820 gst_clear_object (&self->device);
1822 G_OBJECT_CLASS (pool_alloc_parent_class)->finalize (object);
1826 gst_d3d11_pool_allocator_start (GstD3D11PoolAllocator * self)
1828 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1829 ID3D11Device *device_handle;
1837 if (priv->desc.ArraySize == 1) {
1838 priv->started = TRUE;
1842 device_handle = gst_d3d11_device_get_device_handle (self->device);
1844 if (!priv->texture) {
1845 hr = device_handle->CreateTexture2D (&priv->desc, NULL, &priv->texture);
1846 if (!gst_d3d11_result (hr, self->device)) {
1847 GST_ERROR_OBJECT (self, "Failed to allocate texture");
1852 /* Pre-allocate memory objects */
1853 for (i = 0; i < priv->desc.ArraySize; i++) {
1856 priv->texture->AddRef ();
1857 mem = gst_d3d11_allocator_alloc_wrapped_internal (_d3d11_memory_allocator,
1858 self->device, &priv->desc, priv->texture);
1861 if (!gst_d3d11_memory_update_size (mem)) {
1862 GST_ERROR_OBJECT (self, "Failed to calculate memory size");
1863 gst_memory_unref (mem);
1867 priv->mem_size = mem->size;
1869 mem->size = mem->maxsize = priv->mem_size;
1872 GST_D3D11_MEMORY_CAST (mem)->priv->subresource_index = i;
1874 g_atomic_int_add (&priv->cur_mems, 1);
1875 gst_atomic_queue_push (priv->queue, mem);
1876 gst_poll_write_control (priv->poll);
1879 priv->started = TRUE;
1885 gst_d3d11_pool_allocator_do_set_flushing (GstD3D11PoolAllocator * self,
1888 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1890 if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self) == flushing)
1894 g_atomic_int_set (&priv->flushing, 1);
1895 /* Write the flush token to wake up any waiters */
1896 gst_poll_write_control (priv->poll);
1898 while (!gst_poll_read_control (priv->poll)) {
1899 if (errno == EWOULDBLOCK) {
1900 /* This should not really happen unless flushing and unflushing
1901 * happens on different threads. Let's wait a bit to get back flush
1902 * token from the thread that was setting it to flushing */
1906 /* Critical error but GstPoll already complained */
1911 g_atomic_int_set (&priv->flushing, 0);
1916 gst_d3d11_pool_allocator_set_active (GstD3D11Allocator * allocator,
1919 GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (allocator);
1920 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1922 GST_LOG_OBJECT (self, "active %d", active);
1924 GstD3D11CSLockGuard lk (GST_D3D11_POOL_ALLOCATOR_GET_LOCK (self));
1925 /* just return if we are already in the right state */
1926 if (priv->active == active)
1930 if (!gst_d3d11_pool_allocator_start (self)) {
1931 GST_ERROR_OBJECT (self, "start failed");
1935 /* flush_stop may release memory objects, setting to active to avoid running
1936 * do_stop while activating the pool */
1937 priv->active = TRUE;
1939 gst_d3d11_pool_allocator_do_set_flushing (self, FALSE);
1943 /* set to flushing first */
1944 gst_d3d11_pool_allocator_do_set_flushing (self, TRUE);
1946 /* when all memory objects are in the pool, free them. Else they will be
1947 * freed when they are released */
1948 outstanding = g_atomic_int_get (&priv->outstanding);
1949 GST_LOG_OBJECT (self, "outstanding memories %d, (in queue %d)",
1950 outstanding, gst_atomic_queue_length (priv->queue));
1951 if (outstanding == 0) {
1952 if (!gst_d3d11_pool_allocator_stop (self)) {
1953 GST_ERROR_OBJECT (self, "stop failed");
1958 priv->active = FALSE;
1965 gst_d3d11_pool_allocator_free_memory (GstD3D11PoolAllocator * self,
1968 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1970 g_atomic_int_add (&priv->cur_mems, -1);
1971 GST_LOG_OBJECT (self, "freeing memory %p (%u left)", mem, priv->cur_mems);
1973 GST_MINI_OBJECT_CAST (mem)->dispose = NULL;
1974 gst_memory_unref (mem);
1977 /* must be called with the lock */
1979 gst_d3d11_pool_allocator_clear_queue (GstD3D11PoolAllocator * self)
1981 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1984 GST_LOG_OBJECT (self, "Clearing queue");
1986 /* clear the pool */
1987 while ((memory = (GstMemory *) gst_atomic_queue_pop (priv->queue))) {
1988 while (!gst_poll_read_control (priv->poll)) {
1989 if (errno == EWOULDBLOCK) {
1990 /* We put the memory into the queue but did not finish writing control
1991 * yet, let's wait a bit and retry */
1995 /* Critical error but GstPoll already complained */
1999 gst_d3d11_pool_allocator_free_memory (self, memory);
2002 GST_LOG_OBJECT (self, "Clear done");
2004 return priv->cur_mems == 0;
2007 /* must be called with the lock */
2009 gst_d3d11_pool_allocator_stop (GstD3D11PoolAllocator * self)
2011 GstD3D11PoolAllocatorPrivate *priv = self->priv;
2013 GST_DEBUG_OBJECT (self, "Stop");
2015 if (priv->started) {
2016 if (!gst_d3d11_pool_allocator_clear_queue (self))
2019 priv->started = FALSE;
2021 GST_DEBUG_OBJECT (self, "Wasn't started");
2028 dec_outstanding (GstD3D11PoolAllocator * self)
2030 if (g_atomic_int_dec_and_test (&self->priv->outstanding)) {
2031 /* all memory objects are returned to the pool, see if we need to free them */
2032 if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)) {
2033 /* take the lock so that set_active is not run concurrently */
2034 GstD3D11CSLockGuard lk (GST_D3D11_POOL_ALLOCATOR_GET_LOCK (self));
2035 /* now that we have the lock, check if we have been de-activated with
2036 * outstanding buffers */
2037 if (!self->priv->active)
2038 gst_d3d11_pool_allocator_stop (self);
2044 gst_d3d11_pool_allocator_release_memory (GstD3D11PoolAllocator * self,
2047 GST_LOG_OBJECT (self, "Released memory %p", mem);
2049 GST_MINI_OBJECT_CAST (mem)->dispose = NULL;
2050 mem->allocator = (GstAllocator *) gst_object_ref (_d3d11_memory_allocator);
2052 /* keep it around in our queue */
2053 gst_atomic_queue_push (self->priv->queue, mem);
2054 gst_poll_write_control (self->priv->poll);
2055 dec_outstanding (self);
2057 gst_object_unref (self);
2061 gst_d3d11_memory_release (GstMiniObject * mini_object)
2063 GstMemory *mem = GST_MEMORY_CAST (mini_object);
2064 GstD3D11PoolAllocator *alloc;
2066 g_assert (mem->allocator != NULL);
2068 if (!GST_IS_D3D11_POOL_ALLOCATOR (mem->allocator)) {
2069 GST_LOG_OBJECT (mem->allocator, "Not our memory, free");
2073 alloc = GST_D3D11_POOL_ALLOCATOR (mem->allocator);
2074 /* if flushing, free this memory */
2075 if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (alloc)) {
2076 GST_LOG_OBJECT (alloc, "allocator is flushing, free %p", mem);
2080 /* return the memory to the allocator */
2081 gst_memory_ref (mem);
2082 gst_d3d11_pool_allocator_release_memory (alloc, mem);
2087 static GstFlowReturn
2088 gst_d3d11_pool_allocator_alloc (GstD3D11PoolAllocator * self, GstMemory ** mem)
2090 GstD3D11PoolAllocatorPrivate *priv = self->priv;
2093 /* we allcates texture array during start */
2094 if (priv->desc.ArraySize > 1)
2095 return GST_FLOW_EOS;
2097 /* increment the allocation counter */
2098 g_atomic_int_add (&priv->cur_mems, 1);
2099 new_mem = gst_d3d11_allocator_alloc_internal (_d3d11_memory_allocator,
2100 self->device, &priv->desc, nullptr);
2102 GST_ERROR_OBJECT (self, "Failed to allocate new memory");
2103 g_atomic_int_add (&priv->cur_mems, -1);
2104 return GST_FLOW_ERROR;
2107 if (!priv->mem_size) {
2108 if (!gst_d3d11_memory_update_size (new_mem)) {
2109 GST_ERROR_OBJECT (self, "Failed to calculate size");
2110 gst_memory_unref (new_mem);
2111 g_atomic_int_add (&priv->cur_mems, -1);
2113 return GST_FLOW_ERROR;
2116 priv->mem_size = new_mem->size;
2119 new_mem->size = new_mem->maxsize = priv->mem_size;
2126 static GstFlowReturn
2127 gst_d3d11_pool_allocator_acquire_memory_internal (GstD3D11PoolAllocator * self,
2128 GstMemory ** memory)
2130 GstFlowReturn result;
2131 GstD3D11PoolAllocatorPrivate *priv = self->priv;
2134 if (G_UNLIKELY (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)))
2137 /* try to get a memory from the queue */
2138 *memory = (GstMemory *) gst_atomic_queue_pop (priv->queue);
2139 if (G_LIKELY (*memory)) {
2140 while (!gst_poll_read_control (priv->poll)) {
2141 if (errno == EWOULDBLOCK) {
2142 /* We put the memory into the queue but did not finish writing control
2143 * yet, let's wait a bit and retry */
2147 /* Critical error but GstPoll already complained */
2151 result = GST_FLOW_OK;
2152 GST_LOG_OBJECT (self, "acquired memory %p", *memory);
2156 /* no memory, try to allocate some more */
2157 GST_LOG_OBJECT (self, "no memory, trying to allocate");
2158 result = gst_d3d11_pool_allocator_alloc (self, memory);
2159 if (G_LIKELY (result == GST_FLOW_OK))
2160 /* we have a memory, return it */
2163 if (G_UNLIKELY (result != GST_FLOW_EOS))
2164 /* something went wrong, return error */
2167 /* now we release the control socket, we wait for a memory release or
2169 if (!gst_poll_read_control (priv->poll)) {
2170 if (errno == EWOULDBLOCK) {
2171 /* This means that we have two threads trying to allocate memory
2172 * already, and the other one already got the wait token. This
2173 * means that we only have to wait for the poll now and not write the
2174 * token afterwards: we will be woken up once the other thread is
2175 * woken up and that one will write the wait token it removed */
2176 GST_LOG_OBJECT (self, "waiting for free memory or flushing");
2177 gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
2179 /* This is a critical error, GstPoll already gave a warning */
2180 result = GST_FLOW_ERROR;
2184 /* We're the first thread waiting, we got the wait token and have to
2185 * write it again later
2187 * We're a second thread and just consumed the flush token and block all
2188 * other threads, in which case we must not wait and give it back
2190 if (!GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)) {
2191 GST_LOG_OBJECT (self, "waiting for free memory or flushing");
2192 gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
2194 gst_poll_write_control (priv->poll);
2203 GST_DEBUG_OBJECT (self, "we are flushing");
2204 return GST_FLOW_FLUSHING;
2209 * gst_d3d11_pool_allocator_new:
2210 * @device: a #GstD3D11Device
2211 * @desc: a D3D11_TEXTURE2D_DESC for texture allocation
2213 * Creates a new #GstD3D11PoolAllocator instance.
2215 * Returns: (transfer full): a new #GstD3D11PoolAllocator instance
2219 GstD3D11PoolAllocator *
2220 gst_d3d11_pool_allocator_new (GstD3D11Device * device,
2221 const D3D11_TEXTURE2D_DESC * desc)
2223 GstD3D11PoolAllocator *self;
2225 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), NULL);
2226 g_return_val_if_fail (desc != NULL, NULL);
2228 gst_d3d11_memory_init_once ();
2230 self = (GstD3D11PoolAllocator *)
2231 g_object_new (GST_TYPE_D3D11_POOL_ALLOCATOR, NULL);
2232 gst_object_ref_sink (self);
2234 self->device = (GstD3D11Device *) gst_object_ref (device);
2235 self->priv->desc = *desc;
2241 * gst_d3d11_pool_allocator_acquire_memory:
2242 * @allocator: a #GstD3D11PoolAllocator
2243 * @memory: (out): a #GstMemory
2245 * Acquires a #GstMemory from @allocator. @memory should point to a memory
2246 * location that can hold a pointer to the new #GstMemory.
2248 * Returns: a #GstFlowReturn such as %GST_FLOW_FLUSHING when the allocator is
2252 gst_d3d11_pool_allocator_acquire_memory (GstD3D11PoolAllocator * allocator,
2253 GstMemory ** memory)
2255 GstD3D11PoolAllocatorPrivate *priv;
2256 GstFlowReturn result;
2258 g_return_val_if_fail (GST_IS_D3D11_POOL_ALLOCATOR (allocator),
2260 g_return_val_if_fail (memory != NULL, GST_FLOW_ERROR);
2262 priv = allocator->priv;
2264 /* assume we'll have one more outstanding buffer we need to do that so
2265 * that concurrent set_active doesn't clear the buffers */
2266 g_atomic_int_inc (&priv->outstanding);
2267 result = gst_d3d11_pool_allocator_acquire_memory_internal (allocator, memory);
2269 if (result == GST_FLOW_OK) {
2270 GstMemory *mem = *memory;
2271 /* Replace default allocator with ours */
2272 gst_object_unref (mem->allocator);
2273 mem->allocator = (GstAllocator *) gst_object_ref (allocator);
2274 GST_MINI_OBJECT_CAST (mem)->dispose = gst_d3d11_memory_release;
2276 dec_outstanding (allocator);
2283 * gst_d3d11_pool_allocator_get_pool_size:
2284 * @allocator: a #GstD3D11PoolAllocator
2285 * @max_size: (out) (optional): the max size of pool
2286 * @outstanding_size: (out) (optional): the number of outstanding memory
2288 * Returns: %TRUE if the size of memory pool is known
2293 gst_d3d11_pool_allocator_get_pool_size (GstD3D11PoolAllocator * allocator,
2294 guint * max_size, guint * outstanding_size)
2296 GstD3D11PoolAllocatorPrivate *priv;
2298 g_return_val_if_fail (GST_IS_D3D11_POOL_ALLOCATOR (allocator), FALSE);
2300 priv = allocator->priv;
2303 if (priv->desc.ArraySize > 1) {
2304 *max_size = priv->desc.ArraySize;
2306 /* For non-texture-array memory, we don't have any limit yet */
2311 if (outstanding_size)
2312 *outstanding_size = g_atomic_int_get (&priv->outstanding);