d3d11memory: Don't clear wrapped texture memory
[platform/upstream/gstreamer.git] / subprojects / gst-plugins-bad / gst-libs / gst / d3d11 / gstd3d11memory.cpp
index 13a84d2..a593701 100644 (file)
 #include "gstd3d11memory.h"
 #include "gstd3d11device.h"
 #include "gstd3d11utils.h"
-#include "gstd3d11_private.h"
+#include "gstd3d11-private.h"
+
+/**
+ * SECTION:gstd3d11memory
+ * @title: GstD3D11Memory
+ * @short_description: Direct3D11 memory abstraction layer
+ *
+ * Since: 1.22
+ */
 
 GST_DEBUG_CATEGORY_STATIC (gst_d3d11_allocator_debug);
 #define GST_CAT_DEFAULT gst_d3d11_allocator_debug
 
-static GstAllocator *_d3d11_memory_allocator;
+static GstD3D11Allocator *_d3d11_memory_allocator;
 
 GType
 gst_d3d11_allocation_flags_get_type (void)
 {
-  static gsize type = 0;
+  static GType type = 0;
   static const GFlagsValue values[] = {
     {GST_D3D11_ALLOCATION_FLAG_DEFAULT, "GST_D3D11_ALLOCATION_FLAG_DEFAULT",
         "default"},
@@ -45,18 +53,17 @@ gst_d3d11_allocation_flags_get_type (void)
     {0, nullptr, nullptr}
   };
 
-  if (g_once_init_enter (&type)) {
-    GType tmp = g_flags_register_static ("GstD3D11AllocationFlags", values);
-    g_once_init_leave (&type, tmp);
-  }
+  GST_D3D11_CALL_ONCE_BEGIN {
+    type = g_flags_register_static ("GstD3D11AllocationFlags", values);
+  } GST_D3D11_CALL_ONCE_END;
 
-  return (GType) type;
+  return type;
 }
 
 GType
 gst_d3d11_memory_transfer_get_type (void)
 {
-  static gsize type = 0;
+  static GType type = 0;
   static const GFlagsValue values[] = {
     {GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD,
         "GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD", "need-download"},
@@ -65,18 +72,17 @@ gst_d3d11_memory_transfer_get_type (void)
     {0, nullptr, nullptr}
   };
 
-  if (g_once_init_enter (&type)) {
-    GType tmp = g_flags_register_static ("GstD3D11MemoryTransfer", values);
-    g_once_init_leave (&type, tmp);
-  }
+  GST_D3D11_CALL_ONCE_BEGIN {
+    type = g_flags_register_static ("GstD3D11MemoryTransfer", values);
+  } GST_D3D11_CALL_ONCE_END;
 
-  return (GType) type;
+  return type;
 }
 
 GType
 gst_d3d11_memory_native_type_get_type (void)
 {
-  static gsize type = 0;
+  static GType type = 0;
   static const GEnumValue values[] = {
     {GST_D3D11_MEMORY_NATIVE_TYPE_INVALID,
         "GST_D3D11_MEMORY_NATIVE_TYPE_INVALID", "invalid"},
@@ -87,12 +93,11 @@ gst_d3d11_memory_native_type_get_type (void)
     {0, nullptr, nullptr}
   };
 
-  if (g_once_init_enter (&type)) {
-    GType tmp = g_enum_register_static ("GstD3D11MemoryNativeType", values);
-    g_once_init_leave (&type, tmp);
-  }
+  GST_D3D11_CALL_ONCE_BEGIN {
+    type = g_enum_register_static ("GstD3D11MemoryNativeType", values);
+  } GST_D3D11_CALL_ONCE_END;
 
-  return (GType) type;
+  return type;
 }
 
 /* GstD3D11AllocationParams */
@@ -115,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
  */
