From b8e90d3665a98b09fea787712bdebe5f26743e87 Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Tue, 27 Aug 2019 21:49:28 +0000 Subject: [PATCH] zrle: Encode into vector --- inc/zrle.h | 5 +++-- src/server.c | 10 ++++++++-- src/zrle.c | 56 +++++++++++++++++++++++++--------------------------- 3 files changed, 38 insertions(+), 33 deletions(-) diff --git a/inc/zrle.h b/inc/zrle.h index c50a196..2cf9ec8 100644 --- a/inc/zrle.h +++ b/inc/zrle.h @@ -6,6 +6,7 @@ struct rfb_pixel_format; struct pixman_region16; +struct vec; void pixel32_to_cpixel(uint8_t *restrict dst, const struct rfb_pixel_format* dst_fmt, @@ -13,9 +14,9 @@ void pixel32_to_cpixel(uint8_t *restrict dst, const struct rfb_pixel_format* src_fmt, size_t bytes_per_cpixel, size_t len); -int zrle_encode_frame(uv_stream_t *stream, +int zrle_encode_frame(struct vec *dst, const struct rfb_pixel_format *dst_fmt, - uint8_t *src, + const uint8_t *src, const struct rfb_pixel_format *src_fmt, int width, int height, struct pixman_region16 *region); diff --git a/src/server.c b/src/server.c index 239a668..17940d5 100644 --- a/src/server.c +++ b/src/server.c @@ -1,6 +1,7 @@ #include "rfb-proto.h" #include "util.h" #include "zrle.h" +#include "vec.h" #include #include @@ -484,11 +485,16 @@ static int on_client_fb_update_request(struct vnc_client *client) pixman_region_intersect_rect(®ion, ®ion, 0, 0, display->width, display->height); - zrle_encode_frame((uv_stream_t*)&client->stream_handle, &client->pixfmt, - server->fb->addr, &server_fmt, fb->width, fb->height, ®ion); + struct vec frame; + vec_init(&frame, width * height * 3 / 2); + + zrle_encode_frame(&frame, &client->pixfmt, server->fb->addr, + &server_fmt, fb->width, fb->height, ®ion); pixman_region_fini(®ion); + vnc__write((uv_stream_t*)&client->stream_handle, frame.data, frame.len, NULL); + return sizeof(*msg); } diff --git a/src/zrle.c b/src/zrle.c index d875487..09149af 100644 --- a/src/zrle.c +++ b/src/zrle.c @@ -2,6 +2,7 @@ #include "bitmap.h" #include "util.h" #include "vec.h" +#include "zrle.h" #include #include @@ -191,8 +192,8 @@ int zrle_deflate(struct vec* dst, const struct vec* src, z_stream* zs, return 0; } -int zrle_encode_box(uv_stream_t *stream, const struct rfb_pixel_format *dst_fmt, - uint8_t *src, const struct rfb_pixel_format *src_fmt, +int zrle_encode_box(struct vec* out, const struct rfb_pixel_format *dst_fmt, + const uint8_t *src, const struct rfb_pixel_format *src_fmt, int x, int y, int stride, int width, int height) { int r = -1; @@ -200,21 +201,30 @@ int zrle_encode_box(uv_stream_t *stream, const struct rfb_pixel_format *dst_fmt, int chunk_size = 1 + bytes_per_cpixel * 64 * 64; z_stream zs = { 0 }; - struct vec out; struct vec in; if (vec_init(&in, 1 + bytes_per_cpixel * 64 * 64) < 0) goto failure; - if (vec_init(&out, bytes_per_cpixel * width * height / 2) < 0) - goto failure; - r = deflateInit(&zs, Z_DEFAULT_COMPRESSION); if (r != Z_OK) goto failure; + struct rfb_server_fb_rect rect = { + .encoding = htonl(RFB_ENCODING_ZRLE), + .x = htons(x), + .y = htons(y), + .width = htons(width), + .height = htons(height), + }; + + r = vec_append(out, &rect, sizeof(rect)); + if (r < 0) + goto failure; + /* Reserve space for size */ - vec_append_zero(&out, 4); + size_t size_index = out->len; + vec_append_zero(out, 4); int n_tiles = UDIV_UP(width, 64) * UDIV_UP(height, 64); @@ -232,29 +242,29 @@ int zrle_encode_box(uv_stream_t *stream, const struct rfb_pixel_format *dst_fmt, ((uint32_t*)src) + tile_x + tile_y * width, src_fmt, stride, tile_width, tile_height); - r = zrle_deflate(&out, &in, &zs, i == n_tiles - 1); + r = zrle_deflate(out, &in, &zs, i == n_tiles - 1); if (r < 0) goto failure; } deflateEnd(&zs); - uint32_t *out_size = out.data; - *out_size = htonl(out.len - 8); + /* There seems to be something extra at the end */ + out->len -= 4; + printf("Encoded with size: %lu\n", out->len - size_index); - printf("Sending %lu bytes\n", out.len - 4); + uint32_t out_size = htonl(out->len - size_index - 4); + memcpy(((uint8_t*)out->data) + size_index, &out_size, sizeof(out_size)); - r = vnc__write(stream, out.data, out.len - 4, NULL); failure: -// vec_destroy(&out); vec_destroy(&in); return r; #undef CHUNK } -int zrle_encode_frame(uv_stream_t *stream, +int zrle_encode_frame(struct vec* dst, const struct rfb_pixel_format *dst_fmt, - uint8_t *src, + const uint8_t *src, const struct rfb_pixel_format *src_fmt, int width, int height, struct pixman_region16 *region) @@ -273,7 +283,7 @@ int zrle_encode_frame(uv_stream_t *stream, .n_rects = htons(n_rects), }; - rc = vnc__write(stream, &head, sizeof(head), NULL); + rc = vec_append(dst, &head, sizeof(head)); if (rc < 0) return -1; @@ -283,19 +293,7 @@ int zrle_encode_frame(uv_stream_t *stream, int box_width = box[i].x2 - x; int box_height = box[i].y2 - y; - struct rfb_server_fb_rect rect = { - .encoding = htonl(RFB_ENCODING_ZRLE), - .x = htons(x), - .y = htons(y), - .width = htons(box_width), - .height = htons(box_height), - }; - - rc = vnc__write(stream, &rect, sizeof(rect), NULL); - if (rc < 0) - return -1; - - rc = zrle_encode_box(stream, dst_fmt, src, src_fmt, x, y, + rc = zrle_encode_box(dst, dst_fmt, src, src_fmt, x, y, width, box_width, box_height); if (rc < 0) return -1;