blob: 9f201b0845a6a45905710b4198d6f8791f04d8b0 [file] [log] [blame]
Ben Murdoch116680a2014-07-20 18:25:52 -07001// Copyright 2013 The Chromium Authors. All rights reserved.
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Murdoch116680a2014-07-20 18:25:52 -07005#include "mojo/services/view_manager/root_node_manager.h"
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +01006#include "mojo/services/view_manager/window_tree_host_impl.h"
Ben Murdoch4ad1aa42014-04-01 10:55:12 +01007#include "mojo/public/c/gles2/gles2.h"
Torne (Richard Coles)cedac222014-06-03 10:58:34 +01008#include "mojo/services/public/cpp/geometry/geometry_type_converters.h"
Ben Murdoch116680a2014-07-20 18:25:52 -07009#include "mojo/services/public/cpp/input_events/input_events_type_converters.h"
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +010010#include "mojo/services/view_manager/context_factory_impl.h"
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000011#include "ui/aura/env.h"
Ben Murdoch116680a2014-07-20 18:25:52 -070012#include "ui/aura/layout_manager.h"
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000013#include "ui/aura/window.h"
Torne (Richard Coles)a1401312014-03-18 10:20:56 +000014#include "ui/aura/window_event_dispatcher.h"
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000015#include "ui/compositor/compositor.h"
16#include "ui/events/event.h"
17#include "ui/events/event_constants.h"
18#include "ui/gfx/geometry/insets.h"
19#include "ui/gfx/geometry/rect.h"
20
21namespace mojo {
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +010022namespace service {
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000023
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +010024// TODO(sky): nuke this. It shouldn't be static.
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000025// static
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +010026ContextFactoryImpl* WindowTreeHostImpl::context_factory_ = NULL;
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000027
28////////////////////////////////////////////////////////////////////////////////
Ben Murdoch116680a2014-07-20 18:25:52 -070029// RootLayoutManager, layout management for the root window's (one) child
30
31class RootLayoutManager : public aura::LayoutManager {
32 public:
33 RootLayoutManager() : child_(NULL) {}
34
35 // Overridden from aura::LayoutManager
36 virtual void OnWindowResized() OVERRIDE;
37 virtual void OnWindowAddedToLayout(aura::Window* child) OVERRIDE;
38 virtual void OnWillRemoveWindowFromLayout(aura::Window* child) OVERRIDE {}
39 virtual void OnWindowRemovedFromLayout(aura::Window* child) OVERRIDE {}
40 virtual void OnChildWindowVisibilityChanged(aura::Window* child,
41 bool visible) OVERRIDE {}
42 virtual void SetChildBounds(aura::Window* child,
43 const gfx::Rect& requested_bounds) OVERRIDE;
44 private:
45 aura::Window* child_;
46
47 DISALLOW_COPY_AND_ASSIGN(RootLayoutManager);
48};
49
50void RootLayoutManager::OnWindowResized() {
51 if (child_)
52 child_->SetBounds(gfx::Rect(child_->parent()->bounds().size()));
53}
54
55void RootLayoutManager::OnWindowAddedToLayout(aura::Window* child) {
56 DCHECK(!child_);
57 child_ = child;
58}
59
60void RootLayoutManager::SetChildBounds(aura::Window* child,
61 const gfx::Rect& requested_bounds) {
62 SetChildBoundsDirect(child, gfx::Rect(requested_bounds.size()));
63}
64
65////////////////////////////////////////////////////////////////////////////////
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +010066// WindowTreeHostImpl, public:
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000067
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +010068WindowTreeHostImpl::WindowTreeHostImpl(
Torne (Richard Coles)0de60732014-05-15 12:16:31 +010069 NativeViewportPtr viewport,
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000070 const gfx::Rect& bounds,
Ben Murdoch116680a2014-07-20 18:25:52 -070071 const Callback<void()>& compositor_created_callback,
Torne (Richard Coles)6e8cce62014-08-19 13:00:08 +010072 const Callback<void()>& native_viewport_closed_callback,
73 const Callback<void(EventPtr)>& event_received_callback)
Torne (Richard Coles)0de60732014-05-15 12:16:31 +010074 : native_viewport_(viewport.Pass()),
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000075 compositor_created_callback_(compositor_created_callback),
Ben Murdoch116680a2014-07-20 18:25:52 -070076 native_viewport_closed_callback_(native_viewport_closed_callback),
Torne (Richard Coles)6e8cce62014-08-19 13:00:08 +010077 event_received_callback_(event_received_callback),
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000078 bounds_(bounds) {
Torne (Richard Coles)cedac222014-06-03 10:58:34 +010079 native_viewport_.set_client(this);
80 native_viewport_->Create(Rect::From(bounds));
Torne (Richard Coles)0de60732014-05-15 12:16:31 +010081
Torne (Richard Coles)cedac222014-06-03 10:58:34 +010082 MessagePipe pipe;
83 native_viewport_->CreateGLES2Context(
84 MakeRequest<CommandBuffer>(pipe.handle0.Pass()));
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000085
86 // The ContextFactory must exist before any Compositors are created.
Torne (Richard Coles)010d83a2014-05-14 12:12:37 +010087 if (context_factory_) {
Torne (Richard Coles)010d83a2014-05-14 12:12:37 +010088 delete context_factory_;
89 context_factory_ = NULL;
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000090 }
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +010091 context_factory_ = new ContextFactoryImpl(pipe.handle1.Pass());
Torne (Richard Coles)cedac222014-06-03 10:58:34 +010092 aura::Env::GetInstance()->set_context_factory(context_factory_);
Ben Murdoch116680a2014-07-20 18:25:52 -070093
94 window()->SetLayoutManager(new RootLayoutManager());
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +000095}
96
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +010097WindowTreeHostImpl::~WindowTreeHostImpl() {
Torne (Richard Coles)010d83a2014-05-14 12:12:37 +010098 DestroyCompositor();
99 DestroyDispatcher();
100}
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000101
102////////////////////////////////////////////////////////////////////////////////
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100103// WindowTreeHostImpl, aura::WindowTreeHost implementation:
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000104
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100105ui::EventSource* WindowTreeHostImpl::GetEventSource() {
Bo Liu5c02ac12014-05-01 10:37:37 -0700106 return this;
107}
108
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100109gfx::AcceleratedWidget WindowTreeHostImpl::GetAcceleratedWidget() {
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000110 NOTIMPLEMENTED() << "GetAcceleratedWidget";
111 return gfx::kNullAcceleratedWidget;
112}
113
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100114void WindowTreeHostImpl::Show() {
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000115 window()->Show();
116 native_viewport_->Show();
117}
118
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100119void WindowTreeHostImpl::Hide() {
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000120 native_viewport_->Hide();
121 window()->Hide();
122}
123
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100124gfx::Rect WindowTreeHostImpl::GetBounds() const {
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000125 return bounds_;
126}
127
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100128void WindowTreeHostImpl::SetBounds(const gfx::Rect& bounds) {
Torne (Richard Coles)cedac222014-06-03 10:58:34 +0100129 native_viewport_->SetBounds(Rect::From(bounds));
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000130}
131
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100132gfx::Point WindowTreeHostImpl::GetLocationOnNativeScreen() const {
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000133 return gfx::Point(0, 0);
134}
135
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100136void WindowTreeHostImpl::SetCapture() {
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000137 NOTIMPLEMENTED();
138}
139
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100140void WindowTreeHostImpl::ReleaseCapture() {
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000141 NOTIMPLEMENTED();
142}
143
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100144void WindowTreeHostImpl::PostNativeEvent(
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000145 const base::NativeEvent& native_event) {
146 NOTIMPLEMENTED();
147}
148
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100149void WindowTreeHostImpl::SetCursorNative(gfx::NativeCursor cursor) {
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000150 NOTIMPLEMENTED();
151}
152
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100153void WindowTreeHostImpl::MoveCursorToNative(const gfx::Point& location) {
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000154 NOTIMPLEMENTED();
155}
156
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100157void WindowTreeHostImpl::OnCursorVisibilityChangedNative(bool show) {
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000158 NOTIMPLEMENTED();
159}
160
161////////////////////////////////////////////////////////////////////////////////
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100162// WindowTreeHostImpl, ui::EventSource implementation:
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000163
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100164ui::EventProcessor* WindowTreeHostImpl::GetEventProcessor() {
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000165 return dispatcher();
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000166}
167
168////////////////////////////////////////////////////////////////////////////////
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100169// WindowTreeHostImpl, NativeViewportClient implementation:
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000170
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100171void WindowTreeHostImpl::OnCreated() {
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000172 CreateCompositor(GetAcceleratedWidget());
173 compositor_created_callback_.Run();
174}
175
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100176void WindowTreeHostImpl::OnBoundsChanged(RectPtr bounds) {
Torne (Richard Coles)cedac222014-06-03 10:58:34 +0100177 bounds_ = bounds.To<gfx::Rect>();
Torne (Richard Coles)a1401312014-03-18 10:20:56 +0000178 OnHostResized(bounds_.size());
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000179}
180
Ben Murdoch116680a2014-07-20 18:25:52 -0700181void WindowTreeHostImpl::OnDestroyed(const mojo::Callback<void()>& callback) {
182 DestroyCompositor();
183 native_viewport_closed_callback_.Run();
184 // TODO(beng): quit the message loop once we are on our own thread.
185 callback.Run();
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000186}
187
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100188void WindowTreeHostImpl::OnEvent(EventPtr event,
Ben Murdoche5d81f52014-04-03 12:29:45 +0100189 const mojo::Callback<void()>& callback) {
Torne (Richard Coles)6e8cce62014-08-19 13:00:08 +0100190 event_received_callback_.Run(event.Pass());
Ben Murdoche5d81f52014-04-03 12:29:45 +0100191 callback.Run();
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000192};
193
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +0100194} // namespace service
Torne (Richard Coles)5d1f7b12014-02-21 12:16:55 +0000195} // namespace mojo