[client] egl: cleanup/refactor of cursor texture code

This commit is contained in:
Geoffrey McRae 2020-11-10 20:42:14 +11:00
parent 12da2fc0b7
commit cd4dfd7252

View File

@ -34,6 +34,14 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#include "cursor_rgb.frag.h" #include "cursor_rgb.frag.h"
#include "cursor_mono.frag.h" #include "cursor_mono.frag.h"
struct CursorTex
{
struct EGL_Texture * texture;
struct EGL_Shader * shader;
GLuint uMousePos;
GLuint uCBMode;
};
struct EGL_Cursor struct EGL_Cursor
{ {
LG_Lock lock; LG_Lock lock;
@ -50,23 +58,46 @@ struct EGL_Cursor
float x, y, w, h; float x, y, w, h;
int cbMode; int cbMode;
// textures struct CursorTex norm;
struct EGL_Texture * texture; struct CursorTex mono;
struct EGL_Texture * textureMono;
// shaders
struct EGL_Shader * shader;
struct EGL_Shader * shaderMono;
// uniforms
GLuint uMousePos;
GLuint uMousePosMono;
GLuint uCBMode;
// model
struct EGL_Model * model; struct EGL_Model * model;
}; };
static bool egl_cursor_tex_init(struct CursorTex * t,
const char * vertex_code , size_t vertex_size,
const char * fragment_code, size_t fragment_size)
{
if (!egl_texture_init(&t->texture, NULL))
{
DEBUG_ERROR("Failed to initialize the cursor texture");
return false;
}
if (!egl_shader_init(&t->shader))
{
DEBUG_ERROR("Failed to initialize the cursor shader");
return false;
}
if (!egl_shader_compile(t->shader,
vertex_code, vertex_size, fragment_code, fragment_size))
{
DEBUG_ERROR("Failed to compile the cursor shader");
return false;
}
t->uMousePos = egl_shader_get_uniform_location(t->shader, "mouse" );
t->uCBMode = egl_shader_get_uniform_location(t->shader, "cbMode");
return true;
}
static void egl_cursor_tex_free(struct CursorTex * t)
{
egl_texture_free(&t->texture);
egl_shader_free (&t->shader );
};
bool egl_cursor_init(EGL_Cursor ** cursor) bool egl_cursor_init(EGL_Cursor ** cursor)
{ {
*cursor = (EGL_Cursor *)malloc(sizeof(EGL_Cursor)); *cursor = (EGL_Cursor *)malloc(sizeof(EGL_Cursor));
@ -79,52 +110,15 @@ bool egl_cursor_init(EGL_Cursor ** cursor)
memset(*cursor, 0, sizeof(EGL_Cursor)); memset(*cursor, 0, sizeof(EGL_Cursor));
LG_LOCK_INIT((*cursor)->lock); LG_LOCK_INIT((*cursor)->lock);
if (!egl_texture_init(&(*cursor)->texture, NULL)) if (!egl_cursor_tex_init(&(*cursor)->norm,
{
DEBUG_ERROR("Failed to initialize the cursor texture");
return false;
}
if (!egl_texture_init(&(*cursor)->textureMono, NULL))
{
DEBUG_ERROR("Failed to initialize the cursor mono texture");
return false;
}
if (!egl_shader_init(&(*cursor)->shader))
{
DEBUG_ERROR("Failed to initialize the cursor shader");
return false;
}
if (!egl_shader_init(&(*cursor)->shaderMono))
{
DEBUG_ERROR("Failed to initialize the cursor mono shader");
return false;
}
if (!egl_shader_compile(
(*cursor)->shader,
b_shader_cursor_vert , b_shader_cursor_vert_size, b_shader_cursor_vert , b_shader_cursor_vert_size,
b_shader_cursor_rgb_frag, b_shader_cursor_rgb_frag_size)) b_shader_cursor_rgb_frag, b_shader_cursor_rgb_frag_size))
{
DEBUG_ERROR("Failed to compile the cursor shader");
return false; return false;
}
if (!egl_shader_compile( if (!egl_cursor_tex_init(&(*cursor)->mono,
(*cursor)->shaderMono,
b_shader_cursor_vert , b_shader_cursor_vert_size, b_shader_cursor_vert , b_shader_cursor_vert_size,
b_shader_cursor_mono_frag, b_shader_cursor_mono_frag_size)) b_shader_cursor_mono_frag, b_shader_cursor_mono_frag_size))
{
DEBUG_ERROR("Failed to compile the cursor mono shader");
return false; return false;
}
(*cursor)->uMousePos = egl_shader_get_uniform_location((*cursor)->shader , "mouse" );
(*cursor)->uCBMode = egl_shader_get_uniform_location((*cursor)->shader , "cbMode");
(*cursor)->uMousePosMono = egl_shader_get_uniform_location((*cursor)->shaderMono, "mouse" );
if (!egl_model_init(&(*cursor)->model)) if (!egl_model_init(&(*cursor)->model))
{ {
@ -148,10 +142,8 @@ void egl_cursor_free(EGL_Cursor ** cursor)
if ((*cursor)->data) if ((*cursor)->data)
free((*cursor)->data); free((*cursor)->data);
egl_texture_free(&(*cursor)->texture ); egl_cursor_tex_free(&(*cursor)->norm);
egl_texture_free(&(*cursor)->textureMono); egl_cursor_tex_free(&(*cursor)->mono);
egl_shader_free (&(*cursor)->shader );
egl_shader_free (&(*cursor)->shaderMono );
egl_model_free(&(*cursor)->model); egl_model_free(&(*cursor)->model);
free(*cursor); free(*cursor);
@ -222,9 +214,9 @@ void egl_cursor_render(EGL_Cursor * cursor)
case LG_CURSOR_COLOR: case LG_CURSOR_COLOR:
{ {
egl_texture_setup(cursor->texture, EGL_PF_BGRA, cursor->width, cursor->height, cursor->stride, false, false); egl_texture_setup(cursor->norm.texture, EGL_PF_BGRA, cursor->width, cursor->height, cursor->stride, false, false);
egl_texture_update(cursor->texture, data); egl_texture_update(cursor->norm.texture, data);
egl_model_set_texture(cursor->model, cursor->texture); egl_model_set_texture(cursor->model, cursor->norm.texture);
break; break;
} }
@ -246,10 +238,10 @@ void egl_cursor_render(EGL_Cursor * cursor)
xor[y * cursor->width + x] = xorMask; xor[y * cursor->width + x] = xorMask;
} }
egl_texture_setup (cursor->texture , EGL_PF_BGRA, cursor->width, cursor->height, cursor->width * 4, false, false); egl_texture_setup (cursor->norm.texture , EGL_PF_BGRA, cursor->width, cursor->height, cursor->width * 4, false, false);
egl_texture_setup (cursor->textureMono, EGL_PF_BGRA, cursor->width, cursor->height, cursor->width * 4, false, false); egl_texture_setup (cursor->mono.texture, EGL_PF_BGRA, cursor->width, cursor->height, cursor->width * 4, false, false);
egl_texture_update(cursor->texture , (uint8_t *)and); egl_texture_update(cursor->norm.texture , (uint8_t *)and);
egl_texture_update(cursor->textureMono, (uint8_t *)xor); egl_texture_update(cursor->mono.texture, (uint8_t *)xor);
break; break;
} }
} }
@ -261,26 +253,26 @@ void egl_cursor_render(EGL_Cursor * cursor)
{ {
case LG_CURSOR_MONOCHROME: case LG_CURSOR_MONOCHROME:
{ {
egl_shader_use(cursor->shader); egl_shader_use(cursor->norm.shader);
glUniform4f(cursor->uMousePos, cursor->x, cursor->y, cursor->w, cursor->h / 2); glUniform4f(cursor->norm.uMousePos, cursor->x, cursor->y, cursor->w, cursor->h / 2);
glUniform1i(cursor->uCBMode , cursor->cbMode); glUniform1i(cursor->norm.uCBMode , cursor->cbMode);
glBlendFunc(GL_ZERO, GL_SRC_COLOR); glBlendFunc(GL_ZERO, GL_SRC_COLOR);
egl_model_set_texture(cursor->model, cursor->texture); egl_model_set_texture(cursor->model, cursor->norm.texture);
egl_model_render(cursor->model); egl_model_render(cursor->model);
egl_shader_use(cursor->shaderMono); egl_shader_use(cursor->mono.shader);
glUniform4f(cursor->uMousePosMono, cursor->x, cursor->y, cursor->w, cursor->h / 2); glUniform4f(cursor->mono.uMousePos, cursor->x, cursor->y, cursor->w, cursor->h / 2);
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO); glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
egl_model_set_texture(cursor->model, cursor->textureMono); egl_model_set_texture(cursor->model, cursor->mono.texture);
egl_model_render(cursor->model); egl_model_render(cursor->model);
break; break;
} }
case LG_CURSOR_COLOR: case LG_CURSOR_COLOR:
{ {
egl_shader_use(cursor->shader); egl_shader_use(cursor->norm.shader);
glUniform4f(cursor->uMousePos, cursor->x, cursor->y, cursor->w, cursor->h); glUniform4f(cursor->norm.uMousePos, cursor->x, cursor->y, cursor->w, cursor->h);
glUniform1i(cursor->uCBMode , cursor->cbMode); glUniform1i(cursor->norm.uCBMode , cursor->cbMode);
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
egl_model_render(cursor->model); egl_model_render(cursor->model);
break; break;
@ -288,8 +280,8 @@ void egl_cursor_render(EGL_Cursor * cursor)
case LG_CURSOR_MASKED_COLOR: case LG_CURSOR_MASKED_COLOR:
{ {
egl_shader_use(cursor->shaderMono); egl_shader_use(cursor->mono.shader);
glUniform4f(cursor->uMousePos, cursor->x, cursor->y, cursor->w, cursor->h); glUniform4f(cursor->mono.uMousePos, cursor->x, cursor->y, cursor->w, cursor->h);
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO); glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO);
egl_model_render(cursor->model); egl_model_render(cursor->model);
break; break;