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