Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: third_party/WebKit/Source/modules/vr/VRDisplay.cpp

Issue 2453973002: WIP WebVR mojo refactor (Closed)
Patch Set: Fixed crash. VR data doesn't seem to be polling correctly, though. Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/FrameRequestCallback.h"
9 #include "core/dom/Fullscreen.h" 9 #include "core/dom/Fullscreen.h"
10 #include "core/dom/ScriptedAnimationController.h" 10 #include "core/dom/ScriptedAnimationController.h"
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
50 visitor->trace(m_vrDisplay); 50 visitor->trace(m_vrDisplay);
51 51
52 FrameRequestCallback::trace(visitor); 52 FrameRequestCallback::trace(visitor);
53 } 53 }
54 54
55 Member<VRDisplay> m_vrDisplay; 55 Member<VRDisplay> m_vrDisplay;
56 }; 56 };
57 57
58 } // namespace 58 } // namespace
59 59
60 VRDisplay::VRDisplay(NavigatorVR* navigatorVR) 60 VRDisplay::VRDisplay(NavigatorVR* navigatorVR,
61 device::mojom::blink::VRDisplayPtr display)
61 : m_navigatorVR(navigatorVR), 62 : m_navigatorVR(navigatorVR),
62 m_displayId(0),
63 m_isConnected(false), 63 m_isConnected(false),
64 m_isPresenting(false), 64 m_isPresenting(false),
65 m_canUpdateFramePose(true), 65 m_canUpdateFramePose(true),
66 m_capabilities(new VRDisplayCapabilities()), 66 m_capabilities(new VRDisplayCapabilities()),
67 m_eyeParametersLeft(new VREyeParameters()), 67 m_eyeParametersLeft(new VREyeParameters()),
68 m_eyeParametersRight(new VREyeParameters()), 68 m_eyeParametersRight(new VREyeParameters()),
69 m_depthNear(0.01), 69 m_depthNear(0.01),
70 m_depthFar(10000.0), 70 m_depthFar(10000.0),
71 m_fullscreenCheckTimer(this, &VRDisplay::onFullscreenCheck), 71 m_fullscreenCheckTimer(this, &VRDisplay::onFullscreenCheck),
72 m_animationCallbackRequested(false), 72 m_animationCallbackRequested(false),
73 m_inAnimationFrame(false) {} 73 m_inAnimationFrame(false),
74 m_display(std::move(display)),
75 m_binding(this) {
76 m_display->SetClient(m_binding.CreateInterfacePtrAndBind());
77 }
74 78
75 VRDisplay::~VRDisplay() {} 79 VRDisplay::~VRDisplay() {}
76 80
77 VRController* VRDisplay::controller() { 81 VRController* VRDisplay::controller() {
78 return m_navigatorVR->controller(); 82 return m_navigatorVR->controller();
79 } 83 }
80 84
81 void VRDisplay::update(const device::blink::VRDisplayPtr& display) { 85 void VRDisplay::update(const device::mojom::blink::VRDisplayInfoPtr& display) {
82 m_displayId = display->index; 86 m_displayId = display->index;
83 m_displayName = display->displayName; 87 m_displayName = display->displayName;
84 m_isConnected = true; 88 m_isConnected = true;
85 89
86 m_capabilities->setHasOrientation(display->capabilities->hasOrientation); 90 m_capabilities->setHasOrientation(display->capabilities->hasOrientation);
87 m_capabilities->setHasPosition(display->capabilities->hasPosition); 91 m_capabilities->setHasPosition(display->capabilities->hasPosition);
88 m_capabilities->setHasExternalDisplay( 92 m_capabilities->setHasExternalDisplay(
89 display->capabilities->hasExternalDisplay); 93 display->capabilities->hasExternalDisplay);
90 m_capabilities->setCanPresent(display->capabilities->canPresent); 94 m_capabilities->setCanPresent(display->capabilities->canPresent);
91 m_capabilities->setMaxLayers(display->capabilities->canPresent ? 1 : 0); 95 m_capabilities->setMaxLayers(display->capabilities->canPresent ? 1 : 0);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
126 if (!m_framePose) 130 if (!m_framePose)
127 return nullptr; 131 return nullptr;
128 132
129 VRPose* pose = VRPose::create(); 133 VRPose* pose = VRPose::create();
130 pose->setPose(m_framePose); 134 pose->setPose(m_framePose);
131 return pose; 135 return pose;
132 } 136 }
133 137
134 void VRDisplay::updatePose() { 138 void VRDisplay::updatePose() {
135 if (m_canUpdateFramePose) { 139 if (m_canUpdateFramePose) {
136 m_framePose = controller()->getPose(m_displayId); 140 if (!m_display)
141 return;
142 device::mojom::blink::VRPosePtr pose;
143 m_display->GetPose(&pose);
144 m_framePose = std::move(pose);
137 if (m_isPresenting) 145 if (m_isPresenting)
138 m_canUpdateFramePose = false; 146 m_canUpdateFramePose = false;
139 } 147 }
140 } 148 }
141 149
142 void VRDisplay::resetPose() { 150 void VRDisplay::resetPose() {
143 controller()->resetPose(m_displayId); 151 if (!m_display)
152 return;
153
154 m_display->ResetPose();
144 } 155 }
145 156
146 VREyeParameters* VRDisplay::getEyeParameters(const String& whichEye) { 157 VREyeParameters* VRDisplay::getEyeParameters(const String& whichEye) {
147 switch (stringToVREye(whichEye)) { 158 switch (stringToVREye(whichEye)) {
148 case VREyeLeft: 159 case VREyeLeft:
149 return m_eyeParametersLeft; 160 return m_eyeParametersLeft;
150 case VREyeRight: 161 case VREyeRight:
151 return m_eyeParametersRight; 162 return m_eyeParametersRight;
152 default: 163 default:
153 return nullptr; 164 return nullptr;
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 Fullscreen::requestFullscreen(*m_layer.source(), 303 Fullscreen::requestFullscreen(*m_layer.source(),
293 Fullscreen::UnprefixedRequest); 304 Fullscreen::UnprefixedRequest);
294 305
295 // Check to see if the canvas is still the current fullscreen 306 // Check to see if the canvas is still the current fullscreen
296 // element once per second. 307 // element once per second.
297 m_fullscreenCheckTimer.startRepeating(1.0, BLINK_FROM_HERE); 308 m_fullscreenCheckTimer.startRepeating(1.0, BLINK_FROM_HERE);
298 } 309 }
299 310
300 if (firstPresent) { 311 if (firstPresent) {
301 bool secureContext = scriptState->getExecutionContext()->isSecureContext(); 312 bool secureContext = scriptState->getExecutionContext()->isSecureContext();
302 controller()->requestPresent(resolver, m_displayId, secureContext); 313 if (!m_display) {
314 DOMException* exception = DOMException::create(
315 InvalidStateError, "The service is no longer active.");
316 resolver->reject(exception);
317 return promise;
318 }
319 m_display->RequestPresent(
320 secureContext, convertToBaseCallback(WTF::bind(
321 &VRDisplay::onPresentComplete, wrapPersistent(this),
322 wrapPersistent(resolver))));
303 } else { 323 } else {
304 updateLayerBounds(); 324 updateLayerBounds();
305 resolver->resolve(); 325 resolver->resolve();
306 ReportPresentationResult(PresentationResult::SuccessAlreadyPresenting); 326 ReportPresentationResult(PresentationResult::SuccessAlreadyPresenting);
307 } 327 }
308 328
309 return promise; 329 return promise;
310 } 330 }
311 331
332 void VRDisplay::onPresentComplete(ScriptPromiseResolver* resolver,
333 bool success) {
334 if (success) {
335 this->beginPresent(resolver);
336 } else {
337 this->forceExitPresent();
338 DOMException* exception = DOMException::create(
339 NotAllowedError, "Presentation request was denied.");
340 resolver->reject(exception);
341 }
342 }
343
312 ScriptPromise VRDisplay::exitPresent(ScriptState* scriptState) { 344 ScriptPromise VRDisplay::exitPresent(ScriptState* scriptState) {
313 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 345 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
314 ScriptPromise promise = resolver->promise(); 346 ScriptPromise promise = resolver->promise();
315 347
316 if (!m_isPresenting) { 348 if (!m_isPresenting) {
317 // Can't stop presenting if we're not presenting. 349 // Can't stop presenting if we're not presenting.
318 DOMException* exception = 350 DOMException* exception =
319 DOMException::create(InvalidStateError, "VRDisplay is not presenting."); 351 DOMException::create(InvalidStateError, "VRDisplay is not presenting.");
320 resolver->reject(exception); 352 resolver->reject(exception);
321 return promise; 353 return promise;
322 } 354 }
323 355
324 controller()->exitPresent(m_displayId); 356 if (!m_display) {
357 DOMException* exception =
358 DOMException::create(InvalidStateError, "VRService is not available.");
359 resolver->reject(exception);
360 return promise;
361 }
362 m_display->ExitPresent();
325 363
326 resolver->resolve(); 364 resolver->resolve();
327 365
328 forceExitPresent(); 366 forceExitPresent();
329 367
330 return promise; 368 return promise;
331 } 369 }
332 370
333 void VRDisplay::beginPresent(ScriptPromiseResolver* resolver) { 371 void VRDisplay::beginPresent(ScriptPromiseResolver* resolver) {
334 if (m_capabilities->hasExternalDisplay()) { 372 if (m_capabilities->hasExternalDisplay()) {
(...skipping 27 matching lines...) Expand all
362 m_navigatorVR->fireVRDisplayPresentChange(this); 400 m_navigatorVR->fireVRDisplayPresentChange(this);
363 } 401 }
364 402
365 m_isPresenting = false; 403 m_isPresenting = false;
366 m_renderingContext = nullptr; 404 m_renderingContext = nullptr;
367 m_contextGL = nullptr; 405 m_contextGL = nullptr;
368 } 406 }
369 407
370 void VRDisplay::updateLayerBounds() { 408 void VRDisplay::updateLayerBounds() {
371 // Set up the texture bounds for the provided layer 409 // Set up the texture bounds for the provided layer
372 device::blink::VRLayerBoundsPtr leftBounds = 410 device::mojom::blink::VRLayerBoundsPtr leftBounds =
373 device::blink::VRLayerBounds::New(); 411 device::mojom::blink::VRLayerBounds::New();
374 device::blink::VRLayerBoundsPtr rightBounds = 412 device::mojom::blink::VRLayerBoundsPtr rightBounds =
375 device::blink::VRLayerBounds::New(); 413 device::mojom::blink::VRLayerBounds::New();
376 414
377 if (m_layer.leftBounds().size() == 4) { 415 if (m_layer.leftBounds().size() == 4) {
378 leftBounds->left = m_layer.leftBounds()[0]; 416 leftBounds->left = m_layer.leftBounds()[0];
379 leftBounds->top = m_layer.leftBounds()[1]; 417 leftBounds->top = m_layer.leftBounds()[1];
380 leftBounds->width = m_layer.leftBounds()[2]; 418 leftBounds->width = m_layer.leftBounds()[2];
381 leftBounds->height = m_layer.leftBounds()[3]; 419 leftBounds->height = m_layer.leftBounds()[3];
382 } else { 420 } else {
383 // Left eye defaults 421 // Left eye defaults
384 leftBounds->left = 0.0f; 422 leftBounds->left = 0.0f;
385 leftBounds->top = 0.0f; 423 leftBounds->top = 0.0f;
386 leftBounds->width = 0.5f; 424 leftBounds->width = 0.5f;
387 leftBounds->height = 1.0f; 425 leftBounds->height = 1.0f;
388 } 426 }
389 427
390 if (m_layer.rightBounds().size() == 4) { 428 if (m_layer.rightBounds().size() == 4) {
391 rightBounds->left = m_layer.rightBounds()[0]; 429 rightBounds->left = m_layer.rightBounds()[0];
392 rightBounds->top = m_layer.rightBounds()[1]; 430 rightBounds->top = m_layer.rightBounds()[1];
393 rightBounds->width = m_layer.rightBounds()[2]; 431 rightBounds->width = m_layer.rightBounds()[2];
394 rightBounds->height = m_layer.rightBounds()[3]; 432 rightBounds->height = m_layer.rightBounds()[3];
395 } else { 433 } else {
396 // Right eye defaults 434 // Right eye defaults
397 rightBounds->left = 0.5f; 435 rightBounds->left = 0.5f;
398 rightBounds->top = 0.0f; 436 rightBounds->top = 0.0f;
399 rightBounds->width = 0.5f; 437 rightBounds->width = 0.5f;
400 rightBounds->height = 1.0f; 438 rightBounds->height = 1.0f;
401 } 439 }
402 440
403 controller()->updateLayerBounds(m_displayId, std::move(leftBounds), 441 if (!m_display)
404 std::move(rightBounds)); 442 return;
443
444 m_display->UpdateLayerBounds(std::move(leftBounds), std::move(rightBounds));
405 } 445 }
406 446
407 HeapVector<VRLayer> VRDisplay::getLayers() { 447 HeapVector<VRLayer> VRDisplay::getLayers() {
408 HeapVector<VRLayer> layers; 448 HeapVector<VRLayer> layers;
409 449
410 if (m_isPresenting) { 450 if (m_isPresenting) {
411 layers.append(m_layer); 451 layers.append(m_layer);
412 } 452 }
413 453
414 return layers; 454 return layers;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
461 gl->ClearColor((idx & 255) / 255.0f, ((idx >> 8) & 255) / 255.0f, 501 gl->ClearColor((idx & 255) / 255.0f, ((idx >> 8) & 255) / 255.0f,
462 ((idx >> 16) & 255) / 255.0f, 1.0f); 502 ((idx >> 16) & 255) / 255.0f, 1.0f);
463 gl->Clear(GL_COLOR_BUFFER_BIT); 503 gl->Clear(GL_COLOR_BUFFER_BIT);
464 504
465 // Set the GL state back to what was set by the WebVR application. 505 // Set the GL state back to what was set by the WebVR application.
466 m_renderingContext->restoreScissorEnabled(); 506 m_renderingContext->restoreScissorEnabled();
467 m_renderingContext->restoreScissorBox(); 507 m_renderingContext->restoreScissorBox();
468 m_renderingContext->restoreColorMask(); 508 m_renderingContext->restoreColorMask();
469 m_renderingContext->restoreClearColor(); 509 m_renderingContext->restoreClearColor();
470 510
471 controller()->submitFrame(m_displayId, m_framePose.Clone()); 511 if (!m_display)
512 return;
513
514 m_display->SubmitFrame(m_framePose.Clone());
472 m_canUpdateFramePose = true; 515 m_canUpdateFramePose = true;
473 } 516 }
474 517
518 void VRDisplay::OnDisplayChanged(
519 device::mojom::blink::VRDisplayInfoPtr display) {
520 update(display);
521 }
522
523 void VRDisplay::OnExitPresent() {
524 forceExitPresent();
525 }
526
527 void VRDisplay::onDisplayConnected() {
528 m_navigatorVR->fireVREvent(VRDisplayEvent::create(
529 EventTypeNames::vrdisplayconnect, true, false, this, "connect"));
530 }
531
532 void VRDisplay::onDisplayDisconnected() {
533 m_navigatorVR->fireVREvent(VRDisplayEvent::create(
534 EventTypeNames::vrdisplaydisconnect, true, false, this, "disconnect"));
535 }
536
475 void VRDisplay::onFullscreenCheck(TimerBase*) { 537 void VRDisplay::onFullscreenCheck(TimerBase*) {
476 // TODO: This is a temporary measure to track if fullscreen mode has been 538 // TODO: This is a temporary measure to track if fullscreen mode has been
477 // exited by the UA. If so we need to end VR presentation. Soon we won't 539 // exited by the UA. If so we need to end VR presentation. Soon we won't
478 // depend on the Fullscreen API to fake VR presentation, so this will 540 // depend on the Fullscreen API to fake VR presentation, so this will
479 // become unnessecary. Until that point, though, this seems preferable to 541 // become unnessecary. Until that point, though, this seems preferable to
480 // adding a bunch of notification plumbing to Fullscreen. 542 // adding a bunch of notification plumbing to Fullscreen.
481 if (!Fullscreen::isCurrentFullScreenElement(*m_layer.source())) { 543 if (!Fullscreen::isCurrentFullScreenElement(*m_layer.source())) {
482 m_isPresenting = false; 544 m_isPresenting = false;
483 m_navigatorVR->fireVRDisplayPresentChange(this); 545 m_navigatorVR->fireVRDisplayPresentChange(this);
484 m_fullscreenCheckTimer.stop(); 546 m_fullscreenCheckTimer.stop();
485 controller()->exitPresent(m_displayId); 547 if (!m_display)
548 return;
549 m_display->ExitPresent();
486 } 550 }
487 } 551 }
488 552
489 ScriptedAnimationController& VRDisplay::ensureScriptedAnimationController( 553 ScriptedAnimationController& VRDisplay::ensureScriptedAnimationController(
490 Document* doc) { 554 Document* doc) {
491 if (!m_scriptedAnimationController) 555 if (!m_scriptedAnimationController)
492 m_scriptedAnimationController = ScriptedAnimationController::create(doc); 556 m_scriptedAnimationController = ScriptedAnimationController::create(doc);
493 557
494 return *m_scriptedAnimationController; 558 return *m_scriptedAnimationController;
495 } 559 }
496 560
561 void VRDisplay::shutdownMessagePipe() {
562 m_binding.Close();
563 }
564
497 DEFINE_TRACE(VRDisplay) { 565 DEFINE_TRACE(VRDisplay) {
498 visitor->trace(m_navigatorVR); 566 visitor->trace(m_navigatorVR);
499 visitor->trace(m_capabilities); 567 visitor->trace(m_capabilities);
500 visitor->trace(m_stageParameters); 568 visitor->trace(m_stageParameters);
501 visitor->trace(m_eyeParametersLeft); 569 visitor->trace(m_eyeParametersLeft);
502 visitor->trace(m_eyeParametersRight); 570 visitor->trace(m_eyeParametersRight);
503 visitor->trace(m_layer); 571 visitor->trace(m_layer);
504 visitor->trace(m_renderingContext); 572 visitor->trace(m_renderingContext);
505 visitor->trace(m_scriptedAnimationController); 573 visitor->trace(m_scriptedAnimationController);
506 } 574 }
507 575
508 } // namespace blink 576 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/modules/vr/VRDisplay.h ('k') | third_party/WebKit/Source/modules/vr/VREyeParameters.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698