Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / mojo / examples / aura_demo / aura_demo.cc
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.
4
5 #include <stdio.h>
6 #include <string>
7
8 #include "base/at_exit.h"
9 #include "base/command_line.h"
10 #include "base/message_loop/message_loop.h"
11 #include "mojo/examples/aura_demo/demo_screen.h"
12 #include "mojo/examples/aura_demo/root_window_host_mojo.h"
13 #include "mojo/examples/compositor_app/compositor_host.h"
14 #include "mojo/examples/compositor_app/gles2_client_impl.h"
15 #include "mojo/public/bindings/lib/remote_ptr.h"
16 #include "mojo/public/gles2/gles2_cpp.h"
17 #include "mojo/public/system/core.h"
18 #include "mojo/public/system/macros.h"
19 #include "mojom/native_viewport.h"
20 #include "mojom/shell.h"
21 #include "ui/aura/client/default_capture_client.h"
22 #include "ui/aura/client/window_tree_client.h"
23 #include "ui/aura/env.h"
24 #include "ui/aura/root_window.h"
25 #include "ui/aura/window.h"
26 #include "ui/aura/window_delegate.h"
27 #include "ui/base/hit_test.h"
28 #include "ui/gfx/canvas.h"
29
30 #if defined(WIN32)
31 #if !defined(CDECL)
32 #define CDECL __cdecl
33 #endif
34 #define AURA_DEMO_EXPORT __declspec(dllexport)
35 #else
36 #define CDECL
37 #define AURA_DEMO_EXPORT __attribute__((visibility("default")))
38 #endif
39
40 namespace mojo {
41 namespace examples {
42
43 // Trivial WindowDelegate implementation that draws a colored background.
44 class DemoWindowDelegate : public aura::WindowDelegate {
45  public:
46   explicit DemoWindowDelegate(SkColor color) : color_(color) {}
47
48   // Overridden from WindowDelegate:
49   virtual gfx::Size GetMinimumSize() const OVERRIDE {
50     return gfx::Size();
51   }
52
53   virtual gfx::Size GetMaximumSize() const OVERRIDE {
54     return gfx::Size();
55   }
56
57   virtual void OnBoundsChanged(const gfx::Rect& old_bounds,
58                                const gfx::Rect& new_bounds) OVERRIDE {}
59   virtual gfx::NativeCursor GetCursor(const gfx::Point& point) OVERRIDE {
60     return gfx::kNullCursor;
61   }
62   virtual int GetNonClientComponent(const gfx::Point& point) const OVERRIDE {
63     return HTCAPTION;
64   }
65   virtual bool ShouldDescendIntoChildForEventHandling(
66       aura::Window* child,
67       const gfx::Point& location) OVERRIDE {
68     return true;
69   }
70   virtual bool CanFocus() OVERRIDE { return true; }
71   virtual void OnCaptureLost() OVERRIDE {}
72   virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE {
73     canvas->DrawColor(color_, SkXfermode::kSrc_Mode);
74   }
75   virtual void OnDeviceScaleFactorChanged(float device_scale_factor) OVERRIDE {}
76   virtual void OnWindowDestroying() OVERRIDE {}
77   virtual void OnWindowDestroyed() OVERRIDE {}
78   virtual void OnWindowTargetVisibilityChanged(bool visible) OVERRIDE {}
79   virtual bool HasHitTestMask() const OVERRIDE { return false; }
80   virtual void GetHitTestMask(gfx::Path* mask) const OVERRIDE {}
81   virtual void DidRecreateLayer(ui::Layer* old_layer,
82                                 ui::Layer* new_layer) OVERRIDE {}
83
84  private:
85   SkColor color_;
86
87   DISALLOW_COPY_AND_ASSIGN(DemoWindowDelegate);
88 };
89
90 class DemoWindowTreeClient : public aura::client::WindowTreeClient {
91  public:
92   explicit DemoWindowTreeClient(aura::Window* window) : window_(window) {
93     aura::client::SetWindowTreeClient(window_, this);
94   }
95
96   virtual ~DemoWindowTreeClient() {
97     aura::client::SetWindowTreeClient(window_, NULL);
98   }
99
100   // Overridden from aura::client::WindowTreeClient:
101   virtual aura::Window* GetDefaultParent(aura::Window* context,
102                                          aura::Window* window,
103                                          const gfx::Rect& bounds) OVERRIDE {
104     if (!capture_client_) {
105       capture_client_.reset(
106           new aura::client::DefaultCaptureClient(window_->GetRootWindow()));
107     }
108     return window_;
109   }
110
111  private:
112   aura::Window* window_;
113
114   scoped_ptr<aura::client::DefaultCaptureClient> capture_client_;
115
116   DISALLOW_COPY_AND_ASSIGN(DemoWindowTreeClient);
117 };
118
119 class AuraDemo : public ShellClient {
120  public:
121   explicit AuraDemo(ScopedMessagePipeHandle shell_handle)
122       : shell_(shell_handle.Pass(), this) {
123     screen_.reset(DemoScreen::Create());
124     gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get());
125
126     ScopedMessagePipeHandle client_handle, native_viewport_handle;
127     CreateMessagePipe(&client_handle, &native_viewport_handle);
128     root_window_host_.reset(new WindowTreeHostMojo(
129         native_viewport_handle.Pass(),
130         gfx::Rect(800, 600),
131         base::Bind(&AuraDemo::HostContextCreated, base::Unretained(this))));
132     AllocationScope scope;
133     shell_->Connect("mojo:mojo_native_viewport_service", client_handle.Pass());
134   }
135
136   virtual void AcceptConnection(ScopedMessagePipeHandle handle) MOJO_OVERRIDE {
137     NOTREACHED() << "AuraDemo can't be connected to.";
138   }
139
140  private:
141   void HostContextCreated() {
142     aura::RootWindow::CreateParams params(gfx::Rect(0, 0, 500, 500));
143     params.host = root_window_host_.get();
144     root_window_.reset(new aura::RootWindow(params));
145     root_window_host_->set_delegate(root_window_.get());
146     root_window_->Init();
147
148     window_tree_client_.reset(new DemoWindowTreeClient(root_window_->window()));
149
150     delegate1_.reset(new DemoWindowDelegate(SK_ColorBLUE));
151     window1_ = new aura::Window(delegate1_.get());
152     window1_->Init(aura::WINDOW_LAYER_TEXTURED);
153     window1_->SetBounds(gfx::Rect(100, 100, 400, 400));
154     window1_->Show();
155     root_window_->window()->AddChild(window1_);
156
157     delegate2_.reset(new DemoWindowDelegate(SK_ColorRED));
158     window2_ = new aura::Window(delegate2_.get());
159     window2_->Init(aura::WINDOW_LAYER_TEXTURED);
160     window2_->SetBounds(gfx::Rect(200, 200, 350, 350));
161     window2_->Show();
162     root_window_->window()->AddChild(window2_);
163
164     delegate21_.reset(new DemoWindowDelegate(SK_ColorGREEN));
165     window21_ = new aura::Window(delegate21_.get());
166     window21_->Init(aura::WINDOW_LAYER_TEXTURED);
167     window21_->SetBounds(gfx::Rect(10, 10, 50, 50));
168     window21_->Show();
169     window2_->AddChild(window21_);
170
171     root_window_->host()->Show();
172   }
173
174   scoped_ptr<DemoScreen> screen_;
175
176   scoped_ptr<DemoWindowTreeClient> window_tree_client_;
177
178   scoped_ptr<DemoWindowDelegate> delegate1_;
179   scoped_ptr<DemoWindowDelegate> delegate2_;
180   scoped_ptr<DemoWindowDelegate> delegate21_;
181
182   aura::Window* window1_;
183   aura::Window* window2_;
184   aura::Window* window21_;
185
186   RemotePtr<Shell> shell_;
187   scoped_ptr<WindowTreeHostMojo> root_window_host_;
188   scoped_ptr<aura::RootWindow> root_window_;
189 };
190
191 }  // namespace examples
192 }  // namespace mojo
193
194 extern "C" AURA_DEMO_EXPORT MojoResult CDECL MojoMain(
195     MojoHandle shell_handle) {
196   CommandLine::Init(0, NULL);
197   base::AtExitManager at_exit;
198   base::MessageLoop loop;
199   mojo::GLES2Initializer gles2;
200
201   // TODO(beng): This crashes in a DCHECK on X11 because this thread's
202   //             MessageLoop is not of TYPE_UI. I think we need a way to build
203   //             Aura that doesn't define platform-specific stuff.
204   aura::Env::CreateInstance();
205   mojo::examples::AuraDemo app(
206       mojo::MakeScopedHandle(mojo::MessagePipeHandle(shell_handle)).Pass());
207   loop.Run();
208
209   return MOJO_RESULT_OK;
210 }