| 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" | 13 #include "mojo/converters/input_events/input_events_type_converters.h" |
| 14 #include "mojo/converters/surfaces/surfaces_type_converters.h" | 14 #include "mojo/converters/surfaces/surfaces_type_converters.h" |
| 15 #include "mojo/public/cpp/application/application_delegate.h" | 15 #include "mojo/public/cpp/application/application_delegate.h" |
| 16 #include "mojo/public/cpp/application/application_impl.h" | 16 #include "mojo/public/cpp/application/application_impl.h" |
| 17 #include "mojo/public/cpp/application/interface_factory.h" | 17 #include "mojo/public/cpp/application/interface_factory.h" |
| 18 #include "services/gles2/gpu_state.h" |
| 18 #include "services/native_viewport/platform_viewport_headless.h" | 19 #include "services/native_viewport/platform_viewport_headless.h" |
| 19 #include "services/native_viewport/viewport_surface.h" | 20 #include "services/native_viewport/viewport_surface.h" |
| 20 #include "ui/events/event.h" | 21 #include "ui/events/event.h" |
| 21 | 22 |
| 22 namespace native_viewport { | 23 namespace native_viewport { |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 bool IsRateLimitedEventType(ui::Event* event) { | 26 bool IsRateLimitedEventType(ui::Event* event) { |
| 26 return event->type() == ui::ET_MOUSE_MOVED || | 27 return event->type() == ui::ET_MOUSE_MOVED || |
| 27 event->type() == ui::ET_MOUSE_DRAGGED || | 28 event->type() == ui::ET_MOUSE_DRAGGED || |
| 28 event->type() == ui::ET_TOUCH_MOVED; | 29 event->type() == ui::ET_TOUCH_MOVED; |
| 29 } | 30 } |
| 30 | 31 |
| 31 } // namespace | 32 } // namespace |
| 32 | 33 |
| 33 NativeViewportImpl::NativeViewportImpl( | 34 NativeViewportImpl::NativeViewportImpl( |
| 34 mojo::ApplicationImpl* app, | 35 mojo::ApplicationImpl* app, |
| 35 bool is_headless, | 36 bool is_headless, |
| 37 const scoped_refptr<gles2::GpuState>& gpu_state, |
| 36 mojo::InterfaceRequest<mojo::NativeViewport> request) | 38 mojo::InterfaceRequest<mojo::NativeViewport> request) |
| 37 : is_headless_(is_headless), | 39 : is_headless_(is_headless), |
| 40 context_provider_(gpu_state), |
| 38 widget_id_(0u), | 41 widget_id_(0u), |
| 39 sent_metrics_(false), | 42 sent_metrics_(false), |
| 40 metrics_(mojo::ViewportMetrics::New()), | 43 metrics_(mojo::ViewportMetrics::New()), |
| 41 waiting_for_event_ack_(false), | 44 waiting_for_event_ack_(false), |
| 42 binding_(this, request.Pass()), | 45 binding_(this, request.Pass()), |
| 43 weak_factory_(this) { | 46 weak_factory_(this) { |
| 44 binding_.set_error_handler(this); | 47 binding_.set_error_handler(this); |
| 45 app->ConnectToService("mojo:surfaces_service", &surface_); | 48 app->ConnectToService("mojo:surfaces_service", &surface_); |
| 46 // TODO(jamesr): Should be mojo_gpu_service | 49 // TODO(jamesr): Should be mojo_gpu_service |
| 47 app->ConnectToService("mojo:native_viewport_service", &gpu_service_); | 50 app->ConnectToService("mojo:native_viewport_service", &gpu_service_); |
| 48 } | 51 } |
| 49 | 52 |
| 50 NativeViewportImpl::~NativeViewportImpl() { | 53 NativeViewportImpl::~NativeViewportImpl() { |
| 51 // Destroy the NativeViewport early on as it may call us back during | 54 // Destroy the NativeViewport early on as it may call us back during |
| 52 // destruction and we want to be in a known state. | 55 // destruction and we want to be in a known state. |
| 53 platform_viewport_.reset(); | 56 platform_viewport_.reset(); |
| 54 } | 57 } |
| 55 | 58 |
| 56 void NativeViewportImpl::Create( | 59 void NativeViewportImpl::Create(mojo::SizePtr size, |
| 57 mojo::SizePtr size, | 60 const CreateCallback& callback) { |
| 58 const mojo::Callback<void(uint64_t, mojo::ViewportMetricsPtr metrics)>& | |
| 59 callback) { | |
| 60 create_callback_ = callback; | 61 create_callback_ = callback; |
| 61 metrics_->size = size.Clone(); | 62 metrics_->size = size.Clone(); |
| 62 if (is_headless_) | 63 if (is_headless_) |
| 63 platform_viewport_ = PlatformViewportHeadless::Create(this); | 64 platform_viewport_ = PlatformViewportHeadless::Create(this); |
| 64 else | 65 else |
| 65 platform_viewport_ = PlatformViewport::Create(this); | 66 platform_viewport_ = PlatformViewport::Create(this); |
| 66 platform_viewport_->Init(gfx::Rect(size.To<gfx::Size>())); | 67 platform_viewport_->Init(gfx::Rect(size.To<gfx::Size>())); |
| 67 } | 68 } |
| 68 | 69 |
| 69 void NativeViewportImpl::RequestMetrics(const MetricsCallback& callback) { | 70 void NativeViewportImpl::RequestMetrics( |
| 71 const RequestMetricsCallback& callback) { |
| 70 if (!sent_metrics_) { | 72 if (!sent_metrics_) { |
| 71 callback.Run(metrics_.Clone()); | 73 callback.Run(metrics_.Clone()); |
| 72 sent_metrics_ = true; | 74 sent_metrics_ = true; |
| 73 return; | 75 return; |
| 74 } | 76 } |
| 75 metrics_callback_ = callback; | 77 metrics_callback_ = callback; |
| 76 } | 78 } |
| 77 | 79 |
| 78 void NativeViewportImpl::Show() { | 80 void NativeViewportImpl::Show() { |
| 79 platform_viewport_->Show(); | 81 platform_viewport_->Show(); |
| 80 } | 82 } |
| 81 | 83 |
| 82 void NativeViewportImpl::Hide() { | 84 void NativeViewportImpl::Hide() { |
| 83 platform_viewport_->Hide(); | 85 platform_viewport_->Hide(); |
| 84 } | 86 } |
| 85 | 87 |
| 86 void NativeViewportImpl::Close() { | 88 void NativeViewportImpl::Close() { |
| 87 DCHECK(platform_viewport_); | 89 DCHECK(platform_viewport_); |
| 88 platform_viewport_->Close(); | 90 platform_viewport_->Close(); |
| 89 } | 91 } |
| 90 | 92 |
| 91 void NativeViewportImpl::SetSize(mojo::SizePtr size) { | 93 void NativeViewportImpl::SetSize(mojo::SizePtr size) { |
| 92 platform_viewport_->SetBounds(gfx::Rect(size.To<gfx::Size>())); | 94 platform_viewport_->SetBounds(gfx::Rect(size.To<gfx::Size>())); |
| 93 } | 95 } |
| 94 | 96 |
| 97 /* |
| 95 void NativeViewportImpl::SubmittedFrame(mojo::SurfaceIdPtr child_surface_id) { | 98 void NativeViewportImpl::SubmittedFrame(mojo::SurfaceIdPtr child_surface_id) { |
| 96 if (child_surface_id_.is_null()) { | 99 if (child_surface_id_.is_null()) { |
| 97 // If this is the first indication that the client will use surfaces, | 100 // If this is the first indication that the client will use surfaces, |
| 98 // initialize that system. | 101 // initialize that system. |
| 99 // TODO(jamesr): When everything is converted to surfaces initialize this | 102 // TODO(jamesr): When everything is converted to surfaces initialize this |
| 100 // eagerly. | 103 // eagerly. |
| 101 viewport_surface_.reset(new ViewportSurface( | 104 viewport_surface_.reset(new ViewportSurface( |
| 102 surface_.Pass(), gpu_service_.get(), metrics_->size.To<gfx::Size>(), | 105 surface_.Pass(), gpu_service_.get(), metrics_->size.To<gfx::Size>(), |
| 103 child_surface_id.To<cc::SurfaceId>())); | 106 child_surface_id.To<cc::SurfaceId>())); |
| 104 if (widget_id_) | 107 if (widget_id_) |
| 105 viewport_surface_->SetWidgetId(widget_id_); | 108 viewport_surface_->SetWidgetId(widget_id_); |
| 106 } | 109 } |
| 107 child_surface_id_ = child_surface_id.To<cc::SurfaceId>(); | 110 child_surface_id_ = child_surface_id.To<cc::SurfaceId>(); |
| 108 if (viewport_surface_) | 111 if (viewport_surface_) |
| 109 viewport_surface_->SetChildId(child_surface_id_); | 112 viewport_surface_->SetChildId(child_surface_id_); |
| 110 } | 113 } |
| 114 */ |
| 115 |
| 116 void NativeViewportImpl::GetContextProvider( |
| 117 mojo::InterfaceRequest<mojo::ContextProvider> request) { |
| 118 context_provider_.Bind(request.Pass()); |
| 119 } |
| 111 | 120 |
| 112 void NativeViewportImpl::SetEventDispatcher( | 121 void NativeViewportImpl::SetEventDispatcher( |
| 113 mojo::NativeViewportEventDispatcherPtr dispatcher) { | 122 mojo::NativeViewportEventDispatcherPtr dispatcher) { |
| 114 event_dispatcher_ = dispatcher.Pass(); | 123 event_dispatcher_ = dispatcher.Pass(); |
| 115 } | 124 } |
| 116 | 125 |
| 117 void NativeViewportImpl::OnMetricsChanged(mojo::ViewportMetricsPtr metrics) { | 126 void NativeViewportImpl::OnMetricsChanged(mojo::ViewportMetricsPtr metrics) { |
| 118 if (metrics_->Equals(*metrics)) | 127 if (metrics_->Equals(*metrics)) |
| 119 return; | 128 return; |
| 120 | 129 |
| 121 metrics_ = metrics.Pass(); | 130 metrics_ = metrics.Pass(); |
| 122 sent_metrics_ = false; | 131 sent_metrics_ = false; |
| 123 | 132 |
| 124 if (!metrics_callback_.is_null()) { | 133 if (!metrics_callback_.is_null()) { |
| 125 metrics_callback_.Run(metrics_.Clone()); | 134 metrics_callback_.Run(metrics_.Clone()); |
| 126 metrics_callback_.reset(); | 135 metrics_callback_.reset(); |
| 127 sent_metrics_ = true; | 136 sent_metrics_ = true; |
| 128 } | 137 } |
| 129 if (viewport_surface_) | 138 if (viewport_surface_) |
| 130 viewport_surface_->SetSize(metrics_->size.To<gfx::Size>()); | 139 viewport_surface_->SetSize(metrics_->size.To<gfx::Size>()); |
| 131 } | 140 } |
| 132 | 141 |
| 133 void NativeViewportImpl::OnAcceleratedWidgetAvailable( | 142 void NativeViewportImpl::OnAcceleratedWidgetAvailable( |
| 134 gfx::AcceleratedWidget widget) { | 143 gfx::AcceleratedWidget widget) { |
| 144 context_provider_.SetAcceleratedWidget(widget); |
| 145 create_callback_.Run(metrics_.Clone()); |
| 146 sent_metrics_ = true; |
| 147 create_callback_.reset(); |
| 148 /* |
| 135 widget_id_ = static_cast<uint64_t>(bit_cast<uintptr_t>(widget)); | 149 widget_id_ = static_cast<uint64_t>(bit_cast<uintptr_t>(widget)); |
| 136 // TODO(jamesr): Remove once everything is converted to surfaces. | 150 // TODO(jamesr): Remove once everything is converted to surfaces. |
| 137 create_callback_.Run(widget_id_, metrics_.Clone()); | |
| 138 sent_metrics_ = true; | |
| 139 create_callback_.reset(); | |
| 140 if (viewport_surface_) | 151 if (viewport_surface_) |
| 141 viewport_surface_->SetWidgetId(widget_id_); | 152 viewport_surface_->SetWidgetId(widget_id_); |
| 153 */ |
| 142 } | 154 } |
| 143 | 155 |
| 144 bool NativeViewportImpl::OnEvent(ui::Event* ui_event) { | 156 bool NativeViewportImpl::OnEvent(ui::Event* ui_event) { |
| 145 if (!event_dispatcher_.get()) | 157 if (!event_dispatcher_.get()) |
| 146 return false; | 158 return false; |
| 147 | 159 |
| 148 // Must not return early before updating capture. | 160 // Must not return early before updating capture. |
| 149 switch (ui_event->type()) { | 161 switch (ui_event->type()) { |
| 150 case ui::ET_MOUSE_PRESSED: | 162 case ui::ET_MOUSE_PRESSED: |
| 151 case ui::ET_TOUCH_PRESSED: | 163 case ui::ET_TOUCH_PRESSED: |
| (...skipping 25 matching lines...) Expand all Loading... |
| 177 void NativeViewportImpl::OnConnectionError() { | 189 void NativeViewportImpl::OnConnectionError() { |
| 178 binding_.set_error_handler(nullptr); | 190 binding_.set_error_handler(nullptr); |
| 179 delete this; | 191 delete this; |
| 180 } | 192 } |
| 181 | 193 |
| 182 void NativeViewportImpl::AckEvent() { | 194 void NativeViewportImpl::AckEvent() { |
| 183 waiting_for_event_ack_ = false; | 195 waiting_for_event_ack_ = false; |
| 184 } | 196 } |
| 185 | 197 |
| 186 } // namespace native_viewport | 198 } // namespace native_viewport |
| OLD | NEW |