[client] app: implement new overlay rendering framework

This change set implements a framework for overlays to be registered
that make use of ImGui. See `overlay/fps` for a simple implementation
example.
This commit is contained in:
Geoffrey McRae 2021-07-22 17:27:30 +10:00
parent 30c4a4786b
commit fdbdf6f167
11 changed files with 385 additions and 124 deletions

View File

@ -120,6 +120,9 @@ set(SOURCES
src/kb.c src/kb.c
src/egl_dynprocs.c src/egl_dynprocs.c
src/eglutil.c src/eglutil.c
src/overlay/fps.c
src/overlay/graphs.c
) )
# Force cimgui to build as a static library. # Force cimgui to build as a static library.

View File

@ -27,6 +27,7 @@
#include "common/ringbuffer.h" #include "common/ringbuffer.h"
#include "common/types.h" #include "common/types.h"
#include "interface/displayserver.h" #include "interface/displayserver.h"
#include "interface/overlay.h"
typedef enum LG_MsgAlert typedef enum LG_MsgAlert
{ {
@ -79,10 +80,15 @@ void app_glSetSwapInterval(int interval);
void app_glSwapBuffers(void); void app_glSwapBuffers(void);
#endif #endif
typedef struct ImGuiGraph * GraphHandle; void app_registerOverlay(const struct LG_OverlayOps * ops, void * params);
int app_renderOverlay(struct Rect * rects, int maxRects);
void app_freeOverlays(void);
struct OverlayGraph;
typedef struct OverlayGraph * GraphHandle;
GraphHandle app_registerGraph(const char * name, RingBuffer buffer); GraphHandle app_registerGraph(const char * name, RingBuffer buffer);
void app_unregisterGraph(GraphHandle handle); void app_unregisterGraph(GraphHandle handle);
bool app_renderImGui(void);
void app_clipboardRelease(void); void app_clipboardRelease(void);
void app_clipboardNotifyTypes(const LG_ClipboardData types[], int count); void app_clipboardNotifyTypes(const LG_ClipboardData types[], int count);

View File

@ -22,6 +22,8 @@
#define _H_I_OVERLAY_ #define _H_I_OVERLAY_
#include <stdbool.h> #include <stdbool.h>
#include <assert.h>
#include "common/types.h" #include "common/types.h"
enum LG_OverlayFlags enum LG_OverlayFlags
@ -57,7 +59,8 @@ struct LG_OverlayOps
* mode. * mode.
* *
* The caller provides `windowRects` to be populated by the callee and is sized * The caller provides `windowRects` to be populated by the callee and is sized
* according to the return value of `getWindowCount` * according to the return value of `getWindowCount`. Note, `windowRects` may
* be NULL if the caller does not want this information.
*/ */
void (*render)(void * udata, bool interactive, struct Rect windowRects[]); void (*render)(void * udata, bool interactive, struct Rect windowRects[]);

View File

@ -1007,16 +1007,24 @@ bool egl_render(void * opaque, LG_RendererRotate rotate, const bool newFrame)
hasOverlay |= egl_help_render(this->help, this->screenScaleX, this->screenScaleY); hasOverlay |= egl_help_render(this->help, this->screenScaleX, this->screenScaleY);
hasOverlay |= egl_damage_render(this->damage, newFrame ? desktopDamage : NULL); hasOverlay |= egl_damage_render(this->damage, newFrame ? desktopDamage : NULL);
if (app_renderImGui()) struct Rect damage[KVMFR_MAX_DAMAGE_RECTS + 2];
int damageIdx = app_renderOverlay(damage, KVMFR_MAX_DAMAGE_RECTS);
// if no overlay
if (damageIdx == -1)
{
damageIdx = 0;
}
else
{ {
ImGui_ImplOpenGL3_NewFrame(); ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplOpenGL3_RenderDrawData(igGetDrawData()); ImGui_ImplOpenGL3_RenderDrawData(igGetDrawData());
// if there were too many rects invalidate the entire window
if (damageIdx == 0)
hasOverlay = true; hasOverlay = true;
} }
struct Rect damage[KVMFR_MAX_DAMAGE_RECTS + 2];
int damageIdx = 0;
if (!hasOverlay && !this->hadOverlay) if (!hasOverlay && !this->hadOverlay)
{ {
if (this->cursorLast.visible) if (this->cursorLast.visible)

View File

@ -686,7 +686,7 @@ bool opengl_render(void * opaque, LG_RendererRotate rotate, const bool newFrame)
break; break;
} }
if (app_renderImGui()) if (app_renderOverlay(NULL, 0) > -1)
{ {
ImGui_ImplOpenGL2_NewFrame(); ImGui_ImplOpenGL2_NewFrame();
ImGui_ImplOpenGL2_RenderDrawData(igGetDrawData()); ImGui_ImplOpenGL2_RenderDrawData(igGetDrawData());

View File

@ -30,6 +30,8 @@
#include "common/debug.h" #include "common/debug.h"
#include "common/stringutils.h" #include "common/stringutils.h"
#include "interface/overlay.h"
#include "overlays.h"
#include "cimgui.h" #include "cimgui.h"
@ -612,143 +614,87 @@ void app_showHelp(bool show)
free(help); free(help);
} }
struct ImGuiGraph
{
const char * name;
RingBuffer buffer;
bool enabled;
};
GraphHandle app_registerGraph(const char * name, RingBuffer buffer) GraphHandle app_registerGraph(const char * name, RingBuffer buffer)
{ {
struct ImGuiGraph * graph = malloc(sizeof(struct ImGuiGraph)); return overlayGraph_register(name, buffer);
graph->name = name;
graph->buffer = buffer;
graph->enabled = true;
ll_push(g_state.graphs, graph);
return graph;
} }
void app_unregisterGraph(GraphHandle handle) void app_unregisterGraph(GraphHandle handle)
{ {
handle->enabled = false; overlayGraph_unregister(handle);
} }
struct BufferMetrics struct Overlay
{ {
float min; const struct LG_OverlayOps * ops;
float max; void * udata;
float sum; int windowCount;
float avg;
float freq;
}; };
static bool rbCalcMetrics(int index, void * value_, void * udata_) void app_registerOverlay(const struct LG_OverlayOps * ops, void * params)
{ {
float * value = value_; ASSERT_LG_OVERLAY_VALID(ops);
struct BufferMetrics * udata = udata_;
if (index == 0) void * udata;
if (!ops->init(&udata, params))
{ {
udata->min = *value; DEBUG_ERROR("Overlay `%s` failed to initialize", ops->name);
udata->max = *value; return;
udata->sum = *value;
return true;
} }
if (udata->min > *value) struct Overlay * overlay = malloc(sizeof(struct Overlay));
udata->min = *value; overlay->ops = ops;
overlay->udata = udata;
if (udata->max < *value) ll_push(g_state.overlays, overlay);
udata->max = *value;
udata->sum += *value;
return true;
} }
bool app_renderImGui(void) int app_renderOverlay(struct Rect * rects, int maxRects)
{ {
if (!g_state.showFPS && int windowCount = 0;
!g_state.showTiming) struct Overlay * overlay;
return false;
igNewFrame(); // get the total window count
for (ll_reset(g_state.overlays);
ImGuiStyle * style = igGetStyle(); ll_walk(g_state.overlays, (void **)&overlay); )
style->WindowBorderSize = 0.0f;
if (g_state.showFPS)
{ {
const ImVec2 pos = {0.0f, 0.0f}; overlay->windowCount = overlay->ops->getWindowCount(overlay->udata, false);
igSetNextWindowBgAlpha(0.6f); windowCount += overlay->windowCount;
igSetNextWindowPos(pos, 0, pos);
igBegin(
"FPS",
NULL,
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize |
ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing |
ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoTitleBar
);
igText("FPS:%4.2f UPS:%4.2f",
atomic_load_explicit(&g_state.fps, memory_order_relaxed),
atomic_load_explicit(&g_state.ups, memory_order_relaxed));
igEnd();
} }
if (g_state.showTiming) // return -1 if there are no windows to render
{ if (windowCount == 0)
const ImVec2 pos = {0.0f, 0.0f}; return -1;
igSetNextWindowBgAlpha(0.4f);
igSetNextWindowPos(pos, 0, pos);
igBegin( if (windowCount > maxRects)
"Performance Metrics",
NULL,
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize |
ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing |
ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoTitleBar
);
GraphHandle graph;
for (ll_reset(g_state.graphs); ll_walk(g_state.graphs, (void **)&graph); )
{ {
if (!graph->enabled) rects = NULL;
windowCount = 0;
}
// render the overlays
igNewFrame();
for (ll_reset(g_state.overlays);
ll_walk(g_state.overlays, (void **)&overlay); )
{
if (overlay->windowCount == 0)
continue; continue;
struct BufferMetrics metrics = {}; overlay->ops->render(overlay->udata, false, rects);
ringbuffer_forEach(graph->buffer, rbCalcMetrics, &metrics); if (rects)
rects += overlay->windowCount;
if (metrics.sum > 0.0f)
{
metrics.avg = metrics.sum / ringbuffer_getCount(graph->buffer);
metrics.freq = 1000.0f / metrics.avg;
}
char title[64];
const ImVec2 size = {400.0f, 100.0f};
snprintf(title, sizeof(title),
"%s: min:%4.2f max:%4.2f avg:%4.2f/%4.2fHz",
graph->name, metrics.min, metrics.max, metrics.avg, metrics.freq);
igPlotLinesFloatPtr(
"",
(float *)ringbuffer_getValues(graph->buffer),
ringbuffer_getLength(graph->buffer),
ringbuffer_getStart (graph->buffer),
title,
0.0f,
50.0f,
size,
sizeof(float));
}
igEnd();
} }
igRender(); igRender();
return true;
return windowCount;
}
void app_freeOverlays(void)
{
struct Overlay * overlay;
while(ll_shift(g_state.overlays, (void **)&overlay))
{
overlay->ops->free(overlay->udata);
free(overlay);
}
} }

View File

@ -55,6 +55,7 @@
#include "clipboard.h" #include "clipboard.h"
#include "ll.h" #include "ll.h"
#include "egl_dynprocs.h" #include "egl_dynprocs.h"
#include "overlays.h"
// forwards // forwards
static int cursorThread(void * unused); static int cursorThread(void * unused);
@ -758,14 +759,15 @@ static int lg_run(void)
ImFontAtlas_GetTexDataAsRGBA32(g_state.io->Fonts, &text_pixels, ImFontAtlas_GetTexDataAsRGBA32(g_state.io->Fonts, &text_pixels,
&text_w, &text_h, NULL); &text_w, &text_h, NULL);
g_state.graphs = ll_new(); g_state.overlays = ll_new();
app_registerOverlay(&LGOverlayFPS , NULL);
app_registerOverlay(&LGOverlayGraphs, NULL);
// initialize metrics ringbuffers // initialize metrics ringbuffers
g_state.renderTimings = ringbuffer_new(256, sizeof(float)); g_state.renderTimings = ringbuffer_new(256, sizeof(float));
g_state.frameTimings = ringbuffer_new(256, sizeof(float)); g_state.frameTimings = ringbuffer_new(256, sizeof(float));
overlayGraph_register("RENDER", g_state.renderTimings);
app_registerGraph("RENDER", g_state.renderTimings); overlayGraph_register("UPLOAD", g_state.frameTimings );
app_registerGraph("UPLOAD", g_state.frameTimings);
// search for the best displayserver ops to use // search for the best displayserver ops to use
for(int i = 0; i < LG_DISPLAYSERVER_COUNT; ++i) for(int i = 0; i < LG_DISPLAYSERVER_COUNT; ++i)
@ -1091,6 +1093,13 @@ static void lg_shutdown(void)
lgmpClientFree(&g_state.lgmp); lgmpClientFree(&g_state.lgmp);
if (g_state.overlays)
{
app_freeOverlays();
ll_free(g_state.overlays);
g_state.overlays = NULL;
}
if (e_frame) if (e_frame)
{ {
lgFreeEvent(e_frame); lgFreeEvent(e_frame);

View File

@ -48,7 +48,7 @@ struct AppState
enum RunState state; enum RunState state;
ImGuiIO * io; ImGuiIO * io;
struct ll * graphs; struct ll * overlays;
struct LG_DisplayServerOps * ds; struct LG_DisplayServerOps * ds;
bool dsInitialized; bool dsInitialized;

79
client/src/overlay/fps.c Normal file
View File

@ -0,0 +1,79 @@
/**
* Looking Glass
* Copyright (C) 2017-2021 The Looking Glass Authors
* https://looking-glass.io
*
* This program is free software; you can redistribute it and/or modify it
* under the 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 "interface/overlay.h"
#include "cimgui.h"
#include "../main.h"
static bool fps_init(void ** udata, void * params)
{
return true;
}
static void fps_free(void * udata)
{
}
static int fps_getWindowCount(void * udata, bool interactive)
{
return g_state.showFPS ? 1 : 0;
}
static void fps_render(void * udata, bool interactive, struct Rect * windowRects)
{
const ImVec2 pos = {0.0f, 0.0f};
igSetNextWindowBgAlpha(0.6f);
igSetNextWindowPos(pos, 0, pos);
igBegin(
"FPS",
NULL,
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize |
ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing |
ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoTitleBar
);
igText("FPS:%4.2f UPS:%4.2f",
atomic_load_explicit(&g_state.fps, memory_order_relaxed),
atomic_load_explicit(&g_state.ups, memory_order_relaxed));
if (windowRects)
{
ImVec2 pos, size;
igGetWindowPos(&pos);
igGetWindowSize(&size);
windowRects[0].x = pos.x;
windowRects[0].y = pos.y;
windowRects[0].w = size.x;
windowRects[0].h = size.y;
}
igEnd();
}
struct LG_OverlayOps LGOverlayFPS =
{
.name = "FPS",
.init = fps_init,
.free = fps_free,
.getWindowCount = fps_getWindowCount,
.render = fps_render
};

175
client/src/overlay/graphs.c Normal file
View File

@ -0,0 +1,175 @@
/**
* Looking Glass
* Copyright (C) 2017-2021 The Looking Glass Authors
* https://looking-glass.io
*
* This program is free software; you can redistribute it and/or modify it
* under the 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 "interface/overlay.h"
#include "cimgui.h"
#include "../main.h"
#include "ll.h"
#include "common/debug.h"
struct GraphState
{
struct ll * graphs;
};
static struct GraphState gs = {0};
struct OverlayGraph
{
const char * name;
RingBuffer buffer;
bool enabled;
};
static bool graphs_init(void ** udata, void * params)
{
gs.graphs = ll_new();
return true;
}
static void graphs_free(void * udata)
{
ll_free(gs.graphs);
}
static int graphs_getWindowCount(void * udata, bool interactive)
{
return g_state.showTiming ? 1 : 0;
}
struct BufferMetrics
{
float min;
float max;
float sum;
float avg;
float freq;
};
static bool rbCalcMetrics(int index, void * value_, void * udata_)
{
float * value = value_;
struct BufferMetrics * udata = udata_;
if (index == 0)
{
udata->min = *value;
udata->max = *value;
udata->sum = *value;
return true;
}
if (udata->min > *value)
udata->min = *value;
if (udata->max < *value)
udata->max = *value;
udata->sum += *value;
return true;
}
static void graphs_render(void * udata, bool interactive, struct Rect * windowRects)
{
const ImVec2 pos = {0.0f, 0.0f};
igSetNextWindowBgAlpha(0.4f);
igSetNextWindowPos(pos, 0, pos);
igBegin(
"Performance Metrics",
NULL,
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_AlwaysAutoResize |
ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing |
ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoTitleBar
);
GraphHandle graph;
for (ll_reset(gs.graphs); ll_walk(gs.graphs, (void **)&graph); )
{
if (!graph->enabled)
continue;
struct BufferMetrics metrics = {};
ringbuffer_forEach(graph->buffer, rbCalcMetrics, &metrics);
if (metrics.sum > 0.0f)
{
metrics.avg = metrics.sum / ringbuffer_getCount(graph->buffer);
metrics.freq = 1000.0f / metrics.avg;
}
char title[64];
const ImVec2 size = {400.0f, 100.0f};
snprintf(title, sizeof(title),
"%s: min:%4.2f max:%4.2f avg:%4.2f/%4.2fHz",
graph->name, metrics.min, metrics.max, metrics.avg, metrics.freq);
igPlotLinesFloatPtr(
"",
(float *)ringbuffer_getValues(graph->buffer),
ringbuffer_getLength(graph->buffer),
ringbuffer_getStart (graph->buffer),
title,
0.0f,
50.0f,
size,
sizeof(float));
};
if (windowRects)
{
ImVec2 pos, size;
igGetWindowPos(&pos);
igGetWindowSize(&size);
windowRects[0].x = pos.x;
windowRects[0].y = pos.y;
windowRects[0].w = size.x;
windowRects[0].h = size.y;
}
igEnd();
}
struct LG_OverlayOps LGOverlayGraphs =
{
.name = "Graphs",
.init = graphs_init,
.free = graphs_free,
.getWindowCount = graphs_getWindowCount,
.render = graphs_render
};
GraphHandle overlayGraph_register(const char * name, RingBuffer buffer)
{
struct OverlayGraph * graph = malloc(sizeof(struct OverlayGraph));
graph->name = name;
graph->buffer = buffer;
graph->enabled = true;
ll_push(gs.graphs, graph);
return graph;
}
void overlayGraph_unregister(GraphHandle handle)
{
handle->enabled = false;
}

32
client/src/overlays.h Normal file
View File

@ -0,0 +1,32 @@
/**
* Looking Glass
* Copyright (C) 2017-2021 The Looking Glass Authors
* https://looking-glass.io
*
* This program is free software; you can redistribute it and/or modify it
* under the 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
*/
#ifndef _H_OVERLAYS_H
#define _H_OVERLAYS_H
#include "interface/overlay.h"
extern struct LG_OverlayOps LGOverlayFPS;
extern struct LG_OverlayOps LGOverlayGraphs;
GraphHandle overlayGraph_register(const char * name, RingBuffer buffer);
void overlayGraph_unregister();
#endif