OLD | NEW |
---|---|
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/VRDisplay.h" | 5 #include "modules/vr/VRDisplay.h" |
6 | 6 |
7 #include "core/css/StylePropertySet.h" | 7 #include "core/css/StylePropertySet.h" |
8 #include "core/dom/DOMException.h" | 8 #include "core/dom/DOMException.h" |
9 #include "core/dom/DocumentUserGestureToken.h" | 9 #include "core/dom/DocumentUserGestureToken.h" |
10 #include "core/dom/FrameRequestCallback.h" | 10 #include "core/dom/FrameRequestCallback.h" |
(...skipping 29 matching lines...) Expand all Loading... | |
40 static constexpr std::array<uint8_t, 2> kWebVrPosePixelMagicNumbers{{42, 142}}; | 40 static constexpr std::array<uint8_t, 2> kWebVrPosePixelMagicNumbers{{42, 142}}; |
41 | 41 |
42 VREye stringToVREye(const String& whichEye) { | 42 VREye stringToVREye(const String& whichEye) { |
43 if (whichEye == "left") | 43 if (whichEye == "left") |
44 return VREyeLeft; | 44 return VREyeLeft; |
45 if (whichEye == "right") | 45 if (whichEye == "right") |
46 return VREyeRight; | 46 return VREyeRight; |
47 return VREyeNone; | 47 return VREyeNone; |
48 } | 48 } |
49 | 49 |
50 class VRDisplayFrameRequestCallback : public FrameRequestCallback { | |
51 public: | |
52 VRDisplayFrameRequestCallback(VRDisplay* vrDisplay) : m_vrDisplay(vrDisplay) { | |
53 m_useLegacyTimeBase = true; | |
54 } | |
55 ~VRDisplayFrameRequestCallback() override {} | |
56 void handleEvent(double highResTimeMs) override { | |
57 Document* doc = m_vrDisplay->document(); | |
58 if (!doc) | |
59 return; | |
60 | |
61 // Need to divide by 1000 here because serviceScriptedAnimations expects | |
62 // time to be given in seconds. | |
63 m_vrDisplay->serviceScriptedAnimations( | |
64 doc->loader()->timing().pseudoWallTimeToMonotonicTime(highResTimeMs / | |
65 1000.0)); | |
66 } | |
67 | |
68 DEFINE_INLINE_VIRTUAL_TRACE() { | |
69 visitor->trace(m_vrDisplay); | |
70 | |
71 FrameRequestCallback::trace(visitor); | |
72 } | |
73 | |
74 Member<VRDisplay> m_vrDisplay; | |
75 }; | |
76 | |
77 } // namespace | 50 } // namespace |
78 | 51 |
79 VRDisplay::VRDisplay(NavigatorVR* navigatorVR, | 52 VRDisplay::VRDisplay(NavigatorVR* navigatorVR, |
80 device::mojom::blink::VRDisplayPtr display, | 53 device::mojom::blink::VRDisplayPtr display, |
81 device::mojom::blink::VRDisplayClientRequest request) | 54 device::mojom::blink::VRDisplayClientRequest request) |
82 : ContextLifecycleObserver(navigatorVR->document()), | 55 : ContextLifecycleObserver(navigatorVR->document()), |
83 m_navigatorVR(navigatorVR), | 56 m_navigatorVR(navigatorVR), |
84 m_isConnected(false), | |
85 m_isPresenting(false), | |
86 m_isValidDeviceForPresenting(true), | |
87 m_canUpdateFramePose(true), | |
88 m_capabilities(new VRDisplayCapabilities()), | 57 m_capabilities(new VRDisplayCapabilities()), |
89 m_eyeParametersLeft(new VREyeParameters()), | 58 m_eyeParametersLeft(new VREyeParameters()), |
90 m_eyeParametersRight(new VREyeParameters()), | 59 m_eyeParametersRight(new VREyeParameters()), |
91 m_depthNear(0.01), | |
92 m_depthFar(10000.0), | |
93 m_fullscreenCheckTimer(this, &VRDisplay::onFullscreenCheck), | 60 m_fullscreenCheckTimer(this, &VRDisplay::onFullscreenCheck), |
94 m_contextGL(nullptr), | |
95 m_animationCallbackRequested(false), | |
96 m_inAnimationFrame(false), | |
97 m_display(std::move(display)), | 61 m_display(std::move(display)), |
98 m_binding(this, std::move(request)) {} | 62 m_displayClientBinding(this, std::move(request)), |
63 m_vsyncProviderClientBinding(this) {} | |
99 | 64 |
100 VRDisplay::~VRDisplay() {} | 65 VRDisplay::~VRDisplay() {} |
101 | 66 |
102 VRController* VRDisplay::controller() { | 67 VRController* VRDisplay::controller() { |
103 return m_navigatorVR->controller(); | 68 return m_navigatorVR->controller(); |
104 } | 69 } |
105 | 70 |
106 void VRDisplay::update(const device::mojom::blink::VRDisplayInfoPtr& display) { | 71 void VRDisplay::update(const device::mojom::blink::VRDisplayInfoPtr& display) { |
107 m_displayId = display->index; | 72 m_displayId = display->index; |
108 m_displayName = display->displayName; | 73 m_displayName = display->displayName; |
(...skipping 28 matching lines...) Expand all Loading... | |
137 OnPresentChange(); | 102 OnPresentChange(); |
138 } | 103 } |
139 } | 104 } |
140 | 105 |
141 void VRDisplay::disconnected() { | 106 void VRDisplay::disconnected() { |
142 if (m_isConnected) | 107 if (m_isConnected) |
143 m_isConnected = !m_isConnected; | 108 m_isConnected = !m_isConnected; |
144 } | 109 } |
145 | 110 |
146 bool VRDisplay::getFrameData(VRFrameData* frameData) { | 111 bool VRDisplay::getFrameData(VRFrameData* frameData) { |
147 updatePose(); | 112 if (!m_framePose || m_displayBlurred) |
148 | |
149 if (!m_framePose) | |
150 return false; | 113 return false; |
151 | 114 |
152 if (!frameData) | 115 if (!frameData) |
153 return false; | 116 return false; |
154 | 117 |
155 if (m_depthNear == m_depthFar) | 118 if (m_depthNear == m_depthFar) |
156 return false; | 119 return false; |
157 | 120 |
158 return frameData->update(m_framePose, m_eyeParametersLeft, | 121 return frameData->update(m_framePose, m_eyeParametersLeft, |
159 m_eyeParametersRight, m_depthNear, m_depthFar); | 122 m_eyeParametersRight, m_depthNear, m_depthFar); |
160 } | 123 } |
161 | 124 |
162 VRPose* VRDisplay::getPose() { | 125 VRPose* VRDisplay::getPose() { |
163 updatePose(); | 126 if (!m_framePose || m_displayBlurred) |
164 | |
165 if (!m_framePose) | |
166 return nullptr; | 127 return nullptr; |
167 | 128 |
168 VRPose* pose = VRPose::create(); | 129 VRPose* pose = VRPose::create(); |
169 pose->setPose(m_framePose); | 130 pose->setPose(m_framePose); |
170 return pose; | 131 return pose; |
171 } | 132 } |
172 | 133 |
173 void VRDisplay::updatePose() { | |
174 if (m_displayBlurred) { | |
175 // WebVR spec says to return a null pose when the display is blurred. | |
176 m_framePose = nullptr; | |
177 return; | |
178 } | |
179 if (m_canUpdateFramePose) { | |
180 if (!m_display) | |
181 return; | |
182 device::mojom::blink::VRPosePtr pose; | |
183 m_display->GetPose(&pose); | |
184 m_framePose = std::move(pose); | |
185 if (m_isPresenting) | |
186 m_canUpdateFramePose = false; | |
187 } | |
188 } | |
189 | |
190 void VRDisplay::resetPose() { | 134 void VRDisplay::resetPose() { |
191 if (!m_display) | 135 if (!m_display) |
192 return; | 136 return; |
193 | 137 |
194 m_display->ResetPose(); | 138 m_display->ResetPose(); |
195 } | 139 } |
196 | 140 |
197 VREyeParameters* VRDisplay::getEyeParameters(const String& whichEye) { | 141 VREyeParameters* VRDisplay::getEyeParameters(const String& whichEye) { |
198 switch (stringToVREye(whichEye)) { | 142 switch (stringToVREye(whichEye)) { |
199 case VREyeLeft: | 143 case VREyeLeft: |
200 return m_eyeParametersLeft; | 144 return m_eyeParametersLeft; |
201 case VREyeRight: | 145 case VREyeRight: |
202 return m_eyeParametersRight; | 146 return m_eyeParametersRight; |
203 default: | 147 default: |
204 return nullptr; | 148 return nullptr; |
205 } | 149 } |
206 } | 150 } |
207 | 151 |
208 int VRDisplay::requestAnimationFrame(FrameRequestCallback* callback) { | 152 int VRDisplay::requestAnimationFrame(FrameRequestCallback* callback) { |
209 Document* doc = this->document(); | 153 Document* doc = this->document(); |
210 if (!doc) | 154 if (!doc) |
211 return 0; | 155 return 0; |
212 | |
213 if (!m_animationCallbackRequested) { | |
214 doc->requestAnimationFrame(new VRDisplayFrameRequestCallback(this)); | |
215 m_animationCallbackRequested = true; | |
216 } | |
217 | |
218 callback->m_useLegacyTimeBase = false; | 156 callback->m_useLegacyTimeBase = false; |
219 return ensureScriptedAnimationController(doc).registerCallback(callback); | 157 return ensureScriptedAnimationController(doc).registerCallback(callback); |
220 } | 158 } |
221 | 159 |
222 void VRDisplay::cancelAnimationFrame(int id) { | 160 void VRDisplay::cancelAnimationFrame(int id) { |
223 if (!m_scriptedAnimationController) | 161 if (!m_scriptedAnimationController) |
224 return; | 162 return; |
225 m_scriptedAnimationController->cancelCallback(id); | 163 m_scriptedAnimationController->cancelCallback(id); |
226 } | 164 } |
227 | 165 |
228 void VRDisplay::OnBlur() { | 166 void VRDisplay::OnBlur() { |
229 m_displayBlurred = true; | 167 m_displayBlurred = true; |
230 | |
231 m_navigatorVR->enqueueVREvent(VRDisplayEvent::create( | 168 m_navigatorVR->enqueueVREvent(VRDisplayEvent::create( |
232 EventTypeNames::vrdisplayblur, true, false, this, "")); | 169 EventTypeNames::vrdisplayblur, true, false, this, "")); |
233 } | 170 } |
234 | 171 |
235 void VRDisplay::OnFocus() { | 172 void VRDisplay::OnFocus() { |
236 m_displayBlurred = false; | 173 m_displayBlurred = false; |
237 // Restart our internal doc requestAnimationFrame callback, if it fired while | |
238 // the display was blurred. | |
239 // TODO(bajones): Don't use doc->requestAnimationFrame() at all. Animation | |
240 // frames should be tied to the presenting VR display (e.g. should be serviced | |
241 // by GVR library callbacks on Android), and not the doc frame rate. | |
242 if (!m_animationCallbackRequested) { | |
243 Document* doc = this->document(); | |
244 if (!doc) | |
245 return; | |
246 doc->requestAnimationFrame(new VRDisplayFrameRequestCallback(this)); | |
247 } | |
248 m_navigatorVR->enqueueVREvent(VRDisplayEvent::create( | 174 m_navigatorVR->enqueueVREvent(VRDisplayEvent::create( |
249 EventTypeNames::vrdisplayfocus, true, false, this, "")); | 175 EventTypeNames::vrdisplayfocus, true, false, this, "")); |
250 } | 176 } |
251 | 177 |
252 void VRDisplay::serviceScriptedAnimations(double monotonicAnimationStartTime) { | |
253 if (!m_scriptedAnimationController) | |
254 return; | |
255 AutoReset<bool> animating(&m_inAnimationFrame, true); | |
256 m_animationCallbackRequested = false; | |
257 | |
258 // We use an internal rAF callback to run the animation loop at the display | |
259 // speed, and run the user's callback after our internal callback fires. | |
260 // However, when the display is blurred, we want to pause the animation loop, | |
261 // so we don't fire the user's callback until the display is focused. | |
262 if (m_displayBlurred) | |
263 return; | |
264 m_scriptedAnimationController->serviceScriptedAnimations( | |
265 monotonicAnimationStartTime); | |
266 } | |
267 | |
268 void ReportPresentationResult(PresentationResult result) { | 178 void ReportPresentationResult(PresentationResult result) { |
269 // Note that this is called twice for each call to requestPresent - | 179 // Note that this is called twice for each call to requestPresent - |
270 // one to declare that requestPresent was called, and one for the | 180 // one to declare that requestPresent was called, and one for the |
271 // result. | 181 // result. |
272 DEFINE_STATIC_LOCAL( | 182 DEFINE_STATIC_LOCAL( |
273 EnumerationHistogram, vrPresentationResultHistogram, | 183 EnumerationHistogram, vrPresentationResultHistogram, |
274 ("VRDisplayPresentResult", | 184 ("VRDisplayPresentResult", |
275 static_cast<int>(PresentationResult::PresentationResultMax))); | 185 static_cast<int>(PresentationResult::PresentationResultMax))); |
276 vrPresentationResultHistogram.count(static_cast<int>(result)); | 186 vrPresentationResultHistogram.count(static_cast<int>(result)); |
277 } | 187 } |
(...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
685 m_navigatorVR->dispatchVRGestureEvent(VRDisplayEvent::create( | 595 m_navigatorVR->dispatchVRGestureEvent(VRDisplayEvent::create( |
686 EventTypeNames::vrdisplayactivate, true, false, this, reason)); | 596 EventTypeNames::vrdisplayactivate, true, false, this, reason)); |
687 } | 597 } |
688 | 598 |
689 void VRDisplay::OnDeactivate( | 599 void VRDisplay::OnDeactivate( |
690 device::mojom::blink::VRDisplayEventReason reason) { | 600 device::mojom::blink::VRDisplayEventReason reason) { |
691 m_navigatorVR->enqueueVREvent(VRDisplayEvent::create( | 601 m_navigatorVR->enqueueVREvent(VRDisplayEvent::create( |
692 EventTypeNames::vrdisplaydeactivate, true, false, this, reason)); | 602 EventTypeNames::vrdisplaydeactivate, true, false, this, reason)); |
693 } | 603 } |
694 | 604 |
605 void VRDisplay::OnVRVsyncProviderReady( | |
606 const OnVRVsyncProviderReadyCallback& callback) { | |
607 if (m_vsyncProviderClientBinding.is_bound()) | |
608 m_vsyncProviderClientBinding.Unbind(); | |
609 callback.Run(m_vsyncProviderClientBinding.CreateInterfacePtrAndBind()); | |
610 } | |
611 | |
612 void VRDisplay::OnVSync(device::mojom::blink::VRPosePtr pose, | |
613 double timeSeconds, | |
614 const OnVSyncCallback& callback) { | |
615 callback.Run(); | |
616 if (m_displayBlurred) | |
617 return; | |
618 if (!m_scriptedAnimationController) | |
619 return; | |
620 Document* doc = this->document(); | |
621 if (!doc) | |
622 return; | |
623 | |
624 AutoReset<bool> animating(&m_inAnimationFrame, true); | |
625 if (m_canUpdateFramePose) | |
bajones
2017/01/10 00:22:17
I think we can safely drop the logic behind m_canU
klausw
2017/01/10 00:35:23
Strictly speaking that may be a spec violation. As
| |
626 m_framePose = std::move(pose); | |
627 m_canUpdateFramePose = false; | |
628 m_scriptedAnimationController->serviceScriptedAnimations( | |
629 doc->loader()->timing().referenceMonotonicTime() + timeSeconds); | |
bajones
2017/01/10 00:22:17
How confident are we that this will align with the
mthiesse
2017/01/10 00:25:24
I haven't checked, but I can pretty much guarantee
klausw
2017/01/10 00:35:23
This becomes important when switching between wind
| |
630 } | |
631 | |
695 void VRDisplay::onFullscreenCheck(TimerBase*) { | 632 void VRDisplay::onFullscreenCheck(TimerBase*) { |
696 if (!m_isPresenting) { | 633 if (!m_isPresenting) { |
697 m_fullscreenCheckTimer.stop(); | 634 m_fullscreenCheckTimer.stop(); |
698 return; | 635 return; |
699 } | 636 } |
700 // TODO: This is a temporary measure to track if fullscreen mode has been | 637 // TODO: This is a temporary measure to track if fullscreen mode has been |
701 // exited by the UA. If so we need to end VR presentation. Soon we won't | 638 // exited by the UA. If so we need to end VR presentation. Soon we won't |
702 // depend on the Fullscreen API to fake VR presentation, so this will | 639 // depend on the Fullscreen API to fake VR presentation, so this will |
703 // become unnessecary. Until that point, though, this seems preferable to | 640 // become unnessecary. Until that point, though, this seems preferable to |
704 // adding a bunch of notification plumbing to Fullscreen. | 641 // adding a bunch of notification plumbing to Fullscreen. |
(...skipping 25 matching lines...) Expand all Loading... | |
730 | 667 |
731 ScriptedAnimationController& VRDisplay::ensureScriptedAnimationController( | 668 ScriptedAnimationController& VRDisplay::ensureScriptedAnimationController( |
732 Document* doc) { | 669 Document* doc) { |
733 if (!m_scriptedAnimationController) | 670 if (!m_scriptedAnimationController) |
734 m_scriptedAnimationController = ScriptedAnimationController::create(doc); | 671 m_scriptedAnimationController = ScriptedAnimationController::create(doc); |
735 | 672 |
736 return *m_scriptedAnimationController; | 673 return *m_scriptedAnimationController; |
737 } | 674 } |
738 | 675 |
739 void VRDisplay::dispose() { | 676 void VRDisplay::dispose() { |
740 m_binding.Close(); | 677 m_displayClientBinding.Close(); |
678 m_vsyncProviderClientBinding.Close(); | |
741 } | 679 } |
742 | 680 |
743 ExecutionContext* VRDisplay::getExecutionContext() const { | 681 ExecutionContext* VRDisplay::getExecutionContext() const { |
744 return ContextLifecycleObserver::getExecutionContext(); | 682 return ContextLifecycleObserver::getExecutionContext(); |
745 } | 683 } |
746 | 684 |
747 const AtomicString& VRDisplay::interfaceName() const { | 685 const AtomicString& VRDisplay::interfaceName() const { |
748 return EventTargetNames::VRDisplay; | 686 return EventTargetNames::VRDisplay; |
749 } | 687 } |
750 | 688 |
(...skipping 15 matching lines...) Expand all Loading... | |
766 visitor->trace(m_stageParameters); | 704 visitor->trace(m_stageParameters); |
767 visitor->trace(m_eyeParametersLeft); | 705 visitor->trace(m_eyeParametersLeft); |
768 visitor->trace(m_eyeParametersRight); | 706 visitor->trace(m_eyeParametersRight); |
769 visitor->trace(m_layer); | 707 visitor->trace(m_layer); |
770 visitor->trace(m_renderingContext); | 708 visitor->trace(m_renderingContext); |
771 visitor->trace(m_scriptedAnimationController); | 709 visitor->trace(m_scriptedAnimationController); |
772 visitor->trace(m_pendingPresentResolvers); | 710 visitor->trace(m_pendingPresentResolvers); |
773 } | 711 } |
774 | 712 |
775 } // namespace blink | 713 } // namespace blink |
OLD | NEW |