[client] move FPS calculations out of renderers

This commit is contained in:
Geoffrey McRae 2018-11-20 05:26:51 +11:00
parent 0ed9301ed9
commit 90fc2a8164
4 changed files with 89 additions and 75 deletions

View File

@ -36,7 +36,8 @@ Place, Suite 330, Boston, MA 02111-1307 USA
(x)->on_mouse_event && \ (x)->on_mouse_event && \
(x)->on_alert && \ (x)->on_alert && \
(x)->render_startup && \ (x)->render_startup && \
(x)->render) (x)->render && \
(x)->update_fps)
#define LGR_OPTION_COUNT(x) (sizeof(x) / sizeof(LG_RendererOpt)) #define LGR_OPTION_COUNT(x) (sizeof(x) / sizeof(LG_RendererOpt))
@ -116,6 +117,7 @@ typedef bool (* LG_RendererOnMouseEvent)(void * opaque, const bool visib
typedef bool (* LG_RendererOnFrameEvent)(void * opaque, const LG_RendererFormat format, const uint8_t * data); typedef bool (* LG_RendererOnFrameEvent)(void * opaque, const LG_RendererFormat format, const uint8_t * data);
typedef void (* LG_RendererOnAlert )(void * opaque, const LG_RendererAlert alert, const char * message, bool ** closeFlag); typedef void (* LG_RendererOnAlert )(void * opaque, const LG_RendererAlert alert, const char * message, bool ** closeFlag);
typedef bool (* LG_RendererRender )(void * opaque, SDL_Window *window); typedef bool (* LG_RendererRender )(void * opaque, SDL_Window *window);
typedef void (* LG_RendererUpdateFPS )(void * opaque, const float avgFPS, const float renderFPS);
typedef struct LG_Renderer typedef struct LG_Renderer
{ {
@ -132,6 +134,7 @@ typedef struct LG_Renderer
LG_RendererOnAlert on_alert; LG_RendererOnAlert on_alert;
LG_RendererRender render_startup; LG_RendererRender render_startup;
LG_RendererRender render; LG_RendererRender render;
LG_RendererUpdateFPS update_fps;
} }
LG_Renderer; LG_Renderer;

View File

@ -68,6 +68,11 @@ struct AppState
struct KVMFRHeader * shm; struct KVMFRHeader * shm;
unsigned int shmSize; unsigned int shmSize;
int64_t fpsSleep; int64_t fpsSleep;
uint64_t lastFrameTime;
uint64_t renderTime;
uint64_t frameCount;
uint64_t renderCount;
}; };
typedef struct RenderOpts typedef struct RenderOpts
@ -198,6 +203,22 @@ int renderThread(void * unused)
if (!state.lgr->render(state.lgrData, state.window)) if (!state.lgr->render(state.lgrData, state.window))
break; break;
const uint64_t t = nanotime();
state.renderTime += t - state.lastFrameTime;
state.lastFrameTime = t;
++state.renderCount;
if (state.renderTime > 1e9)
{
const float avgFPS = 1000.0f / (((float)state.renderTime / state.frameCount ) / 1e6f);
const float renderFPS = 1000.0f / (((float)state.renderTime / state.renderCount) / 1e6f);
state.renderTime = 0;
state.frameCount = 0;
state.renderCount = 0;
state.lgr->update_fps(state.lgrData, avgFPS, renderFPS);
}
const uint64_t total = microtime() - start; const uint64_t total = microtime() - start;
if (total < state.fpsSleep) if (total < state.fpsSleep)
{ {
@ -409,6 +430,7 @@ int frameThread(void * unused)
break; break;
} }
++state.frameCount;
if (!state.started) if (!state.started)
{ {
state.started = true; state.started = true;

View File

@ -491,6 +491,10 @@ 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 update_mouse_shape(struct Inst * this) void update_mouse_shape(struct Inst * this)
{ {
LG_LOCK(this->mouseLock); LG_LOCK(this->mouseLock);
@ -591,5 +595,6 @@ struct LG_Renderer LGR_EGL =
.on_frame_event = egl_on_frame_event, .on_frame_event = egl_on_frame_event,
.on_alert = egl_on_alert, .on_alert = egl_on_alert,
.render_startup = egl_render_startup, .render_startup = egl_render_startup,
.render = egl_render .render = egl_render,
.update_fps = egl_update_fps
}; };

View File

@ -125,10 +125,6 @@ struct Inst
bool waitDone; bool waitDone;
bool fpsTexture; bool fpsTexture;
uint64_t lastFrameTime;
uint64_t renderTime;
uint64_t frameCount;
uint64_t renderCount;
SDL_Rect fpsRect; SDL_Rect fpsRect;
LG_Lock mouseLock; LG_Lock mouseLock;
@ -141,7 +137,6 @@ struct Inst
bool mouseUpdate; bool mouseUpdate;
bool newShape; bool newShape;
uint64_t lastMouseDraw;
LG_RendererCursor mouseType; LG_RendererCursor mouseType;
bool mouseVisible; bool mouseVisible;
SDL_Rect mousePos; SDL_Rect mousePos;
@ -379,7 +374,6 @@ bool opengl_on_frame_event(void * opaque, const LG_RendererFormat format, const
this->waitFadeTime = microtime() + FADE_TIME; this->waitFadeTime = microtime() + FADE_TIME;
} }
++this->frameCount;
return true; return true;
} }
@ -530,65 +524,6 @@ bool opengl_render(void * opaque, SDL_Window * window)
if (!draw_frame(this)) if (!draw_frame(this))
return false; return false;
if (this->params.showFPS && this->renderTime > 1e9)
{
char str[128];
const float avgFPS = 1000.0f / (((float)this->renderTime / this->frameCount ) / 1e6f);
const float renderFPS = 1000.0f / (((float)this->renderTime / this->renderCount) / 1e6f);
snprintf(str, sizeof(str), "UPS: %8.4f, FPS: %8.4f", avgFPS, renderFPS);
LG_FontBitmap *textSurface = NULL;
if (!(textSurface = this->font->render(this->fontObj, LG_FONT_BITMAP, 0xffffff00, str)))
{
DEBUG_ERROR("Failed to render text");
LG_UNLOCK(this->formatLock);
return false;
}
bitmap_to_texture(textSurface, this->textures[FPS_TEXTURE]);
this->fpsRect.x = 5;
this->fpsRect.y = 5;
this->fpsRect.w = textSurface->width;
this->fpsRect.h = textSurface->height;
this->font->release(this->fontObj, textSurface);
this->renderTime = 0;
this->frameCount = 0;
this->renderCount = 0;
this->fpsTexture = true;
glNewList(this->fpsList, GL_COMPILE);
glPushMatrix();
glLoadIdentity();
glEnable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glColor4f(0.0f, 0.0f, 1.0f, 0.5f);
glBegin(GL_TRIANGLE_STRIP);
glVertex2i(this->fpsRect.x , this->fpsRect.y );
glVertex2i(this->fpsRect.x + this->fpsRect.w, this->fpsRect.y );
glVertex2i(this->fpsRect.x , this->fpsRect.y + this->fpsRect.h);
glVertex2i(this->fpsRect.x + this->fpsRect.w, this->fpsRect.y + this->fpsRect.h);
glEnd();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, this->textures[FPS_TEXTURE]);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(0.0f , 0.0f); glVertex2i(this->fpsRect.x , this->fpsRect.y );
glTexCoord2f(1.0f , 0.0f); glVertex2i(this->fpsRect.x + this->fpsRect.w, this->fpsRect.y );
glTexCoord2f(0.0f , 1.0f); glVertex2i(this->fpsRect.x , this->fpsRect.y + this->fpsRect.h);
glTexCoord2f(1.0f, 1.0f); glVertex2i(this->fpsRect.x + this->fpsRect.w, this->fpsRect.y + this->fpsRect.h);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_BLEND);
glPopMatrix();
glEndList();
}
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
@ -683,16 +618,64 @@ bool opengl_render(void * opaque, SDL_Window * window)
else else
SDL_GL_SwapWindow(window); SDL_GL_SwapWindow(window);
const uint64_t t = nanotime();
this->renderTime += t - this->lastFrameTime;
this->lastFrameTime = t;
++this->renderCount;
this->mouseUpdate = false; this->mouseUpdate = false;
this->lastMouseDraw = t;
return true; return true;
} }
void opengl_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 *textSurface = NULL;
if (!(textSurface = this->font->render(this->fontObj, LG_FONT_BITMAP, 0xffffff00, str)))
DEBUG_ERROR("Failed to render text");
bitmap_to_texture(textSurface, this->textures[FPS_TEXTURE]);
this->fpsRect.x = 5;
this->fpsRect.y = 5;
this->fpsRect.w = textSurface->width;
this->fpsRect.h = textSurface->height;
this->font->release(this->fontObj, textSurface);
this->fpsTexture = true;
glNewList(this->fpsList, GL_COMPILE);
glPushMatrix();
glLoadIdentity();
glEnable(GL_BLEND);
glDisable(GL_TEXTURE_2D);
glColor4f(0.0f, 0.0f, 1.0f, 0.5f);
glBegin(GL_TRIANGLE_STRIP);
glVertex2i(this->fpsRect.x , this->fpsRect.y );
glVertex2i(this->fpsRect.x + this->fpsRect.w, this->fpsRect.y );
glVertex2i(this->fpsRect.x , this->fpsRect.y + this->fpsRect.h);
glVertex2i(this->fpsRect.x + this->fpsRect.w, this->fpsRect.y + this->fpsRect.h);
glEnd();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, this->textures[FPS_TEXTURE]);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2f(0.0f , 0.0f); glVertex2i(this->fpsRect.x , this->fpsRect.y );
glTexCoord2f(1.0f , 0.0f); glVertex2i(this->fpsRect.x + this->fpsRect.w, this->fpsRect.y );
glTexCoord2f(0.0f , 1.0f); glVertex2i(this->fpsRect.x , this->fpsRect.y + this->fpsRect.h);
glTexCoord2f(1.0f, 1.0f); glVertex2i(this->fpsRect.x + this->fpsRect.w, this->fpsRect.y + this->fpsRect.h);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_BLEND);
glPopMatrix();
glEndList();
}
void draw_torus(float x, float y, float inner, float outer, unsigned int pts) void draw_torus(float x, float y, float inner, float outer, unsigned int pts)
{ {
glBegin(GL_QUAD_STRIP); glBegin(GL_QUAD_STRIP);
@ -866,7 +849,8 @@ const LG_Renderer LGR_OpenGL =
.on_frame_event = opengl_on_frame_event, .on_frame_event = opengl_on_frame_event,
.on_alert = opengl_on_alert, .on_alert = opengl_on_alert,
.render_startup = opengl_render_startup, .render_startup = opengl_render_startup,
.render = opengl_render .render = opengl_render,
.update_fps = opengl_update_fps
}; };
static bool _check_gl_error(unsigned int line, const char * name) static bool _check_gl_error(unsigned int line, const char * name)