1 // Copyright 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.
5 #include "ash/accelerators/debug_commands.h"
7 #include "ash/accelerators/accelerator_commands.h"
8 #include "ash/ash_switches.h"
10 #include "ash/desktop_background/desktop_background_controller.h"
11 #include "ash/desktop_background/user_wallpaper_delegate.h"
12 #include "ash/display/display_manager.h"
13 #include "ash/host/ash_window_tree_host.h"
14 #include "ash/root_window_controller.h"
15 #include "ash/shell.h"
16 #include "ash/wm/window_util.h"
17 #include "base/command_line.h"
18 #include "third_party/skia/include/core/SkColor.h"
19 #include "third_party/skia/include/core/SkPaint.h"
20 #include "ui/aura/window.h"
21 #include "ui/aura/window_event_dispatcher.h"
22 #include "ui/compositor/debug_utils.h"
23 #include "ui/compositor/layer.h"
24 #include "ui/gfx/canvas.h"
25 #include "ui/gfx/image/image_skia.h"
26 #include "ui/views/debug_utils.h"
27 #include "ui/views/widget/widget.h"
33 void HandlePrintLayerHierarchy() {
34 aura::Window::Windows root_windows
= Shell::GetAllRootWindows();
35 for (size_t i
= 0; i
< root_windows
.size(); ++i
) {
36 ui::PrintLayerHierarchy(
37 root_windows
[i
]->layer(),
38 root_windows
[i
]->GetHost()->dispatcher()->GetLastMouseLocationInRoot());
42 void HandlePrintViewHierarchy() {
43 aura::Window
* active_window
= ash::wm::GetActiveWindow();
46 views::Widget
* browser_widget
=
47 views::Widget::GetWidgetForNativeWindow(active_window
);
50 views::PrintViewHierarchy(browser_widget
->GetRootView());
53 void PrintWindowHierarchy(aura::Window
* window
,
55 std::ostringstream
* out
) {
56 std::string
indent_str(indent
, ' ');
57 std::string
name(window
->name());
60 *out
<< indent_str
<< name
<< " (" << window
<< ")"
61 << " type=" << window
->type()
62 << (wm::IsActiveWindow(window
) ? " [active] " : " ")
63 << (window
->IsVisible() ? " visible " : " ")
64 << window
->bounds().ToString()
67 for (size_t i
= 0; i
< window
->children().size(); ++i
)
68 PrintWindowHierarchy(window
->children()[i
], indent
+ 3, out
);
71 void HandlePrintWindowHierarchy() {
72 Shell::RootWindowControllerList controllers
=
73 Shell::GetAllRootWindowControllers();
74 for (size_t i
= 0; i
< controllers
.size(); ++i
) {
75 std::ostringstream out
;
76 out
<< "RootWindow " << i
<< ":\n";
77 PrintWindowHierarchy(controllers
[i
]->GetRootWindow(), 0, &out
);
78 // Error so logs can be collected from end-users.
79 LOG(ERROR
) << out
.str();
83 gfx::ImageSkia
CreateWallpaperImage(SkColor fill
, SkColor rect
) {
84 // TODO(oshima): Consider adding a command line option to control
85 // wallpaper images for testing.
86 // The size is randomly picked.
87 gfx::Size
image_size(1366, 768);
88 gfx::Canvas
canvas(image_size
, 1.0f
, true);
89 canvas
.DrawColor(fill
);
92 paint
.setStrokeWidth(10);
93 paint
.setStyle(SkPaint::kStroke_Style
);
94 paint
.setXfermodeMode(SkXfermode::kSrcOver_Mode
);
95 canvas
.DrawRoundRect(gfx::Rect(image_size
), 100, paint
);
96 return gfx::ImageSkia(canvas
.ExtractImageRep());
99 void HandleToggleDesktopBackgroundMode() {
100 static int index
= 0;
101 DesktopBackgroundController
* desktop_background_controller
=
102 Shell::GetInstance()->desktop_background_controller();
103 switch (++index
% 4) {
105 ash::Shell::GetInstance()->user_wallpaper_delegate()->
106 InitializeWallpaper();
109 desktop_background_controller
->SetWallpaperImage(
110 CreateWallpaperImage(SK_ColorRED
, SK_ColorBLUE
),
111 WALLPAPER_LAYOUT_STRETCH
);
114 desktop_background_controller
->SetWallpaperImage(
115 CreateWallpaperImage(SK_ColorBLUE
, SK_ColorGREEN
),
116 WALLPAPER_LAYOUT_CENTER
);
119 desktop_background_controller
->SetWallpaperImage(
120 CreateWallpaperImage(SK_ColorGREEN
, SK_ColorRED
),
121 WALLPAPER_LAYOUT_CENTER_CROPPED
);
128 void PrintUIHierarchies() {
129 // This is a separate command so the user only has to hit one key to generate
130 // all the logs. Developers use the individual dumps repeatedly, so keep
131 // those as separate commands to avoid spamming their logs.
132 HandlePrintLayerHierarchy();
133 HandlePrintWindowHierarchy();
134 HandlePrintViewHierarchy();
137 bool DebugAcceleratorsEnabled() {
138 return base::CommandLine::ForCurrentProcess()->HasSwitch(
139 switches::kAshDebugShortcuts
);
142 bool PerformDebugAction(AcceleratorAction action
) {
143 if (!DebugAcceleratorsEnabled())
147 #if defined(OS_CHROMEOS)
148 case DEBUG_ADD_REMOVE_DISPLAY
:
149 Shell::GetInstance()->display_manager()->AddRemoveDisplay();
152 case DEBUG_PRINT_LAYER_HIERARCHY
:
153 HandlePrintLayerHierarchy();
155 case DEBUG_PRINT_VIEW_HIERARCHY
:
156 HandlePrintViewHierarchy();
158 case DEBUG_PRINT_WINDOW_HIERARCHY
:
159 HandlePrintWindowHierarchy();
161 case DEBUG_TOGGLE_DESKTOP_BACKGROUND_MODE
:
162 HandleToggleDesktopBackgroundMode();
164 case DEBUG_TOGGLE_DEVICE_SCALE_FACTOR
:
165 Shell::GetInstance()->display_manager()->ToggleDisplayScaleFactor();
167 case DEBUG_TOGGLE_ROOT_WINDOW_FULL_SCREEN
:
168 Shell::GetPrimaryRootWindowController()->ash_host()->ToggleFullScreen();
170 case DEBUG_TOGGLE_SHOW_DEBUG_BORDERS
:
171 ToggleShowDebugBorders();
173 case DEBUG_TOGGLE_SHOW_FPS_COUNTER
:
174 ToggleShowFpsCounter();
176 case DEBUG_TOGGLE_SHOW_PAINT_RECTS
:
177 ToggleShowPaintRects();