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.
5 #include "base/at_exit.h"
6 #include "base/command_line.h"
7 #include "base/i18n/icu_util.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/power_monitor/power_monitor.h"
11 #include "base/power_monitor/power_monitor_device_source.h"
12 #include "third_party/skia/include/core/SkXfermode.h"
13 #include "ui/aura/client/default_capture_client.h"
14 #include "ui/aura/client/window_tree_client.h"
15 #include "ui/aura/env.h"
16 #include "ui/aura/test/test_focus_client.h"
17 #include "ui/aura/test/test_screen.h"
18 #include "ui/aura/window.h"
19 #include "ui/aura/window_delegate.h"
20 #include "ui/aura/window_tree_host.h"
21 #include "ui/base/hit_test.h"
22 #include "ui/compositor/paint_recorder.h"
23 #include "ui/compositor/test/in_process_context_factory.h"
24 #include "ui/events/event.h"
25 #include "ui/gfx/canvas.h"
26 #include "ui/gfx/geometry/rect.h"
27 #include "ui/gfx/skia_util.h"
28 #include "ui/gl/gl_surface.h"
31 #include "ui/gfx/x/x11_connection.h"
35 #include "ui/gfx/win/dpi.h"
40 // Trivial WindowDelegate implementation that draws a colored background.
41 class DemoWindowDelegate
: public aura::WindowDelegate
{
43 explicit DemoWindowDelegate(SkColor color
) : color_(color
) {}
45 // Overridden from WindowDelegate:
46 gfx::Size
GetMinimumSize() const override
{ return gfx::Size(); }
48 gfx::Size
GetMaximumSize() const override
{ return gfx::Size(); }
50 void OnBoundsChanged(const gfx::Rect
& old_bounds
,
51 const gfx::Rect
& new_bounds
) override
{
52 window_bounds_
= new_bounds
;
54 gfx::NativeCursor
GetCursor(const gfx::Point
& point
) override
{
55 return gfx::kNullCursor
;
57 int GetNonClientComponent(const gfx::Point
& point
) const override
{
60 bool ShouldDescendIntoChildForEventHandling(
62 const gfx::Point
& location
) override
{
65 bool CanFocus() override
{ return true; }
66 void OnCaptureLost() override
{}
67 void OnPaint(const ui::PaintContext
& context
) override
{
68 ui::PaintRecorder
recorder(context
, window_bounds_
.size());
69 recorder
.canvas()->DrawColor(color_
, SkXfermode::kSrc_Mode
);
71 recorder
.canvas()->GetClipBounds(&r
);
72 // Fill with a non-solid color so that the compositor will exercise its
73 // texture upload path.
74 while (!r
.IsEmpty()) {
76 recorder
.canvas()->FillRect(r
, color_
, SkXfermode::kXor_Mode
);
79 void OnDeviceScaleFactorChanged(float device_scale_factor
) override
{}
80 void OnWindowDestroying(aura::Window
* window
) override
{}
81 void OnWindowDestroyed(aura::Window
* window
) override
{}
82 void OnWindowTargetVisibilityChanged(bool visible
) override
{}
83 bool HasHitTestMask() const override
{ return false; }
84 void GetHitTestMask(gfx::Path
* mask
) const override
{}
88 gfx::Rect window_bounds_
;
90 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate
);
93 class DemoWindowTreeClient
: public aura::client::WindowTreeClient
{
95 explicit DemoWindowTreeClient(aura::Window
* window
) : window_(window
) {
96 aura::client::SetWindowTreeClient(window_
, this);
99 ~DemoWindowTreeClient() override
{
100 aura::client::SetWindowTreeClient(window_
, nullptr);
103 // Overridden from aura::client::WindowTreeClient:
104 aura::Window
* GetDefaultParent(aura::Window
* context
,
105 aura::Window
* window
,
106 const gfx::Rect
& bounds
) override
{
107 if (!capture_client_
) {
108 capture_client_
.reset(
109 new aura::client::DefaultCaptureClient(window_
->GetRootWindow()));
115 aura::Window
* window_
;
117 scoped_ptr
<aura::client::DefaultCaptureClient
> capture_client_
;
119 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient
);
124 // This demo uses InProcessContextFactory which uses X on a separate Gpu
126 gfx::InitializeThreadedX11();
129 gfx::GLSurface::InitializeOneOff();
132 gfx::InitDeviceScaleFactor(1.0f
);
135 // The ContextFactory must exist before any Compositors are created.
136 bool context_factory_for_test
= false;
137 scoped_ptr
<ui::InProcessContextFactory
> context_factory(
138 new ui::InProcessContextFactory(context_factory_for_test
, nullptr));
139 context_factory
->set_use_test_surface(false);
141 // Create the message-loop here before creating the root window.
142 base::MessageLoopForUI message_loop
;
144 base::PowerMonitor
power_monitor(make_scoped_ptr(
145 new base::PowerMonitorDeviceSource
));
147 aura::Env::CreateInstance(true);
148 aura::Env::GetInstance()->set_context_factory(context_factory
.get());
149 scoped_ptr
<aura::TestScreen
> test_screen(
150 aura::TestScreen::Create(gfx::Size()));
151 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE
, test_screen
.get());
152 scoped_ptr
<aura::WindowTreeHost
> host(
153 test_screen
->CreateHostForPrimaryDisplay());
154 scoped_ptr
<DemoWindowTreeClient
> window_tree_client(
155 new DemoWindowTreeClient(host
->window()));
156 aura::test::TestFocusClient focus_client
;
157 aura::client::SetFocusClient(host
->window(), &focus_client
);
159 // Create a hierarchy of test windows.
160 gfx::Rect
window1_bounds(100, 100, 400, 400);
161 DemoWindowDelegate
window_delegate1(SK_ColorBLUE
);
162 aura::Window
window1(&window_delegate1
);
164 window1
.Init(ui::LAYER_TEXTURED
);
165 window1
.SetBounds(window1_bounds
);
167 aura::client::ParentWindowWithContext(&window1
, host
->window(), gfx::Rect());
169 gfx::Rect
window2_bounds(200, 200, 350, 350);
170 DemoWindowDelegate
window_delegate2(SK_ColorRED
);
171 aura::Window
window2(&window_delegate2
);
173 window2
.Init(ui::LAYER_TEXTURED
);
174 window2
.SetBounds(window2_bounds
);
176 aura::client::ParentWindowWithContext(&window2
, host
->window(), gfx::Rect());
178 gfx::Rect
window3_bounds(10, 10, 50, 50);
179 DemoWindowDelegate
window_delegate3(SK_ColorGREEN
);
180 aura::Window
window3(&window_delegate3
);
182 window3
.Init(ui::LAYER_TEXTURED
);
183 window3
.SetBounds(window3_bounds
);
185 window2
.AddChild(&window3
);
188 base::MessageLoopForUI::current()->Run();
195 int main(int argc
, char** argv
) {
196 base::CommandLine::Init(argc
, argv
);
198 // The exit manager is in charge of calling the dtors of singleton objects.
199 base::AtExitManager exit_manager
;
201 base::i18n::InitializeICU();