debug_utils.cc revision 3551c9c881056c480085172ff9840cab31610854
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
5#define _USE_MATH_DEFINES // For VC++ to get M_PI. This has to be first.
6
7#include "ui/compositor/debug_utils.h"
8
9#include <cmath>
10#include <iomanip>
11#include <iostream>
12#include <string>
13
14#include "base/logging.h"
15#include "base/strings/utf_string_conversions.h"
16#include "ui/compositor/layer.h"
17#include "ui/gfx/interpolated_transform.h"
18#include "ui/gfx/point.h"
19#include "ui/gfx/point_conversions.h"
20#include "ui/gfx/transform.h"
21
22namespace ui {
23
24namespace {
25
26void PrintLayerHierarchyImp(const Layer* layer,
27                            int indent,
28                            gfx::Point mouse_location,
29                            std::wostringstream* out) {
30  std::string indent_str(indent, ' ');
31
32  layer->transform().TransformPointReverse(mouse_location);
33  bool mouse_inside_layer_bounds = layer->bounds().Contains(mouse_location);
34  mouse_location.Offset(-layer->bounds().x(), -layer->bounds().y());
35
36  *out << UTF8ToWide(indent_str);
37  if (mouse_inside_layer_bounds)
38    *out << L'*';
39  else
40    *out << L' ';
41
42  *out << UTF8ToWide(layer->name()) << L' ' << layer;
43
44  switch (layer->type()) {
45    case ui::LAYER_NOT_DRAWN:
46      *out << L" not_drawn";
47      break;
48    case ui::LAYER_TEXTURED:
49      *out << L" textured";
50      if (layer->fills_bounds_opaquely())
51        *out << L" opaque";
52      break;
53    case ui::LAYER_SOLID_COLOR:
54      *out << L" solid";
55      break;
56  }
57
58  if (!layer->visible())
59    *out << L" !visible";
60
61  std::string property_indent_str(indent+3, ' ');
62  *out << L'\n' << UTF8ToWide(property_indent_str);
63  *out << L"bounds: " << layer->bounds().x() << L',' << layer->bounds().y();
64  *out << L' ' << layer->bounds().width() << L'x' << layer->bounds().height();
65
66  if (layer->opacity() != 1.0f) {
67    *out << L'\n' << UTF8ToWide(property_indent_str);
68    *out << L"opacity: " << std::setprecision(2) << layer->opacity();
69  }
70
71  gfx::DecomposedTransform decomp;
72  if (!layer->transform().IsIdentity() &&
73      gfx::DecomposeTransform(&decomp, layer->transform())) {
74    *out << L'\n' << UTF8ToWide(property_indent_str);
75    *out << L"translation: " << std::fixed << decomp.translate[0];
76    *out << L", " << decomp.translate[1];
77
78    *out << L'\n' << UTF8ToWide(property_indent_str);
79    *out << L"rotation: ";
80    *out << std::acos(decomp.quaternion[3]) * 360.0 / M_PI;
81
82    *out << L'\n' << UTF8ToWide(property_indent_str);
83    *out << L"scale: " << decomp.scale[0];
84    *out << L", " << decomp.scale[1];
85  }
86
87  *out << L'\n';
88
89  for (size_t i = 0, count = layer->children().size(); i < count; ++i) {
90    PrintLayerHierarchyImp(
91        layer->children()[i], indent + 3, mouse_location, out);
92  }
93}
94
95}  // namespace
96
97void PrintLayerHierarchy(const Layer* layer, gfx::Point mouse_location) {
98  std::wostringstream out;
99  out << L"Layer hierarchy:\n";
100  PrintLayerHierarchyImp(layer, 0, mouse_location, &out);
101  // Error so logs can be collected from end-users.
102  LOG(ERROR) << out.str();
103}
104
105} // namespace ui
106