From 4d358762948dccdcac6d01f4dd7b8635e53de979 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jonas=20=C3=85dahl?= <jadahl@gmail.com>
Date: Mon, 12 Oct 2020 17:34:30 +0200
Subject: [PATCH] vnc: Copy pixels using the right destination stride

We're copying the pixels in a separate thread managed by PipeWire, and
in this thread, accessing the VNC framebuffer dimension and stride is
racy. Instead of fetching the dimension directly, pass the expected
width and get the stride it will eventually have.

Already before this patch, when the copied pixel end up on the main
thread and the dimension still doesn't match up, the frame will be
dropped.
---
 src/grd-session-vnc.c         | 5 +++--
 src/grd-session-vnc.h         | 3 ++-
 src/grd-vnc-pipewire-stream.c | 5 +++--
 3 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/src/grd-session-vnc.c b/src/grd-session-vnc.c
index a06d34d..ba3e5ce 100644
--- a/src/grd-session-vnc.c
+++ b/src/grd-session-vnc.c
@@ -519,9 +519,10 @@ check_rfb_password (rfbClientPtr  rfb_client,
 }
 
 int
-grd_session_vnc_get_framebuffer_stride (GrdSessionVnc *session_vnc)
+grd_session_vnc_get_stride_for_width (GrdSessionVnc *session_vnc,
+                                      int            width)
 {
-  return session_vnc->rfb_screen->paddedWidthInBytes;
+  return width * BGRX_BYTES_PER_PIXEL;
 }
 
 static void
diff --git a/src/grd-session-vnc.h b/src/grd-session-vnc.h
index 07678c8..ee1f986 100644
--- a/src/grd-session-vnc.h
+++ b/src/grd-session-vnc.h
@@ -55,7 +55,8 @@ void grd_session_vnc_move_cursor (GrdSessionVnc *session_vnc,
                                   int            x,
                                   int            y);
 
-int grd_session_vnc_get_framebuffer_stride (GrdSessionVnc *session_vnc);
+int grd_session_vnc_get_stride_for_width (GrdSessionVnc *session_vnc,
+                                          int            width);
 
 gboolean grd_session_vnc_is_client_gone (GrdSessionVnc *session_vnc);
 
diff --git a/src/grd-vnc-pipewire-stream.c b/src/grd-vnc-pipewire-stream.c
index 96dd7c9..82ceb9b 100644
--- a/src/grd-vnc-pipewire-stream.c
+++ b/src/grd-vnc-pipewire-stream.c
@@ -326,10 +326,11 @@ process_buffer (GrdVncPipeWireStream *stream,
       int height;
       int y;
 
-      src_stride = buffer->datas[0].chunk->stride;
-      dst_stride = grd_session_vnc_get_framebuffer_stride (stream->session);
       height = stream->spa_format.size.height;
       width = stream->spa_format.size.width;
+      src_stride = buffer->datas[0].chunk->stride;
+      dst_stride = grd_session_vnc_get_stride_for_width (stream->session,
+                                                         width);
 
       frame->data = g_malloc (height * dst_stride);
       for (y = 0; y < height; y++)
-- 
GitLab

