<br><br><div class="gmail_quote">On Wed, Apr 4, 2012 at 6:37 AM, Kristian Hoegsberg <span dir="ltr">&lt;<a href="mailto:hoegsberg@gmail.com">hoegsberg@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Wed, Apr 04, 2012 at 01:51:08AM -0600, Scott Moreau wrote:<br>
&gt; ---<br>
<br>
Looks pretty good.  There are a few small comments below, but the<br>
overall approach is good.<br>
<div class="im"><br>
&gt; Tested: Side-by-side and stacked output configurations, in x11 and drm.<br>
&gt; Shooting under drm glitches sometimes and either or both output data<br>
&gt; contains garbage.<br>
<br>
</div>I see the drm glitches without this, it&#39;s a different problem.<br>
<div><div class="h5"><br>
&gt;<br>
&gt;  clients/screenshot.c     |   69 ++++++++++++++++++++++++++++++++++++++++------<br>
&gt;  src/compositor-drm.c     |   14 +++++++++<br>
&gt;  src/compositor-wayland.c |   14 +++++++++<br>
&gt;  src/compositor-x11.c     |   14 +++++++++<br>
&gt;  src/compositor.h         |    1 +<br>
&gt;  src/screenshooter.c      |   28 +++++++++---------<br>
&gt;  6 files changed, 117 insertions(+), 23 deletions(-)<br>
&gt;<br>
&gt; diff --git a/clients/screenshot.c b/clients/screenshot.c<br>
&gt; index 6cc2ebb..40f0af4 100644<br>
&gt; --- a/clients/screenshot.c<br>
&gt; +++ b/clients/screenshot.c<br>
&gt; @@ -32,14 +32,24 @@<br>
&gt;  #include &lt;wayland-client.h&gt;<br>
&gt;  #include &quot;screenshooter-client-protocol.h&quot;<br>
&gt;<br>
&gt; +#define MAX(X,Y) ((X) &gt; (Y) ? (X) : (Y))<br>
&gt; +<br>
&gt;  /* The screenshooter is a good example of a custom object exposed by<br>
&gt;   * the compositor and serves as a test bed for implementing client<br>
&gt;   * side marshalling outside libwayland.so */<br>
&gt;<br>
&gt; -static struct wl_output *output;<br>
&gt;  static struct wl_shm *shm;<br>
&gt;  static struct screenshooter *screenshooter;<br>
&gt; -static int output_width, output_height;<br>
&gt; +static struct wl_list output_list;<br>
&gt; +<br>
&gt; +struct ss_output {<br>
&gt; +     struct wl_output *output;<br>
&gt; +     struct wl_buffer *buffer;<br>
&gt; +     int width, height, offset_x, offset_y;<br>
&gt; +     struct wl_list link;<br>
&gt; +};<br>
&gt; +<br>
&gt; +static struct ss_output *output;<br>
<br>
</div></div>Can we call this screenshooter_output instead?<br></blockquote><div><br>I named it screenshooter_output to begin with but felt it made the code too messy.<br>It can be renamed back.<br></div><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

