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