2 * Copyright © 2008-2011 Kristian Høgsberg
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.
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.
26 #include <linux/input.h>
31 #include "compositor.h"
32 #include "screenshooter-server-protocol.h"
34 #include "../wcap/wcap-decode.h"
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;
45 struct screenshooter_frame_listener {
46 struct wl_listener listener;
47 struct wl_buffer *buffer;
48 struct wl_resource *resource;
52 copy_bgra_yflip(uint8_t *dst, uint8_t *src, int height, int stride)
56 end = dst + height * stride;
58 memcpy(dst, src, stride);
65 copy_row_swap_RB(void *vdst, void *vsrc, int bytes)
69 uint32_t *end = dst + bytes / 4;
74 uint32_t tmp = v & 0xff00ff00;
75 tmp |= (v >> 16) & 0x000000ff;
76 tmp |= (v << 16) & 0x00ff0000;
82 copy_rgba_yflip(uint8_t *dst, uint8_t *src, int height, int stride)
86 end = dst + height * stride;
88 copy_row_swap_RB(dst, src, stride);
95 screenshooter_frame_notify(struct wl_listener *listener, void *data)
97 struct screenshooter_frame_listener *l =
98 container_of(listener,
99 struct screenshooter_frame_listener, listener);
100 struct weston_output *output = data;
102 uint8_t *pixels, *d, *s;
104 output->disable_planes--;
105 wl_list_remove(&listener->link);
106 stride = l->buffer->width * (PIXMAN_FORMAT_BPP(output->compositor->read_format) / 8);
107 pixels = malloc(stride * l->buffer->height);
109 if (pixels == NULL) {
110 wl_resource_post_no_memory(l->resource);
115 output->compositor->renderer->read_pixels(output,
116 output->compositor->read_format, pixels,
117 0, 0, output->current->width,
118 output->current->height);
120 stride = wl_shm_buffer_get_stride(l->buffer);
122 d = wl_shm_buffer_get_data(l->buffer);
123 s = pixels + stride * (l->buffer->height - 1);
125 switch (output->compositor->read_format) {
126 case PIXMAN_a8r8g8b8:
127 case PIXMAN_x8r8g8b8:
128 copy_bgra_yflip(d, s, output->current->height, stride);
130 case PIXMAN_x8b8g8r8:
131 case PIXMAN_a8b8g8r8:
132 copy_rgba_yflip(d, s, output->current->height, stride);
138 screenshooter_send_done(l->resource);
144 screenshooter_shoot(struct wl_client *client,
145 struct wl_resource *resource,
146 struct wl_resource *output_resource,
147 struct wl_resource *buffer_resource)
149 struct weston_output *output = output_resource->data;
150 struct screenshooter_frame_listener *l;
151 struct wl_buffer *buffer = buffer_resource->data;
153 if (!wl_buffer_is_shm(buffer))
156 if (buffer->width < output->current->width ||
157 buffer->height < output->current->height)
160 l = malloc(sizeof *l);
162 wl_resource_post_no_memory(resource);
167 l->resource = resource;
169 l->listener.notify = screenshooter_frame_notify;
170 wl_signal_add(&output->frame_signal, &l->listener);
171 output->disable_planes++;
172 weston_output_schedule_repaint(output);
175 struct screenshooter_interface screenshooter_implementation = {
180 bind_shooter(struct wl_client *client,
181 void *data, uint32_t version, uint32_t id)
183 struct screenshooter *shooter = data;
184 struct wl_resource *resource;
186 resource = wl_client_add_object(client, &screenshooter_interface,
187 &screenshooter_implementation, id, data);
189 if (client != shooter->client) {
190 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
191 "screenshooter failed: permission denied");
192 wl_resource_destroy(resource);
197 screenshooter_sigchld(struct weston_process *process, int status)
199 struct screenshooter *shooter =
200 container_of(process, struct screenshooter, process);
202 shooter->client = NULL;
206 screenshooter_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
209 struct screenshooter *shooter = data;
210 const char *screenshooter_exe = LIBEXECDIR "/weston-screenshooter";
212 if (!shooter->client)
213 shooter->client = weston_client_launch(shooter->ec,
215 screenshooter_exe, screenshooter_sigchld);
218 struct weston_recorder {
219 struct weston_output *output;
220 uint32_t *frame, *rect;
223 struct wl_listener frame_listener;
228 output_run(uint32_t *p, uint32_t delta, int run)
234 *p++ = delta | ((run - 1) << 24);
238 i = 24 - __builtin_clz(run);
239 *p++ = delta | ((i + 0xe0) << 24);
247 component_delta(uint32_t next, uint32_t prev)
249 unsigned char dr, dg, db;
251 dr = (next >> 16) - (prev >> 16);
252 dg = (next >> 8) - (prev >> 8);
253 db = (next >> 0) - (prev >> 0);
255 return (dr << 16) | (dg << 8) | (db << 0);
259 transform_rect(struct weston_output *output, pixman_box32_t *r)
261 pixman_box32_t s = *r;
263 switch(output->transform) {
264 case WL_OUTPUT_TRANSFORM_FLIPPED:
265 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
266 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
267 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
268 s.x1 = output->width - r->x2;
269 s.x2 = output->width - r->x1;
275 switch(output->transform) {
276 case WL_OUTPUT_TRANSFORM_NORMAL:
277 case WL_OUTPUT_TRANSFORM_FLIPPED:
281 case WL_OUTPUT_TRANSFORM_90:
282 case WL_OUTPUT_TRANSFORM_FLIPPED_90:
283 r->x1 = output->current->width - s.y2;
285 r->x2 = output->current->width - s.y1;
288 case WL_OUTPUT_TRANSFORM_180:
289 case WL_OUTPUT_TRANSFORM_FLIPPED_180:
290 r->x1 = output->current->width - s.x2;
291 r->y1 = output->current->height - s.y2;
292 r->x2 = output->current->width - s.x1;
293 r->y2 = output->current->height - s.y1;
295 case WL_OUTPUT_TRANSFORM_270:
296 case WL_OUTPUT_TRANSFORM_FLIPPED_270:
298 r->y1 = output->current->height - s.x2;
300 r->y2 = output->current->height - s.x1;
308 weston_recorder_frame_notify(struct wl_listener *listener, void *data)
310 struct weston_recorder *recorder =
311 container_of(listener, struct weston_recorder, frame_listener);
312 struct weston_output *output = data;
313 uint32_t msecs = output->frame_time;
315 pixman_region32_t damage;
316 int i, j, k, n, width, height, run, stride;
317 uint32_t delta, prev, *d, *s, *p, next;
324 pixman_region32_init(&damage);
325 pixman_region32_intersect(&damage, &output->region,
326 &output->previous_damage);
328 r = pixman_region32_rectangles(&damage, &n);
332 for (i = 0; i < n; i++)
333 transform_rect(output, &r[i]);
335 header.msecs = msecs;
337 v[0].iov_base = &header;
338 v[0].iov_len = sizeof header;
340 v[1].iov_len = n * sizeof *r;
341 recorder->total += writev(recorder->fd, v, 2);
342 stride = output->current->width;
344 for (i = 0; i < n; i++) {
345 width = r[i].x2 - r[i].x1;
346 height = r[i].y2 - r[i].y1;
347 output->compositor->renderer->read_pixels(output,
348 output->compositor->read_format, recorder->rect,
349 r[i].x1, output->current->height - r[i].y2,
354 run = prev = 0; /* quiet gcc */
355 for (j = 0; j < height; j++) {
356 d = recorder->frame +
357 stride * (r[i].y2 - j - 1) + r[i].x1;
358 for (k = 0; k < width; k++) {
360 delta = component_delta(next, *d);
362 if (run == 0 || delta == prev) {
365 p = output_run(p, prev, run);
372 p = output_run(p, prev, run);
374 recorder->total += write(recorder->fd,
376 (p - recorder->rect) * 4);
380 "%dx%d at %d,%d rle from %d to %d bytes (%f) total %dM\n",
381 width, height, r[i].x1, r[i].y1,
382 width * height * 4, (int) (p - recorder->rect) * 4,
383 (float) (p - recorder->rect) / (width * height),
384 recorder->total / 1024 / 1024);
388 pixman_region32_fini(&damage);
392 weston_recorder_create(struct weston_output *output, const char *filename)
394 struct weston_recorder *recorder;
396 struct { uint32_t magic, format, width, height; } header;
398 recorder = malloc(sizeof *recorder);
400 stride = output->current->width;
401 size = stride * 4 * output->current->height;
402 recorder->frame = malloc(size);
403 recorder->rect = malloc(size);
406 recorder->output = output;
407 memset(recorder->frame, 0, size);
409 header.magic = WCAP_HEADER_MAGIC;
411 switch (output->compositor->read_format) {
412 case PIXMAN_a8r8g8b8:
413 header.format = WCAP_FORMAT_XRGB8888;
415 case PIXMAN_a8b8g8r8:
416 header.format = WCAP_FORMAT_XBGR8888;
419 weston_log("unknown recorder format\n");
424 recorder->fd = open(filename,
425 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
427 if (recorder->fd < 0) {
428 weston_log("problem opening output file %s: %m\n", filename);
433 header.width = output->current->width;
434 header.height = output->current->height;
435 recorder->total += write(recorder->fd, &header, sizeof header);
437 recorder->frame_listener.notify = weston_recorder_frame_notify;
438 wl_signal_add(&output->frame_signal, &recorder->frame_listener);
439 output->disable_planes++;
440 weston_output_damage(output);
444 weston_recorder_destroy(struct weston_recorder *recorder)
446 wl_list_remove(&recorder->frame_listener.link);
448 free(recorder->frame);
449 free(recorder->rect);
450 recorder->output->disable_planes--;
455 recorder_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
457 struct weston_seat *ws = (struct weston_seat *) seat;
458 struct weston_compositor *ec = ws->compositor;
459 struct weston_output *output =
460 container_of(ec->output_list.next,
461 struct weston_output, link);
462 struct wl_listener *listener;
463 struct weston_recorder *recorder;
464 static const char filename[] = "capture.wcap";
466 listener = wl_signal_get(&output->frame_signal,
467 weston_recorder_frame_notify);
469 recorder = container_of(listener, struct weston_recorder,
473 "stopping recorder, total file size %dM, %d frames\n",
474 recorder->total / (1024 * 1024), recorder->count);
476 weston_recorder_destroy(recorder);
478 fprintf(stderr, "starting recorder, file %s\n", filename);
479 weston_recorder_create(output, filename);
484 screenshooter_destroy(struct wl_listener *listener, void *data)
486 struct screenshooter *shooter =
487 container_of(listener, struct screenshooter, destroy_listener);
489 wl_display_remove_global(shooter->ec->wl_display, shooter->global);
494 screenshooter_create(struct weston_compositor *ec)
496 struct screenshooter *shooter;
498 shooter = malloc(sizeof *shooter);
502 shooter->base.interface = &screenshooter_interface;
503 shooter->base.implementation =
504 (void(**)(void)) &screenshooter_implementation;
506 shooter->client = NULL;
508 shooter->global = wl_display_add_global(ec->wl_display,
509 &screenshooter_interface,
510 shooter, bind_shooter);
511 weston_compositor_add_key_binding(ec, KEY_S, MODIFIER_SUPER,
512 screenshooter_binding, shooter);
513 weston_compositor_add_key_binding(ec, KEY_R, MODIFIER_SUPER,
514 recorder_binding, shooter);
516 shooter->destroy_listener.notify = screenshooter_destroy;
517 wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);