d3d11window: Reuse ID3D11VideoProcessorInputView if possible
authorSeungha Yang <seungha@centricular.com>
Fri, 23 Oct 2020 07:59:00 +0000 (16:59 +0900)
committerGStreamer Merge Bot <gitlab-merge-bot@gstreamer-foundation.org>
Fri, 23 Oct 2020 15:44:10 +0000 (15:44 +0000)
GstMemory object could be disposed if GstBuffer is not allocated
by GstD3D11BufferPool such as via gst_buffer_copy() and/or
gst_buffer_make_writable(). So attaching qdata on GstMemory
object would cause unnecessary view alloc/free.
By using view pool which is implemented in GstD3D11Allocator,
we can avoid redundant view alloc/free.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-bad/-/merge_requests/1716>

sys/d3d11/gstd3d11videoprocessor.c
sys/d3d11/gstd3d11videoprocessor.h
sys/d3d11/gstd3d11window.cpp

index ed2bd74..e4eb208 100644 (file)
@@ -40,32 +40,6 @@ GST_DEBUG_CATEGORY_EXTERN (gst_d3d11_video_processor_debug);
 #define HAVE_VIDEO_CONTEXT_TWO
 #endif
 
-GQuark
-gst_d3d11_video_processor_input_view_quark (void)
-{
-  static volatile gsize quark = 0;
-
-  if (g_once_init_enter (&quark)) {
-    GQuark q = g_quark_from_static_string ("GstD3D11VideoProcessorInputView");
-    g_once_init_leave (&quark, (gsize) q);
-  }
-
-  return (GQuark) quark;
-}
-
-GQuark
-gst_d3d11_video_processor_output_view_quark (void)
-{
-  static volatile gsize quark = 0;
-
-  if (g_once_init_enter (&quark)) {
-    GQuark q = g_quark_from_static_string ("GstD3D11VideoProcessorOutputView");
-    g_once_init_leave (&quark, (gsize) q);
-  }
-
-  return (GQuark) quark;
-}
-
 struct _GstD3D11VideoProcessor
 {
   GstD3D11Device *device;
@@ -426,6 +400,14 @@ gst_d3d11_video_processor_create_input_view (GstD3D11VideoProcessor * processor,
 }
 
 gboolean
+gst_d3d11_video_processor_ensure_input_view (GstD3D11VideoProcessor * processor,
+    GstD3D11Memory * mem)
+{
+  return gst_d3d11_memory_ensure_processor_input_view (mem,
+      processor->video_device, processor->enumerator);
+}
+
+gboolean
 gst_d3d11_video_processor_create_output_view (GstD3D11VideoProcessor *
     processor, D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC * desc,
     ID3D11Resource * resource, ID3D11VideoProcessorOutputView ** view)
index 478d7b4..6c26330 100644 (file)
@@ -28,9 +28,6 @@ G_BEGIN_DECLS
 
 typedef struct _GstD3D11VideoProcessor GstD3D11VideoProcessor;
 
-GQuark gst_d3d11_video_processor_input_view_quark (void);
-GQuark gst_d3d11_video_processor_output_view_quark (void);
-
 GstD3D11VideoProcessor * gst_d3d11_video_processor_new  (GstD3D11Device * device,
                                                          guint in_width,
                                                          guint in_height,
@@ -75,6 +72,9 @@ gboolean  gst_d3d11_video_processor_create_input_view  (GstD3D11VideoProcessor *
                                                         ID3D11Resource *resource,
                                                         ID3D11VideoProcessorInputView ** view);
 
+gboolean  gst_d3d11_video_processor_ensure_input_view  (GstD3D11VideoProcessor * processor,
+                                                        GstD3D11Memory *mem);
+
 gboolean  gst_d3d11_video_processor_create_output_view (GstD3D11VideoProcessor * processor,
                                                         D3D11_VIDEO_PROCESSOR_OUTPUT_VIEW_DESC * desc,
                                                         ID3D11Resource *resource,
index fb46233..b1bceb6 100644 (file)
@@ -837,8 +837,6 @@ gst_d3d11_window_buffer_ensure_processor_input (GstD3D11Window * self,
     GstBuffer * buffer, ID3D11VideoProcessorInputView ** in_view)
 {
   GstD3D11Memory *mem;
-  ID3D11VideoProcessorInputView *view;
-  GQuark quark;
 
   if (!self->processor)
     return FALSE;
@@ -853,33 +851,12 @@ gst_d3d11_window_buffer_ensure_processor_input (GstD3D11Window * self,
     return FALSE;
   }
 
-  quark = gst_d3d11_video_processor_input_view_quark ();
-  view = (ID3D11VideoProcessorInputView *)
-      gst_mini_object_get_qdata (GST_MINI_OBJECT (mem), quark);
-
-  if (!view) {
-    D3D11_VIDEO_PROCESSOR_INPUT_VIEW_DESC in_desc;
-
-    in_desc.FourCC = 0;
-    in_desc.ViewDimension = D3D11_VPIV_DIMENSION_TEXTURE2D;
-    in_desc.Texture2D.MipSlice = 0;
-    in_desc.Texture2D.ArraySlice = mem->subresource_index;
-
-    GST_TRACE_OBJECT (self, "Create new processor input view");
-
-    if (!gst_d3d11_video_processor_create_input_view (self->processor,
-         &in_desc, mem->texture, &view)) {
-      GST_LOG_OBJECT (self, "Failed to create processor input view");
-      return FALSE;
-    }
-
-    gst_mini_object_set_qdata (GST_MINI_OBJECT (mem), quark, view,
-        (GDestroyNotify) gst_d3d11_video_processor_input_view_release);
-  } else {
-    GST_TRACE_OBJECT (self, "Reuse existing processor input view %p", view);
+  if (!gst_d3d11_video_processor_ensure_input_view (self->processor, mem)) {
+    GST_LOG_OBJECT (self, "Failed to create processor input view");
+    return FALSE;
   }
 
-  *in_view = view;
+  *in_view = mem->processor_input_view;
 
   return TRUE;
 }