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

View File

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

View File

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

View File

@ -18,7 +18,7 @@
#include <stdlib.h>
#include <assert.h>
#include <wayland-client.h>
#include <uv.h>
#include <aml.h>
#include <wayland-util.h>
#include "frame-capture.h"
@ -41,7 +41,7 @@ static void dmabuf_capture_stop(struct frame_capture* 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);
fc->status = CAPTURE_STOPPED;
@ -64,7 +64,7 @@ static void dmabuf_frame_start(void* data,
struct dmabuf_capture* self = data;
struct frame_capture* fc = data;
uv_timer_stop(&self->timer);
aml_stop(aml_get_default(), self->timer);
dmabuf_close_fds(self);
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;
}
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;
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;
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);
return;
}
@ -188,7 +189,8 @@ static int dmabuf_capture_start(struct frame_capture* fc)
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.stop = dmabuf_capture_stop;

View File

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

View File

@ -22,7 +22,7 @@
#include <wayland-client-protocol.h>
#include <wayland-client.h>
#include <libdrm/drm_fourcc.h>
#include <uv.h>
#include <aml.h>
#include "shm.h"
#include "screencopy.h"
@ -87,7 +87,7 @@ static void screencopy_stop(struct frame_capture* fc)
{
struct screencopy* self = (void*)fc;
uv_timer_stop(&self->timer);
aml_stop(aml_get_default(), self->timer);
if (self->frame) {
zwlr_screencopy_frame_v1_destroy(self->frame);
@ -199,9 +199,9 @@ static int screencopy__start_capture(struct frame_capture* fc)
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;
screencopy__start_capture(fc);
@ -220,14 +220,18 @@ static int screencopy_start(struct frame_capture* fc)
fc->status = CAPTURE_IN_PROGRESS;
return time_left > 0 ?
uv_timer_start(&self->timer, screencopy__poll, time_left, 0) :
screencopy__start_capture(fc);
if (time_left > 0) {
aml_set_duration(self->timer, time_left);
return aml_start(aml_get_default(), self->timer);
}
return screencopy__start_capture(fc);
}
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;
@ -237,7 +241,8 @@ void screencopy_init(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)
wl_buffer_destroy(self->buffer);