OLD | NEW |
(Empty) | |
| 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/message_loop/message_loop.h" |
| 9 #include "mojo/common/bindings_support_impl.h" |
| 10 #include "mojo/examples/aura_demo/demo_screen.h" |
| 11 #include "mojo/examples/aura_demo/root_window_host_mojo.h" |
| 12 #include "mojo/examples/compositor_app/compositor_host.h" |
| 13 #include "mojo/examples/compositor_app/gles2_client_impl.h" |
| 14 #include "mojo/public/bindings/lib/bindings_support.h" |
| 15 #include "mojo/public/bindings/lib/remote_ptr.h" |
| 16 #include "mojo/public/gles2/gles2.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 ShellClientStub { |
| 120 public: |
| 121 explicit AuraDemo(ScopedMessagePipeHandle shell_handle) |
| 122 : shell_(shell_handle.Pass()) { |
| 123 shell_.SetPeer(this); |
| 124 |
| 125 screen_.reset(DemoScreen::Create()); |
| 126 gfx::Screen::SetScreenInstance(gfx::SCREEN_TYPE_NATIVE, screen_.get()); |
| 127 |
| 128 mojo::ScopedMessagePipeHandle client_handle, native_viewport_handle; |
| 129 CreateMessagePipe(&client_handle, &native_viewport_handle); |
| 130 root_window_host_.reset(new RootWindowHostMojo( |
| 131 native_viewport_handle.Pass(), |
| 132 base::Bind(&AuraDemo::HostContextCreated, base::Unretained(this)))); |
| 133 mojo::AllocationScope scope; |
| 134 shell_->Connect("mojo:mojo_native_viewport_service", client_handle.Pass()); |
| 135 } |
| 136 |
| 137 virtual void AcceptConnection(ScopedMessagePipeHandle handle) MOJO_OVERRIDE { |
| 138 NOTREACHED() << "AuraDemo can't be connected to."; |
| 139 } |
| 140 |
| 141 private: |
| 142 void HostContextCreated() { |
| 143 aura::RootWindow::CreateParams params(gfx::Rect(0, 0, 500, 500)); |
| 144 params.host = root_window_host_.get(); |
| 145 root_window_.reset(new aura::RootWindow(params)); |
| 146 root_window_host_->set_delegate(root_window_.get()); |
| 147 root_window_->Init(); |
| 148 |
| 149 window_tree_client_.reset(new DemoWindowTreeClient(root_window_->window())); |
| 150 |
| 151 delegate1_.reset(new DemoWindowDelegate(SK_ColorBLUE)); |
| 152 window1_ = new aura::Window(delegate1_.get()); |
| 153 window1_->Init(ui::LAYER_TEXTURED); |
| 154 window1_->SetBounds(gfx::Rect(100, 100, 400, 400)); |
| 155 window1_->Show(); |
| 156 root_window_->window()->AddChild(window1_); |
| 157 |
| 158 delegate2_.reset(new DemoWindowDelegate(SK_ColorRED)); |
| 159 window2_ = new aura::Window(delegate2_.get()); |
| 160 window2_->Init(ui::LAYER_TEXTURED); |
| 161 window2_->SetBounds(gfx::Rect(200, 200, 350, 350)); |
| 162 window2_->Show(); |
| 163 root_window_->window()->AddChild(window2_); |
| 164 |
| 165 delegate21_.reset(new DemoWindowDelegate(SK_ColorGREEN)); |
| 166 window21_ = new aura::Window(delegate21_.get()); |
| 167 window21_->Init(ui::LAYER_TEXTURED); |
| 168 window21_->SetBounds(gfx::Rect(10, 10, 50, 50)); |
| 169 window21_->Show(); |
| 170 window2_->AddChild(window21_); |
| 171 |
| 172 root_window_->host()->Show(); |
| 173 } |
| 174 |
| 175 scoped_ptr<DemoScreen> screen_; |
| 176 |
| 177 scoped_ptr<DemoWindowTreeClient> window_tree_client_; |
| 178 |
| 179 scoped_ptr<DemoWindowDelegate> delegate1_; |
| 180 scoped_ptr<DemoWindowDelegate> delegate2_; |
| 181 scoped_ptr<DemoWindowDelegate> delegate21_; |
| 182 |
| 183 aura::Window* window1_; |
| 184 aura::Window* window2_; |
| 185 aura::Window* window21_; |
| 186 |
| 187 mojo::RemotePtr<Shell> shell_; |
| 188 scoped_ptr<RootWindowHostMojo> root_window_host_; |
| 189 scoped_ptr<aura::RootWindow> root_window_; |
| 190 }; |
| 191 |
| 192 } // namespace examples |
| 193 } // namespace mojo |
| 194 |
| 195 extern "C" AURA_DEMO_EXPORT MojoResult CDECL MojoMain( |
| 196 MojoHandle shell_handle) { |
| 197 base::MessageLoop loop; |
| 198 mojo::common::BindingsSupportImpl bindings_support_impl; |
| 199 mojo::BindingsSupport::Set(&bindings_support_impl); |
| 200 MojoGLES2Initialize(); |
| 201 |
| 202 // TODO(beng): This crashes in a DCHECK on X11 because this thread's |
| 203 // MessageLoop is not of TYPE_UI. I think we need a way to build |
| 204 // Aura that doesn't define platform-specific stuff. |
| 205 aura::Env::CreateInstance(); |
| 206 mojo::examples::AuraDemo app( |
| 207 mojo::MakeScopedHandle(mojo::MessagePipeHandle(shell_handle)).Pass()); |
| 208 loop.Run(); |
| 209 |
| 210 MojoGLES2Terminate(); |
| 211 mojo::BindingsSupport::Set(NULL); |
| 212 return MOJO_RESULT_OK; |
| 213 } |
OLD | NEW |