| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "modules/vr/VRController.h" | 5 #include "modules/vr/VRController.h" |
| 6 | 6 |
| 7 #include "core/dom/DOMException.h" | 7 #include "core/dom/DOMException.h" |
| 8 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
| 9 #include "core/frame/LocalFrame.h" | 9 #include "core/frame/LocalFrame.h" |
| 10 #include "modules/vr/NavigatorVR.h" | 10 #include "modules/vr/NavigatorVR.h" |
| 11 #include "modules/vr/VRGetDevicesCallback.h" | 11 #include "modules/vr/VRGetDevicesCallback.h" |
| 12 #include "public/platform/InterfaceProvider.h" | 12 #include "public/platform/InterfaceProvider.h" |
| 13 | 13 |
| 14 #include "wtf/Assertions.h" | 14 #include "wtf/Assertions.h" |
| 15 | 15 |
| 16 namespace blink { | 16 namespace blink { |
| 17 | 17 |
| 18 VRController::VRController(NavigatorVR* navigatorVR) | 18 VRController::VRController(NavigatorVR* navigatorVR) |
| 19 : ContextLifecycleObserver(navigatorVR->document()), | 19 : ContextLifecycleObserver(navigatorVR->document()), |
| 20 m_navigatorVR(navigatorVR), | 20 m_navigatorVR(navigatorVR), |
| 21 m_displaySynced(false), |
| 21 m_binding(this) { | 22 m_binding(this) { |
| 22 navigatorVR->document()->frame()->interfaceProvider()->getInterface( | 23 navigatorVR->document()->frame()->interfaceProvider()->getInterface( |
| 23 mojo::GetProxy(&m_service)); | 24 mojo::GetProxy(&m_service)); |
| 24 m_service->SetClient(m_binding.CreateInterfacePtrAndBind()); | 25 m_service->SetClient( |
| 26 m_binding.CreateInterfacePtrAndBind(), |
| 27 convertToBaseCallback( |
| 28 WTF::bind(&VRController::onDisplaysSynced, wrapPersistent(this)))); |
| 29 ThreadState::current()->registerPreFinalizer(this); |
| 25 } | 30 } |
| 26 | 31 |
| 27 VRController::~VRController() {} | 32 VRController::~VRController() {} |
| 28 | 33 |
| 29 void VRController::getDisplays(ScriptPromiseResolver* resolver) { | 34 void VRController::getDisplays(ScriptPromiseResolver* resolver) { |
| 30 if (!m_service) { | 35 if (!m_service) { |
| 31 DOMException* exception = DOMException::create( | 36 DOMException* exception = DOMException::create( |
| 32 InvalidStateError, "The service is no longer active."); | 37 InvalidStateError, "The service is no longer active."); |
| 33 resolver->reject(exception); | 38 resolver->reject(exception); |
| 34 return; | 39 return; |
| 35 } | 40 } |
| 36 | 41 |
| 37 m_pendingGetDevicesCallbacks.append( | 42 // If we've previously synced the VRDisplays just return the current list. |
| 38 WTF::wrapUnique(new VRGetDevicesCallback(resolver))); | 43 if (m_displaySynced) { |
| 39 m_service->GetDisplays(convertToBaseCallback( | 44 resolver->resolve(m_displays); |
| 40 WTF::bind(&VRController::onGetDisplays, wrapPersistent(this)))); | |
| 41 } | |
| 42 | |
| 43 device::blink::VRPosePtr VRController::getPose(unsigned index) { | |
| 44 if (!m_service) | |
| 45 return nullptr; | |
| 46 | |
| 47 device::blink::VRPosePtr pose; | |
| 48 m_service->GetPose(index, &pose); | |
| 49 return pose; | |
| 50 } | |
| 51 | |
| 52 void VRController::resetPose(unsigned index) { | |
| 53 if (!m_service) | |
| 54 return; | |
| 55 | |
| 56 m_service->ResetPose(index); | |
| 57 } | |
| 58 | |
| 59 void VRController::requestPresent(ScriptPromiseResolver* resolver, | |
| 60 unsigned index, | |
| 61 bool secureOrigin) { | |
| 62 if (!m_service) { | |
| 63 DOMException* exception = DOMException::create( | |
| 64 InvalidStateError, "The service is no longer active."); | |
| 65 resolver->reject(exception); | |
| 66 ReportPresentationResult(PresentationResult::ServiceInactive); | |
| 67 return; | 45 return; |
| 68 } | 46 } |
| 69 | 47 |
| 70 m_service->RequestPresent( | 48 // Otherwise we're still waiting for the full list of displays to be populated |
| 71 index, secureOrigin, | 49 // so queue up the promise for resolution when onDisplaysSynced is called. |
| 72 convertToBaseCallback(WTF::bind(&VRController::onPresentComplete, | 50 m_pendingGetDevicesCallbacks.append( |
| 73 wrapPersistent(this), | 51 wrapUnique(new VRGetDevicesCallback(resolver))); |
| 74 wrapPersistent(resolver), index))); | |
| 75 } | 52 } |
| 76 | 53 |
| 77 void VRController::exitPresent(unsigned index) { | 54 // Each time a new VRDisplay is connected we'll recieve a VRDisplayPtr for it |
| 78 if (!m_service) | 55 // here. Upon calling SetClient in the constructor we should receive one call |
| 79 return; | 56 // for each VRDisplay that was already connected at the time. |
| 57 void VRController::OnDisplayConnected( |
| 58 device::mojom::blink::VRDisplayPtr display, |
| 59 device::mojom::blink::VRDisplayClientRequest request, |
| 60 device::mojom::blink::VRDisplayInfoPtr displayInfo) { |
| 61 VRDisplay* vrDisplay = |
| 62 new VRDisplay(m_navigatorVR, std::move(display), std::move(request)); |
| 63 vrDisplay->update(displayInfo); |
| 64 vrDisplay->onDisplayConnected(); |
| 65 m_displays.append(vrDisplay); |
| 80 | 66 |
| 81 m_service->ExitPresent(index); | 67 if (m_displays.size() == m_numberOfSyncedDisplays) { |
| 82 } | 68 m_displaySynced = true; |
| 83 | 69 onGetDisplays(); |
| 84 void VRController::submitFrame(unsigned index, device::blink::VRPosePtr pose) { | |
| 85 if (!m_service) | |
| 86 return; | |
| 87 | |
| 88 m_service->SubmitFrame(index, std::move(pose)); | |
| 89 } | |
| 90 | |
| 91 void VRController::updateLayerBounds( | |
| 92 unsigned index, | |
| 93 device::blink::VRLayerBoundsPtr leftBounds, | |
| 94 device::blink::VRLayerBoundsPtr rightBounds) { | |
| 95 if (!m_service) | |
| 96 return; | |
| 97 | |
| 98 m_service->UpdateLayerBounds(index, std::move(leftBounds), | |
| 99 std::move(rightBounds)); | |
| 100 } | |
| 101 | |
| 102 VRDisplay* VRController::createOrUpdateDisplay( | |
| 103 const device::blink::VRDisplayPtr& display) { | |
| 104 VRDisplay* vrDisplay = getDisplayForIndex(display->index); | |
| 105 if (!vrDisplay) { | |
| 106 vrDisplay = new VRDisplay(m_navigatorVR); | |
| 107 m_displays.append(vrDisplay); | |
| 108 } | |
| 109 | |
| 110 vrDisplay->update(display); | |
| 111 return vrDisplay; | |
| 112 } | |
| 113 | |
| 114 VRDisplayVector VRController::updateDisplays( | |
| 115 mojo::WTFArray<device::blink::VRDisplayPtr> displays) { | |
| 116 VRDisplayVector vrDisplays; | |
| 117 | |
| 118 for (const auto& display : displays.PassStorage()) { | |
| 119 VRDisplay* vrDisplay = createOrUpdateDisplay(display); | |
| 120 vrDisplays.append(vrDisplay); | |
| 121 } | |
| 122 | |
| 123 return vrDisplays; | |
| 124 } | |
| 125 | |
| 126 VRDisplay* VRController::getDisplayForIndex(unsigned index) { | |
| 127 VRDisplay* display; | |
| 128 for (size_t i = 0; i < m_displays.size(); ++i) { | |
| 129 display = m_displays[i]; | |
| 130 if (display->displayId() == index) { | |
| 131 return display; | |
| 132 } | |
| 133 } | |
| 134 | |
| 135 return 0; | |
| 136 } | |
| 137 | |
| 138 void VRController::onGetDisplays( | |
| 139 mojo::WTFArray<device::blink::VRDisplayPtr> displays) { | |
| 140 VRDisplayVector outDisplays = updateDisplays(std::move(displays)); | |
| 141 | |
| 142 std::unique_ptr<VRGetDevicesCallback> callback = | |
| 143 m_pendingGetDevicesCallbacks.takeFirst(); | |
| 144 if (!callback) | |
| 145 return; | |
| 146 | |
| 147 callback->onSuccess(outDisplays); | |
| 148 } | |
| 149 | |
| 150 void VRController::onPresentComplete(ScriptPromiseResolver* resolver, | |
| 151 unsigned index, | |
| 152 bool success) { | |
| 153 VRDisplay* vrDisplay = getDisplayForIndex(index); | |
| 154 if (!vrDisplay) { | |
| 155 DOMException* exception = | |
| 156 DOMException::create(InvalidStateError, "VRDisplay not found."); | |
| 157 resolver->reject(exception); | |
| 158 ReportPresentationResult(PresentationResult::VRDisplayNotFound); | |
| 159 return; | |
| 160 } | |
| 161 | |
| 162 if (success) { | |
| 163 vrDisplay->beginPresent(resolver); | |
| 164 } else { | |
| 165 vrDisplay->forceExitPresent(); | |
| 166 DOMException* exception = DOMException::create( | |
| 167 NotAllowedError, "Presentation request was denied."); | |
| 168 ReportPresentationResult(PresentationResult::RequestDenied); | |
| 169 resolver->reject(exception); | |
| 170 } | 70 } |
| 171 } | 71 } |
| 172 | 72 |
| 173 void VRController::OnDisplayChanged(device::blink::VRDisplayPtr display) { | 73 // Called when the VRService has called OnDisplayConnected for all active |
| 174 VRDisplay* vrDisplay = getDisplayForIndex(display->index); | 74 // VRDisplays. |
| 175 if (!vrDisplay) | 75 void VRController::onDisplaysSynced(unsigned numberOfDisplays) { |
| 176 return; | 76 m_numberOfSyncedDisplays = numberOfDisplays; |
| 177 | 77 if (m_numberOfSyncedDisplays == m_displays.size()) { |
| 178 vrDisplay->update(display); | 78 m_displaySynced = true; |
| 79 onGetDisplays(); |
| 80 } |
| 179 } | 81 } |
| 180 | 82 |
| 181 void VRController::OnExitPresent(unsigned index) { | 83 void VRController::onGetDisplays() { |
| 182 VRDisplay* vrDisplay = getDisplayForIndex(index); | 84 while (!m_pendingGetDevicesCallbacks.isEmpty()) { |
| 183 if (vrDisplay) | 85 std::unique_ptr<VRGetDevicesCallback> callback = |
| 184 vrDisplay->forceExitPresent(); | 86 m_pendingGetDevicesCallbacks.takeFirst(); |
| 185 } | 87 callback->onSuccess(m_displays); |
| 186 | 88 } |
| 187 void VRController::OnDisplayConnected(device::blink::VRDisplayPtr display) { | |
| 188 VRDisplay* vrDisplay = createOrUpdateDisplay(display); | |
| 189 if (!vrDisplay) | |
| 190 return; | |
| 191 | |
| 192 m_navigatorVR->fireVREvent(VRDisplayEvent::create( | |
| 193 EventTypeNames::vrdisplayconnect, true, false, vrDisplay, "connect")); | |
| 194 } | |
| 195 | |
| 196 void VRController::OnDisplayDisconnected(unsigned index) { | |
| 197 VRDisplay* vrDisplay = getDisplayForIndex(index); | |
| 198 if (!vrDisplay) | |
| 199 return; | |
| 200 | |
| 201 vrDisplay->disconnected(); | |
| 202 | |
| 203 m_navigatorVR->fireVREvent( | |
| 204 VRDisplayEvent::create(EventTypeNames::vrdisplaydisconnect, true, false, | |
| 205 vrDisplay, "disconnect")); | |
| 206 } | 89 } |
| 207 | 90 |
| 208 void VRController::contextDestroyed() { | 91 void VRController::contextDestroyed() { |
| 209 // If the document context was destroyed, shut down the client connection | 92 dispose(); |
| 210 // and never call the mojo service again. | |
| 211 m_binding.Close(); | |
| 212 m_service.reset(); | |
| 213 | |
| 214 // The context is not automatically cleared, so do it manually. | 93 // The context is not automatically cleared, so do it manually. |
| 215 ContextLifecycleObserver::clearContext(); | 94 ContextLifecycleObserver::clearContext(); |
| 216 } | 95 } |
| 217 | 96 |
| 97 void VRController::dispose() { |
| 98 // If the document context was destroyed, shut down the client connection |
| 99 // and never call the mojo service again. |
| 100 m_service.reset(); |
| 101 m_binding.Close(); |
| 102 |
| 103 // Shutdown all displays' message pipe |
| 104 for (size_t i = 0; i < m_displays.size(); ++i) |
| 105 m_displays[i]->dispose(); |
| 106 } |
| 107 |
| 218 DEFINE_TRACE(VRController) { | 108 DEFINE_TRACE(VRController) { |
| 219 visitor->trace(m_navigatorVR); | 109 visitor->trace(m_navigatorVR); |
| 220 visitor->trace(m_displays); | 110 visitor->trace(m_displays); |
| 221 | 111 |
| 222 ContextLifecycleObserver::trace(visitor); | 112 ContextLifecycleObserver::trace(visitor); |
| 223 } | 113 } |
| 224 | 114 |
| 225 } // namespace blink | 115 } // namespace blink |
| OLD | NEW |