Chromium Code Reviews| 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 ReportPresentationResult(PresentationResult::ServiceInactive); | |
| 67 return; | |
| 68 } | |
| 69 | |
| 70 m_service->RequestPresent( | |
| 71 index, secureOrigin, | |
| 72 convertToBaseCallback(WTF::bind(&VRController::onPresentComplete, | |
| 73 wrapPersistent(this), | |
| 74 wrapPersistent(resolver), index))); | |
| 75 } | |
| 76 | |
| 77 void VRController::exitPresent(unsigned index) { | |
| 78 if (!m_service) | |
| 79 return; | |
| 80 | |
| 81 m_service->ExitPresent(index); | |
| 82 } | |
| 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 | 54 |
| 142 std::unique_ptr<VRGetDevicesCallback> callback = | 55 std::unique_ptr<VRGetDevicesCallback> callback = |
| 143 m_pendingGetDevicesCallbacks.takeFirst(); | 56 m_pendingGetDevicesCallbacks.takeFirst(); |
| 144 if (!callback) | 57 if (!callback) |
| 145 return; | 58 return; |
| 146 | 59 |
| 147 callback->onSuccess(outDisplays); | 60 callback->onSuccess(outDisplays); |
|
bajones
2016/10/25 22:21:38
Probably shouldn't call onSuccess if success == fa
| |
| 148 } | 61 } |
| 149 | 62 |
| 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 } | |
| 171 } | |
| 172 | |
| 173 void VRController::OnDisplayChanged(device::blink::VRDisplayPtr display) { | |
| 174 VRDisplay* vrDisplay = getDisplayForIndex(display->index); | |
| 175 if (!vrDisplay) | |
| 176 return; | |
| 177 | |
| 178 vrDisplay->update(display); | |
| 179 } | |
| 180 | |
| 181 void VRController::OnExitPresent(unsigned index) { | |
| 182 VRDisplay* vrDisplay = getDisplayForIndex(index); | |
| 183 if (vrDisplay) | |
| 184 vrDisplay->forceExitPresent(); | |
| 185 } | |
| 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")); | |
| 206 } | |
| 207 | |
| 208 void VRController::contextDestroyed() { | 63 void VRController::contextDestroyed() { |
| 209 // If the document context was destroyed, shut down the client connection | 64 // If the document context was destroyed, shut down the client connection |
| 210 // and never call the mojo service again. | 65 // and never call the mojo service again. |
| 211 m_binding.Close(); | |
| 212 m_service.reset(); | 66 m_service.reset(); |
| 213 | 67 |
| 68 // Shutdown all displays' message pipe | |
| 69 for (size_t i = 0; i < m_displays.size(); ++i) | |
| 70 m_displays[i]->shutdownMessagePipe(); | |
| 71 | |
| 214 // The context is not automatically cleared, so do it manually. | 72 // The context is not automatically cleared, so do it manually. |
| 215 ContextLifecycleObserver::clearContext(); | 73 ContextLifecycleObserver::clearContext(); |
| 216 } | 74 } |
| 217 | 75 |
| 218 DEFINE_TRACE(VRController) { | 76 DEFINE_TRACE(VRController) { |
| 219 visitor->trace(m_navigatorVR); | 77 visitor->trace(m_navigatorVR); |
| 220 visitor->trace(m_displays); | 78 visitor->trace(m_displays); |
| 221 | 79 |
| 222 ContextLifecycleObserver::trace(visitor); | 80 ContextLifecycleObserver::trace(visitor); |
| 223 } | 81 } |
| 224 | 82 |
| 225 } // namespace blink | 83 } // namespace blink |
| OLD | NEW |