blob: d9f26ba36cf3448a432771dcb2dc449ff4e9fec0 [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øgsbergcf57dc52011-04-18 10:33:25 -040034struct screenshooter {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040035 struct wl_object base;
Kristian Høgsberg8334bc12012-01-03 10:29:47 -050036 struct weston_compositor *ec;
Pekka Paalanen35ce06d2012-01-03 11:39:13 +020037 struct wl_global *global;
Scott Moreau56456d62012-03-23 16:42:04 -060038 struct wl_client *client;
Scott Moreau80d27b72012-04-04 11:49:21 -060039 struct weston_process process;
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -040040 struct wl_listener destroy_listener;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040041};
42
Scott Moreau062be7e2012-04-20 13:37:33 -060043struct screenshooter_read_pixels {
44 struct weston_read_pixels base;
45 struct wl_buffer *buffer;
46 struct wl_resource *resource;
47};
48
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040049static void
Scott Moreau72c23722012-04-20 13:37:34 -060050copy_bgra_yflip(uint8_t *dst, uint8_t *src, int height, int stride)
Pekka Paalanen45fab0e2012-04-17 11:55:41 +030051{
52 uint8_t *end;
53
Scott Moreau72c23722012-04-20 13:37:34 -060054 end = dst + height * stride;
Pekka Paalanen45fab0e2012-04-17 11:55:41 +030055 while (dst < end) {
Scott Moreau72c23722012-04-20 13:37:34 -060056 memcpy(dst, src, stride);
57 dst += stride;
58 src -= stride;
Pekka Paalanen45fab0e2012-04-17 11:55:41 +030059 }
60}
61
62static void
Pekka Paalanena1d57db2012-04-17 15:02:08 +030063copy_row_swap_RB(void *vdst, void *vsrc, int bytes)
64{
65 uint32_t *dst = vdst;
66 uint32_t *src = vsrc;
67 uint32_t *end = dst + bytes / 4;
68
69 while (dst < end) {
70 uint32_t v = *src++;
71 /* A R G B */
72 uint32_t tmp = v & 0xff00ff00;
73 tmp |= (v >> 16) & 0x000000ff;
74 tmp |= (v << 16) & 0x00ff0000;
75 *dst++ = tmp;
76 }
77}
78
79static void
Scott Moreau72c23722012-04-20 13:37:34 -060080copy_rgba_yflip(uint8_t *dst, uint8_t *src, int height, int stride)
Pekka Paalanena1d57db2012-04-17 15:02:08 +030081{
82 uint8_t *end;
83
Scott Moreau72c23722012-04-20 13:37:34 -060084 end = dst + height * stride;
Pekka Paalanena1d57db2012-04-17 15:02:08 +030085 while (dst < end) {
Scott Moreau72c23722012-04-20 13:37:34 -060086 copy_row_swap_RB(dst, src, stride);
87 dst += stride;
88 src -= stride;
Pekka Paalanena1d57db2012-04-17 15:02:08 +030089 }
90}
91
92static void
Scott Moreau062be7e2012-04-20 13:37:33 -060093screenshooter_read_pixels_done(struct weston_read_pixels *base,
94 struct weston_output *output)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -040095{
Scott Moreau062be7e2012-04-20 13:37:33 -060096 struct screenshooter_read_pixels *r =
97 (struct screenshooter_read_pixels *) base;
Scott Moreau72c23722012-04-20 13:37:34 -060098 int32_t stride;
Scott Moreau062be7e2012-04-20 13:37:33 -060099 uint8_t *d, *s;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400100
Scott Moreau72c23722012-04-20 13:37:34 -0600101 stride = wl_shm_buffer_get_stride(r->buffer);
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400102
Scott Moreau72c23722012-04-20 13:37:34 -0600103 d = wl_shm_buffer_get_data(r->buffer);
104 s = r->base.data + stride * (r->buffer->height - 1);
Pekka Paalanena1d57db2012-04-17 15:02:08 +0300105
106 switch (output->compositor->read_format) {
107 case GL_BGRA_EXT:
Scott Moreau72c23722012-04-20 13:37:34 -0600108 copy_bgra_yflip(d, s, output->current->height, stride);
Pekka Paalanena1d57db2012-04-17 15:02:08 +0300109 break;
110 case GL_RGBA:
Scott Moreau72c23722012-04-20 13:37:34 -0600111 copy_rgba_yflip(d, s, output->current->height, stride);
Pekka Paalanena1d57db2012-04-17 15:02:08 +0300112 break;
113 default:
114 break;
115 }
Kristian Høgsbergf02a6492012-03-12 01:05:25 -0400116
Scott Moreau062be7e2012-04-20 13:37:33 -0600117 wl_list_remove(&r->base.link);
118
119 screenshooter_send_done(r->resource);
120 free(r->base.data);
121 free(r);
122
123}
124
125static void
126screenshooter_shoot(struct wl_client *client,
127 struct wl_resource *resource,
128 struct wl_resource *output_resource,
129 struct wl_resource *buffer_resource)
130{
131 struct weston_output *output = output_resource->data;
132 struct screenshooter_read_pixels *r;
133 struct wl_buffer *buffer = buffer_resource->data;
134 int32_t stride;
135
136 if (!wl_buffer_is_shm(buffer))
137 return;
138
139 if (buffer->width < output->current->width ||
140 buffer->height < output->current->height)
141 return;
142
143 r = malloc(sizeof *r);
144 if (r == NULL) {
145 wl_resource_post_no_memory(resource);
146 return;
147 }
148
149 r->base.x = 0;
150 r->base.y = 0;
151 r->base.width = output->current->width;
152 r->base.height = output->current->height;
153 r->base.done = screenshooter_read_pixels_done;
154 r->buffer = buffer;
155 r->resource = resource;
156 stride = buffer->width * 4;
157 r->base.data = malloc(stride * buffer->height);
158
159 if (r->base.data == NULL) {
160 free(r);
161 wl_resource_post_no_memory(resource);
162 return;
163 }
164
165 wl_list_insert(output->read_pixels_list.prev, &r->base.link);
166 weston_compositor_schedule_repaint(output->compositor);
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400167}
168
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400169struct screenshooter_interface screenshooter_implementation = {
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400170 screenshooter_shoot
171};
172
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400173static void
174bind_shooter(struct wl_client *client,
175 void *data, uint32_t version, uint32_t id)
176{
Scott Moreau56456d62012-03-23 16:42:04 -0600177 struct screenshooter *shooter = data;
178 struct wl_resource *resource;
179
180 resource = wl_client_add_object(client, &screenshooter_interface,
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400181 &screenshooter_implementation, id, data);
Scott Moreau56456d62012-03-23 16:42:04 -0600182
183 if (client != shooter->client) {
184 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
185 "screenshooter failed: permission denied");
Kristian Høgsbergeae5de72012-04-11 22:42:15 -0400186 wl_resource_destroy(resource);
Scott Moreau56456d62012-03-23 16:42:04 -0600187 }
188}
189
190static void
191screenshooter_sigchld(struct weston_process *process, int status)
192{
193 struct screenshooter *shooter =
Scott Moreau80d27b72012-04-04 11:49:21 -0600194 container_of(process, struct screenshooter, process);
Scott Moreau56456d62012-03-23 16:42:04 -0600195
196 shooter->client = NULL;
197}
198
199static void
Daniel Stone37816df2012-05-16 18:45:18 +0100200screenshooter_binding(struct wl_seat *seat, uint32_t time,
Scott Moreau56456d62012-03-23 16:42:04 -0600201 uint32_t key, uint32_t button, uint32_t axis,
202 int32_t state, void *data)
203{
204 struct screenshooter *shooter = data;
205 const char *screenshooter_exe = LIBEXECDIR "/weston-screenshooter";
206
207 if (!shooter->client)
208 shooter->client = weston_client_launch(shooter->ec,
Scott Moreau80d27b72012-04-04 11:49:21 -0600209 &shooter->process,
Scott Moreau56456d62012-03-23 16:42:04 -0600210 screenshooter_exe, screenshooter_sigchld);
Kristian Høgsberg97d44aa2011-08-26 17:21:20 -0400211}
212
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -0400213struct weston_recorder {
214 struct weston_output *output;
215 uint32_t *frame, *rect;
216 uint32_t total;
217 int fd;
218 struct wl_listener frame_listener;
Kristian Høgsberge012c822012-05-25 17:50:42 -0400219 int count;
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -0400220};
221
222static uint32_t *
223output_run(uint32_t *p, uint32_t delta, int run)
224{
225 int i;
226
227 while (run > 0) {
228 if (run <= 0xe0) {
229 *p++ = delta | ((run - 1) << 24);
230 break;
231 }
232
233 i = 24 - __builtin_clz(run);
234 *p++ = delta | ((i + 0xe0) << 24);
235 run -= 1 << (7 + i);
236 }
237
238 return p;
239}
240
241static void
242weston_recorder_frame_notify(struct wl_listener *listener, void *data)
243{
244 struct weston_recorder *recorder =
245 container_of(listener, struct weston_recorder, frame_listener);
246 struct weston_output *output = recorder->output;
247 uint32_t msecs = * (uint32_t *) data;
248 pixman_box32_t *r;
249 pixman_region32_t damage;
250 int i, j, k, n, width, height, run, stride;
251 uint32_t delta, prev, *d, *s, *p, next;
252 struct {
253 uint32_t msecs;
254 uint32_t nrects;
255 } header;
256 struct iovec v[2];
257
258 pixman_region32_init(&damage);
259 pixman_region32_intersect(&damage, &recorder->output->region,
260 &recorder->output->previous_damage);
261
262 r = pixman_region32_rectangles(&damage, &n);
263 if (n == 0)
264 return;
Kristian Høgsberge012c822012-05-25 17:50:42 -0400265 if (recorder->count++ == 0)
266 /* The first callback gives us the frame immediately
267 * before the weston_output_damage() call, and
268 * typically doesn't give us a full frame of damage.*/
269 return;
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -0400270
271 header.msecs = msecs;
272 header.nrects = n;
273 v[0].iov_base = &header;
274 v[0].iov_len = sizeof header;
275 v[1].iov_base = r;
276 v[1].iov_len = n * sizeof *r;
277 recorder->total += writev(recorder->fd, v, 2);
278 stride = output->current->width;
279
280 for (i = 0; i < n; i++) {
281 width = r[i].x2 - r[i].x1;
282 height = r[i].y2 - r[i].y1;
283 glReadPixels(r[i].x1, output->current->height - r[i].y2,
284 width, height,
285 output->compositor->read_format,
286 GL_UNSIGNED_BYTE, recorder->rect);
287
288 s = recorder->rect;
289 p = recorder->rect;
290 run = prev = 0; /* quiet gcc */
291 for (j = 0; j < height; j++) {
292 d = recorder->frame +
293 stride * (r[i].y2 - j - 1) + r[i].x1;
294 for (k = 0; k < width; k++) {
295 next = *s++;
296 delta = (next - *d) & 0x00ffffff;
297 *d++ = next;
298 if (run == 0 || delta == prev) {
299 run++;
300 } else {
301 p = output_run(p, prev, run);
302 run = 1;
303 prev = delta;
304 }
305 }
306 }
307
308 p = output_run(p, prev, run);
309
310 recorder->total += write(recorder->fd,
311 recorder->rect,
312 (p - recorder->rect) * 4);
313
314#if 0
315 fprintf(stderr,
316 "%dx%d at %d,%d rle from %d to %d bytes (%f) total %dM\n",
317 width, height, r[i].x1, r[i].y1,
318 width * height * 4, (int) (p - rect) * 4,
319 (float) (p - rect) / (width * height),
320 total / 1024 / 1024);
321#endif
322 }
323
324 pixman_region32_fini(&damage);
325}
326
Kristian Høgsberg3b969602012-05-25 17:45:39 -0400327#define WCAP_HEADER_MAGIC 0x57434150
328#define WCAP_FORMAT_XRGB8888 0x34325258
329#define WCAP_FORMAT_XBGR8888 0x34324258
330#define WCAP_FORMAT_RGBX8888 0x34325852
331#define WCAP_FORMAT_BGRX8888 0x34325842
332
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -0400333static void
334weston_recorder_create(struct weston_output *output, const char *filename)
335{
336 struct weston_recorder *recorder;
337 int stride, size;
Kristian Høgsberg3b969602012-05-25 17:45:39 -0400338 struct { uint32_t magic, format, width, height; } header;
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -0400339
340 recorder = malloc(sizeof *recorder);
341 recorder->output = output;
342
343 stride = output->current->width;
344 size = stride * 4 * output->current->height;
345 recorder->frame = malloc(size);
346 recorder->rect = malloc(size);
347 recorder->total = 0;
Kristian Høgsberge012c822012-05-25 17:50:42 -0400348 recorder->count = 0;
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -0400349 memset(recorder->frame, 0, size);
350
351 recorder->fd = open(filename,
352 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
353
Kristian Høgsberg3b969602012-05-25 17:45:39 -0400354 header.magic = WCAP_HEADER_MAGIC;
355
356 switch (output->compositor->read_format) {
357 case GL_BGRA_EXT:
358 header.format = WCAP_FORMAT_XRGB8888;
359 break;
360 case GL_RGBA:
361 header.format = WCAP_FORMAT_XBGR8888;
362 break;
363 }
364
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -0400365 header.width = output->current->width;
366 header.height = output->current->height;
367 recorder->total += write(recorder->fd, &header, sizeof header);
368
369 recorder->frame_listener.notify = weston_recorder_frame_notify;
370 wl_signal_add(&output->frame_signal, &recorder->frame_listener);
371 weston_output_damage(output);
372}
373
374static void
375weston_recorder_destroy(struct weston_recorder *recorder)
376{
377 wl_list_remove(&recorder->frame_listener.link);
378 close(recorder->fd);
379 free(recorder->frame);
380 free(recorder->rect);
381 free(recorder);
382}
383
384static void
385recorder_binding(struct wl_seat *seat, uint32_t time,
386 uint32_t key, uint32_t button, uint32_t axis,
387 int32_t state, void *data)
388{
389 struct weston_seat *ws = (struct weston_seat *) seat;
390 struct weston_compositor *ec = ws->compositor;
391 struct weston_output *output =
392 container_of(ec->output_list.next,
393 struct weston_output, link);
394 struct wl_listener *listener;
395 struct weston_recorder *recorder;
396 static const char filename[] = "capture.wcap";
397
398 listener = wl_signal_get(&output->frame_signal,
399 weston_recorder_frame_notify);
400 if (listener) {
401 recorder = container_of(listener, struct weston_recorder,
402 frame_listener);
403
Kristian Høgsberge012c822012-05-25 17:50:42 -0400404 fprintf(stderr,
405 "stopping recorder, total file size %dM, %d frames\n",
406 recorder->total / (1024 * 1024), recorder->count);
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -0400407
408 weston_recorder_destroy(recorder);
409 } else {
410 fprintf(stderr, "starting recorder, file %s\n", filename);
411 weston_recorder_create(output, filename);
412 }
413}
414
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400415static void
416screenshooter_destroy(struct wl_listener *listener, void *data)
417{
418 struct screenshooter *shooter =
419 container_of(listener, struct screenshooter, destroy_listener);
420
421 wl_display_remove_global(shooter->ec->wl_display, shooter->global);
422 free(shooter);
423}
424
425void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500426screenshooter_create(struct weston_compositor *ec)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400427{
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400428 struct screenshooter *shooter;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400429
430 shooter = malloc(sizeof *shooter);
431 if (shooter == NULL)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400432 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400433
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400434 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400435 shooter->base.implementation =
436 (void(**)(void)) &screenshooter_implementation;
437 shooter->ec = ec;
Scott Moreau56456d62012-03-23 16:42:04 -0600438 shooter->client = NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400439
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200440 shooter->global = wl_display_add_global(ec->wl_display,
441 &screenshooter_interface,
442 shooter, bind_shooter);
Scott Moreau56456d62012-03-23 16:42:04 -0600443 weston_compositor_add_binding(ec, KEY_S, 0, 0, MODIFIER_SUPER,
444 screenshooter_binding, shooter);
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -0400445 weston_compositor_add_binding(ec, KEY_R, 0, 0, MODIFIER_SUPER,
446 recorder_binding, shooter);
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200447
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400448 shooter->destroy_listener.notify = screenshooter_destroy;
449 wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200450}