| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/native_viewport/native_viewport_impl.h" | |
| 6 | |
| 7 #include "base/auto_reset.h" | |
| 8 #include "base/bind.h" | |
| 9 #include "base/macros.h" | |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "base/time/time.h" | |
| 12 #include "components/gles2/gpu_state.h" | |
| 13 #include "components/native_viewport/platform_viewport_headless.h" | |
| 14 #include "mojo/application/public/cpp/interface_factory.h" | |
| 15 #include "mojo/converters/geometry/geometry_type_converters.h" | |
| 16 #include "ui/events/event.h" | |
| 17 | |
| 18 namespace native_viewport { | |
| 19 | |
| 20 NativeViewportImpl::NativeViewportImpl( | |
| 21 bool is_headless, | |
| 22 const scoped_refptr<gles2::GpuState>& gpu_state, | |
| 23 mojo::InterfaceRequest<mojo::NativeViewport> request, | |
| 24 scoped_ptr<mojo::AppRefCount> app_refcount) | |
| 25 : is_headless_(is_headless), | |
| 26 app_refcount_(app_refcount.Pass()), | |
| 27 context_provider_(new OnscreenContextProvider(gpu_state)), | |
| 28 sent_metrics_(false), | |
| 29 metrics_(mojo::ViewportMetrics::New()), | |
| 30 binding_(this, request.Pass()), | |
| 31 weak_factory_(this) { | |
| 32 binding_.set_error_handler(this); | |
| 33 } | |
| 34 | |
| 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 | |
| 41 // Destroy the NativeViewport early on as it may call us back during | |
| 42 // destruction and we want to be in a known state. | |
| 43 platform_viewport_.reset(); | |
| 44 } | |
| 45 | |
| 46 void NativeViewportImpl::Create(mojo::SizePtr size, | |
| 47 const CreateCallback& callback) { | |
| 48 create_callback_ = callback; | |
| 49 metrics_->size = size.Clone(); | |
| 50 if (is_headless_) | |
| 51 platform_viewport_ = PlatformViewportHeadless::Create(this); | |
| 52 else | |
| 53 platform_viewport_ = PlatformViewport::Create(this); | |
| 54 platform_viewport_->Init(gfx::Rect(size.To<gfx::Size>())); | |
| 55 } | |
| 56 | |
| 57 void NativeViewportImpl::RequestMetrics( | |
| 58 const RequestMetricsCallback& callback) { | |
| 59 if (!sent_metrics_) { | |
| 60 callback.Run(metrics_.Clone()); | |
| 61 sent_metrics_ = true; | |
| 62 return; | |
| 63 } | |
| 64 metrics_callback_ = callback; | |
| 65 } | |
| 66 | |
| 67 void NativeViewportImpl::Show() { | |
| 68 platform_viewport_->Show(); | |
| 69 } | |
| 70 | |
| 71 void NativeViewportImpl::Hide() { | |
| 72 platform_viewport_->Hide(); | |
| 73 } | |
| 74 | |
| 75 void NativeViewportImpl::Close() { | |
| 76 DCHECK(platform_viewport_); | |
| 77 platform_viewport_->Close(); | |
| 78 } | |
| 79 | |
| 80 void NativeViewportImpl::SetSize(mojo::SizePtr size) { | |
| 81 platform_viewport_->SetBounds(gfx::Rect(size.To<gfx::Size>())); | |
| 82 } | |
| 83 | |
| 84 void NativeViewportImpl::GetContextProvider( | |
| 85 mojo::InterfaceRequest<mojo::ContextProvider> request) { | |
| 86 context_provider_->Bind(request.Pass()); | |
| 87 } | |
| 88 | |
| 89 void NativeViewportImpl::SetEventDispatcher( | |
| 90 mojo::NativeViewportEventDispatcherPtr dispatcher) { | |
| 91 event_dispatcher_ = dispatcher.Pass(); | |
| 92 } | |
| 93 | |
| 94 void NativeViewportImpl::OnMetricsChanged(mojo::ViewportMetricsPtr metrics) { | |
| 95 if (metrics_->Equals(*metrics)) | |
| 96 return; | |
| 97 | |
| 98 metrics_ = metrics.Pass(); | |
| 99 sent_metrics_ = false; | |
| 100 | |
| 101 if (!metrics_callback_.is_null()) { | |
| 102 metrics_callback_.Run(metrics_.Clone()); | |
| 103 metrics_callback_.reset(); | |
| 104 sent_metrics_ = true; | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 void NativeViewportImpl::OnAcceleratedWidgetAvailable( | |
| 109 gfx::AcceleratedWidget widget, | |
| 110 float device_pixel_ratio) { | |
| 111 metrics_->device_pixel_ratio = device_pixel_ratio; | |
| 112 context_provider_->SetAcceleratedWidget(widget); | |
| 113 // TODO: The metrics here might not match the actual window size on android | |
| 114 // where we don't know the actual size until the first OnMetricsChanged call. | |
| 115 create_callback_.Run(metrics_.Clone()); | |
| 116 sent_metrics_ = true; | |
| 117 create_callback_.reset(); | |
| 118 } | |
| 119 | |
| 120 void NativeViewportImpl::OnAcceleratedWidgetDestroyed() { | |
| 121 context_provider_->SetAcceleratedWidget(gfx::kNullAcceleratedWidget); | |
| 122 } | |
| 123 | |
| 124 bool NativeViewportImpl::OnEvent(mojo::EventPtr event) { | |
| 125 if (event.is_null() || !event_dispatcher_.get()) | |
| 126 return false; | |
| 127 | |
| 128 mojo::NativeViewportEventDispatcher::OnEventCallback callback; | |
| 129 switch (event->action) { | |
| 130 case mojo::EVENT_TYPE_POINTER_MOVE: { | |
| 131 // TODO(sky): add logic to remember last event location and not send if | |
| 132 // the same. | |
| 133 if (pointers_waiting_on_ack_.count(event->pointer_data->pointer_id)) | |
| 134 return false; | |
| 135 | |
| 136 pointers_waiting_on_ack_.insert(event->pointer_data->pointer_id); | |
| 137 callback = | |
| 138 base::Bind(&NativeViewportImpl::AckEvent, weak_factory_.GetWeakPtr(), | |
| 139 event->pointer_data->pointer_id); | |
| 140 break; | |
| 141 } | |
| 142 | |
| 143 case mojo::EVENT_TYPE_POINTER_CANCEL: | |
| 144 pointers_waiting_on_ack_.clear(); | |
| 145 break; | |
| 146 | |
| 147 case mojo::EVENT_TYPE_POINTER_UP: | |
| 148 pointers_waiting_on_ack_.erase(event->pointer_data->pointer_id); | |
| 149 break; | |
| 150 | |
| 151 default: | |
| 152 break; | |
| 153 } | |
| 154 | |
| 155 event_dispatcher_->OnEvent(event.Pass(), callback); | |
| 156 return false; | |
| 157 } | |
| 158 | |
| 159 void NativeViewportImpl::OnDestroyed() { | |
| 160 delete this; | |
| 161 } | |
| 162 | |
| 163 void NativeViewportImpl::OnConnectionError() { | |
| 164 binding_.set_error_handler(nullptr); | |
| 165 delete this; | |
| 166 } | |
| 167 | |
| 168 void NativeViewportImpl::AckEvent(int32 pointer_id) { | |
| 169 pointers_waiting_on_ack_.erase(pointer_id); | |
| 170 } | |
| 171 | |
| 172 } // namespace native_viewport | |
| OLD | NEW |