| 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" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 namespace { | 23 namespace { |
| 24 | 24 |
| 25 bool IsRateLimitedEventType(ui::Event* event) { | 25 bool IsRateLimitedEventType(ui::Event* event) { |
| 26 return event->type() == ui::ET_MOUSE_MOVED || | 26 return event->type() == ui::ET_MOUSE_MOVED || |
| 27 event->type() == ui::ET_MOUSE_DRAGGED || | 27 event->type() == ui::ET_MOUSE_DRAGGED || |
| 28 event->type() == ui::ET_TOUCH_MOVED; | 28 event->type() == ui::ET_TOUCH_MOVED; |
| 29 } | 29 } |
| 30 | 30 |
| 31 } // namespace | 31 } // namespace |
| 32 | 32 |
| 33 NativeViewportImpl::NativeViewportImpl(mojo::ApplicationImpl* app, | 33 NativeViewportImpl::NativeViewportImpl( |
| 34 bool is_headless) | 34 mojo::ApplicationImpl* app, |
| 35 bool is_headless, |
| 36 mojo::InterfaceRequest<mojo::NativeViewport> request) |
| 35 : is_headless_(is_headless), | 37 : is_headless_(is_headless), |
| 36 widget_id_(0u), | 38 widget_id_(0u), |
| 39 sent_metrics_(false), |
| 37 metrics_(mojo::ViewportMetrics::New()), | 40 metrics_(mojo::ViewportMetrics::New()), |
| 38 waiting_for_event_ack_(false), | 41 waiting_for_event_ack_(false), |
| 42 binding_(this, request.Pass()), |
| 39 weak_factory_(this) { | 43 weak_factory_(this) { |
| 44 binding_.set_error_handler(this); |
| 40 app->ConnectToService("mojo:surfaces_service", &surface_); | 45 app->ConnectToService("mojo:surfaces_service", &surface_); |
| 41 // TODO(jamesr): Should be mojo_gpu_service | 46 // TODO(jamesr): Should be mojo_gpu_service |
| 42 app->ConnectToService("mojo:native_viewport_service", &gpu_service_); | 47 app->ConnectToService("mojo:native_viewport_service", &gpu_service_); |
| 43 } | 48 } |
| 44 | 49 |
| 45 NativeViewportImpl::~NativeViewportImpl() { | 50 NativeViewportImpl::~NativeViewportImpl() { |
| 46 // Destroy the NativeViewport early on as it may call us back during | 51 // Destroy the NativeViewport early on as it may call us back during |
| 47 // destruction and we want to be in a known state. | 52 // destruction and we want to be in a known state. |
| 48 platform_viewport_.reset(); | 53 platform_viewport_.reset(); |
| 49 } | 54 } |
| 50 | 55 |
| 51 void NativeViewportImpl::Create( | 56 void NativeViewportImpl::Create( |
| 52 mojo::SizePtr size, | 57 mojo::SizePtr size, |
| 53 const mojo::Callback<void(uint64_t)>& callback) { | 58 const mojo::Callback<void(uint64_t, mojo::ViewportMetricsPtr metrics)>& |
| 59 callback) { |
| 54 create_callback_ = callback; | 60 create_callback_ = callback; |
| 55 metrics_->size = size.Clone(); | 61 metrics_->size = size.Clone(); |
| 56 if (is_headless_) | 62 if (is_headless_) |
| 57 platform_viewport_ = PlatformViewportHeadless::Create(this); | 63 platform_viewport_ = PlatformViewportHeadless::Create(this); |
| 58 else | 64 else |
| 59 platform_viewport_ = PlatformViewport::Create(this); | 65 platform_viewport_ = PlatformViewport::Create(this); |
| 60 platform_viewport_->Init(gfx::Rect(size.To<gfx::Size>())); | 66 platform_viewport_->Init(gfx::Rect(size.To<gfx::Size>())); |
| 61 } | 67 } |
| 62 | 68 |
| 69 void NativeViewportImpl::RequestMetrics(const MetricsCallback& callback) { |
| 70 if (!sent_metrics_) { |
| 71 callback.Run(metrics_.Clone()); |
| 72 sent_metrics_ = true; |
| 73 return; |
| 74 } |
| 75 metrics_callback_ = callback; |
| 76 } |
| 77 |
| 63 void NativeViewportImpl::Show() { | 78 void NativeViewportImpl::Show() { |
| 64 platform_viewport_->Show(); | 79 platform_viewport_->Show(); |
| 65 } | 80 } |
| 66 | 81 |
| 67 void NativeViewportImpl::Hide() { | 82 void NativeViewportImpl::Hide() { |
| 68 platform_viewport_->Hide(); | 83 platform_viewport_->Hide(); |
| 69 } | 84 } |
| 70 | 85 |
| 71 void NativeViewportImpl::Close() { | 86 void NativeViewportImpl::Close() { |
| 72 DCHECK(platform_viewport_); | 87 DCHECK(platform_viewport_); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 97 void NativeViewportImpl::SetEventDispatcher( | 112 void NativeViewportImpl::SetEventDispatcher( |
| 98 mojo::NativeViewportEventDispatcherPtr dispatcher) { | 113 mojo::NativeViewportEventDispatcherPtr dispatcher) { |
| 99 event_dispatcher_ = dispatcher.Pass(); | 114 event_dispatcher_ = dispatcher.Pass(); |
| 100 } | 115 } |
| 101 | 116 |
| 102 void NativeViewportImpl::OnMetricsChanged(mojo::ViewportMetricsPtr metrics) { | 117 void NativeViewportImpl::OnMetricsChanged(mojo::ViewportMetricsPtr metrics) { |
| 103 if (metrics_->Equals(*metrics)) | 118 if (metrics_->Equals(*metrics)) |
| 104 return; | 119 return; |
| 105 | 120 |
| 106 metrics_ = metrics.Pass(); | 121 metrics_ = metrics.Pass(); |
| 122 sent_metrics_ = false; |
| 107 | 123 |
| 108 // Wait for the accelerated widget before telling the client of the bounds. | 124 if (!metrics_callback_.is_null()) { |
| 109 if (create_callback_.is_null()) | 125 metrics_callback_.Run(metrics_.Clone()); |
| 110 ProcessOnMetricsChanged(); | 126 metrics_callback_.reset(); |
| 127 sent_metrics_ = true; |
| 128 } |
| 129 if (viewport_surface_) |
| 130 viewport_surface_->SetSize(metrics_->size.To<gfx::Size>()); |
| 111 } | 131 } |
| 112 | 132 |
| 113 void NativeViewportImpl::OnAcceleratedWidgetAvailable( | 133 void NativeViewportImpl::OnAcceleratedWidgetAvailable( |
| 114 gfx::AcceleratedWidget widget) { | 134 gfx::AcceleratedWidget widget) { |
| 115 widget_id_ = static_cast<uint64_t>(bit_cast<uintptr_t>(widget)); | 135 widget_id_ = static_cast<uint64_t>(bit_cast<uintptr_t>(widget)); |
| 116 // TODO(jamesr): Remove once everything is converted to surfaces. | 136 // TODO(jamesr): Remove once everything is converted to surfaces. |
| 117 create_callback_.Run(widget_id_); | 137 create_callback_.Run(widget_id_, metrics_.Clone()); |
| 138 sent_metrics_ = true; |
| 118 create_callback_.reset(); | 139 create_callback_.reset(); |
| 119 // Immediately tell the client the metrics. The size may be wrong, if so we'll | |
| 120 // get the right one in the next OnMetricsChanged() call. | |
| 121 ProcessOnMetricsChanged(); | |
| 122 if (viewport_surface_) | 140 if (viewport_surface_) |
| 123 viewport_surface_->SetWidgetId(widget_id_); | 141 viewport_surface_->SetWidgetId(widget_id_); |
| 124 } | 142 } |
| 125 | 143 |
| 126 bool NativeViewportImpl::OnEvent(ui::Event* ui_event) { | 144 bool NativeViewportImpl::OnEvent(ui::Event* ui_event) { |
| 127 if (!event_dispatcher_.get()) | 145 if (!event_dispatcher_.get()) |
| 128 return false; | 146 return false; |
| 129 | 147 |
| 130 // Must not return early before updating capture. | 148 // Must not return early before updating capture. |
| 131 switch (ui_event->type()) { | 149 switch (ui_event->type()) { |
| (...skipping 13 matching lines...) Expand all Loading... |
| 145 return false; | 163 return false; |
| 146 | 164 |
| 147 event_dispatcher_->OnEvent( | 165 event_dispatcher_->OnEvent( |
| 148 mojo::Event::From(*ui_event), | 166 mojo::Event::From(*ui_event), |
| 149 base::Bind(&NativeViewportImpl::AckEvent, weak_factory_.GetWeakPtr())); | 167 base::Bind(&NativeViewportImpl::AckEvent, weak_factory_.GetWeakPtr())); |
| 150 waiting_for_event_ack_ = true; | 168 waiting_for_event_ack_ = true; |
| 151 return false; | 169 return false; |
| 152 } | 170 } |
| 153 | 171 |
| 154 void NativeViewportImpl::OnDestroyed() { | 172 void NativeViewportImpl::OnDestroyed() { |
| 155 client()->OnDestroyed(); | 173 // This will signal a connection error and cause us to delete |this|. |
| 174 binding_.Close(); |
| 175 } |
| 176 |
| 177 void NativeViewportImpl::OnConnectionError() { |
| 178 binding_.set_error_handler(nullptr); |
| 179 delete this; |
| 156 } | 180 } |
| 157 | 181 |
| 158 void NativeViewportImpl::AckEvent() { | 182 void NativeViewportImpl::AckEvent() { |
| 159 waiting_for_event_ack_ = false; | 183 waiting_for_event_ack_ = false; |
| 160 } | 184 } |
| 161 | 185 |
| 162 void NativeViewportImpl::ProcessOnMetricsChanged() { | |
| 163 client()->OnMetricsChanged(metrics_.Clone()); | |
| 164 if (viewport_surface_) | |
| 165 viewport_surface_->SetSize(metrics_->size.To<gfx::Size>()); | |
| 166 } | |
| 167 | |
| 168 } // namespace native_viewport | 186 } // namespace native_viewport |
| OLD | NEW |