blob: ecb8db0a8eca18f57628369df3b95dab03c11cee [file] [log] [blame]
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2008-2011 Kristian Høgsberg
Kristian Høgsbergfc783d42010-06-11 12:56:24 -04003 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04004 * 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.
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040013 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040014 * 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.
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040021 */
22
23#include <stdlib.h>
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -040024#include <stdio.h>
Kristian Høgsbergf02a6492012-03-12 01:05:25 -040025#include <string.h>
Scott Moreau56456d62012-03-23 16:42:04 -060026#include <linux/input.h>
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -040027#include <fcntl.h>
28#include <unistd.h>
29#include <sys/uio.h>
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040030
31#include "compositor.h"
Kristian Høgsbergc5dcb902010-09-14 15:53:32 -040032#include "screenshooter-server-protocol.h"
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040033
Kristian Høgsberg2bcb2fc2012-05-25 18:03:52 -040034#include "../wcap/wcap-decode.h"
Kristian Høgsberg894e0b52012-05-25 17:55:20 -040035
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -040036struct screenshooter {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040037 struct wl_object base;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050038 struct weston_compositor *ec;
Pekka Paalanen35ce06d2012-01-03 11:39:13 +020039 struct wl_global *global;
Scott Moreau56456d62012-03-23 16:42:04 -060040 struct wl_client *client;
Scott Moreau80d27b72012-04-04 11:49:21 -060041 struct weston_process process;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040042 struct wl_listener destroy_listener;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040043};
44
Scott Moreau062be7e2012-04-20 13:37:33 -060045struct screenshooter_read_pixels {
46 struct weston_read_pixels base;
47 struct wl_buffer *buffer;
48 struct wl_resource *resource;
49};
50
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040051static void
Scott Moreau72c23722012-04-20 13:37:34 -060052copy_bgra_yflip(uint8_t *dst, uint8_t *src, int height, int stride)
Pekka Paalanen45fab0e2012-04-17 11:55:41 +030053{
54 uint8_t *end;
55
Scott Moreau72c23722012-04-20 13:37:34 -060056 end = dst + height * stride;
Pekka Paalanen45fab0e2012-04-17 11:55:41 +030057 while (dst < end) {
Scott Moreau72c23722012-04-20 13:37:34 -060058 memcpy(dst, src, stride);
59 dst += stride;
60 src -= stride;
Pekka Paalanen45fab0e2012-04-17 11:55:41 +030061 }
62}
63
64static void
Pekka Paalanena1d57db2012-04-17 15:02:08 +030065copy_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
81static void
Scott Moreau72c23722012-04-20 13:37:34 -060082copy_rgba_yflip(uint8_t *dst, uint8_t *src, int height, int stride)
Pekka Paalanena1d57db2012-04-17 15:02:08 +030083{
84 uint8_t *end;
85
Scott Moreau72c23722012-04-20 13:37:34 -060086 end = dst + height * stride;
Pekka Paalanena1d57db2012-04-17 15:02:08 +030087 while (dst < end) {
Scott Moreau72c23722012-04-20 13:37:34 -060088 copy_row_swap_RB(dst, src, stride);
89 dst += stride;
90 src -= stride;
Pekka Paalanena1d57db2012-04-17 15:02:08 +030091 }
92}
93
94static void
Scott Moreau062be7e2012-04-20 13:37:33 -060095screenshooter_read_pixels_done(struct weston_read_pixels *base,
96 struct weston_output *output)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040097{
Scott Moreau062be7e2012-04-20 13:37:33 -060098 struct screenshooter_read_pixels *r =
99 (struct screenshooter_read_pixels *) base;
Scott Moreau72c23722012-04-20 13:37:34 -0600100 int32_t stride;
Scott Moreau062be7e2012-04-20 13:37:33 -0600101 uint8_t *d, *s;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400102
Scott Moreau72c23722012-04-20 13:37:34 -0600103 stride = wl_shm_buffer_get_stride(r->buffer);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400104
Scott Moreau72c23722012-04-20 13:37:34 -0600105 d = wl_shm_buffer_get_data(r->buffer);
106 s = r->base.data + stride * (r->buffer->height - 1);
Pekka Paalanena1d57db2012-04-17 15:02:08 +0300107
108 switch (output->compositor->read_format) {
109 case GL_BGRA_EXT:
Scott Moreau72c23722012-04-20 13:37:34 -0600110 copy_bgra_yflip(d, s, output->current->height, stride);
Pekka Paalanena1d57db2012-04-17 15:02:08 +0300111 break;
112 case GL_RGBA:
Scott Moreau72c23722012-04-20 13:37:34 -0600113 copy_rgba_yflip(d, s, output->current->height, stride);
Pekka Paalanena1d57db2012-04-17 15:02:08 +0300114 break;
115 default:
116 break;
117 }
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400118
Scott Moreau062be7e2012-04-20 13:37:33 -0600119 wl_list_remove(&r->base.link);
120
121 screenshooter_send_done(r->resource);
122 free(r->base.data);
123 free(r);
124
125}
126
127static void
128screenshooter_shoot(struct wl_client *client,
129 struct wl_resource *resource,
130 struct wl_resource *output_resource,
131 struct wl_resource *buffer_resource)
132{
133 struct weston_output *output = output_resource->data;
134 struct screenshooter_read_pixels *r;
135 struct wl_buffer *buffer = buffer_resource->data;
136 int32_t stride;
137
138 if (!wl_buffer_is_shm(buffer))
139 return;
140
141 if (buffer->width < output->current->width ||
142 buffer->height < output->current->height)
143 return;
144
145 r = malloc(sizeof *r);
146 if (r == NULL) {
147 wl_resource_post_no_memory(resource);
148 return;
149 }
150
151 r->base.x = 0;
152 r->base.y = 0;
153 r->base.width = output->current->width;
154 r->base.height = output->current->height;
155 r->base.done = screenshooter_read_pixels_done;
156 r->buffer = buffer;
157 r->resource = resource;
158 stride = buffer->width * 4;
159 r->base.data = malloc(stride * buffer->height);
160
161 if (r->base.data == NULL) {
162 free(r);
163 wl_resource_post_no_memory(resource);
164 return;
165 }
166
167 wl_list_insert(output->read_pixels_list.prev, &r->base.link);
168 weston_compositor_schedule_repaint(output->compositor);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400169}
170
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400171struct screenshooter_interface screenshooter_implementation = {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400172 screenshooter_shoot
173};
174
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400175static void
176bind_shooter(struct wl_client *client,
177 void *data, uint32_t version, uint32_t id)
178{
Scott Moreau56456d62012-03-23 16:42:04 -0600179 struct screenshooter *shooter = data;
180 struct wl_resource *resource;
181
182 resource = wl_client_add_object(client, &screenshooter_interface,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400183 &screenshooter_implementation, id, data);
Scott Moreau56456d62012-03-23 16:42:04 -0600184
185 if (client != shooter->client) {
186 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
187 "screenshooter failed: permission denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400188 wl_resource_destroy(resource);
Scott Moreau56456d62012-03-23 16:42:04 -0600189 }
190}
191
192static void
193screenshooter_sigchld(struct weston_process *process, int status)
194{
195 struct screenshooter *shooter =
Scott Moreau80d27b72012-04-04 11:49:21 -0600196 container_of(process, struct screenshooter, process);
Scott Moreau56456d62012-03-23 16:42:04 -0600197
198 shooter->client = NULL;
199}
200
201static void
Daniel Stone325fc2d2012-05-30 16:31:58 +0100202screenshooter_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
203 void *data)
Scott Moreau56456d62012-03-23 16:42:04 -0600204{
205 struct screenshooter *shooter = data;
206 const char *screenshooter_exe = LIBEXECDIR "/weston-screenshooter";
207
208 if (!shooter->client)
209 shooter->client = weston_client_launch(shooter->ec,
Scott Moreau80d27b72012-04-04 11:49:21 -0600210 &shooter->process,
Scott Moreau56456d62012-03-23 16:42:04 -0600211 screenshooter_exe, screenshooter_sigchld);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400212}
213
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -0400214struct weston_recorder {
215 struct weston_output *output;
216 uint32_t *frame, *rect;
217 uint32_t total;
218 int fd;
219 struct wl_listener frame_listener;
Kristian Høgsberge012c822012-05-25 17:50:42 -0400220 int count;
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -0400221};
222
223static uint32_t *
224output_run(uint32_t *p, uint32_t delta, int run)
225{
226 int i;
227
228 while (run > 0) {
229 if (run <= 0xe0) {
230 *p++ = delta | ((run - 1) << 24);
231 break;
232 }
233
234 i = 24 - __builtin_clz(run);
235 *p++ = delta | ((i + 0xe0) << 24);
236 run -= 1 << (7 + i);
237 }
238
239 return p;
240}
241
Kristian Høgsberg053be422012-05-29 12:15:47 -0400242static uint32_t
243component_delta(uint32_t next, uint32_t prev)
244{
245 unsigned char dr, dg, db;
246
247 dr = (next >> 16) - (prev >> 16);
248 dg = (next >> 8) - (prev >> 8);
249 db = (next >> 0) - (prev >> 0);
250
251 return (dr << 16) | (dg << 8) | (db << 0);
252}
253
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -0400254static void
255weston_recorder_frame_notify(struct wl_listener *listener, void *data)
256{
257 struct weston_recorder *recorder =
258 container_of(listener, struct weston_recorder, frame_listener);
259 struct weston_output *output = recorder->output;
260 uint32_t msecs = * (uint32_t *) data;
261 pixman_box32_t *r;
262 pixman_region32_t damage;
263 int i, j, k, n, width, height, run, stride;
264 uint32_t delta, prev, *d, *s, *p, next;
265 struct {
266 uint32_t msecs;
267 uint32_t nrects;
268 } header;
269 struct iovec v[2];
270
271 pixman_region32_init(&damage);
272 pixman_region32_intersect(&damage, &recorder->output->region,
273 &recorder->output->previous_damage);
274
275 r = pixman_region32_rectangles(&damage, &n);
276 if (n == 0)
277 return;
Kristian Høgsberge012c822012-05-25 17:50:42 -0400278 if (recorder->count++ == 0)
279 /* The first callback gives us the frame immediately
280 * before the weston_output_damage() call, and
281 * typically doesn't give us a full frame of damage.*/
282 return;
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -0400283
284 header.msecs = msecs;
285 header.nrects = n;
286 v[0].iov_base = &header;
287 v[0].iov_len = sizeof header;
288 v[1].iov_base = r;
289 v[1].iov_len = n * sizeof *r;
290 recorder->total += writev(recorder->fd, v, 2);
291 stride = output->current->width;
292
293 for (i = 0; i < n; i++) {
294 width = r[i].x2 - r[i].x1;
295 height = r[i].y2 - r[i].y1;
296 glReadPixels(r[i].x1, output->current->height - r[i].y2,
297 width, height,
298 output->compositor->read_format,
299 GL_UNSIGNED_BYTE, recorder->rect);
300
301 s = recorder->rect;
302 p = recorder->rect;
303 run = prev = 0; /* quiet gcc */
304 for (j = 0; j < height; j++) {
305 d = recorder->frame +
306 stride * (r[i].y2 - j - 1) + r[i].x1;
307 for (k = 0; k < width; k++) {
308 next = *s++;
Kristian Høgsberg053be422012-05-29 12:15:47 -0400309 delta = component_delta(next, *d);
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -0400310 *d++ = next;
311 if (run == 0 || delta == prev) {
312 run++;
313 } else {
314 p = output_run(p, prev, run);
315 run = 1;
316 prev = delta;
317 }
318 }
319 }
320
321 p = output_run(p, prev, run);
322
323 recorder->total += write(recorder->fd,
324 recorder->rect,
325 (p - recorder->rect) * 4);
326
327#if 0
328 fprintf(stderr,
329 "%dx%d at %d,%d rle from %d to %d bytes (%f) total %dM\n",
330 width, height, r[i].x1, r[i].y1,
Kristian Høgsbergf6f69d32012-06-18 17:10:19 -0400331 width * height * 4, (int) (p - recorder->rect) * 4,
332 (float) (p - recorder->rect) / (width * height),
333 recorder->total / 1024 / 1024);
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -0400334#endif
335 }
336
337 pixman_region32_fini(&damage);
338}
339
340static void
341weston_recorder_create(struct weston_output *output, const char *filename)
342{
343 struct weston_recorder *recorder;
344 int stride, size;
Kristian Høgsberg3b969602012-05-25 17:45:39 -0400345 struct { uint32_t magic, format, width, height; } header;
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -0400346
347 recorder = malloc(sizeof *recorder);
348 recorder->output = output;
349
350 stride = output->current->width;
351 size = stride * 4 * output->current->height;
352 recorder->frame = malloc(size);
353 recorder->rect = malloc(size);
354 recorder->total = 0;
Kristian Høgsberge012c822012-05-25 17:50:42 -0400355 recorder->count = 0;
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -0400356 memset(recorder->frame, 0, size);
357
358 recorder->fd = open(filename,
359 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
360
Kristian Høgsberg3b969602012-05-25 17:45:39 -0400361 header.magic = WCAP_HEADER_MAGIC;
362
363 switch (output->compositor->read_format) {
364 case GL_BGRA_EXT:
365 header.format = WCAP_FORMAT_XRGB8888;
366 break;
367 case GL_RGBA:
368 header.format = WCAP_FORMAT_XBGR8888;
369 break;
370 }
371
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -0400372 header.width = output->current->width;
373 header.height = output->current->height;
374 recorder->total += write(recorder->fd, &header, sizeof header);
375
376 recorder->frame_listener.notify = weston_recorder_frame_notify;
377 wl_signal_add(&output->frame_signal, &recorder->frame_listener);
378 weston_output_damage(output);
379}
380
381static void
382weston_recorder_destroy(struct weston_recorder *recorder)
383{
384 wl_list_remove(&recorder->frame_listener.link);
385 close(recorder->fd);
386 free(recorder->frame);
387 free(recorder->rect);
388 free(recorder);
389}
390
391static void
Daniel Stone325fc2d2012-05-30 16:31:58 +0100392recorder_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -0400393{
394 struct weston_seat *ws = (struct weston_seat *) seat;
395 struct weston_compositor *ec = ws->compositor;
396 struct weston_output *output =
397 container_of(ec->output_list.next,
398 struct weston_output, link);
399 struct wl_listener *listener;
400 struct weston_recorder *recorder;
401 static const char filename[] = "capture.wcap";
402
403 listener = wl_signal_get(&output->frame_signal,
404 weston_recorder_frame_notify);
405 if (listener) {
406 recorder = container_of(listener, struct weston_recorder,
407 frame_listener);
408
Kristian Høgsberge012c822012-05-25 17:50:42 -0400409 fprintf(stderr,
410 "stopping recorder, total file size %dM, %d frames\n",
411 recorder->total / (1024 * 1024), recorder->count);
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -0400412
413 weston_recorder_destroy(recorder);
414 } else {
415 fprintf(stderr, "starting recorder, file %s\n", filename);
416 weston_recorder_create(output, filename);
417 }
418}
419
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400420static void
421screenshooter_destroy(struct wl_listener *listener, void *data)
422{
423 struct screenshooter *shooter =
424 container_of(listener, struct screenshooter, destroy_listener);
425
426 wl_display_remove_global(shooter->ec->wl_display, shooter->global);
427 free(shooter);
428}
429
430void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500431screenshooter_create(struct weston_compositor *ec)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400432{
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400433 struct screenshooter *shooter;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400434
435 shooter = malloc(sizeof *shooter);
436 if (shooter == NULL)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400437 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400438
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400439 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400440 shooter->base.implementation =
441 (void(**)(void)) &screenshooter_implementation;
442 shooter->ec = ec;
Scott Moreau56456d62012-03-23 16:42:04 -0600443 shooter->client = NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400444
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200445 shooter->global = wl_display_add_global(ec->wl_display,
446 &screenshooter_interface,
447 shooter, bind_shooter);
Daniel Stone325fc2d2012-05-30 16:31:58 +0100448 weston_compositor_add_key_binding(ec, KEY_S, MODIFIER_SUPER,
449 screenshooter_binding, shooter);
450 weston_compositor_add_key_binding(ec, KEY_R, MODIFIER_SUPER,
451 recorder_binding, shooter);
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200452
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400453 shooter->destroy_listener.notify = screenshooter_destroy;
454 wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200455}