2018-12-12 09:04:43 +00:00
|
|
|
/*
|
|
|
|
Looking Glass - KVM FrameRelay (KVMFR) Client
|
2019-02-22 11:16:14 +00:00
|
|
|
Copyright (C) 2017-2019 Geoffrey McRae <geoff@hostfission.com>
|
2018-12-12 09:04:43 +00:00
|
|
|
https://looking-glass.hostfission.com
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it under
|
|
|
|
cahe 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 "fps.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
#include "texture.h"
|
|
|
|
#include "shader.h"
|
|
|
|
#include "model.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2019-03-28 04:53:15 +00:00
|
|
|
// these headers are auto generated by cmake
|
|
|
|
#include "fps.vert.h"
|
|
|
|
#include "fps.frag.h"
|
|
|
|
#include "fps_bg.frag.h"
|
|
|
|
|
2018-12-12 09:04:43 +00:00
|
|
|
struct EGL_FPS
|
|
|
|
{
|
|
|
|
const LG_Font * font;
|
|
|
|
LG_FontObj fontObj;
|
|
|
|
|
|
|
|
EGL_Texture * texture;
|
|
|
|
EGL_Shader * shader;
|
2018-12-12 11:38:08 +00:00
|
|
|
EGL_Shader * shaderBG;
|
2018-12-12 09:04:43 +00:00
|
|
|
EGL_Model * model;
|
|
|
|
|
|
|
|
bool ready;
|
|
|
|
float width, height;
|
|
|
|
|
|
|
|
// uniforms
|
2018-12-12 11:38:08 +00:00
|
|
|
GLint uScreen , uSize;
|
|
|
|
GLint uScreenBG, uSizeBG;
|
2018-12-12 09:04:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
bool egl_fps_init(EGL_FPS ** fps, const LG_Font * font, LG_FontObj fontObj)
|
|
|
|
{
|
|
|
|
*fps = (EGL_FPS *)malloc(sizeof(EGL_FPS));
|
|
|
|
if (!*fps)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to malloc EGL_FPS");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(*fps, 0, sizeof(EGL_FPS));
|
|
|
|
|
|
|
|
(*fps)->font = font;
|
|
|
|
(*fps)->fontObj = fontObj;
|
|
|
|
|
|
|
|
if (!egl_texture_init(&(*fps)->texture))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to initialize the fps texture");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!egl_shader_init(&(*fps)->shader))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to initialize the fps shader");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-12 11:38:08 +00:00
|
|
|
if (!egl_shader_init(&(*fps)->shaderBG))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to initialize the fps bg shader");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-12 09:04:43 +00:00
|
|
|
if (!egl_shader_compile((*fps)->shader,
|
2019-03-28 04:53:15 +00:00
|
|
|
b_shader_fps_vert, b_shader_fps_vert_size,
|
|
|
|
b_shader_fps_frag, b_shader_fps_frag_size))
|
2018-12-12 11:38:08 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to compile the fps shader");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!egl_shader_compile((*fps)->shaderBG,
|
2019-03-28 04:53:15 +00:00
|
|
|
b_shader_fps_vert , b_shader_fps_vert_size,
|
|
|
|
b_shader_fps_bg_frag, b_shader_fps_bg_frag_size))
|
2018-12-12 09:04:43 +00:00
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to compile the fps shader");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-12 11:38:08 +00:00
|
|
|
|
|
|
|
(*fps)->uSize = egl_shader_get_uniform_location((*fps)->shader , "size" );
|
|
|
|
(*fps)->uScreen = egl_shader_get_uniform_location((*fps)->shader , "screen");
|
|
|
|
(*fps)->uSizeBG = egl_shader_get_uniform_location((*fps)->shaderBG, "size" );
|
|
|
|
(*fps)->uScreenBG = egl_shader_get_uniform_location((*fps)->shaderBG, "screen");
|
2018-12-12 09:04:43 +00:00
|
|
|
|
|
|
|
if (!egl_model_init(&(*fps)->model))
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to initialize the fps model");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-12-12 09:08:52 +00:00
|
|
|
egl_model_set_default((*fps)->model);
|
|
|
|
egl_model_set_texture((*fps)->model, (*fps)->texture);
|
2018-12-12 09:04:43 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void egl_fps_free(EGL_FPS ** fps)
|
|
|
|
{
|
|
|
|
if (!*fps)
|
|
|
|
return;
|
|
|
|
|
2018-12-12 11:38:08 +00:00
|
|
|
egl_texture_free(&(*fps)->texture );
|
|
|
|
egl_shader_free (&(*fps)->shader );
|
|
|
|
egl_shader_free (&(*fps)->shaderBG);
|
|
|
|
egl_model_free (&(*fps)->model );
|
2018-12-12 09:04:43 +00:00
|
|
|
|
|
|
|
free(*fps);
|
|
|
|
*fps = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void egl_fps_update(EGL_FPS * fps, const float avgFPS, const float renderFPS)
|
|
|
|
{
|
|
|
|
char str[128];
|
|
|
|
snprintf(str, sizeof(str), "UPS: %8.4f, FPS: %8.4f", avgFPS, renderFPS);
|
|
|
|
|
|
|
|
LG_FontBitmap * bmp = fps->font->render(fps->fontObj, 0xffffff00, str);
|
|
|
|
if (!bmp)
|
|
|
|
{
|
|
|
|
DEBUG_ERROR("Failed to render fps text");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
egl_texture_setup(
|
|
|
|
fps->texture,
|
|
|
|
EGL_PF_BGRA,
|
|
|
|
bmp->width ,
|
|
|
|
bmp->height,
|
2019-01-01 23:30:19 +00:00
|
|
|
bmp->width * bmp->bpp,
|
2018-12-12 09:04:43 +00:00
|
|
|
false
|
|
|
|
);
|
|
|
|
|
|
|
|
egl_texture_update
|
|
|
|
(
|
|
|
|
fps->texture,
|
|
|
|
bmp->pixels
|
|
|
|
);
|
|
|
|
|
|
|
|
fps->width = bmp->width;
|
|
|
|
fps->height = bmp->height;
|
|
|
|
fps->ready = true;
|
|
|
|
|
|
|
|
fps->font->release(fps->fontObj, bmp);
|
|
|
|
}
|
|
|
|
|
2018-12-15 23:57:01 +00:00
|
|
|
void egl_fps_render(EGL_FPS * fps, const float scaleX, const float scaleY)
|
2018-12-12 09:04:43 +00:00
|
|
|
{
|
|
|
|
if (!fps->ready)
|
|
|
|
return;
|
|
|
|
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
2018-12-12 11:38:08 +00:00
|
|
|
|
|
|
|
// render the background first
|
|
|
|
egl_shader_use(fps->shaderBG);
|
2018-12-15 23:57:01 +00:00
|
|
|
glUniform2f(fps->uScreenBG, scaleX , scaleY );
|
|
|
|
glUniform2f(fps->uSizeBG , fps->width, fps->height);
|
2018-12-12 11:38:08 +00:00
|
|
|
egl_model_render(fps->model);
|
|
|
|
|
|
|
|
// render the texture over the background
|
2018-12-12 09:04:43 +00:00
|
|
|
egl_shader_use(fps->shader);
|
2018-12-15 23:57:01 +00:00
|
|
|
glUniform2f(fps->uScreen, scaleX , scaleY );
|
|
|
|
glUniform2f(fps->uSize , fps->width, fps->height);
|
2018-12-12 09:04:43 +00:00
|
|
|
egl_model_render(fps->model);
|
2018-12-12 11:38:08 +00:00
|
|
|
|
2018-12-12 09:04:43 +00:00
|
|
|
glDisable(GL_BLEND);
|
|
|
|
}
|