graphs_container_view.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // Copyright 2020 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/hud_display/graphs_container_view.h"
  5. #include <numeric>
  6. #include "ash/hud_display/cpu_graph_page_view.h"
  7. #include "ash/hud_display/fps_graph_page_view.h"
  8. #include "ash/hud_display/hud_constants.h"
  9. #include "ash/hud_display/memory_graph_page_view.h"
  10. #include "base/bind.h"
  11. #include "base/task/thread_pool.h"
  12. #include "base/threading/thread_task_runner_handle.h"
  13. #include "ui/base/metadata/metadata_impl_macros.h"
  14. #include "ui/views/layout/fill_layout.h"
  15. namespace ash {
  16. namespace hud_display {
  17. namespace {
  18. // UI refresh interval.
  19. constexpr base::TimeDelta kGraphsDataRefreshInterval = base::Milliseconds(500);
  20. void GetDataSnapshotOnThreadPool(DataSource* data_source,
  21. DataSource::Snapshot* out_snapshot) {
  22. // This is run on the ThreadPool.
  23. *out_snapshot = data_source->GetSnapshotAndReset();
  24. }
  25. } // namespace
  26. ////////////////////////////////////////////////////////////////////////////////
  27. // GraphsContainerView, public:
  28. BEGIN_METADATA(GraphsContainerView, views::View)
  29. END_METADATA
  30. GraphsContainerView::GraphsContainerView()
  31. : start_time_(base::TimeTicks::Now()),
  32. file_task_runner_(base::ThreadPool::CreateSequencedTaskRunner(
  33. {base::MayBlock(), base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN})),
  34. data_source_(new DataSource,
  35. base::OnTaskRunnerDeleter(file_task_runner_)) {
  36. DCHECK_CALLED_ON_VALID_SEQUENCE(ui_sequence_checker_);
  37. // Make all graph pages take the whole view and make sure that only one
  38. // is shown at a time.
  39. SetLayoutManager(std::make_unique<views::FillLayout>());
  40. // Adds another graphs page.
  41. AddChildView(
  42. std::make_unique<MemoryGraphPageView>(kGraphsDataRefreshInterval))
  43. ->SetID(static_cast<int>(HUDDisplayMode::MEMORY));
  44. AddChildView(std::make_unique<CpuGraphPageView>(kGraphsDataRefreshInterval))
  45. ->SetID(static_cast<int>(HUDDisplayMode::CPU));
  46. AddChildView(std::make_unique<FPSGraphPageView>(kGraphsDataRefreshInterval))
  47. ->SetID(static_cast<int>(HUDDisplayMode::FPS));
  48. RequestDataUpdate();
  49. }
  50. GraphsContainerView::~GraphsContainerView() {
  51. DCHECK_CALLED_ON_VALID_SEQUENCE(ui_sequence_checker_);
  52. }
  53. void GraphsContainerView::RequestDataUpdate() {
  54. std::unique_ptr<DataSource::Snapshot> snapshot_container =
  55. std::make_unique<DataSource::Snapshot>();
  56. DataSource::Snapshot* snapshot = snapshot_container.get();
  57. file_task_runner_->PostTaskAndReply(
  58. FROM_HERE,
  59. base::BindOnce(&GetDataSnapshotOnThreadPool,
  60. base::Unretained(data_source_.get()), snapshot),
  61. base::BindOnce(&GraphsContainerView::UpdateData,
  62. weak_factory_.GetWeakPtr(),
  63. std::move(snapshot_container)));
  64. }
  65. void GraphsContainerView::UpdateData(
  66. std::unique_ptr<DataSource::Snapshot> snapshot) {
  67. // Adjust for any missing data.
  68. const off_t expected_updates =
  69. (base::TimeTicks::Now() - start_time_) / kGraphsDataRefreshInterval;
  70. const unsigned intervals =
  71. expected_updates > static_cast<off_t>(data_update_count_)
  72. ? expected_updates - data_update_count_
  73. : 1;
  74. data_update_count_ += intervals;
  75. for (auto* child : children()) {
  76. // Insert missing points.
  77. for (unsigned j = 0; j < intervals; ++j)
  78. static_cast<GraphPageViewBase*>(child)->UpdateData(*snapshot);
  79. }
  80. SchedulePaint();
  81. const base::TimeTicks next_start_time =
  82. start_time_ + kGraphsDataRefreshInterval * data_update_count_;
  83. const base::TimeTicks now = base::TimeTicks::Now();
  84. if (next_start_time <= now) {
  85. RequestDataUpdate();
  86. } else {
  87. base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
  88. FROM_HERE,
  89. base::BindOnce(&GraphsContainerView::RequestDataUpdate,
  90. weak_factory_.GetWeakPtr()),
  91. next_start_time - now);
  92. }
  93. }
  94. void GraphsContainerView::SetMode(HUDDisplayMode mode) {
  95. auto* selected = GetViewByID(static_cast<int>(mode));
  96. if (!selected) {
  97. DCHECK(selected);
  98. return;
  99. }
  100. for (auto* child : children())
  101. child->SetVisible(false);
  102. selected->SetVisible(true);
  103. }
  104. } // namespace hud_display
  105. } // namespace ash