[PATCH weston 1/6] renderer: Add new read_surface_pixels vfunc

Nobuhiko Tanibata NOBUHIKO_TANIBATA at xddp.denso.co.jp
Tue Sep 3 17:10:15 PDT 2013


From: Kristian Høgsberg <krh at bitplanet.net>

This vfunc lets us read out a rectangle of pixels from the currently
attached surface buffer.
---
 src/compositor.h  |    3 +++
 src/gl-renderer.c |   53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 56 insertions(+), 0 deletions(-)

diff --git a/src/compositor.h b/src/compositor.h
index 3437aea..aef18d8 100644
--- a/src/compositor.h
+++ b/src/compositor.h
@@ -500,6 +500,9 @@ struct weston_renderer {
 			       pixman_format_code_t format, void *pixels,
 			       uint32_t x, uint32_t y,
 			       uint32_t width, uint32_t height);
+	int (*read_surface_pixels)(struct weston_surface *es,
+				   pixman_format_code_t format, void *pixels,
+				   int x, int y, int width, int height);
 	void (*repaint_output)(struct weston_output *output,
 			       pixman_region32_t *output_damage);
 	void (*flush_damage)(struct weston_surface *surface);
diff --git a/src/gl-renderer.c b/src/gl-renderer.c
index f43e954..a49742b 100644
--- a/src/gl-renderer.c
+++ b/src/gl-renderer.c
@@ -94,6 +94,8 @@ struct gl_renderer {
 		int32_t width, height;
 	} border;
 
+	GLuint fbo;
+
 	struct wl_array vertices;
 	struct wl_array indices; /* only used in compositor-wayland */
 	struct wl_array vtxcnt;
@@ -849,6 +851,53 @@ repaint_surfaces(struct weston_output *output, pixman_region32_t *damage)
 			draw_surface(surface, output, damage);
 }
 
+static int
+gl_renderer_read_surface_pixels(struct weston_surface *es,
+				pixman_format_code_t format, void *pixels,
+				int x, int y, int width, int height)
+{
+	struct weston_buffer *buffer = es->buffer_ref.buffer;
+	struct weston_compositor *ec = es->compositor;
+	struct gl_renderer *gr = get_renderer(ec);
+	struct gl_surface_state *gs = get_surface_state(es);
+	GLenum gl_format;
+	int size;
+	struct wl_shm_buffer *shm_buffer = NULL;
+
+	switch (format) {
+	case PIXMAN_a8r8g8b8:
+		gl_format = GL_BGRA_EXT;
+		break;
+	case PIXMAN_a8b8g8r8:
+		gl_format = GL_RGBA;
+		break;
+	default:
+		return -1;
+	}
+
+        if (buffer) {
+		shm_buffer = wl_shm_buffer_get(buffer->resource);
+	}
+	if (shm_buffer) {
+		size = buffer->width * 4 * buffer->height;
+		memcpy(pixels, wl_shm_buffer_get_data(shm_buffer), size);
+	} else {
+		if (gr->fbo == 0)
+			glGenFramebuffers(1, &gr->fbo);
+		glBindFramebuffer(GL_FRAMEBUFFER, gr->fbo);
+		glFramebufferTexture2D(GL_FRAMEBUFFER,
+				       GL_COLOR_ATTACHMENT0,
+				       GL_TEXTURE_2D,
+				       gs->textures[0], 0);
+
+		glReadPixels(x, y, width, height,
+			     gl_format, GL_UNSIGNED_BYTE, pixels);
+
+		glBindFramebuffer(GL_FRAMEBUFFER, 0);
+	}
+
+	return 0;
+}
 
 static int
 texture_border(struct weston_output *output)
@@ -1779,6 +1828,9 @@ gl_renderer_destroy(struct weston_compositor *ec)
 {
 	struct gl_renderer *gr = get_renderer(ec);
 
+	if (gr->fbo)
+		glDeleteFramebuffers(1, &gr->fbo);
+
 	if (gr->has_bind_display)
 		gr->unbind_display(gr->egl_display, ec->wl_display);
 
@@ -1874,6 +1926,7 @@ gl_renderer_create(struct weston_compositor *ec, EGLNativeDisplayType display,
 		return -1;
 
 	gr->base.read_pixels = gl_renderer_read_pixels;
+	gr->base.read_surface_pixels = gl_renderer_read_surface_pixels;
 	gr->base.repaint_output = gl_renderer_repaint_output;
 	gr->base.flush_damage = gl_renderer_flush_damage;
 	gr->base.attach = gl_renderer_attach;
-- 
1.7.4.4



More information about the wayland-devel mailing list