Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / mojo / services / view_manager / window_tree_host_impl.cc
blob793225d3c5ec8acf41fc8f25c355c1e9092b56ca
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/public/c/gles2/gles2.h"
6 #include "mojo/services/public/cpp/geometry/geometry_type_converters.h"
7 #include "mojo/services/public/cpp/input_events/input_events_type_converters.h"
8 #include "mojo/services/view_manager/connection_manager.h"
9 #include "mojo/services/view_manager/context_factory_impl.h"
10 #include "mojo/services/view_manager/window_tree_host_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"
21 namespace mojo {
22 namespace service {
24 // TODO(sky): nuke this. It shouldn't be static.
25 // 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 {
32 public:
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;
44 private:
45 aura::Window* child_;
47 DISALLOW_COPY_AND_ASSIGN(RootLayoutManager);
50 void RootLayoutManager::OnWindowResized() {
51 if (child_)
52 child_->SetBounds(gfx::Rect(child_->parent()->bounds().size()));
55 void RootLayoutManager::OnWindowAddedToLayout(aura::Window* child) {
56 DCHECK(!child_);
57 child_ = 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 GpuPtr gpu_service,
71 const gfx::Rect& bounds,
72 const Callback<void()>& compositor_created_callback,
73 const Callback<void()>& native_viewport_closed_callback,
74 const Callback<void(EventPtr)>& event_received_callback)
75 : native_viewport_(viewport.Pass()),
76 gpu_service_(gpu_service.Pass()),
77 widget_(gfx::kNullAcceleratedWidget),
78 compositor_created_callback_(compositor_created_callback),
79 native_viewport_closed_callback_(native_viewport_closed_callback),
80 event_received_callback_(event_received_callback),
81 bounds_(bounds) {
82 native_viewport_.set_client(this);
83 native_viewport_->Create(Rect::From(bounds));
84 native_viewport_->Show();
86 window()->SetLayoutManager(new RootLayoutManager());
89 WindowTreeHostImpl::~WindowTreeHostImpl() {
90 DestroyCompositor();
91 DestroyDispatcher();
92 delete context_factory_;
93 context_factory_ = NULL;
96 ////////////////////////////////////////////////////////////////////////////////
97 // WindowTreeHostImpl, aura::WindowTreeHost implementation:
99 ui::EventSource* WindowTreeHostImpl::GetEventSource() {
100 return this;
103 gfx::AcceleratedWidget WindowTreeHostImpl::GetAcceleratedWidget() {
104 return widget_;
107 void WindowTreeHostImpl::Show() {
108 window()->Show();
111 void WindowTreeHostImpl::Hide() {
112 native_viewport_->Hide();
113 window()->Hide();
116 gfx::Rect WindowTreeHostImpl::GetBounds() const {
117 return bounds_;
120 void WindowTreeHostImpl::SetBounds(const gfx::Rect& bounds) {
121 native_viewport_->SetBounds(Rect::From(bounds));
124 gfx::Point WindowTreeHostImpl::GetLocationOnNativeScreen() const {
125 return gfx::Point(0, 0);
128 void WindowTreeHostImpl::SetCapture() {
129 NOTIMPLEMENTED();
132 void WindowTreeHostImpl::ReleaseCapture() {
133 NOTIMPLEMENTED();
136 void WindowTreeHostImpl::PostNativeEvent(
137 const base::NativeEvent& native_event) {
138 NOTIMPLEMENTED();
141 void WindowTreeHostImpl::SetCursorNative(gfx::NativeCursor cursor) {
142 NOTIMPLEMENTED();
145 void WindowTreeHostImpl::MoveCursorToNative(const gfx::Point& location) {
146 NOTIMPLEMENTED();
149 void WindowTreeHostImpl::OnCursorVisibilityChangedNative(bool show) {
150 NOTIMPLEMENTED();
153 ////////////////////////////////////////////////////////////////////////////////
154 // WindowTreeHostImpl, ui::EventSource implementation:
156 ui::EventProcessor* WindowTreeHostImpl::GetEventProcessor() {
157 return dispatcher();
160 ////////////////////////////////////////////////////////////////////////////////
161 // WindowTreeHostImpl, NativeViewportClient implementation:
163 void WindowTreeHostImpl::OnCreated(uint64_t native_viewport_id) {
164 CommandBufferPtr cb;
165 gpu_service_->CreateOnscreenGLES2Context(
166 native_viewport_id, Size::From(bounds_.size()), Get(&cb));
167 widget_ = bit_cast<gfx::AcceleratedWidget>(
168 static_cast<uintptr_t>(native_viewport_id));
170 // The ContextFactory must exist before any Compositors are created.
171 if (context_factory_) {
172 delete context_factory_;
173 context_factory_ = NULL;
175 context_factory_ = new ContextFactoryImpl(cb.PassMessagePipe());
176 aura::Env::GetInstance()->set_context_factory(context_factory_);
178 CreateCompositor(gfx::kNullAcceleratedWidget);
179 compositor_created_callback_.Run();
182 void WindowTreeHostImpl::OnBoundsChanged(RectPtr bounds) {
183 bounds_ = bounds.To<gfx::Rect>();
184 if (context_factory_)
185 OnHostResized(bounds_.size());
188 void WindowTreeHostImpl::OnDestroyed() {
189 DestroyCompositor();
190 native_viewport_closed_callback_.Run();
191 // TODO(beng): quit the message loop once we are on our own thread.
194 void WindowTreeHostImpl::OnEvent(EventPtr event,
195 const mojo::Callback<void()>& callback) {
196 event_received_callback_.Run(event.Pass());
197 callback.Run();
200 } // namespace service
201 } // namespace mojo