2 * Copyright (C) 2019 Seungha Yang <seungha.yang@navercorp.com>
3 * Copyright (C) 2020 Seungha Yang <seungha@centricular.com>
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
26 #include "gstd3d11memory.h"
27 #include "gstd3d11device.h"
28 #include "gstd3d11utils.h"
29 #include "gstd3d11_private.h"
31 GST_DEBUG_CATEGORY_STATIC (gst_d3d11_allocator_debug);
32 #define GST_CAT_DEFAULT gst_d3d11_allocator_debug
34 static GstAllocator *_d3d11_memory_allocator;
36 /* GstD3D11AllocationParams */
37 static void gst_d3d11_allocation_params_init (GType type);
38 G_DEFINE_BOXED_TYPE_WITH_CODE (GstD3D11AllocationParams,
39 gst_d3d11_allocation_params,
40 (GBoxedCopyFunc) gst_d3d11_allocation_params_copy,
41 (GBoxedFreeFunc) gst_d3d11_allocation_params_free,
42 gst_d3d11_allocation_params_init (g_define_type_id));
45 * gst_d3d11_allocation_params_new:
46 * @device: a #GstD3D11Device
47 * @info: a #GstVideoInfo
48 * @flags: a #GstD3D11AllocationFlags
49 * @bind_flags: D3D11_BIND_FLAG value used for creating Direct3D11 texture
51 * Create #GstD3D11AllocationParams object which is used by #GstD3D11BufferPool
52 * and #GstD3D11Allocator in order to allocate new ID3D11Texture2D
53 * object with given configuration
55 * Returns: a #GstD3D11AllocationParams or %NULL if @info is not supported
59 GstD3D11AllocationParams *
60 gst_d3d11_allocation_params_new (GstD3D11Device * device, GstVideoInfo * info,
61 GstD3D11AllocationFlags flags, guint bind_flags)
63 GstD3D11AllocationParams *ret;
64 const GstD3D11Format *d3d11_format;
67 g_return_val_if_fail (info != NULL, NULL);
69 d3d11_format = gst_d3d11_device_format_from_gst (device,
70 GST_VIDEO_INFO_FORMAT (info));
72 GST_WARNING ("Couldn't get d3d11 format");
76 ret = g_new0 (GstD3D11AllocationParams, 1);
79 ret->aligned_info = *info;
80 ret->d3d11_format = d3d11_format;
83 * https://docs.microsoft.com/en-us/windows/win32/api/d3d11/ne-d3d11-d3d11_usage
85 * +----------------------------------------------------------+
86 * | Resource Usage | Default | Dynamic | Immutable | Staging |
87 * +----------------+---------+---------+-----------+---------+
88 * | GPU-Read | Yes | Yes | Yes | Yes |
89 * | GPU-Write | Yes | | | Yes |
90 * | CPU-Read | | | | Yes |
91 * | CPU-Write | | Yes | | Yes |
92 * +----------------------------------------------------------+
95 /* If corresponding dxgi format is undefined, use resource format instead */
96 if (d3d11_format->dxgi_format == DXGI_FORMAT_UNKNOWN) {
97 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (info); i++) {
98 g_assert (d3d11_format->resource_format[i] != DXGI_FORMAT_UNKNOWN);
100 ret->desc[i].Width = GST_VIDEO_INFO_COMP_WIDTH (info, i);
101 ret->desc[i].Height = GST_VIDEO_INFO_COMP_HEIGHT (info, i);
102 ret->desc[i].MipLevels = 1;
103 ret->desc[i].ArraySize = 1;
104 ret->desc[i].Format = d3d11_format->resource_format[i];
105 ret->desc[i].SampleDesc.Count = 1;
106 ret->desc[i].SampleDesc.Quality = 0;
107 ret->desc[i].Usage = D3D11_USAGE_DEFAULT;
108 ret->desc[i].BindFlags = bind_flags;
111 ret->desc[0].Width = GST_VIDEO_INFO_WIDTH (info);
112 ret->desc[0].Height = GST_VIDEO_INFO_HEIGHT (info);
113 ret->desc[0].MipLevels = 1;
114 ret->desc[0].ArraySize = 1;
115 ret->desc[0].Format = d3d11_format->dxgi_format;
116 ret->desc[0].SampleDesc.Count = 1;
117 ret->desc[0].SampleDesc.Quality = 0;
118 ret->desc[0].Usage = D3D11_USAGE_DEFAULT;
119 ret->desc[0].BindFlags = bind_flags;
128 * gst_d3d11_allocation_params_alignment:
129 * @params: a #GstD3D11AllocationParams
130 * @align: a #GstVideoAlignment
132 * Adjust Width and Height fields of D3D11_TEXTURE2D_DESC with given
135 * Returns: %TRUE if alignment could be applied
140 gst_d3d11_allocation_params_alignment (GstD3D11AllocationParams * params,
141 GstVideoAlignment * align)
144 guint padding_width, padding_height;
146 GstVideoInfo new_info;
148 g_return_val_if_fail (params != NULL, FALSE);
149 g_return_val_if_fail (align != NULL, FALSE);
151 /* d3d11 does not support stride align. Consider padding only */
152 padding_width = align->padding_left + align->padding_right;
153 padding_height = align->padding_top + align->padding_bottom;
155 info = ¶ms->info;
157 if (!gst_video_info_set_format (&new_info, GST_VIDEO_INFO_FORMAT (info),
158 GST_VIDEO_INFO_WIDTH (info) + padding_width,
159 GST_VIDEO_INFO_HEIGHT (info) + padding_height)) {
160 GST_WARNING ("Set format fail");
164 params->aligned_info = new_info;
166 for (i = 0; i < GST_VIDEO_INFO_N_PLANES (info); i++) {
167 params->desc[i].Width = GST_VIDEO_INFO_COMP_WIDTH (&new_info, i);
168 params->desc[i].Height = GST_VIDEO_INFO_COMP_HEIGHT (&new_info, i);
175 * gst_d3d11_allocation_params_copy:
176 * @src: a #GstD3D11AllocationParams
178 * Returns: a copy of @src
182 GstD3D11AllocationParams *
183 gst_d3d11_allocation_params_copy (GstD3D11AllocationParams * src)
185 GstD3D11AllocationParams *dst;
187 g_return_val_if_fail (src != NULL, NULL);
189 dst = g_new0 (GstD3D11AllocationParams, 1);
190 memcpy (dst, src, sizeof (GstD3D11AllocationParams));
196 * gst_d3d11_allocation_params_free:
197 * @params: a #GstD3D11AllocationParams
204 gst_d3d11_allocation_params_free (GstD3D11AllocationParams * params)
210 gst_d3d11_allocation_params_compare (const GstD3D11AllocationParams * p1,
211 const GstD3D11AllocationParams * p2)
213 g_return_val_if_fail (p1 != NULL, -1);
214 g_return_val_if_fail (p2 != NULL, -1);
223 gst_d3d11_allocation_params_init (GType type)
225 static GstValueTable table = {
226 0, (GstValueCompareFunc) gst_d3d11_allocation_params_compare,
231 gst_value_register (&table);
235 #define GST_D3D11_MEMORY_GET_LOCK(m) (&(GST_D3D11_MEMORY_CAST(m)->priv->lock))
236 #define GST_D3D11_MEMORY_LOCK(m) G_STMT_START { \
237 GST_TRACE("Locking %p from thread %p", (m), g_thread_self()); \
238 g_mutex_lock(GST_D3D11_MEMORY_GET_LOCK(m)); \
239 GST_TRACE("Locked %p from thread %p", (m), g_thread_self()); \
242 #define GST_D3D11_MEMORY_UNLOCK(m) G_STMT_START { \
243 GST_TRACE("Unlocking %p from thread %p", (m), g_thread_self()); \
244 g_mutex_unlock(GST_D3D11_MEMORY_GET_LOCK(m)); \
247 struct _GstD3D11MemoryPrivate
249 ID3D11Texture2D *texture;
250 ID3D11Texture2D *staging;
252 D3D11_TEXTURE2D_DESC desc;
254 guint subresource_index;
256 ID3D11ShaderResourceView *shader_resource_view[GST_VIDEO_MAX_PLANES];
257 guint num_shader_resource_views;
259 ID3D11RenderTargetView *render_target_view[GST_VIDEO_MAX_PLANES];
260 guint num_render_target_views;
262 ID3D11VideoDecoderOutputView *decoder_output_view;
263 ID3D11VideoProcessorInputView *processor_input_view;
264 ID3D11VideoProcessorOutputView *processor_output_view;
266 D3D11_MAPPED_SUBRESOURCE map;
273 GST_DEFINE_MINI_OBJECT_TYPE (GstD3D11Memory, gst_d3d11_memory);
275 static inline D3D11_MAP
276 gst_d3d11_map_flags_to_d3d11 (GstMapFlags flags)
278 if ((flags & GST_MAP_READWRITE) == GST_MAP_READWRITE)
279 return D3D11_MAP_READ_WRITE;
280 else if ((flags & GST_MAP_WRITE) == GST_MAP_WRITE)
281 return D3D11_MAP_WRITE;
282 else if ((flags & GST_MAP_READ) == GST_MAP_READ)
283 return D3D11_MAP_READ;
285 g_assert_not_reached ();
287 return D3D11_MAP_READ;
290 static ID3D11Texture2D *
291 gst_d3d11_allocate_staging_texture (GstD3D11Device * device,
292 const D3D11_TEXTURE2D_DESC * ref)
294 D3D11_TEXTURE2D_DESC desc = { 0, };
295 ID3D11Texture2D *texture = NULL;
296 ID3D11Device *device_handle = gst_d3d11_device_get_device_handle (device);
299 desc.Width = ref->Width;
300 desc.Height = ref->Height;
302 desc.Format = ref->Format;
303 desc.SampleDesc.Count = 1;
305 desc.Usage = D3D11_USAGE_STAGING;
306 desc.CPUAccessFlags = (D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE);
308 hr = device_handle->CreateTexture2D (&desc, NULL, &texture);
309 if (!gst_d3d11_result (hr, device)) {
310 GST_ERROR_OBJECT (device, "Failed to create texture");
317 /* Must be called with d3d11 device lock */
319 gst_d3d11_memory_map_cpu_access (GstD3D11Memory * dmem, D3D11_MAP map_type)
321 GstD3D11MemoryPrivate *priv = dmem->priv;
323 ID3D11Resource *staging = (ID3D11Resource *) priv->staging;
324 ID3D11DeviceContext *device_context =
325 gst_d3d11_device_get_device_context_handle (dmem->device);
327 hr = device_context->Map (staging, 0, map_type, 0, &priv->map);
329 if (!gst_d3d11_result (hr, dmem->device)) {
330 GST_ERROR_OBJECT (GST_MEMORY_CAST (dmem)->allocator,
331 "Failed to map staging texture (0x%x)", (guint) hr);
338 /* Must be called with d3d11 device lock */
340 gst_d3d11_memory_upload (GstD3D11Memory * dmem)
342 GstD3D11MemoryPrivate *priv = dmem->priv;
343 ID3D11DeviceContext *device_context;
345 if (!priv->staging || priv->staging == priv->texture ||
346 !GST_MEMORY_FLAG_IS_SET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD))
349 device_context = gst_d3d11_device_get_device_context_handle (dmem->device);
350 device_context->CopySubresourceRegion (priv->texture, priv->subresource_index,
351 0, 0, 0, priv->staging, 0, NULL);
354 /* Must be called with d3d11 device lock */
356 gst_d3d11_memory_download (GstD3D11Memory * dmem)
358 GstD3D11MemoryPrivate *priv = dmem->priv;
359 ID3D11DeviceContext *device_context;
361 if (!priv->staging || priv->staging == priv->texture ||
362 !GST_MEMORY_FLAG_IS_SET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD))
365 device_context = gst_d3d11_device_get_device_context_handle (dmem->device);
366 device_context->CopySubresourceRegion (priv->staging, 0, 0, 0, 0,
367 priv->texture, priv->subresource_index, NULL);
371 gst_d3d11_memory_map_full (GstMemory * mem, GstMapInfo * info, gsize maxsize)
373 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
374 GstD3D11MemoryPrivate *priv = dmem->priv;
375 GstMapFlags flags = info->flags;
378 gst_d3d11_device_lock (dmem->device);
379 GST_D3D11_MEMORY_LOCK (dmem);
381 if ((flags & GST_MAP_D3D11) == GST_MAP_D3D11) {
382 gst_d3d11_memory_upload (dmem);
383 GST_MEMORY_FLAG_UNSET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD);
385 if ((flags & GST_MAP_WRITE) == GST_MAP_WRITE)
386 GST_MINI_OBJECT_FLAG_SET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
388 g_assert (priv->texture != NULL);
393 if (priv->cpu_map_count == 0) {
396 /* Allocate staging texture for CPU access */
397 if (!priv->staging) {
398 priv->staging = gst_d3d11_allocate_staging_texture (dmem->device,
400 if (!priv->staging) {
401 GST_ERROR_OBJECT (mem->allocator, "Couldn't create staging texture");
405 /* first memory, always need download to staging */
406 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
409 gst_d3d11_memory_download (dmem);
410 map_type = gst_d3d11_map_flags_to_d3d11 (flags);
412 if (!gst_d3d11_memory_map_cpu_access (dmem, map_type)) {
413 GST_ERROR_OBJECT (mem->allocator, "Couldn't map staging texture");
418 if ((flags & GST_MAP_WRITE) == GST_MAP_WRITE) {
419 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD);
422 GST_MEMORY_FLAG_UNSET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
424 priv->cpu_map_count++;
425 ret = dmem->priv->map.pData;
428 GST_D3D11_MEMORY_UNLOCK (dmem);
429 gst_d3d11_device_unlock (dmem->device);
434 /* Must be called with d3d11 device lock */
436 gst_d3d11_memory_unmap_cpu_access (GstD3D11Memory * dmem)
438 GstD3D11MemoryPrivate *priv = dmem->priv;
439 ID3D11Resource *staging = (ID3D11Resource *) priv->staging;
440 ID3D11DeviceContext *device_context =
441 gst_d3d11_device_get_device_context_handle (dmem->device);
443 device_context->Unmap (staging, 0);
447 gst_d3d11_memory_unmap_full (GstMemory * mem, GstMapInfo * info)
449 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
450 GstD3D11MemoryPrivate *priv = dmem->priv;
452 gst_d3d11_device_lock (dmem->device);
453 GST_D3D11_MEMORY_LOCK (dmem);
455 if ((info->flags & GST_MAP_D3D11) == GST_MAP_D3D11) {
456 if ((info->flags & GST_MAP_WRITE) == GST_MAP_WRITE)
457 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
462 if ((info->flags & GST_MAP_WRITE) == GST_MAP_WRITE)
463 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD);
465 priv->cpu_map_count--;
466 if (priv->cpu_map_count > 0)
469 gst_d3d11_memory_unmap_cpu_access (dmem);
472 GST_D3D11_MEMORY_UNLOCK (dmem);
473 gst_d3d11_device_unlock (dmem->device);
477 gst_d3d11_memory_share (GstMemory * mem, gssize offset, gssize size)
484 gst_d3d11_memory_update_size (GstMemory * mem)
486 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
487 GstD3D11MemoryPrivate *priv = dmem->priv;
488 gsize offset[GST_VIDEO_MAX_PLANES];
489 gint stride[GST_VIDEO_MAX_PLANES];
491 D3D11_TEXTURE2D_DESC *desc = &priv->desc;
492 gboolean ret = FALSE;
494 if (!priv->staging) {
495 priv->staging = gst_d3d11_allocate_staging_texture (dmem->device,
497 if (!priv->staging) {
498 GST_ERROR_OBJECT (mem->allocator, "Couldn't create staging texture");
502 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
505 gst_d3d11_device_lock (dmem->device);
506 if (!gst_d3d11_memory_map_cpu_access (dmem, D3D11_MAP_READ_WRITE)) {
507 GST_ERROR_OBJECT (mem->allocator, "Couldn't map staging texture");
511 gst_d3d11_memory_unmap_cpu_access (dmem);
513 if (!gst_d3d11_dxgi_format_get_size (desc->Format, desc->Width, desc->Height,
514 priv->map.RowPitch, offset, stride, &size)) {
515 GST_ERROR_OBJECT (mem->allocator, "Couldn't calculate memory size");
519 mem->maxsize = mem->size = size;
523 gst_d3d11_device_unlock (dmem->device);
528 * gst_is_d3d11_memory:
531 * Returns: whether @mem is a #GstD3D11Memory
536 gst_is_d3d11_memory (GstMemory * mem)
538 return mem != NULL && mem->allocator != NULL &&
539 (GST_IS_D3D11_ALLOCATOR (mem->allocator) ||
540 GST_IS_D3D11_POOL_ALLOCATOR (mem->allocator));
544 * gst_d3d11_memory_init_once:
546 * Initializes the Direct3D11 Texture allocator. It is safe to call
547 * this function multiple times. This must be called before any other
548 * GstD3D11Memory operation.
553 gst_d3d11_memory_init_once (void)
555 static gsize _init = 0;
557 if (g_once_init_enter (&_init)) {
559 GST_DEBUG_CATEGORY_INIT (gst_d3d11_allocator_debug, "d3d11allocator", 0,
560 "Direct3D11 Texture Allocator");
562 _d3d11_memory_allocator =
563 (GstAllocator *) g_object_new (GST_TYPE_D3D11_ALLOCATOR, NULL);
564 gst_object_ref_sink (_d3d11_memory_allocator);
566 gst_allocator_register (GST_D3D11_MEMORY_NAME, _d3d11_memory_allocator);
567 g_once_init_leave (&_init, 1);
572 * gst_d3d11_memory_get_texture_handle:
573 * @mem: a #GstD3D11Memory
575 * Returns: (transfer none): a ID3D11Texture2D handle. Caller must not release
581 gst_d3d11_memory_get_texture_handle (GstD3D11Memory * mem)
583 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
585 return mem->priv->texture;
589 * gst_d3d11_memory_get_subresource_index:
590 * @mem: a #GstD3D11Memory
592 * Returns: subresource index corresponding to @mem.
597 gst_d3d11_memory_get_subresource_index (GstD3D11Memory * mem)
599 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), 0);
601 return mem->priv->subresource_index;
605 * gst_d3d11_memory_get_texture_desc:
606 * @mem: a #GstD3D11Memory
607 * @desc: (out): a D3D11_TEXTURE2D_DESC
609 * Fill @desc with D3D11_TEXTURE2D_DESC for ID3D11Texture2D
611 * Returns: %TRUE if successeed
616 gst_d3d11_memory_get_texture_desc (GstD3D11Memory * mem,
617 D3D11_TEXTURE2D_DESC * desc)
619 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), FALSE);
620 g_return_val_if_fail (desc != NULL, FALSE);
622 *desc = mem->priv->desc;
628 gst_d3d11_memory_get_texture_stride (GstD3D11Memory * mem, guint * stride)
630 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), FALSE);
631 g_return_val_if_fail (stride != NULL, FALSE);
633 *stride = mem->priv->map.RowPitch;
639 create_shader_resource_views (GstD3D11Memory * mem)
641 GstD3D11MemoryPrivate *priv = mem->priv;
645 ID3D11Device *device_handle;
646 D3D11_SHADER_RESOURCE_VIEW_DESC resource_desc;
647 DXGI_FORMAT formats[GST_VIDEO_MAX_PLANES] = { DXGI_FORMAT_UNKNOWN, };
649 memset (&resource_desc, 0, sizeof (D3D11_SHADER_RESOURCE_VIEW_DESC));
651 device_handle = gst_d3d11_device_get_device_handle (mem->device);
653 switch (priv->desc.Format) {
654 case DXGI_FORMAT_B8G8R8A8_UNORM:
655 case DXGI_FORMAT_R8G8B8A8_UNORM:
656 case DXGI_FORMAT_R10G10B10A2_UNORM:
657 case DXGI_FORMAT_R8_UNORM:
658 case DXGI_FORMAT_R8G8_UNORM:
659 case DXGI_FORMAT_R16_UNORM:
660 case DXGI_FORMAT_R16G16_UNORM:
661 case DXGI_FORMAT_G8R8_G8B8_UNORM:
662 case DXGI_FORMAT_R8G8_B8G8_UNORM:
663 case DXGI_FORMAT_R16G16B16A16_UNORM:
665 formats[0] = priv->desc.Format;
667 case DXGI_FORMAT_AYUV:
668 case DXGI_FORMAT_YUY2:
670 formats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
672 case DXGI_FORMAT_NV12:
674 formats[0] = DXGI_FORMAT_R8_UNORM;
675 formats[1] = DXGI_FORMAT_R8G8_UNORM;
677 case DXGI_FORMAT_P010:
678 case DXGI_FORMAT_P016:
680 formats[0] = DXGI_FORMAT_R16_UNORM;
681 formats[1] = DXGI_FORMAT_R16G16_UNORM;
683 case DXGI_FORMAT_Y210:
685 formats[0] = DXGI_FORMAT_R16G16B16A16_UNORM;
687 case DXGI_FORMAT_Y410:
689 formats[0] = DXGI_FORMAT_R10G10B10A2_UNORM;
692 g_assert_not_reached ();
696 if ((priv->desc.BindFlags & D3D11_BIND_SHADER_RESOURCE) ==
697 D3D11_BIND_SHADER_RESOURCE) {
698 resource_desc.ViewDimension = D3D11_SRV_DIMENSION_TEXTURE2D;
699 resource_desc.Texture2D.MipLevels = 1;
701 for (i = 0; i < num_views; i++) {
702 resource_desc.Format = formats[i];
703 hr = device_handle->CreateShaderResourceView (priv->texture,
704 &resource_desc, &priv->shader_resource_view[i]);
706 if (!gst_d3d11_result (hr, mem->device)) {
707 GST_ERROR_OBJECT (GST_MEMORY_CAST (mem)->allocator,
708 "Failed to create %dth resource view (0x%x)", i, (guint) hr);
713 priv->num_shader_resource_views = num_views;
721 for (i = 0; i < num_views; i++)
722 GST_D3D11_CLEAR_COM (priv->shader_resource_view[i]);
724 priv->num_shader_resource_views = 0;
730 gst_d3d11_memory_ensure_shader_resource_view (GstD3D11Memory * mem)
732 GstD3D11MemoryPrivate *priv = mem->priv;
733 gboolean ret = FALSE;
735 if (!(priv->desc.BindFlags & D3D11_BIND_SHADER_RESOURCE)) {
736 GST_LOG_OBJECT (GST_MEMORY_CAST (mem)->allocator,
737 "Need BindFlags, current flag 0x%x", priv->desc.BindFlags);
741 GST_D3D11_MEMORY_LOCK (mem);
742 if (priv->num_shader_resource_views) {
747 ret = create_shader_resource_views (mem);
750 GST_D3D11_MEMORY_UNLOCK (mem);
756 * gst_d3d11_memory_get_shader_resource_view_size:
757 * @mem: a #GstD3D11Memory
759 * Returns: the number of ID3D11ShaderResourceView that can be used
760 * for processing GPU operation with @mem
765 gst_d3d11_memory_get_shader_resource_view_size (GstD3D11Memory * mem)
767 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), 0);
769 if (!gst_d3d11_memory_ensure_shader_resource_view (mem))
772 return mem->priv->num_shader_resource_views;
776 * gst_d3d11_memory_get_shader_resource_view:
777 * @mem: a #GstD3D11Memory
778 * @index: the index of the ID3D11ShaderResourceView
780 * Returns: (transfer none) (nullable): a pointer to the
781 * ID3D11ShaderResourceView or %NULL if ID3D11ShaderResourceView is unavailable
786 ID3D11ShaderResourceView *
787 gst_d3d11_memory_get_shader_resource_view (GstD3D11Memory * mem, guint index)
789 GstD3D11MemoryPrivate *priv;
791 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
793 if (!gst_d3d11_memory_ensure_shader_resource_view (mem))
798 if (index >= priv->num_shader_resource_views) {
799 GST_ERROR ("Invalid SRV index %d", index);
803 return priv->shader_resource_view[index];
807 create_render_target_views (GstD3D11Memory * mem)
809 GstD3D11MemoryPrivate *priv = mem->priv;
813 ID3D11Device *device_handle;
814 D3D11_RENDER_TARGET_VIEW_DESC render_desc;
815 DXGI_FORMAT formats[GST_VIDEO_MAX_PLANES] = { DXGI_FORMAT_UNKNOWN, };
817 memset (&render_desc, 0, sizeof (D3D11_RENDER_TARGET_VIEW_DESC));
819 device_handle = gst_d3d11_device_get_device_handle (mem->device);
821 switch (priv->desc.Format) {
822 case DXGI_FORMAT_B8G8R8A8_UNORM:
823 case DXGI_FORMAT_R8G8B8A8_UNORM:
824 case DXGI_FORMAT_R10G10B10A2_UNORM:
825 case DXGI_FORMAT_R8_UNORM:
826 case DXGI_FORMAT_R8G8_UNORM:
827 case DXGI_FORMAT_R16_UNORM:
828 case DXGI_FORMAT_R16G16_UNORM:
830 formats[0] = priv->desc.Format;
832 case DXGI_FORMAT_AYUV:
834 formats[0] = DXGI_FORMAT_R8G8B8A8_UNORM;
836 case DXGI_FORMAT_NV12:
838 formats[0] = DXGI_FORMAT_R8_UNORM;
839 formats[1] = DXGI_FORMAT_R8G8_UNORM;
841 case DXGI_FORMAT_P010:
842 case DXGI_FORMAT_P016:
844 formats[0] = DXGI_FORMAT_R16_UNORM;
845 formats[1] = DXGI_FORMAT_R16G16_UNORM;
848 g_assert_not_reached ();
852 if ((priv->desc.BindFlags & D3D11_BIND_RENDER_TARGET) ==
853 D3D11_BIND_RENDER_TARGET) {
854 render_desc.ViewDimension = D3D11_RTV_DIMENSION_TEXTURE2D;
855 render_desc.Texture2D.MipSlice = 0;
857 for (i = 0; i < num_views; i++) {
858 render_desc.Format = formats[i];
860 hr = device_handle->CreateRenderTargetView (priv->texture, &render_desc,
861 &priv->render_target_view[i]);
862 if (!gst_d3d11_result (hr, mem->device)) {
863 GST_ERROR_OBJECT (GST_MEMORY_CAST (mem)->allocator,
864 "Failed to create %dth render target view (0x%x)", i, (guint) hr);
869 priv->num_render_target_views = num_views;
877 for (i = 0; i < num_views; i++)
878 GST_D3D11_CLEAR_COM (priv->render_target_view[i]);
880 priv->num_render_target_views = 0;
886 gst_d3d11_memory_ensure_render_target_view (GstD3D11Memory * mem)
888 GstD3D11MemoryPrivate *priv = mem->priv;
889 gboolean ret = FALSE;
891 if (!(priv->desc.BindFlags & D3D11_BIND_RENDER_TARGET)) {
892 GST_WARNING_OBJECT (GST_MEMORY_CAST (mem)->allocator,
893 "Need BindFlags, current flag 0x%x", priv->desc.BindFlags);
897 GST_D3D11_MEMORY_LOCK (mem);
898 if (priv->num_render_target_views) {
903 ret = create_render_target_views (mem);
906 GST_D3D11_MEMORY_UNLOCK (mem);
912 * gst_d3d11_memory_get_render_target_view_size:
913 * @mem: a #GstD3D11Memory
915 * Returns: the number of ID3D11RenderTargetView that can be used
916 * for processing GPU operation with @mem
921 gst_d3d11_memory_get_render_target_view_size (GstD3D11Memory * mem)
923 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), 0);
925 if (!gst_d3d11_memory_ensure_render_target_view (mem))
928 return mem->priv->num_render_target_views;
932 * gst_d3d11_memory_get_render_target_view:
933 * @mem: a #GstD3D11Memory
934 * @index: the index of the ID3D11RenderTargetView
936 * Returns: (transfer none) (nullable): a pointer to the
937 * ID3D11RenderTargetView or %NULL if ID3D11RenderTargetView is unavailable
942 ID3D11RenderTargetView *
943 gst_d3d11_memory_get_render_target_view (GstD3D11Memory * mem, guint index)
945 GstD3D11MemoryPrivate *priv;
947 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
949 if (!gst_d3d11_memory_ensure_render_target_view (mem))
954 if (index >= priv->num_render_target_views) {
955 GST_ERROR ("Invalid RTV index %d", index);
959 return priv->render_target_view[index];
963 gst_d3d11_memory_ensure_decoder_output_view (GstD3D11Memory * mem,
964 ID3D11VideoDevice * video_device, GUID * decoder_profile)
966 GstD3D11MemoryPrivate *dmem_priv = mem->priv;
967 GstD3D11Allocator *allocator;
968 D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC desc;
970 gboolean ret = FALSE;
972 allocator = GST_D3D11_ALLOCATOR (GST_MEMORY_CAST (mem)->allocator);
974 if (!(dmem_priv->desc.BindFlags & D3D11_BIND_DECODER)) {
975 GST_LOG_OBJECT (allocator,
976 "Need BindFlags, current flag 0x%x", dmem_priv->desc.BindFlags);
980 GST_D3D11_MEMORY_LOCK (mem);
981 if (dmem_priv->decoder_output_view) {
982 dmem_priv->decoder_output_view->GetDesc (&desc);
983 if (IsEqualGUID (desc.DecodeProfile, *decoder_profile)) {
986 /* Shouldn't happen, but try again anyway */
987 GST_WARNING_OBJECT (allocator,
988 "Existing view has different decoder profile");
989 GST_D3D11_CLEAR_COM (dmem_priv->decoder_output_view);
993 if (dmem_priv->decoder_output_view)
996 desc.DecodeProfile = *decoder_profile;
997 desc.ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D;
998 desc.Texture2D.ArraySlice = dmem_priv->subresource_index;
1000 hr = video_device->CreateVideoDecoderOutputView (dmem_priv->texture, &desc,
1001 &dmem_priv->decoder_output_view);
1002 if (!gst_d3d11_result (hr, mem->device)) {
1003 GST_ERROR_OBJECT (allocator,
1004 "Could not create decoder output view, hr: 0x%x", (guint) hr);
1012 GST_D3D11_MEMORY_UNLOCK (mem);
1018 * gst_d3d11_memory_get_decoder_output_view:
1019 * @mem: a #GstD3D11Memory
1021 * Returns: (transfer none) (nullable): a pointer to the
1022 * ID3D11VideoDecoderOutputView or %NULL if ID3D11VideoDecoderOutputView is
1027 ID3D11VideoDecoderOutputView *
1028 gst_d3d11_memory_get_decoder_output_view (GstD3D11Memory * mem,
1029 ID3D11VideoDevice * video_device, GUID * decoder_profile)
1031 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1032 g_return_val_if_fail (video_device != NULL, NULL);
1033 g_return_val_if_fail (decoder_profile != NULL, NULL);
1035 if (!gst_d3d11_memory_ensure_decoder_output_view (mem,
1036 video_device, decoder_profile))
1039 return mem->priv->decoder_output_view;
1043 check_bind_flags_for_processor_input_view (guint bind_flags)
1045 static const guint compatible_flags = (D3D11_BIND_DECODER |
1046 D3D11_BIND_VIDEO_ENCODER | D3D11_BIND_RENDER_TARGET |
1047 D3D11_BIND_UNORDERED_ACCESS);
1049 if (bind_flags == 0)
1052 if ((bind_flags & compatible_flags) != 0)
1059 gst_d3d11_memory_ensure_processor_input_view (GstD3D11Memory * mem,
1060 ID3D11VideoDevice * video_device,
1061 ID3D11VideoProcessorEnumerator * enumerator)
1063 GstD3D11MemoryPrivate *dmem_priv = mem->priv;
1064 GstD3D11Allocator *allocator;
1065 D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC desc;
1067 gboolean ret = FALSE;
1069 allocator = GST_D3D11_ALLOCATOR (GST_MEMORY_CAST (mem)->allocator);
1071 if (!check_bind_flags_for_processor_input_view (dmem_priv->desc.BindFlags)) {
1072 GST_LOG_OBJECT (allocator,
1073 "Need BindFlags, current flag 0x%x", dmem_priv->desc.BindFlags);
1077 GST_D3D11_MEMORY_LOCK (mem);
1078 if (dmem_priv->processor_input_view)
1082 desc.ViewDimension = D3D11_VPIV_DIMENSION_TEXTURE2D;
1083 desc.Texture2D.MipSlice = 0;
1084 desc.Texture2D.ArraySlice = dmem_priv->subresource_index;
1086 hr = video_device->CreateVideoProcessorInputView (dmem_priv->texture,
1087 enumerator, &desc, &dmem_priv->processor_input_view);
1088 if (!gst_d3d11_result (hr, mem->device)) {
1089 GST_ERROR_OBJECT (allocator,
1090 "Could not create processor input view, hr: 0x%x", (guint) hr);
1098 GST_D3D11_MEMORY_UNLOCK (mem);
1104 * gst_d3d11_memory_get_processor_input_view:
1105 * @mem: a #GstD3D11Memory
1106 * @video_device: a #ID3D11VideoDevice
1107 * @enumerator: a #ID3D11VideoProcessorEnumerator
1109 * Returns: (transfer none) (nullable): a pointer to the
1110 * ID3D11VideoProcessorInputView or %NULL if ID3D11VideoProcessorInputView is
1115 ID3D11VideoProcessorInputView *
1116 gst_d3d11_memory_get_processor_input_view (GstD3D11Memory * mem,
1117 ID3D11VideoDevice * video_device,
1118 ID3D11VideoProcessorEnumerator * enumerator)
1120 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1121 g_return_val_if_fail (video_device != NULL, NULL);
1122 g_return_val_if_fail (enumerator != NULL, NULL);
1124 if (!gst_d3d11_memory_ensure_processor_input_view (mem, video_device,
1128 return mem->priv->processor_input_view;
1132 gst_d3d11_memory_ensure_processor_output_view (GstD3D11Memory * mem,
1133 ID3D11VideoDevice * video_device,
1134 ID3D11VideoProcessorEnumerator * enumerator)
1136 GstD3D11MemoryPrivate *priv = mem->priv;
1137 GstD3D11Allocator *allocator;
1138 D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC desc;
1142 memset (&desc, 0, sizeof (D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC));
1144 allocator = GST_D3D11_ALLOCATOR (GST_MEMORY_CAST (mem)->allocator);
1146 if (!(priv->desc.BindFlags & D3D11_BIND_RENDER_TARGET)) {
1147 GST_LOG_OBJECT (allocator,
1148 "Need BindFlags, current flag 0x%x", priv->desc.BindFlags);
1152 /* FIXME: texture array should be supported at some point */
1153 if (priv->subresource_index != 0) {
1154 GST_FIXME_OBJECT (allocator,
1155 "Texture array is not suppoted for processor output view");
1159 GST_D3D11_MEMORY_LOCK (mem);
1160 if (priv->processor_output_view)
1163 desc.ViewDimension = D3D11_VPOV_DIMENSION_TEXTURE2D;
1164 desc.Texture2D.MipSlice = 0;
1166 hr = video_device->CreateVideoProcessorOutputView (priv->texture,
1167 enumerator, &desc, &priv->processor_output_view);
1168 if (!gst_d3d11_result (hr, mem->device)) {
1169 GST_ERROR_OBJECT (allocator,
1170 "Could not create processor input view, hr: 0x%x", (guint) hr);
1178 GST_D3D11_MEMORY_UNLOCK (mem);
1184 * gst_d3d11_memory_get_processor_output_view:
1185 * @mem: a #GstD3D11Memory
1186 * @video_device: a #ID3D11VideoDevice
1187 * @enumerator: a #ID3D11VideoProcessorEnumerator
1189 * Returns: (transfer none) (nullable): a pointer to the
1190 * ID3D11VideoProcessorOutputView or %NULL if ID3D11VideoProcessorOutputView is
1195 ID3D11VideoProcessorOutputView *
1196 gst_d3d11_memory_get_processor_output_view (GstD3D11Memory * mem,
1197 ID3D11VideoDevice * video_device,
1198 ID3D11VideoProcessorEnumerator * enumerator)
1200 g_return_val_if_fail (gst_is_d3d11_memory (GST_MEMORY_CAST (mem)), NULL);
1201 g_return_val_if_fail (video_device != NULL, NULL);
1202 g_return_val_if_fail (enumerator != NULL, NULL);
1204 if (!gst_d3d11_memory_ensure_processor_output_view (mem, video_device,
1208 return mem->priv->processor_output_view;
1211 /* GstD3D11Allocator */
1212 struct _GstD3D11AllocatorPrivate
1214 GstMemoryCopyFunction fallback_copy;
1217 #define gst_d3d11_allocator_parent_class alloc_parent_class
1218 G_DEFINE_TYPE_WITH_PRIVATE (GstD3D11Allocator,
1219 gst_d3d11_allocator, GST_TYPE_ALLOCATOR);
1221 static GstMemory *gst_d3d11_allocator_dummy_alloc (GstAllocator * allocator,
1222 gsize size, GstAllocationParams * params);
1223 static GstMemory *gst_d3d11_allocator_alloc_internal (GstD3D11Allocator * self,
1224 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc);
1225 static void gst_d3d11_allocator_free (GstAllocator * allocator,
1229 gst_d3d11_allocator_class_init (GstD3D11AllocatorClass * klass)
1231 GstAllocatorClass *allocator_class = GST_ALLOCATOR_CLASS (klass);
1233 allocator_class->alloc = gst_d3d11_allocator_dummy_alloc;
1234 allocator_class->free = gst_d3d11_allocator_free;
1238 gst_d3d11_memory_copy (GstMemory * mem, gssize offset, gssize size)
1240 GstD3D11Allocator *alloc = GST_D3D11_ALLOCATOR (mem->allocator);
1241 GstD3D11AllocatorPrivate *priv = alloc->priv;
1242 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
1243 GstD3D11Memory *copy_dmem;
1244 GstD3D11Device *device = dmem->device;
1245 ID3D11Device *device_handle = gst_d3d11_device_get_device_handle (device);
1246 ID3D11DeviceContext *device_context =
1247 gst_d3d11_device_get_device_context_handle (device);
1248 D3D11_TEXTURE2D_DESC dst_desc = { 0, };
1249 D3D11_TEXTURE2D_DESC src_desc = { 0, };
1250 GstMemory *copy = NULL;
1253 UINT bind_flags = 0;
1254 UINT supported_flags = 0;
1256 /* non-zero offset or different size is not supported */
1257 if (offset != 0 || (size != -1 && (gsize) size != mem->size)) {
1258 GST_DEBUG_OBJECT (alloc, "Different size/offset, try fallback copy");
1259 return priv->fallback_copy (mem, offset, size);
1262 gst_d3d11_device_lock (device);
1263 if (!gst_memory_map (mem, &info,
1264 (GstMapFlags) (GST_MAP_READ | GST_MAP_D3D11))) {
1265 gst_d3d11_device_unlock (device);
1267 GST_WARNING_OBJECT (alloc, "Failed to map memory, try fallback copy");
1269 return priv->fallback_copy (mem, offset, size);
1272 dmem->priv->texture->GetDesc (&src_desc);
1273 dst_desc.Width = src_desc.Width;
1274 dst_desc.Height = src_desc.Height;
1275 dst_desc.MipLevels = 1;
1276 dst_desc.Format = src_desc.Format;
1277 dst_desc.SampleDesc.Count = 1;
1278 dst_desc.ArraySize = 1;
1279 dst_desc.Usage = D3D11_USAGE_DEFAULT;
1281 /* If supported, use bind flags for SRV/RTV */
1282 hr = device_handle->CheckFormatSupport (src_desc.Format, &supported_flags);
1283 if (gst_d3d11_result (hr, device)) {
1284 if ((supported_flags & D3D11_FORMAT_SUPPORT_SHADER_SAMPLE) ==
1285 D3D11_FORMAT_SUPPORT_SHADER_SAMPLE) {
1286 bind_flags |= D3D11_BIND_SHADER_RESOURCE;
1289 if ((supported_flags & D3D11_FORMAT_SUPPORT_RENDER_TARGET) ==
1290 D3D11_FORMAT_SUPPORT_RENDER_TARGET) {
1291 bind_flags |= D3D11_BIND_RENDER_TARGET;
1295 copy = gst_d3d11_allocator_alloc_internal (alloc, device, &dst_desc);
1297 gst_memory_unmap (mem, &info);
1298 gst_d3d11_device_unlock (device);
1300 GST_WARNING_OBJECT (alloc,
1301 "Failed to allocate new d3d11 map memory, try fallback copy");
1303 return priv->fallback_copy (mem, offset, size);
1306 copy_dmem = GST_D3D11_MEMORY_CAST (copy);
1307 device_context->CopySubresourceRegion (copy_dmem->priv->texture, 0, 0, 0, 0,
1308 dmem->priv->texture, dmem->priv->subresource_index, NULL);
1309 copy->maxsize = copy->size = mem->maxsize;
1310 gst_memory_unmap (mem, &info);
1311 gst_d3d11_device_unlock (device);
1313 /* newly allocated memory holds valid image data. We need download this
1314 * pixel data into staging memory for CPU access */
1315 GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
1321 gst_d3d11_allocator_init (GstD3D11Allocator * allocator)
1323 GstAllocator *alloc = GST_ALLOCATOR_CAST (allocator);
1324 GstD3D11AllocatorPrivate *priv;
1326 priv = allocator->priv = (GstD3D11AllocatorPrivate *)
1327 gst_d3d11_allocator_get_instance_private (allocator);
1329 alloc->mem_type = GST_D3D11_MEMORY_NAME;
1330 alloc->mem_map_full = gst_d3d11_memory_map_full;
1331 alloc->mem_unmap_full = gst_d3d11_memory_unmap_full;
1332 alloc->mem_share = gst_d3d11_memory_share;
1334 /* Store pointer to default mem_copy method for fallback copy */
1335 priv->fallback_copy = alloc->mem_copy;
1336 alloc->mem_copy = gst_d3d11_memory_copy;
1338 GST_OBJECT_FLAG_SET (alloc, GST_ALLOCATOR_FLAG_CUSTOM_ALLOC);
1342 gst_d3d11_allocator_dummy_alloc (GstAllocator * allocator, gsize size,
1343 GstAllocationParams * params)
1345 g_return_val_if_reached (NULL);
1349 gst_d3d11_allocator_free (GstAllocator * allocator, GstMemory * mem)
1351 GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
1352 GstD3D11MemoryPrivate *dmem_priv = dmem->priv;
1355 GST_LOG_OBJECT (allocator, "Free memory %p", mem);
1357 for (i = 0; i < GST_VIDEO_MAX_PLANES; i++) {
1358 GST_D3D11_CLEAR_COM (dmem_priv->render_target_view[i]);
1359 GST_D3D11_CLEAR_COM (dmem_priv->shader_resource_view[i]);
1362 GST_D3D11_CLEAR_COM (dmem_priv->decoder_output_view);
1363 GST_D3D11_CLEAR_COM (dmem_priv->processor_input_view);
1364 GST_D3D11_CLEAR_COM (dmem_priv->processor_output_view);
1365 GST_D3D11_CLEAR_COM (dmem_priv->texture);
1366 GST_D3D11_CLEAR_COM (dmem_priv->staging);
1368 gst_clear_object (&dmem->device);
1369 g_mutex_clear (&dmem_priv->lock);
1370 g_free (dmem->priv);
1375 gst_d3d11_allocator_alloc_wrapped (GstD3D11Allocator * self,
1376 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc,
1377 ID3D11Texture2D * texture)
1379 GstD3D11Memory *mem;
1381 mem = g_new0 (GstD3D11Memory, 1);
1382 mem->priv = g_new0 (GstD3D11MemoryPrivate, 1);
1384 gst_memory_init (GST_MEMORY_CAST (mem),
1385 (GstMemoryFlags) 0, GST_ALLOCATOR_CAST (self), NULL, 0, 0, 0, 0);
1386 g_mutex_init (&mem->priv->lock);
1387 mem->priv->texture = texture;
1388 mem->priv->desc = *desc;
1389 mem->device = (GstD3D11Device *) gst_object_ref (device);
1391 /* This is staging texture as well */
1392 if (desc->Usage == D3D11_USAGE_STAGING) {
1393 mem->priv->staging = texture;
1397 return GST_MEMORY_CAST (mem);
1401 gst_d3d11_allocator_alloc_internal (GstD3D11Allocator * self,
1402 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc)
1404 ID3D11Texture2D *texture = NULL;
1405 ID3D11Device *device_handle;
1408 device_handle = gst_d3d11_device_get_device_handle (device);
1410 hr = device_handle->CreateTexture2D (desc, NULL, &texture);
1411 if (!gst_d3d11_result (hr, device)) {
1412 GST_ERROR_OBJECT (self, "Couldn't create texture");
1416 return gst_d3d11_allocator_alloc_wrapped (self, device, desc, texture);
1420 * gst_d3d11_allocator_alloc:
1421 * @allocator: a #GstD3D11Allocator
1422 * @device: a #GstD3D11Device
1423 * @desc: a D3D11_TEXTURE2D_DESC struct
1425 * Returns: a newly allocated #GstD3D11Memory with given parameters.
1430 gst_d3d11_allocator_alloc (GstD3D11Allocator * allocator,
1431 GstD3D11Device * device, const D3D11_TEXTURE2D_DESC * desc)
1435 g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), NULL);
1436 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), NULL);
1437 g_return_val_if_fail (desc != NULL, NULL);
1439 mem = gst_d3d11_allocator_alloc_internal (allocator, device, desc);
1443 if (!gst_d3d11_memory_update_size (mem)) {
1444 GST_ERROR_OBJECT (allocator, "Failed to calculate size");
1445 gst_memory_unref (mem);
1453 gst_d3d11_allocator_set_active (GstD3D11Allocator * allocator, gboolean active)
1455 GstD3D11AllocatorClass *klass;
1457 g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), FALSE);
1459 klass = GST_D3D11_ALLOCATOR_GET_CLASS (allocator);
1460 if (klass->set_actvie)
1461 return klass->set_actvie (allocator, active);
1466 /* GstD3D11PoolAllocator */
1467 #define GST_D3D11_POOL_ALLOCATOR_LOCK(alloc) (g_rec_mutex_lock(&alloc->priv->lock))
1468 #define GST_D3D11_POOL_ALLOCATOR_UNLOCK(alloc) (g_rec_mutex_unlock(&alloc->priv->lock))
1469 #define GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING(alloc) (g_atomic_int_get (&alloc->priv->flushing))
1471 struct _GstD3D11PoolAllocatorPrivate
1473 /* parent texture when array typed memory is used */
1474 ID3D11Texture2D *texture;
1475 D3D11_TEXTURE2D_DESC desc;
1477 /* All below member variables are analogous to that of GstBufferPool */
1478 GstAtomicQueue *queue;
1481 /* This lock will protect all below variables apart from atomic ones
1482 * (identical to GstBufferPool::priv::rec_lock) */
1493 /* Calculated memory size, based on Direct3D11 staging texture map.
1494 * Note that, we cannot know the actually staging texture memory size prior
1495 * to map the staging texture because driver will likely require padding */
1499 static void gst_d3d11_pool_allocator_dispose (GObject * object);
1500 static void gst_d3d11_pool_allocator_finalize (GObject * object);
1503 gst_d3d11_pool_allocator_set_active (GstD3D11Allocator * allocator,
1506 static gboolean gst_d3d11_pool_allocator_start (GstD3D11PoolAllocator * self);
1507 static gboolean gst_d3d11_pool_allocator_stop (GstD3D11PoolAllocator * self);
1508 static gboolean gst_d3d11_memory_release (GstMiniObject * mini_object);
1510 #define gst_d3d11_pool_allocator_parent_class pool_alloc_parent_class
1511 G_DEFINE_TYPE_WITH_PRIVATE (GstD3D11PoolAllocator,
1512 gst_d3d11_pool_allocator, GST_TYPE_D3D11_ALLOCATOR);
1515 gst_d3d11_pool_allocator_class_init (GstD3D11PoolAllocatorClass * klass)
1517 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
1518 GstD3D11AllocatorClass *d3d11alloc_class = GST_D3D11_ALLOCATOR_CLASS (klass);
1520 gobject_class->dispose = gst_d3d11_pool_allocator_dispose;
1521 gobject_class->finalize = gst_d3d11_pool_allocator_finalize;
1523 d3d11alloc_class->set_actvie = gst_d3d11_pool_allocator_set_active;
1527 gst_d3d11_pool_allocator_init (GstD3D11PoolAllocator * allocator)
1529 GstD3D11PoolAllocatorPrivate *priv;
1531 priv = allocator->priv = (GstD3D11PoolAllocatorPrivate *)
1532 gst_d3d11_pool_allocator_get_instance_private (allocator);
1533 g_rec_mutex_init (&priv->lock);
1535 priv->poll = gst_poll_new_timer ();
1536 priv->queue = gst_atomic_queue_new (16);
1538 priv->active = FALSE;
1539 priv->started = FALSE;
1541 /* 1 control write for flushing - the flush token */
1542 gst_poll_write_control (priv->poll);
1543 /* 1 control write for marking that we are not waiting for poll - the wait token */
1544 gst_poll_write_control (priv->poll);
1548 gst_d3d11_pool_allocator_dispose (GObject * object)
1550 GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (object);
1552 gst_clear_object (&self->device);
1554 G_OBJECT_CLASS (pool_alloc_parent_class)->dispose (object);
1558 gst_d3d11_pool_allocator_finalize (GObject * object)
1560 GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (object);
1561 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1563 GST_DEBUG_OBJECT (self, "Finalize");
1565 gst_d3d11_pool_allocator_stop (self);
1566 gst_atomic_queue_unref (priv->queue);
1567 gst_poll_free (priv->poll);
1568 g_rec_mutex_clear (&priv->lock);
1570 GST_D3D11_CLEAR_COM (priv->texture);
1572 G_OBJECT_CLASS (pool_alloc_parent_class)->finalize (object);
1576 gst_d3d11_pool_allocator_start (GstD3D11PoolAllocator * self)
1578 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1579 ID3D11Device *device_handle;
1587 if (priv->desc.ArraySize == 1) {
1588 priv->started = TRUE;
1592 device_handle = gst_d3d11_device_get_device_handle (self->device);
1594 if (!priv->texture) {
1595 hr = device_handle->CreateTexture2D (&priv->desc, NULL, &priv->texture);
1596 if (!gst_d3d11_result (hr, self->device)) {
1597 GST_ERROR_OBJECT (self, "Failed to allocate texture");
1602 /* Pre-allocate memory objects */
1603 for (i = 0; i < priv->desc.ArraySize; i++) {
1606 priv->texture->AddRef ();
1608 gst_d3d11_allocator_alloc_wrapped (GST_D3D11_ALLOCATOR_CAST
1609 (_d3d11_memory_allocator), self->device, &priv->desc, priv->texture);
1612 if (!gst_d3d11_memory_update_size (mem)) {
1613 GST_ERROR_OBJECT (self, "Failed to calculate memory size");
1614 gst_memory_unref (mem);
1618 priv->mem_size = mem->size;
1620 mem->size = mem->maxsize = priv->mem_size;
1623 GST_D3D11_MEMORY_CAST (mem)->priv->subresource_index = i;
1625 g_atomic_int_add (&priv->cur_mems, 1);
1626 gst_atomic_queue_push (priv->queue, mem);
1627 gst_poll_write_control (priv->poll);
1630 priv->started = TRUE;
1636 gst_d3d11_pool_allocator_do_set_flushing (GstD3D11PoolAllocator * self,
1639 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1641 if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self) == flushing)
1645 g_atomic_int_set (&priv->flushing, 1);
1646 /* Write the flush token to wake up any waiters */
1647 gst_poll_write_control (priv->poll);
1649 while (!gst_poll_read_control (priv->poll)) {
1650 if (errno == EWOULDBLOCK) {
1651 /* This should not really happen unless flushing and unflushing
1652 * happens on different threads. Let's wait a bit to get back flush
1653 * token from the thread that was setting it to flushing */
1657 /* Critical error but GstPoll already complained */
1662 g_atomic_int_set (&priv->flushing, 0);
1667 gst_d3d11_pool_allocator_set_active (GstD3D11Allocator * allocator,
1670 GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (allocator);
1671 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1673 GST_LOG_OBJECT (self, "active %d", active);
1675 GST_D3D11_POOL_ALLOCATOR_LOCK (self);
1676 /* just return if we are already in the right state */
1677 if (priv->active == active)
1681 if (!gst_d3d11_pool_allocator_start (self))
1684 /* flush_stop may release memory objects, setting to active to avoid running
1685 * do_stop while activating the pool */
1686 priv->active = TRUE;
1688 gst_d3d11_pool_allocator_do_set_flushing (self, FALSE);
1692 /* set to flushing first */
1693 gst_d3d11_pool_allocator_do_set_flushing (self, TRUE);
1695 /* when all memory objects are in the pool, free them. Else they will be
1696 * freed when they are released */
1697 outstanding = g_atomic_int_get (&priv->outstanding);
1698 GST_LOG_OBJECT (self, "outstanding memories %d, (in queue %d)",
1699 outstanding, gst_atomic_queue_length (priv->queue));
1700 if (outstanding == 0) {
1701 if (!gst_d3d11_pool_allocator_stop (self))
1705 priv->active = FALSE;
1708 GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1714 GST_DEBUG_OBJECT (self, "allocator was in the right state");
1715 GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1720 GST_ERROR_OBJECT (self, "start failed");
1721 GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1726 GST_ERROR_OBJECT (self, "stop failed");
1727 GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1733 gst_d3d11_pool_allocator_free_memory (GstD3D11PoolAllocator * self,
1736 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1738 g_atomic_int_add (&priv->cur_mems, -1);
1739 GST_LOG_OBJECT (self, "freeing memory %p (%u left)", mem, priv->cur_mems);
1741 GST_MINI_OBJECT_CAST (mem)->dispose = NULL;
1742 gst_memory_unref (mem);
1745 /* must be called with the lock */
1747 gst_d3d11_pool_allocator_clear_queue (GstD3D11PoolAllocator * self)
1749 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1752 GST_LOG_OBJECT (self, "Clearing queue");
1754 /* clear the pool */
1755 while ((memory = (GstMemory *) gst_atomic_queue_pop (priv->queue))) {
1756 while (!gst_poll_read_control (priv->poll)) {
1757 if (errno == EWOULDBLOCK) {
1758 /* We put the memory into the queue but did not finish writing control
1759 * yet, let's wait a bit and retry */
1763 /* Critical error but GstPoll already complained */
1767 gst_d3d11_pool_allocator_free_memory (self, memory);
1770 GST_LOG_OBJECT (self, "Clear done");
1772 return priv->cur_mems == 0;
1775 /* must be called with the lock */
1777 gst_d3d11_pool_allocator_stop (GstD3D11PoolAllocator * self)
1779 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1781 GST_DEBUG_OBJECT (self, "Stop");
1783 if (priv->started) {
1784 if (!gst_d3d11_pool_allocator_clear_queue (self))
1787 priv->started = FALSE;
1789 GST_DEBUG_OBJECT (self, "Wasn't started");
1796 dec_outstanding (GstD3D11PoolAllocator * self)
1798 if (g_atomic_int_dec_and_test (&self->priv->outstanding)) {
1799 /* all memory objects are returned to the pool, see if we need to free them */
1800 if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)) {
1801 /* take the lock so that set_active is not run concurrently */
1802 GST_D3D11_POOL_ALLOCATOR_LOCK (self);
1803 /* now that we have the lock, check if we have been de-activated with
1804 * outstanding buffers */
1805 if (!self->priv->active)
1806 gst_d3d11_pool_allocator_stop (self);
1808 GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
1814 gst_d3d11_pool_allocator_release_memory (GstD3D11PoolAllocator * self,
1817 GST_LOG_OBJECT (self, "Released memory %p", mem);
1819 GST_MINI_OBJECT_CAST (mem)->dispose = NULL;
1820 mem->allocator = (GstAllocator *) gst_object_ref (_d3d11_memory_allocator);
1821 gst_object_unref (self);
1823 /* keep it around in our queue */
1824 gst_atomic_queue_push (self->priv->queue, mem);
1825 gst_poll_write_control (self->priv->poll);
1826 dec_outstanding (self);
1830 gst_d3d11_memory_release (GstMiniObject * mini_object)
1832 GstMemory *mem = GST_MEMORY_CAST (mini_object);
1833 GstD3D11PoolAllocator *alloc;
1835 g_assert (mem->allocator != NULL);
1837 if (!GST_IS_D3D11_POOL_ALLOCATOR (mem->allocator)) {
1838 GST_LOG_OBJECT (mem->allocator, "Not our memory, free");
1842 alloc = GST_D3D11_POOL_ALLOCATOR (mem->allocator);
1843 /* if flushing, free this memory */
1844 if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (alloc)) {
1845 GST_LOG_OBJECT (alloc, "allocator is flushing, free %p", mem);
1849 /* return the memory to the allocator */
1850 gst_memory_ref (mem);
1851 gst_d3d11_pool_allocator_release_memory (alloc, mem);
1856 static GstFlowReturn
1857 gst_d3d11_pool_allocator_alloc (GstD3D11PoolAllocator * self, GstMemory ** mem)
1859 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1862 /* we allcates texture array during start */
1863 if (priv->desc.ArraySize > 1)
1864 return GST_FLOW_EOS;
1866 /* increment the allocation counter */
1867 g_atomic_int_add (&priv->cur_mems, 1);
1869 gst_d3d11_allocator_alloc_internal (GST_D3D11_ALLOCATOR_CAST
1870 (_d3d11_memory_allocator), self->device, &priv->desc);
1872 GST_ERROR_OBJECT (self, "Failed to allocate new memory");
1873 g_atomic_int_add (&priv->cur_mems, -1);
1874 return GST_FLOW_ERROR;
1877 if (!priv->mem_size) {
1878 if (!gst_d3d11_memory_update_size (new_mem)) {
1879 GST_ERROR_OBJECT (self, "Failed to calculate size");
1880 gst_memory_unref (new_mem);
1881 g_atomic_int_add (&priv->cur_mems, -1);
1883 return GST_FLOW_ERROR;
1886 priv->mem_size = new_mem->size;
1889 new_mem->size = new_mem->maxsize = priv->mem_size;
1896 static GstFlowReturn
1897 gst_d3d11_pool_allocator_acquire_memory_internal (GstD3D11PoolAllocator * self,
1898 GstMemory ** memory)
1900 GstFlowReturn result;
1901 GstD3D11PoolAllocatorPrivate *priv = self->priv;
1904 if (G_UNLIKELY (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)))
1907 /* try to get a memory from the queue */
1908 *memory = (GstMemory *) gst_atomic_queue_pop (priv->queue);
1909 if (G_LIKELY (*memory)) {
1910 while (!gst_poll_read_control (priv->poll)) {
1911 if (errno == EWOULDBLOCK) {
1912 /* We put the memory into the queue but did not finish writing control
1913 * yet, let's wait a bit and retry */
1917 /* Critical error but GstPoll already complained */
1921 result = GST_FLOW_OK;
1922 GST_LOG_OBJECT (self, "acquired memory %p", *memory);
1926 /* no memory, try to allocate some more */
1927 GST_LOG_OBJECT (self, "no memory, trying to allocate");
1928 result = gst_d3d11_pool_allocator_alloc (self, memory);
1929 if (G_LIKELY (result == GST_FLOW_OK))
1930 /* we have a memory, return it */
1933 if (G_UNLIKELY (result != GST_FLOW_EOS))
1934 /* something went wrong, return error */
1937 /* now we release the control socket, we wait for a memory release or
1939 if (!gst_poll_read_control (priv->poll)) {
1940 if (errno == EWOULDBLOCK) {
1941 /* This means that we have two threads trying to allocate memory
1942 * already, and the other one already got the wait token. This
1943 * means that we only have to wait for the poll now and not write the
1944 * token afterwards: we will be woken up once the other thread is
1945 * woken up and that one will write the wait token it removed */
1946 GST_LOG_OBJECT (self, "waiting for free memory or flushing");
1947 gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
1949 /* This is a critical error, GstPoll already gave a warning */
1950 result = GST_FLOW_ERROR;
1954 /* We're the first thread waiting, we got the wait token and have to
1955 * write it again later
1957 * We're a second thread and just consumed the flush token and block all
1958 * other threads, in which case we must not wait and give it back
1960 if (!GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)) {
1961 GST_LOG_OBJECT (self, "waiting for free memory or flushing");
1962 gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
1964 gst_poll_write_control (priv->poll);
1973 GST_DEBUG_OBJECT (self, "we are flushing");
1974 return GST_FLOW_FLUSHING;
1979 * gst_d3d11_pool_allocator_new:
1980 * @device: a #GstD3D11Device
1981 * @desc: a D3D11_TEXTURE2D_DESC for texture allocation
1983 * Creates a new #GstD3D11PoolAllocator instance.
1985 * Returns: (transfer full): a new #GstD3D11PoolAllocator instance
1987 GstD3D11PoolAllocator *
1988 gst_d3d11_pool_allocator_new (GstD3D11Device * device,
1989 const D3D11_TEXTURE2D_DESC * desc)
1991 GstD3D11PoolAllocator *self;
1993 g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), NULL);
1994 g_return_val_if_fail (desc != NULL, NULL);
1996 gst_d3d11_memory_init_once ();
1998 self = (GstD3D11PoolAllocator *)
1999 g_object_new (GST_TYPE_D3D11_POOL_ALLOCATOR, NULL);
2000 gst_object_ref_sink (self);
2002 self->device = (GstD3D11Device *) gst_object_ref (device);
2003 self->priv->desc = *desc;
2009 * gst_d3d11_pool_allocator_acquire_memory:
2010 * @allocator: a #GstD3D11PoolAllocator
2011 * @memory: (transfer full): a #GstMemory
2013 * Acquires a #GstMemory from @allocator. @memory should point to a memory
2014 * location that can hold a pointer to the new #GstMemory.
2016 * Returns: a #GstFlowReturn such as %GST_FLOW_FLUSHING when the allocator is
2020 gst_d3d11_pool_allocator_acquire_memory (GstD3D11PoolAllocator * allocator,
2021 GstMemory ** memory)
2023 GstD3D11PoolAllocatorPrivate *priv;
2024 GstFlowReturn result;
2026 g_return_val_if_fail (GST_IS_D3D11_POOL_ALLOCATOR (allocator),
2028 g_return_val_if_fail (memory != NULL, GST_FLOW_ERROR);
2030 priv = allocator->priv;
2032 /* assume we'll have one more outstanding buffer we need to do that so
2033 * that concurrent set_active doesn't clear the buffers */
2034 g_atomic_int_inc (&priv->outstanding);
2035 result = gst_d3d11_pool_allocator_acquire_memory_internal (allocator, memory);
2037 if (result == GST_FLOW_OK) {
2038 GstMemory *mem = *memory;
2039 /* Replace default allocator with ours */
2040 gst_object_unref (mem->allocator);
2041 mem->allocator = (GstAllocator *) gst_object_ref (allocator);
2042 GST_MINI_OBJECT_CAST (mem)->dispose = gst_d3d11_memory_release;
2044 dec_outstanding (allocator);
2051 * gst_d3d11_pool_allocator_get_pool_size:
2052 * @allocator: a #GstD3D11PoolAllocator
2053 * @max_size: (out) (optional): the max size of pool
2054 * @outstanding_size: (out) (optional): the number of outstanding memory
2056 * Returns: %TRUE if the size of memory pool is known
2061 gst_d3d11_pool_allocator_get_pool_size (GstD3D11PoolAllocator * allocator,
2062 guint * max_size, guint * outstanding_size)
2064 GstD3D11PoolAllocatorPrivate *priv;
2066 g_return_val_if_fail (GST_IS_D3D11_POOL_ALLOCATOR (allocator), FALSE);
2068 priv = allocator->priv;
2071 if (priv->desc.ArraySize > 1) {
2072 *max_size = priv->desc.ArraySize;
2074 /* For non-texture-array memory, we don't have any limit yet */
2079 if (outstanding_size)
2080 *outstanding_size = g_atomic_int_get (&priv->outstanding);