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

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

Issue 2420743003: mojo VR interface simplified (Closed)
Patch Set: update binding process and update some unittest 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 17 matching lines...) Expand all
28 VREye stringToVREye(const String& whichEye) { 28 VREye stringToVREye(const String& whichEye) {
29 if (whichEye == "left") 29 if (whichEye == "left")
30 return VREyeLeft; 30 return VREyeLeft;
31 if (whichEye == "right") 31 if (whichEye == "right")
32 return VREyeRight; 32 return VREyeRight;
33 return VREyeNone; 33 return VREyeNone;
34 } 34 }
35 35
36 } // namespace 36 } // namespace
37 37
38 unsigned VRDisplay::nextId = 1;
39
38 VRDisplay::VRDisplay(NavigatorVR* navigatorVR) 40 VRDisplay::VRDisplay(NavigatorVR* navigatorVR)
39 : m_navigatorVR(navigatorVR), 41 : m_navigatorVR(navigatorVR),
40 m_displayId(0), 42 m_displayId(nextId),
41 m_isConnected(false), 43 m_isConnected(false),
42 m_isPresenting(false), 44 m_isPresenting(false),
43 m_canUpdateFramePose(true), 45 m_canUpdateFramePose(true),
44 m_capabilities(new VRDisplayCapabilities()), 46 m_capabilities(new VRDisplayCapabilities()),
45 m_eyeParametersLeft(new VREyeParameters()), 47 m_eyeParametersLeft(new VREyeParameters()),
46 m_eyeParametersRight(new VREyeParameters()), 48 m_eyeParametersRight(new VREyeParameters()),
47 m_depthNear(0.01), 49 m_depthNear(0.01),
48 m_depthFar(10000.0), 50 m_depthFar(10000.0),
49 m_fullscreenCheckTimer(this, &VRDisplay::onFullscreenCheck) {} 51 m_fullscreenCheckTimer(this, &VRDisplay::onFullscreenCheck),
52 m_binding(this) {
53 if (nextId != MAX)
54 ++nextId;
55 }
50 56
51 VRDisplay::~VRDisplay() {} 57 VRDisplay::~VRDisplay() {}
52 58
53 VRController* VRDisplay::controller() { 59 VRController* VRDisplay::controller() {
54 return m_navigatorVR->controller(); 60 return m_navigatorVR->controller();
55 } 61 }
56 62
57 void VRDisplay::update(const device::blink::VRDisplayPtr& display) { 63 void VRDisplay::update(const device::blink::VRDisplayPtr& display) {
58 m_displayId = display->index;
59 m_displayName = display->displayName; 64 m_displayName = display->displayName;
60 m_isConnected = true; 65 m_isConnected = true;
61 66
62 m_capabilities->setHasOrientation(display->capabilities->hasOrientation); 67 m_capabilities->setHasOrientation(display->capabilities->hasOrientation);
63 m_capabilities->setHasPosition(display->capabilities->hasPosition); 68 m_capabilities->setHasPosition(display->capabilities->hasPosition);
64 m_capabilities->setHasExternalDisplay( 69 m_capabilities->setHasExternalDisplay(
65 display->capabilities->hasExternalDisplay); 70 display->capabilities->hasExternalDisplay);
66 m_capabilities->setCanPresent(display->capabilities->canPresent); 71 m_capabilities->setCanPresent(display->capabilities->canPresent);
67 m_capabilities->setMaxLayers(display->capabilities->canPresent ? 1 : 0); 72 m_capabilities->setMaxLayers(display->capabilities->canPresent ? 1 : 0);
68 73
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
102 if (!m_framePose) 107 if (!m_framePose)
103 return nullptr; 108 return nullptr;
104 109
105 VRPose* pose = VRPose::create(); 110 VRPose* pose = VRPose::create();
106 pose->setPose(m_framePose); 111 pose->setPose(m_framePose);
107 return pose; 112 return pose;
108 } 113 }
109 114
110 void VRDisplay::updatePose() { 115 void VRDisplay::updatePose() {
111 if (m_canUpdateFramePose) { 116 if (m_canUpdateFramePose) {
112 m_framePose = controller()->getPose(m_displayId); 117 if (!m_client)
118 return;
119 device::blink::VRPosePtr pose;
120 m_client->GetPose(&pose);
121 m_framePose = std::move(pose);
113 if (m_isPresenting) 122 if (m_isPresenting)
114 m_canUpdateFramePose = false; 123 m_canUpdateFramePose = false;
115 } 124 }
116 } 125 }
117 126
118 void VRDisplay::resetPose() { 127 void VRDisplay::resetPose() {
119 controller()->resetPose(m_displayId); 128 if (!m_client)
129 return;
130
131 m_client->ResetPose();
120 } 132 }
121 133
122 VREyeParameters* VRDisplay::getEyeParameters(const String& whichEye) { 134 VREyeParameters* VRDisplay::getEyeParameters(const String& whichEye) {
123 switch (stringToVREye(whichEye)) { 135 switch (stringToVREye(whichEye)) {
124 case VREyeLeft: 136 case VREyeLeft:
125 return m_eyeParametersLeft; 137 return m_eyeParametersLeft;
126 case VREyeRight: 138 case VREyeRight:
127 return m_eyeParametersRight; 139 return m_eyeParametersRight;
128 default: 140 default:
129 return nullptr; 141 return nullptr;
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 Fullscreen::requestFullscreen(*m_layer.source(), 244 Fullscreen::requestFullscreen(*m_layer.source(),
233 Fullscreen::UnprefixedRequest); 245 Fullscreen::UnprefixedRequest);
234 246
235 // Check to see if the canvas is still the current fullscreen 247 // Check to see if the canvas is still the current fullscreen
236 // element once per second. 248 // element once per second.
237 m_fullscreenCheckTimer.startRepeating(1.0, BLINK_FROM_HERE); 249 m_fullscreenCheckTimer.startRepeating(1.0, BLINK_FROM_HERE);
238 } 250 }
239 251
240 if (firstPresent) { 252 if (firstPresent) {
241 bool secureContext = scriptState->getExecutionContext()->isSecureContext(); 253 bool secureContext = scriptState->getExecutionContext()->isSecureContext();
242 controller()->requestPresent(resolver, m_displayId, secureContext); 254 if (!m_client) {
255 DOMException* exception = DOMException::create(
256 InvalidStateError, "The service is no longer active.");
257 resolver->reject(exception);
258 return promise;
259 }
260 m_client->RequestPresent(
261 secureContext, convertToBaseCallback(WTF::bind(
262 &VRDisplay::onPresentComplete, wrapPersistent(this),
263 wrapPersistent(resolver))));
243 } else { 264 } else {
244 updateLayerBounds(); 265 updateLayerBounds();
245 resolver->resolve(); 266 resolver->resolve();
246 } 267 }
247 268
248 return promise; 269 return promise;
249 } 270 }
250 271
272 void VRDisplay::onPresentComplete(ScriptPromiseResolver* resolver,
273 bool success) {
274 if (success) {
275 this->beginPresent(resolver);
276 } else {
277 this->forceExitPresent();
278 DOMException* exception = DOMException::create(
279 NotAllowedError, "Presentation request was denied.");
280 resolver->reject(exception);
281 }
282 }
283
251 ScriptPromise VRDisplay::exitPresent(ScriptState* scriptState) { 284 ScriptPromise VRDisplay::exitPresent(ScriptState* scriptState) {
252 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); 285 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState);
253 ScriptPromise promise = resolver->promise(); 286 ScriptPromise promise = resolver->promise();
254 287
255 if (!m_isPresenting) { 288 if (!m_isPresenting) {
256 // Can't stop presenting if we're not presenting. 289 // Can't stop presenting if we're not presenting.
257 DOMException* exception = 290 DOMException* exception =
258 DOMException::create(InvalidStateError, "VRDisplay is not presenting."); 291 DOMException::create(InvalidStateError, "VRDisplay is not presenting.");
259 resolver->reject(exception); 292 resolver->reject(exception);
260 return promise; 293 return promise;
261 } 294 }
262 295
263 controller()->exitPresent(m_displayId); 296 if (!m_client) {
297 DOMException* exception =
298 DOMException::create(InvalidStateError, "VRService is not available.");
299 resolver->reject(exception);
300 return promise;
301 }
302 m_client->ExitPresent();
264 303
265 resolver->resolve(); 304 resolver->resolve();
266 305
267 forceExitPresent(); 306 forceExitPresent();
268 307
269 return promise; 308 return promise;
270 } 309 }
271 310
272 void VRDisplay::beginPresent(ScriptPromiseResolver* resolver) { 311 void VRDisplay::beginPresent(ScriptPromiseResolver* resolver) {
273 if (m_capabilities->hasExternalDisplay()) { 312 if (m_capabilities->hasExternalDisplay()) {
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 rightBounds->width = m_layer.rightBounds()[2]; 372 rightBounds->width = m_layer.rightBounds()[2];
334 rightBounds->height = m_layer.rightBounds()[3]; 373 rightBounds->height = m_layer.rightBounds()[3];
335 } else { 374 } else {
336 // Right eye defaults 375 // Right eye defaults
337 rightBounds->left = 0.5f; 376 rightBounds->left = 0.5f;
338 rightBounds->top = 0.0f; 377 rightBounds->top = 0.0f;
339 rightBounds->width = 0.5f; 378 rightBounds->width = 0.5f;
340 rightBounds->height = 1.0f; 379 rightBounds->height = 1.0f;
341 } 380 }
342 381
343 controller()->updateLayerBounds(m_displayId, std::move(leftBounds), 382 if (!m_client)
344 std::move(rightBounds)); 383 return;
384
385 m_client->UpdateLayerBounds(std::move(leftBounds), std::move(rightBounds));
345 } 386 }
346 387
347 HeapVector<VRLayer> VRDisplay::getLayers() { 388 HeapVector<VRLayer> VRDisplay::getLayers() {
348 HeapVector<VRLayer> layers; 389 HeapVector<VRLayer> layers;
349 390
350 if (m_isPresenting) { 391 if (m_isPresenting) {
351 layers.append(m_layer); 392 layers.append(m_layer);
352 } 393 }
353 394
354 return layers; 395 return layers;
(...skipping 26 matching lines...) Expand all
381 gl->ClearColor((idx & 255) / 255.0f, ((idx >> 8) & 255) / 255.0f, 422 gl->ClearColor((idx & 255) / 255.0f, ((idx >> 8) & 255) / 255.0f,
382 ((idx >> 16) & 255) / 255.0f, 1.0f); 423 ((idx >> 16) & 255) / 255.0f, 1.0f);
383 gl->Clear(GL_COLOR_BUFFER_BIT); 424 gl->Clear(GL_COLOR_BUFFER_BIT);
384 425
385 // Set the GL state back to what was set by the WebVR application. 426 // Set the GL state back to what was set by the WebVR application.
386 m_renderingContext->restoreScissorEnabled(); 427 m_renderingContext->restoreScissorEnabled();
387 m_renderingContext->restoreScissorBox(); 428 m_renderingContext->restoreScissorBox();
388 m_renderingContext->restoreColorMask(); 429 m_renderingContext->restoreColorMask();
389 m_renderingContext->restoreClearColor(); 430 m_renderingContext->restoreClearColor();
390 431
391 controller()->submitFrame(m_displayId, m_framePose.Clone()); 432 if (!m_client)
433 return;
434
435 m_client->SubmitFrame(m_framePose.Clone());
392 m_canUpdateFramePose = true; 436 m_canUpdateFramePose = true;
393 } 437 }
394 438
439 void VRDisplay::RegisterDeviceClient(device::blink::VRDeviceClientPtr client) {
440 if (!client) {
441 m_client = std::move(client);
442 }
443 }
444
445 void VRDisplay::UpdateDisplayInfo(device::blink::VRDisplayPtr display) {
446 if (!display.is_null()) {
447 update(display);
448 }
449 }
450
451 void VRDisplay::OnDisplayChanged(device::blink::VRDisplayPtr display) {
452 update(display);
453 }
454
455 void VRDisplay::OnExitPresent() {
456 forceExitPresent();
457 }
458
459 void VRDisplay::OnDisplayConnected(device::blink::VRDisplayPtr display) {
460 update(display);
461
462 m_navigatorVR->fireVREvent(VRDisplayEvent::create(
463 EventTypeNames::vrdisplayconnect, true, false, this, "connect"));
464 }
465
466 void VRDisplay::OnDisplayDisconnected() {
467 m_navigatorVR->fireVREvent(VRDisplayEvent::create(
468 EventTypeNames::vrdisplaydisconnect, true, false, this, "disconnect"));
469 }
470
395 void VRDisplay::onFullscreenCheck(TimerBase*) { 471 void VRDisplay::onFullscreenCheck(TimerBase*) {
396 // TODO: This is a temporary measure to track if fullscreen mode has been 472 // TODO: This is a temporary measure to track if fullscreen mode has been
397 // exited by the UA. If so we need to end VR presentation. Soon we won't 473 // exited by the UA. If so we need to end VR presentation. Soon we won't
398 // depend on the Fullscreen API to fake VR presentation, so this will 474 // depend on the Fullscreen API to fake VR presentation, so this will
399 // become unnessecary. Until that point, though, this seems preferable to 475 // become unnessecary. Until that point, though, this seems preferable to
400 // adding a bunch of notification plumbing to Fullscreen. 476 // adding a bunch of notification plumbing to Fullscreen.
401 if (!Fullscreen::isCurrentFullScreenElement(*m_layer.source())) { 477 if (!Fullscreen::isCurrentFullScreenElement(*m_layer.source())) {
402 m_isPresenting = false; 478 m_isPresenting = false;
403 m_navigatorVR->fireVRDisplayPresentChange(this); 479 m_navigatorVR->fireVRDisplayPresentChange(this);
404 m_fullscreenCheckTimer.stop(); 480 m_fullscreenCheckTimer.stop();
405 controller()->exitPresent(m_displayId); 481 if (!m_client)
482 return;
483 m_client->ExitPresent();
406 } 484 }
407 } 485 }
408 486
487 device::blink::VRDisplayClientPtr VRDisplay::BindClient() {
488 return std::move(m_binding.CreateInterfacePtrAndBind());
489 }
490
491 void VRDisplay::shutdownMessagePipe() {
492 m_binding.Close();
493 }
494
409 DEFINE_TRACE(VRDisplay) { 495 DEFINE_TRACE(VRDisplay) {
410 visitor->trace(m_navigatorVR); 496 visitor->trace(m_navigatorVR);
411 visitor->trace(m_capabilities); 497 visitor->trace(m_capabilities);
412 visitor->trace(m_stageParameters); 498 visitor->trace(m_stageParameters);
413 visitor->trace(m_eyeParametersLeft); 499 visitor->trace(m_eyeParametersLeft);
414 visitor->trace(m_eyeParametersRight); 500 visitor->trace(m_eyeParametersRight);
415 visitor->trace(m_layer); 501 visitor->trace(m_layer);
416 visitor->trace(m_renderingContext); 502 visitor->trace(m_renderingContext);
417 } 503 }
418 504
419 } // namespace blink 505 } // namespace blink
OLDNEW
« device/vr/vr_service.mojom ('K') | « third_party/WebKit/Source/modules/vr/VRDisplay.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698