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 ui::TextInputClient
* GetFocusedTextInputClient() override
{ return nullptr; }
53 gfx::NativeCursor
GetCursor(const gfx::Point
& point
) override
{
54 return gfx::kNullCursor
;
56 int GetNonClientComponent(const gfx::Point
& point
) const override
{
59 bool ShouldDescendIntoChildForEventHandling(
61 const gfx::Point
& location
) override
{
64 bool CanFocus() override
{ return true; }
65 void OnCaptureLost() override
{}
66 void OnPaint(const ui::PaintContext
& context
) override
{
67 ui::PaintRecorder
recorder(context
);
68 recorder
.canvas()->DrawColor(color_
, SkXfermode::kSrc_Mode
);
70 recorder
.canvas()->GetClipBounds(&r
);
71 // Fill with a non-solid color so that the compositor will exercise its
72 // texture upload path.
73 while (!r
.IsEmpty()) {
75 recorder
.canvas()->FillRect(r
, color_
, SkXfermode::kXor_Mode
);
78 void OnDeviceScaleFactorChanged(float device_scale_factor
) override
{}
79 void OnWindowDestroying(aura::Window
* window
) override
{}
80 void OnWindowDestroyed(aura::Window
* window
) override
{}
81 void OnWindowTargetVisibilityChanged(bool visible
) override
{}
82 bool HasHitTestMask() const override
{ return false; }
83 void GetHitTestMask(gfx::Path
* mask
) const override
{}
88 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate
);
91 class DemoWindowTreeClient
: public aura::client::WindowTreeClient
{
93 explicit DemoWindowTreeClient(aura::Window
* window
) : window_(window
) {
94 aura::client::SetWindowTreeClient(window_
, this);
97 ~DemoWindowTreeClient() override
{
98 aura::client::SetWindowTreeClient(window_
, nullptr);
101 // Overridden from aura::client::WindowTreeClient:
102 aura::Window
* GetDefaultParent(aura::Window
* context
,
103 aura::Window
* window
,
104 const gfx::Rect
& bounds
) override
{
105 if (!capture_client_
) {
106 capture_client_
.reset(
107 new aura::client::DefaultCaptureClient(window_
->GetRootWindow()));
113 aura::Window
* window_
;
115 scoped_ptr
<aura::client::DefaultCaptureClient
> capture_client_
;
117 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient
);
122 // This demo uses InProcessContextFactory which uses X on a separate Gpu
124 gfx::InitializeThreadedX11();
127 gfx::GLSurface::InitializeOneOff();
130 gfx::InitDeviceScaleFactor(1.0f
);
133 // The ContextFactory must exist before any Compositors are created.
134 bool context_factory_for_test
= false;
135 scoped_ptr
<ui::InProcessContextFactory
> context_factory(
136 new ui::InProcessContextFactory(context_factory_for_test
, nullptr));
137 context_factory
->set_use_test_surface(false);
139 // Create the message-loop here before creating the root window.
140 base::MessageLoopForUI message_loop
;
142 base::PowerMonitor
power_monitor(make_scoped_ptr(
143 new base::PowerMonitorDeviceSource
));
145 aura::Env::CreateInstance(true);
146 aura::Env::GetInstance()->set_context_factory(context_factory
.get());
147 scoped_ptr
<aura::TestScreen
> test_screen(
148 aura::TestScreen::Create(gfx::Size()));
149 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE
, test_screen
.get());
150 scoped_ptr
<aura::WindowTreeHost
> host(
151 test_screen
->CreateHostForPrimaryDisplay());
152 scoped_ptr
<DemoWindowTreeClient
> window_tree_client(
153 new DemoWindowTreeClient(host
->window()));
154 aura::test::TestFocusClient focus_client
;
155 aura::client::SetFocusClient(host
->window(), &focus_client
);
157 // Create a hierarchy of test windows.
158 DemoWindowDelegate
window_delegate1(SK_ColorBLUE
);
159 aura::Window
window1(&window_delegate1
);
161 window1
.Init(ui::LAYER_TEXTURED
);
162 window1
.SetBounds(gfx::Rect(100, 100, 400, 400));
164 aura::client::ParentWindowWithContext(&window1
, host
->window(), gfx::Rect());
166 DemoWindowDelegate
window_delegate2(SK_ColorRED
);
167 aura::Window
window2(&window_delegate2
);
169 window2
.Init(ui::LAYER_TEXTURED
);
170 window2
.SetBounds(gfx::Rect(200, 200, 350, 350));
172 aura::client::ParentWindowWithContext(&window2
, host
->window(), gfx::Rect());
174 DemoWindowDelegate
window_delegate3(SK_ColorGREEN
);
175 aura::Window
window3(&window_delegate3
);
177 window3
.Init(ui::LAYER_TEXTURED
);
178 window3
.SetBounds(gfx::Rect(10, 10, 50, 50));
180 window2
.AddChild(&window3
);
183 base::MessageLoopForUI::current()->Run();
190 int main(int argc
, char** argv
) {
191 base::CommandLine::Init(argc
, argv
);
193 // The exit manager is in charge of calling the dtors of singleton objects.
194 base::AtExitManager exit_manager
;
196 base::i18n::InitializeICU();