@@ -240,7 +245,7 @@ gst_d3d11_allocation_params_alignment (GstD3D11AllocationParams * params,
  * gst_d3d11_allocation_params_copy:
  * @src: a #GstD3D11AllocationParams
  *
- * Returns: a copy of @src
+ * Returns: (transfer full): a copy of @src
  *
  * Since: 1.22
  */
@@ -298,29 +303,26 @@ gst_d3d11_allocation_params_init (GType type)
 
 /* GstD3D11Memory */
 #define GST_D3D11_MEMORY_GET_LOCK(m) (&(GST_D3D11_MEMORY_CAST(m)->priv->lock))
-#define GST_D3D11_MEMORY_LOCK(m) G_STMT_START { \
-  GST_TRACE("Locking %p from thread %p", (m), g_thread_self()); \
-  g_mutex_lock(GST_D3D11_MEMORY_GET_LOCK(m)); \
-  GST_TRACE("Locked %p from thread %p", (m), g_thread_self()); \
-} G_STMT_END
-
-#define GST_D3D11_MEMORY_UNLOCK(m) G_STMT_START { \
-  GST_TRACE("Unlocking %p from thread %p", (m), g_thread_self()); \
-  g_mutex_unlock(GST_D3D11_MEMORY_GET_LOCK(m)); \
-} G_STMT_END
 
 struct _GstD3D11MemoryPrivate
 {
   ID3D11Texture2D *texture;
   ID3D11Buffer *buffer;
 
-  ID3D11Resource *staging;
+  GstD3D11MemoryNativeType native_type;
 
   D3D11_TEXTURE2D_DESC desc;
   D3D11_BUFFER_DESC buffer_desc;
 
   guint subresource_index;
 
+  /* protected by device lock */
+  ID3D11Resource *staging;
+  D3D11_MAPPED_SUBRESOURCE map;
+  gint cpu_map_count;
+
+  /* protects resource objects */
+  SRWLOCK lock;
   ID3D11ShaderResourceView *shader_resource_view[GST_VIDEO_MAX_PLANES];
   guint num_shader_resource_views;
 
@@ -333,13 +335,6 @@ struct _GstD3D11MemoryPrivate
   ID3D11VideoProcessorInputView *processor_input_view;
   ID3D11VideoProcessorOutputView *processor_output_view;
 
-  D3D11_MAPPED_SUBRESOURCE map;
-
-  GstD3D11MemoryNativeType native_type;
-
-  GMutex lock;
-  gint cpu_map_count;
-
   GDestroyNotify notify;
   gpointer user_data;
 };
@@ -444,11 +439,8 @@ gst_d3d11_memory_map_full (GstMemory * mem, GstMapInfo * info, gsize maxsize)
   GstD3D11Memory *dmem = GST_D3D11_MEMORY_CAST (mem);
   GstD3D11MemoryPrivate *priv = dmem->priv;
   GstMapFlags flags = info->flags;
-  gpointer ret = NULL;
   GstD3D11DeviceLockGuard lk (dmem->device);
 
-  GST_D3D11_MEMORY_LOCK (dmem);
-
   memset (info->user_data, 0, sizeof (info->user_data));
   info->user_data[0] = GUINT_TO_POINTER (dmem->priv->subresource_index);
 
@@ -456,7 +448,7 @@ gst_d3d11_memory_map_full (GstMemory * mem, GstMapInfo * info, gsize maxsize)
     if (priv->native_type == GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER) {
       /* FIXME: handle non-staging buffer */
       g_assert (priv->buffer != nullptr);
-      ret = priv->buffer;
+      return priv->buffer;
     } else {
       gst_d3d11_memory_upload (dmem);
       GST_MEMORY_FLAG_UNSET (dmem, GST_D3D11_MEMORY_TRANSFER_NEED_UPLOAD);
@@ -466,8 +458,7 @@ gst_d3d11_memory_map_full (GstMemory * mem, GstMapInfo * info, gsize maxsize)
             GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
 
       g_assert (priv->texture != NULL);
-      ret = priv->texture;
-      goto out;
+      return priv->texture;
     }
   }
 
