Skip to content

Commit 6cbe4ae

Browse files
committed
Reduce cursor latency by rendering to VNC framebuf
Previously, cursor movement was handled by passing pointer events into twin screen, and then calculated the damage region for the cursor's new position and transmitted it to the VNC framebuffer. This process introduced latency in cursor display due to the multiple steps involved. To address this delay, the cursor is now rendered directly within the VNC framebuffer by leveraging neatvnc's internal cursor API. This change bypasses the intermediate steps and provides a smoother and more responsive cursor movement experience in the VNC backend.
1 parent 9be6240 commit 6cbe4ae

File tree

5 files changed

+29
-1
lines changed

5 files changed

+29
-1
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ endif
113113
ifeq ($(CONFIG_BACKEND_VNC), y)
114114
BACKEND = vnc
115115
libtwin.a_files-y += backend/vnc.c
116+
libtwin.a_files-y += src/cursor.c
116117
libtwin.a_cflags-y += $(shell pkg-config --cflags neatvnc aml pixman-1)
117118
TARGET_LIBS += $(shell pkg-config --libs neatvnc aml pixman-1)
118119
endif

backend/vnc.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ typedef struct {
4747
enum nvnc_button_mask prev_button;
4848
} twin_peer_t;
4949

50+
#define CURSOR_WIDTH 14
51+
#define CURSOR_HEIGHT 20
52+
5053
static void _twin_vnc_put_begin(twin_coord_t left,
5154
twin_coord_t top,
5255
twin_coord_t right,
@@ -147,6 +150,21 @@ static void _twin_vnc_pointer_event(struct nvnc_client *client,
147150
twin_screen_dispatch(tx->screen, &tev);
148151
}
149152

153+
static struct nvnc_fb *_twin_vnc_create_cursor()
154+
{
155+
struct nvnc_fb *fb = nvnc_fb_new(CURSOR_WIDTH, CURSOR_HEIGHT,
156+
DRM_FORMAT_ARGB8888, CURSOR_WIDTH);
157+
uint32_t *pixels = nvnc_fb_get_addr(fb);
158+
for (int i = 0; i < CURSOR_WIDTH * CURSOR_HEIGHT; i++) {
159+
uint32_t a = _twin_cursor_default[i * 4];
160+
uint32_t r = _twin_cursor_default[i * 4 + 1];
161+
uint32_t g = _twin_cursor_default[i * 4 + 2];
162+
uint32_t b = _twin_cursor_default[i * 4 + 3];
163+
pixels[i] = (a << 24) | (r << 16) | (g << 8) | b;
164+
}
165+
return fb;
166+
}
167+
150168
twin_context_t *twin_vnc_init(int width, int height)
151169
{
152170
twin_context_t *ctx = calloc(1, sizeof(twin_context_t));
@@ -200,6 +218,10 @@ twin_context_t *twin_vnc_init(int width, int height)
200218
nvnc_set_pointer_fn(tx->server, _twin_vnc_pointer_event);
201219
nvnc_set_new_client_fn(tx->server, _twin_vnc_new_client);
202220
nvnc_set_userdata(tx->server, tx, NULL);
221+
struct nvnc_fb *cursor = _twin_vnc_create_cursor();
222+
nvnc_set_cursor(tx->server, cursor, CURSOR_WIDTH, CURSOR_HEIGHT, 0, 0,
223+
true);
224+
nvnc_fb_unref(cursor);
203225

204226
ctx->screen = twin_screen_create(width, height, _twin_vnc_put_begin,
205227
_twin_vnc_put_span, ctx);

configs/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ comment "Logging is disabled"
4141
config CURSOR
4242
bool "Manipulate cursor"
4343
default n
44+
depends on !BACKEND_VNC
4445

4546
endmenu
4647

include/twin_private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,8 @@ static inline int twin_clz(uint32_t v)
619619
}
620620
#endif
621621

622+
extern const uint8_t _twin_cursor_default[];
623+
622624
/* Pattern Matching for C macros.
623625
* https://github.com/pfultz2/Cloak/wiki/C-Preprocessor-tricks,-tips,-and-idioms
624626
*/

src/cursor.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#include <stddef.h>
1010
#include <twin.h>
1111

12-
static const uint8_t _twin_cursor_default[] = {
12+
#include "twin_private.h"
13+
14+
const uint8_t _twin_cursor_default[] = {
1315
0x19, 0x19, 0x19, 0xb8, 0x1e, 0x1e, 0x1e, 0xc8, 0x00, 0x00, 0x00, 0x13,
1416
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00,
1517
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

0 commit comments

Comments
 (0)