From c7dd0624988096ededf053f041d7541895747b2d Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Sat, 4 Sep 2021 23:53:30 +0000 Subject: [PATCH] Add a stride parameter to nvnc_fb --- bench/zrle-bench.c | 11 ++++++----- examples/draw.c | 3 ++- include/fb.h | 3 ++- include/neatvnc.h | 7 ++++--- src/fb.c | 13 ++++++++++--- src/fb_pool.c | 15 ++++++++++----- src/pngfb.c | 4 ++-- src/raw-encoding.c | 4 ++-- src/tight.c | 6 +++--- src/zrle.c | 4 ++-- 10 files changed, 43 insertions(+), 27 deletions(-) diff --git a/bench/zrle-bench.c b/bench/zrle-bench.c index 8048606..226f6fb 100644 --- a/bench/zrle-bench.c +++ b/bench/zrle-bench.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 Andri Yngvason + * Copyright (c) 2019 - 2021 Andri Yngvason * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -55,6 +55,7 @@ static int run_benchmark(const char *image) void *addr = nvnc_fb_get_addr(fb); int width = nvnc_fb_get_width(fb); int height = nvnc_fb_get_height(fb); + int stride = nvnc_fb_get_stride(fb); struct rfb_pixel_format pixfmt; rfb_pixfmt_from_fourcc(&pixfmt, DRM_FORMAT_ARGB8888); @@ -65,7 +66,7 @@ static int run_benchmark(const char *image) pixman_region_union_rect(®ion, ®ion, 0, 0, width, height); struct vec frame; - vec_init(&frame, width * height * 3 / 2); + vec_init(&frame, stride * height * 3 / 2); z_stream zs = { 0 }; @@ -75,13 +76,13 @@ static int run_benchmark(const char *image) /* mem level: */ 9, /* strategy: */ Z_DEFAULT_STRATEGY); - void *dummy = malloc(width * height * 4); + void *dummy = malloc(stride * height * 4); if (!dummy) goto failure; uint64_t start_time = gettime_us(CLOCK_PROCESS_CPUTIME_ID); - memcpy_unoptimized(dummy, addr, width * height * 4); + memcpy_unoptimized(dummy, addr, stride * height * 4); uint64_t end_time = gettime_us(CLOCK_PROCESS_CPUTIME_ID); printf("memcpy baseline for %s took %"PRIu64" micro seconds\n", image, @@ -96,7 +97,7 @@ static int run_benchmark(const char *image) printf("Encoding %s took %"PRIu64" micro seconds\n", image, end_time - start_time); - double orig_size = width * height * 4; + double orig_size = stride * height * 4; double compressed_size = frame.len; double reduction = (orig_size - compressed_size) / orig_size; diff --git a/examples/draw.c b/examples/draw.c index 9210c72..dfbf56e 100644 --- a/examples/draw.c +++ b/examples/draw.c @@ -213,7 +213,8 @@ int main(int argc, char* argv[]) draw.width * 4); assert(draw.whiteboard); - draw.fb_pool = nvnc_fb_pool_new(draw.width, draw.height, draw.format); + draw.fb_pool = nvnc_fb_pool_new(draw.width, draw.height, draw.format, + draw.width); assert(draw.fb_pool); struct nvnc* server = nvnc_open("127.0.0.1", 5900); diff --git a/include/fb.h b/include/fb.h index 384e0c9..24444ca 100644 --- a/include/fb.h +++ b/include/fb.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 - 2020 Andri Yngvason + * Copyright (c) 2019 - 2021 Andri Yngvason * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -34,6 +34,7 @@ struct nvnc_fb { size_t size; uint16_t width; uint16_t height; + int32_t stride; uint32_t fourcc_format; uint64_t fourcc_modifier; }; diff --git a/include/neatvnc.h b/include/neatvnc.h index a7de5a7..6b3f9f7 100644 --- a/include/neatvnc.h +++ b/include/neatvnc.h @@ -82,7 +82,7 @@ int nvnc_enable_auth(struct nvnc* self, const char* privkey_path, const char* cert_path, nvnc_auth_fn, void* userdata); struct nvnc_fb* nvnc_fb_new(uint16_t width, uint16_t height, - uint32_t fourcc_format); + uint32_t fourcc_format, uint16_t stride); void nvnc_fb_ref(struct nvnc_fb* fb); void nvnc_fb_unref(struct nvnc_fb* fb); @@ -96,11 +96,12 @@ void* nvnc_fb_get_addr(const struct nvnc_fb* fb); uint16_t nvnc_fb_get_width(const struct nvnc_fb* fb); uint16_t nvnc_fb_get_height(const struct nvnc_fb* fb); uint32_t nvnc_fb_get_fourcc_format(const struct nvnc_fb* fb); +int32_t nvnc_fb_get_stride(const struct nvnc_fb* fb); struct nvnc_fb_pool* nvnc_fb_pool_new(uint16_t width, uint16_t height, - uint32_t fourcc_format); + uint32_t fourcc_format, uint16_t stride); bool nvnc_fb_pool_resize(struct nvnc_fb_pool*, uint16_t width, uint16_t height, - uint32_t fourcc_format); + uint32_t fourcc_format, uint16_t stride); void nvnc_fb_pool_ref(struct nvnc_fb_pool*); void nvnc_fb_pool_unref(struct nvnc_fb_pool*); diff --git a/src/fb.c b/src/fb.c index 08dca95..32ba580 100644 --- a/src/fb.c +++ b/src/fb.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 - 2020 Andri Yngvason + * Copyright (c) 2019 - 2021 Andri Yngvason * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -28,7 +28,7 @@ EXPORT struct nvnc_fb* nvnc_fb_new(uint16_t width, uint16_t height, - uint32_t fourcc_format) + uint32_t fourcc_format, uint16_t stride) { struct nvnc_fb* fb = calloc(1, sizeof(*fb)); if (!fb) @@ -38,7 +38,8 @@ struct nvnc_fb* nvnc_fb_new(uint16_t width, uint16_t height, fb->width = width; fb->height = height; fb->fourcc_format = fourcc_format; - fb->size = width * height * 4; /* Assume 4 byte format for now */ + fb->stride = stride; + fb->size = width * stride * 4; /* Assume 4 byte format for now */ size_t alignment = MAX(4, sizeof(void*)); size_t aligned_size = ALIGN_UP(fb->size, alignment); @@ -92,6 +93,12 @@ uint32_t nvnc_fb_get_fourcc_format(const struct nvnc_fb* fb) return fb->fourcc_format; } +EXPORT +int32_t nvnc_fb_get_stride(const struct nvnc_fb* fb) +{ + return fb->stride; +} + static void nvnc__fb_free(struct nvnc_fb* fb) { nvnc_cleanup_fn cleanup = fb->common.cleanup_fn; diff --git a/src/fb_pool.c b/src/fb_pool.c index 4dbd630..fdfe704 100644 --- a/src/fb_pool.c +++ b/src/fb_pool.c @@ -38,12 +38,13 @@ struct nvnc_fb_pool { uint16_t width; uint16_t height; + int32_t stride; uint32_t fourcc_format; }; EXPORT struct nvnc_fb_pool* nvnc_fb_pool_new(uint16_t width, uint16_t height, - uint32_t fourcc_format) + uint32_t fourcc_format, uint16_t stride) { struct nvnc_fb_pool* self = calloc(1, sizeof(*self)); if (!self) @@ -54,6 +55,7 @@ struct nvnc_fb_pool* nvnc_fb_pool_new(uint16_t width, uint16_t height, TAILQ_INIT(&self->fbs); self->width = width; self->height = height; + self->stride = stride; self->fourcc_format = fourcc_format; return self; @@ -77,16 +79,18 @@ static void nvnc_fb_pool__destroy(struct nvnc_fb_pool* self) EXPORT bool nvnc_fb_pool_resize(struct nvnc_fb_pool* self, uint16_t width, - uint16_t height, uint32_t fourcc_format) + uint16_t height, uint32_t fourcc_format, uint16_t stride) { if (width == self->width && height == self->height && - fourcc_format == self->fourcc_format) + fourcc_format == self->fourcc_format && + stride == self->stride) return false; nvnc_fb_pool__destroy_fbs(self); self->width = width; self->height = height; + self->stride = stride; self->fourcc_format = fourcc_format; return true; @@ -116,7 +120,7 @@ static void nvnc_fb_pool__on_fb_release(struct nvnc_fb* fb, void* userdata) static struct nvnc_fb* nvnc_fb_pool__acquire_new(struct nvnc_fb_pool* self) { struct nvnc_fb* fb = nvnc_fb_new(self->width, self->height, - self->fourcc_format); + self->fourcc_format, self->stride); if (!fb) return NULL; @@ -152,7 +156,8 @@ EXPORT void nvnc_fb_pool_release(struct nvnc_fb_pool* self, struct nvnc_fb* fb) { if (fb->width != self->width || fb->height != self->height || - fb->fourcc_format != self->fourcc_format) { + fb->fourcc_format != self->fourcc_format || + fb->stride != self->stride) { return; } diff --git a/src/pngfb.c b/src/pngfb.c index 4661c79..4ad675f 100644 --- a/src/pngfb.c +++ b/src/pngfb.c @@ -73,8 +73,8 @@ struct nvnc_fb* read_png_file(const char* filename) png_read_update_info(png, info); size_t row_bytes = png_get_rowbytes(png, info); - assert(row_bytes == width * 4); - struct nvnc_fb* fb = nvnc_fb_new(width, height, DRM_FORMAT_ABGR8888); + struct nvnc_fb* fb = nvnc_fb_new(width, height, DRM_FORMAT_ABGR8888, + row_bytes / 4); assert(fb); uint8_t* addr = nvnc_fb_get_addr(fb); diff --git a/src/raw-encoding.c b/src/raw-encoding.c index 81b1294..2fa3fcd 100644 --- a/src/raw-encoding.c +++ b/src/raw-encoding.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 - 2020 Andri Yngvason + * Copyright (c) 2019 - 2021 Andri Yngvason * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -86,7 +86,7 @@ int raw_encode_frame(struct vec* dst, const struct rfb_pixel_format* dst_fmt, int box_height = box[i].y2 - y; rc = raw_encode_box(dst, dst_fmt, src, src_fmt, x, y, - src->width, box_width, box_height); + src->stride, box_width, box_height); if (rc < 0) return -1; } diff --git a/src/tight.c b/src/tight.c index 692a4c2..7288963 100644 --- a/src/tight.c +++ b/src/tight.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 - 2020 Andri Yngvason + * Copyright (c) 2019 - 2021 Andri Yngvason * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -260,7 +260,7 @@ static void tight_encode_tile_basic(struct tight_encoder* self, memcpy(&cfmt, &self->dfmt, sizeof(cfmt)); uint32_t* addr = nvnc_fb_get_addr(self->fb); - uint32_t stride = nvnc_fb_get_width(self->fb); + int32_t stride = nvnc_fb_get_stride(self->fb); // TODO: Limit width and hight to the sides for (uint32_t y = y_start; y < y_start + height; ++y) { @@ -324,7 +324,7 @@ static int tight_encode_tile_jpeg(struct tight_encoder* self, return -1; uint32_t* addr = nvnc_fb_get_addr(self->fb); - uint32_t stride = nvnc_fb_get_width(self->fb); + int32_t stride = nvnc_fb_get_stride(self->fb); void* img = (uint32_t*)addr + x + y * stride; int rc = -1; diff --git a/src/zrle.c b/src/zrle.c index 6dadf51..0275821 100644 --- a/src/zrle.c +++ b/src/zrle.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2019 - 2020 Andri Yngvason + * Copyright (c) 2019 - 2021 Andri Yngvason * * Permission to use, copy, modify, and/or distribute this software for any * purpose with or without fee is hereby granted, provided that the above @@ -287,7 +287,7 @@ int zrle_encode_frame(z_stream* zs, struct vec* dst, int box_height = box[i].y2 - y; rc = zrle_encode_box(dst, dst_fmt, src, src_fmt, x, y, - src->width, box_width, box_height, zs); + src->stride, box_width, box_height, zs); if (rc < 0) return -1; }