<div class="im"><br>
&gt;  static void<br>
&gt;  display_handle_geometry(void *data,<br>
&gt; @@ -52,6 +62,14 @@ display_handle_geometry(void *data,<br>
&gt;                       const char *make,<br>
&gt;                       const char *model)<br>
&gt;  {<br>
&gt; +     struct ss_output *ss_output;<br>
&gt; +<br>
&gt; +     wl_list_for_each(ss_output, &amp;output_list, link) {<br>
&gt; +             if (wl_output == ss_output-&gt;output) {<br>
&gt; +                     ss_output-&gt;offset_x = x;<br>
&gt; +                     ss_output-&gt;offset_y = y;<br>
&gt; +             }<br>
&gt; +     }<br>
<br>
</div>Instead of this loop, you can just set the user data on the wl_output<br>
object (the data arg to add_listener) and then get it here using<br>
<br>
  ss_output = wl_output_get_user_data(wl_output);<br></blockquote><div><br>Yes, I didn&#39;t realize you can do this. <br></div><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

<div class="im"><br>
<br>
&gt;  }<br>
&gt;<br>
&gt;  static void<br>
&gt; @@ -62,8 +80,15 @@ display_handle_mode(void *data,<br>
&gt;                   int height,<br>
&gt;                   int refresh)<br>
&gt;  {<br>
&gt; -     output_width = width;<br>
&gt; -     output_height = height;<br>
&gt; +     struct ss_output *ss_output;<br>
&gt; +<br>
&gt; +     wl_list_for_each(ss_output, &amp;output_list, link) {<br>
&gt; +             if (wl_output == ss_output-&gt;output &amp;&amp;<br>
&gt; +                             (flags &amp; WL_OUTPUT_MODE_CURRENT)) {<br>
&gt; +                     ss_output-&gt;width = width;<br>
&gt; +                     ss_output-&gt;height = height;<br>
&gt; +             }<br>
&gt; +     }<br>
<br>
</div>Same here.<br></blockquote><div><br>Right. <br></div><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div class="im"><br>
&gt;  }<br>
&gt;<br>
&gt;  static const struct wl_output_listener output_listener = {<br>
&gt; @@ -76,8 +101,10 @@ handle_global(struct wl_display *display, uint32_t id,<br>
&gt;             const char *interface, uint32_t version, void *data)<br>
&gt;  {<br>
&gt;       if (strcmp(interface, &quot;wl_output&quot;) == 0) {<br>
&gt; -             output = wl_display_bind(display, id, &amp;wl_output_interface);<br>
&gt; -             wl_output_add_listener(output, &amp;output_listener, NULL);<br>
&gt; +             output = malloc(sizeof *output);<br>
&gt; +             output-&gt;output = wl_display_bind(display, id, &amp;wl_output_interface);<br>
&gt; +             wl_list_insert(&amp;output_list, &amp;output-&gt;link);<br>
&gt; +             wl_output_add_listener(output-&gt;output, &amp;output_listener, NULL);<br>
<br>
</div>Pass output instead of NULL as the last arg here.<br></blockquote><div><br>I see.. <br></div><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

<div><div class="h5"><br>
&gt;       } else if (strcmp(interface, &quot;wl_shm&quot;) == 0) {<br>
&gt;               shm = wl_display_bind(display, id, &amp;wl_shm_interface);<br>
&gt;       } else if (strcmp(interface, &quot;screenshooter&quot;) == 0) {<br>
&gt; @@ -144,6 +171,8 @@ int main(int argc, char *argv[])<br>
&gt;       struct wl_display *display;<br>
&gt;       struct wl_buffer *buffer;<br>
&gt;       void *data = NULL;<br>
&gt; +     struct ss_output *ss_output;<br>
&gt; +     int ss_area_width = 0, ss_area_height = 0, num_outputs = 0;<br>
&gt;<br>
&gt;       display = wl_display_connect(NULL);<br>
&gt;       if (display == NULL) {<br>
&gt; @@ -151,6 +180,7 @@ int main(int argc, char *argv[])<br>
&gt;               return -1;<br>
&gt;       }<br>
&gt;<br>
&gt; +     wl_list_init(&amp;output_list);<br>
&gt;       wl_display_add_global_listener(display, handle_global, &amp;screenshooter);<br>
&gt;       wl_display_iterate(display, WL_DISPLAY_READABLE);<br>
&gt;       wl_display_roundtrip(display);<br>
&gt; @@ -159,11 +189,32 @@ int main(int argc, char *argv[])<br>
&gt;               return -1;<br>
&gt;       }<br>
&gt;<br>
&gt; -     buffer = create_shm_buffer(output_width, output_height, &amp;data);<br>
&gt; -     screenshooter_shoot(screenshooter, output, buffer);<br>
&gt; +     wl_list_for_each(ss_output, &amp;output_list, link) {<br>
&gt; +<br>
&gt; +             if (!num_outputs) {<br>
&gt; +                     ss_area_width = ss_output-&gt;width + ss_output-&gt;offset_x;<br>
&gt; +                     ss_area_height = ss_output-&gt;height + ss_output-&gt;offset_y;<br>
&gt; +             }<br>
<br>
</div></div>I&#39;d just set ss_area_width/height to 0 initially.<br></blockquote><div><br>num_outputs must be initialised since it checks !num_outputs on the first pass.<br>Unless there is another way to do this, we need to set num_outputs to begin with.<br>
</div><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div class="im"><br>
<br>
&gt; +             else {<br>
&gt; +                     ss_area_width = MAX(ss_area_width, ss_output-&gt;offset_x + ss_output-&gt;width);<br>
&gt; +                     ss_area_height = MAX(ss_area_height, ss_output-&gt;offset_y + ss_output-&gt;height);<br>
&gt; +             }<br>
&gt; +             num_outputs++;<br>
&gt; +     }<br>
&gt; +<br>
&gt; +     buffer = create_shm_buffer(ss_area_width, ss_area_height, &amp;data);<br>
&gt; +<br>
&gt; +     wl_list_for_each(ss_output, &amp;output_list, link) {<br>
&gt; +             screenshooter_shoot(screenshooter, ss_output-&gt;output, buffer);<br>
&gt; +     }<br>
&gt; +<br>
&gt;       wl_display_roundtrip(display);<br>
&gt;<br>
&gt; -     write_png(output_width, output_height, data);<br>
&gt; +     write_png(ss_area_width, ss_area_height, data);<br>
&gt; +<br>
&gt; +     wl_list_for_each(ss_output, &amp;output_list, link) {<br>
&gt; +             free(ss_output);<br>
<br>
</div>You need wl_list_for_each_safe here since you&#39;re freeing the structs<br>
that holds the prev/next pointers.<br></blockquote><div><br>Ah, I was wondering about that. <br></div><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

<div><div class="h5"><br>
&gt; +     }<br>
&gt;<br>
&gt;       return 0;<br>
&gt;  }<br>
&gt; diff --git a/src/compositor-drm.c b/src/compositor-drm.c<br>
&gt; index d428c82..7f2886e 100644<br>
&gt; --- a/src/compositor-drm.c<br>
&gt; +++ b/src/compositor-drm.c<br>
&gt; @@ -1047,6 +1047,19 @@ drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)<br>
&gt;       drmModeFreeConnector(connector);<br>
&gt;  }<br>
&gt;<br>
&gt; +static void<br>
&gt; +drm_output_read_pixels(struct weston_output *output_base, void *data) {<br>
&gt; +     struct drm_output *output = (struct drm_output *) output_base;<br>
&gt; +     struct drm_compositor *compositor =<br>
&gt; +             (struct drm_compositor *) output-&gt;base.compositor;<br>
&gt; +<br>
&gt; +     eglMakeCurrent(compositor-&gt;base.display, output-&gt;egl_surface,<br>
&gt; +                     output-&gt;egl_surface, compositor-&gt;base.context);<br>
&gt; +<br>
&gt; +     glReadPixels(0, 0, output_base-&gt;current-&gt;width, output_base-&gt;current-&gt;height,<br>
&gt; +                             GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);<br>
&gt; +}<br>
&gt; +<br>
&gt;  static int<br>
&gt;  create_output_for_connector(struct drm_compositor *ec,<br>
&gt;                           drmModeRes *resources,<br>
&gt; @@ -1085,6 +1098,7 @@ create_output_for_connector(struct drm_compositor *ec,<br>
&gt;       output-&gt;base.subpixel = drm_subpixel_to_wayland(connector-&gt;subpixel);<br>
&gt;       output-&gt;base.make = &quot;unknown&quot;;<br>
&gt;       output-&gt;base.model = &quot;unknown&quot;;<br>
&gt; +     output-&gt;base.read_pixels = drm_output_read_pixels;<br>
<br>
</div></div>Group this assignment with the other function pointer assignments<br>
(repaint, destroy etc, at the end of the function).<br></blockquote><div><br>Got it. <br></div><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div><div class="h5"><br>
&gt;       wl_list_init(&amp;output-&gt;base.mode_list);<br>
&gt;<br>
&gt;       output-&gt;crtc_id = resources-&gt;crtcs[i];<br>
&gt; diff --git a/src/compositor-wayland.c b/src/compositor-wayland.c<br>
&gt; index 593e272..8c87721 100644<br>
&gt; --- a/src/compositor-wayland.c<br>
&gt; +++ b/src/compositor-wayland.c<br>
&gt; @@ -377,6 +377,19 @@ wayland_output_destroy(struct weston_output *output_base)<br>
&gt;       return;<br>
&gt;  }<br>
&gt;<br>
&gt; +static void<br>
&gt; +wayland_output_read_pixels(struct weston_output *output_base, void *data) {<br>
&gt; +     struct wayland_output *output = (struct wayland_output *) output_base;<br>
&gt; +     struct wayland_compositor *compositor =<br>
&gt; +             (struct wayland_compositor *) output-&gt;base.compositor;<br>
&gt; +<br>
&gt; +     eglMakeCurrent(compositor-&gt;base.display, output-&gt;egl_surface,<br>
&gt; +                     output-&gt;egl_surface, compositor-&gt;base.context);<br>
&gt; +<br>
&gt; +     glReadPixels(0, 0, output_base-&gt;current-&gt;width, output_base-&gt;current-&gt;height,<br>
&gt; +                             GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);<br>
&gt; +}<br>
&gt; +<br>
&gt;  static int<br>
&gt;  wayland_compositor_create_output(struct wayland_compositor *c,<br>
&gt;                                int width, int height)<br>
&gt; @@ -393,6 +406,7 @@ wayland_compositor_create_output(struct wayland_compositor *c,<br>
&gt;       output-&gt;mode.width = width;<br>
&gt;       output-&gt;mode.height = height;<br>
&gt;       output-&gt;mode.refresh = 60;<br>
&gt; +     output-&gt;base.read_pixels = wayland_output_read_pixels;<br>
&gt;       wl_list_init(&amp;output-&gt;base.mode_list);<br>
&gt;       wl_list_insert(&amp;output-&gt;base.mode_list, &amp;output-&gt;mode.link);<br>
&gt;<br>
&gt; diff --git a/src/compositor-x11.c b/src/compositor-x11.c<br>
&gt; index d0203ca..09aa117 100644<br>
&gt; --- a/src/compositor-x11.c<br>
&gt; +++ b/src/compositor-x11.c<br>
&gt; @@ -344,6 +344,19 @@ x11_output_set_icon(struct x11_compositor *c,<br>
&gt;       pixman_image_unref(image);<br>
&gt;  }<br>
&gt;<br>
&gt; +static void<br>
&gt; +x11_output_read_pixels(struct weston_output *output_base, void *data) {<br>
&gt; +     struct x11_output *output = (struct x11_output *) output_base;<br>
&gt; +     struct x11_compositor *compositor =<br>
&gt; +             (struct x11_compositor *) output-&gt;base.compositor;<br>
&gt; +<br>
&gt; +     eglMakeCurrent(compositor-&gt;base.display, output-&gt;egl_surface,<br>
&gt; +                     output-&gt;egl_surface, compositor-&gt;base.context);<br>
&gt; +<br>
&gt; +     glReadPixels(0, 0, output_base-&gt;current-&gt;width, output_base-&gt;current-&gt;height,<br>
&gt; +                             GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);<br>
&gt; +}<br>
&gt; +<br>
&gt;  static int<br>
&gt;  x11_compositor_create_output(struct x11_compositor *c, int x, int y,<br>
&gt;                            int width, int height, int fullscreen)<br>
&gt; @@ -381,6 +394,7 @@ x11_compositor_create_output(struct x11_compositor *c, int x, int y,<br>
&gt;       output-&gt;mode.width = width;<br>
&gt;       output-&gt;mode.height = height;<br>
&gt;       output-&gt;mode.refresh = 60;<br>
&gt; +     output-&gt;base.read_pixels = x11_output_read_pixels;<br>
&gt;       wl_list_init(&amp;output-&gt;base.mode_list);<br>
&gt;       wl_list_insert(&amp;output-&gt;base.mode_list, &amp;output-&gt;mode.link);<br>
&gt;<br>
&gt; diff --git a/src/compositor.h b/src/compositor.h<br>
&gt; index d1cd7c8..ea18b7b 100644<br>
&gt; --- a/src/compositor.h<br>
&gt; +++ b/src/compositor.h<br>
&gt; @@ -96,6 +96,7 @@ struct weston_output {<br>
&gt;                       pixman_region32_t *damage);<br>
&gt;       void (*destroy)(struct weston_output *output);<br>
&gt;       void (*assign_planes)(struct weston_output *output);<br>
&gt; +     void (*read_pixels)(struct weston_output *output, void *data);<br>
&gt;<br>
&gt;       /* backlight values are on 0-255 range, where higher is brighter */<br>
&gt;       uint32_t backlight_current;<br>
&gt; diff --git a/src/screenshooter.c b/src/screenshooter.c<br>
&gt; index f9497f7..3fe7bc3 100644<br>
&gt; --- a/src/screenshooter.c<br>
&gt; +++ b/src/screenshooter.c<br>
&gt; @@ -32,7 +32,7 @@ struct screenshooter {<br>
&gt;       struct weston_compositor *ec;<br>
&gt;       struct wl_global *global;<br>
&gt;       struct wl_client *client;<br>
&gt; -     struct weston_process screenshooter_process;<br>
&gt; +     struct weston_process process;<br>
&gt;  };<br>
&gt;<br>
&gt;  static void<br>
&gt; @@ -44,7 +44,7 @@ screenshooter_shoot(struct wl_client *client,<br>
&gt;       struct weston_output *output = output_resource-&gt;data;<br>
&gt;       struct wl_buffer *buffer = buffer_resource-&gt;data;<br>
&gt;       uint8_t *tmp, *d, *s;<br>
&gt; -     int32_t stride, i;<br>
&gt; +     int32_t buffer_stride, output_stride, i;<br>
&gt;<br>
&gt;       if (!wl_buffer_is_shm(buffer))<br>
&gt;               return;<br>
&gt; @@ -53,24 +53,24 @@ screenshooter_shoot(struct wl_client *client,<br>
&gt;           buffer-&gt;height &lt; output-&gt;current-&gt;height)<br>
&gt;               return;<br>
&gt;<br>
&gt; -     stride = wl_shm_buffer_get_stride(buffer);<br>
&gt; -     tmp = malloc(stride * buffer-&gt;height);<br>
&gt; +     buffer_stride = wl_shm_buffer_get_stride(buffer);<br>
&gt; +     output_stride = output-&gt;current-&gt;width * 4;<br>
&gt; +     tmp = malloc(output_stride * output-&gt;current-&gt;height);<br>
&gt;       if (tmp == NULL) {<br>
&gt;               wl_resource_post_no_memory(resource);<br>
&gt;               return;<br>
&gt;       }<br>
&gt;<br>
&gt;       glPixelStorei(GL_PACK_ALIGNMENT, 1);<br>
&gt; -     glReadPixels(0, 0, output-&gt;current-&gt;width, output-&gt;current-&gt;height,<br>
&gt; -                  GL_BGRA_EXT, GL_UNSIGNED_BYTE, tmp);<br>
&gt; +     output-&gt;read_pixels(output, tmp);<br>
&gt;<br>
&gt; -     d = wl_shm_buffer_get_data(buffer);<br>
&gt; -     s = tmp + stride * (buffer-&gt;height - 1);<br>
&gt; +     d = wl_shm_buffer_get_data(buffer) + output-&gt;y * buffer_stride;<br>
<br>
</div></div>Just add output-&gt;x * 4 term to d here instead of inside the loop.<br></blockquote><div><br>Good idea. <br></div><blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">

<div class="im"><br>
&gt; +     s = tmp + output_stride * (output-&gt;current-&gt;height - 1);<br>
&gt;<br>
&gt; -     for (i = 0; i &lt; buffer-&gt;height; i++) {<br>
&gt; -             memcpy(d, s, stride);<br>
&gt; -             d += stride;<br>
&gt; -             s -= stride;<br>
&gt; +     for (i = 0; i &lt; output-&gt;current-&gt;height; i++) {<br>
&gt; +             memcpy(d + output-&gt;x * 4, s, output_stride);<br>
&gt; +             d += buffer_stride;<br>
&gt; +             s -= output_stride;<br>
&gt;       }<br>
&gt;<br>
&gt;       free(tmp);<br>
&gt; @@ -101,7 +101,7 @@ static void<br>
&gt;  screenshooter_sigchld(struct weston_process *process, int status)<br>
&gt;  {<br>
&gt;       struct screenshooter *shooter =<br>
&gt; -             container_of(process, struct screenshooter, screenshooter_process);<br>
&gt; +             container_of(process, struct screenshooter, process);<br>
&gt;<br>
&gt;       shooter-&gt;client = NULL;<br>
&gt;  }<br>
&gt; @@ -116,7 +116,7 @@ screenshooter_binding(struct wl_input_device *device, uint32_t time,<br>
&gt;<br>
&gt;       if (!shooter-&gt;client)<br>
&gt;               shooter-&gt;client = weston_client_launch(shooter-&gt;ec,<br>
&gt; -                                     &amp;shooter-&gt;screenshooter_process,<br>
&gt; +                                     &amp;shooter-&gt;process,<br>
&gt;                                       screenshooter_exe, screenshooter_sigchld);<br>
&gt;  }<br>
&gt;<br>
&gt; --<br>
&gt; 1.7.4.1<br>
&gt;<br>
</div>&gt; _______________________________________________<br>
&gt; wayland-devel mailing list<br>
&gt; <a href="mailto:wayland-devel@lists.freedesktop.org">wayland-devel@lists.freedesktop.org</a><br>
&gt; <a href="http://lists.freedesktop.org/mailman/listinfo/wayland-devel" target="_blank">http://lists.freedesktop.org/mailman/listinfo/wayland-devel</a><br></blockquote><div><br><br><br>Thanks for your review, I will write a new patch considering what you&#39;ve mentioned here.<br>
<br><br>Scott<br></div></div><br>