diff --git a/client/decoder/android/android_decoder.cpp b/client/decoder/android/android_decoder.cpp index 27b99254..5fe7971a 100644 --- a/client/decoder/android/android_decoder.cpp +++ b/client/decoder/android/android_decoder.cpp @@ -591,7 +591,7 @@ void decoder::push_data(std::span> data, uint64_t frame push_nals(data, fake_timestamp_us, partial ? AMEDIACODEC_BUFFER_FLAG_PARTIAL_FRAME : 0); } -void decoder::frame_completed(xrt::drivers::wivrn::from_headset::feedback & feedback, const xrt::drivers::wivrn::to_headset::video_stream_data_shard::view_info_t & view_info) +void decoder::frame_completed(xrt::drivers::wivrn::from_headset::feedback & feedback, const xrt::drivers::wivrn::to_headset::video_stream_data_shard::timing_info_t & timing_info, const xrt::drivers::wivrn::to_headset::video_stream_data_shard::view_info_t & view_info) { if (not media_codec) { @@ -605,7 +605,11 @@ void decoder::frame_completed(xrt::drivers::wivrn::from_headset::feedback & feed // nothing required for decoder, mediacodec will callback when done feedback.sent_to_decoder = application::now(); - frame_infos.push(std::make_pair(feedback, view_info)); + frame_infos.push(frame_info{ + .feedback = feedback, + .timing_info = timing_info, + .view_info = view_info, + }); } void decoder::on_image_available(void * context, AImageReader * reader) @@ -634,9 +638,9 @@ void decoder::on_image_available(AImageReader * reader) check(AImage_getTimestamp(image, &fake_timestamp_ns), "AImage_getTimestamp"); uint64_t frame_index = (fake_timestamp_ns + 5'000'000) / (10'000'000); - frame_infos.drop_until([frame_index](auto & x) { return x.first.frame_index >= frame_index; }); + frame_infos.drop_until([frame_index](auto & x) { return x.feedback.frame_index >= frame_index; }); - auto info = frame_infos.pop_if([frame_index](auto & x) { return x.first.frame_index == frame_index; }); + auto info = frame_infos.pop_if([frame_index](auto & x) { return x.feedback.frame_index == frame_index; }); if (!info) { @@ -645,14 +649,13 @@ void decoder::on_image_available(AImageReader * reader) return; } - auto [feedback, view_info] = *info; - - feedback.received_from_decoder = application::now(); - assert(feedback.frame_index == frame_index); + info->feedback.received_from_decoder = application::now(); + assert(info->feedback.frame_index == frame_index); auto handle = std::make_shared(); - handle->feedback = feedback; - handle->view_info = view_info; + handle->feedback = info->feedback; + handle->timing_info = info->timing_info; + handle->view_info = info->view_info; handle->vk_data = map_hardware_buffer(image); handle->aimage = image; diff --git a/client/decoder/android/android_decoder.h b/client/decoder/android/android_decoder.h index 208cfd89..4aa0fe42 100644 --- a/client/decoder/android/android_decoder.h +++ b/client/decoder/android/android_decoder.h @@ -79,6 +79,7 @@ public: struct blit_handle { xrt::drivers::wivrn::from_headset::feedback feedback; + xrt::drivers::wivrn::to_headset::video_stream_data_shard::timing_info_t timing_info; xrt::drivers::wivrn::to_headset::video_stream_data_shard::view_info_t view_info; std::shared_ptr vk_data; @@ -108,7 +109,14 @@ private: utils::sync_queue input_buffers; utils::sync_queue output_buffers; - utils::sync_queue> frame_infos; + + struct frame_info + { + xrt::drivers::wivrn::from_headset::feedback feedback; + xrt::drivers::wivrn::to_headset::video_stream_data_shard::timing_info_t timing_info; + xrt::drivers::wivrn::to_headset::video_stream_data_shard::view_info_t view_info; + }; + utils::sync_queue frame_infos; std::thread output_releaser; static void on_media_error(AMediaCodec *, void * userdata, media_status_t error, int32_t actionCode, const char * detail); @@ -141,7 +149,7 @@ public: void push_data(std::span> data, uint64_t frame_index, bool partial); - void frame_completed(xrt::drivers::wivrn::from_headset::feedback &, const xrt::drivers::wivrn::to_headset::video_stream_data_shard::view_info_t & view_info); + void frame_completed(xrt::drivers::wivrn::from_headset::feedback & feedback, const xrt::drivers::wivrn::to_headset::video_stream_data_shard::timing_info_t & timing_info, const xrt::drivers::wivrn::to_headset::video_stream_data_shard::view_info_t & view_info); const auto & desc() const { diff --git a/client/decoder/ffmpeg/ffmpeg_decoder.cpp b/client/decoder/ffmpeg/ffmpeg_decoder.cpp index 3206ffd2..d6ed6d66 100644 --- a/client/decoder/ffmpeg/ffmpeg_decoder.cpp +++ b/client/decoder/ffmpeg/ffmpeg_decoder.cpp @@ -193,7 +193,7 @@ void decoder::push_data(std::span> data, uint64_t frame this->frame_index = frame_index; } -void decoder::frame_completed(const xrt::drivers::wivrn::from_headset::feedback & feedback, const xrt::drivers::wivrn::to_headset::video_stream_data_shard::view_info_t & view_info) +void decoder::frame_completed(const xrt::drivers::wivrn::from_headset::feedback & feedback, const xrt::drivers::wivrn::to_headset::video_stream_data_shard::timing_info_t & timing_info, const xrt::drivers::wivrn::to_headset::video_stream_data_shard::view_info_t & view_info) { spdlog::trace("ffmpeg decoder:frame_completed {}", frame_index); AVPacket packet{}; @@ -210,7 +210,7 @@ void decoder::frame_completed(const xrt::drivers::wivrn::from_headset::feedback if (res == AVERROR(EAGAIN)) { spdlog::warn("EAGAIN in avcodec_send_packet"); - frame_completed(feedback, view_info); + frame_completed(feedback, timing_info, view_info); } } if (res < 0) @@ -250,6 +250,7 @@ void decoder::frame_completed(const xrt::drivers::wivrn::from_headset::feedback auto handle = std::make_shared(); handle->feedback = feedback; + handle->timing_info = timing_info; handle->view_info = view_info; handle->image = decoded_images[index].image; handle->image_index = index; diff --git a/client/decoder/ffmpeg/ffmpeg_decoder.h b/client/decoder/ffmpeg/ffmpeg_decoder.h index b68a914c..14cc8bc2 100644 --- a/client/decoder/ffmpeg/ffmpeg_decoder.h +++ b/client/decoder/ffmpeg/ffmpeg_decoder.h @@ -93,7 +93,7 @@ public: void push_data(std::span> data, uint64_t frame_index, bool partial); - void frame_completed(const xrt::drivers::wivrn::from_headset::feedback &, const xrt::drivers::wivrn::to_headset::video_stream_data_shard::view_info_t & view_info); + void frame_completed(const xrt::drivers::wivrn::from_headset::feedback & feedback, const xrt::drivers::wivrn::to_headset::video_stream_data_shard::timing_info_t & timing_info, const xrt::drivers::wivrn::to_headset::video_stream_data_shard::view_info_t & view_info); const auto & desc() const { @@ -103,6 +103,7 @@ public: struct blit_handle { xrt::drivers::wivrn::from_headset::feedback feedback; + xrt::drivers::wivrn::to_headset::video_stream_data_shard::timing_info_t timing_info; xrt::drivers::wivrn::to_headset::video_stream_data_shard::view_info_t view_info; int image_index; VkImage image; diff --git a/client/decoder/shard_accumulator.cpp b/client/decoder/shard_accumulator.cpp index 7a4a3fdd..6621fc76 100644 --- a/client/decoder/shard_accumulator.cpp +++ b/client/decoder/shard_accumulator.cpp @@ -176,8 +176,13 @@ void shard_accumulator::try_submit_frame(uint16_t shard_idx) uint64_t frame_index = data_shards[0]->frame_idx; std::vector> payload; payload.reserve(data_shards.size()); + + data_shard::timing_info_t timing_info{}; for (const auto & shard: data_shards) { + if (shard->timing_info) + timing_info = *shard->timing_info; + payload.emplace_back(shard->payload); } decoder->push_data(payload, frame_index, false); @@ -191,7 +196,7 @@ void shard_accumulator::try_submit_frame(uint16_t shard_idx) feedback.received_pose = data_shards.front()->view_info->pose; // Try to extract a frame - decoder->frame_completed(feedback, *data_shards.front()->view_info); + decoder->frame_completed(feedback, timing_info, *data_shards.front()->view_info); advance(); } diff --git a/client/scenes/stream.cpp b/client/scenes/stream.cpp index 23e6e845..774f9748 100644 --- a/client/scenes/stream.cpp +++ b/client/scenes/stream.cpp @@ -36,7 +36,6 @@ #include #include "audio/audio.h" #include "hardware.h" -#include "implot.h" using namespace xrt::drivers::wivrn; @@ -289,158 +288,6 @@ std::shared_ptr scenes::stream::accumulator_imag return nullptr; } -scenes::stream::metric scenes::stream::get_metrics(XrTime predicted_display_time) -{ - uint64_t rx = network_session->bytes_received(); - uint64_t tx = network_session->bytes_sent(); - - float dt = (predicted_display_time - last_metric_time) * 1e-9f; - - bandwidth_rx = 0.8 * bandwidth_rx + 0.2 * float(rx - bytes_received) / dt; - bandwidth_tx = 0.8 * bandwidth_tx + 0.2 * float(tx - bytes_sent ) /dt; - - last_metric_time = predicted_display_time; - bytes_received = rx; - bytes_sent = tx; - - return metric{ - .cpu_time = application::get_cpu_time().count() * 1e-9f, - .gpu_time = application::get_gpu_time().count() * 1e-9f, - .bandwidth_rx = bandwidth_rx * 8, - .bandwidth_tx = bandwidth_tx * 8, - }; -} - -static float compute_plot_max_value(float * data, int count, ptrdiff_t stride) -{ - float max = 0; - uintptr_t ptr = (uintptr_t)data; - for(int i = 0; i < count; i++) - { - max = std::max(max, *(float*)(ptr + i * stride)); - } - - // First power of 10 less than the max - float x = pow(10, floor(log10(max))); - return ceil(max / x) * x; -} - -static std::pair compute_plot_unit(float max_value) -{ - if (max_value > 1e9) - return { 1e-9, "G" }; - if (max_value > 1e6) - return { 1e-6, "M" }; - if (max_value > 1e3) - return { 1e-3, "k" }; - if (max_value > 1) - return { 1, "" }; - if (max_value > 1e-3) - return { 1e3, "m" }; - if (max_value > 1e-6) - return { 1e6, "u" }; - return { 1e9, "n" }; -} - -struct getter_data -{ - uintptr_t data; - int stride; - int offset; - int count; - float multiplier; -}; - -static ImPlotPoint getter(int index, void * data_) -{ - getter_data& data = *(getter_data*)data_; - - int offset_index = (index + data.offset) % data.count; - - return ImPlotPoint(index, *(float*)(data.data + offset_index * data.stride) * data.multiplier); -} - -XrCompositionLayerQuad scenes::stream::plot_performance_metrics(XrTime predicted_display_time) -{ - imgui_ctx->new_frame(predicted_display_time); - const ImGuiStyle & style = ImGui::GetStyle(); - - ImGui::SetNextWindowPos({0, 0}); - ImGui::SetNextWindowSize(ImGui::GetMainViewport()->Size); - ImGui::Begin("Performance metrics", nullptr, - ImGuiWindowFlags_NoTitleBar | - ImGuiWindowFlags_NoResize | - ImGuiWindowFlags_NoMove); - - ImVec2 window_size = ImGui::GetWindowSize() - ImVec2(2,2) * style.WindowPadding; - - metrics[metrics_offset] = get_metrics(predicted_display_time); - metrics_offset = (metrics_offset + 1) % metrics.size(); - - static const std::array plots = { - std::make_tuple(ImPlot::GetColormapColor(0), "CPU time", "s", &metric::cpu_time), - std::make_tuple(ImPlot::GetColormapColor(1), "GPU time", "s", &metric::gpu_time), - std::make_tuple(ImPlot::GetColormapColor(2), "Download", "bit/s", &metric::bandwidth_rx), - std::make_tuple(ImPlot::GetColormapColor(3), "Upload", "bit/s", &metric::bandwidth_tx), - }; - - axis_scale.resize(plots.size()); - - int n_cols = 2; - int n_rows = ceil((float)plots.size() / n_cols); - - ImVec2 plot_size = ImVec2( - window_size.x / n_cols - style.ItemSpacing.x * (n_cols-1) / n_cols, - window_size.y / n_rows - style.ItemSpacing.y * (n_rows-1) / n_rows); - - ImPlot::PushStyleColor(ImPlotCol_PlotBg, IM_COL32(32, 32, 32, 64)); - ImPlot::PushStyleColor(ImPlotCol_FrameBg, IM_COL32(0, 0, 0, 0)); - ImPlot::PushStyleColor(ImPlotCol_AxisBg, IM_COL32(0, 0, 0, 0)); - ImPlot::PushStyleColor(ImPlotCol_AxisBgActive, IM_COL32(0, 0, 0, 0)); - ImPlot::PushStyleColor(ImPlotCol_AxisBgHovered, IM_COL32(0, 0, 0, 0)); - - int n = 0; - for(auto [color, title, unit, data]: plots) - { - if (ImPlot::BeginPlot(title, plot_size, ImPlotFlags_CanvasOnly|ImPlotFlags_NoChild)) - { - float min_v = 0; - float max_v = compute_plot_max_value(&(metrics.data()->*data), metrics.size(), sizeof(metric)); - auto [ multiplier, prefix ] = compute_plot_unit(max_v); - - if (axis_scale[n] == 0) - axis_scale[n] = max_v; - else - axis_scale[n] = 0.99 * axis_scale[n] + 0.01 * max_v; - - getter_data gdata{ - .data = (uintptr_t)&(metrics.data()->*data), - .stride = sizeof(metric), - .offset = metrics_offset, - .count = (int)metrics.size(), - .multiplier = multiplier - }; - - std::string title_with_units = std::string(title) + " [" + prefix + unit + "]"; - ImPlot::SetupAxes(nullptr, title_with_units.c_str(), ImPlotAxisFlags_NoDecorations, 0); - ImPlot::SetupAxesLimits(0, metrics.size() - 1, min_v * multiplier, axis_scale[n] * multiplier, ImGuiCond_Always); - ImPlot::SetNextLineStyle(color); - ImPlot::SetNextFillStyle(color, 0.25); - ImPlot::PlotLineG(title, getter, &gdata, metrics.size(), ImPlotLineFlags_Shaded); - // ImPlot::PlotLine(title, &(metrics.data()->*data), metrics.size(), 1, 0, ImPlotLineFlags_Shaded, metrics_offset, sizeof(metric)); - ImPlot::EndPlot(); - } - - if (++n % 2 != 0) - ImGui::SameLine(); - } - - ImPlot::PopStyleColor(5); - ImGui::End(); - - return imgui_ctx->end_frame(); -} - void scenes::stream::render(XrTime predicted_display_time, bool should_render) { if (exiting) @@ -620,7 +467,10 @@ void scenes::stream::render(XrTime predicted_display_time, bool should_render) XrCompositionLayerQuad imgui_layer; if (imgui_ctx) + { + accumulate_metrics(predicted_display_time, current_blit_handles); imgui_layer = plot_performance_metrics(predicted_display_time); + } layers_base.push_back(reinterpret_cast(&layer)); diff --git a/client/scenes/stream.h b/client/scenes/stream.h index 8fcf2fda..6bc34b5e 100644 --- a/client/scenes/stream.h +++ b/client/scenes/stream.h @@ -150,7 +150,7 @@ private: float bandwidth_rx = 0; float bandwidth_tx = 0; - struct metric + struct global_metric { float cpu_time = 0; float gpu_time = 0; @@ -158,12 +158,26 @@ private: float bandwidth_tx = 0; }; - std::vector metrics{300}; + struct decoder_metric + { + // All times are in seconds relative to encode_begin + float send_begin; + float send_end; + float received_first_packet; + float received_last_packet; + float sent_to_decoder; + float received_from_decoder; + float blitted; + float displayed; + }; + + std::vector global_metrics{300}; + std::vector> decoder_metrics; std::vector axis_scale; XrTime last_metric_time = 0; int metrics_offset = 0; - metric get_metrics(XrTime predicted_display_time); + void accumulate_metrics(XrTime predicted_display_time, const std::vector>& blit_handles); XrCompositionLayerQuad plot_performance_metrics(XrTime predicted_display_time); }; } // namespace scenes diff --git a/client/scenes/stream_stats.cpp b/client/scenes/stream_stats.cpp new file mode 100644 index 00000000..1718958e --- /dev/null +++ b/client/scenes/stream_stats.cpp @@ -0,0 +1,303 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2022 Guillaume Meunier + * Copyright (C) 2022 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "stream.h" + +#include "application.h" +#include "implot.h" +#include "utils/ranges.h" +#include +#include + +namespace +{ +float compute_plot_max_value(float * data, int count, ptrdiff_t stride) +{ + float max = 0; + uintptr_t ptr = (uintptr_t)data; + for(int i = 0; i < count; i++) + { + max = std::max(max, *(float*)(ptr + i * stride)); + } + + // First power of 10 less than the max + float x = pow(10, floor(log10(max))); + return ceil(max / x) * x; +} + +std::pair compute_plot_unit(float max_value) +{ + if (max_value > 1e9) + return { 1e-9, "G" }; + if (max_value > 1e6) + return { 1e-6, "M" }; + if (max_value > 1e3) + return { 1e-3, "k" }; + if (max_value > 1) + return { 1, "" }; + if (max_value > 1e-3) + return { 1e3, "m" }; + if (max_value > 1e-6) + return { 1e6, "u" }; + return { 1e9, "n" }; +} + +struct getter_data +{ + uintptr_t data; + int stride; + int offset; + int count; + float multiplier; +}; + +ImPlotPoint getter(int index, void * data_) +{ + getter_data& data = *(getter_data*)data_; + + int offset_index = (index + data.offset) % data.count; + + return ImPlotPoint(index, *(float*)(data.data + offset_index * data.stride) * data.multiplier); +} +} + +void scenes::stream::accumulate_metrics(XrTime predicted_display_time, const std::vector>& blit_handles) +{ + uint64_t rx = network_session->bytes_received(); + uint64_t tx = network_session->bytes_sent(); + + float dt = (predicted_display_time - last_metric_time) * 1e-9f; + + bandwidth_rx = 0.8 * bandwidth_rx + 0.2 * float(rx - bytes_received) / dt; + bandwidth_tx = 0.8 * bandwidth_tx + 0.2 * float(tx - bytes_sent ) /dt; + + last_metric_time = predicted_display_time; + bytes_received = rx; + bytes_sent = tx; + + global_metrics[metrics_offset] = global_metric{ + .cpu_time = application::get_cpu_time().count() * 1e-9f, + .gpu_time = application::get_gpu_time().count() * 1e-9f, + .bandwidth_rx = bandwidth_rx * 8, + .bandwidth_tx = bandwidth_tx * 8, + }; + + if (decoder_metrics.size() != blit_handles.size()) + decoder_metrics.resize(blit_handles.size()); + + uint64_t min_encode_begin = std::numeric_limits::max(); + for(const auto& bh: blit_handles) + min_encode_begin = std::min(min_encode_begin, bh->timing_info.encode_begin); + + for(auto&& [metrics, bh]: utils::zip(decoder_metrics, blit_handles)) + { + if (metrics.size() != global_metrics.size()) + metrics.resize(global_metrics.size()); + + metrics[metrics_offset] = decoder_metric{ + .send_begin = (bh->timing_info.send_begin - min_encode_begin) * 1e-9f, + .send_end = (bh->timing_info.send_end - min_encode_begin) * 1e-9f, + .received_first_packet = (bh->feedback.received_first_packet - min_encode_begin) * 1e-9f, + .received_last_packet = (bh->feedback.received_last_packet - min_encode_begin) * 1e-9f, + .sent_to_decoder = (bh->feedback.sent_to_decoder - min_encode_begin) * 1e-9f, + .received_from_decoder = (bh->feedback.received_from_decoder - min_encode_begin) * 1e-9f, + .blitted = (bh->feedback.blitted - min_encode_begin) * 1e-9f, + .displayed = (bh->feedback.displayed - min_encode_begin) * 1e-9f, + }; + } + + metrics_offset = (metrics_offset + 1) % global_metrics.size(); +} + +XrCompositionLayerQuad scenes::stream::plot_performance_metrics(XrTime predicted_display_time) +{ + imgui_ctx->new_frame(predicted_display_time); + const ImGuiStyle & style = ImGui::GetStyle(); + + ImGui::SetNextWindowPos({0, 0}); + ImGui::SetNextWindowSize(ImGui::GetMainViewport()->Size); + ImGui::Begin("Performance metrics", nullptr, + ImGuiWindowFlags_NoTitleBar | + ImGuiWindowFlags_NoResize | + ImGuiWindowFlags_NoMove); + + ImVec2 window_size = ImGui::GetWindowSize() - ImVec2(2,2) * style.WindowPadding; + + static const std::array plots = { + std::make_tuple("CPU time", "s", &global_metric::cpu_time), + std::make_tuple("GPU time", "s", &global_metric::gpu_time), + std::make_tuple("Download", "bit/s", &global_metric::bandwidth_rx), + std::make_tuple("Upload", "bit/s", &global_metric::bandwidth_tx), + }; + + int n_plots = plots.size() + decoders.size(); + axis_scale.resize(n_plots); + + int n_cols = 2; + int n_rows = ceil((float)n_plots / n_cols); + + ImVec2 plot_size = ImVec2( + window_size.x / n_cols - style.ItemSpacing.x * (n_cols-1) / n_cols, + window_size.y / n_rows - style.ItemSpacing.y * (n_rows-1) / n_rows); + + ImPlot::PushStyleColor(ImPlotCol_PlotBg, IM_COL32(32, 32, 32, 64)); + ImPlot::PushStyleColor(ImPlotCol_FrameBg, IM_COL32(0, 0, 0, 0)); + ImPlot::PushStyleColor(ImPlotCol_AxisBg, IM_COL32(0, 0, 0, 0)); + ImPlot::PushStyleColor(ImPlotCol_AxisBgActive, IM_COL32(0, 0, 0, 0)); + ImPlot::PushStyleColor(ImPlotCol_AxisBgHovered, IM_COL32(0, 0, 0, 0)); + + int n = 0; + for(auto [title, unit, data]: plots) + { + if (ImPlot::BeginPlot(title, plot_size, ImPlotFlags_CanvasOnly | ImPlotFlags_NoChild)) + { + float min_v = 0; + float max_v = compute_plot_max_value(&(global_metrics.data()->*data), global_metrics.size(), sizeof(global_metric)); + auto [ multiplier, prefix ] = compute_plot_unit(max_v); + + if (axis_scale[n] == 0) + axis_scale[n] = max_v; + else + axis_scale[n] = 0.99 * axis_scale[n] + 0.01 * max_v; + + getter_data gdata{ + .data = (uintptr_t)&(global_metrics.data()->*data), + .stride = sizeof(global_metric), + .offset = metrics_offset, + .count = (int)global_metrics.size(), + .multiplier = multiplier + }; + + auto color = ImPlot::GetColormapColor(n); + + std::string title_with_units = std::string(title) + " [" + prefix + unit + "]"; + ImPlot::SetupAxes(nullptr, title_with_units.c_str(), ImPlotAxisFlags_NoDecorations, 0); + ImPlot::SetupAxesLimits(0, global_metrics.size() - 1, min_v * multiplier, axis_scale[n] * multiplier, ImGuiCond_Always); + ImPlot::SetNextLineStyle(color); + ImPlot::SetNextFillStyle(color, 0.25); + ImPlot::PlotLineG(title, getter, &gdata, global_metrics.size(), ImPlotLineFlags_Shaded); + ImPlot::EndPlot(); + } + + if (++n % n_cols != 0) + ImGui::SameLine(); + } + + for(auto&& [index, metrics]: utils::enumerate(decoder_metrics)) + { + std::string title = "Decoder " + std::to_string(index); + if (ImPlot::BeginPlot(title.c_str(), plot_size, ImPlotFlags_CanvasOnly | ImPlotFlags_NoChild)) + { + float min_v = 0; + float max_v = compute_plot_max_value(&(metrics.data()->displayed), metrics.size(), sizeof(decoder_metric)); + // auto [ multiplier, prefix ] = compute_plot_unit(max_v); + + if (axis_scale[n] == 0) + axis_scale[n] = max_v; + else + axis_scale[n] = 0.99 * axis_scale[n] + 0.01 * max_v; + + const char * title_with_units = "Timings [ms]"; + ImPlot::SetupAxes(nullptr, title_with_units, ImPlotAxisFlags_NoDecorations, 0); + ImPlot::SetupAxesLimits(0, metrics.size() - 1, min_v * 1e3f, axis_scale[n] * 1e3f, ImGuiCond_Always); + + + getter_data getter_send_begin{ + .data = (uintptr_t)&(metrics.data()->send_begin), + .stride = sizeof(decoder_metric), + .offset = metrics_offset, + .count = (int)metrics.size(), + .multiplier = 1e3f + }; + + getter_data getter_send_end{ + .data = (uintptr_t)&(metrics.data()->send_end), + .stride = sizeof(decoder_metric), + .offset = metrics_offset, + .count = (int)metrics.size(), + .multiplier = 1e3f + }; + + getter_data getter_received_first_packet{ + .data = (uintptr_t)&(metrics.data()->received_first_packet), + .stride = sizeof(decoder_metric), + .offset = metrics_offset, + .count = (int)metrics.size(), + .multiplier = 1e3f + }; + + getter_data getter_received_last_packet{ + .data = (uintptr_t)&(metrics.data()->received_last_packet), + .stride = sizeof(decoder_metric), + .offset = metrics_offset, + .count = (int)metrics.size(), + .multiplier = 1e3f + }; + + getter_data getter_sent_to_decoder{ + .data = (uintptr_t)&(metrics.data()->sent_to_decoder), + .stride = sizeof(decoder_metric), + .offset = metrics_offset, + .count = (int)metrics.size(), + .multiplier = 1e3f + }; + + getter_data getter_received_from_decoder{ + .data = (uintptr_t)&(metrics.data()->received_from_decoder), + .stride = sizeof(decoder_metric), + .offset = metrics_offset, + .count = (int)metrics.size(), + .multiplier = 1e3f + }; + + getter_data getter_blitted{ + .data = (uintptr_t)&(metrics.data()->blitted), + .stride = sizeof(decoder_metric), + .offset = metrics_offset, + .count = (int)metrics.size(), + .multiplier = 1e3f + }; + + getter_data getter_displayed{ + .data = (uintptr_t)&(metrics.data()->displayed), + .stride = sizeof(decoder_metric), + .offset = metrics_offset, + .count = (int)metrics.size(), + .multiplier = 1e3f + }; + + ImPlot::PlotShadedG("Send", getter, &getter_send_begin, getter, &getter_send_end, metrics.size()); + ImPlot::PlotShadedG("Receive", getter, &getter_received_first_packet, getter, &getter_received_last_packet, metrics.size()); + ImPlot::PlotShadedG("Decode", getter, &getter_sent_to_decoder, getter, &getter_received_from_decoder, metrics.size()); + ImPlot::PlotLineG("Blitted", getter, &getter_blitted, metrics.size()); + ImPlot::PlotLineG("Displayed", getter, &getter_displayed, metrics.size()); + + ImPlot::EndPlot(); + } + + if (++n % n_cols != 0) + ImGui::SameLine(); + + } + + ImPlot::PopStyleColor(5); + ImGui::End(); + + return imgui_ctx->end_frame(); +}