@@ -482,7 +473,7 @@ gst_d3d11_memory_map_full (GstMemory * mem, GstMapInfo * info, gsize maxsize)
             &priv->desc);
         if (!priv->staging) {
           GST_ERROR_OBJECT (mem->allocator, "Couldn't create staging texture");
-          goto out;
+          return nullptr;
         }
 
         /* first memory, always need download to staging */
@@ -495,7 +486,7 @@ gst_d3d11_memory_map_full (GstMemory * mem, GstMapInfo * info, gsize maxsize)
     map_type = gst_d3d11_map_flags_to_d3d11 (flags);
     if (!gst_d3d11_memory_map_cpu_access (dmem, map_type)) {
       GST_ERROR_OBJECT (mem->allocator, "Couldn't map staging texture");
-      goto out;
+      return nullptr;
     }
   }
 
@@ -506,12 +497,7 @@ gst_d3d11_memory_map_full (GstMemory * mem, GstMapInfo * info, gsize maxsize)
   GST_MEMORY_FLAG_UNSET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
 
   priv->cpu_map_count++;
-  ret = dmem->priv->map.pData;
-
-out:
-  GST_D3D11_MEMORY_UNLOCK (dmem);
-
-  return ret;
+  return dmem->priv->map.pData;
 }
 
 /* Must be called with d3d11 device lock */
@@ -532,13 +518,11 @@ gst_d3d11_memory_unmap_full (GstMemory * mem, GstMapInfo * info)
   GstD3D11MemoryPrivate *priv = dmem->priv;
   GstD3D11DeviceLockGuard lk (dmem->device);
 
-  GST_D3D11_MEMORY_LOCK (dmem);
-
   if ((info->flags & GST_MAP_D3D11) == GST_MAP_D3D11) {
     if ((info->flags & GST_MAP_WRITE) == GST_MAP_WRITE)
       GST_MINI_OBJECT_FLAG_SET (mem, GST_D3D11_MEMORY_TRANSFER_NEED_DOWNLOAD);
 
-    goto out;
+    return;
   }
 
   if ((info->flags & GST_MAP_WRITE) == GST_MAP_WRITE)
@@ -546,12 +530,9 @@ gst_d3d11_memory_unmap_full (GstMemory * mem, GstMapInfo * info)
 
   priv->cpu_map_count--;
   if (priv->cpu_map_count > 0)
-    goto out;
+    return;
 
   gst_d3d11_memory_unmap_cpu_access (dmem);
-
-out:
-  GST_D3D11_MEMORY_UNLOCK (dmem);
 }
 
 static GstMemory *
@@ -570,7 +551,6 @@ gst_d3d11_memory_update_size (GstMemory * mem)
   gint stride[GST_VIDEO_MAX_PLANES];
   gsize size;
   D3D11_TEXTURE2D_DESC *desc = &priv->desc;
