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/dom/DOMException.h" | 7 #include "core/dom/DOMException.h" |
| 8 #include "core/dom/FrameRequestCallback.h" |
8 #include "core/dom/Fullscreen.h" | 9 #include "core/dom/Fullscreen.h" |
| 10 #include "core/dom/ScriptedAnimationController.h" |
9 #include "core/frame/UseCounter.h" | 11 #include "core/frame/UseCounter.h" |
10 #include "core/inspector/ConsoleMessage.h" | 12 #include "core/inspector/ConsoleMessage.h" |
11 #include "gpu/command_buffer/client/gles2_interface.h" | 13 #include "gpu/command_buffer/client/gles2_interface.h" |
12 #include "modules/vr/NavigatorVR.h" | 14 #include "modules/vr/NavigatorVR.h" |
13 #include "modules/vr/VRController.h" | 15 #include "modules/vr/VRController.h" |
14 #include "modules/vr/VRDisplayCapabilities.h" | 16 #include "modules/vr/VRDisplayCapabilities.h" |
15 #include "modules/vr/VREyeParameters.h" | 17 #include "modules/vr/VREyeParameters.h" |
16 #include "modules/vr/VRFrameData.h" | 18 #include "modules/vr/VRFrameData.h" |
17 #include "modules/vr/VRLayer.h" | 19 #include "modules/vr/VRLayer.h" |
18 #include "modules/vr/VRPose.h" | 20 #include "modules/vr/VRPose.h" |
19 #include "modules/vr/VRStageParameters.h" | 21 #include "modules/vr/VRStageParameters.h" |
20 #include "modules/webgl/WebGLRenderingContextBase.h" | 22 #include "modules/webgl/WebGLRenderingContextBase.h" |
21 #include "platform/Histogram.h" | 23 #include "platform/Histogram.h" |
22 #include "platform/UserGestureIndicator.h" | 24 #include "platform/UserGestureIndicator.h" |
23 #include "public/platform/Platform.h" | 25 #include "public/platform/Platform.h" |
| 26 #include "wtf/AutoReset.h" |
24 | 27 |
25 namespace blink { | 28 namespace blink { |
26 | 29 |
27 namespace { | 30 namespace { |
28 | 31 |
29 VREye stringToVREye(const String& whichEye) { | 32 VREye stringToVREye(const String& whichEye) { |
30 if (whichEye == "left") | 33 if (whichEye == "left") |
31 return VREyeLeft; | 34 return VREyeLeft; |
32 if (whichEye == "right") | 35 if (whichEye == "right") |
33 return VREyeRight; | 36 return VREyeRight; |
34 return VREyeNone; | 37 return VREyeNone; |
35 } | 38 } |
36 | 39 |
| 40 class VRDisplayFrameRequestCallback : public FrameRequestCallback { |
| 41 public: |
| 42 VRDisplayFrameRequestCallback(VRDisplay* vrDisplay) |
| 43 : m_vrDisplay(vrDisplay) {} |
| 44 ~VRDisplayFrameRequestCallback() override {} |
| 45 void handleEvent(double highResTimeMs) override { |
| 46 m_vrDisplay->serviceScriptedAnimations(highResTimeMs); |
| 47 } |
| 48 |
| 49 DEFINE_INLINE_VIRTUAL_TRACE() { |
| 50 visitor->trace(m_vrDisplay); |
| 51 |
| 52 FrameRequestCallback::trace(visitor); |
| 53 } |
| 54 |
| 55 Member<VRDisplay> m_vrDisplay; |
| 56 }; |
| 57 |
37 } // namespace | 58 } // namespace |
38 | 59 |
39 VRDisplay::VRDisplay(NavigatorVR* navigatorVR) | 60 VRDisplay::VRDisplay(NavigatorVR* navigatorVR) |
40 : m_navigatorVR(navigatorVR), | 61 : m_navigatorVR(navigatorVR), |
41 m_displayId(0), | 62 m_displayId(0), |
42 m_isConnected(false), | 63 m_isConnected(false), |
43 m_isPresenting(false), | 64 m_isPresenting(false), |
44 m_canUpdateFramePose(true), | 65 m_canUpdateFramePose(true), |
45 m_capabilities(new VRDisplayCapabilities()), | 66 m_capabilities(new VRDisplayCapabilities()), |
46 m_eyeParametersLeft(new VREyeParameters()), | 67 m_eyeParametersLeft(new VREyeParameters()), |
47 m_eyeParametersRight(new VREyeParameters()), | 68 m_eyeParametersRight(new VREyeParameters()), |
48 m_depthNear(0.01), | 69 m_depthNear(0.01), |
49 m_depthFar(10000.0), | 70 m_depthFar(10000.0), |
50 m_fullscreenCheckTimer(this, &VRDisplay::onFullscreenCheck) {} | 71 m_fullscreenCheckTimer(this, &VRDisplay::onFullscreenCheck), |
| 72 m_animationCallbackRequested(false), |
| 73 m_inAnimationFrame(false) {} |
51 | 74 |
52 VRDisplay::~VRDisplay() {} | 75 VRDisplay::~VRDisplay() {} |
53 | 76 |
54 VRController* VRDisplay::controller() { | 77 VRController* VRDisplay::controller() { |
55 return m_navigatorVR->controller(); | 78 return m_navigatorVR->controller(); |
56 } | 79 } |
57 | 80 |
58 void VRDisplay::update(const device::blink::VRDisplayPtr& display) { | 81 void VRDisplay::update(const device::blink::VRDisplayPtr& display) { |
59 m_displayId = display->index; | 82 m_displayId = display->index; |
60 m_displayName = display->displayName; | 83 m_displayName = display->displayName; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
125 case VREyeLeft: | 148 case VREyeLeft: |
126 return m_eyeParametersLeft; | 149 return m_eyeParametersLeft; |
127 case VREyeRight: | 150 case VREyeRight: |
128 return m_eyeParametersRight; | 151 return m_eyeParametersRight; |
129 default: | 152 default: |
130 return nullptr; | 153 return nullptr; |
131 } | 154 } |
132 } | 155 } |
133 | 156 |
134 int VRDisplay::requestAnimationFrame(FrameRequestCallback* callback) { | 157 int VRDisplay::requestAnimationFrame(FrameRequestCallback* callback) { |
135 // TODO: Use HMD-specific rAF when an external display is present. | 158 Document* doc = m_navigatorVR->document(); |
| 159 if (!doc) |
| 160 return 0; |
| 161 |
| 162 if (!m_animationCallbackRequested) { |
| 163 doc->requestAnimationFrame(new VRDisplayFrameRequestCallback(this)); |
| 164 m_animationCallbackRequested = true; |
| 165 } |
| 166 |
136 callback->m_useLegacyTimeBase = false; | 167 callback->m_useLegacyTimeBase = false; |
137 if (Document* doc = m_navigatorVR->document()) | 168 return ensureScriptedAnimationController(doc).registerCallback(callback); |
138 return doc->requestAnimationFrame(callback); | |
139 return 0; | |
140 } | 169 } |
141 | 170 |
142 void VRDisplay::cancelAnimationFrame(int id) { | 171 void VRDisplay::cancelAnimationFrame(int id) { |
143 if (Document* document = m_navigatorVR->document()) | 172 if (!m_scriptedAnimationController) |
144 document->cancelAnimationFrame(id); | 173 return; |
| 174 m_scriptedAnimationController->cancelCallback(id); |
| 175 } |
| 176 |
| 177 void VRDisplay::serviceScriptedAnimations(double monotonicAnimationStartTime) { |
| 178 if (!m_scriptedAnimationController) |
| 179 return; |
| 180 AutoReset<bool> animating(&m_inAnimationFrame, true); |
| 181 m_animationCallbackRequested = false; |
| 182 m_scriptedAnimationController->serviceScriptedAnimations( |
| 183 monotonicAnimationStartTime); |
145 } | 184 } |
146 | 185 |
147 void ReportPresentationResult(PresentationResult result) { | 186 void ReportPresentationResult(PresentationResult result) { |
148 // Note that this is called twice for each call to requestPresent - | 187 // Note that this is called twice for each call to requestPresent - |
149 // one to declare that requestPresent was called, and one for the | 188 // one to declare that requestPresent was called, and one for the |
150 // result. | 189 // result. |
151 DEFINE_STATIC_LOCAL( | 190 DEFINE_STATIC_LOCAL( |
152 EnumerationHistogram, vrPresentationResultHistogram, | 191 EnumerationHistogram, vrPresentationResultHistogram, |
153 ("VRDisplayPresentResult", | 192 ("VRDisplayPresentResult", |
154 static_cast<int>(PresentationResult::PresentationResultMax))); | 193 static_cast<int>(PresentationResult::PresentationResultMax))); |
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 HeapVector<VRLayer> layers; | 408 HeapVector<VRLayer> layers; |
370 | 409 |
371 if (m_isPresenting) { | 410 if (m_isPresenting) { |
372 layers.append(m_layer); | 411 layers.append(m_layer); |
373 } | 412 } |
374 | 413 |
375 return layers; | 414 return layers; |
376 } | 415 } |
377 | 416 |
378 void VRDisplay::submitFrame() { | 417 void VRDisplay::submitFrame() { |
379 if (!m_isPresenting || !m_contextGL) { | 418 Document* doc = m_navigatorVR->document(); |
380 // Something got confused, we can't submit frames if we're not presenting. | 419 if (!m_isPresenting) { |
| 420 if (doc) { |
| 421 doc->addConsoleMessage(ConsoleMessage::create( |
| 422 RenderingMessageSource, WarningMessageLevel, |
| 423 "submitFrame has no effect when the VRDisplay is not presenting.")); |
| 424 } |
381 return; | 425 return; |
382 } | 426 } |
383 | 427 |
| 428 if (!m_inAnimationFrame) { |
| 429 if (doc) { |
| 430 doc->addConsoleMessage( |
| 431 ConsoleMessage::create(RenderingMessageSource, WarningMessageLevel, |
| 432 "submitFrame must be called within a " |
| 433 "VRDisplay.requestAnimationFrame callback.")); |
| 434 } |
| 435 return; |
| 436 } |
| 437 |
| 438 if (!m_contextGL) { |
| 439 // Something got confused, we can't submit frames without a GL context. |
| 440 return; |
| 441 } |
| 442 |
384 // Write the frame number for the pose used into a bottom left pixel block. | 443 // Write the frame number for the pose used into a bottom left pixel block. |
385 // It is read by chrome/browser/android/vr_shell/vr_shell.cc to associate | 444 // It is read by chrome/browser/android/vr_shell/vr_shell.cc to associate |
386 // the correct corresponding pose for submission. | 445 // the correct corresponding pose for submission. |
387 auto gl = m_contextGL; | 446 auto gl = m_contextGL; |
388 | 447 |
389 // We must ensure that the WebGL app's GL state is preserved. We do this by | 448 // We must ensure that the WebGL app's GL state is preserved. We do this by |
390 // calling low-level GL commands directly so that the rendering context's | 449 // calling low-level GL commands directly so that the rendering context's |
391 // saved parameters don't get overwritten. | 450 // saved parameters don't get overwritten. |
392 | 451 |
393 gl->Enable(GL_SCISSOR_TEST); | 452 gl->Enable(GL_SCISSOR_TEST); |
(...skipping 26 matching lines...) Expand all Loading... |
420 // become unnessecary. Until that point, though, this seems preferable to | 479 // become unnessecary. Until that point, though, this seems preferable to |
421 // adding a bunch of notification plumbing to Fullscreen. | 480 // adding a bunch of notification plumbing to Fullscreen. |
422 if (!Fullscreen::isCurrentFullScreenElement(*m_layer.source())) { | 481 if (!Fullscreen::isCurrentFullScreenElement(*m_layer.source())) { |
423 m_isPresenting = false; | 482 m_isPresenting = false; |
424 m_navigatorVR->fireVRDisplayPresentChange(this); | 483 m_navigatorVR->fireVRDisplayPresentChange(this); |
425 m_fullscreenCheckTimer.stop(); | 484 m_fullscreenCheckTimer.stop(); |
426 controller()->exitPresent(m_displayId); | 485 controller()->exitPresent(m_displayId); |
427 } | 486 } |
428 } | 487 } |
429 | 488 |
| 489 ScriptedAnimationController& VRDisplay::ensureScriptedAnimationController( |
| 490 Document* doc) { |
| 491 if (!m_scriptedAnimationController) |
| 492 m_scriptedAnimationController = ScriptedAnimationController::create(doc); |
| 493 |
| 494 return *m_scriptedAnimationController; |
| 495 } |
| 496 |
430 DEFINE_TRACE(VRDisplay) { | 497 DEFINE_TRACE(VRDisplay) { |
431 visitor->trace(m_navigatorVR); | 498 visitor->trace(m_navigatorVR); |
432 visitor->trace(m_capabilities); | 499 visitor->trace(m_capabilities); |
433 visitor->trace(m_stageParameters); | 500 visitor->trace(m_stageParameters); |
434 visitor->trace(m_eyeParametersLeft); | 501 visitor->trace(m_eyeParametersLeft); |
435 visitor->trace(m_eyeParametersRight); | 502 visitor->trace(m_eyeParametersRight); |
436 visitor->trace(m_layer); | 503 visitor->trace(m_layer); |
437 visitor->trace(m_renderingContext); | 504 visitor->trace(m_renderingContext); |
| 505 visitor->trace(m_scriptedAnimationController); |
438 } | 506 } |
439 | 507 |
440 } // namespace blink | 508 } // namespace blink |
OLD | NEW |