[gnome-remote-desktop] rdp: Combine buffer creation and resize operations



commit 81bab7e518a90ee5c3281646ea08c366c790752e
Author: Pascal Nowack <Pascal Nowack gmx de>
Date:   Sat Mar 5 08:30:28 2022 +0100

    rdp: Combine buffer creation and resize operations
    
    Before the buffer pool can be used, it needs to be resized anyway.
    So, don't initially fill the buffer pool.
    This also removes the need of the additional buffer resize function,
    making the implementation easier.

 src/grd-rdp-buffer-pool.c |  70 +++++++++-------------
 src/grd-rdp-buffer.c      | 144 ++++++++++++++++++++++------------------------
 src/grd-rdp-buffer.h      |  12 ++--
 3 files changed, 102 insertions(+), 124 deletions(-)
---
diff --git a/src/grd-rdp-buffer-pool.c b/src/grd-rdp-buffer-pool.c
index 6327f0e8..c4a5dcbe 100644
--- a/src/grd-rdp-buffer-pool.c
+++ b/src/grd-rdp-buffer-pool.c
@@ -39,7 +39,6 @@ struct _GrdRdpBufferPool
 
   CUstream cuda_stream;
 
-  gboolean has_buffer_size;
   uint32_t buffer_width;
   uint32_t buffer_height;
   uint32_t buffer_stride;
@@ -66,17 +65,13 @@ add_buffer_to_pool (GrdRdpBufferPool *buffer_pool,
   buffer = grd_rdp_buffer_new (buffer_pool,
                                buffer_pool->egl_thread,
                                buffer_pool->hwaccel_nvidia,
-                               buffer_pool->cuda_stream);
-  if (buffer_pool->has_buffer_size &&
-      !grd_rdp_buffer_resize (buffer,
-                              buffer_pool->buffer_width,
-                              buffer_pool->buffer_height,
-                              buffer_pool->buffer_stride,
-                              preallocate_on_gpu))
-    {
-      grd_rdp_buffer_free (buffer);
-      return FALSE;
-    }
+                               buffer_pool->cuda_stream,
+                               buffer_pool->buffer_width,
+                               buffer_pool->buffer_height,
+                               buffer_pool->buffer_stride,
+                               preallocate_on_gpu);
+  if (!buffer)
+    return FALSE;
 
   buffer_info = g_new0 (BufferInfo, 1);
 
@@ -85,6 +80,20 @@ add_buffer_to_pool (GrdRdpBufferPool *buffer_pool,
   return TRUE;
 }
 
