From 7bcfccd0bd3a5239f31b3b1443b27b5b3f505819 Mon Sep 17 00:00:00 2001 From: Seungha Yang Date: Fri, 19 Aug 2022 00:05:17 +0900 Subject: [PATCH] d3d11: Use CRITICAL_SECTION instead of GRecMutex The GRecMutex abstraction (and heap allocation happens in GLib) is unnecessary for this plugin. Part-of: --- .../sys/d3d11/gstd3d11deinterlace.cpp | 28 ++++----- .../sys/d3d11/gstd3d11screencapture.cpp | 21 +++---- .../sys/d3d11/gstd3d11videosink.cpp | 70 +++++++--------------- 3 files changed, 41 insertions(+), 78 deletions(-) diff --git a/subprojects/gst-plugins-bad/sys/d3d11/gstd3d11deinterlace.cpp b/subprojects/gst-plugins-bad/sys/d3d11/gstd3d11deinterlace.cpp index 49b8830..69c854d 100644 --- a/subprojects/gst-plugins-bad/sys/d3d11/gstd3d11deinterlace.cpp +++ b/subprojects/gst-plugins-bad/sys/d3d11/gstd3d11deinterlace.cpp @@ -222,7 +222,7 @@ typedef struct _GstD3D11Deinterlace GstD3D11DeinterlaceMethod method; - GRecMutex lock; + CRITICAL_SECTION lock; GQueue past_frame_queue; GQueue future_frame_queue; GstBuffer *to_process; @@ -259,10 +259,6 @@ static GstElementClass *parent_class = NULL; #define GST_D3D11_DEINTERLACE_GET_CLASS(object) \ (G_TYPE_INSTANCE_GET_CLASS ((object),G_TYPE_FROM_INSTANCE (object), \ GstD3D11DeinterlaceClass)) -#define GST_D3D11_DEINTERLACE_LOCK(self) \ - g_rec_mutex_lock (&GST_D3D11_DEINTERLACE (self)->lock); -#define GST_D3D11_DEINTERLACE_UNLOCK(self) \ - g_rec_mutex_unlock (&GST_D3D11_DEINTERLACE (self)->lock); static gboolean gst_d3d11_deinterlace_update_method (GstD3D11Deinterlace * self); @@ -420,7 +416,7 @@ gst_d3d11_deinterlace_init (GstD3D11Deinterlace * self) g_queue_init (&self->past_frame_queue); g_queue_init (&self->future_frame_queue); - g_rec_mutex_init (&self->lock); + InitializeCriticalSection (&self->lock); } static void @@ -567,7 +563,7 @@ gst_d3d11_deinterlace_finalize (GObject * object) { GstD3D11Deinterlace *self = GST_D3D11_DEINTERLACE (object); - g_rec_mutex_clear (&self->lock); + DeleteCriticalSection (&self->lock); G_OBJECT_CLASS (parent_class)->finalize (object); } @@ -630,7 +626,8 @@ gst_d3d11_deinterlace_reset_history (GstD3D11Deinterlace * self) static void gst_d3d11_deinterlace_reset (GstD3D11Deinterlace * self) { - GST_D3D11_DEINTERLACE_LOCK (self); + GstD3D11CSLockGuard lk (&self->lock); + if (self->fallback_in_pool) { gst_buffer_pool_set_active (self->fallback_in_pool, FALSE); gst_object_unref (self->fallback_in_pool); @@ -648,8 +645,6 @@ gst_d3d11_deinterlace_reset (GstD3D11Deinterlace * self) gst_d3d11_deinterlace_reset_history (self); self->default_buffer_duration = GST_CLOCK_TIME_NONE; - - GST_D3D11_DEINTERLACE_UNLOCK (self); } static void @@ -1809,9 +1804,9 @@ gst_d3d11_deinterlace_sink_event (GstBaseTransform * trans, GstEvent * event) gst_d3d11_deinterlace_drain (self); break; case GST_EVENT_FLUSH_STOP: - GST_D3D11_DEINTERLACE_LOCK (self); + EnterCriticalSection (&self->lock); gst_d3d11_deinterlace_reset_history (self); - GST_D3D11_DEINTERLACE_UNLOCK (self); + LeaveCriticalSection (&self->lock); break; default: break; @@ -1890,7 +1885,8 @@ gst_d3d11_deinterlace_drain (GstD3D11Deinterlace * self) GstFlowReturn ret = GST_FLOW_OK; GstBuffer *outbuf = NULL; - GST_D3D11_DEINTERLACE_LOCK (self); + EnterCriticalSection (&self->lock); + if (gst_base_transform_is_passthrough (trans)) { /* If we were passthrough, nothing to do */ goto done; @@ -1910,16 +1906,16 @@ gst_d3d11_deinterlace_drain (GstD3D11Deinterlace * self) ret = gst_d3d11_deinterlace_generate_output (trans, &outbuf); if (outbuf != NULL) { /* Release lock during push buffer */ - GST_D3D11_DEINTERLACE_UNLOCK (self); + LeaveCriticalSection (&self->lock); ret = gst_pad_push (trans->srcpad, outbuf); - GST_D3D11_DEINTERLACE_LOCK (self); + EnterCriticalSection (&self->lock); } } while (ret == GST_FLOW_OK && outbuf != NULL); } done: gst_d3d11_deinterlace_reset_history (self); - GST_D3D11_DEINTERLACE_UNLOCK (self); + LeaveCriticalSection (&self->lock); return ret; } diff --git a/subprojects/gst-plugins-bad/sys/d3d11/gstd3d11screencapture.cpp b/subprojects/gst-plugins-bad/sys/d3d11/gstd3d11screencapture.cpp index 14b7c06..15616c9 100644 --- a/subprojects/gst-plugins-bad/sys/d3d11/gstd3d11screencapture.cpp +++ b/subprojects/gst-plugins-bad/sys/d3d11/gstd3d11screencapture.cpp @@ -1518,7 +1518,7 @@ struct _GstD3D11ScreenCapture gboolean prepared; gint64 adapter_luid; - GRecMutex lock; + CRITICAL_SECTION lock; }; static void gst_d3d11_screen_capture_constructed (GObject * object); @@ -1557,7 +1557,7 @@ gst_d3d11_screen_capture_class_init (GstD3D11ScreenCaptureClass * klass) static void gst_d3d11_screen_capture_init (GstD3D11ScreenCapture * self) { - g_rec_mutex_init (&self->lock); + InitializeCriticalSection (&self->lock); memset (&self->desktop_coordinates, 0, sizeof (RECT)); } @@ -1715,7 +1715,7 @@ gst_d3d11_screen_capture_finalize (GObject * object) { GstD3D11ScreenCapture *self = GST_D3D11_SCREEN_CAPTURE (object); - g_rec_mutex_clear (&self->lock); + DeleteCriticalSection (&self->lock); G_OBJECT_CLASS (parent_class)->finalize (object); } @@ -1789,10 +1789,10 @@ gst_d3d11_screen_capture_prepare (GstD3D11ScreenCapture * capture) g_return_val_if_fail (GST_IS_D3D11_SCREEN_CAPTURE (capture), GST_FLOW_ERROR); g_return_val_if_fail (capture->device != nullptr, GST_FLOW_ERROR); - g_rec_mutex_lock (&capture->lock); + GstD3D11CSLockGuard lk (&capture->lock); + if (capture->prepared) { GST_DEBUG_OBJECT (capture, "Already prepared"); - g_rec_mutex_unlock (&capture->lock); return GST_FLOW_OK; } @@ -1805,13 +1805,11 @@ gst_d3d11_screen_capture_prepare (GstD3D11ScreenCapture * capture) delete capture->dupl_obj; capture->dupl_obj = nullptr; - g_rec_mutex_unlock (&capture->lock); return ret; } capture->prepared = TRUE; - g_rec_mutex_unlock (&capture->lock); return GST_FLOW_OK; } @@ -1824,7 +1822,7 @@ gst_d3d11_screen_capture_get_size (GstD3D11ScreenCapture * capture, g_return_val_if_fail (width != nullptr, FALSE); g_return_val_if_fail (height != nullptr, FALSE); - g_rec_mutex_lock (&capture->lock); + GstD3D11CSLockGuard lk (&capture->lock); *width = 0; *height = 0; @@ -1835,7 +1833,6 @@ gst_d3d11_screen_capture_get_size (GstD3D11ScreenCapture * capture, *width = capture->cached_width; *height = capture->cached_height; - g_rec_mutex_unlock (&capture->lock); return TRUE; } @@ -1903,13 +1900,12 @@ gst_d3d11_screen_capture_do_capture (GstD3D11ScreenCapture * capture, shared_device = TRUE; } - g_rec_mutex_lock (&capture->lock); + GstD3D11CSLockGuard lk (&capture->lock); if (!capture->prepared) ret = gst_d3d11_screen_capture_prepare (capture); if (ret != GST_FLOW_OK) { GST_WARNING_OBJECT (capture, "We are not prepared"); - g_rec_mutex_unlock (&capture->lock); return ret; } @@ -1921,7 +1917,6 @@ gst_d3d11_screen_capture_do_capture (GstD3D11ScreenCapture * capture, "Capture area (%u, %u, %u, %u) doesn't fit into screen size %ux%u", crop_box->left, crop_box->right, crop_box->top, crop_box->bottom, width, height); - g_rec_mutex_unlock (&capture->lock); return GST_D3D11_SCREEN_CAPTURE_FLOW_SIZE_CHANGED; } @@ -1942,7 +1937,6 @@ gst_d3d11_screen_capture_do_capture (GstD3D11ScreenCapture * capture, GST_ERROR_OBJECT (capture, "Unexpected failure during capture"); } - g_rec_mutex_unlock (&capture->lock); return ret; } @@ -1964,7 +1958,6 @@ out: gst_d3d11_device_unlock (device); gst_d3d11_device_unlock (capture->device); - g_rec_mutex_unlock (&capture->lock); return ret; } diff --git a/subprojects/gst-plugins-bad/sys/d3d11/gstd3d11videosink.cpp b/subprojects/gst-plugins-bad/sys/d3d11/gstd3d11videosink.cpp index 0a0f47e..ff0695f 100644 --- a/subprojects/gst-plugins-bad/sys/d3d11/gstd3d11videosink.cpp +++ b/subprojects/gst-plugins-bad/sys/d3d11/gstd3d11videosink.cpp @@ -134,7 +134,7 @@ struct _GstD3D11VideoSink /* For drawing on user texture */ gboolean drawing; GstBuffer *current_buffer; - GRecMutex lock; + CRITICAL_SECTION lock; gchar *title; @@ -146,18 +146,6 @@ struct _GstD3D11VideoSink GstVideoOrientationMethod selected_method; }; -#define GST_D3D11_VIDEO_SINK_GET_LOCK(d) (&(GST_D3D11_VIDEO_SINK_CAST(d)->lock)) -#define GST_D3D11_VIDEO_SINK_LOCK(d) G_STMT_START { \ - GST_TRACE_OBJECT (d, "Locking from thread %p", g_thread_self()); \ - g_rec_mutex_lock (GST_D3D11_VIDEO_SINK_GET_LOCK (d)); \ - GST_TRACE_OBJECT (d, "Locked from thread %p", g_thread_self()); \ - } G_STMT_END - -#define GST_D3D11_VIDEO_SINK_UNLOCK(d) G_STMT_START { \ - GST_TRACE_OBJECT (d, "Unlocking from thread %p", g_thread_self()); \ - g_rec_mutex_unlock (GST_D3D11_VIDEO_SINK_GET_LOCK (d)); \ - } G_STMT_END - static void gst_d3d11_videosink_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec); static void gst_d3d11_videosink_get_property (GObject * object, guint prop_id, @@ -409,7 +397,7 @@ gst_d3d11_video_sink_init (GstD3D11VideoSink * self) self->gamma_mode = DEFAULT_GAMMA_MODE; self->primaries_mode = DEFAULT_PRIMARIES_MODE; - g_rec_mutex_init (&self->lock); + InitializeCriticalSection (&self->lock); } static void @@ -417,8 +405,8 @@ gst_d3d11_videosink_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec) { GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (object); + GstD3D11CSLockGuard lk (&self->lock); - GST_D3D11_VIDEO_SINK_LOCK (self); switch (prop_id) { case PROP_ADAPTER: self->adapter = g_value_get_int (value); @@ -467,7 +455,6 @@ gst_d3d11_videosink_set_property (GObject * object, guint prop_id, G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } - GST_D3D11_VIDEO_SINK_UNLOCK (self); } static void @@ -475,8 +462,8 @@ gst_d3d11_videosink_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec) { GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (object); + GstD3D11CSLockGuard lk (&self->lock); - GST_D3D11_VIDEO_SINK_LOCK (self); switch (prop_id) { case PROP_ADAPTER: g_value_set_int (value, self->adapter); @@ -513,7 +500,6 @@ gst_d3d11_videosink_get_property (GObject * object, guint prop_id, G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); break; } - GST_D3D11_VIDEO_SINK_UNLOCK (self); } static void @@ -521,7 +507,7 @@ gst_d3d11_video_sink_finalize (GObject * object) { GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (object); - g_rec_mutex_clear (&self->lock); + DeleteCriticalSection (&self->lock); g_free (self->title); G_OBJECT_CLASS (parent_class)->finalize (object); @@ -610,9 +596,9 @@ gst_d3d11_video_sink_update_window (GstD3D11VideoSink * self, GstCaps * caps) self->caps_updated = FALSE; - GST_D3D11_VIDEO_SINK_LOCK (self); + EnterCriticalSection (&self->lock); if (!gst_d3d11_video_sink_prepare_window (self)) { - GST_D3D11_VIDEO_SINK_UNLOCK (self); + LeaveCriticalSection (&self->lock); GST_ELEMENT_ERROR (self, RESOURCE, NOT_FOUND, (nullptr), ("Failed to open window.")); @@ -623,7 +609,7 @@ gst_d3d11_video_sink_update_window (GstD3D11VideoSink * self, GstCaps * caps) if (!gst_video_info_from_caps (&self->info, caps)) { GST_DEBUG_OBJECT (self, "Could not locate image format from caps %" GST_PTR_FORMAT, caps); - GST_D3D11_VIDEO_SINK_UNLOCK (self); + LeaveCriticalSection (&self->lock); return FALSE; } @@ -639,7 +625,7 @@ gst_d3d11_video_sink_update_window (GstD3D11VideoSink * self, GstCaps * caps) if (!gst_video_calculate_display_ratio (&num, &den, video_width, video_height, video_par_n, video_par_d, display_par_n, display_par_d)) { - GST_D3D11_VIDEO_SINK_UNLOCK (self); + LeaveCriticalSection (&self->lock); GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (nullptr), ("Error calculating the output display ratio of the video.")); @@ -682,7 +668,7 @@ gst_d3d11_video_sink_update_window (GstD3D11VideoSink * self, GstCaps * caps) self->video_height = video_height; if (GST_VIDEO_SINK_WIDTH (self) <= 0 || GST_VIDEO_SINK_HEIGHT (self) <= 0) { - GST_D3D11_VIDEO_SINK_UNLOCK (self); + LeaveCriticalSection (&self->lock); GST_ELEMENT_ERROR (self, CORE, NEGOTIATION, (nullptr), ("Error calculating the output display ratio of the video.")); @@ -706,7 +692,7 @@ gst_d3d11_video_sink_update_window (GstD3D11VideoSink * self, GstCaps * caps) GST_VIDEO_SINK_HEIGHT (self), caps, config, &error)) { GstMessage *error_msg; - GST_D3D11_VIDEO_SINK_UNLOCK (self); + LeaveCriticalSection (&self->lock); GST_ERROR_OBJECT (self, "cannot create swapchain"); error_msg = gst_message_new_error (GST_OBJECT_CAST (self), @@ -722,7 +708,7 @@ gst_d3d11_video_sink_update_window (GstD3D11VideoSink * self, GstCaps * caps) g_clear_pointer (&self->title, g_free); } - GST_D3D11_VIDEO_SINK_UNLOCK (self); + LeaveCriticalSection (&self->lock); return TRUE; } @@ -881,18 +867,15 @@ static gboolean gst_d3d11_video_sink_stop (GstBaseSink * sink) { GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (sink); + GstD3D11CSLockGuard lk (&self->lock); GST_DEBUG_OBJECT (self, "Stop"); - GST_D3D11_VIDEO_SINK_LOCK (self); if (self->window) gst_d3d11_window_unprepare (self->window); gst_clear_object (&self->window); - GST_D3D11_VIDEO_SINK_UNLOCK (self); - gst_clear_object (&self->device); - g_clear_pointer (&self->title, g_free); return TRUE; @@ -1034,11 +1017,10 @@ static gboolean gst_d3d11_video_sink_unlock (GstBaseSink * sink) { GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (sink); + GstD3D11CSLockGuard lk (&self->lock); - GST_D3D11_VIDEO_SINK_LOCK (self); if (self->window) gst_d3d11_window_unlock (self->window); - GST_D3D11_VIDEO_SINK_UNLOCK (self); return TRUE; } @@ -1047,11 +1029,10 @@ static gboolean gst_d3d11_video_sink_unlock_stop (GstBaseSink * sink) { GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (sink); + GstD3D11CSLockGuard lk (&self->lock); - GST_D3D11_VIDEO_SINK_LOCK (self); if (self->window) gst_d3d11_window_unlock_stop (self->window); - GST_D3D11_VIDEO_SINK_UNLOCK (self); return TRUE; } @@ -1073,6 +1054,7 @@ gst_d3d11_video_sink_event (GstBaseSink * sink, GstEvent * event) if (title) { const gchar *app_name = g_get_application_name (); std::string title_string; + GstD3D11CSLockGuard lk (&self->lock); if (app_name) { title_string = std::string (title) + " : " + std::string (app_name); @@ -1080,22 +1062,19 @@ gst_d3d11_video_sink_event (GstBaseSink * sink, GstEvent * event) title_string = std::string (title); } - GST_D3D11_VIDEO_SINK_LOCK (self); if (self->window) { gst_d3d11_window_set_title (self->window, title_string.c_str ()); } else { g_free (self->title); self->title = g_strdup (title_string.c_str ()); } - GST_D3D11_VIDEO_SINK_UNLOCK (self); g_free (title); } if (gst_video_orientation_from_tag (taglist, &method)) { - GST_D3D11_VIDEO_SINK_LOCK (self); + GstD3D11CSLockGuard lk (&self->lock); gst_d3d11_video_sink_set_orientation (self, method, TRUE); - GST_D3D11_VIDEO_SINK_UNLOCK (self); } break; } @@ -1199,7 +1178,8 @@ gst_d3d11_video_sink_show_frame (GstVideoSink * sink, GstBuffer * buf) gst_d3d11_window_show (self->window); if (self->draw_on_shared_texture) { - GST_D3D11_VIDEO_SINK_LOCK (self); + GstD3D11CSLockGuard lk (&self->lock); + self->current_buffer = buf; self->drawing = TRUE; @@ -1212,7 +1192,6 @@ gst_d3d11_video_sink_show_frame (GstVideoSink * sink, GstBuffer * buf) GST_LOG_OBJECT (self, "End drawing"); self->drawing = FALSE; self->current_buffer = nullptr; - GST_D3D11_VIDEO_SINK_UNLOCK (self); } else { ret = gst_d3d11_window_render (self->window, buf); } @@ -1244,11 +1223,11 @@ gst_d3d11_video_sink_set_render_rectangle (GstVideoOverlay * overlay, gint x, gint y, gint width, gint height) { GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (overlay); + GstD3D11CSLockGuard lk (&self->lock); GST_DEBUG_OBJECT (self, "render rect x: %d, y: %d, width: %d, height %d", x, y, width, height); - GST_D3D11_VIDEO_SINK_LOCK (self); if (self->window) { GstVideoRectangle rect; @@ -1267,19 +1246,16 @@ gst_d3d11_video_sink_set_render_rectangle (GstVideoOverlay * overlay, gint x, self->render_rect.h = height; self->pending_render_rect = TRUE; } - - GST_D3D11_VIDEO_SINK_UNLOCK (self); } static void gst_d3d11_video_sink_expose (GstVideoOverlay * overlay) { GstD3D11VideoSink *self = GST_D3D11_VIDEO_SINK (overlay); + GstD3D11CSLockGuard lk (&self->lock); - GST_D3D11_VIDEO_SINK_LOCK (self); if (self->window && self->window->swap_chain) gst_d3d11_window_render (self->window, nullptr); - GST_D3D11_VIDEO_SINK_UNLOCK (self); } static void @@ -1337,10 +1313,9 @@ gst_d3d11_video_sink_draw_action (GstD3D11VideoSink * self, return FALSE; } - GST_D3D11_VIDEO_SINK_LOCK (self); + GstD3D11CSLockGuard lk (&self->lock); if (!self->drawing || !self->current_buffer) { GST_WARNING_OBJECT (self, "Nothing to draw"); - GST_D3D11_VIDEO_SINK_UNLOCK (self); return FALSE; } @@ -1352,7 +1327,6 @@ gst_d3d11_video_sink_draw_action (GstD3D11VideoSink * self, ret = gst_d3d11_window_render_on_shared_handle (self->window, self->current_buffer, shared_handle, texture_misc_flags, acquire_key, release_key); - GST_D3D11_VIDEO_SINK_UNLOCK (self); return ret == GST_FLOW_OK; } -- 2.7.4