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_read_pixels {
46 struct weston_read_pixels base;
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_read_pixels_done(struct weston_read_pixels *base,
96 struct weston_output *output)
98 struct screenshooter_read_pixels *r =
99 (struct screenshooter_read_pixels *) base;
103 stride = wl_shm_buffer_get_stride(r->buffer);
105 d = wl_shm_buffer_get_data(r->buffer);
106 s = r->base.data + stride * (r->buffer->height - 1);
108 switch (output->compositor->read_format) {
110 copy_bgra_yflip(d, s, output->current->height, stride);
113 copy_rgba_yflip(d, s, output->current->height, stride);
119 wl_list_remove(&r->base.link);
121 screenshooter_send_done(r->resource);
128 screenshooter_shoot(struct wl_client *client,
129 struct wl_resource *resource,
130 struct wl_resource *output_resource,
131 struct wl_resource *buffer_resource)
133 struct weston_output *output = output_resource->data;
134 struct screenshooter_read_pixels *r;
135 struct wl_buffer *buffer = buffer_resource->data;
138 if (!wl_buffer_is_shm(buffer))
141 if (buffer->width < output->current->width ||
142 buffer->height < output->current->height)
145 r = malloc(sizeof *r);
147 wl_resource_post_no_memory(resource);
153 r->base.width = output->current->width;
154 r->base.height = output->current->height;
155 r->base.done = screenshooter_read_pixels_done;
157 r->resource = resource;
158 stride = buffer->width * 4;
159 r->base.data = malloc(stride * buffer->height);
161 if (r->base.data == NULL) {
163 wl_resource_post_no_memory(resource);
167 wl_list_insert(output->read_pixels_list.prev, &r->base.link);
168 weston_compositor_schedule_repaint(output->compositor);
171 struct screenshooter_interface screenshooter_implementation = {
176 bind_shooter(struct wl_client *client,
177 void *data, uint32_t version, uint32_t id)
179 struct screenshooter *shooter = data;
180 struct wl_resource *resource;
182 resource = wl_client_add_object(client, &screenshooter_interface,
183 &screenshooter_implementation, id, data);
185 if (client != shooter->client) {
186 wl_resource_post_error(resource, WL_DISPLAY_ERROR_INVALID_OBJECT,
187 "screenshooter failed: permission denied");
188 wl_resource_destroy(resource);
193 screenshooter_sigchld(struct weston_process *process, int status)
195 struct screenshooter *shooter =
196 container_of(process, struct screenshooter, process);
198 shooter->client = NULL;
202 screenshooter_binding(struct wl_seat *seat, uint32_t time, uint32_t key,
205 struct screenshooter *shooter = data;
206 const char *screenshooter_exe = LIBEXECDIR "/weston-screenshooter";
208 if (!shooter->client)
209 shooter->client = weston_client_launch(shooter->ec,
211 screenshooter_exe, screenshooter_sigchld);
214 struct weston_recorder {
215 struct weston_output *output;
216 uint32_t *frame, *rect;
219 struct wl_listener frame_listener;
224 output_run(uint32_t *p, uint32_t delta, int run)
230 *p++ = delta | ((run - 1) << 24);
234 i = 24 - __builtin_clz(run);
235 *p++ = delta | ((i + 0xe0) << 24);
243 component_delta(uint32_t next, uint32_t prev)
245 unsigned char dr, dg, db;
247 dr = (next >> 16) - (prev >> 16);
248 dg = (next >> 8) - (prev >> 8);
249 db = (next >> 0) - (prev >> 0);
251 return (dr << 16) | (dg << 8) | (db << 0);
255 weston_recorder_frame_notify(struct wl_listener *listener, void *data)
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;
262 pixman_region32_t damage;
263 int i, j, k, n, width, height, run, stride;
264 uint32_t delta, prev, *d, *s, *p, next;
271 pixman_region32_init(&damage);
272 pixman_region32_intersect(&damage, &recorder->output->region,
273 &recorder->output->previous_damage);
275 r = pixman_region32_rectangles(&damage, &n);
279 header.msecs = msecs;
281 v[0].iov_base = &header;
282 v[0].iov_len = sizeof header;
284 v[1].iov_len = n * sizeof *r;
285 recorder->total += writev(recorder->fd, v, 2);
286 stride = output->current->width;
288 for (i = 0; i < n; i++) {
289 width = r[i].x2 - r[i].x1;
290 height = r[i].y2 - r[i].y1;
291 glReadPixels(r[i].x1, output->current->height - r[i].y2,
293 output->compositor->read_format,
294 GL_UNSIGNED_BYTE, recorder->rect);
298 run = prev = 0; /* quiet gcc */
299 for (j = 0; j < height; j++) {
300 d = recorder->frame +
301 stride * (r[i].y2 - j - 1) + r[i].x1;
302 for (k = 0; k < width; k++) {
304 delta = component_delta(next, *d);
306 if (run == 0 || delta == prev) {
309 p = output_run(p, prev, run);
316 p = output_run(p, prev, run);
318 recorder->total += write(recorder->fd,
320 (p - recorder->rect) * 4);
324 "%dx%d at %d,%d rle from %d to %d bytes (%f) total %dM\n",
325 width, height, r[i].x1, r[i].y1,
326 width * height * 4, (int) (p - recorder->rect) * 4,
327 (float) (p - recorder->rect) / (width * height),
328 recorder->total / 1024 / 1024);
332 pixman_region32_fini(&damage);
336 weston_recorder_create(struct weston_output *output, const char *filename)
338 struct weston_recorder *recorder;
340 struct { uint32_t magic, format, width, height; } header;
342 recorder = malloc(sizeof *recorder);
343 recorder->output = output;
345 stride = output->current->width;
346 size = stride * 4 * output->current->height;
347 recorder->frame = malloc(size);
348 recorder->rect = malloc(size);
351 memset(recorder->frame, 0, size);
353 recorder->fd = open(filename,
354 O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
356 header.magic = WCAP_HEADER_MAGIC;
358 switch (output->compositor->read_format) {
360 header.format = WCAP_FORMAT_XRGB8888;
363 header.format = WCAP_FORMAT_XBGR8888;
367 header.width = output->current->width;
368 header.height = output->current->height;
369 recorder->total += write(recorder->fd, &header, sizeof header);
371 recorder->frame_listener.notify = weston_recorder_frame_notify;
372 wl_signal_add(&output->frame_signal, &recorder->frame_listener);
373 weston_output_damage(output);
377 weston_recorder_destroy(struct weston_recorder *recorder)
379 wl_list_remove(&recorder->frame_listener.link);
381 free(recorder->frame);
382 free(recorder->rect);
387 recorder_binding(struct wl_seat *seat, uint32_t time, uint32_t key, void *data)
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";
398 listener = wl_signal_get(&output->frame_signal,
399 weston_recorder_frame_notify);
401 recorder = container_of(listener, struct weston_recorder,
405 "stopping recorder, total file size %dM, %d frames\n",
406 recorder->total / (1024 * 1024), recorder->count);
408 weston_recorder_destroy(recorder);
410 fprintf(stderr, "starting recorder, file %s\n", filename);
411 weston_recorder_create(output, filename);
416 screenshooter_destroy(struct wl_listener *listener, void *data)
418 struct screenshooter *shooter =
419 container_of(listener, struct screenshooter, destroy_listener);
421 wl_display_remove_global(shooter->ec->wl_display, shooter->global);
426 screenshooter_create(struct weston_compositor *ec)
428 struct screenshooter *shooter;
430 shooter = malloc(sizeof *shooter);
434 shooter->base.interface = &screenshooter_interface;
435 shooter->base.implementation =
436 (void(**)(void)) &screenshooter_implementation;
438 shooter->client = NULL;
440 shooter->global = wl_display_add_global(ec->wl_display,
441 &screenshooter_interface,
442 shooter, bind_shooter);
443 weston_compositor_add_key_binding(ec, KEY_S, MODIFIER_SUPER,
444 screenshooter_binding, shooter);
445 weston_compositor_add_key_binding(ec, KEY_R, MODIFIER_SUPER,
446 recorder_binding, shooter);
448 shooter->destroy_listener.notify = screenshooter_destroy;
449 wl_signal_add(&ec->destroy_signal, &shooter->destroy_listener);