Move read_pixels from output to renderer

This commit is contained in:
emersion 2017-10-08 02:11:56 +02:00
parent 55c063f440
commit 0ce3135304
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
9 changed files with 64 additions and 36 deletions

View file

@ -30,27 +30,32 @@ struct wlr_texture *wlr_render_texture_create(struct wlr_renderer *r);
* This will render the texture at <123, 321>.
*/
bool wlr_render_with_matrix(struct wlr_renderer *r,
struct wlr_texture *texture, const float (*matrix)[16]);
struct wlr_texture *texture, const float (*matrix)[16]);
/**
* Renders a solid quad in the specified color.
*/
void wlr_render_colored_quad(struct wlr_renderer *r,
const float (*color)[4], const float (*matrix)[16]);
const float (*color)[4], const float (*matrix)[16]);
/**
* Renders a solid ellipse in the specified color.
*/
void wlr_render_colored_ellipse(struct wlr_renderer *r,
const float (*color)[4], const float (*matrix)[16]);
const float (*color)[4], const float (*matrix)[16]);
/**
* Returns a list of pixel formats supported by this renderer.
*/
const enum wl_shm_format *wlr_renderer_get_formats(
struct wlr_renderer *r, size_t *len);
struct wlr_renderer *r, size_t *len);
/**
* Returns true if this wl_buffer is a DRM buffer.
*/
bool wlr_renderer_buffer_is_drm(struct wlr_renderer *renderer,
struct wl_resource *buffer);
struct wl_resource *buffer);
/**
* Reads pixels and stores them in out_data as ARGB8888.
*/
void wlr_renderer_read_pixels(struct wlr_renderer *r, int x, int y,
int width, int height, void *out_data);
/**
* Destroys this wlr_renderer. Textures must be destroyed separately.
*/

View file

@ -28,6 +28,8 @@ struct wlr_renderer_impl {
struct wlr_renderer *renderer, size_t *len);
bool (*buffer_is_drm)(struct wlr_renderer *renderer,
struct wl_resource *buffer);
void (*read_pixels)(struct wlr_renderer *renderer, int x, int y, int width,
int height, void *out_data);
void (*destroy)(struct wlr_renderer *renderer);
};

View file

@ -72,9 +72,4 @@ void wlr_output_set_gamma(struct wlr_output *output,
uint16_t size, uint16_t *r, uint16_t *g, uint16_t *b);
uint16_t wlr_output_get_gamma_size(struct wlr_output *output);
/**
* Reads all pixels from the output and stores them as ARGB.
*/
void wlr_output_read_pixels(struct wlr_output *output, void *out_data);
#endif

View file

@ -4,18 +4,23 @@
struct wlr_screenshooter {
struct wl_global *wl_global;
struct wlr_renderer *renderer;
void *data;
};
struct wlr_screenshot {
struct wl_resource *resource;
struct wl_resource *output;
struct wl_resource *output_resource;
struct wlr_output *output;
struct wlr_screenshooter *screenshooter;
void* data;
};
struct wlr_screenshooter *wlr_screenshooter_create(struct wl_display *display);
struct wlr_screenshooter *wlr_screenshooter_create(struct wl_display *display,
struct wlr_renderer *renderer);
void wlr_screenshooter_destroy(struct wlr_screenshooter *screenshooter);
#endif

View file

@ -226,7 +226,23 @@ static bool wlr_gles2_buffer_is_drm(struct wlr_renderer *_renderer,
(struct wlr_gles2_renderer *)_renderer;
EGLint format;
return wlr_egl_query_buffer(renderer->egl, buffer,
EGL_TEXTURE_FORMAT, &format);
EGL_TEXTURE_FORMAT, &format);
}
static void rgba_to_argb(uint32_t *data, size_t height, size_t stride) {
size_t n = height*stride/4;
for (size_t i = 0; i < n; ++i) {
uint32_t v = data[i];
uint32_t rgb = (v & 0xffffff00) >> 8;
uint32_t a = v & 0x000000ff;
data[i] = rgb | (a << 24);
}
}
static void wlr_gles2_read_pixels(struct wlr_renderer *renderer, int x, int y,
int width, int height, void *out_data) {
glReadPixels(x, y, width, height, GL_RGBA, GL_UNSIGNED_BYTE, out_data);
rgba_to_argb(out_data, height, width*4);
}
static struct wlr_renderer_impl wlr_renderer_impl = {
@ -238,6 +254,7 @@ static struct wlr_renderer_impl wlr_renderer_impl = {
.render_ellipse = wlr_gles2_render_ellipse,
.formats = wlr_gles2_formats,
.buffer_is_drm = wlr_gles2_buffer_is_drm,
.read_pixels = wlr_gles2_read_pixels,
};
struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_backend *backend) {

View file

@ -51,3 +51,8 @@ bool wlr_renderer_buffer_is_drm(struct wlr_renderer *r,
struct wl_resource *buffer) {
return r->impl->buffer_is_drm(r, buffer);
}
void wlr_renderer_read_pixels(struct wlr_renderer *r, int x, int y,
int width, int height, void *out_data) {
r->impl->read_pixels(r, x, y, width, height, out_data);
}

View file

@ -182,7 +182,8 @@ struct roots_desktop *desktop_create(struct roots_server *server,
desktop->gamma_control_manager = wlr_gamma_control_manager_create(
server->wl_display);
desktop->screenshooter = wlr_screenshooter_create(server->wl_display);
desktop->screenshooter = wlr_screenshooter_create(server->wl_display,
server->renderer);
return desktop;
}

View file

@ -250,20 +250,3 @@ uint16_t wlr_output_get_gamma_size(struct wlr_output *output) {
}
return output->impl->get_gamma_size(output);
}
static void rgba_to_argb(uint32_t *data, size_t height, size_t stride) {
size_t n = height*stride/4;
for (size_t i = 0; i < n; ++i) {
uint32_t v = data[i];
uint32_t rgb = (v & 0xffffff00) >> 8;
uint32_t a = v & 0x000000ff;
data[i] = rgb | (a << 24);
}
}
void wlr_output_read_pixels(struct wlr_output *output, void *out_data) {
wlr_output_make_current(output);
glReadPixels(0, 0, output->width, output->height, GL_RGBA, GL_UNSIGNED_BYTE,
out_data);
rgba_to_argb(out_data, output->height, output->width*4);
}

