screenshooter.c 12.1 KB
Newer Older
1
/*
2
 * Copyright © 2008-2011 Kristian Høgsberg
3
 *
4
5
6
7
8
9
10
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
11
 *
12
13
14
15
16
17
18
19
20
21
22
23
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
24
25
 */

26
27
#include "config.h"

28
#include <stdlib.h>
29
#include <stdint.h>
Kristian Høgsberg's avatar
Kristian Høgsberg committed
30
#include <stdio.h>
31
#include <string.h>
Scott Moreau's avatar
Scott Moreau committed
32
#include <linux/input.h>
Kristian Høgsberg's avatar
Kristian Høgsberg committed
33
34
#include <fcntl.h>
#include <unistd.h>
35
#include <errno.h>
Kristian Høgsberg's avatar
Kristian Høgsberg committed
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
47
struct screenshooter_frame_listener {
	struct wl_listener listener;
48
	struct weston_buffer *buffer;
49
50
	weston_screenshooter_done_func_t done;
	void *data;
51
52
};

53
static void
54
copy_bgra_yflip(uint8_t *dst, uint8_t *src, int height, int stride)
55
56
57
{
	uint8_t *end;

58
	end = dst + height * stride;
59
	while (dst < end) {
60
61
62
		memcpy(dst, src, stride);
		dst += stride;
		src -= stride;
63
64
65
	}
}

66
67
68
69
70
71
72
static void
copy_bgra(uint8_t *dst, uint8_t *src, int height, int stride)
{
	/* TODO: optimize this out */
	memcpy(dst, src, height * stride);
}

73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
static void
copy_row_swap_RB(void *vdst, void *vsrc, int bytes)
{
	uint32_t *dst = vdst;
	uint32_t *src = vsrc;
	uint32_t *end = dst + bytes / 4;

	while (dst < end) {
		uint32_t v = *src++;
		/*                    A R G B */
		uint32_t tmp = v & 0xff00ff00;
		tmp |= (v >> 16) & 0x000000ff;
		tmp |= (v << 16) & 0x00ff0000;
		*dst++ = tmp;
	}
}

static void
91
copy_rgba_yflip(uint8_t *dst, uint8_t *src, int height, int stride)
92
93
94
{
	uint8_t *end;

95
	end = dst + height * stride;
96
	while (dst < end) {
97
98
99
		copy_row_swap_RB(dst, src, stride);
		dst += stride;
		src -= stride;
100
101
102
	}
}

103
104
105
106
107
108
109
110
111
112
113
114
115
static void
copy_rgba(uint8_t *dst, uint8_t *src, int height, int stride)
{
	uint8_t *end;

	end = dst + height * stride;
	while (dst < end) {
		copy_row_swap_RB(dst, src, stride);
		dst += stride;
		src += stride;
	}
}

116
static void
117
screenshooter_frame_notify(struct wl_listener *listener, void *data)
118
{
119
120
121
	struct screenshooter_frame_listener *l =
		container_of(listener,
			     struct screenshooter_frame_listener, listener);
122
	struct weston_output *output = data;
123
	struct weston_compositor *compositor = output->compositor;
124
	int32_t stride;
125
	uint8_t *pixels, *d, *s;
126

127
	output->disable_planes--;
128
	wl_list_remove(&listener->link);
129
	stride = l->buffer->width * (PIXMAN_FORMAT_BPP(compositor->read_format) / 8);
130
	pixels = malloc(stride * l->buffer->height);
131

132
	if (pixels == NULL) {
133
		l->done(l->data, WESTON_SCREENSHOOTER_NO_MEMORY);
134
135
136
137
		free(l);
		return;
	}

138
139
	compositor->renderer->read_pixels(output,
			     compositor->read_format, pixels,
140
141
			     0, 0, output->current_mode->width,
			     output->current_mode->height);
142

143
	stride = wl_shm_buffer_get_stride(l->buffer->shm_buffer);
144

145
	d = wl_shm_buffer_get_data(l->buffer->shm_buffer);
146
	s = pixels + stride * (l->buffer->height - 1);
147

148
149
	wl_shm_buffer_begin_access(l->buffer->shm_buffer);

150
	switch (compositor->read_format) {
151
	case PIXMAN_a8r8g8b8:
152
	case PIXMAN_x8r8g8b8:
153
		if (compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP)
154
			copy_bgra_yflip(d, s, output->current_mode->height, stride);
155
		else
156
			copy_bgra(d, pixels, output->current_mode->height, stride);
157
		break;
158
	case PIXMAN_x8b8g8r8:
159
	case PIXMAN_a8b8g8r8:
160
		if (compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP)
161
			copy_rgba_yflip(d, s, output->current_mode->height, stride);
162
		else
163
			copy_rgba(d, pixels, output->current_mode->height, stride);
164
165
166
167
168
		break;
	default:
		break;
	}

169
170
	wl_shm_buffer_end_access(l->buffer->shm_buffer);

171
	l->done(l->data, WESTON_SCREENSHOOTER_SUCCESS);
172
173
	free(pixels);
	free(l);
174
175
}

