You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
488 lines
12 KiB
488 lines
12 KiB
15 years ago
|
/*
|
||
13 years ago
|
* Copyright © 2008-2011 Kristian Høgsberg
|
||
15 years ago
|
*
|
||
10 years ago
|
* 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:
|
||
15 years ago
|
*
|
||
10 years ago
|
* 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.
|
||
15 years ago
|
*/
|
||
|
|
||
12 years ago
|
#include "config.h"
|
||
|
|
||
15 years ago
|
#include <stdlib.h>
|
||
13 years ago
|
#include <stdio.h>
|
||
13 years ago
|
#include <string.h>
|
||
13 years ago
|
#include <linux/input.h>
|
||
13 years ago
|
#include <fcntl.h>
|
||
|
#include <unistd.h>
|
||
|
#include <sys/uio.h>
|
||
15 years ago
|
|
||
|
#include "compositor.h"
|
||
10 years ago
|
#include "shared/helpers.h"
|
||
15 years ago
|
|
||
10 years ago
|
#include "wcap/wcap-decode.h"
|
||
13 years ago
|
|
||
13 years ago
|
struct screenshooter_frame_listener {
|
||
|
struct wl_listener listener;
|
||
12 years ago
|
struct weston_buffer *buffer;
|
||
11 years ago
|
weston_screenshooter_done_func_t done;
|
||
|
void *data;
|
||
13 years ago
|
};
|
||
|
|
||
13 years ago
|
static void
|
||
13 years ago
|
copy_bgra_yflip(uint8_t *dst, uint8_t *src, int height, int stride)
|
||
13 years ago
|
{
|
||
|
uint8_t *end;
|
||
|
|
||
13 years ago
|
end = dst + height * stride;
|
||
13 years ago
|
while (dst < end) {
|
||
13 years ago
|
memcpy(dst, src, stride);
|
||
|
dst += stride;
|
||
|
src -= stride;
|
||
13 years ago
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
static void
|
||
|
copy_bgra(uint8_t *dst, uint8_t *src, int height, int stride)
|
||
|
{
|
||
|
/* TODO: optimize this out */
|
||
|
memcpy(dst, src, height * stride);
|
||
|
}
|
||
|
|
||
13 years ago
|
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
|
||
13 years ago
|
copy_rgba_yflip(uint8_t *dst, uint8_t *src, int height, int stride)
|
||
13 years ago
|
{
|
||
|
uint8_t *end;
|
||
|
|
||
13 years ago
|
end = dst + height * stride;
|
||
13 years ago
|
while (dst < end) {
|
||
13 years ago
|
copy_row_swap_RB(dst, src, stride);
|
||
|
dst += stride;
|
||
|
src -= stride;
|
||
13 years ago
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
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;
|
||
|
}
|
||
|
}
|
||
|
|
||
13 years ago
|
static void
|
||
13 years ago
|
screenshooter_frame_notify(struct wl_listener *listener, void *data)
|
||
13 years ago
|
{
|
||
13 years ago
|
struct screenshooter_frame_listener *l =
|
||
|
container_of(listener,
|
||
|
struct screenshooter_frame_listener, listener);
|
||
13 years ago
|
struct weston_output *output = data;
|
||
12 years ago
|
struct weston_compositor *compositor = output->compositor;
|
||
13 years ago
|
int32_t stride;
|
||
13 years ago
|
uint8_t *pixels, *d, *s;
|
||
13 years ago
|
|
||
12 years ago
|
output->disable_planes--;
|
||
13 years ago
|
wl_list_remove(&listener->link);
|
||
12 years ago
|
stride = l->buffer->width * (PIXMAN_FORMAT_BPP(compositor->read_format) / 8);
|
||
13 years ago
|
pixels = malloc(stride * l->buffer->height);
|
||
13 years ago
|
|
||
13 years ago
|
if (pixels == NULL) {
|
||
11 years ago
|
l->done(l->data, WESTON_SCREENSHOOTER_NO_MEMORY);
|
||
13 years ago
|
free(l);
|
||
|
return;
|
||
|
}
|
||
|
|
||
12 years ago
|
compositor->renderer->read_pixels(output,
|
||
|
compositor->read_format, pixels,
|
||
11 years ago
|
0, 0, output->current_mode->width,
|
||
|
output->current_mode->height);
|
||
13 years ago
|
|
||
12 years ago
|
stride = wl_shm_buffer_get_stride(l->buffer->shm_buffer);
|
||
13 years ago
|
|
||
12 years ago
|
d = wl_shm_buffer_get_data(l->buffer->shm_buffer);
|
||
13 years ago
|
s = pixels + stride * (l->buffer->height - 1);
|
||
13 years ago
|
|
||
11 years ago
|
wl_shm_buffer_begin_access(l->buffer->shm_buffer);
|
||
|
|
||
12 years ago
|
switch (compositor->read_format) {
|
||
12 years ago
|
case PIXMAN_a8r8g8b8:
|
||
12 years ago
|
case PIXMAN_x8r8g8b8:
|
||
12 years ago
|
if (compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP)
|
||
11 years ago
|
copy_bgra_yflip(d, s, output->current_mode->height, stride);
|
||
12 years ago
|
else
|
||
11 years ago
|
copy_bgra(d, pixels, output->current_mode->height, stride);
|
||
13 years ago
|
break;
|
||
12 years ago
|
case PIXMAN_x8b8g8r8:
|
||
12 years ago
|
case PIXMAN_a8b8g8r8:
|
||
12 years ago
|
if (compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP)
|
||
11 years ago
|
copy_rgba_yflip(d, s, output->current_mode->height, stride);
|
||
12 years ago
|
else
|
||
11 years ago
|
copy_rgba(d, pixels, output->current_mode->height, stride);
|
||
13 years ago
|
break;
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
|
||
11 years ago
|
wl_shm_buffer_end_access(l->buffer->shm_buffer);
|
||
|
|
||
11 years ago
|
l->done(l->data, WESTON_SCREENSHOOTER_SUCCESS);
|
||
13 years ago
|
free(pixels);
|
||
|
free(l);
|
||
13 years ago
|
}
|
||
|
|
||
11 years ago
|
WL_EXPORT int
|
||
|
weston_screenshooter_shoot(struct weston_output *output,
|
||
|
struct weston_buffer *buffer,
|
||
|
weston_screenshooter_done_func_t done, void *data)
|
||
15 years ago
|
{
|
||
13 years ago
|
struct screenshooter_frame_listener *l;
|
||
15 years ago
|
|
||
11 years ago
|
if (!wl_shm_buffer_get(buffer->resource)) {
|
||
|
done(data, WESTON_SCREENSHOOTER_BAD_BUFFER);
|
||
|
return -1;
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
12 years ago
|
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);
|
||
15 years ago
|
|
||
11 years ago
|
if (buffer->width < output->current_mode->width ||
|
||
11 years ago
|
buffer->height < output->current_mode->height) {
|
||
|
done(data, WESTON_SCREENSHOOTER_BAD_BUFFER);
|
||
|
return -1;
|
||
|
}
|
||
15 years ago
|
|
||
13 years ago
|
l = malloc(sizeof *l);
|
||
|
if (l == NULL) {
|
||
11 years ago
|
done(data, WESTON_SCREENSHOOTER_NO_MEMORY);
|
||
|
return -1;
|
||
13 years ago
|
}
|
||
|
|
||
13 years ago
|
l->buffer = buffer;
|
||
11 years ago
|
l->done = done;
|
||
|
l->data = data;
|
||
13 years ago
|
l->listener.notify = screenshooter_frame_notify;
|
||
|
wl_signal_add(&output->frame_signal, &l->listener);
|
||
12 years ago
|
output->disable_planes++;
|
||
13 years ago
|
weston_output_schedule_repaint(output);
|
||
11 years ago
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
13 years ago
|
struct weston_recorder {
|
||
12 years ago
|
struct weston_output *output;
|
||
13 years ago
|
uint32_t *frame, *rect;
|
||
12 years ago
|
uint32_t *tmpbuf;
|
||
13 years ago
|
uint32_t total;
|
||
|
int fd;
|
||
|
struct wl_listener frame_listener;
|
||
11 years ago
|
int count, destroying;
|
||
13 years ago
|
};
|
||
|
|
||
|
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;
|
||
|
}
|
||
|
|
||
13 years ago
|
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);
|
||
|
}
|
||
|
|
||
11 years ago
|
static void
|
||
|
weston_recorder_destroy(struct weston_recorder *recorder);
|
||
|
|
||
13 years ago
|
static void
|
||
|
weston_recorder_frame_notify(struct wl_listener *listener, void *data)
|
||
|
{
|
||
|
struct weston_recorder *recorder =
|
||
|
container_of(listener, struct weston_recorder, frame_listener);
|
||
13 years ago
|
struct weston_output *output = data;
|
||
12 years ago
|
struct weston_compositor *compositor = output->compositor;
|
||
13 years ago
|
uint32_t msecs = output->frame_time;
|
||
13 years ago
|
pixman_box32_t *r;
|
||
11 years ago
|
pixman_region32_t damage, transformed_damage;
|
||
13 years ago
|
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];
|
||
12 years ago
|
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;
|
||
13 years ago
|
|
||
|
pixman_region32_init(&damage);
|
||
11 years ago
|
pixman_region32_init(&transformed_damage);
|
||
12 years ago
|
pixman_region32_intersect(&damage, &output->region,
|
||
|
&output->previous_damage);
|
||
11 years ago
|
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);
|
||
13 years ago
|
|
||
11 years ago
|
r = pixman_region32_rectangles(&transformed_damage, &n);
|
||
|
if (n == 0) {
|
||
|
pixman_region32_fini(&transformed_damage);
|
||
13 years ago
|
return;
|
||
11 years ago
|
}
|
||
12 years ago
|
|
||
13 years ago
|
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);
|
||
11 years ago
|
stride = output->current_mode->width;
|
||
13 years ago
|
|
||
|
for (i = 0; i < n; i++) {
|
||
|
width = r[i].x2 - r[i].x1;
|
||
|
height = r[i].y2 - r[i].y1;
|
||
12 years ago
|
|
||
|
if (do_yflip)
|
||
11 years ago
|
y_orig = output->current_mode->height - r[i].y2;
|
||
12 years ago
|
else
|
||
|
y_orig = r[i].y1;
|
||
|
|
||
|
compositor->renderer->read_pixels(output,
|
||
|
compositor->read_format, recorder->rect,
|
||
|
r[i].x1, y_orig, width, height);
|
||
13 years ago
|
|
||
12 years ago
|
p = outbuf;
|
||
13 years ago
|
run = prev = 0; /* quiet gcc */
|
||
|
for (j = 0; j < height; j++) {
|
||
12 years ago
|
if (do_yflip)
|
||
10 years ago
|
s = recorder->rect + width * j;
|
||
12 years ago
|
else
|
||
10 years ago
|
s = recorder->rect + width * (height - j - 1);
|
||
|
y_orig = r[i].y2 - j - 1;
|
||
12 years ago
|
d = recorder->frame + stride * y_orig + r[i].x1;
|
||
|
|
||
13 years ago
|
for (k = 0; k < width; k++) {
|
||
|
next = *s++;
|
||
13 years ago
|
delta = component_delta(next, *d);
|
||
13 years ago
|
*d++ = next;
|
||
|
if (run == 0 || delta == prev) {
|
||
|
run++;
|
||
|
} else {
|
||
|
p = output_run(p, prev, run);
|
||
|
run = 1;
|
||
|
}
|
||
13 years ago
|
prev = delta;
|
||
13 years ago
|
}
|
||
|
}
|
||
|
|
||
|
p = output_run(p, prev, run);
|
||
|
|
||
|
recorder->total += write(recorder->fd,
|
||
12 years ago
|
outbuf, (p - outbuf) * 4);
|
||
13 years ago
|
|
||
|
#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,
|
||
12 years ago
|
width * height * 4, (int) (p - outbuf) * 4,
|
||
|
(float) (p - outbuf) / (width * height),
|
||
13 years ago
|
recorder->total / 1024 / 1024);
|
||
13 years ago
|
#endif
|
||
|
}
|
||
|
|
||
11 years ago
|
pixman_region32_fini(&transformed_damage);
|
||
12 years ago
|
recorder->count++;
|
||
11 years ago
|
|
||
|
if (recorder->destroying)
|
||
|
weston_recorder_destroy(recorder);
|
||
13 years ago
|
}
|
||
|
|
||
11 years ago
|
static void
|
||
|
weston_recorder_free(struct weston_recorder *recorder)
|
||
|
{
|
||
|
if (recorder == NULL)
|
||
|
return;
|
||
10 years ago
|
|
||
11 years ago
|
free(recorder->tmpbuf);
|
||
10 years ago
|
free(recorder->rect);
|
||
11 years ago
|
free(recorder->frame);
|
||
|
free(recorder);
|
||
|
}
|
||
|
|
||
9 years ago
|
static struct weston_recorder *
|
||
13 years ago
|
weston_recorder_create(struct weston_output *output, const char *filename)
|
||
|
{
|
||
12 years ago
|
struct weston_compositor *compositor = output->compositor;
|
||
13 years ago
|
struct weston_recorder *recorder;
|
||
|
int stride, size;
|
||
13 years ago
|
struct { uint32_t magic, format, width, height; } header;
|
||
12 years ago
|
int do_yflip;
|
||
|
|
||
|
do_yflip = !!(compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP);
|
||
13 years ago
|
|
||
10 years ago
|
recorder = zalloc(sizeof *recorder);
|
||
11 years ago
|
if (recorder == NULL) {
|
||
|
weston_log("%s: out of memory\n", __func__);
|
||
9 years ago
|
return NULL;
|
||
11 years ago
|
}
|
||
|
|
||
11 years ago
|
stride = output->current_mode->width;
|
||
|
size = stride * 4 * output->current_mode->height;
|
||
11 years ago
|
recorder->frame = zalloc(size);
|
||
13 years ago
|
recorder->rect = malloc(size);
|
||
12 years ago
|
recorder->output = output;
|
||
13 years ago
|
|
||
11 years ago
|
if ((recorder->frame == NULL) || (recorder->rect == NULL)) {
|
||
|
weston_log("%s: out of memory\n", __func__);
|
||
10 years ago
|
goto err_recorder;
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
if (!do_yflip) {
|
||
12 years ago
|
recorder->tmpbuf = malloc(size);
|
||
10 years ago
|
if (recorder->tmpbuf == NULL) {
|
||
|
weston_log("%s: out of memory\n", __func__);
|
||
|
goto err_recorder;
|
||
|
}
|
||
|
}
|
||
12 years ago
|
|
||
13 years ago
|
header.magic = WCAP_HEADER_MAGIC;
|
||
|
|
||
12 years ago
|
switch (compositor->read_format) {
|
||
12 years ago
|
case PIXMAN_x8r8g8b8:
|
||
12 years ago
|
case PIXMAN_a8r8g8b8:
|
||
13 years ago
|
header.format = WCAP_FORMAT_XRGB8888;
|
||
|
break;
|
||
12 years ago
|
case PIXMAN_a8b8g8r8:
|
||
13 years ago
|
header.format = WCAP_FORMAT_XBGR8888;
|
||
|
break;
|
||
12 years ago
|
default:
|
||
|
weston_log("unknown recorder format\n");
|
||
10 years ago
|
goto err_recorder;
|
||
13 years ago
|
}
|
||
|
|
||
12 years ago
|
recorder->fd = open(filename,
|
||
|
O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, 0644);
|
||
|
|
||
|
if (recorder->fd < 0) {
|
||
|
weston_log("problem opening output file %s: %m\n", filename);
|
||
10 years ago
|
goto err_recorder;
|
||
12 years ago
|
}
|
||
|
|
||
11 years ago
|
header.width = output->current_mode->width;
|
||
|
header.height = output->current_mode->height;
|
||
13 years ago
|
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);
|
||
12 years ago
|
output->disable_planes++;
|
||
13 years ago
|
weston_output_damage(output);
|
||
10 years ago
|
|
||
9 years ago
|
return recorder;
|
||
10 years ago
|
|
||
|
err_recorder:
|
||
|
weston_recorder_free(recorder);
|
||
9 years ago
|
return NULL;
|
||
13 years ago
|
}
|
||
|
|
||
|
static void
|
||
|
weston_recorder_destroy(struct weston_recorder *recorder)
|
||
|
{
|
||
|
wl_list_remove(&recorder->frame_listener.link);
|
||
|
close(recorder->fd);
|
||
12 years ago
|
recorder->output->disable_planes--;
|
||
11 years ago
|
weston_recorder_free(recorder);
|
||
13 years ago
|
}
|
||
|
|
||
9 years ago
|
WL_EXPORT struct weston_recorder *
|
||
|
weston_recorder_start(struct weston_output *output, const char *filename)
|
||
13 years ago
|
{
|
||
9 years ago
|
struct wl_listener *listener;
|
||
11 years ago
|
|
||
9 years ago
|
listener = wl_signal_get(&output->frame_signal,
|
||
|
weston_recorder_frame_notify);
|
||
13 years ago
|
if (listener) {
|
||
9 years ago
|
weston_log("a recorder on output %s is already running\n",
|
||
|
output->name);
|
||
|
return NULL;
|
||
13 years ago
|
}
|
||
|
|
||
9 years ago
|
weston_log("starting recorder for output %s, file %s\n",
|
||
|
output->name, filename);
|
||
|
return weston_recorder_create(output, filename);
|
||
13 years ago
|
}
|
||
|
|
||
11 years ago
|
WL_EXPORT void
|
||
9 years ago
|
weston_recorder_stop(struct weston_recorder *recorder)
|
||
15 years ago
|
{
|
||
9 years ago
|
weston_log("stopping recorder, total file size %dM, %d frames\n",
|
||
|
recorder->total / (1024 * 1024), recorder->count);
|
||
13 years ago
|
|
||
9 years ago
|
recorder->destroying = 1;
|
||
|
weston_output_schedule_repaint(recorder->output);
|
||
13 years ago
|
}
|