| 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), | |
| 22 m_binding(this) { | 21 m_binding(this) { |
| 23 navigatorVR->document()->frame()->interfaceProvider()->getInterface( | 22 navigatorVR->document()->frame()->interfaceProvider()->getInterface( |
| 24 mojo::GetProxy(&m_service)); | 23 mojo::GetProxy(&m_service)); |
| 25 m_service->SetClient( | 24 m_service->SetClient(m_binding.CreateInterfacePtrAndBind()); |
| 26 m_binding.CreateInterfacePtrAndBind(), | |
| 27 convertToBaseCallback( | |
| 28 WTF::bind(&VRController::onDisplaysSynced, wrapPersistent(this)))); | |
| 29 ThreadState::current()->registerPreFinalizer(this); | |
| 30 } | 25 } |
| 31 | 26 |
| 32 VRController::~VRController() {} | 27 VRController::~VRController() {} |
| 33 | 28 |
| 34 void VRController::getDisplays(ScriptPromiseResolver* resolver) { | 29 void VRController::getDisplays(ScriptPromiseResolver* resolver) { |
| 35 if (!m_service) { | 30 if (!m_service) { |
| 36 DOMException* exception = DOMException::create( | 31 DOMException* exception = DOMException::create( |
| 37 InvalidStateError, "The service is no longer active."); | 32 InvalidStateError, "The service is no longer active."); |
| 38 resolver->reject(exception); | 33 resolver->reject(exception); |
| 39 return; | 34 return; |
| 40 } | 35 } |
| 41 | 36 |
| 42 // If we've previously synced the VRDisplays just return the current list. | 37 m_pendingGetDevicesCallbacks.append( |
| 43 if (m_displaySynced) { | 38 WTF::wrapUnique(new VRGetDevicesCallback(resolver))); |
| 44 resolver->resolve(m_displays); | 39 m_service->GetDisplays(convertToBaseCallback( |
| 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); |
| 45 return; | 67 return; |
| 46 } | 68 } |
| 47 | 69 |
| 48 // Otherwise we're still waiting for the full list of displays to be populated | 70 m_service->RequestPresent( |
| 49 // so queue up the promise for resolution when onDisplaysSynced is called. | 71 index, secureOrigin, |
| 50 m_pendingGetDevicesCallbacks.append( | 72 convertToBaseCallback(WTF::bind(&VRController::onPresentComplete, |
| 51 wrapUnique(new VRGetDevicesCallback(resolver))); | 73 wrapPersistent(this), |
| 74 wrapPersistent(resolver), index))); |
| 52 } | 75 } |
| 53 | 76 |
| 54 // Each time a new VRDisplay is connected we'll recieve a VRDisplayPtr for it | 77 void VRController::exitPresent(unsigned index) { |
| 55 // here. Upon calling SetClient in the constructor we should receive one call | 78 if (!m_service) |
| 56 // for each VRDisplay that was already connected at the time. | 79 return; |
| 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); | |
| 66 | 80 |
| 67 if (m_displays.size() == m_numberOfSyncedDisplays) { | 81 m_service->ExitPresent(index); |
| 68 m_displaySynced = true; | 82 } |
| 69 onGetDisplays(); | 83 |
| 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); |
| 70 } | 170 } |
| 71 } | 171 } |
| 72 | 172 |
| 73 // Called when the VRService has called OnDisplayConnected for all active | 173 void VRController::OnDisplayChanged(device::blink::VRDisplayPtr display) { |
| 74 // VRDisplays. | 174 VRDisplay* vrDisplay = getDisplayForIndex(display->index); |
| 75 void VRController::onDisplaysSynced(unsigned numberOfDisplays) { | 175 if (!vrDisplay) |
| 76 m_numberOfSyncedDisplays = numberOfDisplays; | 176 return; |
| 77 if (m_numberOfSyncedDisplays == m_displays.size()) { | 177 |
| 78 m_displaySynced = true; | 178 vrDisplay->update(display); |
| 79 onGetDisplays(); | |
| 80 } | |
| 81 } | 179 } |
| 82 | 180 |
| 83 void VRController::onGetDisplays() { | 181 void VRController::OnExitPresent(unsigned index) { |
| 84 while (!m_pendingGetDevicesCallbacks.isEmpty()) { | 182 VRDisplay* vrDisplay = getDisplayForIndex(index); |
| 85 std::unique_ptr<VRGetDevicesCallback> callback = | 183 if (vrDisplay) |
| 86 m_pendingGetDevicesCallbacks.takeFirst(); | 184 vrDisplay->forceExitPresent(); |
| 87 callback->onSuccess(m_displays); | 185 } |
| 88 } | 186 |
| 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")); |
| 89 } | 206 } |
| 90 | 207 |
| 91 void VRController::contextDestroyed() { | 208 void VRController::contextDestroyed() { |
| 92 dispose(); | 209 // If the document context was destroyed, shut down the client connection |
| 210 // and never call the mojo service again. |
| 211 m_binding.Close(); |
| 212 m_service.reset(); |
| 213 |
| 93 // The context is not automatically cleared, so do it manually. | 214 // The context is not automatically cleared, so do it manually. |
| 94 ContextLifecycleObserver::clearContext(); | 215 ContextLifecycleObserver::clearContext(); |
| 95 } | 216 } |
| 96 | 217 |
| 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 | |
| 108 DEFINE_TRACE(VRController) { | 218 DEFINE_TRACE(VRController) { |
| 109 visitor->trace(m_navigatorVR); | 219 visitor->trace(m_navigatorVR); |
| 110 visitor->trace(m_displays); | 220 visitor->trace(m_displays); |
| 111 | 221 |
| 112 ContextLifecycleObserver::trace(visitor); | 222 ContextLifecycleObserver::trace(visitor); |
| 113 } | 223 } |
| 114 | 224 |
| 115 } // namespace blink | 225 } // namespace blink |
| OLD | NEW |