-  gboolean ret = FALSE;
 
   if (!priv->staging) {
     priv->staging = gst_d3d11_allocate_staging_texture (dmem->device,
@@ -582,7 +562,6 @@ gst_d3d11_memory_update_size (GstMemory * mem)
   }
 
   GstD3D11DeviceLockGuard lk (dmem->device);
-
   if (!gst_d3d11_memory_map_cpu_access (dmem, D3D11_MAP_READ_WRITE)) {
     GST_ERROR_OBJECT (mem->allocator, "Couldn't map staging texture");
     return FALSE;
@@ -593,16 +572,14 @@ gst_d3d11_memory_update_size (GstMemory * mem)
   if (!gst_d3d11_dxgi_format_get_size (desc->Format, desc->Width, desc->Height,
           priv->map.RowPitch, offset, stride, &size)) {
     GST_ERROR_OBJECT (mem->allocator, "Couldn't calculate memory size");
-    goto out;
+    GST_D3D11_CLEAR_COM (priv->staging);
+    return FALSE;
   }
 
-  mem->maxsize = mem->size = size;
-  ret = TRUE;
-
-out:
   GST_D3D11_CLEAR_COM (priv->staging);
+  mem->maxsize = mem->size = size;
 
-  return ret;
+  return TRUE;
 }
 
 /**
@@ -650,27 +627,25 @@ gst_d3d11_memory_get_native_type (GstD3D11Memory * mem)
 void
 gst_d3d11_memory_init_once (void)
 {
-  static gsize _init = 0;
-
-  if (g_once_init_enter (&_init)) {
-
+  GST_D3D11_CALL_ONCE_BEGIN {
     GST_DEBUG_CATEGORY_INIT (gst_d3d11_allocator_debug, "d3d11allocator", 0,
         "Direct3D11 Texture Allocator");
 
     _d3d11_memory_allocator =
-        (GstAllocator *) g_object_new (GST_TYPE_D3D11_ALLOCATOR, NULL);
+        (GstD3D11Allocator *) g_object_new (GST_TYPE_D3D11_ALLOCATOR, NULL);
     gst_object_ref_sink (_d3d11_memory_allocator);
+    gst_object_ref (_d3d11_memory_allocator);
 
-    gst_allocator_register (GST_D3D11_MEMORY_NAME, _d3d11_memory_allocator);
-    g_once_init_leave (&_init, 1);
-  }
+    gst_allocator_register (GST_D3D11_MEMORY_NAME,
+        GST_ALLOCATOR_CAST (_d3d11_memory_allocator));
+  } GST_D3D11_CALL_ONCE_END;
 }
 
 /**
  * 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
@@ -845,7 +820,6 @@ static gboolean
 gst_d3d11_memory_ensure_shader_resource_view (GstD3D11Memory * mem)
 {
   GstD3D11MemoryPrivate *priv = mem->priv;
-  gboolean ret = FALSE;
 
   if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
     return FALSE;
@@ -856,18 +830,11 @@ gst_d3d11_memory_ensure_shader_resource_view (GstD3D11Memory * mem)
     return FALSE;
   }
 
-  GST_D3D11_MEMORY_LOCK (mem);
-  if (priv->num_shader_resource_views) {
-    ret = TRUE;
-    goto done;
-  }
-
-  ret = create_shader_resource_views (mem);
-
-done:
-  GST_D3D11_MEMORY_UNLOCK (mem);
+  GstD3D11SRWLockGuard lk (GST_D3D11_MEMORY_GET_LOCK (mem));
+  if (priv->num_shader_resource_views)
+    return TRUE;
 
-  return ret;
+  return create_shader_resource_views (mem);
 }
 
 /**
@@ -983,7 +950,6 @@ static gboolean
 gst_d3d11_memory_ensure_render_target_view (GstD3D11Memory * mem)
 {
   GstD3D11MemoryPrivate *priv = mem->priv;
-  gboolean ret = FALSE;
 
   if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
     return FALSE;
@@ -994,18 +960,11 @@ gst_d3d11_memory_ensure_render_target_view (GstD3D11Memory * mem)
     return FALSE;
   }
 
-  GST_D3D11_MEMORY_LOCK (mem);
-  if (priv->num_render_target_views) {
-    ret = TRUE;
-    goto done;
-  }
-
-  ret = create_render_target_views (mem);
-
-done:
-  GST_D3D11_MEMORY_UNLOCK (mem);
+  GstD3D11SRWLockGuard lk (GST_D3D11_MEMORY_GET_LOCK (mem));
+  if (priv->num_render_target_views)
+    return TRUE;
 
-  return ret;
+  return create_render_target_views (mem);
 }
 
 /**
@@ -1068,7 +1027,6 @@ gst_d3d11_memory_ensure_decoder_output_view (GstD3D11Memory * mem,
   GstD3D11Allocator *allocator;
   D3D11_VIDEO_DECODER_OUTPUT_VIEW_DESC desc;
   HRESULT hr;
-  gboolean ret = FALSE;
 
   if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
     return FALSE;
@@ -1081,12 +1039,12 @@ gst_d3d11_memory_ensure_decoder_output_view (GstD3D11Memory * mem,
     return FALSE;
   }
 
-  GST_D3D11_MEMORY_LOCK (mem);
+  GstD3D11SRWLockGuard lk (GST_D3D11_MEMORY_GET_LOCK (mem));
   if (dmem_priv->decoder_output_view) {
     dmem_priv->decoder_output_view->GetDesc (&desc);
     if (IsEqualGUID (desc.DecodeProfile, *decoder_profile) &&
         dmem_priv->decoder_handle == decoder) {
-      goto succeeded;
+      return TRUE;
     } else {
       /* Shouldn't happen, but try again anyway */
       GST_WARNING_OBJECT (allocator,
@@ -1096,9 +1054,6 @@ gst_d3d11_memory_ensure_decoder_output_view (GstD3D11Memory * mem,
     }
   }
 
-  if (dmem_priv->decoder_output_view)
-    goto succeeded;
-
   desc.DecodeProfile = *decoder_profile;
   desc.ViewDimension = D3D11_VDOV_DIMENSION_TEXTURE2D;
   desc.Texture2D.ArraySlice = dmem_priv->subresource_index;
@@ -1108,7 +1063,7 @@ gst_d3d11_memory_ensure_decoder_output_view (GstD3D11Memory * mem,
   if (!gst_d3d11_result (hr, mem->device)) {
     GST_ERROR_OBJECT (allocator,
         "Could not create decoder output view, hr: 0x%x", (guint) hr);
-    goto done;
+    return FALSE;
   }
 
   /* XXX: decoder output view is bound to video device, not decoder handle
@@ -1117,13 +1072,7 @@ gst_d3d11_memory_ensure_decoder_output_view (GstD3D11Memory * mem,
   dmem_priv->decoder_handle = decoder;
   decoder->AddRef ();
 
-succeeded:
-  ret = TRUE;
-
-done:
-  GST_D3D11_MEMORY_UNLOCK (mem);
-
-  return ret;
+  return TRUE;
 }
 
 /**
@@ -1181,7 +1130,6 @@ gst_d3d11_memory_ensure_processor_input_view (GstD3D11Memory * mem,
   GstD3D11Allocator *allocator;
   D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC desc;
   HRESULT hr;
-  gboolean ret = FALSE;
 
   if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
     return FALSE;
@@ -1194,9 +1142,9 @@ gst_d3d11_memory_ensure_processor_input_view (GstD3D11Memory * mem,
     return FALSE;
   }
 
-  GST_D3D11_MEMORY_LOCK (mem);
+  GstD3D11SRWLockGuard lk (GST_D3D11_MEMORY_GET_LOCK (mem));
   if (dmem_priv->processor_input_view)
-    goto succeeded;
+    return TRUE;
 
   desc.FourCC = 0;
   desc.ViewDimension = D3D11_VPIV_DIMENSION_TEXTURE2D;
@@ -1208,16 +1156,10 @@ gst_d3d11_memory_ensure_processor_input_view (GstD3D11Memory * mem,
   if (!gst_d3d11_result (hr, mem->device)) {
     GST_ERROR_OBJECT (allocator,
         "Could not create processor input view, hr: 0x%x", (guint) hr);
-    goto done;
+    return FALSE;
   }
 
-succeeded:
-  ret = TRUE;
-
-done:
-  GST_D3D11_MEMORY_UNLOCK (mem);
-
-  return ret;
+  return TRUE;
 }
 
 /**
@@ -1257,7 +1199,6 @@ gst_d3d11_memory_ensure_processor_output_view (GstD3D11Memory * mem,
   GstD3D11Allocator *allocator;
   D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC desc;
   HRESULT hr;
-  gboolean ret;
 
   if (mem->priv->native_type != GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D)
     return FALSE;
@@ -1279,9 +1220,9 @@ gst_d3d11_memory_ensure_processor_output_view (GstD3D11Memory * mem,
     return FALSE;
   }
 
-  GST_D3D11_MEMORY_LOCK (mem);
+  GstD3D11SRWLockGuard lk (GST_D3D11_MEMORY_GET_LOCK (mem));
   if (priv->processor_output_view)
-    goto succeeded;
+    return TRUE;
 
   desc.ViewDimension = D3D11_VPOV_DIMENSION_TEXTURE2D;
   desc.Texture2D.MipSlice = 0;
@@ -1291,16 +1232,10 @@ gst_d3d11_memory_ensure_processor_output_view (GstD3D11Memory * mem,
   if (!gst_d3d11_result (hr, mem->device)) {
     GST_ERROR_OBJECT (allocator,
         "Could not create processor input view, hr: 0x%x", (guint) hr);
-    goto done;
+    return FALSE;
   }
 
-succeeded:
-  ret = TRUE;
-
-done:
-  GST_D3D11_MEMORY_UNLOCK (mem);
-
-  return ret;
+  return TRUE;
 }
 
 /**
@@ -1366,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);
@@ -1404,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) {
@@ -1494,7 +1412,6 @@ gst_d3d11_allocator_free (GstAllocator * allocator, GstMemory * mem)
   GST_D3D11_CLEAR_COM (dmem_priv->decoder_handle);
 
   gst_clear_object (&dmem->device);
-  g_mutex_clear (&dmem_priv->lock);
 
   if (dmem_priv->notify)
     dmem_priv->notify (dmem_priv->user_data);
@@ -1515,7 +1432,6 @@ gst_d3d11_allocator_alloc_wrapped_internal (GstD3D11Allocator * self,
 
   gst_memory_init (GST_MEMORY_CAST (mem),
       (GstMemoryFlags) 0, GST_ALLOCATOR_CAST (self), NULL, 0, 0, 0, 0);
-  g_mutex_init (&mem->priv->lock);
   mem->priv->texture = texture;
   mem->priv->desc = *desc;
   mem->priv->native_type = GST_D3D11_MEMORY_NATIVE_TYPE_TEXTURE_2D;
@@ -1557,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);
 
@@ -1566,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 =
@@ -1573,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)
@@ -1607,11 +1530,11 @@ gst_d3d11_allocator_alloc_internal (GstD3D11Allocator * self,
 
 /**
  * gst_d3d11_allocator_alloc:
- * @allocator: a #GstD3D11Allocator
- * @device: a #GstD3D11Device
+ * @allocator: (transfer none) (allow-none): a #GstD3D11Allocator
+ * @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
  */
@@ -1621,6 +1544,11 @@ gst_d3d11_allocator_alloc (GstD3D11Allocator * allocator,
 {
   GstMemory *mem;
 
+  if (!allocator) {
+    gst_d3d11_memory_init_once ();
+    allocator = _d3d11_memory_allocator;
+  }
+
   g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), NULL);
   g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), NULL);
   g_return_val_if_fail (desc != NULL, NULL);
