Merge pull request #722 from emersion/matrix-redesign

Matrix redesign
This commit is contained in:
Drew DeVault 2018-03-17 15:22:00 -04:00 committed by GitHub
commit 1956d3cedb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
27 changed files with 334 additions and 468 deletions

View file

@ -6,11 +6,13 @@ lib_wlr_types = static_library(
'wlr_cursor.c',
'wlr_data_device.c',
'wlr_gamma_control.c',
'wlr_idle_inhibit_v1.c',
'wlr_idle.c',
'wlr_input_device.c',
'wlr_keyboard.c',
'wlr_linux_dmabuf.c',
'wlr_list.c',
'wlr_matrix.c',
'wlr_output_damage.c',
'wlr_output_layout.c',
'wlr_output.c',
@ -28,7 +30,6 @@ lib_wlr_types = static_library(
'wlr_xcursor_manager.c',
'wlr_xdg_shell_v6.c',
'wlr_xdg_shell.c',
'wlr_idle_inhibit_v1.c',
),
include_directories: wlr_inc,
dependencies: [pixman, xkbcommon, wayland_server, wlr_protos],

160
types/wlr_matrix.c Normal file
View file

@ -0,0 +1,160 @@
#include <math.h>
#include <string.h>
#include <wayland-server-protocol.h>
#include <wlr/types/wlr_matrix.h>
#include <wlr/types/wlr_box.h>
#include <wlr/types/wlr_output.h>
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_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_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_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_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,
};
wlr_matrix_multiply(mat, mat, rotate);
}
static const float transforms[][9] = {
[WL_OUTPUT_TRANSFORM_NORMAL] = {
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, 0.0f,
1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f,
},
[WL_OUTPUT_TRANSFORM_180] = {
-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, 0.0f,
-1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f,
},
[WL_OUTPUT_TRANSFORM_FLIPPED] = {
-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, 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,
0.0f, -1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
},
[WL_OUTPUT_TRANSFORM_FLIPPED_270] = {
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 9],
enum wl_output_transform transform) {
wlr_matrix_multiply(mat, mat, transforms[transform]);
}
// Equivilent to glOrtho(0, width, 0, height, 1, -1) with the transform applied
void wlr_matrix_projection(float mat[static 9], int width, int height,
enum wl_output_transform transform) {
memset(mat, 0, sizeof(*mat) * 9);
const float *t = transforms[transform];
float x = 2.0f / width;
float y = 2.0f / height;
// Rotation + reflection
mat[0] = x * t[0];
mat[1] = x * t[1];
mat[3] = y * -t[3];
mat[4] = y * -t[4];
// Translation
mat[2] = -copysign(1.0f, mat[0] + mat[1]);
mat[5] = -copysign(1.0f, mat[3] + mat[4]);
// Identity
mat[8] = 1.0f;
}
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 9]) {
int x = box->x;
int y = box->y;
int width = box->width;
int height = box->height;
wlr_matrix_identity(mat);
wlr_matrix_translate(mat, x, y);
if (rotation != 0) {
wlr_matrix_translate(mat, width/2, height/2);
wlr_matrix_rotate(mat, rotation);
wlr_matrix_translate(mat, -width/2, -height/2);
}
wlr_matrix_scale(mat, width, height);
if (transform != WL_OUTPUT_TRANSFORM_NORMAL) {
wlr_matrix_translate(mat, 0.5, 0.5);
wlr_matrix_transform(mat, transform);
wlr_matrix_translate(mat, -0.5, -0.5);
}
wlr_matrix_multiply(mat, projection, mat);
}

View file

@ -7,7 +7,7 @@
#include <wayland-server.h>
#include <wlr/interfaces/wlr_output.h>
#include <wlr/render.h>
#include <wlr/render/matrix.h>
#include <wlr/types/wlr_matrix.h>
#include <wlr/types/wlr_box.h>
#include <wlr/types/wlr_output.h>
#include <wlr/types/wlr_surface.h>
@ -139,8 +139,8 @@ void wlr_output_update_enabled(struct wlr_output *output, bool enabled) {
}
static void wlr_output_update_matrix(struct wlr_output *output) {
wlr_matrix_texture(output->transform_matrix, output->width, output->height,
output->transform);
wlr_matrix_projection(output->transform_matrix, output->width,
output->height, output->transform);
}
void wlr_output_enable(struct wlr_output *output, bool enable) {
@ -368,25 +368,25 @@ 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;
}
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,
&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_texture_with_matrix(surface->renderer, surface->texture, matrix, 1.0f);
}
wlr_renderer_scissor(renderer, NULL);
@ -435,15 +435,15 @@ static void output_cursor_render(struct wlr_output_cursor *cursor,
goto surface_damage_finish;
}
float matrix[16];
wlr_matrix_project_box(&matrix, &box, WL_OUTPUT_TRANSFORM_NORMAL, 0,
&cursor->output->transform_matrix);
float matrix[9];
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_texture_with_matrix(renderer, texture, matrix, 1.0f);
}
wlr_renderer_scissor(renderer, NULL);

View file

@ -3,7 +3,7 @@
#include <wayland-server.h>
#include <wlr/render/egl.h>
#include <wlr/render/interface.h>
#include <wlr/render/matrix.h>
#include <wlr/types/wlr_matrix.h>
#include <wlr/types/wlr_region.h>
#include <wlr/types/wlr_surface.h>
#include <wlr/util/log.h>
@ -628,22 +628,6 @@ struct wlr_surface *wlr_surface_create(struct wl_resource *res,
return surface;
}
void wlr_surface_get_matrix(struct wlr_surface *surface,
float (*matrix)[16],
const float (*projection)[16],
const float (*transform)[16]) {
int width = surface->texture->width;
int height = surface->texture->height;
float scale[16];
wlr_matrix_identity(matrix);
if (transform) {
wlr_matrix_mul(matrix, transform, matrix);
}
wlr_matrix_scale(&scale, width, height, 1);
wlr_matrix_mul(matrix, &scale, matrix);
wlr_matrix_mul(projection, matrix, matrix);
}
bool wlr_surface_has_buffer(struct wlr_surface *surface) {
return surface->texture && surface->texture->valid;
}