| 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 "components/native_viewport/native_viewport_impl.h" | 5 #include "components/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 "components/gles2/gpu_state.h" | 12 #include "components/gles2/gpu_state.h" |
| 13 #include "components/native_viewport/platform_viewport_headless.h" | 13 #include "components/native_viewport/platform_viewport_headless.h" |
| 14 #include "mojo/application/public/cpp/interface_factory.h" | 14 #include "mojo/application/public/cpp/interface_factory.h" |
| 15 #include "mojo/converters/geometry/geometry_type_converters.h" | 15 #include "mojo/converters/geometry/geometry_type_converters.h" |
| 16 #include "ui/events/event.h" | 16 #include "ui/events/event.h" |
| 17 | 17 |
| 18 namespace native_viewport { | 18 namespace native_viewport { |
| 19 | 19 |
| 20 NativeViewportImpl::NativeViewportImpl( | 20 NativeViewportImpl::NativeViewportImpl( |
| 21 bool is_headless, | 21 bool is_headless, |
| 22 const scoped_refptr<gles2::GpuState>& gpu_state, | 22 const scoped_refptr<gles2::GpuState>& gpu_state, |
| 23 mojo::InterfaceRequest<mojo::NativeViewport> request) | 23 mojo::InterfaceRequest<mojo::NativeViewport> request, |
| 24 scoped_ptr<mojo::AppRefCount> app_refcount) |
| 24 : is_headless_(is_headless), | 25 : is_headless_(is_headless), |
| 25 context_provider_(gpu_state), | 26 app_refcount_(app_refcount.Pass()), |
| 27 context_provider_(new OnscreenContextProvider(gpu_state)), |
| 26 sent_metrics_(false), | 28 sent_metrics_(false), |
| 27 metrics_(mojo::ViewportMetrics::New()), | 29 metrics_(mojo::ViewportMetrics::New()), |
| 28 binding_(this, request.Pass()), | 30 binding_(this, request.Pass()), |
| 29 weak_factory_(this) { | 31 weak_factory_(this) { |
| 30 binding_.set_error_handler(this); | 32 binding_.set_error_handler(this); |
| 31 } | 33 } |
| 32 | 34 |
| 33 NativeViewportImpl::~NativeViewportImpl() { | 35 NativeViewportImpl::~NativeViewportImpl() { |
| 36 // Destroy before |platform_viewport_| because this will destroy |
| 37 // CommandBufferDriver objects that contain child windows. Otherwise if this |
| 38 // class destroys its window first, X errors will occur. |
| 39 context_provider_.reset(); |
| 40 |
| 34 // Destroy the NativeViewport early on as it may call us back during | 41 // Destroy the NativeViewport early on as it may call us back during |
| 35 // destruction and we want to be in a known state. | 42 // destruction and we want to be in a known state. |
| 36 platform_viewport_.reset(); | 43 platform_viewport_.reset(); |
| 37 } | 44 } |
| 38 | 45 |
| 39 void NativeViewportImpl::Create(mojo::SizePtr size, | 46 void NativeViewportImpl::Create(mojo::SizePtr size, |
| 40 const CreateCallback& callback) { | 47 const CreateCallback& callback) { |
| 41 create_callback_ = callback; | 48 create_callback_ = callback; |
| 42 metrics_->size = size.Clone(); | 49 metrics_->size = size.Clone(); |
| 43 if (is_headless_) | 50 if (is_headless_) |
| (...skipping 25 matching lines...) Expand all Loading... |
| 69 DCHECK(platform_viewport_); | 76 DCHECK(platform_viewport_); |
| 70 platform_viewport_->Close(); | 77 platform_viewport_->Close(); |
| 71 } | 78 } |
| 72 | 79 |
| 73 void NativeViewportImpl::SetSize(mojo::SizePtr size) { | 80 void NativeViewportImpl::SetSize(mojo::SizePtr size) { |
| 74 platform_viewport_->SetBounds(gfx::Rect(size.To<gfx::Size>())); | 81 platform_viewport_->SetBounds(gfx::Rect(size.To<gfx::Size>())); |
| 75 } | 82 } |
| 76 | 83 |
| 77 void NativeViewportImpl::GetContextProvider( | 84 void NativeViewportImpl::GetContextProvider( |
| 78 mojo::InterfaceRequest<mojo::ContextProvider> request) { | 85 mojo::InterfaceRequest<mojo::ContextProvider> request) { |
| 79 context_provider_.Bind(request.Pass()); | 86 context_provider_->Bind(request.Pass()); |
| 80 } | 87 } |
| 81 | 88 |
| 82 void NativeViewportImpl::SetEventDispatcher( | 89 void NativeViewportImpl::SetEventDispatcher( |
| 83 mojo::NativeViewportEventDispatcherPtr dispatcher) { | 90 mojo::NativeViewportEventDispatcherPtr dispatcher) { |
| 84 event_dispatcher_ = dispatcher.Pass(); | 91 event_dispatcher_ = dispatcher.Pass(); |
| 85 } | 92 } |
| 86 | 93 |
| 87 void NativeViewportImpl::OnMetricsChanged(mojo::ViewportMetricsPtr metrics) { | 94 void NativeViewportImpl::OnMetricsChanged(mojo::ViewportMetricsPtr metrics) { |
| 88 if (metrics_->Equals(*metrics)) | 95 if (metrics_->Equals(*metrics)) |
| 89 return; | 96 return; |
| 90 | 97 |
| 91 metrics_ = metrics.Pass(); | 98 metrics_ = metrics.Pass(); |
| 92 sent_metrics_ = false; | 99 sent_metrics_ = false; |
| 93 | 100 |
| 94 if (!metrics_callback_.is_null()) { | 101 if (!metrics_callback_.is_null()) { |
| 95 metrics_callback_.Run(metrics_.Clone()); | 102 metrics_callback_.Run(metrics_.Clone()); |
| 96 metrics_callback_.reset(); | 103 metrics_callback_.reset(); |
| 97 sent_metrics_ = true; | 104 sent_metrics_ = true; |
| 98 } | 105 } |
| 99 } | 106 } |
| 100 | 107 |
| 101 void NativeViewportImpl::OnAcceleratedWidgetAvailable( | 108 void NativeViewportImpl::OnAcceleratedWidgetAvailable( |
| 102 gfx::AcceleratedWidget widget, | 109 gfx::AcceleratedWidget widget, |
| 103 float device_pixel_ratio) { | 110 float device_pixel_ratio) { |
| 104 metrics_->device_pixel_ratio = device_pixel_ratio; | 111 metrics_->device_pixel_ratio = device_pixel_ratio; |
| 105 context_provider_.SetAcceleratedWidget(widget); | 112 context_provider_->SetAcceleratedWidget(widget); |
| 106 // TODO: The metrics here might not match the actual window size on android | 113 // TODO: The metrics here might not match the actual window size on android |
| 107 // where we don't know the actual size until the first OnMetricsChanged call. | 114 // where we don't know the actual size until the first OnMetricsChanged call. |
| 108 create_callback_.Run(metrics_.Clone()); | 115 create_callback_.Run(metrics_.Clone()); |
| 109 sent_metrics_ = true; | 116 sent_metrics_ = true; |
| 110 create_callback_.reset(); | 117 create_callback_.reset(); |
| 111 } | 118 } |
| 112 | 119 |
| 113 void NativeViewportImpl::OnAcceleratedWidgetDestroyed() { | 120 void NativeViewportImpl::OnAcceleratedWidgetDestroyed() { |
| 114 context_provider_.SetAcceleratedWidget(gfx::kNullAcceleratedWidget); | 121 context_provider_->SetAcceleratedWidget(gfx::kNullAcceleratedWidget); |
| 115 } | 122 } |
| 116 | 123 |
| 117 bool NativeViewportImpl::OnEvent(mojo::EventPtr event) { | 124 bool NativeViewportImpl::OnEvent(mojo::EventPtr event) { |
| 118 if (event.is_null() || !event_dispatcher_.get()) | 125 if (event.is_null() || !event_dispatcher_.get()) |
| 119 return false; | 126 return false; |
| 120 | 127 |
| 121 mojo::NativeViewportEventDispatcher::OnEventCallback callback; | 128 mojo::NativeViewportEventDispatcher::OnEventCallback callback; |
| 122 switch (event->action) { | 129 switch (event->action) { |
| 123 case mojo::EVENT_TYPE_POINTER_MOVE: { | 130 case mojo::EVENT_TYPE_POINTER_MOVE: { |
| 124 // TODO(sky): add logic to remember last event location and not send if | 131 // TODO(sky): add logic to remember last event location and not send if |
| (...skipping 18 matching lines...) Expand all Loading... |
| 143 | 150 |
| 144 default: | 151 default: |
| 145 break; | 152 break; |
| 146 } | 153 } |
| 147 | 154 |
| 148 event_dispatcher_->OnEvent(event.Pass(), callback); | 155 event_dispatcher_->OnEvent(event.Pass(), callback); |
| 149 return false; | 156 return false; |
| 150 } | 157 } |
| 151 | 158 |
| 152 void NativeViewportImpl::OnDestroyed() { | 159 void NativeViewportImpl::OnDestroyed() { |
| 153 // This will signal a connection error and cause us to delete |this|. | 160 delete this; |
| 154 binding_.Close(); | |
| 155 } | 161 } |
| 156 | 162 |
| 157 void NativeViewportImpl::OnConnectionError() { | 163 void NativeViewportImpl::OnConnectionError() { |
| 158 binding_.set_error_handler(nullptr); | 164 binding_.set_error_handler(nullptr); |
| 159 delete this; | 165 delete this; |
| 160 } | 166 } |
| 161 | 167 |
| 162 void NativeViewportImpl::AckEvent(int32 pointer_id) { | 168 void NativeViewportImpl::AckEvent(int32 pointer_id) { |
| 163 pointers_waiting_on_ack_.erase(pointer_id); | 169 pointers_waiting_on_ack_.erase(pointer_id); |
| 164 } | 170 } |
| 165 | 171 |
| 166 } // namespace native_viewport | 172 } // namespace native_viewport |
| OLD | NEW |