From 26ff812ea6c3fdb2d5fa89d9b91fbf5343986824 Mon Sep 17 00:00:00 2001 From: Andri Yngvason Date: Mon, 20 Sep 2021 21:15:32 +0000 Subject: [PATCH] Add damage refinery from wayvnc --- include/damage-refinery.h | 39 ++++++++++ include/display.h | 2 + include/murmurhash.h | 48 +++++++++++++ include/transform-util.h | 4 ++ meson.build | 2 + src/damage-refinery.c | 147 ++++++++++++++++++++++++++++++++++++++ src/display.c | 34 +++++++-- src/murmurhash.c | 91 +++++++++++++++++++++++ src/transform-util.c | 98 +++++++++++++++++++++++++ 9 files changed, 460 insertions(+), 5 deletions(-) create mode 100644 include/damage-refinery.h create mode 100644 include/murmurhash.h create mode 100644 src/damage-refinery.c create mode 100644 src/murmurhash.c diff --git a/include/damage-refinery.h b/include/damage-refinery.h new file mode 100644 index 0000000..ce3ac9f --- /dev/null +++ b/include/damage-refinery.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2020 - 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 + * 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. + */ + +#pragma once + +#include + +struct pixman_region16; +struct nvnc_fb; + +struct damage_refinery { + uint32_t* hashes; + uint32_t width; + uint32_t height; +}; + +int damage_refinery_init(struct damage_refinery* self, uint32_t width, + uint32_t height); +int damage_refinery_resize(struct damage_refinery* self, uint32_t width, + uint32_t height); +void damage_refinery_destroy(struct damage_refinery* self); + +void damage_refine(struct damage_refinery* self, + struct pixman_region16* refined, + struct pixman_region16* hint, + struct nvnc_fb* buffer); diff --git a/include/display.h b/include/display.h index dfed4ba..99599ce 100644 --- a/include/display.h +++ b/include/display.h @@ -17,6 +17,7 @@ #pragma once #include "neatvnc.h" +#include "damage-refinery.h" #include #include @@ -31,4 +32,5 @@ struct nvnc_display { uint16_t x_pos, y_pos; struct nvnc_fb* buffer; struct resampler* resampler; + struct damage_refinery damage_refinery; }; diff --git a/include/murmurhash.h b/include/murmurhash.h new file mode 100644 index 0000000..b12ac2d --- /dev/null +++ b/include/murmurhash.h @@ -0,0 +1,48 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2014 Joseph Werle + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef MURMURHASH_H +#define MURMURHASH_H 1 + +#include + +#define MURMURHASH_VERSION "0.0.3" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Returns a murmur hash of `key' based on `seed' + * using the MurmurHash3 algorithm + */ + +uint32_t +murmurhash (const char *, uint32_t, uint32_t); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/transform-util.h b/include/transform-util.h index 7322f9d..929163c 100644 --- a/include/transform-util.h +++ b/include/transform-util.h @@ -25,3 +25,7 @@ void nvnc_transform_to_pixman_transform(pixman_transform_t* dst, void nvnc_transform_dimensions(enum nvnc_transform transform, uint32_t* width, uint32_t* height); + +void nvnc_transform_region(struct pixman_region16* dst, + struct pixman_region16* src, enum nvnc_transform transform, + int width, int height); diff --git a/meson.build b/meson.build index c450abb..e134d9f 100644 --- a/meson.build +++ b/meson.build @@ -75,6 +75,8 @@ sources = [ 'src/qnum-to-evdev.c', 'src/resampler.c', 'src/transform-util.c', + 'src/damage-refinery.c', + 'src/murmurhash.c', ] dependencies = [ diff --git a/src/damage-refinery.c b/src/damage-refinery.c new file mode 100644 index 0000000..7ec5bd0 --- /dev/null +++ b/src/damage-refinery.c @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2020 - 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 + * 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. + */ + +#include +#include +#include +#include +#include +#include + +#include "fb.h" +#include "damage-refinery.h" +#include "murmurhash.h" + +#define UDIV_UP(a, b) (((a) + (b) - 1) / (b)) + +#define HASH_SEED 0 + +int damage_refinery_init(struct damage_refinery* self, uint32_t width, + uint32_t height) +{ + self->width = width; + self->height = height; + + uint32_t twidth = UDIV_UP(width, 32); + uint32_t theight = UDIV_UP(height, 32); + + self->hashes = calloc(twidth * theight, sizeof(*self->hashes)); + if (!self->hashes) + return -1; + + return 0; +} + +int damage_refinery_resize(struct damage_refinery* self, uint32_t width, + uint32_t height) +{ + if (width == self->width && height == self->height) + return 0; + + damage_refinery_destroy(self); + return damage_refinery_init(self, width, height); +} + +void damage_refinery_destroy(struct damage_refinery* self) +{ + free(self->hashes); +} + +static uint32_t damage_hash_tile(struct damage_refinery* self, uint32_t tx, + uint32_t ty, const struct nvnc_fb* buffer) +{ + uint32_t* pixels = buffer->addr; + int pixel_stride = buffer->stride; + + int x_start = tx * 32; + int x_stop = MIN((tx + 1) * 32, self->width); + int y_start = ty * 32; + int y_stop = MIN((ty + 1) * 32, self->height); + + uint32_t hash = 0; + + // TODO: Support different pixel sizes + for (int y = y_start; y < y_stop; ++y) + hash = murmurhash((void*)&(pixels[x_start + y * pixel_stride]), + 4 * (x_stop - x_start), hash); + + return hash; +} + +static uint32_t* damage_tile_hash_ptr(struct damage_refinery* self, + uint32_t tx, uint32_t ty) +{ + uint32_t twidth = UDIV_UP(self->width, 32); + return &self->hashes[tx + ty * twidth]; +} + +static void damage_refine_tile(struct damage_refinery* self, + struct pixman_region16* refined, uint32_t tx, uint32_t ty, + const struct nvnc_fb* buffer) +{ + uint32_t hash = damage_hash_tile(self, tx, ty, buffer); + uint32_t* old_hash_ptr = damage_tile_hash_ptr(self, tx, ty); + int is_damaged = hash != *old_hash_ptr; + *old_hash_ptr = hash; + + if (is_damaged) + pixman_region_union_rect(refined, refined, tx * 32, ty * 32, 32, + 32); +} + +static void tile_region_from_region(struct pixman_region16* dst, + struct pixman_region16* src) +{ + int n_rects = 0; + struct pixman_box16* rects = pixman_region_rectangles(src, &n_rects); + + for (int i = 0; i < n_rects; ++i) { + int x1 = rects[i].x1 / 32; + int y1 = rects[i].y1 / 32; + int x2 = UDIV_UP(rects[i].x2, 32); + int y2 = UDIV_UP(rects[i].y2, 32); + + pixman_region_union_rect(dst, dst, x1, y1, x2 - x1, y2 - y1); + } +} + +void damage_refine(struct damage_refinery* self, + struct pixman_region16* refined, + struct pixman_region16* hint, + struct nvnc_fb* buffer) +{ + assert(self->width == (uint32_t)buffer->width && + self->height == (uint32_t)buffer->height); + + nvnc_fb_map(buffer); + + struct pixman_region16 tile_region; + pixman_region_init(&tile_region); + tile_region_from_region(&tile_region, hint); + + int n_rects = 0; + struct pixman_box16* rects = pixman_region_rectangles(&tile_region, + &n_rects); + + for (int i = 0; i < n_rects; ++i) + for (int ty = rects[i].y1; ty < rects[i].y2; ++ty) + for (int tx = rects[i].x1; tx < rects[i].x2; ++tx) + damage_refine_tile(self, refined, tx, ty, buffer); + + pixman_region_fini(&tile_region); + pixman_region_intersect_rect(refined, refined, 0, 0, self->width, + self->height); +} diff --git a/src/display.c b/src/display.c index dddc8c1..b76c014 100644 --- a/src/display.c +++ b/src/display.c @@ -19,6 +19,7 @@ #include "common.h" #include "fb.h" #include "resampler.h" +#include "transform-util.h" #include #include @@ -52,16 +53,24 @@ struct nvnc_display* nvnc_display_new(uint16_t x_pos, uint16_t y_pos) return NULL; self->resampler = resampler_create(); - if (!self->resampler) { - free(self); - return NULL; - } + if (!self->resampler) + goto resampler_failure; + + if (damage_refinery_init(&self->damage_refinery, 0, 0) < 0) + goto refinery_failure; self->ref = 1; self->x_pos = x_pos; self->y_pos = y_pos; return self; + +refinery_failure: + resampler_destroy(self->resampler); +resampler_failure: + free(self); + + return NULL; } static void nvnc__display_free(struct nvnc_display* self) @@ -70,6 +79,7 @@ static void nvnc__display_free(struct nvnc_display* self) nvnc_fb_release(self->buffer); nvnc_fb_unref(self->buffer); } + damage_refinery_destroy(&self->damage_refinery); resampler_destroy(self->resampler); free(self); } @@ -97,6 +107,20 @@ EXPORT void nvnc_display_feed_buffer(struct nvnc_display* self, struct nvnc_fb* fb, struct pixman_region16* damage) { - resampler_feed(self->resampler, fb, damage, + damage_refinery_resize(&self->damage_refinery, fb->width, fb->height); + + // TODO: Run the refinery in a worker thread? + struct pixman_region16 refined_damage; + pixman_region_init(&refined_damage); + damage_refine(&self->damage_refinery, &refined_damage, damage, fb); + + struct pixman_region16 transformed_damage; + pixman_region_init(&transformed_damage); + nvnc_transform_region(&transformed_damage, &refined_damage, + fb->transform, fb->width, fb->height); + + resampler_feed(self->resampler, fb, &transformed_damage, nvnc_display__on_resampler_done, self); + + pixman_region_fini(&refined_damage); } diff --git a/src/murmurhash.c b/src/murmurhash.c new file mode 100644 index 0000000..2edbf0e --- /dev/null +++ b/src/murmurhash.c @@ -0,0 +1,91 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2014 Joseph Werle + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#include +#include +#include +#include "murmurhash.h" + +uint32_t +murmurhash (const char *key, uint32_t len, uint32_t seed) { + uint32_t c1 = 0xcc9e2d51; + uint32_t c2 = 0x1b873593; + uint32_t r1 = 15; + uint32_t r2 = 13; + uint32_t m = 5; + uint32_t n = 0xe6546b64; + uint32_t h = 0; + uint32_t k = 0; + uint8_t *d = (uint8_t *) key; // 32 bit extract from `key' + const uint32_t *chunks = NULL; + const uint8_t *tail = NULL; // tail - last 8 bytes + int i = 0; + int l = len / 4; // chunk length + + h = seed; + + chunks = (const uint32_t *) (d + l * 4); // body + tail = (const uint8_t *) (d + l * 4); // last 8 byte chunk of `key' + + // for each 4 byte chunk of `key' + for (i = -l; i != 0; ++i) { + // next 4 byte chunk of `key' + k = chunks[i]; + + // encode next 4 byte chunk of `key' + k *= c1; + k = (k << r1) | (k >> (32 - r1)); + k *= c2; + + // append to hash + h ^= k; + h = (h << r2) | (h >> (32 - r2)); + h = h * m + n; + } + + k = 0; + + // remainder + switch (len & 3) { // `len % 4' + case 3: k ^= (tail[2] << 16); + case 2: k ^= (tail[1] << 8); + + case 1: + k ^= tail[0]; + k *= c1; + k = (k << r1) | (k >> (32 - r1)); + k *= c2; + h ^= k; + } + + h ^= len; + + h ^= (h >> 16); + h *= 0x85ebca6b; + h ^= (h >> 13); + h *= 0xc2b2ae35; + h ^= (h >> 16); + + return h; +} diff --git a/src/transform-util.c b/src/transform-util.c index 2cf85c2..ca4822a 100644 --- a/src/transform-util.c +++ b/src/transform-util.c @@ -12,6 +12,28 @@ * 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. + * + * For code borrowed from wlroots: + * Copyright (c) 2017, 2018 Drew DeVault + * Copyright (c) 2014 Jari Vetoniemi + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. */ #include "transform-util.h" @@ -138,3 +160,79 @@ void nvnc_transform_dimensions(enum nvnc_transform transform, uint32_t* width, *height = tmp; } } + +/* Borrowed this from wlroots */ +void nvnc_transform_region(struct pixman_region16* dst, + struct pixman_region16* src, enum nvnc_transform transform, + int width, int height) +{ + if (transform == NVNC_TRANSFORM_NORMAL) { + pixman_region_copy(dst, src); + return; + } + + int nrects = 0; + pixman_box16_t* src_rects = pixman_region_rectangles(src, &nrects); + + pixman_box16_t* dst_rects = malloc(nrects * sizeof(*dst_rects)); + if (dst_rects == NULL) { + return; + } + + for (int i = 0; i < nrects; ++i) { + switch (transform) { + case NVNC_TRANSFORM_NORMAL: + dst_rects[i].x1 = src_rects[i].x1; + dst_rects[i].y1 = src_rects[i].y1; + dst_rects[i].x2 = src_rects[i].x2; + dst_rects[i].y2 = src_rects[i].y2; + break; + case NVNC_TRANSFORM_90: + dst_rects[i].x1 = height - src_rects[i].y2; + dst_rects[i].y1 = src_rects[i].x1; + dst_rects[i].x2 = height - src_rects[i].y1; + dst_rects[i].y2 = src_rects[i].x2; + break; + case NVNC_TRANSFORM_180: + dst_rects[i].x1 = width - src_rects[i].x2; + dst_rects[i].y1 = height - src_rects[i].y2; + dst_rects[i].x2 = width - src_rects[i].x1; + dst_rects[i].y2 = height - src_rects[i].y1; + break; + case NVNC_TRANSFORM_270: + dst_rects[i].x1 = src_rects[i].y1; + dst_rects[i].y1 = width - src_rects[i].x2; + dst_rects[i].x2 = src_rects[i].y2; + dst_rects[i].y2 = width - src_rects[i].x1; + break; + case NVNC_TRANSFORM_FLIPPED: + dst_rects[i].x1 = width - src_rects[i].x2; + dst_rects[i].y1 = src_rects[i].y1; + dst_rects[i].x2 = width - src_rects[i].x1; + dst_rects[i].y2 = src_rects[i].y2; + break; + case NVNC_TRANSFORM_FLIPPED_90: + dst_rects[i].x1 = src_rects[i].y1; + dst_rects[i].y1 = src_rects[i].x1; + dst_rects[i].x2 = src_rects[i].y2; + dst_rects[i].y2 = src_rects[i].x2; + break; + case NVNC_TRANSFORM_FLIPPED_180: + dst_rects[i].x1 = src_rects[i].x1; + dst_rects[i].y1 = height - src_rects[i].y2; + dst_rects[i].x2 = src_rects[i].x2; + dst_rects[i].y2 = height - src_rects[i].y1; + break; + case NVNC_TRANSFORM_FLIPPED_270: + dst_rects[i].x1 = height - src_rects[i].y2; + dst_rects[i].y1 = width - src_rects[i].x2; + dst_rects[i].x2 = height - src_rects[i].y1; + dst_rects[i].y2 = width - src_rects[i].x1; + break; + } + } + + pixman_region_fini(dst); + pixman_region_init_rects(dst, dst_rects, nrects); + free(dst_rects); +}