From b6a3f240c7621d1ebb5774fcdf7784d976500ee1 Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 15 Mar 2018 09:11:03 +0100 Subject: matrix: move to types/ --- include/wlr/render/matrix.h | 22 ---------------------- 1 file changed, 22 deletions(-) delete mode 100644 include/wlr/render/matrix.h (limited to 'include/wlr/render') diff --git a/include/wlr/render/matrix.h b/include/wlr/render/matrix.h deleted file mode 100644 index a333bf0f..00000000 --- a/include/wlr/render/matrix.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef WLR_RENDER_MATRIX_H -#define WLR_RENDER_MATRIX_H - -#include -#include - -void wlr_matrix_identity(float (*output)[16]); -void wlr_matrix_translate(float (*output)[16], float x, float y, float z); -void wlr_matrix_scale(float (*output)[16], float x, float y, float z); -void wlr_matrix_rotate(float (*output)[16], float radians); -void wlr_matrix_mul(const float (*x)[16], const float (*y)[16], float (*product)[16]); - -enum wl_output_transform; -void wlr_matrix_transform(float mat[static 16], - enum wl_output_transform transform); -void wlr_matrix_texture(float mat[static 16], int32_t width, int32_t height, - enum wl_output_transform transform); -void wlr_matrix_project_box(float (*mat)[16], struct wlr_box *box, - enum wl_output_transform transform, float rotation, float - (*projection)[16]); - -#endif -- cgit v1.2.3 From d26b67cb06509fb39d9ed473a5d27b1f241ff635 Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 15 Mar 2018 11:10:56 +0100 Subject: matrix: unify API, don't use array pointers --- backend/drm/drm.c | 4 +- backend/drm/renderer.c | 2 +- examples/output-layout.c | 8 +-- examples/rotation.c | 8 +-- examples/tablet.c | 19 +++--- examples/touch.c | 10 +-- include/wlr/render.h | 12 ++-- include/wlr/render/interface.h | 13 ++-- include/wlr/types/wlr_matrix.h | 21 +++--- include/wlr/types/wlr_surface.h | 7 +- render/gles2/renderer.c | 21 +++--- render/gles2/texture.c | 14 ++-- render/wlr_renderer.c | 9 +-- render/wlr_texture.c | 4 +- rootston/output.c | 14 ++-- types/wlr_matrix.c | 147 ++++++++++++++++++++-------------------- types/wlr_output.c | 16 ++--- types/wlr_surface.c | 16 ++--- 18 files changed, 175 insertions(+), 170 deletions(-) (limited to 'include/wlr/render') diff --git a/backend/drm/drm.c b/backend/drm/drm.c index af701c6a..1e78d301 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -648,9 +648,9 @@ static bool wlr_drm_connector_set_cursor(struct wlr_output *output, glClear(GL_COLOR_BUFFER_BIT); float matrix[16]; - wlr_texture_get_matrix(plane->wlr_tex, &matrix, &plane->matrix, 0, 0); + wlr_texture_get_matrix(plane->wlr_tex, matrix, plane->matrix, 0, 0); wlr_render_with_matrix(plane->surf.renderer->wlr_rend, plane->wlr_tex, - &matrix, 1.0f); + matrix, 1.0f); glFinish(); glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, bo_stride); diff --git a/backend/drm/renderer.c b/backend/drm/renderer.c index 13311df3..ab56e6c2 100644 --- a/backend/drm/renderer.c +++ b/backend/drm/renderer.c @@ -239,7 +239,7 @@ struct gbm_bo *wlr_drm_surface_mgpu_copy(struct wlr_drm_surface *dest, glViewport(0, 0, dest->width, dest->height); glClearColor(0.0, 0.0, 0.0, 1.0); glClear(GL_COLOR_BUFFER_BIT); - wlr_render_with_matrix(dest->renderer->wlr_rend, tex, &matrix, 1.0f); + wlr_render_with_matrix(dest->renderer->wlr_rend, tex, matrix, 1.0f); return wlr_drm_surface_swap_buffers(dest, NULL); } diff --git a/examples/output-layout.c b/examples/output-layout.c index 8ba77861..f4df73a0 100644 --- a/examples/output-layout.c +++ b/examples/output-layout.c @@ -102,7 +102,7 @@ static void handle_output_frame(struct output_state *output, wlr_output_make_current(wlr_output, NULL); wlr_renderer_begin(sample->renderer, wlr_output); - wlr_renderer_clear(sample->renderer, &(float[]){0.25f, 0.25f, 0.25f, 1}); + wlr_renderer_clear(sample->renderer, (float[]){0.25f, 0.25f, 0.25f, 1}); animate_cat(sample, output->output); @@ -119,10 +119,10 @@ static void handle_output_frame(struct output_state *output, wlr_output_layout_output_coords(sample->layout, output->output, &local_x, &local_y); - wlr_texture_get_matrix(sample->cat_texture, &matrix, - &wlr_output->transform_matrix, local_x, local_y); + wlr_texture_get_matrix(sample->cat_texture, matrix, + wlr_output->transform_matrix, local_x, local_y); wlr_render_with_matrix(sample->renderer, - sample->cat_texture, &matrix, 1.0f); + sample->cat_texture, matrix, 1.0f); } wlr_renderer_end(sample->renderer); diff --git a/examples/rotation.c b/examples/rotation.c index e38d53f4..0b0c6adf 100644 --- a/examples/rotation.c +++ b/examples/rotation.c @@ -44,15 +44,15 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts wlr_output_make_current(wlr_output, NULL); wlr_renderer_begin(sample->renderer, wlr_output); - wlr_renderer_clear(sample->renderer, &(float[]){0.25f, 0.25f, 0.25f, 1}); + wlr_renderer_clear(sample->renderer, (float[]){0.25f, 0.25f, 0.25f, 1}); float matrix[16]; for (int y = -128 + (int)odata->y_offs; y < height; y += 128) { for (int x = -128 + (int)odata->x_offs; x < width; x += 128) { - wlr_texture_get_matrix(sample->cat_texture, &matrix, - &wlr_output->transform_matrix, x, y); + wlr_texture_get_matrix(sample->cat_texture, matrix, + wlr_output->transform_matrix, x, y); wlr_render_with_matrix(sample->renderer, - sample->cat_texture, &matrix, 1.0f); + sample->cat_texture, matrix, 1.0f); } } diff --git a/examples/tablet.c b/examples/tablet.c index aa02c6f4..82f86553 100644 --- a/examples/tablet.c +++ b/examples/tablet.c @@ -47,7 +47,7 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts wlr_output_make_current(wlr_output, NULL); wlr_renderer_begin(sample->renderer, wlr_output); - wlr_renderer_clear(sample->renderer, &(float[]){0.25f, 0.25f, 0.25f, 1}); + wlr_renderer_clear(sample->renderer, (float[]){0.25f, 0.25f, 0.25f, 1}); float matrix[16]; float distance = 0.8f * (1 - sample->distance); @@ -65,9 +65,8 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts .x = left, .y = top, .width = pad_width, .height = pad_height, }; - wlr_matrix_project_box(&matrix, &box, 0, 0, - &wlr_output->transform_matrix); - wlr_render_colored_quad(sample->renderer, &sample->pad_color, &matrix); + wlr_matrix_project_box(matrix, &box, 0, 0, wlr_output->transform_matrix); + wlr_render_colored_quad(sample->renderer, sample->pad_color, matrix); if (sample->proximity) { struct wlr_box box = { @@ -76,16 +75,16 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts .width = 16 * (sample->pressure + 1), .height = 16 * (sample->pressure + 1), }; - wlr_matrix_project_box(&matrix, &box, 0, sample->ring, - &wlr_output->transform_matrix); - wlr_render_colored_quad(sample->renderer, &tool_color, &matrix); + wlr_matrix_project_box(matrix, &box, 0, sample->ring, + wlr_output->transform_matrix); + wlr_render_colored_quad(sample->renderer, tool_color, matrix); box.x += sample->x_tilt; box.y += sample->y_tilt; box.width /= 2; box.height /= 2; - wlr_matrix_project_box(&matrix, &box, 0, 0, - &wlr_output->transform_matrix); - wlr_render_colored_quad(sample->renderer, &tool_color, &matrix); + wlr_matrix_project_box(matrix, &box, 0, 0, + wlr_output->transform_matrix); + wlr_render_colored_quad(sample->renderer, tool_color, matrix); } wlr_renderer_end(sample->renderer); diff --git a/examples/touch.c b/examples/touch.c index 7b4559b9..6f4821ad 100644 --- a/examples/touch.c +++ b/examples/touch.c @@ -43,17 +43,17 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts wlr_output_make_current(wlr_output, NULL); wlr_renderer_begin(sample->renderer, wlr_output); - wlr_renderer_clear(sample->renderer, &(float[]){0.25f, 0.25f, 0.25f, 1}); + wlr_renderer_clear(sample->renderer, (float[]){0.25f, 0.25f, 0.25f, 1}); float matrix[16]; struct touch_point *p; wl_list_for_each(p, &sample->touch_points, link) { - wlr_texture_get_matrix(sample->cat_texture, &matrix, - &wlr_output->transform_matrix, + wlr_texture_get_matrix(sample->cat_texture, matrix, + wlr_output->transform_matrix, (int)(p->x * width) - sample->cat_texture->width / 2, (int)(p->y * height) - sample->cat_texture->height / 2); - wlr_render_with_matrix(sample->renderer, - sample->cat_texture, &matrix, 1.0f); + wlr_render_with_matrix(sample->renderer, sample->cat_texture, + matrix, 1.0f); } wlr_renderer_end(sample->renderer); diff --git a/include/wlr/render.h b/include/wlr/render.h index 747603da..50c2dd13 100644 --- a/include/wlr/render.h +++ b/include/wlr/render.h @@ -13,7 +13,7 @@ struct wlr_renderer; void wlr_renderer_begin(struct wlr_renderer *r, struct wlr_output *output); void wlr_renderer_end(struct wlr_renderer *r); -void wlr_renderer_clear(struct wlr_renderer *r, const float (*color)[4]); +void wlr_renderer_clear(struct wlr_renderer *r, const float color[static 4]); /** * Defines a scissor box. Only pixels that lie within the scissor box can be * modified by drawing functions. Providing a NULL `box` disables the scissor @@ -38,18 +38,18 @@ struct wlr_texture *wlr_render_texture_create(struct wlr_renderer *r); * This will render the texture at <123, 321> with an alpha channel of 0.5. */ bool wlr_render_with_matrix(struct wlr_renderer *r, - struct wlr_texture *texture, const float (*matrix)[16], float alpha); + struct wlr_texture *texture, const float matrix[static 16], float alpha); /** * 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[static 4], const float matrix[static 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[static 4], const float matrix[static 16]); /** * Returns a list of pixel formats supported by this renderer. */ @@ -139,8 +139,8 @@ bool wlr_texture_update_shm(struct wlr_texture *surf, uint32_t format, * width) and [0, height], and the x and y coordinates provided are used as * such. */ -void wlr_texture_get_matrix(struct wlr_texture *texture, - float (*matrix)[16], const float (*projection)[16], int x, int y); +void wlr_texture_get_matrix(struct wlr_texture *texture, float mat[static 16], + const float projection[static 16], int x, int y); /** * Destroys this wlr_texture. */ diff --git a/include/wlr/render/interface.h b/include/wlr/render/interface.h index eda5af1c..4d81bdf9 100644 --- a/include/wlr/render/interface.h +++ b/include/wlr/render/interface.h @@ -18,15 +18,16 @@ struct wlr_renderer { struct wlr_renderer_impl { void (*begin)(struct wlr_renderer *renderer, struct wlr_output *output); void (*end)(struct wlr_renderer *renderer); - void (*clear)(struct wlr_renderer *renderer, const float (*color)[4]); + void (*clear)(struct wlr_renderer *renderer, const float color[static 4]); void (*scissor)(struct wlr_renderer *renderer, struct wlr_box *box); struct wlr_texture *(*texture_create)(struct wlr_renderer *renderer); bool (*render_with_matrix)(struct wlr_renderer *renderer, - struct wlr_texture *texture, const float (*matrix)[16], float alpha); + struct wlr_texture *texture, const float matrix[static 16], + float alpha); void (*render_quad)(struct wlr_renderer *renderer, - const float (*color)[4], const float (*matrix)[16]); + const float color[static 4], const float matrix[static 16]); void (*render_ellipse)(struct wlr_renderer *renderer, - const float (*color)[4], const float (*matrix)[16]); + const float color[static 4], const float matrix[static 16]); const enum wl_shm_format *(*formats)( struct wlr_renderer *renderer, size_t *len); bool (*buffer_is_drm)(struct wlr_renderer *renderer, @@ -58,8 +59,8 @@ struct wlr_texture_impl { struct wl_resource *drm_buf); bool (*upload_eglimage)(struct wlr_texture *texture, EGLImageKHR image, uint32_t width, uint32_t height); - void (*get_matrix)(struct wlr_texture *state, - float (*matrix)[16], const float (*projection)[16], int x, int y); + void (*get_matrix)(struct wlr_texture *state, float mat[static 16], + const float projection[static 16], int x, int y); void (*get_buffer_size)(struct wlr_texture *texture, struct wl_resource *resource, int *width, int *height); void (*bind)(struct wlr_texture *texture); diff --git a/include/wlr/types/wlr_matrix.h b/include/wlr/types/wlr_matrix.h index 498b643c..cf5ad20d 100644 --- a/include/wlr/types/wlr_matrix.h +++ b/include/wlr/types/wlr_matrix.h @@ -4,19 +4,20 @@ #include #include -void wlr_matrix_identity(float (*output)[16]); -void wlr_matrix_translate(float (*output)[16], float x, float y, float z); -void wlr_matrix_scale(float (*output)[16], float x, float y, float z); -void wlr_matrix_rotate(float (*output)[16], float radians); -void wlr_matrix_mul(const float (*x)[16], const float (*y)[16], float (*product)[16]); +void wlr_matrix_identity(float mat[static 16]); +void wlr_matrix_translate(float mat[static 16], float x, float y, float z); +void wlr_matrix_scale(float mat[static 16], float x, float y, float z); +void wlr_matrix_rotate(float mat[static 16], float radians); +void wlr_matrix_mul(float mat[static 16], const float x[static 16], + const float y[static 16]); enum wl_output_transform; void wlr_matrix_transform(float mat[static 16], - enum wl_output_transform transform); + enum wl_output_transform transform); void wlr_matrix_texture(float mat[static 16], int32_t width, int32_t height, - enum wl_output_transform transform); -void wlr_matrix_project_box(float (*mat)[16], struct wlr_box *box, - enum wl_output_transform transform, float rotation, float - (*projection)[16]); + enum wl_output_transform transform); +void wlr_matrix_project_box(float mat[static 16], const struct wlr_box *box, + enum wl_output_transform transform, float rotation, + const float projection[static 16]); #endif diff --git a/include/wlr/types/wlr_surface.h b/include/wlr/types/wlr_surface.h index 203345bd..1d4c1f01 100644 --- a/include/wlr/types/wlr_surface.h +++ b/include/wlr/types/wlr_surface.h @@ -99,6 +99,7 @@ struct wlr_surface { struct wlr_renderer; struct wlr_surface *wlr_surface_create(struct wl_resource *res, struct wlr_renderer *renderer); + /** * Gets a matrix you can pass into wlr_render_with_matrix to display this * surface. `matrix` is the output matrix, `projection` is the wlr_output @@ -108,9 +109,9 @@ struct wlr_surface *wlr_surface_create(struct wl_resource *res, * from 0 to 1 in both dimensions. */ void wlr_surface_get_matrix(struct wlr_surface *surface, - float (*matrix)[16], - const float (*projection)[16], - const float (*transform)[16]); + float mat[static 16], + const float projection[static 16], + const float transform[static 16]); /** diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c index 5b1395fb..9134a2fd 100644 --- a/render/gles2/renderer.c +++ b/render/gles2/renderer.c @@ -122,8 +122,8 @@ static void wlr_gles2_end(struct wlr_renderer *wlr_renderer) { } static void wlr_gles2_clear(struct wlr_renderer *wlr_renderer, - const float (*color)[4]) { - glClearColor((*color)[0], (*color)[1], (*color)[2], (*color)[3]); + const float color[static 4]) { + glClearColor(color[0], color[1], color[2], color[3]); glClear(GL_COLOR_BUFFER_BIT); } @@ -171,14 +171,15 @@ static void draw_quad() { } static bool wlr_gles2_render_texture(struct wlr_renderer *wlr_renderer, - struct wlr_texture *texture, const float (*matrix)[16], float alpha) { + struct wlr_texture *texture, const float matrix[static 16], + float alpha) { if (!texture || !texture->valid) { wlr_log(L_ERROR, "attempt to render invalid texture"); return false; } wlr_texture_bind(texture); - GL_CALL(glUniformMatrix4fv(0, 1, GL_FALSE, *matrix)); + GL_CALL(glUniformMatrix4fv(0, 1, GL_FALSE, matrix)); GL_CALL(glUniform1f(2, alpha)); draw_quad(); return true; @@ -186,18 +187,18 @@ static bool wlr_gles2_render_texture(struct wlr_renderer *wlr_renderer, static void wlr_gles2_render_quad(struct wlr_renderer *wlr_renderer, - const float (*color)[4], const float (*matrix)[16]) { + const float color[static 4], const float matrix[static 16]) { GL_CALL(glUseProgram(shaders.quad)); - GL_CALL(glUniformMatrix4fv(0, 1, GL_FALSE, *matrix)); - GL_CALL(glUniform4f(1, (*color)[0], (*color)[1], (*color)[2], (*color)[3])); + GL_CALL(glUniformMatrix4fv(0, 1, GL_FALSE, matrix)); + GL_CALL(glUniform4f(1, color[0], color[1], color[2], color[3])); draw_quad(); } static void wlr_gles2_render_ellipse(struct wlr_renderer *wlr_renderer, - const float (*color)[4], const float (*matrix)[16]) { + const float color[static 4], const float matrix[static 16]) { GL_CALL(glUseProgram(shaders.ellipse)); - GL_CALL(glUniformMatrix4fv(0, 1, GL_TRUE, *matrix)); - GL_CALL(glUniform4f(1, (*color)[0], (*color)[1], (*color)[2], (*color)[3])); + GL_CALL(glUniformMatrix4fv(0, 1, GL_TRUE, matrix)); + GL_CALL(glUniform4f(1, color[0], color[1], color[2], color[3])); draw_quad(); } diff --git a/render/gles2/texture.c b/render/gles2/texture.c index 7a180446..0dfebc48 100644 --- a/render/gles2/texture.c +++ b/render/gles2/texture.c @@ -227,16 +227,16 @@ static bool gles2_texture_upload_eglimage(struct wlr_texture *wlr_tex, } static void gles2_texture_get_matrix(struct wlr_texture *_texture, - float (*matrix)[16], const float (*projection)[16], int x, int y) { + float mat[static 16], const float projection[static 16], int x, int y) { struct wlr_gles2_texture *texture = (struct wlr_gles2_texture *)_texture; float world[16]; - wlr_matrix_identity(matrix); - wlr_matrix_translate(&world, x, y, 0); - wlr_matrix_mul(matrix, &world, matrix); - wlr_matrix_scale(&world, + wlr_matrix_identity(mat); + wlr_matrix_translate(world, x, y, 0); + wlr_matrix_mul(mat, mat, world); + wlr_matrix_scale(world, texture->wlr_texture.width, texture->wlr_texture.height, 1); - wlr_matrix_mul(matrix, &world, matrix); - wlr_matrix_mul(projection, matrix, matrix); + wlr_matrix_mul(mat, mat, world); + wlr_matrix_mul(mat, projection, mat); } static void gles2_texture_get_buffer_size(struct wlr_texture *texture, struct diff --git a/render/wlr_renderer.c b/render/wlr_renderer.c index ce8fbe36..372e4caf 100644 --- a/render/wlr_renderer.c +++ b/render/wlr_renderer.c @@ -23,7 +23,7 @@ void wlr_renderer_end(struct wlr_renderer *r) { r->impl->end(r); } -void wlr_renderer_clear(struct wlr_renderer *r, const float (*color)[4]) { +void wlr_renderer_clear(struct wlr_renderer *r, const float color[static 4]) { r->impl->clear(r, color); } @@ -36,17 +36,18 @@ struct wlr_texture *wlr_render_texture_create(struct wlr_renderer *r) { } bool wlr_render_with_matrix(struct wlr_renderer *r, - struct wlr_texture *texture, const float (*matrix)[16], float alpha) { + struct wlr_texture *texture, const float matrix[static 16], + float alpha) { return r->impl->render_with_matrix(r, texture, matrix, alpha); } void wlr_render_colored_quad(struct wlr_renderer *r, - const float (*color)[4], const float (*matrix)[16]) { + const float color[static 4], const float matrix[static 16]) { r->impl->render_quad(r, color, matrix); } void wlr_render_colored_ellipse(struct wlr_renderer *r, - const float (*color)[4], const float (*matrix)[16]) { + const float color[static 4], const float matrix[static 16]) { r->impl->render_ellipse(r, color, matrix); } diff --git a/render/wlr_texture.c b/render/wlr_texture.c index a82a16b2..0bfac32c 100644 --- a/render/wlr_texture.c +++ b/render/wlr_texture.c @@ -54,8 +54,8 @@ bool wlr_texture_upload_eglimage(struct wlr_texture *texture, } void wlr_texture_get_matrix(struct wlr_texture *texture, - float (*matrix)[16], const float (*projection)[16], int x, int y) { - texture->impl->get_matrix(texture, matrix, projection, x, y); + float mat[static 16], const float projection[static 16], int x, int y) { + texture->impl->get_matrix(texture, mat, projection, x, y); } void wlr_texture_get_buffer_size(struct wlr_texture *texture, struct wl_resource diff --git a/rootston/output.c b/rootston/output.c index bdf025ad..47236720 100644 --- a/rootston/output.c +++ b/rootston/output.c @@ -290,14 +290,14 @@ static void render_surface(struct wlr_surface *surface, double lx, double ly, float matrix[16]; enum wl_output_transform transform = wlr_output_transform_invert(surface->current->transform); - wlr_matrix_project_box(&matrix, &box, transform, rotation, - &output->wlr_output->transform_matrix); + wlr_matrix_project_box(matrix, &box, transform, rotation, + output->wlr_output->transform_matrix); int nrects; pixman_box32_t *rects = pixman_region32_rectangles(&damage, &nrects); for (int i = 0; i < nrects; ++i) { scissor_output(output, &rects[i]); - wlr_render_with_matrix(renderer, surface->texture, &matrix, data->alpha); + wlr_render_with_matrix(renderer, surface->texture, matrix, data->alpha); } damage_finish: @@ -354,8 +354,8 @@ static void render_decorations(struct roots_view *view, } float matrix[16]; - wlr_matrix_project_box(&matrix, &box, WL_OUTPUT_TRANSFORM_NORMAL, - view->rotation, &output->wlr_output->transform_matrix); + wlr_matrix_project_box(matrix, &box, WL_OUTPUT_TRANSFORM_NORMAL, + view->rotation, output->wlr_output->transform_matrix); float color[] = { 0.2, 0.2, 0.2, view->alpha }; int nrects; @@ -363,7 +363,7 @@ static void render_decorations(struct roots_view *view, pixman_region32_rectangles(&damage, &nrects); for (int i = 0; i < nrects; ++i) { scissor_output(output, &rects[i]); - wlr_render_colored_quad(renderer, &color, &matrix); + wlr_render_colored_quad(renderer, color, matrix); } damage_finish: @@ -489,7 +489,7 @@ static void render_output(struct roots_output *output) { pixman_box32_t *rects = pixman_region32_rectangles(&damage, &nrects); for (int i = 0; i < nrects; ++i) { scissor_output(output, &rects[i]); - wlr_renderer_clear(renderer, &clear_color); + wlr_renderer_clear(renderer, clear_color); } // If a view is fullscreen on this output, render it diff --git a/types/wlr_matrix.c b/types/wlr_matrix.c index f3546e0e..a6be9908 100644 --- a/types/wlr_matrix.c +++ b/types/wlr_matrix.c @@ -10,79 +10,80 @@ static inline int mind(int row, int col) { return (row - 1) * 4 + col - 1; } -void wlr_matrix_identity(float (*output)[16]) { +void wlr_matrix_identity(float mat[static 16]) { static const float identity[16] = { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 1.0f + 0.0f, 0.0f, 0.0f, 1.0f, }; - memcpy(*output, identity, sizeof(identity)); + memcpy(mat, identity, sizeof(identity)); } -void wlr_matrix_translate(float (*output)[16], float x, float y, float z) { - wlr_matrix_identity(output); - (*output)[mind(1, 4)] = x; - (*output)[mind(2, 4)] = y; - (*output)[mind(3, 4)] = z; +void wlr_matrix_translate(float mat[static 16], float x, float y, float z) { + wlr_matrix_identity(mat); + mat[mind(1, 4)] = x; + mat[mind(2, 4)] = y; + mat[mind(3, 4)] = z; } -void wlr_matrix_scale(float (*output)[16], float x, float y, float z) { - wlr_matrix_identity(output); - (*output)[mind(1, 1)] = x; - (*output)[mind(2, 2)] = y; - (*output)[mind(3, 3)] = z; +void wlr_matrix_scale(float mat[static 16], float x, float y, float z) { + wlr_matrix_identity(mat); + mat[mind(1, 1)] = x; + mat[mind(2, 2)] = y; + mat[mind(3, 3)] = z; } -void wlr_matrix_rotate(float (*output)[16], float radians) { - wlr_matrix_identity(output); +void wlr_matrix_rotate(float mat[static 16], float radians) { + wlr_matrix_identity(mat); float _cos = cosf(radians); float _sin = sinf(radians); - (*output)[mind(1, 1)] = _cos; - (*output)[mind(1, 2)] = _sin; - (*output)[mind(2, 1)] = -_sin; - (*output)[mind(2, 2)] = _cos; + mat[mind(1, 1)] = _cos; + mat[mind(1, 2)] = _sin; + mat[mind(2, 1)] = -_sin; + mat[mind(2, 2)] = _cos; } -void wlr_matrix_mul(const float (*x)[16], const float (*y)[16], float (*product)[16]) { - float _product[16] = { - (*x)[mind(1, 1)] * (*y)[mind(1, 1)] + (*x)[mind(1, 2)] * (*y)[mind(2, 1)] + - (*x)[mind(1, 3)] * (*y)[mind(3, 1)] + (*x)[mind(1, 4)] * (*y)[mind(4, 1)], - (*x)[mind(1, 1)] * (*y)[mind(1, 2)] + (*x)[mind(1, 2)] * (*y)[mind(2, 2)] + - (*x)[mind(1, 3)] * (*y)[mind(3, 2)] + (*x)[mind(1, 4)] * (*y)[mind(4, 2)], - (*x)[mind(1, 1)] * (*y)[mind(1, 3)] + (*x)[mind(1, 2)] * (*y)[mind(2, 3)] + - (*x)[mind(1, 3)] * (*y)[mind(3, 3)] + (*x)[mind(1, 4)] * (*y)[mind(4, 3)], - (*x)[mind(1, 1)] * (*y)[mind(1, 4)] + (*x)[mind(1, 2)] * (*y)[mind(2, 4)] + - (*x)[mind(1, 4)] * (*y)[mind(3, 4)] + (*x)[mind(1, 4)] * (*y)[mind(4, 4)], - - (*x)[mind(2, 1)] * (*y)[mind(1, 1)] + (*x)[mind(2, 2)] * (*y)[mind(2, 1)] + - (*x)[mind(2, 3)] * (*y)[mind(3, 1)] + (*x)[mind(2, 4)] * (*y)[mind(4, 1)], - (*x)[mind(2, 1)] * (*y)[mind(1, 2)] + (*x)[mind(2, 2)] * (*y)[mind(2, 2)] + - (*x)[mind(2, 3)] * (*y)[mind(3, 2)] + (*x)[mind(2, 4)] * (*y)[mind(4, 2)], - (*x)[mind(2, 1)] * (*y)[mind(1, 3)] + (*x)[mind(2, 2)] * (*y)[mind(2, 3)] + - (*x)[mind(2, 3)] * (*y)[mind(3, 3)] + (*x)[mind(2, 4)] * (*y)[mind(4, 3)], - (*x)[mind(2, 1)] * (*y)[mind(1, 4)] + (*x)[mind(2, 2)] * (*y)[mind(2, 4)] + - (*x)[mind(2, 4)] * (*y)[mind(3, 4)] + (*x)[mind(2, 4)] * (*y)[mind(4, 4)], - - (*x)[mind(3, 1)] * (*y)[mind(1, 1)] + (*x)[mind(3, 2)] * (*y)[mind(2, 1)] + - (*x)[mind(3, 3)] * (*y)[mind(3, 1)] + (*x)[mind(3, 4)] * (*y)[mind(4, 1)], - (*x)[mind(3, 1)] * (*y)[mind(1, 2)] + (*x)[mind(3, 2)] * (*y)[mind(2, 2)] + - (*x)[mind(3, 3)] * (*y)[mind(3, 2)] + (*x)[mind(3, 4)] * (*y)[mind(4, 2)], - (*x)[mind(3, 1)] * (*y)[mind(1, 3)] + (*x)[mind(3, 2)] * (*y)[mind(2, 3)] + - (*x)[mind(3, 3)] * (*y)[mind(3, 3)] + (*x)[mind(3, 4)] * (*y)[mind(4, 3)], - (*x)[mind(3, 1)] * (*y)[mind(1, 4)] + (*x)[mind(3, 2)] * (*y)[mind(2, 4)] + - (*x)[mind(3, 4)] * (*y)[mind(3, 4)] + (*x)[mind(3, 4)] * (*y)[mind(4, 4)], - - (*x)[mind(4, 1)] * (*y)[mind(1, 1)] + (*x)[mind(4, 2)] * (*y)[mind(2, 1)] + - (*x)[mind(4, 3)] * (*y)[mind(3, 1)] + (*x)[mind(4, 4)] * (*y)[mind(4, 1)], - (*x)[mind(4, 1)] * (*y)[mind(1, 2)] + (*x)[mind(4, 2)] * (*y)[mind(2, 2)] + - (*x)[mind(4, 3)] * (*y)[mind(3, 2)] + (*x)[mind(4, 4)] * (*y)[mind(4, 2)], - (*x)[mind(4, 1)] * (*y)[mind(1, 3)] + (*x)[mind(4, 2)] * (*y)[mind(2, 3)] + - (*x)[mind(4, 3)] * (*y)[mind(3, 3)] + (*x)[mind(4, 4)] * (*y)[mind(4, 3)], - (*x)[mind(4, 1)] * (*y)[mind(1, 4)] + (*x)[mind(4, 2)] * (*y)[mind(2, 4)] + - (*x)[mind(4, 4)] * (*y)[mind(3, 4)] + (*x)[mind(4, 4)] * (*y)[mind(4, 4)], +void wlr_matrix_mul(float mat[static 16], const float x[static 16], + const float y[static 16]) { + float product[16] = { + x[mind(1, 1)] * y[mind(1, 1)] + x[mind(1, 2)] * y[mind(2, 1)] + + x[mind(1, 3)] * y[mind(3, 1)] + x[mind(1, 4)] * y[mind(4, 1)], + x[mind(1, 1)] * y[mind(1, 2)] + x[mind(1, 2)] * y[mind(2, 2)] + + x[mind(1, 3)] * y[mind(3, 2)] + x[mind(1, 4)] * y[mind(4, 2)], + x[mind(1, 1)] * y[mind(1, 3)] + x[mind(1, 2)] * y[mind(2, 3)] + + x[mind(1, 3)] * y[mind(3, 3)] + x[mind(1, 4)] * y[mind(4, 3)], + x[mind(1, 1)] * y[mind(1, 4)] + x[mind(1, 2)] * y[mind(2, 4)] + + x[mind(1, 4)] * y[mind(3, 4)] + x[mind(1, 4)] * y[mind(4, 4)], + + x[mind(2, 1)] * y[mind(1, 1)] + x[mind(2, 2)] * y[mind(2, 1)] + + x[mind(2, 3)] * y[mind(3, 1)] + x[mind(2, 4)] * y[mind(4, 1)], + x[mind(2, 1)] * y[mind(1, 2)] + x[mind(2, 2)] * y[mind(2, 2)] + + x[mind(2, 3)] * y[mind(3, 2)] + x[mind(2, 4)] * y[mind(4, 2)], + x[mind(2, 1)] * y[mind(1, 3)] + x[mind(2, 2)] * y[mind(2, 3)] + + x[mind(2, 3)] * y[mind(3, 3)] + x[mind(2, 4)] * y[mind(4, 3)], + x[mind(2, 1)] * y[mind(1, 4)] + x[mind(2, 2)] * y[mind(2, 4)] + + x[mind(2, 4)] * y[mind(3, 4)] + x[mind(2, 4)] * y[mind(4, 4)], + + x[mind(3, 1)] * y[mind(1, 1)] + x[mind(3, 2)] * y[mind(2, 1)] + + x[mind(3, 3)] * y[mind(3, 1)] + x[mind(3, 4)] * y[mind(4, 1)], + x[mind(3, 1)] * y[mind(1, 2)] + x[mind(3, 2)] * y[mind(2, 2)] + + x[mind(3, 3)] * y[mind(3, 2)] + x[mind(3, 4)] * y[mind(4, 2)], + x[mind(3, 1)] * y[mind(1, 3)] + x[mind(3, 2)] * y[mind(2, 3)] + + x[mind(3, 3)] * y[mind(3, 3)] + x[mind(3, 4)] * y[mind(4, 3)], + x[mind(3, 1)] * y[mind(1, 4)] + x[mind(3, 2)] * y[mind(2, 4)] + + x[mind(3, 4)] * y[mind(3, 4)] + x[mind(3, 4)] * y[mind(4, 4)], + + x[mind(4, 1)] * y[mind(1, 1)] + x[mind(4, 2)] * y[mind(2, 1)] + + x[mind(4, 3)] * y[mind(3, 1)] + x[mind(4, 4)] * y[mind(4, 1)], + x[mind(4, 1)] * y[mind(1, 2)] + x[mind(4, 2)] * y[mind(2, 2)] + + x[mind(4, 3)] * y[mind(3, 2)] + x[mind(4, 4)] * y[mind(4, 2)], + x[mind(4, 1)] * y[mind(1, 3)] + x[mind(4, 2)] * y[mind(2, 3)] + + x[mind(4, 3)] * y[mind(3, 3)] + x[mind(4, 4)] * y[mind(4, 3)], + x[mind(4, 1)] * y[mind(1, 4)] + x[mind(4, 2)] * y[mind(2, 4)] + + x[mind(4, 4)] * y[mind(3, 4)] + x[mind(4, 4)] * y[mind(4, 4)], }; - memcpy(*product, _product, sizeof(_product)); + memcpy(mat, product, sizeof(product)); } static const float transforms[][4] = { @@ -161,9 +162,9 @@ void wlr_matrix_texture(float mat[static 16], int32_t width, int32_t height, mat[15] = 1.0f; } -void wlr_matrix_project_box(float (*mat)[16], struct wlr_box *box, +void wlr_matrix_project_box(float mat[static 16], const struct wlr_box *box, enum wl_output_transform transform, float rotation, - float (*projection)[16]) { + const float projection[static 16]) { int x = box->x; int y = box->y; int width = box->width; @@ -173,38 +174,38 @@ void wlr_matrix_project_box(float (*mat)[16], struct wlr_box *box, if (rotation != 0) { float translate_center[16]; - wlr_matrix_translate(&translate_center, width/2, height/2, 0); + wlr_matrix_translate(translate_center, width/2, height/2, 0); float rotate[16]; - wlr_matrix_rotate(&rotate, rotation); + wlr_matrix_rotate(rotate, rotation); float translate_origin[16]; - wlr_matrix_translate(&translate_origin, -width/2, -height/2, 0); + wlr_matrix_translate(translate_origin, -width/2, -height/2, 0); - wlr_matrix_mul(mat, &translate_center, mat); - wlr_matrix_mul(mat, &rotate, mat); - wlr_matrix_mul(mat, &translate_origin, mat); + wlr_matrix_mul(mat, mat, translate_center); + wlr_matrix_mul(mat, mat, rotate); + wlr_matrix_mul(mat, mat, translate_origin); } float scale[16]; - wlr_matrix_scale(&scale, width, height, 1); + wlr_matrix_scale(scale, width, height, 1); - wlr_matrix_mul(mat, &scale, mat); + wlr_matrix_mul(mat, mat, scale); if (transform != WL_OUTPUT_TRANSFORM_NORMAL) { float surface_translate_center[16]; - wlr_matrix_translate(&surface_translate_center, 0.5, 0.5, 0); + wlr_matrix_translate(surface_translate_center, 0.5, 0.5, 0); float surface_transform[16]; wlr_matrix_transform(surface_transform, transform); float surface_translate_origin[16]; - wlr_matrix_translate(&surface_translate_origin, -0.5, -0.5, 0); + wlr_matrix_translate(surface_translate_origin, -0.5, -0.5, 0); - wlr_matrix_mul(mat, &surface_translate_center, mat); - wlr_matrix_mul(mat, &surface_transform, mat); - wlr_matrix_mul(mat, &surface_translate_origin, mat); + wlr_matrix_mul(mat, mat, surface_translate_center); + wlr_matrix_mul(mat, mat, surface_transform); + wlr_matrix_mul(mat, mat, surface_translate_origin); } - wlr_matrix_mul(projection, mat, mat); + wlr_matrix_mul(mat, projection, mat); } diff --git a/types/wlr_output.c b/types/wlr_output.c index 21343a68..a9fb01c5 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -368,7 +368,7 @@ static void output_fullscreen_surface_render(struct wlr_output *output, assert(renderer); if (!wlr_surface_has_buffer(surface)) { - wlr_renderer_clear(renderer, &(float[]){0, 0, 0, 0}); + wlr_renderer_clear(renderer, (float[]){0, 0, 0, 0}); return; } @@ -378,15 +378,15 @@ static void output_fullscreen_surface_render(struct wlr_output *output, float matrix[16]; enum wl_output_transform transform = wlr_output_transform_invert(surface->current->transform); - wlr_matrix_project_box(&matrix, &box, transform, 0, - &output->transform_matrix); + wlr_matrix_project_box(matrix, &box, transform, 0, + output->transform_matrix); int nrects; pixman_box32_t *rects = pixman_region32_rectangles(damage, &nrects); for (int i = 0; i < nrects; ++i) { output_scissor(output, &rects[i]); - wlr_renderer_clear(renderer, &(float[]){0, 0, 0, 0}); - wlr_render_with_matrix(surface->renderer, surface->texture, &matrix, 1.0f); + wlr_renderer_clear(renderer, (float[]){0, 0, 0, 0}); + wlr_render_with_matrix(surface->renderer, surface->texture, matrix, 1.0f); } wlr_renderer_scissor(renderer, NULL); @@ -436,14 +436,14 @@ static void output_cursor_render(struct wlr_output_cursor *cursor, } float matrix[16]; - wlr_matrix_project_box(&matrix, &box, WL_OUTPUT_TRANSFORM_NORMAL, 0, - &cursor->output->transform_matrix); + wlr_matrix_project_box(matrix, &box, WL_OUTPUT_TRANSFORM_NORMAL, 0, + cursor->output->transform_matrix); int nrects; pixman_box32_t *rects = pixman_region32_rectangles(&surface_damage, &nrects); for (int i = 0; i < nrects; ++i) { output_scissor(cursor->output, &rects[i]); - wlr_render_with_matrix(renderer, texture, &matrix, 1.0f); + wlr_render_with_matrix(renderer, texture, matrix, 1.0f); } wlr_renderer_scissor(renderer, NULL); diff --git a/types/wlr_surface.c b/types/wlr_surface.c index 5d52fd2a..04dcb141 100644 --- a/types/wlr_surface.c +++ b/types/wlr_surface.c @@ -625,19 +625,19 @@ struct wlr_surface *wlr_surface_create(struct wl_resource *res, } void wlr_surface_get_matrix(struct wlr_surface *surface, - float (*matrix)[16], - const float (*projection)[16], - const float (*transform)[16]) { + float mat[static 16], + const float projection[static 16], + const float transform[static 16]) { int width = surface->texture->width; int height = surface->texture->height; float scale[16]; - wlr_matrix_identity(matrix); + wlr_matrix_identity(mat); if (transform) { - wlr_matrix_mul(matrix, transform, matrix); + wlr_matrix_mul(mat, mat, transform); } - wlr_matrix_scale(&scale, width, height, 1); - wlr_matrix_mul(matrix, &scale, matrix); - wlr_matrix_mul(projection, matrix, matrix); + wlr_matrix_scale(scale, width, height, 1); + wlr_matrix_mul(mat, mat, scale); + wlr_matrix_mul(mat, projection, mat); } bool wlr_surface_has_buffer(struct wlr_surface *surface) { -- cgit v1.2.3 From 14cdb6153f4293d7e058465ab4acaebd6e1f647c Mon Sep 17 00:00:00 2001 From: Guido Günther Date: Fri, 23 Feb 2018 18:45:16 +0100 Subject: Add initial linux_dmabuf protocol support Tested with ./weston-simple-dmabuf-drm ./weston-simple-dmabuf-drm --import-immediate=1 ./weston-simple-dmabuf-drm --y-inverted=1 (and combinations) Supports only single plane XRGB dmabufs for now. --- include/wlr/render.h | 2 + include/wlr/render/egl.h | 28 +++ include/wlr/render/interface.h | 3 + include/wlr/types/wlr_linux_dmabuf.h | 84 +++++++ protocol/meson.build | 1 + render/egl.c | 135 ++++++++++ render/glapi.txt | 2 + render/gles2/texture.c | 62 ++++- render/wlr_texture.c | 5 + types/meson.build | 1 + types/wlr_linux_dmabuf.c | 464 +++++++++++++++++++++++++++++++++++ types/wlr_surface.c | 4 + 12 files changed, 787 insertions(+), 4 deletions(-) create mode 100644 include/wlr/types/wlr_linux_dmabuf.h create mode 100644 types/wlr_linux_dmabuf.c (limited to 'include/wlr/render') diff --git a/include/wlr/render.h b/include/wlr/render.h index 77449556..9080175f 100644 --- a/include/wlr/render.h +++ b/include/wlr/render.h @@ -123,6 +123,8 @@ bool wlr_texture_upload_drm(struct wlr_texture *tex, bool wlr_texture_upload_eglimage(struct wlr_texture *tex, EGLImageKHR image, uint32_t width, uint32_t height); +bool wlr_texture_upload_dmabuf(struct wlr_texture *tex, + struct wl_resource *dmabuf_resource); /** * Copies a rectangle of pixels from a wl_shm_buffer onto the texture. The * buffer is not accessed after this function returns. Under some circumstances, diff --git a/include/wlr/render/egl.h b/include/wlr/render/egl.h index 97a28016..f05a9837 100644 --- a/include/wlr/render/egl.h +++ b/include/wlr/render/egl.h @@ -6,6 +6,7 @@ #include #include #include +#include struct wlr_egl { EGLDisplay display; @@ -18,6 +19,8 @@ struct wlr_egl { struct { bool buffer_age; bool swap_buffers_with_damage; + bool dmabuf_import; + bool dmabuf_import_modifiers; } egl_exts; struct wl_display *wl_display; @@ -61,6 +64,31 @@ EGLSurface wlr_egl_create_surface(struct wlr_egl *egl, void *window); EGLImageKHR wlr_egl_create_image(struct wlr_egl *egl, EGLenum target, EGLClientBuffer buffer, const EGLint *attribs); +/** + * Creates an egl image from the given dmabuf attributes. Check usability + * of the dmabuf with wlr_egl_check_import_dmabuf once first. + */ +EGLImageKHR wlr_egl_create_image_from_dmabuf(struct wlr_egl *egl, + struct wlr_dmabuf_buffer_attribs *attributes); + +/** + * Try to import the given dmabuf. On success return true false otherwise. + * If this succeeds the dmabuf can be used for rendering on a texture + */ +bool wlr_egl_check_import_dmabuf(struct wlr_egl *egl, + struct wlr_dmabuf_buffer *dmabuf); + +/** + * Get the available dmabuf formats + */ +int wlr_egl_get_dmabuf_formats(struct wlr_egl *egl, int **formats); + +/** + * Get the available dmabuf modifiers for a given format + */ +int wlr_egl_get_dmabuf_modifiers(struct wlr_egl *egl, int format, + uint64_t **modifiers); + /** * Destroys an egl image created with the given wlr_egl. */ diff --git a/include/wlr/render/interface.h b/include/wlr/render/interface.h index eda5af1c..2ba584d9 100644 --- a/include/wlr/render/interface.h +++ b/include/wlr/render/interface.h @@ -8,6 +8,7 @@ #include #include #include +#include struct wlr_renderer_impl; @@ -58,6 +59,8 @@ struct wlr_texture_impl { struct wl_resource *drm_buf); bool (*upload_eglimage)(struct wlr_texture *texture, EGLImageKHR image, uint32_t width, uint32_t height); + bool (*upload_dmabuf)(struct wlr_texture *texture, + struct wl_resource *dmabuf_resource); void (*get_matrix)(struct wlr_texture *state, float (*matrix)[16], const float (*projection)[16], int x, int y); void (*get_buffer_size)(struct wlr_texture *texture, diff --git a/include/wlr/types/wlr_linux_dmabuf.h b/include/wlr/types/wlr_linux_dmabuf.h new file mode 100644 index 00000000..9d71e598 --- /dev/null +++ b/include/wlr/types/wlr_linux_dmabuf.h @@ -0,0 +1,84 @@ +#ifndef WLR_TYPES_WLR_LINUX_DMABUF_H +#define WLR_TYPES_WLR_LINUX_DMABUF_H + +#define WLR_LINUX_DMABUF_MAX_PLANES 4 + +#include +#include + +/* So we don't have to pull in linux specific drm headers */ +#ifndef DRM_FORMAT_MOD_INVALID +#define DRM_FORMAT_MOD_INVALID ((1ULL<<56) - 1) +#endif + +struct wlr_dmabuf_buffer_attribs { + /* set via params_add */ + int n_planes; + uint32_t offset[WLR_LINUX_DMABUF_MAX_PLANES]; + uint32_t stride[WLR_LINUX_DMABUF_MAX_PLANES]; + uint64_t modifier[WLR_LINUX_DMABUF_MAX_PLANES]; + int fd[WLR_LINUX_DMABUF_MAX_PLANES]; + /* set via params_create */ + int32_t width; + int32_t height; + uint32_t format; + uint32_t flags; /* enum zlinux_buffer_params_flags */ +}; + +struct wlr_dmabuf_buffer { + struct wlr_egl *egl; + struct wl_resource *buffer_resource; + struct wl_resource *params_resource; + struct wlr_dmabuf_buffer_attribs attributes; +}; + +/** + * Returns true if the given resource was created via the linux-dmabuf + * buffer protocol, false otherwise + */ +bool wlr_dmabuf_resource_is_buffer(struct wl_resource *buffer_resource); + +/** + * Returns the wlr_dmabuf_buffer if the given resource was created + * via the linux-dmabuf buffer protocol + */ +struct wlr_dmabuf_buffer *wlr_dmabuf_buffer_from_buffer_resource( + struct wl_resource *buffer_resource); + +/** + * Returns the wlr_dmabuf_buffer if the given resource was created + * via the linux-dmabuf params protocol + */ +struct wlr_dmabuf_buffer *wlr_dmabuf_buffer_from_params_resource( + struct wl_resource *params_resource); + +/** + * Returns true if the given dmabuf has y-axis inverted, false otherwise + */ +bool wlr_dmabuf_buffer_has_inverted_y(struct wlr_dmabuf_buffer *dmabuf); + +/* the protocol interface */ +struct wlr_linux_dmabuf { + struct wl_global *wl_global; + struct wl_listener display_destroy; + struct wlr_egl *egl; +}; + +/** + * Create linux-dmabuf interface + */ +struct wlr_linux_dmabuf *wlr_linux_dmabuf_create(struct wl_display *display, + struct wlr_egl *egl); +/** + * Destroy the linux-dmabuf interface + */ +void wlr_linux_dmabuf_destroy(struct wlr_linux_dmabuf *linux_dmabuf); + +/** + * Returns the wlr_linux_dmabuf if the given resource was created + * via the linux_dmabuf protocol + */ +struct wlr_linux_dmabuf *wlr_linux_dmabuf_from_resource( + struct wl_resource *resource); + +#endif diff --git a/protocol/meson.build b/protocol/meson.build index 200cbd7a..638b0c46 100644 --- a/protocol/meson.build +++ b/protocol/meson.build @@ -23,6 +23,7 @@ wayland_scanner_client = generator( protocols = [ [wl_protocol_dir, 'stable/xdg-shell/xdg-shell.xml'], [wl_protocol_dir, 'unstable/idle-inhibit/idle-inhibit-unstable-v1.xml'], + [wl_protocol_dir, 'unstable/linux-dmabuf/linux-dmabuf-unstable-v1.xml'], [wl_protocol_dir, 'unstable/xdg-shell/xdg-shell-unstable-v6.xml'], 'gamma-control.xml', 'gtk-primary-selection.xml', diff --git a/render/egl.c b/render/egl.c index 0a68d6e5..55809983 100644 --- a/render/egl.c +++ b/render/egl.c @@ -167,6 +167,12 @@ bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, void *remote_display, check_egl_ext(egl->egl_exts_str, "EGL_EXT_swap_buffers_with_damage") || check_egl_ext(egl->egl_exts_str, "EGL_KHR_swap_buffers_with_damage"); + egl->egl_exts.dmabuf_import = + check_egl_ext(egl->egl_exts_str, "EGL_EXT_image_dma_buf_import"); + egl->egl_exts.dmabuf_import_modifiers = + check_egl_ext(egl->egl_exts_str, "EGL_EXT_image_dma_buf_import_modifiers") + && eglQueryDmaBufFormatsEXT && eglQueryDmaBufModifiersEXT; + return true; error: @@ -299,3 +305,132 @@ bool wlr_egl_swap_buffers(struct wlr_egl *egl, EGLSurface surface, } return true; } + +EGLImage wlr_egl_create_image_from_dmabuf(struct wlr_egl *egl, + struct wlr_dmabuf_buffer_attribs *attributes) { + int atti = 0; + EGLint attribs[20]; + attribs[atti++] = EGL_WIDTH; + attribs[atti++] = attributes->width; + attribs[atti++] = EGL_HEIGHT; + attribs[atti++] = attributes->height; + attribs[atti++] = EGL_LINUX_DRM_FOURCC_EXT; + attribs[atti++] = attributes->format; + + bool has_modifier = false; + if (attributes->modifier[0] != DRM_FORMAT_MOD_INVALID) { + if (!egl->egl_exts.dmabuf_import_modifiers) { + return NULL; + } + has_modifier = true; + } + + /* TODO: YUV planes have up four planes but we only support a + single EGLImage for now */ + if (attributes->n_planes > 1) { + return NULL; + } + + attribs[atti++] = EGL_DMA_BUF_PLANE0_FD_EXT; + attribs[atti++] = attributes->fd[0]; + attribs[atti++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT; + attribs[atti++] = attributes->offset[0]; + attribs[atti++] = EGL_DMA_BUF_PLANE0_PITCH_EXT; + attribs[atti++] = attributes->stride[0]; + if (has_modifier) { + attribs[atti++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT; + attribs[atti++] = attributes->modifier[0] & 0xFFFFFFFF; + attribs[atti++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT; + attribs[atti++] = attributes->modifier[0] >> 32; + } + attribs[atti++] = EGL_NONE; + return eglCreateImageKHR(egl->display, EGL_NO_CONTEXT, + EGL_LINUX_DMA_BUF_EXT, NULL, attribs); +} + +#ifndef DRM_FORMAT_BIG_ENDIAN +# define DRM_FORMAT_BIG_ENDIAN 0x80000000 +#endif +bool wlr_egl_check_import_dmabuf(struct wlr_egl *egl, + struct wlr_dmabuf_buffer *dmabuf) { + switch (dmabuf->attributes.format & ~DRM_FORMAT_BIG_ENDIAN) { + /* YUV based formats not yet supported */ + case WL_SHM_FORMAT_YUYV: + case WL_SHM_FORMAT_YVYU: + case WL_SHM_FORMAT_UYVY: + case WL_SHM_FORMAT_VYUY: + case WL_SHM_FORMAT_AYUV: + return false; + default: + break; + } + + EGLImage egl_image = wlr_egl_create_image_from_dmabuf(egl, + &dmabuf->attributes); + if (egl_image) { + /* We can import the image, good. No need to keep it + since wlr_texture_upload_dmabuf will import it again */ + wlr_egl_destroy_image(egl, egl_image); + return true; + } + /* TODO: import yuv dmabufs */ + return false; +} + +int wlr_egl_get_dmabuf_formats(struct wlr_egl *egl, + int **formats) { + if (!egl->egl_exts.dmabuf_import || + !egl->egl_exts.dmabuf_import_modifiers) { + wlr_log(L_ERROR, "dmabuf extension not present"); + return -1; + } + + EGLint num; + if (!eglQueryDmaBufFormatsEXT(egl->display, 0, NULL, &num)) { + wlr_log(L_ERROR, "failed to query number of dmabuf formats"); + return -1; + } + + *formats = calloc(num, sizeof(int)); + if (*formats == NULL) { + wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); + return -1; + } + + if (!eglQueryDmaBufFormatsEXT(egl->display, num, *formats, &num)) { + wlr_log(L_ERROR, "failed to query dmabuf format"); + free(*formats); + return -1; + } + return num; +} + +int wlr_egl_get_dmabuf_modifiers(struct wlr_egl *egl, + int format, uint64_t **modifiers) { + if (!egl->egl_exts.dmabuf_import || + !egl->egl_exts.dmabuf_import_modifiers) { + wlr_log(L_ERROR, "dmabuf extension not present"); + return -1; + } + + EGLint num; + if (!eglQueryDmaBufModifiersEXT(egl->display, format, 0, + NULL, NULL, &num)) { + wlr_log(L_ERROR, "failed to query dmabuf number of modifiers"); + return -1; + } + + *modifiers = calloc(num, sizeof(uint64_t)); + if (*modifiers == NULL) { + wlr_log(L_ERROR, "Allocation failed: %s", strerror(errno)); + return -1; + } + + if (!eglQueryDmaBufModifiersEXT(egl->display, format, num, + *modifiers, NULL, &num)) { + wlr_log(L_ERROR, "failed to query dmabuf modifiers"); + free(*modifiers); + return -1; + } + return num; +} diff --git a/render/glapi.txt b/render/glapi.txt index 0b0b452c..02ac7dd8 100644 --- a/render/glapi.txt +++ b/render/glapi.txt @@ -8,3 +8,5 @@ eglCreatePlatformWindowSurfaceEXT -glEGLImageTargetTexture2DOES -eglSwapBuffersWithDamageEXT -eglSwapBuffersWithDamageKHR +-eglQueryDmaBufFormatsEXT +-eglQueryDmaBufModifiersEXT diff --git a/render/gles2/texture.c b/render/gles2/texture.c index d25d1809..5e84890f 100644 --- a/render/gles2/texture.c +++ b/render/gles2/texture.c @@ -227,6 +227,42 @@ static bool gles2_texture_upload_eglimage(struct wlr_texture *wlr_tex, return true; } + +static bool gles2_texture_upload_dmabuf(struct wlr_texture *_tex, + struct wl_resource *dmabuf_resource) { + struct wlr_gles2_texture *tex = (struct wlr_gles2_texture *)_tex; + struct wlr_dmabuf_buffer *dmabuf = wlr_dmabuf_buffer_from_buffer_resource( + dmabuf_resource); + + if (!tex->egl->egl_exts.dmabuf_import) { + wlr_log(L_ERROR, "Want dmabuf but extension not present"); + return false; + } + + tex->wlr_texture.width = dmabuf->attributes.width; + tex->wlr_texture.height = dmabuf->attributes.height; + + if (tex->image) { + wlr_egl_destroy_image(tex->egl, tex->image); + } + + if (wlr_dmabuf_buffer_has_inverted_y(dmabuf)) { + _tex->inverted_y = true; + } + + GLenum target = GL_TEXTURE_2D; + const struct pixel_format *pf = + gl_format_for_wl_format(WL_SHM_FORMAT_ARGB8888); + gles2_texture_ensure_texture(tex); + GL_CALL(glBindTexture(target, tex->tex_id)); + tex->image = wlr_egl_create_image_from_dmabuf(tex->egl, &dmabuf->attributes); + GL_CALL(glActiveTexture(GL_TEXTURE0)); + GL_CALL(glEGLImageTargetTexture2DOES(target, tex->image)); + tex->pixel_format = pf; + tex->wlr_texture.valid = true; + return true; +} + static void gles2_texture_get_matrix(struct wlr_texture *_texture, float (*matrix)[16], const float (*projection)[16], int x, int y) { struct wlr_gles2_texture *texture = (struct wlr_gles2_texture *)_texture; @@ -240,6 +276,21 @@ static void gles2_texture_get_matrix(struct wlr_texture *_texture, wlr_matrix_mul(projection, matrix, matrix); } + +static bool gles2_texture_get_dmabuf_size(struct wlr_texture *texture, struct + wl_resource *resource, int *width, int *height) { + if (!wlr_dmabuf_resource_is_buffer(resource)) { + return false; + } + + struct wlr_dmabuf_buffer *dmabuf = wlr_dmabuf_buffer_from_buffer_resource( + resource); + *width = dmabuf->attributes.width; + *height = dmabuf->attributes.height; + return true; +} + + static void gles2_texture_get_buffer_size(struct wlr_texture *texture, struct wl_resource *resource, int *width, int *height) { struct wl_shm_buffer *buffer = wl_shm_buffer_get(resource); @@ -250,10 +301,12 @@ static void gles2_texture_get_buffer_size(struct wlr_texture *texture, struct } if (!wlr_egl_query_buffer(tex->egl, resource, EGL_WIDTH, (EGLint*)width)) { - wlr_log(L_ERROR, "could not get size of the buffer " - "(no buffer found)"); - return; - }; + if (!gles2_texture_get_dmabuf_size(texture, resource, + width, height)) { + wlr_log(L_ERROR, "could not get size of the buffer"); + return; + } + } wlr_egl_query_buffer(tex->egl, resource, EGL_HEIGHT, (EGLint*)height); @@ -292,6 +345,7 @@ static struct wlr_texture_impl wlr_texture_impl = { .upload_shm = gles2_texture_upload_shm, .update_shm = gles2_texture_update_shm, .upload_drm = gles2_texture_upload_drm, + .upload_dmabuf = gles2_texture_upload_dmabuf, .upload_eglimage = gles2_texture_upload_eglimage, .get_matrix = gles2_texture_get_matrix, .get_buffer_size = gles2_texture_get_buffer_size, diff --git a/render/wlr_texture.c b/render/wlr_texture.c index a82a16b2..e4e6b9ff 100644 --- a/render/wlr_texture.c +++ b/render/wlr_texture.c @@ -53,6 +53,11 @@ bool wlr_texture_upload_eglimage(struct wlr_texture *texture, return texture->impl->upload_eglimage(texture, image, width, height); } +bool wlr_texture_upload_dmabuf(struct wlr_texture *texture, + struct wl_resource *dmabuf_resource) { + return texture->impl->upload_dmabuf(texture, dmabuf_resource); +} + void wlr_texture_get_matrix(struct wlr_texture *texture, float (*matrix)[16], const float (*projection)[16], int x, int y) { texture->impl->get_matrix(texture, matrix, projection, x, y); diff --git a/types/meson.build b/types/meson.build index 703b06ca..11aa5e20 100644 --- a/types/meson.build +++ b/types/meson.build @@ -9,6 +9,7 @@ lib_wlr_types = static_library( 'wlr_idle.c', 'wlr_input_device.c', 'wlr_keyboard.c', + 'wlr_linux_dmabuf.c', 'wlr_list.c', 'wlr_output_damage.c', 'wlr_output_layout.c', diff --git a/types/wlr_linux_dmabuf.c b/types/wlr_linux_dmabuf.c new file mode 100644 index 00000000..407a5717 --- /dev/null +++ b/types/wlr_linux_dmabuf.c @@ -0,0 +1,464 @@ +#ifndef _POSIX_C_SOURCE +#define _POSIX_C_SOURCE 200809L +#endif +#include +#include +#include +#include +#include +#include +#include +#include +#include "linux-dmabuf-unstable-v1-protocol.h" + +static void wl_buffer_destroy(struct wl_client *client, + struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static const struct wl_buffer_interface wl_buffer_impl = { + wl_buffer_destroy, +}; + +bool wlr_dmabuf_buffer_has_inverted_y(struct wlr_dmabuf_buffer *dmabuf) { + return dmabuf->attributes.flags + & ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT; +} + +bool wlr_dmabuf_resource_is_buffer(struct wl_resource *buffer_resource) { + if (!wl_resource_instance_of(buffer_resource, &wl_buffer_interface, + &wl_buffer_impl)) { + return false; + } + + struct wlr_dmabuf_buffer *buffer = wl_resource_get_user_data(buffer_resource); + if (buffer && buffer->buffer_resource && !buffer->params_resource && + buffer->buffer_resource == buffer_resource) { + return true; + } + + return false; +} + +struct wlr_dmabuf_buffer *wlr_dmabuf_buffer_from_buffer_resource( + struct wl_resource *buffer_resource) { + assert(wl_resource_instance_of(buffer_resource, &wl_buffer_interface, + &wl_buffer_impl)); + + struct wlr_dmabuf_buffer *buffer = wl_resource_get_user_data(buffer_resource); + assert(buffer); + assert(buffer->buffer_resource); + assert(!buffer->params_resource); + assert(buffer->buffer_resource == buffer_resource); + + return buffer; +} + +static void linux_dmabuf_buffer_destroy(struct wlr_dmabuf_buffer *buffer) { + for (int i = 0; i < buffer->attributes.n_planes; i++) { + close(buffer->attributes.fd[i]); + buffer->attributes.fd[i] = -1; + } + buffer->attributes.n_planes = 0; + free(buffer); +} + +static void params_destroy(struct wl_client *client, struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static void params_add(struct wl_client *client, + struct wl_resource *params_resource, int32_t name_fd, + uint32_t plane_idx, uint32_t offset, uint32_t stride, + uint32_t modifier_hi, uint32_t modifier_lo) { + struct wlr_dmabuf_buffer *buffer = wlr_dmabuf_buffer_from_params_resource( + params_resource); + + if (!buffer) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_ALREADY_USED, + "params was already used to create a wl_buffer"); + close(name_fd); + return; + } + + if (plane_idx >= WLR_LINUX_DMABUF_MAX_PLANES) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_PLANE_IDX, + "plane index %u > %u", plane_idx, WLR_LINUX_DMABUF_MAX_PLANES); + close(name_fd); + return; + } + + if (buffer->attributes.fd[plane_idx] != -1) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_PLANE_SET, + "a dmabuf with id %d has already been added for plane %u", + buffer->attributes.fd[plane_idx], + plane_idx); + close(name_fd); + return; + } + + buffer->attributes.fd[plane_idx] = name_fd; + buffer->attributes.offset[plane_idx] = offset; + buffer->attributes.stride[plane_idx] = stride; + buffer->attributes.modifier[plane_idx] = ((uint64_t)modifier_hi << 32) | + modifier_lo; + buffer->attributes.n_planes++; +} + +static void handle_buffer_destroy(struct wl_resource *buffer_resource) +{ + struct wlr_dmabuf_buffer *buffer = wlr_dmabuf_buffer_from_buffer_resource( + buffer_resource); + + linux_dmabuf_buffer_destroy(buffer); +} + +static void params_create_common(struct wl_client *client, + struct wl_resource *params_resource, uint32_t buffer_id, int32_t width, + int32_t height, uint32_t format, uint32_t flags) { + if (!wl_resource_get_user_data(params_resource)) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_ALREADY_USED, + "params was already used to create a wl_buffer"); + return; + } + struct wlr_dmabuf_buffer *buffer = wlr_dmabuf_buffer_from_params_resource( + params_resource); + + /* Switch the linux_dmabuf_buffer object from params resource to + * eventually wl_buffer resource. */ + wl_resource_set_user_data(buffer->params_resource, NULL); + buffer->params_resource = NULL; + + if (!buffer->attributes.n_planes) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INCOMPLETE, + "no dmabuf has been added to the params"); + goto err_out; + } + + /* TODO: support more planes */ + if (buffer->attributes.n_planes != 1) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INCOMPLETE, + "only single plane buffers supported not %d", + buffer->attributes.n_planes); + goto err_out; + } + + if (buffer->attributes.fd[0] == -1) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INCOMPLETE, + "no dmabuf has been added for plane"); + goto err_out; + } + + buffer->attributes.width = width; + buffer->attributes.height = height; + buffer->attributes.format = format; + buffer->attributes.flags = flags; + + if (width < 1 || height < 1) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INVALID_DIMENSIONS, + "invalid width %d or height %d", width, height); + goto err_out; + } + + if ((uint64_t)buffer->attributes.offset[0] + buffer->attributes.stride[0] > UINT32_MAX) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS, + "size overflow for plane"); + goto err_out; + } + + if ((uint64_t)buffer->attributes.offset[0] + + (uint64_t) buffer->attributes.stride[0] * height > UINT32_MAX) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS, + "size overflow for plane"); + goto err_out; + } + + off_t size = lseek(buffer->attributes.fd[0], 0, SEEK_END); + if (size != -1) { /* Skip checks if kernel does no support seek on buffer */ + if (buffer->attributes.offset[0] >= size) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS, + "invalid offset %i for plane", + buffer->attributes.offset[0]); + goto err_out; + } + + if (buffer->attributes.offset[0] + buffer->attributes.stride[0] > size) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS, + "invalid stride %i for plane", + buffer->attributes.stride[0]); + goto err_out; + } + + if (buffer->attributes.offset[0] + buffer->attributes.stride[0] * height > size) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_OUT_OF_BOUNDS, + "invalid buffer stride or height for plane"); + goto err_out; + } + } + + /* reject unknown flags */ + if (buffer->attributes.flags & ~ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT) { + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INVALID_FORMAT, + "Unknown dmabuf flags %"PRIu32, buffer->attributes.flags); + goto err_out; + } + + /* Check if dmabuf is usable */ + if (!wlr_egl_check_import_dmabuf(buffer->egl, buffer)) { + goto err_failed; + } + + buffer->buffer_resource = wl_resource_create(client, &wl_buffer_interface, + 1, buffer_id); + if (!buffer->buffer_resource) { + wl_resource_post_no_memory(params_resource); + goto err_failed; + } + + wl_resource_set_implementation(buffer->buffer_resource, + &wl_buffer_impl, buffer, handle_buffer_destroy); + + /* send 'created' event when the request is not for an immediate + * import, that is buffer_id is zero */ + if (buffer_id == 0) { + zwp_linux_buffer_params_v1_send_created(params_resource, + buffer->buffer_resource); + } + return; + +err_failed: + if (buffer_id == 0) { + zwp_linux_buffer_params_v1_send_failed(params_resource); + } else { + /* since the behavior is left implementation defined by the + * protocol in case of create_immed failure due to an unknown cause, + * we choose to treat it as a fatal error and immediately kill the + * client instead of creating an invalid handle and waiting for it + * to be used. + */ + wl_resource_post_error(params_resource, + ZWP_LINUX_BUFFER_PARAMS_V1_ERROR_INVALID_WL_BUFFER, + "importing the supplied dmabufs failed"); + } +err_out: + linux_dmabuf_buffer_destroy(buffer); + return; +} + +static void params_create(struct wl_client *client, + struct wl_resource *params_resource, + int32_t width, int32_t height,uint32_t format, uint32_t flags) { + params_create_common(client, params_resource, 0, width, height, format, flags); +} + +static void params_create_immed(struct wl_client *client, + struct wl_resource *params_resource, uint32_t buffer_id, + int32_t width, int32_t height,uint32_t format, uint32_t flags) { + params_create_common(client, params_resource, buffer_id, width, height, format, flags); +} + +static const struct zwp_linux_buffer_params_v1_interface linux_buffer_params_impl = { + params_destroy, + params_add, + params_create, + params_create_immed, +}; + +struct wlr_dmabuf_buffer *wlr_dmabuf_buffer_from_params_resource( + struct wl_resource *params_resource) { + assert(wl_resource_instance_of(params_resource, + &zwp_linux_buffer_params_v1_interface, + &linux_buffer_params_impl)); + + struct wlr_dmabuf_buffer *buffer = wl_resource_get_user_data(params_resource); + assert(buffer); + assert(buffer->params_resource); + assert(!buffer->buffer_resource); + assert(buffer->params_resource == params_resource); + + return buffer; +} + +static void handle_params_destroy(struct wl_resource *params_resource) { + /* Check for NULL since wlr_dmabuf_buffer_from_params_resource will choke */ + if (!wl_resource_get_user_data(params_resource)) { + return; + } + + struct wlr_dmabuf_buffer *buffer = + wlr_dmabuf_buffer_from_params_resource(params_resource); + linux_dmabuf_buffer_destroy(buffer); +} + +static void linux_dmabuf_create_params(struct wl_client *client, + struct wl_resource *linux_dmabuf_resource, + uint32_t params_id) { + struct wlr_linux_dmabuf *linux_dmabuf = wlr_linux_dmabuf_from_resource( + linux_dmabuf_resource); + + uint32_t version = wl_resource_get_version(linux_dmabuf_resource); + struct wlr_dmabuf_buffer *buffer = calloc(1, sizeof *buffer); + if (!buffer) { + goto err; + } + + for (int i = 0; i < WLR_LINUX_DMABUF_MAX_PLANES; i++) { + buffer->attributes.fd[i] = -1; + } + + buffer->egl = linux_dmabuf->egl; + buffer->params_resource = wl_resource_create(client, + &zwp_linux_buffer_params_v1_interface, + version, params_id); + if (!buffer->params_resource) { + goto err_free; + } + + wl_resource_set_implementation(buffer->params_resource, + &linux_buffer_params_impl,buffer, handle_params_destroy); + return; + +err_free: + free(buffer); +err: + wl_resource_post_no_memory(linux_dmabuf_resource); +} + +static void linux_dmabuf_destroy(struct wl_client *client, struct wl_resource *resource) { + wl_resource_destroy(resource); +} + +static const struct zwp_linux_dmabuf_v1_interface linux_dmabuf_impl = { + linux_dmabuf_destroy, + linux_dmabuf_create_params +}; + +struct wlr_linux_dmabuf *wlr_linux_dmabuf_from_resource( + struct wl_resource *resource) { + assert(wl_resource_instance_of(resource, &zwp_linux_dmabuf_v1_interface, + &linux_dmabuf_impl)); + + struct wlr_linux_dmabuf *dmabuf = wl_resource_get_user_data(resource); + assert(dmabuf); + return dmabuf; +} + +static void linux_dmabuf_send_modifiers(struct wlr_linux_dmabuf *linux_dmabuf, + struct wl_resource *resource) { + struct wlr_egl *egl = linux_dmabuf->egl; + /* + * Use EGL_EXT_image_dma_buf_import_modifiers to query and advertise + * format/modifier codes. + */ + uint64_t modifier_invalid = DRM_FORMAT_MOD_INVALID; + int *formats = NULL; + int num_formats = wlr_egl_get_dmabuf_formats(egl, &formats); + + if (num_formats < 0) { + return; + } + + for (int i = 0; i < num_formats; i++) { + int num_modifiers; + uint64_t *modifiers = NULL; + + num_modifiers = wlr_egl_get_dmabuf_modifiers(egl, formats[i], + &modifiers); + if (num_modifiers < 0) { + return; + } + /* send DRM_FORMAT_MOD_INVALID token when no modifiers are supported + * for this format */ + if (num_modifiers == 0) { + num_modifiers = 1; + modifiers = &modifier_invalid; + } + for (int j = 0; j < num_modifiers; j++) { + uint32_t modifier_lo = modifiers[j] & 0xFFFFFFFF; + uint32_t modifier_hi = modifiers[j] >> 32; + zwp_linux_dmabuf_v1_send_modifier(resource, formats[i], + modifier_hi, + modifier_lo); + } + if (modifiers != &modifier_invalid) { + free(modifiers); + } + } + free(formats); +} + +static void linux_dmabuf_bind(struct wl_client *client, + void *data, uint32_t version, uint32_t id) { + struct wlr_linux_dmabuf *linux_dmabuf = data; + struct wl_resource *resource = wl_resource_create(client, + &zwp_linux_dmabuf_v1_interface, + version, id); + + if (resource == NULL) { + wl_client_post_no_memory(client); + return; + } + + wl_resource_set_implementation(resource, &linux_dmabuf_impl, + linux_dmabuf, NULL); + if (version < ZWP_LINUX_DMABUF_V1_MODIFIER_SINCE_VERSION) { + return; + } + + linux_dmabuf_send_modifiers(linux_dmabuf, resource); +} + +void wlr_linux_dmabuf_destroy(struct wlr_linux_dmabuf *linux_dmabuf) { + if (!linux_dmabuf) { + return; + } + wl_list_remove(&linux_dmabuf->display_destroy.link); + + wl_global_destroy(linux_dmabuf->wl_global); + free(linux_dmabuf); +} + +static void handle_display_destroy(struct wl_listener *listener, void *data) { + struct wlr_linux_dmabuf *linux_dmabuf = wl_container_of(listener, linux_dmabuf, display_destroy); + wlr_linux_dmabuf_destroy(linux_dmabuf); +} + +struct wlr_linux_dmabuf *wlr_linux_dmabuf_create(struct wl_display *display, + struct wlr_egl *egl) { + struct wlr_linux_dmabuf *linux_dmabuf = + calloc(1, sizeof(struct wlr_linux_dmabuf)); + if (linux_dmabuf == NULL) { + wlr_log(L_ERROR, "could not create simple dmabuf manager"); + return NULL; + } + + linux_dmabuf->display_destroy.notify = handle_display_destroy; + wl_display_add_destroy_listener(display, &linux_dmabuf->display_destroy); + + linux_dmabuf->wl_global = + wl_global_create(display, &zwp_linux_dmabuf_v1_interface, + 3, linux_dmabuf, linux_dmabuf_bind); + + linux_dmabuf->egl = egl; + if (!linux_dmabuf->wl_global) { + wlr_log(L_ERROR, "could not create linux dmabuf v1 wl global"); + free(linux_dmabuf); + return NULL; + } + + return linux_dmabuf; +} diff --git a/types/wlr_surface.c b/types/wlr_surface.c index d63fc1f1..fd5bf4f1 100644 --- a/types/wlr_surface.c +++ b/types/wlr_surface.c @@ -325,6 +325,10 @@ static void wlr_surface_apply_damage(struct wlr_surface *surface, surface->current->buffer)) { wlr_texture_upload_drm(surface->texture, surface->current->buffer); goto release; + } else if (wlr_dmabuf_resource_is_buffer( + surface->current->buffer)) { + wlr_texture_upload_dmabuf(surface->texture, surface->current->buffer); + goto release; } else { wlr_log(L_INFO, "Unknown buffer handle attached"); return; -- cgit v1.2.3 From 824a95ad19062e867178593f0937d14049422989 Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 15 Mar 2018 15:33:58 +0100 Subject: matrix: use 2D matrices --- backend/drm/drm.c | 4 +- backend/drm/renderer.c | 20 ++-- examples/output-layout.c | 6 +- examples/rotation.c | 4 +- examples/tablet.c | 2 +- examples/touch.c | 4 +- include/backend/drm/drm.h | 2 +- include/wlr/render.h | 20 ++-- include/wlr/render/interface.h | 12 +-- include/wlr/types/wlr_matrix.h | 24 ++--- include/wlr/types/wlr_output.h | 2 +- include/wlr/types/wlr_surface.h | 10 +- render/gles2/renderer.c | 24 +++-- render/gles2/shaders.c | 67 ++++++------ render/gles2/texture.c | 13 +-- render/wlr_renderer.c | 10 +- render/wlr_texture.c | 2 +- rootston/output.c | 6 +- types/wlr_matrix.c | 227 ++++++++++++++++------------------------ types/wlr_output.c | 8 +- types/wlr_surface.c | 16 ++- 21 files changed, 214 insertions(+), 269 deletions(-) (limited to 'include/wlr/render') diff --git a/backend/drm/drm.c b/backend/drm/drm.c index 1e78d301..e956f280 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -647,9 +647,9 @@ static bool wlr_drm_connector_set_cursor(struct wlr_output *output, glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); - float matrix[16]; + float matrix[9]; wlr_texture_get_matrix(plane->wlr_tex, matrix, plane->matrix, 0, 0); - wlr_render_with_matrix(plane->surf.renderer->wlr_rend, plane->wlr_tex, + wlr_render_texture_with_matrix(plane->surf.renderer->wlr_rend, plane->wlr_tex, matrix, 1.0f); glFinish(); diff --git a/backend/drm/renderer.c b/backend/drm/renderer.c index ab56e6c2..f700dd59 100644 --- a/backend/drm/renderer.c +++ b/backend/drm/renderer.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -226,20 +227,21 @@ struct gbm_bo *wlr_drm_surface_mgpu_copy(struct wlr_drm_surface *dest, wlr_drm_surface_make_current(dest, NULL); struct wlr_texture *tex = get_tex_for_bo(dest->renderer, src); + assert(tex); - static const float matrix[16] = { + static const float color[] = {0.0, 0.0, 0.0, 1.0}; + + static const float mat[9] = { [0] = 2.0f, - [3] = -1.0f, - [5] = 2.0f, - [7] = -1.0f, - [10] = 1.0f, - [15] = 1.0f, + [2] = -1.0f, + [4] = 2.0f, + [5] = -1.0f, + [8] = 1.0f, }; glViewport(0, 0, dest->width, dest->height); - glClearColor(0.0, 0.0, 0.0, 1.0); - glClear(GL_COLOR_BUFFER_BIT); - wlr_render_with_matrix(dest->renderer->wlr_rend, tex, matrix, 1.0f); + wlr_renderer_clear(dest->renderer->wlr_rend, color); + wlr_render_texture_with_matrix(dest->renderer->wlr_rend, tex, mat, 1.0f); return wlr_drm_surface_swap_buffers(dest, NULL); } diff --git a/examples/output-layout.c b/examples/output-layout.c index f4df73a0..9c914137 100644 --- a/examples/output-layout.c +++ b/examples/output-layout.c @@ -111,7 +111,7 @@ static void handle_output_frame(struct output_state *output, .width = 128, .height = 128, }; if (wlr_output_layout_intersects(sample->layout, output->output, &box)) { - float matrix[16]; + float matrix[9]; // transform global coordinates to local coordinates double local_x = sample->x_offs; @@ -121,8 +121,8 @@ static void handle_output_frame(struct output_state *output, wlr_texture_get_matrix(sample->cat_texture, matrix, wlr_output->transform_matrix, local_x, local_y); - wlr_render_with_matrix(sample->renderer, - sample->cat_texture, matrix, 1.0f); + wlr_render_texture_with_matrix(sample->renderer, sample->cat_texture, + matrix, 1.0f); } wlr_renderer_end(sample->renderer); diff --git a/examples/rotation.c b/examples/rotation.c index 0b0c6adf..4431f60a 100644 --- a/examples/rotation.c +++ b/examples/rotation.c @@ -46,12 +46,12 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts wlr_renderer_begin(sample->renderer, wlr_output); wlr_renderer_clear(sample->renderer, (float[]){0.25f, 0.25f, 0.25f, 1}); - float matrix[16]; + float matrix[9]; for (int y = -128 + (int)odata->y_offs; y < height; y += 128) { for (int x = -128 + (int)odata->x_offs; x < width; x += 128) { wlr_texture_get_matrix(sample->cat_texture, matrix, wlr_output->transform_matrix, x, y); - wlr_render_with_matrix(sample->renderer, + wlr_render_texture_with_matrix(sample->renderer, sample->cat_texture, matrix, 1.0f); } } diff --git a/examples/tablet.c b/examples/tablet.c index 82f86553..521447b9 100644 --- a/examples/tablet.c +++ b/examples/tablet.c @@ -49,7 +49,7 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts wlr_renderer_begin(sample->renderer, wlr_output); wlr_renderer_clear(sample->renderer, (float[]){0.25f, 0.25f, 0.25f, 1}); - float matrix[16]; + float matrix[9]; float distance = 0.8f * (1 - sample->distance); float tool_color[4] = { distance, distance, distance, 1 }; for (size_t i = 0; sample->button && i < 4; ++i) { diff --git a/examples/touch.c b/examples/touch.c index 6f4821ad..47bbebc2 100644 --- a/examples/touch.c +++ b/examples/touch.c @@ -45,14 +45,14 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts wlr_renderer_begin(sample->renderer, wlr_output); wlr_renderer_clear(sample->renderer, (float[]){0.25f, 0.25f, 0.25f, 1}); - float matrix[16]; + float matrix[9]; struct touch_point *p; wl_list_for_each(p, &sample->touch_points, link) { wlr_texture_get_matrix(sample->cat_texture, matrix, wlr_output->transform_matrix, (int)(p->x * width) - sample->cat_texture->width / 2, (int)(p->y * height) - sample->cat_texture->height / 2); - wlr_render_with_matrix(sample->renderer, sample->cat_texture, + wlr_render_texture_with_matrix(sample->renderer, sample->cat_texture, matrix, 1.0f); } diff --git a/include/backend/drm/drm.h b/include/backend/drm/drm.h index ee3fd38e..26189340 100644 --- a/include/backend/drm/drm.h +++ b/include/backend/drm/drm.h @@ -26,7 +26,7 @@ struct wlr_drm_plane { struct wlr_drm_surface mgpu_surf; // Only used by cursor - float matrix[16]; + float matrix[9]; struct wlr_texture *wlr_tex; struct gbm_bo *cursor_bo; bool cursor_enabled; diff --git a/include/wlr/render.h b/include/wlr/render.h index 50c2dd13..d1498b07 100644 --- a/include/wlr/render.h +++ b/include/wlr/render.h @@ -30,26 +30,26 @@ struct wlr_texture *wlr_render_texture_create(struct wlr_renderer *r); * * struct wlr_renderer *renderer; * struct wlr_texture *texture; - * float projection[16]; - * float matrix[16]; - * wlr_texture_get_matrix(texture, &matrix, &projection, 123, 321); - * wlr_render_with_matrix(renderer, texture, &matrix, 0.5f); + * float projection[9]; + * float matrix[9]; + * wlr_texture_get_matrix(texture, matrix, projection, 123, 321); + * wlr_render_texture_with_matrix(renderer, texture, matrix, 0.5f); * * This will render the texture at <123, 321> with an alpha channel of 0.5. */ -bool wlr_render_with_matrix(struct wlr_renderer *r, - struct wlr_texture *texture, const float matrix[static 16], float alpha); +bool wlr_render_texture_with_matrix(struct wlr_renderer *r, + struct wlr_texture *texture, const float matrix[static 9], float alpha); /** * Renders a solid quad in the specified color. */ void wlr_render_colored_quad(struct wlr_renderer *r, - const float color[static 4], const float matrix[static 16]); + const float color[static 4], const float matrix[static 9]); /** * Renders a solid ellipse in the specified color. */ void wlr_render_colored_ellipse(struct wlr_renderer *r, - const float color[static 4], const float matrix[static 16]); + const float color[static 4], const float matrix[static 9]); /** * Returns a list of pixel formats supported by this renderer. */ @@ -139,8 +139,8 @@ bool wlr_texture_update_shm(struct wlr_texture *surf, uint32_t format, * width) and [0, height], and the x and y coordinates provided are used as * such. */ -void wlr_texture_get_matrix(struct wlr_texture *texture, float mat[static 16], - const float projection[static 16], int x, int y); +void wlr_texture_get_matrix(struct wlr_texture *texture, float mat[static 9], + const float projection[static 9], int x, int y); /** * Destroys this wlr_texture. */ diff --git a/include/wlr/render/interface.h b/include/wlr/render/interface.h index 4d81bdf9..03b8309f 100644 --- a/include/wlr/render/interface.h +++ b/include/wlr/render/interface.h @@ -21,13 +21,13 @@ struct wlr_renderer_impl { void (*clear)(struct wlr_renderer *renderer, const float color[static 4]); void (*scissor)(struct wlr_renderer *renderer, struct wlr_box *box); struct wlr_texture *(*texture_create)(struct wlr_renderer *renderer); - bool (*render_with_matrix)(struct wlr_renderer *renderer, - struct wlr_texture *texture, const float matrix[static 16], + bool (*render_texture_with_matrix)(struct wlr_renderer *renderer, + struct wlr_texture *texture, const float matrix[static 9], float alpha); void (*render_quad)(struct wlr_renderer *renderer, - const float color[static 4], const float matrix[static 16]); + const float color[static 4], const float matrix[static 9]); void (*render_ellipse)(struct wlr_renderer *renderer, - const float color[static 4], const float matrix[static 16]); + const float color[static 4], const float matrix[static 9]); const enum wl_shm_format *(*formats)( struct wlr_renderer *renderer, size_t *len); bool (*buffer_is_drm)(struct wlr_renderer *renderer, @@ -59,8 +59,8 @@ struct wlr_texture_impl { struct wl_resource *drm_buf); bool (*upload_eglimage)(struct wlr_texture *texture, EGLImageKHR image, uint32_t width, uint32_t height); - void (*get_matrix)(struct wlr_texture *state, float mat[static 16], - const float projection[static 16], int x, int y); + void (*get_matrix)(struct wlr_texture *state, float mat[static 9], + const float projection[static 9], int x, int y); void (*get_buffer_size)(struct wlr_texture *texture, struct wl_resource *resource, int *width, int *height); void (*bind)(struct wlr_texture *texture); diff --git a/include/wlr/types/wlr_matrix.h b/include/wlr/types/wlr_matrix.h index cf5ad20d..5255980a 100644 --- a/include/wlr/types/wlr_matrix.h +++ b/include/wlr/types/wlr_matrix.h @@ -2,22 +2,22 @@ #define WLR_TYPES_WLR_MATRIX_H #include +#include #include -void wlr_matrix_identity(float mat[static 16]); -void wlr_matrix_translate(float mat[static 16], float x, float y, float z); -void wlr_matrix_scale(float mat[static 16], float x, float y, float z); -void wlr_matrix_rotate(float mat[static 16], float radians); -void wlr_matrix_mul(float mat[static 16], const float x[static 16], - const float y[static 16]); - -enum wl_output_transform; -void wlr_matrix_transform(float mat[static 16], +void wlr_matrix_identity(float mat[static 9]); +void wlr_matrix_translate(float mat[static 9], float x, float y); +void wlr_matrix_scale(float mat[static 9], float x, float y); +void wlr_matrix_rotate(float mat[static 9], float rad); +void wlr_matrix_multiply(float mat[static 9], const float a[static 9], + const float b[static 9]); +void wlr_matrix_transform(float mat[static 9], enum wl_output_transform transform); -void wlr_matrix_texture(float mat[static 16], int32_t width, int32_t height, + +void wlr_matrix_texture(float mat[static 9], int32_t width, int32_t height, enum wl_output_transform transform); -void wlr_matrix_project_box(float mat[static 16], const struct wlr_box *box, +void wlr_matrix_project_box(float mat[static 9], const struct wlr_box *box, enum wl_output_transform transform, float rotation, - const float projection[static 16]); + const float projection[static 9]); #endif diff --git a/include/wlr/types/wlr_output.h b/include/wlr/types/wlr_output.h index a8138a80..b838a737 100644 --- a/include/wlr/types/wlr_output.h +++ b/include/wlr/types/wlr_output.h @@ -76,7 +76,7 @@ struct wlr_output { // damage for cursors and fullscreen surface, in output-local coordinates pixman_region32_t damage; bool frame_pending; - float transform_matrix[16]; + float transform_matrix[9]; struct { struct wl_signal frame; diff --git a/include/wlr/types/wlr_surface.h b/include/wlr/types/wlr_surface.h index 1d4c1f01..2bfd1bc9 100644 --- a/include/wlr/types/wlr_surface.h +++ b/include/wlr/types/wlr_surface.h @@ -70,8 +70,8 @@ struct wlr_surface { struct wlr_surface_state *current, *pending; const char *role; // the lifetime-bound role or null - float buffer_to_surface_matrix[16]; - float surface_to_buffer_matrix[16]; + float buffer_to_surface_matrix[9]; + float surface_to_buffer_matrix[9]; struct { struct wl_signal commit; @@ -109,9 +109,9 @@ struct wlr_surface *wlr_surface_create(struct wl_resource *res, * from 0 to 1 in both dimensions. */ void wlr_surface_get_matrix(struct wlr_surface *surface, - float mat[static 16], - const float projection[static 16], - const float transform[static 16]); + float mat[static 9], + const float projection[static 9], + const float transform[static 9]); /** diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c index 9134a2fd..e0a98d29 100644 --- a/render/gles2/renderer.c +++ b/render/gles2/renderer.c @@ -85,11 +85,13 @@ static void init_default_shaders() { if (!compile_program(quad_vertex_src, quad_fragment_src, &shaders.quad)) { goto error; } - if (!compile_program(quad_vertex_src, ellipse_fragment_src, &shaders.ellipse)) { + if (!compile_program(quad_vertex_src, ellipse_fragment_src, + &shaders.ellipse)) { goto error; } if (glEGLImageTargetTexture2DOES) { - if (!compile_program(quad_vertex_src, fragment_src_external, &shaders.external)) { + if (!compile_program(quad_vertex_src, fragment_src_external, + &shaders.external)) { goto error; } } @@ -170,16 +172,16 @@ static void draw_quad() { GL_CALL(glDisableVertexAttribArray(1)); } -static bool wlr_gles2_render_texture(struct wlr_renderer *wlr_renderer, - struct wlr_texture *texture, const float matrix[static 16], - float alpha) { +static bool wlr_gles2_render_texture_with_matrix( + struct wlr_renderer *wlr_renderer, struct wlr_texture *texture, + const float matrix[static 9], float alpha) { if (!texture || !texture->valid) { wlr_log(L_ERROR, "attempt to render invalid texture"); return false; } wlr_texture_bind(texture); - GL_CALL(glUniformMatrix4fv(0, 1, GL_FALSE, matrix)); + GL_CALL(glUniformMatrix3fv(0, 1, GL_FALSE, matrix)); GL_CALL(glUniform1f(2, alpha)); draw_quad(); return true; @@ -187,17 +189,17 @@ static bool wlr_gles2_render_texture(struct wlr_renderer *wlr_renderer, static void wlr_gles2_render_quad(struct wlr_renderer *wlr_renderer, - const float color[static 4], const float matrix[static 16]) { + const float color[static 4], const float matrix[static 9]) { GL_CALL(glUseProgram(shaders.quad)); - GL_CALL(glUniformMatrix4fv(0, 1, GL_FALSE, matrix)); + GL_CALL(glUniformMatrix3fv(0, 1, GL_FALSE, matrix)); GL_CALL(glUniform4f(1, color[0], color[1], color[2], color[3])); draw_quad(); } static void wlr_gles2_render_ellipse(struct wlr_renderer *wlr_renderer, - const float color[static 4], const float matrix[static 16]) { + const float color[static 4], const float matrix[static 9]) { GL_CALL(glUseProgram(shaders.ellipse)); - GL_CALL(glUniformMatrix4fv(0, 1, GL_TRUE, matrix)); + GL_CALL(glUniformMatrix3fv(0, 1, GL_TRUE, matrix)); GL_CALL(glUniform4f(1, color[0], color[1], color[2], color[3])); draw_quad(); } @@ -258,7 +260,7 @@ static struct wlr_renderer_impl wlr_renderer_impl = { .clear = wlr_gles2_clear, .scissor = wlr_gles2_scissor, .texture_create = wlr_gles2_texture_create, - .render_with_matrix = wlr_gles2_render_texture, + .render_texture_with_matrix = wlr_gles2_render_texture_with_matrix, .render_quad = wlr_gles2_render_quad, .render_ellipse = wlr_gles2_render_ellipse, .formats = wlr_gles2_formats, diff --git a/render/gles2/shaders.c b/render/gles2/shaders.c index 46a10248..9d3a67c2 100644 --- a/render/gles2/shaders.c +++ b/render/gles2/shaders.c @@ -3,29 +3,29 @@ // Colored quads const GLchar quad_vertex_src[] = -"uniform mat4 proj;" +"uniform mat3 proj;" "uniform vec4 color;" "attribute vec2 pos;" "attribute vec2 texcoord;" "varying vec4 v_color;" "varying vec2 v_texcoord;" -"mat4 transpose(in mat4 inMatrix) {" -" vec4 i0 = inMatrix[0];" -" vec4 i1 = inMatrix[1];" -" vec4 i2 = inMatrix[2];" -" vec4 i3 = inMatrix[3];" -" mat4 outMatrix = mat4(" -" vec4(i0.x, i1.x, i2.x, i3.x)," -" vec4(i0.y, i1.y, i2.y, i3.y)," -" vec4(i0.z, i1.z, i2.z, i3.z)," -" vec4(i0.w, i1.w, i2.w, i3.w)" -" );" -" return outMatrix;" +"" +"mat3 transpose(in mat3 inMatrix) {" +" vec3 i0 = inMatrix[0];" +" vec3 i1 = inMatrix[1];" +" vec3 i2 = inMatrix[2];" +" mat3 outMatrix = mat3(" +" vec3(i0.x, i1.x, i2.x)," +" vec3(i0.y, i1.y, i2.y)," +" vec3(i0.z, i1.z, i2.z)" +" );" +" return outMatrix;" "}" +"" "void main() {" -" gl_Position = transpose(proj) * vec4(pos, 0.0, 1.0);" -" v_color = color;" -" v_texcoord = texcoord;" +" gl_Position = vec4(transpose(proj) * vec3(pos, 1.0), 1.0);" +" v_color = color;" +" v_texcoord = texcoord;" "}"; const GLchar quad_fragment_src[] = @@ -49,26 +49,25 @@ const GLchar ellipse_fragment_src[] = // Textured quads const GLchar vertex_src[] = -"uniform mat4 proj;" +"uniform mat3 proj;" "attribute vec2 pos;" "attribute vec2 texcoord;" "varying vec2 v_texcoord;" -"mat4 transpose(in mat4 inMatrix) {" -" vec4 i0 = inMatrix[0];" -" vec4 i1 = inMatrix[1];" -" vec4 i2 = inMatrix[2];" -" vec4 i3 = inMatrix[3];" -" mat4 outMatrix = mat4(" -" vec4(i0.x, i1.x, i2.x, i3.x)," -" vec4(i0.y, i1.y, i2.y, i3.y)," -" vec4(i0.z, i1.z, i2.z, i3.z)," -" vec4(i0.w, i1.w, i2.w, i3.w)" -" );" "" -" return outMatrix;" +"mat3 transpose(in mat3 inMatrix) {" +" vec3 i0 = inMatrix[0];" +" vec3 i1 = inMatrix[1];" +" vec3 i2 = inMatrix[2];" +" mat3 outMatrix = mat3(" +" vec3(i0.x, i1.x, i2.x)," +" vec3(i0.y, i1.y, i2.y)," +" vec3(i0.z, i1.z, i2.z)" +" );" +" return outMatrix;" "}" +"" "void main() {" -" gl_Position = transpose(proj) * vec4(pos, 0.0, 1.0);" +" gl_Position = vec4(transpose(proj) * vec3(pos, 1.0), 1.0);" " v_texcoord = texcoord;" "}"; @@ -87,8 +86,8 @@ const GLchar fragment_src_rgbx[] = "uniform sampler2D tex;" "uniform float alpha;" "void main() {" -" gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb;" -" gl_FragColor.a = alpha;" +" gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb;" +" gl_FragColor.a = alpha;" "}"; const GLchar fragment_src_external[] = @@ -97,6 +96,6 @@ const GLchar fragment_src_external[] = "varying vec2 v_texcoord;" "uniform samplerExternalOES texture0;" "void main() {" -" vec4 col = texture2D(texture0, v_texcoord);" -" gl_FragColor = vec4(col.rgb, col.a);" +" vec4 col = texture2D(texture0, v_texcoord);" +" gl_FragColor = vec4(col.rgb, col.a);" "}"; diff --git a/render/gles2/texture.c b/render/gles2/texture.c index 0dfebc48..9ee2a3e3 100644 --- a/render/gles2/texture.c +++ b/render/gles2/texture.c @@ -227,16 +227,13 @@ static bool gles2_texture_upload_eglimage(struct wlr_texture *wlr_tex, } static void gles2_texture_get_matrix(struct wlr_texture *_texture, - float mat[static 16], const float projection[static 16], int x, int y) { + float mat[static 9], const float projection[static 9], int x, int y) { struct wlr_gles2_texture *texture = (struct wlr_gles2_texture *)_texture; - float world[16]; wlr_matrix_identity(mat); - wlr_matrix_translate(world, x, y, 0); - wlr_matrix_mul(mat, mat, world); - wlr_matrix_scale(world, - texture->wlr_texture.width, texture->wlr_texture.height, 1); - wlr_matrix_mul(mat, mat, world); - wlr_matrix_mul(mat, projection, mat); + wlr_matrix_translate(mat, x, y); + wlr_matrix_scale(mat, texture->wlr_texture.width, + texture->wlr_texture.height); + wlr_matrix_multiply(mat, projection, mat); } static void gles2_texture_get_buffer_size(struct wlr_texture *texture, struct diff --git a/render/wlr_renderer.c b/render/wlr_renderer.c index 372e4caf..e847fcc2 100644 --- a/render/wlr_renderer.c +++ b/render/wlr_renderer.c @@ -35,19 +35,19 @@ struct wlr_texture *wlr_render_texture_create(struct wlr_renderer *r) { return r->impl->texture_create(r); } -bool wlr_render_with_matrix(struct wlr_renderer *r, - struct wlr_texture *texture, const float matrix[static 16], +bool wlr_render_texture_with_matrix(struct wlr_renderer *r, + struct wlr_texture *texture, const float matrix[static 9], float alpha) { - return r->impl->render_with_matrix(r, texture, matrix, alpha); + return r->impl->render_texture_with_matrix(r, texture, matrix, alpha); } void wlr_render_colored_quad(struct wlr_renderer *r, - const float color[static 4], const float matrix[static 16]) { + const float color[static 4], const float matrix[static 9]) { r->impl->render_quad(r, color, matrix); } void wlr_render_colored_ellipse(struct wlr_renderer *r, - const float color[static 4], const float matrix[static 16]) { + const float color[static 4], const float matrix[static 9]) { r->impl->render_ellipse(r, color, matrix); } diff --git a/render/wlr_texture.c b/render/wlr_texture.c index 0bfac32c..1ec9beb9 100644 --- a/render/wlr_texture.c +++ b/render/wlr_texture.c @@ -54,7 +54,7 @@ bool wlr_texture_upload_eglimage(struct wlr_texture *texture, } void wlr_texture_get_matrix(struct wlr_texture *texture, - float mat[static 16], const float projection[static 16], int x, int y) { + float mat[static 9], const float projection[static 9], int x, int y) { texture->impl->get_matrix(texture, mat, projection, x, y); } diff --git a/rootston/output.c b/rootston/output.c index 47236720..29fb1202 100644 --- a/rootston/output.c +++ b/rootston/output.c @@ -287,7 +287,7 @@ static void render_surface(struct wlr_surface *surface, double lx, double ly, goto damage_finish; } - float matrix[16]; + float matrix[9]; enum wl_output_transform transform = wlr_output_transform_invert(surface->current->transform); wlr_matrix_project_box(matrix, &box, transform, rotation, @@ -297,7 +297,7 @@ static void render_surface(struct wlr_surface *surface, double lx, double ly, pixman_box32_t *rects = pixman_region32_rectangles(&damage, &nrects); for (int i = 0; i < nrects; ++i) { scissor_output(output, &rects[i]); - wlr_render_with_matrix(renderer, surface->texture, matrix, data->alpha); + wlr_render_texture_with_matrix(renderer, surface->texture, matrix, data->alpha); } damage_finish: @@ -353,7 +353,7 @@ static void render_decorations(struct roots_view *view, goto damage_finish; } - float matrix[16]; + float matrix[9]; wlr_matrix_project_box(matrix, &box, WL_OUTPUT_TRANSFORM_NORMAL, view->rotation, output->wlr_output->transform_matrix); float color[] = { 0.2, 0.2, 0.2, view->alpha }; diff --git a/types/wlr_matrix.c b/types/wlr_matrix.c index a6be9908..85a294d9 100644 --- a/types/wlr_matrix.c +++ b/types/wlr_matrix.c @@ -5,143 +5,113 @@ #include #include -/* Obtains the index for the given row/column */ -static inline int mind(int row, int col) { - return (row - 1) * 4 + col - 1; -} - -void wlr_matrix_identity(float mat[static 16]) { - static const float identity[16] = { - 1.0f, 0.0f, 0.0f, 0.0f, - 0.0f, 1.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 1.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 1.0f, +void wlr_matrix_identity(float mat[static 9]) { + static const float identity[9] = { + 1.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 1.0f, }; memcpy(mat, identity, sizeof(identity)); } -void wlr_matrix_translate(float mat[static 16], float x, float y, float z) { - wlr_matrix_identity(mat); - mat[mind(1, 4)] = x; - mat[mind(2, 4)] = y; - mat[mind(3, 4)] = z; +void wlr_matrix_multiply(float mat[static 9], const float a[static 9], + const float b[static 9]) { + float product[9]; + + product[0] = a[0]*b[0] + a[1]*b[3] + a[2]*b[6]; + product[1] = a[0]*b[1] + a[1]*b[4] + a[2]*b[7]; + product[2] = a[0]*b[2] + a[1]*b[5] + a[2]*b[8]; + + product[3] = a[3]*b[0] + a[4]*b[3] + a[5]*b[6]; + product[4] = a[3]*b[1] + a[4]*b[4] + a[5]*b[7]; + product[5] = a[3]*b[2] + a[4]*b[5] + a[5]*b[8]; + + product[6] = a[6]*b[0] + a[7]*b[3] + a[8]*b[6]; + product[7] = a[6]*b[1] + a[7]*b[4] + a[8]*b[7]; + product[8] = a[6]*b[2] + a[7]*b[5] + a[8]*b[8]; + + memcpy(mat, product, sizeof(product)); } -void wlr_matrix_scale(float mat[static 16], float x, float y, float z) { - wlr_matrix_identity(mat); - mat[mind(1, 1)] = x; - mat[mind(2, 2)] = y; - mat[mind(3, 3)] = z; +void wlr_matrix_translate(float mat[static 9], float x, float y) { + float translate[9] = { + 1.0f, 0.0f, x, + 0.0f, 1.0f, y, + 0.0f, 0.0f, 1.0f, + }; + wlr_matrix_multiply(mat, mat, translate); } -void wlr_matrix_rotate(float mat[static 16], float radians) { - wlr_matrix_identity(mat); - float _cos = cosf(radians); - float _sin = sinf(radians); - mat[mind(1, 1)] = _cos; - mat[mind(1, 2)] = _sin; - mat[mind(2, 1)] = -_sin; - mat[mind(2, 2)] = _cos; +void wlr_matrix_scale(float mat[static 9], float x, float y) { + float scale[9] = { + x, 0.0f, 0.0f, + 0.0f, y, 0.0f, + 0.0f, 0.0f, 1.0f, + }; + wlr_matrix_multiply(mat, mat, scale); } -void wlr_matrix_mul(float mat[static 16], const float x[static 16], - const float y[static 16]) { - float product[16] = { - x[mind(1, 1)] * y[mind(1, 1)] + x[mind(1, 2)] * y[mind(2, 1)] + - x[mind(1, 3)] * y[mind(3, 1)] + x[mind(1, 4)] * y[mind(4, 1)], - x[mind(1, 1)] * y[mind(1, 2)] + x[mind(1, 2)] * y[mind(2, 2)] + - x[mind(1, 3)] * y[mind(3, 2)] + x[mind(1, 4)] * y[mind(4, 2)], - x[mind(1, 1)] * y[mind(1, 3)] + x[mind(1, 2)] * y[mind(2, 3)] + - x[mind(1, 3)] * y[mind(3, 3)] + x[mind(1, 4)] * y[mind(4, 3)], - x[mind(1, 1)] * y[mind(1, 4)] + x[mind(1, 2)] * y[mind(2, 4)] + - x[mind(1, 4)] * y[mind(3, 4)] + x[mind(1, 4)] * y[mind(4, 4)], - - x[mind(2, 1)] * y[mind(1, 1)] + x[mind(2, 2)] * y[mind(2, 1)] + - x[mind(2, 3)] * y[mind(3, 1)] + x[mind(2, 4)] * y[mind(4, 1)], - x[mind(2, 1)] * y[mind(1, 2)] + x[mind(2, 2)] * y[mind(2, 2)] + - x[mind(2, 3)] * y[mind(3, 2)] + x[mind(2, 4)] * y[mind(4, 2)], - x[mind(2, 1)] * y[mind(1, 3)] + x[mind(2, 2)] * y[mind(2, 3)] + - x[mind(2, 3)] * y[mind(3, 3)] + x[mind(2, 4)] * y[mind(4, 3)], - x[mind(2, 1)] * y[mind(1, 4)] + x[mind(2, 2)] * y[mind(2, 4)] + - x[mind(2, 4)] * y[mind(3, 4)] + x[mind(2, 4)] * y[mind(4, 4)], - - x[mind(3, 1)] * y[mind(1, 1)] + x[mind(3, 2)] * y[mind(2, 1)] + - x[mind(3, 3)] * y[mind(3, 1)] + x[mind(3, 4)] * y[mind(4, 1)], - x[mind(3, 1)] * y[mind(1, 2)] + x[mind(3, 2)] * y[mind(2, 2)] + - x[mind(3, 3)] * y[mind(3, 2)] + x[mind(3, 4)] * y[mind(4, 2)], - x[mind(3, 1)] * y[mind(1, 3)] + x[mind(3, 2)] * y[mind(2, 3)] + - x[mind(3, 3)] * y[mind(3, 3)] + x[mind(3, 4)] * y[mind(4, 3)], - x[mind(3, 1)] * y[mind(1, 4)] + x[mind(3, 2)] * y[mind(2, 4)] + - x[mind(3, 4)] * y[mind(3, 4)] + x[mind(3, 4)] * y[mind(4, 4)], - - x[mind(4, 1)] * y[mind(1, 1)] + x[mind(4, 2)] * y[mind(2, 1)] + - x[mind(4, 3)] * y[mind(3, 1)] + x[mind(4, 4)] * y[mind(4, 1)], - x[mind(4, 1)] * y[mind(1, 2)] + x[mind(4, 2)] * y[mind(2, 2)] + - x[mind(4, 3)] * y[mind(3, 2)] + x[mind(4, 4)] * y[mind(4, 2)], - x[mind(4, 1)] * y[mind(1, 3)] + x[mind(4, 2)] * y[mind(2, 3)] + - x[mind(4, 3)] * y[mind(3, 3)] + x[mind(4, 4)] * y[mind(4, 3)], - x[mind(4, 1)] * y[mind(1, 4)] + x[mind(4, 2)] * y[mind(2, 4)] + - x[mind(4, 4)] * y[mind(3, 4)] + x[mind(4, 4)] * y[mind(4, 4)], +void wlr_matrix_rotate(float mat[static 9], float rad) { + float rotate[9] = { + cos(rad), -sin(rad), 0.0f, + sin(rad), cos(rad), 0.0f, + 0.0f, 0.0f, 1.0f, }; - memcpy(mat, product, sizeof(product)); + wlr_matrix_multiply(mat, mat, rotate); } -static const float transforms[][4] = { +static const float transforms[][9] = { [WL_OUTPUT_TRANSFORM_NORMAL] = { - 1.0f, 0.0f, - 0.0f, 1.0f, + 1.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 1.0f, }, [WL_OUTPUT_TRANSFORM_90] = { - 0.0f, -1.0f, - 1.0f, 0.0f, + 0.0f, -1.0f, 0.0f, + 1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, }, [WL_OUTPUT_TRANSFORM_180] = { - -1.0f, 0.0f, - 0.0f, -1.0f, + -1.0f, 0.0f, 0.0f, + 0.0f, -1.0f, 0.0f, + 0.0f, 0.0f, 1.0f, }, [WL_OUTPUT_TRANSFORM_270] = { - 0.0f, 1.0f, - -1.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + -1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, }, [WL_OUTPUT_TRANSFORM_FLIPPED] = { - -1.0f, 0.0f, - 0.0f, 1.0f, + -1.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 1.0f, }, [WL_OUTPUT_TRANSFORM_FLIPPED_90] = { - 0.0f, -1.0f, - -1.0f, 0.0f, + 0.0f, -1.0f, 0.0f, + -1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, }, [WL_OUTPUT_TRANSFORM_FLIPPED_180] = { - 1.0f, 0.0f, - 0.0f, -1.0f, + 1.0f, 0.0f, 0.0f, + 0.0f, -1.0f, 0.0f, + 0.0f, 0.0f, 1.0f, }, [WL_OUTPUT_TRANSFORM_FLIPPED_270] = { - 0.0f, 1.0f, - 1.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + 1.0f, 0.0f, 0.0f, + 0.0f, 0.0f, 1.0f, }, }; -void wlr_matrix_transform(float mat[static 16], +void wlr_matrix_transform(float mat[static 9], enum wl_output_transform transform) { - memset(mat, 0, sizeof(*mat) * 16); - - const float *t = transforms[transform]; - - // Rotation + reflection - mat[0] = t[0]; - mat[1] = t[1]; - mat[4] = t[2]; - mat[5] = t[3]; - - // Identity - mat[10] = 1.0f; - mat[15] = 1.0f; + wlr_matrix_multiply(mat, mat, transforms[transform]); } // Equivilent to glOrtho(0, width, 0, height, 1, -1) with the transform applied -void wlr_matrix_texture(float mat[static 16], int32_t width, int32_t height, +void wlr_matrix_texture(float mat[static 9], int32_t width, int32_t height, enum wl_output_transform transform) { - memset(mat, 0, sizeof(*mat) * 16); + memset(mat, 0, sizeof(*mat) * 9); const float *t = transforms[transform]; float x = 2.0f / width; @@ -150,62 +120,41 @@ void wlr_matrix_texture(float mat[static 16], int32_t width, int32_t height, // Rotation + reflection mat[0] = x * t[0]; mat[1] = x * t[1]; - mat[4] = y * -t[2]; - mat[5] = y * -t[3]; + mat[3] = y * -t[3]; + mat[4] = y * -t[4]; // Translation - mat[3] = -copysign(1.0f, mat[0] + mat[1]); - mat[7] = -copysign(1.0f, mat[4] + mat[5]); + mat[2] = -copysign(1.0f, mat[0] + mat[1]); + mat[5] = -copysign(1.0f, mat[3] + mat[4]); // Identity - mat[10] = 1.0f; - mat[15] = 1.0f; + mat[8] = 1.0f; } -void wlr_matrix_project_box(float mat[static 16], const struct wlr_box *box, +void wlr_matrix_project_box(float mat[static 9], const struct wlr_box *box, enum wl_output_transform transform, float rotation, - const float projection[static 16]) { + const float projection[static 9]) { int x = box->x; int y = box->y; int width = box->width; int height = box->height; - wlr_matrix_translate(mat, x, y, 0); + wlr_matrix_identity(mat); + wlr_matrix_translate(mat, x, y); if (rotation != 0) { - float translate_center[16]; - wlr_matrix_translate(translate_center, width/2, height/2, 0); - - float rotate[16]; - wlr_matrix_rotate(rotate, rotation); - - float translate_origin[16]; - wlr_matrix_translate(translate_origin, -width/2, -height/2, 0); - - wlr_matrix_mul(mat, mat, translate_center); - wlr_matrix_mul(mat, mat, rotate); - wlr_matrix_mul(mat, mat, translate_origin); + wlr_matrix_translate(mat, width/2, height/2); + wlr_matrix_rotate(mat, rotation); + wlr_matrix_translate(mat, -width/2, -height/2); } - float scale[16]; - wlr_matrix_scale(scale, width, height, 1); - - wlr_matrix_mul(mat, mat, scale); + wlr_matrix_scale(mat, width, height); if (transform != WL_OUTPUT_TRANSFORM_NORMAL) { - float surface_translate_center[16]; - wlr_matrix_translate(surface_translate_center, 0.5, 0.5, 0); - - float surface_transform[16]; - wlr_matrix_transform(surface_transform, transform); - - float surface_translate_origin[16]; - wlr_matrix_translate(surface_translate_origin, -0.5, -0.5, 0); - - wlr_matrix_mul(mat, mat, surface_translate_center); - wlr_matrix_mul(mat, mat, surface_transform); - wlr_matrix_mul(mat, mat, surface_translate_origin); + wlr_matrix_translate(mat, 0.5, 0.5); + wlr_matrix_transform(mat, transform); + wlr_matrix_translate(mat, -0.5, -0.5); } - wlr_matrix_mul(mat, projection, mat); + wlr_matrix_multiply(mat, projection, mat); } diff --git a/types/wlr_output.c b/types/wlr_output.c index a9fb01c5..9a0edd44 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -375,7 +375,7 @@ static void output_fullscreen_surface_render(struct wlr_output *output, struct wlr_box box; output_fullscreen_surface_get_box(output, surface, &box); - float matrix[16]; + float matrix[9]; enum wl_output_transform transform = wlr_output_transform_invert(surface->current->transform); wlr_matrix_project_box(matrix, &box, transform, 0, @@ -386,7 +386,7 @@ static void output_fullscreen_surface_render(struct wlr_output *output, for (int i = 0; i < nrects; ++i) { output_scissor(output, &rects[i]); wlr_renderer_clear(renderer, (float[]){0, 0, 0, 0}); - wlr_render_with_matrix(surface->renderer, surface->texture, matrix, 1.0f); + wlr_render_texture_with_matrix(surface->renderer, surface->texture, matrix, 1.0f); } wlr_renderer_scissor(renderer, NULL); @@ -435,7 +435,7 @@ static void output_cursor_render(struct wlr_output_cursor *cursor, goto surface_damage_finish; } - float matrix[16]; + float matrix[9]; wlr_matrix_project_box(matrix, &box, WL_OUTPUT_TRANSFORM_NORMAL, 0, cursor->output->transform_matrix); @@ -443,7 +443,7 @@ static void output_cursor_render(struct wlr_output_cursor *cursor, pixman_box32_t *rects = pixman_region32_rectangles(&surface_damage, &nrects); for (int i = 0; i < nrects; ++i) { output_scissor(cursor->output, &rects[i]); - wlr_render_with_matrix(renderer, texture, matrix, 1.0f); + wlr_render_texture_with_matrix(renderer, texture, matrix, 1.0f); } wlr_renderer_scissor(renderer, NULL); diff --git a/types/wlr_surface.c b/types/wlr_surface.c index 04dcb141..ecab4842 100644 --- a/types/wlr_surface.c +++ b/types/wlr_surface.c @@ -624,20 +624,16 @@ struct wlr_surface *wlr_surface_create(struct wl_resource *res, return surface; } -void wlr_surface_get_matrix(struct wlr_surface *surface, - float mat[static 16], - const float projection[static 16], - const float transform[static 16]) { +void wlr_surface_get_matrix(struct wlr_surface *surface, float mat[static 9], + const float projection[static 9], const float transform[static 9]) { int width = surface->texture->width; int height = surface->texture->height; - float scale[16]; wlr_matrix_identity(mat); - if (transform) { - wlr_matrix_mul(mat, mat, transform); + if (transform != NULL) { + wlr_matrix_multiply(mat, mat, transform); } - wlr_matrix_scale(scale, width, height, 1); - wlr_matrix_mul(mat, mat, scale); - wlr_matrix_mul(mat, projection, mat); + wlr_matrix_scale(mat, width, height); + wlr_matrix_multiply(mat, projection, mat); } bool wlr_surface_has_buffer(struct wlr_surface *surface) { -- cgit v1.2.3 From 876f07e9f13946609047e8e32ca7918548315e1c Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 15 Mar 2018 19:31:02 +0100 Subject: renderer: replace wlr_texture_get_matrix by wlr_render_texture --- backend/drm/drm.c | 6 ++---- examples/output-layout.c | 8 ++------ examples/rotation.c | 7 ++----- examples/touch.c | 11 ++++------- include/wlr/render.h | 41 ++++++++++++----------------------------- include/wlr/render/interface.h | 2 -- render/gles2/texture.c | 14 -------------- render/wlr_renderer.c | 12 ++++++++++++ render/wlr_texture.c | 5 ----- 9 files changed, 34 insertions(+), 72 deletions(-) (limited to 'include/wlr/render') diff --git a/backend/drm/drm.c b/backend/drm/drm.c index e956f280..0447d865 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -647,10 +647,8 @@ static bool wlr_drm_connector_set_cursor(struct wlr_output *output, glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); - float matrix[9]; - wlr_texture_get_matrix(plane->wlr_tex, matrix, plane->matrix, 0, 0); - wlr_render_texture_with_matrix(plane->surf.renderer->wlr_rend, plane->wlr_tex, - matrix, 1.0f); + wlr_render_texture(plane->surf.renderer->wlr_rend, plane->wlr_tex, + plane->matrix, 0, 0, 1.0f); glFinish(); glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, bo_stride); diff --git a/examples/output-layout.c b/examples/output-layout.c index 9c914137..8f506208 100644 --- a/examples/output-layout.c +++ b/examples/output-layout.c @@ -111,18 +111,14 @@ static void handle_output_frame(struct output_state *output, .width = 128, .height = 128, }; if (wlr_output_layout_intersects(sample->layout, output->output, &box)) { - float matrix[9]; - // transform global coordinates to local coordinates double local_x = sample->x_offs; double local_y = sample->y_offs; wlr_output_layout_output_coords(sample->layout, output->output, &local_x, &local_y); - wlr_texture_get_matrix(sample->cat_texture, matrix, - wlr_output->transform_matrix, local_x, local_y); - wlr_render_texture_with_matrix(sample->renderer, sample->cat_texture, - matrix, 1.0f); + wlr_render_texture(sample->renderer, sample->cat_texture, + wlr_output->transform_matrix, local_x, local_y, 1.0f); } wlr_renderer_end(sample->renderer); diff --git a/examples/rotation.c b/examples/rotation.c index 4431f60a..aaf006cf 100644 --- a/examples/rotation.c +++ b/examples/rotation.c @@ -46,13 +46,10 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts wlr_renderer_begin(sample->renderer, wlr_output); wlr_renderer_clear(sample->renderer, (float[]){0.25f, 0.25f, 0.25f, 1}); - float matrix[9]; for (int y = -128 + (int)odata->y_offs; y < height; y += 128) { for (int x = -128 + (int)odata->x_offs; x < width; x += 128) { - wlr_texture_get_matrix(sample->cat_texture, matrix, - wlr_output->transform_matrix, x, y); - wlr_render_texture_with_matrix(sample->renderer, - sample->cat_texture, matrix, 1.0f); + wlr_render_texture(sample->renderer, sample->cat_texture, + wlr_output->transform_matrix, x, y, 1.0f); } } diff --git a/examples/touch.c b/examples/touch.c index 47bbebc2..0af8bc4a 100644 --- a/examples/touch.c +++ b/examples/touch.c @@ -45,15 +45,12 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts wlr_renderer_begin(sample->renderer, wlr_output); wlr_renderer_clear(sample->renderer, (float[]){0.25f, 0.25f, 0.25f, 1}); - float matrix[9]; struct touch_point *p; wl_list_for_each(p, &sample->touch_points, link) { - wlr_texture_get_matrix(sample->cat_texture, matrix, - wlr_output->transform_matrix, - (int)(p->x * width) - sample->cat_texture->width / 2, - (int)(p->y * height) - sample->cat_texture->height / 2); - wlr_render_texture_with_matrix(sample->renderer, sample->cat_texture, - matrix, 1.0f); + int x = (int)(p->x * width) - sample->cat_texture->width / 2; + int y = (int)(p->y * height) - sample->cat_texture->height / 2; + wlr_render_texture(sample->renderer, sample->cat_texture, + wlr_output->transform_matrix, x, y, 1.0f); } wlr_renderer_end(sample->renderer); diff --git a/include/wlr/render.h b/include/wlr/render.h index c3bf5c97..01d05fb1 100644 --- a/include/wlr/render.h +++ b/include/wlr/render.h @@ -25,21 +25,15 @@ void wlr_renderer_scissor(struct wlr_renderer *r, struct wlr_box *box); */ struct wlr_texture *wlr_render_texture_create(struct wlr_renderer *r); /** - * Renders the requested texture using the provided matrix. A typical texture - * rendering goes like so: - * - * struct wlr_renderer *renderer; - * struct wlr_texture *texture; - * float projection[9]; - * float matrix[9]; - * wlr_texture_get_matrix(texture, matrix, projection, 123, 321); - * wlr_render_texture_with_matrix(renderer, texture, matrix, 0.5f); - * - * This will render the texture at <123, 321> with an alpha channel of 0.5. + * Renders the requested texture. + */ +bool wlr_render_texture(struct wlr_renderer *r, struct wlr_texture *texture, + const float projection[static 9], int x, int y, float alpha); +/** + * Renders the requested texture using the provided matrix. */ bool wlr_render_texture_with_matrix(struct wlr_renderer *r, struct wlr_texture *texture, const float matrix[static 9], float alpha); - /** * Renders a solid quad in the specified color. */ @@ -95,22 +89,22 @@ struct wlr_texture { * returns. */ bool wlr_texture_upload_pixels(struct wlr_texture *tex, - enum wl_shm_format format, int stride, int width, int height, - const unsigned char *pixels); + enum wl_shm_format format, int stride, int width, int height, + const unsigned char *pixels); /** * Copies pixels to this texture. The buffer is not accessed after this function * returns. Under some circumstances, this function may re-upload the entire * buffer - therefore, the entire buffer must be valid. */ bool wlr_texture_update_pixels(struct wlr_texture *surf, - enum wl_shm_format format, int stride, int x, int y, - int width, int height, const unsigned char *pixels); + enum wl_shm_format format, int stride, int x, int y, + int width, int height, const unsigned char *pixels); /** * Copies pixels from a wl_shm_buffer into this texture. The buffer is not * accessed after this function returns. */ bool wlr_texture_upload_shm(struct wlr_texture *tex, uint32_t format, - struct wl_shm_buffer *shm); + struct wl_shm_buffer *shm); /** * Attaches the contents from the given wl_drm wl_buffer resource onto the @@ -132,18 +126,7 @@ bool wlr_texture_upload_dmabuf(struct wlr_texture *tex, * must be valid. */ bool wlr_texture_update_shm(struct wlr_texture *surf, uint32_t format, - int x, int y, int width, int height, struct wl_shm_buffer *shm); -/** - * Prepares a matrix with the appropriate scale for the given texture and - * multiplies it with the projection, producing a matrix that the shader can - * muptlipy vertex coordinates with to get final screen coordinates. - * - * The projection matrix is assumed to be an orthographic projection of [0, - * width) and [0, height], and the x and y coordinates provided are used as - * such. - */ -void wlr_texture_get_matrix(struct wlr_texture *texture, float mat[static 9], - const float projection[static 9], int x, int y); + int x, int y, int width, int height, struct wl_shm_buffer *shm); /** * Destroys this wlr_texture. */ diff --git a/include/wlr/render/interface.h b/include/wlr/render/interface.h index c8b4c8eb..1b8b7946 100644 --- a/include/wlr/render/interface.h +++ b/include/wlr/render/interface.h @@ -62,8 +62,6 @@ struct wlr_texture_impl { uint32_t width, uint32_t height); bool (*upload_dmabuf)(struct wlr_texture *texture, struct wl_resource *dmabuf_resource); - void (*get_matrix)(struct wlr_texture *state, float mat[static 9], - const float projection[static 9], int x, int y); void (*get_buffer_size)(struct wlr_texture *texture, struct wl_resource *resource, int *width, int *height); void (*bind)(struct wlr_texture *texture); diff --git a/render/gles2/texture.c b/render/gles2/texture.c index 875affe2..ea05cde2 100644 --- a/render/gles2/texture.c +++ b/render/gles2/texture.c @@ -227,7 +227,6 @@ static bool gles2_texture_upload_eglimage(struct wlr_texture *wlr_tex, return true; } - static bool gles2_texture_upload_dmabuf(struct wlr_texture *_tex, struct wl_resource *dmabuf_resource) { struct wlr_gles2_texture *tex = (struct wlr_gles2_texture *)_tex; @@ -263,17 +262,6 @@ static bool gles2_texture_upload_dmabuf(struct wlr_texture *_tex, return true; } -static void gles2_texture_get_matrix(struct wlr_texture *_texture, - float mat[static 9], const float projection[static 9], int x, int y) { - struct wlr_gles2_texture *texture = (struct wlr_gles2_texture *)_texture; - wlr_matrix_identity(mat); - wlr_matrix_translate(mat, x, y); - wlr_matrix_scale(mat, texture->wlr_texture.width, - texture->wlr_texture.height); - wlr_matrix_multiply(mat, projection, mat); -} - - static bool gles2_texture_get_dmabuf_size(struct wlr_texture *texture, struct wl_resource *resource, int *width, int *height) { if (!wlr_dmabuf_resource_is_buffer(resource)) { @@ -287,7 +275,6 @@ static bool gles2_texture_get_dmabuf_size(struct wlr_texture *texture, struct return true; } - static void gles2_texture_get_buffer_size(struct wlr_texture *texture, struct wl_resource *resource, int *width, int *height) { struct wl_shm_buffer *buffer = wl_shm_buffer_get(resource); @@ -344,7 +331,6 @@ static struct wlr_texture_impl wlr_texture_impl = { .upload_drm = gles2_texture_upload_drm, .upload_dmabuf = gles2_texture_upload_dmabuf, .upload_eglimage = gles2_texture_upload_eglimage, - .get_matrix = gles2_texture_get_matrix, .get_buffer_size = gles2_texture_get_buffer_size, .bind = gles2_texture_bind, .destroy = gles2_texture_destroy, diff --git a/render/wlr_renderer.c b/render/wlr_renderer.c index e847fcc2..c4f91d5e 100644 --- a/render/wlr_renderer.c +++ b/render/wlr_renderer.c @@ -1,6 +1,7 @@ #include #include #include +#include void wlr_renderer_init(struct wlr_renderer *renderer, struct wlr_renderer_impl *impl) { @@ -35,6 +36,17 @@ struct wlr_texture *wlr_render_texture_create(struct wlr_renderer *r) { return r->impl->texture_create(r); } +bool wlr_render_texture(struct wlr_renderer *r, struct wlr_texture *texture, + const float projection[static 9], int x, int y, float alpha) { + float mat[9]; + wlr_matrix_identity(mat); + wlr_matrix_translate(mat, x, y); + wlr_matrix_scale(mat, texture->width, texture->height); + wlr_matrix_multiply(mat, projection, mat); + + return wlr_render_texture_with_matrix(r, texture, mat, alpha); +} + bool wlr_render_texture_with_matrix(struct wlr_renderer *r, struct wlr_texture *texture, const float matrix[static 9], float alpha) { diff --git a/render/wlr_texture.c b/render/wlr_texture.c index 3685185a..48f76c8e 100644 --- a/render/wlr_texture.c +++ b/render/wlr_texture.c @@ -58,11 +58,6 @@ bool wlr_texture_upload_dmabuf(struct wlr_texture *texture, return texture->impl->upload_dmabuf(texture, dmabuf_resource); } -void wlr_texture_get_matrix(struct wlr_texture *texture, - float mat[static 9], const float projection[static 9], int x, int y) { - texture->impl->get_matrix(texture, mat, projection, x, y); -} - void wlr_texture_get_buffer_size(struct wlr_texture *texture, struct wl_resource *resource, int *width, int *height) { texture->impl->get_buffer_size(texture, resource, width, height); -- cgit v1.2.3 From c41de2d1be5c8e814e99e3a1859cdaa885b6042d Mon Sep 17 00:00:00 2001 From: emersion Date: Mon, 19 Mar 2018 23:16:29 +0100 Subject: render: split render.h into wlr_renderer.h and wlr_texture.h --- backend/drm/drm.c | 2 +- backend/drm/renderer.c | 2 +- examples/idle-inhibit.c | 2 +- examples/idle.c | 6 +- examples/multi-pointer.c | 28 ++++---- examples/output-layout.c | 26 +++---- examples/pointer.c | 28 ++++---- examples/rotation.c | 16 ++--- examples/simple.c | 6 +- examples/tablet.c | 20 +++--- examples/touch.c | 20 +++--- include/backend/drm/renderer.h | 2 +- include/backend/wayland.h | 2 +- include/render/gles2.h | 3 +- include/rootston/server.h | 2 +- include/wlr/render.h | 135 ------------------------------------- include/wlr/render/gles2.h | 2 +- include/wlr/render/interface.h | 5 +- include/wlr/render/wlr_renderer.h | 75 +++++++++++++++++++++ include/wlr/render/wlr_texture.h | 69 +++++++++++++++++++ include/wlr/types/wlr_compositor.h | 2 +- render/gles2/renderer.c | 2 +- render/gles2/texture.c | 2 +- render/wlr_renderer.c | 1 + render/wlr_texture.c | 1 + rootston/main.c | 2 +- types/wlr_linux_dmabuf.c | 1 - types/wlr_output.c | 2 +- types/wlr_screenshooter.c | 2 +- 29 files changed, 239 insertions(+), 227 deletions(-) delete mode 100644 include/wlr/render.h create mode 100644 include/wlr/render/wlr_renderer.h create mode 100644 include/wlr/render/wlr_texture.h (limited to 'include/wlr/render') diff --git a/backend/drm/drm.c b/backend/drm/drm.c index fd436659..344756f8 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -15,8 +15,8 @@ #include #include #include -#include #include +#include #include #include #include diff --git a/backend/drm/renderer.c b/backend/drm/renderer.c index 97fba95a..7e330990 100644 --- a/backend/drm/renderer.c +++ b/backend/drm/renderer.c @@ -7,9 +7,9 @@ #include #include #include -#include #include #include +#include #include #include #include "backend/drm/drm.h" diff --git a/examples/idle-inhibit.c b/examples/idle-inhibit.c index b7b9c21c..c09e6507 100644 --- a/examples/idle-inhibit.c +++ b/examples/idle-inhibit.c @@ -5,8 +5,8 @@ #include #include #include -#include "xdg-shell-client-protocol.h" #include "idle-inhibit-unstable-v1-client-protocol.h" +#include "xdg-shell-client-protocol.h" #include diff --git a/examples/idle.c b/examples/idle.c index 2b155c68..b65a81cf 100644 --- a/examples/idle.c +++ b/examples/idle.c @@ -1,11 +1,11 @@ +#include +#include #include #include #include #include -#include -#include -#include #include +#include #include #include "idle-client-protocol.h" diff --git a/examples/multi-pointer.c b/examples/multi-pointer.c index 6e56bfdc..43ccdb66 100644 --- a/examples/multi-pointer.c +++ b/examples/multi-pointer.c @@ -1,30 +1,30 @@ #define _POSIX_C_SOURCE 199309L #define _XOPEN_SOURCE 500 +#include +#include +#include #include #include -#include #include +#include #include -#include -#include -#include #include -#include -#include -#include -#include -#include +#include #include #include +#include +#include +#include #include +#include +#include #include -#include -#include #include -#include -#include "support/shared.h" -#include "support/config.h" +#include +#include #include "support/cat.h" +#include "support/config.h" +#include "support/shared.h" struct sample_state; diff --git a/examples/output-layout.c b/examples/output-layout.c index 8f506208..de134a71 100644 --- a/examples/output-layout.c +++ b/examples/output-layout.c @@ -1,29 +1,29 @@ #define _POSIX_C_SOURCE 199309L #define _XOPEN_SOURCE 700 +#include +#include +#include #include #include -#include #include #include +#include #include -#include -#include #include -#include -#include -#include -#include -#include -#include +#include #include #include +#include +#include +#include +#include #include #include -#include -#include -#include "support/shared.h" -#include "support/config.h" +#include +#include #include "support/cat.h" +#include "support/config.h" +#include "support/shared.h" struct sample_state { struct example_config *config; diff --git a/examples/pointer.c b/examples/pointer.c index e80b346a..e8a0e892 100644 --- a/examples/pointer.c +++ b/examples/pointer.c @@ -1,30 +1,30 @@ #define _POSIX_C_SOURCE 199309L #define _XOPEN_SOURCE 500 +#include +#include +#include #include #include -#include #include +#include #include -#include -#include -#include #include -#include -#include -#include -#include -#include +#include #include #include +#include +#include +#include #include +#include +#include #include -#include -#include #include -#include -#include "support/shared.h" -#include "support/config.h" +#include +#include #include "support/cat.h" +#include "support/config.h" +#include "support/shared.h" struct sample_state { struct compositor_state *compositor; diff --git a/examples/rotation.c b/examples/rotation.c index aaf006cf..7f50b620 100644 --- a/examples/rotation.c +++ b/examples/rotation.c @@ -1,23 +1,23 @@ #define _POSIX_C_SOURCE 199309L #define _XOPEN_SOURCE 500 +#include +#include #include #include -#include #include #include +#include #include -#include #include -#include -#include -#include -#include -#include +#include #include #include +#include +#include #include +#include #include -#include +#include #include "support/shared.h" #include "support/config.h" #include "support/cat.h" diff --git a/examples/simple.c b/examples/simple.c index 90808b0f..79db4a0c 100644 --- a/examples/simple.c +++ b/examples/simple.c @@ -1,11 +1,11 @@ #define _POSIX_C_SOURCE 199309L -#include +#include +#include #include #include +#include #include -#include #include -#include #include #include #include diff --git a/examples/tablet.c b/examples/tablet.c index 521447b9..1b995003 100644 --- a/examples/tablet.c +++ b/examples/tablet.c @@ -1,27 +1,27 @@ #define _POSIX_C_SOURCE 199309L #define _XOPEN_SOURCE 500 +#include +#include #include #include -#include #include +#include #include -#include #include -#include -#include -#include -#include -#include +#include #include #include +#include +#include #include +#include #include -#include #include +#include #include -#include -#include "support/shared.h" +#include #include "support/cat.h" +#include "support/shared.h" struct sample_state { struct wlr_renderer *renderer; diff --git a/examples/touch.c b/examples/touch.c index 0af8bc4a..0968e82a 100644 --- a/examples/touch.c +++ b/examples/touch.c @@ -1,25 +1,25 @@ #define _POSIX_C_SOURCE 199309L #define _XOPEN_SOURCE 500 +#include +#include +#include #include #include -#include #include +#include #include -#include -#include -#include #include -#include -#include -#include -#include -#include +#include #include #include +#include +#include #include +#include #include -#include "support/shared.h" +#include #include "support/cat.h" +#include "support/shared.h" struct sample_state { struct wlr_renderer *renderer; diff --git a/include/backend/drm/renderer.h b/include/backend/drm/renderer.h index a56a8673..73adfc27 100644 --- a/include/backend/drm/renderer.h +++ b/include/backend/drm/renderer.h @@ -5,7 +5,7 @@ #include #include #include -#include +#include struct wlr_drm_backend; struct wlr_drm_plane; diff --git a/include/backend/wayland.h b/include/backend/wayland.h index 16c8e13b..b68208af 100644 --- a/include/backend/wayland.h +++ b/include/backend/wayland.h @@ -7,8 +7,8 @@ #include #include #include -#include #include +#include #include struct wlr_wl_backend { diff --git a/include/render/gles2.h b/include/render/gles2.h index e015160d..e8124050 100644 --- a/include/render/gles2.h +++ b/include/render/gles2.h @@ -9,9 +9,10 @@ #include #include #include -#include #include #include +#include +#include #include extern PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES; diff --git a/include/rootston/server.h b/include/rootston/server.h index 5f35dd90..7ab15682 100644 --- a/include/rootston/server.h +++ b/include/rootston/server.h @@ -5,7 +5,7 @@ #include #include #include -#include +#include #include #ifdef WLR_HAS_XWAYLAND #include diff --git a/include/wlr/render.h b/include/wlr/render.h deleted file mode 100644 index 01d05fb1..00000000 --- a/include/wlr/render.h +++ /dev/null @@ -1,135 +0,0 @@ -#ifndef WLR_RENDER_H -#define WLR_RENDER_H - -#include -#include -#include -#include -#include -#include - -struct wlr_texture; -struct wlr_renderer; - -void wlr_renderer_begin(struct wlr_renderer *r, struct wlr_output *output); -void wlr_renderer_end(struct wlr_renderer *r); -void wlr_renderer_clear(struct wlr_renderer *r, const float color[static 4]); -/** - * Defines a scissor box. Only pixels that lie within the scissor box can be - * modified by drawing functions. Providing a NULL `box` disables the scissor - * box. - */ -void wlr_renderer_scissor(struct wlr_renderer *r, struct wlr_box *box); -/** - * Requests a texture handle from this renderer. - */ -struct wlr_texture *wlr_render_texture_create(struct wlr_renderer *r); -/** - * Renders the requested texture. - */ -bool wlr_render_texture(struct wlr_renderer *r, struct wlr_texture *texture, - const float projection[static 9], int x, int y, float alpha); -/** - * Renders the requested texture using the provided matrix. - */ -bool wlr_render_texture_with_matrix(struct wlr_renderer *r, - struct wlr_texture *texture, const float matrix[static 9], float alpha); -/** - * Renders a solid quad in the specified color. - */ -void wlr_render_colored_quad(struct wlr_renderer *r, - const float color[static 4], const float matrix[static 9]); -/** - * Renders a solid ellipse in the specified color. - */ -void wlr_render_colored_ellipse(struct wlr_renderer *r, - const float color[static 4], const float matrix[static 9]); -/** - * 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); -/** - * Returns true if this wl_buffer is a DRM buffer. - */ -bool wlr_renderer_buffer_is_drm(struct wlr_renderer *renderer, - struct wl_resource *buffer); -/** - * Reads out of pixels of the currently bound surface into data. `stride` is in - * bytes. - */ -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); -/** - * Checks if a format is supported. - */ -bool wlr_renderer_format_supported(struct wlr_renderer *r, - enum wl_shm_format fmt); -/** - * Destroys this wlr_renderer. Textures must be destroyed separately. - */ -void wlr_renderer_destroy(struct wlr_renderer *renderer); - -struct wlr_texture_impl; - -struct wlr_texture { - struct wlr_texture_impl *impl; - - bool valid; - uint32_t format; - int width, height; - bool inverted_y; - struct wl_signal destroy_signal; - struct wl_resource *resource; -}; - -/** - * Copies pixels to this texture. The buffer is not accessed after this function - * returns. - */ -bool wlr_texture_upload_pixels(struct wlr_texture *tex, - enum wl_shm_format format, int stride, int width, int height, - const unsigned char *pixels); -/** - * Copies pixels to this texture. The buffer is not accessed after this function - * returns. Under some circumstances, this function may re-upload the entire - * buffer - therefore, the entire buffer must be valid. - */ -bool wlr_texture_update_pixels(struct wlr_texture *surf, - enum wl_shm_format format, int stride, int x, int y, - int width, int height, const unsigned char *pixels); -/** - * Copies pixels from a wl_shm_buffer into this texture. The buffer is not - * accessed after this function returns. - */ -bool wlr_texture_upload_shm(struct wlr_texture *tex, uint32_t format, - struct wl_shm_buffer *shm); - -/** - * Attaches the contents from the given wl_drm wl_buffer resource onto the - * texture. The wl_resource is not used after this call. - * Will fail (return false) if the given resource is no drm buffer. - */ -bool wlr_texture_upload_drm(struct wlr_texture *tex, - struct wl_resource *drm_buffer); - -bool wlr_texture_upload_eglimage(struct wlr_texture *tex, - EGLImageKHR image, uint32_t width, uint32_t height); - -bool wlr_texture_upload_dmabuf(struct wlr_texture *tex, - struct wl_resource *dmabuf_resource); -/** - * Copies a rectangle of pixels from a wl_shm_buffer onto the texture. The - * buffer is not accessed after this function returns. Under some circumstances, - * this function may re-upload the entire buffer - therefore, the entire buffer - * must be valid. - */ -bool wlr_texture_update_shm(struct wlr_texture *surf, uint32_t format, - int x, int y, int width, int height, struct wl_shm_buffer *shm); -/** - * Destroys this wlr_texture. - */ -void wlr_texture_destroy(struct wlr_texture *texture); - -#endif diff --git a/include/wlr/render/gles2.h b/include/wlr/render/gles2.h index a924a065..b3b43ab2 100644 --- a/include/wlr/render/gles2.h +++ b/include/wlr/render/gles2.h @@ -2,7 +2,7 @@ #define WLR_RENDER_GLES2_H #include -#include +#include struct wlr_egl; struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_backend *backend); diff --git a/include/wlr/render/interface.h b/include/wlr/render/interface.h index 1b8b7946..f0307230 100644 --- a/include/wlr/render/interface.h +++ b/include/wlr/render/interface.h @@ -5,10 +5,11 @@ #include #include #include -#include +#include +#include #include -#include #include +#include struct wlr_renderer_impl; diff --git a/include/wlr/render/wlr_renderer.h b/include/wlr/render/wlr_renderer.h new file mode 100644 index 00000000..d5f3cf70 --- /dev/null +++ b/include/wlr/render/wlr_renderer.h @@ -0,0 +1,75 @@ +#ifndef WLR_RENDER_WLR_RENDERER_H +#define WLR_RENDER_WLR_RENDERER_H + +#include +#include +#include +#include +#include +#include + +struct wlr_output; + +struct wlr_renderer; + +void wlr_renderer_begin(struct wlr_renderer *r, struct wlr_output *output); +void wlr_renderer_end(struct wlr_renderer *r); +void wlr_renderer_clear(struct wlr_renderer *r, const float color[static 4]); +/** + * Defines a scissor box. Only pixels that lie within the scissor box can be + * modified by drawing functions. Providing a NULL `box` disables the scissor + * box. + */ +void wlr_renderer_scissor(struct wlr_renderer *r, struct wlr_box *box); +/** + * Requests a texture handle from this renderer. + */ +struct wlr_texture *wlr_render_texture_create(struct wlr_renderer *r); +/** + * Renders the requested texture. + */ +bool wlr_render_texture(struct wlr_renderer *r, struct wlr_texture *texture, + const float projection[static 9], int x, int y, float alpha); +/** + * Renders the requested texture using the provided matrix. + */ +bool wlr_render_texture_with_matrix(struct wlr_renderer *r, + struct wlr_texture *texture, const float matrix[static 9], float alpha); +/** + * Renders a solid quad in the specified color. + */ +void wlr_render_colored_quad(struct wlr_renderer *r, + const float color[static 4], const float matrix[static 9]); +/** + * Renders a solid ellipse in the specified color. + */ +void wlr_render_colored_ellipse(struct wlr_renderer *r, + const float color[static 4], const float matrix[static 9]); +/** + * 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); +/** + * Returns true if this wl_buffer is a DRM buffer. + */ +bool wlr_renderer_buffer_is_drm(struct wlr_renderer *renderer, + struct wl_resource *buffer); +/** + * Reads out of pixels of the currently bound surface into data. `stride` is in + * bytes. + */ +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); +/** + * Checks if a format is supported. + */ +bool wlr_renderer_format_supported(struct wlr_renderer *r, + enum wl_shm_format fmt); +/** + * Destroys this wlr_renderer. Textures must be destroyed separately. + */ +void wlr_renderer_destroy(struct wlr_renderer *renderer); + +#endif diff --git a/include/wlr/render/wlr_texture.h b/include/wlr/render/wlr_texture.h new file mode 100644 index 00000000..49aa93d7 --- /dev/null +++ b/include/wlr/render/wlr_texture.h @@ -0,0 +1,69 @@ +#ifndef WLR_RENDER_WLR_TEXTURE_H +#define WLR_RENDER_WLR_TEXTURE_H + +#include +#include +#include +#include + +struct wlr_texture_impl; + +struct wlr_texture { + struct wlr_texture_impl *impl; + + bool valid; + uint32_t format; + int width, height; + bool inverted_y; + struct wl_signal destroy_signal; + struct wl_resource *resource; +}; + +/** + * Copies pixels to this texture. The buffer is not accessed after this function + * returns. + */ +bool wlr_texture_upload_pixels(struct wlr_texture *tex, + enum wl_shm_format format, int stride, int width, int height, + const unsigned char *pixels); +/** + * Copies pixels to this texture. The buffer is not accessed after this function + * returns. Under some circumstances, this function may re-upload the entire + * buffer - therefore, the entire buffer must be valid. + */ +bool wlr_texture_update_pixels(struct wlr_texture *surf, + enum wl_shm_format format, int stride, int x, int y, + int width, int height, const unsigned char *pixels); +/** + * Copies pixels from a wl_shm_buffer into this texture. The buffer is not + * accessed after this function returns. + */ +bool wlr_texture_upload_shm(struct wlr_texture *tex, uint32_t format, + struct wl_shm_buffer *shm); +/** + * Attaches the contents from the given wl_drm wl_buffer resource onto the + * texture. The wl_resource is not used after this call. + * Will fail (return false) if the given resource is no drm buffer. + */ +bool wlr_texture_upload_drm(struct wlr_texture *tex, + struct wl_resource *drm_buffer); + +bool wlr_texture_upload_eglimage(struct wlr_texture *tex, + EGLImageKHR image, uint32_t width, uint32_t height); + +bool wlr_texture_upload_dmabuf(struct wlr_texture *tex, + struct wl_resource *dmabuf_resource); +/** + * Copies a rectangle of pixels from a wl_shm_buffer onto the texture. The + * buffer is not accessed after this function returns. Under some circumstances, + * this function may re-upload the entire buffer - therefore, the entire buffer + * must be valid. + */ +bool wlr_texture_update_shm(struct wlr_texture *surf, uint32_t format, + int x, int y, int width, int height, struct wl_shm_buffer *shm); +/** + * Destroys this wlr_texture. + */ +void wlr_texture_destroy(struct wlr_texture *texture); + +#endif diff --git a/include/wlr/types/wlr_compositor.h b/include/wlr/types/wlr_compositor.h index 5919b934..11bfac71 100644 --- a/include/wlr/types/wlr_compositor.h +++ b/include/wlr/types/wlr_compositor.h @@ -2,7 +2,7 @@ #define WLR_TYPES_WLR_COMPOSITOR_H #include -#include +#include struct wlr_compositor { struct wl_global *wl_global; diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c index 8d8dd17f..55797805 100644 --- a/render/gles2/renderer.c +++ b/render/gles2/renderer.c @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/render/gles2/texture.c b/render/gles2/texture.c index fc94ead5..c5f79ba8 100644 --- a/render/gles2/texture.c +++ b/render/gles2/texture.c @@ -5,7 +5,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/render/wlr_renderer.c b/render/wlr_renderer.c index c4f91d5e..21a7901a 100644 --- a/render/wlr_renderer.c +++ b/render/wlr_renderer.c @@ -1,6 +1,7 @@ #include #include #include +#include #include void wlr_renderer_init(struct wlr_renderer *renderer, diff --git a/render/wlr_texture.c b/render/wlr_texture.c index 48f76c8e..a5d0abde 100644 --- a/render/wlr_texture.c +++ b/render/wlr_texture.c @@ -1,6 +1,7 @@ #include #include #include +#include void wlr_texture_init(struct wlr_texture *texture, struct wlr_texture_impl *impl) { diff --git a/rootston/main.c b/rootston/main.c index 5450ade2..d85701ca 100644 --- a/rootston/main.c +++ b/rootston/main.c @@ -7,7 +7,7 @@ #include #include #include -#include +#include #include #include "rootston/config.h" #include "rootston/server.h" diff --git a/types/wlr_linux_dmabuf.c b/types/wlr_linux_dmabuf.c index 3b86166e..1883bc49 100644 --- a/types/wlr_linux_dmabuf.c +++ b/types/wlr_linux_dmabuf.c @@ -5,7 +5,6 @@ #include #include #include -#include #include #include #include diff --git a/types/wlr_output.c b/types/wlr_output.c index 9575f071..e30c3b78 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/types/wlr_screenshooter.c b/types/wlr_screenshooter.c index e756b6aa..e1386be6 100644 --- a/types/wlr_screenshooter.c +++ b/types/wlr_screenshooter.c @@ -3,7 +3,7 @@ #include #include #include -#include +#include #include #include #include -- cgit v1.2.3 From 95e86e675a7eef326958ab9047ff4ca0d0182cfc Mon Sep 17 00:00:00 2001 From: emersion Date: Tue, 20 Mar 2018 19:14:33 +0100 Subject: render/gles2: remove global state, use OpenGL debug extension --- include/render/gles2.h | 43 ++-- include/wlr/render/interface.h | 10 +- include/wlr/render/wlr_texture.h | 2 +- render/glapi.txt | 5 + render/gles2/pixel_format.c | 8 +- render/gles2/renderer.c | 448 ++++++++++++++++++++++++++------------- render/gles2/shaders.c | 148 ++++++------- render/gles2/texture.c | 224 +++++++++++--------- render/wlr_renderer.c | 2 +- render/wlr_texture.c | 6 +- 10 files changed, 525 insertions(+), 371 deletions(-) (limited to 'include/wlr/render') diff --git a/include/render/gles2.h b/include/render/gles2.h index 0a4fb49f..9ff09065 100644 --- a/include/render/gles2.h +++ b/include/render/gles2.h @@ -17,17 +17,24 @@ extern PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES; -struct pixel_format { +struct gles2_pixel_format { uint32_t wl_format; GLint gl_format, gl_type; int depth, bpp; - GLuint *shader; }; struct wlr_gles2_renderer { struct wlr_renderer wlr_renderer; struct wlr_egl *egl; + + struct { + GLuint quad; + GLuint ellipse; + GLuint tex_rgba; + GLuint tex_rgbx; + GLuint tex_ext; + } shaders; }; struct wlr_gles2_texture { @@ -35,37 +42,19 @@ struct wlr_gles2_texture { struct wlr_egl *egl; GLuint tex_id; - const struct pixel_format *pixel_format; + const struct gles2_pixel_format *pixel_format; EGLImageKHR image; GLenum target; }; -struct shaders { - bool initialized; - GLuint rgba, rgbx; - GLuint quad; - GLuint ellipse; - GLuint external; -}; - -extern struct shaders shaders; - -const struct pixel_format *gl_format_for_wl_format(enum wl_shm_format fmt); +const struct gles2_pixel_format *gles2_format_from_wl(enum wl_shm_format fmt); struct wlr_texture *gles2_texture_create(); +struct wlr_gles2_texture *gles2_get_texture(struct wlr_texture *wlr_texture); -extern const GLchar quad_vertex_src[]; -extern const GLchar quad_fragment_src[]; -extern const GLchar ellipse_fragment_src[]; -extern const GLchar vertex_src[]; -extern const GLchar fragment_src_rgba[]; -extern const GLchar fragment_src_rgbx[]; -extern const GLchar fragment_src_external[]; - -bool _gles2_flush_errors(const char *file, int line); -#define gles2_flush_errors(...) \ - _gles2_flush_errors(wlr_strip_path(__FILE__), __LINE__) - -#define GL_CALL(func) func; gles2_flush_errors() +void gles2_push_marker(const char *file, const char *func); +void gles2_pop_marker(void); +#define GLES2_DEBUG_PUSH gles2_push_marker(wlr_strip_path(__FILE__), __func__) +#define GLES2_DEBUG_POP gles2_pop_marker() #endif diff --git a/include/wlr/render/interface.h b/include/wlr/render/interface.h index f0307230..da0aaec9 100644 --- a/include/wlr/render/interface.h +++ b/include/wlr/render/interface.h @@ -14,7 +14,7 @@ struct wlr_renderer_impl; struct wlr_renderer { - struct wlr_renderer_impl *impl; + const struct wlr_renderer_impl *impl; }; struct wlr_renderer_impl { @@ -44,7 +44,7 @@ struct wlr_renderer_impl { }; void wlr_renderer_init(struct wlr_renderer *renderer, - struct wlr_renderer_impl *impl); + const struct wlr_renderer_impl *impl); struct wlr_texture_impl { bool (*upload_pixels)(struct wlr_texture *texture, @@ -65,14 +65,12 @@ struct wlr_texture_impl { struct wl_resource *dmabuf_resource); void (*get_buffer_size)(struct wlr_texture *texture, struct wl_resource *resource, int *width, int *height); - void (*bind)(struct wlr_texture *texture); void (*destroy)(struct wlr_texture *texture); }; void wlr_texture_init(struct wlr_texture *texture, - struct wlr_texture_impl *impl); -void wlr_texture_bind(struct wlr_texture *texture); + const struct wlr_texture_impl *impl); void wlr_texture_get_buffer_size(struct wlr_texture *texture, - struct wl_resource *resource, int *width, int *height); + struct wl_resource *resource, int *width, int *height); #endif diff --git a/include/wlr/render/wlr_texture.h b/include/wlr/render/wlr_texture.h index 49aa93d7..095097e6 100644 --- a/include/wlr/render/wlr_texture.h +++ b/include/wlr/render/wlr_texture.h @@ -9,7 +9,7 @@ struct wlr_texture_impl; struct wlr_texture { - struct wlr_texture_impl *impl; + const struct wlr_texture_impl *impl; bool valid; uint32_t format; diff --git a/render/glapi.txt b/render/glapi.txt index 02ac7dd8..a8e4aaba 100644 --- a/render/glapi.txt +++ b/render/glapi.txt @@ -10,3 +10,8 @@ eglCreatePlatformWindowSurfaceEXT -eglSwapBuffersWithDamageKHR -eglQueryDmaBufFormatsEXT -eglQueryDmaBufModifiersEXT +-eglDebugMessageControlKHR +-glDebugMessageCallbackKHR +-glDebugMessageControlKHR +-glPopDebugGroupKHR +-glPushDebugGroupKHR diff --git a/render/gles2/pixel_format.c b/render/gles2/pixel_format.c index a544077b..7c34896c 100644 --- a/render/gles2/pixel_format.c +++ b/render/gles2/pixel_format.c @@ -6,14 +6,13 @@ * 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[] = { +struct gles2_pixel_format formats[] = { { .wl_format = WL_SHM_FORMAT_ARGB8888, .depth = 32, .bpp = 32, .gl_format = GL_BGRA_EXT, .gl_type = GL_UNSIGNED_BYTE, - .shader = &shaders.rgba }, { .wl_format = WL_SHM_FORMAT_XRGB8888, @@ -21,7 +20,6 @@ struct pixel_format formats[] = { .bpp = 32, .gl_format = GL_BGRA_EXT, .gl_type = GL_UNSIGNED_BYTE, - .shader = &shaders.rgbx }, { .wl_format = WL_SHM_FORMAT_XBGR8888, @@ -29,7 +27,6 @@ struct pixel_format formats[] = { .bpp = 32, .gl_format = GL_RGBA, .gl_type = GL_UNSIGNED_BYTE, - .shader = &shaders.rgbx }, { .wl_format = WL_SHM_FORMAT_ABGR8888, @@ -37,12 +34,11 @@ struct pixel_format formats[] = { .bpp = 32, .gl_format = GL_RGBA, .gl_type = GL_UNSIGNED_BYTE, - .shader = &shaders.rgba }, }; // TODO: more pixel formats -const struct pixel_format *gl_format_for_wl_format(enum wl_shm_format fmt) { +const struct gles2_pixel_format *gles2_format_from_wl(enum wl_shm_format fmt) { for (size_t i = 0; i < sizeof(formats) / sizeof(*formats); ++i) { if (formats[i].wl_format == fmt) { return &formats[i]; diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c index f525869d..14eea666 100644 --- a/render/gles2/renderer.c +++ b/render/gles2/renderer.c @@ -2,145 +2,80 @@ #include #include #include +#include #include #include #include #include -#include #include #include +#include #include #include #include "render/gles2.h" #include "glapi.h" -struct shaders shaders; - -static bool compile_shader(GLuint type, const GLchar *src, GLuint *shader) { - *shader = GL_CALL(glCreateShader(type)); - int len = strlen(src); - GL_CALL(glShaderSource(*shader, 1, &src, &len)); - GL_CALL(glCompileShader(*shader)); - GLint success; - GL_CALL(glGetShaderiv(*shader, GL_COMPILE_STATUS, &success)); - if (success == GL_FALSE) { - GLint loglen; - GL_CALL(glGetShaderiv(*shader, GL_INFO_LOG_LENGTH, &loglen)); - GLchar msg[loglen]; - GL_CALL(glGetShaderInfoLog(*shader, loglen, &loglen, msg)); - wlr_log(L_ERROR, "Shader compilation failed"); - wlr_log(L_ERROR, "%s", msg); - glDeleteShader(*shader); - return false; - } - return true; -} +static const struct wlr_renderer_impl renderer_impl; -static bool compile_program(const GLchar *vert_src, - const GLchar *frag_src, GLuint *program) { - GLuint vertex, fragment; - if (!compile_shader(GL_VERTEX_SHADER, vert_src, &vertex)) { - return false; - } - if (!compile_shader(GL_FRAGMENT_SHADER, frag_src, &fragment)) { - glDeleteShader(vertex); - return false; - } - *program = GL_CALL(glCreateProgram()); - GL_CALL(glAttachShader(*program, vertex)); - GL_CALL(glAttachShader(*program, fragment)); - GL_CALL(glLinkProgram(*program)); - GLint success; - GL_CALL(glGetProgramiv(*program, GL_LINK_STATUS, &success)); - if (success == GL_FALSE) { - GLint loglen; - GL_CALL(glGetProgramiv(*program, GL_INFO_LOG_LENGTH, &loglen)); - GLchar msg[loglen]; - GL_CALL(glGetProgramInfoLog(*program, loglen, &loglen, msg)); - wlr_log(L_ERROR, "Program link failed"); - wlr_log(L_ERROR, "%s", msg); - glDeleteProgram(*program); - glDeleteShader(vertex); - glDeleteShader(fragment); - return false; - } - glDetachShader(*program, vertex); - glDetachShader(*program, fragment); - glDeleteShader(vertex); - glDeleteShader(fragment); - - return true; +static struct wlr_gles2_renderer *gles2_get_renderer( + struct wlr_renderer *wlr_renderer) { + assert(wlr_renderer->impl == &renderer_impl); + struct wlr_gles2_renderer *renderer = + (struct wlr_gles2_renderer *)wlr_renderer; + assert(eglGetCurrentContext() == renderer->egl->context); + return renderer; } -static void init_default_shaders() { - if (shaders.initialized) { - return; - } - if (!compile_program(vertex_src, fragment_src_rgba, &shaders.rgba)) { - goto error; - } - if (!compile_program(vertex_src, fragment_src_rgbx, &shaders.rgbx)) { - goto error; - } - if (!compile_program(quad_vertex_src, quad_fragment_src, &shaders.quad)) { - goto error; - } - if (!compile_program(quad_vertex_src, ellipse_fragment_src, - &shaders.ellipse)) { - goto error; - } - if (glEGLImageTargetTexture2DOES) { - if (!compile_program(vertex_src, fragment_src_external, - &shaders.external)) { - goto error; - } - } - - wlr_log(L_DEBUG, "Compiled default shaders"); - shaders.initialized = true; - return; -error: - wlr_log(L_ERROR, "Failed to set up default shaders!"); -} +static void gles2_begin(struct wlr_renderer *wlr_renderer, + struct wlr_output *output) { + gles2_get_renderer(wlr_renderer); -static void init_globals() { - init_default_shaders(); -} + GLES2_DEBUG_PUSH; -static void wlr_gles2_begin(struct wlr_renderer *wlr_renderer, - struct wlr_output *output) { - GL_CALL(glViewport(0, 0, output->width, output->height)); + glViewport(0, 0, output->width, output->height); // enable transparency - GL_CALL(glEnable(GL_BLEND)); - GL_CALL(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - // Note: maybe we should save output projection and remove some of the need + // XXX: maybe we should save output projection and remove some of the need // for users to sling matricies themselves + + GLES2_DEBUG_POP; } -static void wlr_gles2_end(struct wlr_renderer *wlr_renderer) { +static void gles2_end(struct wlr_renderer *wlr_renderer) { + gles2_get_renderer(wlr_renderer); // no-op } -static void wlr_gles2_clear(struct wlr_renderer *wlr_renderer, +static void gles2_clear(struct wlr_renderer *wlr_renderer, const float color[static 4]) { + gles2_get_renderer(wlr_renderer); + + GLES2_DEBUG_PUSH; glClearColor(color[0], color[1], color[2], color[3]); glClear(GL_COLOR_BUFFER_BIT); + GLES2_DEBUG_POP; } -static void wlr_gles2_scissor(struct wlr_renderer *wlr_renderer, +static void gles2_scissor(struct wlr_renderer *wlr_renderer, struct wlr_box *box) { + gles2_get_renderer(wlr_renderer); + + GLES2_DEBUG_PUSH; if (box != NULL) { glScissor(box->x, box->y, box->width, box->height); glEnable(GL_SCISSOR_TEST); } else { glDisable(GL_SCISSOR_TEST); } + GLES2_DEBUG_POP; } -static struct wlr_texture *wlr_gles2_texture_create( +static struct wlr_texture *gles2_renderer_texture_create( struct wlr_renderer *wlr_renderer) { + assert(wlr_renderer->impl == &renderer_impl); struct wlr_gles2_renderer *renderer = (struct wlr_gles2_renderer *)wlr_renderer; return gles2_texture_create(renderer->egl); @@ -160,69 +95,93 @@ static void draw_quad() { 0, 1, // bottom left }; - GL_CALL(glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts)); - GL_CALL(glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoord)); + glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, verts); + glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, texcoord); - GL_CALL(glEnableVertexAttribArray(0)); - GL_CALL(glEnableVertexAttribArray(1)); + glEnableVertexAttribArray(0); + glEnableVertexAttribArray(1); - GL_CALL(glDrawArrays(GL_TRIANGLE_STRIP, 0, 4)); + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); - GL_CALL(glDisableVertexAttribArray(0)); - GL_CALL(glDisableVertexAttribArray(1)); + glDisableVertexAttribArray(0); + glDisableVertexAttribArray(1); } -static bool wlr_gles2_render_texture_with_matrix( - struct wlr_renderer *wlr_renderer, struct wlr_texture *texture, +static bool gles2_render_texture_with_matrix( + struct wlr_renderer *wlr_renderer, struct wlr_texture *wlr_texture, const float matrix[static 9], float alpha) { - if (!texture || !texture->valid) { + struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer); + struct wlr_gles2_texture *texture = gles2_get_texture(wlr_texture); + if (!wlr_texture->valid) { wlr_log(L_ERROR, "attempt to render invalid texture"); return false; } + GLuint prog = renderer->shaders.tex_rgba; + if (texture->target == GL_TEXTURE_EXTERNAL_OES) { + prog = renderer->shaders.tex_ext; + } else if (texture->pixel_format->wl_format == WL_SHM_FORMAT_XRGB8888 || + texture->pixel_format->wl_format == WL_SHM_FORMAT_XBGR8888) { + prog = renderer->shaders.tex_rgbx; + } + // OpenGL ES 2 requires the glUniformMatrix3fv transpose parameter to be set // to GL_FALSE float transposition[9]; wlr_matrix_transpose(transposition, matrix); - wlr_texture_bind(texture); - GL_CALL(glUniformMatrix3fv(0, 1, GL_FALSE, transposition)); - GL_CALL(glUniform1i(1, texture->inverted_y)); - GL_CALL(glUniform1f(3, alpha)); + GLES2_DEBUG_PUSH; + glBindTexture(texture->target, texture->tex_id); + glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glUseProgram(prog); + + glUniformMatrix3fv(0, 1, GL_FALSE, transposition); + glUniform1i(1, wlr_texture->inverted_y); + glUniform1f(3, alpha); draw_quad(); + GLES2_DEBUG_POP; return true; } -static void wlr_gles2_render_quad(struct wlr_renderer *wlr_renderer, +static void gles2_render_quad(struct wlr_renderer *wlr_renderer, const float color[static 4], const float matrix[static 9]) { + struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer); + // OpenGL ES 2 requires the glUniformMatrix3fv transpose parameter to be set // to GL_FALSE float transposition[9]; wlr_matrix_transpose(transposition, matrix); - GL_CALL(glUseProgram(shaders.quad)); - GL_CALL(glUniformMatrix3fv(0, 1, GL_FALSE, transposition)); - GL_CALL(glUniform4f(1, color[0], color[1], color[2], color[3])); + GLES2_DEBUG_PUSH; + glUseProgram(renderer->shaders.quad); + glUniformMatrix3fv(0, 1, GL_FALSE, transposition); + glUniform4f(1, color[0], color[1], color[2], color[3]); draw_quad(); + GLES2_DEBUG_POP; } -static void wlr_gles2_render_ellipse(struct wlr_renderer *wlr_renderer, +static void gles2_render_ellipse(struct wlr_renderer *wlr_renderer, const float color[static 4], const float matrix[static 9]) { + struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer); + // OpenGL ES 2 requires the glUniformMatrix3fv transpose parameter to be set // to GL_FALSE float transposition[9]; wlr_matrix_transpose(transposition, matrix); - GL_CALL(glUseProgram(shaders.ellipse)); - GL_CALL(glUniformMatrix3fv(0, 1, GL_FALSE, transposition)); - GL_CALL(glUniform4f(1, color[0], color[1], color[2], color[3])); + GLES2_DEBUG_PUSH; + glUseProgram(renderer->shaders.ellipse); + glUniformMatrix3fv(0, 1, GL_FALSE, transposition); + glUniform4f(1, color[0], color[1], color[2], color[3]); draw_quad(); + GLES2_DEBUG_POP; } -static const enum wl_shm_format *wlr_gles2_formats( +static const enum wl_shm_format *gles2_formats( struct wlr_renderer *renderer, size_t *len) { - static enum wl_shm_format formats[] = { + static const enum wl_shm_format formats[] = { WL_SHM_FORMAT_ARGB8888, WL_SHM_FORMAT_XRGB8888, WL_SHM_FORMAT_ABGR8888, @@ -232,25 +191,29 @@ static const enum wl_shm_format *wlr_gles2_formats( return formats; } -static bool wlr_gles2_buffer_is_drm(struct wlr_renderer *wlr_renderer, +static bool gles2_buffer_is_drm(struct wlr_renderer *wlr_renderer, struct wl_resource *buffer) { - struct wlr_gles2_renderer *renderer = - (struct wlr_gles2_renderer *)wlr_renderer; + struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer); + EGLint format; - return wlr_egl_query_buffer(renderer->egl, buffer, - EGL_TEXTURE_FORMAT, &format); + return wlr_egl_query_buffer(renderer->egl, buffer, EGL_TEXTURE_FORMAT, + &format); } -static bool wlr_gles2_read_pixels(struct wlr_renderer *renderer, +static bool gles2_read_pixels(struct wlr_renderer *wlr_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); + gles2_get_renderer(wlr_renderer); + + const struct gles2_pixel_format *fmt = gles2_format_from_wl(wl_fmt); if (fmt == NULL) { wlr_log(L_ERROR, "Cannot read pixels: unsupported pixel format"); return false; } + GLES2_DEBUG_PUSH; + // Make sure any pending drawing is finished before we try to read it glFinish(); @@ -262,38 +225,225 @@ static bool wlr_gles2_read_pixels(struct wlr_renderer *renderer, fmt->gl_type, p + i * stride + dst_x * fmt->bpp / 8); } + GLES2_DEBUG_POP; + return true; } -static bool wlr_gles2_format_supported(struct wlr_renderer *r, +static bool gles2_format_supported(struct wlr_renderer *r, enum wl_shm_format wl_fmt) { - return gl_format_for_wl_format(wl_fmt); + return gles2_format_from_wl(wl_fmt) != NULL; +} + +static void gles2_destroy(struct wlr_renderer *wlr_renderer) { + struct wlr_gles2_renderer *renderer = gles2_get_renderer(wlr_renderer); + + wlr_egl_make_current(renderer->egl, EGL_NO_SURFACE, NULL); + + GLES2_DEBUG_PUSH; + glDeleteProgram(renderer->shaders.quad); + glDeleteProgram(renderer->shaders.ellipse); + glDeleteProgram(renderer->shaders.tex_rgba); + glDeleteProgram(renderer->shaders.tex_rgbx); + glDeleteProgram(renderer->shaders.tex_ext); + GLES2_DEBUG_POP; + + if (glDebugMessageCallbackKHR) { + glDisable(GL_DEBUG_OUTPUT_KHR); + glDebugMessageCallbackKHR(NULL, NULL); + } + + free(renderer); } -static struct wlr_renderer_impl wlr_renderer_impl = { - .begin = wlr_gles2_begin, - .end = wlr_gles2_end, - .clear = wlr_gles2_clear, - .scissor = wlr_gles2_scissor, - .texture_create = wlr_gles2_texture_create, - .render_texture_with_matrix = wlr_gles2_render_texture_with_matrix, - .render_quad = wlr_gles2_render_quad, - .render_ellipse = wlr_gles2_render_ellipse, - .formats = wlr_gles2_formats, - .buffer_is_drm = wlr_gles2_buffer_is_drm, - .read_pixels = wlr_gles2_read_pixels, - .format_supported = wlr_gles2_format_supported, +static const struct wlr_renderer_impl renderer_impl = { + .destroy = gles2_destroy, + .begin = gles2_begin, + .end = gles2_end, + .clear = gles2_clear, + .scissor = gles2_scissor, + .texture_create = gles2_renderer_texture_create, + .render_texture_with_matrix = gles2_render_texture_with_matrix, + .render_quad = gles2_render_quad, + .render_ellipse = gles2_render_ellipse, + .formats = gles2_formats, + .buffer_is_drm = gles2_buffer_is_drm, + .read_pixels = gles2_read_pixels, + .format_supported = gles2_format_supported, }; +void gles2_push_marker(const char *file, const char *func) { + if (!glPushDebugGroupKHR) { + return; + } + + int len = snprintf(NULL, 0, "%s:%s", file, func) + 1; + char str[len]; + snprintf(str, len, "%s:%s", file, func); + glPushDebugGroupKHR(GL_DEBUG_SOURCE_APPLICATION_KHR, 1, -1, str); +} + +void gles2_pop_marker(void) { + if (glPopDebugGroupKHR) { + glPopDebugGroupKHR(); + } +} + +static log_importance_t gles2_log_importance_to_wlr(GLenum type) { + switch (type) { + case GL_DEBUG_TYPE_ERROR_KHR: return L_ERROR; + case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR_KHR: return L_DEBUG; + case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR_KHR: return L_ERROR; + case GL_DEBUG_TYPE_PORTABILITY_KHR: return L_DEBUG; + case GL_DEBUG_TYPE_PERFORMANCE_KHR: return L_DEBUG; + case GL_DEBUG_TYPE_OTHER_KHR: return L_INFO; + case GL_DEBUG_TYPE_MARKER_KHR: return L_DEBUG; + case GL_DEBUG_TYPE_PUSH_GROUP_KHR: return L_DEBUG; + case GL_DEBUG_TYPE_POP_GROUP_KHR: return L_DEBUG; + default: return L_INFO; + } +} + +static void gles2_log(GLenum src, GLenum type, GLuint id, GLenum severity, + GLsizei len, const GLchar *msg, const void *user) { + _wlr_log(gles2_log_importance_to_wlr(type), "[GLES2] %s", msg); +} + +static GLuint compile_shader(GLuint type, const GLchar *src) { + GLES2_DEBUG_PUSH; + + GLuint shader = glCreateShader(type); + glShaderSource(shader, 1, &src, NULL); + glCompileShader(shader); + + GLint ok; + glGetShaderiv(shader, GL_COMPILE_STATUS, &ok); + if (ok == GL_FALSE) { + glDeleteShader(shader); + shader = 0; + } + + GLES2_DEBUG_POP; + return shader; +} + +static GLuint link_program(const GLchar *vert_src, const GLchar *frag_src) { + GLES2_DEBUG_PUSH; + + GLuint vert = compile_shader(GL_VERTEX_SHADER, vert_src); + if (!vert) { + goto error; + } + + GLuint frag = compile_shader(GL_FRAGMENT_SHADER, frag_src); + if (!frag) { + glDeleteShader(vert); + goto error; + } + + GLuint prog = glCreateProgram(); + glAttachShader(prog, vert); + glAttachShader(prog, frag); + glLinkProgram(prog); + + glDetachShader(prog, vert); + glDetachShader(prog, frag); + glDeleteShader(vert); + glDeleteShader(frag); + + GLint ok; + glGetProgramiv(prog, GL_LINK_STATUS, &ok); + if (ok == GL_FALSE) { + glDeleteProgram(prog); + goto error; + } + + GLES2_DEBUG_POP; + return prog; + +error: + GLES2_DEBUG_POP; + return 0; +} + +extern const GLchar quad_vertex_src[]; +extern const GLchar quad_fragment_src[]; +extern const GLchar ellipse_fragment_src[]; +extern const GLchar tex_vertex_src[]; +extern const GLchar tex_fragment_src_rgba[]; +extern const GLchar tex_fragment_src_rgbx[]; +extern const GLchar tex_fragment_src_external[]; + struct wlr_renderer *wlr_gles2_renderer_create(struct wlr_backend *backend) { - init_globals(); - struct wlr_gles2_renderer *renderer; - if (!(renderer = calloc(1, sizeof(struct wlr_gles2_renderer)))) { + struct wlr_gles2_renderer *renderer = + calloc(1, sizeof(struct wlr_gles2_renderer)); + if (renderer == NULL) { return NULL; } - wlr_renderer_init(&renderer->wlr_renderer, &wlr_renderer_impl); + wlr_renderer_init(&renderer->wlr_renderer, &renderer_impl); renderer->egl = wlr_backend_get_egl(backend); + wlr_egl_make_current(renderer->egl, EGL_NO_SURFACE, NULL); + + if (glDebugMessageCallbackKHR && glDebugMessageControlKHR) { + glEnable(GL_DEBUG_OUTPUT_KHR); + glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_KHR); + glDebugMessageCallbackKHR(gles2_log, NULL); + + // Silence unwanted message types + glDebugMessageControlKHR(GL_DONT_CARE, GL_DEBUG_TYPE_POP_GROUP_KHR, + GL_DONT_CARE, 0, NULL, GL_FALSE); + glDebugMessageControlKHR(GL_DONT_CARE, GL_DEBUG_TYPE_PUSH_GROUP_KHR, + GL_DONT_CARE, 0, NULL, GL_FALSE); + } + + GLES2_DEBUG_PUSH; + + renderer->shaders.quad = link_program(quad_vertex_src, quad_fragment_src); + if (!renderer->shaders.quad) { + goto error; + } + renderer->shaders.ellipse = + link_program(quad_vertex_src, ellipse_fragment_src); + if (!renderer->shaders.ellipse) { + goto error; + } + renderer->shaders.tex_rgba = + link_program(tex_vertex_src, tex_fragment_src_rgba); + if (!renderer->shaders.tex_rgba) { + goto error; + } + renderer->shaders.tex_rgbx = + link_program(tex_vertex_src, tex_fragment_src_rgbx); + if (!renderer->shaders.tex_rgbx) { + goto error; + } + if (glEGLImageTargetTexture2DOES) { + renderer->shaders.tex_ext = + link_program(tex_vertex_src, tex_fragment_src_external); + if (!renderer->shaders.tex_ext) { + goto error; + } + } + + GLES2_DEBUG_POP; return &renderer->wlr_renderer; + +error: + glDeleteProgram(renderer->shaders.quad); + glDeleteProgram(renderer->shaders.ellipse); + glDeleteProgram(renderer->shaders.tex_rgba); + glDeleteProgram(renderer->shaders.tex_rgbx); + glDeleteProgram(renderer->shaders.tex_ext); + + GLES2_DEBUG_POP; + + if (glDebugMessageCallbackKHR) { + glDisable(GL_DEBUG_OUTPUT_KHR); + glDebugMessageCallbackKHR(NULL, NULL); + } + + free(renderer); + return NULL; } diff --git a/render/gles2/shaders.c b/render/gles2/shaders.c index 30e12e4c..ba3bd971 100644 --- a/render/gles2/shaders.c +++ b/render/gles2/shaders.c @@ -3,86 +3,88 @@ // Colored quads const GLchar quad_vertex_src[] = -"uniform mat3 proj;" -"uniform vec4 color;" -"attribute vec2 pos;" -"attribute vec2 texcoord;" -"varying vec4 v_color;" -"varying vec2 v_texcoord;" -"" -"void main() {" -" gl_Position = vec4(proj * vec3(pos, 1.0), 1.0);" -" v_color = color;" -" v_texcoord = texcoord;" -"}"; +"uniform mat3 proj;\n" +"uniform vec4 color;\n" +"attribute vec2 pos;\n" +"attribute vec2 texcoord;\n" +"varying vec4 v_color;\n" +"varying vec2 v_texcoord;\n" +"\n" +"void main() {\n" +" gl_Position = vec4(proj * vec3(pos, 1.0), 1.0);\n" +" v_color = color;\n" +" v_texcoord = texcoord;\n" +"}\n"; const GLchar quad_fragment_src[] = -"precision mediump float;" -"varying vec4 v_color;" -"varying vec2 v_texcoord;" -"" -"void main() {" -" gl_FragColor = v_color;" -"}"; +"precision mediump float;\n" +"varying vec4 v_color;\n" +"varying vec2 v_texcoord;\n" +"\n" +"void main() {\n" +" gl_FragColor = v_color;\n" +"}\n"; // Colored ellipses const GLchar ellipse_fragment_src[] = -"precision mediump float;" -"varying vec4 v_color;" -"varying vec2 v_texcoord;" -"" -"void main() {" -" float l = length(v_texcoord - vec2(0.5, 0.5));" -" if (l > 0.5) discard;" -" gl_FragColor = v_color;" -"}"; +"precision mediump float;\n" +"varying vec4 v_color;\n" +"varying vec2 v_texcoord;\n" +"\n" +"void main() {\n" +" float l = length(v_texcoord - vec2(0.5, 0.5));\n" +" if (l > 0.5) {\n" +" discard;\n" +" }\n" +" gl_FragColor = v_color;\n" +"}\n"; // Textured quads -const GLchar vertex_src[] = -"uniform mat3 proj;" -"uniform bool invert_y;" -"attribute vec2 pos;" -"attribute vec2 texcoord;" -"varying vec2 v_texcoord;" -"" -"void main() {" -" gl_Position = vec4(proj * vec3(pos, 1.0), 1.0);" -" if (invert_y) {" -" v_texcoord = vec2(texcoord.s, 1.0 - texcoord.t);" -" } else {" -" v_texcoord = texcoord;" -" }" -"}"; +const GLchar tex_vertex_src[] = +"uniform mat3 proj;\n" +"uniform bool invert_y;\n" +"attribute vec2 pos;\n" +"attribute vec2 texcoord;\n" +"varying vec2 v_texcoord;\n" +"\n" +"void main() {\n" +" gl_Position = vec4(proj * vec3(pos, 1.0), 1.0);\n" +" if (invert_y) {\n" +" v_texcoord = vec2(texcoord.s, 1.0 - texcoord.t);\n" +" } else {\n" +" v_texcoord = texcoord;\n" +" }\n" +"}\n"; -const GLchar fragment_src_rgba[] = -"precision mediump float;" -"varying vec2 v_texcoord;" -"uniform sampler2D tex;" -"uniform float alpha;" -"" -"void main() {" -" gl_FragColor = alpha * texture2D(tex, v_texcoord);" -"}"; +const GLchar tex_fragment_src_rgba[] = +"precision mediump float;\n" +"varying vec2 v_texcoord;\n" +"uniform sampler2D tex;\n" +"uniform float alpha;\n" +"\n" +"void main() {\n" +" gl_FragColor = alpha * texture2D(tex, v_texcoord);\n" +"}\n"; -const GLchar fragment_src_rgbx[] = -"precision mediump float;" -"varying vec2 v_texcoord;" -"uniform sampler2D tex;" -"uniform float alpha;" -"" -"void main() {" -" gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb;" -" gl_FragColor.a = alpha;" -"}"; +const GLchar tex_fragment_src_rgbx[] = +"precision mediump float;\n" +"varying vec2 v_texcoord;\n" +"uniform sampler2D tex;\n" +"uniform float alpha;\n" +"\n" +"void main() {\n" +" gl_FragColor.rgb = alpha * texture2D(tex, v_texcoord).rgb;\n" +" gl_FragColor.a = alpha;\n" +"}\n"; -const GLchar fragment_src_external[] = -"#extension GL_OES_EGL_image_external : require\n" -"precision mediump float;" -"varying vec2 v_texcoord;" -"uniform samplerExternalOES texture0;" -"uniform float alpha;" -"" -"void main() {" -" vec4 col = texture2D(texture0, v_texcoord);" -" gl_FragColor = vec4(col.rgb, col.a * alpha);" -"}"; +const GLchar tex_fragment_src_external[] = +"#extension GL_OES_EGL_image_external : require\n\n" +"precision mediump float;\n" +"varying vec2 v_texcoord;\n" +"uniform samplerExternalOES texture0;\n" +"uniform float alpha;\n" +"\n" +"void main() {\n" +" vec4 col = texture2D(texture0, v_texcoord);\n" +" gl_FragColor = vec4(col.rgb, col.a * alpha);\n" +"}\n"; diff --git a/render/gles2/texture.c b/render/gles2/texture.c index 99f153b8..e21bd77f 100644 --- a/render/gles2/texture.c +++ b/render/gles2/texture.c @@ -13,33 +13,39 @@ #include "render/gles2.h" #include "util/signal.h" -static struct pixel_format external_pixel_format = { +static struct gles2_pixel_format external_pixel_format = { .wl_format = 0, .depth = 0, .bpp = 0, .gl_format = 0, .gl_type = 0, - .shader = &shaders.external }; -static void gles2_texture_ensure_texture(struct wlr_gles2_texture *texture, +static void gles2_texture_ensure(struct wlr_gles2_texture *texture, GLenum target) { if (texture->tex_id) { return; } texture->target = target; - GL_CALL(glGenTextures(1, &texture->tex_id)); - GL_CALL(glBindTexture(target, texture->tex_id)); - GL_CALL(glTexParameteri(target, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE)); - GL_CALL(glTexParameteri(target, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE)); + glGenTextures(1, &texture->tex_id); + glBindTexture(target, texture->tex_id); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); } -static bool gles2_texture_upload_pixels(struct wlr_texture *_texture, +static const struct wlr_texture_impl texture_impl; + +struct wlr_gles2_texture *gles2_get_texture(struct wlr_texture *wlr_texture) { + assert(wlr_texture->impl == &texture_impl); + return (struct wlr_gles2_texture *)wlr_texture; +} + +static bool gles2_texture_upload_pixels(struct wlr_texture *wlr_texture, enum wl_shm_format format, int stride, int width, int height, const unsigned char *pixels) { - struct wlr_gles2_texture *texture = (struct wlr_gles2_texture *)_texture; - assert(texture); - const struct pixel_format *fmt = gl_format_for_wl_format(format); + struct wlr_gles2_texture *texture = gles2_get_texture(wlr_texture); + + const struct gles2_pixel_format *fmt = gles2_format_from_wl(format); if (!fmt || !fmt->gl_format) { wlr_log(L_ERROR, "No supported pixel format for this texture"); return false; @@ -49,44 +55,50 @@ static bool gles2_texture_upload_pixels(struct wlr_texture *_texture, texture->wlr_texture.format = format; texture->pixel_format = fmt; - gles2_texture_ensure_texture(texture, GL_TEXTURE_2D); - GL_CALL(glBindTexture(GL_TEXTURE_2D, texture->tex_id)); - GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, stride)); - GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, fmt->gl_format, width, height, 0, - fmt->gl_format, fmt->gl_type, pixels)); + GLES2_DEBUG_PUSH; + gles2_texture_ensure(texture, GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, texture->tex_id); + glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, stride); + glTexImage2D(GL_TEXTURE_2D, 0, fmt->gl_format, width, height, 0, + fmt->gl_format, fmt->gl_type, pixels); + GLES2_DEBUG_POP; + texture->wlr_texture.valid = true; return true; } -static bool gles2_texture_update_pixels(struct wlr_texture *_texture, +static bool gles2_texture_update_pixels(struct wlr_texture *wlr_texture, enum wl_shm_format format, int stride, int x, int y, int width, int height, const unsigned char *pixels) { - struct wlr_gles2_texture *texture = (struct wlr_gles2_texture *)_texture; - assert(texture); + struct wlr_gles2_texture *texture = gles2_get_texture(wlr_texture); + // TODO: Test if the unpack subimage extension is supported and adjust the // upload strategy if not if (!texture->wlr_texture.valid || texture->wlr_texture.format != format /* || unpack not supported */) { - return gles2_texture_upload_pixels(&texture->wlr_texture, - format, stride, width, height, pixels); + return gles2_texture_upload_pixels(&texture->wlr_texture, format, + stride, width, height, pixels); } - const struct pixel_format *fmt = texture->pixel_format; - GL_CALL(glBindTexture(GL_TEXTURE_2D, texture->tex_id)); - GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, stride)); - GL_CALL(glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, x)); - GL_CALL(glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, y)); - GL_CALL(glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, - fmt->gl_format, fmt->gl_type, pixels)); - GL_CALL(glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0)); - GL_CALL(glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0)); + const struct gles2_pixel_format *fmt = texture->pixel_format; + GLES2_DEBUG_PUSH; + glBindTexture(GL_TEXTURE_2D, texture->tex_id); + glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, stride); + glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, x); + glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, y); + glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, fmt->gl_format, + fmt->gl_type, pixels); + glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0); + glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0); + GLES2_DEBUG_POP; return true; } -static bool gles2_texture_upload_shm(struct wlr_texture *_texture, +static bool gles2_texture_upload_shm(struct wlr_texture *wlr_texture, uint32_t format, struct wl_shm_buffer *buffer) { - struct wlr_gles2_texture *texture = (struct wlr_gles2_texture *)_texture; - const struct pixel_format *fmt = gl_format_for_wl_format(format); + struct wlr_gles2_texture *texture = gles2_get_texture(wlr_texture); + + const struct gles2_pixel_format *fmt = gles2_format_from_wl(format); if (!fmt || !fmt->gl_format) { wlr_log(L_ERROR, "Unsupported pixel format %"PRIu32" for this texture", format); @@ -102,23 +114,26 @@ static bool gles2_texture_upload_shm(struct wlr_texture *_texture, texture->wlr_texture.format = format; texture->pixel_format = fmt; - gles2_texture_ensure_texture(texture, GL_TEXTURE_2D); - GL_CALL(glBindTexture(GL_TEXTURE_2D, texture->tex_id)); - GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, pitch)); - GL_CALL(glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0)); - GL_CALL(glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0)); - GL_CALL(glTexImage2D(GL_TEXTURE_2D, 0, fmt->gl_format, width, height, 0, - fmt->gl_format, fmt->gl_type, pixels)); + GLES2_DEBUG_PUSH; + gles2_texture_ensure(texture, GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, texture->tex_id); + glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, pitch); + glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0); + glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0); + glTexImage2D(GL_TEXTURE_2D, 0, fmt->gl_format, width, height, 0, + fmt->gl_format, fmt->gl_type, pixels); + GLES2_DEBUG_POP; texture->wlr_texture.valid = true; wl_shm_buffer_end_access(buffer); return true; } -static bool gles2_texture_update_shm(struct wlr_texture *_texture, +static bool gles2_texture_update_shm(struct wlr_texture *wlr_texture, uint32_t format, int x, int y, int width, int height, struct wl_shm_buffer *buffer) { - struct wlr_gles2_texture *texture = (struct wlr_gles2_texture *)_texture; + struct wlr_gles2_texture *texture = gles2_get_texture(wlr_texture); + // TODO: Test if the unpack subimage extension is supported and adjust the // upload strategy if not assert(texture); @@ -127,28 +142,30 @@ static bool gles2_texture_update_shm(struct wlr_texture *_texture, /* || unpack not supported */) { return gles2_texture_upload_shm(&texture->wlr_texture, format, buffer); } - const struct pixel_format *fmt = texture->pixel_format; + const struct gles2_pixel_format *fmt = texture->pixel_format; wl_shm_buffer_begin_access(buffer); uint8_t *pixels = wl_shm_buffer_get_data(buffer); int pitch = wl_shm_buffer_get_stride(buffer) / (fmt->bpp / 8); - GL_CALL(glBindTexture(GL_TEXTURE_2D, texture->tex_id)); - GL_CALL(glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, pitch)); - GL_CALL(glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, x)); - GL_CALL(glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, y)); - GL_CALL(glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, - fmt->gl_format, fmt->gl_type, pixels)); - GL_CALL(glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0)); - GL_CALL(glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0)); + GLES2_DEBUG_PUSH; + glBindTexture(GL_TEXTURE_2D, texture->tex_id); + glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, pitch); + glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, x); + glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, y); + glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, + fmt->gl_format, fmt->gl_type, pixels); + glPixelStorei(GL_UNPACK_SKIP_PIXELS_EXT, 0); + glPixelStorei(GL_UNPACK_SKIP_ROWS_EXT, 0); + GLES2_DEBUG_POP; wl_shm_buffer_end_access(buffer); return true; } -static bool gles2_texture_upload_drm(struct wlr_texture *_tex, +static bool gles2_texture_upload_drm(struct wlr_texture *wlr_texture, struct wl_resource *buf) { - struct wlr_gles2_texture *tex = (struct wlr_gles2_texture *)_tex; + struct wlr_gles2_texture *tex = gles2_get_texture(wlr_texture); if (!glEGLImageTargetTexture2DOES) { return false; } @@ -160,9 +177,9 @@ static bool gles2_texture_upload_drm(struct wlr_texture *_tex, } wlr_egl_query_buffer(tex->egl, buf, EGL_WIDTH, - (EGLint*)&tex->wlr_texture.width); + (EGLint*)&tex->wlr_texture.width); wlr_egl_query_buffer(tex->egl, buf, EGL_HEIGHT, - (EGLint*)&tex->wlr_texture.height); + (EGLint*)&tex->wlr_texture.height); EGLint inverted_y; if (wlr_egl_query_buffer(tex->egl, buf, EGL_WAYLAND_Y_INVERTED_WL, @@ -171,12 +188,12 @@ static bool gles2_texture_upload_drm(struct wlr_texture *_tex, } GLenum target; - const struct pixel_format *pf; + const struct gles2_pixel_format *pf; switch (format) { case EGL_TEXTURE_RGB: case EGL_TEXTURE_RGBA: target = GL_TEXTURE_2D; - pf = gl_format_for_wl_format(WL_SHM_FORMAT_ARGB8888); + pf = gles2_format_from_wl(WL_SHM_FORMAT_ARGB8888); break; case EGL_TEXTURE_EXTERNAL_WL: target = GL_TEXTURE_EXTERNAL_OES; @@ -187,8 +204,11 @@ static bool gles2_texture_upload_drm(struct wlr_texture *_tex, return false; } - gles2_texture_ensure_texture(tex, target); - GL_CALL(glBindTexture(GL_TEXTURE_2D, tex->tex_id)); + GLES2_DEBUG_PUSH; + gles2_texture_ensure(tex, target); + glBindTexture(GL_TEXTURE_2D, tex->tex_id); + GLES2_DEBUG_POP; + EGLint attribs[] = { EGL_WAYLAND_PLANE_WL, 0, EGL_NONE }; if (tex->image) { @@ -202,18 +222,20 @@ static bool gles2_texture_upload_drm(struct wlr_texture *_tex, return false; } - GL_CALL(glActiveTexture(GL_TEXTURE0)); - GL_CALL(glBindTexture(target, tex->tex_id)); - GL_CALL(glEGLImageTargetTexture2DOES(target, tex->image)); + GLES2_DEBUG_PUSH; + glActiveTexture(GL_TEXTURE0); + glBindTexture(target, tex->tex_id); + glEGLImageTargetTexture2DOES(target, tex->image); + GLES2_DEBUG_POP; tex->wlr_texture.valid = true; tex->pixel_format = pf; return true; } -static bool gles2_texture_upload_eglimage(struct wlr_texture *wlr_tex, +static bool gles2_texture_upload_eglimage(struct wlr_texture *wlr_texture, EGLImageKHR image, uint32_t width, uint32_t height) { - struct wlr_gles2_texture *tex = (struct wlr_gles2_texture *)wlr_tex; + struct wlr_gles2_texture *tex = gles2_get_texture(wlr_texture); tex->image = image; tex->pixel_format = &external_pixel_format; @@ -221,20 +243,21 @@ static bool gles2_texture_upload_eglimage(struct wlr_texture *wlr_tex, tex->wlr_texture.width = width; tex->wlr_texture.height = height; - gles2_texture_ensure_texture(tex, GL_TEXTURE_2D); - - GL_CALL(glActiveTexture(GL_TEXTURE0)); - GL_CALL(glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex->tex_id)); - GL_CALL(glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, tex->image)); + GLES2_DEBUG_PUSH; + gles2_texture_ensure(tex, GL_TEXTURE_2D); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_EXTERNAL_OES, tex->tex_id); + glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, tex->image); + GLES2_DEBUG_POP; return true; } -static bool gles2_texture_upload_dmabuf(struct wlr_texture *_tex, +static bool gles2_texture_upload_dmabuf(struct wlr_texture *wlr_texture, struct wl_resource *dmabuf_resource) { - struct wlr_gles2_texture *tex = (struct wlr_gles2_texture *)_tex; - struct wlr_dmabuf_buffer *dmabuf = wlr_dmabuf_buffer_from_buffer_resource( - dmabuf_resource); + struct wlr_gles2_texture *tex = gles2_get_texture(wlr_texture); + struct wlr_dmabuf_buffer *dmabuf = + wlr_dmabuf_buffer_from_buffer_resource(dmabuf_resource); if (!tex->egl->egl_exts.dmabuf_import) { wlr_log(L_ERROR, "Want dmabuf but extension not present"); @@ -249,17 +272,19 @@ static bool gles2_texture_upload_dmabuf(struct wlr_texture *_tex, } if (wlr_dmabuf_buffer_has_inverted_y(dmabuf)) { - _tex->inverted_y = true; + wlr_texture->inverted_y = true; } GLenum target = GL_TEXTURE_2D; - const struct pixel_format *pf = - gl_format_for_wl_format(WL_SHM_FORMAT_ARGB8888); - gles2_texture_ensure_texture(tex, target); - GL_CALL(glBindTexture(target, tex->tex_id)); + const struct gles2_pixel_format *pf = + gles2_format_from_wl(WL_SHM_FORMAT_ARGB8888); + GLES2_DEBUG_PUSH; + gles2_texture_ensure(tex, target); + glBindTexture(target, tex->tex_id); tex->image = wlr_egl_create_image_from_dmabuf(tex->egl, &dmabuf->attributes); - GL_CALL(glActiveTexture(GL_TEXTURE0)); - GL_CALL(glEGLImageTargetTexture2DOES(target, tex->image)); + glActiveTexture(GL_TEXTURE0); + glEGLImageTargetTexture2DOES(target, tex->image); + GLES2_DEBUG_POP; tex->pixel_format = pf; tex->wlr_texture.valid = true; return true; @@ -271,15 +296,15 @@ static bool gles2_texture_get_dmabuf_size(struct wlr_texture *texture, struct return false; } - struct wlr_dmabuf_buffer *dmabuf = wlr_dmabuf_buffer_from_buffer_resource( - resource); + struct wlr_dmabuf_buffer *dmabuf = + wlr_dmabuf_buffer_from_buffer_resource(resource); *width = dmabuf->attributes.width; *height = dmabuf->attributes.height; return true; } -static void gles2_texture_get_buffer_size(struct wlr_texture *texture, struct - wl_resource *resource, int *width, int *height) { +static void gles2_texture_get_buffer_size(struct wlr_texture *texture, + struct wl_resource *resource, int *width, int *height) { struct wl_shm_buffer *buffer = wl_shm_buffer_get(resource); if (!buffer) { struct wlr_gles2_texture *tex = (struct wlr_gles2_texture *)texture; @@ -288,14 +313,13 @@ static void gles2_texture_get_buffer_size(struct wlr_texture *texture, struct } if (!wlr_egl_query_buffer(tex->egl, resource, EGL_WIDTH, (EGLint*)width)) { - if (!gles2_texture_get_dmabuf_size(texture, resource, - width, height)) { + if (!gles2_texture_get_dmabuf_size(texture, resource, width, + height)) { wlr_log(L_ERROR, "could not get size of the buffer"); return; } } - wlr_egl_query_buffer(tex->egl, resource, EGL_HEIGHT, - (EGLint*)height); + wlr_egl_query_buffer(tex->egl, resource, EGL_HEIGHT, (EGLint*)height); return; } @@ -304,20 +328,15 @@ static void gles2_texture_get_buffer_size(struct wlr_texture *texture, struct *height = wl_shm_buffer_get_height(buffer); } -static void gles2_texture_bind(struct wlr_texture *_texture) { - struct wlr_gles2_texture *texture = (struct wlr_gles2_texture *)_texture; - - GL_CALL(glBindTexture(texture->target, texture->tex_id)); - GL_CALL(glTexParameteri(texture->target, GL_TEXTURE_MIN_FILTER, GL_LINEAR)); - GL_CALL(glTexParameteri(texture->target, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); - GL_CALL(glUseProgram(*texture->pixel_format->shader)); -} +static void gles2_texture_destroy(struct wlr_texture *wlr_texture) { + struct wlr_gles2_texture *texture = gles2_get_texture(wlr_texture); -static void gles2_texture_destroy(struct wlr_texture *_texture) { - struct wlr_gles2_texture *texture = (struct wlr_gles2_texture *)_texture; - wlr_signal_emit_safe(&texture->wlr_texture.destroy_signal, &texture->wlr_texture); + wlr_signal_emit_safe(&texture->wlr_texture.destroy_signal, + &texture->wlr_texture); if (texture->tex_id) { - GL_CALL(glDeleteTextures(1, &texture->tex_id)); + GLES2_DEBUG_PUSH; + glDeleteTextures(1, &texture->tex_id); + GLES2_DEBUG_POP; } if (texture->image) { @@ -327,7 +346,7 @@ static void gles2_texture_destroy(struct wlr_texture *_texture) { free(texture); } -static struct wlr_texture_impl wlr_texture_impl = { +static const struct wlr_texture_impl texture_impl = { .upload_pixels = gles2_texture_upload_pixels, .update_pixels = gles2_texture_update_pixels, .upload_shm = gles2_texture_upload_shm, @@ -336,7 +355,6 @@ static struct wlr_texture_impl wlr_texture_impl = { .upload_dmabuf = gles2_texture_upload_dmabuf, .upload_eglimage = gles2_texture_upload_eglimage, .get_buffer_size = gles2_texture_get_buffer_size, - .bind = gles2_texture_bind, .destroy = gles2_texture_destroy, }; @@ -345,7 +363,7 @@ struct wlr_texture *gles2_texture_create(struct wlr_egl *egl) { if (!(texture = calloc(1, sizeof(struct wlr_gles2_texture)))) { return NULL; } - wlr_texture_init(&texture->wlr_texture, &wlr_texture_impl); + wlr_texture_init(&texture->wlr_texture, &texture_impl); texture->egl = egl; return &texture->wlr_texture; } diff --git a/render/wlr_renderer.c b/render/wlr_renderer.c index 21a7901a..79e0b5d5 100644 --- a/render/wlr_renderer.c +++ b/render/wlr_renderer.c @@ -5,7 +5,7 @@ #include void wlr_renderer_init(struct wlr_renderer *renderer, - struct wlr_renderer_impl *impl) { + const struct wlr_renderer_impl *impl) { renderer->impl = impl; } diff --git a/render/wlr_texture.c b/render/wlr_texture.c index a5d0abde..33c91822 100644 --- a/render/wlr_texture.c +++ b/render/wlr_texture.c @@ -4,7 +4,7 @@ #include void wlr_texture_init(struct wlr_texture *texture, - struct wlr_texture_impl *impl) { + const struct wlr_texture_impl *impl) { texture->impl = impl; wl_signal_init(&texture->destroy_signal); } @@ -17,10 +17,6 @@ void wlr_texture_destroy(struct wlr_texture *texture) { } } -void wlr_texture_bind(struct wlr_texture *texture) { - texture->impl->bind(texture); -} - bool wlr_texture_upload_pixels(struct wlr_texture *texture, uint32_t format, int stride, int width, int height, const unsigned char *pixels) { return texture->impl->upload_pixels(texture, format, stride, -- cgit v1.2.3 From 3581573bdcbe3c905eae83af53cccbcdd52edad2 Mon Sep 17 00:00:00 2001 From: emersion Date: Tue, 20 Mar 2018 23:10:42 +0100 Subject: render/gles2: make wlr_renderer_begin take viewport size This allows raw GL calls outside wlr_renderer to be removed. --- backend/drm/drm.c | 13 ++++++------- backend/drm/renderer.c | 23 +++++++++++------------ backend/headless/output.c | 14 ++++++-------- backend/wayland/output.c | 21 ++++++++++----------- examples/output-layout.c | 2 +- examples/rotation.c | 2 +- examples/tablet.c | 2 +- examples/touch.c | 2 +- include/wlr/render/interface.h | 3 ++- include/wlr/render/wlr_renderer.h | 2 +- render/gles2/renderer.c | 6 +++--- render/wlr_renderer.c | 4 ++-- rootston/output.c | 2 +- wlroots.syms | 2 +- 14 files changed, 47 insertions(+), 51 deletions(-) (limited to 'include/wlr/render') diff --git a/backend/drm/drm.c b/backend/drm/drm.c index 344756f8..345c0dd8 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -643,14 +643,13 @@ static bool wlr_drm_connector_set_cursor(struct wlr_output *output, wlr_texture_upload_pixels(plane->wlr_tex, WL_SHM_FORMAT_ARGB8888, stride, width, height, buf); - glViewport(0, 0, plane->surf.width, plane->surf.height); - glClearColor(0.0, 0.0, 0.0, 0.0); - glClear(GL_COLOR_BUFFER_BIT); + struct wlr_renderer *rend = plane->surf.renderer->wlr_rend; + wlr_renderer_begin(rend, plane->surf.width, plane->surf.height); + wlr_renderer_clear(rend, (float[]){ 0.0, 0.0, 0.0, 0.0 }); + wlr_render_texture(rend, plane->wlr_tex, plane->matrix, 0, 0, 1.0f); + wlr_renderer_end(rend); - wlr_render_texture(plane->surf.renderer->wlr_rend, plane->wlr_tex, - plane->matrix, 0, 0, 1.0f); - - glFinish(); + // TODO: remove these raw GL calls glPixelStorei(GL_UNPACK_ROW_LENGTH_EXT, bo_stride); glReadPixels(0, 0, plane->surf.width, plane->surf.height, GL_BGRA_EXT, GL_UNSIGNED_BYTE, bo_data); diff --git a/backend/drm/renderer.c b/backend/drm/renderer.c index 7e330990..3b9aaded 100644 --- a/backend/drm/renderer.c +++ b/backend/drm/renderer.c @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include @@ -107,9 +106,6 @@ void wlr_drm_surface_finish(struct wlr_drm_surface *surf) { return; } - eglMakeCurrent(surf->renderer->egl.display, EGL_NO_SURFACE, EGL_NO_SURFACE, - EGL_NO_CONTEXT); - if (surf->front) { gbm_surface_release_buffer(surf->gbm, surf->front); } @@ -151,9 +147,10 @@ struct gbm_bo *wlr_drm_surface_get_front(struct wlr_drm_surface *surf) { } wlr_drm_surface_make_current(surf, NULL); - glViewport(0, 0, surf->width, surf->height); - glClearColor(0.0, 0.0, 0.0, 1.0); - glClear(GL_COLOR_BUFFER_BIT); + struct wlr_renderer *renderer = surf->renderer->wlr_rend; + wlr_renderer_begin(renderer, surf->width, surf->height); + wlr_renderer_clear(renderer, (float[]){ 0.0, 0.0, 0.0, 1.0 }); + wlr_renderer_end(renderer); return wlr_drm_surface_swap_buffers(surf, NULL); } @@ -185,6 +182,8 @@ static struct wlr_texture *get_tex_for_bo(struct wlr_drm_renderer *renderer, return tex->tex; } + // TODO: use wlr_texture_upload_dmabuf instead + tex = malloc(sizeof(*tex)); if (!tex) { wlr_log_errno(L_ERROR, "Allocation failed"); @@ -230,14 +229,14 @@ struct gbm_bo *wlr_drm_surface_mgpu_copy(struct wlr_drm_surface *dest, struct wlr_texture *tex = get_tex_for_bo(dest->renderer, src); assert(tex); - static const float color[] = {0.0, 0.0, 0.0, 1.0}; - float mat[9]; wlr_matrix_projection(mat, 1, 1, WL_OUTPUT_TRANSFORM_FLIPPED_180); - glViewport(0, 0, dest->width, dest->height); - wlr_renderer_clear(dest->renderer->wlr_rend, color); - wlr_render_texture_with_matrix(dest->renderer->wlr_rend, tex, mat, 1.0f); + struct wlr_renderer *renderer = dest->renderer->wlr_rend; + wlr_renderer_begin(renderer, dest->width, dest->height); + wlr_renderer_clear(renderer, (float[]){ 0.0, 0.0, 0.0, 1.0 }); + wlr_render_texture_with_matrix(renderer, tex, mat, 1.0f); + wlr_renderer_end(renderer); return wlr_drm_surface_swap_buffers(dest, NULL); } diff --git a/backend/headless/output.c b/backend/headless/output.c index 6ce8fc35..4746d2f2 100644 --- a/backend/headless/output.c +++ b/backend/headless/output.c @@ -1,8 +1,8 @@ #include #include -#include #include #include +#include #include #include "backend/headless.h" #include "util/signal.h" @@ -120,16 +120,14 @@ struct wlr_output *wlr_headless_add_output(struct wlr_backend *wlr_backend, snprintf(wlr_output->name, sizeof(wlr_output->name), "HEADLESS-%d", wl_list_length(&backend->outputs) + 1); - if (!eglMakeCurrent(output->backend->egl.display, - output->egl_surface, output->egl_surface, - output->backend->egl.context)) { - wlr_log(L_ERROR, "eglMakeCurrent failed: %s", egl_error()); + if (!wlr_egl_make_current(&output->backend->egl, output->egl_surface, + NULL)) { goto error; } - glViewport(0, 0, wlr_output->width, wlr_output->height); - glClearColor(1.0, 1.0, 1.0, 1.0); - glClear(GL_COLOR_BUFFER_BIT); + wlr_renderer_begin(backend->renderer, wlr_output->width, wlr_output->height); + wlr_renderer_clear(backend->renderer, (float[]){ 1.0, 1.0, 1.0, 1.0 }); + wlr_renderer_end(backend->renderer); struct wl_event_loop *ev = wl_display_get_event_loop(backend->display); output->frame_timer = wl_event_loop_add_timer(ev, signal_frame, output); diff --git a/backend/wayland/output.c b/backend/wayland/output.c index fc40dea0..d528c888 100644 --- a/backend/wayland/output.c +++ b/backend/wayland/output.c @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -9,6 +8,7 @@ #include #include #include +#include #include #include "backend/wayland.h" #include "util/signal.h" @@ -313,27 +313,26 @@ struct wlr_output *wlr_wl_output_create(struct wlr_backend *_backend) { output->egl_window = wl_egl_window_create(output->surface, wlr_output->width, wlr_output->height); - output->egl_surface = wlr_egl_create_surface(&backend->egl, output->egl_window); + output->egl_surface = wlr_egl_create_surface(&backend->egl, + output->egl_window); wl_display_roundtrip(output->backend->remote_display); // start rendering loop per callbacks by rendering first frame - if (!eglMakeCurrent(output->backend->egl.display, - output->egl_surface, output->egl_surface, - output->backend->egl.context)) { - wlr_log(L_ERROR, "eglMakeCurrent failed: %s", egl_error()); + if (!wlr_egl_make_current(&output->backend->egl, output->egl_surface, + NULL)) { goto error; } - glViewport(0, 0, wlr_output->width, wlr_output->height); - glClearColor(1.0, 1.0, 1.0, 1.0); - glClear(GL_COLOR_BUFFER_BIT); + wlr_renderer_begin(backend->renderer, wlr_output->width, wlr_output->height); + wlr_renderer_clear(backend->renderer, (float[]){ 1.0, 1.0, 1.0, 1.0 }); + wlr_renderer_end(backend->renderer); output->frame_callback = wl_surface_frame(output->surface); wl_callback_add_listener(output->frame_callback, &frame_listener, output); - if (!eglSwapBuffers(output->backend->egl.display, output->egl_surface)) { - wlr_log(L_ERROR, "eglSwapBuffers failed: %s", egl_error()); + if (!wlr_egl_swap_buffers(&output->backend->egl, output->egl_surface, + NULL)) { goto error; } diff --git a/examples/output-layout.c b/examples/output-layout.c index de134a71..45d896b0 100644 --- a/examples/output-layout.c +++ b/examples/output-layout.c @@ -101,7 +101,7 @@ static void handle_output_frame(struct output_state *output, struct wlr_output *wlr_output = output->output; wlr_output_make_current(wlr_output, NULL); - wlr_renderer_begin(sample->renderer, wlr_output); + wlr_renderer_begin(sample->renderer, wlr_output->width, wlr_output->height); wlr_renderer_clear(sample->renderer, (float[]){0.25f, 0.25f, 0.25f, 1}); animate_cat(sample, output->output); diff --git a/examples/rotation.c b/examples/rotation.c index 7f50b620..cbff09a1 100644 --- a/examples/rotation.c +++ b/examples/rotation.c @@ -43,7 +43,7 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts wlr_output_effective_resolution(wlr_output, &width, &height); wlr_output_make_current(wlr_output, NULL); - wlr_renderer_begin(sample->renderer, wlr_output); + wlr_renderer_begin(sample->renderer, wlr_output->width, wlr_output->height); wlr_renderer_clear(sample->renderer, (float[]){0.25f, 0.25f, 0.25f, 1}); for (int y = -128 + (int)odata->y_offs; y < height; y += 128) { diff --git a/examples/tablet.c b/examples/tablet.c index 1b995003..65c559cb 100644 --- a/examples/tablet.c +++ b/examples/tablet.c @@ -46,7 +46,7 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts wlr_output_effective_resolution(wlr_output, &width, &height); wlr_output_make_current(wlr_output, NULL); - wlr_renderer_begin(sample->renderer, wlr_output); + wlr_renderer_begin(sample->renderer, wlr_output->width, wlr_output->height); wlr_renderer_clear(sample->renderer, (float[]){0.25f, 0.25f, 0.25f, 1}); float matrix[9]; diff --git a/examples/touch.c b/examples/touch.c index 0968e82a..f9c496cf 100644 --- a/examples/touch.c +++ b/examples/touch.c @@ -42,7 +42,7 @@ static void handle_output_frame(struct output_state *output, struct timespec *ts wlr_output_effective_resolution(wlr_output, &width, &height); wlr_output_make_current(wlr_output, NULL); - wlr_renderer_begin(sample->renderer, wlr_output); + wlr_renderer_begin(sample->renderer, wlr_output->width, wlr_output->height); wlr_renderer_clear(sample->renderer, (float[]){0.25f, 0.25f, 0.25f, 1}); struct touch_point *p; diff --git a/include/wlr/render/interface.h b/include/wlr/render/interface.h index da0aaec9..7f25c0ff 100644 --- a/include/wlr/render/interface.h +++ b/include/wlr/render/interface.h @@ -18,7 +18,8 @@ struct wlr_renderer { }; struct wlr_renderer_impl { - void (*begin)(struct wlr_renderer *renderer, struct wlr_output *output); + void (*begin)(struct wlr_renderer *renderer, uint32_t width, + uint32_t height); void (*end)(struct wlr_renderer *renderer); void (*clear)(struct wlr_renderer *renderer, const float color[static 4]); void (*scissor)(struct wlr_renderer *renderer, struct wlr_box *box); diff --git a/include/wlr/render/wlr_renderer.h b/include/wlr/render/wlr_renderer.h index d5f3cf70..6f0d2ecc 100644 --- a/include/wlr/render/wlr_renderer.h +++ b/include/wlr/render/wlr_renderer.h @@ -12,7 +12,7 @@ struct wlr_output; struct wlr_renderer; -void wlr_renderer_begin(struct wlr_renderer *r, struct wlr_output *output); +void wlr_renderer_begin(struct wlr_renderer *r, int width, int height); void wlr_renderer_end(struct wlr_renderer *r); void wlr_renderer_clear(struct wlr_renderer *r, const float color[static 4]); /** diff --git a/render/gles2/renderer.c b/render/gles2/renderer.c index 14eea666..6b046bc4 100644 --- a/render/gles2/renderer.c +++ b/render/gles2/renderer.c @@ -26,13 +26,13 @@ static struct wlr_gles2_renderer *gles2_get_renderer( return renderer; } -static void gles2_begin(struct wlr_renderer *wlr_renderer, - struct wlr_output *output) { +static void gles2_begin(struct wlr_renderer *wlr_renderer, uint32_t width, + uint32_t height) { gles2_get_renderer(wlr_renderer); GLES2_DEBUG_PUSH; - glViewport(0, 0, output->width, output->height); + glViewport(0, 0, width, height); // enable transparency glEnable(GL_BLEND); diff --git a/render/wlr_renderer.c b/render/wlr_renderer.c index 79e0b5d5..622aa1dd 100644 --- a/render/wlr_renderer.c +++ b/render/wlr_renderer.c @@ -17,8 +17,8 @@ void wlr_renderer_destroy(struct wlr_renderer *r) { } } -void wlr_renderer_begin(struct wlr_renderer *r, struct wlr_output *o) { - r->impl->begin(r, o); +void wlr_renderer_begin(struct wlr_renderer *r, int width, int height) { + r->impl->begin(r, width, height); } void wlr_renderer_end(struct wlr_renderer *r) { diff --git a/rootston/output.c b/rootston/output.c index 4146e3e2..1de27bad 100644 --- a/rootston/output.c +++ b/rootston/output.c @@ -479,7 +479,7 @@ static void render_output(struct roots_output *output) { goto damage_finish; } - wlr_renderer_begin(renderer, wlr_output); + wlr_renderer_begin(renderer, wlr_output->width, wlr_output->height); if (!pixman_region32_not_empty(&damage)) { // Output isn't damaged but needs buffer swap diff --git a/wlroots.syms b/wlroots.syms index 3f45e045..cb030a6d 100644 --- a/wlroots.syms +++ b/wlroots.syms @@ -15,8 +15,8 @@ WLROOTS_0_0_0 { wlr_drm_get_connector_props; wlr_drm_get_crtc_props; wlr_drm_get_plane_props; - wlr_drm_get_prop; wlr_drm_get_prop_blob; + wlr_drm_get_prop; wlr_drm_plane_surfaces_init; wlr_drm_renderer_finish; wlr_drm_renderer_init; -- cgit v1.2.3 From b1f93bc5cc6eee4fd15b16bdc0337d665e83a040 Mon Sep 17 00:00:00 2001 From: emersion Date: Wed, 21 Mar 2018 10:42:43 +0100 Subject: render/egl: use EGL_KHR_debug --- backend/drm/backend.c | 2 +- backend/drm/renderer.c | 2 +- backend/headless/output.c | 4 +-- backend/wayland/output.c | 4 +-- include/wlr/render/egl.h | 5 --- render/egl.c | 87 ++++++++++++++++++++++------------------------- render/glapi.txt | 1 + render/gles2/texture.c | 2 +- 8 files changed, 49 insertions(+), 58 deletions(-) (limited to 'include/wlr/render') diff --git a/backend/drm/backend.c b/backend/drm/backend.c index 75b44210..43a8d017 100644 --- a/backend/drm/backend.c +++ b/backend/drm/backend.c @@ -173,7 +173,7 @@ struct wlr_backend *wlr_drm_backend_create(struct wl_display *display, } if (!wlr_egl_bind_display(&drm->renderer.egl, display)) { - wlr_log(L_INFO, "Failed to bind egl/wl display: %s", egl_error()); + wlr_log(L_INFO, "Failed to bind egl/wl display"); } drm->display_destroy.notify = handle_display_destroy; diff --git a/backend/drm/renderer.c b/backend/drm/renderer.c index 7e330990..528cd26c 100644 --- a/backend/drm/renderer.c +++ b/backend/drm/renderer.c @@ -211,7 +211,7 @@ static struct wlr_texture *get_tex_for_bo(struct wlr_drm_renderer *renderer, tex->img = eglCreateImageKHR(renderer->egl.display, EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, attribs); if (!tex->img) { - wlr_log(L_ERROR, "Failed to create EGL image: %s", egl_error()); + wlr_log(L_ERROR, "Failed to create EGL image"); abort(); } diff --git a/backend/headless/output.c b/backend/headless/output.c index 6ce8fc35..fc275eaf 100644 --- a/backend/headless/output.c +++ b/backend/headless/output.c @@ -13,7 +13,7 @@ static EGLSurface egl_create_surface(struct wlr_egl *egl, unsigned int width, EGLSurface surf = eglCreatePbufferSurface(egl->display, egl->config, attribs); if (surf == EGL_NO_SURFACE) { - wlr_log(L_ERROR, "Failed to create EGL surface: %s", egl_error()); + wlr_log(L_ERROR, "Failed to create EGL surface"); return EGL_NO_SURFACE; } return surf; @@ -123,7 +123,7 @@ struct wlr_output *wlr_headless_add_output(struct wlr_backend *wlr_backend, if (!eglMakeCurrent(output->backend->egl.display, output->egl_surface, output->egl_surface, output->backend->egl.context)) { - wlr_log(L_ERROR, "eglMakeCurrent failed: %s", egl_error()); + wlr_log(L_ERROR, "eglMakeCurrent failed"); goto error; } diff --git a/backend/wayland/output.c b/backend/wayland/output.c index fc40dea0..6becceb7 100644 --- a/backend/wayland/output.c +++ b/backend/wayland/output.c @@ -321,7 +321,7 @@ struct wlr_output *wlr_wl_output_create(struct wlr_backend *_backend) { if (!eglMakeCurrent(output->backend->egl.display, output->egl_surface, output->egl_surface, output->backend->egl.context)) { - wlr_log(L_ERROR, "eglMakeCurrent failed: %s", egl_error()); + wlr_log(L_ERROR, "eglMakeCurrent failed"); goto error; } @@ -333,7 +333,7 @@ struct wlr_output *wlr_wl_output_create(struct wlr_backend *_backend) { wl_callback_add_listener(output->frame_callback, &frame_listener, output); if (!eglSwapBuffers(output->backend->egl.display, output->egl_surface)) { - wlr_log(L_ERROR, "eglSwapBuffers failed: %s", egl_error()); + wlr_log(L_ERROR, "eglSwapBuffers failed"); goto error; } diff --git a/include/wlr/render/egl.h b/include/wlr/render/egl.h index f05a9837..aa429e8e 100644 --- a/include/wlr/render/egl.h +++ b/include/wlr/render/egl.h @@ -94,11 +94,6 @@ int wlr_egl_get_dmabuf_modifiers(struct wlr_egl *egl, int format, */ bool wlr_egl_destroy_image(struct wlr_egl *egl, EGLImageKHR image); -/** - * Returns a string for the last error ocurred with egl. - */ -const char *egl_error(void); - bool wlr_egl_make_current(struct wlr_egl *egl, EGLSurface surface, int *buffer_age); diff --git a/render/egl.c b/render/egl.c index d60da2ab..b1bd4884 100644 --- a/render/egl.c +++ b/render/egl.c @@ -12,43 +12,6 @@ // https://www.khronos.org/registry/EGL/extensions/KHR/EGL_KHR_image_base.txt. // https://cgit.freedesktop.org/mesa/mesa/tree/docs/specs/WL_bind_wayland_display.spec -const char *egl_error(void) { - switch (eglGetError()) { - case EGL_SUCCESS: - return "Success"; - case EGL_NOT_INITIALIZED: - return "Not initialized"; - case EGL_BAD_ACCESS: - return "Bad access"; - case EGL_BAD_ALLOC: - return "Bad alloc"; - case EGL_BAD_ATTRIBUTE: - return "Bad attribute"; - case EGL_BAD_CONTEXT: - return "Bad Context"; - case EGL_BAD_CONFIG: - return "Bad Config"; - case EGL_BAD_CURRENT_SURFACE: - return "Bad current surface"; - case EGL_BAD_DISPLAY: - return "Bad display"; - case EGL_BAD_SURFACE: - return "Bad surface"; - case EGL_BAD_MATCH: - return "Bad match"; - case EGL_BAD_PARAMETER: - return "Bad parameter"; - case EGL_BAD_NATIVE_PIXMAP: - return "Bad native pixmap"; - case EGL_BAD_NATIVE_WINDOW: - return "Bad native window"; - case EGL_CONTEXT_LOST: - return "Context lost"; - default: - return "Unknown"; - } -} - static bool egl_get_config(EGLDisplay disp, EGLint *attribs, EGLConfig *out, EGLint visual_id) { EGLint count = 0, matched = 0, ret; @@ -84,6 +47,21 @@ static bool egl_get_config(EGLDisplay disp, EGLint *attribs, EGLConfig *out, return false; } +static log_importance_t egl_log_importance_to_wlr(EGLint type) { + switch (type) { + case EGL_DEBUG_MSG_CRITICAL_KHR: return L_ERROR; + case EGL_DEBUG_MSG_ERROR_KHR: return L_ERROR; + case EGL_DEBUG_MSG_WARN_KHR: return L_ERROR; + case EGL_DEBUG_MSG_INFO_KHR: return L_INFO; + default: return L_INFO; + } +} + +static void egl_log(EGLenum error, const char *command, EGLint msg_type, + EGLLabelKHR thread, EGLLabelKHR obj, const char *msg) { + _wlr_log(egl_log_importance_to_wlr(msg_type), "[EGL] %s: %s", command, msg); +} + static bool check_egl_ext(const char *egl_exts, const char *ext) { size_t extlen = strlen(ext); const char *end = egl_exts + strlen(egl_exts); @@ -128,8 +106,19 @@ bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, void *remote_display, return false; } + if (eglDebugMessageControlKHR) { + static const EGLAttrib debug_attribs[] = { + EGL_DEBUG_MSG_CRITICAL_KHR, EGL_TRUE, + EGL_DEBUG_MSG_ERROR_KHR, EGL_TRUE, + EGL_DEBUG_MSG_WARN_KHR, EGL_TRUE, + EGL_DEBUG_MSG_INFO_KHR, EGL_TRUE, + EGL_NONE, + }; + eglDebugMessageControlKHR(egl_log, debug_attribs); + } + if (eglBindAPI(EGL_OPENGL_ES_API) == EGL_FALSE) { - wlr_log(L_ERROR, "Failed to bind to the OpenGL ES API: %s", egl_error()); + wlr_log(L_ERROR, "Failed to bind to the OpenGL ES API"); goto error; } @@ -140,13 +129,13 @@ bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, void *remote_display, egl->display = eglGetPlatformDisplayEXT(platform, remote_display, NULL); } if (egl->display == EGL_NO_DISPLAY) { - wlr_log(L_ERROR, "Failed to create EGL display: %s", egl_error()); + wlr_log(L_ERROR, "Failed to create EGL display"); goto error; } EGLint major, minor; if (eglInitialize(egl->display, &major, &minor) == EGL_FALSE) { - wlr_log(L_ERROR, "Failed to initialize EGL: %s", egl_error()); + wlr_log(L_ERROR, "Failed to initialize EGL"); goto error; } @@ -161,7 +150,7 @@ bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, void *remote_display, EGL_NO_CONTEXT, attribs); if (egl->context == EGL_NO_CONTEXT) { - wlr_log(L_ERROR, "Failed to create EGL context: %s", egl_error()); + wlr_log(L_ERROR, "Failed to create EGL context"); goto error; } @@ -199,12 +188,18 @@ bool wlr_egl_init(struct wlr_egl *egl, EGLenum platform, void *remote_display, error: eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); - eglTerminate(egl->display); + if (egl->display) { + eglTerminate(egl->display); + } eglReleaseThread(); return false; } void wlr_egl_finish(struct wlr_egl *egl) { + if (egl == NULL) { + return; + } + eglMakeCurrent(EGL_NO_DISPLAY, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); if (egl->wl_display && eglUnbindWaylandDisplayWL) { eglUnbindWaylandDisplayWL(egl->display, egl->wl_display); @@ -259,7 +254,7 @@ EGLSurface wlr_egl_create_surface(struct wlr_egl *egl, void *window) { EGLSurface surf = eglCreatePlatformWindowSurfaceEXT(egl->display, egl->config, window, NULL); if (surf == EGL_NO_SURFACE) { - wlr_log(L_ERROR, "Failed to create EGL surface: %s", egl_error()); + wlr_log(L_ERROR, "Failed to create EGL surface"); return EGL_NO_SURFACE; } return surf; @@ -274,7 +269,7 @@ int wlr_egl_get_buffer_age(struct wlr_egl *egl, EGLSurface surface) { EGLBoolean ok = eglQuerySurface(egl->display, surface, EGL_BUFFER_AGE_EXT, &buffer_age); if (!ok) { - wlr_log(L_ERROR, "Failed to get EGL surface buffer age: %s", egl_error()); + wlr_log(L_ERROR, "Failed to get EGL surface buffer age"); return -1; } @@ -284,7 +279,7 @@ int wlr_egl_get_buffer_age(struct wlr_egl *egl, EGLSurface surface) { bool wlr_egl_make_current(struct wlr_egl *egl, EGLSurface surface, int *buffer_age) { if (!eglMakeCurrent(egl->display, surface, surface, egl->context)) { - wlr_log(L_ERROR, "eglMakeCurrent failed: %s", egl_error()); + wlr_log(L_ERROR, "eglMakeCurrent failed"); return false; } @@ -322,7 +317,7 @@ bool wlr_egl_swap_buffers(struct wlr_egl *egl, EGLSurface surface, } if (!ret) { - wlr_log(L_ERROR, "eglSwapBuffers failed: %s", egl_error()); + wlr_log(L_ERROR, "eglSwapBuffers failed"); return false; } return true; diff --git a/render/glapi.txt b/render/glapi.txt index 02ac7dd8..2077d1c3 100644 --- a/render/glapi.txt +++ b/render/glapi.txt @@ -10,3 +10,4 @@ eglCreatePlatformWindowSurfaceEXT -eglSwapBuffersWithDamageKHR -eglQueryDmaBufFormatsEXT -eglQueryDmaBufModifiersEXT +-eglDebugMessageControlKHR diff --git a/render/gles2/texture.c b/render/gles2/texture.c index 99f153b8..8da40032 100644 --- a/render/gles2/texture.c +++ b/render/gles2/texture.c @@ -198,7 +198,7 @@ static bool gles2_texture_upload_drm(struct wlr_texture *_tex, tex->image = wlr_egl_create_image(tex->egl, EGL_WAYLAND_BUFFER_WL, (EGLClientBuffer*) buf, attribs); if (!tex->image) { - wlr_log(L_ERROR, "failed to create egl image: %s", egl_error()); + wlr_log(L_ERROR, "failed to create EGL image"); return false; } -- cgit v1.2.3