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/examples/aura_demo/root_window_host_mojo.h"
7 #include "mojo/examples/aura_demo/demo_context_factory.h"
8 #include "mojo/examples/compositor_app/gles2_client_impl.h"
9 #include "mojo/public/gles2/gles2.h"
10 #include "mojo/services/native_viewport/geometry_conversions.h"
11 #include "ui/aura/env.h"
12 #include "ui/aura/window.h"
13 #include "ui/aura/window_tree_host_delegate.h"
14 #include "ui/compositor/compositor.h"
15 #include "ui/events/event.h"
16 #include "ui/events/event_constants.h"
17 #include "ui/gfx/geometry/insets.h"
18 #include "ui/gfx/geometry/rect.h"
24 ui::ContextFactory* WindowTreeHostMojo::context_factory_ = NULL;
26 ////////////////////////////////////////////////////////////////////////////////
27 // WindowTreeHostMojo, public:
29 WindowTreeHostMojo::WindowTreeHostMojo(
30 ScopedMessagePipeHandle viewport_handle,
31 const gfx::Rect& bounds,
32 const base::Callback<void()>& compositor_created_callback)
33 : native_viewport_(viewport_handle.Pass(), this),
34 compositor_created_callback_(compositor_created_callback) {
35 AllocationScope scope;
36 native_viewport_->Create(bounds);
38 ScopedMessagePipeHandle gles2_handle;
39 ScopedMessagePipeHandle gles2_client_handle;
40 CreateMessagePipe(&gles2_handle, &gles2_client_handle);
42 // The ContextFactory must exist before any Compositors are created.
43 if (!context_factory_) {
44 scoped_ptr<DemoContextFactory> cf(new DemoContextFactory(this));
46 context_factory_ = cf.release();
47 ui::ContextFactory::SetInstance(context_factory_);
49 CHECK(context_factory_) << "No GL bindings.";
51 gles2_client_.reset(new GLES2ClientImpl(
53 base::Bind(&WindowTreeHostMojo::DidCreateContext,
54 base::Unretained(this))));
55 native_viewport_->CreateGLES2Context(gles2_client_handle.Pass());
58 WindowTreeHostMojo::~WindowTreeHostMojo() {}
60 ////////////////////////////////////////////////////////////////////////////////
61 // WindowTreeHostMojo, aura::WindowTreeHost implementation:
63 aura::RootWindow* WindowTreeHostMojo::GetRootWindow() {
64 return delegate_->AsRootWindow();
67 gfx::AcceleratedWidget WindowTreeHostMojo::GetAcceleratedWidget() {
68 NOTIMPLEMENTED() << "GetAcceleratedWidget";
69 return gfx::kNullAcceleratedWidget;
72 void WindowTreeHostMojo::Show() {
73 native_viewport_->Show();
76 void WindowTreeHostMojo::Hide() {
80 void WindowTreeHostMojo::ToggleFullScreen() {
84 gfx::Rect WindowTreeHostMojo::GetBounds() const {
88 void WindowTreeHostMojo::SetBounds(const gfx::Rect& bounds) {
89 AllocationScope scope;
90 native_viewport_->SetBounds(bounds);
93 gfx::Insets WindowTreeHostMojo::GetInsets() const {
98 void WindowTreeHostMojo::SetInsets(const gfx::Insets& insets) {
102 gfx::Point WindowTreeHostMojo::GetLocationOnNativeScreen() const {
103 return gfx::Point(0, 0);
106 void WindowTreeHostMojo::SetCapture() {
110 void WindowTreeHostMojo::ReleaseCapture() {
114 void WindowTreeHostMojo::SetCursor(gfx::NativeCursor cursor) {
118 bool WindowTreeHostMojo::QueryMouseLocation(gfx::Point* location_return) {
119 NOTIMPLEMENTED() << "QueryMouseLocation";
123 bool WindowTreeHostMojo::ConfineCursorToRootWindow() {
128 void WindowTreeHostMojo::UnConfineCursor() {
132 void WindowTreeHostMojo::OnCursorVisibilityChanged(bool show) {
136 void WindowTreeHostMojo::MoveCursorTo(const gfx::Point& location) {
140 void WindowTreeHostMojo::PostNativeEvent(
141 const base::NativeEvent& native_event) {
145 void WindowTreeHostMojo::OnDeviceScaleFactorChanged(float device_scale_factor) {
149 void WindowTreeHostMojo::PrepareForShutdown() {
153 ////////////////////////////////////////////////////////////////////////////////
154 // WindowTreeHostMojo, NativeViewportClient implementation:
156 void WindowTreeHostMojo::OnCreated() {
159 void WindowTreeHostMojo::OnBoundsChanged(const Rect& bounds) {
160 bounds_ = gfx::Rect(bounds.position().x(), bounds.position().y(),
161 bounds.size().width(), bounds.size().height());
162 window()->SetBounds(gfx::Rect(bounds_.size()));
165 void WindowTreeHostMojo::OnDestroyed() {
166 base::MessageLoop::current()->Quit();
169 void WindowTreeHostMojo::OnEvent(const Event& event) {
170 if (!event.location().is_null())
171 native_viewport_->AckEvent(event);
173 switch (event.action()) {
174 case ui::ET_MOUSE_PRESSED:
175 case ui::ET_MOUSE_DRAGGED:
176 case ui::ET_MOUSE_RELEASED:
177 case ui::ET_MOUSE_MOVED:
178 case ui::ET_MOUSE_ENTERED:
179 case ui::ET_MOUSE_EXITED: {
180 gfx::Point location(event.location().x(), event.location().y());
181 ui::MouseEvent ev(static_cast<ui::EventType>(event.action()), location,
182 location, event.flags(), 0);
183 delegate_->OnHostMouseEvent(&ev);
186 case ui::ET_KEY_PRESSED:
187 case ui::ET_KEY_RELEASED: {
189 static_cast<ui::EventType>(event.action()),
190 static_cast<ui::KeyboardCode>(event.key_data().key_code()),
191 event.flags(), event.key_data().is_char());
192 delegate_->OnHostKeyEvent(&ev);
195 // TODO(beng): touch, etc.
199 ////////////////////////////////////////////////////////////////////////////////
200 // WindowTreeHostMojo, private:
202 void WindowTreeHostMojo::DidCreateContext(gfx::Size viewport_size) {
203 CreateCompositor(GetAcceleratedWidget());
204 compositor_created_callback_.Run();
205 NotifyHostResized(viewport_size);
208 } // namespace examples