debug.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright (c) 2013 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/debug.h"
  5. #include <memory>
  6. #include <string>
  7. #include "ash/public/cpp/debug_utils.h"
  8. #include "ash/public/cpp/window_properties.h"
  9. #include "ash/root_window_controller.h"
  10. #include "ash/shell.h"
  11. #include "ash/wm/window_properties.h"
  12. #include "ash/wm/window_state.h"
  13. #include "ash/wm/window_util.h"
  14. #include "cc/debug/layer_tree_debug_state.h"
  15. #include "ui/accessibility/aura/aura_window_properties.h"
  16. #include "ui/aura/client/aura_constants.h"
  17. #include "ui/aura/window_tree_host.h"
  18. #include "ui/compositor/compositor.h"
  19. #include "ui/compositor/debug_utils.h"
  20. #include "ui/compositor/layer.h"
  21. #include "ui/views/debug_utils.h"
  22. #include "ui/views/widget/widget.h"
  23. namespace ash {
  24. namespace debug {
  25. void PrintLayerHierarchy(std::ostringstream* out) {
  26. for (aura::Window* root : Shell::Get()->GetAllRootWindows()) {
  27. ui::Layer* layer = root->layer();
  28. if (layer) {
  29. ui::PrintLayerHierarchy(
  30. layer,
  31. RootWindowController::ForWindow(root)->GetLastMouseLocationInRoot(),
  32. out);
  33. }
  34. }
  35. }
  36. void PrintViewHierarchy(std::ostringstream* out) {
  37. aura::Window* active_window = window_util::GetActiveWindow();
  38. if (!active_window)
  39. return;
  40. views::Widget* widget = views::Widget::GetWidgetForNativeView(active_window);
  41. if (!widget)
  42. return;
  43. views::PrintViewHierarchy(widget->GetRootView(), out);
  44. }
  45. void PrintWindowHierarchy(const aura::Window* active_window,
  46. const aura::Window* focused_window,
  47. const aura::Window* capture_window,
  48. aura::Window* window,
  49. int indent,
  50. bool scrub_data,
  51. std::vector<std::string>* out_window_titles,
  52. std::ostringstream* out) {
  53. std::string indent_str(indent, ' ');
  54. std::string name(window->GetName());
  55. if (name.empty())
  56. name = "\"\"";
  57. const gfx::Vector2dF& subpixel_position_offset =
  58. window->layer()->GetSubpixelOffset();
  59. *out << indent_str;
  60. *out << name << " (" << window << ")"
  61. << " type=" << window->GetType();
  62. int window_id = window->GetId();
  63. if (window_id != aura::Window::kInitialId)
  64. *out << " id=" << window_id;
  65. if (window->GetProperty(kWindowStateKey))
  66. *out << " " << WindowState::Get(window)->GetStateType();
  67. *out << ((window == active_window) ? " [active]" : "")
  68. << ((window == focused_window) ? " [focused]" : "")
  69. << ((window == capture_window) ? " [capture]" : "")
  70. << (window->GetTransparent() ? " [transparent]" : "")
  71. << (window->IsVisible() ? " [visible]" : "") << " "
  72. << (window->GetOcclusionState() != aura::Window::OcclusionState::UNKNOWN
  73. ? base::UTF16ToUTF8(aura::Window::OcclusionStateToString(
  74. window->GetOcclusionState()))
  75. .c_str()
  76. : "")
  77. << " " << window->bounds().ToString();
  78. if (!subpixel_position_offset.IsZero())
  79. *out << " subpixel offset=" + subpixel_position_offset.ToString();
  80. std::string* tree_id = window->GetProperty(ui::kChildAXTreeID);
  81. if (tree_id)
  82. *out << " ax_tree_id=" << *tree_id;
  83. std::u16string title(window->GetTitle());
  84. if (!title.empty()) {
  85. out_window_titles->push_back(base::UTF16ToUTF8(title));
  86. if (!scrub_data) {
  87. *out << " title=" << title;
  88. }
  89. }
  90. int app_type = window->GetProperty(aura::client::kAppType);
  91. *out << " app_type=" << app_type;
  92. std::string* pkg_name = window->GetProperty(ash::kArcPackageNameKey);
  93. if (pkg_name)
  94. *out << " pkg_name=" << *pkg_name;
  95. *out << '\n';
  96. for (aura::Window* child : window->children()) {
  97. PrintWindowHierarchy(active_window, focused_window, capture_window, child,
  98. indent + 3, scrub_data, out_window_titles, out);
  99. }
  100. }
  101. std::vector<std::string> PrintWindowHierarchy(std::ostringstream* out,
  102. bool scrub_data) {
  103. aura::Window* active_window = window_util::GetActiveWindow();
  104. aura::Window* focused_window = window_util::GetFocusedWindow();
  105. aura::Window* capture_window = window_util::GetCaptureWindow();
  106. aura::Window::Windows roots = Shell::Get()->GetAllRootWindows();
  107. std::vector<std::string> window_titles;
  108. for (size_t i = 0; i < roots.size(); ++i) {
  109. *out << "RootWindow " << i << ":\n";
  110. PrintWindowHierarchy(active_window, focused_window, capture_window,
  111. roots[i], 0, scrub_data, &window_titles, out);
  112. }
  113. return window_titles;
  114. }
  115. void ToggleShowDebugBorders() {
  116. aura::Window::Windows root_windows = Shell::Get()->GetAllRootWindows();
  117. std::unique_ptr<cc::DebugBorderTypes> value;
  118. for (auto* window : root_windows) {
  119. ui::Compositor* compositor = window->GetHost()->compositor();
  120. cc::LayerTreeDebugState state = compositor->GetLayerTreeDebugState();
  121. if (!value.get())
  122. value = std::make_unique<cc::DebugBorderTypes>(
  123. state.show_debug_borders.flip());
  124. state.show_debug_borders = *value.get();
  125. compositor->SetLayerTreeDebugState(state);
  126. }
  127. }
  128. void ToggleShowFpsCounter() {
  129. aura::Window::Windows root_windows = Shell::Get()->GetAllRootWindows();
  130. std::unique_ptr<bool> value;
  131. for (auto* window : root_windows) {
  132. ui::Compositor* compositor = window->GetHost()->compositor();
  133. cc::LayerTreeDebugState state = compositor->GetLayerTreeDebugState();
  134. if (!value.get())
  135. value = std::make_unique<bool>(!state.show_fps_counter);
  136. state.show_fps_counter = *value.get();
  137. compositor->SetLayerTreeDebugState(state);
  138. }
  139. }
  140. void ToggleShowPaintRects() {
  141. aura::Window::Windows root_windows = Shell::Get()->GetAllRootWindows();
  142. std::unique_ptr<bool> value;
  143. for (auto* window : root_windows) {
  144. ui::Compositor* compositor = window->GetHost()->compositor();
  145. cc::LayerTreeDebugState state = compositor->GetLayerTreeDebugState();
  146. if (!value.get())
  147. value = std::make_unique<bool>(!state.show_paint_rects);
  148. state.show_paint_rects = *value.get();
  149. compositor->SetLayerTreeDebugState(state);
  150. }
  151. }
  152. } // namespace debug
  153. } // namespace ash