176
177
178
179
WL_EXPORT int
weston_screenshooter_shoot(struct weston_output *output,
			   struct weston_buffer *buffer,
			   weston_screenshooter_done_func_t done, void *data)
180
{
181
	struct screenshooter_frame_listener *l;
182

183
184
185
	if (!wl_shm_buffer_get(buffer->resource)) {
		done(data, WESTON_SCREENSHOOTER_BAD_BUFFER);
		return -1;
186
	}
187

188
189
190
	buffer->shm_buffer = wl_shm_buffer_get(buffer->resource);
	buffer->width = wl_shm_buffer_get_width(buffer->shm_buffer);
	buffer->height = wl_shm_buffer_get_height(buffer->shm_buffer);
191

192
	if (buffer->width < output->current_mode->width ||
193
194
195
196
	    buffer->height < output->current_mode->height) {
		done(data, WESTON_SCREENSHOOTER_BAD_BUFFER);
		return -1;
	}
197

198
199
	l = malloc(sizeof *l);
	if (l == NULL) {
200
201
		done(data, WESTON_SCREENSHOOTER_NO_MEMORY);
		return -1;
202
203
	}

204
	l->buffer = buffer;
205
206
	l->done = done;
	l->data = data;
207
208
	l->listener.notify = screenshooter_frame_notify;
	wl_signal_add(&output->frame_signal, &l->listener);
209
	output->disable_planes++;
210
	weston_output_damage(output);
211
212
213
214

	return 0;
}

Kristian Høgsberg's avatar
Kristian Høgsberg committed
215
struct weston_recorder {
216
	struct weston_output *output;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
217
	uint32_t *frame, *rect;
218
	uint32_t *tmpbuf;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
219
220
221
	uint32_t total;
	int fd;
	struct wl_listener frame_listener;
222
	int count, destroying;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
};

static uint32_t *
output_run(uint32_t *p, uint32_t delta, int run)
{
	int i;

	while (run > 0) {
		if (run <= 0xe0) {
			*p++ = delta | ((run - 1) << 24);
			break;
		}

		i = 24 - __builtin_clz(run);
		*p++ = delta | ((i + 0xe0) << 24);
		run -= 1 << (7 + i);
	}

	return p;
}

244
245
246
247
248
249
250
251
252
253
254
255
static uint32_t
component_delta(uint32_t next, uint32_t prev)
{
	unsigned char dr, dg, db;

	dr = (next >> 16) - (prev >> 16);
	dg = (next >>  8) - (prev >>  8);
	db = (next >>  0) - (prev >>  0);

	return (dr << 16) | (dg << 8) | (db << 0);
}

256
257
258
static void
weston_recorder_destroy(struct weston_recorder *recorder);

