debug_utils.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright (c) 2012 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 "ui/views/debug_utils.h"
  5. #include <ostream>
  6. #include "base/logging.h"
  7. #include "ui/compositor/layer.h"
  8. #include "ui/views/view.h"
  9. #include "ui/views/widget/widget.h"
  10. #if !defined(NDEBUG)
  11. #include "ui/gfx/geometry/angle_conversions.h"
  12. #include "ui/gfx/geometry/transform_util.h"
  13. #endif
  14. namespace views {
  15. namespace {
  16. void PrintViewHierarchyImp(const View* view,
  17. size_t indent,
  18. std::ostringstream* out) {
  19. *out << std::string(indent, ' ');
  20. *out << view->GetClassName();
  21. *out << ' ';
  22. *out << view->GetID();
  23. *out << ' ';
  24. *out << view->x() << "," << view->y() << ",";
  25. *out << view->bounds().right() << "," << view->bounds().bottom();
  26. *out << ' ';
  27. *out << view;
  28. *out << '\n';
  29. for (const View* child : view->children())
  30. PrintViewHierarchyImp(child, indent + 2, out);
  31. }
  32. void PrintFocusHierarchyImp(const View* view,
  33. size_t indent,
  34. std::ostringstream* out) {
  35. *out << std::string(indent, ' ');
  36. *out << view->GetClassName();
  37. *out << ' ';
  38. *out << view->GetID();
  39. *out << ' ';
  40. *out << view->GetClassName();
  41. *out << ' ';
  42. *out << view;
  43. *out << '\n';
  44. if (!view->children().empty())
  45. PrintFocusHierarchyImp(view->children().front(), indent + 2, out);
  46. const View* next_focusable = view->GetNextFocusableView();
  47. if (next_focusable)
  48. PrintFocusHierarchyImp(next_focusable, indent, out);
  49. }
  50. #if !defined(NDEBUG)
  51. std::string PrintViewGraphImpl(const View* view) {
  52. // 64-bit pointer = 16 bytes of hex + "0x" + '\0' = 19.
  53. const size_t kMaxPointerStringLength = 19;
  54. std::string result;
  55. // Node characteristics.
  56. char p[kMaxPointerStringLength];
  57. const std::string class_name(view->GetClassName());
  58. size_t base_name_index = class_name.find_last_of('/');
  59. if (base_name_index == std::string::npos)
  60. base_name_index = 0;
  61. else
  62. base_name_index++;
  63. constexpr size_t kBoundsBufferSize = 512;
  64. char bounds_buffer[kBoundsBufferSize];
  65. // Information about current node.
  66. base::snprintf(p, kBoundsBufferSize, "%p", view);
  67. result.append(" N");
  68. result.append(p + 2);
  69. result.append(" [label=\"");
  70. result.append(class_name.substr(base_name_index).c_str());
  71. base::snprintf(bounds_buffer, kBoundsBufferSize,
  72. "\\n bounds: (%d, %d), (%dx%d)", view->bounds().x(),
  73. view->bounds().y(), view->bounds().width(),
  74. view->bounds().height());
  75. result.append(bounds_buffer);
  76. gfx::DecomposedTransform decomp;
  77. if (!view->GetTransform().IsIdentity() &&
  78. gfx::DecomposeTransform(&decomp, view->GetTransform())) {
  79. base::snprintf(bounds_buffer, kBoundsBufferSize,
  80. "\\n translation: (%f, %f)", decomp.translate[0],
  81. decomp.translate[1]);
  82. result.append(bounds_buffer);
  83. base::snprintf(bounds_buffer, kBoundsBufferSize, "\\n rotation: %3.2f",
  84. gfx::RadToDeg(std::acos(decomp.quaternion.w()) * 2));
  85. result.append(bounds_buffer);
  86. base::snprintf(bounds_buffer, kBoundsBufferSize,
  87. "\\n scale: (%2.4f, %2.4f)", decomp.scale[0],
  88. decomp.scale[1]);
  89. result.append(bounds_buffer);
  90. }
  91. result.append("\"");
  92. if (!view->parent())
  93. result.append(", shape=box");
  94. if (view->layer()) {
  95. if (view->layer()->has_external_content())
  96. result.append(", color=green");
  97. else
  98. result.append(", color=red");
  99. if (view->layer()->fills_bounds_opaquely())
  100. result.append(", style=filled");
  101. }
  102. result.append("]\n");
  103. // Link to parent.
  104. if (view->parent()) {
  105. char pp[kMaxPointerStringLength];
  106. base::snprintf(pp, kMaxPointerStringLength, "%p", view->parent());
  107. result.append(" N");
  108. result.append(pp + 2);
  109. result.append(" -> N");
  110. result.append(p + 2);
  111. result.append("\n");
  112. }
  113. for (const View* child : view->children())
  114. result.append(PrintViewGraphImpl(child));
  115. return result;
  116. }
  117. #endif
  118. } // namespace
  119. void PrintWidgetInformation(const Widget& widget,
  120. bool detailed,
  121. std::ostringstream* out) {
  122. *out << " name=" << widget.GetName();
  123. *out << " (" << &widget << ")";
  124. if (widget.parent())
  125. *out << " parent=" << widget.parent();
  126. else
  127. *out << " parent=none";
  128. const ui::Layer* layer = widget.GetLayer();
  129. if (layer)
  130. *out << " layer=" << layer;
  131. else
  132. *out << " layer=none";
  133. *out << (widget.is_top_level() ? " [top_level]" : " [!top_level]");
  134. if (detailed) {
  135. *out << (widget.IsActive() ? " [active]" : " [!active]");
  136. *out << (widget.IsVisible() ? " [visible]" : " [!visible]");
  137. }
  138. *out << (widget.IsClosed() ? " [closed]" : "");
  139. *out << (widget.IsMaximized() ? " [maximized]" : "");
  140. *out << (widget.IsMinimized() ? " [minimized]" : "");
  141. *out << (widget.IsFullscreen() ? " [fullscreen]" : "");
  142. if (detailed)
  143. *out << " " << widget.GetWindowBoundsInScreen().ToString();
  144. *out << '\n';
  145. }
  146. void PrintViewHierarchy(const View* view) {
  147. std::ostringstream out;
  148. PrintViewHierarchy(view, &out);
  149. // Error so users in the field can generate and upload logs.
  150. LOG(ERROR) << out.str();
  151. }
  152. void PrintViewHierarchy(const View* view, std::ostringstream* out) {
  153. *out << "View hierarchy:\n";
  154. PrintViewHierarchyImp(view, 0, out);
  155. }
  156. void PrintFocusHierarchy(const View* view) {
  157. std::ostringstream out;
  158. out << "Focus hierarchy:\n";
  159. PrintFocusHierarchyImp(view, 0, &out);
  160. // Error so users in the field can generate and upload logs.
  161. LOG(ERROR) << out.str();
  162. }
  163. #if !defined(NDEBUG)
  164. std::string PrintViewGraph(const View* view) {
  165. return "digraph {\n" + PrintViewGraphImpl(view) + "}\n";
  166. }
  167. #endif
  168. } // namespace views