zrle: Encode into vector

tight-png
Andri Yngvason 2019-08-27 21:49:28 +00:00 committed by Andri Yngvason
parent b3b9339adb
commit b8e90d3665
3 changed files with 38 additions and 33 deletions

View File

@ -6,6 +6,7 @@
struct rfb_pixel_format; struct rfb_pixel_format;
struct pixman_region16; struct pixman_region16;
struct vec;
void pixel32_to_cpixel(uint8_t *restrict dst, void pixel32_to_cpixel(uint8_t *restrict dst,
const struct rfb_pixel_format* dst_fmt, 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, const struct rfb_pixel_format* src_fmt,
size_t bytes_per_cpixel, size_t len); 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, const struct rfb_pixel_format *dst_fmt,
uint8_t *src, const uint8_t *src,
const struct rfb_pixel_format *src_fmt, const struct rfb_pixel_format *src_fmt,
int width, int height, int width, int height,
struct pixman_region16 *region); struct pixman_region16 *region);

View File

@ -1,6 +1,7 @@
#include "rfb-proto.h" #include "rfb-proto.h"
#include "util.h" #include "util.h"
#include "zrle.h" #include "zrle.h"
#include "vec.h"
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
@ -484,11 +485,16 @@ static int on_client_fb_update_request(struct vnc_client *client)
pixman_region_intersect_rect(&region, &region, 0, 0, display->width, pixman_region_intersect_rect(&region, &region, 0, 0, display->width,
display->height); display->height);
zrle_encode_frame((uv_stream_t*)&client->stream_handle, &client->pixfmt, struct vec frame;
server->fb->addr, &server_fmt, fb->width, fb->height, &region); vec_init(&frame, width * height * 3 / 2);
zrle_encode_frame(&frame, &client->pixfmt, server->fb->addr,
&server_fmt, fb->width, fb->height, &region);
pixman_region_fini(&region); pixman_region_fini(&region);
vnc__write((uv_stream_t*)&client->stream_handle, frame.data, frame.len, NULL);
return sizeof(*msg); return sizeof(*msg);
} }

View File

@ -2,6 +2,7 @@
#include "bitmap.h" #include "bitmap.h"
#include "util.h" #include "util.h"
#include "vec.h" #include "vec.h"
#include "zrle.h"
#include <stdint.h> #include <stdint.h>
#include <unistd.h> #include <unistd.h>
@ -191,8 +192,8 @@ int zrle_deflate(struct vec* dst, const struct vec* src, z_stream* zs,
return 0; return 0;
} }
int zrle_encode_box(uv_stream_t *stream, const struct rfb_pixel_format *dst_fmt, int zrle_encode_box(struct vec* out, const struct rfb_pixel_format *dst_fmt,
uint8_t *src, const struct rfb_pixel_format *src_fmt, const uint8_t *src, const struct rfb_pixel_format *src_fmt,
int x, int y, int stride, int width, int height) int x, int y, int stride, int width, int height)
{ {
int r = -1; 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; int chunk_size = 1 + bytes_per_cpixel * 64 * 64;
z_stream zs = { 0 }; z_stream zs = { 0 };
struct vec out;
struct vec in; struct vec in;
if (vec_init(&in, 1 + bytes_per_cpixel * 64 * 64) < 0) if (vec_init(&in, 1 + bytes_per_cpixel * 64 * 64) < 0)
goto failure; goto failure;
if (vec_init(&out, bytes_per_cpixel * width * height / 2) < 0)
goto failure;
r = deflateInit(&zs, Z_DEFAULT_COMPRESSION); r = deflateInit(&zs, Z_DEFAULT_COMPRESSION);
if (r != Z_OK) if (r != Z_OK)
goto failure; 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 */ /* 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); 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, ((uint32_t*)src) + tile_x + tile_y * width,
src_fmt, stride, tile_width, tile_height); 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) if (r < 0)
goto failure; goto failure;
} }
deflateEnd(&zs); deflateEnd(&zs);
uint32_t *out_size = out.data; /* There seems to be something extra at the end */
*out_size = htonl(out.len - 8); 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: failure:
// vec_destroy(&out);
vec_destroy(&in); vec_destroy(&in);
return r; return r;
#undef CHUNK #undef CHUNK
} }
int zrle_encode_frame(uv_stream_t *stream, int zrle_encode_frame(struct vec* dst,
const struct rfb_pixel_format *dst_fmt, const struct rfb_pixel_format *dst_fmt,
uint8_t *src, const uint8_t *src,
const struct rfb_pixel_format *src_fmt, const struct rfb_pixel_format *src_fmt,
int width, int height, int width, int height,
struct pixman_region16 *region) struct pixman_region16 *region)
@ -273,7 +283,7 @@ int zrle_encode_frame(uv_stream_t *stream,
.n_rects = htons(n_rects), .n_rects = htons(n_rects),
}; };
rc = vnc__write(stream, &head, sizeof(head), NULL); rc = vec_append(dst, &head, sizeof(head));
if (rc < 0) if (rc < 0)
return -1; return -1;
@ -283,19 +293,7 @@ int zrle_encode_frame(uv_stream_t *stream,
int box_width = box[i].x2 - x; int box_width = box[i].x2 - x;
int box_height = box[i].y2 - y; int box_height = box[i].y2 - y;
struct rfb_server_fb_rect rect = { rc = zrle_encode_box(dst, dst_fmt, src, src_fmt, x, y,
.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,
width, box_width, box_height); width, box_width, box_height);
if (rc < 0) if (rc < 0)
return -1; return -1;