diff options
-rw-r--r-- | .gitignore | 2 | ||||
-rw-r--r-- | include/render/fx_renderer/fx_renderer.h | 80 | ||||
-rw-r--r-- | include/render/fx_renderer/matrix.h | 9 | ||||
-rw-r--r-- | meson.build | 10 | ||||
-rw-r--r-- | render/fx_renderer/fx_renderer.c | 414 | ||||
-rw-r--r-- | render/fx_renderer/gles2/meson.build | 2 | ||||
-rw-r--r-- | render/fx_renderer/gles2/shaders/common.vert | 12 | ||||
-rw-r--r-- | render/fx_renderer/gles2/shaders/embed.sh | 11 | ||||
-rw-r--r-- | render/fx_renderer/gles2/shaders/meson.build | 21 | ||||
-rw-r--r-- | render/fx_renderer/gles2/shaders/quad.frag | 7 | ||||
-rw-r--r-- | render/fx_renderer/gles2/shaders/tex.frag | 44 | ||||
-rw-r--r-- | render/fx_renderer/matrix.c | 70 | ||||
-rw-r--r-- | render/fx_renderer/meson.build | 22 | ||||
-rw-r--r-- | render/meson.build | 2 | ||||
-rw-r--r-- | types/scene/wlr_scene.c | 63 |
15 files changed, 741 insertions, 28 deletions
@@ -1 +1,3 @@ /subprojects/ +build/ +.cache/ diff --git a/include/render/fx_renderer/fx_renderer.h b/include/render/fx_renderer/fx_renderer.h new file mode 100644 index 0000000..085cd1c --- /dev/null +++ b/include/render/fx_renderer/fx_renderer.h @@ -0,0 +1,80 @@ +#ifndef _FX_OPENGL_H +#define _FX_OPENGL_H + +#include <GLES2/gl2.h> +#include <GLES2/gl2ext.h> +#include <stdbool.h> +#include <wlr/render/egl.h> +#include <wlr/render/wlr_texture.h> +#include <wlr/util/addon.h> +#include <wlr/util/box.h> + +enum fx_tex_shader_source { + SHADER_SOURCE_TEXTURE_RGBA = 1, + SHADER_SOURCE_TEXTURE_RGBX = 2, + SHADER_SOURCE_TEXTURE_EXTERNAL = 3, +}; + +struct quad_shader { + GLuint program; + GLint proj; + GLint color; + GLint pos_attrib; +}; + +struct tex_shader { + GLuint program; + GLint proj; + GLint tex; + GLint alpha; + GLint pos_attrib; + GLint tex_attrib; + GLint size; + GLint position; + GLint radius; +}; + +struct fx_renderer { + float projection[9]; + + struct wlr_addon addon; + + struct { + bool OES_egl_image_external; + } exts; + + struct { + PFNGLEGLIMAGETARGETTEXTURE2DOESPROC glEGLImageTargetTexture2DOES; + } procs; + + struct { + struct quad_shader quad; + struct tex_shader tex_rgba; + struct tex_shader tex_rgbx; + struct tex_shader tex_ext; + } shaders; +}; + +void fx_renderer_init_addon(struct wlr_egl *egl, struct wlr_addon_set *addons, + const void * owner); + +struct fx_renderer *fx_renderer_addon_find(struct wlr_addon_set *addons, + const void * owner); + +struct fx_renderer *fx_renderer_create(struct wlr_egl *egl); + +void fx_renderer_fini(struct fx_renderer *renderer); + +void fx_renderer_begin(struct fx_renderer *renderer, int width, int height); + +void fx_renderer_clear(const float color[static 4]); + +void fx_renderer_scissor(struct wlr_box *box); + +bool fx_render_subtexture_with_matrix(struct fx_renderer *renderer, struct wlr_texture *wlr_texture, + const struct wlr_fbox *src_box, const struct wlr_box *dst_box, const float matrix[static 9]); + +void fx_render_rect(struct fx_renderer *renderer, const struct wlr_box *box, + const float color[static 4], const float projection[static 9]); + +#endif diff --git a/include/render/fx_renderer/matrix.h b/include/render/fx_renderer/matrix.h new file mode 100644 index 0000000..6931e8d --- /dev/null +++ b/include/render/fx_renderer/matrix.h @@ -0,0 +1,9 @@ +#ifndef _MATRIX_H +#define _MATRIX_H + +#include <wlr/types/wlr_output.h> + +void matrix_projection(float mat[static 9], int width, int height, + enum wl_output_transform transform); + +#endif diff --git a/meson.build b/meson.build index 75c7a7b..e96f2e6 100644 --- a/meson.build +++ b/meson.build @@ -107,9 +107,15 @@ wayland_server = dependency('wayland-server', ) wlroots_options = [ 'examples=false' ] +wlroots_version = ['>=0.16.0', '<0.17.0'] +subproject( + 'wlroots', + default_options: wlroots_options, + required: false, + version: wlroots_version, +) wlroots = dependency('wlroots', - version: '0.16.2', - fallback: 'wayland', + version: wlroots_version, default_options: wlroots_options, ) diff --git a/render/fx_renderer/fx_renderer.c b/render/fx_renderer/fx_renderer.c new file mode 100644 index 0000000..c30108a --- /dev/null +++ b/render/fx_renderer/fx_renderer.c @@ -0,0 +1,414 @@ +/* + The original wlr_renderer was heavily referenced in making this project + https://gitlab.freedesktop.org/wlroots/wlroots/-/tree/master/render/gles2 +*/ + +#include <assert.h> +#include <GLES2/gl2.h> +#include <stdio.h> +#include <stdlib.h> +#include <wlr/backend.h> +#include <wlr/render/egl.h> +#include <wlr/render/gles2.h> +#include <wlr/types/wlr_matrix.h> +#include <wlr/util/box.h> +#include <wlr/util/log.h> + +#include "render/fx_renderer/fx_renderer.h" +#include "render/fx_renderer/matrix.h" + +// shaders +#include "common_vert_src.h" +#include "quad_frag_src.h" +#include "tex_frag_src.h" + +static const GLfloat verts[] = { + 1, 0, // top right + 0, 0, // top left + 1, 1, // bottom right + 0, 1, // bottom left +}; + +static GLuint compile_shader(GLuint type, const GLchar *src) { + GLuint shader = glCreateShader(type); + glShaderSource(shader, 1, &src, NULL); + glCompileShader(shader); + + GLint ok; + glGetShaderiv(shader, GL_COMPILE_STATUS, &ok); + if (ok == GL_FALSE) { + wlr_log(WLR_ERROR, "Failed to compile shader"); + glDeleteShader(shader); + shader = 0; + } + + return shader; +} + +static GLuint link_program(const GLchar *frag_src) { + const GLchar *vert_src = common_vert_src; + 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) { + wlr_log(WLR_ERROR, "Failed to link shader"); + glDeleteProgram(prog); + goto error; + } + + return prog; + +error: + return 0; +} + +static bool link_quad_program(struct quad_shader *shader) { + GLuint prog; + shader->program = prog = link_program(quad_frag_src); + if (!shader->program) { + return false; + } + + shader->proj = glGetUniformLocation(prog, "proj"); + shader->color = glGetUniformLocation(prog, "color"); + shader->pos_attrib = glGetAttribLocation(prog, "pos"); + + return true; +} + +static bool link_tex_program(struct tex_shader *shader, + enum fx_tex_shader_source source) { + GLchar frag_src[2048]; + snprintf(frag_src, sizeof(frag_src), + "#define SOURCE %d\n%s", source, tex_frag_src); + + GLuint prog; + shader->program = prog = link_program(frag_src); + if (!shader->program) { + return false; + } + + shader->proj = glGetUniformLocation(prog, "proj"); + shader->tex = glGetUniformLocation(prog, "tex"); + shader->alpha = glGetUniformLocation(prog, "alpha"); + shader->pos_attrib = glGetAttribLocation(prog, "pos"); + shader->tex_attrib = glGetAttribLocation(prog, "texcoord"); + shader->size = glGetUniformLocation(prog, "size"); + shader->position = glGetUniformLocation(prog, "position"); + shader->radius = glGetUniformLocation(prog, "radius"); + + return true; +} + +static bool check_gl_ext(const char *exts, const char *ext) { + size_t extlen = strlen(ext); + const char *end = exts + strlen(exts); + + while (exts < end) { + if (exts[0] == ' ') { + exts++; + continue; + } + size_t n = strcspn(exts, " "); + if (n == extlen && strncmp(ext, exts, n) == 0) { + return true; + } + exts += n; + } + return false; +} + +static void load_gl_proc(void *proc_ptr, const char *name) { + void *proc = (void *)eglGetProcAddress(name); + if (proc == NULL) { + wlr_log(WLR_ERROR, "GLES2 RENDERER: eglGetProcAddress(%s) failed", name); + abort(); + } + *(void **)proc_ptr = proc; +} + +static void fx_renderer_handle_destroy(struct wlr_addon *addon) { + struct fx_renderer *renderer = + wl_container_of(addon, renderer, addon); + fx_renderer_fini(renderer); + free(renderer); +} +static const struct wlr_addon_interface fx_renderer_addon_impl = { + .name = "fx_renderer", + .destroy = fx_renderer_handle_destroy, +}; + +void fx_renderer_init_addon(struct wlr_egl *egl, struct wlr_addon_set *addons, + const void * owner) { + struct fx_renderer *renderer = fx_renderer_create(egl); + if (!renderer) { + wlr_log(WLR_ERROR, "Failed to create fx_renderer"); + abort(); + } + wlr_addon_init(&renderer->addon, addons, owner, &fx_renderer_addon_impl); +} + +struct fx_renderer *fx_renderer_addon_find(struct wlr_addon_set *addons, + const void * owner) { + struct wlr_addon *addon = + wlr_addon_find(addons, owner, &fx_renderer_addon_impl); + if (addon == NULL) { + return NULL; + } + struct fx_renderer *renderer = wl_container_of(addon, renderer, addon); + return renderer; +} + +struct fx_renderer *fx_renderer_create(struct wlr_egl *egl) { + struct fx_renderer *renderer = calloc(1, sizeof(struct fx_renderer)); + if (renderer == NULL) { + return NULL; + } + + if (!eglMakeCurrent(wlr_egl_get_display(egl), EGL_NO_SURFACE, EGL_NO_SURFACE, + wlr_egl_get_context(egl))) { + wlr_log(WLR_ERROR, "GLES2 RENDERER: Could not make EGL current"); + return NULL; + } + + // get extensions + const char *exts_str = (const char *)glGetString(GL_EXTENSIONS); + if (exts_str == NULL) { + wlr_log(WLR_ERROR, "GLES2 RENDERER: Failed to get GL_EXTENSIONS"); + return NULL; + } + + wlr_log(WLR_INFO, "Creating scenefx GLES2 renderer"); + wlr_log(WLR_INFO, "Using %s", glGetString(GL_VERSION)); + wlr_log(WLR_INFO, "GL vendor: %s", glGetString(GL_VENDOR)); + wlr_log(WLR_INFO, "GL renderer: %s", glGetString(GL_RENDERER)); + wlr_log(WLR_INFO, "Supported GLES2 extensions: %s", exts_str); + + // TODO: the rest of the gl checks + if (check_gl_ext(exts_str, "GL_OES_EGL_image_external")) { + renderer->exts.OES_egl_image_external = true; + load_gl_proc(&renderer->procs.glEGLImageTargetTexture2DOES, + "glEGLImageTargetTexture2DOES"); + } + + // quad fragment shader + if (!link_quad_program(&renderer->shaders.quad)) { + goto error; + } + // fragment shaders + if (!link_tex_program(&renderer->shaders.tex_rgba, SHADER_SOURCE_TEXTURE_RGBA)) { + goto error; + } + if (!link_tex_program(&renderer->shaders.tex_rgbx, SHADER_SOURCE_TEXTURE_RGBX)) { + goto error; + } + if (!link_tex_program(&renderer->shaders.tex_ext, SHADER_SOURCE_TEXTURE_EXTERNAL)) { + goto error; + } + + if (!eglMakeCurrent(wlr_egl_get_display(egl), + EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) { + wlr_log(WLR_ERROR, "GLES2 RENDERER: Could not unset current EGL"); + goto error; + } + + wlr_log(WLR_INFO, "GLES2 RENDERER: Shaders Initialized Successfully"); + return renderer; + +error: + glDeleteProgram(renderer->shaders.quad.program); + glDeleteProgram(renderer->shaders.tex_rgba.program); + glDeleteProgram(renderer->shaders.tex_rgbx.program); + glDeleteProgram(renderer->shaders.tex_ext.program); + + if (!eglMakeCurrent(wlr_egl_get_display(egl), + EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT)) { + wlr_log(WLR_ERROR, "GLES2 RENDERER: Could not unset current EGL"); + } + + // TODO: more freeing? + free(renderer); + + wlr_log(WLR_ERROR, "GLES2 RENDERER: Error Initializing Shaders"); + return NULL; +} + +void fx_renderer_fini(struct fx_renderer *renderer) { + // NO OP +} + +void fx_renderer_begin(struct fx_renderer *renderer, int width, int height) { + glViewport(0, 0, width, height); + + // refresh projection matrix + matrix_projection(renderer->projection, width, height, + WL_OUTPUT_TRANSFORM_FLIPPED_180); + + glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); +} + +void fx_renderer_clear(const float color[static 4]) { + glClearColor(color[0], color[1], color[2], color[3]); + glClearStencil(0); + glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); +} + +void fx_renderer_scissor(struct wlr_box *box) { + if (box) { + glScissor(box->x, box->y, box->width, box->height); + glEnable(GL_SCISSOR_TEST); + } else { + glDisable(GL_SCISSOR_TEST); + } +} + +bool fx_render_subtexture_with_matrix(struct fx_renderer *renderer, struct wlr_texture *wlr_texture, + const struct wlr_fbox *src_box, const struct wlr_box *dst_box, const float matrix[static 9]) { + + assert(wlr_texture_is_gles2(wlr_texture)); + struct wlr_gles2_texture_attribs texture_attrs; + wlr_gles2_texture_get_attribs(wlr_texture, &texture_attrs); + + struct tex_shader *shader = NULL; + + switch (texture_attrs.target) { + case GL_TEXTURE_2D: + if (texture_attrs.has_alpha) { + shader = &renderer->shaders.tex_rgba; + } else { + shader = &renderer->shaders.tex_rgbx; + } + break; + case GL_TEXTURE_EXTERNAL_OES: + shader = &renderer->shaders.tex_ext; + + if (!renderer->exts.OES_egl_image_external) { + wlr_log(WLR_ERROR, "Failed to render texture: " + "GL_TEXTURE_EXTERNAL_OES not supported"); + return false; + } + break; + default: + wlr_log(WLR_ERROR, "Aborting render"); + abort(); + } + + float gl_matrix[9]; + wlr_matrix_multiply(gl_matrix, renderer->projection, matrix); + + // OpenGL ES 2 requires the glUniformMatrix3fv transpose parameter to be set + // to GL_FALSE + wlr_matrix_transpose(gl_matrix, gl_matrix); + + // TODO: accept me as params! + float alpha = 1.0; + int corner_radius = 0; + + // if there's no opacity or rounded corners we don't need to blend + if (!texture_attrs.has_alpha && alpha == 1.0 && !corner_radius) { + glDisable(GL_BLEND); + } else { + glEnable(GL_BLEND); + } + + glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); + + glActiveTexture(GL_TEXTURE0); + glBindTexture(texture_attrs.target, texture_attrs.tex); + + glTexParameteri(texture_attrs.target, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + + glUseProgram(shader->program); + + glUniformMatrix3fv(shader->proj, 1, GL_FALSE, gl_matrix); + glUniform1i(shader->tex, 0); + glUniform2f(shader->size, dst_box->width, dst_box->height); + glUniform2f(shader->position, dst_box->x, dst_box->y); + glUniform1f(shader->alpha, alpha); + glUniform1f(shader->radius, corner_radius); + + const GLfloat x1 = src_box->x / wlr_texture->width; + const GLfloat y1 = src_box->y / wlr_texture->height; + const GLfloat x2 = (src_box->x + src_box->width) / wlr_texture->width; + const GLfloat y2 = (src_box->y + src_box->height) / wlr_texture->height; + const GLfloat texcoord[] = { + x2, y1, // top right + x1, y1, // top left + x2, y2, // bottom right + x1, y2, // bottom left + }; + + glVertexAttribPointer(shader->pos_attrib, 2, GL_FLOAT, GL_FALSE, 0, verts); + glVertexAttribPointer(shader->tex_attrib, 2, GL_FLOAT, GL_FALSE, 0, texcoord); + + glEnableVertexAttribArray(shader->pos_attrib); + glEnableVertexAttribArray(shader->tex_attrib); + + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + + glDisableVertexAttribArray(shader->pos_attrib); + glDisableVertexAttribArray(shader->tex_attrib); + + glBindTexture(texture_attrs.target, 0); + + return true; +} + +void fx_render_rect(struct fx_renderer *renderer, const struct wlr_box *box, + const float color[static 4], const float projection[static 9]) { + if (box->width == 0 || box->height == 0) { + return; + } + assert(box->width > 0 && box->height > 0); + float matrix[9]; + wlr_matrix_project_box(matrix, box, WL_OUTPUT_TRANSFORM_NORMAL, 0, projection); + + float gl_matrix[9]; + wlr_matrix_multiply(gl_matrix, renderer->projection, matrix); + + // TODO: investigate why matrix is flipped prior to this cmd + // wlr_matrix_multiply(gl_matrix, flip_180, gl_matrix); + + wlr_matrix_transpose(gl_matrix, gl_matrix); + + if (color[3] == 1.0) { + glDisable(GL_BLEND); + } else { + glEnable(GL_BLEND); + } + + struct quad_shader shader = renderer->shaders.quad; + glUseProgram(shader.program); + + glUniformMatrix3fv(shader.proj, 1, GL_FALSE, gl_matrix); + glUniform4f(shader.color, color[0], color[1], color[2], color[3]); + + glVertexAttribPointer(shader.pos_attrib, 2, GL_FLOAT, GL_FALSE, + 0, verts); + + glEnableVertexAttribArray(shader.pos_attrib); + + glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); + + glDisableVertexAttribArray(shader.pos_attrib); +} diff --git a/render/fx_renderer/gles2/meson.build b/render/fx_renderer/gles2/meson.build new file mode 100644 index 0000000..70cde2c --- /dev/null +++ b/render/fx_renderer/gles2/meson.build @@ -0,0 +1,2 @@ +subdir('shaders') + diff --git a/render/fx_renderer/gles2/shaders/common.vert b/render/fx_renderer/gles2/shaders/common.vert new file mode 100644 index 0000000..811e0f2 --- /dev/null +++ b/render/fx_renderer/gles2/shaders/common.vert @@ -0,0 +1,12 @@ +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; +} diff --git a/render/fx_renderer/gles2/shaders/embed.sh b/render/fx_renderer/gles2/shaders/embed.sh new file mode 100644 index 0000000..47f0789 --- /dev/null +++ b/render/fx_renderer/gles2/shaders/embed.sh @@ -0,0 +1,11 @@ +#!/bin/sh -eu + +var=${1:-data} +hex="$(od -A n -t x1 -v)" + +echo "static const char $var[] = {" +for byte in $hex; do + echo " 0x$byte," +done +echo " 0x00," +echo "};" diff --git a/render/fx_renderer/gles2/shaders/meson.build b/render/fx_renderer/gles2/shaders/meson.build new file mode 100644 index 0000000..ee030a2 --- /dev/null +++ b/render/fx_renderer/gles2/shaders/meson.build @@ -0,0 +1,21 @@ +embed = find_program('./embed.sh', native: true) + +shaders = [ + 'common.vert', + 'quad.frag', + 'tex.frag', +] + +foreach name : shaders + output = name.underscorify() + '_src.h' + var = name.underscorify() + '_src' + wlr_files += custom_target( + output, + command: [embed, var], + input: name, + output: output, + feed: true, + capture: true, + ) +endforeach + diff --git a/render/fx_renderer/gles2/shaders/quad.frag b/render/fx_renderer/gles2/shaders/quad.frag new file mode 100644 index 0000000..7c76327 --- /dev/null +++ b/render/fx_renderer/gles2/shaders/quad.frag @@ -0,0 +1,7 @@ +precision mediump float; +varying vec4 v_color; +varying vec2 v_texcoord; + +void main() { + gl_FragColor = v_color; +} diff --git a/render/fx_renderer/gles2/shaders/tex.frag b/render/fx_renderer/gles2/shaders/tex.frag new file mode 100644 index 0000000..bd3c596 --- /dev/null +++ b/render/fx_renderer/gles2/shaders/tex.frag @@ -0,0 +1,44 @@ +#define SOURCE_TEXTURE_RGBA 1 +#define SOURCE_TEXTURE_RGBX 2 +#define SOURCE_TEXTURE_EXTERNAL 3 + +#if !defined(SOURCE) +#error "Missing shader preamble" +#endif + +#if SOURCE == SOURCE_TEXTURE_EXTERNAL +#extension GL_OES_EGL_image_external : require +#endif + +precision mediump float; + +varying vec2 v_texcoord; + +#if SOURCE == SOURCE_TEXTURE_EXTERNAL +uniform samplerExternalOES tex; +#elif SOURCE == SOURCE_TEXTURE_RGBA || SOURCE == SOURCE_TEXTURE_RGBX +uniform sampler2D tex; +#endif + +uniform float alpha; +uniform vec2 size; +uniform vec2 position; +uniform float radius; + +vec4 sample_texture() { +#if SOURCE == SOURCE_TEXTURE_RGBA || SOURCE == SOURCE_TEXTURE_EXTERNAL + return texture2D(tex, v_texcoord); +#elif SOURCE == SOURCE_TEXTURE_RGBX + return vec4(texture2D(tex, v_texcoord).rgb, 1.0); +#endif +} + +void main() { + gl_FragColor = sample_texture() * alpha; + vec2 corner_distance = min(gl_FragCoord.xy - position, size + position - gl_FragCoord.xy); + if (max(corner_distance.x, corner_distance.y) < radius) { + float d = radius - distance(corner_distance, vec2(radius)); + float smooth = smoothstep(-1.0, 0.5, d); + gl_FragColor = mix(vec4(0), gl_FragColor, smooth); + } +} diff --git a/render/fx_renderer/matrix.c b/render/fx_renderer/matrix.c new file mode 100644 index 0000000..8f0fe15 --- /dev/null +++ b/render/fx_renderer/matrix.c @@ -0,0 +1,70 @@ +#include <math.h> +#include <string.h> +#include <wlr/types/wlr_output.h> + +#include "render/fx_renderer/matrix.h" + +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 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; +} diff --git a/render/fx_renderer/meson.build b/render/fx_renderer/meson.build new file mode 100644 index 0000000..951b10d --- /dev/null +++ b/render/fx_renderer/meson.build @@ -0,0 +1,22 @@ +renderers = get_option('renderers') +if 'auto' in renderers and get_option('auto_features').enabled() + renderers = ['gles2', 'vulkan'] +elif 'auto' in renderers and get_option('auto_features').disabled() + renderers = [] +endif + +wlr_files += files( + 'matrix.c', + 'fx_renderer.c', +) + +if 'gles2' in renderers or 'auto' in renderers + egl = dependency('egl', required: 'gles2' in renderers) + gbm = dependency('gbm', required: 'gles2' in renderers) + if egl.found() and gbm.found() + wlr_deps += [egl, gbm] + internal_features += { 'egl': true } + endif + subdir('gles2') +endif + diff --git a/render/meson.build b/render/meson.build index 7916da6..4dd5c6a 100644 --- a/render/meson.build +++ b/render/meson.build @@ -1,3 +1,5 @@ wlr_files += files( 'pixel_format.c', ) + +subdir('fx_renderer') diff --git a/types/scene/wlr_scene.c b/types/scene/wlr_scene.c index eb1ce3e..265f38e 100644 --- a/types/scene/wlr_scene.c +++ b/types/scene/wlr_scene.c @@ -3,7 +3,7 @@ #include <stdlib.h> #include <string.h> #include <wlr/backend.h> -#include <wlr/render/wlr_renderer.h> +#include <wlr/render/gles2.h> #include <wlr/types/wlr_compositor.h> #include <wlr/types/wlr_damage_ring.h> #include <wlr/types/wlr_matrix.h> @@ -11,6 +11,7 @@ #include <wlr/types/wlr_scene.h> #include <wlr/util/log.h> #include <wlr/util/region.h> +#include "render/fx_renderer/fx_renderer.h" #include "types/wlr_buffer.h" #include "types/wlr_scene.h" #include "util/array.h" @@ -1008,9 +1009,6 @@ struct wlr_scene_node *wlr_scene_node_at(struct wlr_scene_node *node, } static void scissor_output(struct wlr_output *output, pixman_box32_t *rect) { - struct wlr_renderer *renderer = output->renderer; - assert(renderer); - struct wlr_box box = { .x = rect->x1, .y = rect->y1, @@ -1025,29 +1023,27 @@ static void scissor_output(struct wlr_output *output, pixman_box32_t *rect) { wlr_output_transform_invert(output->transform); wlr_box_transform(&box, &box, transform, ow, oh); - wlr_renderer_scissor(renderer, &box); + fx_renderer_scissor(&box); } -static void render_rect(struct wlr_output *output, +static void render_rect(struct fx_renderer *fx_renderer, struct wlr_output *output, pixman_region32_t *damage, const float color[static 4], const struct wlr_box *box, const float matrix[static 9]) { - struct wlr_renderer *renderer = output->renderer; - assert(renderer); + assert(fx_renderer); 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_rect(renderer, box, color, matrix); + fx_render_rect(fx_renderer, box, color, matrix); } } -static void render_texture(struct wlr_output *output, +static void render_texture(struct fx_renderer *fx_renderer, struct wlr_output *output, pixman_region32_t *damage, struct wlr_texture *texture, const struct wlr_fbox *src_box, const struct wlr_box *dst_box, const float matrix[static 9]) { - struct wlr_renderer *renderer = output->renderer; - assert(renderer); + assert(fx_renderer); struct wlr_fbox default_src_box = {0}; if (wlr_fbox_empty(src_box)) { @@ -1056,15 +1052,24 @@ static void render_texture(struct wlr_output *output, src_box = &default_src_box; } + // ensure the box is updated as per the output orientation + struct wlr_box transformed_box; + int width, height; + wlr_output_transformed_resolution(output, &width, &height); + wlr_box_transform(&transformed_box, dst_box, + wlr_output_transform_invert(output->transform), width, height); + 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_subtexture_with_matrix(renderer, texture, src_box, matrix, 1.0); + + fx_render_subtexture_with_matrix(fx_renderer, texture, src_box, + &transformed_box, matrix); } } -static void scene_node_render(struct wlr_scene_node *node, +static void scene_node_render(struct fx_renderer *fx_renderer, struct wlr_scene_node *node, struct wlr_scene_output *scene_output, pixman_region32_t *damage) { int x, y; wlr_scene_node_coords(node, &x, &y); @@ -1101,7 +1106,7 @@ static void scene_node_render(struct wlr_scene_node *node, case WLR_SCENE_NODE_RECT:; struct wlr_scene_rect *scene_rect = scene_rect_from_node(node); - render_rect(output, &render_region, scene_rect->color, &dst_box, + render_rect(fx_renderer, output, &render_region, scene_rect->color, &dst_box, output->transform_matrix); break; case WLR_SCENE_NODE_BUFFER:; @@ -1110,15 +1115,12 @@ static void scene_node_render(struct wlr_scene_node *node, struct wlr_renderer *renderer = output->renderer; texture = scene_buffer_get_texture(scene_buffer, renderer); - if (texture == NULL) { - break; - } transform = wlr_output_transform_invert(scene_buffer->transform); wlr_matrix_project_box(matrix, &dst_box, transform, 0.0, output->transform_matrix); - render_texture(output, &render_region, texture, &scene_buffer->src_box, + render_texture(fx_renderer, output, &render_region, texture, &scene_buffer->src_box, &dst_box, matrix); wl_signal_emit_mutable(&scene_buffer->events.output_present, scene_output); @@ -1156,6 +1158,7 @@ static const struct wlr_addon_interface output_addon_impl = { .destroy = scene_output_handle_destroy, }; + static void scene_node_output_update(struct wlr_scene_node *node, struct wl_list *outputs, struct wlr_scene_output *ignore) { if (node->type == WLR_SCENE_NODE_TREE) { @@ -1225,6 +1228,10 @@ struct wlr_scene_output *wlr_scene_output_create(struct wlr_scene *scene, scene_output->scene = scene; wlr_addon_init(&scene_output->addon, &output->addons, scene, &output_addon_impl); + // Init FX Renderer + struct wlr_egl *egl = wlr_gles2_renderer_get_egl(output->renderer); + fx_renderer_init_addon(egl, &output->addons, scene); + wlr_damage_ring_init(&scene_output->damage_ring); wl_list_init(&scene_output->damage_highlight_regions); @@ -1443,7 +1450,9 @@ bool wlr_scene_output_commit(struct wlr_scene_output *scene_output) { enum wlr_scene_debug_damage_option debug_damage = scene_output->scene->debug_damage_option; - struct wlr_renderer *renderer = output->renderer; + // Find the fx_renderer addon + struct fx_renderer *renderer = + fx_renderer_addon_find(&output->addons, scene_output->scene); assert(renderer != NULL); struct render_list_constructor_data list_con = { @@ -1546,7 +1555,7 @@ bool wlr_scene_output_commit(struct wlr_scene_output *scene_output) { return true; } - wlr_renderer_begin(renderer, output->width, output->height); + fx_renderer_begin(renderer, output->width, output->height); pixman_region32_t background; pixman_region32_init(&background); @@ -1592,16 +1601,16 @@ bool wlr_scene_output_commit(struct wlr_scene_output *scene_output) { pixman_box32_t *rects = pixman_region32_rectangles(&background, &nrects); for (int i = 0; i < nrects; ++i) { scissor_output(output, &rects[i]); - wlr_renderer_clear(renderer, (float[4]){ 0.0, 0.0, 0.0, 1.0 }); + fx_renderer_clear((float[4]){ 0.0, 0.0, 0.0, 1.0 }); } pixman_region32_fini(&background); for (int i = list_len - 1; i >= 0; i--) { struct wlr_scene_node *node = list_data[i]; - scene_node_render(node, scene_output, &damage); + scene_node_render(renderer, node, scene_output, &damage); } - wlr_renderer_scissor(renderer, NULL); + fx_renderer_scissor(NULL); if (debug_damage == WLR_SCENE_DEBUG_DAMAGE_HIGHLIGHT) { struct highlight_region *damage; @@ -1622,14 +1631,16 @@ bool wlr_scene_output_commit(struct wlr_scene_output *scene_output) { }; float color[4] = { alpha * .5, 0.0, 0.0, alpha * .5 }; - wlr_render_rect(renderer, &box, color, output->transform_matrix); + fx_render_rect(renderer, &box, color, output->transform_matrix); } } } + // Draw the software cursors + wlr_renderer_begin(output->renderer, output->width, output->height); wlr_output_render_software_cursors(output, &damage); + wlr_renderer_end(output->renderer); - wlr_renderer_end(renderer); pixman_region32_fini(&damage); int tr_width, tr_height; |