[client] egl: added FPS rendering

This commit is contained in:
Geoffrey McRae 2018-11-20 09:50:09 +11:00
parent 5b453d604e
commit ab98c87e7c
6 changed files with 165 additions and 17 deletions

View File

@ -50,6 +50,7 @@ link_libraries(
set(SOURCES set(SOURCES
main.c main.c
lg-renderer.c lg-renderer.c
lg-fonts.c
ll.c ll.c
utils.c utils.c
spice/rsa.c spice/rsa.c

View File

@ -116,7 +116,7 @@ static LG_FontBitmap * lgf_sdl_render(LG_FontObj opaque, unsigned int fg_color,
if (!(surface = TTF_RenderText_Blended(this->font, text, color))) if (!(surface = TTF_RenderText_Blended(this->font, text, color)))
{ {
DEBUG_ERROR("Failed to render text: %s", TTF_GetError()); DEBUG_ERROR("Failed to render text: %s", TTF_GetError());
return false; return NULL;
} }
LG_FontBitmap * out = malloc(sizeof(LG_FontBitmap)); LG_FontBitmap * out = malloc(sizeof(LG_FontBitmap));
@ -124,7 +124,7 @@ static LG_FontBitmap * lgf_sdl_render(LG_FontObj opaque, unsigned int fg_color,
{ {
SDL_FreeSurface(surface); SDL_FreeSurface(surface);
DEBUG_ERROR("Failed to allocate memory for font bitmap"); DEBUG_ERROR("Failed to allocate memory for font bitmap");
return false; return NULL;
} }
out->reserved = surface; out->reserved = surface;

View File

@ -20,6 +20,7 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
#include <stdbool.h>
typedef void * LG_FontObj; typedef void * LG_FontObj;
typedef struct LG_FontBitmap typedef struct LG_FontBitmap

View File

@ -20,12 +20,4 @@ Place, Suite 330, Boston, MA 02111-1307 USA
#pragma once #pragma once
#include "lg-font.h" #include "lg-font.h"
extern const LG_Font LGF_SDL; extern const LG_Font * LG_Fonts[];
const LG_Font * LG_Fonts[] =
{
&LGF_SDL,
NULL // end of array sentinal
};
#define LG_FONT_COUNT ((sizeof(LG_Font) / sizeof(LG_Font *)) - 1)

View File

@ -18,8 +18,10 @@ Place, Suite 330, Boston, MA 02111-1307 USA
*/ */
#include "lg-renderer.h" #include "lg-renderer.h"
#include "debug.h" #include "debug.h"
#include "utils.h" #include "utils.h"
#include "lg-fonts.h"
#include <SDL2/SDL_syswm.h> #include <SDL2/SDL_syswm.h>
#include <SDL2/SDL_egl.h> #include <SDL2/SDL_egl.h>
@ -42,6 +44,7 @@ struct Models
{ {
struct EGL_Model * desktop; struct EGL_Model * desktop;
struct EGL_Model * mouse; struct EGL_Model * mouse;
struct EGL_Model * fps;
}; };
struct Shaders struct Shaders
@ -52,6 +55,8 @@ struct Shaders
struct EGL_Shader * mouse; struct EGL_Shader * mouse;
struct EGL_Shader * mouse_mono; struct EGL_Shader * mouse_mono;
struct EGL_Shader * fps;
}; };
struct Textures struct Textures
@ -59,6 +64,7 @@ struct Textures
struct EGL_Texture * desktop; struct EGL_Texture * desktop;
struct EGL_Texture * mouse; struct EGL_Texture * mouse;
struct EGL_Texture * mouse_mono; struct EGL_Texture * mouse_mono;
struct EGL_Texture * fps;
}; };
struct Inst struct Inst
@ -85,6 +91,9 @@ struct Inst
const uint8_t * data; const uint8_t * data;
bool update; bool update;
int width, height;
LG_RendererRect destRect;
float translateX, translateY; float translateX, translateY;
float scaleX , scaleY; float scaleX , scaleY;
GLint uDesktopPos; GLint uDesktopPos;
@ -102,6 +111,13 @@ struct Inst
uint8_t * mouseData; uint8_t * mouseData;
size_t mouseDataSize; size_t mouseDataSize;
bool mouseUpdate; bool mouseUpdate;
const LG_Font * font;
LG_FontObj fontObj;
bool fpsReady;
float fpsWidth, fpsHeight;
GLint uFPSScreen, uFPSSize;
}; };
@ -134,6 +150,13 @@ bool egl_create(void ** opaque, const LG_RendererParams params)
this->scaleX = 1.0f; this->scaleX = 1.0f;
this->scaleY = 1.0f; this->scaleY = 1.0f;
this->font = LG_Fonts[0];
if (!this->font->create(&this->fontObj, NULL, 14))
{
DEBUG_ERROR("Failed to create a font instance");
return false;
}
return true; return true;
} }
@ -151,16 +174,22 @@ void egl_deinitialize(void * opaque)
{ {
struct Inst * this = (struct Inst *)opaque; struct Inst * this = (struct Inst *)opaque;
if (this->font && this->fontObj)
this->font->destroy(this->fontObj);
egl_model_free (&this->models .desktop ); egl_model_free (&this->models .desktop );
egl_model_free (&this->models .mouse ); egl_model_free (&this->models .mouse );
egl_model_free (&this->models .fps );
egl_shader_free (&this->shaders .rgba ); egl_shader_free (&this->shaders .rgba );
egl_shader_free (&this->shaders .bgra ); egl_shader_free (&this->shaders .bgra );
egl_shader_free (&this->shaders .yuv ); egl_shader_free (&this->shaders .yuv );
egl_shader_free (&this->shaders .mouse ); egl_shader_free (&this->shaders .mouse );
egl_shader_free (&this->shaders .mouse_mono); egl_shader_free (&this->shaders .mouse_mono);
egl_shader_free (&this->shaders .fps );
egl_texture_free(&this->textures.desktop ); egl_texture_free(&this->textures.desktop );
egl_texture_free(&this->textures.mouse ); egl_texture_free(&this->textures.mouse );
egl_texture_free(&this->textures.mouse_mono); egl_texture_free(&this->textures.mouse_mono);
egl_texture_free(&this->textures.fps );
LG_LOCK_FREE(this->mouseLock); LG_LOCK_FREE(this->mouseLock);
if (this->mouseData) if (this->mouseData)
@ -173,6 +202,10 @@ void egl_on_resize(void * opaque, const int width, const int height, const LG_Re
{ {
struct Inst * this = (struct Inst *)opaque; struct Inst * this = (struct Inst *)opaque;
this->width = width;
this->height = height;
memcpy(&this->destRect, &destRect, sizeof(LG_RendererRect));
glViewport(0, 0, width, height); glViewport(0, 0, width, height);
if (destRect.valid) if (destRect.valid)
@ -260,6 +293,8 @@ bool egl_on_frame_event(void * opaque, const LG_RendererFormat format, const uin
default: default:
return false; return false;
} }
this->uDesktopPos = egl_shader_get_uniform_location(this->shader, "position");
} }
this->data = data; this->data = data;
@ -371,6 +406,9 @@ bool egl_render_startup(void * opaque, SDL_Window * window)
if (!egl_shader_init(&this->shaders.mouse_mono)) if (!egl_shader_init(&this->shaders.mouse_mono))
return false; return false;
if (!egl_shader_init(&this->shaders.fps))
return false;
if (!egl_shader_compile(this->shaders.rgba, egl_vertex_shader_desktop, sizeof(egl_vertex_shader_desktop), egl_fragment_shader_rgba, sizeof(egl_fragment_shader_rgba))) if (!egl_shader_compile(this->shaders.rgba, egl_vertex_shader_desktop, sizeof(egl_vertex_shader_desktop), egl_fragment_shader_rgba, sizeof(egl_fragment_shader_rgba)))
return false; return false;
@ -386,8 +424,13 @@ bool egl_render_startup(void * opaque, SDL_Window * window)
if (!egl_shader_compile(this->shaders.mouse_mono, egl_vertex_shader_mouse, sizeof(egl_vertex_shader_mouse), egl_fragment_shader_mouse_mono, sizeof(egl_fragment_shader_mouse_mono))) if (!egl_shader_compile(this->shaders.mouse_mono, egl_vertex_shader_mouse, sizeof(egl_vertex_shader_mouse), egl_fragment_shader_mouse_mono, sizeof(egl_fragment_shader_mouse_mono)))
return false; return false;
this->uMousePos = egl_shader_get_uniform_location(this->shaders.mouse , "mouse"); if (!egl_shader_compile(this->shaders.fps, egl_vertex_shader_fps, sizeof(egl_vertex_shader_fps), egl_fragment_shader_fps, sizeof(egl_fragment_shader_fps)))
this->uMousePosMono = egl_shader_get_uniform_location(this->shaders.mouse_mono, "mouse"); return false;
this->uMousePos = egl_shader_get_uniform_location(this->shaders.mouse , "mouse" );
this->uMousePosMono = egl_shader_get_uniform_location(this->shaders.mouse_mono, "mouse" );
this->uFPSSize = egl_shader_get_uniform_location(this->shaders.fps , "size" );
this->uFPSScreen = egl_shader_get_uniform_location(this->shaders.fps , "screen" );
if (!egl_texture_init(&this->textures.desktop)) if (!egl_texture_init(&this->textures.desktop))
return false; return false;
@ -398,19 +441,29 @@ bool egl_render_startup(void * opaque, SDL_Window * window)
if (!egl_texture_init(&this->textures.mouse_mono)) if (!egl_texture_init(&this->textures.mouse_mono))
return false; return false;
if (!egl_texture_init(&this->textures.fps))
return false;
if (!egl_model_init(&this->models.desktop)) if (!egl_model_init(&this->models.desktop))
return false; return false;
if (!egl_model_init(&this->models.mouse)) if (!egl_model_init(&this->models.mouse))
return false; return false;
if (!egl_model_init(&this->models.fps))
return false;
egl_model_set_verticies(this->models.desktop, square , sizeof(square) / sizeof(GLfloat)); egl_model_set_verticies(this->models.desktop, square , sizeof(square) / sizeof(GLfloat));
egl_model_set_uvs (this->models.desktop, uvs , sizeof(uvs ) / sizeof(GLfloat)); egl_model_set_uvs (this->models.desktop, uvs , sizeof(uvs ) / sizeof(GLfloat));
egl_model_set_texture (this->models.desktop, this->textures.desktop); egl_model_set_texture (this->models.desktop, this->textures.desktop);
egl_model_set_verticies(this->models.mouse , square , sizeof(square) / sizeof(GLfloat)); egl_model_set_verticies(this->models.mouse, square, sizeof(square) / sizeof(GLfloat));
egl_model_set_uvs (this->models.mouse , uvs , sizeof(uvs ) / sizeof(GLfloat)); egl_model_set_uvs (this->models.mouse, uvs , sizeof(uvs ) / sizeof(GLfloat));
egl_model_set_verticies(this->models.fps, square, sizeof(square) / sizeof(GLfloat));
egl_model_set_uvs (this->models.fps, uvs , sizeof(uvs ) / sizeof(GLfloat));
egl_model_set_texture (this->models.fps, this->textures.fps);
eglSwapInterval(this->display, this->opt.vsync ? 1 : 0); eglSwapInterval(this->display, this->opt.vsync ? 1 : 0);
@ -461,6 +514,17 @@ bool egl_render(void * opaque, SDL_Window * window)
} }
} }
if (this->fpsReady)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
egl_shader_use(this->shaders.fps);
glUniform2f(this->uFPSScreen, this->width , this->height );
glUniform2f(this->uFPSSize , this->fpsWidth, this->fpsHeight);
egl_model_render(this->models.fps);
glDisable(GL_BLEND);
}
eglSwapBuffers(this->display, this->surface); eglSwapBuffers(this->display, this->surface);
// defer texture uploads until after the flip to avoid stalling // defer texture uploads until after the flip to avoid stalling
@ -491,8 +555,42 @@ bool egl_render(void * opaque, SDL_Window * window)
return true; return true;
} }
void egl_update_fps(void * opaque, const float avgFps, const float renderFps) void egl_update_fps(void * opaque, const float avgFPS, const float renderFPS)
{ {
struct Inst * this = (struct Inst *)opaque;
if (!this->params.showFPS)
return;
char str[128];
snprintf(str, sizeof(str), "UPS: %8.4f, FPS: %8.4f", avgFPS, renderFPS);
LG_FontBitmap * bmp = this->font->render(this->fontObj, 0xffffff00, str);
if (!bmp)
{
DEBUG_ERROR("Failed to render FPS text");
return;
}
egl_texture_setup(
this->textures.fps,
EGL_PF_BGRA,
bmp->width ,
bmp->height,
bmp->width * bmp->height * bmp->bpp,
false
);
egl_texture_update
(
this->textures.fps,
bmp->pixels
);
this->fpsWidth = bmp->width;
this->fpsHeight = bmp->height;
this->fpsReady = true;
this->font->release(this->fontObj, bmp);
} }
void update_mouse_shape(struct Inst * this) void update_mouse_shape(struct Inst * this)

