• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2008-2011 Kristian Høgsberg
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
26 #include "config.h"
27 
28 #include <stdlib.h>
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <linux/input.h>
33 #include <fcntl.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <sys/uio.h>
37 
38 #include <libweston/libweston.h>
39 #include "shared/helpers.h"
40 #include "shared/timespec-util.h"
41 #include "backend.h"
42 #include "libweston-internal.h"
43 
44 #include "wcap/wcap-decode.h"
45 
46 struct screenshooter_frame_listener {
47 	struct wl_listener listener;
48 	struct weston_buffer *buffer;
49 	struct weston_output *output;
50 	weston_screenshooter_done_func_t done;
51 	void *data;
52 };
53 
54 static void
copy_bgra_yflip(uint8_t * dst,uint8_t * src,int height,int stride)55 copy_bgra_yflip(uint8_t *dst, uint8_t *src, int height, int stride)
56 {
57 	uint8_t *end;
58 
59 	end = dst + height * stride;
60 	while (dst < end) {
61 		memcpy(dst, src, stride);
62 		dst += stride;
63 		src -= stride;
64 	}
65 }
66 
67 static void
copy_bgra(uint8_t * dst,uint8_t * src,int height,int stride)68 copy_bgra(uint8_t *dst, uint8_t *src, int height, int stride)
69 {
70 	/* TODO: optimize this out */
71 	memcpy(dst, src, height * stride);
72 }
73 
74 static void
copy_row_swap_RB(void * vdst,void * vsrc,int bytes)75 copy_row_swap_RB(void *vdst, void *vsrc, int bytes)
76 {
77 	uint32_t *dst = vdst;
78 	uint32_t *src = vsrc;
79 	uint32_t *end = dst + bytes / 4;
80 
81 	while (dst < end) {
82 		uint32_t v = *src++;
83 		/*                    A R G B */
84 		uint32_t tmp = v & 0xff00ff00;
85 		tmp |= (v >> 16) & 0x000000ff;
86 		tmp |= (v << 16) & 0x00ff0000;
87 		*dst++ = tmp;
88 	}
89 }
90 
91 static void
copy_rgba_yflip(uint8_t * dst,uint8_t * src,int height,int stride)92 copy_rgba_yflip(uint8_t *dst, uint8_t *src, int height, int stride)
93 {
94 	uint8_t *end;
95 
96 	end = dst + height * stride;
97 	while (dst < end) {
98 		copy_row_swap_RB(dst, src, stride);
99 		dst += stride;
100 		src -= stride;
101 	}
102 }
103 
104 static void
copy_rgba(uint8_t * dst,uint8_t * src,int height,int stride)105 copy_rgba(uint8_t *dst, uint8_t *src, int height, int stride)
106 {
107 	uint8_t *end;
108 
109 	end = dst + height * stride;
110 	while (dst < end) {
111 		copy_row_swap_RB(dst, src, stride);
112 		dst += stride;
113 		src += stride;
114 	}
115 }
116 
117 static void
screenshooter_frame_notify(struct wl_listener * listener,void * data)118 screenshooter_frame_notify(struct wl_listener *listener, void *data)
119 {
120 	struct screenshooter_frame_listener *l =
121 		container_of(listener,
122 			     struct screenshooter_frame_listener, listener);
123 	struct weston_output *output = l->output;
124 	struct weston_compositor *compositor = output->compositor;
125 	int32_t stride;
126 	uint8_t *pixels, *d, *s;
127 
128 	weston_output_disable_planes_decr(output);
129 	wl_list_remove(&listener->link);
130 	stride = l->buffer->width * (PIXMAN_FORMAT_BPP(compositor->read_format) / 8);
131 	pixels = malloc(stride * l->buffer->height);
132 
133 	if (pixels == NULL) {
134 		l->done(l->data, WESTON_SCREENSHOOTER_NO_MEMORY);
135 		free(l);
136 		return;
137 	}
138 
139 	compositor->renderer->read_pixels(output,
140 			     compositor->read_format, pixels,
141 			     0, 0, output->current_mode->width,
142 			     output->current_mode->height);
143 
144 	stride = wl_shm_buffer_get_stride(l->buffer->shm_buffer);
145 
146 	d = wl_shm_buffer_get_data(l->buffer->shm_buffer);
147 	s = pixels + stride * (l->buffer->height - 1);
148 
149 	wl_shm_buffer_begin_access(l->buffer->shm_buffer);
150 
151 	switch (compositor->read_format) {
152 	case PIXMAN_a8r8g8b8:
153 	case PIXMAN_x8r8g8b8:
154 		if (compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP)
155 			copy_bgra_yflip(d, s, output->current_mode->height, stride);
156 		else
157 			copy_bgra(d, pixels, output->current_mode->height, stride);
158 		break;
159 	case PIXMAN_x8b8g8r8:
160 	case PIXMAN_a8b8g8r8:
161 		if (compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP)
162 			copy_rgba_yflip(d, s, output->current_mode->height, stride);
163 		else
164 			copy_rgba(d, pixels, output->current_mode->height, stride);
165 		break;
166 	default:
167 		break;
168 	}
169 
170 	wl_shm_buffer_end_access(l->buffer->shm_buffer);
171 
172 	l->done(l->data, WESTON_SCREENSHOOTER_SUCCESS);
173 	free(pixels);
174 	free(l);
175 }
176 
177 WL_EXPORT int
weston_screenshooter_shoot(struct weston_output * output,struct weston_buffer * buffer,weston_screenshooter_done_func_t done,void * data)178 weston_screenshooter_shoot(struct weston_output *output,
179 			   struct weston_buffer *buffer,
180 			   weston_screenshooter_done_func_t done, void *data)
181 {
182 	struct screenshooter_frame_listener *l;
183 
184 	if (!wl_shm_buffer_get(buffer->resource)) {
185 		done(data, WESTON_SCREENSHOOTER_BAD_BUFFER);
186 		return -1;
187 	}
188 
189 	buffer->shm_buffer = wl_shm_buffer_get(buffer->resource);
190 	buffer->width = wl_shm_buffer_get_width(buffer->shm_buffer);
191 	buffer->height = wl_shm_buffer_get_height(buffer->shm_buffer);
192 
193 	if (buffer->width < output->current_mode->width ||
194 	    buffer->height < output->current_mode->height) {
195 		done(data, WESTON_SCREENSHOOTER_BAD_BUFFER);
196 		return -1;
197 	}
198 
199 	l = malloc(sizeof *l);
200 	if (l == NULL) {
201 		done(data, WESTON_SCREENSHOOTER_NO_MEMORY);
202 		return -1;
203 	}
204 
205 	l->buffer = buffer;
206 	l->output = output;
207 	l->done = done;
208 	l->data = data;
209 	l->listener.notify = screenshooter_frame_notify;
210 	wl_signal_add(&output->frame_signal, &l->listener);
211 	weston_output_disable_planes_incr(output);
212 	weston_output_damage(output);
213 
214 	return 0;
215 }
216 
217 struct weston_recorder {
218 	struct weston_output *output;
219 	uint32_t *frame, *rect;
220 	uint32_t *tmpbuf;
221 	uint32_t total;
222 	int fd;
223 	struct wl_listener frame_listener;
224 	int count, destroying;
225 };
226 
227 static uint32_t *
output_run(uint32_t * p,uint32_t delta,int run)228 output_run(uint32_t *p, uint32_t delta, int run)
229 {
230 	int i;
231 
232 	while (run > 0) {
233 		if (run <= 0xe0) {
234 			*p++ = delta | ((run - 1) << 24);
235 			break;
236 		}
237 
238 		i = 24 - __builtin_clz(run);
239 		*p++ = delta | ((i + 0xe0) << 24);
240 		run -= 1 << (7 + i);
241 	}
242 
243 	return p;
244 }
245 
246 static uint32_t
component_delta(uint32_t next,uint32_t prev)247 component_delta(uint32_t next, uint32_t prev)
248 {
249 	unsigned char dr, dg, db;
250 
251 	dr = (next >> 16) - (prev >> 16);
252 	dg = (next >>  8) - (prev >>  8);
253 	db = (next >>  0) - (prev >>  0);
254 
255 	return (dr << 16) | (dg << 8) | (db << 0);
256 }
257 
258 static void
259 weston_recorder_destroy(struct weston_recorder *recorder);
260 
261 static void
weston_recorder_frame_notify(struct wl_listener * listener,void * data)262 weston_recorder_frame_notify(struct wl_listener *listener, void *data)
263 {
264 	struct weston_recorder *recorder =
265 		container_of(listener, struct weston_recorder, frame_listener);
266 	struct weston_output *output = recorder->output;
267 	struct weston_compositor *compositor = output->compositor;
268 	uint32_t msecs = timespec_to_msec(&output->frame_time);
269 	pixman_box32_t *r;
270 	pixman_region32_t damage, transformed_damage;
271 	int i, j, k, n, width, height, run, stride;
272 	uint32_t delta, prev, *d, *s, *p, next;
273 	struct {
274 		uint32_t msecs;
275 		uint32_t nrects;
276 	} header;
277 	struct iovec v[2];
278 	int do_yflip;
279 	int y_orig;
280 	uint32_t *outbuf;
281 
282 	do_yflip = !!(compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP);
283 	if (do_yflip)
284 		outbuf = recorder->rect;
285 	else
286 		outbuf = recorder->tmpbuf;
287 
288 	pixman_region32_init(&damage);
289 	pixman_region32_init(&transformed_damage);
290 	pixman_region32_intersect(&damage, &output->region, data);
291 	pixman_region32_translate(&damage, -output->x, -output->y);
292 	weston_transformed_region(output->width, output->height,
293 				 output->transform, output->current_scale,
294 				 &damage, &transformed_damage);
295 	pixman_region32_fini(&damage);
296 
297 	r = pixman_region32_rectangles(&transformed_damage, &n);
298 	if (n == 0) {
299 		pixman_region32_fini(&transformed_damage);
300 		return;
301 	}
302 
303 	header.msecs = msecs;
304 	header.nrects = n;
305 	v[0].iov_base = &header;
306 	v[0].iov_len = sizeof header;
307 	v[1].iov_base = r;
308 	v[1].iov_len = n * sizeof *r;
309 	recorder->total += writev(recorder->fd, v, 2);
310 	stride = output->current_mode->width;
311 
312 	for (i = 0; i < n; i++) {
313 		width = r[i].x2 - r[i].x1;
314 		height = r[i].y2 - r[i].y1;
315 
316 		if (do_yflip)
317 			y_orig = output->current_mode->height - r[i].y2;
318 		else
319 			y_orig = r[i].y1;
320 
321 		compositor->renderer->read_pixels(output,
322 				compositor->read_format, recorder->rect,
323 				r[i].x1, y_orig, width, height);
324 
325 		p = outbuf;
326 		run = prev = 0; /* quiet gcc */
327 		for (j = 0; j < height; j++) {
328 			if (do_yflip)
329 				s = recorder->rect + width * j;
330 			else
331 				s = recorder->rect + width * (height - j - 1);
332 			y_orig = r[i].y2 - j - 1;
333 			d = recorder->frame + stride * y_orig + r[i].x1;
334 
335 			for (k = 0; k < width; k++) {
336 				next = *s++;
337 				delta = component_delta(next, *d);
338 				*d++ = next;
339 				if (run == 0 || delta == prev) {
340 					run++;
341 				} else {
342 					p = output_run(p, prev, run);
343 					run = 1;
344 				}
345 				prev = delta;
346 			}
347 		}
348 
349 		p = output_run(p, prev, run);
350 
351 		recorder->total += write(recorder->fd,
352 					 outbuf, (p - outbuf) * 4);
353 
354 #if 0
355 		fprintf(stderr,
356 			"%dx%d at %d,%d rle from %d to %d bytes (%f) total %dM\n",
357 			width, height, r[i].x1, r[i].y1,
358 			width * height * 4, (int) (p - outbuf) * 4,
359 			(float) (p - outbuf) / (width * height),
360 			recorder->total / 1024 / 1024);
361 #endif
362 	}
363 
364 	pixman_region32_fini(&transformed_damage);
365 	recorder->count++;
366 
367 	if (recorder->destroying)
368 		weston_recorder_destroy(recorder);
369 }
370 
371 static void
weston_recorder_free(struct weston_recorder * recorder)372 weston_recorder_free(struct weston_recorder *recorder)
373 {
374 	if (recorder == NULL)
375 		return;
376 
377 	free(recorder->tmpbuf);
378 	free(recorder->rect);
379 	free(recorder->frame);
380 	free(recorder);
381 }
382 
383 static struct weston_recorder *
weston_recorder_create(struct weston_output * output,const char * filename)384 weston_recorder_create(struct weston_output *output, const char *filename)
385 {
386 	struct weston_compositor *compositor = output->compositor;
387 	struct weston_recorder *recorder;
388 	int stride, size;
389 	struct { uint32_t magic, format, width, height; } header;
390 	int do_yflip;
391 
392 	do_yflip = !!(compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP);
393 
394 	recorder = zalloc(sizeof *recorder);
395 	if (recorder == NULL) {
396 		weston_log("%s: out of memory\n", __func__);
397 		return NULL;
398 	}
399 
400 	stride = output->current_mode->width;
401 	size = stride * 4 * output->current_mode->height;
402 	recorder->frame = zalloc(size);
403 	recorder->rect = malloc(size);
404 	recorder->output = output;
405 
406 	if ((recorder->frame == NULL) || (recorder->rect == NULL)) {
407 		weston_log("%s: out of memory\n", __func__);
408 		goto err_recorder;
409 	}
410 
411 	if (!do_yflip) {
412 		recorder->tmpbuf = malloc(size);
413 		if (recorder->tmpbuf == NULL) {
414 			weston_log("%s: out of memory\n", __func__);
415 			goto err_recorder;
416 		}
417 	}
418 
419 	header.magic = WCAP_HEADER_MAGIC;
420 
421 	switch (compositor->read_format) {
422 	case PIXMAN_x8r8g8b8:
423 	case PIXMAN_a8r8g8b8:
424 		header.format = WCAP_FORMAT_XRGB8888;
425 		break;
426 	case PIXMAN_a8b8g8r8:
427 		header.format = WCAP_FORMAT_XBGR8888;
428 		break;
429 	default:
430 		weston_log("unknown recorder format\n");
431 		goto err_recorder;
432 	}
433 
434 	recorder->fd = open(filename,
435 			    O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
436 
437 	if (recorder->fd < 0) {
438 		weston_log("problem opening output file %s: %s\n", filename,
439 			   strerror(errno));
440 		goto err_recorder;
441 	}
442 
443 	header.width = output->current_mode->width;
444 	header.height = output->current_mode->height;
445 	recorder->total += write(recorder->fd, &header, sizeof header);
446 
447 	recorder->frame_listener.notify = weston_recorder_frame_notify;
448 	wl_signal_add(&output->frame_signal, &recorder->frame_listener);
449 	weston_output_disable_planes_incr(output);
450 	weston_output_damage(output);
451 
452 	return recorder;
453 
454 err_recorder:
455 	weston_recorder_free(recorder);
456 	return NULL;
457 }
458 
459 static void
weston_recorder_destroy(struct weston_recorder * recorder)460 weston_recorder_destroy(struct weston_recorder *recorder)
461 {
462 	wl_list_remove(&recorder->frame_listener.link);
463 	close(recorder->fd);
464 	weston_output_disable_planes_decr(recorder->output);
465 	weston_recorder_free(recorder);
466 }
467 
468 WL_EXPORT struct weston_recorder *
weston_recorder_start(struct weston_output * output,const char * filename)469 weston_recorder_start(struct weston_output *output, const char *filename)
470 {
471 	struct wl_listener *listener;
472 
473 	listener = wl_signal_get(&output->frame_signal,
474 				 weston_recorder_frame_notify);
475 	if (listener) {
476 		weston_log("a recorder on output %s is already running\n",
477 			   output->name);
478 		return NULL;
479 	}
480 
481 	weston_log("starting recorder for output %s, file %s\n",
482 		   output->name, filename);
483 	return weston_recorder_create(output, filename);
484 }
485 
486 WL_EXPORT void
weston_recorder_stop(struct weston_recorder * recorder)487 weston_recorder_stop(struct weston_recorder *recorder)
488 {
489 	weston_log("stopping recorder, total file size %dM, %d frames\n",
490 		   recorder->total / (1024 * 1024), recorder->count);
491 
492 	recorder->destroying = 1;
493 	weston_output_schedule_repaint(recorder->output);
494 }
495