Skip to content

Commit

Permalink
[fixup] Make AnnotationTrack not inherit from Track
Browse files Browse the repository at this point in the history
  • Loading branch information
vickyliu-go4it committed Jun 16, 2021
1 parent b08bd6f commit b8aefd8
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 133 deletions.
60 changes: 23 additions & 37 deletions src/OrbitGl/AnnotationTrack.cpp
Expand Up @@ -4,39 +4,44 @@

#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]);
}

// Add value lower bound text box.
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]);
}

Expand All @@ -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<float>(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);
}
50 changes: 28 additions & 22 deletions src/OrbitGl/AnnotationTrack.h
Expand Up @@ -5,40 +5,46 @@
#ifndef ORBIT_GL_ANNOTATION_TRACK_H_
#define ORBIT_GL_ANNOTATION_TRACK_H_

#include <array>
#include <map>
#include <optional>
#include <string>

#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<std::pair<std::string, double>> GetWarningThreshold() const {
return warning_threshold_;
}
[[nodiscard]] std::optional<std::pair<std::string, double>> GetValueUpperBound() const {
return value_upper_bound_;
}
[[nodiscard]] std::optional<std::pair<std::string, double>> 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<std::pair<std::string, double>> warning_threshold_ = std::nullopt;
std::optional<std::pair<std::string, double>> value_upper_bound_ = std::nullopt;
std::optional<std::pair<std::string, double>> value_lower_bound_ = std::nullopt;
Expand Down
2 changes: 1 addition & 1 deletion src/OrbitGl/GraphTrack.h
Expand Up @@ -19,7 +19,7 @@
#include "Viewport.h"

template <size_t Dimension>
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,
Expand Down
20 changes: 10 additions & 10 deletions src/OrbitGl/MemoryTrack.cpp
Expand Up @@ -16,28 +16,28 @@ void MemoryTrack<Dimension>::Draw(Batcher& batcher, TextRenderer& text_renderer,
float z_offset) {
GraphTrack<Dimension>::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 <size_t Dimension>
void MemoryTrack<Dimension>::SetWarningThresholdWhenEmpty(const std::string& pretty_label,
double raw_value) {
AnnotationTrack::SetWarningThresholdWhenEmpty(pretty_label, raw_value);
void MemoryTrack<Dimension>::SetWarningThreshold(const std::string& pretty_label,
double raw_value) {
AnnotationTrack::SetWarningThreshold(pretty_label, raw_value);
GraphTrack<Dimension>::UpdateMinAndMax(raw_value);
}

template <size_t Dimension>
void MemoryTrack<Dimension>::SetValueUpperBoundWhenEmpty(const std::string& pretty_label,
double raw_value) {
AnnotationTrack::SetValueUpperBoundWhenEmpty(pretty_label, raw_value);
void MemoryTrack<Dimension>::SetValueUpperBound(const std::string& pretty_label, double raw_value) {
AnnotationTrack::SetValueUpperBound(pretty_label, raw_value);
GraphTrack<Dimension>::UpdateMinAndMax(raw_value);
}

template <size_t Dimension>
void MemoryTrack<Dimension>::SetValueLowerBoundWhenEmpty(const std::string& pretty_label,
double raw_value) {
AnnotationTrack::SetValueLowerBoundWhenEmpty(pretty_label, raw_value);
void MemoryTrack<Dimension>::SetValueLowerBound(const std::string& pretty_label, double raw_value) {
AnnotationTrack::SetValueLowerBound(pretty_label, raw_value);
GraphTrack<Dimension>::UpdateMinAndMax(raw_value);
}

Expand Down
32 changes: 17 additions & 15 deletions src/OrbitGl/MemoryTrack.h
Expand Up @@ -21,28 +21,30 @@ class MemoryTrack final : public GraphTrack<Dimension>, public AnnotationTrack {
explicit MemoryTrack(CaptureViewElement* parent, TimeGraph* time_graph,
orbit_gl::Viewport* viewport, TimeGraphLayout* layout, std::string name,
std::array<std::string, Dimension> 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<Dimension>(parent, time_graph, viewport, layout, name, series_names,
const orbit_client_model::CaptureData* capture_data)
: GraphTrack<Dimension>(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<kSystemMemoryTrackSize>;
constexpr size_t kSystemMemoryTrackDimension = 3;
using SystemMemoryTrack = MemoryTrack<kSystemMemoryTrackDimension>;

} // namespace orbit_gl

Expand Down
88 changes: 43 additions & 45 deletions src/OrbitGl/TimeGraph.cpp
Expand Up @@ -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<double>(warning_threshold_kb) / kMegabytesToKilobytes;

int64_t total_kb = orbit_api::Decode<int64_t>(timer_info.registers(
static_cast<size_t>(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<double>(total_kb) / kMegabytesToKilobytes;
}

int64_t unused_kb = orbit_api::Decode<int64_t>(
timer_info.registers(static_cast<size_t>(OrbitApp::SystemMemoryUsageEncodingIndex::kFreeKb)));
int64_t buffers_kb = orbit_api::Decode<int64_t>(timer_info.registers(
static_cast<size_t>(OrbitApp::SystemMemoryUsageEncodingIndex::kBuffersKb)));
int64_t cached_kb = orbit_api::Decode<int64_t>(timer_info.registers(
static_cast<size_t>(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<std::string, orbit_gl::kSystemMemoryTrackSize> 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<std::string, orbit_gl::kSystemMemoryTrackDimension> kSeriesNames = {
"Used", "Buffers / Cached", "Unused"};
track = track_manager_->CreateAndGetSystemMemoryTrack(kSeriesNames);
}
double unused_mb = static_cast<double>(unused_kb) / kMegabytesToKilobytes;
double buffers_or_cached_mb = static_cast<double>(buffers_kb + cached_kb) / kMegabytesToKilobytes;
double used_mb =
static_cast<double>(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<double>(unused_kb) / kMegabytesToKilobytes;
double buffers_or_cached_mb =
static_cast<double>(buffers_kb + cached_kb) / kMegabytesToKilobytes;
double used_mb =
static_cast<double>(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<double>(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<double>(warning_threshold_kb) / kMegabytesToKilobytes;
track->SetWarningThreshold(warning_threshold_pretty_label, warning_threshold_raw_value);
}
}

Expand Down
1 change: 0 additions & 1 deletion src/OrbitGl/Track.h
Expand Up @@ -39,7 +39,6 @@ class Track : public orbit_gl::CaptureViewElement, public std::enable_shared_fro
kGraphTrack,
kSchedulerTrack,
kAsyncTrack,
kAnnotationTrack,
kMemoryTrack,
kUnknown,
};
Expand Down
2 changes: 1 addition & 1 deletion src/OrbitGl/TrackManager.cpp
Expand Up @@ -423,7 +423,7 @@ FrameTrack* TrackManager::GetOrCreateFrameTrack(
}

SystemMemoryTrack* TrackManager::CreateAndGetSystemMemoryTrack(
const std::array<std::string, orbit_gl::kSystemMemoryTrackSize>& series_names) {
const std::array<std::string, orbit_gl::kSystemMemoryTrackDimension>& series_names) {
std::lock_guard<std::recursive_mutex> lock(mutex_);
if (system_memory_track_ == nullptr) {
constexpr uint8_t kTrackValueDecimalDigits = 2;
Expand Down

0 comments on commit b8aefd8

Please sign in to comment.