2 * Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
3 * Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
26 #include "gstd3d11memory.h"
27 #include "gstd3d11device.h"
28 #include "gstd3d11utils.h"
29 #include "gstd3d11_private.h"
31 GST_DEBUG_CATEGORY_STATIC (gst_d3d11_allocator_debug);
32 #define GST_CAT_DEFAULT gst_d3d11_allocator_debug
34 static GstAllocator *_d3d11_memory_allocator;
36 /* GstD3D11AllocationParams */
37 static void gst_d3d11_allocation_params_init (GType type);
38 G_DEFINE_BOXED_TYPE_WITH_CODE (GstD3D11AllocationParams,
39 gst_d3d11_allocation_params,
40 (GBoxedCopyFunc) gst_d3d11_allocation_params_copy,
41 (GBoxedFreeFunc) gst_d3d11_allocation_params_free,
42 gst_d3d11_allocation_params_init (g_define_type_id));
45 * gst_d3d11_allocation_params_new:
46 * @device: a #GstD3D11Device
47 * @info: a #GstVideoInfo
48 * @flags: a #GstD3D11AllocationFlags
49 * @bind_flags: D3D11_BIND_FLAG value used for creating texture
50 * @misc_flags: D3D11_RESOURCE_MISC_FLAG value used for creating texture
52 * Create #GstD3D11AllocationParams object which is used by #GstD3D11BufferPool
53 * and #GstD3D11Allocator in order to allocate new ID3D11Texture2D
54 * object with given configuration
56 * Returns: a #GstD3D11AllocationParams or %NULL if @info is not supported
60 GstD3D11AllocationParams *
61 gst_d3d11_allocation_params_new (GstD3D11Device * device,
62 const GstVideoInfo * info, GstD3D11AllocationFlags flags, guint bind_flags,
65 GstD3D11AllocationParams *ret;
66 GstD3D11Format d3d11_format;
69 g_return_val_if_fail (info != NULL, NULL);
71 if (!gst_d3d11_device_get_format (device, GST_VIDEO_INFO_FORMAT (info),
73 GST_WARNING ("Couldn't get d3d11 format");
77 ret = g_new0 (GstD3D11AllocationParams, 1);
80 ret->aligned_info = *info;
81 ret->d3d11_format = d3d11_format;
84 * https://docs.microsoft.com/en-us/windows/win32/api/d3d11/ne-d3d11-d3d11_usage
86 * +----------------------------------------------------------+
87 * | Resource Usage | Default | Dynamic | Immutable | Staging |
88 * +----------------+---------+---------+-----------+---------+
89 * | GPU-Read | Yes | Yes | Yes | Yes |
90 * | GPU-Write | Yes | | | Yes |
91 * | CPU-Read | | | | Yes |
92 * | CPU-Write | | Yes | | Yes |
93 * +----------------------------------------------------------+
96 /* If corresponding dxgi format is undefined, use resource format instead */
97 if (d3d11_format.dxgi_format == DXGI_FORMAT_UNKNOWN) {
98 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (info); i++) {
99 g_assert (d3d11_format.resource_format[i] != DXGI_FORMAT_UNKNOWN);
101 ret->desc[i].Width = GST_VIDEO_INFO_COMP_WIDTH (info, i);
102 ret->desc[i].Height = GST_VIDEO_INFO_COMP_HEIGHT (info, i);
103 ret->desc[i].MipLevels = 1;
104 ret->desc[i].ArraySize = 1;
105 ret->desc[i].Format = d3d11_format.resource_format[i];
106 ret->desc[i].SampleDesc.Count = 1;
107 ret->desc[i].SampleDesc.Quality = 0;
108 ret->desc[i].Usage = D3D11_USAGE_DEFAULT;
109 ret->desc[i].BindFlags = bind_flags;
110 ret->desc[i].MiscFlags = misc_flags;
113 ret->desc[0].Width = GST_VIDEO_INFO_WIDTH (info);
114 ret->desc[0].Height = GST_VIDEO_INFO_HEIGHT (info);
115 ret->desc[0].MipLevels = 1;
116 ret->desc[0].ArraySize = 1;
117 ret->desc[0].Format = d3d11_format.dxgi_format;
118 ret->desc[0].SampleDesc.Count = 1;
119 ret->desc[0].SampleDesc.Quality = 0;
120 ret->desc[0].Usage = D3D11_USAGE_DEFAULT;
121 ret->desc[0].BindFlags = bind_flags;
122 ret->desc[0].MiscFlags = misc_flags;
131 * gst_d3d11_allocation_params_alignment:
132 * @params: a #GstD3D11AllocationParams
133 * @align: a #GstVideoAlignment
135 * Adjust Width and Height fields of D3D11_TEXTURE2D_DESC with given
138 * Returns: %TRUE if alignment could be applied
143 gst_d3d11_allocation_params_alignment (GstD3D11AllocationParams * params,
144 const GstVideoAlignment * align)
147 guint padding_width, padding_height;
149 GstVideoInfo new_info;
151 g_return_val_if_fail (params != NULL, FALSE);
152 g_return_val_if_fail (align != NULL, FALSE);
154 /* d3d11 does not support stride align. Consider padding only */
155 padding_width = align->padding_left + align->padding_right;
156 padding_height = align->padding_top + align->padding_bottom;
158 info = ¶ms->info;
160 if (!gst_video_info_set_format (&new_info, GST_VIDEO_INFO_FORMAT (info),
161 GST_VIDEO_INFO_WIDTH (info) + padding_width,
162 GST_VIDEO_INFO_HEIGHT (info) + padding_height)) {
163 GST_WARNING ("Set format fail");
167 params->aligned_info = new_info;
169 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (info); i++) {
170 params->desc[i].Width = GST_VIDEO_INFO_COMP_WIDTH (&new_info, i);
171 params->desc[i].Height = GST_VIDEO_INFO_COMP_HEIGHT (&new_info, i);
178 * gst_d3d11_allocation_params_copy:
179 * @src: a #GstD3D11AllocationParams
181 * Returns: a copy of @src
185 GstD3D11AllocationParams *
186 gst_d3d11_allocation_params_copy (GstD3D11AllocationParams * src)
188 GstD3D11AllocationParams *dst;
190 g_return_val_if_fail (src != NULL, NULL);
192 dst = g_new0 (GstD3D11AllocationParams, 1);
193 memcpy (dst, src, sizeof (GstD3D11AllocationParams));
199 * gst_d3d11_allocation_params_free:
200 * @params: a #GstD3D11AllocationParams
207 gst_d3d11_allocation_params_free (GstD3D11AllocationParams * params)
213 gst_d3d11_allocation_params_compare (const GstD3D11AllocationParams * p1,
214 const GstD3D11AllocationParams * p2)
216 g_return_val_if_fail (p1 != NULL, -1);
217 g_return_val_if_fail (p2 != NULL, -1);
226 gst_d3d11_allocation_params_init (GType type)
228 static GstValueTable table = {
229 0, (GstValueCompareFunc) gst_d3d11_allocation_params_compare,
234 gst_value_register (&table);
238 #define GST_D3D11_MEMORY_GET_LOCK(m) (&(GST_D3D11_MEMORY_CAST(m)->priv->lock))
239 #define GST_D3D11_MEMORY_LOCK(m) G_STMT_START { \
240 GST_TRACE("Locking %p from thread %p", (m), g_thread_self()); \
241 g_mutex_lock(GST_D3D11_MEMORY_GET_LOCK(m)); \
242 GST_TRACE("Locked %p from thread %p", (m), g_thread_self()); \
245 #define GST_D3D11_MEMORY_UNLOCK(m) G_STMT_START { \
246 GST_TRACE("Unlocking %p from thread %p", (m), g_thread_self()); \
247 g_mutex_unlock(GST_D3D11_MEMORY_GET_LOCK(m)); \
250 struct _GstD3D11MemoryPrivate
252 ID3D11Texture2D *texture;
253 ID3D11Buffer *buffer;
255 ID3D11Resource *staging;
257 D3D11_TEXTURE2D_DESC desc;
258 D3D11_BUFFER_DESC buffer_desc;
260 guint subresource_index;
262 ID3D11ShaderResourceView *shader_resource_view[GST_VIDEO_MAX_PLANES];
263 guint num_shader_resource_views;
265 ID3D11RenderTargetView *render_target_view[GST_VIDEO_MAX_PLANES];
266 guint num_render_target_views;
268 ID3D11VideoDecoderOutputView *decoder_output_view;
269 ID3D11VideoDecoder *decoder_handle;
271 ID3D11VideoProcessorInputView *processor_input_view;
272 ID3D11VideoProcessorOutputView *processor_output_view;
274 D3D11_MAPPED_SUBRESOURCE map;
276 GstD3D11MemoryNativeType native_type;
282 static inline D3D11_MAP
283 gst_d3d11_map_flags_to_d3d11 (GstMapFlags flags)
285 if ((flags & GST_MAP_READWRITE) == GST_MAP_READWRITE)
286 return D3D11_MAP_READ_WRITE;
287 else if ((flags & GST_MAP_WRITE) == GST_MAP_WRITE)
288 return D3D11_MAP_WRITE;
289 else if ((flags & GST_MAP_READ) == GST_MAP_READ)
290 return D3D11_MAP_READ;
292 g_assert_not_reached ();
294 return D3D11_MAP_READ;
297 static ID3D11Texture2D *
298 gst_d3d11_allocate_staging_texture (GstD3D11Device * device,
299 const D3D11_TEXTURE2D_DESC * ref)
301 D3D11_TEXTURE2D_DESC desc = { 0, };
302 ID3D11Texture2D *texture = NULL;
303 ID3D11Device *device_handle = gst_d3d11_device_get_device_handle (device);
306 desc.Width = ref->Width;
307 desc.Height = ref->Height;
309 desc.Format = ref->Format;
310 desc.SampleDesc.Count = 1;
312 desc.Usage = D3D11_USAGE_STAGING;
313 desc.CPUAccessFlags = (D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE);
315 hr = device_handle->CreateTexture2D (&desc, NULL, &texture);
316 if (!gst_d3d11_result (hr, device)) {
317 GST_ERROR_OBJECT (device, "Failed to create texture");
324 /* Must be called with d3d11 device lock */
326 gst_d3d11_memory_map_cpu_access (GstD3D11Memory * dmem, D3D11_MAP map_type)
328 GstD3D11MemoryPrivate *priv = dmem->priv;
330 ID3D11DeviceContext *device_context =
331 gst_d3d11_device_get_device_context_handle (dmem->device);
333 hr = device_context->Map (priv->staging, 0, map_type, 0, &priv->map);
335 if (!gst_d3d11_result (hr, dmem->device)) {
336 GST_ERROR_OBJECT (GST_MEMORY_CAST (dmem)->allocator,
337 "Failed to map staging texture (0x%x)", (guint) hr);
344 /* Must be called with d3d11 device lock */
346 gst_d3d11_memory_upload (GstD3D11Memory * dmem)
348 GstD3D11MemoryPrivate *priv = dmem->priv;
349 ID3D11DeviceContext *device_context;
351 if (!priv->staging ||
352 !GST_MEMORY_FLAG_IS_SET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD))
355 device_context = gst_d3d11_device_get_device_context_handle (dmem->device);
356 device_context->CopySubresourceRegion (priv->texture, priv->subresource_index,
357 0, 0, 0, priv->staging, 0, NULL);
360 /* Must be called with d3d11 device lock */
362 gst_d3d11_memory_download (GstD3D11Memory * dmem)
364 GstD3D11MemoryPrivate *priv = dmem->priv;
365 ID3D11DeviceContext *device_context;
367 if (!priv->staging ||
368 !GST_MEMORY_FLAG_IS_SET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD))
371 device_context = gst_d3d11_device_get_device_context_handle (dmem->device);
372 device_context->CopySubresourceRegion (priv->staging, 0, 0, 0, 0,
373 priv->texture, priv->subresource_index, NULL);
377 gst_d3d11_memory_map_full (GstMemory * mem, GstMapInfo * info, gsize maxsize)
379 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
380 GstD3D11MemoryPrivate *priv = dmem->priv;
381 GstMapFlags flags = info->flags;
384 gst_d3d11_device_lock (dmem->device);
385 GST_D3D11_MEMORY_LOCK (dmem);
387 memset (info->user_data, 0, sizeof (info->user_data));
388 info->user_data[0] = GUINT_TO_POINTER (dmem->priv->subresource_index);
390 if ((flags & GST_MAP_D3D11) == GST_MAP_D3D11) {
391 if (priv->native_type == GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER) {
392 /* FIXME: handle non-staging buffer */
393 g_assert (priv->buffer != nullptr);
396 gst_d3d11_memory_upload (dmem);
397 GST_MEMORY_FLAG_UNSET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD);
399 if ((flags & GST_MAP_WRITE) == GST_MAP_WRITE)
400 GST_MINI_OBJECT_FLAG_SET (dmem,
401 GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
403 g_assert (priv->texture != NULL);
409 if (priv->cpu_map_count == 0) {
412 /* FIXME: handle non-staging buffer */
413 if (priv->native_type == GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D) {
414 /* Allocate staging texture for CPU access */
415 if (!priv->staging) {
416 priv->staging = gst_d3d11_allocate_staging_texture (dmem->device,
418 if (!priv->staging) {
419 GST_ERROR_OBJECT (mem->allocator, "Couldn't create staging texture");
423 /* first memory, always need download to staging */
424 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
427 gst_d3d11_memory_download (dmem);
430 map_type = gst_d3d11_map_flags_to_d3d11 (flags);
431 if (!gst_d3d11_memory_map_cpu_access (dmem, map_type)) {
432 GST_ERROR_OBJECT (mem->allocator, "Couldn't map staging texture");
437 if ((flags & GST_MAP_WRITE) == GST_MAP_WRITE) {
438 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD);
441 GST_MEMORY_FLAG_UNSET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
443 priv->cpu_map_count++;
444 ret = dmem->priv->map.pData;
447 GST_D3D11_MEMORY_UNLOCK (dmem);
448 gst_d3d11_device_unlock (dmem->device);
453 /* Must be called with d3d11 device lock */
455 gst_d3d11_memory_unmap_cpu_access (GstD3D11Memory * dmem)
457 GstD3D11MemoryPrivate *priv = dmem->priv;
458 ID3D11DeviceContext *device_context =
459 gst_d3d11_device_get_device_context_handle (dmem->device);
461 device_context->Unmap (priv->staging, 0);
465 gst_d3d11_memory_unmap_full (GstMemory * mem, GstMapInfo * info)
467 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
468 GstD3D11MemoryPrivate *priv = dmem->priv;
470 gst_d3d11_device_lock (dmem->device);
471 GST_D3D11_MEMORY_LOCK (dmem);
473 if ((info->flags & GST_MAP_D3D11) == GST_MAP_D3D11) {
474 if ((info->flags & GST_MAP_WRITE) == GST_MAP_WRITE)
475 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
480 if ((info->flags & GST_MAP_WRITE) == GST_MAP_WRITE)
481 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD);
483 priv->cpu_map_count--;
484 if (priv->cpu_map_count > 0)
487 gst_d3d11_memory_unmap_cpu_access (dmem);
490 GST_D3D11_MEMORY_UNLOCK (dmem);
491 gst_d3d11_device_unlock (dmem->device);
495 gst_d3d11_memory_share (GstMemory * mem, gssize offset, gssize size)
502 gst_d3d11_memory_update_size (GstMemory * mem)
504 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
505 GstD3D11MemoryPrivate *priv = dmem->priv;
506 gsize offset[GST_VIDEO_MAX_PLANES];
507 gint stride[GST_VIDEO_MAX_PLANES];
509 D3D11_TEXTURE2D_DESC *desc = &priv->desc;
510 gboolean ret = FALSE;
512 if (!priv->staging) {
513 priv->staging = gst_d3d11_allocate_staging_texture (dmem->device,
515 if (!priv->staging) {
516 GST_ERROR_OBJECT (mem->allocator, "Couldn't create staging texture");
520 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
523 gst_d3d11_device_lock (dmem->device);
524 if (!gst_d3d11_memory_map_cpu_access (dmem, D3D11_MAP_READ_WRITE)) {
525 GST_ERROR_OBJECT (mem->allocator, "Couldn't map staging texture");
529 gst_d3d11_memory_unmap_cpu_access (dmem);
531 if (!gst_d3d11_dxgi_format_get_size (desc->Format, desc->Width, desc->Height,
532 priv->map.RowPitch, offset, stride, &size)) {
533 GST_ERROR_OBJECT (mem->allocator, "Couldn't calculate memory size");
537 mem->maxsize = mem->size = size;
541 gst_d3d11_device_unlock (dmem->device);
546 * gst_is_d3d11_memory:
549 * Returns: whether @mem is a #GstD3D11Memory
554 gst_is_d3d11_memory (GstMemory * mem)
556 return mem != NULL && mem->allocator != NULL &&
557 (GST_IS_D3D11_ALLOCATOR (mem->allocator) ||
558 GST_IS_D3D11_POOL_ALLOCATOR (mem->allocator));
562 * gst_d3d11_memory_get_native_type:
563 * @mem: a #GstD3D11Memory
565 * Returns: a #GstD3D11MemoryNativeType
569 GstD3D11MemoryNativeType
570 gst_d3d11_memory_get_native_type (GstD3D11Memory * mem)
572 if (!gst_is_d3d11_memory (GST_MEMORY_CAST (mem)))
573 return GST_D3D11_MEMORY_NATIVE_TYPE_INVALID;
575 return mem->priv->native_type;
579 * gst_d3d11_memory_init_once:
581 * Initializes the Direct3D11 Texture allocator. It is safe to call
582 * this function multiple times. This must be called before any other
583 * GstD3D11Memory operation.
588 gst_d3d11_memory_init_once (void)
590 static gsize _init = 0;
592 if (g_once_init_enter (&_init)) {
594 GST_DEBUG_CATEGORY_INIT (gst_d3d11_allocator_debug, "d3d11allocator", 0,
595 "Direct3D11 Texture Allocator");
597 _d3d11_memory_allocator =
598 (GstAllocator *) g_object_new (GST_TYPE_D3D11_ALLOCATOR, NULL);
599 gst_object_ref_sink (_d3d11_memory_allocator);
601 gst_allocator_register (GST_D3D11_MEMORY_NAME, _d3d11_memory_allocator);
602 g_once_init_leave (&_init, 1);
607 * gst_d3d11_memory_get_resource_handle:
608 * @mem: a #GstD3D11Memory
610 * Returns: (transfer none): a ID3D11Resource handle. Caller must not release
616 gst_d3d11_memory_get_resource_handle (GstD3D11Memory * mem)
618 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
620 switch (mem->priv->native_type) {
621 case GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER:
622 return mem->priv->buffer;
623 case GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D:
624 return mem->priv->texture;
633 * gst_d3d11_memory_get_subresource_index:
634 * @mem: a #GstD3D11Memory
636 * Returns: subresource index corresponding to @mem.
641 gst_d3d11_memory_get_subresource_index (GstD3D11Memory * mem)
643 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), 0);
645 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
648 return mem->priv->subresource_index;
652 * gst_d3d11_memory_get_texture_desc:
653 * @mem: a #GstD3D11Memory
654 * @desc: (out): a D3D11_TEXTURE2D_DESC
656 * Fill @desc with D3D11_TEXTURE2D_DESC for ID3D11Texture2D
658 * Returns: %TRUE if successeed
663 gst_d3d11_memory_get_texture_desc (GstD3D11Memory * mem,
664 D3D11_TEXTURE2D_DESC * desc)
666 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), FALSE);
667 g_return_val_if_fail (desc != NULL, FALSE);
669 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
672 *desc = mem->priv->desc;
678 * gst_d3d11_memory_get_buffer_desc:
679 * @mem: a #GstD3D11Memory
680 * @desc: (out): a D3D11_BUFFER_DESC
682 * Fill @desc with D3D11_BUFFER_DESC for ID3D11Buffer
684 * Returns: %TRUE if successeed
689 gst_d3d11_memory_get_buffer_desc (GstD3D11Memory * mem,
690 D3D11_BUFFER_DESC * desc)
692 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), FALSE);
693 g_return_val_if_fail (desc != NULL, FALSE);
695 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER)
698 *desc = mem->priv->buffer_desc;
704 * gst_d3d11_memory_get_resource_stride:
705 * @mem: a #GstD3D11Memory
706 * @stride: (out): stride of resource
708 * Returns: %TRUE if successeed
713 gst_d3d11_memory_get_resource_stride (GstD3D11Memory * mem, guint * stride)
715 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), FALSE);
716 g_return_val_if_fail (stride != NULL, FALSE);
718 *stride = mem->priv->map.RowPitch;
724 create_shader_resource_views (GstD3D11Memory * mem)
726 GstD3D11MemoryPrivate *priv = mem->priv;
730 ID3D11Device *device_handle;
731 D3D11_SHADER_RESOURCE_VIEW_DESC resource_desc;
732 DXGI_FORMAT formats[GST_VIDEO_MAX_PLANES] = { DXGI_FORMAT_UNKNOWN, };
734 memset (&resource_desc, 0, sizeof (D3D11_SHADER_RESOURCE_VIEW_DESC));
736 device_handle = gst_d3d11_device_get_device_handle (mem->device);
738 num_views = gst_d3d11_dxgi_format_get_resource_format (priv->desc.Format,
741 GST_ERROR_OBJECT (GST_MEMORY_CAST (mem)->allocator,
742 "Unknown resource formats for DXGI format %s (%d)",
743 gst_d3d11_dxgi_format_to_string (priv->desc.Format), priv->desc.Format);
747 if ((priv->desc.BindFlags & D3D11_BIND_SHADER_RESOURCE) != 0) {
748 resource_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
749 resource_desc.Texture2D.MipLevels = 1;
751 for (i = 0; i < num_views; i++) {
752 resource_desc.Format = formats[i];
753 hr = device_handle->CreateShaderResourceView (priv->texture,
754 &resource_desc, &priv->shader_resource_view[i]);
756 if (!gst_d3d11_result (hr, mem->device)) {
757 GST_ERROR_OBJECT (GST_MEMORY_CAST (mem)->allocator,
758 "Failed to create resource DXGI format %s (%d) for plane %d"
759 " view (0x%x)", gst_d3d11_dxgi_format_to_string (formats[i]),
760 formats[i], i, (guint) hr);
765 priv->num_shader_resource_views = num_views;
773 for (i = 0; i < num_views; i++)
774 GST_D3D11_CLEAR_COM (priv->shader_resource_view[i]);
776 priv->num_shader_resource_views = 0;
782 gst_d3d11_memory_ensure_shader_resource_view (GstD3D11Memory * mem)
784 GstD3D11MemoryPrivate *priv = mem->priv;
785 gboolean ret = FALSE;
787 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
790 if (!(priv->desc.BindFlags & D3D11_BIND_SHADER_RESOURCE)) {
791 GST_LOG_OBJECT (GST_MEMORY_CAST (mem)->allocator,
792 "Need BindFlags, current flag 0x%x", priv->desc.BindFlags);
796 GST_D3D11_MEMORY_LOCK (mem);
797 if (priv->num_shader_resource_views) {
802 ret = create_shader_resource_views (mem);
805 GST_D3D11_MEMORY_UNLOCK (mem);
811 * gst_d3d11_memory_get_shader_resource_view_size:
812 * @mem: a #GstD3D11Memory
814 * Returns: the number of ID3D11ShaderResourceView that can be used
815 * for processing GPU operation with @mem
820 gst_d3d11_memory_get_shader_resource_view_size (GstD3D11Memory * mem)
822 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), 0);
824 if (!gst_d3d11_memory_ensure_shader_resource_view (mem))
827 return mem->priv->num_shader_resource_views;
831 * gst_d3d11_memory_get_shader_resource_view:
832 * @mem: a #GstD3D11Memory
833 * @index: the index of the ID3D11ShaderResourceView
835 * Returns: (transfer none) (nullable): a pointer to the
836 * ID3D11ShaderResourceView or %NULL if ID3D11ShaderResourceView is unavailable
841 ID3D11ShaderResourceView *
842 gst_d3d11_memory_get_shader_resource_view (GstD3D11Memory * mem, guint index)
844 GstD3D11MemoryPrivate *priv;
846 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
848 if (!gst_d3d11_memory_ensure_shader_resource_view (mem))
853 if (index >= priv->num_shader_resource_views) {
854 GST_ERROR ("Invalid SRV index %d", index);
858 return priv->shader_resource_view[index];
862 create_render_target_views (GstD3D11Memory * mem)
864 GstD3D11MemoryPrivate *priv = mem->priv;
868 ID3D11Device *device_handle;
869 D3D11_RENDER_TARGET_VIEW_DESC render_desc;
870 DXGI_FORMAT formats[GST_VIDEO_MAX_PLANES] = { DXGI_FORMAT_UNKNOWN, };
872 memset (&render_desc, 0, sizeof (D3D11_RENDER_TARGET_VIEW_DESC));
874 device_handle = gst_d3d11_device_get_device_handle (mem->device);
876 num_views = gst_d3d11_dxgi_format_get_resource_format (priv->desc.Format,
879 GST_ERROR_OBJECT (GST_MEMORY_CAST (mem)->allocator,
880 "Unknown resource formats for DXGI format %s (%d)",
881 gst_d3d11_dxgi_format_to_string (priv->desc.Format), priv->desc.Format);
885 if ((priv->desc.BindFlags & D3D11_BIND_RENDER_TARGET) != 0) {
886 render_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
887 render_desc.Texture2D.MipSlice = 0;
889 for (i = 0; i < num_views; i++) {
890 render_desc.Format = formats[i];
892 hr = device_handle->CreateRenderTargetView (priv->texture, &render_desc,
893 &priv->render_target_view[i]);
894 if (!gst_d3d11_result (hr, mem->device)) {
895 GST_ERROR_OBJECT (GST_MEMORY_CAST (mem)->allocator,
896 "Failed to create resource DXGI format %s (%d) for plane %d"
897 " view (0x%x)", gst_d3d11_dxgi_format_to_string (formats[i]),
898 formats[i], i, (guint) hr);
903 priv->num_render_target_views = num_views;
911 for (i = 0; i < num_views; i++)
912 GST_D3D11_CLEAR_COM (priv->render_target_view[i]);
914 priv->num_render_target_views = 0;
920 gst_d3d11_memory_ensure_render_target_view (GstD3D11Memory * mem)
922 GstD3D11MemoryPrivate *priv = mem->priv;
923 gboolean ret = FALSE;
925 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
928 if (!(priv->desc.BindFlags & D3D11_BIND_RENDER_TARGET)) {
929 GST_WARNING_OBJECT (GST_MEMORY_CAST (mem)->allocator,
930 "Need BindFlags, current flag 0x%x", priv->desc.BindFlags);
934 GST_D3D11_MEMORY_LOCK (mem);
935 if (priv->num_render_target_views) {
940 ret = create_render_target_views (mem);
943 GST_D3D11_MEMORY_UNLOCK (mem);
949 * gst_d3d11_memory_get_render_target_view_size:
950 * @mem: a #GstD3D11Memory
952 * Returns: the number of ID3D11RenderTargetView that can be used
953 * for processing GPU operation with @mem
958 gst_d3d11_memory_get_render_target_view_size (GstD3D11Memory * mem)
960 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), 0);
962 if (!gst_d3d11_memory_ensure_render_target_view (mem))
965 return mem->priv->num_render_target_views;
969 * gst_d3d11_memory_get_render_target_view:
970 * @mem: a #GstD3D11Memory
971 * @index: the index of the ID3D11RenderTargetView
973 * Returns: (transfer none) (nullable): a pointer to the
974 * ID3D11RenderTargetView or %NULL if ID3D11RenderTargetView is unavailable
979 ID3D11RenderTargetView *
980 gst_d3d11_memory_get_render_target_view (GstD3D11Memory * mem, guint index)
982 GstD3D11MemoryPrivate *priv;
984 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
986 if (!gst_d3d11_memory_ensure_render_target_view (mem))
991 if (index >= priv->num_render_target_views) {
992 GST_ERROR ("Invalid RTV index %d", index);
996 return priv->render_target_view[index];
1000 gst_d3d11_memory_ensure_decoder_output_view (GstD3D11Memory * mem,
1001 ID3D11VideoDevice * video_device, ID3D11VideoDecoder * decoder,
1002 const GUID * decoder_profile)
1004 GstD3D11MemoryPrivate *dmem_priv = mem->priv;
1005 GstD3D11Allocator *allocator;
1006 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC desc;
1008 gboolean ret = FALSE;
1010 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
1013 allocator = GST_D3D11_ALLOCATOR (GST_MEMORY_CAST (mem)->allocator);
1015 if (!(dmem_priv->desc.BindFlags & D3D11_BIND_DECODER)) {
1016 GST_LOG_OBJECT (allocator,
1017 "Need BindFlags, current flag 0x%x", dmem_priv->desc.BindFlags);
1021 GST_D3D11_MEMORY_LOCK (mem);
1022 if (dmem_priv->decoder_output_view) {
1023 dmem_priv->decoder_output_view->GetDesc (&desc);
1024 if (IsEqualGUID (desc.DecodeProfile, *decoder_profile) &&
1025 dmem_priv->decoder_handle == decoder) {
1028 /* Shouldn't happen, but try again anyway */
1029 GST_WARNING_OBJECT (allocator,
1030 "Existing view has different decoder profile");
1031 GST_D3D11_CLEAR_COM (dmem_priv->decoder_output_view);
1032 GST_D3D11_CLEAR_COM (dmem_priv->decoder_handle);
1036 if (dmem_priv->decoder_output_view)
1039 desc.DecodeProfile = *decoder_profile;
1040 desc.ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D;
1041 desc.Texture2D.ArraySlice = dmem_priv->subresource_index;
1043 hr = video_device->CreateVideoDecoderOutputView (dmem_priv->texture, &desc,
1044 &dmem_priv->decoder_output_view);
1045 if (!gst_d3d11_result (hr, mem->device)) {
1046 GST_ERROR_OBJECT (allocator,
1047 "Could not create decoder output view, hr: 0x%x", (guint) hr);
1051 /* XXX: decoder output view is bound to video device, not decoder handle
1052 * from API point of view. But some driver seems to be unhappy
1053 * when decoder handle is released while there are outstanding view objects */
1054 dmem_priv->decoder_handle = decoder;
1061 GST_D3D11_MEMORY_UNLOCK (mem);
1067 * gst_d3d11_memory_get_decoder_output_view:
1068 * @mem: a #GstD3D11Memory
1069 * @video_device: (transfer none): a ID3D11VideoDevice handle
1070 * @decoder: (transfer none): a ID3D11VideoDecoder handle
1071 * @decoder_profile: a DXVA decoder profile GUID
1073 * Returns: (transfer none) (nullable): a pointer to the
1074 * ID3D11VideoDecoderOutputView or %NULL if ID3D11VideoDecoderOutputView is
1079 ID3D11VideoDecoderOutputView *
1080 gst_d3d11_memory_get_decoder_output_view (GstD3D11Memory * mem,
1081 ID3D11VideoDevice * video_device, ID3D11VideoDecoder * decoder,
1082 const GUID * decoder_profile)
1084 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1085 g_return_val_if_fail (video_device != NULL, NULL);
1086 g_return_val_if_fail (decoder != NULL, NULL);
1087 g_return_val_if_fail (decoder_profile != NULL, NULL);
1089 if (!gst_d3d11_memory_ensure_decoder_output_view (mem,
1090 video_device, decoder, decoder_profile))
1093 return mem->priv->decoder_output_view;
1097 check_bind_flags_for_processor_input_view (guint bind_flags)
1099 static const guint compatible_flags = (D3D11_BIND_DECODER |
1100 D3D11_BIND_VIDEO_ENCODER | D3D11_BIND_RENDER_TARGET |
1101 D3D11_BIND_UNORDERED_ACCESS);
1103 if (bind_flags == 0)
1106 if ((bind_flags & compatible_flags) != 0)
1113 gst_d3d11_memory_ensure_processor_input_view (GstD3D11Memory * mem,
1114 ID3D11VideoDevice * video_device,
1115 ID3D11VideoProcessorEnumerator * enumerator)
1117 GstD3D11MemoryPrivate *dmem_priv = mem->priv;
1118 GstD3D11Allocator *allocator;
1119 D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC desc;
1121 gboolean ret = FALSE;
1123 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
1126 allocator = GST_D3D11_ALLOCATOR (GST_MEMORY_CAST (mem)->allocator);
1128 if (!check_bind_flags_for_processor_input_view (dmem_priv->desc.BindFlags)) {
1129 GST_LOG_OBJECT (allocator,
1130 "Need BindFlags, current flag 0x%x", dmem_priv->desc.BindFlags);
1134 GST_D3D11_MEMORY_LOCK (mem);
1135 if (dmem_priv->processor_input_view)
1139 desc.ViewDimension = D3D11_VPIV_DIMENSION_TEXTURE2D;
1140 desc.Texture2D.MipSlice = 0;
1141 desc.Texture2D.ArraySlice = dmem_priv->subresource_index;
1143 hr = video_device->CreateVideoProcessorInputView (dmem_priv->texture,
1144 enumerator, &desc, &dmem_priv->processor_input_view);
1145 if (!gst_d3d11_result (hr, mem->device)) {
1146 GST_ERROR_OBJECT (allocator,
1147 "Could not create processor input view, hr: 0x%x", (guint) hr);
1155 GST_D3D11_MEMORY_UNLOCK (mem);
1161 * gst_d3d11_memory_get_processor_input_view:
1162 * @mem: a #GstD3D11Memory
1163 * @video_device: a #ID3D11VideoDevice
1164 * @enumerator: a #ID3D11VideoProcessorEnumerator
1166 * Returns: (transfer none) (nullable): a pointer to the
1167 * ID3D11VideoProcessorInputView or %NULL if ID3D11VideoProcessorInputView is
1172 ID3D11VideoProcessorInputView *
1173 gst_d3d11_memory_get_processor_input_view (GstD3D11Memory * mem,
1174 ID3D11VideoDevice * video_device,
1175 ID3D11VideoProcessorEnumerator * enumerator)
1177 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1178 g_return_val_if_fail (video_device != NULL, NULL);
1179 g_return_val_if_fail (enumerator != NULL, NULL);
1181 if (!gst_d3d11_memory_ensure_processor_input_view (mem, video_device,
1185 return mem->priv->processor_input_view;
1189 gst_d3d11_memory_ensure_processor_output_view (GstD3D11Memory * mem,
1190 ID3D11VideoDevice * video_device,
1191 ID3D11VideoProcessorEnumerator * enumerator)
1193 GstD3D11MemoryPrivate *priv = mem->priv;
1194 GstD3D11Allocator *allocator;
1195 D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC desc;
1199 if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
1202 memset (&desc, 0, sizeof (D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC));
1204 allocator = GST_D3D11_ALLOCATOR (GST_MEMORY_CAST (mem)->allocator);
1206 if (!(priv->desc.BindFlags & D3D11_BIND_RENDER_TARGET)) {
1207 GST_LOG_OBJECT (allocator,
1208 "Need BindFlags, current flag 0x%x", priv->desc.BindFlags);
1212 /* FIXME: texture array should be supported at some point */
1213 if (priv->subresource_index != 0) {
1214 GST_FIXME_OBJECT (allocator,
1215 "Texture array is not suppoted for processor output view");
1219 GST_D3D11_MEMORY_LOCK (mem);
1220 if (priv->processor_output_view)
1223 desc.ViewDimension = D3D11_VPOV_DIMENSION_TEXTURE2D;
1224 desc.Texture2D.MipSlice = 0;
1226 hr = video_device->CreateVideoProcessorOutputView (priv->texture,
1227 enumerator, &desc, &priv->processor_output_view);
1228 if (!gst_d3d11_result (hr, mem->device)) {
1229 GST_ERROR_OBJECT (allocator,
1230 "Could not create processor input view, hr: 0x%x", (guint) hr);
1238 GST_D3D11_MEMORY_UNLOCK (mem);
1244 * gst_d3d11_memory_get_processor_output_view:
1245 * @mem: a #GstD3D11Memory
1246 * @video_device: a #ID3D11VideoDevice
1247 * @enumerator: a #ID3D11VideoProcessorEnumerator
1249 * Returns: (transfer none) (nullable): a pointer to the
1250 * ID3D11VideoProcessorOutputView or %NULL if ID3D11VideoProcessorOutputView is
1255 ID3D11VideoProcessorOutputView *
1256 gst_d3d11_memory_get_processor_output_view (GstD3D11Memory * mem,
1257 ID3D11VideoDevice * video_device,
1258 ID3D11VideoProcessorEnumerator * enumerator)
1260 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1261 g_return_val_if_fail (video_device != NULL, NULL);
1262 g_return_val_if_fail (enumerator != NULL, NULL);
1264 if (!gst_d3d11_memory_ensure_processor_output_view (mem, video_device,
1268 return mem->priv->processor_output_view;
1271 /* GstD3D11Allocator */
1272 struct _GstD3D11AllocatorPrivate
1274 GstMemoryCopyFunction fallback_copy;
1277 #define gst_d3d11_allocator_parent_class alloc_parent_class
1278 G_DEFINE_TYPE_WITH_PRIVATE (GstD3D11Allocator,
1279 gst_d3d11_allocator, GST_TYPE_ALLOCATOR);
1281 static GstMemory *gst_d3d11_allocator_dummy_alloc (GstAllocator * allocator,
1282 gsize size, GstAllocationParams * params);
1283 static GstMemory *gst_d3d11_allocator_alloc_internal (GstD3D11Allocator * self,
1284 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc);
1285 static void gst_d3d11_allocator_free (GstAllocator * allocator,
1289 gst_d3d11_allocator_class_init (GstD3D11AllocatorClass * klass)
1291 GstAllocatorClass *allocator_class = GST_ALLOCATOR_CLASS (klass);
1293 allocator_class->alloc = gst_d3d11_allocator_dummy_alloc;
1294 allocator_class->free = gst_d3d11_allocator_free;
1298 gst_d3d11_memory_copy (GstMemory * mem, gssize offset, gssize size)
1300 GstD3D11Allocator *alloc = GST_D3D11_ALLOCATOR (mem->allocator);
1301 GstD3D11AllocatorPrivate *priv = alloc->priv;
1302 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
1303 GstD3D11Memory *copy_dmem;
1304 GstD3D11Device *device = dmem->device;
1305 ID3D11Device *device_handle = gst_d3d11_device_get_device_handle (device);
1306 ID3D11DeviceContext *device_context =
1307 gst_d3d11_device_get_device_context_handle (device);
1308 D3D11_TEXTURE2D_DESC dst_desc = { 0, };
1309 D3D11_TEXTURE2D_DESC src_desc = { 0, };
1310 GstMemory *copy = NULL;
1313 UINT bind_flags = 0;
1314 UINT supported_flags = 0;
1316 if (dmem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
1317 return priv->fallback_copy (mem, offset, size);
1319 /* non-zero offset or different size is not supported */
1320 if (offset != 0 || (size != -1 && (gsize) size != mem->size)) {
1321 GST_DEBUG_OBJECT (alloc, "Different size/offset, try fallback copy");
1322 return priv->fallback_copy (mem, offset, size);
1325 gst_d3d11_device_lock (device);
1326 if (!gst_memory_map (mem, &info,
1327 (GstMapFlags) (GST_MAP_READ | GST_MAP_D3D11))) {
1328 gst_d3d11_device_unlock (device);
1330 GST_WARNING_OBJECT (alloc, "Failed to map memory, try fallback copy");
1332 return priv->fallback_copy (mem, offset, size);
1335 dmem->priv->texture->GetDesc (&src_desc);
1336 dst_desc.Width = src_desc.Width;
1337 dst_desc.Height = src_desc.Height;
1338 dst_desc.MipLevels = 1;
1339 dst_desc.Format = src_desc.Format;
1340 dst_desc.SampleDesc.Count = 1;
1341 dst_desc.ArraySize = 1;
1342 dst_desc.Usage = D3D11_USAGE_DEFAULT;
1344 /* If supported, use bind flags for SRV/RTV */
1345 hr = device_handle->CheckFormatSupport (src_desc.Format, &supported_flags);
1346 if (gst_d3d11_result (hr, device)) {
1347 if ((supported_flags & D3D11_FORMAT_SUPPORT_SHADER_SAMPLE) ==
1348 D3D11_FORMAT_SUPPORT_SHADER_SAMPLE) {
1349 bind_flags |= D3D11_BIND_SHADER_RESOURCE;
1352 if ((supported_flags & D3D11_FORMAT_SUPPORT_RENDER_TARGET) ==
1353 D3D11_FORMAT_SUPPORT_RENDER_TARGET) {
1354 bind_flags |= D3D11_BIND_RENDER_TARGET;
1358 copy = gst_d3d11_allocator_alloc_internal (alloc, device, &dst_desc);
1360 gst_memory_unmap (mem, &info);
1361 gst_d3d11_device_unlock (device);
1363 GST_WARNING_OBJECT (alloc,
1364 "Failed to allocate new d3d11 map memory, try fallback copy");
1366 return priv->fallback_copy (mem, offset, size);
1369 copy_dmem = GST_D3D11_MEMORY_CAST (copy);
1370 device_context->CopySubresourceRegion (copy_dmem->priv->texture, 0, 0, 0, 0,
1371 dmem->priv->texture, dmem->priv->subresource_index, NULL);
1372 copy->maxsize = copy->size = mem->maxsize;
1373 gst_memory_unmap (mem, &info);
1374 gst_d3d11_device_unlock (device);
1376 /* newly allocated memory holds valid image data. We need download this
1377 * pixel data into staging memory for CPU access */
1378 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
1384 gst_d3d11_allocator_init (GstD3D11Allocator * allocator)
1386 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
1387 GstD3D11AllocatorPrivate *priv;
1389 priv = allocator->priv = (GstD3D11AllocatorPrivate *)
1390 gst_d3d11_allocator_get_instance_private (allocator);
1392 alloc->mem_type = GST_D3D11_MEMORY_NAME;
1393 alloc->mem_map_full = gst_d3d11_memory_map_full;
1394 alloc->mem_unmap_full = gst_d3d11_memory_unmap_full;
1395 alloc->mem_share = gst_d3d11_memory_share;
1397 /* Store pointer to default mem_copy method for fallback copy */
1398 priv->fallback_copy = alloc->mem_copy;
1399 alloc->mem_copy = gst_d3d11_memory_copy;
1401 GST_OBJECT_FLAG_SET (alloc, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
1405 gst_d3d11_allocator_dummy_alloc (GstAllocator * allocator, gsize size,
1406 GstAllocationParams * params)
1408 g_return_val_if_reached (NULL);
1412 gst_d3d11_allocator_free (GstAllocator * allocator, GstMemory * mem)
1414 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
1415 GstD3D11MemoryPrivate *dmem_priv = dmem->priv;
1418 GST_LOG_OBJECT (allocator, "Free memory %p", mem);
1420 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
1421 GST_D3D11_CLEAR_COM (dmem_priv->render_target_view[i]);
1422 GST_D3D11_CLEAR_COM (dmem_priv->shader_resource_view[i]);
1425 GST_D3D11_CLEAR_COM (dmem_priv->decoder_output_view);
1426 GST_D3D11_CLEAR_COM (dmem_priv->processor_input_view);
1427 GST_D3D11_CLEAR_COM (dmem_priv->processor_output_view);
1428 GST_D3D11_CLEAR_COM (dmem_priv->texture);
1429 GST_D3D11_CLEAR_COM (dmem_priv->staging);
1430 GST_D3D11_CLEAR_COM (dmem_priv->buffer);
1432 GST_D3D11_CLEAR_COM (dmem_priv->decoder_handle);
1434 gst_clear_object (&dmem->device);
1435 g_mutex_clear (&dmem_priv->lock);
1436 g_free (dmem->priv);
1441 gst_d3d11_allocator_alloc_wrapped (GstD3D11Allocator * self,
1442 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc,
1443 ID3D11Texture2D * texture)
1445 GstD3D11Memory *mem;
1447 mem = g_new0 (GstD3D11Memory, 1);
1448 mem->priv = g_new0 (GstD3D11MemoryPrivate, 1);
1450 gst_memory_init (GST_MEMORY_CAST (mem),
1451 (GstMemoryFlags) 0, GST_ALLOCATOR_CAST (self), NULL, 0, 0, 0, 0);
1452 g_mutex_init (&mem->priv->lock);
1453 mem->priv->texture = texture;
1454 mem->priv->desc = *desc;
1455 mem->priv->native_type = GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D;
1456 mem->device = (GstD3D11Device *) gst_object_ref (device);
1458 return GST_MEMORY_CAST (mem);
1461 typedef void (*GstD3D11ClearRTVFunc) (ID3D11DeviceContext * context_handle,
1462 ID3D11RenderTargetView * rtv);
1465 clear_rtv_chroma (ID3D11DeviceContext * context_handle,
1466 ID3D11RenderTargetView * rtv)
1468 const FLOAT clear_color[4] = { 0.5f, 0.5f, 0.5f, 1.0f };
1470 context_handle->ClearRenderTargetView (rtv, clear_color);
1474 clear_rtv_vuya (ID3D11DeviceContext * context_handle,
1475 ID3D11RenderTargetView * rtv)
1477 const FLOAT clear_color[4] = { 0.5f, 0.5f, 0.0f, 1.0f };
1479 context_handle->ClearRenderTargetView (rtv, clear_color);
1483 gst_d3d11_allocator_alloc_internal (GstD3D11Allocator * self,
1484 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc)
1486 ID3D11Texture2D *texture = NULL;
1487 ID3D11Device *device_handle;
1488 ID3D11DeviceContext *context_handle;
1491 GstD3D11Memory *dmem;
1492 ID3D11RenderTargetView *rtv = nullptr;
1493 GstD3D11ClearRTVFunc clear_func = nullptr;
1495 device_handle = gst_d3d11_device_get_device_handle (device);
1497 hr = device_handle->CreateTexture2D (desc, NULL, &texture);
1498 if (!gst_d3d11_result (hr, device)) {
1499 GST_ERROR_OBJECT (self, "Couldn't create texture");
1503 mem = gst_d3d11_allocator_alloc_wrapped (self, device, desc, texture);
1507 /* Clear with YUV black if needed and possible
1508 * TODO: do this using UAV if RTV is not allowed (e.g., packed YUV formats) */
1509 if ((desc->BindFlags & D3D11_BIND_RENDER_TARGET) == 0)
1512 dmem = GST_D3D11_MEMORY_CAST (mem);
1513 switch (desc->Format) {
1514 case DXGI_FORMAT_NV12:
1515 case DXGI_FORMAT_P010:
1516 case DXGI_FORMAT_P016:
1517 /* Y component will be zero already */
1518 rtv = gst_d3d11_memory_get_render_target_view (dmem, 1);
1519 clear_func = (GstD3D11ClearRTVFunc) clear_rtv_chroma;
1521 case DXGI_FORMAT_AYUV:
1522 rtv = gst_d3d11_memory_get_render_target_view (dmem, 0);
1523 clear_func = (GstD3D11ClearRTVFunc) clear_rtv_vuya;
1532 context_handle = gst_d3d11_device_get_device_context_handle (device);
1533 gst_d3d11_device_lock (device);
1534 clear_func (context_handle, rtv);
1535 gst_d3d11_device_unlock (device);
1541 * gst_d3d11_allocator_alloc:
1542 * @allocator: a #GstD3D11Allocator
1543 * @device: a #GstD3D11Device
1544 * @desc: a D3D11_TEXTURE2D_DESC struct
1546 * Returns: a newly allocated #GstD3D11Memory with given parameters.
1551 gst_d3d11_allocator_alloc (GstD3D11Allocator * allocator,
1552 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc)
1556 g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), NULL);
1557 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), NULL);
1558 g_return_val_if_fail (desc != NULL, NULL);
1560 mem = gst_d3d11_allocator_alloc_internal (allocator, device, desc);
1564 if (!gst_d3d11_memory_update_size (mem)) {
1565 GST_ERROR_OBJECT (allocator, "Failed to calculate size");
1566 gst_memory_unref (mem);
1574 * gst_d3d11_allocator_alloc_buffer:
1575 * @allocator: a #GstD3D11Allocator
1576 * @device: a #GstD3D11Device
1577 * @desc: a D3D11_BUFFER_DESC struct
1579 * Returns: a newly allocated #GstD3D11Memory with given parameters.
1584 gst_d3d11_allocator_alloc_buffer (GstD3D11Allocator * allocator,
1585 GstD3D11Device * device, const D3D11_BUFFER_DESC * desc)
1587 GstD3D11Memory *mem;
1588 ID3D11Buffer *buffer;
1589 ID3D11Device *device_handle;
1592 g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), nullptr);
1593 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), nullptr);
1594 g_return_val_if_fail (desc != nullptr, nullptr);
1596 if (desc->Usage != D3D11_USAGE_STAGING) {
1597 GST_FIXME_OBJECT (allocator, "Non staging buffer is not supported");
1601 device_handle = gst_d3d11_device_get_device_handle (device);
1603 hr = device_handle->CreateBuffer (desc, nullptr, &buffer);
1604 if (!gst_d3d11_result (hr, device)) {
1605 GST_ERROR_OBJECT (allocator, "Couldn't create buffer");
1609 mem = g_new0 (GstD3D11Memory, 1);
1610 mem->priv = g_new0 (GstD3D11MemoryPrivate, 1);
1612 gst_memory_init (GST_MEMORY_CAST (mem),
1613 (GstMemoryFlags) 0, GST_ALLOCATOR_CAST (allocator), nullptr, 0, 0, 0, 0);
1614 g_mutex_init (&mem->priv->lock);
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_set_active:
1628 * @allocator: a #GstD3D11Allocator
1629 * @active: the new active state
1631 * Controls the active state of @allocator. Default #GstD3D11Allocator is
1632 * stateless and therefore active state is ignored, but subclass implementation
1633 * (e.g., #GstD3D11PoolAllocator) will require explicit active state control
1634 * for its internal resource management.
1636 * This method is conceptually identical to gst_buffer_pool_set_active method.
1638 * Returns: %TRUE if active state of @allocator was successfully updated.
1643 gst_d3d11_allocator_set_active (GstD3D11Allocator * allocator, gboolean active)
1645 GstD3D11AllocatorClass *klass;
1647 g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), FALSE);
1649 klass = GST_D3D11_ALLOCATOR_GET_CLASS (allocator);
1650 if (klass->set_actvie)
1651 return klass->set_actvie (allocator, active);
1656 /* GstD3D11PoolAllocator */
1657 #define GST_D3D11_POOL_ALLOCATOR_LOCK(alloc) (g_rec_mutex_lock(&alloc->priv->lock))
1658 #define GST_D3D11_POOL_ALLOCATOR_UNLOCK(alloc) (g_rec_mutex_unlock(&alloc->priv->lock))
1659 #define GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING(alloc) (g_atomic_int_get (&alloc->priv->flushing))
1661 struct _GstD3D11PoolAllocatorPrivate
1663 /* parent texture when array typed memory is used */
1664 ID3D11Texture2D *texture;
1665 D3D11_TEXTURE2D_DESC desc;
1667 /* All below member variables are analogous to that of GstBufferPool */
1668 GstAtomicQueue *queue;
1671 /* This lock will protect all below variables apart from atomic ones
1672 * (identical to GstBufferPool::priv::rec_lock) */
1683 /* Calculated memory size, based on Direct3D11 staging texture map.
1684 * Note that, we cannot know the actually staging texture memory size prior
1685 * to map the staging texture because driver will likely require padding */
1689 static void gst_d3d11_pool_allocator_dispose (GObject * object);
1690 static void gst_d3d11_pool_allocator_finalize (GObject * object);
1693 gst_d3d11_pool_allocator_set_active (GstD3D11Allocator * allocator,
1696 static gboolean gst_d3d11_pool_allocator_start (GstD3D11PoolAllocator * self);
1697 static gboolean gst_d3d11_pool_allocator_stop (GstD3D11PoolAllocator * self);
1698 static gboolean gst_d3d11_memory_release (GstMiniObject * mini_object);
1700 #define gst_d3d11_pool_allocator_parent_class pool_alloc_parent_class
1701 G_DEFINE_TYPE_WITH_PRIVATE (GstD3D11PoolAllocator,
1702 gst_d3d11_pool_allocator, GST_TYPE_D3D11_ALLOCATOR);
1705 gst_d3d11_pool_allocator_class_init (GstD3D11PoolAllocatorClass * klass)
1707 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1708 GstD3D11AllocatorClass *d3d11alloc_class = GST_D3D11_ALLOCATOR_CLASS (klass);
1710 gobject_class->dispose = gst_d3d11_pool_allocator_dispose;
1711 gobject_class->finalize = gst_d3d11_pool_allocator_finalize;
1713 d3d11alloc_class->set_actvie = gst_d3d11_pool_allocator_set_active;
1717 gst_d3d11_pool_allocator_init (GstD3D11PoolAllocator * allocator)
1719 GstD3D11PoolAllocatorPrivate *priv;
1721 priv = allocator->priv = (GstD3D11PoolAllocatorPrivate *)
1722 gst_d3d11_pool_allocator_get_instance_private (allocator);
1723 g_rec_mutex_init (&priv->lock);
1725 priv->poll = gst_poll_new_timer ();
1726 priv->queue = gst_atomic_queue_new (16);
1728 priv->active = FALSE;
1729 priv->started = FALSE;
1731 /* 1 control write for flushing - the flush token */
1732 gst_poll_write_control (priv->poll);
1733 /* 1 control write for marking that we are not waiting for poll - the wait token */
1734 gst_poll_write_control (priv->poll);
1738 gst_d3d11_pool_allocator_dispose (GObject * object)
1740 GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (object);
1742 gst_clear_object (&self->device);
1744 G_OBJECT_CLASS (pool_alloc_parent_class)->dispose (object);
1748 gst_d3d11_pool_allocator_finalize (GObject * object)
1750 GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (object);
1751 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1753 GST_DEBUG_OBJECT (self, "Finalize");
1755 gst_d3d11_pool_allocator_stop (self);
1756 gst_atomic_queue_unref (priv->queue);
1757 gst_poll_free (priv->poll);
1758 g_rec_mutex_clear (&priv->lock);
1760 GST_D3D11_CLEAR_COM (priv->texture);
1762 G_OBJECT_CLASS (pool_alloc_parent_class)->finalize (object);
1766 gst_d3d11_pool_allocator_start (GstD3D11PoolAllocator * self)
1768 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1769 ID3D11Device *device_handle;
1777 if (priv->desc.ArraySize == 1) {
1778 priv->started = TRUE;
1782 device_handle = gst_d3d11_device_get_device_handle (self->device);
1784 if (!priv->texture) {
1785 hr = device_handle->CreateTexture2D (&priv->desc, NULL, &priv->texture);
1786 if (!gst_d3d11_result (hr, self->device)) {
1787 GST_ERROR_OBJECT (self, "Failed to allocate texture");
1792 /* Pre-allocate memory objects */
1793 for (i = 0; i < priv->desc.ArraySize; i++) {
1796 priv->texture->AddRef ();
1798 gst_d3d11_allocator_alloc_wrapped (GST_D3D11_ALLOCATOR_CAST
1799 (_d3d11_memory_allocator), self->device, &priv->desc, priv->texture);
1802 if (!gst_d3d11_memory_update_size (mem)) {
1803 GST_ERROR_OBJECT (self, "Failed to calculate memory size");
1804 gst_memory_unref (mem);
1808 priv->mem_size = mem->size;
1810 mem->size = mem->maxsize = priv->mem_size;
1813 GST_D3D11_MEMORY_CAST (mem)->priv->subresource_index = i;
1815 g_atomic_int_add (&priv->cur_mems, 1);
1816 gst_atomic_queue_push (priv->queue, mem);
1817 gst_poll_write_control (priv->poll);
1820 priv->started = TRUE;
1826 gst_d3d11_pool_allocator_do_set_flushing (GstD3D11PoolAllocator * self,
1829 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1831 if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self) == flushing)
1835 g_atomic_int_set (&priv->flushing, 1);
1836 /* Write the flush token to wake up any waiters */
1837 gst_poll_write_control (priv->poll);
1839 while (!gst_poll_read_control (priv->poll)) {
1840 if (errno == EWOULDBLOCK) {
1841 /* This should not really happen unless flushing and unflushing
1842 * happens on different threads. Let's wait a bit to get back flush
1843 * token from the thread that was setting it to flushing */
1847 /* Critical error but GstPoll already complained */
1852 g_atomic_int_set (&priv->flushing, 0);
1857 gst_d3d11_pool_allocator_set_active (GstD3D11Allocator * allocator,
1860 GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (allocator);
1861 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1863 GST_LOG_OBJECT (self, "active %d", active);
1865 GST_D3D11_POOL_ALLOCATOR_LOCK (self);
1866 /* just return if we are already in the right state */
1867 if (priv->active == active)
1871 if (!gst_d3d11_pool_allocator_start (self))
1874 /* flush_stop may release memory objects, setting to active to avoid running
1875 * do_stop while activating the pool */
1876 priv->active = TRUE;
1878 gst_d3d11_pool_allocator_do_set_flushing (self, FALSE);
1882 /* set to flushing first */
1883 gst_d3d11_pool_allocator_do_set_flushing (self, TRUE);
1885 /* when all memory objects are in the pool, free them. Else they will be
1886 * freed when they are released */
1887 outstanding = g_atomic_int_get (&priv->outstanding);
1888 GST_LOG_OBJECT (self, "outstanding memories %d, (in queue %d)",
1889 outstanding, gst_atomic_queue_length (priv->queue));
1890 if (outstanding == 0) {
1891 if (!gst_d3d11_pool_allocator_stop (self))
1895 priv->active = FALSE;
1898 GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1904 GST_DEBUG_OBJECT (self, "allocator was in the right state");
1905 GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1910 GST_ERROR_OBJECT (self, "start failed");
1911 GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1916 GST_ERROR_OBJECT (self, "stop failed");
1917 GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1923 gst_d3d11_pool_allocator_free_memory (GstD3D11PoolAllocator * self,
1926 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1928 g_atomic_int_add (&priv->cur_mems, -1);
1929 GST_LOG_OBJECT (self, "freeing memory %p (%u left)", mem, priv->cur_mems);
1931 GST_MINI_OBJECT_CAST (mem)->dispose = NULL;
1932 gst_memory_unref (mem);
1935 /* must be called with the lock */
1937 gst_d3d11_pool_allocator_clear_queue (GstD3D11PoolAllocator * self)
1939 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1942 GST_LOG_OBJECT (self, "Clearing queue");
1944 /* clear the pool */
1945 while ((memory = (GstMemory *) gst_atomic_queue_pop (priv->queue))) {
1946 while (!gst_poll_read_control (priv->poll)) {
1947 if (errno == EWOULDBLOCK) {
1948 /* We put the memory into the queue but did not finish writing control
1949 * yet, let's wait a bit and retry */
1953 /* Critical error but GstPoll already complained */
1957 gst_d3d11_pool_allocator_free_memory (self, memory);
1960 GST_LOG_OBJECT (self, "Clear done");
1962 return priv->cur_mems == 0;
1965 /* must be called with the lock */
1967 gst_d3d11_pool_allocator_stop (GstD3D11PoolAllocator * self)
1969 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1971 GST_DEBUG_OBJECT (self, "Stop");
1973 if (priv->started) {
1974 if (!gst_d3d11_pool_allocator_clear_queue (self))
1977 priv->started = FALSE;
1979 GST_DEBUG_OBJECT (self, "Wasn't started");
1986 dec_outstanding (GstD3D11PoolAllocator * self)
1988 if (g_atomic_int_dec_and_test (&self->priv->outstanding)) {
1989 /* all memory objects are returned to the pool, see if we need to free them */
1990 if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)) {
1991 /* take the lock so that set_active is not run concurrently */
1992 GST_D3D11_POOL_ALLOCATOR_LOCK (self);
1993 /* now that we have the lock, check if we have been de-activated with
1994 * outstanding buffers */
1995 if (!self->priv->active)
1996 gst_d3d11_pool_allocator_stop (self);
1998 GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
2004 gst_d3d11_pool_allocator_release_memory (GstD3D11PoolAllocator * self,
2007 GST_LOG_OBJECT (self, "Released memory %p", mem);
2009 GST_MINI_OBJECT_CAST (mem)->dispose = NULL;
2010 mem->allocator = (GstAllocator *) gst_object_ref (_d3d11_memory_allocator);
2011 gst_object_unref (self);
2013 /* keep it around in our queue */
2014 gst_atomic_queue_push (self->priv->queue, mem);
2015 gst_poll_write_control (self->priv->poll);
2016 dec_outstanding (self);
2020 gst_d3d11_memory_release (GstMiniObject * mini_object)
2022 GstMemory *mem = GST_MEMORY_CAST (mini_object);
2023 GstD3D11PoolAllocator *alloc;
2025 g_assert (mem->allocator != NULL);
2027 if (!GST_IS_D3D11_POOL_ALLOCATOR (mem->allocator)) {
2028 GST_LOG_OBJECT (mem->allocator, "Not our memory, free");
2032 alloc = GST_D3D11_POOL_ALLOCATOR (mem->allocator);
2033 /* if flushing, free this memory */
2034 if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (alloc)) {
2035 GST_LOG_OBJECT (alloc, "allocator is flushing, free %p", mem);
2039 /* return the memory to the allocator */
2040 gst_memory_ref (mem);
2041 gst_d3d11_pool_allocator_release_memory (alloc, mem);
2046 static GstFlowReturn
2047 gst_d3d11_pool_allocator_alloc (GstD3D11PoolAllocator * self, GstMemory ** mem)
2049 GstD3D11PoolAllocatorPrivate *priv = self->priv;
2052 /* we allcates texture array during start */
2053 if (priv->desc.ArraySize > 1)
2054 return GST_FLOW_EOS;
2056 /* increment the allocation counter */
2057 g_atomic_int_add (&priv->cur_mems, 1);
2059 gst_d3d11_allocator_alloc_internal (GST_D3D11_ALLOCATOR_CAST
2060 (_d3d11_memory_allocator), self->device, &priv->desc);
2062 GST_ERROR_OBJECT (self, "Failed to allocate new memory");
2063 g_atomic_int_add (&priv->cur_mems, -1);
2064 return GST_FLOW_ERROR;
2067 if (!priv->mem_size) {
2068 if (!gst_d3d11_memory_update_size (new_mem)) {
2069 GST_ERROR_OBJECT (self, "Failed to calculate size");
2070 gst_memory_unref (new_mem);
2071 g_atomic_int_add (&priv->cur_mems, -1);
2073 return GST_FLOW_ERROR;
2076 priv->mem_size = new_mem->size;
2079 new_mem->size = new_mem->maxsize = priv->mem_size;
2086 static GstFlowReturn
2087 gst_d3d11_pool_allocator_acquire_memory_internal (GstD3D11PoolAllocator * self,
2088 GstMemory ** memory)
2090 GstFlowReturn result;
2091 GstD3D11PoolAllocatorPrivate *priv = self->priv;
2094 if (G_UNLIKELY (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)))
2097 /* try to get a memory from the queue */
2098 *memory = (GstMemory *) gst_atomic_queue_pop (priv->queue);
2099 if (G_LIKELY (*memory)) {
2100 while (!gst_poll_read_control (priv->poll)) {
2101 if (errno == EWOULDBLOCK) {
2102 /* We put the memory into the queue but did not finish writing control
2103 * yet, let's wait a bit and retry */
2107 /* Critical error but GstPoll already complained */
2111 result = GST_FLOW_OK;
2112 GST_LOG_OBJECT (self, "acquired memory %p", *memory);
2116 /* no memory, try to allocate some more */
2117 GST_LOG_OBJECT (self, "no memory, trying to allocate");
2118 result = gst_d3d11_pool_allocator_alloc (self, memory);
2119 if (G_LIKELY (result == GST_FLOW_OK))
2120 /* we have a memory, return it */
2123 if (G_UNLIKELY (result != GST_FLOW_EOS))
2124 /* something went wrong, return error */
2127 /* now we release the control socket, we wait for a memory release or
2129 if (!gst_poll_read_control (priv->poll)) {
2130 if (errno == EWOULDBLOCK) {
2131 /* This means that we have two threads trying to allocate memory
2132 * already, and the other one already got the wait token. This
2133 * means that we only have to wait for the poll now and not write the
2134 * token afterwards: we will be woken up once the other thread is
2135 * woken up and that one will write the wait token it removed */
2136 GST_LOG_OBJECT (self, "waiting for free memory or flushing");
2137 gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
2139 /* This is a critical error, GstPoll already gave a warning */
2140 result = GST_FLOW_ERROR;
2144 /* We're the first thread waiting, we got the wait token and have to
2145 * write it again later
2147 * We're a second thread and just consumed the flush token and block all
2148 * other threads, in which case we must not wait and give it back
2150 if (!GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)) {
2151 GST_LOG_OBJECT (self, "waiting for free memory or flushing");
2152 gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
2154 gst_poll_write_control (priv->poll);
2163 GST_DEBUG_OBJECT (self, "we are flushing");
2164 return GST_FLOW_FLUSHING;
2169 * gst_d3d11_pool_allocator_new:
2170 * @device: a #GstD3D11Device
2171 * @desc: a D3D11_TEXTURE2D_DESC for texture allocation
2173 * Creates a new #GstD3D11PoolAllocator instance.
2175 * Returns: (transfer full): a new #GstD3D11PoolAllocator instance
2179 GstD3D11PoolAllocator *
2180 gst_d3d11_pool_allocator_new (GstD3D11Device * device,
2181 const D3D11_TEXTURE2D_DESC * desc)
2183 GstD3D11PoolAllocator *self;
2185 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), NULL);
2186 g_return_val_if_fail (desc != NULL, NULL);
2188 gst_d3d11_memory_init_once ();
2190 self = (GstD3D11PoolAllocator *)
2191 g_object_new (GST_TYPE_D3D11_POOL_ALLOCATOR, NULL);
2192 gst_object_ref_sink (self);
2194 self->device = (GstD3D11Device *) gst_object_ref (device);
2195 self->priv->desc = *desc;
2201 * gst_d3d11_pool_allocator_acquire_memory:
2202 * @allocator: a #GstD3D11PoolAllocator
2203 * @memory: (out): a #GstMemory
2205 * Acquires a #GstMemory from @allocator. @memory should point to a memory
2206 * location that can hold a pointer to the new #GstMemory.
2208 * Returns: a #GstFlowReturn such as %GST_FLOW_FLUSHING when the allocator is
2212 gst_d3d11_pool_allocator_acquire_memory (GstD3D11PoolAllocator * allocator,
2213 GstMemory ** memory)
2215 GstD3D11PoolAllocatorPrivate *priv;
2216 GstFlowReturn result;
2218 g_return_val_if_fail (GST_IS_D3D11_POOL_ALLOCATOR (allocator),
2220 g_return_val_if_fail (memory != NULL, GST_FLOW_ERROR);
2222 priv = allocator->priv;
2224 /* assume we'll have one more outstanding buffer we need to do that so
2225 * that concurrent set_active doesn't clear the buffers */
2226 g_atomic_int_inc (&priv->outstanding);
2227 result = gst_d3d11_pool_allocator_acquire_memory_internal (allocator, memory);
2229 if (result == GST_FLOW_OK) {
2230 GstMemory *mem = *memory;
2231 /* Replace default allocator with ours */
2232 gst_object_unref (mem->allocator);
2233 mem->allocator = (GstAllocator *) gst_object_ref (allocator);
2234 GST_MINI_OBJECT_CAST (mem)->dispose = gst_d3d11_memory_release;
2236 dec_outstanding (allocator);
2243 * gst_d3d11_pool_allocator_get_pool_size:
2244 * @allocator: a #GstD3D11PoolAllocator
2245 * @max_size: (out) (optional): the max size of pool
2246 * @outstanding_size: (out) (optional): the number of outstanding memory
2248 * Returns: %TRUE if the size of memory pool is known
2253 gst_d3d11_pool_allocator_get_pool_size (GstD3D11PoolAllocator * allocator,
2254 guint * max_size, guint * outstanding_size)
2256 GstD3D11PoolAllocatorPrivate *priv;
2258 g_return_val_if_fail (GST_IS_D3D11_POOL_ALLOCATOR (allocator), FALSE);
2260 priv = allocator->priv;
2263 if (priv->desc.ArraySize > 1) {
2264 *max_size = priv->desc.ArraySize;
2266 /* For non-texture-array memory, we don't have any limit yet */
2271 if (outstanding_size)
2272 *outstanding_size = g_atomic_int_get (&priv->outstanding);