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  std::string content_indent_str(indent+1, ' ');
32
33  layer->transform().TransformPointReverse(mouse_location);
34  bool mouse_inside_layer_bounds = layer->bounds().Contains(mouse_location);
35  mouse_location.Offset(-layer->bounds().x(), -layer->bounds().y());
36
37  *out << UTF8ToWide(indent_str);
38  if (mouse_inside_layer_bounds)
39    *out << L'*';
40  else
41    *out << L' ';
42
43  *out << UTF8ToWide(layer->name()) << L' ' << layer;
44
45  switch (layer->type()) {
46    case ui::LAYER_NOT_DRAWN:
47      *out << L" not_drawn";
48      break;
49    case ui::LAYER_TEXTURED:
50      *out << L" textured";
51      if (layer->fills_bounds_opaquely())
52        *out << L" opaque";
53      break;
54    case ui::LAYER_SOLID_COLOR:
55      *out << L" solid";
56      break;
57  }
58
59  if (!layer->visible())
60    *out << L" !visible";
61
62  std::string property_indent_str(indent+3, ' ');
63  *out << L'\n' << UTF8ToWide(property_indent_str);
64  *out << L"bounds: " << layer->bounds().x() << L',' << layer->bounds().y();
65  *out << L' ' << layer->bounds().width() << L'x' << layer->bounds().height();
66
67  if (layer->opacity() != 1.0f) {
68    *out << L'\n' << UTF8ToWide(property_indent_str);
69    *out << L"opacity: " << std::setprecision(2) << layer->opacity();
70  }
71
72  gfx::DecomposedTransform decomp;
73  if (!layer->transform().IsIdentity() &&
74      gfx::DecomposeTransform(&decomp, layer->transform())) {
75    *out << L'\n' << UTF8ToWide(property_indent_str);
76    *out << L"translation: " << std::fixed << decomp.translate[0];
77    *out << L", " << decomp.translate[1];
78
79    *out << L'\n' << UTF8ToWide(property_indent_str);
80    *out << L"rotation: ";
81    *out << std::acos(decomp.quaternion[3]) * 360.0 / M_PI;
82
83    *out << L'\n' << UTF8ToWide(property_indent_str);
84    *out << L"scale: " << decomp.scale[0];
85    *out << L", " << decomp.scale[1];
86  }
87
88  *out << L'\n';
89
90  for (size_t i = 0, count = layer->children().size(); i < count; ++i) {
91    PrintLayerHierarchyImp(
92        layer->children()[i], indent + 3, mouse_location, out);
93  }
94}
95
96}  // namespace
97
98void PrintLayerHierarchy(const Layer* layer, gfx::Point mouse_location) {
99  std::wostringstream out;
100  out << L"Layer hierarchy:\n";
101  PrintLayerHierarchyImp(layer, 0, mouse_location, &out);
102  // Error so logs can be collected from end-users.
103  LOG(ERROR) << out.str();
104}
105
106} // namespace ui
107