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 "third_party/skia/include/core/SkXfermode.h"
11 #include "ui/aura/client/default_capture_client.h"
12 #include "ui/aura/client/window_tree_client.h"
13 #include "ui/aura/env.h"
14 #include "ui/aura/test/test_focus_client.h"
15 #include "ui/aura/test/test_screen.h"
16 #include "ui/aura/window.h"
17 #include "ui/aura/window_delegate.h"
18 #include "ui/aura/window_tree_host.h"
19 #include "ui/base/hit_test.h"
20 #include "ui/compositor/test/in_process_context_factory.h"
21 #include "ui/events/event.h"
22 #include "ui/gfx/canvas.h"
23 #include "ui/gfx/rect.h"
24 #include "ui/gl/gl_surface.h"
27 #include "ui/gfx/x/x11_connection.h"
31 #include "ui/gfx/win/dpi.h"
36 // Trivial WindowDelegate implementation that draws a colored background.
37 class DemoWindowDelegate : public aura::WindowDelegate {
39 explicit DemoWindowDelegate(SkColor color) : color_(color) {}
41 // Overridden from WindowDelegate:
42 gfx::Size GetMinimumSize() const override { return gfx::Size(); }
44 gfx::Size GetMaximumSize() const override { return gfx::Size(); }
46 void OnBoundsChanged(const gfx::Rect& old_bounds,
47 const gfx::Rect& new_bounds) override {}
48 gfx::NativeCursor GetCursor(const gfx::Point& point) override {
49 return gfx::kNullCursor;
51 int GetNonClientComponent(const gfx::Point& point) const override {
54 bool ShouldDescendIntoChildForEventHandling(
56 const gfx::Point& location) override {
59 bool CanFocus() override { return true; }
60 void OnCaptureLost() override {}
61 void OnPaint(gfx::Canvas* canvas) override {
62 canvas->DrawColor(color_, SkXfermode::kSrc_Mode);
64 void OnDeviceScaleFactorChanged(float device_scale_factor) override {}
65 void OnWindowDestroying(aura::Window* window) override {}
66 void OnWindowDestroyed(aura::Window* window) override {}
67 void OnWindowTargetVisibilityChanged(bool visible) override {}
68 bool HasHitTestMask() const override { return false; }
69 void GetHitTestMask(gfx::Path* mask) const override {}
74 DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate);
77 class DemoWindowTreeClient : public aura::client::WindowTreeClient {
79 explicit DemoWindowTreeClient(aura::Window* window) : window_(window) {
80 aura::client::SetWindowTreeClient(window_, this);
83 ~DemoWindowTreeClient() override {
84 aura::client::SetWindowTreeClient(window_, NULL);
87 // Overridden from aura::client::WindowTreeClient:
88 aura::Window* GetDefaultParent(aura::Window* context,
90 const gfx::Rect& bounds) override {
91 if (!capture_client_) {
92 capture_client_.reset(
93 new aura::client::DefaultCaptureClient(window_->GetRootWindow()));
99 aura::Window* window_;
101 scoped_ptr<aura::client::DefaultCaptureClient> capture_client_;
103 DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient);
108 // This demo uses InProcessContextFactory which uses X on a separate Gpu
110 gfx::InitializeThreadedX11();
113 gfx::GLSurface::InitializeOneOff();
116 gfx::InitDeviceScaleFactor(1.0f);
119 // The ContextFactory must exist before any Compositors are created.
120 scoped_ptr<ui::InProcessContextFactory> context_factory(
121 new ui::InProcessContextFactory());
123 // Create the message-loop here before creating the root window.
124 base::MessageLoopForUI message_loop;
126 aura::Env::CreateInstance(true);
127 aura::Env::GetInstance()->set_context_factory(context_factory.get());
128 scoped_ptr<aura::TestScreen> test_screen(
129 aura::TestScreen::Create(gfx::Size()));
130 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, test_screen.get());
131 scoped_ptr<aura::WindowTreeHost> host(
132 test_screen->CreateHostForPrimaryDisplay());
133 scoped_ptr<DemoWindowTreeClient> window_tree_client(
134 new DemoWindowTreeClient(host->window()));
135 aura::test::TestFocusClient focus_client;
136 aura::client::SetFocusClient(host->window(), &focus_client);
138 // Create a hierarchy of test windows.
139 DemoWindowDelegate window_delegate1(SK_ColorBLUE);
140 aura::Window window1(&window_delegate1);
142 window1.Init(aura::WINDOW_LAYER_TEXTURED);
143 window1.SetBounds(gfx::Rect(100, 100, 400, 400));
145 aura::client::ParentWindowWithContext(&window1, host->window(), gfx::Rect());
147 DemoWindowDelegate window_delegate2(SK_ColorRED);
148 aura::Window window2(&window_delegate2);
150 window2.Init(aura::WINDOW_LAYER_TEXTURED);
151 window2.SetBounds(gfx::Rect(200, 200, 350, 350));
153 aura::client::ParentWindowWithContext(&window2, host->window(), gfx::Rect());
155 DemoWindowDelegate window_delegate3(SK_ColorGREEN);
156 aura::Window window3(&window_delegate3);
158 window3.Init(aura::WINDOW_LAYER_TEXTURED);
159 window3.SetBounds(gfx::Rect(10, 10, 50, 50));
161 window2.AddChild(&window3);
164 base::MessageLoopForUI::current()->Run();
171 int main(int argc, char** argv) {
172 CommandLine::Init(argc, argv);
174 // The exit manager is in charge of calling the dtors of singleton objects.
175 base::AtExitManager exit_manager;
177 base::i18n::InitializeICU();