@@ -1640,11 +1568,11 @@ gst_d3d11_allocator_alloc (GstD3D11Allocator * allocator,
 
 /**
  * gst_d3d11_allocator_alloc_buffer:
- * @allocator: a #GstD3D11Allocator
- * @device: a #GstD3D11Device
+ * @allocator: (transfer none) (allow-none): a #GstD3D11Allocator
+ * @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
  */
@@ -1657,6 +1585,11 @@ gst_d3d11_allocator_alloc_buffer (GstD3D11Allocator * allocator,
   ID3D11Device *device_handle;
   HRESULT hr;
 
+  if (!allocator) {
+    gst_d3d11_memory_init_once ();
+    allocator = _d3d11_memory_allocator;
+  }
+
   g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), nullptr);
   g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), nullptr);
   g_return_val_if_fail (desc != nullptr, nullptr);
@@ -1679,7 +1612,6 @@ gst_d3d11_allocator_alloc_buffer (GstD3D11Allocator * allocator,
 
   gst_memory_init (GST_MEMORY_CAST (mem),
       (GstMemoryFlags) 0, GST_ALLOCATOR_CAST (allocator), nullptr, 0, 0, 0, 0);
-  g_mutex_init (&mem->priv->lock);
   mem->priv->buffer = buffer;
   mem->priv->buffer_desc = *desc;
   mem->priv->native_type = GST_D3D11_MEMORY_NATIVE_TYPE_BUFFER;
@@ -1693,8 +1625,8 @@ gst_d3d11_allocator_alloc_buffer (GstD3D11Allocator * allocator,
 
 /**
  * gst_d3d11_allocator_alloc_wrapped:
- * @allocator: a #GstD3D11Allocator
- * @device: a #GstD3D11Device
+ * @allocator: (transfer none) (allow-none): a #GstD3D11Allocator
+ * @device: (transfer none): a #GstD3D11Device
  * @texture: a ID3D11Texture2D
  * @size: CPU accessible memory size
  * @user_data: (allow-none): user data
@@ -1711,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.
  *
@@ -1728,6 +1660,11 @@ gst_d3d11_allocator_alloc_wrapped (GstD3D11Allocator * allocator,
   ID3D11Texture2D *tex = nullptr;
   HRESULT hr;
 
+  if (!allocator) {
+    gst_d3d11_memory_init_once ();
+    allocator = _d3d11_memory_allocator;
+  }
+
   g_return_val_if_fail (GST_IS_D3D11_ALLOCATOR (allocator), nullptr);
   g_return_val_if_fail (GST_IS_D3D11_DEVICE (device), nullptr);
   g_return_val_if_fail (texture != nullptr, nullptr);
@@ -1786,15 +1723,15 @@ 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;
 }
 
 /* GstD3D11PoolAllocator */
-#define GST_D3D11_POOL_ALLOCATOR_LOCK(alloc)   (g_rec_mutex_lock(&alloc->priv->lock))
-#define GST_D3D11_POOL_ALLOCATOR_UNLOCK(alloc) (g_rec_mutex_unlock(&alloc->priv->lock))
+#define GST_D3D11_POOL_ALLOCATOR_GET_LOCK(alloc) \
+  (&(GST_D3D11_POOL_ALLOCATOR_CAST(alloc)->priv->lock))
 #define GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING(alloc)  (g_atomic_int_get (&alloc->priv->flushing))
 
 struct _GstD3D11PoolAllocatorPrivate
@@ -1809,7 +1746,7 @@ struct _GstD3D11PoolAllocatorPrivate
 
   /* This lock will protect all below variables apart from atomic ones
    * (identical to GstBufferPool::priv::rec_lock) */
-  GRecMutex lock;
+  CRITICAL_SECTION lock;
   gboolean started;
   gboolean active;
 
@@ -1825,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
@@ -1846,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
@@ -1859,7 +1794,8 @@ gst_d3d11_pool_allocator_init (GstD3D11PoolAllocator * allocator)
 
   priv = allocator->priv = (GstD3D11PoolAllocatorPrivate *)
       gst_d3d11_pool_allocator_get_instance_private (allocator);
-  g_rec_mutex_init (&priv->lock);
+
+  InitializeCriticalSection (&priv->lock);
 
   priv->poll = gst_poll_new_timer ();
   priv->queue = gst_atomic_queue_new (16);
@@ -1874,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);
@@ -1894,10 +1820,12 @@ gst_d3d11_pool_allocator_finalize (GObject * object)
   gst_d3d11_pool_allocator_stop (self);
   gst_atomic_queue_unref (priv->queue);
   gst_poll_free (priv->poll);
-  g_rec_mutex_clear (&priv->lock);
+  DeleteCriticalSection (&priv->lock);
 
   GST_D3D11_CLEAR_COM (priv->texture);
 
+  gst_clear_object (&self->device);
+
   G_OBJECT_CLASS (pool_alloc_parent_class)->finalize (object);
 }
 
