[client] egl: implement frame damage display

This commit is contained in:
Quantum 2021-07-15 06:04:25 -04:00 committed by Geoffrey McRae
parent 12cb3e512f
commit 2b3f31700b
6 changed files with 287 additions and 7 deletions

View File

@ -19,6 +19,8 @@ make_object(
shader/cursor.vert
shader/cursor_rgb.frag
shader/cursor_mono.frag
shader/damage.vert
shader/damage.frag
shader/fps.vert
shader/fps.frag
shader/fps_bg.frag
@ -52,6 +54,7 @@ add_library(renderer_EGL STATIC
draw.c
splash.c
alert.c
damage.c
${EGL_SHADER_OBJS}
"${EGL_SHADER_INCS}/desktop_rgb.def.h"
${PROJECT_TOP}/repos/cimgui/imgui/backends/imgui_impl_opengl3.cpp

View File

@ -0,0 +1,210 @@
/**
* Looking Glass
* Copyright (C) 2017-2021 The Looking Glass Authors
* https://looking-glass.io
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "damage.h"
#include "common/debug.h"
#include "common/KVMFR.h"
#include "common/locking.h"
#include "app.h"
#include "shader.h"
#include <stdlib.h>
#include <string.h>
// these headers are auto generated by cmake
#include "damage.vert.h"
#include "damage.frag.h"
struct EGL_Damage
{
EGL_Shader * shader;
GLfloat transform[6];
GLuint buffers[2];
GLuint vao;
GLfloat * vertices;
bool show;
KeybindHandle toggleHandle;
int width , height;
float translateX, translateY;
float scaleX , scaleY;
// uniforms
GLint uTransform;
};
void egl_damage_show_toggle(int key, void * opaque)
{
EGL_Damage * damage = opaque;
damage->show ^= true;
}
bool egl_damage_init(EGL_Damage ** damage)
{
*damage = (EGL_Damage *)malloc(sizeof(EGL_Damage));
if (!*damage)
{
DEBUG_ERROR("Failed to malloc EGL_Alert");
return false;
}
memset(*damage, 0, sizeof(EGL_Damage));
if (!egl_shader_init(&(*damage)->shader))
{
DEBUG_ERROR("Failed to initialize the damage shader");
return false;
}
if (!egl_shader_compile((*damage)->shader,
b_shader_damage_vert, b_shader_damage_vert_size,
b_shader_damage_frag, b_shader_damage_frag_size))
{
DEBUG_ERROR("Failed to compile the damage shader");
return false;
}
glGenVertexArrays(1, &(*damage)->vao);
glBindVertexArray((*damage)->vao);
glGenBuffers(2, (*damage)->buffers);
glBindBuffer(GL_ARRAY_BUFFER, (*damage)->buffers[0]);
glBufferData(GL_ARRAY_BUFFER, KVMFR_MAX_DAMAGE_RECTS * 8 * sizeof(GLfloat), NULL, GL_STREAM_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);
(*damage)->vertices = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
glBindBuffer(GL_ARRAY_BUFFER, 0);
GLushort indices[KVMFR_MAX_DAMAGE_RECTS * 6];
for (int i = 0; i < KVMFR_MAX_DAMAGE_RECTS; ++i)
{
indices[6 * i + 0] = 4 * i + 0;
indices[6 * i + 1] = 4 * i + 1;
indices[6 * i + 2] = 4 * i + 2;
indices[6 * i + 3] = 4 * i + 0;
indices[6 * i + 4] = 4 * i + 2;
indices[6 * i + 5] = 4 * i + 3;
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, (*damage)->buffers[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof indices, indices, GL_STATIC_DRAW);
glBindVertexArray(0);
(*damage)->uTransform = egl_shader_get_uniform_location((*damage)->shader, "transform");
(*damage)->toggleHandle = app_registerKeybind(KEY_A, egl_damage_show_toggle, *damage, "Toggle damage display");
return true;
}
void egl_damage_free(EGL_Damage ** damage)
{
if (!*damage)
return;
app_releaseKeybind(&(*damage)->toggleHandle);
glDeleteVertexArrays(1, &(*damage)->vao);
glBindBuffer(GL_ARRAY_BUFFER, (*damage)->buffers[0]);
glUnmapBuffer(GL_ARRAY_BUFFER);
glDeleteBuffers(2, (*damage)->buffers);
egl_shader_free(&(*damage)->shader);
free(*damage);
*damage = NULL;
}
static void update_matrix(EGL_Damage * damage)
{
damage->transform[0] = 2.0f * damage->scaleX / damage->width;
damage->transform[1] = 0.0f;
damage->transform[2] = 0.0f;
damage->transform[3] = -2.0f * damage->scaleY / damage->height;
damage->transform[4] = damage->translateX - damage->scaleX;
damage->transform[5] = damage->translateY + damage->scaleY;
}
void egl_damage_setup(EGL_Damage * damage, int width, int height)
{
damage->width = width;
damage->height = height;
update_matrix(damage);
}
void egl_damage_resize(EGL_Damage * damage, float translateX, float translateY,
float scaleX, float scaleY)
{
damage->translateX = translateX;
damage->translateY = translateY;
damage->scaleX = scaleX;
damage->scaleY = scaleY;
update_matrix(damage);
}
inline static void rectToVertices(GLfloat * vertex, const FrameDamageRect * rect)
{
vertex[0] = rect->x;
vertex[1] = rect->y;
vertex[2] = rect->x + rect->width;
vertex[3] = rect->y;
vertex[4] = rect->x + rect->width;
vertex[5] = rect->y + rect->height;
vertex[6] = rect->x;
vertex[7] = rect->y + rect->height;
}
bool egl_damage_render(EGL_Damage * damage, const struct DesktopDamage * data)
{
if (!damage->show || !data)
return false;
int count = data->count;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
egl_shader_use(damage->shader);
glUniformMatrix3x2fv(damage->uTransform, 1, GL_FALSE, damage->transform);
if (count == 0)
{
FrameDamageRect rect = {
.x = 0, .y = 0, .width = damage->width, .height = damage->height,
};
rectToVertices(damage->vertices, &rect);
count = 1;
}
else
{
for (int i = 0; i < count; ++i)
rectToVertices(damage->vertices + i * 8, data->rects + i);
}
glBindVertexArray(damage->vao);
glDrawElements(GL_TRIANGLES, 6 * count, GL_UNSIGNED_SHORT, NULL);
glBindVertexArray(0);
glDisable(GL_BLEND);
return true;
}

View File

@ -0,0 +1,40 @@
/**
* Looking Glass
* Copyright (C) 2017-2021 The Looking Glass Authors
* https://looking-glass.io
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#pragma once
#include <stdbool.h>
#include "common/KVMFR.h"
struct DesktopDamage
{
int count;
FrameDamageRect rects[KVMFR_MAX_DAMAGE_RECTS];
};
typedef struct EGL_Damage EGL_Damage;
bool egl_damage_init(EGL_Damage ** damage);
void egl_damage_free(EGL_Damage ** damage);
void egl_damage_setup(EGL_Damage * damage, int width, int height);
void egl_damage_resize(EGL_Damage * damage, float translateX, float translateY,
float scaleX, float scaleY);
bool egl_damage_render(EGL_Damage * damage, const struct DesktopDamage * data);

View File

@ -44,6 +44,7 @@
#include "egl_dynprocs.h"
#include "model.h"
#include "shader.h"
#include "damage.h"
#include "desktop.h"
#include "cursor.h"
#include "fps.h"
@ -60,12 +61,6 @@ struct Options
bool doubleBuffer;
};
struct DesktopDamage
{
int count;
FrameDamageRect rects[KVMFR_MAX_DAMAGE_RECTS];
};
struct Inst
{
bool dmaSupport;
@ -84,6 +79,7 @@ struct Inst
EGL_Splash * splash; // the splash screen
EGL_Alert * alert; // the alert display
EGL_Help * help; // the help display
EGL_Damage * damage; // the damage display
bool imgui; // if imgui was initialized
LG_RendererFormat format;
@ -96,6 +92,7 @@ struct Inst
uint64_t alertTimeout;
bool useCloseFlag;
bool closeFlag;
bool showDamage;
int width, height;
float uiScale;
@ -314,7 +311,8 @@ void egl_deinitialize(void * opaque)
egl_fps_free (&this->fps );
egl_splash_free (&this->splash);
egl_alert_free (&this->alert );
egl_help_free (&this->help);
egl_help_free (&this->help );
egl_damage_free (&this->damage);
LG_LOCK_FREE(this->lock);
@ -507,6 +505,8 @@ void egl_on_resize(void * opaque, const int width, const int height, const doubl
}
damage->count = 0;
free(atomic_exchange(&this->desktopDamage, damage));
egl_damage_resize(this->damage, this->translateX, this->translateY, this->scaleX, this->scaleY);
}
bool egl_on_mouse_shape(void * opaque, const LG_RendererCursor cursor,
@ -566,6 +566,8 @@ bool egl_on_frame_format(void * opaque, const LG_RendererFormat format, bool use
}
egl_update_scale_type(this);
egl_damage_setup(this->damage, format.width, format.height);
return egl_desktop_setup(this->desktop, format, useDMA);
}
@ -807,6 +809,12 @@ bool egl_render_startup(void * opaque)
return false;
}
if (!egl_damage_init(&this->damage))
{
DEBUG_ERROR("Failed to initialize the damage display");
return false;
}
// glew is needed for imgui
if (!glewInit())
{
@ -913,6 +921,7 @@ bool egl_render(void * opaque, LG_RendererRotate rotate)
hasOverlay |= egl_fps_render(this->fps, this->screenScaleX, this->screenScaleY);
hasOverlay |= egl_help_render(this->help, this->screenScaleX, this->screenScaleY);
hasOverlay |= egl_damage_render(this->damage, desktopDamage);
if (app_renderImGui())
{

View File

@ -0,0 +1,8 @@
#version 300 es
out highp vec4 color;
void main()
{
color = vec4(1.0, 1.0, 0.0, 0.5);
}

View File

@ -0,0 +1,10 @@
#version 300 es
layout(location = 0) in vec2 vertex;
uniform mat3x2 transform;
void main()
{
gl_Position = vec4(transform * vec3(vertex, 1.0), 0.0, 1.0);
}