Replace libuv with aml

aml
Andri Yngvason 2020-03-16 22:01:43 +00:00
parent 7ea17d04aa
commit 23e0928a2a
6 changed files with 88 additions and 79 deletions

View File

@ -18,12 +18,13 @@
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
#include <uv.h> #include <aml.h>
#include "frame-capture.h" #include "frame-capture.h"
struct zwlr_export_dmabuf_manager_v1; struct zwlr_export_dmabuf_manager_v1;
struct zwlr_export_dmabuf_frame_v1; struct zwlr_export_dmabuf_frame_v1;
struct wl_output; struct wl_output;
struct aml_timer;
struct dmabuf_plane { struct dmabuf_plane {
int fd; int fd;
@ -50,7 +51,7 @@ struct dmabuf_capture {
struct dmabuf_frame frame; struct dmabuf_frame frame;
uint64_t last_time; uint64_t last_time;
uv_timer_t timer; struct aml_timer* timer;
}; };
void dmabuf_capture_init(struct dmabuf_capture* self); void dmabuf_capture_init(struct dmabuf_capture* self);

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include <stdbool.h> #include <stdbool.h>
#include <uv.h>
#include "wlr-screencopy-unstable-v1.h" #include "wlr-screencopy-unstable-v1.h"
#include "frame-capture.h" #include "frame-capture.h"
@ -12,6 +11,7 @@ struct zwlr_screencopy_frame_v1;
struct wl_output; struct wl_output;
struct wl_buffer; struct wl_buffer;
struct wl_shm; struct wl_shm;
struct aml_timer;
enum screencopy_status { enum screencopy_status {
SCREENCOPY_STATUS_CAPTURING = 0, SCREENCOPY_STATUS_CAPTURING = 0,
@ -34,7 +34,7 @@ struct screencopy {
uint64_t last_time; uint64_t last_time;
uint64_t start_time; uint64_t start_time;
uv_timer_t timer; struct aml_timer* timer;
struct smooth delay_smoother; struct smooth delay_smoother;
double delay; double delay;

View File

@ -24,9 +24,9 @@ add_project_arguments(c_args, language: 'c')
cc = meson.get_compiler('c') cc = meson.get_compiler('c')
libm = cc.find_library('m', required: false) libm = cc.find_library('m', required: false)
librt = cc.find_library('rt', required: false)
pixman = dependency('pixman-1') pixman = dependency('pixman-1')
libuv = dependency('libuv')
egl = dependency('egl') egl = dependency('egl')
glesv2 = dependency('glesv2') glesv2 = dependency('glesv2')
xkbcommon = dependency('xkbcommon') xkbcommon = dependency('xkbcommon')
@ -37,6 +37,13 @@ neatvnc_project = subproject(
required: false, required: false,
) )
aml_project = subproject('aml', required: false)
if aml_project.found()
aml = aml_project.get_variable('aml_dep')
else
aml = dependency('aml')
endif
if neatvnc_project.found() if neatvnc_project.found()
neatvnc = neatvnc_project.get_variable('neatvnc_dep') neatvnc = neatvnc_project.get_variable('neatvnc_dep')
else else
@ -65,8 +72,9 @@ sources = [
dependencies = [ dependencies = [
libm, libm,
librt,
pixman, pixman,
libuv, aml,
egl, egl,
glesv2, glesv2,
wayland_client, wayland_client,

View File

@ -18,7 +18,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include <wayland-client.h> #include <wayland-client.h>
#include <uv.h> #include <aml.h>
#include <wayland-util.h> #include <wayland-util.h>
#include "frame-capture.h" #include "frame-capture.h"
@ -41,7 +41,7 @@ static void dmabuf_capture_stop(struct frame_capture* fc)
{ {
struct dmabuf_capture* self = (void*)fc; struct dmabuf_capture* self = (void*)fc;
if (uv_timer_stop(&self->timer)) if (aml_stop(aml_get_default(), self->timer) >= 0)
dmabuf_close_fds(self); dmabuf_close_fds(self);
fc->status = CAPTURE_STOPPED; fc->status = CAPTURE_STOPPED;
@ -64,7 +64,7 @@ static void dmabuf_frame_start(void* data,
struct dmabuf_capture* self = data; struct dmabuf_capture* self = data;
struct frame_capture* fc = data; struct frame_capture* fc = data;
uv_timer_stop(&self->timer); aml_stop(aml_get_default(), self->timer);
dmabuf_close_fds(self); dmabuf_close_fds(self);
uint64_t mod = ((uint64_t)mod_high << 32) | (uint64_t)mod_low; uint64_t mod = ((uint64_t)mod_high << 32) | (uint64_t)mod_low;
@ -99,9 +99,9 @@ static void dmabuf_frame_object(void* data,
self->frame.plane[plane_index].pitch = stride; self->frame.plane[plane_index].pitch = stride;
} }
static void dmabuf_timer_ready(uv_timer_t* timer) static void dmabuf_timer_ready(void* timer)
{ {
struct dmabuf_capture* self = wl_container_of(timer, self, timer); struct dmabuf_capture* self = aml_get_userdata(timer);
struct frame_capture* fc = (struct frame_capture*)self; struct frame_capture* fc = (struct frame_capture*)self;
if (fc->status != CAPTURE_IN_PROGRESS) if (fc->status != CAPTURE_IN_PROGRESS)
@ -130,7 +130,8 @@ static void dmabuf_frame_ready(void* data,
double time_left = (1.0 / RATE_LIMIT - dt) * 1.0e3; double time_left = (1.0 / RATE_LIMIT - dt) * 1.0e3;
if (time_left >= 0.0) { if (time_left >= 0.0) {
uv_timer_start(&self->timer, dmabuf_timer_ready, time_left, 0); aml_set_duration(self->timer, time_left);
aml_start(aml_get_default(), self->timer);
frame_capture_start(fc); frame_capture_start(fc);
return; return;
} }
@ -188,7 +189,8 @@ static int dmabuf_capture_start(struct frame_capture* fc)
void dmabuf_capture_init(struct dmabuf_capture* self) void dmabuf_capture_init(struct dmabuf_capture* self)
{ {
uv_timer_init(uv_default_loop(), &self->timer); self->timer = aml_timer_new(0, dmabuf_timer_ready, self, NULL);
assert(self->timer);
self->fc.backend.start = dmabuf_capture_start; self->fc.backend.start = dmabuf_capture_start;
self->fc.backend.stop = dmabuf_capture_stop; self->fc.backend.stop = dmabuf_capture_stop;

View File

@ -25,7 +25,8 @@
#include <inttypes.h> #include <inttypes.h>
#include <errno.h> #include <errno.h>
#include <neatvnc.h> #include <neatvnc.h>
#include <uv.h> #include <aml.h>
#include <signal.h>
#include <libdrm/drm_fourcc.h> #include <libdrm/drm_fourcc.h>
#include <wayland-client-protocol.h> #include <wayland-client-protocol.h>
#include <wayland-client.h> #include <wayland-client.h>
@ -59,6 +60,8 @@ enum frame_capture_backend_type {
}; };
struct wayvnc { struct wayvnc {
bool do_exit;
struct wl_display* display; struct wl_display* display;
struct wl_registry* registry; struct wl_registry* registry;
struct wl_list outputs; struct wl_list outputs;
@ -81,22 +84,18 @@ struct wayvnc {
struct pointer pointer_backend; struct pointer pointer_backend;
struct keyboard keyboard_backend; struct keyboard keyboard_backend;
uv_poll_t wayland_poller; struct aml_handler* wayland_handler;
uv_prepare_t flusher; struct aml_signal* signal_handler;
uv_signal_t signal_handler;
uv_timer_t performance_timer;
struct nvnc* nvnc; struct nvnc* nvnc;
struct nvnc_fb* current_fb; struct nvnc_fb* current_fb;
struct nvnc_fb* last_fb; struct nvnc_fb* last_fb;
uint32_t n_frames;
const char* kb_layout; const char* kb_layout;
}; };
void wayvnc_exit(void); void wayvnc_exit(struct wayvnc* self);
void on_capture_done(struct frame_capture* capture); void on_capture_done(struct frame_capture* capture);
static enum frame_capture_backend_type static enum frame_capture_backend_type
@ -314,65 +313,49 @@ failure:
return -1; return -1;
} }
void on_wayland_event(uv_poll_t* handle, int status, int event) void on_wayland_event(void* obj)
{ {
if (event & UV_DISCONNECT) { struct wayvnc* self = aml_get_userdata(obj);
log_error("The compositor is gone. Exiting...\n"); // TODO: Check if the compositor has gone away
wayvnc_exit();
return;
}
struct wayvnc* self = wl_container_of(handle, self, wayland_poller);
wl_display_dispatch(self->display); wl_display_dispatch(self->display);
} }
void prepare_for_poll(uv_prepare_t* handle) void wayvnc_exit(struct wayvnc* self)
{ {
struct wayvnc* self = wl_container_of(handle, self, flusher); self->do_exit = true;
wl_display_flush(self->display);
} }
void on_uv_walk(uv_handle_t* handle, void* arg) void on_signal(void* obj)
{ {
uv_unref(handle); struct wayvnc* self = aml_get_userdata(obj);
} wayvnc_exit(self);
void wayvnc_exit(void)
{
uv_walk(uv_default_loop(), on_uv_walk, NULL);
}
void on_signal(uv_signal_t* handle, int signo)
{
wayvnc_exit();
}
void on_performance_tick(uv_timer_t* handle)
{
struct wayvnc* self = wl_container_of(handle, self, performance_timer);
printf("%"PRIu32" FPS\n", self->n_frames);
self->n_frames = 0;
} }
int init_main_loop(struct wayvnc* self) int init_main_loop(struct wayvnc* self)
{ {
uv_loop_t* loop = uv_default_loop(); struct aml* loop = aml_get_default();
uv_poll_init(loop, &self->wayland_poller, struct aml_handler* wl_handler;
wl_display_get_fd(self->display)); wl_handler = aml_handler_new(wl_display_get_fd(self->display),
uv_poll_start(&self->wayland_poller, UV_READABLE | UV_DISCONNECT, on_wayland_event, self, NULL);
on_wayland_event); if (!wl_handler)
return -1;
uv_prepare_init(loop, &self->flusher); int rc = aml_start(loop, wl_handler);
uv_prepare_start(&self->flusher, prepare_for_poll); aml_unref(wl_handler);
if (rc < 0)
return -1;
uv_signal_init(loop, &self->signal_handler); struct aml_signal* sig;
uv_signal_start(&self->signal_handler, on_signal, SIGINT); sig = aml_signal_new(SIGINT, on_signal, self, NULL);
if (!sig)
return -1;
uv_timer_init(loop, &self->performance_timer); rc = aml_start(loop, sig);
uv_timer_start(&self->performance_timer, on_performance_tick, 1000, aml_unref(sig);
1000); if (rc < 0)
return -1;
return 0; return 0;
} }
@ -468,7 +451,7 @@ void on_damage_check_done(struct pixman_region16* damage, void* userdata)
if (wayvnc_start_capture(self) < 0) { if (wayvnc_start_capture(self) < 0) {
log_error("Failed to start capture. Exiting...\n"); log_error("Failed to start capture. Exiting...\n");
wayvnc_exit(); wayvnc_exit(self);
} }
} }
@ -509,7 +492,7 @@ void wayvnc_update_vnc(struct wayvnc* self, struct nvnc_fb* fb)
if (wayvnc_start_capture(self) < 0) { if (wayvnc_start_capture(self) < 0) {
log_error("Failed to start capture. Exiting...\n"); log_error("Failed to start capture. Exiting...\n");
wayvnc_exit(); wayvnc_exit(self);
} }
} }
@ -566,16 +549,15 @@ void on_capture_done(struct frame_capture* capture)
break; break;
case CAPTURE_FATAL: case CAPTURE_FATAL:
log_error("Fatal error while capturing. Exiting...\n"); log_error("Fatal error while capturing. Exiting...\n");
wayvnc_exit(); wayvnc_exit(self);
break; break;
case CAPTURE_FAILED: case CAPTURE_FAILED:
if (wayvnc_start_capture(self) < 0) { if (wayvnc_start_capture(self) < 0) {
log_error("Failed to start capture. Exiting...\n"); log_error("Failed to start capture. Exiting...\n");
wayvnc_exit(); wayvnc_exit(self);
} }
break; break;
case CAPTURE_DONE: case CAPTURE_DONE:
self->n_frames++;
if (self->capture_backend == (struct frame_capture*)&self->screencopy_backend) if (self->capture_backend == (struct frame_capture*)&self->screencopy_backend)
wayvnc_process_screen(self); wayvnc_process_screen(self);
else else
@ -803,6 +785,12 @@ int main(int argc, char* argv[])
goto failure; goto failure;
} }
struct aml* aml = aml_new(NULL, 0);
if (!aml)
goto main_loop_failure;
aml_set_default(aml);
if (init_main_loop(&self) < 0) if (init_main_loop(&self) < 0)
goto main_loop_failure; goto main_loop_failure;
@ -847,9 +835,13 @@ int main(int argc, char* argv[])
if (wayvnc_start_capture(&self) < 0) if (wayvnc_start_capture(&self) < 0)
goto capture_failure; goto capture_failure;
uv_run(uv_default_loop(), UV_RUN_DEFAULT); wl_display_dispatch(self.display);
uv_loop_close(uv_default_loop()); while (!self.do_exit) {
wl_display_flush(self.display);
aml_poll(aml, -1);
aml_dispatch(aml);
}
frame_capture_stop(self.capture_backend); frame_capture_stop(self.capture_backend);
@ -860,6 +852,7 @@ int main(int argc, char* argv[])
renderer_destroy(&self.renderer); renderer_destroy(&self.renderer);
screencopy_destroy(&self.screencopy_backend); screencopy_destroy(&self.screencopy_backend);
wayvnc_destroy(&self); wayvnc_destroy(&self);
aml_unref(aml);
return 0; return 0;

View File

@ -22,7 +22,7 @@
#include <wayland-client-protocol.h> #include <wayland-client-protocol.h>
#include <wayland-client.h> #include <wayland-client.h>
#include <libdrm/drm_fourcc.h> #include <libdrm/drm_fourcc.h>
#include <uv.h> #include <aml.h>
#include "shm.h" #include "shm.h"
#include "screencopy.h" #include "screencopy.h"
@ -87,7 +87,7 @@ static void screencopy_stop(struct frame_capture* fc)
{ {
struct screencopy* self = (void*)fc; struct screencopy* self = (void*)fc;
uv_timer_stop(&self->timer); aml_stop(aml_get_default(), self->timer);
if (self->frame) { if (self->frame) {
zwlr_screencopy_frame_v1_destroy(self->frame); zwlr_screencopy_frame_v1_destroy(self->frame);
@ -199,9 +199,9 @@ static int screencopy__start_capture(struct frame_capture* fc)
return 0; return 0;
} }
static void screencopy__poll(uv_timer_t* timer) static void screencopy__poll(void* obj)
{ {
struct screencopy* self = wl_container_of(timer, self, timer); struct screencopy* self = aml_get_userdata(obj);
struct frame_capture* fc = (struct frame_capture*)self; struct frame_capture* fc = (struct frame_capture*)self;
screencopy__start_capture(fc); screencopy__start_capture(fc);
@ -220,14 +220,18 @@ static int screencopy_start(struct frame_capture* fc)
fc->status = CAPTURE_IN_PROGRESS; fc->status = CAPTURE_IN_PROGRESS;
return time_left > 0 ? if (time_left > 0) {
uv_timer_start(&self->timer, screencopy__poll, time_left, 0) : aml_set_duration(self->timer, time_left);
screencopy__start_capture(fc); return aml_start(aml_get_default(), self->timer);
}
return screencopy__start_capture(fc);
} }
void screencopy_init(struct screencopy* self) void screencopy_init(struct screencopy* self)
{ {
uv_timer_init(uv_default_loop(), &self->timer); self->timer = aml_timer_new(0, screencopy__poll, self, NULL);
assert(self->timer);
self->delay_smoother.time_constant = DELAY_SMOOTHER_TIME_CONSTANT; self->delay_smoother.time_constant = DELAY_SMOOTHER_TIME_CONSTANT;
@ -237,7 +241,8 @@ void screencopy_init(struct screencopy* self)
void screencopy_destroy(struct screencopy* self) void screencopy_destroy(struct screencopy* self)
{ {
uv_timer_stop(&self->timer); aml_stop(aml_get_default(), self->timer);
aml_unref(self->timer);
if (self->buffer) if (self->buffer)
wl_buffer_destroy(self->buffer); wl_buffer_destroy(self->buffer);