af2c754a80e9b31445fcc8a220861bb09d01c305
[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 "config.h"
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <linux/input.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <sys/uio.h>
32
33 #include "compositor.h"
34 #include "screenshooter-server-protocol.h"
35
36 #include "../wcap/wcap-decode.h"
37
38 struct screenshooter {
39         struct weston_compositor *ec;
40         struct wl_global *global;
41         struct wl_client *client;
42         struct weston_process process;
43         struct wl_listener destroy_listener;
44 };
45
46 struct screenshooter_frame_listener {
47         struct wl_listener listener;
48         struct weston_buffer *buffer;
49         weston_screenshooter_done_func_t done;
50         void *data;
51 };
52
53 static void
54 copy_bgra_yflip(uint8_t *dst, uint8_t *src, int height, int stride)
55 {
56         uint8_t *end;
57
58         end = dst + height * stride;
59         while (dst < end) {
60                 memcpy(dst, src, stride);
61                 dst += stride;
62                 src -= stride;
63         }
64 }
65
66 static void
67 copy_bgra(uint8_t *dst, uint8_t *src, int height, int stride)
68 {
69         /* TODO: optimize this out */
70         memcpy(dst, src, height * stride);
71 }
72
73 static void
74 copy_row_swap_RB(void *vdst, void *vsrc, int bytes)
75 {
76         uint32_t *dst = vdst;
77         uint32_t *src = vsrc;
78         uint32_t *end = dst + bytes / 4;
79
80         while (dst < end) {
81                 uint32_t v = *src++;
82                 /*                    A R G B */
83                 uint32_t tmp = v & 0xff00ff00;
84                 tmp |= (v >> 16) & 0x000000ff;
85                 tmp |= (v << 16) & 0x00ff0000;
86                 *dst++ = tmp;
87         }
88 }
89
90 static void
91 copy_rgba_yflip(uint8_t *dst, uint8_t *src, int height, int stride)
92 {
93         uint8_t *end;
94
95         end = dst + height * stride;
96         while (dst < end) {
97                 copy_row_swap_RB(dst, src, stride);
98                 dst += stride;
99                 src -= stride;
100         }
101 }
102
103 static void
104 copy_rgba(uint8_t *dst, uint8_t *src, int height, int stride)
105 {
106         uint8_t *end;
107
108         end = dst + height * stride;
109         while (dst < end) {
110                 copy_row_swap_RB(dst, src, stride);
111                 dst += stride;
112                 src += stride;
113         }
114 }
115
116 static void
117 screenshooter_frame_notify(struct wl_listener *listener, void *data)
118 {
119         struct screenshooter_frame_listener *l =
120                 container_of(listener,
121                              struct screenshooter_frame_listener, listener);
122         struct weston_output *output = data;
123         struct weston_compositor *compositor = output->compositor;
124         int32_t stride;
125         uint8_t *pixels, *d, *s;
126
127         output->disable_planes--;
128         wl_list_remove(&listener->link);
129         stride = l->buffer->width * (PIXMAN_FORMAT_BPP(compositor->read_format) / 8);
130         pixels = malloc(stride * l->buffer->height);
131
132         if (pixels == NULL) {
133                 l->done(l->data, WESTON_SCREENSHOOTER_NO_MEMORY);
134                 free(l);
135                 return;
136         }
137
138         compositor->renderer->read_pixels(output,
139                              compositor->read_format, pixels,
140                              0, 0, output->current_mode->width,
141                              output->current_mode->height);
142
143         stride = wl_shm_buffer_get_stride(l->buffer->shm_buffer);
144
145         d = wl_shm_buffer_get_data(l->buffer->shm_buffer);
146         s = pixels + stride * (l->buffer->height - 1);
147
148         wl_shm_buffer_begin_access(l->buffer->shm_buffer);
149
150         switch (compositor->read_format) {
151         case PIXMAN_a8r8g8b8:
152         case PIXMAN_x8r8g8b8:
153                 if (compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP)
154                         copy_bgra_yflip(d, s, output->current_mode->height, stride);
155                 else
156                         copy_bgra(d, pixels, output->current_mode->height, stride);
157                 break;
158         case PIXMAN_x8b8g8r8:
159         case PIXMAN_a8b8g8r8:
160                 if (compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP)
161                         copy_rgba_yflip(d, s, output->current_mode->height, stride);
162                 else
163                         copy_rgba(d, pixels, output->current_mode->height, stride);
164                 break;
165         default:
166                 break;
167         }
168
169         wl_shm_buffer_end_access(l->buffer->shm_buffer);
170
171         l->done(l->data, WESTON_SCREENSHOOTER_SUCCESS);
172         free(pixels);
173         free(l);
174 }
175
176 WL_EXPORT int
177 weston_screenshooter_shoot(struct weston_output *output,
178                            struct weston_buffer *buffer,
179                            weston_screenshooter_done_func_t done, void *data)
180 {
181         struct screenshooter_frame_listener *l;
182
183         if (!wl_shm_buffer_get(buffer->resource)) {
184                 done(data, WESTON_SCREENSHOOTER_BAD_BUFFER);
185                 return -1;
186         }
187
188         buffer->shm_buffer = wl_shm_buffer_get(buffer->resource);
189         buffer->width = wl_shm_buffer_get_width(buffer->shm_buffer);
190         buffer->height = wl_shm_buffer_get_height(buffer->shm_buffer);
191
192         if (buffer->width < output->current_mode->width ||
193             buffer->height < output->current_mode->height) {
194                 done(data, WESTON_SCREENSHOOTER_BAD_BUFFER);
195                 return -1;
196         }
197
198         l = malloc(sizeof *l);
199         if (l == NULL) {
200                 done(data, WESTON_SCREENSHOOTER_NO_MEMORY);
201                 return -1;
202         }
203
204         l->buffer = buffer;
205         l->done = done;
206         l->data = data;
207         l->listener.notify = screenshooter_frame_notify;
208         wl_signal_add(&output->frame_signal, &l->listener);
209         output->disable_planes++;
210         weston_output_schedule_repaint(output);
211
212         return 0;
213 }
214
215 static void
216 screenshooter_done(void *data, enum weston_screenshooter_outcome outcome)
217 {
218         struct wl_resource *resource = data;
219
220         switch (outcome) {
221         case WESTON_SCREENSHOOTER_SUCCESS:
222                 screenshooter_send_done(resource);
223                 break;
224         case WESTON_SCREENSHOOTER_NO_MEMORY:
225                 wl_resource_post_no_memory(resource);
226                 break;
227         default:
228                 break;
229         }
230 }
231
232 static void
233 screenshooter_shoot(struct wl_client *client,
234                     struct wl_resource *resource,
235                     struct wl_resource *output_resource,
236                     struct wl_resource *buffer_resource)
237 {
238         struct weston_output *output =
239                 wl_resource_get_user_data(output_resource);
240         struct weston_buffer *buffer =
241                 weston_buffer_from_resource(buffer_resource);
242
243         if (buffer == NULL) {
244                 wl_resource_post_no_memory(resource);
245                 return;
246         }
247
248         weston_screenshooter_shoot(output, buffer, screenshooter_done, resource);
249 }
250
251 struct screenshooter_interface screenshooter_implementation = {
252         screenshooter_shoot
253 };
254
255 static void
256 bind_shooter(struct wl_client *client,
257              void *data, uint32_t version, uint32_t id)
258 {
259         struct screenshooter *shooter = data;
260         struct wl_resource *resource;
261
262         resource = wl_resource_create(client,
263                                       &screenshooter_interface, 1, id);
264
265         if (client != shooter->client) {
266                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
267                                        "screenshooter failed: permission denied");
268                 return;
269         }
270
271         wl_resource_set_implementation(resource, &screenshooter_implementation,
272                                        data, NULL);
273 }
274
275 static void
276 screenshooter_sigchld(struct weston_process *process, int status)
277 {
278         struct screenshooter *shooter =
279                 container_of(process, struct screenshooter, process);
280
281         shooter->client = NULL;
282 }
283
284 static void
285 screenshooter_binding(struct weston_seat *seat, uint32_t time, uint32_t key,
286                       void *data)
287 {
288         struct screenshooter *shooter = data;
289         char *screenshooter_exe;
290
291         asprintf(&screenshooter_exe, "%s/%s", weston_config_get_libexec_dir(),
292                                               "/weston-screenshooter");
293
294         if (!shooter->client)
295                 shooter->client = weston_client_launch(shooter->ec,
296                                         &shooter->process,
297                                         screenshooter_exe, screenshooter_sigchld);
298         free(screenshooter_exe);
299 }
300
301 struct weston_recorder {
302         struct weston_output *output;
303         uint32_t *frame, *rect;
304         uint32_t *tmpbuf;
305         uint32_t total;
306         int fd;
307         struct wl_listener frame_listener;
308         int count, destroying;
309 };
310
311 static uint32_t *
312 output_run(uint32_t *p, uint32_t delta, int run)
313 {
314         int i;
315
316         while (run > 0) {
317                 if (run <= 0xe0) {
318                         *p++ = delta | ((run - 1) << 24);
319                         break;
320                 }
321
322                 i = 24 - __builtin_clz(run);
323                 *p++ = delta | ((i + 0xe0) << 24);
324                 run -= 1 << (7 + i);
325         }
326
327         return p;
328 }
329
330 static uint32_t
331 component_delta(uint32_t next, uint32_t prev)
332 {
333         unsigned char dr, dg, db;
334
335         dr = (next >> 16) - (prev >> 16);
336         dg = (next >>  8) - (prev >>  8);
337         db = (next >>  0) - (prev >>  0);
338
339         return (dr << 16) | (dg << 8) | (db << 0);
340 }
341
342 static void
343 weston_recorder_destroy(struct weston_recorder *recorder);
344
345 static void
346 weston_recorder_frame_notify(struct wl_listener *listener, void *data)
347 {
348         struct weston_recorder *recorder =
349                 container_of(listener, struct weston_recorder, frame_listener);
350         struct weston_output *output = data;
351         struct weston_compositor *compositor = output->compositor;
352         uint32_t msecs = output->frame_time;
353         pixman_box32_t *r;
354         pixman_region32_t damage, transformed_damage;
355         int i, j, k, n, width, height, run, stride;
356         uint32_t delta, prev, *d, *s, *p, next;
357         struct {
358                 uint32_t msecs;
359                 uint32_t nrects;
360         } header;
361         struct iovec v[2];
362         int do_yflip;
363         int y_orig;
364         uint32_t *outbuf;
365
366         do_yflip = !!(compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP);
367         if (do_yflip)
368                 outbuf = recorder->rect;
369         else
370                 outbuf = recorder->tmpbuf;
371
372         pixman_region32_init(&damage);
373         pixman_region32_init(&transformed_damage);
374         pixman_region32_intersect(&damage, &output->region,
375                                   &output->previous_damage);
376         pixman_region32_translate(&damage, -output->x, -output->y);
377         weston_transformed_region(output->width, output->height,
378                                  output->transform, output->current_scale,
379                                  &damage, &transformed_damage);
380         pixman_region32_fini(&damage);
381
382         r = pixman_region32_rectangles(&transformed_damage, &n);
383         if (n == 0) {
384                 pixman_region32_fini(&transformed_damage);
385                 return;
386         }
387
388         header.msecs = msecs;
389         header.nrects = n;
390         v[0].iov_base = &header;
391         v[0].iov_len = sizeof header;
392         v[1].iov_base = r;
393         v[1].iov_len = n * sizeof *r;
394         recorder->total += writev(recorder->fd, v, 2);
395         stride = output->current_mode->width;
396
397         for (i = 0; i < n; i++) {
398                 width = r[i].x2 - r[i].x1;
399                 height = r[i].y2 - r[i].y1;
400
401                 if (do_yflip)
402                         y_orig = output->current_mode->height - r[i].y2;
403                 else
404                         y_orig = r[i].y1;
405
406                 compositor->renderer->read_pixels(output,
407                                 compositor->read_format, recorder->rect,
408                                 r[i].x1, y_orig, width, height);
409
410                 s = recorder->rect;
411                 p = outbuf;
412                 run = prev = 0; /* quiet gcc */
413                 for (j = 0; j < height; j++) {
414                         if (do_yflip)
415                                 y_orig = r[i].y2 - j - 1;
416                         else
417                                 y_orig = r[i].y1 + j;
418                         d = recorder->frame + stride * y_orig + r[i].x1;
419
420                         for (k = 0; k < width; k++) {
421                                 next = *s++;
422                                 delta = component_delta(next, *d);
423                                 *d++ = next;
424                                 if (run == 0 || delta == prev) {
425                                         run++;
426                                 } else {
427                                         p = output_run(p, prev, run);
428                                         run = 1;
429                                 }
430                                 prev = delta;
431                         }
432                 }
433
434                 p = output_run(p, prev, run);
435
436                 recorder->total += write(recorder->fd,
437                                          outbuf, (p - outbuf) * 4);
438
439 #if 0
440                 fprintf(stderr,
441                         "%dx%d at %d,%d rle from %d to %d bytes (%f) total %dM\n",
442                         width, height, r[i].x1, r[i].y1,
443                         width * height * 4, (int) (p - outbuf) * 4,
444                         (float) (p - outbuf) / (width * height),
445                         recorder->total / 1024 / 1024);
446 #endif
447         }
448
449         pixman_region32_fini(&transformed_damage);
450         recorder->count++;
451
452         if (recorder->destroying)
453                 weston_recorder_destroy(recorder);
454 }
455
456 static void
457 weston_recorder_free(struct weston_recorder *recorder)
458 {
459         if (recorder == NULL)
460                 return;
461         free(recorder->rect);
462         free(recorder->tmpbuf);
463         free(recorder->frame);
464         free(recorder);
465 }
466
467 static void
468 weston_recorder_create(struct weston_output *output, const char *filename)
469 {
470         struct weston_compositor *compositor = output->compositor;
471         struct weston_recorder *recorder;
472         int stride, size;
473         struct { uint32_t magic, format, width, height; } header;
474         int do_yflip;
475
476         do_yflip = !!(compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP);
477
478         recorder = malloc(sizeof *recorder);
479         if (recorder == NULL) {
480                 weston_log("%s: out of memory\n", __func__);
481                 return;
482         }
483
484         stride = output->current_mode->width;
485         size = stride * 4 * output->current_mode->height;
486         recorder->frame = zalloc(size);
487         recorder->rect = malloc(size);
488         recorder->total = 0;
489         recorder->count = 0;
490         recorder->destroying = 0;
491         recorder->output = output;
492
493         if ((recorder->frame == NULL) || (recorder->rect == NULL)) {
494                 weston_log("%s: out of memory\n", __func__);
495                 weston_recorder_free(recorder);
496                 return;
497         }
498
499         if (do_yflip)
500                 recorder->tmpbuf = NULL;
501         else
502                 recorder->tmpbuf = malloc(size);
503
504         header.magic = WCAP_HEADER_MAGIC;
505
506         switch (compositor->read_format) {
507         case PIXMAN_x8r8g8b8:
508         case PIXMAN_a8r8g8b8:
509                 header.format = WCAP_FORMAT_XRGB8888;
510                 break;
511         case PIXMAN_a8b8g8r8:
512                 header.format = WCAP_FORMAT_XBGR8888;
513                 break;
514         default:
515                 weston_log("unknown recorder format\n");
516                 weston_recorder_free(recorder);
517                 return;
518         }
519
520         recorder->fd = open(filename,
521                             O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
522
523         if (recorder->fd < 0) {
524                 weston_log("problem opening output file %s: %m\n", filename);
525                 weston_recorder_free(recorder);
526                 return;
527         }
528
529         header.width = output->current_mode->width;
530         header.height = output->current_mode->height;
531         recorder->total += write(recorder->fd, &header, sizeof header);
532
533         recorder->frame_listener.notify = weston_recorder_frame_notify;
534         wl_signal_add(&output->frame_signal, &recorder->frame_listener);
535         output->disable_planes++;
536         weston_output_damage(output);
537 }
538
539 static void
540 weston_recorder_destroy(struct weston_recorder *recorder)
541 {
542         wl_list_remove(&recorder->frame_listener.link);
543         close(recorder->fd);
544         recorder->output->disable_planes--;
545         weston_recorder_free(recorder);
546 }
547
548 static void
549 recorder_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
550 {
551         struct weston_seat *ws = (struct weston_seat *) seat;
552         struct weston_compositor *ec = ws->compositor;
553         struct weston_output *output;
554         struct wl_listener *listener = NULL;
555         struct weston_recorder *recorder;
556         static const char filename[] = "capture.wcap";
557
558         wl_list_for_each(output, &seat->compositor->output_list, link) {
559                 listener = wl_signal_get(&output->frame_signal,
560                                          weston_recorder_frame_notify);
561                 if (listener)
562                         break;
563         }
564
565         if (listener) {
566                 recorder = container_of(listener, struct weston_recorder,
567                                         frame_listener);
568
569                 weston_log(
570                         "stopping recorder, total file size %dM, %d frames\n",
571                         recorder->total / (1024 * 1024), recorder->count);
572
573                 recorder->destroying = 1;
574                 weston_output_schedule_repaint(recorder->output);
575         } else {
576                 if (seat->keyboard && seat->keyboard->focus &&
577                     seat->keyboard->focus->output)
578                         output = seat->keyboard->focus->output;
579                 else
580                         output = container_of(ec->output_list.next,
581                                               struct weston_output, link);
582
583                 weston_log("starting recorder for output %s, file %s\n",
584                            output->name, filename);
585                 weston_recorder_create(output, filename);
586         }
587 }
588
589 static void
590 screenshooter_destroy(struct wl_listener *listener, void *data)
591 {
592         struct screenshooter *shooter =
593                 container_of(listener, struct screenshooter, destroy_listener);
594
595         wl_global_destroy(shooter->global);
596         free(shooter);
597 }
598
599 WL_EXPORT void
600 screenshooter_create(struct weston_compositor *ec)
601 {
602         struct screenshooter *shooter;
603
604         shooter = malloc(sizeof *shooter);
605         if (shooter == NULL)
606                 return;
607
608         shooter->ec = ec;
609         shooter->client = NULL;
610
611         shooter->global = wl_global_create(ec->wl_display,
612                                            &screenshooter_interface, 1,
613                                            shooter, bind_shooter);
614         weston_compositor_add_key_binding(ec, KEY_S, MODIFIER_SUPER,
615                                           screenshooter_binding, shooter);
616         weston_compositor_add_key_binding(ec, KEY_R, MODIFIER_SUPER,
617                                           recorder_binding, shooter);
618
619         shooter->destroy_listener.notify = screenshooter_destroy;
620         wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);
621 }