diff --git a/src/overlay.cpp b/src/overlay.cpp index 7263f526..9ac2db76 100644 --- a/src/overlay.cpp +++ b/src/overlay.cpp @@ -817,26 +817,31 @@ void calculate_benchmark_data(void *params_void){ benchmark.total = benchmark.total + fps_; } + size_t max_label_size = 0; + for (std::string percentile : params->benchmark_percentiles) { float result; // special case handling for a mean-based average - if (percentile.find("AVG") == 0) { + if (percentile == "AVG") { result = benchmark.total / sorted.size(); - } - // everything else is used to calculate percentiles - else { - char *endChar; - float fraction = std::strtof(percentile.c_str(), &endChar) / 100; + } else { + // the percentiles are already validated when they're parsed from the config. + float fraction = parse_float(percentile) / 100; - if (*endChar != '%' || fraction <= 0.0 || fraction > 1.0) { - std::cerr << "MANGOHUD: Invalid percentile value for benchmark: '" << percentile << "'\n"; - continue; - } result = sorted[(fraction * sorted.size()) - 1]; + percentile += "%"; } + + if (percentile.length() > max_label_size) + max_label_size = percentile.length(); + benchmark.percentile_data.push_back({percentile, result}); } + + for (auto& entry : benchmark.percentile_data) { + entry.first.append(max_label_size - entry.first.length(), ' '); + } } void update_hud_info(struct swapchain_stats& sw_stats, struct overlay_params& params, uint32_t vendorID){ diff --git a/src/overlay_params.cpp b/src/overlay_params.cpp index 53def4be..2f9fc729 100644 --- a/src/overlay_params.cpp +++ b/src/overlay_params.cpp @@ -206,27 +206,38 @@ static std::vector parse_benchmark_percentiles(const char *str) { std::vector percentiles; - std::stringstream percentStrings(str); + std::stringstream percent_strings(str); std::string value; - size_t maxLength = 0; // for padding later on - - while (std::getline(percentStrings, value, '+')) { + while (std::getline(percent_strings, value, '+')) { trim(value); - // everything except AVG is assumed to be a percentile. - if (value != "AVG") - value += '%'; + if (value == "AVG") { + percentiles.push_back(value); + continue; + } - percentiles.push_back(value); + float as_float; + size_t float_len = 0; - if (value.length() > maxLength) - maxLength = value.length(); - } + try { + as_float = parse_float(value, &float_len); + } catch (const std::invalid_argument&) { + std::cerr << "MANGOHUD: invalid benchmark percentile: '" << value << "'\n"; + continue; + } - // now reiterate over the strings and add equal padding - for (size_t i = 0; i < percentiles.size(); i++) { - percentiles[i].append(maxLength - percentiles[i].length(), ' '); + if (float_len != value.length()) { + std::cerr << "MANGOHUD: invalid benchmark percentile: '" << value << "'\n"; + continue; + } + + if (as_float > 100 || as_float < 0) { + std::cerr << "MANGOHUD: benchmark percentile is not between 0 and 100 (" << value << ")\n"; + continue; + } + + percentiles.push_back(value); } return percentiles; @@ -423,7 +434,7 @@ parse_overlay_config(struct overlay_params *params, params->log_interval = 100; params->media_player_order = { MP_ORDER_TITLE, MP_ORDER_ARTIST, MP_ORDER_ALBUM }; params->permit_upload = 0; - params->benchmark_percentiles = { "97% ", "AVG ", "1% ", "0.1%" }; + params->benchmark_percentiles = { "97", "AVG", "1", "0.1" }; #ifdef HAVE_X11 params->toggle_hud = { XK_Shift_R, XK_F12 };