From b8aefd8050b3ce406c1e5ce6c663bf9c68608e4d Mon Sep 17 00:00:00 2001 From: Tingwei LIU Date: Wed, 16 Jun 2021 00:30:19 +0200 Subject: [PATCH] [fixup] Make AnnotationTrack not inherit from Track --- src/OrbitGl/AnnotationTrack.cpp | 60 +++++++++------------- src/OrbitGl/AnnotationTrack.h | 50 ++++++++++--------- src/OrbitGl/GraphTrack.h | 2 +- src/OrbitGl/MemoryTrack.cpp | 20 ++++---- src/OrbitGl/MemoryTrack.h | 32 ++++++------ src/OrbitGl/TimeGraph.cpp | 88 ++++++++++++++++----------------- src/OrbitGl/Track.h | 1 - src/OrbitGl/TrackManager.cpp | 2 +- src/OrbitGl/TrackManager.h | 2 +- 9 files changed, 124 insertions(+), 133 deletions(-) diff --git a/src/OrbitGl/AnnotationTrack.cpp b/src/OrbitGl/AnnotationTrack.cpp index 6eb6dae898b..03ebb4a24d2 100644 --- a/src/OrbitGl/AnnotationTrack.cpp +++ b/src/OrbitGl/AnnotationTrack.cpp @@ -4,28 +4,33 @@ #include "AnnotationTrack.h" -#include "GlCanvas.h" +namespace { + +const Color kWhite(255, 255, 255, 255); +const Color kThresholdColor(244, 67, 54, 255); + +} // namespace void AnnotationTrack::DrawAnnotation(Batcher& batcher, TextRenderer& text_renderer, - PickingMode picking_mode, float z) { - if (picking_mode != PickingMode::kNone || collapse_toggle_->IsCollapsed()) return; + TimeGraphLayout* layout, float z) { + uint32_t font_size = layout->CalculateZoomedFontSize(); + Vec2 track_size = GetAnnotatedTrackSize(); + Vec2 track_pos = GetAnnotatedTrackPosition(); - uint32_t font_size = layout_->CalculateZoomedFontSize(); float content_right_x = - pos_[0] + size_[0] - layout_->GetRightMargin() - layout_->GetSliderWidth(); - float content_bottom_y = pos_[1] - size_[1] + layout_->GetTrackBottomMargin(); - float content_height = GetContentHeight(); + track_pos[0] + track_size[0] - layout->GetRightMargin() - layout->GetSliderWidth(); + float content_bottom_y = track_pos[1] - track_size[1] + layout->GetTrackBottomMargin(); + float content_height = GetAnnotatedTrackContentHeight(); // Add value upper bound text box (e.g., the "System Memory Total" text box for memory tracks). - const Color kWhite(255, 255, 255, 255); if (value_upper_bound_.has_value()) { std::string text = value_upper_bound_.value().first; float string_width = text_renderer.GetStringWidth(text.c_str(), font_size); - Vec2 text_box_size(string_width, layout_->GetTextBoxHeight()); + Vec2 text_box_size(string_width, layout->GetTextBoxHeight()); Vec2 text_box_position(content_right_x - text_box_size[0], content_bottom_y + content_height - text_box_size[1]); text_renderer.AddText(text.c_str(), text_box_position[0], - text_box_position[1] + layout_->GetTextOffset(), z, kWhite, font_size, + text_box_position[1] + layout->GetTextOffset(), z, kWhite, font_size, text_box_size[0]); } @@ -33,10 +38,10 @@ void AnnotationTrack::DrawAnnotation(Batcher& batcher, TextRenderer& text_render if (value_lower_bound_.has_value()) { std::string text = value_lower_bound_.value().first; float string_width = text_renderer.GetStringWidth(text.c_str(), font_size); - Vec2 text_box_size(string_width, layout_->GetTextBoxHeight()); + Vec2 text_box_size(string_width, layout->GetTextBoxHeight()); Vec2 text_box_position(content_right_x - text_box_size[0], content_bottom_y); text_renderer.AddText(text.c_str(), text_box_position[0], - text_box_position[1] + layout_->GetTextOffset(), z, kWhite, font_size, + text_box_position[1] + layout->GetTextOffset(), z, kWhite, font_size, text_box_size[0]); } @@ -46,40 +51,21 @@ void AnnotationTrack::DrawAnnotation(Batcher& batcher, TextRenderer& text_render double value_range = value_upper_bound_.value().second - value_lower_bound_.value().second; if (value_range <= 0) return; - const Color kThresholdColor(244, 67, 54, 255); double normalized_value = (warning_threshold_.value().second - value_lower_bound_.value().second) / value_range; float y = content_bottom_y + static_cast(normalized_value) * content_height; std::string text = warning_threshold_.value().first; float string_width = text_renderer.GetStringWidth(text.c_str(), font_size); - Vec2 text_box_size(string_width, layout_->GetTextBoxHeight()); - Vec2 text_box_position(pos_[0] + layout_->GetRightMargin(), y - text_box_size[1] / 2.f); + Vec2 text_box_size(string_width, layout->GetTextBoxHeight()); + Vec2 text_box_position(track_pos[0] + layout->GetRightMargin(), y - text_box_size[1] / 2.f); text_renderer.AddText(text.c_str(), text_box_position[0], - text_box_position[1] + layout_->GetTextOffset(), z, kThresholdColor, + text_box_position[1] + layout->GetTextOffset(), z, kThresholdColor, font_size, text_box_size[0]); - Vec2 from(pos_[0], y); - Vec2 to(pos_[0] + size_[0], y); - batcher.AddLine(from, from + Vec2(layout_->GetRightMargin() / 2.f, 0), z, kThresholdColor); + Vec2 from(track_pos[0], y); + Vec2 to(track_pos[0] + track_size[0], y); + batcher.AddLine(from, from + Vec2(layout->GetRightMargin() / 2.f, 0), z, kThresholdColor); batcher.AddLine(Vec2(text_box_position[0] + text_box_size[0], y), to, z, kThresholdColor); } -} - -void AnnotationTrack::SetWarningThresholdWhenEmpty(const std::string& pretty_label, - double raw_value) { - if (warning_threshold_.has_value()) return; - warning_threshold_ = std::make_pair(pretty_label, raw_value); -} - -void AnnotationTrack::SetValueUpperBoundWhenEmpty(const std::string& pretty_label, - double raw_value) { - if (value_upper_bound_.has_value()) return; - value_upper_bound_ = std::make_pair(pretty_label, raw_value); -} - -void AnnotationTrack::SetValueLowerBoundWhenEmpty(const std::string& pretty_label, - double raw_value) { - if (value_lower_bound_.has_value()) return; - value_lower_bound_ = std::make_pair(pretty_label, raw_value); } \ No newline at end of file diff --git a/src/OrbitGl/AnnotationTrack.h b/src/OrbitGl/AnnotationTrack.h index 5748e6b2a11..d41d20e3001 100644 --- a/src/OrbitGl/AnnotationTrack.h +++ b/src/OrbitGl/AnnotationTrack.h @@ -5,40 +5,46 @@ #ifndef ORBIT_GL_ANNOTATION_TRACK_H_ #define ORBIT_GL_ANNOTATION_TRACK_H_ -#include -#include #include #include #include "Batcher.h" #include "CoreMath.h" -#include "PickingManager.h" -#include "Timer.h" -#include "Track.h" -#include "Viewport.h" +#include "TextRenderer.h" +#include "TimeGraphLayout.h" -class AnnotationTrack : public virtual Track { +class AnnotationTrack { public: - explicit AnnotationTrack(CaptureViewElement* parent, TimeGraph* time_graph, - orbit_gl::Viewport* viewport, TimeGraphLayout* layout, - const orbit_client_model::CaptureData* capture_data, - uint32_t indentation_level = 0) - : Track(parent, time_graph, viewport, layout, capture_data, indentation_level) {} - - ~AnnotationTrack() override = default; - [[nodiscard]] Type GetType() const override { return Type::kAnnotationTrack; } - [[nodiscard]] virtual float GetContentHeight() const { - return size_[1] - layout_->GetTrackTabHeight() - layout_->GetTrackBottomMargin(); + explicit AnnotationTrack() = default; + + [[nodiscard]] std::optional> GetWarningThreshold() const { + return warning_threshold_; + } + [[nodiscard]] std::optional> GetValueUpperBound() const { + return value_upper_bound_; + } + [[nodiscard]] std::optional> GetValueLowerBound() const { + return value_lower_bound_; } - virtual void SetWarningThresholdWhenEmpty(const std::string& pretty_label, double raw_value); - virtual void SetValueUpperBoundWhenEmpty(const std::string& pretty_label, double raw_value); - virtual void SetValueLowerBoundWhenEmpty(const std::string& pretty_label, double raw_value); + virtual void SetWarningThreshold(const std::string& pretty_label, double raw_value) { + warning_threshold_ = std::make_pair(pretty_label, raw_value); + } + virtual void SetValueUpperBound(const std::string& pretty_label, double raw_value) { + value_upper_bound_ = std::make_pair(pretty_label, raw_value); + } + virtual void SetValueLowerBound(const std::string& pretty_label, double raw_value) { + value_lower_bound_ = std::make_pair(pretty_label, raw_value); + } - protected: - void DrawAnnotation(Batcher& batcher, TextRenderer& text_renderer, PickingMode picking_mode, + void DrawAnnotation(Batcher& batcher, TextRenderer& text_renderer, TimeGraphLayout* layout, float z); + private: + [[nodiscard]] virtual float GetAnnotatedTrackContentHeight() const = 0; + [[nodiscard]] virtual Vec2 GetAnnotatedTrackPosition() const = 0; + [[nodiscard]] virtual Vec2 GetAnnotatedTrackSize() const = 0; + std::optional> warning_threshold_ = std::nullopt; std::optional> value_upper_bound_ = std::nullopt; std::optional> value_lower_bound_ = std::nullopt; diff --git a/src/OrbitGl/GraphTrack.h b/src/OrbitGl/GraphTrack.h index 0df1600eca5..0b3b4fef0f4 100644 --- a/src/OrbitGl/GraphTrack.h +++ b/src/OrbitGl/GraphTrack.h @@ -19,7 +19,7 @@ #include "Viewport.h" template -class GraphTrack : public virtual Track { +class GraphTrack : public Track { public: explicit GraphTrack(CaptureViewElement* parent, TimeGraph* time_graph, orbit_gl::Viewport* viewport, TimeGraphLayout* layout, std::string name, diff --git a/src/OrbitGl/MemoryTrack.cpp b/src/OrbitGl/MemoryTrack.cpp index 566db011ab3..c7ab695c86f 100644 --- a/src/OrbitGl/MemoryTrack.cpp +++ b/src/OrbitGl/MemoryTrack.cpp @@ -16,28 +16,28 @@ void MemoryTrack::Draw(Batcher& batcher, TextRenderer& text_renderer, float z_offset) { GraphTrack::Draw(batcher, text_renderer, current_mouse_time_ns, picking_mode, z_offset); - AnnotationTrack::DrawAnnotation(batcher, text_renderer, picking_mode, + + if (picking_mode != PickingMode::kNone || this->collapse_toggle_->IsCollapsed()) return; + AnnotationTrack::DrawAnnotation(batcher, text_renderer, this->layout_, GlCanvas::kZValueTrackText + z_offset); } template -void MemoryTrack::SetWarningThresholdWhenEmpty(const std::string& pretty_label, - double raw_value) { - AnnotationTrack::SetWarningThresholdWhenEmpty(pretty_label, raw_value); +void MemoryTrack::SetWarningThreshold(const std::string& pretty_label, + double raw_value) { + AnnotationTrack::SetWarningThreshold(pretty_label, raw_value); GraphTrack::UpdateMinAndMax(raw_value); } template -void MemoryTrack::SetValueUpperBoundWhenEmpty(const std::string& pretty_label, - double raw_value) { - AnnotationTrack::SetValueUpperBoundWhenEmpty(pretty_label, raw_value); +void MemoryTrack::SetValueUpperBound(const std::string& pretty_label, double raw_value) { + AnnotationTrack::SetValueUpperBound(pretty_label, raw_value); GraphTrack::UpdateMinAndMax(raw_value); } template -void MemoryTrack::SetValueLowerBoundWhenEmpty(const std::string& pretty_label, - double raw_value) { - AnnotationTrack::SetValueLowerBoundWhenEmpty(pretty_label, raw_value); +void MemoryTrack::SetValueLowerBound(const std::string& pretty_label, double raw_value) { + AnnotationTrack::SetValueLowerBound(pretty_label, raw_value); GraphTrack::UpdateMinAndMax(raw_value); } diff --git a/src/OrbitGl/MemoryTrack.h b/src/OrbitGl/MemoryTrack.h index 57273cf4a45..a27b188df66 100644 --- a/src/OrbitGl/MemoryTrack.h +++ b/src/OrbitGl/MemoryTrack.h @@ -21,28 +21,30 @@ class MemoryTrack final : public GraphTrack, public AnnotationTrack { explicit MemoryTrack(CaptureViewElement* parent, TimeGraph* time_graph, orbit_gl::Viewport* viewport, TimeGraphLayout* layout, std::string name, std::array series_names, - const orbit_client_model::CaptureData* capture_data, - uint32_t indentation_level = 0) - : Track(parent, time_graph, viewport, layout, capture_data, indentation_level), - GraphTrack(parent, time_graph, viewport, layout, name, series_names, + const orbit_client_model::CaptureData* capture_data) + : GraphTrack(parent, time_graph, viewport, layout, name, series_names, capture_data), - AnnotationTrack(parent, time_graph, viewport, layout, capture_data, indentation_level) {} + AnnotationTrack() {} ~MemoryTrack() override = default; - [[nodiscard]] Type GetType() const override { return Type::kMemoryTrack; } - [[nodiscard]] float GetContentHeight() const override { - return size_[1] - layout_->GetTrackTabHeight() - layout_->GetTrackBottomMargin() - - this->GetLegendHeight(); - } + [[nodiscard]] Track::Type GetType() const override { return Track::Type::kMemoryTrack; } void Draw(Batcher& batcher, TextRenderer& text_renderer, uint64_t current_mouse_time_ns, PickingMode picking_mode, float z_offset = 0) override; - void SetWarningThresholdWhenEmpty(const std::string& pretty_label, double raw_value) override; - void SetValueUpperBoundWhenEmpty(const std::string& pretty_label, double raw_value) override; - void SetValueLowerBoundWhenEmpty(const std::string& pretty_label, double raw_value) override; + void SetWarningThreshold(const std::string& pretty_label, double raw_value) override; + void SetValueUpperBound(const std::string& pretty_label, double raw_value) override; + void SetValueLowerBound(const std::string& pretty_label, double raw_value) override; + + private: + float GetAnnotatedTrackContentHeight() const override { + return this->size_[1] - this->layout_->GetTrackTabHeight() - + this->layout_->GetTrackBottomMargin() - this->GetLegendHeight(); + } + Vec2 GetAnnotatedTrackPosition() const override { return this->pos_; }; + Vec2 GetAnnotatedTrackSize() const override { return this->size_; }; }; -constexpr size_t kSystemMemoryTrackSize = 3; -using SystemMemoryTrack = MemoryTrack; +constexpr size_t kSystemMemoryTrackDimension = 3; +using SystemMemoryTrack = MemoryTrack; } // namespace orbit_gl diff --git a/src/OrbitGl/TimeGraph.cpp b/src/OrbitGl/TimeGraph.cpp index 853c6c6fd96..9963805603c 100644 --- a/src/OrbitGl/TimeGraph.cpp +++ b/src/OrbitGl/TimeGraph.cpp @@ -423,61 +423,59 @@ void TimeGraph::ProcessValueTrackingTimer(const TimerInfo& timer_info) { } void TimeGraph::ProcessMemoryTrackingTimer(const TimerInfo& timer_info) { - const std::string kWarningThresholdLabel = "Production Limit"; - const std::string kValueUpperBoundLabel = "System Memory Total"; - const std::string kValueLowerBoundLabel = "Minimum: 0 GB"; - - constexpr double kValueLowerBoundRawValue = 0.0; - constexpr uint64_t kKilobytesToBytes = 1024; - constexpr double kMegabytesToKilobytes = 1024.0; - - uint64_t warning_threshold_kb = app_->GetMemoryWarningThresholdKb(); - std::string warning_threshold_pretty_size = - GetPrettySize(warning_threshold_kb * kKilobytesToBytes); - std::string warning_threshold_pretty_label = - absl::StrFormat("%s: %s", kWarningThresholdLabel, warning_threshold_pretty_size); - double warning_threshold_raw_value = - static_cast(warning_threshold_kb) / kMegabytesToKilobytes; - int64_t total_kb = orbit_api::Decode(timer_info.registers( static_cast(OrbitApp::SystemMemoryUsageEncodingIndex::kTotalKb))); - std::string total_pretty_label; - double total_raw_value = 0.0; - if (total_kb != kMissingInfo) { - std::string total_pretty_size = GetPrettySize(total_kb * kKilobytesToBytes); - total_pretty_label = absl::StrFormat("%s: %s", kValueUpperBoundLabel, total_pretty_size); - total_raw_value = static_cast(total_kb) / kMegabytesToKilobytes; - } - int64_t unused_kb = orbit_api::Decode( timer_info.registers(static_cast(OrbitApp::SystemMemoryUsageEncodingIndex::kFreeKb))); int64_t buffers_kb = orbit_api::Decode(timer_info.registers( static_cast(OrbitApp::SystemMemoryUsageEncodingIndex::kBuffersKb))); int64_t cached_kb = orbit_api::Decode(timer_info.registers( static_cast(OrbitApp::SystemMemoryUsageEncodingIndex::kCachedKb))); + if (total_kb == kMissingInfo && unused_kb == kMissingInfo && buffers_kb == kMissingInfo && + cached_kb == kMissingInfo) { + return; + } - if (total_kb != kMissingInfo && unused_kb != kMissingInfo && buffers_kb != kMissingInfo && - cached_kb != kMissingInfo) { - orbit_gl::SystemMemoryTrack* track = track_manager_->GetSystemMemoryTrack(); - if (track == nullptr) { - const std::array kSeriesNames = { - "Used", "Buffers / Cached", "Unused"}; - track = track_manager_->CreateAndGetSystemMemoryTrack(kSeriesNames); - } + constexpr double kMegabytesToKilobytes = 1024.0; + orbit_gl::SystemMemoryTrack* track = track_manager_->GetSystemMemoryTrack(); + if (track == nullptr) { + const std::array kSeriesNames = { + "Used", "Buffers / Cached", "Unused"}; + track = track_manager_->CreateAndGetSystemMemoryTrack(kSeriesNames); + } + double unused_mb = static_cast(unused_kb) / kMegabytesToKilobytes; + double buffers_or_cached_mb = static_cast(buffers_kb + cached_kb) / kMegabytesToKilobytes; + double used_mb = + static_cast(total_kb) / kMegabytesToKilobytes - unused_mb - buffers_or_cached_mb; + track->AddValues(timer_info.start(), {used_mb, buffers_or_cached_mb, unused_mb}); + track->OnTimer(timer_info); - track->SetValueUpperBoundWhenEmpty(total_pretty_label, total_raw_value); - track->SetValueLowerBoundWhenEmpty(kValueLowerBoundLabel, kValueLowerBoundRawValue); - if (absl::GetFlag(FLAGS_enable_warning_threshold)) { - track->SetWarningThresholdWhenEmpty(warning_threshold_pretty_label, - warning_threshold_raw_value); - } - double unused_mb = static_cast(unused_kb) / kMegabytesToKilobytes; - double buffers_or_cached_mb = - static_cast(buffers_kb + cached_kb) / kMegabytesToKilobytes; - double used_mb = - static_cast(total_kb) / kMegabytesToKilobytes - unused_mb - buffers_or_cached_mb; - track->AddValues(timer_info.start(), {used_mb, buffers_or_cached_mb, unused_mb}); - track->OnTimer(timer_info); + constexpr uint64_t kKilobytesToBytes = 1024; + if (!track->GetValueUpperBound().has_value()) { + const std::string kValueUpperBoundLabel = "System Memory Total"; + std::string total_pretty_size = GetPrettySize(total_kb * kKilobytesToBytes); + std::string total_pretty_label = + absl::StrFormat("%s: %s", kValueUpperBoundLabel, total_pretty_size); + double total_raw_value = static_cast(total_kb) / kMegabytesToKilobytes; + track->SetValueUpperBound(total_pretty_label, total_raw_value); + } + + if (!track->GetValueLowerBound().has_value()) { + const std::string kValueLowerBoundLabel = "Minimum: 0 GB"; + constexpr double kValueLowerBoundRawValue = 0.0; + track->SetValueLowerBound(kValueLowerBoundLabel, kValueLowerBoundRawValue); + } + + if (absl::GetFlag(FLAGS_enable_warning_threshold) && !track->GetWarningThreshold().has_value()) { + const std::string kWarningThresholdLabel = "Production Limit"; + uint64_t warning_threshold_kb = app_->GetMemoryWarningThresholdKb(); + std::string warning_threshold_pretty_size = + GetPrettySize(warning_threshold_kb * kKilobytesToBytes); + std::string warning_threshold_pretty_label = + absl::StrFormat("%s: %s", kWarningThresholdLabel, warning_threshold_pretty_size); + double warning_threshold_raw_value = + static_cast(warning_threshold_kb) / kMegabytesToKilobytes; + track->SetWarningThreshold(warning_threshold_pretty_label, warning_threshold_raw_value); } } diff --git a/src/OrbitGl/Track.h b/src/OrbitGl/Track.h index 47b95212f4b..9310333a4ef 100644 --- a/src/OrbitGl/Track.h +++ b/src/OrbitGl/Track.h @@ -39,7 +39,6 @@ class Track : public orbit_gl::CaptureViewElement, public std::enable_shared_fro kGraphTrack, kSchedulerTrack, kAsyncTrack, - kAnnotationTrack, kMemoryTrack, kUnknown, }; diff --git a/src/OrbitGl/TrackManager.cpp b/src/OrbitGl/TrackManager.cpp index 6594ef4fd8c..1b33f5f6a57 100644 --- a/src/OrbitGl/TrackManager.cpp +++ b/src/OrbitGl/TrackManager.cpp @@ -423,7 +423,7 @@ FrameTrack* TrackManager::GetOrCreateFrameTrack( } SystemMemoryTrack* TrackManager::CreateAndGetSystemMemoryTrack( - const std::array& series_names) { + const std::array& series_names) { std::lock_guard lock(mutex_); if (system_memory_track_ == nullptr) { constexpr uint8_t kTrackValueDecimalDigits = 2; diff --git a/src/OrbitGl/TrackManager.h b/src/OrbitGl/TrackManager.h index 148118b6b78..445b2a8d1d8 100644 --- a/src/OrbitGl/TrackManager.h +++ b/src/OrbitGl/TrackManager.h @@ -68,7 +68,7 @@ class TrackManager { FrameTrack* GetOrCreateFrameTrack(const orbit_grpc_protos::InstrumentedFunction& function); orbit_gl::SystemMemoryTrack* GetSystemMemoryTrack() const { return system_memory_track_.get(); } orbit_gl::SystemMemoryTrack* CreateAndGetSystemMemoryTrack( - const std::array& series_names); + const std::array& series_names); [[nodiscard]] bool GetIsDataFromSavedCapture() const { return data_from_saved_capture_; } void SetIsDataFromSavedCapture(bool value) { data_from_saved_capture_ = value; }