Kristian Høgsberg's avatar
Kristian Høgsberg committed
259
260
261
262
263
static void
weston_recorder_frame_notify(struct wl_listener *listener, void *data)
{
	struct weston_recorder *recorder =
		container_of(listener, struct weston_recorder, frame_listener);
264
	struct weston_output *output = data;
265
	struct weston_compositor *compositor = output->compositor;
266
	uint32_t msecs = timespec_to_msec(&output->frame_time);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
267
	pixman_box32_t *r;
268
	pixman_region32_t damage, transformed_damage;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
269
270
271
272
273
274
275
	int i, j, k, n, width, height, run, stride;
	uint32_t delta, prev, *d, *s, *p, next;
	struct {
		uint32_t msecs;
		uint32_t nrects;
	} header;
	struct iovec v[2];
276
277
278
279
280
281
282
283
284
	int do_yflip;
	int y_orig;
	uint32_t *outbuf;

	do_yflip = !!(compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP);
	if (do_yflip)
		outbuf = recorder->rect;
	else
		outbuf = recorder->tmpbuf;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
285
286

	pixman_region32_init(&damage);
287
	pixman_region32_init(&transformed_damage);
288
289
	pixman_region32_intersect(&damage, &output->region,
				  &output->previous_damage);
290
291
292
293
294
	pixman_region32_translate(&damage, -output->x, -output->y);
	weston_transformed_region(output->width, output->height,
				 output->transform, output->current_scale,
				 &damage, &transformed_damage);
	pixman_region32_fini(&damage);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
295

296
297
298
	r = pixman_region32_rectangles(&transformed_damage, &n);
	if (n == 0) {
		pixman_region32_fini(&transformed_damage);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
299
		return;
300
	}
301

Kristian Høgsberg's avatar
Kristian Høgsberg committed
302
303
304
305
306
307
308
	header.msecs = msecs;
	header.nrects = n;
	v[0].iov_base = &header;
	v[0].iov_len = sizeof header;
	v[1].iov_base = r;
	v[1].iov_len = n * sizeof *r;
	recorder->total += writev(recorder->fd, v, 2);
309
	stride = output->current_mode->width;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
310
311
312
313

	for (i = 0; i < n; i++) {
		width = r[i].x2 - r[i].x1;
		height = r[i].y2 - r[i].y1;
314
315

		if (do_yflip)
316
			y_orig = output->current_mode->height - r[i].y2;
317
318
319
320
321
322
		else
			y_orig = r[i].y1;

		compositor->renderer->read_pixels(output,
				compositor->read_format, recorder->rect,
				r[i].x1, y_orig, width, height);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
323

324
		p = outbuf;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
325
326
		run = prev = 0; /* quiet gcc */
		for (j = 0; j < height; j++) {
327
			if (do_yflip)
328
				s = recorder->rect + width * j;
329
			else
330
331
				s = recorder->rect + width * (height - j - 1);
			y_orig = r[i].y2 - j - 1;
332
333
			d = recorder->frame + stride * y_orig + r[i].x1;

Kristian Høgsberg's avatar
Kristian Høgsberg committed
334
335
			for (k = 0; k < width; k++) {
				next = *s++;
336
				delta = component_delta(next, *d);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
337
338
339
340
341
342
343
				*d++ = next;
				if (run == 0 || delta == prev) {
					run++;
				} else {
					p = output_run(p, prev, run);
					run = 1;
				}
344
				prev = delta;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
345
346
347
348
349
350
			}
		}

		p = output_run(p, prev, run);

		recorder->total += write(recorder->fd,
351
					 outbuf, (p - outbuf) * 4);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
352
353
354
355
356

#if 0
		fprintf(stderr,
			"%dx%d at %d,%d rle from %d to %d bytes (%f) total %dM\n",
			width, height, r[i].x1, r[i].y1,
357
358
			width * height * 4, (int) (p - outbuf) * 4,
			(float) (p - outbuf) / (width * height),
359
			recorder->total / 1024 / 1024);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
360
361
362
#endif
	}

363
	pixman_region32_fini(&transformed_damage);
364
	recorder->count++;
365
366
367

	if (recorder->destroying)
		weston_recorder_destroy(recorder);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
368
369
}

370
371
372
373
374
static void
weston_recorder_free(struct weston_recorder *recorder)
{
	if (recorder == NULL)
		return;
375

376
	free(recorder->tmpbuf);
377
	free(recorder->rect);
378
379
380
381
	free(recorder->frame);
	free(recorder);
}

