From 65c0e91c373fe10392a4ac0894da7463eb12ef56 Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Sun, 12 Dec 2021 16:05:29 +0000 Subject: [PATCH] Move update header out of encoders --- include/enc-util.h | 1 - include/encoder.h | 2 ++ src/enc-util.c | 10 ---------- src/raw-encoding.c | 7 +++---- src/server.c | 12 ++++++++++-- src/tight.c | 6 ++++-- src/zrle.c | 7 +++---- 7 files changed, 22 insertions(+), 23 deletions(-) diff --git a/include/enc-util.h b/include/enc-util.h index 5ec7b78..6f3abac 100644 --- a/include/enc-util.h +++ b/include/enc-util.h @@ -22,7 +22,6 @@ struct vec; -int encode_rect_count(struct vec* dst, uint32_t count); int encode_rect_head(struct vec* dst, enum rfb_encodings encoding, uint32_t x, uint32_t y, uint32_t width, uint32_t height); uint32_t calc_bytes_per_cpixel(const struct rfb_pixel_format* fmt); diff --git a/include/encoder.h b/include/encoder.h index a0dcfbe..f27749a 100644 --- a/include/encoder.h +++ b/include/encoder.h @@ -45,6 +45,8 @@ struct encoder { uint16_t x_pos; uint16_t y_pos; + int n_rects; + void (*on_done)(struct encoder*, struct rcbuf* result); void* userdata; }; diff --git a/src/enc-util.c b/src/enc-util.c index 922f00c..10e163c 100644 --- a/src/enc-util.c +++ b/src/enc-util.c @@ -20,16 +20,6 @@ #include -int encode_rect_count(struct vec* dst, uint32_t count) -{ - struct rfb_server_fb_update_msg msg = { - .type = RFB_SERVER_TO_CLIENT_FRAMEBUFFER_UPDATE, - .n_rects = htons(count), - }; - - return vec_append(dst, &msg, sizeof(msg)); -} - int encode_rect_head(struct vec* dst, enum rfb_encodings encoding, uint32_t x, uint32_t y, uint32_t width, uint32_t height) { diff --git a/src/raw-encoding.c b/src/raw-encoding.c index 6de559d..ba0482d 100644 --- a/src/raw-encoding.c +++ b/src/raw-encoding.c @@ -93,6 +93,8 @@ static int raw_encode_frame(struct raw_encoder* self, struct vec* dst, { int rc = -1; + self->encoder.n_rects = 0; + int n_rects = 0; struct pixman_box16* box = pixman_region_rectangles(region, &n_rects); if (n_rects > UINT16_MAX) { @@ -108,10 +110,6 @@ static int raw_encode_frame(struct raw_encoder* self, struct vec* dst, if (rc < 0) return -1; - rc = encode_rect_count(dst, n_rects); - if (rc < 0) - return -1; - for (int i = 0; i < n_rects; ++i) { int x = box[i].x1; int y = box[i].y1; @@ -124,6 +122,7 @@ static int raw_encode_frame(struct raw_encoder* self, struct vec* dst, return -1; } + self->encoder.n_rects = n_rects; return 0; } diff --git a/src/server.c b/src/server.c index 07b15e7..d7e11c0 100644 --- a/src/server.c +++ b/src/server.c @@ -28,6 +28,7 @@ #include "usdt.h" #include "encoder.h" #include "tight.h" +#include "enc-util.h" #include #include @@ -1223,12 +1224,19 @@ static bool client_has_encoding(const struct nvnc_client* client, return false; } -static void finish_fb_update(struct nvnc_client* client, struct rcbuf* payload) +static void finish_fb_update(struct nvnc_client* client, struct rcbuf* payload, + int n_rects) { client_ref(client); if (client->net_stream->state != STREAM_STATE_CLOSED) { DTRACE_PROBE1(neatvnc, send_fb_start, client); + struct rfb_server_fb_update_msg update_msg = { + .type = RFB_SERVER_TO_CLIENT_FRAMEBUFFER_UPDATE, + .n_rects = htons(n_rects), + }; + stream_write(client->net_stream, &update_msg, + sizeof(update_msg), NULL, NULL); rcbuf_ref(payload); stream_send(client->net_stream, payload, on_write_frame_done, client); @@ -1251,7 +1259,7 @@ static void finish_fb_update(struct nvnc_client* client, struct rcbuf* payload) static void on_encode_frame_done(struct encoder* encoder, struct rcbuf* result) { struct nvnc_client* client = encoder->userdata; - finish_fb_update(client, result); + finish_fb_update(client, result, encoder->n_rects); client_unref(client); } diff --git a/src/tight.c b/src/tight.c index dad367f..d10f52e 100644 --- a/src/tight.c +++ b/src/tight.c @@ -514,6 +514,8 @@ static void on_tight_finished(void* obj) struct rcbuf* result = rcbuf_new(self->dst.data, self->dst.len); assert(result); + self->encoder.n_rects = self->n_rects; + if (self->encoder.on_done) self->encoder.on_done(&self->encoder, result); @@ -580,6 +582,8 @@ static int tight_encoder_encode(struct encoder* encoder, struct nvnc_fb* fb, struct tight_encoder* self = tight_encoder(encoder); int rc; + self->encoder.n_rects = 0; + rc = rfb_pixfmt_from_fourcc(&self->sfmt, nvnc_fb_get_fourcc_format(fb)); assert(rc == 0); @@ -598,8 +602,6 @@ static int tight_encoder_encode(struct encoder* encoder, struct nvnc_fb* fb, self->n_rects = tight_apply_damage(self, damage); assert(self->n_rects > 0); - encode_rect_count(&self->dst, self->n_rects); - nvnc_fb_ref(self->fb); if (tight_schedule_encoding_jobs(self) < 0) { diff --git a/src/zrle.c b/src/zrle.c index 0e0f56d..1642252 100644 --- a/src/zrle.c +++ b/src/zrle.c @@ -299,6 +299,8 @@ static int zrle_encode_frame(struct zrle_encoder* self, z_stream* zs, { int rc = -1; + self->encoder.n_rects = 0; + int n_rects = 0; struct pixman_box16* box = pixman_region_rectangles(region, &n_rects); if (n_rects > UINT16_MAX) { @@ -310,10 +312,6 @@ static int zrle_encode_frame(struct zrle_encoder* self, z_stream* zs, if (rc < 0) return -1; - rc = encode_rect_count(dst, n_rects); - if (rc < 0) - return -1; - for (int i = 0; i < n_rects; ++i) { int x = box[i].x1; int y = box[i].y1; @@ -326,6 +324,7 @@ static int zrle_encode_frame(struct zrle_encoder* self, z_stream* zs, return -1; } + self->encoder.n_rects = n_rects; return 0; }