compositor-drm: Work around page flip not setting tiling mode on BYT
[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 "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         int ret;
291
292         ret = asprintf(&screenshooter_exe, "%s/%s",
293                        weston_config_get_libexec_dir(),
294                        "/weston-screenshooter");
295         if (ret < 0) {
296                 weston_log("Could not construct screenshooter path.\n");
297                 return;
298         }
299
300         if (!shooter->client)
301                 shooter->client = weston_client_launch(shooter->ec,
302                                         &shooter->process,
303                                         screenshooter_exe, screenshooter_sigchld);
304         free(screenshooter_exe);
305 }
306
307 struct weston_recorder {
308         struct weston_output *output;
309         uint32_t *frame, *rect;
310         uint32_t *tmpbuf;
311         uint32_t total;
312         int fd;
313         struct wl_listener frame_listener;
314         int count, destroying;
315 };
316
317 static uint32_t *
318 output_run(uint32_t *p, uint32_t delta, int run)
319 {
320         int i;
321
322         while (run > 0) {
323                 if (run <= 0xe0) {
324                         *p++ = delta | ((run - 1) << 24);
325                         break;
326                 }
327
328                 i = 24 - __builtin_clz(run);
329                 *p++ = delta | ((i + 0xe0) << 24);
330                 run -= 1 << (7 + i);
331         }
332
333         return p;
334 }
335
336 static uint32_t
337 component_delta(uint32_t next, uint32_t prev)
338 {
339         unsigned char dr, dg, db;
340
341         dr = (next >> 16) - (prev >> 16);
342         dg = (next >>  8) - (prev >>  8);
343         db = (next >>  0) - (prev >>  0);
344
345         return (dr << 16) | (dg << 8) | (db << 0);
346 }
347
348 static void
349 weston_recorder_destroy(struct weston_recorder *recorder);
350
351 static void
352 weston_recorder_frame_notify(struct wl_listener *listener, void *data)
353 {
354         struct weston_recorder *recorder =
355                 container_of(listener, struct weston_recorder, frame_listener);
356         struct weston_output *output = data;
357         struct weston_compositor *compositor = output->compositor;
358         uint32_t msecs = output->frame_time;
359         pixman_box32_t *r;
360         pixman_region32_t damage, transformed_damage;
361         int i, j, k, n, width, height, run, stride;
362         uint32_t delta, prev, *d, *s, *p, next;
363         struct {
364                 uint32_t msecs;
365                 uint32_t nrects;
366         } header;
367         struct iovec v[2];
368         int do_yflip;
369         int y_orig;
370         uint32_t *outbuf;
371
372         do_yflip = !!(compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP);
373         if (do_yflip)
374                 outbuf = recorder->rect;
375         else
376                 outbuf = recorder->tmpbuf;
377
378         pixman_region32_init(&damage);
379         pixman_region32_init(&transformed_damage);
380         pixman_region32_intersect(&damage, &output->region,
381                                   &output->previous_damage);
382         pixman_region32_translate(&damage, -output->x, -output->y);
383         weston_transformed_region(output->width, output->height,
384                                  output->transform, output->current_scale,
385                                  &damage, &transformed_damage);
386         pixman_region32_fini(&damage);
387
388         r = pixman_region32_rectangles(&transformed_damage, &n);
389         if (n == 0) {
390                 pixman_region32_fini(&transformed_damage);
391                 return;
392         }
393
394         header.msecs = msecs;
395         header.nrects = n;
396         v[0].iov_base = &header;
397         v[0].iov_len = sizeof header;
398         v[1].iov_base = r;
399         v[1].iov_len = n * sizeof *r;
400         recorder->total += writev(recorder->fd, v, 2);
401         stride = output->current_mode->width;
402
403         for (i = 0; i < n; i++) {
404                 width = r[i].x2 - r[i].x1;
405                 height = r[i].y2 - r[i].y1;
406
407                 if (do_yflip)
408                         y_orig = output->current_mode->height - r[i].y2;
409                 else
410                         y_orig = r[i].y1;
411
412                 compositor->renderer->read_pixels(output,
413                                 compositor->read_format, recorder->rect,
414                                 r[i].x1, y_orig, width, height);
415
416                 s = recorder->rect;
417                 p = outbuf;
418                 run = prev = 0; /* quiet gcc */
419                 for (j = 0; j < height; j++) {
420                         if (do_yflip)
421                                 y_orig = r[i].y2 - j - 1;
422                         else
423                                 y_orig = r[i].y1 + j;
424                         d = recorder->frame + stride * y_orig + r[i].x1;
425
426                         for (k = 0; k < width; k++) {
427                                 next = *s++;
428                                 delta = component_delta(next, *d);
429                                 *d++ = next;
430                                 if (run == 0 || delta == prev) {
431                                         run++;
432                                 } else {
433                                         p = output_run(p, prev, run);
434                                         run = 1;
435                                 }
436                                 prev = delta;
437                         }
438                 }
439
440                 p = output_run(p, prev, run);
441
442                 recorder->total += write(recorder->fd,
443                                          outbuf, (p - outbuf) * 4);
444
445 #if 0
446                 fprintf(stderr,
447                         "%dx%d at %d,%d rle from %d to %d bytes (%f) total %dM\n",
448                         width, height, r[i].x1, r[i].y1,
449                         width * height * 4, (int) (p - outbuf) * 4,
450                         (float) (p - outbuf) / (width * height),
451                         recorder->total / 1024 / 1024);
452 #endif
453         }
454
455         pixman_region32_fini(&transformed_damage);
456         recorder->count++;
457
458         if (recorder->destroying)
459                 weston_recorder_destroy(recorder);
460 }
461
462 static void
463 weston_recorder_free(struct weston_recorder *recorder)
464 {
465         if (recorder == NULL)
466                 return;
467         free(recorder->rect);
468         free(recorder->tmpbuf);
469         free(recorder->frame);
470         free(recorder);
471 }
472
473 static void
474 weston_recorder_create(struct weston_output *output, const char *filename)
475 {
476         struct weston_compositor *compositor = output->compositor;
477         struct weston_recorder *recorder;
478         int stride, size;
479         struct { uint32_t magic, format, width, height; } header;
480         int do_yflip;
481
482         do_yflip = !!(compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP);
483
484         recorder = malloc(sizeof *recorder);
485         if (recorder == NULL) {
486                 weston_log("%s: out of memory\n", __func__);
487                 return;
488         }
489
490         stride = output->current_mode->width;
491         size = stride * 4 * output->current_mode->height;
492         recorder->frame = zalloc(size);
493         recorder->rect = malloc(size);
494         recorder->total = 0;
495         recorder->count = 0;
496         recorder->destroying = 0;
497         recorder->output = output;
498
499         if ((recorder->frame == NULL) || (recorder->rect == NULL)) {
500                 weston_log("%s: out of memory\n", __func__);
501                 weston_recorder_free(recorder);
502                 return;
503         }
504
505         if (do_yflip)
506                 recorder->tmpbuf = NULL;
507         else
508                 recorder->tmpbuf = malloc(size);
509
510         header.magic = WCAP_HEADER_MAGIC;
511
512         switch (compositor->read_format) {
513         case PIXMAN_x8r8g8b8:
514         case PIXMAN_a8r8g8b8:
515                 header.format = WCAP_FORMAT_XRGB8888;
516                 break;
517         case PIXMAN_a8b8g8r8:
518                 header.format = WCAP_FORMAT_XBGR8888;
519                 break;
520         default:
521                 weston_log("unknown recorder format\n");
522                 weston_recorder_free(recorder);
523                 return;
524         }
525
526         recorder->fd = open(filename,
527                             O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
528
529         if (recorder->fd < 0) {
530                 weston_log("problem opening output file %s: %m\n", filename);
531                 weston_recorder_free(recorder);
532                 return;
533         }
534
535         header.width = output->current_mode->width;
536         header.height = output->current_mode->height;
537         recorder->total += write(recorder->fd, &header, sizeof header);
538
539         recorder->frame_listener.notify = weston_recorder_frame_notify;
540         wl_signal_add(&output->frame_signal, &recorder->frame_listener);
541         output->disable_planes++;
542         weston_output_damage(output);
543 }
544
545 static void
546 weston_recorder_destroy(struct weston_recorder *recorder)
547 {
548         wl_list_remove(&recorder->frame_listener.link);
549         close(recorder->fd);
550         recorder->output->disable_planes--;
551         weston_recorder_free(recorder);
552 }
553
554 static void
555 recorder_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
556 {
557         struct weston_seat *ws = (struct weston_seat *) seat;
558         struct weston_compositor *ec = ws->compositor;
559         struct weston_output *output;
560         struct wl_listener *listener = NULL;
561         struct weston_recorder *recorder;
562         static const char filename[] = "capture.wcap";
563
564         wl_list_for_each(output, &seat->compositor->output_list, link) {
565                 listener = wl_signal_get(&output->frame_signal,
566                                          weston_recorder_frame_notify);
567                 if (listener)
568                         break;
569         }
570
571         if (listener) {
572                 recorder = container_of(listener, struct weston_recorder,
573                                         frame_listener);
574
575                 weston_log(
576                         "stopping recorder, total file size %dM, %d frames\n",
577                         recorder->total / (1024 * 1024), recorder->count);
578
579                 recorder->destroying = 1;
580                 weston_output_schedule_repaint(recorder->output);
581         } else {
582                 if (seat->keyboard && seat->keyboard->focus &&
583                     seat->keyboard->focus->output)
584                         output = seat->keyboard->focus->output;
585                 else
586                         output = container_of(ec->output_list.next,
587                                               struct weston_output, link);
588
589                 weston_log("starting recorder for output %s, file %s\n",
590                            output->name, filename);
591                 weston_recorder_create(output, filename);
592         }
593 }
594
595 static void
596 screenshooter_destroy(struct wl_listener *listener, void *data)
597 {
598         struct screenshooter *shooter =
599                 container_of(listener, struct screenshooter, destroy_listener);
600
601         wl_global_destroy(shooter->global);
602         free(shooter);
603 }
604
605 WL_EXPORT void
606 screenshooter_create(struct weston_compositor *ec)
607 {
608         struct screenshooter *shooter;
609
610         shooter = malloc(sizeof *shooter);
611         if (shooter == NULL)
612                 return;
613
614         shooter->ec = ec;
615         shooter->client = NULL;
616
617         shooter->global = wl_global_create(ec->wl_display,
618                                            &screenshooter_interface, 1,
619                                            shooter, bind_shooter);
620         weston_compositor_add_key_binding(ec, KEY_S, MODIFIER_SUPER,
621                                           screenshooter_binding, shooter);
622         weston_compositor_add_key_binding(ec, KEY_R, MODIFIER_SUPER,
623                                           recorder_binding, shooter);
624
625         shooter->destroy_listener.notify = screenshooter_destroy;
626         wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);
627 }