From f4754ad1a21eaede20f6e9c5d4e0c78294119cc1 Mon Sep 17 00:00:00 2001 From: emersion Date: Thu, 30 Nov 2017 23:58:12 +0100 Subject: [PATCH 1/6] Fix surface transforms --- include/wlr/render/matrix.h | 2 ++ include/wlr/types/wlr_output.h | 2 ++ render/matrix.c | 17 +++++++++++++++++ rootston/output.c | 15 +++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/include/wlr/render/matrix.h b/include/wlr/render/matrix.h index 177af4b35..0b35aad3f 100644 --- a/include/wlr/render/matrix.h +++ b/include/wlr/render/matrix.h @@ -10,6 +10,8 @@ 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); diff --git a/include/wlr/types/wlr_output.h b/include/wlr/types/wlr_output.h index d382b593d..b1b830445 100644 --- a/include/wlr/types/wlr_output.h +++ b/include/wlr/types/wlr_output.h @@ -106,4 +106,6 @@ bool wlr_output_cursor_move(struct wlr_output_cursor *cursor, double x, double y); void wlr_output_cursor_destroy(struct wlr_output_cursor *cursor); +enum wl_output_transform wlr_output_transform_invert(enum wl_output_transform); + #endif diff --git a/render/matrix.c b/render/matrix.c index e49d365e3..8a3352e03 100644 --- a/render/matrix.c +++ b/render/matrix.c @@ -118,6 +118,23 @@ static const float transforms[][4] = { }, }; +void wlr_matrix_transform(float mat[static 16], + enum wl_output_transform transform) { + memset(mat, 0, sizeof(*mat) * 16); + + const float *t = transforms[transform]; + + // Rotation + relection + 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; +} + // 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, enum wl_output_transform transform) { diff --git a/rootston/output.c b/rootston/output.c index bf684f2f4..c1d2e9f35 100644 --- a/rootston/output.c +++ b/rootston/output.c @@ -63,10 +63,25 @@ static void render_surface(struct wlr_surface *surface, float scale[16]; wlr_matrix_scale(&scale, render_width, render_height, 1); + float translate_mdr[16]; + wlr_matrix_translate(&translate_mdr, 0.5, 0.5, 0); + + float surface_transform[16]; + wlr_matrix_transform(surface_transform, + wlr_output_transform_invert(surface->current->transform)); // TODO + + float translate_mdr2[16]; + wlr_matrix_translate(&translate_mdr2, -0.5, -0.5, 0); + float transform[16]; wlr_matrix_mul(&translate_origin, &rotate, &transform); wlr_matrix_mul(&transform, &translate_center, &transform); wlr_matrix_mul(&transform, &scale, &transform); + + wlr_matrix_mul(&transform, &translate_mdr, &transform); + wlr_matrix_mul(&transform, &surface_transform, &transform); + wlr_matrix_mul(&transform, &translate_mdr2, &transform); + wlr_matrix_mul(&wlr_output->transform_matrix, &transform, &matrix); wlr_render_with_matrix(desktop->server->renderer, surface->texture, From 6a69b4419fa25f4bbcbe3253ed000276cb6ace42 Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 1 Dec 2017 09:15:33 +0100 Subject: [PATCH 2/6] Make wlr_output_transform_* functions public, refactoring --- backend/drm/drm.c | 5 ++- backend/wayland/wl_seat.c | 5 ++- include/wlr/interfaces/wlr_output.h | 4 --- include/wlr/types/wlr_box.h | 4 +++ render/matrix.c | 28 ++++++++--------- types/wlr_box.c | 47 +++++++++++++++++++++++++++++ types/wlr_output.c | 46 ---------------------------- 7 files changed, 69 insertions(+), 70 deletions(-) diff --git a/backend/drm/drm.c b/backend/drm/drm.c index dd2479988..cf6ad550a 100644 --- a/backend/drm/drm.c +++ b/backend/drm/drm.c @@ -569,8 +569,7 @@ static bool wlr_drm_connector_set_cursor(struct wlr_output *output, enum wl_output_transform transform = wlr_output_transform_invert(output->transform); struct wlr_box transformed_hotspot; - wlr_output_transform_apply_to_box(transform, &hotspot, - &transformed_hotspot); + wlr_box_transform(&hotspot, transform, &transformed_hotspot); plane->cursor_hotspot_x = transformed_hotspot.x; plane->cursor_hotspot_y = transformed_hotspot.y; @@ -632,7 +631,7 @@ static bool wlr_drm_connector_move_cursor(struct wlr_output *output, enum wl_output_transform transform = wlr_output_transform_invert(output->transform); struct wlr_box transformed_box; - wlr_output_transform_apply_to_box(transform, &box, &transformed_box); + wlr_box_transform(&box, transform, &transformed_box); transformed_box.x -= plane->cursor_hotspot_x; transformed_box.y -= plane->cursor_hotspot_y; diff --git a/backend/wayland/wl_seat.c b/backend/wayland/wl_seat.c index a2da8df59..77c46164d 100644 --- a/backend/wayland/wl_seat.c +++ b/backend/wayland/wl_seat.c @@ -55,9 +55,8 @@ static void pointer_handle_motion(void *data, struct wl_pointer *wl_pointer, box.x = wl_fixed_to_int(surface_x); box.y = wl_fixed_to_int(surface_y); struct wlr_box transformed; - wlr_output_transform_apply_to_box( - wlr_wl_pointer->current_output->wlr_output.transform, &box, - &transformed); + wlr_box_transform(&box, + wlr_wl_pointer->current_output->wlr_output.transform, &transformed); struct wlr_event_pointer_motion_absolute wlr_event; wlr_event.device = dev; diff --git a/include/wlr/interfaces/wlr_output.h b/include/wlr/interfaces/wlr_output.h index b4f39d354..8fbb650a4 100644 --- a/include/wlr/interfaces/wlr_output.h +++ b/include/wlr/interfaces/wlr_output.h @@ -32,8 +32,4 @@ struct wl_global *wlr_output_create_global(struct wlr_output *wlr_output, struct wl_display *display); void wlr_output_destroy_global(struct wlr_output *wlr_output); -void wlr_output_transform_apply_to_box(enum wl_output_transform transform, - struct wlr_box *box, struct wlr_box *dest); -enum wl_output_transform wlr_output_transform_invert(enum wl_output_transform); - #endif diff --git a/include/wlr/types/wlr_box.h b/include/wlr/types/wlr_box.h index 5b8b00c97..5290ada5f 100644 --- a/include/wlr/types/wlr_box.h +++ b/include/wlr/types/wlr_box.h @@ -18,4 +18,8 @@ bool wlr_box_contains_point(struct wlr_box *box, double x, double y); bool wlr_box_empty(struct wlr_box *box); +enum wl_output_transform; +void wlr_box_transform(struct wlr_box *box, enum wl_output_transform transform, + struct wlr_box *dest); + #endif diff --git a/render/matrix.c b/render/matrix.c index 8a3352e03..6118e6a57 100644 --- a/render/matrix.c +++ b/render/matrix.c @@ -86,35 +86,35 @@ void wlr_matrix_mul(const float (*x)[16], const float (*y)[16], float (*product) static const float transforms[][4] = { [WL_OUTPUT_TRANSFORM_NORMAL] = { 1.0f, 0.0f, - 0.0f, -1.0f, + 0.0f, 1.0f, }, [WL_OUTPUT_TRANSFORM_90] = { 0.0f, -1.0f, - -1.0f, 0.0f, + 1.0f, 0.0f, }, [WL_OUTPUT_TRANSFORM_180] = { -1.0f, 0.0f, - 0.0f, 1.0f, + 0.0f, -1.0f, }, [WL_OUTPUT_TRANSFORM_270] = { 0.0f, 1.0f, - 1.0f, 0.0f, + -1.0f, 0.0f, }, [WL_OUTPUT_TRANSFORM_FLIPPED] = { -1.0f, 0.0f, - 0.0f, -1.0f, + 0.0f, 1.0f, }, [WL_OUTPUT_TRANSFORM_FLIPPED_90] = { 0.0f, 1.0f, - -1.0f, 0.0f, + 1.0f, 0.0f, }, [WL_OUTPUT_TRANSFORM_FLIPPED_180] = { 1.0f, 0.0f, - 0.0f, 1.0f, + 0.0f, -1.0f, }, [WL_OUTPUT_TRANSFORM_FLIPPED_270] = { 0.0f, -1.0f, - 1.0f, 0.0f, + -1.0f, 0.0f, }, }; @@ -124,11 +124,11 @@ void wlr_matrix_transform(float mat[static 16], const float *t = transforms[transform]; - // Rotation + relection + // Rotation + reflection mat[0] = t[0]; mat[1] = t[1]; - mat[4] = -t[2]; - mat[5] = -t[3]; + mat[4] = t[2]; + mat[5] = t[3]; // Identity mat[10] = 1.0f; @@ -144,11 +144,11 @@ void wlr_matrix_texture(float mat[static 16], int32_t width, int32_t height, float x = 2.0f / width; float y = 2.0f / height; - // Rotation + relection + // Rotation + reflection mat[0] = x * t[0]; mat[1] = x * t[1]; - mat[4] = y * t[2]; - mat[5] = y * t[3]; + mat[4] = y * -t[2]; + mat[5] = y * -t[3]; // Translation mat[3] = -copysign(1.0f, mat[0] + mat[1]); diff --git a/types/wlr_box.c b/types/wlr_box.c index 7e981833b..88f4b8ab4 100644 --- a/types/wlr_box.c +++ b/types/wlr_box.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -65,3 +66,49 @@ bool wlr_box_contains_point(struct wlr_box *box, double x, double y) { y >= box->y && y <= box->y + box->height; } } + +void wlr_box_transform(struct wlr_box *box, + enum wl_output_transform transform, struct wlr_box *dest) { + if (transform % 2 == 0) { + dest->width = box->width; + dest->height = box->height; + } else { + dest->width = box->height; + dest->height = box->width; + } + + switch (transform) { + case WL_OUTPUT_TRANSFORM_NORMAL: + dest->x = box->x; + dest->y = box->y; + break; + case WL_OUTPUT_TRANSFORM_90: + dest->x = box->y; + dest->y = box->width - box->x; + break; + case WL_OUTPUT_TRANSFORM_180: + dest->x = box->width - box->x; + dest->y = box->height - box->y; + break; + case WL_OUTPUT_TRANSFORM_270: + dest->x = box->height - box->y; + dest->y = box->x; + break; + case WL_OUTPUT_TRANSFORM_FLIPPED: + dest->x = box->width - box->x; + dest->y = box->y; + break; + case WL_OUTPUT_TRANSFORM_FLIPPED_90: + dest->x = box->y; + dest->y = box->x; + break; + case WL_OUTPUT_TRANSFORM_FLIPPED_180: + dest->x = box->x; + dest->y = box->height - box->y; + break; + case WL_OUTPUT_TRANSFORM_FLIPPED_270: + dest->x = box->height - box->y; + dest->y = box->width - box->x; + break; + } +} diff --git a/types/wlr_output.c b/types/wlr_output.c index 21d94cfb7..aad7dfeca 100644 --- a/types/wlr_output.c +++ b/types/wlr_output.c @@ -626,52 +626,6 @@ void wlr_output_cursor_destroy(struct wlr_output_cursor *cursor) { free(cursor); } -void wlr_output_transform_apply_to_box(enum wl_output_transform transform, - struct wlr_box *box, struct wlr_box *dest) { - if (transform % 2 == 0) { - dest->width = box->width; - dest->height = box->height; - } else { - dest->width = box->height; - dest->height = box->width; - } - - switch (transform) { - case WL_OUTPUT_TRANSFORM_NORMAL: - dest->x = box->x; - dest->y = box->y; - break; - case WL_OUTPUT_TRANSFORM_90: - dest->x = box->y; - dest->y = box->width - box->x; - break; - case WL_OUTPUT_TRANSFORM_180: - dest->x = box->width - box->x; - dest->y = box->height - box->y; - break; - case WL_OUTPUT_TRANSFORM_270: - dest->x = box->height - box->y; - dest->y = box->x; - break; - case WL_OUTPUT_TRANSFORM_FLIPPED: - dest->x = box->width - box->x; - dest->y = box->y; - break; - case WL_OUTPUT_TRANSFORM_FLIPPED_90: - dest->x = box->y; - dest->y = box->x; - break; - case WL_OUTPUT_TRANSFORM_FLIPPED_180: - dest->x = box->x; - dest->y = box->height - box->y; - break; - case WL_OUTPUT_TRANSFORM_FLIPPED_270: - dest->x = box->height - box->y; - dest->y = box->width - box->x; - break; - } -} - enum wl_output_transform wlr_output_transform_invert( enum wl_output_transform transform) { if ((transform & WL_OUTPUT_TRANSFORM_90) && From 4a56957a37669b38038910b41eb2b128f3a5042e Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 1 Dec 2017 09:49:32 +0100 Subject: [PATCH 3/6] Fix inverted flipped-90 and flipped-270 --- render/matrix.c | 8 ++++---- types/wlr_box.c | 8 ++++---- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/render/matrix.c b/render/matrix.c index 6118e6a57..54dba4cc6 100644 --- a/render/matrix.c +++ b/render/matrix.c @@ -105,16 +105,16 @@ static const float transforms[][4] = { 0.0f, 1.0f, }, [WL_OUTPUT_TRANSFORM_FLIPPED_90] = { - 0.0f, 1.0f, - 1.0f, 0.0f, + 0.0f, -1.0f, + -1.0f, 0.0f, }, [WL_OUTPUT_TRANSFORM_FLIPPED_180] = { 1.0f, 0.0f, 0.0f, -1.0f, }, [WL_OUTPUT_TRANSFORM_FLIPPED_270] = { - 0.0f, -1.0f, - -1.0f, 0.0f, + 0.0f, 1.0f, + 1.0f, 0.0f, }, }; diff --git a/types/wlr_box.c b/types/wlr_box.c index 88f4b8ab4..5e910b2e6 100644 --- a/types/wlr_box.c +++ b/types/wlr_box.c @@ -99,16 +99,16 @@ void wlr_box_transform(struct wlr_box *box, dest->y = box->y; break; case WL_OUTPUT_TRANSFORM_FLIPPED_90: - dest->x = box->y; - dest->y = box->x; + dest->x = box->height - box->y; + dest->y = box->width - box->x; break; case WL_OUTPUT_TRANSFORM_FLIPPED_180: dest->x = box->x; dest->y = box->height - box->y; break; case WL_OUTPUT_TRANSFORM_FLIPPED_270: - dest->x = box->height - box->y; - dest->y = box->width - box->x; + dest->x = box->y; + dest->y = box->x; break; } } From acc8f368945d540796078f0530234734e5cb1b37 Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 1 Dec 2017 16:08:01 +0100 Subject: [PATCH 4/6] Fix pointer input for transformed surfaces --- rootston/desktop.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/rootston/desktop.c b/rootston/desktop.c index bb3af2586..8cede819b 100644 --- a/rootston/desktop.c +++ b/rootston/desktop.c @@ -295,10 +295,8 @@ static bool view_at(struct roots_view *view, double lx, double ly, struct wlr_surface_state *state = view->wlr_surface->current; struct wlr_box box = { - .x = 0, - .y = 0, - .width = state->buffer_width / state->scale, - .height = state->buffer_height / state->scale, + .x = 0, .y = 0, + .width = state->width, .height = state->height, }; if (view->rotation != 0.0) { // Coordinates relative to the center of the view From d1b29a54b92c5d13739430fe98b12ff169964272 Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 1 Dec 2017 16:13:24 +0100 Subject: [PATCH 5/6] Refactor: rename matrices in rootston --- rootston/output.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/rootston/output.c b/rootston/output.c index c1d2e9f35..9df95a91f 100644 --- a/rootston/output.c +++ b/rootston/output.c @@ -49,38 +49,38 @@ static void render_surface(struct wlr_surface *surface, lx, ly, lx + render_width, ly + render_height)) { float matrix[16]; - float translate_origin[16]; - wlr_matrix_translate(&translate_origin, + float translate_center[16]; + wlr_matrix_translate(&translate_center, (int)ox + render_width / 2, (int)oy + render_height / 2, 0); float rotate[16]; wlr_matrix_rotate(&rotate, rotation); - float translate_center[16]; - wlr_matrix_translate(&translate_center, -render_width / 2, + float translate_origin[16]; + wlr_matrix_translate(&translate_origin, -render_width / 2, -render_height / 2, 0); float scale[16]; wlr_matrix_scale(&scale, render_width, render_height, 1); - float translate_mdr[16]; - wlr_matrix_translate(&translate_mdr, 0.5, 0.5, 0); + 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, - wlr_output_transform_invert(surface->current->transform)); // TODO + wlr_output_transform_invert(surface->current->transform)); - float translate_mdr2[16]; - wlr_matrix_translate(&translate_mdr2, -0.5, -0.5, 0); + float surface_translate_origin[16]; + wlr_matrix_translate(&surface_translate_origin, -0.5, -0.5, 0); float transform[16]; - wlr_matrix_mul(&translate_origin, &rotate, &transform); - wlr_matrix_mul(&transform, &translate_center, &transform); + wlr_matrix_mul(&translate_center, &rotate, &transform); + wlr_matrix_mul(&transform, &translate_origin, &transform); wlr_matrix_mul(&transform, &scale, &transform); - wlr_matrix_mul(&transform, &translate_mdr, &transform); + wlr_matrix_mul(&transform, &surface_translate_center, &transform); wlr_matrix_mul(&transform, &surface_transform, &transform); - wlr_matrix_mul(&transform, &translate_mdr2, &transform); + wlr_matrix_mul(&transform, &surface_translate_origin, &transform); wlr_matrix_mul(&wlr_output->transform_matrix, &transform, &matrix); From eb763439f75d3c9174280d0c75ef44941dd99340 Mon Sep 17 00:00:00 2001 From: emersion Date: Fri, 1 Dec 2017 16:20:09 +0100 Subject: [PATCH 6/6] optimize a bit rootston renderer if there's no surface transform --- rootston/output.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/rootston/output.c b/rootston/output.c index 9df95a91f..996f819dd 100644 --- a/rootston/output.c +++ b/rootston/output.c @@ -63,24 +63,28 @@ static void render_surface(struct wlr_surface *surface, float scale[16]; wlr_matrix_scale(&scale, render_width, render_height, 1); - 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, - wlr_output_transform_invert(surface->current->transform)); - - float surface_translate_origin[16]; - wlr_matrix_translate(&surface_translate_origin, -0.5, -0.5, 0); - float transform[16]; wlr_matrix_mul(&translate_center, &rotate, &transform); wlr_matrix_mul(&transform, &translate_origin, &transform); wlr_matrix_mul(&transform, &scale, &transform); - wlr_matrix_mul(&transform, &surface_translate_center, &transform); - wlr_matrix_mul(&transform, &surface_transform, &transform); - wlr_matrix_mul(&transform, &surface_translate_origin, &transform); + if (surface->current->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, + wlr_output_transform_invert(surface->current->transform)); + + float surface_translate_origin[16]; + wlr_matrix_translate(&surface_translate_origin, -0.5, -0.5, 0); + + wlr_matrix_mul(&transform, &surface_translate_center, + &transform); + wlr_matrix_mul(&transform, &surface_transform, &transform); + wlr_matrix_mul(&transform, &surface_translate_origin, + &transform); + } wlr_matrix_mul(&wlr_output->transform_matrix, &transform, &matrix);