OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/services/native_viewport/native_viewport_service.h" | 5 #include "mojo/services/native_viewport/native_viewport_service.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/message_loop/message_loop.h" | 8 #include "base/message_loop/message_loop.h" |
9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
10 #include "mojo/public/cpp/bindings/allocation_scope.h" | 10 #include "mojo/public/cpp/bindings/allocation_scope.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 | 27 |
28 } | 28 } |
29 | 29 |
30 class NativeViewportImpl | 30 class NativeViewportImpl |
31 : public ServiceConnection<mojo::NativeViewport, | 31 : public ServiceConnection<mojo::NativeViewport, |
32 NativeViewportImpl, | 32 NativeViewportImpl, |
33 shell::Context>, | 33 shell::Context>, |
34 public NativeViewportDelegate { | 34 public NativeViewportDelegate { |
35 public: | 35 public: |
36 NativeViewportImpl() | 36 NativeViewportImpl() |
37 : widget_(gfx::kNullAcceleratedWidget), | 37 : client_(NULL), |
| 38 widget_(gfx::kNullAcceleratedWidget), |
38 waiting_for_event_ack_(false) {} | 39 waiting_for_event_ack_(false) {} |
39 virtual ~NativeViewportImpl() {} | 40 virtual ~NativeViewportImpl() {} |
40 | 41 |
| 42 virtual void SetClient(NativeViewportClient* client) OVERRIDE { |
| 43 client_ = client; |
| 44 } |
| 45 |
41 virtual void Create(const Rect& bounds) OVERRIDE { | 46 virtual void Create(const Rect& bounds) OVERRIDE { |
42 native_viewport_ = | 47 native_viewport_ = |
43 services::NativeViewport::Create(context(), this); | 48 services::NativeViewport::Create(context(), this); |
44 native_viewport_->Init(bounds); | 49 native_viewport_->Init(bounds); |
45 client()->OnCreated(); | 50 client_->OnCreated(); |
46 OnBoundsChanged(bounds); | 51 OnBoundsChanged(bounds); |
47 } | 52 } |
48 | 53 |
49 virtual void Show() OVERRIDE { | 54 virtual void Show() OVERRIDE { |
50 native_viewport_->Show(); | 55 native_viewport_->Show(); |
51 } | 56 } |
52 | 57 |
53 virtual void Hide() OVERRIDE { | 58 virtual void Hide() OVERRIDE { |
54 native_viewport_->Hide(); | 59 native_viewport_->Hide(); |
55 } | 60 } |
56 | 61 |
57 virtual void Close() OVERRIDE { | 62 virtual void Close() OVERRIDE { |
58 command_buffer_.reset(); | 63 command_buffer_.reset(); |
59 DCHECK(native_viewport_); | 64 DCHECK(native_viewport_); |
60 native_viewport_->Close(); | 65 native_viewport_->Close(); |
61 } | 66 } |
62 | 67 |
63 virtual void SetBounds(const Rect& bounds) OVERRIDE { | 68 virtual void SetBounds(const Rect& bounds) OVERRIDE { |
64 gfx::Rect gfx_bounds(bounds.position().x(), bounds.position().y(), | 69 gfx::Rect gfx_bounds(bounds.position().x(), bounds.position().y(), |
65 bounds.size().width(), bounds.size().height()); | 70 bounds.size().width(), bounds.size().height()); |
66 native_viewport_->SetBounds(gfx_bounds); | 71 native_viewport_->SetBounds(gfx_bounds); |
67 } | 72 } |
68 | 73 |
69 virtual void CreateGLES2Context(ScopedMessagePipeHandle client_handle) | 74 virtual void CreateGLES2Context(ScopedMessagePipeHandle client_handle) |
70 OVERRIDE { | 75 OVERRIDE { |
71 if (command_buffer_ || command_buffer_handle_.is_valid()) { | 76 if (command_buffer_.get() || command_buffer_handle_.is_valid()) { |
72 LOG(ERROR) << "Can't create multiple contexts on a NativeViewport"; | 77 LOG(ERROR) << "Can't create multiple contexts on a NativeViewport"; |
73 return; | 78 return; |
74 } | 79 } |
75 | 80 |
76 // TODO(darin): | 81 // TODO(darin): CreateGLES2Context should accept a |CommandBufferPtr*|. |
77 // CreateGLES2Context should accept a ScopedCommandBufferClientHandle once | 82 command_buffer_handle_ = client_handle.Pass(); |
78 // it is possible to import interface definitions from another module. For | |
79 // now, we just kludge it. | |
80 command_buffer_handle_.reset( | |
81 InterfaceHandle<CommandBufferClient>(client_handle.release().value())); | |
82 | 83 |
83 CreateCommandBufferIfNeeded(); | 84 CreateCommandBufferIfNeeded(); |
84 } | 85 } |
85 | 86 |
86 void AckEvent() { | 87 void AckEvent() { |
87 waiting_for_event_ack_ = false; | 88 waiting_for_event_ack_ = false; |
88 } | 89 } |
89 | 90 |
90 void CreateCommandBufferIfNeeded() { | 91 void CreateCommandBufferIfNeeded() { |
91 if (!command_buffer_handle_.is_valid()) | 92 if (!command_buffer_handle_.is_valid()) |
92 return; | 93 return; |
93 DCHECK(!command_buffer_.get()); | 94 DCHECK(!command_buffer_.get()); |
94 if (widget_ == gfx::kNullAcceleratedWidget) | 95 if (widget_ == gfx::kNullAcceleratedWidget) |
95 return; | 96 return; |
96 gfx::Size size = native_viewport_->GetSize(); | 97 gfx::Size size = native_viewport_->GetSize(); |
97 if (size.IsEmpty()) | 98 if (size.IsEmpty()) |
98 return; | 99 return; |
99 command_buffer_.reset(new CommandBufferImpl( | 100 command_buffer_.Bind( |
100 command_buffer_handle_.Pass(), widget_, native_viewport_->GetSize())); | 101 new CommandBufferImpl(widget_, native_viewport_->GetSize())); |
| 102 command_buffer_.ConfigureStub(command_buffer_handle_.Pass()); |
101 } | 103 } |
102 | 104 |
103 virtual bool OnEvent(ui::Event* ui_event) OVERRIDE { | 105 virtual bool OnEvent(ui::Event* ui_event) OVERRIDE { |
104 // Must not return early before updating capture. | 106 // Must not return early before updating capture. |
105 switch (ui_event->type()) { | 107 switch (ui_event->type()) { |
106 case ui::ET_MOUSE_PRESSED: | 108 case ui::ET_MOUSE_PRESSED: |
107 case ui::ET_TOUCH_PRESSED: | 109 case ui::ET_TOUCH_PRESSED: |
108 native_viewport_->SetCapture(); | 110 native_viewport_->SetCapture(); |
109 break; | 111 break; |
110 case ui::ET_MOUSE_RELEASED: | 112 case ui::ET_MOUSE_RELEASED: |
(...skipping 29 matching lines...) Expand all Loading... |
140 touch_data.set_pointer_id(touch_event->touch_id()); | 142 touch_data.set_pointer_id(touch_event->touch_id()); |
141 event.set_touch_data(touch_data.Finish()); | 143 event.set_touch_data(touch_data.Finish()); |
142 } else if (ui_event->IsKeyEvent()) { | 144 } else if (ui_event->IsKeyEvent()) { |
143 ui::KeyEvent* key_event = static_cast<ui::KeyEvent*>(ui_event); | 145 ui::KeyEvent* key_event = static_cast<ui::KeyEvent*>(ui_event); |
144 KeyData::Builder key_data; | 146 KeyData::Builder key_data; |
145 key_data.set_key_code(key_event->key_code()); | 147 key_data.set_key_code(key_event->key_code()); |
146 key_data.set_is_char(key_event->is_char()); | 148 key_data.set_is_char(key_event->is_char()); |
147 event.set_key_data(key_data.Finish()); | 149 event.set_key_data(key_data.Finish()); |
148 } | 150 } |
149 | 151 |
150 client()->OnEvent(event.Finish(), | 152 client_->OnEvent(event.Finish(), |
151 base::Bind(&NativeViewportImpl::AckEvent, | 153 base::Bind(&NativeViewportImpl::AckEvent, |
152 base::Unretained(this))); | 154 base::Unretained(this))); |
153 waiting_for_event_ack_ = true; | 155 waiting_for_event_ack_ = true; |
154 return false; | 156 return false; |
155 } | 157 } |
156 | 158 |
157 virtual void OnAcceleratedWidgetAvailable( | 159 virtual void OnAcceleratedWidgetAvailable( |
158 gfx::AcceleratedWidget widget) OVERRIDE { | 160 gfx::AcceleratedWidget widget) OVERRIDE { |
159 widget_ = widget; | 161 widget_ = widget; |
160 CreateCommandBufferIfNeeded(); | 162 CreateCommandBufferIfNeeded(); |
161 } | 163 } |
162 | 164 |
163 virtual void OnBoundsChanged(const gfx::Rect& bounds) OVERRIDE { | 165 virtual void OnBoundsChanged(const gfx::Rect& bounds) OVERRIDE { |
164 CreateCommandBufferIfNeeded(); | 166 CreateCommandBufferIfNeeded(); |
165 AllocationScope scope; | 167 AllocationScope scope; |
166 client()->OnBoundsChanged(bounds); | 168 client_->OnBoundsChanged(bounds); |
167 } | 169 } |
168 | 170 |
169 virtual void OnDestroyed() OVERRIDE { | 171 virtual void OnDestroyed() OVERRIDE { |
170 command_buffer_.reset(); | 172 command_buffer_.reset(); |
171 client()->OnDestroyed(); | 173 client_->OnDestroyed(); |
172 base::MessageLoop::current()->Quit(); | 174 base::MessageLoop::current()->Quit(); |
173 } | 175 } |
174 | 176 |
175 private: | 177 private: |
| 178 NativeViewportClient* client_; |
176 gfx::AcceleratedWidget widget_; | 179 gfx::AcceleratedWidget widget_; |
177 scoped_ptr<services::NativeViewport> native_viewport_; | 180 scoped_ptr<services::NativeViewport> native_viewport_; |
178 ScopedCommandBufferClientHandle command_buffer_handle_; | 181 ScopedMessagePipeHandle command_buffer_handle_; |
179 scoped_ptr<CommandBufferImpl> command_buffer_; | 182 CommandBufferPtr command_buffer_; |
180 bool waiting_for_event_ack_; | 183 bool waiting_for_event_ack_; |
181 }; | 184 }; |
182 | 185 |
183 } // namespace services | 186 } // namespace services |
184 } // namespace mojo | 187 } // namespace mojo |
185 | 188 |
186 | 189 |
187 MOJO_NATIVE_VIEWPORT_EXPORT mojo::Application* | 190 MOJO_NATIVE_VIEWPORT_EXPORT mojo::Application* |
188 CreateNativeViewportService(mojo::shell::Context* context, | 191 CreateNativeViewportService(mojo::shell::Context* context, |
189 mojo::ScopedShellHandle shell_handle) { | 192 mojo::ScopedMessagePipeHandle shell_handle) { |
190 mojo::Application* app = new mojo::Application(shell_handle.Pass()); | 193 mojo::Application* app = new mojo::Application(shell_handle.Pass()); |
191 app->AddServiceConnector( | 194 app->AddServiceConnector( |
192 new mojo::ServiceConnector<mojo::services::NativeViewportImpl, | 195 new mojo::ServiceConnector<mojo::services::NativeViewportImpl, |
193 mojo::shell::Context>(context)); | 196 mojo::shell::Context>(context)); |
194 return app; | 197 return app; |
195 } | 198 } |
196 | 199 |
OLD | NEW |