From 7725f3c11a7a601848ae5d24d3199360c5b49d96 Mon Sep 17 00:00:00 2001 From: Pascal Nowack Date: Tue, 29 Nov 2022 15:20:32 +0100 Subject: [PATCH 1/9] rdp-buffer: Add getters to retrieve buffer attributes This is a preparatory step in making the buffer struct private. --- src/grd-rdp-buffer.c | 30 ++++++++++++++++++++++++++++++ src/grd-rdp-buffer.h | 10 ++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/grd-rdp-buffer.c b/src/grd-rdp-buffer.c index 9439f559..4fec24ed 100644 --- a/src/grd-rdp-buffer.c +++ b/src/grd-rdp-buffer.c @@ -185,6 +185,36 @@ grd_rdp_buffer_free (GrdRdpBuffer *buffer) g_free (buffer); } +uint32_t +grd_rdp_buffer_get_width (GrdRdpBuffer *rdp_buffer) +{ + return rdp_buffer->width; +} + +uint32_t +grd_rdp_buffer_get_height (GrdRdpBuffer *rdp_buffer) +{ + return rdp_buffer->height; +} + +uint8_t * +grd_rdp_buffer_get_local_data (GrdRdpBuffer *rdp_buffer) +{ + return rdp_buffer->local_data; +} + +uint32_t +grd_rdp_buffer_get_pbo (GrdRdpBuffer *rdp_buffer) +{ + return rdp_buffer->pbo; +} + +CUdeviceptr +grd_rdp_buffer_get_mapped_cuda_pointer (GrdRdpBuffer *rdp_buffer) +{ + return rdp_buffer->mapped_cuda_pointer; +} + static gboolean cuda_unmap_resource (gpointer user_data) { diff --git a/src/grd-rdp-buffer.h b/src/grd-rdp-buffer.h index 5ff689f5..e213a9e3 100644 --- a/src/grd-rdp-buffer.h +++ b/src/grd-rdp-buffer.h @@ -56,6 +56,16 @@ GrdRdpBuffer *grd_rdp_buffer_new (GrdRdpBufferPool *buffer_pool, void grd_rdp_buffer_free (GrdRdpBuffer *buffer); +uint32_t grd_rdp_buffer_get_width (GrdRdpBuffer *rdp_buffer); + +uint32_t grd_rdp_buffer_get_height (GrdRdpBuffer *rdp_buffer); + +uint8_t *grd_rdp_buffer_get_local_data (GrdRdpBuffer *rdp_buffer); + +uint32_t grd_rdp_buffer_get_pbo (GrdRdpBuffer *rdp_buffer); + +CUdeviceptr grd_rdp_buffer_get_mapped_cuda_pointer (GrdRdpBuffer *rdp_buffer); + void grd_rdp_buffer_unmap_resources (GrdRdpBuffer *buffer); void grd_rdp_buffer_release (GrdRdpBuffer *buffer); -- GitLab From 5ad6b0a2d88a0cb18a1fc9b1aacdae062ca90ddb Mon Sep 17 00:00:00 2001 From: Pascal Nowack Date: Tue, 29 Nov 2022 15:30:32 +0100 Subject: [PATCH 2/9] rdp-buffer: Rename unmap_resources to queue_resource_unmap The task is not directly done, but actually queued to the EGL thread. Also invert the is-mapped-condition for more readability of the code. --- src/grd-rdp-buffer-pool.c | 2 +- src/grd-rdp-buffer.c | 52 +++++++++++++++++++-------------------- src/grd-rdp-buffer.h | 2 +- 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/grd-rdp-buffer-pool.c b/src/grd-rdp-buffer-pool.c index c4a5dcbe..ab6c3b3f 100644 --- a/src/grd-rdp-buffer-pool.c +++ b/src/grd-rdp-buffer-pool.c @@ -250,7 +250,7 @@ unmap_untaken_buffers (gpointer user_data) (gpointer *) &buffer_info)) { if (!buffer_info->buffer_taken) - grd_rdp_buffer_unmap_resources (buffer); + grd_rdp_buffer_queue_resource_unmap (buffer); } g_mutex_unlock (&buffer_pool->pool_mutex); diff --git a/src/grd-rdp-buffer.c b/src/grd-rdp-buffer.c index 4fec24ed..3554a515 100644 --- a/src/grd-rdp-buffer.c +++ b/src/grd-rdp-buffer.c @@ -215,6 +215,12 @@ grd_rdp_buffer_get_mapped_cuda_pointer (GrdRdpBuffer *rdp_buffer) return rdp_buffer->mapped_cuda_pointer; } +void +grd_rdp_buffer_release (GrdRdpBuffer *buffer) +{ + grd_rdp_buffer_pool_release_buffer (buffer->buffer_pool, buffer); +} + static gboolean cuda_unmap_resource (gpointer user_data) { @@ -228,31 +234,25 @@ cuda_unmap_resource (gpointer user_data) } void -grd_rdp_buffer_unmap_resources (GrdRdpBuffer *buffer) +grd_rdp_buffer_queue_resource_unmap (GrdRdpBuffer *rdp_buffer) { - if (buffer->mapped_cuda_pointer) - { - UnmapBufferData *data; - - data = g_new0 (UnmapBufferData, 1); - data->hwaccel_nvidia = buffer->hwaccel_nvidia; - data->cuda_resource = buffer->cuda_resource; - data->cuda_stream = buffer->cuda_stream; - grd_egl_thread_run_custom_task (buffer->egl_thread, - cuda_unmap_resource, - data, - NULL, data, g_free); - - /* - * The mapped CUDA pointer indicates whether the resource is mapped, but - * it is itself not needed to unmap the resource. - */ - buffer->mapped_cuda_pointer = 0; - } -} - -void -grd_rdp_buffer_release (GrdRdpBuffer *buffer) -{ - grd_rdp_buffer_pool_release_buffer (buffer->buffer_pool, buffer); + UnmapBufferData *data; + + if (!rdp_buffer->mapped_cuda_pointer) + return; + + data = g_new0 (UnmapBufferData, 1); + data->hwaccel_nvidia = rdp_buffer->hwaccel_nvidia; + data->cuda_resource = rdp_buffer->cuda_resource; + data->cuda_stream = rdp_buffer->cuda_stream; + grd_egl_thread_run_custom_task (rdp_buffer->egl_thread, + cuda_unmap_resource, + data, + NULL, data, g_free); + + /* + * The mapped CUDA pointer indicates whether the resource is mapped, but + * it is itself not needed to unmap the resource. + */ + rdp_buffer->mapped_cuda_pointer = 0; } diff --git a/src/grd-rdp-buffer.h b/src/grd-rdp-buffer.h index e213a9e3..6877b097 100644 --- a/src/grd-rdp-buffer.h +++ b/src/grd-rdp-buffer.h @@ -66,7 +66,7 @@ uint32_t grd_rdp_buffer_get_pbo (GrdRdpBuffer *rdp_buffer); CUdeviceptr grd_rdp_buffer_get_mapped_cuda_pointer (GrdRdpBuffer *rdp_buffer); -void grd_rdp_buffer_unmap_resources (GrdRdpBuffer *buffer); +void grd_rdp_buffer_queue_resource_unmap (GrdRdpBuffer *rdp_buffer); void grd_rdp_buffer_release (GrdRdpBuffer *buffer); -- GitLab From 783163f94cae435c4628f3eb2e30145c37d59721 Mon Sep 17 00:00:00 2001 From: Pascal Nowack Date: Tue, 29 Nov 2022 15:33:29 +0100 Subject: [PATCH 3/9] rdp-buffer: Add API to actually unmap the CUDA resource This allows us later to hide the buffer struct internals, while keeping its functionality. The actual code was just copied from the rdp-pipewire-stream class. --- src/grd-rdp-buffer.c | 12 ++++++++++++ src/grd-rdp-buffer.h | 2 ++ 2 files changed, 14 insertions(+) diff --git a/src/grd-rdp-buffer.c b/src/grd-rdp-buffer.c index 3554a515..77b599c4 100644 --- a/src/grd-rdp-buffer.c +++ b/src/grd-rdp-buffer.c @@ -221,6 +221,18 @@ grd_rdp_buffer_release (GrdRdpBuffer *buffer) grd_rdp_buffer_pool_release_buffer (buffer->buffer_pool, buffer); } +void +grd_rdp_buffer_unmap_cuda_resource (GrdRdpBuffer *rdp_buffer) +{ + if (!rdp_buffer->mapped_cuda_pointer) + return; + + grd_hwaccel_nvidia_unmap_cuda_resource (rdp_buffer->hwaccel_nvidia, + rdp_buffer->cuda_resource, + rdp_buffer->cuda_stream); + rdp_buffer->mapped_cuda_pointer = 0; +} + static gboolean cuda_unmap_resource (gpointer user_data) { diff --git a/src/grd-rdp-buffer.h b/src/grd-rdp-buffer.h index 6877b097..85caaca1 100644 --- a/src/grd-rdp-buffer.h +++ b/src/grd-rdp-buffer.h @@ -66,6 +66,8 @@ uint32_t grd_rdp_buffer_get_pbo (GrdRdpBuffer *rdp_buffer); CUdeviceptr grd_rdp_buffer_get_mapped_cuda_pointer (GrdRdpBuffer *rdp_buffer); +void grd_rdp_buffer_unmap_cuda_resource (GrdRdpBuffer *rdp_buffer); + void grd_rdp_buffer_queue_resource_unmap (GrdRdpBuffer *rdp_buffer); void grd_rdp_buffer_release (GrdRdpBuffer *buffer); -- GitLab From 1539eb76b02c6f88e6f685eae5bb8356768fdf8c Mon Sep 17 00:00:00 2001 From: Pascal Nowack Date: Tue, 29 Nov 2022 15:53:22 +0100 Subject: [PATCH 4/9] rdp-buffer: Add API to map CUDA resource This allows us later to hide the buffer struct internals, while keeping its functionality. The actual code was just copied from the rdp-pipewire-stream class. --- src/grd-rdp-buffer.c | 12 ++++++++++++ src/grd-rdp-buffer.h | 2 ++ 2 files changed, 14 insertions(+) diff --git a/src/grd-rdp-buffer.c b/src/grd-rdp-buffer.c index 77b599c4..13231066 100644 --- a/src/grd-rdp-buffer.c +++ b/src/grd-rdp-buffer.c @@ -221,6 +221,18 @@ grd_rdp_buffer_release (GrdRdpBuffer *buffer) grd_rdp_buffer_pool_release_buffer (buffer->buffer_pool, buffer); } +gboolean +grd_rdp_buffer_map_cuda_resource (GrdRdpBuffer *rdp_buffer) +{ + size_t mapped_size = 0; + + return grd_hwaccel_nvidia_map_cuda_resource (rdp_buffer->hwaccel_nvidia, + rdp_buffer->cuda_resource, + &rdp_buffer->mapped_cuda_pointer, + &mapped_size, + rdp_buffer->cuda_stream); +} + void grd_rdp_buffer_unmap_cuda_resource (GrdRdpBuffer *rdp_buffer) { diff --git a/src/grd-rdp-buffer.h b/src/grd-rdp-buffer.h index 85caaca1..7157ba1d 100644 --- a/src/grd-rdp-buffer.h +++ b/src/grd-rdp-buffer.h @@ -66,6 +66,8 @@ uint32_t grd_rdp_buffer_get_pbo (GrdRdpBuffer *rdp_buffer); CUdeviceptr grd_rdp_buffer_get_mapped_cuda_pointer (GrdRdpBuffer *rdp_buffer); +gboolean grd_rdp_buffer_map_cuda_resource (GrdRdpBuffer *rdp_buffer); + void grd_rdp_buffer_unmap_cuda_resource (GrdRdpBuffer *rdp_buffer); void grd_rdp_buffer_queue_resource_unmap (GrdRdpBuffer *rdp_buffer); -- GitLab From 88d08075cf3f3b5d19db840681e6e2c1291a4207 Mon Sep 17 00:00:00 2001 From: Pascal Nowack Date: Tue, 29 Nov 2022 15:56:27 +0100 Subject: [PATCH 5/9] rdp-buffer: Add API to register read-only GL buffer This allows us later to hide the buffer struct internals, while keeping its functionality. The actual code was just copied from the rdp-pipewire-stream class. --- src/grd-rdp-buffer.c | 16 ++++++++++++++++ src/grd-rdp-buffer.h | 3 +++ 2 files changed, 19 insertions(+) diff --git a/src/grd-rdp-buffer.c b/src/grd-rdp-buffer.c index 13231066..7328b044 100644 --- a/src/grd-rdp-buffer.c +++ b/src/grd-rdp-buffer.c @@ -221,6 +221,22 @@ grd_rdp_buffer_release (GrdRdpBuffer *buffer) grd_rdp_buffer_pool_release_buffer (buffer->buffer_pool, buffer); } +gboolean +grd_rdp_buffer_register_read_only_gl_buffer (GrdRdpBuffer *rdp_buffer, + uint32_t pbo) +{ + gboolean success; + + success = + grd_hwaccel_nvidia_register_read_only_gl_buffer (rdp_buffer->hwaccel_nvidia, + &rdp_buffer->cuda_resource, + pbo); + if (success) + rdp_buffer->pbo = pbo; + + return success; +} + gboolean grd_rdp_buffer_map_cuda_resource (GrdRdpBuffer *rdp_buffer) { diff --git a/src/grd-rdp-buffer.h b/src/grd-rdp-buffer.h index 7157ba1d..2c78421a 100644 --- a/src/grd-rdp-buffer.h +++ b/src/grd-rdp-buffer.h @@ -66,6 +66,9 @@ uint32_t grd_rdp_buffer_get_pbo (GrdRdpBuffer *rdp_buffer); CUdeviceptr grd_rdp_buffer_get_mapped_cuda_pointer (GrdRdpBuffer *rdp_buffer); +gboolean grd_rdp_buffer_register_read_only_gl_buffer (GrdRdpBuffer *rdp_buffer, + uint32_t pbo); + gboolean grd_rdp_buffer_map_cuda_resource (GrdRdpBuffer *rdp_buffer); void grd_rdp_buffer_unmap_cuda_resource (GrdRdpBuffer *rdp_buffer); -- GitLab From be0091b7f57684f444597a02b77cc259fe2933eb Mon Sep 17 00:00:00 2001 From: Pascal Nowack Date: Tue, 29 Nov 2022 16:24:50 +0100 Subject: [PATCH 6/9] rdp: Avoid direct usage of the attributes of RDP buffers With the previously added APIs to access some of the buffer internals, avoid now the direct usage of any buffer attributes by using the buffer API methods instead. --- src/grd-rdp-buffer-pool.c | 2 +- src/grd-rdp-damage-detector-cuda.c | 10 ++++-- src/grd-rdp-damage-detector-memcmp.c | 6 ++-- src/grd-rdp-graphics-pipeline.c | 5 +-- src/grd-rdp-pipewire-stream.c | 52 +++++++--------------------- src/grd-session-rdp.c | 10 +++--- 6 files changed, 32 insertions(+), 53 deletions(-) diff --git a/src/grd-rdp-buffer-pool.c b/src/grd-rdp-buffer-pool.c index ab6c3b3f..847333fb 100644 --- a/src/grd-rdp-buffer-pool.c +++ b/src/grd-rdp-buffer-pool.c @@ -119,7 +119,7 @@ grd_rdp_buffer_pool_resize_buffers (GrdRdpBufferPool *buffer_pool, static gboolean buffer_has_mapped_data (GrdRdpBuffer *buffer) { - if (buffer->mapped_cuda_pointer) + if (grd_rdp_buffer_get_mapped_cuda_pointer (buffer)) return TRUE; return FALSE; diff --git a/src/grd-rdp-damage-detector-cuda.c b/src/grd-rdp-damage-detector-cuda.c index 74f07977..e828de93 100644 --- a/src/grd-rdp-damage-detector-cuda.c +++ b/src/grd-rdp-damage-detector-cuda.c @@ -151,6 +151,8 @@ submit_new_framebuffer (GrdRdpDamageDetector *detector, CudaFunctions *cuda_funcs = detector_cuda->cuda_funcs; uint32_t surface_width = detector_cuda->surface_width; uint32_t surface_height = detector_cuda->surface_height; + CUdeviceptr current_data; + CUdeviceptr previous_data; unsigned int grid_dim_x, grid_dim_y, grid_dim_z; unsigned int block_dim_x, block_dim_y, block_dim_z; void *args[8]; @@ -181,6 +183,10 @@ submit_new_framebuffer (GrdRdpDamageDetector *detector, return FALSE; } + current_data = grd_rdp_buffer_get_mapped_cuda_pointer (buffer); + previous_data = + grd_rdp_buffer_get_mapped_cuda_pointer (detector_cuda->last_framebuffer); + /* Threads per blocks */ block_dim_x = 32; block_dim_y = 16; @@ -194,8 +200,8 @@ submit_new_framebuffer (GrdRdpDamageDetector *detector, args[0] = &detector_cuda->damage_array; args[1] = &detector_cuda->region_is_damaged; - args[2] = &buffer->mapped_cuda_pointer; - args[3] = &detector_cuda->last_framebuffer->mapped_cuda_pointer; + args[2] = ¤t_data; + args[3] = &previous_data; args[4] = &surface_width; args[5] = &surface_width; args[6] = &surface_height; diff --git a/src/grd-rdp-damage-detector-memcmp.c b/src/grd-rdp-damage-detector-memcmp.c index e28d5153..33a15231 100644 --- a/src/grd-rdp-damage-detector-memcmp.c +++ b/src/grd-rdp-damage-detector-memcmp.c @@ -123,6 +123,7 @@ submit_new_framebuffer (GrdRdpDamageDetector *detector, { for (x = 0; x < detector_memcmp->cols; ++x) { + GrdRdpBuffer *last_framebuffer = detector_memcmp->last_framebuffer; cairo_rectangle_int_t tile; uint8_t tile_damaged = 0; @@ -133,9 +134,8 @@ submit_new_framebuffer (GrdRdpDamageDetector *detector, tile.height = surface_height - tile.y < TILE_HEIGHT ? surface_height - tile.y : TILE_HEIGHT; - if (grd_is_tile_dirty (&tile, - buffer->local_data, - detector_memcmp->last_framebuffer->local_data, + if (grd_is_tile_dirty (&tile, grd_rdp_buffer_get_local_data (buffer), + grd_rdp_buffer_get_local_data (last_framebuffer), surface_width * 4, 4)) { tile_damaged = 1; diff --git a/src/grd-rdp-graphics-pipeline.c b/src/grd-rdp-graphics-pipeline.c index afab6452..ef1670ef 100644 --- a/src/grd-rdp-graphics-pipeline.c +++ b/src/grd-rdp-graphics-pipeline.c @@ -551,6 +551,7 @@ refresh_gfx_surface_avc420 (GrdRdpGraphicsPipeline *graphics_pipeline, grd_rdp_gfx_surface_get_render_surface (gfx_surface); GrdRdpGfxFrameController *frame_controller = grd_rdp_gfx_surface_get_frame_controller (gfx_surface); + CUdeviceptr src_data = grd_rdp_buffer_get_mapped_cuda_pointer (buffer); RDPGFX_SURFACE_COMMAND cmd = {0}; RDPGFX_START_FRAME_PDU cmd_start = {0}; RDPGFX_END_FRAME_PDU cmd_end = {0}; @@ -577,7 +578,7 @@ refresh_gfx_surface_avc420 (GrdRdpGraphicsPipeline *graphics_pipeline, if (!grd_hwaccel_nvidia_avc420_encode_bgrx_frame (graphics_pipeline->hwaccel_nvidia, hwaccel_context->encode_session_id, - buffer->mapped_cuda_pointer, + src_data, &rdp_surface->avc.main_view, surface_width, surface_height, aligned_width, aligned_height, @@ -900,7 +901,7 @@ refresh_gfx_surface_rfx_progressive (GrdRdpGraphicsPipeline *graphics_pipeline, rfx_message = rfx_encode_message (graphics_pipeline->rfx_context, rfx_rects, n_rects, - buffer->local_data, + grd_rdp_buffer_get_local_data (buffer), surface_width, surface_height, src_stride); diff --git a/src/grd-rdp-pipewire-stream.c b/src/grd-rdp-pipewire-stream.c index edc09f6d..11146635 100644 --- a/src/grd-rdp-pipewire-stream.c +++ b/src/grd-rdp-pipewire-stream.c @@ -82,19 +82,16 @@ struct _GrdRdpFrame typedef struct { - GrdHwAccelNvidia *hwaccel_nvidia; GrdRdpBuffer *rdp_buffer; } UnmapBufferData; typedef struct { - GrdHwAccelNvidia *hwaccel_nvidia; GrdRdpBuffer *rdp_buffer; } AllocateBufferData; typedef struct { - GrdHwAccelNvidia *hwaccel_nvidia; GrdRdpBuffer *rdp_buffer; } RealizeBufferData; @@ -465,7 +462,7 @@ copy_frame_data (GrdRdpFrame *frame, for (y = 0; y < height; ++y) { - memcpy (((uint8_t *) buffer->local_data) + y * dst_stride, + memcpy (grd_rdp_buffer_get_local_data (buffer) + y * dst_stride, ((uint8_t *) src_data) + y * src_stride, width * 4); } @@ -476,26 +473,18 @@ cuda_unmap_resource (gpointer user_data) { UnmapBufferData *data = user_data; - if (!data->rdp_buffer->mapped_cuda_pointer) - return TRUE; - - grd_hwaccel_nvidia_unmap_cuda_resource (data->hwaccel_nvidia, - data->rdp_buffer->cuda_resource, - data->rdp_buffer->cuda_stream); - data->rdp_buffer->mapped_cuda_pointer = 0; + grd_rdp_buffer_unmap_cuda_resource (data->rdp_buffer); return TRUE; } static void -unmap_cuda_resources (GrdEglThread *egl_thread, - GrdHwAccelNvidia *hwaccel_nvidia, - GrdRdpBuffer *rdp_buffer) +unmap_cuda_resources (GrdEglThread *egl_thread, + GrdRdpBuffer *rdp_buffer) { UnmapBufferData *data; data = g_new0 (UnmapBufferData, 1); - data->hwaccel_nvidia = hwaccel_nvidia; data->rdp_buffer = rdp_buffer; grd_egl_thread_run_custom_task (egl_thread, @@ -509,15 +498,8 @@ cuda_allocate_buffer (gpointer user_data, { AllocateBufferData *data = user_data; GrdRdpBuffer *rdp_buffer = data->rdp_buffer; - gboolean success; - - success = grd_hwaccel_nvidia_register_read_only_gl_buffer (data->hwaccel_nvidia, - &rdp_buffer->cuda_resource, - pbo); - if (success) - rdp_buffer->pbo = pbo; - return success; + return grd_rdp_buffer_register_read_only_gl_buffer (rdp_buffer, pbo); } static gboolean @@ -534,13 +516,8 @@ cuda_map_resource (gpointer user_data) { RealizeBufferData *data = user_data; GrdRdpBuffer *rdp_buffer = data->rdp_buffer; - size_t mapped_size = 0; - return grd_hwaccel_nvidia_map_cuda_resource (data->hwaccel_nvidia, - rdp_buffer->cuda_resource, - &rdp_buffer->mapped_cuda_pointer, - &mapped_size, - rdp_buffer->cuda_stream); + return grd_rdp_buffer_map_cuda_resource (rdp_buffer); } static gboolean @@ -621,7 +598,7 @@ process_frame_data (GrdRdpPipeWireStream *stream, return; } rdp_buffer = frame->buffer; - pbo = rdp_buffer->pbo; + pbo = grd_rdp_buffer_get_pbo (rdp_buffer); if (stream->rdp_surface->needs_no_local_data && src_stride == dst_stride) @@ -643,7 +620,7 @@ process_frame_data (GrdRdpPipeWireStream *stream, munmap (map, size); - data_to_upload = rdp_buffer->local_data; + data_to_upload = grd_rdp_buffer_get_local_data (rdp_buffer); } if (!hwaccel_nvidia) @@ -652,14 +629,12 @@ process_frame_data (GrdRdpPipeWireStream *stream, return; } - unmap_cuda_resources (egl_thread, hwaccel_nvidia, rdp_buffer); + unmap_cuda_resources (egl_thread, rdp_buffer); allocate_buffer_data = g_new0 (AllocateBufferData, 1); - allocate_buffer_data->hwaccel_nvidia = hwaccel_nvidia; allocate_buffer_data->rdp_buffer = rdp_buffer; realize_buffer_data = g_new0 (RealizeBufferData, 1); - realize_buffer_data->hwaccel_nvidia = hwaccel_nvidia; realize_buffer_data->rdp_buffer = rdp_buffer; grd_egl_thread_upload (egl_thread, @@ -724,27 +699,24 @@ process_frame_data (GrdRdpPipeWireStream *stream, } if (!stream->rdp_surface->needs_no_local_data) - dst_data = frame->buffer->local_data; + dst_data = grd_rdp_buffer_get_local_data (frame->buffer); if (hwaccel_nvidia) { - unmap_cuda_resources (egl_thread, hwaccel_nvidia, rdp_buffer); + unmap_cuda_resources (egl_thread, rdp_buffer); iface.allocate = allocate_buffer; iface.realize = realize_buffer; import_buffer_data = g_new0 (ImportBufferData, 1); - import_buffer_data->allocate.hwaccel_nvidia = hwaccel_nvidia; import_buffer_data->allocate.rdp_buffer = rdp_buffer; - - import_buffer_data->realize.hwaccel_nvidia = hwaccel_nvidia; import_buffer_data->realize.rdp_buffer = rdp_buffer; import_buffer_data->rdp_buffer = rdp_buffer; } grd_egl_thread_download (egl_thread, - rdp_buffer->pbo, + grd_rdp_buffer_get_pbo (rdp_buffer), height, dst_stride, hwaccel_nvidia ? &iface : NULL, diff --git a/src/grd-session-rdp.c b/src/grd-session-rdp.c index 718f3c8b..ae21e120 100644 --- a/src/grd-session-rdp.c +++ b/src/grd-session-rdp.c @@ -289,8 +289,8 @@ take_or_encode_frame (GrdSessionRdp *session_rdp, GrdRdpBuffer *buffer) { SessionMetrics *session_metrics = &session_rdp->session_metrics; - uint16_t width = buffer->width; - uint16_t height = buffer->height; + uint16_t width = grd_rdp_buffer_get_width (buffer); + uint16_t height = grd_rdp_buffer_get_height (buffer); if (!session_metrics->received_first_frame) { @@ -779,7 +779,7 @@ rdp_peer_refresh_rfx (GrdSessionRdp *session_rdp, RdpPeerContext *rdp_peer_context = (RdpPeerContext *) peer->context; rdpSettings *rdp_settings = peer->settings; rdpUpdate *rdp_update = peer->update; - uint8_t *data = buffer->local_data; + uint8_t *data = grd_rdp_buffer_get_local_data (buffer); uint32_t src_stride = grd_session_rdp_get_stride_for_width (session_rdp, rdp_surface->width); SURFACE_BITS_COMMAND cmd = {0}; @@ -929,7 +929,7 @@ rdp_peer_refresh_nsc (GrdSessionRdp *session_rdp, RdpPeerContext *rdp_peer_context = (RdpPeerContext *) peer->context; rdpSettings *rdp_settings = peer->settings; rdpUpdate *rdp_update = peer->update; - uint8_t *data = buffer->local_data; + uint8_t *data = grd_rdp_buffer_get_local_data (buffer); uint32_t src_stride = grd_session_rdp_get_stride_for_width (session_rdp, rdp_surface->width); NSCThreadPoolContext *thread_pool_context = @@ -1174,7 +1174,7 @@ rdp_peer_refresh_raw (GrdSessionRdp *session_rdp, RdpPeerContext *rdp_peer_context = (RdpPeerContext *) peer->context; rdpSettings *rdp_settings = peer->settings; rdpUpdate *rdp_update = peer->update; - uint8_t *data = buffer->local_data; + uint8_t *data = grd_rdp_buffer_get_local_data (buffer); uint32_t src_stride = grd_session_rdp_get_stride_for_width (session_rdp, rdp_surface->width); RawThreadPoolContext *thread_pool_context = -- GitLab From 7c3696bff34e2bbcae31aa859c066e23fd629619 Mon Sep 17 00:00:00 2001 From: Pascal Nowack Date: Tue, 29 Nov 2022 16:47:30 +0100 Subject: [PATCH 7/9] rdp-buffer: Make buffer struct private Now that the buffer attributes are no longer used directly, make the buffer struct private. --- src/grd-rdp-buffer.c | 100 ++++++++++++++++++++++++------------------- src/grd-rdp-buffer.h | 26 ++--------- 2 files changed, 60 insertions(+), 66 deletions(-) diff --git a/src/grd-rdp-buffer.c b/src/grd-rdp-buffer.c index 7328b044..f474069a 100644 --- a/src/grd-rdp-buffer.c +++ b/src/grd-rdp-buffer.c @@ -21,8 +21,6 @@ #include "grd-rdp-buffer.h" -#include - #include "grd-egl-thread.h" #include "grd-hwaccel-nvidia.h" #include "grd-rdp-buffer-pool.h" @@ -39,7 +37,7 @@ typedef struct typedef struct { GrdHwAccelNvidia *hwaccel_nvidia; - GrdRdpBuffer *buffer; + GrdRdpBuffer *rdp_buffer; } AllocateBufferData; typedef struct @@ -49,19 +47,38 @@ typedef struct CUstream cuda_stream; } UnmapBufferData; +struct _GrdRdpBuffer +{ + GrdRdpBufferPool *buffer_pool; + + GrdEglThread *egl_thread; + GrdHwAccelNvidia *hwaccel_nvidia; + + uint32_t width; + uint32_t height; + + uint8_t *local_data; + + uint32_t pbo; + + CUgraphicsResource cuda_resource; + CUstream cuda_stream; + CUdeviceptr mapped_cuda_pointer; +}; + static gboolean cuda_allocate_buffer (gpointer user_data, uint32_t pbo) { AllocateBufferData *data = user_data; - GrdRdpBuffer *buffer = data->buffer; + GrdRdpBuffer *rdp_buffer = data->rdp_buffer; gboolean success; success = grd_hwaccel_nvidia_register_read_only_gl_buffer (data->hwaccel_nvidia, - &buffer->cuda_resource, + &rdp_buffer->cuda_resource, pbo); if (success) - buffer->pbo = pbo; + rdp_buffer->pbo = pbo; return success; } @@ -90,34 +107,34 @@ grd_rdp_buffer_new (GrdRdpBufferPool *buffer_pool, uint32_t stride, gboolean preallocate_on_gpu) { - GrdRdpBuffer *buffer; + g_autoptr (GrdRdpBuffer) rdp_buffer = NULL; gboolean success = TRUE; - buffer = g_new0 (GrdRdpBuffer, 1); - buffer->buffer_pool = buffer_pool; - buffer->egl_thread = egl_thread; - buffer->hwaccel_nvidia = hwaccel_nvidia; + rdp_buffer = g_new0 (GrdRdpBuffer, 1); + rdp_buffer->buffer_pool = buffer_pool; + rdp_buffer->egl_thread = egl_thread; + rdp_buffer->hwaccel_nvidia = hwaccel_nvidia; - buffer->cuda_stream = cuda_stream; + rdp_buffer->cuda_stream = cuda_stream; - buffer->width = width; - buffer->height = height; - buffer->local_data = g_malloc0 (stride * height * sizeof (uint8_t)); + rdp_buffer->width = width; + rdp_buffer->height = height; + rdp_buffer->local_data = g_malloc0 (stride * height * sizeof (uint8_t)); if (preallocate_on_gpu && - buffer->hwaccel_nvidia) + rdp_buffer->hwaccel_nvidia) { AllocateBufferData data = {}; GrdSyncPoint sync_point = {}; - g_assert (buffer->egl_thread); + g_assert (rdp_buffer->egl_thread); grd_sync_point_init (&sync_point); - data.hwaccel_nvidia = buffer->hwaccel_nvidia; - data.buffer = buffer; + data.hwaccel_nvidia = rdp_buffer->hwaccel_nvidia; + data.rdp_buffer = rdp_buffer; - grd_egl_thread_allocate (buffer->egl_thread, - buffer->height, + grd_egl_thread_allocate (rdp_buffer->egl_thread, + rdp_buffer->height, stride, cuda_allocate_buffer, &data, @@ -130,9 +147,9 @@ grd_rdp_buffer_new (GrdRdpBufferPool *buffer_pool, } if (!success) - g_clear_pointer (&buffer, grd_rdp_buffer_free); + return NULL; - return buffer; + return g_steal_pointer (&rdp_buffer); } static void @@ -152,37 +169,32 @@ cuda_deallocate_buffer (gpointer user_data) data->cuda_stream); } -static void -clear_buffers (GrdRdpBuffer *buffer) +void +grd_rdp_buffer_free (GrdRdpBuffer *rdp_buffer) { - if (buffer->cuda_resource) + if (rdp_buffer->cuda_resource) { ClearBufferData *data; data = g_new0 (ClearBufferData, 1); - data->hwaccel_nvidia = buffer->hwaccel_nvidia; - data->cuda_resource = buffer->cuda_resource; - data->cuda_stream = buffer->cuda_stream; - data->is_mapped = !!buffer->mapped_cuda_pointer; - grd_egl_thread_deallocate (buffer->egl_thread, - buffer->pbo, + data->hwaccel_nvidia = rdp_buffer->hwaccel_nvidia; + data->cuda_resource = rdp_buffer->cuda_resource; + data->cuda_stream = rdp_buffer->cuda_stream; + data->is_mapped = !!rdp_buffer->mapped_cuda_pointer; + grd_egl_thread_deallocate (rdp_buffer->egl_thread, + rdp_buffer->pbo, cuda_deallocate_buffer, data, NULL, data, g_free); - buffer->mapped_cuda_pointer = 0; - buffer->cuda_resource = NULL; - buffer->pbo = 0; + rdp_buffer->mapped_cuda_pointer = 0; + rdp_buffer->cuda_resource = NULL; + rdp_buffer->pbo = 0; } - g_clear_pointer (&buffer->local_data, g_free); -} + g_clear_pointer (&rdp_buffer->local_data, g_free); -void -grd_rdp_buffer_free (GrdRdpBuffer *buffer) -{ - clear_buffers (buffer); - g_free (buffer); + g_free (rdp_buffer); } uint32_t @@ -216,9 +228,9 @@ grd_rdp_buffer_get_mapped_cuda_pointer (GrdRdpBuffer *rdp_buffer) } void -grd_rdp_buffer_release (GrdRdpBuffer *buffer) +grd_rdp_buffer_release (GrdRdpBuffer *rdp_buffer) { - grd_rdp_buffer_pool_release_buffer (buffer->buffer_pool, buffer); + grd_rdp_buffer_pool_release_buffer (rdp_buffer->buffer_pool, rdp_buffer); } gboolean diff --git a/src/grd-rdp-buffer.h b/src/grd-rdp-buffer.h index 2c78421a..68dd8bf0 100644 --- a/src/grd-rdp-buffer.h +++ b/src/grd-rdp-buffer.h @@ -22,29 +22,9 @@ #include #include -#include #include "grd-types.h" -struct _GrdRdpBuffer -{ - GrdRdpBufferPool *buffer_pool; - - GrdEglThread *egl_thread; - GrdHwAccelNvidia *hwaccel_nvidia; - - uint32_t width; - uint32_t height; - - uint8_t *local_data; - - uint32_t pbo; - - CUgraphicsResource cuda_resource; - CUstream cuda_stream; - CUdeviceptr mapped_cuda_pointer; -}; - GrdRdpBuffer *grd_rdp_buffer_new (GrdRdpBufferPool *buffer_pool, GrdEglThread *egl_thread, GrdHwAccelNvidia *hwaccel_nvidia, @@ -54,7 +34,7 @@ GrdRdpBuffer *grd_rdp_buffer_new (GrdRdpBufferPool *buffer_pool, uint32_t stride, gboolean preallocate_on_gpu); -void grd_rdp_buffer_free (GrdRdpBuffer *buffer); +void grd_rdp_buffer_free (GrdRdpBuffer *rdp_buffer); uint32_t grd_rdp_buffer_get_width (GrdRdpBuffer *rdp_buffer); @@ -66,6 +46,8 @@ uint32_t grd_rdp_buffer_get_pbo (GrdRdpBuffer *rdp_buffer); CUdeviceptr grd_rdp_buffer_get_mapped_cuda_pointer (GrdRdpBuffer *rdp_buffer); +void grd_rdp_buffer_release (GrdRdpBuffer *rdp_buffer); + gboolean grd_rdp_buffer_register_read_only_gl_buffer (GrdRdpBuffer *rdp_buffer, uint32_t pbo); @@ -75,6 +57,6 @@ void grd_rdp_buffer_unmap_cuda_resource (GrdRdpBuffer *rdp_buffer); void grd_rdp_buffer_queue_resource_unmap (GrdRdpBuffer *rdp_buffer); -void grd_rdp_buffer_release (GrdRdpBuffer *buffer); +G_DEFINE_AUTOPTR_CLEANUP_FUNC (GrdRdpBuffer, grd_rdp_buffer_free) #endif /* GRD_RDP_BUFFER_H */ -- GitLab From b599861a0543fb165ff81fd4729b99d693b08908 Mon Sep 17 00:00:00 2001 From: Pascal Nowack Date: Thu, 8 Dec 2022 09:56:06 +0100 Subject: [PATCH 8/9] egl-thread: Remove unused callback context --- src/grd-egl-thread.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/grd-egl-thread.c b/src/grd-egl-thread.c index 4b36cccd..b942a64a 100644 --- a/src/grd-egl-thread.c +++ b/src/grd-egl-thread.c @@ -56,7 +56,6 @@ typedef struct _GrdEglTask GFunc func; GDestroyNotify destroy; - GMainContext *callback_context; GrdEglThreadCallback callback; gpointer callback_user_data; GDestroyNotify callback_destroy; -- GitLab From 4750effe9e9f40df8edfa7c949b016be7c26f549 Mon Sep 17 00:00:00 2001 From: Pascal Nowack Date: Fri, 9 Dec 2022 08:30:32 +0100 Subject: [PATCH 9/9] egl-thread: Remove unused typedef --- src/grd-egl-thread.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/grd-egl-thread.c b/src/grd-egl-thread.c index b942a64a..ebff9605 100644 --- a/src/grd-egl-thread.c +++ b/src/grd-egl-thread.c @@ -139,9 +139,6 @@ typedef struct _GrdEglTaskDownload uint64_t *modifiers; } GrdEglTaskDownload; -typedef void (* GrdEglFrameReady) (uint8_t *data, - gpointer user_data); - static gboolean is_hardware_accelerated (void) { -- GitLab