screenshooter: Grab pixel data directly before buffer swap
[profile/ivi/weston-ivi-shell.git] / src / screenshooter.c
1 /*
2  * Copyright © 2008-2011 Kristian Høgsberg
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and
5  * its documentation for any purpose is hereby granted without fee, provided
6  * that the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation, and that the name of the copyright holders not be used in
9  * advertising or publicity pertaining to distribution of the software
10  * without specific, written prior permission.  The copyright holders make
11  * no representations about the suitability of this software for any
12  * purpose.  It is provided "as is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16  * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17  * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19  * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <linux/input.h>
26
27 #include "compositor.h"
28 #include "screenshooter-server-protocol.h"
29
30 struct screenshooter {
31         struct wl_object base;
32         struct weston_compositor *ec;
33         struct wl_global *global;
34         struct wl_client *client;
35         struct weston_process process;
36         struct wl_listener destroy_listener;
37 };
38
39 struct screenshooter_read_pixels {
40         struct weston_read_pixels base;
41         struct wl_buffer *buffer;
42         struct wl_resource *resource;
43 };
44
45 static void
46 copy_bgra_yflip(uint8_t *dst, uint8_t *src, int height,
47                 int dst_stride, int src_stride)
48 {
49         uint8_t *end;
50
51         end = dst + height * dst_stride;
52         while (dst < end) {
53                 memcpy(dst, src, src_stride);
54                 dst += dst_stride;
55                 src -= src_stride;
56         }
57 }
58
59 static void
60 copy_row_swap_RB(void *vdst, void *vsrc, int bytes)
61 {
62         uint32_t *dst = vdst;
63         uint32_t *src = vsrc;
64         uint32_t *end = dst + bytes / 4;
65
66         while (dst < end) {
67                 uint32_t v = *src++;
68                 /*                    A R G B */
69                 uint32_t tmp = v & 0xff00ff00;
70                 tmp |= (v >> 16) & 0x000000ff;
71                 tmp |= (v << 16) & 0x00ff0000;
72                 *dst++ = tmp;
73         }
74 }
75
76 static void
77 copy_rgba_yflip(uint8_t *dst, uint8_t *src, int height,
78                 int dst_stride, int src_stride)
79 {
80         uint8_t *end;
81
82         end = dst + height * dst_stride;
83         while (dst < end) {
84                 copy_row_swap_RB(dst, src, src_stride);
85                 dst += dst_stride;
86                 src -= src_stride;
87         }
88 }
89
90 static void
91 screenshooter_read_pixels_done(struct weston_read_pixels *base,
92                                struct weston_output *output)
93 {
94         struct screenshooter_read_pixels *r =
95                 (struct screenshooter_read_pixels *) base;
96         int32_t buffer_stride, output_stride;
97         uint8_t *d, *s;
98
99         buffer_stride = wl_shm_buffer_get_stride(r->buffer);
100         output_stride = output->current->width * 4;
101
102         d = wl_shm_buffer_get_data(r->buffer) +
103                 output->y * buffer_stride + output->x * 4;
104         s = r->base.data + output_stride * (output->current->height - 1);
105
106         switch (output->compositor->read_format) {
107         case GL_BGRA_EXT:
108                 copy_bgra_yflip(d, s, output->current->height,
109                                 buffer_stride, output_stride);
110                 break;
111         case GL_RGBA:
112                 copy_rgba_yflip(d, s, output->current->height,
113                                 buffer_stride, output_stride);
114                 break;
115         default:
116                 break;
117         }
118
119         wl_list_remove(&r->base.link);
120
121         screenshooter_send_done(r->resource);
122         free(r->base.data);
123         free(r);
124
125 }
126
127 static void
128 screenshooter_shoot(struct wl_client *client,
129                     struct wl_resource *resource,
130                     struct wl_resource *output_resource,
131                     struct wl_resource *buffer_resource)
132 {
133         struct weston_output *output = output_resource->data;
134         struct screenshooter_read_pixels *r;
135         struct wl_buffer *buffer = buffer_resource->data;
136         int32_t stride;
137
138         if (!wl_buffer_is_shm(buffer))
139                 return;
140
141         if (buffer->width < output->current->width ||
142             buffer->height < output->current->height)
143                 return;
144
145         r = malloc(sizeof *r);
146         if (r == NULL) {
147                 wl_resource_post_no_memory(resource);
148                 return;
149         }
150
151         r->base.x = 0;
152         r->base.y = 0;
153         r->base.width = output->current->width;
154         r->base.height = output->current->height;
155         r->base.done = screenshooter_read_pixels_done;
156         r->buffer = buffer;
157         r->resource = resource;
158         stride = buffer->width * 4;
159         r->base.data = malloc(stride * buffer->height);
160
161         if (r->base.data == NULL) {
162                 free(r);
163                 wl_resource_post_no_memory(resource);
164                 return;
165         }
166
167         wl_list_insert(output->read_pixels_list.prev, &r->base.link);
168         weston_compositor_schedule_repaint(output->compositor);
169 }
170
171 struct screenshooter_interface screenshooter_implementation = {
172         screenshooter_shoot
173 };
174
175 static void
176 bind_shooter(struct wl_client *client,
177              void *data, uint32_t version, uint32_t id)
178 {
179         struct screenshooter *shooter = data;
180         struct wl_resource *resource;
181
182         resource = wl_client_add_object(client, &screenshooter_interface,
183                              &screenshooter_implementation, id, data);
184
185         if (client != shooter->client) {
186                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
187                                        "screenshooter failed: permission denied");
188                 wl_resource_destroy(resource);
189         }
190 }
191
192 static void
193 screenshooter_sigchld(struct weston_process *process, int status)
194 {
195         struct screenshooter *shooter =
196                 container_of(process, struct screenshooter, process);
197
198         shooter->client = NULL;
199 }
200
201 static void
202 screenshooter_binding(struct wl_input_device *device, uint32_t time,
203                  uint32_t key, uint32_t button, uint32_t axis,
204                  int32_t state, void *data)
205 {
206         struct screenshooter *shooter = data;
207         const char *screenshooter_exe = LIBEXECDIR "/weston-screenshooter";
208
209         if (!shooter->client)
210                 shooter->client = weston_client_launch(shooter->ec,
211                                         &shooter->process,
212                                         screenshooter_exe, screenshooter_sigchld);
213 }
214
215 static void
216 screenshooter_destroy(struct wl_listener *listener, void *data)
217 {
218         struct screenshooter *shooter =
219                 container_of(listener, struct screenshooter, destroy_listener);
220
221         wl_display_remove_global(shooter->ec->wl_display, shooter->global);
222         free(shooter);
223 }
224
225 void
226 screenshooter_create(struct weston_compositor *ec)
227 {
228         struct screenshooter *shooter;
229
230         shooter = malloc(sizeof *shooter);
231         if (shooter == NULL)
232                 return;
233
234         shooter->base.interface = &screenshooter_interface;
235         shooter->base.implementation =
236                 (void(**)(void)) &screenshooter_implementation;
237         shooter->ec = ec;
238         shooter->client = NULL;
239
240         shooter->global = wl_display_add_global(ec->wl_display,
241                                                 &screenshooter_interface,
242                                                 shooter, bind_shooter);
243         weston_compositor_add_binding(ec, KEY_S, 0, 0, MODIFIER_SUPER,
244                                         screenshooter_binding, shooter);
245
246         shooter->destroy_listener.notify = screenshooter_destroy;
247         wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);
248 }