382
static struct weston_recorder *
Kristian Høgsberg's avatar
Kristian Høgsberg committed
383
384
weston_recorder_create(struct weston_output *output, const char *filename)
{
385
	struct weston_compositor *compositor = output->compositor;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
386
387
	struct weston_recorder *recorder;
	int stride, size;
388
	struct { uint32_t magic, format, width, height; } header;
389
390
391
	int do_yflip;

	do_yflip = !!(compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
392

393
	recorder = zalloc(sizeof *recorder);
394
395
	if (recorder == NULL) {
		weston_log("%s: out of memory\n", __func__);
396
		return NULL;
397
398
	}

399
400
	stride = output->current_mode->width;
	size = stride * 4 * output->current_mode->height;
Peter Hutterer's avatar
Peter Hutterer committed
401
	recorder->frame = zalloc(size);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
402
	recorder->rect = malloc(size);
403
	recorder->output = output;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
404

405
406
	if ((recorder->frame == NULL) || (recorder->rect == NULL)) {
		weston_log("%s: out of memory\n", __func__);
407
		goto err_recorder;
408
409
	}

410
	if (!do_yflip) {
411
		recorder->tmpbuf = malloc(size);
412
413
414
415
416
		if (recorder->tmpbuf == NULL) {
			weston_log("%s: out of memory\n", __func__);
			goto err_recorder;
		}
	}
417

418
419
	header.magic = WCAP_HEADER_MAGIC;

420
	switch (compositor->read_format) {
421
	case PIXMAN_x8r8g8b8:
422
	case PIXMAN_a8r8g8b8:
423
424
		header.format = WCAP_FORMAT_XRGB8888;
		break;
425
	case PIXMAN_a8b8g8r8:
426
427
		header.format = WCAP_FORMAT_XBGR8888;
		break;
428
429
	default:
		weston_log("unknown recorder format\n");
430
		goto err_recorder;
431
432
	}

433
434
435
436
	recorder->fd = open(filename,
			    O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);

	if (recorder->fd < 0) {
437
438
		weston_log("problem opening output file %s: %s\n", filename,
			   strerror(errno));
439
		goto err_recorder;
440
441
	}

442
443
	header.width = output->current_mode->width;
	header.height = output->current_mode->height;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
444
445
446
447
	recorder->total += write(recorder->fd, &header, sizeof header);

	recorder->frame_listener.notify = weston_recorder_frame_notify;
	wl_signal_add(&output->frame_signal, &recorder->frame_listener);
448
	output->disable_planes++;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
449
	weston_output_damage(output);
450

451
	return recorder;
452
453
454

err_recorder:
	weston_recorder_free(recorder);
455
	return NULL;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
456
457
458
459
460
461
462
}

static void
weston_recorder_destroy(struct weston_recorder *recorder)
{
	wl_list_remove(&recorder->frame_listener.link);
	close(recorder->fd);
463
	recorder->output->disable_planes--;
464
	weston_recorder_free(recorder);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
465
466
}

467
468
WL_EXPORT struct weston_recorder *
weston_recorder_start(struct weston_output *output, const char *filename)
Kristian Høgsberg's avatar
Kristian Høgsberg committed
469
{
470
	struct wl_listener *listener;
471

472
473
	listener = wl_signal_get(&output->frame_signal,
				 weston_recorder_frame_notify);
Kristian Høgsberg's avatar
Kristian Høgsberg committed
474
	if (listener) {
475
476
477
		weston_log("a recorder on output %s is already running\n",
			   output->name);
		return NULL;
Kristian Høgsberg's avatar
Kristian Høgsberg committed
478
479
	}

480
481
482
	weston_log("starting recorder for output %s, file %s\n",
		   output->name, filename);
	return weston_recorder_create(output, filename);
483
484
}

485
WL_EXPORT void
486
weston_recorder_stop(struct weston_recorder *recorder)
487
{
488
489
	weston_log("stopping recorder, total file size %dM, %d frames\n",
		   recorder->total / (1024 * 1024), recorder->count);
490

491
492
	recorder->destroying = 1;
	weston_output_schedule_repaint(recorder->output);
493
}