neuwld

patched neuwld library (tracked)
git clone https://git.daat.foo/neuwld.git
Log | Files | Refs | README | LICENSE

commit fae91de6c330d9919d97f6705fc7188bdd03fae8
parent a4f8758074baef191fb8be64d9790722a5f0143a
Author: Michael Forney <mforney@mforney.org>
Date:   Thu, 25 Jul 2013 00:52:05 -0700

wayland: Implement damage tracking (including copy and submit)

Diffstat:
MTODO | 1-
Mwayland.c | 76+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Mwayland.h | 33++++++++++++++++++++++++++++++++-
3 files changed, 107 insertions(+), 3 deletions(-)

diff --git a/TODO b/TODO @@ -2,6 +2,5 @@ TODO ==== - Non-monochrome text rendering - Probably requires 3d engine blending for intel backend -- Optional damage tracking and handling for Wayland drawables - Add doxygen documentation diff --git a/wayland.c b/wayland.c @@ -50,8 +50,10 @@ struct wayland_drawable struct wl_buffer * wl; struct wld_drawable * drawable; bool busy; + pixman_region32_t damage; } buffers[2]; uint8_t front_buffer; + uint32_t damage_tracking; }; _Static_assert(offsetof(struct wayland_drawable, base) == 0, @@ -194,7 +196,8 @@ void wld_wayland_destroy_context(struct wld_wayland_context * wayland) struct wld_drawable * wld_wayland_create_drawable (struct wld_wayland_context * context, struct wl_surface * surface, - uint32_t width, uint32_t height, enum wld_format format) + uint32_t width, uint32_t height, enum wld_format format, + uint32_t damage_flags) { struct wayland_drawable * wayland; @@ -218,8 +221,11 @@ struct wld_drawable * wld_wayland_create_drawable wayland->buffers[0].busy = false; wayland->buffers[1].busy = false; + pixman_region32_init(&wayland->buffers[0].damage); + pixman_region32_init(&wayland->buffers[1].damage); wayland->front_buffer = 0; wayland->surface = surface; + wayland->damage_tracking = damage_flags; wayland->base.interface = &wayland_draw; wayland->base.width = width; @@ -240,6 +246,20 @@ struct wld_drawable * wld_wayland_create_drawable return NULL; } +void wld_wayland_drawable_set_damage_tracking(struct wld_drawable * drawable, + uint32_t flags) +{ + struct wayland_drawable * wayland = (void *) drawable; + + if (!wayland->damage_tracking && flags) + { + pixman_region32_clear(&wayland->buffers[0].damage); + pixman_region32_clear(&wayland->buffers[1].damage); + } + + wayland->damage_tracking = flags; +} + int wayland_roundtrip(struct wl_display * display, struct wl_event_queue * queue) { @@ -293,6 +313,16 @@ static void begin(struct wayland_drawable * wayland) { /* Wait for the back buffer to become available. */ wait_for_backbuf(wayland); + + /* Copy across damage from front buffer. */ + if (wayland->damage_tracking & WLD_WAYLAND_DAMAGE_COPY + && pixman_region32_not_empty(&FRONTBUF(wayland).damage)) + { + wld_copy_region(FRONTBUF(wayland).drawable, + BACKBUF(wayland).drawable, + &FRONTBUF(wayland).damage, 0, 0); + pixman_region32_clear(&FRONTBUF(wayland).damage); + } } static void wayland_fill_rectangle(struct wld_drawable * drawable, @@ -303,6 +333,13 @@ static void wayland_fill_rectangle(struct wld_drawable * drawable, begin(wayland); wld_fill_rectangle(BACKBUF(wayland).drawable, color, x, y, width, height); + + if (wayland->damage_tracking) + { + pixman_region32_union_rect(&BACKBUF(wayland).damage, + &BACKBUF(wayland).damage, + x, y, width, height); + } } static void wayland_fill_region(struct wld_drawable * drawable, uint32_t color, @@ -312,6 +349,12 @@ static void wayland_fill_region(struct wld_drawable * drawable, uint32_t color, begin(wayland); wld_fill_region(BACKBUF(wayland).drawable, color, region); + + if (wayland->damage_tracking) + { + pixman_region32_union(&BACKBUF(wayland).damage, + &BACKBUF(wayland).damage, region); + } } static void wayland_copy_rectangle(struct wld_drawable * src_drawable, @@ -337,20 +380,49 @@ static void wayland_draw_text_utf8(struct wld_drawable * drawable, const char * text, int32_t length, struct wld_extents * extents) { + struct wld_extents extents0; struct wayland_drawable * wayland = (void *) drawable; + if (wayland->damage_tracking && !extents) + extents = &extents0; + begin(wayland); wld_draw_text_utf8_n(BACKBUF(wayland).drawable, &font->base, color, x, y, text, length, extents); + + if (wayland->damage_tracking) + { + pixman_region32_union_rect(&BACKBUF(wayland).damage, + &BACKBUF(wayland).damage, + x, y - font->base.ascent, + extents->advance, font->base.height); + } } static void wayland_flush(struct wld_drawable * drawable) { struct wayland_drawable * wayland = (void *) drawable; + pixman_region32_t damage; wait_for_backbuf(wayland); wld_flush(BACKBUF(wayland).drawable); + + if (wayland->damage_tracking & WLD_WAYLAND_DAMAGE_SUBMIT) + { + pixman_box32_t * box; + int num_boxes; + + box = pixman_region32_rectangles(&BACKBUF(wayland).damage, &num_boxes); + + while (num_boxes--) + { + wl_surface_damage(wayland->surface, box->x1, box->y1, + box->x2 - box->x1, box->y2 - box->y1); + ++box; + } + } + wl_surface_attach(wayland->surface, BACKBUF(wayland).wl, 0, 0); wl_surface_commit(wayland->surface); @@ -367,5 +439,7 @@ static void wayland_destroy(struct wld_drawable * drawable) wl_buffer_destroy(wayland->buffers[1].wl); wld_destroy_drawable(wayland->buffers[0].drawable); wld_destroy_drawable(wayland->buffers[1].drawable); + pixman_region32_fini(&wayland->buffers[0].damage); + pixman_region32_fini(&wayland->buffers[1].damage); } diff --git a/wayland.h b/wayland.h @@ -49,6 +49,25 @@ enum wld_wayland_interface_id WLD_SHM }; +enum wld_wayland_damage_flags +{ + /** + * Copy the damaged region from the front buffer to the back buffer after + * swapping buffers. + * + * This allows you to think of the drawable as a single image. + */ + WLD_WAYLAND_DAMAGE_COPY = 1 << 0, + + /** + * Submit the damaged region to the compositor before swapping buffers. + * + * If this is not selected, you must manually damage the surface before + * calling wld_flush. + */ + WLD_WAYLAND_DAMAGE_SUBMIT = 1 << 1 +}; + /** * Create a new drawing context which uses various available Wayland interfaces * (such as wl_shm and wl_drm) to create buffers backed by drawables specific @@ -72,10 +91,22 @@ void wld_wayland_destroy_context(struct wld_wayland_context * context); /** * Create a new Wayland drawable for the given surface with a particular pixel * format. + * + * @param damage_flags Initial damage tracking mode. + * @see enum wld_wayland_damage_tracking_flags */ struct wld_drawable * wld_wayland_create_drawable (struct wld_wayland_context * context, struct wl_surface * surface, - uint32_t width, uint32_t height, enum wld_format format); + uint32_t width, uint32_t height, enum wld_format format, + uint32_t damage_flags); + +/** + * Enable or disable damage tracking on the specified Wayland drawable. + * + * @see enum wld_wayland_damage_tracking_flags + */ +void wld_wayland_drawable_set_damage_tracking(struct wld_drawable * drawable, + uint32_t flags); #endif