26#include <freerdp/client.h>
27#include <freerdp/codecs.h>
28#include <freerdp/log.h>
30#include "sdl_overlay.hpp"
31#include "sdl_context.hpp"
32#include "sdl_utils.hpp"
33#include "dialogs/res/sdl3_resource_manager.hpp"
35#define TAG CLIENT_TAG("SDL.overlay")
38static constexpr uint64_t firstIntervalNS = 100ull * 1000ull * 1000ull;
39static constexpr uint64_t intervalNS = 1000ull * 1000ull * 1000ull;
43static std::vector<std::string> statsNames()
45 std::vector<std::string> names;
46 const size_t count = freerdp_client_stats_max_index();
48 for (
size_t x = 0; x < count; x++)
50 const char* name = freerdp_client_stats_name_for_index(x);
51 names.emplace_back(name ? name :
"");
59 const size_t count = freerdp_client_stats_max_index();
60 reading.values.resize(count);
61 for (
size_t x = 0; x < count; x++)
62 reading.values.at(x) = freerdp_client_stats_value_for_index(context, x);
65static std::string decodersSupported(rdpContext* context)
69 FreeRDP_CodecFlags flag;
73 const std::vector<Codec> codecs = {
74 { FREERDP_CODEC_AVC420,
"H.264 4:2:0" }, { FREERDP_CODEC_AVC444,
"H.264 4:4:4" },
75#if defined(WITH_GFX_AV1)
76 { FREERDP_CODEC_AV1_I420,
"AV1 4:2:0" }, { FREERDP_CODEC_AV1_I444,
"AV1 4:4:4" },
78#if defined(WITH_GFX_AV2)
79 { FREERDP_CODEC_AV2_I420,
"AV2 4:2:0" }, { FREERDP_CODEC_AV2_I444,
"AV2 4:4:4" },
81#if defined(WITH_GFX_AZURE)
82 { FREERDP_CODEC_HEVC_I420,
"HEVC" },
84 { FREERDP_CODEC_REMOTEFX,
"RemoteFX" }, { FREERDP_CODEC_PROGRESSIVE,
"Progressive" },
85 { FREERDP_CODEC_CLEARCODEC,
"ClearCodec" }, { FREERDP_CODEC_PLANAR,
"Planar" },
88 for (
const auto& codec : codecs)
90 if (!freerdp_codec_supported(context, codec.flag, FALSE))
92 list += list.empty() ? codec.label : std::string(
", ") + codec.label;
97static std::string version(
int v)
99 return std::to_string(SDL_VERSIONNUM_MAJOR(v)) +
"." + std::to_string(SDL_VERSIONNUM_MINOR(v)) +
100 "." + std::to_string(SDL_VERSIONNUM_MICRO(v));
104static bool sameScale(
float a,
float b)
106 return std::fabs(a - b) < 0.001F;
114bool SdlOverlay::visible()
const
122 if (!on || !_sdl->isConnected())
130 std::scoped_lock lock(_mutex);
131 _since = std::chrono::steady_clock::now();
140bool SdlOverlay::arm()
143 bool expected =
false;
144 if (!_armed.compare_exchange_strong(expected,
true))
147 if (freerdp_timer_add(_sdl->context(), firstIntervalNS, onTimer,
this,
true) == 0)
150 WLog_Print(_sdl->getWLog(), WLOG_ERROR,
"the overlay's timer could not start");
156uint64_t SdlOverlay::onTimer(rdpContext* context,
void* userdata,
157 WINPR_ATTR_UNUSED FreeRDP_TimerID timerID, uint64_t timestamp,
158 WINPR_ATTR_UNUSED uint64_t interval)
160 auto overlay =
static_cast<SdlOverlay*
>(userdata);
161 WINPR_ASSERT(overlay);
163 if (!overlay->_visible)
167 overlay->_armed =
false;
168 if (!overlay->_visible)
170 bool expected =
false;
171 if (!overlay->_armed.compare_exchange_strong(expected,
true))
175 if (!overlay->read(context, timestamp))
176 WLog_Print(overlay->_sdl->getWLog(), WLOG_WARN,
"the overlay's reading was not passed on");
180bool SdlOverlay::read(rdpContext* context, uint64_t timestamp)
182 WINPR_ASSERT(context);
184 sdl::overlay::Reading reading;
185 reading.
ns = timestamp;
188 statsRead(context, reading);
191 std::scoped_lock lock(_mutex);
192 _pending = std::move(reading);
194 return sdl_push_user_event(SDL_EVENT_USER_OVERLAY);
197void SdlOverlay::clientRows(
const char* rendererName)
200 std::string freerdp = freerdp_get_version_string();
201 const char* revision = freerdp_get_build_revision();
203 if (revision && (strlen(revision) > 0) && (strcmp(revision,
"n/a") != 0))
204 freerdp += std::string(
" (") + revision +
")";
205 _client.push_back({
"FreeRDP", freerdp });
207 _client.push_back({
"SDL", version(SDL_GetVersion()) +
", SDL_ttf " + version(TTF_Version()) +
208 ", renderer " + (rendererName ? rendererName :
"unknown") });
209 _client.push_back({
"Decoders", decodersSupported(_sdl->context()) });
214 std::optional<sdl::overlay::Reading> reading;
215 std::chrono::steady_clock::time_point since;
216 bool restart =
false;
218 std::scoped_lock lock(_mutex);
219 reading.swap(_pending);
226 if (!reading || !_visible)
229 const auto& names = _names ? *_names : _names.emplace(statsNames());
231 clientRows(rendererName);
234 view.windowWidth = windowWidth;
235 view.windowHeight = windowHeight;
236 view.sessionSeconds =
static_cast<uint64_t
>(
237 std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - since)
242 _prev = std::move(reading);
252const std::vector<sdl::overlay::Row>& SdlOverlay::right()
const
257uint64_t SdlOverlay::generation()
const
262SdlOverlayPainter::SdlOverlayPainter(SDL_Renderer* renderer)
263 : _renderer(renderer), _engine(TTF_CreateRendererTextEngine(renderer))
266 WLog_WARN(
TAG,
"TTF_CreateRendererTextEngine: %s", SDL_GetError());
269SdlOverlayPainter::~SdlOverlayPainter()
273 TTF_CloseFont(_font);
275 TTF_DestroyRendererTextEngine(_engine);
278void SdlOverlayPainter::clear()
280 for (
auto* list : { &_left, &_right })
282 for (
auto& cell : *list)
285 TTF_DestroyText(cell.label);
287 TTF_DestroyText(cell.value);
293std::vector<SdlOverlayPainter::Cell>
294SdlOverlayPainter::cells(
const std::vector<sdl::overlay::Row>& rows)
296 std::vector<Cell> list;
297 for (
const auto& row : rows)
300 if (!row.label.empty())
301 cell.label = TTF_CreateText(_engine, _font, row.label.c_str(), row.label.size());
302 if (!row.value.empty())
303 cell.value = TTF_CreateText(_engine, _font, row.value.c_str(), row.value.size());
304 for (
auto* text : { cell.label, cell.value })
307 std::ignore = TTF_SetTextColor(text, 0xFF, 0xFF, 0xFF, 0xFF);
311 std::ignore = TTF_SetTextColor(cell.label, 0xB8, 0xC4, 0xD0, 0xFF);
312 list.push_back(cell);
317bool SdlOverlayPainter::rebuild(
const SdlOverlay& overlay,
float scale)
322 constexpr float points = 13.0F;
326 "OpenSans-VariableFont_wdth,wght.ttf");
329 _font = TTF_OpenFontIO(ops,
true, points * scale);
332 WLog_WARN(
TAG,
"TTF_OpenFontIO: %s", SDL_GetError());
336 else if (!sameScale(scale, _scale))
337 std::ignore = TTF_SetFontSize(_font, points * scale);
340 _left = cells(overlay.
left());
341 _right = cells(overlay.right());
343 _generation = overlay.generation();
347SDL_FPoint SdlOverlayPainter::panelSize(
const std::vector<Cell>& list,
float& labelWidth)
const
350 float valueWidth = 0;
351 for (
const auto& cell : list)
355 if (cell.label && TTF_GetTextSize(cell.label, &w, &h))
356 labelWidth = std::max(labelWidth,
static_cast<float>(w));
357 if (cell.value && TTF_GetTextSize(cell.value, &w, &h))
358 valueWidth = std::max(valueWidth,
static_cast<float>(w));
360 const float gap = 12.0F * _scale;
361 const float pad = 8.0F * _scale;
362 const auto lines =
static_cast<float>(list.size());
363 return { pad + labelWidth + gap + valueWidth + pad,
364 pad + (lines *
static_cast<float>(TTF_GetFontLineSkip(_font))) + pad };
367void SdlOverlayPainter::drawPanel(
const std::vector<Cell>& list,
const sdl::overlay::Box& where)
372 float labelWidth = 0;
373 std::ignore = panelSize(list, labelWidth);
374 const float x = where.x;
375 const float y = where.y;
377 const SDL_FRect box = { where.x, where.y, where.w, where.h };
378 std::ignore = SDL_SetRenderDrawColor(_renderer, 0x10, 0x14, 0x18, 0xA8);
379 std::ignore = SDL_RenderFillRect(_renderer, &box);
381 const float gap = 12.0F * _scale;
382 const float pad = 8.0F * _scale;
383 const auto skip =
static_cast<float>(TTF_GetFontLineSkip(_font));
384 float line = y + pad;
385 for (
const auto& cell : list)
388 std::ignore = TTF_DrawRendererText(cell.label, x + pad, line);
390 std::ignore = TTF_DrawRendererText(cell.value, x + pad + labelWidth + gap, line);
397 if ((scale <= 0.0F) || (width <= 0) || (height <= 0))
399 if ((overlay.generation() != _generation) || !sameScale(scale, _scale))
401 if (!rebuild(overlay, scale))
405 SDL_BlendMode mode = SDL_BLENDMODE_NONE;
410 std::ignore = SDL_GetRenderDrawBlendMode(_renderer, &mode);
411 std::ignore = SDL_GetRenderDrawColor(_renderer, &r, &g, &b, &a);
412 std::ignore = SDL_SetRenderDrawBlendMode(_renderer, SDL_BLENDMODE_BLEND);
414 float labelWidth = 0;
415 const auto left = _left.empty() ? SDL_FPoint{ 0, 0 } : panelSize(_left, labelWidth);
416 const auto right = _right.empty() ? SDL_FPoint{ 0, 0 } : panelSize(_right, labelWidth);
418 static_cast<float>(width), 12.0F * _scale);
419 drawPanel(_left, where.left);
420 drawPanel(_right, where.right);
422 std::ignore = SDL_SetRenderDrawBlendMode(_renderer, mode);
423 std::ignore = SDL_SetRenderDrawColor(_renderer, r, g, b, a);
static SDL_IOStream * get(const std::string &type, const std::string &id)
static std::string typeFonts()
void draw(const SdlOverlay &overlay, int width, int height, float scale)
const std::vector< sdl::overlay::Row > & left() const
bool update(int windowWidth, int windowHeight, const char *rendererName)
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
Layout layout(float leftWidth, float leftHeight, float rightWidth, float rightHeight, float width, float margin)
std::vector< Row > realtimeRows(const Names &names, const Reading &now, const Reading *prev, const View &view)
std::string buildSummary(const char *config)