Finally implement the commit/ack/frame protocol and improve repaint loop.
This implements the commit/ack/frame protocol that let clients batch up
a series of requests and then commit them atomically using the commit request.
The commit requests generats two following events: the acknowledge event,
which lets the client know that the server has received the request and
which frame the rendering has been scheduled for. At this point the client
can start rendering the next frame or free up temporary buffers. Then when
the compositor finally makes the newly composited frame visible on screen
the server sends a frame event, which contains the number of the frame that
was presented and the time when it happened.
The window and flower clients have been updated to use these two events in
their main loops and everything now updates per frame. The EGL compositor
repaint loop has been tweaked to delay the compositing of the screen to
10ms after last swapbuffer completed so as to allow processing as many
requests as possible before blocking on the next vertical retrace.
diff --git a/egl-compositor.c b/egl-compositor.c
index 132fcbd..7d6cd0e 100644
--- a/egl-compositor.c
+++ b/egl-compositor.c
@@ -12,10 +12,13 @@
#include <cairo.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <glib.h>
+#include <sys/poll.h>
#include <png.h>
#include <math.h>
#include <linux/input.h>
#include <xf86drmMode.h>
+#include <sys/timerfd.h>
+#include <time.h>
#include "wayland.h"
#include "cairo-util.h"
@@ -38,6 +41,14 @@
struct egl_surface *background;
struct egl_surface *overlay;
double overlay_y, overlay_target, overlay_previous;
+
+ /* Repaint state. */
+ struct wl_event_source *timer_source;
+ int repaint_needed;
+ int repaint_on_timeout;
+ int timer_fd;
+ struct timespec previous_swap;
+ uint32_t current_frame;
};
struct egl_surface {
@@ -516,12 +527,24 @@
}
static void
-repaint(void *data)
+repaint(int fd, uint32_t mask, void *data)
{
struct egl_compositor *ec = data;
+ struct itimerspec its;
struct wl_surface_iterator *iterator;
struct wl_surface *surface;
struct egl_surface *es;
+ struct timespec ts;
+ uint64_t expires;
+ uint32_t msecs;
+
+ if (ec->repaint_on_timeout)
+ read(fd, &expires, sizeof expires);
+
+ if (!ec->repaint_needed) {
+ ec->repaint_on_timeout = 0;
+ return;
+ }
draw_surface(ec->background);
@@ -540,19 +563,42 @@
draw_surface(ec->pointer);
eglSwapBuffers(ec->display, ec->surface);
+ ec->repaint_needed = 0;
- wl_display_post_acknowledge(ec->wl_display);
+ clock_gettime(CLOCK_MONOTONIC, &ts);
+ msecs = ts.tv_sec * 1000 + ts.tv_nsec / (1000 * 1000);
+ wl_display_post_frame(ec->wl_display, ec->current_frame, msecs);
+ ec->current_frame++;
+
+ its.it_interval.tv_sec = 0;
+ its.it_interval.tv_nsec = 0;
+ its.it_value.tv_sec = 0;
+ its.it_value.tv_nsec = 10 * 1000 * 1000;
+ if (timerfd_settime(ec->timer_fd, 0, &its, NULL) < 0) {
+ fprintf(stderr, "could not set timerfd\n: %m");
+ return;
+ }
+ ec->repaint_on_timeout = 1;
animate_overlay(ec);
}
static void
+idle_repaint(void *data)
+{
+ repaint(0, 0, data);
+}
+
+static void
schedule_repaint(struct egl_compositor *ec)
{
struct wl_event_loop *loop;
- loop = wl_display_get_event_loop(ec->wl_display);
- wl_event_loop_add_idle(loop, repaint, ec);
+ ec->repaint_needed = 1;
+ if (!ec->repaint_on_timeout) {
+ loop = wl_display_get_event_loop(ec->wl_display);
+ wl_event_loop_add_idle(loop, idle_repaint, ec);
+ }
}
static void
@@ -611,15 +657,12 @@
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
eglBindTexImage(ec->display, es->surface, GL_TEXTURE_2D);
-
- schedule_repaint(ec);
}
static void
notify_surface_map(struct wl_compositor *compositor,
struct wl_surface *surface, struct wl_map *map)
{
- struct egl_compositor *ec = (struct egl_compositor *) compositor;
struct egl_surface *es;
es = wl_surface_get_data(surface);
@@ -627,8 +670,6 @@
return;
es->map = *map;
-
- schedule_repaint(ec);
}
static void
@@ -654,7 +695,7 @@
eglCopyNativeBuffers(ec->display, es->surface, GL_FRONT_LEFT, dst_x, dst_y,
src, GL_FRONT_LEFT, x, y, width, height);
- schedule_repaint(ec);
+ eglDestroySurface(ec->display, src);
}
static void
@@ -662,10 +703,17 @@
struct wl_surface *surface,
int32_t x, int32_t y, int32_t width, int32_t height)
{
+ /* FIXME: This need to take a damage region, of course. */
+}
+
+static uint32_t
+notify_commit(struct wl_compositor *compositor)
+{
struct egl_compositor *ec = (struct egl_compositor *) compositor;
- /* FIXME: This need to take a damage region, of course. */
schedule_repaint(ec);
+
+ return ec->current_frame;
}
static void
@@ -702,6 +750,7 @@
notify_surface_map,
notify_surface_copy,
notify_surface_damage,
+ notify_commit,
notify_pointer_motion,
notify_key
};
@@ -889,6 +938,7 @@
struct screenshooter *shooter;
uint32_t fb_name;
int stride;
+ struct wl_event_loop *loop;
const static EGLint attribs[] =
{ EGL_RENDER_BUFFER, EGL_BACK_BUFFER, EGL_NONE };
@@ -960,6 +1010,19 @@
wl_display_add_object(display, &shooter->base);
wl_display_add_global(display, &shooter->base);
+ ec->timer_fd = timerfd_create(CLOCK_MONOTONIC, 0);
+ if (ec->timer_fd < 0) {
+ fprintf(stderr, "could not create timerfd\n: %m");
+ return NULL;
+ }
+
+ loop = wl_display_get_event_loop(ec->wl_display);
+ ec->timer_source = wl_event_loop_add_fd(loop, ec->timer_fd,
+ WL_EVENT_READABLE,
+ repaint, ec);
+ ec->repaint_needed = 0;
+ ec->repaint_on_timeout = 0;
+
schedule_repaint(ec);
return &ec->base;