+static gboolean
+fill_buffer_pool (GrdRdpBufferPool *buffer_pool)
+{
+  uint32_t minimum_size = buffer_pool->minimum_pool_size;
+
+  while (g_hash_table_size (buffer_pool->buffer_table) < minimum_size)
+    {
+      if (!add_buffer_to_pool (buffer_pool, TRUE))
+        return FALSE;
+    }
+
+  return TRUE;
+}
+
 gboolean
 grd_rdp_buffer_pool_resize_buffers (GrdRdpBufferPool *buffer_pool,
                                     uint32_t          buffer_width,
@@ -92,9 +101,6 @@ grd_rdp_buffer_pool_resize_buffers (GrdRdpBufferPool *buffer_pool,
                                     uint32_t          buffer_stride)
 {
   g_autoptr (GMutexLocker) locker = NULL;
-  GHashTableIter iter;
-  GrdRdpBuffer *buffer;
-  BufferInfo *buffer_info;
 
   locker = g_mutex_locker_new (&buffer_pool->pool_mutex);
   g_assert (buffer_pool->buffers_taken == 0);
@@ -102,17 +108,10 @@ grd_rdp_buffer_pool_resize_buffers (GrdRdpBufferPool *buffer_pool,
   buffer_pool->buffer_width = buffer_width;
   buffer_pool->buffer_height = buffer_height;
   buffer_pool->buffer_stride = buffer_stride;
-  buffer_pool->has_buffer_size = TRUE;
 
-  g_hash_table_iter_init (&iter, buffer_pool->buffer_table);
-  while (g_hash_table_iter_next (&iter, (gpointer *) &buffer,
-                                        (gpointer *) &buffer_info))
-    {
-      g_assert (!buffer_info->buffer_taken);
-      if (!grd_rdp_buffer_resize (buffer, buffer_width, buffer_height,
-                                 buffer_stride, TRUE))
-        return FALSE;
-    }
+  g_hash_table_remove_all (buffer_pool->buffer_table);
+  if (!fill_buffer_pool (buffer_pool))
+    return FALSE;
 
   return TRUE;
 }
@@ -135,6 +134,10 @@ grd_rdp_buffer_pool_acquire (GrdRdpBufferPool *buffer_pool)
   BufferInfo *buffer_info;
   gboolean buffer_found = FALSE;
 
+  g_assert (buffer_pool->buffer_width > 0);
+  g_assert (buffer_pool->buffer_height > 0);
+  g_assert (buffer_pool->buffer_stride > 0);
+
   locker = g_mutex_locker_new (&buffer_pool->pool_mutex);
   if (g_hash_table_size (buffer_pool->buffer_table) <= buffer_pool->buffers_taken &&
       !add_buffer_to_pool (buffer_pool, FALSE))
@@ -269,20 +272,6 @@ static GSourceFuncs buffer_pool_source_funcs =
   .dispatch = buffer_pool_source_dispatch,
 };
 
-static gboolean
-fill_buffer_pool (GrdRdpBufferPool *buffer_pool)
-{
-  uint32_t minimum_size = buffer_pool->minimum_pool_size;
-
-  while (g_hash_table_size (buffer_pool->buffer_table) < minimum_size)
-    {
-      if (!add_buffer_to_pool (buffer_pool, TRUE))
-        return FALSE;
-    }
-
-  return TRUE;
-}
-
 GrdRdpBufferPool *
 grd_rdp_buffer_pool_new (GrdEglThread     *egl_thread,
                          GrdHwAccelNvidia *hwaccel_nvidia,
@@ -312,9 +301,6 @@ grd_rdp_buffer_pool_new (GrdEglThread     *egl_thread,
   g_source_set_ready_time (buffer_pool->unmap_source, -1);
   g_source_attach (buffer_pool->unmap_source, NULL);
 
-  if (!fill_buffer_pool (buffer_pool))
-    return NULL;
-
   return g_steal_pointer (&buffer_pool);
 }
 
diff --git a/src/grd-rdp-buffer.c b/src/grd-rdp-buffer.c
index 242a725c..9439f559 100644
--- a/src/grd-rdp-buffer.c
+++ b/src/grd-rdp-buffer.c
@@ -49,13 +49,49 @@ typedef struct
   CUstream cuda_stream;
 } UnmapBufferData;
 
+static gboolean
+cuda_allocate_buffer (gpointer user_data,
+                      uint32_t pbo)
+{
+  AllocateBufferData *data = user_data;
+  GrdRdpBuffer *buffer = data->buffer;
+  gboolean success;
+
+  success = grd_hwaccel_nvidia_register_read_only_gl_buffer (data->hwaccel_nvidia,
+                                                             &buffer->cuda_resource,
+                                                             pbo);
+  if (success)
+    buffer->pbo = pbo;
+
+  return success;
+}
+
+static void
+resources_ready (gboolean success,
+                 gpointer user_data)
+{
+  GrdSyncPoint *sync_point = user_data;
+
+  if (success)
+    g_debug ("[RDP] Allocating GL resources was successful");
+  else
+    g_warning ("[RDP] Failed to allocate GL resources");
+
+  grd_sync_point_complete (sync_point, success);
+}
+
 GrdRdpBuffer *
 grd_rdp_buffer_new (GrdRdpBufferPool *buffer_pool,
                     GrdEglThread     *egl_thread,
                     GrdHwAccelNvidia *hwaccel_nvidia,
-                    CUstream          cuda_stream)
+                    CUstream          cuda_stream,
+                    uint32_t          width,
+                    uint32_t          height,
+                    uint32_t          stride,
+                    gboolean          preallocate_on_gpu)
 {
   GrdRdpBuffer *buffer;
+  gboolean success = TRUE;
 
   buffer = g_new0 (GrdRdpBuffer, 1);
   buffer->buffer_pool = buffer_pool;
@@ -64,6 +100,38 @@ grd_rdp_buffer_new (GrdRdpBufferPool *buffer_pool,
 
   buffer->cuda_stream = cuda_stream;
 
+  buffer->width = width;
+  buffer->height = height;
+  buffer->local_data = g_malloc0 (stride * height * sizeof (uint8_t));
+
+  if (preallocate_on_gpu &&
+      buffer->hwaccel_nvidia)
+    {
+      AllocateBufferData data = {};
+      GrdSyncPoint sync_point = {};
+
+      g_assert (buffer->egl_thread);
+
+      grd_sync_point_init (&sync_point);
+      data.hwaccel_nvidia = buffer->hwaccel_nvidia;
+      data.buffer = buffer;
+
+      grd_egl_thread_allocate (buffer->egl_thread,
+                               buffer->height,
+                               stride,
+                               cuda_allocate_buffer,
+                               &data,
+                               resources_ready,
+                               &sync_point,
+                               NULL);
+
+      success = grd_sync_point_wait_for_completion (&sync_point);
+      grd_sync_point_clear (&sync_point);
+    }
+
+  if (!success)
+    g_clear_pointer (&buffer, grd_rdp_buffer_free);
+
   return buffer;
 }
 
@@ -158,77 +226,3 @@ grd_rdp_buffer_release (GrdRdpBuffer *buffer)
 {
   grd_rdp_buffer_pool_release_buffer (buffer->buffer_pool, buffer);
 }
-
-static gboolean
-cuda_allocate_buffer (gpointer user_data,
-                      uint32_t pbo)
-{
-  AllocateBufferData *data = user_data;
-  GrdRdpBuffer *buffer = data->buffer;
-  gboolean success;
-
-  success = grd_hwaccel_nvidia_register_read_only_gl_buffer (data->hwaccel_nvidia,
-                                                             &buffer->cuda_resource,
-                                                             pbo);
-  if (success)
-    buffer->pbo = pbo;
-
-  return success;
-}
-
-static void
-resources_ready (gboolean success,
-                 gpointer user_data)
-{
-  GrdSyncPoint *sync_point = user_data;
-
-  if (success)
-    g_debug ("[RDP] Allocating GL resources was successful");
-  else
-    g_warning ("[RDP] Failed to allocate GL resources");
-
-  grd_sync_point_complete (sync_point, success);
-}
-
-gboolean
-grd_rdp_buffer_resize (GrdRdpBuffer *buffer,
-                       uint32_t      width,
-                       uint32_t      height,
-                       uint32_t      stride,
-                       gboolean      preallocate_on_gpu)
-{
-  gboolean success = TRUE;
-
-  clear_buffers (buffer);
-
-  buffer->width = width;
-  buffer->height = height;
-  buffer->local_data = g_malloc0 (stride * height * sizeof (uint8_t));
-
-  if (preallocate_on_gpu &&
-      buffer->hwaccel_nvidia)
-    {
-      AllocateBufferData data = {};
-      GrdSyncPoint sync_point = {};
-
-      g_assert (buffer->egl_thread);
-
-      grd_sync_point_init (&sync_point);
-      data.hwaccel_nvidia = buffer->hwaccel_nvidia;
-      data.buffer = buffer;
-
-      grd_egl_thread_allocate (buffer->egl_thread,
-                               buffer->height,
-                               stride,
-                               cuda_allocate_buffer,
-                               &data,
-                               resources_ready,
-                               &sync_point,
-                               NULL);
-
-      success = grd_sync_point_wait_for_completion (&sync_point);
-      grd_sync_point_clear (&sync_point);
-    }
-
-  return success;
-}
diff --git a/src/grd-rdp-buffer.h b/src/grd-rdp-buffer.h
index 54583094..5ff689f5 100644
--- a/src/grd-rdp-buffer.h
+++ b/src/grd-rdp-buffer.h
@@ -48,7 +48,11 @@ struct _GrdRdpBuffer
 GrdRdpBuffer *grd_rdp_buffer_new (GrdRdpBufferPool *buffer_pool,
                                   GrdEglThread     *egl_thread,
                                   GrdHwAccelNvidia *hwaccel_nvidia,
-                                  CUstream          cuda_stream);
+                                  CUstream          cuda_stream,
+                                  uint32_t          width,
+                                  uint32_t          height,
+                                  uint32_t          stride,
+                                  gboolean          preallocate_on_gpu);
 
 void grd_rdp_buffer_free (GrdRdpBuffer *buffer);
 
@@ -56,10 +60,4 @@ void grd_rdp_buffer_unmap_resources (GrdRdpBuffer *buffer);
 
 void grd_rdp_buffer_release (GrdRdpBuffer *buffer);
 
-gboolean grd_rdp_buffer_resize (GrdRdpBuffer *buffer,
-                                uint32_t      width,
-                                uint32_t      height,
-                                uint32_t      stride,
-                                gboolean      preallocate_on_gpu);
-
 #endif /* GRD_RDP_BUFFER_H */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]