d3d11memory: Don't clear wrapped texture memory
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / gst-libs / gst / d3d11 / gstd3d11memory.cpp
index 3707e21..a593701 100644 (file)
@@ -120,7 +120,7 @@ G_DEFINE_BOXED_TYPE_WITH_CODE (GstD3D11AllocationParams,
  * and #GstD3D11Allocator in order to allocate new ID3D11Texture2D
  * object with given configuration
  *
- * Returns: a #GstD3D11AllocationParams or %NULL if @info is not supported
+ * Returns: (transfer full) (nullable): a #GstD3D11AllocationParams or %NULL if @info is not supported
  *
  * Since: 1.22
  */
@@ -645,7 +645,7 @@ gst_d3d11_memory_init_once (void)
  * gst_d3d11_memory_get_resource_handle:
  * @mem: a #GstD3D11Memory
  *
- * Returns: (transfer none): a ID3D11Resource handle. Caller must not release
+ * Returns: (transfer none) (nullable): a ID3D11Resource handle. Caller must not release
  * returned handle.
  *
  * Since: 1.22
@@ -1301,16 +1301,12 @@ gst_d3d11_memory_copy (GstMemory * mem, gssize offset, gssize size)
   GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
   GstD3D11Memory *copy_dmem;
   GstD3D11Device *device = dmem->device;
-  ID3D11Device *device_handle = gst_d3d11_device_get_device_handle (device);
   ID3D11DeviceContext *device_context =
       gst_d3d11_device_get_device_context_handle (device);
   D3D11_TEXTURE2D_DESC dst_desc = { 0, };
   D3D11_TEXTURE2D_DESC src_desc = { 0, };
   GstMemory *copy = NULL;
   GstMapInfo info;
-  HRESULT hr;
-  UINT bind_flags = 0;
-  UINT supported_flags = 0;
 
   if (dmem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
     return priv->fallback_copy (mem, offset, size);
@@ -1339,20 +1335,7 @@ gst_d3d11_memory_copy (GstMemory * mem, gssize offset, gssize size)
   dst_desc.SampleDesc.Count = 1;
   dst_desc.ArraySize = 1;
   dst_desc.Usage = D3D11_USAGE_DEFAULT;
-
-  /* If supported, use bind flags for SRV/RTV */
-  hr = device_handle->CheckFormatSupport (src_desc.Format, &supported_flags);
-  if (gst_d3d11_result (hr, device)) {
-    if ((supported_flags & D3D11_FORMAT_SUPPORT_SHADER_SAMPLE) ==
-        D3D11_FORMAT_SUPPORT_SHADER_SAMPLE) {
-      bind_flags |= D3D11_BIND_SHADER_RESOURCE;
-    }
-
-    if ((supported_flags & D3D11_FORMAT_SUPPORT_RENDER_TARGET) ==
-        D3D11_FORMAT_SUPPORT_RENDER_TARGET) {
-      bind_flags |= D3D11_BIND_RENDER_TARGET;
-    }
-  }
+  dst_desc.BindFlags = src_desc.BindFlags;
 
   copy = gst_d3d11_allocator_alloc_internal (alloc, device, &dst_desc, nullptr);
   if (!copy) {
@@ -1490,6 +1473,7 @@ gst_d3d11_allocator_alloc_internal (GstD3D11Allocator * self,
   GstD3D11Memory *dmem;
   ID3D11RenderTargetView *rtv = nullptr;
   GstD3D11ClearRTVFunc clear_func = nullptr;
+  gboolean is_new_texture = TRUE;
 
   device_handle = gst_d3d11_device_get_device_handle (device);
 
@@ -1499,6 +1483,8 @@ gst_d3d11_allocator_alloc_internal (GstD3D11Allocator * self,
       GST_ERROR_OBJECT (self, "Couldn't create texture");
       return nullptr;
     }
+  } else {
+    is_new_texture = FALSE;
   }
 
   mem =
@@ -1506,6 +1492,10 @@ gst_d3d11_allocator_alloc_internal (GstD3D11Allocator * self,
   if (!mem)
     return nullptr;
 
+  /* Don't clear external texture */
+  if (!is_new_texture)
+    return mem;
+
   /* Clear with YUV black if needed and possible
    * TODO: do this using UAV if RTV is not allowed (e.g., packed YUV formats) */
   if ((desc->BindFlags & D3D11_BIND_RENDER_TARGET) == 0)
@@ -1544,7 +1534,7 @@ gst_d3d11_allocator_alloc_internal (GstD3D11Allocator * self,
  * @device: (transfer none): a #GstD3D11Device
  * @desc: a D3D11_TEXTURE2D_DESC struct
  *
- * Returns: a newly allocated #GstD3D11Memory with given parameters.
+ * Returns: (transfer full) (nullable): a newly allocated #GstD3D11Memory with given parameters.
  *
  * Since: 1.22
  */
@@ -1582,7 +1572,7 @@ gst_d3d11_allocator_alloc (GstD3D11Allocator * allocator,
  * @device: (transfer none): a #GstD3D11Device
  * @desc: a D3D11_BUFFER_DESC struct
  *
- * Returns: a newly allocated #GstD3D11Memory with given parameters.
+ * Returns: (transfer full) (nullable): a newly allocated #GstD3D11Memory with given parameters.
  *
  * Since: 1.22
  */
@@ -1653,7 +1643,7 @@ gst_d3d11_allocator_alloc_buffer (GstD3D11Allocator * allocator,
  * Caller must not be confused that @size is CPU accessible size, not raw
  * texture size.
  *
- * Returns: a newly allocated #GstD3D11Memory with given @texture
+ * Returns: (transfer full) (nullable): a newly allocated #GstD3D11Memory with given @texture
  * if successful, or %NULL if @texture is not a valid handle or configuration
  * is not supported.
  *
@@ -1733,8 +1723,8 @@ gst_d3d11_allocator_set_active (GstD3D11Allocator * allocator, gboolean active)
   g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), FALSE);
 
   klass = GST_D3D11_ALLOCATOR_GET_CLASS (allocator);
-  if (klass->set_actvie)
-    return klass->set_actvie (allocator, active);
+  if (klass->set_active)
+    return klass->set_active (allocator, active);
 
   return TRUE;
 }
@@ -1772,7 +1762,6 @@ struct _GstD3D11PoolAllocatorPrivate
   gsize mem_size;
 };
 
-static void gst_d3d11_pool_allocator_dispose (GObject * object);
 static void gst_d3d11_pool_allocator_finalize (GObject * object);
 
 static gboolean
@@ -1793,10 +1782,9 @@ gst_d3d11_pool_allocator_class_init (GstD3D11PoolAllocatorClass * klass)
   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
   GstD3D11AllocatorClass *d3d11alloc_class = GST_D3D11_ALLOCATOR_CLASS (klass);
 
-  gobject_class->dispose = gst_d3d11_pool_allocator_dispose;
   gobject_class->finalize = gst_d3d11_pool_allocator_finalize;
 
-  d3d11alloc_class->set_actvie = gst_d3d11_pool_allocator_set_active;
+  d3d11alloc_class->set_active = gst_d3d11_pool_allocator_set_active;
 }
 
 static void
@@ -1822,16 +1810,6 @@ gst_d3d11_pool_allocator_init (GstD3D11PoolAllocator * allocator)
 }
 
 static void
-gst_d3d11_pool_allocator_dispose (GObject * object)
-{
-  GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (object);
-
-  gst_clear_object (&self->device);
-
-  G_OBJECT_CLASS (pool_alloc_parent_class)->dispose (object);
-}
-
-static void
 gst_d3d11_pool_allocator_finalize (GObject * object)
 {
   GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (object);
@@ -1846,6 +1824,8 @@ gst_d3d11_pool_allocator_finalize (GObject * object)
 
   GST_D3D11_CLEAR_COM (priv->texture);
 
+  gst_clear_object (&self->device);
+
   G_OBJECT_CLASS (pool_alloc_parent_class)->finalize (object);
 }
 
@@ -2075,12 +2055,13 @@ gst_d3d11_pool_allocator_release_memory (GstD3D11PoolAllocator * self,
 
   GST_MINI_OBJECT_CAST (mem)->dispose = NULL;
   mem->allocator = (GstAllocator *) gst_object_ref (_d3d11_memory_allocator);
-  gst_object_unref (self);
 
   /* keep it around in our queue */
   gst_atomic_queue_push (self->priv->queue, mem);
   gst_poll_write_control (self->priv->poll);
   dec_outstanding (self);
+
+  gst_object_unref (self);
 }
 
 static gboolean