Replace commit/ack/frame protocol with simpler sync and frame callbacks

This commit is contained in:
Kristian Høgsberg 2010-09-03 14:46:38 -04:00
parent 13b8ae4986
commit 9d69f8e796
15 changed files with 257 additions and 188 deletions

View file

@ -183,8 +183,8 @@ dnd_draw(struct dnd *dnd)
cairo_destroy(cr);
window_copy_surface(dnd->window, &rectangle, surface);
window_commit(dnd->window, dnd->key);
cairo_surface_destroy(surface);
window_flush(dnd->window);
}
static void

View file

@ -98,21 +98,22 @@ draw_stuff(cairo_surface_t *surface, int width, int height)
}
struct flower {
struct display *display;
struct window *window;
int x, y, width, height;
int offset;
};
static void
handle_frame(struct window *window,
uint32_t frame, uint32_t timestamp, void *data)
frame_callback(void *data, uint32_t time)
{
struct flower *flower = data;
window_move(flower->window,
flower->x + cos((flower->offset + timestamp) / 400.0) * 400 - flower->width / 2,
flower->y + sin((flower->offset + timestamp) / 320.0) * 300 - flower->height / 2);
window_commit(flower->window, 0);
flower->x + cos((flower->offset + time) / 400.0) * 400 - flower->width / 2,
flower->y + sin((flower->offset + time) / 320.0) * 300 - flower->height / 2);
wl_display_frame_callback(display_get_display(flower->display),
frame_callback, flower);
}
int main(int argc, char *argv[])
@ -128,6 +129,7 @@ int main(int argc, char *argv[])
flower.y = 384;
flower.width = 200;
flower.height = 200;
flower.display = d;
flower.window = window_create(d, "flower", flower.x, flower.y,
flower.width, flower.height);
@ -145,10 +147,11 @@ int main(int argc, char *argv[])
draw_stuff(s, flower.width, flower.height);
cairo_surface_flush(s);
window_flush(flower.window);
window_set_user_data(flower.window, &flower);
window_set_frame_handler(flower.window, handle_frame);
window_commit(flower.window, 0);
wl_display_frame_callback(display_get_display(d),
frame_callback, &flower);
display_run(d);

View file

@ -317,31 +317,21 @@ keyboard_focus_handler(struct window *window,
}
static void
acknowledge_handler(struct window *window,
uint32_t key, uint32_t frame,
void *data)
frame_callback(void *data, uint32_t time)
{
struct gears *gears = data;
if (key == 10) {
if (gears->resized)
resize_window(gears);
draw_gears(gears);
}
}
static void
frame_handler(struct window *window,
uint32_t frame, uint32_t timestamp, void *data)
{
struct gears *gears = data;
window_copy_image(gears->window, &gears->rectangle, gears->image);
window_commit(gears->window, 10);
if (gears->resized)
resize_window(gears);
gears->angle = (GLfloat) (timestamp % 8192) * 360 / 8192.0;
draw_gears(gears);
gears->angle = (GLfloat) (time % 8192) * 360 / 8192.0;
wl_display_frame_callback(display_get_display(gears->d),
frame_callback, gears);
}
static struct gears *
@ -417,13 +407,13 @@ gears_create(struct display *display)
resize_window(gears);
draw_gears(gears);
frame_handler(gears->window, 0, 0, gears);
window_set_user_data(gears->window, gears);
window_set_resize_handler(gears->window, resize_handler);
window_set_keyboard_focus_handler(gears->window, keyboard_focus_handler);
window_set_acknowledge_handler(gears->window, acknowledge_handler);
window_set_frame_handler(gears->window, frame_handler);
wl_display_frame_callback(display_get_display(gears->d),
frame_callback, gears);
return gears;
}

View file

@ -176,7 +176,7 @@ image_draw(struct image *image)
g_object_unref(pb);
window_copy_surface(image->window, &rectangle, surface);
window_commit(image->window, image->key);
window_flush(image->window);
cairo_surface_destroy(surface);
}

View file

@ -215,7 +215,7 @@ terminal_draw(struct terminal *terminal)
window_draw(terminal->window);
terminal_draw_contents(terminal);
window_commit(terminal->window, 0);
window_flush(terminal->window);
}
static void

View file

@ -101,8 +101,7 @@ view_draw(struct view *view)
poppler_page_render(page, cr);
cairo_destroy(cr);
g_object_unref(G_OBJECT(page));
window_commit(view->window, 0);
window_flush(view->window);
}
static void

View file