View file

@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include <wayland-server.h>
#include <wlr/render.h>
#include <wlr/types/wlr_screenshooter.h>
#include <wlr/types/wlr_output.h>
#include <wlr/util/log.h>
@ -21,7 +22,6 @@ struct screenshot_state {
int32_t width, height, stride;
uint8_t *pixels;
struct wl_shm_buffer *shm_buffer;
struct wlr_output *output;
struct wlr_screenshot *screenshot;
struct wl_listener frame_listener;
};
@ -29,8 +29,12 @@ struct screenshot_state {
static void output_frame_notify(struct wl_listener *listener, void *_data) {
struct screenshot_state *state = wl_container_of(listener, state,
frame_listener);
struct wlr_renderer *renderer = state->screenshot->screenshooter->renderer;
struct wlr_output *output = state->screenshot->output;
wlr_output_read_pixels(state->output, state->pixels);
wlr_output_make_current(output);
wlr_renderer_read_pixels(renderer, 0, 0, output->width, output->height,
state->pixels);
void *data = wl_shm_buffer_get_data(state->shm_buffer);
wl_shm_buffer_begin_access(state->shm_buffer);
@ -49,6 +53,8 @@ static void output_frame_notify(struct wl_listener *listener, void *_data) {
static void screenshooter_shoot(struct wl_client *client,
struct wl_resource *_screenshooter, uint32_t id,
struct wl_resource *_output, struct wl_resource *_buffer) {
struct wlr_screenshooter *screenshooter =
wl_resource_get_user_data(_screenshooter);
struct wlr_output *output = wl_resource_get_user_data(_output);
if (!wl_shm_buffer_get(_buffer)) {
wlr_log(L_ERROR, "Invalid buffer: not a shared memory buffer");
@ -81,7 +87,9 @@ static void screenshooter_shoot(struct wl_client *client,
wl_client_post_no_memory(client);
return;
}
screenshot->output = _output;
screenshot->output_resource = _output;
screenshot->output = output;
screenshot->screenshooter = screenshooter;
screenshot->resource = wl_resource_create(client,
&orbital_screenshot_interface, wl_resource_get_version(_screenshooter),
id);
@ -91,12 +99,15 @@ static void screenshooter_shoot(struct wl_client *client,
NULL);
struct screenshot_state *state = calloc(1, sizeof(struct screenshot_state));
if (!state) {
wl_client_post_no_memory(client);
return;
}
state->width = width;
state->height = height;
state->stride = stride;
state->pixels = pixels;
state->shm_buffer = shm_buffer;
state->output = output;
state->screenshot = screenshot;
state->frame_listener.notify = output_frame_notify;
wl_signal_add(&output->events.swap_buffers, &state->frame_listener);
@ -122,12 +133,15 @@ static void screenshooter_bind(struct wl_client *wl_client,
screenshooter, NULL);
}
struct wlr_screenshooter *wlr_screenshooter_create(struct wl_display *display) {
struct wlr_screenshooter *wlr_screenshooter_create(struct wl_display *display,
struct wlr_renderer *renderer) {
struct wlr_screenshooter *screenshooter =
calloc(1, sizeof(struct wlr_screenshooter));
if (!screenshooter) {
return NULL;
}
screenshooter->renderer = renderer;
struct wl_global *wl_global = wl_global_create(display,
&orbital_screenshooter_interface, 1, screenshooter, screenshooter_bind);
if (!wl_global) {
@ -135,6 +149,7 @@ struct wlr_screenshooter *wlr_screenshooter_create(struct wl_display *display) {
return NULL;
}
screenshooter->wl_global = wl_global;
return screenshooter;
}