2019-09-07 16:51:07 +00:00
|
|
|
/*
|
2021-09-04 23:53:30 +00:00
|
|
|
* Copyright (c) 2019 - 2021 Andri Yngvason
|
2019-09-07 16:51:07 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
|
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
|
|
|
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2019-08-27 22:29:46 +00:00
|
|
|
#include "zrle.h"
|
|
|
|
#include "rfb-proto.h"
|
|
|
|
#include "vec.h"
|
2019-08-29 21:47:02 +00:00
|
|
|
#include "neatvnc.h"
|
2020-05-27 21:59:34 +00:00
|
|
|
#include "pixels.h"
|
2019-08-27 22:29:46 +00:00
|
|
|
|
2020-05-27 21:59:34 +00:00
|
|
|
#include <stdio.h>
|
2019-08-27 22:29:46 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <libdrm/drm_fourcc.h>
|
|
|
|
#include <pixman.h>
|
2019-08-31 16:11:33 +00:00
|
|
|
#include <time.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
2020-05-27 21:59:34 +00:00
|
|
|
static uint64_t gettime_us(clockid_t clock)
|
2019-08-31 16:11:33 +00:00
|
|
|
{
|
|
|
|
struct timespec ts = { 0 };
|
|
|
|
clock_gettime(clock, &ts);
|
|
|
|
return ts.tv_sec * 1000000ULL + ts.tv_nsec / 1000ULL;
|
|
|
|
}
|
2019-08-27 22:29:46 +00:00
|
|
|
|
2020-05-27 21:59:34 +00:00
|
|
|
#pragma GCC push_options
|
2019-09-07 18:38:32 +00:00
|
|
|
#pragma GCC optimize ("-O0")
|
2020-05-27 21:59:34 +00:00
|
|
|
static void memcpy_unoptimized(void* dst, const void* src, size_t len)
|
2019-09-07 18:38:32 +00:00
|
|
|
{
|
|
|
|
memcpy(dst, src, len);
|
|
|
|
}
|
2020-05-27 21:59:34 +00:00
|
|
|
#pragma GCC pop_options
|
2019-09-07 18:38:32 +00:00
|
|
|
|
2019-10-07 17:39:54 +00:00
|
|
|
struct nvnc_fb* read_png_file(const char *filename);
|
2019-08-27 22:29:46 +00:00
|
|
|
|
2020-05-27 21:59:34 +00:00
|
|
|
static int run_benchmark(const char *image)
|
2019-08-27 22:29:46 +00:00
|
|
|
{
|
|
|
|
int rc = -1;
|
|
|
|
|
2019-10-07 17:39:54 +00:00
|
|
|
struct nvnc_fb* fb = read_png_file(image);
|
|
|
|
if (!fb)
|
2019-08-27 22:29:46 +00:00
|
|
|
return -1;
|
|
|
|
|
2019-10-07 17:39:54 +00:00
|
|
|
void *addr = nvnc_fb_get_addr(fb);
|
|
|
|
int width = nvnc_fb_get_width(fb);
|
|
|
|
int height = nvnc_fb_get_height(fb);
|
2021-09-04 23:53:30 +00:00
|
|
|
int stride = nvnc_fb_get_stride(fb);
|
2019-10-07 17:39:54 +00:00
|
|
|
|
2019-08-27 22:29:46 +00:00
|
|
|
struct rfb_pixel_format pixfmt;
|
|
|
|
rfb_pixfmt_from_fourcc(&pixfmt, DRM_FORMAT_ARGB8888);
|
|
|
|
|
|
|
|
struct pixman_region16 region;
|
|
|
|
pixman_region_init(®ion);
|
|
|
|
|
2019-10-07 17:39:54 +00:00
|
|
|
pixman_region_union_rect(®ion, ®ion, 0, 0, width, height);
|
2019-08-27 22:29:46 +00:00
|
|
|
|
|
|
|
struct vec frame;
|
2021-09-04 23:53:30 +00:00
|
|
|
vec_init(&frame, stride * height * 3 / 2);
|
2019-08-27 22:29:46 +00:00
|
|
|
|
2019-09-01 18:40:11 +00:00
|
|
|
z_stream zs = { 0 };
|
|
|
|
|
2019-10-12 16:42:59 +00:00
|
|
|
deflateInit2(&zs, /* compression level: */ 1,
|
2023-07-27 06:00:32 +00:00
|
|
|
/* method: */ Z_DEFLATED,
|
|
|
|
/* window bits: */ 15,
|
|
|
|
/* mem level: */ 9,
|
|
|
|
/* strategy: */ Z_DEFAULT_STRATEGY);
|
2019-09-01 18:40:11 +00:00
|
|
|
|
2021-09-04 23:53:30 +00:00
|
|
|
void *dummy = malloc(stride * height * 4);
|
2019-09-07 18:38:32 +00:00
|
|
|
if (!dummy)
|
|
|
|
goto failure;
|
|
|
|
|
2019-08-31 16:11:33 +00:00
|
|
|
uint64_t start_time = gettime_us(CLOCK_PROCESS_CPUTIME_ID);
|
|
|
|
|
2021-09-04 23:53:30 +00:00
|
|
|
memcpy_unoptimized(dummy, addr, stride * height * 4);
|
2019-09-07 18:38:32 +00:00
|
|
|
|
|
|
|
uint64_t end_time = gettime_us(CLOCK_PROCESS_CPUTIME_ID);
|
|
|
|
printf("memcpy baseline for %s took %"PRIu64" micro seconds\n", image,
|
2023-07-27 06:00:32 +00:00
|
|
|
end_time - start_time);
|
2019-09-07 18:38:32 +00:00
|
|
|
|
|
|
|
free(dummy);
|
|
|
|
|
|
|
|
start_time = gettime_us(CLOCK_PROCESS_CPUTIME_ID);
|
2019-10-07 17:39:54 +00:00
|
|
|
rc = zrle_encode_frame(&zs, &frame, &pixfmt, fb, &pixfmt, ®ion);
|
2019-08-27 22:29:46 +00:00
|
|
|
|
2019-09-07 18:38:32 +00:00
|
|
|
end_time = gettime_us(CLOCK_PROCESS_CPUTIME_ID);
|
2019-08-31 16:11:33 +00:00
|
|
|
printf("Encoding %s took %"PRIu64" micro seconds\n", image,
|
2023-07-27 06:00:32 +00:00
|
|
|
end_time - start_time);
|
2019-08-31 16:11:33 +00:00
|
|
|
|
2021-09-04 23:53:30 +00:00
|
|
|
double orig_size = stride * height * 4;
|
2019-10-12 16:42:59 +00:00
|
|
|
double compressed_size = frame.len;
|
|
|
|
|
|
|
|
double reduction = (orig_size - compressed_size) / orig_size;
|
|
|
|
printf("Size reduction: %.1f %%\n", reduction * 100.0);
|
|
|
|
|
2019-09-01 18:40:11 +00:00
|
|
|
deflateEnd(&zs);
|
|
|
|
|
2019-08-27 22:29:46 +00:00
|
|
|
if (rc < 0)
|
|
|
|
goto failure;
|
|
|
|
|
|
|
|
rc = 0;
|
|
|
|
failure:
|
|
|
|
pixman_region_fini(®ion);
|
|
|
|
vec_destroy(&frame);
|
2019-10-07 17:39:54 +00:00
|
|
|
nvnc_fb_unref(fb);
|
2019-08-27 22:29:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int rc = 0;
|
|
|
|
|
|
|
|
char *image = argv[1];
|
|
|
|
|
|
|
|
if (image)
|
|
|
|
return run_benchmark(image) < 0 ? 1 :0;
|
|
|
|
|
|
|
|
rc |= run_benchmark("test-images/tv-test-card.png") < 0 ? 1 : 0;
|
|
|
|
rc |= run_benchmark("test-images/lena-soderberg.png") < 0 ? 1 : 0;
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|