View File

@ -150,4 +150,60 @@ void main()\
}\ }\
"; ";
static const char egl_vertex_shader_fps[] = "\
#version 300 es\n\
\
layout(location = 0) in vec3 vertexPosition_modelspace;\
layout(location = 1) in vec2 vertexUV;\
\
uniform vec2 screen;\
uniform vec2 size;\
\
out highp vec2 uv;\
\
void main()\
{\
highp vec2 pix = (vec2(1.0, 1.0) / screen); \
gl_Position.xyz = vertexPosition_modelspace; \
gl_Position.w = 1.0; \
gl_Position.x *= pix.x * size.x; \
gl_Position.y *= pix.y * size.y; \
gl_Position.x -= 1.0 - (pix.x * size.x);\
gl_Position.y += 1.0 - (pix.y * size.y);\
gl_Position.x += pix.x * 10.0; \
gl_Position.y -= pix.y * 10.0; \
\
uv = vertexUV;\
}\
";
static const char egl_fragment_shader_fps[] = "\
#version 300 es\n\
\
in highp vec2 uv;\
out highp vec4 color;\
\
uniform sampler2D sampler1;\
\
void main()\
{\
highp vec4 tmp = texture(sampler1, uv);\
if (tmp.a == 0.0) \
{ \
color.r = 0.0;\
color.g = 0.0;\
color.b = 1.0;\
color.a = 0.5;\
} \
else \
{ \
color.r = tmp.b;\
color.g = tmp.g;\
color.b = tmp.r;\
color.a = tmp.a;\
} \
}\
";
#endif #endif