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.push_back({ "FreeRDP", freerdp });
206 _client.push_back({ "Build", sdl::overlay::buildSummary(freerdp_get_build_config()) });
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()) });
210}
211
212bool SdlOverlay::update(int windowWidth, int windowHeight, const char* rendererName)
213{
214 std::optional<sdl::overlay::Reading> reading;
215 std::chrono::steady_clock::time_point since;
216 bool restart = false;
217 {
218 std::scoped_lock lock(_mutex);
219 reading.swap(_pending);
220 since = _since;
221 restart = _restart;
222 _restart = false;
223 }
224 if (restart)
225 _prev.reset();
226 if (!reading || !_visible)
227 return false;
228
229 const auto& names = _names ? *_names : _names.emplace(statsNames());
230 if (_client.empty())
231 clientRows(rendererName);
232
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)
238 .count());
239
240 _left = _client;
241 _right = sdl::overlay::realtimeRows(names, *reading, _prev ? &*_prev : nullptr, view);
242 _prev = std::move(reading);
243 _generation++;
244 return true;
245}
246
247const std::vector<sdl::overlay::Row>& SdlOverlay::left() const
248{
249 return _left;
250}
251
252const std::vector<sdl::overlay::Row>& SdlOverlay::right() const
253{
254 return _right;
255}
256
257uint64_t SdlOverlay::generation() const
258{
259 return _generation;
260}
261
262SdlOverlayPainter::SdlOverlayPainter(SDL_Renderer* renderer)
263 : _renderer(renderer), _engine(TTF_CreateRendererTextEngine(renderer))
264{
265 if (!_engine)
266 WLog_WARN(TAG, "TTF_CreateRendererTextEngine: %s", SDL_GetError());
267}
268
269SdlOverlayPainter::~SdlOverlayPainter()
270{
271 clear();
272 if (_font)
273 TTF_CloseFont(_font);
274 if (_engine)
275 TTF_DestroyRendererTextEngine(_engine);
276}
277
278void SdlOverlayPainter::clear()
279{
280 for (auto* list : { &_left, &_right })
281 {
282 for (auto& cell : *list)
283 {
284 if (cell.label)
285 TTF_DestroyText(cell.label);
286 if (cell.value)
287 TTF_DestroyText(cell.value);
288 }
289 list->clear();
290 }
291}
292
293std::vector<SdlOverlayPainter::Cell>
294SdlOverlayPainter::cells(const std::vector<sdl::overlay::Row>& rows)
295{
296 std::vector<Cell> list;
297 for (const auto& row : rows)
298 {
299 Cell cell;
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 })
305 {
306 if (text)
307 std::ignore = TTF_SetTextColor(text, 0xFF, 0xFF, 0xFF, 0xFF);
308 }
309 /* the labels a little dimmer than the values */
310 if (cell.label)
311 std::ignore = TTF_SetTextColor(cell.label, 0xB8, 0xC4, 0xD0, 0xFF);
312 list.push_back(cell);
313 }
314 return list;
315}
316
317bool SdlOverlayPainter::rebuild(const SdlOverlay& overlay, float scale)
318{
319 if (!_engine)
320 return false;
321
322 constexpr float points = 13.0F;
323 if (!_font)
324 {
326 "OpenSans-VariableFont_wdth,wght.ttf");
327 if (!ops)
328 return false;
329 _font = TTF_OpenFontIO(ops, true, points * scale);
330 if (!_font)
331 {
332 WLog_WARN(TAG, "TTF_OpenFontIO: %s", SDL_GetError());
333 return false;
334 }
335 }
336 else if (!sameScale(scale, _scale))
337 std::ignore = TTF_SetFontSize(_font, points * scale);
338
339 clear();
340 _left = cells(overlay.left());
341 _right = cells(overlay.right());
342 _scale = scale;
343 _generation = overlay.generation();
344 return true;
345}
346
347SDL_FPoint SdlOverlayPainter::panelSize(const std::vector<Cell>& list, float& labelWidth) const
348{
349 labelWidth = 0;
350 float valueWidth = 0;
351 for (const auto& cell : list)
352 {
353 int w = 0;
354 int h = 0;
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));
359 }
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 };
365}
366
367void SdlOverlayPainter::drawPanel(const std::vector<Cell>& list, const sdl::overlay::Box& where)
368{
369 if (list.empty())
370 return;
371
372 float labelWidth = 0;
373 std::ignore = panelSize(list, labelWidth);
374 const float x = where.x;
375 const float y = where.y;
376
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);
380
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)
386 {
387 if (cell.label)
388 std::ignore = TTF_DrawRendererText(cell.label, x + pad, line);
389 if (cell.value)
390 std::ignore = TTF_DrawRendererText(cell.value, x + pad + labelWidth + gap, line);
391 line += skip;
392 }
393}
394
395void SdlOverlayPainter::draw(const SdlOverlay& overlay, int width, int height, float scale)
396{
397 if ((scale <= 0.0F) || (width <= 0) || (height <= 0))
398 return;
399 if ((overlay.generation() != _generation) || !sameScale(scale, _scale))
400 {
401 if (!rebuild(overlay, scale))
402 return;
403 }
404
405 SDL_BlendMode mode = SDL_BLENDMODE_NONE;
406 Uint8 r = 0;
407 Uint8 g = 0;
408 Uint8 b = 0;
409 Uint8 a = 0;
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);
413
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);
417 const auto where = sdl::overlay::layout(left.x, left.y, right.x, right.y,
418 static_cast<float>(width), 12.0F * _scale);
419 drawPanel(_left, where.left);
420 drawPanel(_right, where.right);
421
422 std::ignore = SDL_SetRenderDrawBlendMode(_renderer, mode);
423 std::ignore = SDL_SetRenderDrawColor(_renderer, r, g, b, a);
424}
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