FreeRDP
Loading...
Searching...
No Matches
sdl_overlay.cpp
1
19
20#include <algorithm>
21#include <array>
22#include <cmath>
23#include <cstring>
24#include <string>
25
26#include <freerdp/client.h>
27#include <freerdp/codecs.h>
28#include <freerdp/log.h>
29
30#include "sdl_overlay.hpp"
31#include "sdl_context.hpp"
32#include "sdl_utils.hpp"
33#include "dialogs/res/sdl3_resource_manager.hpp"
34
35#define TAG CLIENT_TAG("SDL.overlay")
36
37/* The first reading comes soon after the overlay shows, then one a second. */
38static constexpr uint64_t firstIntervalNS = 100ull * 1000ull * 1000ull;
39static constexpr uint64_t intervalNS = 1000ull * 1000ull * 1000ull;
40
41/* What the overlay reads of the libraries, all of it: the client's runtime statistics. Keep
42 * it the only place that knows them. */
43static std::vector<std::string> statsNames()
44{
45 std::vector<std::string> names;
46 const size_t count = freerdp_client_stats_max_index();
47 names.reserve(count);
48 for (size_t x = 0; x < count; x++)
49 {
50 const char* name = freerdp_client_stats_name_for_index(x);
51 names.emplace_back(name ? name : "");
52 }
53 return names;
54}
55
56/* On the session thread, which owns what is read */
57static void statsRead(rdpContext* context, sdl::overlay::Reading& reading)
58{
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);
63}
64
65static std::string decodersSupported(rdpContext* context)
66{
67 struct Codec
68 {
69 FreeRDP_CodecFlags flag;
70 const char* label;
71 };
72 /* the flags of codecs a build leaves out do not exist */
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" },
77#endif
78#if defined(WITH_GFX_AV2)
79 { FREERDP_CODEC_AV2_I420, "AV2 4:2:0" }, { FREERDP_CODEC_AV2_I444, "AV2 4:4:4" },
80#endif
81#if defined(WITH_GFX_AZURE)
82 { FREERDP_CODEC_HEVC_I420, "HEVC" },
83#endif
84 { FREERDP_CODEC_REMOTEFX, "RemoteFX" }, { FREERDP_CODEC_PROGRESSIVE, "Progressive" },
85 { FREERDP_CODEC_CLEARCODEC, "ClearCodec" }, { FREERDP_CODEC_PLANAR, "Planar" },
86 };
87 std::string list;
88 for (const auto& codec : codecs)
89 {
90 if (!freerdp_codec_supported(context, codec.flag, FALSE))
91 continue;
92 list += list.empty() ? codec.label : std::string(", ") + codec.label;
93 }
94 return list;
95}
96
97static std::string version(int v)
98{
99 return std::to_string(SDL_VERSIONNUM_MAJOR(v)) + "." + std::to_string(SDL_VERSIONNUM_MINOR(v)) +
100 "." + std::to_string(SDL_VERSIONNUM_MICRO(v));
101}
102
103/* a scale the font and the panels were made for: the same unless it really changed */
104static bool sameScale(float a, float b)
105{
106 return std::fabs(a - b) < 0.001F;
107}
108
109SdlOverlay::SdlOverlay(SdlContext* sdl) : _sdl(sdl)
110{
111 WINPR_ASSERT(_sdl);
112}
113
114bool SdlOverlay::visible() const
115{
116 return _visible;
117}
118
119bool SdlOverlay::show(bool on)
120{
121 _visible = on;
122 if (!on || !_sdl->isConnected())
123 return true;
124 return arm();
125}
126
128{
129 {
130 std::scoped_lock lock(_mutex);
131 _since = std::chrono::steady_clock::now();
132 _pending.reset();
133 _restart = true;
134 }
135 if (!_visible)
136 return true;
137 return arm();
138}
139
140bool SdlOverlay::arm()
141{
142 /* one timer at most: the one that sees _armed false adds it */
143 bool expected = false;
144 if (!_armed.compare_exchange_strong(expected, true))
145 return true;
146
147 if (freerdp_timer_add(_sdl->context(), firstIntervalNS, onTimer, this, true) == 0)
148 {
149 _armed = false;
150 WLog_Print(_sdl->getWLog(), WLOG_ERROR, "the overlay's timer could not start");
151 return false;
152 }
153 return true;
154}
155
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)
159{
160 auto overlay = static_cast<SdlOverlay*>(userdata);
161 WINPR_ASSERT(overlay);
162
163 if (!overlay->_visible)
164 {
165 /* Stop, unless it was shown again meanwhile: then this timer goes on, or the one a
166 * show added does (the compare exchange lets only one go on). */
167 overlay->_armed = false;
168 if (!overlay->_visible)
169 return 0;
170 bool expected = false;
171 if (!overlay->_armed.compare_exchange_strong(expected, true))
172 return 0;
173 }
174
175 if (!overlay->read(context, timestamp))
176 WLog_Print(overlay->_sdl->getWLog(), WLOG_WARN, "the overlay's reading was not passed on");
177 return intervalNS;
178}
179
180bool SdlOverlay::read(rdpContext* context, uint64_t timestamp)
181{
182 WINPR_ASSERT(context);
183
184 sdl::overlay::Reading reading;
185 reading.ns = timestamp;
186 reading.desktopWidth = freerdp_settings_get_uint32(context->settings, FreeRDP_DesktopWidth);
187 reading.desktopHeight = freerdp_settings_get_uint32(context->settings, FreeRDP_DesktopHeight);
188 statsRead(context, reading);
189
190 {
191 std::scoped_lock lock(_mutex);
192 _pending = std::move(reading);
193 }
194 return sdl_push_user_event(SDL_EVENT_USER_OVERLAY);
195}
196
197void SdlOverlay::clientRows(const char* rendererName)
198{
199 _client.clear();
200 std::string freerdp = freerdp_get_version_string();
201 const char* revision = freerdp_get_build_revision();
202 /* a build from outside a git tree says "n/a" */
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(
207 sdl::overlay::Row{ "Build", sdl::overlay::buildSummary(freerdp_get_build_config()) });
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()) });
212}
213
214bool SdlOverlay::update(int windowWidth, int windowHeight, const char* rendererName)
215{
216 std::optional<sdl::overlay::Reading> reading;
217 std::chrono::steady_clock::time_point since;
218 bool restart = false;
219 {
220 std::scoped_lock lock(_mutex);
221 reading.swap(_pending);
222 since = _since;
223 restart = _restart;
224 _restart = false;
225 }
226 if (restart)
227 _prev.reset();
228 if (!reading || !_visible)
229 return false;
230
231 const auto& names = _names ? *_names : _names.emplace(statsNames());
232 if (_client.empty())
233 clientRows(rendererName);
234
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)
240 .count());
241
242 _left = _client;
243 _right = sdl::overlay::realtimeRows(names, *reading, _prev ? &*_prev : nullptr, view);
244 _prev = std::move(reading);
245 _generation++;
246 return true;
247}
248
249const std::vector<sdl::overlay::Row>& SdlOverlay::left() const
250{
251 return _left;
252}
253
254const std::vector<sdl::overlay::Row>& SdlOverlay::right() const
255{
256 return _right;
257}
258
259uint64_t SdlOverlay::generation() const
260{
261 return _generation;
262}
263
264SdlOverlayPainter::SdlOverlayPainter(SDL_Renderer* renderer)
265 : _renderer(renderer), _engine(TTF_CreateRendererTextEngine(renderer))
266{
267 if (!_engine)
268 WLog_WARN(TAG, "TTF_CreateRendererTextEngine: %s", SDL_GetError());
269}
270
271SdlOverlayPainter::~SdlOverlayPainter()
272{
273 clear();
274 if (_font)
275 TTF_CloseFont(_font);
276 if (_engine)
277 TTF_DestroyRendererTextEngine(_engine);
278}
279
280void SdlOverlayPainter::clear()
281{
282 for (auto* list : { &_left, &_right })
283 {
284 for (auto& cell : *list)
285 {
286 if (cell.label)
287 TTF_DestroyText(cell.label);
288 if (cell.value)
289 TTF_DestroyText(cell.value);
290 }
291 list->clear();
292 }
293}
294
295std::vector<SdlOverlayPainter::Cell>
296SdlOverlayPainter::cells(const std::vector<sdl::overlay::Row>& rows)
297{
298 std::vector<Cell> list;
299 for (const auto& row : rows)
300 {
301 Cell cell;
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 })
307 {
308 if (text)
309 std::ignore = TTF_SetTextColor(text, 0xFF, 0xFF, 0xFF, 0xFF);
310 }
311 /* the labels a little dimmer than the values */
312 if (cell.label)
313 std::ignore = TTF_SetTextColor(cell.label, 0xB8, 0xC4, 0xD0, 0xFF);
314 list.push_back(cell);
315 }
316 return list;
317}
318
319bool SdlOverlayPainter::rebuild(const SdlOverlay& overlay, float scale)
320{
321 if (!_engine)
322 return false;
323
324 constexpr float points = 13.0F;
325 if (!_font)
326 {
328 "OpenSans-VariableFont_wdth,wght.ttf");
329 if (!ops)
330 return false;
331 _font = TTF_OpenFontIO(ops, true, points * scale);
332 if (!_font)
333 {
334 WLog_WARN(TAG, "TTF_OpenFontIO: %s", SDL_GetError());
335 return false;
336 }
337 }
338 else if (!sameScale(scale, _scale))
339 std::ignore = TTF_SetFontSize(_font, points * scale);
340
341 clear();
342 _left = cells(overlay.left());
343 _right = cells(overlay.right());
344 _scale = scale;
345 _generation = overlay.generation();
346 return true;
347}
348
349SDL_FPoint SdlOverlayPainter::panelSize(const std::vector<Cell>& list, float& labelWidth) const
350{
351 labelWidth = 0;
352 float valueWidth = 0;
353 for (const auto& cell : list)
354 {
355 int w = 0;
356 int h = 0;
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));
361 }
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 };
367}
368
369void SdlOverlayPainter::drawPanel(const std::vector<Cell>& list, const sdl::overlay::Box& where)
370{
371 if (list.empty())
372 return;
373
374 float labelWidth = 0;
375 std::ignore = panelSize(list, labelWidth);
376 const float x = where.x;
377 const float y = where.y;
378
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);
382
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)
388 {
389 if (cell.label)
390 std::ignore = TTF_DrawRendererText(cell.label, x + pad, line);
391 if (cell.value)
392 std::ignore = TTF_DrawRendererText(cell.value, x + pad + labelWidth + gap, line);
393 line += skip;
394 }
395}
396
397void SdlOverlayPainter::draw(const SdlOverlay& overlay, int width, int height, float scale)
398{
399 if ((scale <= 0.0F) || (width <= 0) || (height <= 0))
400 return;
401 if ((overlay.generation() != _generation) || !sameScale(scale, _scale))
402 {
403 if (!rebuild(overlay, scale))
404 return;
405 }
406
407 SDL_BlendMode mode = SDL_BLENDMODE_NONE;
408 Uint8 r = 0;
409 Uint8 g = 0;
410 Uint8 b = 0;
411 Uint8 a = 0;
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);
415
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);
419 const auto where = sdl::overlay::layout(left.x, left.y, right.x, right.y,
420 static_cast<float>(width), 12.0F * _scale);
421 drawPanel(_left, where.left);
422 drawPanel(_right, where.right);
423
424 std::ignore = SDL_SetRenderDrawBlendMode(_renderer, mode);
425 std::ignore = SDL_SetRenderDrawColor(_renderer, r, g, b, a);
426}
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)
bool connected()
bool show(bool on)
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)
#define TAG