d3d11memory: Remove GstPoll from GstD3D11PoolAllocator

GstBufferPool implementation was referenced for this GstD3D11PoolAllocator,
for example GstAtomicQueue, various atomic operations, and GstPoll ones.
However, such combination seems to be almost pointless
since gst_poll_{read,write}_control() takes mutex and also
GstPoll uses Win32 event handle internally.
Use simple SRWLOCK and CONDITION_VARIABLE instead, and don't make things
complicated/inefficient.

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/2887>
This commit is contained in:
Seungha Yang 2023-03-05 22:03:59 +09:00
parent 3c739b9b76
commit 76d1257cd3

View file

@ -29,6 +29,8 @@
#include "gstd3d11-private.h"
#include <map>
#include <memory>
#include <queue>
#include <atomic>
/**
* SECTION:gstd3d11memory
@ -1862,37 +1864,41 @@ gst_d3d11_allocator_set_active (GstD3D11Allocator * allocator, gboolean active)
}
/* GstD3D11PoolAllocator */
#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))
/* *INDENT-OFF* */
struct _GstD3D11PoolAllocatorPrivate
{
_GstD3D11PoolAllocatorPrivate ()
{
outstanding = 0;
}
~_GstD3D11PoolAllocatorPrivate ()
{
GST_D3D11_CLEAR_COM (texture);
}
/* parent texture when array typed memory is used */
ID3D11Texture2D *texture;
ID3D11Texture2D *texture = nullptr;
D3D11_TEXTURE2D_DESC desc;
/* All below member variables are analogous to that of GstBufferPool */
GstAtomicQueue *queue;
GstPoll *poll;
std::queue<GstMemory *> queue;
/* This lock will protect all below variables apart from atomic ones
* (identical to GstBufferPool::priv::rec_lock) */
CRITICAL_SECTION lock;
gboolean started;
gboolean active;
SRWLOCK lock = SRWLOCK_INIT;
CONDITION_VARIABLE cond = CONDITION_VARIABLE_INIT;
gboolean started = FALSE;
gboolean active = FALSE;
/* atomic */
gint outstanding;
guint max_mems;
guint cur_mems;
gboolean flushing;
std::atomic<guint> outstanding;
guint cur_mems = 0;
gboolean flushing = FALSE;
/* Calculated memory size, based on Direct3D11 staging texture map.
* Note that, we cannot know the actually staging texture memory size prior
* to map the staging texture because driver will likely require padding */
gsize mem_size;
gsize mem_size = 0;
guint mem_pitch = 0;
};
/* *INDENT-ON* */
static void gst_d3d11_pool_allocator_finalize (GObject * object);
@ -1905,8 +1911,8 @@ static gboolean gst_d3d11_pool_allocator_stop (GstD3D11PoolAllocator * self);
static gboolean gst_d3d11_memory_release (GstMiniObject * mini_object);
#define gst_d3d11_pool_allocator_parent_class pool_alloc_parent_class
G_DEFINE_TYPE_WITH_PRIVATE (GstD3D11PoolAllocator,
gst_d3d11_pool_allocator, GST_TYPE_D3D11_ALLOCATOR);
G_DEFINE_TYPE (GstD3D11PoolAllocator, gst_d3d11_pool_allocator,
GST_TYPE_D3D11_ALLOCATOR);
static void
gst_d3d11_pool_allocator_class_init (GstD3D11PoolAllocatorClass * klass)
@ -1920,47 +1926,27 @@ gst_d3d11_pool_allocator_class_init (GstD3D11PoolAllocatorClass * klass)
}
static void
gst_d3d11_pool_allocator_init (GstD3D11PoolAllocator * allocator)
gst_d3d11_pool_allocator_init (GstD3D11PoolAllocator * self)
{
GstD3D11PoolAllocatorPrivate *priv;
priv = allocator->priv = (GstD3D11PoolAllocatorPrivate *)
gst_d3d11_pool_allocator_get_instance_private (allocator);
InitializeCriticalSection (&priv->lock);
priv->poll = gst_poll_new_timer ();
priv->queue = gst_atomic_queue_new (16);
priv->flushing = 1;
priv->active = FALSE;
priv->started = FALSE;
/* 1 control write for flushing - the flush token */
gst_poll_write_control (priv->poll);
/* 1 control write for marking that we are not waiting for poll - the wait token */
gst_poll_write_control (priv->poll);
self->priv = new GstD3D11PoolAllocatorPrivate ();
}
static void
gst_d3d11_pool_allocator_finalize (GObject * object)
{
GstD3D11PoolAllocator *self = GST_D3D11_POOL_ALLOCATOR (object);
GstD3D11PoolAllocatorPrivate *priv = self->priv;
GST_DEBUG_OBJECT (self, "Finalize");
gst_d3d11_pool_allocator_stop (self);
gst_atomic_queue_unref (priv->queue);
gst_poll_free (priv->poll);
DeleteCriticalSection (&priv->lock);
GST_D3D11_CLEAR_COM (priv->texture);
delete self->priv;
gst_clear_object (&self->device);
G_OBJECT_CLASS (pool_alloc_parent_class)->finalize (object);
}
/* must be called with the lock */
static gboolean
gst_d3d11_pool_allocator_start (GstD3D11PoolAllocator * self)
{
@ -2004,15 +1990,16 @@ gst_d3d11_pool_allocator_start (GstD3D11PoolAllocator * self)
}
priv->mem_size = mem->size;
priv->mem_pitch = GST_D3D11_MEMORY_CAST (mem)->priv->map.RowPitch;
} else {
mem->size = mem->maxsize = priv->mem_size;
GST_D3D11_MEMORY_CAST (mem)->priv->map.RowPitch = priv->mem_pitch;
}
GST_D3D11_MEMORY_CAST (mem)->priv->subresource_index = i;
g_atomic_int_add (&priv->cur_mems, 1);
gst_atomic_queue_push (priv->queue, mem);
gst_poll_write_control (priv->poll);
priv->cur_mems++;
priv->queue.push (mem);
}
priv->started = TRUE;
@ -2020,37 +2007,6 @@ gst_d3d11_pool_allocator_start (GstD3D11PoolAllocator * self)
return TRUE;
}
static void
gst_d3d11_pool_allocator_do_set_flushing (GstD3D11PoolAllocator * self,
gboolean flushing)
{
GstD3D11PoolAllocatorPrivate *priv = self->priv;
if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self) == flushing)
return;
if (flushing) {
g_atomic_int_set (&priv->flushing, 1);
/* Write the flush token to wake up any waiters */
gst_poll_write_control (priv->poll);
} else {
while (!gst_poll_read_control (priv->poll)) {
if (errno == EWOULDBLOCK) {
/* This should not really happen unless flushing and unflushing
* happens on different threads. Let's wait a bit to get back flush
* token from the thread that was setting it to flushing */
g_thread_yield ();
continue;
} else {
/* Critical error but GstPoll already complained */
break;
}
}
g_atomic_int_set (&priv->flushing, 0);
}
}
static gboolean
gst_d3d11_pool_allocator_set_active (GstD3D11Allocator * allocator,
gboolean active)
@ -2060,7 +2016,7 @@ gst_d3d11_pool_allocator_set_active (GstD3D11Allocator * allocator,
GST_LOG_OBJECT (self, "active %d", active);
GstD3D11CSLockGuard lk (GST_D3D11_POOL_ALLOCATOR_GET_LOCK (self));
GstD3D11SRWLockGuard lk (&priv->lock);
/* just return if we are already in the right state */
if (priv->active == active)
return TRUE;
@ -2071,76 +2027,57 @@ gst_d3d11_pool_allocator_set_active (GstD3D11Allocator * allocator,
return FALSE;
}
/* flush_stop may release memory objects, setting to active to avoid running
* do_stop while activating the pool */
priv->active = TRUE;
gst_d3d11_pool_allocator_do_set_flushing (self, FALSE);
priv->flushing = FALSE;
} else {
gint outstanding;
/* set to flushing first */
gst_d3d11_pool_allocator_do_set_flushing (self, TRUE);
priv->flushing = TRUE;
priv->active = FALSE;
WakeAllConditionVariable (&priv->cond);
/* when all memory objects are in the pool, free them. Else they will be
* freed when they are released */
outstanding = g_atomic_int_get (&priv->outstanding);
GST_LOG_OBJECT (self, "outstanding memories %d, (in queue %d)",
outstanding, gst_atomic_queue_length (priv->queue));
if (outstanding == 0) {
GST_LOG_OBJECT (self, "outstanding memories %d, (in queue %u)",
priv->outstanding.load (), (guint) priv->queue.size ());
if (priv->outstanding == 0) {
if (!gst_d3d11_pool_allocator_stop (self)) {
GST_ERROR_OBJECT (self, "stop failed");
return FALSE;
}
}
priv->active = FALSE;
}
return TRUE;
}
/* must be called with the lock */
static void
gst_d3d11_pool_allocator_free_memory (GstD3D11PoolAllocator * self,
GstMemory * mem)
{
GstD3D11PoolAllocatorPrivate *priv = self->priv;
g_atomic_int_add (&priv->cur_mems, -1);
priv->cur_mems--;
GST_LOG_OBJECT (self, "freeing memory %p (%u left)", mem, priv->cur_mems);
GST_MINI_OBJECT_CAST (mem)->dispose = NULL;
GST_MINI_OBJECT_CAST (mem)->dispose = nullptr;
gst_memory_unref (mem);
}
/* must be called with the lock */
static gboolean
static void
gst_d3d11_pool_allocator_clear_queue (GstD3D11PoolAllocator * self)
{
GstD3D11PoolAllocatorPrivate *priv = self->priv;
GstMemory *memory;
GST_LOG_OBJECT (self, "Clearing queue");
/* clear the pool */
while ((memory = (GstMemory *) gst_atomic_queue_pop (priv->queue))) {
while (!gst_poll_read_control (priv->poll)) {
if (errno == EWOULDBLOCK) {
/* We put the memory into the queue but did not finish writing control
* yet, let's wait a bit and retry */
g_thread_yield ();
continue;
} else {
/* Critical error but GstPoll already complained */
break;
}
}
gst_d3d11_pool_allocator_free_memory (self, memory);
while (!priv->queue.empty ()) {
GstMemory *mem = priv->queue.front ();
priv->queue.pop ();
gst_d3d11_pool_allocator_free_memory (self, mem);
}
GST_LOG_OBJECT (self, "Clear done");
return priv->cur_mems == 0;
}
/* must be called with the lock */
@ -2152,8 +2089,7 @@ gst_d3d11_pool_allocator_stop (GstD3D11PoolAllocator * self)
GST_DEBUG_OBJECT (self, "Stop");
if (priv->started) {
if (!gst_d3d11_pool_allocator_clear_queue (self))
return FALSE;
gst_d3d11_pool_allocator_clear_queue (self);
priv->started = FALSE;
} else {
@ -2163,46 +2099,35 @@ gst_d3d11_pool_allocator_stop (GstD3D11PoolAllocator * self)
return TRUE;
}
static inline void
dec_outstanding (GstD3D11PoolAllocator * self)
{
if (g_atomic_int_dec_and_test (&self->priv->outstanding)) {
/* 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 */
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);
}
}
}
/* Must be called with the lock and unlocked in this method */
static void
gst_d3d11_pool_allocator_release_memory (GstD3D11PoolAllocator * self,
GstMemory * mem)
{
GstD3D11PoolAllocatorPrivate *priv = self->priv;
GST_LOG_OBJECT (self, "Released memory %p", mem);
GST_MINI_OBJECT_CAST (mem)->dispose = NULL;
GST_MINI_OBJECT_CAST (mem)->dispose = nullptr;
mem->allocator = (GstAllocator *) gst_object_ref (_d3d11_memory_allocator);
/* keep it around in our queue */
gst_atomic_queue_push (self->priv->queue, mem);
gst_poll_write_control (self->priv->poll);
dec_outstanding (self);
priv->queue.push (mem);
priv->outstanding--;
WakeAllConditionVariable (&priv->cond);
ReleaseSRWLockExclusive (&priv->lock);
gst_object_unref (self);
}
static gboolean
gst_d3d11_memory_release (GstMiniObject * mini_object)
gst_d3d11_memory_release (GstMiniObject * object)
{
GstMemory *mem = GST_MEMORY_CAST (mini_object);
GstMemory *mem = GST_MEMORY_CAST (object);
GstD3D11PoolAllocator *alloc;
GstD3D11PoolAllocatorPrivate *priv;
g_assert (mem->allocator != NULL);
g_assert (mem->allocator);
if (!GST_IS_D3D11_POOL_ALLOCATOR (mem->allocator)) {
GST_LOG_OBJECT (mem->allocator, "Not our memory, free");
@ -2210,8 +2135,12 @@ gst_d3d11_memory_release (GstMiniObject * mini_object)
}
alloc = GST_D3D11_POOL_ALLOCATOR (mem->allocator);
priv = alloc->priv;
AcquireSRWLockExclusive (&priv->lock);
/* if flushing, free this memory */
if (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (alloc)) {
if (alloc->priv->flushing) {
ReleaseSRWLockExclusive (&priv->lock);
GST_LOG_OBJECT (alloc, "allocator is flushing, free %p", mem);
return TRUE;
}
@ -2223,6 +2152,7 @@ gst_d3d11_memory_release (GstMiniObject * mini_object)
return FALSE;
}
/* must be called with the lock */
static GstFlowReturn
gst_d3d11_pool_allocator_alloc (GstD3D11PoolAllocator * self, GstMemory ** mem)
{
@ -2233,13 +2163,10 @@ gst_d3d11_pool_allocator_alloc (GstD3D11PoolAllocator * self, GstMemory ** mem)
if (priv->desc.ArraySize > 1)
return GST_FLOW_EOS;
/* increment the allocation counter */
g_atomic_int_add (&priv->cur_mems, 1);
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);
return GST_FLOW_ERROR;
}
@ -2247,101 +2174,60 @@ gst_d3d11_pool_allocator_alloc (GstD3D11PoolAllocator * self, GstMemory ** mem)
if (!gst_d3d11_memory_update_size (new_mem)) {
GST_ERROR_OBJECT (self, "Failed to calculate size");
gst_memory_unref (new_mem);
g_atomic_int_add (&priv->cur_mems, -1);
return GST_FLOW_ERROR;
}
priv->mem_size = new_mem->size;
priv->mem_pitch = GST_D3D11_MEMORY_CAST (new_mem)->priv->map.RowPitch;
} else {
new_mem->size = new_mem->maxsize = priv->mem_size;
GST_D3D11_MEMORY_CAST (new_mem)->priv->map.RowPitch = priv->mem_pitch;
}
new_mem->size = new_mem->maxsize = priv->mem_size;
priv->cur_mems++;
*mem = new_mem;
return GST_FLOW_OK;
}
/* must be called with the lock */
static GstFlowReturn
gst_d3d11_pool_allocator_acquire_memory_internal (GstD3D11PoolAllocator * self,
GstMemory ** memory)
{
GstFlowReturn result;
GstD3D11PoolAllocatorPrivate *priv = self->priv;
GstFlowReturn ret = GST_FLOW_ERROR;
while (TRUE) {
if (G_UNLIKELY (GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)))
goto flushing;
do {
if (priv->flushing) {
GST_DEBUG_OBJECT (self, "we are flushing");
return GST_FLOW_FLUSHING;
}
/* try to get a memory from the queue */
*memory = (GstMemory *) gst_atomic_queue_pop (priv->queue);
if (G_LIKELY (*memory)) {
while (!gst_poll_read_control (priv->poll)) {
if (errno == EWOULDBLOCK) {
/* We put the memory into the queue but did not finish writing control
* yet, let's wait a bit and retry */
g_thread_yield ();
continue;
} else {
/* Critical error but GstPoll already complained */
break;
}
}
result = GST_FLOW_OK;
if (!priv->queue.empty ()) {
*memory = priv->queue.front ();
priv->queue.pop ();
GST_LOG_OBJECT (self, "acquired memory %p", *memory);
break;
return GST_FLOW_OK;
}
/* no memory, try to allocate some more */
GST_LOG_OBJECT (self, "no memory, trying to allocate");
result = gst_d3d11_pool_allocator_alloc (self, memory);
if (G_LIKELY (result == GST_FLOW_OK))
/* we have a memory, return it */
break;
ret = gst_d3d11_pool_allocator_alloc (self, memory);
if (ret == GST_FLOW_OK)
return ret;
if (G_UNLIKELY (result != GST_FLOW_EOS))
/* something went wrong, return error */
if (ret != GST_FLOW_EOS)
break;
/* now we release the control socket, we wait for a memory release or
* flushing */
if (!gst_poll_read_control (priv->poll)) {
if (errno == EWOULDBLOCK) {
/* This means that we have two threads trying to allocate memory
* already, and the other one already got the wait token. This
* means that we only have to wait for the poll now and not write the
* token afterwards: we will be woken up once the other thread is
* woken up and that one will write the wait token it removed */
GST_LOG_OBJECT (self, "waiting for free memory or flushing");
gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
} else {
/* This is a critical error, GstPoll already gave a warning */
result = GST_FLOW_ERROR;
break;
}
} else {
/* We're the first thread waiting, we got the wait token and have to
* write it again later
* OR
* We're a second thread and just consumed the flush token and block all
* other threads, in which case we must not wait and give it back
* immediately */
if (!GST_D3D11_POOL_ALLOCATOR_IS_FLUSHING (self)) {
GST_LOG_OBJECT (self, "waiting for free memory or flushing");
gst_poll_wait (priv->poll, GST_CLOCK_TIME_NONE);
}
gst_poll_write_control (priv->poll);
}
}
SleepConditionVariableSRW (&priv->cond, &priv->lock, INFINITE, 0);
} while (TRUE);
return result;
/* ERRORS */
flushing:
{
GST_DEBUG_OBJECT (self, "we are flushing");
return GST_FLOW_FLUSHING;
}
return ret;
}
/**
@ -2391,31 +2277,27 @@ GstFlowReturn
gst_d3d11_pool_allocator_acquire_memory (GstD3D11PoolAllocator * allocator,
GstMemory ** memory)
{
GstFlowReturn ret;
GstD3D11PoolAllocatorPrivate *priv;
GstFlowReturn result;
g_return_val_if_fail (GST_IS_D3D11_POOL_ALLOCATOR (allocator),
GST_FLOW_ERROR);
g_return_val_if_fail (memory != NULL, GST_FLOW_ERROR);
g_return_val_if_fail (memory != nullptr, GST_FLOW_ERROR);
priv = allocator->priv;
/* assume we'll have one more outstanding buffer we need to do that so
* that concurrent set_active doesn't clear the buffers */
g_atomic_int_inc (&priv->outstanding);
result = gst_d3d11_pool_allocator_acquire_memory_internal (allocator, memory);
if (result == GST_FLOW_OK) {
GstD3D11SRWLockGuard lk (&priv->lock);
ret = gst_d3d11_pool_allocator_acquire_memory_internal (allocator, memory);
if (ret == GST_FLOW_OK) {
GstMemory *mem = *memory;
/* Replace default allocator with ours */
gst_object_unref (mem->allocator);
mem->allocator = (GstAllocator *) gst_object_ref (allocator);
GST_MINI_OBJECT_CAST (mem)->dispose = gst_d3d11_memory_release;
} else {
dec_outstanding (allocator);
allocator->priv->outstanding++;
}
return result;
return ret;
}
/**
@ -2448,7 +2330,7 @@ gst_d3d11_pool_allocator_get_pool_size (GstD3D11PoolAllocator * allocator,
}
if (outstanding_size)
*outstanding_size = g_atomic_int_get (&priv->outstanding);
*outstanding_size = priv->outstanding;
return TRUE;
}