Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "services/native_viewport/native_viewport_impl.h" | 5 #include "services/native_viewport/native_viewport_impl.h" |
| 6 | 6 |
| 7 #include "base/auto_reset.h" | 7 #include "base/auto_reset.h" |
| 8 #include "base/bind.h" | 8 #include "base/bind.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| 11 #include "base/time/time.h" | 11 #include "base/time/time.h" |
| 12 #include "mojo/converters/geometry/geometry_type_converters.h" | 12 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 13 #include "mojo/converters/input_events/input_events_type_converters.h" | |
| 14 #include "mojo/public/cpp/application/interface_factory.h" | 13 #include "mojo/public/cpp/application/interface_factory.h" |
| 15 #include "services/gles2/gpu_state.h" | 14 #include "services/gles2/gpu_state.h" |
| 16 #include "services/native_viewport/platform_viewport_headless.h" | 15 #include "services/native_viewport/platform_viewport_headless.h" |
| 17 #include "ui/events/event.h" | 16 #include "ui/events/event.h" |
| 18 | 17 |
| 19 namespace native_viewport { | 18 namespace native_viewport { |
| 20 namespace { | |
| 21 | |
| 22 bool IsRateLimitedEventType(const mojo::EventPtr& event) { | |
| 23 return event->action == mojo::EVENT_TYPE_POINTER_MOVE; | |
| 24 } | |
| 25 | |
| 26 } // namespace | |
| 27 | 19 |
| 28 NativeViewportImpl::NativeViewportImpl( | 20 NativeViewportImpl::NativeViewportImpl( |
| 29 bool is_headless, | 21 bool is_headless, |
| 30 const scoped_refptr<gles2::GpuState>& gpu_state, | 22 const scoped_refptr<gles2::GpuState>& gpu_state, |
| 31 mojo::InterfaceRequest<mojo::NativeViewport> request) | 23 mojo::InterfaceRequest<mojo::NativeViewport> request) |
| 32 : is_headless_(is_headless), | 24 : is_headless_(is_headless), |
| 33 context_provider_(gpu_state), | 25 context_provider_(gpu_state), |
| 34 sent_metrics_(false), | 26 sent_metrics_(false), |
| 35 metrics_(mojo::ViewportMetrics::New()), | 27 metrics_(mojo::ViewportMetrics::New()), |
| 36 waiting_for_event_ack_(false), | |
| 37 binding_(this, request.Pass()), | 28 binding_(this, request.Pass()), |
| 38 weak_factory_(this) { | 29 weak_factory_(this) { |
| 39 binding_.set_error_handler(this); | 30 binding_.set_error_handler(this); |
| 40 } | 31 } |
| 41 | 32 |
| 42 NativeViewportImpl::~NativeViewportImpl() { | 33 NativeViewportImpl::~NativeViewportImpl() { |
| 43 // Destroy the NativeViewport early on as it may call us back during | 34 // Destroy the NativeViewport early on as it may call us back during |
| 44 // destruction and we want to be in a known state. | 35 // destruction and we want to be in a known state. |
| 45 platform_viewport_.reset(); | 36 platform_viewport_.reset(); |
| 46 } | 37 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 115 create_callback_.Run(metrics_.Clone()); | 106 create_callback_.Run(metrics_.Clone()); |
| 116 sent_metrics_ = true; | 107 sent_metrics_ = true; |
| 117 create_callback_.reset(); | 108 create_callback_.reset(); |
| 118 } | 109 } |
| 119 | 110 |
| 120 void NativeViewportImpl::OnAcceleratedWidgetDestroyed() { | 111 void NativeViewportImpl::OnAcceleratedWidgetDestroyed() { |
| 121 context_provider_.SetAcceleratedWidget(gfx::kNullAcceleratedWidget); | 112 context_provider_.SetAcceleratedWidget(gfx::kNullAcceleratedWidget); |
| 122 } | 113 } |
| 123 | 114 |
| 124 bool NativeViewportImpl::OnEvent(mojo::EventPtr event) { | 115 bool NativeViewportImpl::OnEvent(mojo::EventPtr event) { |
| 125 if (event.is_null() || !event_dispatcher_.get() || | 116 if (event.is_null() || !event_dispatcher_.get()) |
| 126 (waiting_for_event_ack_ && IsRateLimitedEventType(event))) { | |
| 127 return false; | 117 return false; |
| 118 | |
| 119 mojo::NativeViewportEventDispatcher::OnEventCallback callback; | |
| 120 switch (event->action) { | |
| 121 case mojo::EVENT_TYPE_POINTER_MOVE: { | |
| 122 // TODO(sky): add logic to remember last event location and not send if | |
| 123 // the same. | |
| 124 if (pointers_waiting_on_ack_.count(event->pointer_data->pointer_id)) | |
| 125 return false; | |
| 126 | |
| 127 pointers_waiting_on_ack_.insert(event->pointer_data->pointer_id); | |
| 128 scoped_ptr<PointerId> id_wrapper(new PointerId); | |
| 129 id_wrapper->id = event->pointer_data->pointer_id; | |
| 130 callback = | |
| 131 base::Bind(&NativeViewportImpl::AckEvent, weak_factory_.GetWeakPtr(), | |
| 132 base::Owned(id_wrapper.release())); | |
|
qsr
2015/03/24 08:20:22
Do you expect that this callback can be called mul
sky
2015/03/24 13:18:21
Done.
| |
| 133 break; | |
| 134 } | |
| 135 | |
| 136 case mojo::EVENT_TYPE_POINTER_CANCEL: | |
| 137 pointers_waiting_on_ack_.clear(); | |
| 138 break; | |
| 139 | |
| 140 case mojo::EVENT_TYPE_POINTER_UP: | |
| 141 pointers_waiting_on_ack_.erase(event->pointer_data->pointer_id); | |
| 142 break; | |
| 143 | |
| 144 default: | |
| 145 break; | |
| 128 } | 146 } |
| 129 | 147 |
| 130 event_dispatcher_->OnEvent( | 148 event_dispatcher_->OnEvent(event.Pass(), callback); |
| 131 event.Pass(), | |
| 132 base::Bind(&NativeViewportImpl::AckEvent, weak_factory_.GetWeakPtr())); | |
| 133 waiting_for_event_ack_ = true; | |
| 134 return false; | 149 return false; |
| 135 } | 150 } |
| 136 | 151 |
| 137 void NativeViewportImpl::OnDestroyed() { | 152 void NativeViewportImpl::OnDestroyed() { |
| 138 // This will signal a connection error and cause us to delete |this|. | 153 // This will signal a connection error and cause us to delete |this|. |
| 139 binding_.Close(); | 154 binding_.Close(); |
| 140 } | 155 } |
| 141 | 156 |
| 142 void NativeViewportImpl::OnConnectionError() { | 157 void NativeViewportImpl::OnConnectionError() { |
| 143 binding_.set_error_handler(nullptr); | 158 binding_.set_error_handler(nullptr); |
| 144 delete this; | 159 delete this; |
| 145 } | 160 } |
| 146 | 161 |
| 147 void NativeViewportImpl::AckEvent() { | 162 void NativeViewportImpl::AckEvent(PointerId* id) { |
| 148 waiting_for_event_ack_ = false; | 163 pointers_waiting_on_ack_.erase(id->id); |
| 149 } | 164 } |
| 150 | 165 |
| 151 } // namespace native_viewport | 166 } // namespace native_viewport |
| OLD | NEW |