<p dir="ltr"><br>
On Apr 12, 2014 4:25 AM, "Pekka Paalanen" <<a href="mailto:ppaalanen@gmail.com">ppaalanen@gmail.com</a>> wrote:<br>
><br>
> On Fri, 11 Apr 2014 05:01:17 -0500<br>
> Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>> wrote:<br>
><br>
> > Signed-off-by: Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>><br>
> > ---<br>
> >  Makefile.am             |  12 +<br>
> >  clients/simple-damage.c | 869 ++++++++++++++++++++++++++++++++++++++++++++++++<br>
> >  2 files changed, 881 insertions(+)<br>
> >  create mode 100644 clients/simple-damage.c<br>
> ><br>
> > diff --git a/Makefile.am b/Makefile.am<br>
> > index c2df8b6..eb05a47 100644<br>
> > --- a/Makefile.am<br>
> > +++ b/Makefile.am<br>
> > @@ -386,6 +386,7 @@ endif<br>
> >  if BUILD_SIMPLE_CLIENTS<br>
> >  demo_clients +=                                      \<br>
> >       weston-simple-shm                       \<br>
> > +     weston-simple-damage                    \<br>
> >       weston-simple-touch                     \<br>
> >       weston-multi-resource<br>
> ><br>
> > @@ -398,6 +399,17 @@ nodist_weston_simple_shm_SOURCES =               \<br>
> >  weston_simple_shm_CFLAGS = $(AM_CFLAGS) $(SIMPLE_CLIENT_CFLAGS)<br>
> >  weston_simple_shm_LDADD = $(SIMPLE_CLIENT_LIBS) <a href="http://libshared.la">libshared.la</a><br>
> ><br>
> > +weston_simple_damage_SOURCES = clients/simple-damage.c<br>
> > +nodist_weston_simple_damage_SOURCES =                \<br>
> > +     protocol/scaler-protocol.c              \<br>
> > +     protocol/scaler-client-protocol.h       \<br>
> > +     protocol/xdg-shell-protocol.c           \<br>
> > +     protocol/xdg-shell-client-protocol.h    \<br>
> > +     protocol/fullscreen-shell-protocol.c    \<br>
> > +     protocol/fullscreen-shell-client-protocol.h<br>
> > +weston_simple_damage_CFLAGS = $(AM_CFLAGS) $(SIMPLE_CLIENT_CFLAGS)<br>
> > +weston_simple_damage_LDADD = $(SIMPLE_CLIENT_LIBS) <a href="http://libshared.la">libshared.la</a><br>
> > +<br>
> >  weston_simple_touch_SOURCES = clients/simple-touch.c<br>
> >  weston_simple_touch_CFLAGS = $(AM_CFLAGS) $(SIMPLE_CLIENT_CFLAGS)<br>
> >  weston_simple_touch_LDADD = $(SIMPLE_CLIENT_LIBS) <a href="http://libshared.la">libshared.la</a><br>
> > diff --git a/clients/simple-damage.c b/clients/simple-damage.c<br>
> > new file mode 100644<br>
> > index 0000000..35f3a6f<br>
> > --- /dev/null<br>
> > +++ b/clients/simple-damage.c<br>
> > @@ -0,0 +1,869 @@<br>
> > +/*<br>
> > + * Copyright © 2014 Jason Ekstrand<br>
> > + * Copyright © 2011 Benjamin Franzke<br>
> > + * Copyright © 2010 Intel Corporation<br>
> > + *<br>
> > + * Permission to use, copy, modify, distribute, and sell this software and its<br>
> > + * documentation for any purpose is hereby granted without fee, provided that<br>
> > + * the above copyright notice appear in all copies and that both that copyright<br>
> > + * notice and this permission notice appear in supporting documentation, and<br>
> > + * that the name of the copyright holders not be used in advertising or<br>
> > + * publicity pertaining to distribution of the software without specific,<br>
> > + * written prior permission.  The copyright holders make no representations<br>
> > + * about the suitability of this software for any purpose.  It is provided "as<br>
> > + * is" without express or implied warranty.<br>
> > + *<br>
> > + * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,<br>
> > + * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO<br>
> > + * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR<br>
> > + * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,<br>
> > + * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER<br>
> > + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE<br>
> > + * OF THIS SOFTWARE.<br>
> > + */<br>
> > +<br>
> > +#include <config.h><br>
> > +<br>
> > +#include <stdio.h><br>
> > +#include <stdlib.h><br>
> > +#include <string.h><br>
> > +#include <stdbool.h><br>
> > +#include <assert.h><br>
> > +#include <unistd.h><br>
> > +#include <sys/mman.h><br>
> > +#include <sys/time.h><br>
> > +#include <signal.h><br>
> > +<br>
> > +#include <wayland-client.h><br>
> > +#include "../shared/os-compatibility.h"<br>
> > +#include "xdg-shell-client-protocol.h"<br>
> > +#include "fullscreen-shell-client-protocol.h"<br>
> > +#include "scaler-client-protocol.h"<br>
> > +<br>
> > +int print_debug = 0;<br>
> > +<br>
> > +struct display {<br>
> > +     struct wl_display *display;<br>
> > +     struct wl_registry *registry;<br>
> > +     int target_version;<br>
> > +     struct wl_compositor *compositor;<br>
> > +     struct wl_scaler *scaler;<br>
> > +     struct xdg_shell *shell;<br>
> > +     struct _wl_fullscreen_shell *fshell;<br>
> > +     struct wl_shm *shm;<br>
> > +     uint32_t formats;<br>
> > +};<br>
> > +<br>
> > +struct buffer {<br>
> > +     struct wl_buffer *buffer;<br>
> > +     uint32_t *shm_data;<br>
> > +     int busy;<br>
> > +};<br>
> > +<br>
> > +struct window {<br>
> > +     struct display *display;<br>
> > +     int width, height, border;<br>
> > +     struct wl_surface *surface;<br>
> > +     struct wl_viewport *viewport;<br>
> > +     struct xdg_surface *xdg_surface;<br>
> > +     struct wl_callback *callback;<br>
> > +     struct buffer buffers[2];<br>
> > +     struct buffer *prev_buffer;<br>
> > +<br>
> > +     int scale;<br>
> > +     enum wl_output_transform transform;<br>
> > +<br>
> > +     struct {<br>
> > +             float x, y; /* position in pixels */<br>
> > +             float dx, dy; /* velocity in pixels/second */<br>
> > +             int radius; /* radius in pixels */<br>
> > +             uint32_t prev_time;<br>
> > +     } ball;<br>
> > +};<br>
> > +<br>
> > +static int running = 1;<br>
> > +<br>
> > +static void<br>
> > +buffer_release(void *data, struct wl_buffer *buffer)<br>
> > +{<br>
> > +     struct buffer *mybuf = data;<br>
> > +<br>
> > +     mybuf->busy = 0;<br>
> > +}<br>
> > +<br>
> > +static const struct wl_buffer_listener buffer_listener = {<br>
> > +     buffer_release<br>
> > +};<br>
> > +<br>
> > +static int<br>
> > +create_shm_buffer(struct display *display, struct buffer *buffer,<br>
> > +               int width, int height, uint32_t format)<br>
> > +{<br>
> > +     struct wl_shm_pool *pool;<br>
> > +     int fd, size, stride;<br>
> > +     void *data;<br>
> > +<br>
> > +     stride = width * 4;<br>
> > +     size = stride * height;<br>
> > +<br>
> > +     fd = os_create_anonymous_file(size);<br>
> > +     if (fd < 0) {<br>
> > +             fprintf(stderr, "creating a buffer file for %d B failed: %m\n",<br>
> > +                     size);<br>
> > +             return -1;<br>
> > +     }<br>
> > +<br>
> > +     data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);<br>
> > +     if (data == MAP_FAILED) {<br>
> > +             fprintf(stderr, "mmap failed: %m\n");<br>
> > +             close(fd);<br>
> > +             return -1;<br>
> > +     }<br>
> > +<br>
> > +     pool = wl_shm_create_pool(display->shm, fd, size);<br>
> > +     buffer->buffer = wl_shm_pool_create_buffer(pool, 0,<br>
> > +                                                width, height,<br>
> > +                                                stride, format);<br>
> > +     wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);<br>
> > +     wl_shm_pool_destroy(pool);<br>
> > +     close(fd);<br>
> > +<br>
> > +     buffer->shm_data = data;<br>
> > +<br>
> > +     return 0;<br>
> > +}<br>
> > +<br>
> > +static void<br>
> > +handle_configure(void *data, struct xdg_surface *surface,<br>
> > +              int32_t width, int32_t height)<br>
> > +{<br>
> > +}<br>
> > +<br>
> > +static void<br>
> > +handle_change_state(void *data, struct xdg_surface *xdg_surface,<br>
> > +                 uint32_t state,<br>
> > +                 uint32_t value,<br>
> > +                 uint32_t serial)<br>
> > +{<br>
> > +}<br>
> > +<br>
> > +static void<br>
> > +handle_activated(void *data, struct xdg_surface *xdg_surface)<br>
> > +{<br>
> > +}<br>
> > +<br>
> > +static void<br>
> > +handle_deactivated(void *data, struct xdg_surface *xdg_surface)<br>
> > +{<br>
> > +}<br>
> > +<br>
> > +static void<br>
> > +handle_delete(void *data, struct xdg_surface *xdg_surface)<br>
> > +{<br>
> > +     running = 0;<br>
> > +}<br>
> > +<br>
> > +static const struct xdg_surface_listener xdg_surface_listener = {<br>
> > +     handle_configure,<br>
> > +     handle_change_state,<br>
> > +     handle_activated,<br>
> > +     handle_deactivated,<br>
> > +     handle_delete,<br>
> > +};<br>
> > +<br>
> > +static float<br>
> > +bounded_randf(float a, float b)<br>
> > +{<br>
> > +     return a + ((float)rand() / (float)RAND_MAX) * (b - a);<br>
> > +}<br>
> > +<br>
> > +static void<br>
> > +window_init_game(struct window *window)<br>
> > +{<br>
> > +     int ax1, ay1, ax2, ay2; /* playable arena size */<br>
> > +     struct timeval tv;<br>
> > +<br>
> > +     gettimeofday(&tv, NULL);<br>
> > +     srand(tv.tv_usec);<br>
> > +<br>
> > +     ax1 = window->border + window->ball.radius;<br>
> > +     ay1 = window->border + window->ball.radius;<br>
> > +     ax2 = window->width - window->border - window->ball.radius;<br>
> > +     ay2 = window->height - window->border - window->ball.radius;<br>
> > +<br>
> > +     window->ball.radius = 10;<br>
> > +     window->ball.x = bounded_randf(ax1, ax2);<br>
> > +     window->ball.y = bounded_randf(ay1, ay2);<br>
> > +<br>
> > +     window->ball.dx = bounded_randf(0, window->width);<br>
> > +     window->ball.dy = bounded_randf(0, window->height);<br>
> > +<br>
> > +     window->ball.prev_time = 0;<br>
> > +}<br>
> > +<br>
> > +static void<br>
> > +window_advance_game(struct window *window, uint32_t timestamp)<br>
> > +{<br>
> > +     int ax1, ay1, ax2, ay2; /* Arena size */<br>
> > +     float dt;<br>
> > +<br>
> > +     if (window->ball.prev_time == 0) {<br>
> > +             /* first pass, don't do anything */<br>
> > +             window->ball.prev_time = timestamp;<br>
> > +             return;<br>
> > +     }<br>
> > +<br>
> > +     /* dt in seconds */<br>
> > +     dt = (float)(timestamp - window->ball.prev_time) / 1000.0f;<br>
> > +<br>
> > +     ax1 = window->border + window->ball.radius;<br>
> > +     ay1 = window->border + window->ball.radius;<br>
> > +     ax2 = window->width - window->border - window->ball.radius;<br>
> > +     ay2 = window->height - window->border - window->ball.radius;<br>
> > +<br>
> > +     window->ball.x += window->ball.dx * dt;<br>
> > +     while (window->ball.x < ax1 || ax2 < window->ball.x) {<br>
> > +             if (window->ball.x < ax1)<br>
> > +                     window->ball.x = 2 * ax1 - window->ball.x;<br>
> > +             if (ax2 <= window->ball.x)<br>
> > +                     window->ball.x = 2 * ax2 - window->ball.x;<br>
> > +<br>
> > +             window->ball.dx *= -1.0f;<br>
> > +     }<br>
> > +<br>
> > +     window->ball.y += window->ball.dy * dt;<br>
> > +     while (window->ball.y < ay1 || ay2 < window->ball.y) {<br>
> > +             if (window->ball.y < ay1)<br>
> > +                     window->ball.y = 2 * ay1 - window->ball.y;<br>
> > +             if (ay2 <= window->ball.y)<br>
> > +                     window->ball.y = 2 * ay2 - window->ball.y;<br>
> > +<br>
> > +             window->ball.dy *= -1.0f;<br>
> > +     }<br>
> > +<br>
> > +     window->ball.prev_time = timestamp;<br>
> > +}<br>
> > +<br>
> > +static struct window *<br>
> > +create_window(struct display *display, int width, int height,<br>
> > +           enum wl_output_transform transform, int scale, int use_viewport)<br>
> > +{<br>
> > +     struct window *window;<br>
> > +<br>
> > +     if (wl_compositor_get_version(display->compositor) < 2 &&<br>
> > +         transform != WL_OUTPUT_TRANSFORM_NORMAL) {<br>
> > +             fprintf(stderr, "wl_surface.buffer_transform unsupported in "<br>
> > +                             "wl_surface version %d\n",<br>
> > +                     wl_compositor_get_version(display->compositor));<br>
> > +             exit(1);<br>
> > +     }<br>
> > +<br>
> > +     if (wl_compositor_get_version(display->compositor) < 3 &&<br>
> > +         (! use_viewport) && scale != 1) {<br>
> > +             fprintf(stderr, "wl_surface.buffer_scale unsupported in "<br>
> > +                             "wl_surface version %d\n",<br>
> > +                     wl_compositor_get_version(display->compositor));<br>
> > +             exit(1);<br>
> > +     }<br>
> > +<br>
> > +     if (display->scaler == NULL && use_viewport) {<br>
> > +             fprintf(stderr, "Compositor does not support wl_viewport");<br>
> > +             exit(1);<br>
> > +     }<br>
> > +<br>
> > +     window = calloc(1, sizeof *window);<br>
> > +     if (!window)<br>
> > +             return NULL;<br>
> > +<br>
> > +     window->callback = NULL;<br>
> > +     window->display = display;<br>
> > +     window->width = width;<br>
> > +     window->height = height;<br>
> > +     window->border = 10;<br>
> > +     window->transform = transform;<br>
> > +     window->scale = scale;<br>
> > +<br>
> > +     window_init_game(window);<br>
> > +<br>
> > +     window->surface = wl_compositor_create_surface(display->compositor);<br>
> > +<br>
> > +     if (use_viewport)<br>
> > +             window->viewport = wl_scaler_get_viewport(display->scaler,<br>
> > +                                                       window->surface);<br>
> > +<br>
> > +     if (display->shell) {<br>
> > +             window->xdg_surface =<br>
> > +                     xdg_shell_get_xdg_surface(display->shell,<br>
> > +                                               window->surface);<br>
> > +<br>
> > +             assert(window->xdg_surface);<br>
> > +<br>
> > +             xdg_surface_add_listener(window->xdg_surface,<br>
> > +                                      &xdg_surface_listener, window);<br>
> > +<br>
> > +             xdg_surface_set_title(window->xdg_surface, "simple-damage");<br>
> > +     } else if (display->fshell) {<br>
> > +             _wl_fullscreen_shell_present_surface(display->fshell,<br>
> > +                                                  window->surface,<br>
> > +                                                  _WL_FULLSCREEN_SHELL_PRESENT_METHOD_DEFAULT,<br>
> > +                                                  NULL);<br>
> > +     } else {<br>
> > +             assert(0);<br>
> > +     }<br>
> > +<br>
> > +     /* Initialise damage to full surface, so the padding gets painted */<br>
> > +     wl_surface_damage(window->surface, 0, 0, INT32_MAX, INT32_MAX);<br>
> > +<br>
> > +     return window;<br>
> > +}<br>
> > +<br>
> > +static void<br>
> > +destroy_window(struct window *window)<br>
> > +{<br>
> > +     if (window->callback)<br>
> > +             wl_callback_destroy(window->callback);<br>
> > +<br>
> > +     if (window->buffers[0].buffer)<br>
> > +             wl_buffer_destroy(window->buffers[0].buffer);<br>
> > +     if (window->buffers[1].buffer)<br>
> > +             wl_buffer_destroy(window->buffers[1].buffer);<br>
> > +<br>
> > +     if (window->xdg_surface)<br>
> > +             xdg_surface_destroy(window->xdg_surface);<br>
> > +     if (window->viewport)<br>
> > +             wl_viewport_destroy(window->viewport);<br>
> > +     wl_surface_destroy(window->surface);<br>
> > +     free(window);<br>
> > +}<br>
> > +<br>
> > +static struct buffer *<br>
> > +window_next_buffer(struct window *window)<br>
> > +{<br>
> > +     struct buffer *buffer;<br>
> > +     int ret = 0, bwidth, bheight;<br>
> > +<br>
> > +     if (!window->buffers[0].busy)<br>
> > +             buffer = &window->buffers[0];<br>
> > +     else if (!window->buffers[1].busy)<br>
> > +             buffer = &window->buffers[1];<br>
> > +     else<br>
> > +             return NULL;<br>
> > +<br>
> > +     switch (window->transform) {<br>
> > +     case WL_OUTPUT_TRANSFORM_NORMAL:<br>
> > +     case WL_OUTPUT_TRANSFORM_180:<br>
> > +     case WL_OUTPUT_TRANSFORM_FLIPPED:<br>
> > +     case WL_OUTPUT_TRANSFORM_FLIPPED_180:<br>
> > +             bwidth = window->width * window->scale;<br>
> > +             bheight = window->height * window->scale;<br>
> > +             break;<br>
> > +     case WL_OUTPUT_TRANSFORM_90:<br>
> > +     case WL_OUTPUT_TRANSFORM_270:<br>
> > +     case WL_OUTPUT_TRANSFORM_FLIPPED_90:<br>
> > +     case WL_OUTPUT_TRANSFORM_FLIPPED_270:<br>
> > +             bwidth = window->height * window->scale;<br>
> > +             bheight = window->width * window->scale;<br>
> > +             break;<br>
> > +     }<br>
> > +<br>
> > +     /* If we're using a viewport, we make it double-size and crop */<br>
> > +     if (window->viewport) {<br>
> > +             bwidth *= 2;<br>
> > +             bheight *= 2;<br>
> > +     }<br>
> > +<br>
> > +     if (!buffer->buffer) {<br>
> > +             ret = create_shm_buffer(window->display, buffer,<br>
> > +                                     bwidth, bheight,<br>
> > +                                     WL_SHM_FORMAT_ARGB8888);<br>
> > +<br>
> > +             if (ret < 0)<br>
> > +                     return NULL;<br>
> > +     }<br>
> > +<br>
> > +     return buffer;<br>
> > +}<br>
> > +<br>
> > +static void<br>
> > +paint_box(uint32_t *pixels, int stride, int x, int y, int width, int height,<br>
> > +       uint32_t color)<br>
> > +{<br>
> > +     int i, j;<br>
> > +<br>
> > +     for (j = y; j < y + height; ++j)<br>
> > +             for (i = x; i < x + width; ++i)<br>
> > +                     pixels[i + j * stride] = color;<br>
> > +}<br>
> > +<br>
> > +static void<br>
> > +paint_circle(uint32_t *pixels, int stride, float x, float y, int radius,<br>
> > +          uint32_t color)<br>
> > +{<br>
> > +     int i, j;<br>
> > +<br>
> > +     for (j = y - radius; j <= (int)(y + radius); ++j)<br>
> > +             for (i = x - radius; i <= (int)(x + radius); ++i)<br>
> > +                     if ((j+0.5f-y)*(j+0.5f-y) + (i+0.5f-x)*(i+0.5f-x) <= radius * radius)<br>
> > +                             pixels[i + j * stride] = color;<br>
> > +}<br>
> > +<br>
> > +static void<br>
> > +window_get_transformed_ball(struct window *window, float *bx, float *by)<br>
> > +{<br>
> > +     float wx, wy;<br>
> > +<br>
> > +     wx = window->ball.x;<br>
> > +     wy = window->ball.y;<br>
> > +<br>
> > +     switch (window->transform) {<br>
> > +     case WL_OUTPUT_TRANSFORM_NORMAL:<br>
> > +             *bx = wx;<br>
> > +             *by = wy;<br>
> > +             break;<br>
> > +     case WL_OUTPUT_TRANSFORM_90:<br>
> > +             *bx = window->height - wy;<br>
> > +             *by = wx;<br>
> > +             break;<br>
> > +     case WL_OUTPUT_TRANSFORM_180:<br>
> > +             *bx = window->width - wx;<br>
> > +             *by = window->height - wy;<br>
> > +             break;<br>
> > +     case WL_OUTPUT_TRANSFORM_270:<br>
> > +             *bx = wy;<br>
> > +             *by = window->width - wx;<br>
> > +             break;<br>
> > +     case WL_OUTPUT_TRANSFORM_FLIPPED:<br>
> > +             *bx = window->width - wx;<br>
> > +             *by = wy;<br>
> > +             break;<br>
> > +     case WL_OUTPUT_TRANSFORM_FLIPPED_90:<br>
> > +             *bx = window->height - wy;<br>
> > +             *by = window->width - wx;<br>
> > +             break;<br>
> > +     case WL_OUTPUT_TRANSFORM_FLIPPED_180:<br>
> > +             *bx = wx;<br>
> > +             *by = window->height - wy;<br>
> > +             break;<br>
> > +     case WL_OUTPUT_TRANSFORM_FLIPPED_270:<br>
> > +             *bx = wy;<br>
> > +             *by = wx;<br>
> > +             break;<br>
> > +     }<br>
> > +<br>
> > +     *bx *= window->scale;<br>
> > +     *by *= window->scale;<br>
> > +}<br>
> > +<br>
> > +static const struct wl_callback_listener frame_listener;<br>
> > +<br>
> > +static void<br>
> > +redraw(void *data, struct wl_callback *callback, uint32_t time)<br>
> > +{<br>
> > +     struct window *window = data;<br>
> > +     struct buffer *buffer;<br>
> > +     int bwidth, bheight, bborder, bstride;<br>
> > +     uint32_t *buffer_data;<br>
> > +     float bx, by;<br>
> > +<br>
> > +     buffer = window_next_buffer(window);<br>
> > +     if (!buffer) {<br>
> > +             fprintf(stderr,<br>
> > +                     !callback ? "Failed to create the first buffer.\n" :<br>
> > +                     "Both buffers busy at redraw(). Server bug?\n");<br>
> > +             abort();<br>
> > +     }<br>
> > +<br>
> > +     switch (window->transform) {<br>
> > +     case WL_OUTPUT_TRANSFORM_NORMAL:<br>
> > +     case WL_OUTPUT_TRANSFORM_180:<br>
> > +     case WL_OUTPUT_TRANSFORM_FLIPPED:<br>
> > +     case WL_OUTPUT_TRANSFORM_FLIPPED_180:<br>
> > +             bwidth = window->width * window->scale;<br>
> > +             bheight = window->height * window->scale;<br>
> > +             break;<br>
> > +     case WL_OUTPUT_TRANSFORM_90:<br>
> > +     case WL_OUTPUT_TRANSFORM_270:<br>
> > +     case WL_OUTPUT_TRANSFORM_FLIPPED_90:<br>
> > +     case WL_OUTPUT_TRANSFORM_FLIPPED_270:<br>
> > +             bwidth = window->height * window->scale;<br>
> > +             bheight = window->width * window->scale;<br>
> > +             break;<br>
> > +     }<br>
> > +<br>
> > +     if (window->viewport) {<br>
> > +             /* The buffer is actually twice as big.  We'll crop it down<br>
> > +              * to size */<br>
> > +             bstride = bwidth * 2;<br>
> > +             buffer_data = buffer->shm_data;<br>
> > +<br>
> > +             switch (window->transform) {<br>
> > +             case WL_OUTPUT_TRANSFORM_NORMAL:<br>
> > +             case WL_OUTPUT_TRANSFORM_FLIPPED_270:<br>
> > +                     buffer_data += (bheight / 3) * bstride +<br>
> > +                                    (bwidth / 3);<br>
> > +                     break;<br>
> > +             case WL_OUTPUT_TRANSFORM_90:<br>
> > +             case WL_OUTPUT_TRANSFORM_FLIPPED:<br>
> > +                     buffer_data += (bheight / 3) * bstride +<br>
> > +                                    (bwidth - bwidth / 3);<br>
> > +                     break;<br>
> > +             case WL_OUTPUT_TRANSFORM_180:<br>
> > +             case WL_OUTPUT_TRANSFORM_FLIPPED_90:<br>
> > +                     buffer_data += (bheight - bheight / 3) * bstride +<br>
> > +                                    (bwidth - bwidth / 3);<br>
> > +                     break;<br>
> > +             case WL_OUTPUT_TRANSFORM_270:<br>
> > +             case WL_OUTPUT_TRANSFORM_FLIPPED_180:<br>
> > +                     buffer_data += (bheight - bheight / 3) * bstride +<br>
> > +                                    (bwidth / 3);<br>
> > +                     break;<br>
> > +             }<br>
> > +<br>
> > +             wl_viewport_set_source(window->viewport,<br>
> > +                                    wl_fixed_from_int(window->width * window->scale / 3),<br>
> > +                                    wl_fixed_from_int(window->height * window->scale / 3),<br>
> > +                                    wl_fixed_from_int(window->width * window->scale),<br>
> > +                                    wl_fixed_from_int(window->height * window->scale));<br>
> > +<br>
> > +             /* Fill the whole thing with red to detect viewport errors */<br>
> > +             paint_box(buffer->shm_data, bstride,<br>
> > +                       0, 0, bwidth * 2, bheight * 2, 0xffff0000);<br>
> > +     } else {<br>
> > +             bstride = bwidth;<br>
> > +             buffer_data = buffer->shm_data;<br>
> > +     }<br>
> > +<br>
> > +     bborder = window->border * window->scale;<br>
> > +<br>
> > +     /* Paint the border */<br>
> > +     paint_box(buffer_data, bstride, 0, 0, bwidth, bborder, 0xffffffff);<br>
> > +     paint_box(buffer_data, bstride, 0, 0, bborder, bheight, 0xffffffff);<br>
> > +     paint_box(buffer_data, bstride,<br>
> > +               bwidth - bborder, 0, bborder, bheight, 0xffffffff);<br>
> > +     paint_box(buffer_data, bstride,<br>
> > +               0, bheight - bborder, bwidth, bborder, 0xffffffff);<br>
> > +<br>
> > +     /* fill with translucent */<br>
> > +     paint_box(buffer_data, bstride, bborder, bborder,<br>
> > +               bwidth - 2 * bborder, bheight - 2 * bborder, 0x80000000);<br>
> > +<br>
> > +     /* Damage where the ball was */<br>
> > +     wl_surface_damage(window->surface,<br>
> > +                       window->ball.x - window->ball.radius,<br>
> > +                       window->ball.y - window->ball.radius,<br>
> > +                       window->ball.radius * 2 + 1,<br>
> > +                       window->ball.radius * 2 + 1);<br>
> > +<br>
> > +     window_advance_game(window, time);<br>
> > +<br>
> > +     window_get_transformed_ball(window, &bx, &by);<br>
> > +<br>
> > +     /* Paint the ball */<br>
> > +     paint_circle(buffer_data, bstride, bx, by,<br>
> > +                  window->ball.radius * window->scale, 0xff00ff00);<br>
> > +<br>
> > +     if (print_debug) {<br>
> > +             printf("Ball now located at (%f, %f)\n",<br>
> > +                    window->ball.x, window->ball.y);<br>
> > +<br>
> > +             printf("Circle painted at (%f, %f), radius %d\n", bx, by,<br>
> > +                    window->ball.radius * window->scale);<br>
> > +<br>
> > +             printf("Buffer damage rectangle: (%d, %d) @ %dx%d\n",<br>
> > +                    (int)(bx - window->ball.radius * window->scale),<br>
> > +                    (int)(by - window->ball.radius * window->scale),<br>
> > +                    window->ball.radius * window->scale * 2 + 1,<br>
> > +                    window->ball.radius * window->scale * 2 + 1);<br>
> > +     }<br>
> > +<br>
> > +     /* Damage where the ball is now */<br>
> > +     wl_surface_damage(window->surface,<br>
> > +                       window->ball.x - window->ball.radius,<br>
> > +                       window->ball.y - window->ball.radius,<br>
> > +                       window->ball.radius * 2 + 1,<br>
> > +                       window->ball.radius * 2 + 1);<br>
> > +<br>
> > +     wl_surface_attach(window->surface, buffer->buffer, 0, 0);<br>
> > +<br>
> > +     if (wl_surface_get_version(window->surface) >= 2 &&<br>
> > +         window->transform != WL_OUTPUT_TRANSFORM_NORMAL)<br>
> > +             wl_surface_set_buffer_transform(window->surface,<br>
> > +                                             window->transform);<br>
> > +<br>
> > +     if (window->scale != 1) {<br>
> > +             if (window->viewport) {<br>
> > +                     wl_viewport_set_destination(window->viewport,<br>
> > +                                                 window->width,<br>
> > +                                                 window->height);<br>
> > +             } else {<br>
> > +                     wl_surface_set_buffer_scale(window->surface,<br>
> > +                                                 window->scale);<br>
> > +             }<br>
> > +     }<br>
> > +<br>
> > +     if (callback)<br>
> > +             wl_callback_destroy(callback);<br>
> > +<br>
> > +     window->callback = wl_surface_frame(window->surface);<br>
> > +     wl_callback_add_listener(window->callback, &frame_listener, window);<br>
> > +     wl_surface_commit(window->surface);<br>
> > +     buffer->busy = 1;<br>
> > +}<br>
> > +<br>
> > +static const struct wl_callback_listener frame_listener = {<br>
> > +     redraw<br>
> > +};<br>
> > +<br>
> > +static void<br>
> > +shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)<br>
> > +{<br>
> > +     struct display *d = data;<br>
> > +<br>
> > +     d->formats |= (1 << format);<br>
> > +}<br>
> > +<br>
> > +struct wl_shm_listener shm_listener = {<br>
> > +     shm_format<br>
> > +};<br>
> > +<br>
> > +static void<br>
> > +xdg_shell_ping(void *data, struct xdg_shell *shell, uint32_t serial)<br>
> > +{<br>
> > +     xdg_shell_pong(shell, serial);<br>
> > +}<br>
> > +<br>
> > +static const struct xdg_shell_listener xdg_shell_listener = {<br>
> > +     xdg_shell_ping,<br>
> > +};<br>
> > +<br>
> > +#define XDG_VERSION 3 /* The version of xdg-shell that we implement */<br>
> > +#ifdef static_assert<br>
> > +static_assert(XDG_VERSION == XDG_SHELL_VERSION_CURRENT,<br>
> > +           "Interface version doesn't match implementation version");<br>
> > +#endif<br>
> > +<br>
> > +static void<br>
> > +registry_handle_global(void *data, struct wl_registry *registry,<br>
> > +                    uint32_t id, const char *interface, uint32_t version)<br>
> > +{<br>
> > +     struct display *d = data;<br>
> > +<br>
> > +     if (strcmp(interface, "wl_compositor") == 0) {<br>
> > +             if (d->target_version > (int)version) {<br>
> > +                     fprintf(stderr, "Compositor does not support "<br>
> > +                             "wl_surface version %d\n", d->target_version);<br>
> > +                     exit(1);<br>
> > +             }<br>
> > +<br>
> > +             if (d->target_version > 0)<br>
> > +                     version = d->target_version;<br>
> > +<br>
> > +             d->compositor =<br>
> > +                     wl_registry_bind(registry,<br>
> > +                                      id, &wl_compositor_interface, version);<br>
> > +     } else if (strcmp(interface, "wl_scaler") == 0 && version >= 2) {<br>
> > +             d->scaler = wl_registry_bind(registry,<br>
> > +                                          id, &wl_scaler_interface, 2);<br>
> > +     } else if (strcmp(interface, "xdg_shell") == 0) {<br>
> > +             d->shell = wl_registry_bind(registry,<br>
> > +                                         id, &xdg_shell_interface, 1);<br>
> > +             xdg_shell_use_unstable_version(d->shell, XDG_VERSION);<br>
> > +             xdg_shell_add_listener(d->shell, &xdg_shell_listener, d);<br>
> > +     } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {<br>
> > +             d->fshell = wl_registry_bind(registry,<br>
> > +                                          id, &_wl_fullscreen_shell_interface, 1);<br>
> > +     } else if (strcmp(interface, "wl_shm") == 0) {<br>
> > +             d->shm = wl_registry_bind(registry,<br>
> > +                                       id, &wl_shm_interface, 1);<br>
> > +             wl_shm_add_listener(d->shm, &shm_listener, d);<br>
> > +     }<br>
> > +}<br>
> > +<br>
> > +static void<br>
> > +registry_handle_global_remove(void *data, struct wl_registry *registry,<br>
> > +                           uint32_t name)<br>
> > +{<br>
> > +}<br>
> > +<br>
> > +static const struct wl_registry_listener registry_listener = {<br>
> > +     registry_handle_global,<br>
> > +     registry_handle_global_remove<br>
> > +};<br>
> > +<br>
> > +static struct display *<br>
> > +create_display(int version)<br>
> > +{<br>
> > +     struct display *display;<br>
> > +<br>
> > +     display = malloc(sizeof *display);<br>
> > +     if (display == NULL) {<br>
> > +             fprintf(stderr, "out of memory\n");<br>
> > +             exit(1);<br>
> > +     }<br>
> > +     display->display = wl_display_connect(NULL);<br>
> > +     assert(display->display);<br>
> > +<br>
> > +     display->target_version = version;<br>
> > +     display->formats = 0;<br>
> > +     display->registry = wl_display_get_registry(display->display);<br>
> > +     wl_registry_add_listener(display->registry,<br>
> > +                              &registry_listener, display);<br>
> > +     wl_display_roundtrip(display->display);<br>
> > +     if (display->shm == NULL) {<br>
> > +             fprintf(stderr, "No wl_shm global\n");<br>
> > +             exit(1);<br>
> > +     }<br>
> > +<br>
> > +     wl_display_roundtrip(display->display);<br>
> > +<br>
> > +     if (!(display->formats & (1 << WL_SHM_FORMAT_XRGB8888))) {<br>
> > +             fprintf(stderr, "WL_SHM_FORMAT_XRGB32 not available\n");<br>
> > +             exit(1);<br>
> > +     }<br>
> > +<br>
> > +     return display;<br>
> > +}<br>
> > +<br>
> > +static void<br>
> > +destroy_display(struct display *display)<br>
> > +{<br>
> > +     if (display->shm)<br>
> > +             wl_shm_destroy(display->shm);<br>
> > +<br>
> > +     if (display->shell)<br>
> > +             xdg_shell_destroy(display->shell);<br>
> > +<br>
> > +     if (display->fshell)<br>
> > +             _wl_fullscreen_shell_release(display->fshell);<br>
> > +<br>
> > +     if (display->scaler)<br>
> > +             wl_scaler_destroy(display->scaler);<br>
> > +<br>
> > +     if (display->compositor)<br>
> > +             wl_compositor_destroy(display->compositor);<br>
> > +<br>
> > +     wl_registry_destroy(display->registry);<br>
> > +     wl_display_flush(display->display);<br>
> > +     wl_display_disconnect(display->display);<br>
> > +     free(display);<br>
> > +}<br>
> > +<br>
> > +static void<br>
> > +signal_int(int signum)<br>
> > +{<br>
> > +     running = 0;<br>
> > +}<br>
> > +<br>
> > +static void<br>
> > +print_usage(int retval)<br>
> > +{<br>
> > +     printf(<br>
> > +             "usage: weston-simple-damage [options]\n\n"<br>
> > +             "options:\n"<br>
> > +             "  -h, --help\t\tPring this help\n"<br>
> > +             "  --verbose\t\tPrint verbose log information\n"<br>
> > +             "  --version=VERSION\tVersion of wl_surface to use\n"<br>
> > +             "  --width=WIDTH\t\tWidth of the X window\n"<br>
> > +             "  --height=HEIGHT\tHeight of the X window\n"<br>
> > +             "  --scale=SCALE\t\tScale factor for the surface\n"<br>
> > +             "  --transform=TRANSFORM\tTransform for the surface\n"<br>
> > +             "  --viewport\t\tUse wl_viewport\n"<br>
> > +     );<br>
> > +<br>
> > +     exit(retval);<br>
> > +}<br>
> > +<br>
> > +static int<br>
> > +parse_transform(const char *str, enum wl_output_transform *transform)<br>
> > +{<br>
> > +     int i;<br>
> > +     static const struct {<br>
> > +             const char *name;<br>
> > +             enum wl_output_transform transform;<br>
> > +     } names[] = {<br>
> > +             { "normal",     WL_OUTPUT_TRANSFORM_NORMAL },<br>
> > +             { "90",         WL_OUTPUT_TRANSFORM_90 },<br>
> > +             { "180",        WL_OUTPUT_TRANSFORM_180 },<br>
> > +             { "270",        WL_OUTPUT_TRANSFORM_270 },<br>
> > +             { "flipped",    WL_OUTPUT_TRANSFORM_FLIPPED },<br>
> > +             { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },<br>
> > +             { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },<br>
> > +             { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },<br>
> > +     };<br>
> > +<br>
> > +     for (i = 0; i < 8; i++) {<br>
> > +             if (strcmp(names[i].name, str) == 0) {<br>
> > +                     *transform = names[i].transform;<br>
> > +                     return 1;<br>
> > +             }<br>
> > +     }<br>
> > +<br>
> > +     return 0;<br>
> > +}<br>
> > +<br>
> > +int<br>
> > +main(int argc, char **argv)<br>
> > +{<br>
> > +     struct sigaction sigint;<br>
> > +     struct display *display;<br>
> > +     struct window *window;<br>
> > +     int i, ret = 0;<br>
> > +     int version = 0;<br>
> > +     int width = 300, height = 200, scale = 1, use_viewport = 0;<br>
> > +     enum wl_output_transform transform = WL_OUTPUT_TRANSFORM_NORMAL;<br>
> > +<br>
> > +     for (i = 1; i < argc; ++i) {<br>
> > +             if (strcmp(argv[i], "--help") == 0 ||<br>
> > +                 strcmp(argv[i], "-h") == 0) {<br>
> > +                     print_usage(0);<br>
> > +             } else if (sscanf(argv[i], "--version=%d", &version) > 0) {<br>
> > +                     if (version < 1 || version > 3) {<br>
> > +                             fprintf(stderr, "Unsupported wl_surface version: %d\n",<br>
> > +                                     version);<br>
> > +                             return 1;<br>
> > +                     }<br>
> > +                     continue;<br>
> > +             } else if (strcmp(argv[i], "--verbose") == 0) {<br>
> > +                     print_debug = 1;<br>
> > +                     continue;<br>
> > +             } else if (sscanf(argv[i], "--width=%d", &width) > 0) {<br>
> > +                     continue;<br>
> > +             } else if (sscanf(argv[i], "--height=%d", &height) > 0) {<br>
> > +                     continue;<br>
> > +             } else if (strncmp(argv[i], "--transform=", 12) == 0 &&<br>
> > +                        parse_transform(argv[i] + 12, &transform) > 0) {<br>
> > +                     continue;<br>
> > +             } else if (sscanf(argv[i], "--scale=%d", &scale) > 0) {<br>
> > +                     continue;<br>
> > +             } else if (strcmp(argv[i], "--use-viewport") == 0) {<br>
> > +                     use_viewport = 1;<br>
> > +                     continue;<br>
> > +             } else {<br>
> > +                     printf("Invalid option: %s\n", argv[i]);<br>
> > +                     print_usage(255);<br>
> > +             }<br>
> > +     }<br>
> > +<br>
> > +     display = create_display(version);<br>
> > +<br>
> > +     window = create_window(display, width, height, transform, scale,<br>
> > +                            use_viewport);<br>
> > +     if (!window)<br>
> > +             return 1;<br>
> > +<br>
> > +     sigint.sa_handler = signal_int;<br>
> > +     sigemptyset(&sigint.sa_mask);<br>
> > +     sigint.sa_flags = SA_RESETHAND;<br>
> > +     sigaction(SIGINT, &sigint, NULL);<br>
> > +<br>
> > +     redraw(window, NULL, 0);<br>
> > +<br>
> > +     while (running && ret != -1)<br>
> > +             ret = wl_display_dispatch(display->display);<br>
> > +<br>
> > +     fprintf(stderr, "simple-shm exiting\n");<br>
> > +     destroy_window(window);<br>
> > +     destroy_display(display);<br>
> > +<br>
> > +     return 0;<br>
> > +}<br>
><br>
> Hi,<br>
><br>
> I haven't tried or reviewed this properly yet, but I very much like<br>
> the idea, and would like to see this merged.<br>
><br>
> Furthermore, Jason said this exposes bugs in the wl_viewport vs.<br>
> buffer_transform interactions, and maybe in other places, so this<br>
> would clearly be valuable.<br>
><br>
> Then we could have easily reproducable bug reports filed, too.</p>
<p dir="ltr">Thanks Pekka,<br>
I have some local modifications and this is part of a bigger project to fix damage handling in Wayland/Weston.  If it doesn't get merged now, I'll include it in the patch series for that.  Unfortunately, it's not trivial and I have very little time to dedicate to it right now so it may be a while.</p>

<p dir="ltr">--Jason Ekstrand<br>
</p>