From cd4dfd725222355e380bd2ee5660e85b390a680d Mon Sep 17 00:00:00 2001 From: Geoffrey McRae Date: Tue, 10 Nov 2020 20:42:14 +1100 Subject: [PATCH] [client] egl: cleanup/refactor of cursor texture code --- client/renderers/EGL/cursor.c | 150 ++++++++++++++++------------------ 1 file changed, 71 insertions(+), 79 deletions(-) diff --git a/client/renderers/EGL/cursor.c b/client/renderers/EGL/cursor.c index 7410b507..a7838455 100644 --- a/client/renderers/EGL/cursor.c +++ b/client/renderers/EGL/cursor.c @@ -34,6 +34,14 @@ Place, Suite 330, Boston, MA 02111-1307 USA #include "cursor_rgb.frag.h" #include "cursor_mono.frag.h" +struct CursorTex +{ + struct EGL_Texture * texture; + struct EGL_Shader * shader; + GLuint uMousePos; + GLuint uCBMode; +}; + struct EGL_Cursor { LG_Lock lock; @@ -50,21 +58,44 @@ struct EGL_Cursor float x, y, w, h; int cbMode; - // textures - struct EGL_Texture * texture; - struct EGL_Texture * textureMono; + struct CursorTex norm; + struct CursorTex mono; + struct EGL_Model * model; +}; - // shaders - struct EGL_Shader * shader; - struct EGL_Shader * shaderMono; +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; + } - // uniforms - GLuint uMousePos; - GLuint uMousePosMono; - GLuint uCBMode; + if (!egl_shader_init(&t->shader)) + { + DEBUG_ERROR("Failed to initialize the cursor shader"); + return false; + } - // model - struct EGL_Model * model; + 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) @@ -79,52 +110,15 @@ bool egl_cursor_init(EGL_Cursor ** cursor) memset(*cursor, 0, sizeof(EGL_Cursor)); LG_LOCK_INIT((*cursor)->lock); - if (!egl_texture_init(&(*cursor)->texture, NULL)) - { - DEBUG_ERROR("Failed to initialize the cursor texture"); + if (!egl_cursor_tex_init(&(*cursor)->norm, + b_shader_cursor_vert , b_shader_cursor_vert_size, + b_shader_cursor_rgb_frag, b_shader_cursor_rgb_frag_size)) return false; - } - if (!egl_texture_init(&(*cursor)->textureMono, NULL)) - { - DEBUG_ERROR("Failed to initialize the cursor mono texture"); + if (!egl_cursor_tex_init(&(*cursor)->mono, + b_shader_cursor_vert , b_shader_cursor_vert_size, + b_shader_cursor_mono_frag, b_shader_cursor_mono_frag_size)) 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_rgb_frag, b_shader_cursor_rgb_frag_size)) - { - DEBUG_ERROR("Failed to compile the cursor shader"); - return false; - } - - if (!egl_shader_compile( - (*cursor)->shaderMono, - b_shader_cursor_vert , b_shader_cursor_vert_size, - b_shader_cursor_mono_frag, b_shader_cursor_mono_frag_size)) - { - DEBUG_ERROR("Failed to compile the cursor mono shader"); - 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)) { @@ -148,11 +142,9 @@ void egl_cursor_free(EGL_Cursor ** cursor) if ((*cursor)->data) free((*cursor)->data); - egl_texture_free(&(*cursor)->texture ); - egl_texture_free(&(*cursor)->textureMono); - egl_shader_free (&(*cursor)->shader ); - egl_shader_free (&(*cursor)->shaderMono ); - egl_model_free (&(*cursor)->model ); + egl_cursor_tex_free(&(*cursor)->norm); + egl_cursor_tex_free(&(*cursor)->mono); + egl_model_free(&(*cursor)->model); free(*cursor); *cursor = NULL; @@ -222,9 +214,9 @@ void egl_cursor_render(EGL_Cursor * cursor) case LG_CURSOR_COLOR: { - egl_texture_setup(cursor->texture, EGL_PF_BGRA, cursor->width, cursor->height, cursor->stride, false, false); - egl_texture_update(cursor->texture, data); - egl_model_set_texture(cursor->model, cursor->texture); + egl_texture_setup(cursor->norm.texture, EGL_PF_BGRA, cursor->width, cursor->height, cursor->stride, false, false); + egl_texture_update(cursor->norm.texture, data); + egl_model_set_texture(cursor->model, cursor->norm.texture); break; } @@ -246,10 +238,10 @@ void egl_cursor_render(EGL_Cursor * cursor) 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->textureMono, EGL_PF_BGRA, cursor->width, cursor->height, cursor->width * 4, false, false); - egl_texture_update(cursor->texture , (uint8_t *)and); - egl_texture_update(cursor->textureMono, (uint8_t *)xor); + egl_texture_setup (cursor->norm.texture , 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->norm.texture , (uint8_t *)and); + egl_texture_update(cursor->mono.texture, (uint8_t *)xor); break; } } @@ -261,26 +253,26 @@ void egl_cursor_render(EGL_Cursor * cursor) { case LG_CURSOR_MONOCHROME: { - egl_shader_use(cursor->shader); - glUniform4f(cursor->uMousePos, cursor->x, cursor->y, cursor->w, cursor->h / 2); - glUniform1i(cursor->uCBMode , cursor->cbMode); + egl_shader_use(cursor->norm.shader); + glUniform4f(cursor->norm.uMousePos, cursor->x, cursor->y, cursor->w, cursor->h / 2); + glUniform1i(cursor->norm.uCBMode , cursor->cbMode); 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_shader_use(cursor->shaderMono); - glUniform4f(cursor->uMousePosMono, cursor->x, cursor->y, cursor->w, cursor->h / 2); + egl_shader_use(cursor->mono.shader); + glUniform4f(cursor->mono.uMousePos, cursor->x, cursor->y, cursor->w, cursor->h / 2); 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); break; } case LG_CURSOR_COLOR: { - egl_shader_use(cursor->shader); - glUniform4f(cursor->uMousePos, cursor->x, cursor->y, cursor->w, cursor->h); - glUniform1i(cursor->uCBMode , cursor->cbMode); + egl_shader_use(cursor->norm.shader); + glUniform4f(cursor->norm.uMousePos, cursor->x, cursor->y, cursor->w, cursor->h); + glUniform1i(cursor->norm.uCBMode , cursor->cbMode); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); egl_model_render(cursor->model); break; @@ -288,8 +280,8 @@ void egl_cursor_render(EGL_Cursor * cursor) case LG_CURSOR_MASKED_COLOR: { - egl_shader_use(cursor->shaderMono); - glUniform4f(cursor->uMousePos, cursor->x, cursor->y, cursor->w, cursor->h); + egl_shader_use(cursor->mono.shader); + glUniform4f(cursor->mono.uMousePos, cursor->x, cursor->y, cursor->w, cursor->h); glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ZERO); egl_model_render(cursor->model); break;