wcap: Compute per-component deltas
[profile/ivi/weston.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 <stdio.h>
25 #include <string.h>
26 #include <linux/input.h>
27 #include <fcntl.h>
28 #include <unistd.h>
29 #include <sys/uio.h>
30
31 #include "compositor.h"
32 #include "screenshooter-server-protocol.h"
33
34 #include "../wcap/wcap-decode.h"
35
36 struct screenshooter {
37         struct wl_object base;
38         struct weston_compositor *ec;
39         struct wl_global *global;
40         struct wl_client *client;
41         struct weston_process process;
42         struct wl_listener destroy_listener;
43 };
44
45 struct screenshooter_read_pixels {
46         struct weston_read_pixels base;
47         struct wl_buffer *buffer;
48         struct wl_resource *resource;
49 };
50
51 static void
52 copy_bgra_yflip(uint8_t *dst, uint8_t *src, int height, int stride)
53 {
54         uint8_t *end;
55
56         end = dst + height * stride;
57         while (dst < end) {
58                 memcpy(dst, src, stride);
59                 dst += stride;
60                 src -= stride;
61         }
62 }
63
64 static void
65 copy_row_swap_RB(void *vdst, void *vsrc, int bytes)
66 {
67         uint32_t *dst = vdst;
68         uint32_t *src = vsrc;
69         uint32_t *end = dst + bytes / 4;
70
71         while (dst < end) {
72                 uint32_t v = *src++;
73                 /*                    A R G B */
74                 uint32_t tmp = v & 0xff00ff00;
75                 tmp |= (v >> 16) & 0x000000ff;
76                 tmp |= (v << 16) & 0x00ff0000;
77                 *dst++ = tmp;
78         }
79 }
80
81 static void
82 copy_rgba_yflip(uint8_t *dst, uint8_t *src, int height, int stride)
83 {
84         uint8_t *end;
85
86         end = dst + height * stride;
87         while (dst < end) {
88                 copy_row_swap_RB(dst, src, stride);
89                 dst += stride;
90                 src -= stride;
91         }
92 }
93
94 static void
95 screenshooter_read_pixels_done(struct weston_read_pixels *base,
96                                struct weston_output *output)
97 {
98         struct screenshooter_read_pixels *r =
99                 (struct screenshooter_read_pixels *) base;
100         int32_t stride;
101         uint8_t *d, *s;
102
103         stride = wl_shm_buffer_get_stride(r->buffer);
104
105         d = wl_shm_buffer_get_data(r->buffer);
106         s = r->base.data + stride * (r->buffer->height - 1);
107
108         switch (output->compositor->read_format) {
109         case GL_BGRA_EXT:
110                 copy_bgra_yflip(d, s, output->current->height, stride);
111                 break;
112         case GL_RGBA:
113                 copy_rgba_yflip(d, s, output->current->height, 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_seat *seat, 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 struct weston_recorder {
216         struct weston_output *output;
217         uint32_t *frame, *rect;
218         uint32_t total;
219         int fd;
220         struct wl_listener frame_listener;
221         int count;
222 };
223
224 static uint32_t *
225 output_run(uint32_t *p, uint32_t delta, int run)
226 {
227         int i;
228
229         while (run > 0) {
230                 if (run <= 0xe0) {
231                         *p++ = delta | ((run - 1) << 24);
232                         break;
233                 }
234
235                 i = 24 - __builtin_clz(run);
236                 *p++ = delta | ((i + 0xe0) << 24);
237                 run -= 1 << (7 + i);
238         }
239
240         return p;
241 }
242
243 static uint32_t
244 component_delta(uint32_t next, uint32_t prev)
245 {
246         unsigned char dr, dg, db;
247
248         dr = (next >> 16) - (prev >> 16);
249         dg = (next >>  8) - (prev >>  8);
250         db = (next >>  0) - (prev >>  0);
251
252         return (dr << 16) | (dg << 8) | (db << 0);
253 }
254
255 static void
256 weston_recorder_frame_notify(struct wl_listener *listener, void *data)
257 {
258         struct weston_recorder *recorder =
259                 container_of(listener, struct weston_recorder, frame_listener);
260         struct weston_output *output = recorder->output;
261         uint32_t msecs = * (uint32_t *) data;
262         pixman_box32_t *r;
263         pixman_region32_t damage;
264         int i, j, k, n, width, height, run, stride;
265         uint32_t delta, prev, *d, *s, *p, next;
266         struct {
267                 uint32_t msecs;
268                 uint32_t nrects;
269         } header;
270         struct iovec v[2];
271
272         pixman_region32_init(&damage);
273         pixman_region32_intersect(&damage, &recorder->output->region,
274                                   &recorder->output->previous_damage);
275
276         r = pixman_region32_rectangles(&damage, &n);
277         if (n == 0)
278                 return;
279         if (recorder->count++ == 0)
280                 /* The first callback gives us the frame immediately
281                  * before the weston_output_damage() call, and
282                  * typically doesn't give us a full frame of damage.*/
283                 return;
284
285         header.msecs = msecs;
286         header.nrects = n;
287         v[0].iov_base = &header;
288         v[0].iov_len = sizeof header;
289         v[1].iov_base = r;
290         v[1].iov_len = n * sizeof *r;
291         recorder->total += writev(recorder->fd, v, 2);
292         stride = output->current->width;
293
294         for (i = 0; i < n; i++) {
295                 width = r[i].x2 - r[i].x1;
296                 height = r[i].y2 - r[i].y1;
297                 glReadPixels(r[i].x1, output->current->height - r[i].y2,
298                              width, height,
299                              output->compositor->read_format,
300                              GL_UNSIGNED_BYTE, recorder->rect);
301
302                 s = recorder->rect;
303                 p = recorder->rect;
304                 run = prev = 0; /* quiet gcc */
305                 for (j = 0; j < height; j++) {
306                         d = recorder->frame +
307                                 stride * (r[i].y2 - j - 1) + r[i].x1;
308                         for (k = 0; k < width; k++) {
309                                 next = *s++;
310                                 delta = component_delta(next, *d);
311                                 *d++ = next;
312                                 if (run == 0 || delta == prev) {
313                                         run++;
314                                 } else {
315                                         p = output_run(p, prev, run);
316                                         run = 1;
317                                         prev = delta;
318                                 }
319                         }
320                 }
321
322                 p = output_run(p, prev, run);
323
324                 recorder->total += write(recorder->fd,
325                                          recorder->rect,
326                                          (p - recorder->rect) * 4);
327
328 #if 0
329                 fprintf(stderr,
330                         "%dx%d at %d,%d rle from %d to %d bytes (%f) total %dM\n",
331                         width, height, r[i].x1, r[i].y1,
332                         width * height * 4, (int) (p - rect) * 4,
333                         (float) (p - rect) / (width * height),
334                         total / 1024 / 1024);
335 #endif
336         }
337
338         pixman_region32_fini(&damage);
339 }
340
341 static void
342 weston_recorder_create(struct weston_output *output, const char *filename)
343 {
344         struct weston_recorder *recorder;
345         int stride, size;
346         struct { uint32_t magic, format, width, height; } header;
347
348         recorder = malloc(sizeof *recorder);
349         recorder->output = output;
350
351         stride = output->current->width;
352         size = stride * 4 * output->current->height;
353         recorder->frame = malloc(size);
354         recorder->rect = malloc(size);
355         recorder->total = 0;
356         recorder->count = 0;
357         memset(recorder->frame, 0, size);
358
359         recorder->fd = open(filename,
360                             O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
361
362         header.magic = WCAP_HEADER_MAGIC;
363
364         switch (output->compositor->read_format) {
365         case GL_BGRA_EXT:
366                 header.format = WCAP_FORMAT_XRGB8888;
367                 break;
368         case GL_RGBA:
369                 header.format = WCAP_FORMAT_XBGR8888;
370                 break;
371         }
372
373         header.width = output->current->width;
374         header.height = output->current->height;
375         recorder->total += write(recorder->fd, &header, sizeof header);
376
377         recorder->frame_listener.notify = weston_recorder_frame_notify;
378         wl_signal_add(&output->frame_signal, &recorder->frame_listener);
379         weston_output_damage(output);
380 }
381
382 static void
383 weston_recorder_destroy(struct weston_recorder *recorder)
384 {
385         wl_list_remove(&recorder->frame_listener.link);
386         close(recorder->fd);
387         free(recorder->frame);
388         free(recorder->rect);
389         free(recorder);
390 }
391
392 static void
393 recorder_binding(struct wl_seat *seat, uint32_t time,
394                  uint32_t key, uint32_t button, uint32_t axis,
395                  int32_t state, void *data)
396 {
397         struct weston_seat *ws = (struct weston_seat *) seat;
398         struct weston_compositor *ec = ws->compositor;
399         struct weston_output *output =
400                 container_of(ec->output_list.next,
401                              struct weston_output, link);
402         struct wl_listener *listener;
403         struct weston_recorder *recorder;
404         static const char filename[] = "capture.wcap";
405
406         listener = wl_signal_get(&output->frame_signal,
407                                  weston_recorder_frame_notify);
408         if (listener) {
409                 recorder = container_of(listener, struct weston_recorder,
410                                         frame_listener);
411
412                 fprintf(stderr,
413                         "stopping recorder, total file size %dM, %d frames\n",
414                         recorder->total / (1024 * 1024), recorder->count);
415
416                 weston_recorder_destroy(recorder);
417         } else {
418                 fprintf(stderr, "starting recorder, file %s\n", filename);
419                 weston_recorder_create(output, filename);
420         }
421 }
422
423 static void
424 screenshooter_destroy(struct wl_listener *listener, void *data)
425 {
426         struct screenshooter *shooter =
427                 container_of(listener, struct screenshooter, destroy_listener);
428
429         wl_display_remove_global(shooter->ec->wl_display, shooter->global);
430         free(shooter);
431 }
432
433 void
434 screenshooter_create(struct weston_compositor *ec)
435 {
436         struct screenshooter *shooter;
437
438         shooter = malloc(sizeof *shooter);
439         if (shooter == NULL)
440                 return;
441
442         shooter->base.interface = &screenshooter_interface;
443         shooter->base.implementation =
444                 (void(**)(void)) &screenshooter_implementation;
445         shooter->ec = ec;
446         shooter->client = NULL;
447
448         shooter->global = wl_display_add_global(ec->wl_display,
449                                                 &screenshooter_interface,
450                                                 shooter, bind_shooter);
451         weston_compositor_add_binding(ec, KEY_S, 0, 0, MODIFIER_SUPER,
452                                         screenshooter_binding, shooter);
453         weston_compositor_add_binding(ec, KEY_R, 0, 0, MODIFIER_SUPER,
454                                         recorder_binding, shooter);
455
456         shooter->destroy_listener.notify = screenshooter_destroy;
457         wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);
458 }