blob: 77d34ea00af6157197651396a44293292a35caec [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;
219};
220
221static uint32_t *
222output_run(uint32_t *p, uint32_t delta, int run)
223{
224 int i;
225
226 while (run > 0) {
227 if (run <= 0xe0) {
228 *p++ = delta | ((run - 1) << 24);
229 break;
230 }
231
232 i = 24 - __builtin_clz(run);
233 *p++ = delta | ((i + 0xe0) << 24);
234 run -= 1 << (7 + i);
235 }
236
237 return p;
238}
239
240static void
241weston_recorder_frame_notify(struct wl_listener *listener, void *data)
242{
243 struct weston_recorder *recorder =
244 container_of(listener, struct weston_recorder, frame_listener);
245 struct weston_output *output = recorder->output;
246 uint32_t msecs = * (uint32_t *) data;
247 pixman_box32_t *r;
248 pixman_region32_t damage;
249 int i, j, k, n, width, height, run, stride;
250 uint32_t delta, prev, *d, *s, *p, next;
251 struct {
252 uint32_t msecs;
253 uint32_t nrects;
254 } header;
255 struct iovec v[2];
256
257 pixman_region32_init(&damage);
258 pixman_region32_intersect(&damage, &recorder->output->region,
259 &recorder->output->previous_damage);
260
261 r = pixman_region32_rectangles(&damage, &n);
262 if (n == 0)
263 return;
264
265 header.msecs = msecs;
266 header.nrects = n;
267 v[0].iov_base = &header;
268 v[0].iov_len = sizeof header;
269 v[1].iov_base = r;
270 v[1].iov_len = n * sizeof *r;
271 recorder->total += writev(recorder->fd, v, 2);
272 stride = output->current->width;
273
274 for (i = 0; i < n; i++) {
275 width = r[i].x2 - r[i].x1;
276 height = r[i].y2 - r[i].y1;
277 glReadPixels(r[i].x1, output->current->height - r[i].y2,
278 width, height,
279 output->compositor->read_format,
280 GL_UNSIGNED_BYTE, recorder->rect);
281
282 s = recorder->rect;
283 p = recorder->rect;
284 run = prev = 0; /* quiet gcc */
285 for (j = 0; j < height; j++) {
286 d = recorder->frame +
287 stride * (r[i].y2 - j - 1) + r[i].x1;
288 for (k = 0; k < width; k++) {
289 next = *s++;
290 delta = (next - *d) & 0x00ffffff;
291 *d++ = next;
292 if (run == 0 || delta == prev) {
293 run++;
294 } else {
295 p = output_run(p, prev, run);
296 run = 1;
297 prev = delta;
298 }
299 }
300 }
301
302 p = output_run(p, prev, run);
303
304 recorder->total += write(recorder->fd,
305 recorder->rect,
306 (p - recorder->rect) * 4);
307
308#if 0
309 fprintf(stderr,
310 "%dx%d at %d,%d rle from %d to %d bytes (%f) total %dM\n",
311 width, height, r[i].x1, r[i].y1,
312 width * height * 4, (int) (p - rect) * 4,
313 (float) (p - rect) / (width * height),
314 total / 1024 / 1024);
315#endif
316 }
317
318 pixman_region32_fini(&damage);
319}
320
321static void
322weston_recorder_create(struct weston_output *output, const char *filename)
323{
324 struct weston_recorder *recorder;
325 int stride, size;
326 struct { uint32_t width, height; } header;
327
328 recorder = malloc(sizeof *recorder);
329 recorder->output = output;
330
331 stride = output->current->width;
332 size = stride * 4 * output->current->height;
333 recorder->frame = malloc(size);
334 recorder->rect = malloc(size);
335 recorder->total = 0;
336 memset(recorder->frame, 0, size);
337
338 recorder->fd = open(filename,
339 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
340
341 header.width = output->current->width;
342 header.height = output->current->height;
343 recorder->total += write(recorder->fd, &header, sizeof header);
344
345 recorder->frame_listener.notify = weston_recorder_frame_notify;
346 wl_signal_add(&output->frame_signal, &recorder->frame_listener);
347 weston_output_damage(output);
348}
349
350static void
351weston_recorder_destroy(struct weston_recorder *recorder)
352{
353 wl_list_remove(&recorder->frame_listener.link);
354 close(recorder->fd);
355 free(recorder->frame);
356 free(recorder->rect);
357 free(recorder);
358}
359
360static void
361recorder_binding(struct wl_seat *seat, uint32_t time,
362 uint32_t key, uint32_t button, uint32_t axis,
363 int32_t state, void *data)
364{
365 struct weston_seat *ws = (struct weston_seat *) seat;
366 struct weston_compositor *ec = ws->compositor;
367 struct weston_output *output =
368 container_of(ec->output_list.next,
369 struct weston_output, link);
370 struct wl_listener *listener;
371 struct weston_recorder *recorder;
372 static const char filename[] = "capture.wcap";
373
374 listener = wl_signal_get(&output->frame_signal,
375 weston_recorder_frame_notify);
376 if (listener) {
377 recorder = container_of(listener, struct weston_recorder,
378 frame_listener);
379
380 fprintf(stderr, "stopping recorder, total file size %dM\n",
381 recorder->total / (1024 * 1024));
382
383 weston_recorder_destroy(recorder);
384 } else {
385 fprintf(stderr, "starting recorder, file %s\n", filename);
386 weston_recorder_create(output, filename);
387 }
388}
389
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400390static void
391screenshooter_destroy(struct wl_listener *listener, void *data)
392{
393 struct screenshooter *shooter =
394 container_of(listener, struct screenshooter, destroy_listener);
395
396 wl_display_remove_global(shooter->ec->wl_display, shooter->global);
397 free(shooter);
398}
399
400void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500401screenshooter_create(struct weston_compositor *ec)
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400402{
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400403 struct screenshooter *shooter;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400404
405 shooter = malloc(sizeof *shooter);
406 if (shooter == NULL)
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400407 return;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400408
Kristian Høgsbergcf57dc52011-04-18 10:33:25 -0400409 shooter->base.interface = &screenshooter_interface;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400410 shooter->base.implementation =
411 (void(**)(void)) &screenshooter_implementation;
412 shooter->ec = ec;
Scott Moreau56456d62012-03-23 16:42:04 -0600413 shooter->client = NULL;
Kristian Høgsbergfc783d42010-06-11 12:56:24 -0400414
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200415 shooter->global = wl_display_add_global(ec->wl_display,
416 &screenshooter_interface,
417 shooter, bind_shooter);
Scott Moreau56456d62012-03-23 16:42:04 -0600418 weston_compositor_add_binding(ec, KEY_S, 0, 0, MODIFIER_SUPER,
419 screenshooter_binding, shooter);
Kristian Høgsberg5fb70bf2012-05-24 12:29:46 -0400420 weston_compositor_add_binding(ec, KEY_R, 0, 0, MODIFIER_SUPER,
421 recorder_binding, shooter);
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200422
Kristian Høgsberg02e79dc2012-04-12 09:55:26 -0400423 shooter->destroy_listener.notify = screenshooter_destroy;
424 wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);
Pekka Paalanen35ce06d2012-01-03 11:39:13 +0200425}