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 * 4;
107 pixels = malloc(stride * l->buffer->height);
109 if (pixels == NULL) {
110 wl_resource_post_no_memory(l->resource);
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);
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) {
127 copy_bgra_yflip(d, s, output->current->height, stride);
130 copy_rgba_yflip(d, s, output->current->height, stride);
136 screenshooter_send_done(l->resource);
142 screenshooter_shoot(struct wl_client *client,
143 struct wl_resource *resource,
144 struct wl_resource *output_resource,
145 struct wl_resource *buffer_resource)
147 struct weston_output *output = output_resource->data;
148 struct screenshooter_frame_listener *l;
149 struct wl_buffer *buffer = buffer_resource->data;
151 if (!wl_buffer_is_shm(buffer))
154 if (buffer->width < output->current->width ||
155 buffer->height < output->current->height)
158 l = malloc(sizeof *l);
160 wl_resource_post_no_memory(resource);
165 l->resource = resource;
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);
173 struct screenshooter_interface screenshooter_implementation = {
178 bind_shooter(struct wl_client *client,
179 void *data, uint32_t version, uint32_t id)
181 struct screenshooter *shooter = data;
182 struct wl_resource *resource;
184 resource = wl_client_add_object(client, &screenshooter_interface,
185 &screenshooter_implementation, id, data);
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);
195 screenshooter_sigchld(struct weston_process *process, int status)
197 struct screenshooter *shooter =
198 container_of(process, struct screenshooter, process);
200 shooter->client = NULL;
204 screenshooter_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
207 struct screenshooter *shooter = data;
208 const char *screenshooter_exe = LIBEXECDIR "/weston-screenshooter";
210 if (!shooter->client)
211 shooter->client = weston_client_launch(shooter->ec,
213 screenshooter_exe, screenshooter_sigchld);
216 struct weston_recorder {
217 struct weston_output *output;
218 uint32_t *frame, *rect;
221 struct wl_listener frame_listener;
226 output_run(uint32_t *p, uint32_t delta, int run)
232 *p++ = delta | ((run - 1) << 24);
236 i = 24 - __builtin_clz(run);
237 *p++ = delta | ((i + 0xe0) << 24);
245 component_delta(uint32_t next, uint32_t prev)
247 unsigned char dr, dg, db;
249 dr = (next >> 16) - (prev >> 16);
250 dg = (next >> 8) - (prev >> 8);
251 db = (next >> 0) - (prev >> 0);
253 return (dr << 16) | (dg << 8) | (db << 0);
257 weston_recorder_frame_notify(struct wl_listener *listener, void *data)
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;
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;
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];
283 pixman_region32_init(&damage);
284 pixman_region32_intersect(&damage, &output->region, previous_damage);
286 r = pixman_region32_rectangles(&damage, &n);
290 header.msecs = msecs;
292 v[0].iov_base = &header;
293 v[0].iov_len = sizeof header;
295 v[1].iov_len = n * sizeof *r;
296 recorder->total += writev(recorder->fd, v, 2);
297 stride = output->current->width;
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,
304 output->compositor->read_format,
305 GL_UNSIGNED_BYTE, 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++) {
315 delta = component_delta(next, *d);
317 if (run == 0 || delta == prev) {
320 p = output_run(p, prev, run);
327 p = output_run(p, prev, run);
329 recorder->total += write(recorder->fd,
331 (p - recorder->rect) * 4);
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);
343 pixman_region32_fini(&damage);
347 weston_recorder_create(struct weston_output *output, const char *filename)
349 struct weston_recorder *recorder;
351 struct { uint32_t magic, format, width, height; } header;
353 recorder = malloc(sizeof *recorder);
355 stride = output->current->width;
356 size = stride * 4 * output->current->height;
357 recorder->frame = malloc(size);
358 recorder->rect = malloc(size);
361 recorder->output = output;
362 memset(recorder->frame, 0, size);
364 recorder->fd = open(filename,
365 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
367 header.magic = WCAP_HEADER_MAGIC;
369 switch (output->compositor->read_format) {
371 header.format = WCAP_FORMAT_XRGB8888;
374 header.format = WCAP_FORMAT_XBGR8888;
378 header.width = output->current->width;
379 header.height = output->current->height;
380 recorder->total += write(recorder->fd, &header, sizeof header);
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);
389 weston_recorder_destroy(struct weston_recorder *recorder)
391 wl_list_remove(&recorder->frame_listener.link);
393 free(recorder->frame);
394 free(recorder->rect);
395 recorder->output->disable_planes--;
400 recorder_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
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";
411 listener = wl_signal_get(&output->frame_signal,
412 weston_recorder_frame_notify);
414 recorder = container_of(listener, struct weston_recorder,
418 "stopping recorder, total file size %dM, %d frames\n",
419 recorder->total / (1024 * 1024), recorder->count);
421 weston_recorder_destroy(recorder);
423 fprintf(stderr, "starting recorder, file %s\n", filename);
424 weston_recorder_create(output, filename);
429 screenshooter_destroy(struct wl_listener *listener, void *data)
431 struct screenshooter *shooter =
432 container_of(listener, struct screenshooter, destroy_listener);
434 wl_display_remove_global(shooter->ec->wl_display, shooter->global);
439 screenshooter_create(struct weston_compositor *ec)
441 struct screenshooter *shooter;
443 shooter = malloc(sizeof *shooter);
447 shooter->base.interface = &screenshooter_interface;
448 shooter->base.implementation =
449 (void(**)(void)) &screenshooter_implementation;
451 shooter->client = NULL;
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);
461 shooter->destroy_listener.notify = screenshooter_destroy;
462 wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);