 Chromium Code Reviews
 Chromium Code Reviews Issue 2413903002:
  Allow VRDisplay to manage it's own rAF callbacks  (Closed)
    
  
    Issue 2413903002:
  Allow VRDisplay to manage it's own rAF callbacks  (Closed) 
  | 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" | 
| 24 | 26 | 
| 25 namespace blink { | 27 namespace blink { | 
| 26 | 28 | 
| 27 namespace { | 29 namespace { | 
| 28 | 30 | 
| 29 VREye stringToVREye(const String& whichEye) { | 31 VREye stringToVREye(const String& whichEye) { | 
| 30 if (whichEye == "left") | 32 if (whichEye == "left") | 
| 31 return VREyeLeft; | 33 return VREyeLeft; | 
| 32 if (whichEye == "right") | 34 if (whichEye == "right") | 
| 33 return VREyeRight; | 35 return VREyeRight; | 
| 34 return VREyeNone; | 36 return VREyeNone; | 
| 35 } | 37 } | 
| 36 | 38 | 
| 39 class VRDisplayFrameRequestCallback : public FrameRequestCallback { | |
| 40 public: | |
| 41 VRDisplayFrameRequestCallback(VRDisplay* vrDisplay) | |
| 42 : m_vrDisplay(vrDisplay) {} | |
| 43 ~VRDisplayFrameRequestCallback() override {} | |
| 44 void handleEvent(double highResTimeMs) override { | |
| 45 m_vrDisplay->serviceScriptedAnimations(highResTimeMs); | |
| 46 } | |
| 47 | |
| 48 DEFINE_INLINE_VIRTUAL_TRACE() { | |
| 49 visitor->trace(m_vrDisplay); | |
| 50 | |
| 51 FrameRequestCallback::trace(visitor); | |
| 52 } | |
| 53 | |
| 54 Member<VRDisplay> m_vrDisplay; | |
| 55 }; | |
| 56 | |
| 37 } // namespace | 57 } // namespace | 
| 38 | 58 | 
| 39 VRDisplay::VRDisplay(NavigatorVR* navigatorVR) | 59 VRDisplay::VRDisplay(NavigatorVR* navigatorVR) | 
| 40 : m_navigatorVR(navigatorVR), | 60 : m_navigatorVR(navigatorVR), | 
| 41 m_displayId(0), | 61 m_displayId(0), | 
| 42 m_isConnected(false), | 62 m_isConnected(false), | 
| 43 m_isPresenting(false), | 63 m_isPresenting(false), | 
| 44 m_canUpdateFramePose(true), | 64 m_canUpdateFramePose(true), | 
| 45 m_capabilities(new VRDisplayCapabilities()), | 65 m_capabilities(new VRDisplayCapabilities()), | 
| 46 m_eyeParametersLeft(new VREyeParameters()), | 66 m_eyeParametersLeft(new VREyeParameters()), | 
| 47 m_eyeParametersRight(new VREyeParameters()), | 67 m_eyeParametersRight(new VREyeParameters()), | 
| 48 m_depthNear(0.01), | 68 m_depthNear(0.01), | 
| 49 m_depthFar(10000.0), | 69 m_depthFar(10000.0), | 
| 50 m_fullscreenCheckTimer(this, &VRDisplay::onFullscreenCheck) {} | 70 m_fullscreenCheckTimer(this, &VRDisplay::onFullscreenCheck), | 
| 71 m_animationCallbackRequested(false), | |
| 72 m_inAnimationFrame(false) {} | |
| 51 | 73 | 
| 52 VRDisplay::~VRDisplay() {} | 74 VRDisplay::~VRDisplay() {} | 
| 53 | 75 | 
| 54 VRController* VRDisplay::controller() { | 76 VRController* VRDisplay::controller() { | 
| 55 return m_navigatorVR->controller(); | 77 return m_navigatorVR->controller(); | 
| 56 } | 78 } | 
| 57 | 79 | 
| 58 void VRDisplay::update(const device::blink::VRDisplayPtr& display) { | 80 void VRDisplay::update(const device::blink::VRDisplayPtr& display) { | 
| 59 m_displayId = display->index; | 81 m_displayId = display->index; | 
| 60 m_displayName = display->displayName; | 82 m_displayName = display->displayName; | 
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 case VREyeLeft: | 147 case VREyeLeft: | 
| 126 return m_eyeParametersLeft; | 148 return m_eyeParametersLeft; | 
| 127 case VREyeRight: | 149 case VREyeRight: | 
| 128 return m_eyeParametersRight; | 150 return m_eyeParametersRight; | 
| 129 default: | 151 default: | 
| 130 return nullptr; | 152 return nullptr; | 
| 131 } | 153 } | 
| 132 } | 154 } | 
| 133 | 155 | 
| 134 int VRDisplay::requestAnimationFrame(FrameRequestCallback* callback) { | 156 int VRDisplay::requestAnimationFrame(FrameRequestCallback* callback) { | 
| 135 // TODO: Use HMD-specific rAF when an external display is present. | 157 Document* doc = m_navigatorVR->document(); | 
| 158 if (!doc) | |
| 159 return 0; | |
| 160 | |
| 161 if (!m_animationCallbackRequested) { | |
| 162 doc->requestAnimationFrame(new VRDisplayFrameRequestCallback(this)); | |
| 163 m_animationCallbackRequested = true; | |
| 164 } | |
| 165 | |
| 136 callback->m_useLegacyTimeBase = false; | 166 callback->m_useLegacyTimeBase = false; | 
| 137 if (Document* doc = m_navigatorVR->document()) | 167 return ensureScriptedAnimationController(doc).registerCallback(callback); | 
| 
Sami
2016/10/24 10:12:23
Does it ever make sense to register more than one
 | |
| 138 return doc->requestAnimationFrame(callback); | |
| 139 return 0; | |
| 140 } | 168 } | 
| 141 | 169 | 
| 142 void VRDisplay::cancelAnimationFrame(int id) { | 170 void VRDisplay::cancelAnimationFrame(int id) { | 
| 143 if (Document* document = m_navigatorVR->document()) | 171 if (!m_scriptedAnimationController) | 
| 144 document->cancelAnimationFrame(id); | 172 return; | 
| 173 m_scriptedAnimationController->cancelCallback(id); | |
| 174 } | |
| 175 | |
| 176 void VRDisplay::serviceScriptedAnimations(double monotonicAnimationStartTime) { | |
| 177 if (!m_scriptedAnimationController) | |
| 178 return; | |
| 179 m_animationCallbackRequested = false; | |
| 180 m_inAnimationFrame = true; | |
| 
Sami
2016/10/24 10:12:23
nit: Could use AutoReset here.
 | |
| 181 m_scriptedAnimationController->serviceScriptedAnimations( | |
| 182 monotonicAnimationStartTime); | |
| 183 m_inAnimationFrame = false; | |
| 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 // We need to make sure that we don't start up the animation controller on a | |
| 
Sami
2016/10/24 10:12:23
Should this be a TODO since we're not resuming the
 | |
| 494 // background tab, for example. | |
| 495 | |
| 496 if (!doc->page()) | |
| 497 m_scriptedAnimationController->suspend(); | |
| 498 } | |
| 499 | |
| 500 return *m_scriptedAnimationController; | |
| 501 } | |
| 502 | |
| 430 DEFINE_TRACE(VRDisplay) { | 503 DEFINE_TRACE(VRDisplay) { | 
| 431 visitor->trace(m_navigatorVR); | 504 visitor->trace(m_navigatorVR); | 
| 432 visitor->trace(m_capabilities); | 505 visitor->trace(m_capabilities); | 
| 433 visitor->trace(m_stageParameters); | 506 visitor->trace(m_stageParameters); | 
| 434 visitor->trace(m_eyeParametersLeft); | 507 visitor->trace(m_eyeParametersLeft); | 
| 435 visitor->trace(m_eyeParametersRight); | 508 visitor->trace(m_eyeParametersRight); | 
| 436 visitor->trace(m_layer); | 509 visitor->trace(m_layer); | 
| 437 visitor->trace(m_renderingContext); | 510 visitor->trace(m_renderingContext); | 
| 511 visitor->trace(m_scriptedAnimationController); | |
| 438 } | 512 } | 
| 439 | 513 | 
| 440 } // namespace blink | 514 } // namespace blink | 
| OLD | NEW |