Merge pull request #453 from emersion/surface-transform
Add surface transforms support
This commit is contained in:
commit
23fb663ea4
|
@ -565,8 +565,7 @@ static bool wlr_drm_connector_set_cursor(struct wlr_output *output,
|
||||||
enum wl_output_transform transform =
|
enum wl_output_transform transform =
|
||||||
wlr_output_transform_invert(output->transform);
|
wlr_output_transform_invert(output->transform);
|
||||||
struct wlr_box transformed_hotspot;
|
struct wlr_box transformed_hotspot;
|
||||||
wlr_output_transform_apply_to_box(transform, &hotspot,
|
wlr_box_transform(&hotspot, transform, &transformed_hotspot);
|
||||||
&transformed_hotspot);
|
|
||||||
plane->cursor_hotspot_x = transformed_hotspot.x;
|
plane->cursor_hotspot_x = transformed_hotspot.x;
|
||||||
plane->cursor_hotspot_y = transformed_hotspot.y;
|
plane->cursor_hotspot_y = transformed_hotspot.y;
|
||||||
|
|
||||||
|
@ -628,7 +627,7 @@ static bool wlr_drm_connector_move_cursor(struct wlr_output *output,
|
||||||
enum wl_output_transform transform =
|
enum wl_output_transform transform =
|
||||||
wlr_output_transform_invert(output->transform);
|
wlr_output_transform_invert(output->transform);
|
||||||
struct wlr_box transformed_box;
|
struct wlr_box transformed_box;
|
||||||
wlr_output_transform_apply_to_box(transform, &box, &transformed_box);
|
wlr_box_transform(&box, transform, &transformed_box);
|
||||||
|
|
||||||
if (plane != NULL) {
|
if (plane != NULL) {
|
||||||
transformed_box.x -= plane->cursor_hotspot_x;
|
transformed_box.x -= plane->cursor_hotspot_x;
|
||||||
|
|
|
@ -60,7 +60,8 @@ static void pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,
|
||||||
box.x = wl_fixed_to_int(surface_x);
|
box.x = wl_fixed_to_int(surface_x);
|
||||||
box.y = wl_fixed_to_int(surface_y);
|
box.y = wl_fixed_to_int(surface_y);
|
||||||
struct wlr_box transformed;
|
struct wlr_box transformed;
|
||||||
wlr_output_transform_apply_to_box(wlr_output->transform, &box, &transformed);
|
|
||||||
|
wlr_box_transform(&box, wlr_output->transform, &transformed);
|
||||||
box.x /= wlr_output->scale;
|
box.x /= wlr_output->scale;
|
||||||
box.y /= wlr_output->scale;
|
box.y /= wlr_output->scale;
|
||||||
|
|
||||||
|
|
|
@ -34,8 +34,4 @@ struct wl_global *wlr_output_create_global(struct wlr_output *wlr_output,
|
||||||
struct wl_display *display);
|
struct wl_display *display);
|
||||||
void wlr_output_destroy_global(struct wlr_output *wlr_output);
|
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
|
#endif
|
||||||
|
|
|
@ -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]);
|
void wlr_matrix_mul(const float (*x)[16], const float (*y)[16], float (*product)[16]);
|
||||||
|
|
||||||
enum wl_output_transform;
|
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,
|
void wlr_matrix_texture(float mat[static 16], int32_t width, int32_t height,
|
||||||
enum wl_output_transform transform);
|
enum wl_output_transform transform);
|
||||||
|
|
||||||
|
|
|
@ -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);
|
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
|
#endif
|
||||||
|
|
|
@ -117,4 +117,6 @@ bool wlr_output_cursor_move(struct wlr_output_cursor *cursor,
|
||||||
double x, double y);
|
double x, double y);
|
||||||
void wlr_output_cursor_destroy(struct wlr_output_cursor *cursor);
|
void wlr_output_cursor_destroy(struct wlr_output_cursor *cursor);
|
||||||
|
|
||||||
|
enum wl_output_transform wlr_output_transform_invert(enum wl_output_transform);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -86,38 +86,55 @@ void wlr_matrix_mul(const float (*x)[16], const float (*y)[16], float (*product)
|
||||||
static const float transforms[][4] = {
|
static const float transforms[][4] = {
|
||||||
[WL_OUTPUT_TRANSFORM_NORMAL] = {
|
[WL_OUTPUT_TRANSFORM_NORMAL] = {
|
||||||
1.0f, 0.0f,
|
1.0f, 0.0f,
|
||||||
0.0f, -1.0f,
|
0.0f, 1.0f,
|
||||||
},
|
},
|
||||||
[WL_OUTPUT_TRANSFORM_90] = {
|
[WL_OUTPUT_TRANSFORM_90] = {
|
||||||
0.0f, -1.0f,
|
0.0f, -1.0f,
|
||||||
-1.0f, 0.0f,
|
1.0f, 0.0f,
|
||||||
},
|
},
|
||||||
[WL_OUTPUT_TRANSFORM_180] = {
|
[WL_OUTPUT_TRANSFORM_180] = {
|
||||||
-1.0f, 0.0f,
|
-1.0f, 0.0f,
|
||||||
0.0f, 1.0f,
|
0.0f, -1.0f,
|
||||||
},
|
},
|
||||||
[WL_OUTPUT_TRANSFORM_270] = {
|
[WL_OUTPUT_TRANSFORM_270] = {
|
||||||
0.0f, 1.0f,
|
0.0f, 1.0f,
|
||||||
1.0f, 0.0f,
|
-1.0f, 0.0f,
|
||||||
},
|
},
|
||||||
[WL_OUTPUT_TRANSFORM_FLIPPED] = {
|
[WL_OUTPUT_TRANSFORM_FLIPPED] = {
|
||||||
-1.0f, 0.0f,
|
-1.0f, 0.0f,
|
||||||
0.0f, -1.0f,
|
0.0f, 1.0f,
|
||||||
},
|
},
|
||||||
[WL_OUTPUT_TRANSFORM_FLIPPED_90] = {
|
[WL_OUTPUT_TRANSFORM_FLIPPED_90] = {
|
||||||
0.0f, 1.0f,
|
0.0f, -1.0f,
|
||||||
-1.0f, 0.0f,
|
-1.0f, 0.0f,
|
||||||
},
|
},
|
||||||
[WL_OUTPUT_TRANSFORM_FLIPPED_180] = {
|
[WL_OUTPUT_TRANSFORM_FLIPPED_180] = {
|
||||||
1.0f, 0.0f,
|
1.0f, 0.0f,
|
||||||
0.0f, 1.0f,
|
0.0f, -1.0f,
|
||||||
},
|
},
|
||||||
[WL_OUTPUT_TRANSFORM_FLIPPED_270] = {
|
[WL_OUTPUT_TRANSFORM_FLIPPED_270] = {
|
||||||
0.0f, -1.0f,
|
0.0f, 1.0f,
|
||||||
1.0f, 0.0f,
|
1.0f, 0.0f,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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 + 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;
|
||||||
|
}
|
||||||
|
|
||||||
// Equivilent to glOrtho(0, width, 0, height, 1, -1) with the transform applied
|
// 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 16], int32_t width, int32_t height,
|
||||||
enum wl_output_transform transform) {
|
enum wl_output_transform transform) {
|
||||||
|
@ -127,11 +144,11 @@ void wlr_matrix_texture(float mat[static 16], int32_t width, int32_t height,
|
||||||
float x = 2.0f / width;
|
float x = 2.0f / width;
|
||||||
float y = 2.0f / height;
|
float y = 2.0f / height;
|
||||||
|
|
||||||
// Rotation + relection
|
// Rotation + reflection
|
||||||
mat[0] = x * t[0];
|
mat[0] = x * t[0];
|
||||||
mat[1] = x * t[1];
|
mat[1] = x * t[1];
|
||||||
mat[4] = y * t[2];
|
mat[4] = y * -t[2];
|
||||||
mat[5] = y * t[3];
|
mat[5] = y * -t[3];
|
||||||
|
|
||||||
// Translation
|
// Translation
|
||||||
mat[3] = -copysign(1.0f, mat[0] + mat[1]);
|
mat[3] = -copysign(1.0f, mat[0] + mat[1]);
|
||||||
|
|
|
@ -294,10 +294,8 @@ static bool view_at(struct roots_view *view, double lx, double ly,
|
||||||
|
|
||||||
struct wlr_surface_state *state = view->wlr_surface->current;
|
struct wlr_surface_state *state = view->wlr_surface->current;
|
||||||
struct wlr_box box = {
|
struct wlr_box box = {
|
||||||
.x = 0,
|
.x = 0, .y = 0,
|
||||||
.y = 0,
|
.width = state->width, .height = state->height,
|
||||||
.width = state->buffer_width / state->scale,
|
|
||||||
.height = state->buffer_height / state->scale,
|
|
||||||
};
|
};
|
||||||
if (view->rotation != 0.0) {
|
if (view->rotation != 0.0) {
|
||||||
// Coordinates relative to the center of the view
|
// Coordinates relative to the center of the view
|
||||||
|
|
|
@ -48,24 +48,43 @@ static void render_surface(struct wlr_surface *surface,
|
||||||
lx, ly, lx + render_width, ly + render_height)) {
|
lx, ly, lx + render_width, ly + render_height)) {
|
||||||
float matrix[16];
|
float matrix[16];
|
||||||
|
|
||||||
float translate_origin[16];
|
float translate_center[16];
|
||||||
wlr_matrix_translate(&translate_origin,
|
wlr_matrix_translate(&translate_center,
|
||||||
(int)ox + render_width / 2, (int)oy + render_height / 2, 0);
|
(int)ox + render_width / 2, (int)oy + render_height / 2, 0);
|
||||||
|
|
||||||
float rotate[16];
|
float rotate[16];
|
||||||
wlr_matrix_rotate(&rotate, rotation);
|
wlr_matrix_rotate(&rotate, rotation);
|
||||||
|
|
||||||
float translate_center[16];
|
float translate_origin[16];
|
||||||
wlr_matrix_translate(&translate_center, -render_width / 2,
|
wlr_matrix_translate(&translate_origin, -render_width / 2,
|
||||||
-render_height / 2, 0);
|
-render_height / 2, 0);
|
||||||
|
|
||||||
float scale[16];
|
float scale[16];
|
||||||
wlr_matrix_scale(&scale, render_width, render_height, 1);
|
wlr_matrix_scale(&scale, render_width, render_height, 1);
|
||||||
|
|
||||||
float transform[16];
|
float transform[16];
|
||||||
wlr_matrix_mul(&translate_origin, &rotate, &transform);
|
wlr_matrix_mul(&translate_center, &rotate, &transform);
|
||||||
wlr_matrix_mul(&transform, &translate_center, &transform);
|
wlr_matrix_mul(&transform, &translate_origin, &transform);
|
||||||
wlr_matrix_mul(&transform, &scale, &transform);
|
wlr_matrix_mul(&transform, &scale, &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);
|
wlr_matrix_mul(&wlr_output->transform_matrix, &transform, &matrix);
|
||||||
|
|
||||||
wlr_render_with_matrix(desktop->server->renderer, surface->texture,
|
wlr_render_with_matrix(desktop->server->renderer, surface->texture,
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <wayland-server-protocol.h>
|
||||||
#include <wlr/types/wlr_box.h>
|
#include <wlr/types/wlr_box.h>
|
||||||
#include <wlr/util/log.h>
|
#include <wlr/util/log.h>
|
||||||
|
|
||||||
|
@ -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;
|
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->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->y;
|
||||||
|
dest->y = box->x;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -713,52 +713,6 @@ void wlr_output_cursor_destroy(struct wlr_output_cursor *cursor) {
|
||||||
free(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 wlr_output_transform_invert(
|
||||||
enum wl_output_transform transform) {
|
enum wl_output_transform transform) {
|
||||||
if ((transform & WL_OUTPUT_TRANSFORM_90) &&
|
if ((transform & WL_OUTPUT_TRANSFORM_90) &&
|
||||||
|
|
Loading…
Reference in New Issue