@@ -1933,9 +1861,8 @@ gst_d3d11_pool_allocator_start (GstD3D11PoolAllocator * self)
     GstMemory *mem;
 
     priv->texture->AddRef ();
-    mem =
-        gst_d3d11_allocator_alloc_wrapped_internal (GST_D3D11_ALLOCATOR_CAST
-        (_d3d11_memory_allocator), self->device, &priv->desc, priv->texture);
+    mem = gst_d3d11_allocator_alloc_wrapped_internal (_d3d11_memory_allocator,
+        self->device, &priv->desc, priv->texture);
 
     if (i == 0) {
       if (!gst_d3d11_memory_update_size (mem)) {
@@ -2001,14 +1928,16 @@ gst_d3d11_pool_allocator_set_active (GstD3D11Allocator * allocator,
 
   GST_LOG_OBJECT (self, "active %d", active);
 
-  GST_D3D11_POOL_ALLOCATOR_LOCK (self);
+  GstD3D11CSLockGuard lk (GST_D3D11_POOL_ALLOCATOR_GET_LOCK (self));
   /* just return if we are already in the right state */
   if (priv->active == active)
-    goto was_ok;
+    return TRUE;
 
   if (active) {
-    if (!gst_d3d11_pool_allocator_start (self))
-      goto start_failed;
+    if (!gst_d3d11_pool_allocator_start (self)) {
+      GST_ERROR_OBJECT (self, "start failed");
+      return FALSE;
+    }
 
     /* flush_stop may release memory objects, setting to active to avoid running
      * do_stop while activating the pool */
@@ -2027,35 +1956,16 @@ gst_d3d11_pool_allocator_set_active (GstD3D11Allocator * allocator,
     GST_LOG_OBJECT (self, "outstanding memories %d, (in queue %d)",
         outstanding, gst_atomic_queue_length (priv->queue));
     if (outstanding == 0) {
-      if (!gst_d3d11_pool_allocator_stop (self))
-        goto stop_failed;
+      if (!gst_d3d11_pool_allocator_stop (self)) {
+        GST_ERROR_OBJECT (self, "stop failed");
+        return FALSE;
+      }
     }
 
     priv->active = FALSE;
   }
 
-  GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
-
   return TRUE;
-
-was_ok:
-  {
-    GST_DEBUG_OBJECT (self, "allocator was in the right state");
-    GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
-    return TRUE;
-  }
-start_failed:
-  {
-    GST_ERROR_OBJECT (self, "start failed");
-    GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
-    return FALSE;
-  }
-stop_failed:
-  {
-    GST_ERROR_OBJECT (self, "stop failed");
-    GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
-    return FALSE;
-  }
 }
 
 static void
@@ -2128,13 +2038,11 @@ dec_outstanding (GstD3D11PoolAllocator * self)
     /* all memory objects are returned to the pool, see if we need to free them */
     if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)) {
       /* take the lock so that set_active is not run concurrently */
-      GST_D3D11_POOL_ALLOCATOR_LOCK (self);
+      GstD3D11CSLockGuard lk (GST_D3D11_POOL_ALLOCATOR_GET_LOCK (self));
       /* now that we have the lock, check if we have been de-activated with
        * outstanding buffers */
       if (!self->priv->active)
         gst_d3d11_pool_allocator_stop (self);
-
-      GST_D3D11_POOL_ALLOCATOR_UNLOCK (self);
     }
   }
 }
@@ -2147,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
@@ -2194,9 +2103,8 @@ gst_d3d11_pool_allocator_alloc (GstD3D11PoolAllocator * self, GstMemory ** mem)
 
   /* increment the allocation counter */
   g_atomic_int_add (&priv->cur_mems, 1);
-  new_mem =
-      gst_d3d11_allocator_alloc_internal (GST_D3D11_ALLOCATOR_CAST
-      (_d3d11_memory_allocator), self->device, &priv->desc, nullptr);
+  new_mem = gst_d3d11_allocator_alloc_internal (_d3d11_memory_allocator,
+      self->device, &priv->desc, nullptr);
   if (!new_mem) {
     GST_ERROR_OBJECT (self, "Failed to allocate new memory");
     g_atomic_int_add (&priv->cur_mems, -1);