screenshooter: Properly handle multiple outputs.

Scott Moreau 13 years ago committed by Kristian Høgsberg
parent d081020435
commit 80d27b7e06
  1. 69
      clients/screenshot.c
  2. 15
      src/compositor-drm.c
  3. 15
      src/compositor-wayland.c
  4. 15
      src/compositor-x11.c
  5. 1
      src/compositor.h
  6. 29
      src/screenshooter.c

@ -32,14 +32,22 @@
#include <wayland-client.h> #include <wayland-client.h>
#include "screenshooter-client-protocol.h" #include "screenshooter-client-protocol.h"
#define MAX(X,Y) ((X) > (Y) ? (X) : (Y))
/* The screenshooter is a good example of a custom object exposed by /* The screenshooter is a good example of a custom object exposed by
* the compositor and serves as a test bed for implementing client * the compositor and serves as a test bed for implementing client
* side marshalling outside libwayland.so */ * side marshalling outside libwayland.so */
static struct wl_output *output;
static struct wl_shm *shm; static struct wl_shm *shm;
static struct screenshooter *screenshooter; static struct screenshooter *screenshooter;
static int output_width, output_height; static struct wl_list output_list;
struct screenshooter_output {
struct wl_output *output;
struct wl_buffer *buffer;
int width, height, offset_x, offset_y;
struct wl_list link;
};
static void static void
display_handle_geometry(void *data, display_handle_geometry(void *data,
@ -52,6 +60,14 @@ display_handle_geometry(void *data,
const char *make, const char *make,
const char *model) const char *model)
{ {
struct screenshooter_output *output;
output = wl_output_get_user_data(wl_output);
if (wl_output == output->output) {
output->offset_x = x;
output->offset_y = y;
}
} }
static void static void
@ -62,8 +78,15 @@ display_handle_mode(void *data,
int height, int height,
int refresh) int refresh)
{ {
output_width = width; struct screenshooter_output *output;
output_height = height;
output = wl_output_get_user_data(wl_output);
if (wl_output == output->output &&
(flags & WL_OUTPUT_MODE_CURRENT)) {
output->width = width;
output->height = height;
}
} }
static const struct wl_output_listener output_listener = { static const struct wl_output_listener output_listener = {
@ -75,9 +98,13 @@ static void
handle_global(struct wl_display *display, uint32_t id, handle_global(struct wl_display *display, uint32_t id,
const char *interface, uint32_t version, void *data) const char *interface, uint32_t version, void *data)
{ {
static struct screenshooter_output *output;
if (strcmp(interface, "wl_output") == 0) { if (strcmp(interface, "wl_output") == 0) {
output = wl_display_bind(display, id, &wl_output_interface); output = malloc(sizeof *output);
wl_output_add_listener(output, &output_listener, NULL); output->output = wl_display_bind(display, id, &wl_output_interface);
wl_list_insert(&output_list, &output->link);
wl_output_add_listener(output->output, &output_listener, output);
} else if (strcmp(interface, "wl_shm") == 0) { } else if (strcmp(interface, "wl_shm") == 0) {
shm = wl_display_bind(display, id, &wl_shm_interface); shm = wl_display_bind(display, id, &wl_shm_interface);
} else if (strcmp(interface, "screenshooter") == 0) { } else if (strcmp(interface, "screenshooter") == 0) {
@ -144,6 +171,8 @@ int main(int argc, char *argv[])
struct wl_display *display; struct wl_display *display;
struct wl_buffer *buffer; struct wl_buffer *buffer;
void *data = NULL; void *data = NULL;
struct screenshooter_output *output, *next;
int ss_area_width = 0, ss_area_height = 0, num_outputs = 0;
display = wl_display_connect(NULL); display = wl_display_connect(NULL);
if (display == NULL) { if (display == NULL) {
@ -151,6 +180,7 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
wl_list_init(&output_list);
wl_display_add_global_listener(display, handle_global, &screenshooter); wl_display_add_global_listener(display, handle_global, &screenshooter);
wl_display_iterate(display, WL_DISPLAY_READABLE); wl_display_iterate(display, WL_DISPLAY_READABLE);
wl_display_roundtrip(display); wl_display_roundtrip(display);
@ -159,11 +189,32 @@ int main(int argc, char *argv[])
return -1; return -1;
} }
buffer = create_shm_buffer(output_width, output_height, &data); wl_list_for_each(output, &output_list, link) {
screenshooter_shoot(screenshooter, output, buffer);
if (!num_outputs) {
ss_area_width = output->width + output->offset_x;
ss_area_height = output->height + output->offset_y;
}
else {
ss_area_width = MAX(ss_area_width, output->offset_x + output->width);
ss_area_height = MAX(ss_area_height, output->offset_y + output->height);
}
num_outputs++;
}
buffer = create_shm_buffer(ss_area_width, ss_area_height, &data);
wl_list_for_each(output, &output_list, link) {
screenshooter_shoot(screenshooter, output->output, buffer);
}
wl_display_roundtrip(display); wl_display_roundtrip(display);
write_png(output_width, output_height, data); write_png(ss_area_width, ss_area_height, data);
wl_list_for_each_safe(output, next, &output_list, link) {
free(output);
}
return 0; return 0;
} }

@ -1047,6 +1047,20 @@ drm_set_dpms(struct weston_output *output_base, enum dpms_enum level)
drmModeFreeConnector(connector); drmModeFreeConnector(connector);
} }
static void
drm_output_read_pixels(struct weston_output *output_base, void *data)
{
struct drm_output *output = (struct drm_output *) output_base;
struct drm_compositor *compositor =
(struct drm_compositor *) output->base.compositor;
eglMakeCurrent(compositor->base.display, output->egl_surface,
output->egl_surface, compositor->base.context);
glReadPixels(0, 0, output_base->current->width, output_base->current->height,
GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
}
static int static int
create_output_for_connector(struct drm_compositor *ec, create_output_for_connector(struct drm_compositor *ec,
drmModeRes *resources, drmModeRes *resources,
@ -1154,6 +1168,7 @@ create_output_for_connector(struct drm_compositor *ec,
output->base.repaint = drm_output_repaint; output->base.repaint = drm_output_repaint;
output->base.destroy = drm_output_destroy; output->base.destroy = drm_output_destroy;
output->base.assign_planes = drm_assign_planes; output->base.assign_planes = drm_assign_planes;
output->base.read_pixels = drm_output_read_pixels;
output->base.set_dpms = drm_set_dpms; output->base.set_dpms = drm_set_dpms;
return 0; return 0;

@ -377,6 +377,20 @@ wayland_output_destroy(struct weston_output *output_base)
return; return;
} }
static void
wayland_output_read_pixels(struct weston_output *output_base, void *data)
{
struct wayland_output *output = (struct wayland_output *) output_base;
struct wayland_compositor *compositor =
(struct wayland_compositor *) output->base.compositor;
eglMakeCurrent(compositor->base.display, output->egl_surface,
output->egl_surface, compositor->base.context);
glReadPixels(0, 0, output_base->current->width, output_base->current->height,
GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
}
static int static int
wayland_compositor_create_output(struct wayland_compositor *c, wayland_compositor_create_output(struct wayland_compositor *c,
int width, int height) int width, int height)
@ -444,6 +458,7 @@ wayland_compositor_create_output(struct wayland_compositor *c,
output->base.repaint = wayland_output_repaint; output->base.repaint = wayland_output_repaint;
output->base.destroy = wayland_output_destroy; output->base.destroy = wayland_output_destroy;
output->base.assign_planes = NULL; output->base.assign_planes = NULL;
output->base.read_pixels = wayland_output_read_pixels;
output->base.set_backlight = NULL; output->base.set_backlight = NULL;
output->base.set_dpms = NULL; output->base.set_dpms = NULL;

@ -344,6 +344,20 @@ x11_output_set_icon(struct x11_compositor *c,
pixman_image_unref(image); pixman_image_unref(image);
} }
static void
x11_output_read_pixels(struct weston_output *output_base, void *data)
{
struct x11_output *output = (struct x11_output *) output_base;
struct x11_compositor *compositor =
(struct x11_compositor *) output->base.compositor;
eglMakeCurrent(compositor->base.display, output->egl_surface,
output->egl_surface, compositor->base.context);
glReadPixels(0, 0, output_base->current->width, output_base->current->height,
GL_BGRA_EXT, GL_UNSIGNED_BYTE, data);
}
static int static int
x11_compositor_create_output(struct x11_compositor *c, int x, int y, x11_compositor_create_output(struct x11_compositor *c, int x, int y,
int width, int height, int fullscreen) int width, int height, int fullscreen)
@ -454,6 +468,7 @@ x11_compositor_create_output(struct x11_compositor *c, int x, int y,
output->base.repaint = x11_output_repaint; output->base.repaint = x11_output_repaint;
output->base.destroy = x11_output_destroy; output->base.destroy = x11_output_destroy;
output->base.assign_planes = NULL; output->base.assign_planes = NULL;
output->base.read_pixels = x11_output_read_pixels;
output->base.set_backlight = NULL; output->base.set_backlight = NULL;
output->base.set_dpms = NULL; output->base.set_dpms = NULL;

@ -96,6 +96,7 @@ struct weston_output {
pixman_region32_t *damage); pixman_region32_t *damage);
void (*destroy)(struct weston_output *output); void (*destroy)(struct weston_output *output);
void (*assign_planes)(struct weston_output *output); void (*assign_planes)(struct weston_output *output);
void (*read_pixels)(struct weston_output *output, void *data);
/* backlight values are on 0-255 range, where higher is brighter */ /* backlight values are on 0-255 range, where higher is brighter */
uint32_t backlight_current; uint32_t backlight_current;

@ -32,7 +32,7 @@ struct screenshooter {
struct weston_compositor *ec; struct weston_compositor *ec;
struct wl_global *global; struct wl_global *global;
struct wl_client *client; struct wl_client *client;
struct weston_process screenshooter_process; struct weston_process process;
}; };
static void static void
@ -44,7 +44,7 @@ screenshooter_shoot(struct wl_client *client,
struct weston_output *output = output_resource->data; struct weston_output *output = output_resource->data;
struct wl_buffer *buffer = buffer_resource->data; struct wl_buffer *buffer = buffer_resource->data;
uint8_t *tmp, *d, *s; uint8_t *tmp, *d, *s;
int32_t stride, i; int32_t buffer_stride, output_stride, i;
if (!wl_buffer_is_shm(buffer)) if (!wl_buffer_is_shm(buffer))
return; return;
@ -53,24 +53,25 @@ screenshooter_shoot(struct wl_client *client,
buffer->height < output->current->height) buffer->height < output->current->height)
return; return;
stride = wl_shm_buffer_get_stride(buffer); buffer_stride = wl_shm_buffer_get_stride(buffer);
tmp = malloc(stride * buffer->height); output_stride = output->current->width * 4;
tmp = malloc(output_stride * output->current->height);
if (tmp == NULL) { if (tmp == NULL) {
wl_resource_post_no_memory(resource); wl_resource_post_no_memory(resource);
return; return;
} }
glPixelStorei(GL_PACK_ALIGNMENT, 1); glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(0, 0, output->current->width, output->current->height, output->read_pixels(output, tmp);
GL_BGRA_EXT, GL_UNSIGNED_BYTE, tmp);
d = wl_shm_buffer_get_data(buffer); d = wl_shm_buffer_get_data(buffer) + output->y * buffer_stride +
s = tmp + stride * (buffer->height - 1); output->x * 4;
s = tmp + output_stride * (output->current->height - 1);
for (i = 0; i < buffer->height; i++) { for (i = 0; i < output->current->height; i++) {
memcpy(d, s, stride); memcpy(d, s, output_stride);
d += stride; d += buffer_stride;
s -= stride; s -= output_stride;
} }
free(tmp); free(tmp);
@ -101,7 +102,7 @@ static void
screenshooter_sigchld(struct weston_process *process, int status) screenshooter_sigchld(struct weston_process *process, int status)
{ {
struct screenshooter *shooter = struct screenshooter *shooter =
container_of(process, struct screenshooter, screenshooter_process); container_of(process, struct screenshooter, process);
shooter->client = NULL; shooter->client = NULL;
} }
@ -116,7 +117,7 @@ screenshooter_binding(struct wl_input_device *device, uint32_t time,
if (!shooter->client) if (!shooter->client)
shooter->client = weston_client_launch(shooter->ec, shooter->client = weston_client_launch(shooter->ec,
&shooter->screenshooter_process, &shooter->process,
screenshooter_exe, screenshooter_sigchld); screenshooter_exe, screenshooter_sigchld);
} }

Loading…
Cancel
Save