| 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 "mojo/services/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 "mojo/converters/geometry/geometry_type_converters.h" | |
| 13 #include "mojo/converters/input_events/input_events_type_converters.h" | |
| 14 #include "mojo/converters/surfaces/surfaces_type_converters.h" | |
| 15 #include "mojo/public/cpp/application/application_delegate.h" | |
| 16 #include "mojo/public/cpp/application/application_impl.h" | |
| 17 #include "mojo/public/cpp/application/interface_factory.h" | |
| 18 #include "mojo/services/native_viewport/platform_viewport_headless.h" | |
| 19 #include "mojo/services/native_viewport/viewport_surface.h" | |
| 20 #include "ui/events/event.h" | |
| 21 | |
| 22 namespace mojo { | |
| 23 namespace { | |
| 24 | |
| 25 bool IsRateLimitedEventType(ui::Event* event) { | |
| 26 return event->type() == ui::ET_MOUSE_MOVED || | |
| 27 event->type() == ui::ET_MOUSE_DRAGGED || | |
| 28 event->type() == ui::ET_TOUCH_MOVED; | |
| 29 } | |
| 30 | |
| 31 } // namespace | |
| 32 | |
| 33 NativeViewportImpl::NativeViewportImpl(ApplicationImpl* app, bool is_headless) | |
| 34 : is_headless_(is_headless), | |
| 35 widget_id_(0u), | |
| 36 waiting_for_event_ack_(false), | |
| 37 weak_factory_(this) { | |
| 38 app->ConnectToService("mojo:surfaces_service", &surfaces_service_); | |
| 39 // TODO(jamesr): Should be mojo_gpu_service | |
| 40 app->ConnectToService("mojo:native_viewport_service", &gpu_service_); | |
| 41 } | |
| 42 | |
| 43 NativeViewportImpl::~NativeViewportImpl() { | |
| 44 // Destroy the NativeViewport early on as it may call us back during | |
| 45 // destruction and we want to be in a known state. | |
| 46 platform_viewport_.reset(); | |
| 47 } | |
| 48 | |
| 49 void NativeViewportImpl::Create(SizePtr size, | |
| 50 const Callback<void(uint64_t)>& callback) { | |
| 51 create_callback_ = callback; | |
| 52 size_ = size.To<gfx::Size>(); | |
| 53 if (is_headless_) | |
| 54 platform_viewport_ = PlatformViewportHeadless::Create(this); | |
| 55 else | |
| 56 platform_viewport_ = PlatformViewport::Create(this); | |
| 57 platform_viewport_->Init(gfx::Rect(size.To<gfx::Size>())); | |
| 58 } | |
| 59 | |
| 60 void NativeViewportImpl::Show() { | |
| 61 platform_viewport_->Show(); | |
| 62 } | |
| 63 | |
| 64 void NativeViewportImpl::Hide() { | |
| 65 platform_viewport_->Hide(); | |
| 66 } | |
| 67 | |
| 68 void NativeViewportImpl::Close() { | |
| 69 DCHECK(platform_viewport_); | |
| 70 platform_viewport_->Close(); | |
| 71 } | |
| 72 | |
| 73 void NativeViewportImpl::SetSize(SizePtr size) { | |
| 74 platform_viewport_->SetBounds(gfx::Rect(size.To<gfx::Size>())); | |
| 75 } | |
| 76 | |
| 77 void NativeViewportImpl::SubmittedFrame(SurfaceIdPtr child_surface_id) { | |
| 78 if (child_surface_id_.is_null()) { | |
| 79 // If this is the first indication that the client will use surfaces, | |
| 80 // initialize that system. | |
| 81 // TODO(jamesr): When everything is converted to surfaces initialize this | |
| 82 // eagerly. | |
| 83 viewport_surface_.reset( | |
| 84 new ViewportSurface(surfaces_service_.get(), | |
| 85 gpu_service_.get(), | |
| 86 size_, | |
| 87 child_surface_id.To<cc::SurfaceId>())); | |
| 88 if (widget_id_) | |
| 89 viewport_surface_->SetWidgetId(widget_id_); | |
| 90 } | |
| 91 child_surface_id_ = child_surface_id.To<cc::SurfaceId>(); | |
| 92 if (viewport_surface_) | |
| 93 viewport_surface_->SetChildId(child_surface_id_); | |
| 94 } | |
| 95 | |
| 96 void NativeViewportImpl::SetEventDispatcher( | |
| 97 NativeViewportEventDispatcherPtr dispatcher) { | |
| 98 event_dispatcher_ = dispatcher.Pass(); | |
| 99 } | |
| 100 | |
| 101 void NativeViewportImpl::OnBoundsChanged(const gfx::Rect& bounds) { | |
| 102 if (size_ == bounds.size()) | |
| 103 return; | |
| 104 | |
| 105 size_ = bounds.size(); | |
| 106 | |
| 107 // Wait for the accelerated widget before telling the client of the bounds. | |
| 108 if (create_callback_.is_null()) | |
| 109 ProcessOnBoundsChanged(); | |
| 110 } | |
| 111 | |
| 112 void NativeViewportImpl::OnAcceleratedWidgetAvailable( | |
| 113 gfx::AcceleratedWidget widget) { | |
| 114 widget_id_ = static_cast<uint64_t>(bit_cast<uintptr_t>(widget)); | |
| 115 // TODO(jamesr): Remove once everything is converted to surfaces. | |
| 116 create_callback_.Run(widget_id_); | |
| 117 create_callback_.reset(); | |
| 118 // Immediately tell the client of the size. The size may be wrong, if so we'll | |
| 119 // get the right one in the next OnBoundsChanged() call. | |
| 120 ProcessOnBoundsChanged(); | |
| 121 if (viewport_surface_) | |
| 122 viewport_surface_->SetWidgetId(widget_id_); | |
| 123 } | |
| 124 | |
| 125 bool NativeViewportImpl::OnEvent(ui::Event* ui_event) { | |
| 126 if (!event_dispatcher_.get()) | |
| 127 return false; | |
| 128 | |
| 129 // Must not return early before updating capture. | |
| 130 switch (ui_event->type()) { | |
| 131 case ui::ET_MOUSE_PRESSED: | |
| 132 case ui::ET_TOUCH_PRESSED: | |
| 133 platform_viewport_->SetCapture(); | |
| 134 break; | |
| 135 case ui::ET_MOUSE_RELEASED: | |
| 136 case ui::ET_TOUCH_RELEASED: | |
| 137 platform_viewport_->ReleaseCapture(); | |
| 138 break; | |
| 139 default: | |
| 140 break; | |
| 141 } | |
| 142 | |
| 143 if (waiting_for_event_ack_ && IsRateLimitedEventType(ui_event)) | |
| 144 return false; | |
| 145 | |
| 146 event_dispatcher_->OnEvent( | |
| 147 Event::From(*ui_event), | |
| 148 base::Bind(&NativeViewportImpl::AckEvent, weak_factory_.GetWeakPtr())); | |
| 149 waiting_for_event_ack_ = true; | |
| 150 return false; | |
| 151 } | |
| 152 | |
| 153 void NativeViewportImpl::OnDestroyed() { | |
| 154 client()->OnDestroyed(); | |
| 155 } | |
| 156 | |
| 157 void NativeViewportImpl::AckEvent() { | |
| 158 waiting_for_event_ack_ = false; | |
| 159 } | |
| 160 | |
| 161 void NativeViewportImpl::ProcessOnBoundsChanged() { | |
| 162 client()->OnSizeChanged(Size::From(size_)); | |
| 163 if (viewport_surface_) | |
| 164 viewport_surface_->SetSize(size_); | |
| 165 } | |
| 166 | |
| 167 } // namespace mojo | |
| 168 | |
| OLD | NEW |