OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #include "mojo/services/native_viewport/native_viewport_service.h" |
| 6 |
| 7 #include "base/message_loop/message_loop.h" |
| 8 #include "base/time/time.h" |
| 9 #include "mojo/common/bindings_support_impl.h" |
| 10 #include "mojo/services/gles2/gles2_impl.h" |
| 11 #include "mojo/services/native_viewport/native_viewport.h" |
| 12 #include "mojom/native_viewport.h" |
| 13 #include "ui/events/event.h" |
| 14 |
| 15 namespace mojo { |
| 16 namespace services { |
| 17 namespace { |
| 18 |
| 19 bool IsRateLimitedEventType(ui::Event* event) { |
| 20 return event->type() == ui::ET_MOUSE_MOVED || |
| 21 event->type() == ui::ET_MOUSE_DRAGGED || |
| 22 event->type() == ui::ET_TOUCH_MOVED; |
| 23 } |
| 24 |
| 25 } |
| 26 |
| 27 class NativeViewportService::NativeViewportImpl |
| 28 : public NativeViewportStub, |
| 29 public NativeViewportDelegate { |
| 30 public: |
| 31 NativeViewportImpl(NativeViewportService* service, |
| 32 ScopedMessagePipeHandle client_handle) |
| 33 : service_(service), |
| 34 widget_(gfx::kNullAcceleratedWidget), |
| 35 waiting_for_event_ack_(false), |
| 36 pending_event_timestamp_(0), |
| 37 client_(client_handle.Pass()) { |
| 38 client_.SetPeer(this); |
| 39 } |
| 40 virtual ~NativeViewportImpl() {} |
| 41 |
| 42 virtual void Open() MOJO_OVERRIDE { |
| 43 native_viewport_ = |
| 44 services::NativeViewport::Create(service_->context_, this); |
| 45 native_viewport_->Init(); |
| 46 client_->OnCreated(); |
| 47 } |
| 48 |
| 49 virtual void Close() MOJO_OVERRIDE { |
| 50 gles2_.reset(); |
| 51 DCHECK(native_viewport_); |
| 52 native_viewport_->Close(); |
| 53 } |
| 54 |
| 55 virtual void CreateGLES2Context(ScopedMessagePipeHandle client_handle) |
| 56 MOJO_OVERRIDE { |
| 57 gles2_.reset(new GLES2Impl(client_handle.Pass())); |
| 58 CreateGLES2ContextIfNeeded(); |
| 59 } |
| 60 |
| 61 virtual void AckEvent(const Event& event) MOJO_OVERRIDE { |
| 62 DCHECK_EQ(event.time_stamp(), pending_event_timestamp_); |
| 63 waiting_for_event_ack_ = false; |
| 64 } |
| 65 |
| 66 void CreateGLES2ContextIfNeeded() { |
| 67 if (widget_ == gfx::kNullAcceleratedWidget || !gles2_) |
| 68 return; |
| 69 gles2_->CreateContext(widget_, native_viewport_->GetSize()); |
| 70 } |
| 71 |
| 72 virtual bool OnEvent(ui::Event* ui_event) MOJO_OVERRIDE { |
| 73 // Must not return early before updating capture. |
| 74 switch (ui_event->type()) { |
| 75 case ui::ET_MOUSE_PRESSED: |
| 76 case ui::ET_TOUCH_PRESSED: |
| 77 native_viewport_->SetCapture(); |
| 78 break; |
| 79 case ui::ET_MOUSE_RELEASED: |
| 80 case ui::ET_TOUCH_RELEASED: |
| 81 native_viewport_->ReleaseCapture(); |
| 82 break; |
| 83 default: |
| 84 break; |
| 85 } |
| 86 |
| 87 if (waiting_for_event_ack_ && IsRateLimitedEventType(ui_event)) |
| 88 return false; |
| 89 |
| 90 AllocationScope scope; |
| 91 |
| 92 Event::Builder event; |
| 93 event.set_action(ui_event->type()); |
| 94 event.set_time_stamp(ui_event->time_stamp().ToInternalValue()); |
| 95 |
| 96 if (ui_event->IsMouseEvent() || ui_event->IsTouchEvent()) { |
| 97 ui::LocatedEvent* located_event = |
| 98 static_cast<ui::LocatedEvent*>(ui_event); |
| 99 Point::Builder location; |
| 100 location.set_x(located_event->location().x()); |
| 101 location.set_y(located_event->location().y()); |
| 102 event.set_location(location.Finish()); |
| 103 } |
| 104 |
| 105 if (ui_event->IsTouchEvent()) { |
| 106 ui::TouchEvent* touch_event = static_cast<ui::TouchEvent*>(ui_event); |
| 107 TouchData::Builder touch_data; |
| 108 touch_data.set_pointer_id(touch_event->touch_id()); |
| 109 event.set_touch_data(touch_data.Finish()); |
| 110 } |
| 111 |
| 112 client_->OnEvent(event.Finish()); |
| 113 waiting_for_event_ack_ = true; |
| 114 return false; |
| 115 } |
| 116 |
| 117 virtual void OnAcceleratedWidgetAvailable( |
| 118 gfx::AcceleratedWidget widget) MOJO_OVERRIDE { |
| 119 widget_ = widget; |
| 120 CreateGLES2ContextIfNeeded(); |
| 121 } |
| 122 |
| 123 virtual void OnResized(const gfx::Size& size) MOJO_OVERRIDE { |
| 124 } |
| 125 |
| 126 virtual void OnDestroyed() MOJO_OVERRIDE { |
| 127 // TODO(beng): |
| 128 // Destroying |gles2_| on the shell thread here hits thread checker |
| 129 // asserts. All code must stop touching the AcceleratedWidget at this |
| 130 // point as it is dead after this call stack. jamesr said we probably |
| 131 // should make our own GLSurface and simply tell it to stop touching the |
| 132 // AcceleratedWidget via Destroy() but we have no good way of doing that |
| 133 // right now given our current threading model so james' recommendation |
| 134 // was just to wait until after we move the gl service out of process. |
| 135 // gles2_.reset(); |
| 136 client_->OnDestroyed(); |
| 137 base::MessageLoop::current()->Quit(); |
| 138 } |
| 139 |
| 140 private: |
| 141 NativeViewportService* service_; |
| 142 gfx::AcceleratedWidget widget_; |
| 143 scoped_ptr<services::NativeViewport> native_viewport_; |
| 144 scoped_ptr<GLES2Impl> gles2_; |
| 145 bool waiting_for_event_ack_; |
| 146 int64 pending_event_timestamp_; |
| 147 |
| 148 RemotePtr<NativeViewportClient> client_; |
| 149 }; |
| 150 |
| 151 NativeViewportService::NativeViewportService( |
| 152 ScopedMessagePipeHandle shell_handle) |
| 153 : shell_(shell_handle.Pass()), |
| 154 context_(NULL) { |
| 155 shell_.SetPeer(this); |
| 156 } |
| 157 |
| 158 NativeViewportService::~NativeViewportService() {} |
| 159 |
| 160 void NativeViewportService::AcceptConnection( |
| 161 ScopedMessagePipeHandle client_handle) { |
| 162 // TODO(davemoore): We need a mechanism to determine when connectors |
| 163 // go away, so we can remove viewports correctly. |
| 164 viewports_.push_back(new NativeViewportImpl(this, client_handle.Pass())); |
| 165 } |
| 166 |
| 167 } // namespace services |
| 168 } // namespace mojo |
| 169 |
| 170 #if defined(OS_ANDROID) |
| 171 |
| 172 // Android will call this. |
| 173 mojo::services::NativeViewportService* |
| 174 CreateNativeViewportService(mojo::ScopedMessagePipeHandle shell_handle) { |
| 175 return new mojo::services::NativeViewportService(shell_handle.Pass()); |
| 176 } |
| 177 |
| 178 #else |
| 179 |
| 180 extern "C" MOJO_NATIVE_VIEWPORT_EXPORT MojoResult MojoMain( |
| 181 const MojoHandle shell_handle) { |
| 182 mojo::common::BindingsSupportImpl bindings_support_impl; |
| 183 mojo::BindingsSupport::Set(&bindings_support_impl); |
| 184 |
| 185 base::MessageLoop loop(base::MessageLoop::TYPE_UI); |
| 186 mojo::services::NativeViewportService app( |
| 187 mojo::MakeScopedHandle(mojo::MessagePipeHandle(shell_handle)).Pass()); |
| 188 base::MessageLoop::current()->Run(); |
| 189 |
| 190 mojo::BindingsSupport::Set(NULL); |
| 191 return MOJO_RESULT_OK; |
| 192 } |
| 193 |
| 194 #endif // !OS_ANDROID |
OLD | NEW |