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

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

Issue 2420743003: mojo VR interface simplified (Closed)
Patch Set: address leon@ comments about name 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/Fullscreen.h" 8 #include "core/dom/Fullscreen.h"
9 #include "core/frame/UseCounter.h" 9 #include "core/frame/UseCounter.h"
10 #include "core/inspector/ConsoleMessage.h" 10 #include "core/inspector/ConsoleMessage.h"
(...skipping 18 matching lines...) Expand all
29 VREye stringToVREye(const String& whichEye) { 29 VREye stringToVREye(const String& whichEye) {
30 if (whichEye == "left") 30 if (whichEye == "left")
31 return VREyeLeft; 31 return VREyeLeft;
32 if (whichEye == "right") 32 if (whichEye == "right")
33 return VREyeRight; 33 return VREyeRight;
34 return VREyeNone; 34 return VREyeNone;
35 } 35 }
36 36
37 } // namespace 37 } // namespace
38 38
39 unsigned VRDisplay::nextId = 1;
40
39 VRDisplay::VRDisplay(NavigatorVR* navigatorVR) 41 VRDisplay::VRDisplay(NavigatorVR* navigatorVR)
40 : m_navigatorVR(navigatorVR), 42 : m_navigatorVR(navigatorVR),
41 m_displayId(0), 43 m_displayId(nextId),
bajones 2016/10/25 22:21:38 I know that there's been discussion about the fact
shaobo.yan 2016/10/26 01:19:22 Shall I take the chance to see if the gamepad part
42 m_isConnected(false), 44 m_isConnected(false),
43 m_isPresenting(false), 45 m_isPresenting(false),
44 m_canUpdateFramePose(true), 46 m_canUpdateFramePose(true),
45 m_capabilities(new VRDisplayCapabilities()), 47 m_capabilities(new VRDisplayCapabilities()),
46 m_eyeParametersLeft(new VREyeParameters()), 48 m_eyeParametersLeft(new VREyeParameters()),
47 m_eyeParametersRight(new VREyeParameters()), 49 m_eyeParametersRight(new VREyeParameters()),
48 m_depthNear(0.01), 50 m_depthNear(0.01),
49 m_depthFar(10000.0), 51 m_depthFar(10000.0),
50 m_fullscreenCheckTimer(this, &VRDisplay::onFullscreenCheck) {} 52 m_fullscreenCheckTimer(this, &VRDisplay::onFullscreenCheck),
53 m_binding(this) {
54 if (nextId != MAX)
55 ++nextId;
56 }
51 57
52 VRDisplay::~VRDisplay() {} 58 VRDisplay::~VRDisplay() {}
53 59
54 VRController* VRDisplay::controller() { 60 VRController* VRDisplay::controller() {
55 return m_navigatorVR->controller(); 61 return m_navigatorVR->controller();
56 } 62 }
57 63
58 void VRDisplay::update(const device::blink::VRDisplayPtr& display) { 64 void VRDisplay::update(const device::mojom::blink::VRDisplayPtr& display) {
59 m_displayId = display->index;
60 m_displayName = display->displayName; 65 m_displayName = display->displayName;
61 m_isConnected = true; 66 m_isConnected = true;
62 67
63 m_capabilities->setHasOrientation(display->capabilities->hasOrientation); 68 m_capabilities->setHasOrientation(display->capabilities->hasOrientation);
64 m_capabilities->setHasPosition(display->capabilities->hasPosition); 69 m_capabilities->setHasPosition(display->capabilities->hasPosition);
65 m_capabilities->setHasExternalDisplay( 70 m_capabilities->setHasExternalDisplay(
66 display->capabilities->hasExternalDisplay); 71 display->capabilities->hasExternalDisplay);
67 m_capabilities->setCanPresent(display->capabilities->canPresent); 72 m_capabilities->setCanPresent(display->capabilities->canPresent);
68 m_capabilities->setMaxLayers(display->capabilities->canPresent ? 1 : 0); 73 m_capabilities->setMaxLayers(display->capabilities->canPresent ? 1 : 0);
69 74
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 if (!m_framePose) 108 if (!m_framePose)
104 return nullptr; 109 return nullptr;
105 110
106 VRPose* pose = VRPose::create(); 111 VRPose* pose = VRPose::create();
107 pose->setPose(m_framePose); 112 pose->setPose(m_framePose);
108 return pose; 113 return pose;
109 } 114 }
110 115
111 void VRDisplay::updatePose() { 116 void VRDisplay::updatePose() {
112 if (m_canUpdateFramePose) { 117 if (m_canUpdateFramePose) {
113 m_framePose = controller()->getPose(m_displayId); 118 if (!m_device)
119 return;
120 device::mojom::blink::VRPosePtr pose;
121 m_device->GetPose(&pose);
122 m_framePose = std::move(pose);
114 if (m_isPresenting) 123 if (m_isPresenting)
115 m_canUpdateFramePose = false; 124 m_canUpdateFramePose = false;
116 } 125 }
117 } 126 }
118 127
119 void VRDisplay::resetPose() { 128 void VRDisplay::resetPose() {
120 controller()->resetPose(m_displayId); 129 if (!m_device)
130 return;
131
132 m_device->ResetPose();
121 } 133 }
122 134
123 VREyeParameters* VRDisplay::getEyeParameters(const String& whichEye) { 135 VREyeParameters* VRDisplay::getEyeParameters(const String& whichEye) {
124 switch (stringToVREye(whichEye)) { 136 switch (stringToVREye(whichEye)) {
125 case VREyeLeft: 137 case VREyeLeft:
126 return m_eyeParametersLeft; 138 return m_eyeParametersLeft;
127 case VREyeRight: 139 case VREyeRight:
128 return m_eyeParametersRight; 140 return m_eyeParametersRight;
129 default: 141 default:
130 return nullptr; 142 return nullptr;
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 Fullscreen::requestFullscreen(*m_layer.source(), 265 Fullscreen::requestFullscreen(*m_layer.source(),
254 Fullscreen::UnprefixedRequest); 266 Fullscreen::UnprefixedRequest);
255 267
256 // Check to see if the canvas is still the current fullscreen 268 // Check to see if the canvas is still the current fullscreen
257 // element once per second. 269 // element once per second.
258 m_fullscreenCheckTimer.startRepeating(1.0, BLINK_FROM_HERE); 270 m_fullscreenCheckTimer.startRepeating(1.0, BLINK_FROM_HERE);
259 } 271 }
260 272
261 if (firstPresent) { 273 if (firstPresent) {
262 bool secureContext = scriptState->getExecutionContext()->isSecureContext(); 274 bool secureContext = scriptState->getExecutionContext()->isSecureContext();
263 controller()->requestPresent(resolver, m_displayId, secureContext); 275 if (!m_device) {
276 DOMException* exception = DOMException::create(
277 InvalidStateError, "The service is no longer active.");
278 resolver->reject(exception);
279 return promise;
280 }
281 m_device->RequestPresent(
282 secureContext, convertToBaseCallback(WTF::bind(
283 &VRDisplay::onPresentComplete, wrapPersistent(this),
284 wrapPersistent(resolver))));
264 } else { 285 } else {
265 updateLayerBounds(); 286 updateLayerBounds();
266 resolver->resolve(); 287 resolver->resolve();
267 ReportPresentationResult(PresentationResult::SuccessAlreadyPresenting); 288 ReportPresentationResult(PresentationResult::SuccessAlreadyPresenting);
268 } 289 }
269 290
270 return promise; 291 return promise;
271 } 292 }
272 293
294 void VRDisplay::onPresentComplete(ScriptPromiseResolver* resolver,
295 bool success) {
296 if (success) {
297 this->beginPresent(resolver);
298 } else {
299 this->forceExitPresent();
300 DOMException* exception = DOMException::create(
301 NotAllowedError, "Presentation request was denied.");
302 resolver->reject(exception);
303 }
304 }
305
273 ScriptPromise VRDisplay::exitPresent(ScriptState* scriptState) { 306 ScriptPromise VRDisplay::exitPresent(ScriptState* scriptState) {
274 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 307 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
275 ScriptPromise promise = resolver->promise(); 308 ScriptPromise promise = resolver->promise();
276 309
277 if (!m_isPresenting) { 310 if (!m_isPresenting) {
278 // Can't stop presenting if we're not presenting. 311 // Can't stop presenting if we're not presenting.
279 DOMException* exception = 312 DOMException* exception =
280 DOMException::create(InvalidStateError, "VRDisplay is not presenting."); 313 DOMException::create(InvalidStateError, "VRDisplay is not presenting.");
281 resolver->reject(exception); 314 resolver->reject(exception);
282 return promise; 315 return promise;
283 } 316 }
284 317
285 controller()->exitPresent(m_displayId); 318 if (!m_device) {
319 DOMException* exception =
320 DOMException::create(InvalidStateError, "VRService is not available.");
321 resolver->reject(exception);
322 return promise;
323 }
324 m_device->ExitPresent();
286 325
287 resolver->resolve(); 326 resolver->resolve();
288 327
289 forceExitPresent(); 328 forceExitPresent();
290 329
291 return promise; 330 return promise;
292 } 331 }
293 332
294 void VRDisplay::beginPresent(ScriptPromiseResolver* resolver) { 333 void VRDisplay::beginPresent(ScriptPromiseResolver* resolver) {
295 if (m_capabilities->hasExternalDisplay()) { 334 if (m_capabilities->hasExternalDisplay()) {
(...skipping 27 matching lines...) Expand all
323 m_navigatorVR->fireVRDisplayPresentChange(this); 362 m_navigatorVR->fireVRDisplayPresentChange(this);
324 } 363 }
325 364
326 m_isPresenting = false; 365 m_isPresenting = false;
327 m_renderingContext = nullptr; 366 m_renderingContext = nullptr;
328 m_contextGL = nullptr; 367 m_contextGL = nullptr;
329 } 368 }
330 369
331 void VRDisplay::updateLayerBounds() { 370 void VRDisplay::updateLayerBounds() {
332 // Set up the texture bounds for the provided layer 371 // Set up the texture bounds for the provided layer
333 device::blink::VRLayerBoundsPtr leftBounds = 372 device::mojom::blink::VRLayerBoundsPtr leftBounds =
334 device::blink::VRLayerBounds::New(); 373 device::mojom::blink::VRLayerBounds::New();
335 device::blink::VRLayerBoundsPtr rightBounds = 374 device::mojom::blink::VRLayerBoundsPtr rightBounds =
336 device::blink::VRLayerBounds::New(); 375 device::mojom::blink::VRLayerBounds::New();
337 376
338 if (m_layer.leftBounds().size() == 4) { 377 if (m_layer.leftBounds().size() == 4) {
339 leftBounds->left = m_layer.leftBounds()[0]; 378 leftBounds->left = m_layer.leftBounds()[0];
340 leftBounds->top = m_layer.leftBounds()[1]; 379 leftBounds->top = m_layer.leftBounds()[1];
341 leftBounds->width = m_layer.leftBounds()[2]; 380 leftBounds->width = m_layer.leftBounds()[2];
342 leftBounds->height = m_layer.leftBounds()[3]; 381 leftBounds->height = m_layer.leftBounds()[3];
343 } else { 382 } else {
344 // Left eye defaults 383 // Left eye defaults
345 leftBounds->left = 0.0f; 384 leftBounds->left = 0.0f;
346 leftBounds->top = 0.0f; 385 leftBounds->top = 0.0f;
347 leftBounds->width = 0.5f; 386 leftBounds->width = 0.5f;
348 leftBounds->height = 1.0f; 387 leftBounds->height = 1.0f;
349 } 388 }
350 389
351 if (m_layer.rightBounds().size() == 4) { 390 if (m_layer.rightBounds().size() == 4) {
352 rightBounds->left = m_layer.rightBounds()[0]; 391 rightBounds->left = m_layer.rightBounds()[0];
353 rightBounds->top = m_layer.rightBounds()[1]; 392 rightBounds->top = m_layer.rightBounds()[1];
354 rightBounds->width = m_layer.rightBounds()[2]; 393 rightBounds->width = m_layer.rightBounds()[2];
355 rightBounds->height = m_layer.rightBounds()[3]; 394 rightBounds->height = m_layer.rightBounds()[3];
356 } else { 395 } else {
357 // Right eye defaults 396 // Right eye defaults
358 rightBounds->left = 0.5f; 397 rightBounds->left = 0.5f;
359 rightBounds->top = 0.0f; 398 rightBounds->top = 0.0f;
360 rightBounds->width = 0.5f; 399 rightBounds->width = 0.5f;
361 rightBounds->height = 1.0f; 400 rightBounds->height = 1.0f;
362 } 401 }
363 402
364 controller()->updateLayerBounds(m_displayId, std::move(leftBounds), 403 if (!m_device)
365 std::move(rightBounds)); 404 return;
405
406 m_device->UpdateLayerBounds(std::move(leftBounds), std::move(rightBounds));
366 } 407 }
367 408
368 HeapVector<VRLayer> VRDisplay::getLayers() { 409 HeapVector<VRLayer> VRDisplay::getLayers() {
369 HeapVector<VRLayer> layers; 410 HeapVector<VRLayer> layers;
370 411
371 if (m_isPresenting) { 412 if (m_isPresenting) {
372 layers.append(m_layer); 413 layers.append(m_layer);
373 } 414 }
374 415
375 return layers; 416 return layers;
(...skipping 26 matching lines...) Expand all
402 gl->ClearColor((idx & 255) / 255.0f, ((idx >> 8) & 255) / 255.0f, 443 gl->ClearColor((idx & 255) / 255.0f, ((idx >> 8) & 255) / 255.0f,
403 ((idx >> 16) & 255) / 255.0f, 1.0f); 444 ((idx >> 16) & 255) / 255.0f, 1.0f);
404 gl->Clear(GL_COLOR_BUFFER_BIT); 445 gl->Clear(GL_COLOR_BUFFER_BIT);
405 446
406 // Set the GL state back to what was set by the WebVR application. 447 // Set the GL state back to what was set by the WebVR application.
407 m_renderingContext->restoreScissorEnabled(); 448 m_renderingContext->restoreScissorEnabled();
408 m_renderingContext->restoreScissorBox(); 449 m_renderingContext->restoreScissorBox();
409 m_renderingContext->restoreColorMask(); 450 m_renderingContext->restoreColorMask();
410 m_renderingContext->restoreClearColor(); 451 m_renderingContext->restoreClearColor();
411 452
412 controller()->submitFrame(m_displayId, m_framePose.Clone()); 453 if (!m_device)
454 return;
455
456 m_device->SubmitFrame(m_framePose.Clone());
413 m_canUpdateFramePose = true; 457 m_canUpdateFramePose = true;
414 } 458 }
415 459
460 void VRDisplay::RegisterDevice(device::mojom::blink::VRDevicePtr client) {
461 if (!client) {
462 m_device = std::move(client);
463 }
464 }
465
466 void VRDisplay::UpdateDisplayInfo(device::mojom::blink::VRDisplayPtr display) {
467 if (!display.is_null()) {
468 update(display);
469 }
470 }
471
472 void VRDisplay::OnDisplayChanged(device::mojom::blink::VRDisplayPtr display) {
473 update(display);
474 }
475
476 void VRDisplay::OnExitPresent() {
477 forceExitPresent();
478 }
479
480 void VRDisplay::OnDisplayConnected(device::mojom::blink::VRDisplayPtr display) {
481 update(display);
482
483 m_navigatorVR->fireVREvent(VRDisplayEvent::create(
484 EventTypeNames::vrdisplayconnect, true, false, this, "connect"));
485 }
486
487 void VRDisplay::OnDisplayDisconnected() {
488 m_navigatorVR->fireVREvent(VRDisplayEvent::create(
489 EventTypeNames::vrdisplaydisconnect, true, false, this, "disconnect"));
490 }
491
416 void VRDisplay::onFullscreenCheck(TimerBase*) { 492 void VRDisplay::onFullscreenCheck(TimerBase*) {
417 // TODO: This is a temporary measure to track if fullscreen mode has been 493 // TODO: This is a temporary measure to track if fullscreen mode has been
418 // exited by the UA. If so we need to end VR presentation. Soon we won't 494 // exited by the UA. If so we need to end VR presentation. Soon we won't
419 // depend on the Fullscreen API to fake VR presentation, so this will 495 // depend on the Fullscreen API to fake VR presentation, so this will
420 // become unnessecary. Until that point, though, this seems preferable to 496 // become unnessecary. Until that point, though, this seems preferable to
421 // adding a bunch of notification plumbing to Fullscreen. 497 // adding a bunch of notification plumbing to Fullscreen.
422 if (!Fullscreen::isCurrentFullScreenElement(*m_layer.source())) { 498 if (!Fullscreen::isCurrentFullScreenElement(*m_layer.source())) {
423 m_isPresenting = false; 499 m_isPresenting = false;
424 m_navigatorVR->fireVRDisplayPresentChange(this); 500 m_navigatorVR->fireVRDisplayPresentChange(this);
425 m_fullscreenCheckTimer.stop(); 501 m_fullscreenCheckTimer.stop();
426 controller()->exitPresent(m_displayId); 502 if (!m_device)
503 return;
504 m_device->ExitPresent();
427 } 505 }
428 } 506 }
429 507
508 device::mojom::blink::VRDisplayClientPtr VRDisplay::BindClient() {
509 return m_binding.CreateInterfacePtrAndBind();
510 }
511
512 void VRDisplay::shutdownMessagePipe() {
513 m_binding.Close();
514 }
515
430 DEFINE_TRACE(VRDisplay) { 516 DEFINE_TRACE(VRDisplay) {
431 visitor->trace(m_navigatorVR); 517 visitor->trace(m_navigatorVR);
432 visitor->trace(m_capabilities); 518 visitor->trace(m_capabilities);
433 visitor->trace(m_stageParameters); 519 visitor->trace(m_stageParameters);
434 visitor->trace(m_eyeParametersLeft); 520 visitor->trace(m_eyeParametersLeft);
435 visitor->trace(m_eyeParametersRight); 521 visitor->trace(m_eyeParametersRight);
436 visitor->trace(m_layer); 522 visitor->trace(m_layer);
437 visitor->trace(m_renderingContext); 523 visitor->trace(m_renderingContext);
438 } 524 }
439 525
440 } // namespace blink 526 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698