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.
5 #include "mojo/services/view_manager/root_node_manager.h"
6 #include "mojo/services/view_manager/window_tree_host_impl.h"
7 #include "mojo/public/c/gles2/gles2.h"
8 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h"
9 #include "mojo/services/public/cpp/input_events/input_events_type_converters.h"
10 #include "mojo/services/view_manager/context_factory_impl.h"
11 #include "ui/aura/env.h"
12 #include "ui/aura/layout_manager.h"
13 #include "ui/aura/window.h"
14 #include "ui/aura/window_event_dispatcher.h"
15 #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"
24 // TODO(sky): nuke this. It shouldn't be static.
26 ContextFactoryImpl* WindowTreeHostImpl::context_factory_ = NULL;
28 ////////////////////////////////////////////////////////////////////////////////
29 // RootLayoutManager, layout management for the root window's (one) child
31 class RootLayoutManager : public aura::LayoutManager {
33 RootLayoutManager() : child_(NULL) {}
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;
47 DISALLOW_COPY_AND_ASSIGN(RootLayoutManager);
50 void RootLayoutManager::OnWindowResized() {
52 child_->SetBounds(gfx::Rect(child_->parent()->bounds().size()));
55 void RootLayoutManager::OnWindowAddedToLayout(aura::Window* child) {
60 void RootLayoutManager::SetChildBounds(aura::Window* child,
61 const gfx::Rect& requested_bounds) {
62 SetChildBoundsDirect(child, gfx::Rect(requested_bounds.size()));
65 ////////////////////////////////////////////////////////////////////////////////
66 // WindowTreeHostImpl, public:
68 WindowTreeHostImpl::WindowTreeHostImpl(
69 NativeViewportPtr viewport,
70 const gfx::Rect& bounds,
71 const Callback<void()>& compositor_created_callback,
72 const Callback<void()>& native_viewport_closed_callback,
73 const Callback<void(EventPtr)>& event_received_callback)
74 : native_viewport_(viewport.Pass()),
75 compositor_created_callback_(compositor_created_callback),
76 native_viewport_closed_callback_(native_viewport_closed_callback),
77 event_received_callback_(event_received_callback),
79 native_viewport_.set_client(this);
80 native_viewport_->Create(Rect::From(bounds));
83 native_viewport_->CreateGLES2Context(
84 MakeRequest<CommandBuffer>(pipe.handle0.Pass()));
86 // The ContextFactory must exist before any Compositors are created.
87 if (context_factory_) {
88 delete context_factory_;
89 context_factory_ = NULL;
91 context_factory_ = new ContextFactoryImpl(pipe.handle1.Pass());
92 aura::Env::GetInstance()->set_context_factory(context_factory_);
94 window()->SetLayoutManager(new RootLayoutManager());
97 WindowTreeHostImpl::~WindowTreeHostImpl() {
102 ////////////////////////////////////////////////////////////////////////////////
103 // WindowTreeHostImpl, aura::WindowTreeHost implementation:
105 ui::EventSource* WindowTreeHostImpl::GetEventSource() {
109 gfx::AcceleratedWidget WindowTreeHostImpl::GetAcceleratedWidget() {
110 NOTIMPLEMENTED() << "GetAcceleratedWidget";
111 return gfx::kNullAcceleratedWidget;
114 void WindowTreeHostImpl::Show() {
116 native_viewport_->Show();
119 void WindowTreeHostImpl::Hide() {
120 native_viewport_->Hide();
124 gfx::Rect WindowTreeHostImpl::GetBounds() const {
128 void WindowTreeHostImpl::SetBounds(const gfx::Rect& bounds) {
129 native_viewport_->SetBounds(Rect::From(bounds));
132 gfx::Point WindowTreeHostImpl::GetLocationOnNativeScreen() const {
133 return gfx::Point(0, 0);
136 void WindowTreeHostImpl::SetCapture() {
140 void WindowTreeHostImpl::ReleaseCapture() {
144 void WindowTreeHostImpl::PostNativeEvent(
145 const base::NativeEvent& native_event) {
149 void WindowTreeHostImpl::SetCursorNative(gfx::NativeCursor cursor) {
153 void WindowTreeHostImpl::MoveCursorToNative(const gfx::Point& location) {
157 void WindowTreeHostImpl::OnCursorVisibilityChangedNative(bool show) {
161 ////////////////////////////////////////////////////////////////////////////////
162 // WindowTreeHostImpl, ui::EventSource implementation:
164 ui::EventProcessor* WindowTreeHostImpl::GetEventProcessor() {
168 ////////////////////////////////////////////////////////////////////////////////
169 // WindowTreeHostImpl, NativeViewportClient implementation:
171 void WindowTreeHostImpl::OnCreated() {
172 CreateCompositor(GetAcceleratedWidget());
173 compositor_created_callback_.Run();
176 void WindowTreeHostImpl::OnBoundsChanged(RectPtr bounds) {
177 bounds_ = bounds.To<gfx::Rect>();
178 OnHostResized(bounds_.size());
181 void WindowTreeHostImpl::OnDestroyed(const mojo::Callback<void()>& callback) {
183 native_viewport_closed_callback_.Run();
184 // TODO(beng): quit the message loop once we are on our own thread.
188 void WindowTreeHostImpl::OnEvent(EventPtr event,
189 const mojo::Callback<void()>& callback) {
190 event_received_callback_.Run(event.Pass());
194 } // namespace service