diff --git a/include/display.h b/include/display.h index a3d289d..dfed4ba 100644 --- a/include/display.h +++ b/include/display.h @@ -17,18 +17,18 @@ #pragma once #include "neatvnc.h" -#include "resampler.h" #include #include struct nvnc; struct nvnc_fb; +struct resampler; struct nvnc_display { int ref; struct nvnc* server; uint16_t x_pos, y_pos; struct nvnc_fb* buffer; - struct resampler resampler; + struct resampler* resampler; }; diff --git a/include/resampler.h b/include/resampler.h index 637edd1..cc90650 100644 --- a/include/resampler.h +++ b/include/resampler.h @@ -1,19 +1,34 @@ +/* + * Copyright (c) 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 nvnc_fb; -struct nvnc_fb_pool; struct pixman_region16; -struct resampler { - struct nvnc_fb_pool *pool; - void (*on_done)(struct resampler*, struct nvnc_fb*, - struct pixman_region16* damage); -}; +struct resampler; -int resampler_init(struct resampler*); +typedef void (*resampler_fn)(struct nvnc_fb*, struct pixman_region16* damage, + void* userdata); + +struct resampler* resampler_create(void); void resampler_destroy(struct resampler*); int resampler_feed(struct resampler*, struct nvnc_fb* fb, - struct pixman_region16* damage); + struct pixman_region16* damage, resampler_fn on_done, + void* userdata); diff --git a/src/display.c b/src/display.c index abb27fb..dddc8c1 100644 --- a/src/display.c +++ b/src/display.c @@ -18,18 +18,17 @@ #include "neatvnc.h" #include "common.h" #include "fb.h" -#include "type-macros.h" +#include "resampler.h" #include #include #define EXPORT __attribute__((visibility("default"))) -static void nvnc_display__on_resampler_done(struct resampler* resampler, - struct nvnc_fb* fb, struct pixman_region16* damage) +static void nvnc_display__on_resampler_done(struct nvnc_fb* fb, + struct pixman_region16* damage, void* userdata) { - struct nvnc_display* self = container_of(resampler, struct nvnc_display, - resampler); + struct nvnc_display* self = userdata; if (self->buffer) { nvnc_fb_release(self->buffer); @@ -52,7 +51,8 @@ struct nvnc_display* nvnc_display_new(uint16_t x_pos, uint16_t y_pos) if (!self) return NULL; - if (resampler_init(&self->resampler) < 0) { + self->resampler = resampler_create(); + if (!self->resampler) { free(self); return NULL; } @@ -60,7 +60,6 @@ struct nvnc_display* nvnc_display_new(uint16_t x_pos, uint16_t y_pos) self->ref = 1; self->x_pos = x_pos; self->y_pos = y_pos; - self->resampler.on_done = nvnc_display__on_resampler_done; return self; } @@ -71,7 +70,7 @@ static void nvnc__display_free(struct nvnc_display* self) nvnc_fb_release(self->buffer); nvnc_fb_unref(self->buffer); } - resampler_destroy(&self->resampler); + resampler_destroy(self->resampler); free(self); } @@ -98,5 +97,6 @@ EXPORT void nvnc_display_feed_buffer(struct nvnc_display* self, struct nvnc_fb* fb, struct pixman_region16* damage) { - resampler_feed(&self->resampler, fb, damage); + resampler_feed(self->resampler, fb, damage, + nvnc_display__on_resampler_done, self); } diff --git a/src/resampler.c b/src/resampler.c index 7c3cf39..4764dff 100644 --- a/src/resampler.c +++ b/src/resampler.c @@ -1,3 +1,19 @@ +/* + * Copyright (c) 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 "resampler.h" #include "neatvnc.h" #include "fb.h" @@ -10,11 +26,16 @@ #include #include +struct resampler { + struct nvnc_fb_pool *pool; +}; + struct resampler_work { struct pixman_region16 damage; struct nvnc_fb* src; struct nvnc_fb* dst; - struct resampler* resampler; + resampler_fn on_done; + void* userdata; }; static void resampler_work_free(void* userdata) @@ -31,15 +52,25 @@ static void resampler_work_free(void* userdata) free(work); } -int resampler_init(struct resampler* self) +struct resampler* resampler_create(void) { + struct resampler* self = calloc(1, sizeof(*self)); + if (!self) + return NULL; + self->pool = nvnc_fb_pool_new(0, 0, DRM_FORMAT_INVALID, 0); - return self->pool ? 0 : -1; + if (!self->pool) { + free(self); + return NULL; + } + + return self; } void resampler_destroy(struct resampler* self) { nvnc_fb_pool_unref(self->pool); + free(self); } static void do_work(void* handle) @@ -93,14 +124,15 @@ static void on_work_done(void* handle) struct aml_work* work = handle; struct resampler_work* ctx = aml_get_userdata(work); - ctx->resampler->on_done(ctx->resampler, ctx->dst, &ctx->damage); + ctx->on_done(ctx->dst, &ctx->damage, ctx->userdata); } int resampler_feed(struct resampler* self, struct nvnc_fb* fb, - struct pixman_region16* damage) + struct pixman_region16* damage, resampler_fn on_done, + void* userdata) { if (fb->transform == NVNC_TRANSFORM_NORMAL) { - self->on_done(self, fb, damage); + on_done(fb, damage, userdata); return 0; } @@ -127,7 +159,8 @@ int resampler_feed(struct resampler* self, struct nvnc_fb* fb, nvnc_fb_ref(fb); nvnc_fb_hold(fb); - ctx->resampler = self; + ctx->on_done = on_done; + ctx->userdata = userdata; struct aml_work* work = aml_work_new(do_work, on_work_done, ctx, resampler_work_free);