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.emplace_back(sdl::overlay::Row{
"FreeRDP", freerdp });
206 _client.emplace_back(
208 _client.emplace_back(sdl::overlay::Row{
"SDL", version(SDL_GetVersion()) +
", SDL_ttf " +
209 version(TTF_Version()) +
", renderer " +
210 (rendererName ? rendererName :
"unknown") });
211 _client.emplace_back(sdl::overlay::Row{
"Decoders", decodersSupported(_sdl->context()) });
216 std::optional<sdl::overlay::Reading> reading;
217 std::chrono::steady_clock::time_point since;
218 bool restart =
false;
220 std::scoped_lock lock(_mutex);
221 reading.swap(_pending);
228 if (!reading || !_visible)
231 const auto& names = _names ? *_names : _names.emplace(statsNames());
233 clientRows(rendererName);
236 view.windowWidth = windowWidth;
237 view.windowHeight = windowHeight;
238 view.sessionSeconds =
static_cast<uint64_t
>(
239 std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - since)
244 _prev = std::move(reading);
254const std::vector<sdl::overlay::Row>& SdlOverlay::right()
const
259uint64_t SdlOverlay::generation()
const
264SdlOverlayPainter::SdlOverlayPainter(SDL_Renderer* renderer)
265 : _renderer(renderer), _engine(TTF_CreateRendererTextEngine(renderer))
268 WLog_WARN(
TAG,
"TTF_CreateRendererTextEngine: %s", SDL_GetError());
271SdlOverlayPainter::~SdlOverlayPainter()
275 TTF_CloseFont(_font);
277 TTF_DestroyRendererTextEngine(_engine);
280void SdlOverlayPainter::clear()
282 for (
auto* list : { &_left, &_right })
284 for (
auto& cell : *list)
287 TTF_DestroyText(cell.label);
289 TTF_DestroyText(cell.value);
295std::vector<SdlOverlayPainter::Cell>
296SdlOverlayPainter::cells(
const std::vector<sdl::overlay::Row>& rows)
298 std::vector<Cell> list;
299 for (
const auto& row : rows)
302 if (!row.label.empty())
303 cell.label = TTF_CreateText(_engine, _font, row.label.c_str(), row.label.size());
304 if (!row.value.empty())
305 cell.value = TTF_CreateText(_engine, _font, row.value.c_str(), row.value.size());
306 for (
auto* text : { cell.label, cell.value })
309 std::ignore = TTF_SetTextColor(text, 0xFF, 0xFF, 0xFF, 0xFF);
313 std::ignore = TTF_SetTextColor(cell.label, 0xB8, 0xC4, 0xD0, 0xFF);
314 list.push_back(cell);
319bool SdlOverlayPainter::rebuild(
const SdlOverlay& overlay,
float scale)
324 constexpr float points = 13.0F;
328 "OpenSans-VariableFont_wdth,wght.ttf");
331 _font = TTF_OpenFontIO(ops,
true, points * scale);
334 WLog_WARN(
TAG,
"TTF_OpenFontIO: %s", SDL_GetError());
338 else if (!sameScale(scale, _scale))
339 std::ignore = TTF_SetFontSize(_font, points * scale);
342 _left = cells(overlay.
left());
343 _right = cells(overlay.right());
345 _generation = overlay.generation();
349SDL_FPoint SdlOverlayPainter::panelSize(
const std::vector<Cell>& list,
float& labelWidth)
const
352 float valueWidth = 0;
353 for (
const auto& cell : list)
357 if (cell.label && TTF_GetTextSize(cell.label, &w, &h))
358 labelWidth = std::max(labelWidth,
static_cast<float>(w));
359 if (cell.value && TTF_GetTextSize(cell.value, &w, &h))
360 valueWidth = std::max(valueWidth,
static_cast<float>(w));
362 const float gap = 12.0F * _scale;
363 const float pad = 8.0F * _scale;
364 const auto lines =
static_cast<float>(list.size());
365 return { pad + labelWidth + gap + valueWidth + pad,
366 pad + (lines *
static_cast<float>(TTF_GetFontLineSkip(_font))) + pad };
369void SdlOverlayPainter::drawPanel(
const std::vector<Cell>& list,
const sdl::overlay::Box& where)
374 float labelWidth = 0;
375 std::ignore = panelSize(list, labelWidth);
376 const float x = where.x;
377 const float y = where.y;
379 const SDL_FRect box = { where.x, where.y, where.w, where.h };
380 std::ignore = SDL_SetRenderDrawColor(_renderer, 0x10, 0x14, 0x18, 0xA8);
381 std::ignore = SDL_RenderFillRect(_renderer, &box);
383 const float gap = 12.0F * _scale;
384 const float pad = 8.0F * _scale;
385 const auto skip =
static_cast<float>(TTF_GetFontLineSkip(_font));
386 float line = y + pad;
387 for (
const auto& cell : list)
390 std::ignore = TTF_DrawRendererText(cell.label, x + pad, line);
392 std::ignore = TTF_DrawRendererText(cell.value, x + pad + labelWidth + gap, line);
399 if ((scale <= 0.0F) || (width <= 0) || (height <= 0))
401 if ((overlay.generation() != _generation) || !sameScale(scale, _scale))
403 if (!rebuild(overlay, scale))
407 SDL_BlendMode mode = SDL_BLENDMODE_NONE;
412 std::ignore = SDL_GetRenderDrawBlendMode(_renderer, &mode);
413 std::ignore = SDL_GetRenderDrawColor(_renderer, &r, &g, &b, &a);
414 std::ignore = SDL_SetRenderDrawBlendMode(_renderer, SDL_BLENDMODE_BLEND);
416 float labelWidth = 0;
417 const auto left = _left.empty() ? SDL_FPoint{ 0, 0 } : panelSize(_left, labelWidth);
418 const auto right = _right.empty() ? SDL_FPoint{ 0, 0 } : panelSize(_right, labelWidth);
420 static_cast<float>(width), 12.0F * _scale);
421 drawPanel(_left, where.left);
422 drawPanel(_right, where.right);
424 std::ignore = SDL_SetRenderDrawBlendMode(_renderer, mode);
425 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)