toytoolkit: Don't draw shadows for maximized windows.
[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 weston_recorder_frame_notify(struct wl_listener *listener, void *data)
258 {
259         struct weston_recorder *recorder =
260                 container_of(listener, struct weston_recorder, frame_listener);
261         struct weston_output *output = data;
262         uint32_t msecs = output->frame_time;
263         pixman_box32_t *r;
264         pixman_region32_t damage, *previous_damage;
265         int i, j, k, n, width, height, run, stride;
266         uint32_t delta, prev, *d, *s, *p, next;
267         struct {
268                 uint32_t msecs;
269                 uint32_t nrects;
270         } header;
271         struct iovec v[2];
272
273         /* When recording, this will be exactly the region that was repainted
274          * in this frame. Since overlays are disabled, the whole primary plane
275          * damage is rendered. For the first frame, the whole output will be
276          * damaged and that damage will be added to both buffers causing the
277          * non-current buffer damage to be while output. Rendering will clear
278          * all the damage in the current buffer so in the next frame (when
279          * that is non-current) the only damage left will be the one added
280          * from the primary plane. */
281         previous_damage = &output->buffer_damage[output->current_buffer ^ 1];
282
283         pixman_region32_init(&damage);
284         pixman_region32_intersect(&damage, &output->region, previous_damage);
285
286         r = pixman_region32_rectangles(&damage, &n);
287         if (n == 0)
288                 return;
289
290         header.msecs = msecs;
291         header.nrects = n;
292         v[0].iov_base = &header;
293         v[0].iov_len = sizeof header;
294         v[1].iov_base = r;
295         v[1].iov_len = n * sizeof *r;
296         recorder->total += writev(recorder->fd, v, 2);
297         stride = output->current->width;
298
299         for (i = 0; i < n; i++) {
300                 width = r[i].x2 - r[i].x1;
301                 height = r[i].y2 - r[i].y1;
302                 glReadPixels(r[i].x1, output->current->height - r[i].y2,
303                              width, height,
304                              output->compositor->read_format,
305                              GL_UNSIGNED_BYTE, recorder->rect);
306
307                 s = recorder->rect;
308                 p = recorder->rect;
309                 run = prev = 0; /* quiet gcc */
310                 for (j = 0; j < height; j++) {
311                         d = recorder->frame +
312                                 stride * (r[i].y2 - j - 1) + r[i].x1;
313                         for (k = 0; k < width; k++) {
314                                 next = *s++;
315                                 delta = component_delta(next, *d);
316                                 *d++ = next;
317                                 if (run == 0 || delta == prev) {
318                                         run++;
319                                 } else {
320                                         p = output_run(p, prev, run);
321                                         run = 1;
322                                 }
323                                 prev = delta;
324                         }
325                 }
326
327                 p = output_run(p, prev, run);
328
329                 recorder->total += write(recorder->fd,
330                                          recorder->rect,
331                                          (p - recorder->rect) * 4);
332
333 #if 0
334                 fprintf(stderr,
335                         "%dx%d at %d,%d rle from %d to %d bytes (%f) total %dM\n",
336                         width, height, r[i].x1, r[i].y1,
337                         width * height * 4, (int) (p - recorder->rect) * 4,
338                         (float) (p - recorder->rect) / (width * height),
339                         recorder->total / 1024 / 1024);
340 #endif
341         }
342
343         pixman_region32_fini(&damage);
344 }
345
346 static void
347 weston_recorder_create(struct weston_output *output, const char *filename)
348 {
349         struct weston_recorder *recorder;
350         int stride, size;
351         struct { uint32_t magic, format, width, height; } header;
352
353         recorder = malloc(sizeof *recorder);
354
355         stride = output->current->width;
356         size = stride * 4 * output->current->height;
357         recorder->frame = malloc(size);
358         recorder->rect = malloc(size);
359         recorder->total = 0;
360         recorder->count = 0;
361         recorder->output = output;
362         memset(recorder->frame, 0, size);
363
364         recorder->fd = open(filename,
365                             O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
366
367         header.magic = WCAP_HEADER_MAGIC;
368
369         switch (output->compositor->read_format) {
370         case GL_BGRA_EXT:
371                 header.format = WCAP_FORMAT_XRGB8888;
372                 break;
373         case GL_RGBA:
374                 header.format = WCAP_FORMAT_XBGR8888;
375                 break;
376         }
377
378         header.width = output->current->width;
379         header.height = output->current->height;
380         recorder->total += write(recorder->fd, &header, sizeof header);
381
382         recorder->frame_listener.notify = weston_recorder_frame_notify;
383         wl_signal_add(&output->frame_signal, &recorder->frame_listener);
384         output->disable_planes++;
385         weston_output_damage(output);
386 }
387
388 static void
389 weston_recorder_destroy(struct weston_recorder *recorder)
390 {
391         wl_list_remove(&recorder->frame_listener.link);
392         close(recorder->fd);
393         free(recorder->frame);
394         free(recorder->rect);
395         recorder->output->disable_planes--;
396         free(recorder);
397 }
398
399 static void
400 recorder_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
401 {
402         struct weston_seat *ws = (struct weston_seat *) seat;
403         struct weston_compositor *ec = ws->compositor;
404         struct weston_output *output =
405                 container_of(ec->output_list.next,
406                              struct weston_output, link);
407         struct wl_listener *listener;
408         struct weston_recorder *recorder;
409         static const char filename[] = "capture.wcap";
410
411         listener = wl_signal_get(&output->frame_signal,
412                                  weston_recorder_frame_notify);
413         if (listener) {
414                 recorder = container_of(listener, struct weston_recorder,
415                                         frame_listener);
416
417                 fprintf(stderr,
418                         "stopping recorder, total file size %dM, %d frames\n",
419                         recorder->total / (1024 * 1024), recorder->count);
420
421                 weston_recorder_destroy(recorder);
422         } else {
423                 fprintf(stderr, "starting recorder, file %s\n", filename);
424                 weston_recorder_create(output, filename);
425         }
426 }
427
428 static void
429 screenshooter_destroy(struct wl_listener *listener, void *data)
430 {
431         struct screenshooter *shooter =
432                 container_of(listener, struct screenshooter, destroy_listener);
433
434         wl_display_remove_global(shooter->ec->wl_display, shooter->global);
435         free(shooter);
436 }
437
438 void
439 screenshooter_create(struct weston_compositor *ec)
440 {
441         struct screenshooter *shooter;
442
443         shooter = malloc(sizeof *shooter);
444         if (shooter == NULL)
445                 return;
446
447         shooter->base.interface = &screenshooter_interface;
448         shooter->base.implementation =
449                 (void(**)(void)) &screenshooter_implementation;
450         shooter->ec = ec;
451         shooter->client = NULL;
452
453         shooter->global = wl_display_add_global(ec->wl_display,
454                                                 &screenshooter_interface,
455                                                 shooter, bind_shooter);
456         weston_compositor_add_key_binding(ec, KEY_S, MODIFIER_SUPER,
457                                           screenshooter_binding, shooter);
458         weston_compositor_add_key_binding(ec, KEY_R, MODIFIER_SUPER,
459                                           recorder_binding, shooter);
460
461         shooter->destroy_listener.notify = screenshooter_destroy;
462         wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);
463 }