| 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" |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 resolver->reject(exception); | 33 resolver->reject(exception); |
| 34 return; | 34 return; |
| 35 } | 35 } |
| 36 | 36 |
| 37 m_pendingGetDevicesCallbacks.append( | 37 m_pendingGetDevicesCallbacks.append( |
| 38 WTF::wrapUnique(new VRGetDevicesCallback(resolver))); | 38 WTF::wrapUnique(new VRGetDevicesCallback(resolver))); |
| 39 m_service->GetDisplays(convertToBaseCallback( | 39 m_service->GetDisplays(convertToBaseCallback( |
| 40 WTF::bind(&VRController::onGetDisplays, wrapPersistent(this)))); | 40 WTF::bind(&VRController::onGetDisplays, wrapPersistent(this)))); |
| 41 } | 41 } |
| 42 | 42 |
| 43 device::blink::VRPosePtr VRController::getPose(unsigned index) { | 43 void VRController::GetDisplayClient(const GetDisplayClientCallback& callback) { |
| 44 if (!m_service) | 44 VRDisplay* vrDisplay = new VRDisplay(m_navigatorVR); |
| 45 return nullptr; | 45 m_displays.append(vrDisplay); |
| 46 | 46 callback.Run(vrDisplay->BindClient()); |
| 47 device::blink::VRPosePtr pose; | |
| 48 m_service->GetPose(index, &pose); | |
| 49 return pose; | |
| 50 } | 47 } |
| 51 | 48 |
| 52 void VRController::resetPose(unsigned index) { | 49 void VRController::onGetDisplays(bool success) { |
| 53 if (!m_service) | 50 VRDisplayVector outDisplays; |
| 54 return; | |
| 55 | 51 |
| 56 m_service->ResetPose(index); | 52 if (success) |
| 57 } | 53 outDisplays = m_displays; |
| 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 return; | |
| 67 } | |
| 68 | |
| 69 m_service->RequestPresent( | |
| 70 index, secureOrigin, | |
| 71 convertToBaseCallback(WTF::bind(&VRController::onPresentComplete, | |
| 72 wrapPersistent(this), | |
| 73 wrapPersistent(resolver), index))); | |
| 74 } | |
| 75 | |
| 76 void VRController::exitPresent(unsigned index) { | |
| 77 if (!m_service) | |
| 78 return; | |
| 79 | |
| 80 m_service->ExitPresent(index); | |
| 81 } | |
| 82 | |
| 83 void VRController::submitFrame(unsigned index, device::blink::VRPosePtr pose) { | |
| 84 if (!m_service) | |
| 85 return; | |
| 86 | |
| 87 m_service->SubmitFrame(index, std::move(pose)); | |
| 88 } | |
| 89 | |
| 90 void VRController::updateLayerBounds( | |
| 91 unsigned index, | |
| 92 device::blink::VRLayerBoundsPtr leftBounds, | |
| 93 device::blink::VRLayerBoundsPtr rightBounds) { | |
| 94 if (!m_service) | |
| 95 return; | |
| 96 | |
| 97 m_service->UpdateLayerBounds(index, std::move(leftBounds), | |
| 98 std::move(rightBounds)); | |
| 99 } | |
| 100 | |
| 101 VRDisplay* VRController::createOrUpdateDisplay( | |
| 102 const device::blink::VRDisplayPtr& display) { | |
| 103 VRDisplay* vrDisplay = getDisplayForIndex(display->index); | |
| 104 if (!vrDisplay) { | |
| 105 vrDisplay = new VRDisplay(m_navigatorVR); | |
| 106 m_displays.append(vrDisplay); | |
| 107 } | |
| 108 | |
| 109 vrDisplay->update(display); | |
| 110 return vrDisplay; | |
| 111 } | |
| 112 | |
| 113 VRDisplayVector VRController::updateDisplays( | |
| 114 mojo::WTFArray<device::blink::VRDisplayPtr> displays) { | |
| 115 VRDisplayVector vrDisplays; | |
| 116 | |
| 117 for (const auto& display : displays.PassStorage()) { | |
| 118 VRDisplay* vrDisplay = createOrUpdateDisplay(display); | |
| 119 vrDisplays.append(vrDisplay); | |
| 120 } | |
| 121 | |
| 122 return vrDisplays; | |
| 123 } | |
| 124 | |
| 125 VRDisplay* VRController::getDisplayForIndex(unsigned index) { | |
| 126 VRDisplay* display; | |
| 127 for (size_t i = 0; i < m_displays.size(); ++i) { | |
| 128 display = m_displays[i]; | |
| 129 if (display->displayId() == index) { | |
| 130 return display; | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 return 0; | |
| 135 } | |
| 136 | |
| 137 void VRController::onGetDisplays( | |
| 138 mojo::WTFArray<device::blink::VRDisplayPtr> displays) { | |
| 139 VRDisplayVector outDisplays = updateDisplays(std::move(displays)); | |
| 140 | 54 |
| 141 std::unique_ptr<VRGetDevicesCallback> callback = | 55 std::unique_ptr<VRGetDevicesCallback> callback = |
| 142 m_pendingGetDevicesCallbacks.takeFirst(); | 56 m_pendingGetDevicesCallbacks.takeFirst(); |
| 143 if (!callback) | 57 if (!callback) |
| 144 return; | 58 return; |
| 145 | 59 |
| 146 callback->onSuccess(outDisplays); | 60 callback->onSuccess(outDisplays); |
| 147 } | 61 } |
| 148 | 62 |
| 149 void VRController::onPresentComplete(ScriptPromiseResolver* resolver, | |
| 150 unsigned index, | |
| 151 bool success) { | |
| 152 VRDisplay* vrDisplay = getDisplayForIndex(index); | |
| 153 if (!vrDisplay) { | |
| 154 DOMException* exception = | |
| 155 DOMException::create(InvalidStateError, "VRDisplay not found."); | |
| 156 resolver->reject(exception); | |
| 157 return; | |
| 158 } | |
| 159 | |
| 160 if (success) { | |
| 161 vrDisplay->beginPresent(resolver); | |
| 162 } else { | |
| 163 vrDisplay->forceExitPresent(); | |
| 164 DOMException* exception = DOMException::create( | |
| 165 NotAllowedError, "Presentation request was denied."); | |
| 166 resolver->reject(exception); | |
| 167 } | |
| 168 } | |
| 169 | |
| 170 void VRController::OnDisplayChanged(device::blink::VRDisplayPtr display) { | |
| 171 VRDisplay* vrDisplay = getDisplayForIndex(display->index); | |
| 172 if (!vrDisplay) | |
| 173 return; | |
| 174 | |
| 175 vrDisplay->update(display); | |
| 176 } | |
| 177 | |
| 178 void VRController::OnExitPresent(unsigned index) { | |
| 179 VRDisplay* vrDisplay = getDisplayForIndex(index); | |
| 180 if (vrDisplay) | |
| 181 vrDisplay->forceExitPresent(); | |
| 182 } | |
| 183 | |
| 184 void VRController::OnDisplayConnected(device::blink::VRDisplayPtr display) { | |
| 185 VRDisplay* vrDisplay = createOrUpdateDisplay(display); | |
| 186 if (!vrDisplay) | |
| 187 return; | |
| 188 | |
| 189 m_navigatorVR->fireVREvent(VRDisplayEvent::create( | |
| 190 EventTypeNames::vrdisplayconnect, true, false, vrDisplay, "connect")); | |
| 191 } | |
| 192 | |
| 193 void VRController::OnDisplayDisconnected(unsigned index) { | |
| 194 VRDisplay* vrDisplay = getDisplayForIndex(index); | |
| 195 if (!vrDisplay) | |
| 196 return; | |
| 197 | |
| 198 vrDisplay->disconnected(); | |
| 199 | |
| 200 m_navigatorVR->fireVREvent( | |
| 201 VRDisplayEvent::create(EventTypeNames::vrdisplaydisconnect, true, false, | |
| 202 vrDisplay, "disconnect")); | |
| 203 } | |
| 204 | |
| 205 void VRController::contextDestroyed() { | 63 void VRController::contextDestroyed() { |
| 206 // If the document context was destroyed, shut down the client connection | 64 // If the document context was destroyed, shut down the client connection |
| 207 // and never call the mojo service again. | 65 // and never call the mojo service again. |
| 208 m_binding.Close(); | |
| 209 m_service.reset(); | 66 m_service.reset(); |
| 210 | 67 |
| 68 // Shutdown all displays' message pipe |
| 69 for (size_t i = 0; i < m_displays.size(); ++i) |
| 70 m_displays[i]->shutdownMessagePipe(); |
| 71 |
| 211 // The context is not automatically cleared, so do it manually. | 72 // The context is not automatically cleared, so do it manually. |
| 212 ContextLifecycleObserver::clearContext(); | 73 ContextLifecycleObserver::clearContext(); |
| 213 } | 74 } |
| 214 | 75 |
| 215 DEFINE_TRACE(VRController) { | 76 DEFINE_TRACE(VRController) { |
| 216 visitor->trace(m_navigatorVR); | 77 visitor->trace(m_navigatorVR); |
| 217 visitor->trace(m_displays); | 78 visitor->trace(m_displays); |
| 218 | 79 |
| 219 ContextLifecycleObserver::trace(visitor); | 80 ContextLifecycleObserver::trace(visitor); |
| 220 } | 81 } |
| 221 | 82 |
| 222 } // namespace blink | 83 } // namespace blink |
| OLD | NEW |