backend: gl_common: implement read_pixel

Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
Yuxuan Shui 2021-04-13 14:58:48 +01:00
parent 6faafa95bf
commit 7ba87598c1
No known key found for this signature in database
GPG Key ID: D3A4405BE6CC17F4
3 changed files with 21 additions and 0 deletions

View File

@ -1810,6 +1810,8 @@ static inline void gl_image_decouple(backend_t *base, struct gl_image *img) {
img->color_inverted = false;
img->dim = 0;
img->opacity = 1;
gl_check_err();
}
static void gl_image_apply_alpha(backend_t *base, struct gl_image *img,
@ -1908,6 +1910,23 @@ bool gl_image_op(backend_t *base, enum image_operations op, void *image_data,
return true;
}
bool gl_read_pixel(backend_t *base, void *image_data, int x, int y, struct color *output) {
struct gl_image *tex = image_data;
gl_image_decouple(base, tex);
assert(tex->inner->refcount == 1);
GLfloat color[4];
glReadPixels(x, tex->inner->y_inverted ? tex->inner->height - y : y, 1, 1,
GL_RGBA, GL_FLOAT, color);
output->alpha = color[3];
output->red = color[0];
output->green = color[1];
output->blue = color[2];
bool ret = glGetError() == GL_NO_ERROR;
gl_clear_err();
return ret;
}
bool gl_is_image_transparent(backend_t *base attr_unused, void *image_data) {
struct gl_image *img = image_data;
return img->has_alpha;

View File

@ -127,6 +127,7 @@ bool gl_is_image_transparent(backend_t *base, void *image_data);
void gl_fill(backend_t *base, struct color, const region_t *clip);
void gl_present(backend_t *base, const region_t *);
bool gl_read_pixel(backend_t *base, void *image_data, int x, int y, struct color *output);
static inline void gl_delete_texture(GLuint texture) {
glDeleteTextures(1, &texture);

View File

@ -526,6 +526,7 @@ struct backend_operations glx_ops = {
.release_image = gl_release_image,
.compose = gl_compose,
.image_op = gl_image_op,
.read_pixel = gl_read_pixel,
.copy = gl_copy,
.blur = gl_blur,
.is_image_transparent = gl_is_image_transparent,