From cdc33848838ca13959a242ad34d8334e052c2de0 Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Sat, 15 Aug 2020 18:16:11 +1000 Subject: [PATCH] [host] dxgi: improve frame signaling mechanics --- host/platform/Windows/capture/DXGI/src/dxgi.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/host/platform/Windows/capture/DXGI/src/dxgi.c b/host/platform/Windows/capture/DXGI/src/dxgi.c index a461330d..d4f4975a 100644 --- a/host/platform/Windows/capture/DXGI/src/dxgi.c +++ b/host/platform/Windows/capture/DXGI/src/dxgi.c @@ -26,6 +26,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include "common/event.h" #include +#include #include #include #include @@ -82,7 +83,7 @@ struct iface Texture * texture; int texRIndex; int texWIndex; - volatile int texReady; + atomic_int texReady; bool needsRelease; CaptureGetPointerBuffer getPointerBufferFn; @@ -228,9 +229,9 @@ static bool dxgi_init() this->stop = false; this->texRIndex = 0; this->texWIndex = 0; - this->texReady = 0; + atomic_store(&this->texReady, 0); - lgResetEvent(this->frameEvent ); + lgResetEvent(this->frameEvent); status = CreateDXGIFactory1(&IID_IDXGIFactory1, (void **)&this->factory); if (FAILED(status)) @@ -755,8 +756,8 @@ static CaptureResult dxgi_capture() // set the state, and signal tex->state = TEXTURE_STATE_PENDING_MAP; - INTERLOCKED_INC(&this->texReady); - lgSignalEvent(this->frameEvent); + if (atomic_fetch_add_explicit(&this->texReady, 1, memory_order_relaxed) == 0) + lgSignalEvent(this->frameEvent); // advance the write index if (++this->texWIndex == this->maxTextures) @@ -852,12 +853,13 @@ static CaptureResult dxgi_waitFrame(CaptureFrame * frame) assert(this->initialized); // NOTE: the event may be signaled when there are no frames available - if(this->texReady == 0) + if(atomic_load_explicit(&this->texReady, memory_order_acquire) == 0) { if (!lgWaitEvent(this->frameEvent, 1000)) return CAPTURE_RESULT_TIMEOUT; - if (this->texReady == 0) + // the count will still be zero if we are stopping + if(atomic_load_explicit(&this->texReady, memory_order_acquire) == 0) return CAPTURE_RESULT_TIMEOUT; } @@ -883,7 +885,7 @@ static CaptureResult dxgi_waitFrame(CaptureFrame * frame) frame->stride = this->stride; frame->format = this->format; - INTERLOCKED_DEC(&this->texReady); + atomic_fetch_sub_explicit(&this->texReady, 1, memory_order_release); return CAPTURE_RESULT_OK; }