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/test/in_process_context_factory.h"
23 #include "ui/events/event.h"
24 #include "ui/gfx/canvas.h"
25 #include "ui/gfx/geometry/rect.h"
26 #include "ui/gfx/skia_util.h"
27 #include "ui/gl/gl_surface.h"
30 #include "ui/gfx/x/x11_connection.h"
34 #include "ui/gfx/win/dpi.h"
39 // Trivial WindowDelegate implementation that draws a colored background.
40 class DemoWindowDelegate
: public aura::WindowDelegate
{
42 explicit DemoWindowDelegate(SkColor color
) : color_(color
) {}
44 // Overridden from WindowDelegate:
45 gfx::Size
GetMinimumSize() const override
{ return gfx::Size(); }
47 gfx::Size
GetMaximumSize() const override
{ return gfx::Size(); }
49 void OnBoundsChanged(const gfx::Rect
& old_bounds
,
50 const gfx::Rect
& new_bounds
) override
{}
51 ui::TextInputClient
* GetFocusedTextInputClient() override
{ return nullptr; }
52 gfx::NativeCursor
GetCursor(const gfx::Point
& point
) override
{
53 return gfx::kNullCursor
;
55 int GetNonClientComponent(const gfx::Point
& point
) const override
{
58 bool ShouldDescendIntoChildForEventHandling(
60 const gfx::Point
& location
) override
{
63 bool CanFocus() override
{ return true; }
64 void OnCaptureLost() override
{}
65 void OnPaint(gfx::Canvas
* canvas
) override
{
66 canvas
->DrawColor(color_
, SkXfermode::kSrc_Mode
);
68 canvas
->GetClipBounds(&r
);
69 // Fill with a non-solid color so that the compositor will exercise its
70 // texture upload path.
71 while (!r
.IsEmpty()) {
73 canvas
->FillRect(r
, color_
, SkXfermode::kXor_Mode
);
76 void OnDeviceScaleFactorChanged(float device_scale_factor
) override
{}
77 void OnWindowDestroying(aura::Window
* window
) override
{}
78 void OnWindowDestroyed(aura::Window
* window
) override
{}
79 void OnWindowTargetVisibilityChanged(bool visible
) override
{}
80 bool HasHitTestMask() const override
{ return false; }
81 void GetHitTestMask(gfx::Path
* mask
) const override
{}
86 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate
);
89 class DemoWindowTreeClient
: public aura::client::WindowTreeClient
{
91 explicit DemoWindowTreeClient(aura::Window
* window
) : window_(window
) {
92 aura::client::SetWindowTreeClient(window_
, this);
95 ~DemoWindowTreeClient() override
{
96 aura::client::SetWindowTreeClient(window_
, nullptr);
99 // Overridden from aura::client::WindowTreeClient:
100 aura::Window
* GetDefaultParent(aura::Window
* context
,
101 aura::Window
* window
,
102 const gfx::Rect
& bounds
) override
{
103 if (!capture_client_
) {
104 capture_client_
.reset(
105 new aura::client::DefaultCaptureClient(window_
->GetRootWindow()));
111 aura::Window
* window_
;
113 scoped_ptr
<aura::client::DefaultCaptureClient
> capture_client_
;
115 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient
);
120 // This demo uses InProcessContextFactory which uses X on a separate Gpu
122 gfx::InitializeThreadedX11();
125 gfx::GLSurface::InitializeOneOff();
128 gfx::InitDeviceScaleFactor(1.0f
);
131 // The ContextFactory must exist before any Compositors are created.
132 bool context_factory_for_test
= false;
133 scoped_ptr
<ui::InProcessContextFactory
> context_factory(
134 new ui::InProcessContextFactory(context_factory_for_test
, nullptr));
135 context_factory
->set_use_test_surface(false);
137 // Create the message-loop here before creating the root window.
138 base::MessageLoopForUI message_loop
;
140 base::PowerMonitor
power_monitor(make_scoped_ptr(
141 new base::PowerMonitorDeviceSource
));
143 aura::Env::CreateInstance(true);
144 aura::Env::GetInstance()->set_context_factory(context_factory
.get());
145 scoped_ptr
<aura::TestScreen
> test_screen(
146 aura::TestScreen::Create(gfx::Size()));
147 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE
, test_screen
.get());
148 scoped_ptr
<aura::WindowTreeHost
> host(
149 test_screen
->CreateHostForPrimaryDisplay());
150 scoped_ptr
<DemoWindowTreeClient
> window_tree_client(
151 new DemoWindowTreeClient(host
->window()));
152 aura::test::TestFocusClient focus_client
;
153 aura::client::SetFocusClient(host
->window(), &focus_client
);
155 // Create a hierarchy of test windows.
156 DemoWindowDelegate
window_delegate1(SK_ColorBLUE
);
157 aura::Window
window1(&window_delegate1
);
159 window1
.Init(aura::WINDOW_LAYER_TEXTURED
);
160 window1
.SetBounds(gfx::Rect(100, 100, 400, 400));
162 aura::client::ParentWindowWithContext(&window1
, host
->window(), gfx::Rect());
164 DemoWindowDelegate
window_delegate2(SK_ColorRED
);
165 aura::Window
window2(&window_delegate2
);
167 window2
.Init(aura::WINDOW_LAYER_TEXTURED
);
168 window2
.SetBounds(gfx::Rect(200, 200, 350, 350));
170 aura::client::ParentWindowWithContext(&window2
, host
->window(), gfx::Rect());
172 DemoWindowDelegate
window_delegate3(SK_ColorGREEN
);
173 aura::Window
window3(&window_delegate3
);
175 window3
.Init(aura::WINDOW_LAYER_TEXTURED
);
176 window3
.SetBounds(gfx::Rect(10, 10, 50, 50));
178 window2
.AddChild(&window3
);
181 base::MessageLoopForUI::current()->Run();
188 int main(int argc
, char** argv
) {
189 base::CommandLine::Init(argc
, argv
);
191 // The exit manager is in charge of calling the dtors of singleton objects.
192 base::AtExitManager exit_manager
;
194 base::i18n::InitializeICU();