1 // Copyright (c) 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/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/view_manager/context_factory_impl.h"
10 #include "ui/aura/env.h"
11 #include "ui/aura/window.h"
12 #include "ui/aura/window_event_dispatcher.h"
13 #include "ui/compositor/compositor.h"
14 #include "ui/events/event.h"
15 #include "ui/events/event_constants.h"
16 #include "ui/gfx/geometry/insets.h"
17 #include "ui/gfx/geometry/rect.h"
20 namespace view_manager {
23 // TODO(sky): nuke this. It shouldn't be static.
25 ContextFactoryImpl* WindowTreeHostImpl::context_factory_ = NULL;
27 ////////////////////////////////////////////////////////////////////////////////
28 // WindowTreeHostImpl, public:
30 WindowTreeHostImpl::WindowTreeHostImpl(
31 NativeViewportPtr viewport,
32 const gfx::Rect& bounds,
33 const base::Callback<void()>& compositor_created_callback)
34 : native_viewport_(viewport.Pass()),
35 compositor_created_callback_(compositor_created_callback),
37 native_viewport_.set_client(this);
38 native_viewport_->Create(Rect::From(bounds));
41 native_viewport_->CreateGLES2Context(
42 MakeRequest<CommandBuffer>(pipe.handle0.Pass()));
44 // The ContextFactory must exist before any Compositors are created.
45 if (context_factory_) {
46 delete context_factory_;
47 context_factory_ = NULL;
49 context_factory_ = new ContextFactoryImpl(pipe.handle1.Pass());
50 aura::Env::GetInstance()->set_context_factory(context_factory_);
51 CHECK(context_factory_) << "No GL bindings.";
54 WindowTreeHostImpl::~WindowTreeHostImpl() {
59 ////////////////////////////////////////////////////////////////////////////////
60 // WindowTreeHostImpl, aura::WindowTreeHost implementation:
62 ui::EventSource* WindowTreeHostImpl::GetEventSource() {
66 gfx::AcceleratedWidget WindowTreeHostImpl::GetAcceleratedWidget() {
67 NOTIMPLEMENTED() << "GetAcceleratedWidget";
68 return gfx::kNullAcceleratedWidget;
71 void WindowTreeHostImpl::Show() {
73 native_viewport_->Show();
76 void WindowTreeHostImpl::Hide() {
77 native_viewport_->Hide();
81 gfx::Rect WindowTreeHostImpl::GetBounds() const {
85 void WindowTreeHostImpl::SetBounds(const gfx::Rect& bounds) {
86 native_viewport_->SetBounds(Rect::From(bounds));
89 gfx::Point WindowTreeHostImpl::GetLocationOnNativeScreen() const {
90 return gfx::Point(0, 0);
93 void WindowTreeHostImpl::SetCapture() {
97 void WindowTreeHostImpl::ReleaseCapture() {
101 void WindowTreeHostImpl::PostNativeEvent(
102 const base::NativeEvent& native_event) {
106 void WindowTreeHostImpl::OnDeviceScaleFactorChanged(float device_scale_factor) {
110 void WindowTreeHostImpl::SetCursorNative(gfx::NativeCursor cursor) {
114 void WindowTreeHostImpl::MoveCursorToNative(const gfx::Point& location) {
118 void WindowTreeHostImpl::OnCursorVisibilityChangedNative(bool show) {
122 ////////////////////////////////////////////////////////////////////////////////
123 // WindowTreeHostImpl, ui::EventSource implementation:
125 ui::EventProcessor* WindowTreeHostImpl::GetEventProcessor() {
129 ////////////////////////////////////////////////////////////////////////////////
130 // WindowTreeHostImpl, NativeViewportClient implementation:
132 void WindowTreeHostImpl::OnCreated() {
133 CreateCompositor(GetAcceleratedWidget());
134 compositor_created_callback_.Run();
137 void WindowTreeHostImpl::OnBoundsChanged(RectPtr bounds) {
138 bounds_ = bounds.To<gfx::Rect>();
139 OnHostResized(bounds_.size());
142 void WindowTreeHostImpl::OnDestroyed() {
143 base::MessageLoop::current()->Quit();
146 void WindowTreeHostImpl::OnEvent(EventPtr event,
147 const mojo::Callback<void()>& callback) {
148 switch (event->action) {
149 case ui::ET_MOUSE_PRESSED:
150 case ui::ET_MOUSE_DRAGGED:
151 case ui::ET_MOUSE_RELEASED:
152 case ui::ET_MOUSE_MOVED:
153 case ui::ET_MOUSE_ENTERED:
154 case ui::ET_MOUSE_EXITED: {
155 gfx::Point location(event->location->x, event->location->y);
156 ui::MouseEvent ev(static_cast<ui::EventType>(event->action), location,
157 location, event->flags, 0);
158 SendEventToProcessor(&ev);
161 case ui::ET_KEY_PRESSED:
162 case ui::ET_KEY_RELEASED: {
164 static_cast<ui::EventType>(event->action),
165 static_cast<ui::KeyboardCode>(event->key_data->key_code),
166 event->flags, event->key_data->is_char);
167 SendEventToProcessor(&ev);
170 // TODO(beng): touch, etc.
175 } // namespace service
176 } // namespace view_manager