@ -98,8 +98,6 @@ struct window {
window_key_handler_t key_handler;
window_button_handler_t button_handler;
window_keyboard_focus_handler_t keyboard_focus_handler;
window_acknowledge_handler_t acknowledge_handler;
window_frame_handler_t frame_handler;
window_motion_handler_t motion_handler;
void *user_data;
@ -331,6 +329,21 @@ display_get_pointer_surface(struct display *display, int pointer,
return cairo_surface_reference(surface);
}
static void
window_attach_surface(struct window *window);
static void
free_surface(void *data)
{
struct window *window = data;
cairo_surface_destroy(window->pending_surface);
window->pending_surface = NULL;
if (window->cairo_surface)
window_attach_surface(window);
}
static void
window_attach_surface(struct window *window)
{
@ -353,17 +366,14 @@ window_attach_surface(struct window *window)
window->allocation.width,
window->allocation.height);
wl_compositor_commit(window->display->compositor, 0);
wl_display_sync_callback(display->display, free_surface, window);
}
void
window_commit(struct window *window, uint32_t key)
window_flush(struct window *window)
{
if (window->cairo_surface) {
window_attach_surface(window);
} else {
wl_compositor_commit(window->display->compositor, key);
}
if (window->cairo_surface)
window_attach_surface(window);
}
static void
@ -848,6 +858,10 @@ window_copy_surface(struct window *window,
cairo_paint (cr);
cairo_destroy (cr);
wl_surface_damage(window->surface,
rectangle->x, rectangle->y,
rectangle->width, rectangle->height);
}
static gboolean
@ -933,20 +947,6 @@ window_set_button_handler(struct window *window,
window->button_handler = handler;
}
void
window_set_acknowledge_handler(struct window *window,
window_acknowledge_handler_t handler)
{
window->acknowledge_handler = handler;
}
void
window_set_frame_handler(struct window *window,
window_frame_handler_t handler)
{
window->frame_handler = handler;
}
void
window_set_motion_handler(struct window *window,
window_motion_handler_t handler)
@ -1022,48 +1022,6 @@ static const struct wl_drm_listener drm_listener = {
drm_handle_authenticated
};
static void
display_handle_acknowledge(void *data,
struct wl_compositor *compositor,
uint32_t key, uint32_t frame)
{
struct display *d = data;
struct window *window;
/* The acknowledge event means that the server processed our
* last commit request and we can now safely free the old
* window buffer if we resized and render the next frame into
* our back buffer.. */
wl_list_for_each(window, &d->window_list, link) {
cairo_surface_destroy(window->pending_surface);
window->pending_surface = NULL;
if (window->cairo_surface)
window_attach_surface(window);
if (window->acknowledge_handler)
(*window->acknowledge_handler)(window, key, frame, window->user_data);
}
}
static void
display_handle_frame(void *data,
struct wl_compositor *compositor,
uint32_t frame, uint32_t timestamp)
{
struct display *d = data;
struct window *window;
wl_list_for_each(window, &d->window_list, link) {
if (window->frame_handler)
(*window->frame_handler)(window, frame,
timestamp, window->user_data);
}
}
static const struct wl_compositor_listener compositor_listener = {
display_handle_acknowledge,
display_handle_frame,
};
static void
display_handle_geometry(void *data,
struct wl_output *output,
@ -1111,8 +1069,6 @@ display_handle_global(struct wl_display *display, uint32_t id,
if (strcmp(interface, "compositor") == 0) {
d->compositor = wl_compositor_create(display, id);
wl_compositor_add_listener(d->compositor,
&compositor_listener, d);
} else if (strcmp(interface, "output") == 0) {
d->output = wl_output_create(display, id);
wl_output_add_listener(d->output, &output_listener, d);
@ -1290,6 +1246,12 @@ display_create(int *argc, char **argv[], const GOptionEntry *option_entries)
return d;
}
struct wl_display *
display_get_display(struct display *display)
{
return display->display;
}
struct wl_compositor *
display_get_compositor(struct display *display)
{

View file

@ -38,6 +38,9 @@ struct input;
struct display *
display_create(int *argc, char **argv[], const GOptionEntry *option_entries);
struct wl_display *
display_get_display(struct display *display);
struct wl_compositor *
display_get_compositor(struct display *display);
@ -96,7 +99,6 @@ enum pointer_type {
typedef void (*window_resize_handler_t)(struct window *window, void *data);
typedef void (*window_redraw_handler_t)(struct window *window, void *data);
typedef void (*window_frame_handler_t)(struct window *window, uint32_t frame, uint32_t timestamp, void *data);
typedef void (*window_acknowledge_handler_t)(struct window *window, uint32_t key, uint32_t frame, void *data);
typedef void (*window_key_handler_t)(struct window *window, uint32_t key, uint32_t unicode,
uint32_t state, uint32_t modifiers, void *data);
typedef void (*window_keyboard_focus_handler_t)(struct window *window,
@ -121,8 +123,6 @@ window_create(struct display *display, const char *title,
void
window_draw(struct window *window);
void
window_commit(struct window *window, uint32_t key);
void
window_get_child_rectangle(struct window *window,
struct rectangle *rectangle);
void
@ -146,6 +146,9 @@ window_copy_surface(struct window *window,
struct rectangle *rectangle,
cairo_surface_t *surface);
void
window_flush(struct window *window);
void
window_set_fullscreen(struct window *window, int fullscreen);
@ -168,9 +171,6 @@ window_set_resize_handler(struct window *window,
void
window_set_frame_handler(struct window *window,
window_frame_handler_t handler);
void
window_set_acknowledge_handler(struct window *window,
window_acknowledge_handler_t handler);
void
window_set_key_handler(struct window *window,
@ -188,10 +188,6 @@ void
window_set_keyboard_focus_handler(struct window *window,
window_keyboard_focus_handler_t handler);
void
window_set_acknowledge_handler(struct window *window,
window_acknowledge_handler_t handler);
void
window_set_frame_handler(struct window *window,
window_frame_handler_t handler);