debug.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. *out << "Host widget:\n";
  44. views::PrintWidgetInformation(*widget, /*detailed*/ true, out);
  45. views::PrintViewHierarchy(widget->GetRootView(), out);
  46. }
  47. void PrintWindowHierarchy(const aura::Window* active_window,
  48. const aura::Window* focused_window,
  49. const aura::Window* capture_window,
  50. aura::Window* window,
  51. int indent,
  52. bool scrub_data,
  53. std::vector<std::string>* out_window_titles,
  54. std::ostringstream* out) {
  55. std::string indent_str(indent, ' ');
  56. std::string name(window->GetName());
  57. if (name.empty())
  58. name = "\"\"";
  59. const gfx::Vector2dF& subpixel_position_offset =
  60. window->layer()->GetSubpixelOffset();
  61. *out << indent_str;
  62. *out << " [window]";
  63. *out << " " << name << " (" << window << ")"
  64. << " type=" << window->GetType();
  65. int window_id = window->GetId();
  66. if (window_id != aura::Window::kInitialId)
  67. *out << " id=" << window_id;
  68. if (window->GetProperty(kWindowStateKey))
  69. *out << " " << WindowState::Get(window)->GetStateType();
  70. *out << ((window == active_window) ? " [active]" : "")
  71. << ((window == focused_window) ? " [focused]" : "")
  72. << ((window == capture_window) ? " [capture]" : "")
  73. << (window->GetTransparent() ? " [transparent]" : "")
  74. << (window->IsVisible() ? " [visible]" : "") << " "
  75. << (window->GetOcclusionState() != aura::Window::OcclusionState::UNKNOWN
  76. ? base::UTF16ToUTF8(aura::Window::OcclusionStateToString(
  77. window->GetOcclusionState()))
  78. .c_str()
  79. : "")
  80. << " " << window->bounds().ToString();
  81. if (!subpixel_position_offset.IsZero())
  82. *out << " subpixel offset=" + subpixel_position_offset.ToString();
  83. std::string* tree_id = window->GetProperty(ui::kChildAXTreeID);
  84. if (tree_id)
  85. *out << " ax_tree_id=" << *tree_id;
  86. std::u16string title(window->GetTitle());
  87. if (!title.empty()) {
  88. out_window_titles->push_back(base::UTF16ToUTF8(title));
  89. if (!scrub_data) {
  90. *out << " title=" << title;
  91. }
  92. }
  93. int app_type = window->GetProperty(aura::client::kAppType);
  94. *out << " app_type=" << app_type;
  95. std::string* pkg_name = window->GetProperty(ash::kArcPackageNameKey);
  96. if (pkg_name)
  97. *out << " pkg_name=" << *pkg_name;
  98. *out << '\n';
  99. views::Widget* widget = views::Widget::GetWidgetForNativeView(window);
  100. if (widget) {
  101. *out << std::string(indent + 3, ' ');
  102. *out << " [widget]";
  103. views::PrintWidgetInformation(*widget, /*detailed*/ false, out);
  104. }
  105. for (aura::Window* child : window->children()) {
  106. PrintWindowHierarchy(active_window, focused_window, capture_window, child,
  107. indent + 3, scrub_data, out_window_titles, out);
  108. }
  109. }
  110. std::vector<std::string> PrintWindowHierarchy(std::ostringstream* out,
  111. bool scrub_data) {
  112. aura::Window* active_window = window_util::GetActiveWindow();
  113. aura::Window* focused_window = window_util::GetFocusedWindow();
  114. aura::Window* capture_window = window_util::GetCaptureWindow();
  115. aura::Window::Windows roots = Shell::Get()->GetAllRootWindows();
  116. std::vector<std::string> window_titles;
  117. for (size_t i = 0; i < roots.size(); ++i) {
  118. *out << "RootWindow " << i << ":\n";
  119. PrintWindowHierarchy(active_window, focused_window, capture_window,
  120. roots[i], 0, scrub_data, &window_titles, out);
  121. }
  122. return window_titles;
  123. }
  124. void ToggleShowDebugBorders() {
  125. aura::Window::Windows root_windows = Shell::Get()->GetAllRootWindows();
  126. std::unique_ptr<cc::DebugBorderTypes> value;
  127. for (auto* window : root_windows) {
  128. ui::Compositor* compositor = window->GetHost()->compositor();
  129. cc::LayerTreeDebugState state = compositor->GetLayerTreeDebugState();
  130. if (!value.get())
  131. value = std::make_unique<cc::DebugBorderTypes>(
  132. state.show_debug_borders.flip());
  133. state.show_debug_borders = *value.get();
  134. compositor->SetLayerTreeDebugState(state);
  135. }
  136. }
  137. void ToggleShowFpsCounter() {
  138. aura::Window::Windows root_windows = Shell::Get()->GetAllRootWindows();
  139. std::unique_ptr<bool> value;
  140. for (auto* window : root_windows) {
  141. ui::Compositor* compositor = window->GetHost()->compositor();
  142. cc::LayerTreeDebugState state = compositor->GetLayerTreeDebugState();
  143. if (!value.get())
  144. value = std::make_unique<bool>(!state.show_fps_counter);
  145. state.show_fps_counter = *value.get();
  146. compositor->SetLayerTreeDebugState(state);
  147. }
  148. }
  149. void ToggleShowPaintRects() {
  150. aura::Window::Windows root_windows = Shell::Get()->GetAllRootWindows();
  151. std::unique_ptr<bool> value;
  152. for (auto* window : root_windows) {
  153. ui::Compositor* compositor = window->GetHost()->compositor();
  154. cc::LayerTreeDebugState state = compositor->GetLayerTreeDebugState();
  155. if (!value.get())
  156. value = std::make_unique<bool>(!state.show_paint_rects);
  157. state.show_paint_rects = *value.get();
  158. compositor->SetLayerTreeDebugState(state);
  159. }
  160. }
  161. } // namespace debug
  162. } // namespace ash