input: Rename weston_device_repick() to weston_seat_repick()
[platform/upstream/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_frame_listener {
46         struct wl_listener listener;
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_frame_notify(struct wl_listener *listener, void *data)
96 {
97         struct screenshooter_frame_listener *l =
98                 container_of(listener,
99                              struct screenshooter_frame_listener, listener);
100         struct weston_output *output = data;
101         int32_t stride;
102         uint8_t *pixels, *d, *s;
103
104         output->disable_planes--;
105         wl_list_remove(&listener->link);
106         stride = l->buffer->width * (PIXMAN_FORMAT_BPP(output->compositor->read_format) / 8);
107         pixels = malloc(stride * l->buffer->height);
108
109         if (pixels == NULL) {
110                 wl_resource_post_no_memory(l->resource);
111                 free(l);
112                 return;
113         }
114
115         output->compositor->renderer->read_pixels(output,
116                              output->compositor->read_format, pixels,
117                              0, 0, output->current->width,
118                              output->current->height);
119
120         stride = wl_shm_buffer_get_stride(l->buffer);
121
122         d = wl_shm_buffer_get_data(l->buffer);
123         s = pixels + stride * (l->buffer->height - 1);
124
125         switch (output->compositor->read_format) {
126         case PIXMAN_a8r8g8b8:
127         case PIXMAN_x8r8g8b8:
128                 copy_bgra_yflip(d, s, output->current->height, stride);
129                 break;
130         case PIXMAN_x8b8g8r8:
131         case PIXMAN_a8b8g8r8:
132                 copy_rgba_yflip(d, s, output->current->height, stride);
133                 break;
134         default:
135                 break;
136         }
137
138         screenshooter_send_done(l->resource);
139         free(pixels);
140         free(l);
141 }
142
143 static void
144 screenshooter_shoot(struct wl_client *client,
145                     struct wl_resource *resource,
146                     struct wl_resource *output_resource,
147                     struct wl_resource *buffer_resource)
148 {
149         struct weston_output *output = output_resource->data;
150         struct screenshooter_frame_listener *l;
151         struct wl_buffer *buffer = buffer_resource->data;
152
153         if (!wl_buffer_is_shm(buffer))
154                 return;
155
156         if (buffer->width < output->current->width ||
157             buffer->height < output->current->height)
158                 return;
159
160         l = malloc(sizeof *l);
161         if (l == NULL) {
162                 wl_resource_post_no_memory(resource);
163                 return;
164         }
165
166         l->buffer = buffer;
167         l->resource = resource;
168
169         l->listener.notify = screenshooter_frame_notify;
170         wl_signal_add(&output->frame_signal, &l->listener);
171         output->disable_planes++;
172         weston_output_schedule_repaint(output);
173 }
174
175 struct screenshooter_interface screenshooter_implementation = {
176         screenshooter_shoot
177 };
178
179 static void
180 bind_shooter(struct wl_client *client,
181              void *data, uint32_t version, uint32_t id)
182 {
183         struct screenshooter *shooter = data;
184         struct wl_resource *resource;
185
186         resource = wl_client_add_object(client, &screenshooter_interface,
187                              &screenshooter_implementation, id, data);
188
189         if (client != shooter->client) {
190                 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
191                                        "screenshooter failed: permission denied");
192                 wl_resource_destroy(resource);
193         }
194 }
195
196 static void
197 screenshooter_sigchld(struct weston_process *process, int status)
198 {
199         struct screenshooter *shooter =
200                 container_of(process, struct screenshooter, process);
201
202         shooter->client = NULL;
203 }
204
205 static void
206 screenshooter_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
207                       void *data)
208 {
209         struct screenshooter *shooter = data;
210         const char *screenshooter_exe = LIBEXECDIR "/weston-screenshooter";
211
212         if (!shooter->client)
213                 shooter->client = weston_client_launch(shooter->ec,
214                                         &shooter->process,
215                                         screenshooter_exe, screenshooter_sigchld);
216 }
217
218 struct weston_recorder {
219         struct weston_output *output;
220         uint32_t *frame, *rect;
221         uint32_t total;
222         int fd;
223         struct wl_listener frame_listener;
224         int count;
225 };
226
227 static uint32_t *
228 output_run(uint32_t *p, uint32_t delta, int run)
229 {
230         int i;
231
232         while (run > 0) {
233                 if (run <= 0xe0) {
234                         *p++ = delta | ((run - 1) << 24);
235                         break;
236                 }
237
238                 i = 24 - __builtin_clz(run);
239                 *p++ = delta | ((i + 0xe0) << 24);
240                 run -= 1 << (7 + i);
241         }
242
243         return p;
244 }
245
246 static uint32_t
247 component_delta(uint32_t next, uint32_t prev)
248 {
249         unsigned char dr, dg, db;
250
251         dr = (next >> 16) - (prev >> 16);
252         dg = (next >>  8) - (prev >>  8);
253         db = (next >>  0) - (prev >>  0);
254
255         return (dr << 16) | (dg << 8) | (db << 0);
256 }
257
258 static void
259 transform_rect(struct weston_output *output, pixman_box32_t *r)
260 {
261         pixman_box32_t s = *r;
262
263         switch(output->transform) {
264         case WL_OUTPUT_TRANSFORM_FLIPPED:
265         case WL_OUTPUT_TRANSFORM_FLIPPED_90:
266         case WL_OUTPUT_TRANSFORM_FLIPPED_180:
267         case WL_OUTPUT_TRANSFORM_FLIPPED_270:
268                 s.x1 = output->width - r->x2;
269                 s.x2 = output->width - r->x1;
270                 break;
271         default:
272                 break;
273         }
274
275         switch(output->transform) {
276         case WL_OUTPUT_TRANSFORM_NORMAL:
277         case WL_OUTPUT_TRANSFORM_FLIPPED:
278                 r->x1 = s.x1;
279                 r->x2 = s.x2;
280                 break;
281         case WL_OUTPUT_TRANSFORM_90:
282         case WL_OUTPUT_TRANSFORM_FLIPPED_90:
283                 r->x1 = output->current->width - s.y2;
284                 r->y1 = s.x1;
285                 r->x2 = output->current->width - s.y1;
286                 r->y2 = s.x2;
287                 break;
288         case WL_OUTPUT_TRANSFORM_180:
289         case WL_OUTPUT_TRANSFORM_FLIPPED_180:
290                 r->x1 = output->current->width - s.x2;
291                 r->y1 = output->current->height - s.y2;
292                 r->x2 = output->current->width - s.x1;
293                 r->y2 = output->current->height - s.y1;
294                 break;
295         case WL_OUTPUT_TRANSFORM_270:
296         case WL_OUTPUT_TRANSFORM_FLIPPED_270:
297                 r->x1 = s.y1; 
298                 r->y1 = output->current->height - s.x2;
299                 r->x2 = s.y2; 
300                 r->y2 = output->current->height - s.x1;
301                 break;
302         default:
303                 break;
304         }
305 }
306
307 static void
308 weston_recorder_frame_notify(struct wl_listener *listener, void *data)
309 {
310         struct weston_recorder *recorder =
311                 container_of(listener, struct weston_recorder, frame_listener);
312         struct weston_output *output = data;
313         uint32_t msecs = output->frame_time;
314         pixman_box32_t *r;
315         pixman_region32_t damage;
316         int i, j, k, n, width, height, run, stride;
317         uint32_t delta, prev, *d, *s, *p, next;
318         struct {
319                 uint32_t msecs;
320                 uint32_t nrects;
321         } header;
322         struct iovec v[2];
323
324         pixman_region32_init(&damage);
325         pixman_region32_intersect(&damage, &output->region,
326                                   &output->previous_damage);
327
328         r = pixman_region32_rectangles(&damage, &n);
329         if (n == 0)
330                 return;
331
332         for (i = 0; i < n; i++)
333                 transform_rect(output, &r[i]);
334
335         header.msecs = msecs;
336         header.nrects = n;
337         v[0].iov_base = &header;
338         v[0].iov_len = sizeof header;
339         v[1].iov_base = r;
340         v[1].iov_len = n * sizeof *r;
341         recorder->total += writev(recorder->fd, v, 2);
342         stride = output->current->width;
343
344         for (i = 0; i < n; i++) {
345                 width = r[i].x2 - r[i].x1;
346                 height = r[i].y2 - r[i].y1;
347                 output->compositor->renderer->read_pixels(output,
348                              output->compositor->read_format, recorder->rect,
349                              r[i].x1, output->current->height - r[i].y2,
350                              width, height);
351
352                 s = recorder->rect;
353                 p = recorder->rect;
354                 run = prev = 0; /* quiet gcc */
355                 for (j = 0; j < height; j++) {
356                         d = recorder->frame +
357                                 stride * (r[i].y2 - j - 1) + r[i].x1;
358                         for (k = 0; k < width; k++) {
359                                 next = *s++;
360                                 delta = component_delta(next, *d);
361                                 *d++ = next;
362                                 if (run == 0 || delta == prev) {
363                                         run++;
364                                 } else {
365                                         p = output_run(p, prev, run);
366                                         run = 1;
367                                 }
368                                 prev = delta;
369                         }
370                 }
371
372                 p = output_run(p, prev, run);
373
374                 recorder->total += write(recorder->fd,
375                                          recorder->rect,
376                                          (p - recorder->rect) * 4);
377
378 #if 0
379                 fprintf(stderr,
380                         "%dx%d at %d,%d rle from %d to %d bytes (%f) total %dM\n",
381                         width, height, r[i].x1, r[i].y1,
382                         width * height * 4, (int) (p - recorder->rect) * 4,
383                         (float) (p - recorder->rect) / (width * height),
384                         recorder->total / 1024 / 1024);
385 #endif
386         }
387
388         pixman_region32_fini(&damage);
389 }
390
391 static void
392 weston_recorder_create(struct weston_output *output, const char *filename)
393 {
394         struct weston_recorder *recorder;
395         int stride, size;
396         struct { uint32_t magic, format, width, height; } header;
397
398         recorder = malloc(sizeof *recorder);
399
400         stride = output->current->width;
401         size = stride * 4 * output->current->height;
402         recorder->frame = malloc(size);
403         recorder->rect = malloc(size);
404         recorder->total = 0;
405         recorder->count = 0;
406         recorder->output = output;
407         memset(recorder->frame, 0, size);
408
409         header.magic = WCAP_HEADER_MAGIC;
410
411         switch (output->compositor->read_format) {
412         case PIXMAN_a8r8g8b8:
413                 header.format = WCAP_FORMAT_XRGB8888;
414                 break;
415         case PIXMAN_a8b8g8r8:
416                 header.format = WCAP_FORMAT_XBGR8888;
417                 break;
418         default:
419                 weston_log("unknown recorder format\n");
420                 free(recorder);
421                 return;
422         }
423
424         recorder->fd = open(filename,
425                             O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
426
427         if (recorder->fd < 0) {
428                 weston_log("problem opening output file %s: %m\n", filename);
429                 free(recorder);
430                 return;
431         }
432
433         header.width = output->current->width;
434         header.height = output->current->height;
435         recorder->total += write(recorder->fd, &header, sizeof header);
436
437         recorder->frame_listener.notify = weston_recorder_frame_notify;
438         wl_signal_add(&output->frame_signal, &recorder->frame_listener);
439         output->disable_planes++;
440         weston_output_damage(output);
441 }
442
443 static void
444 weston_recorder_destroy(struct weston_recorder *recorder)
445 {
446         wl_list_remove(&recorder->frame_listener.link);
447         close(recorder->fd);
448         free(recorder->frame);
449         free(recorder->rect);
450         recorder->output->disable_planes--;
451         free(recorder);
452 }
453
454 static void
455 recorder_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
456 {
457         struct weston_seat *ws = (struct weston_seat *) seat;
458         struct weston_compositor *ec = ws->compositor;
459         struct weston_output *output =
460                 container_of(ec->output_list.next,
461                              struct weston_output, link);
462         struct wl_listener *listener;
463         struct weston_recorder *recorder;
464         static const char filename[] = "capture.wcap";
465
466         listener = wl_signal_get(&output->frame_signal,
467                                  weston_recorder_frame_notify);
468         if (listener) {
469                 recorder = container_of(listener, struct weston_recorder,
470                                         frame_listener);
471
472                 fprintf(stderr,
473                         "stopping recorder, total file size %dM, %d frames\n",
474                         recorder->total / (1024 * 1024), recorder->count);
475
476                 weston_recorder_destroy(recorder);
477         } else {
478                 fprintf(stderr, "starting recorder, file %s\n", filename);
479                 weston_recorder_create(output, filename);
480         }
481 }
482
483 static void
484 screenshooter_destroy(struct wl_listener *listener, void *data)
485 {
486         struct screenshooter *shooter =
487                 container_of(listener, struct screenshooter, destroy_listener);
488
489         wl_display_remove_global(shooter->ec->wl_display, shooter->global);
490         free(shooter);
491 }
492
493 void
494 screenshooter_create(struct weston_compositor *ec)
495 {
496         struct screenshooter *shooter;
497
498         shooter = malloc(sizeof *shooter);
499         if (shooter == NULL)
500                 return;
501
502         shooter->base.interface = &screenshooter_interface;
503         shooter->base.implementation =
504                 (void(**)(void)) &screenshooter_implementation;
505         shooter->ec = ec;
506         shooter->client = NULL;
507
508         shooter->global = wl_display_add_global(ec->wl_display,
509                                                 &screenshooter_interface,
510                                                 shooter, bind_shooter);
511         weston_compositor_add_key_binding(ec, KEY_S, MODIFIER_SUPER,
512                                           screenshooter_binding, shooter);
513         weston_compositor_add_key_binding(ec, KEY_R, MODIFIER_SUPER,
514                                           recorder_binding, shooter);
515
516         shooter->destroy_listener.notify = screenshooter_destroy;
517         wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);
518 }