Chromium Code Reviews| 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" |
| 11 #include "core/dom/Fullscreen.h" | |
| 12 #include "core/dom/ScriptedAnimationController.h" | 11 #include "core/dom/ScriptedAnimationController.h" |
| 13 #include "core/dom/TaskRunnerHelper.h" | 12 #include "core/dom/TaskRunnerHelper.h" |
| 13 #include "core/frame/FrameView.h" | |
| 14 #include "core/frame/ImageBitmap.h" | |
| 14 #include "core/frame/UseCounter.h" | 15 #include "core/frame/UseCounter.h" |
| 15 #include "core/inspector/ConsoleMessage.h" | 16 #include "core/inspector/ConsoleMessage.h" |
| 17 #include "core/layout/LayoutView.h" | |
| 18 #include "core/layout/compositing/PaintLayerCompositor.h" | |
| 16 #include "core/loader/DocumentLoader.h" | 19 #include "core/loader/DocumentLoader.h" |
| 17 #include "gpu/command_buffer/client/gles2_interface.h" | 20 #include "gpu/command_buffer/client/gles2_interface.h" |
| 18 #include "modules/EventTargetModules.h" | 21 #include "modules/EventTargetModules.h" |
| 19 #include "modules/vr/NavigatorVR.h" | 22 #include "modules/vr/NavigatorVR.h" |
| 20 #include "modules/vr/VRController.h" | 23 #include "modules/vr/VRController.h" |
| 21 #include "modules/vr/VRDisplayCapabilities.h" | 24 #include "modules/vr/VRDisplayCapabilities.h" |
| 22 #include "modules/vr/VREyeParameters.h" | 25 #include "modules/vr/VREyeParameters.h" |
| 23 #include "modules/vr/VRFrameData.h" | 26 #include "modules/vr/VRFrameData.h" |
| 24 #include "modules/vr/VRLayer.h" | 27 #include "modules/vr/VRLayer.h" |
| 25 #include "modules/vr/VRPose.h" | 28 #include "modules/vr/VRPose.h" |
| 26 #include "modules/vr/VRStageParameters.h" | 29 #include "modules/vr/VRStageParameters.h" |
| 27 #include "modules/webgl/WebGLRenderingContextBase.h" | 30 #include "modules/webgl/WebGLRenderingContextBase.h" |
| 28 #include "platform/Histogram.h" | 31 #include "platform/Histogram.h" |
| 29 #include "platform/UserGestureIndicator.h" | 32 #include "platform/UserGestureIndicator.h" |
| 33 #include "platform/instrumentation/tracing/TraceEvent.h" | |
| 30 #include "public/platform/Platform.h" | 34 #include "public/platform/Platform.h" |
| 31 #include "wtf/AutoReset.h" | 35 #include "wtf/AutoReset.h" |
| 32 #include "wtf/Time.h" | 36 #include "wtf/Time.h" |
| 33 | 37 |
| 34 #include <array> | 38 #include <array> |
| 35 | 39 |
| 36 namespace blink { | 40 namespace blink { |
| 37 | 41 |
| 38 namespace { | 42 namespace { |
| 39 | 43 |
| 40 // Magic numbers used to mark valid pose index values encoded in frame | |
| 41 // data. Must match the magic numbers used in vr_shell.cc. | |
| 42 static constexpr std::array<uint8_t, 2> kWebVrPosePixelMagicNumbers{{42, 142}}; | |
| 43 | |
| 44 VREye stringToVREye(const String& whichEye) { | 44 VREye stringToVREye(const String& whichEye) { |
| 45 if (whichEye == "left") | 45 if (whichEye == "left") |
| 46 return VREyeLeft; | 46 return VREyeLeft; |
| 47 if (whichEye == "right") | 47 if (whichEye == "right") |
| 48 return VREyeRight; | 48 return VREyeRight; |
| 49 return VREyeNone; | 49 return VREyeNone; |
| 50 } | 50 } |
| 51 | 51 |
| 52 } // namespace | 52 } // namespace |
| 53 | 53 |
| 54 VRDisplay::VRDisplay(NavigatorVR* navigatorVR, | 54 VRDisplay::VRDisplay(NavigatorVR* navigatorVR, |
| 55 device::mojom::blink::VRDisplayPtr display, | 55 device::mojom::blink::VRDisplayPtr display, |
| 56 device::mojom::blink::VRDisplayClientRequest request) | 56 device::mojom::blink::VRDisplayClientRequest request) |
| 57 : ContextLifecycleObserver(navigatorVR->document()), | 57 : ContextLifecycleObserver(navigatorVR->document()), |
| 58 m_navigatorVR(navigatorVR), | 58 m_navigatorVR(navigatorVR), |
| 59 m_capabilities(new VRDisplayCapabilities()), | 59 m_capabilities(new VRDisplayCapabilities()), |
| 60 m_eyeParametersLeft(new VREyeParameters()), | 60 m_eyeParametersLeft(new VREyeParameters()), |
| 61 m_eyeParametersRight(new VREyeParameters()), | 61 m_eyeParametersRight(new VREyeParameters()), |
| 62 m_fullscreenCheckTimer( | |
| 63 TaskRunnerHelper::get(TaskType::UnspecedTimer, | |
| 64 navigatorVR->document()->frame()), | |
| 65 this, | |
| 66 &VRDisplay::onFullscreenCheck), | |
| 67 m_display(std::move(display)), | 62 m_display(std::move(display)), |
| 63 m_submit_frame_client_binding(this), | |
| 68 m_displayClientBinding(this, std::move(request)) {} | 64 m_displayClientBinding(this, std::move(request)) {} |
| 69 | 65 |
| 70 VRDisplay::~VRDisplay() {} | 66 VRDisplay::~VRDisplay() {} |
| 71 | 67 |
| 72 VRController* VRDisplay::controller() { | 68 VRController* VRDisplay::controller() { |
| 73 return m_navigatorVR->controller(); | 69 return m_navigatorVR->controller(); |
| 74 } | 70 } |
| 75 | 71 |
| 76 void VRDisplay::update(const device::mojom::blink::VRDisplayInfoPtr& display) { | 72 void VRDisplay::update(const device::mojom::blink::VRDisplayInfoPtr& display) { |
| 77 m_displayId = display->index; | 73 m_displayId = display->index; |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 232 // allowed outside a user gesture so that the presented content may be | 228 // allowed outside a user gesture so that the presented content may be |
| 233 // updated. | 229 // updated. |
| 234 if (firstPresent && !UserGestureIndicator::utilizeUserGesture()) { | 230 if (firstPresent && !UserGestureIndicator::utilizeUserGesture()) { |
| 235 DOMException* exception = DOMException::create( | 231 DOMException* exception = DOMException::create( |
| 236 InvalidStateError, "API can only be initiated by a user gesture."); | 232 InvalidStateError, "API can only be initiated by a user gesture."); |
| 237 resolver->reject(exception); | 233 resolver->reject(exception); |
| 238 ReportPresentationResult(PresentationResult::NotInitiatedByUserGesture); | 234 ReportPresentationResult(PresentationResult::NotInitiatedByUserGesture); |
| 239 return promise; | 235 return promise; |
| 240 } | 236 } |
| 241 | 237 |
| 242 // TODO(mthiesse): Remove fullscreen requirement for presentation. See | |
| 243 // crbug.com/687369 | |
| 244 Document* doc = this->document(); | |
| 245 if (!doc || !Fullscreen::fullscreenEnabled(*doc)) { | |
| 246 DOMException* exception = | |
| 247 DOMException::create(InvalidStateError, "Fullscreen is not enabled."); | |
| 248 resolver->reject(exception); | |
| 249 ReportPresentationResult(PresentationResult::FullscreenNotEnabled); | |
| 250 return promise; | |
| 251 } | |
| 252 | |
| 253 // A valid number of layers must be provided in order to present. | 238 // A valid number of layers must be provided in order to present. |
| 254 if (layers.size() == 0 || layers.size() > m_capabilities->maxLayers()) { | 239 if (layers.size() == 0 || layers.size() > m_capabilities->maxLayers()) { |
| 255 forceExitPresent(); | 240 forceExitPresent(); |
| 256 DOMException* exception = | 241 DOMException* exception = |
| 257 DOMException::create(InvalidStateError, "Invalid number of layers."); | 242 DOMException::create(InvalidStateError, "Invalid number of layers."); |
| 258 resolver->reject(exception); | 243 resolver->reject(exception); |
| 259 ReportPresentationResult(PresentationResult::InvalidNumberOfLayers); | 244 ReportPresentationResult(PresentationResult::InvalidNumberOfLayers); |
| 260 return promise; | 245 return promise; |
| 261 } | 246 } |
| 262 | 247 |
| 263 // If what we were given has an invalid source, need to exit fullscreen with | 248 // If what we were given has an invalid source, need to exit fullscreen with |
| 264 // previous, valid source, so delay m_layer reassignment | 249 // previous, valid source, so delay m_layer reassignment |
| 265 if (layers[0].source().isNull()) { | 250 if (layers[0].source().isNull()) { |
| 266 forceExitPresent(); | 251 forceExitPresent(); |
| 267 DOMException* exception = | 252 DOMException* exception = |
| 268 DOMException::create(InvalidStateError, "Invalid layer source."); | 253 DOMException::create(InvalidStateError, "Invalid layer source."); |
| 269 resolver->reject(exception); | 254 resolver->reject(exception); |
| 270 ReportPresentationResult(PresentationResult::InvalidLayerSource); | 255 ReportPresentationResult(PresentationResult::InvalidLayerSource); |
| 271 return promise; | 256 return promise; |
| 272 } | 257 } |
| 273 m_layer = layers[0]; | 258 m_layer = layers[0]; |
| 274 | 259 |
| 260 if (firstPresent) { | |
|
bajones
2017/03/07 00:48:07
if (firstPresent) { /* Do nothing. I just like bra
klausw
2017/03/07 02:55:55
Oops, removed.
| |
| 261 } | |
| 262 | |
| 275 CanvasRenderingContext* renderingContext; | 263 CanvasRenderingContext* renderingContext; |
| 276 if (m_layer.source().isHTMLCanvasElement()) { | 264 if (m_layer.source().isHTMLCanvasElement()) { |
| 277 renderingContext = | 265 renderingContext = |
| 278 m_layer.source().getAsHTMLCanvasElement()->renderingContext(); | 266 m_layer.source().getAsHTMLCanvasElement()->renderingContext(); |
| 279 } else { | 267 } else { |
| 280 DCHECK(m_layer.source().isOffscreenCanvas()); | 268 DCHECK(m_layer.source().isOffscreenCanvas()); |
| 281 renderingContext = | 269 renderingContext = |
| 282 m_layer.source().getAsOffscreenCanvas()->renderingContext(); | 270 m_layer.source().getAsOffscreenCanvas()->renderingContext(); |
| 283 } | 271 } |
| 284 | 272 |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 388 "VR Presentation not implemented for this VRDisplay."); | 376 "VR Presentation not implemented for this VRDisplay."); |
| 389 while (!m_pendingPresentResolvers.isEmpty()) { | 377 while (!m_pendingPresentResolvers.isEmpty()) { |
| 390 ScriptPromiseResolver* resolver = m_pendingPresentResolvers.takeFirst(); | 378 ScriptPromiseResolver* resolver = m_pendingPresentResolvers.takeFirst(); |
| 391 resolver->reject(exception); | 379 resolver->reject(exception); |
| 392 } | 380 } |
| 393 ReportPresentationResult( | 381 ReportPresentationResult( |
| 394 PresentationResult::PresentationNotSupportedByDisplay); | 382 PresentationResult::PresentationNotSupportedByDisplay); |
| 395 return; | 383 return; |
| 396 } else { | 384 } else { |
| 397 if (m_layer.source().isHTMLCanvasElement()) { | 385 if (m_layer.source().isHTMLCanvasElement()) { |
| 398 HTMLCanvasElement* canvas = m_layer.source().getAsHTMLCanvasElement(); | 386 // Stop unneeded compositor updates. We do so by hiding the view. We can't |
| 399 // TODO(klausw,crbug.com/655722): Need a proper VR compositor, but | 387 // hide the page, doing so causes an assertion failure (!m_isHidden) in |
| 400 // for the moment on mobile we'll just make the canvas fullscreen | 388 // DrawingBuffer::prepareTextureMailboxInternal(). Do this only when we're |
| 401 // so that VrShell can pick it up through the standard (high | 389 // actually presenting (m_isPresenting is true), see corresponding show() |
| 402 // latency) compositing path. auto canvas = | 390 // in forceExitPresent(). Otherwise the view may remain hidden for failing |
| 403 // m_layer.source().getAsHTMLCanvasElement(); | 391 // DON flow. |
| 404 auto inlineStyle = canvas->inlineStyle(); | 392 auto doc = m_navigatorVR->document(); |
| 405 if (inlineStyle) { | 393 doc->view()->hide(); |
| 406 // THREE.js's VREffect sets explicit style.width/height on its rendering | 394 doc->layoutView()->compositor()->setCompositingModeEnabled(false); |
| 407 // canvas based on the non-fullscreen window dimensions, and it keeps | |
| 408 // those unchanged when presenting. Unfortunately it appears that a | |
| 409 // fullscreened canvas just gets centered if it has explicitly set a | |
| 410 // size smaller than the fullscreen dimensions. Manually set size to | |
| 411 // 100% in this case and restore it when exiting fullscreen. This is a | |
| 412 // stopgap measure since THREE.js's usage appears legal according to the | |
| 413 // WebVR API spec. This will no longer be necessary once we can get rid | |
| 414 // of this fullscreen hack. | |
| 415 m_fullscreenOrigWidth = inlineStyle->getPropertyValue(CSSPropertyWidth); | |
| 416 if (!m_fullscreenOrigWidth.isNull()) { | |
| 417 canvas->setInlineStyleProperty(CSSPropertyWidth, "100%"); | |
| 418 } | |
| 419 m_fullscreenOrigHeight = | |
| 420 inlineStyle->getPropertyValue(CSSPropertyHeight); | |
| 421 if (!m_fullscreenOrigHeight.isNull()) { | |
| 422 canvas->setInlineStyleProperty(CSSPropertyHeight, "100%"); | |
| 423 } | |
| 424 } else { | |
| 425 m_fullscreenOrigWidth = String(); | |
| 426 m_fullscreenOrigHeight = String(); | |
| 427 } | |
| 428 | |
| 429 if (doc) { | |
| 430 // Since the callback for requestPresent is asynchronous, we've lost our | |
| 431 // UserGestureToken, and need to create a new one to enter fullscreen. | |
| 432 gestureIndicator = WTF::wrapUnique( | |
| 433 new UserGestureIndicator(DocumentUserGestureToken::create( | |
| 434 doc, UserGestureToken::Status::PossiblyExistingGesture))); | |
| 435 } | |
| 436 Fullscreen::requestFullscreen(*canvas); | |
| 437 | |
| 438 // Check to see if the canvas is still the current fullscreen | |
| 439 // element once every 2 seconds. | |
| 440 m_fullscreenCheckTimer.startRepeating(2.0, BLINK_FROM_HERE); | |
| 441 m_reenteredFullscreen = false; | |
| 442 } else { | 395 } else { |
| 443 DCHECK(m_layer.source().isOffscreenCanvas()); | 396 DCHECK(m_layer.source().isOffscreenCanvas()); |
| 444 // TODO(junov, crbug.com/695497): Implement OffscreenCanvas presentation | 397 // TODO(junov, crbug.com/695497): Implement OffscreenCanvas presentation |
| 445 forceExitPresent(); | 398 forceExitPresent(); |
| 446 DOMException* exception = DOMException::create( | 399 DOMException* exception = DOMException::create( |
| 447 InvalidStateError, "OffscreenCanvas presentation not implemented."); | 400 InvalidStateError, "OffscreenCanvas presentation not implemented."); |
| 448 while (!m_pendingPresentResolvers.isEmpty()) { | 401 while (!m_pendingPresentResolvers.isEmpty()) { |
| 449 ScriptPromiseResolver* resolver = m_pendingPresentResolvers.takeFirst(); | 402 ScriptPromiseResolver* resolver = m_pendingPresentResolvers.takeFirst(); |
| 450 resolver->reject(exception); | 403 resolver->reject(exception); |
| 451 } | 404 } |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 512 } else { | 465 } else { |
| 513 // Right eye defaults | 466 // Right eye defaults |
| 514 rightBounds->left = 0.5f; | 467 rightBounds->left = 0.5f; |
| 515 rightBounds->top = 0.0f; | 468 rightBounds->top = 0.0f; |
| 516 rightBounds->width = 0.5f; | 469 rightBounds->width = 0.5f; |
| 517 rightBounds->height = 1.0f; | 470 rightBounds->height = 1.0f; |
| 518 m_layer.setRightBounds({0.5f, 0.0f, 0.5f, 1.0f}); | 471 m_layer.setRightBounds({0.5f, 0.0f, 0.5f, 1.0f}); |
| 519 } | 472 } |
| 520 | 473 |
| 521 m_display->UpdateLayerBounds(m_vrFrameId, std::move(leftBounds), | 474 m_display->UpdateLayerBounds(m_vrFrameId, std::move(leftBounds), |
| 522 std::move(rightBounds)); | 475 std::move(rightBounds), m_sourceWidth, |
| 476 m_sourceHeight); | |
| 523 } | 477 } |
| 524 | 478 |
| 525 HeapVector<VRLayer> VRDisplay::getLayers() { | 479 HeapVector<VRLayer> VRDisplay::getLayers() { |
| 526 HeapVector<VRLayer> layers; | 480 HeapVector<VRLayer> layers; |
| 527 | 481 |
| 528 if (m_isPresenting) { | 482 if (m_isPresenting) { |
| 529 layers.push_back(m_layer); | 483 layers.push_back(m_layer); |
| 530 } | 484 } |
| 531 | 485 |
| 532 return layers; | 486 return layers; |
| 533 } | 487 } |
| 534 | 488 |
| 535 void VRDisplay::submitFrame() { | 489 void VRDisplay::submitFrame() { |
| 536 if (!m_display) | 490 if (!m_display) |
| 537 return; | 491 return; |
| 492 TRACE_EVENT1("gpu", "submitFrame", "frame", m_vrFrameId); | |
| 538 | 493 |
| 539 Document* doc = this->document(); | 494 Document* doc = this->document(); |
| 540 if (!m_isPresenting) { | 495 if (!m_isPresenting) { |
| 541 if (doc) { | 496 if (doc) { |
| 542 doc->addConsoleMessage(ConsoleMessage::create( | 497 doc->addConsoleMessage(ConsoleMessage::create( |
| 543 RenderingMessageSource, WarningMessageLevel, | 498 RenderingMessageSource, WarningMessageLevel, |
| 544 "submitFrame has no effect when the VRDisplay is not presenting.")); | 499 "submitFrame has no effect when the VRDisplay is not presenting.")); |
| 545 } | 500 } |
| 546 return; | 501 return; |
| 547 } | 502 } |
| 548 | 503 |
| 549 if (!m_inAnimationFrame) { | 504 if (!m_inAnimationFrame) { |
| 550 if (doc) { | 505 if (doc) { |
| 551 doc->addConsoleMessage( | 506 doc->addConsoleMessage( |
| 552 ConsoleMessage::create(RenderingMessageSource, WarningMessageLevel, | 507 ConsoleMessage::create(RenderingMessageSource, WarningMessageLevel, |
| 553 "submitFrame must be called within a " | 508 "submitFrame must be called within a " |
| 554 "VRDisplay.requestAnimationFrame callback.")); | 509 "VRDisplay.requestAnimationFrame callback.")); |
| 555 } | 510 } |
| 556 return; | 511 return; |
| 557 } | 512 } |
| 558 | 513 |
| 559 if (!m_contextGL) { | 514 if (!m_contextGL) { |
| 560 // Something got confused, we can't submit frames without a GL context. | 515 // Something got confused, we can't submit frames without a GL context. |
| 561 return; | 516 return; |
| 562 } | 517 } |
| 563 | 518 |
| 564 // No frame Id to write before submitting the frame. | 519 // No frame Id to write before submitting the frame. |
| 565 if (m_vrFrameId < 0) { | 520 if (m_vrFrameId < 0) { |
| 566 m_display->SubmitFrame(m_framePose.Clone()); | 521 // TODO(klausw): There used to be a submitFrame here, but we can't |
| 522 // submit without a frameId and associated pose data. Just drop it. | |
| 567 return; | 523 return; |
| 568 } | 524 } |
| 569 | 525 |
| 570 // Write the frame number for the pose used into a bottom left pixel block. | 526 m_contextGL->Flush(); |
| 571 // It is read by chrome/browser/android/vr_shell/vr_shell.cc to associate | 527 auto elem = m_layer.source(); |
| 572 // the correct corresponding pose for submission. | |
| 573 auto gl = m_contextGL; | |
| 574 | 528 |
| 575 // We must ensure that the WebGL app's GL state is preserved. We do this by | 529 // Check if the canvas got resized, if yes send a bounds update. |
| 576 // calling low-level GL commands directly so that the rendering context's | 530 int currentWidth = m_renderingContext->drawingBufferWidth(); |
| 577 // saved parameters don't get overwritten. | 531 int currentHeight = m_renderingContext->drawingBufferHeight(); |
| 532 if ((currentWidth != m_sourceWidth || currentHeight != m_sourceHeight) && | |
| 533 currentWidth != 0 && currentHeight != 0) { | |
| 534 m_sourceWidth = currentWidth; | |
| 535 m_sourceHeight = currentHeight; | |
| 536 updateLayerBounds(); | |
| 537 } | |
| 578 | 538 |
| 579 gl->Enable(GL_SCISSOR_TEST); | 539 // There's two types of synchronization needed for submitting frames: |
| 580 // Use a few pixels to ensure we get a clean color. The resolution for the | 540 // |
| 581 // WebGL buffer may not match the final rendered destination size, and | 541 // - Before submitting, need to wait for the previous frame to be |
| 582 // texture filtering could interfere for single pixels. This isn't visible | 542 // pulled off the transfer surface to avoid lost frames. This |
| 583 // since the final rendering hides the edges via a vignette effect. | 543 // is currently a compile-time option, normally we always want |
| 584 gl->Scissor(0, 0, 4, 4); | 544 // to defer this wait to increase parallelism. |
| 585 gl->ColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); | 545 // |
| 586 // Careful with the arithmetic here. Float color 1.f is equivalent to int 255. | 546 // - After submitting, need to wait for the mailbox to be consumed, |
| 587 // Use the low byte of the index as the red component, and store an arbitrary | 547 // and must remain inside the execution context while waiting. |
| 588 // magic number in green/blue. This number must match the reading code in | 548 // The waitForPreviousTransferToFinish option defers this wait |
| 589 // vr_shell.cc. Avoid all-black/all-white. | 549 // until the next frame. That's more efficient, but seems to |
| 590 gl->ClearColor((m_vrFrameId & 255) / 255.0f, | 550 // cause wobbly framerates. |
| 591 kWebVrPosePixelMagicNumbers[0] / 255.0f, | 551 bool waitForPreviousTransferToFinish = |
| 592 kWebVrPosePixelMagicNumbers[1] / 255.0f, 1.0f); | 552 RuntimeEnabledFeatures::webVRExperimentalRenderingEnabled(); |
| 593 gl->Clear(GL_COLOR_BUFFER_BIT); | |
| 594 | 553 |
| 595 // Set the GL state back to what was set by the WebVR application. | 554 if (waitForPreviousTransferToFinish) { |
| 596 m_renderingContext->restoreScissorEnabled(); | 555 TRACE_EVENT0("gpu", "waitPrevXfer"); |
| 597 m_renderingContext->restoreScissorBox(); | 556 while (m_pendingSubmitFrame) { |
| 598 m_renderingContext->restoreColorMask(); | 557 if (!m_submit_frame_client_binding.WaitForIncomingMethodCall()) { |
| 599 m_renderingContext->restoreClearColor(); | 558 LOG(ERROR) << "Failed to receive SubmitFrame response"; |
| 559 break; | |
| 560 } | |
| 561 } | |
| 562 } | |
| 600 | 563 |
| 601 m_display->SubmitFrame(m_framePose.Clone()); | 564 auto image_ref = m_renderingContext->getImage( |
| 565 PreferAcceleration, SnapshotReasonCreateImageBitmap); | |
| 566 | |
| 567 // Hardware-accelerated rendering should always be texture backed. | |
| 568 // I hope nobody is trying to do WebVR with software rendering. | |
| 569 DCHECK(image_ref->isTextureBacked()); | |
| 570 | |
| 571 // The AcceleratedStaticBitmapImage must be kept alive until the | |
| 572 // mailbox is used via createAndConsumeTextureCHROMIUM, the mailbox | |
| 573 // itself does not keep it alive. We must keep a reference to the | |
| 574 // image until the mailbox was consumed. | |
| 575 StaticBitmapImage* staticImage = | |
| 576 static_cast<StaticBitmapImage*>(image_ref.get()); | |
| 577 staticImage->ensureMailbox(); | |
| 578 auto mailbox = staticImage->mailbox(); | |
| 579 | |
| 580 if (waitForPreviousTransferToFinish) { | |
| 581 // Save a reference to the image to keep it alive until next frame, | |
| 582 // where we'll wait for the transfer to finish before overwriting | |
| 583 // it. | |
| 584 m_previousImage = std::move(image_ref); | |
| 585 } | |
| 586 | |
| 587 // Wait for the previous render to finish, to avoid losing frames in the | |
| 588 // Android Surface / GLConsumer pair. TODO(klausw): make this tunable? | |
| 589 // Other devices may have different preferences. | |
| 590 { | |
| 591 TRACE_EVENT0("gpu", "waitPrevRender"); | |
| 592 while (m_pendingPreviousFrameRender) { | |
| 593 if (!m_submit_frame_client_binding.WaitForIncomingMethodCall()) { | |
| 594 LOG(ERROR) << "Failed to receive SubmitFrame response"; | |
| 595 break; | |
| 596 } | |
| 597 } | |
| 598 TRACE_EVENT_END0("gpu", "waitPrevRender"); | |
| 599 } | |
| 600 | |
| 601 m_pendingPreviousFrameRender = true; | |
| 602 m_pendingSubmitFrame = true; | |
| 603 | |
| 604 #define USE_MAILBOX_TYPEMAP_WORKAROUND 1 | |
| 605 #if USE_MAILBOX_TYPEMAP_WORKAROUND | |
| 606 // Temporary workaround for being unable to use the mojo typemap | |
| 607 // for the mailbox type. See added comment in blink_typemaps.gni. | |
| 608 | |
| 609 // Convert mailbox to a mojo type manually. | |
| 610 auto mailbox_ptr = gpu::mojom::blink::Mailbox::New(); | |
| 611 WTF::Vector<int8_t> tmpname(GL_MAILBOX_SIZE_CHROMIUM); | |
| 612 memcpy(&tmpname[0], &mailbox.name[0], GL_MAILBOX_SIZE_CHROMIUM); | |
| 613 mailbox_ptr->name = std::move(tmpname); | |
| 614 | |
| 615 m_display->SubmitFrame( | |
| 616 m_vrFrameId, std::move(mailbox_ptr), staticImage->syncToken(), | |
| 617 m_submit_frame_client_binding.CreateInterfacePtrAndBind()); | |
| 618 #else | |
| 619 m_display->SubmitFrame( | |
| 620 m_vrFrameId, mailbox, staticImage->syncToken(), | |
| 621 m_submit_frame_client_binding.CreateInterfacePtrAndBind()); | |
| 622 #endif | |
| 623 | |
| 624 // If we're not deferring the wait for transferring the mailbox, | |
| 625 // we need to wait for it now to prevent the image going out of | |
| 626 // scope before its mailbox is retrieved. | |
| 627 if (!waitForPreviousTransferToFinish) { | |
| 628 TRACE_EVENT0("gpu", "waitCurrXfer"); | |
| 629 while (m_pendingSubmitFrame) { | |
| 630 if (!m_submit_frame_client_binding.WaitForIncomingMethodCall()) { | |
| 631 LOG(ERROR) << "Failed to receive SubmitFrame response"; | |
| 632 break; | |
| 633 } | |
| 634 } | |
| 635 } | |
| 636 } | |
| 637 | |
| 638 void VRDisplay::OnSubmitFrameTransferred() { | |
| 639 m_pendingSubmitFrame = false; | |
| 640 } | |
| 641 | |
| 642 void VRDisplay::OnSubmitFrameRendered() { | |
| 643 m_pendingPreviousFrameRender = false; | |
| 602 } | 644 } |
| 603 | 645 |
| 604 Document* VRDisplay::document() { | 646 Document* VRDisplay::document() { |
| 605 return m_navigatorVR->document(); | 647 return m_navigatorVR->document(); |
| 606 } | 648 } |
| 607 | 649 |
| 608 void VRDisplay::OnPresentChange() { | 650 void VRDisplay::OnPresentChange() { |
| 609 if (m_isPresenting && !m_isValidDeviceForPresenting) { | 651 if (m_isPresenting && !m_isValidDeviceForPresenting) { |
| 610 VLOG(1) << __FUNCTION__ << ": device not valid, not sending event"; | 652 VLOG(1) << __FUNCTION__ << ": device not valid, not sending event"; |
| 611 return; | 653 return; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 629 | 671 |
| 630 void VRDisplay::onDisconnected() { | 672 void VRDisplay::onDisconnected() { |
| 631 m_navigatorVR->enqueueVREvent(VRDisplayEvent::create( | 673 m_navigatorVR->enqueueVREvent(VRDisplayEvent::create( |
| 632 EventTypeNames::vrdisplaydisconnect, true, false, this, "disconnect")); | 674 EventTypeNames::vrdisplaydisconnect, true, false, this, "disconnect")); |
| 633 } | 675 } |
| 634 | 676 |
| 635 void VRDisplay::stopPresenting() { | 677 void VRDisplay::stopPresenting() { |
| 636 if (m_isPresenting) { | 678 if (m_isPresenting) { |
| 637 if (!m_capabilities->hasExternalDisplay()) { | 679 if (!m_capabilities->hasExternalDisplay()) { |
| 638 if (m_layer.source().isHTMLCanvasElement()) { | 680 if (m_layer.source().isHTMLCanvasElement()) { |
| 639 auto canvas = m_layer.source().getAsHTMLCanvasElement(); | 681 // TODO(klausw): Is there a need to restore the original |
| 640 Fullscreen::fullyExitFullscreen(canvas->document()); | 682 // hide/show state and compositing mode? AFAIK there's no way |
| 641 m_fullscreenCheckTimer.stop(); | 683 // to enter presentation without being visible and compositing, |
| 642 if (!m_fullscreenOrigWidth.isNull()) { | 684 // so we'd always restore to these values. |
| 643 canvas->setInlineStyleProperty(CSSPropertyWidth, | 685 auto doc = m_navigatorVR->document(); |
| 644 m_fullscreenOrigWidth); | 686 doc->view()->show(); |
| 645 m_fullscreenOrigWidth = String(); | 687 doc->layoutView()->compositor()->setCompositingModeEnabled(true); |
| 646 } | |
| 647 if (!m_fullscreenOrigHeight.isNull()) { | |
| 648 canvas->setInlineStyleProperty(CSSPropertyWidth, | |
| 649 m_fullscreenOrigHeight); | |
| 650 m_fullscreenOrigHeight = String(); | |
| 651 } | |
| 652 } else { | 688 } else { |
| 653 // TODO(junov, crbug.com/695497): Implement for OffscreenCanvas | 689 // TODO(junov, crbug.com/695497): Implement for OffscreenCanvas |
| 654 } | 690 } |
| 655 } else { | 691 } else { |
| 656 // Can't get into this presentation mode, so nothing to do here. | 692 // Can't get into this presentation mode, so nothing to do here. |
| 657 } | 693 } |
| 658 m_isPresenting = false; | 694 m_isPresenting = false; |
| 659 OnPresentChange(); | 695 OnPresentChange(); |
| 660 } | 696 } |
| 661 | 697 |
| 662 m_renderingContext = nullptr; | 698 m_renderingContext = nullptr; |
| 663 m_contextGL = nullptr; | 699 m_contextGL = nullptr; |
| 700 m_pendingSubmitFrame = false; | |
| 701 m_pendingPreviousFrameRender = false; | |
| 664 } | 702 } |
| 665 | 703 |
| 666 void VRDisplay::OnActivate(device::mojom::blink::VRDisplayEventReason reason) { | 704 void VRDisplay::OnActivate(device::mojom::blink::VRDisplayEventReason reason) { |
| 667 if (!m_navigatorVR->isFocused() || m_displayBlurred) | 705 if (!m_navigatorVR->isFocused() || m_displayBlurred) |
| 668 return; | 706 return; |
| 669 m_navigatorVR->dispatchVRGestureEvent(VRDisplayEvent::create( | 707 m_navigatorVR->dispatchVRGestureEvent(VRDisplayEvent::create( |
| 670 EventTypeNames::vrdisplayactivate, true, false, this, reason)); | 708 EventTypeNames::vrdisplayactivate, true, false, this, reason)); |
| 671 } | 709 } |
| 672 | 710 |
| 673 void VRDisplay::OnDeactivate( | 711 void VRDisplay::OnDeactivate( |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 716 if (!m_navigatorVR->isFocused() || m_vrVSyncProvider.is_bound()) | 754 if (!m_navigatorVR->isFocused() || m_vrVSyncProvider.is_bound()) |
| 717 return; | 755 return; |
| 718 m_display->GetVRVSyncProvider(mojo::MakeRequest(&m_vrVSyncProvider)); | 756 m_display->GetVRVSyncProvider(mojo::MakeRequest(&m_vrVSyncProvider)); |
| 719 if (m_pendingRaf && !m_displayBlurred) { | 757 if (m_pendingRaf && !m_displayBlurred) { |
| 720 m_pendingVsync = true; | 758 m_pendingVsync = true; |
| 721 m_vrVSyncProvider->GetVSync(convertToBaseCallback( | 759 m_vrVSyncProvider->GetVSync(convertToBaseCallback( |
| 722 WTF::bind(&VRDisplay::OnVSync, wrapWeakPersistent(this)))); | 760 WTF::bind(&VRDisplay::OnVSync, wrapWeakPersistent(this)))); |
| 723 } | 761 } |
| 724 } | 762 } |
| 725 | 763 |
| 726 void VRDisplay::onFullscreenCheck(TimerBase*) { | |
| 727 DCHECK(m_layer.source().isHTMLCanvasElement()); | |
| 728 if (!m_isPresenting) { | |
| 729 m_fullscreenCheckTimer.stop(); | |
| 730 return; | |
| 731 } | |
| 732 // TODO: This is a temporary measure to track if fullscreen mode has been | |
| 733 // exited by the UA. If so we need to end VR presentation. Soon we won't | |
| 734 // depend on the Fullscreen API to fake VR presentation, so this will | |
| 735 // become unnessecary. Until that point, though, this seems preferable to | |
| 736 // adding a bunch of notification plumbing to Fullscreen. | |
| 737 if (!Fullscreen::isCurrentFullScreenElement( | |
| 738 *m_layer.source().getAsHTMLCanvasElement())) { | |
| 739 // TODO(mthiesse): Due to asynchronous resizing, we might get kicked out of | |
| 740 // fullscreen when changing display parameters upon entering WebVR. So one | |
| 741 // time only, we reenter fullscreen after having left it; otherwise we exit | |
| 742 // presentation. | |
| 743 if (m_reenteredFullscreen) { | |
| 744 m_isPresenting = false; | |
| 745 OnPresentChange(); | |
| 746 m_fullscreenCheckTimer.stop(); | |
| 747 if (m_display) | |
| 748 m_display->ExitPresent(); | |
| 749 return; | |
| 750 } | |
| 751 m_reenteredFullscreen = true; | |
| 752 auto canvas = m_layer.source().getAsHTMLCanvasElement(); | |
| 753 Document* doc = this->document(); | |
| 754 std::unique_ptr<UserGestureIndicator> gestureIndicator; | |
| 755 if (doc) { | |
| 756 gestureIndicator = WTF::wrapUnique( | |
| 757 new UserGestureIndicator(DocumentUserGestureToken::create( | |
| 758 doc, UserGestureToken::Status::PossiblyExistingGesture))); | |
| 759 } | |
| 760 Fullscreen::requestFullscreen(*canvas); | |
| 761 } | |
| 762 } | |
| 763 | |
| 764 ScriptedAnimationController& VRDisplay::ensureScriptedAnimationController( | 764 ScriptedAnimationController& VRDisplay::ensureScriptedAnimationController( |
| 765 Document* doc) { | 765 Document* doc) { |
| 766 if (!m_scriptedAnimationController) | 766 if (!m_scriptedAnimationController) |
| 767 m_scriptedAnimationController = ScriptedAnimationController::create(doc); | 767 m_scriptedAnimationController = ScriptedAnimationController::create(doc); |
| 768 | 768 |
| 769 return *m_scriptedAnimationController; | 769 return *m_scriptedAnimationController; |
| 770 } | 770 } |
| 771 | 771 |
| 772 void VRDisplay::dispose() { | 772 void VRDisplay::dispose() { |
| 773 m_displayClientBinding.Close(); | 773 m_displayClientBinding.Close(); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 807 visitor->trace(m_stageParameters); | 807 visitor->trace(m_stageParameters); |
| 808 visitor->trace(m_eyeParametersLeft); | 808 visitor->trace(m_eyeParametersLeft); |
| 809 visitor->trace(m_eyeParametersRight); | 809 visitor->trace(m_eyeParametersRight); |
| 810 visitor->trace(m_layer); | 810 visitor->trace(m_layer); |
| 811 visitor->trace(m_renderingContext); | 811 visitor->trace(m_renderingContext); |
| 812 visitor->trace(m_scriptedAnimationController); | 812 visitor->trace(m_scriptedAnimationController); |
| 813 visitor->trace(m_pendingPresentResolvers); | 813 visitor->trace(m_pendingPresentResolvers); |
| 814 } | 814 } |
| 815 | 815 |
| 816 } // namespace blink | 816 } // namespace blink |
| OLD | NEW |