From 4fa90b05119720a67ccb63cd89246abdb4abf4ea Mon Sep 17 00:00:00 2001 From: emersion Date: Tue, 23 Jan 2018 22:06:54 +0100 Subject: Backport screenshooter fixes from the renderer redesign v1 This backports some changes to #319 to fix the screenshooter data format. This also adds wlr_backend_get_renderer which will be useful to support multiple renderers. --- render/gles2/pixel_format.c | 5 ++++- render/gles2/renderer.c | 36 +++++++++++++++++++++++++----------- render/wlr_renderer.c | 14 +++++++++++--- 3 files changed, 40 insertions(+), 15 deletions(-) (limited to 'render') diff --git a/render/gles2/pixel_format.c b/render/gles2/pixel_format.c index a0b9d09f..9cc75923 100644 --- a/render/gles2/pixel_format.c +++ b/render/gles2/pixel_format.c @@ -2,7 +2,10 @@ #include #include "render/gles2.h" -// Adapted from weston +/* +* The wayland formats are little endian while the GL formats are big endian, +* so WL_SHM_FORMAT_ARGB8888 is actually compatible with GL_BGRA_EXT. +*/ struct pixel_format formats[] = { { .wl_format = WL_SHM_FORMAT_ARGB8888, diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c index 89cc4ffb..6b1a9c59 100644 --- a/render/gles2/renderer.c +++ b/render/gles2/renderer.c @@ -211,20 +211,33 @@ static bool wlr_gles2_buffer_is_drm(struct wlr_renderer *_renderer, 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 bool wlr_gles2_read_pixels(struct wlr_renderer *renderer, + enum wl_shm_format wl_fmt, uint32_t stride, uint32_t width, + uint32_t height, uint32_t src_x, uint32_t src_y, uint32_t dst_x, + uint32_t dst_y, void *data) { + const struct pixel_format *fmt = gl_format_for_wl_format(wl_fmt); + if (fmt == NULL) { + wlr_log(L_ERROR, "Cannot read pixels: unsupported pixel format"); + return false; + } + + // Make sure any pending drawing is finished before we try to read it + glFinish(); + + // Unfortunately GLES2 doesn't support GL_PACK_*, so we have to read + // the lines out row by row + unsigned char *p = data + dst_y * stride; + for (size_t i = src_y; i < src_y + height; ++i) { + glReadPixels(src_x, src_y + height - i - 1, width, 1, fmt->gl_format, + fmt->gl_type, p + i * stride + dst_x * 4); } + + return true; } -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 bool wlr_gles2_format_supported(struct wlr_renderer *r, + enum wl_shm_format wl_fmt) { + return gl_format_for_wl_format(wl_fmt); } static struct wlr_renderer_impl wlr_renderer_impl = { @@ -237,6 +250,7 @@ static struct wlr_renderer_impl wlr_renderer_impl = { .formats = wlr_gles2_formats, .buffer_is_drm = wlr_gles2_buffer_is_drm, .read_pixels = wlr_gles2_read_pixels, + .format_supported = wlr_gles2_format_supported, }; struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_backend *backend) { diff --git a/render/wlr_renderer.c b/render/wlr_renderer.c index ef0c31be..711f11ef 100644 --- a/render/wlr_renderer.c +++ b/render/wlr_renderer.c @@ -52,7 +52,15 @@ bool wlr_renderer_buffer_is_drm(struct wlr_renderer *r, 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); +bool wlr_renderer_read_pixels(struct wlr_renderer *r, enum wl_shm_format fmt, + uint32_t stride, uint32_t width, uint32_t height, + uint32_t src_x, uint32_t src_y, uint32_t dst_x, uint32_t dst_y, + void *data) { + return r->impl->read_pixels(r, fmt, stride, width, height, src_x, src_y, + dst_x, dst_y, data); +} + +bool wlr_renderer_format_supported(struct wlr_renderer *r, + enum wl_shm_format fmt) { + return r->impl->format_supported(r, fmt); } -- cgit v1.2.3 From f34a1b75ebd9dbebf4fb9c2cf34d878fce902c03 Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 26 Jan 2018 23:13:41 +0100 Subject: render/gles2: use format bpp when reading pixels --- render/gles2/pixel_format.c | 4 ++++ render/gles2/renderer.c | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) (limited to 'render') diff --git a/render/gles2/pixel_format.c b/render/gles2/pixel_format.c index 9cc75923..a544077b 100644 --- a/render/gles2/pixel_format.c +++ b/render/gles2/pixel_format.c @@ -25,12 +25,16 @@ struct pixel_format formats[] = { }, { .wl_format = WL_SHM_FORMAT_XBGR8888, + .depth = 24, + .bpp = 32, .gl_format = GL_RGBA, .gl_type = GL_UNSIGNED_BYTE, .shader = &shaders.rgbx }, { .wl_format = WL_SHM_FORMAT_ABGR8888, + .depth = 32, + .bpp = 32, .gl_format = GL_RGBA, .gl_type = GL_UNSIGNED_BYTE, .shader = &shaders.rgba diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c index 6b1a9c59..cc06ad08 100644 --- a/render/gles2/renderer.c +++ b/render/gles2/renderer.c @@ -229,7 +229,7 @@ static bool wlr_gles2_read_pixels(struct wlr_renderer *renderer, unsigned char *p = data + dst_y * stride; for (size_t i = src_y; i < src_y + height; ++i) { glReadPixels(src_x, src_y + height - i - 1, width, 1, fmt->gl_format, - fmt->gl_type, p + i * stride + dst_x * 4); + fmt->gl_type, p + i * stride + dst_x * fmt->bpp / 8); } return true; -- cgit v1.2.3