Title: [233846] trunk/Source/WebCore
Revision
233846
Author
[email protected]
Date
2018-07-16 07:24:22 -0700 (Mon, 16 Jul 2018)

Log Message

[WebVR] Add support for connect/disconnect and mount/unmount device events
https://bugs.webkit.org/show_bug.cgi?id=187343

Reviewed by Žan Doberšek.

WebVR specs define a series of events as part of the Window Interface Extension. We're
adding support for the connect/disconnect and mount/unmount events both at the module level
and the platform level using OpenVR.

In order to do that we need to keep lists of VRPlatformDisplays at platform level and
VRDisplays at bindings level. We then update those lists accordingly to detect potential
additions/removals, and emit the corresponding signals. A new client interface
VRPlatformDisplayClient was also defined so that VRPlatformDisplay implementations could
notify their clients (typically a VRDisplay).

Last but not least, NavigatorWebVR was updated so it supplements Navigator instead of
supplementing Page.

* Modules/webvr/NavigatorWebVR.cpp: Supplement Navigator not Page.
(WebCore::NavigatorWebVR::getVRDisplays): Keep a list of VRDisplays and update them
conveniently, also emitting the required events under certain conditions (like device
disconnection).
(WebCore::NavigatorWebVR::supplementName): New method.
(WebCore::NavigatorWebVR::from): Ditto.
* Modules/webvr/NavigatorWebVR.h: Supplement Navigator not Page.
* Modules/webvr/VRDisplay.cpp:
(WebCore::VRDisplay::create): Moved suspendIfNeeded() to constructor.
(WebCore::VRDisplay::VRDisplay): Set itself as VRPlatformDisplay client.
(WebCore::VRDisplay::~VRDisplay): Unset as  VRPlatformDisplay client.
(WebCore::VRDisplay::VRPlatformDisplayConnected): Dispatch event on DOM window.
(WebCore::VRDisplay::VRPlatformDisplayDisconnected): Ditto.
(WebCore::VRDisplay::VRPlatformDisplayMounted): Ditto.
(WebCore::VRDisplay::VRPlatformDisplayUnmounted): Ditto.
* Modules/webvr/VRDisplay.h: Extend from VRPlatformDisplayClient.
(WebCore::VRDisplay::document):
* Modules/webvr/VRDisplayEvent.cpp: Updated Copyright.
* Modules/webvr/VRDisplayEvent.h: Ditto.
* Sources.txt: Added the two new files.
* WebCore.xcodeproj/project.pbxproj: Ditto.
* platform/vr/VRManager.cpp:
(WebCore::VRManager::getVRDisplays): Keep a list of VRPlatformDisplays and update them conveniently,
also emitting the required events under certain conditions (like device disconnection).
* platform/vr/VRManager.h:
* platform/vr/VRPlatformDisplay.cpp: New file with common implementations for VRPlatformDisplays.
(WebCore::VRPlatformDisplay::setClient):
(WebCore::VRPlatformDisplay::notifyVRPlatformDisplayEvent):
* platform/vr/VRPlatformDisplay.h: Added a generic method to notify about different
events. Added the client pointer.
* platform/vr/VRPlatformDisplayClient.h: New file. VRPlatformDisplay implementations will
call the client methods in the event of some circumstances happening.
(WebCore::VRPlatformDisplayClient::VRPlatformDisplayConnected):
(WebCore::VRPlatformDisplayClient::VRPlatformDisplayDisconnected):
(WebCore::VRPlatformDisplayClient::VRPlatformDisplayMounted):
(WebCore::VRPlatformDisplayClient::VRPlatformDisplayUnmounted):
* platform/vr/openvr/VRPlatformDisplayOpenVR.cpp:
(WebCore::VRPlatformDisplayOpenVR::updateDisplayInfo): Poll the device for new events to
detect connection/disconnections or device activations/deactivations (HMD
mounted/unmounted).
* platform/vr/openvr/VRPlatformDisplayOpenVR.h:

Modified Paths

Added Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (233845 => 233846)


--- trunk/Source/WebCore/ChangeLog	2018-07-16 12:24:08 UTC (rev 233845)
+++ trunk/Source/WebCore/ChangeLog	2018-07-16 14:24:22 UTC (rev 233846)
@@ -1,3 +1,65 @@
+2018-07-16  Sergio Villar Senin  <[email protected]>
+
+        [WebVR] Add support for connect/disconnect and mount/unmount device events
+        https://bugs.webkit.org/show_bug.cgi?id=187343
+
+        Reviewed by Žan Doberšek.
+
+        WebVR specs define a series of events as part of the Window Interface Extension. We're
+        adding support for the connect/disconnect and mount/unmount events both at the module level
+        and the platform level using OpenVR.
+
+        In order to do that we need to keep lists of VRPlatformDisplays at platform level and
+        VRDisplays at bindings level. We then update those lists accordingly to detect potential
+        additions/removals, and emit the corresponding signals. A new client interface
+        VRPlatformDisplayClient was also defined so that VRPlatformDisplay implementations could
+        notify their clients (typically a VRDisplay).
+
+        Last but not least, NavigatorWebVR was updated so it supplements Navigator instead of
+        supplementing Page.
+
+        * Modules/webvr/NavigatorWebVR.cpp: Supplement Navigator not Page.
+        (WebCore::NavigatorWebVR::getVRDisplays): Keep a list of VRDisplays and update them
+        conveniently, also emitting the required events under certain conditions (like device
+        disconnection).
+        (WebCore::NavigatorWebVR::supplementName): New method.
+        (WebCore::NavigatorWebVR::from): Ditto.
+        * Modules/webvr/NavigatorWebVR.h: Supplement Navigator not Page.
+        * Modules/webvr/VRDisplay.cpp:
+        (WebCore::VRDisplay::create): Moved suspendIfNeeded() to constructor.
+        (WebCore::VRDisplay::VRDisplay): Set itself as VRPlatformDisplay client.
+        (WebCore::VRDisplay::~VRDisplay): Unset as  VRPlatformDisplay client.
+        (WebCore::VRDisplay::VRPlatformDisplayConnected): Dispatch event on DOM window.
+        (WebCore::VRDisplay::VRPlatformDisplayDisconnected): Ditto.
+        (WebCore::VRDisplay::VRPlatformDisplayMounted): Ditto.
+        (WebCore::VRDisplay::VRPlatformDisplayUnmounted): Ditto.
+        * Modules/webvr/VRDisplay.h: Extend from VRPlatformDisplayClient.
+        (WebCore::VRDisplay::document):
+        * Modules/webvr/VRDisplayEvent.cpp: Updated Copyright.
+        * Modules/webvr/VRDisplayEvent.h: Ditto.
+        * Sources.txt: Added the two new files.
+        * WebCore.xcodeproj/project.pbxproj: Ditto.
+        * platform/vr/VRManager.cpp:
+        (WebCore::VRManager::getVRDisplays): Keep a list of VRPlatformDisplays and update them conveniently,
+        also emitting the required events under certain conditions (like device disconnection).
+        * platform/vr/VRManager.h:
+        * platform/vr/VRPlatformDisplay.cpp: New file with common implementations for VRPlatformDisplays.
+        (WebCore::VRPlatformDisplay::setClient):
+        (WebCore::VRPlatformDisplay::notifyVRPlatformDisplayEvent):
+        * platform/vr/VRPlatformDisplay.h: Added a generic method to notify about different
+        events. Added the client pointer.
+        * platform/vr/VRPlatformDisplayClient.h: New file. VRPlatformDisplay implementations will
+        call the client methods in the event of some circumstances happening.
+        (WebCore::VRPlatformDisplayClient::VRPlatformDisplayConnected):
+        (WebCore::VRPlatformDisplayClient::VRPlatformDisplayDisconnected):
+        (WebCore::VRPlatformDisplayClient::VRPlatformDisplayMounted):
+        (WebCore::VRPlatformDisplayClient::VRPlatformDisplayUnmounted):
+        * platform/vr/openvr/VRPlatformDisplayOpenVR.cpp:
+        (WebCore::VRPlatformDisplayOpenVR::updateDisplayInfo): Poll the device for new events to
+        detect connection/disconnections or device activations/deactivations (HMD
+        mounted/unmounted).
+        * platform/vr/openvr/VRPlatformDisplayOpenVR.h:
+
 2018-07-16  Zan Dobersek  <[email protected]>
 
         [Nicosia] Add Nicosia::PlatformLayer, Nicosia::CompositionLayer classes

Modified: trunk/Source/WebCore/Modules/webvr/NavigatorWebVR.cpp (233845 => 233846)


--- trunk/Source/WebCore/Modules/webvr/NavigatorWebVR.cpp	2018-07-16 12:24:08 UTC (rev 233845)
+++ trunk/Source/WebCore/Modules/webvr/NavigatorWebVR.cpp	2018-07-16 14:24:22 UTC (rev 233846)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 Igalia S.L. All rights reserved.
+ * Copyright (C) 2017-2018 Igalia S.L. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -32,27 +32,50 @@
 #include "RuntimeEnabledFeatures.h"
 #include "VRDisplay.h"
 #include "VRManager.h"
+#include "VRPlatformDisplay.h"
 
 namespace WebCore {
 
-void NavigatorWebVR::getVRDisplays(Navigator&, Document& document, GetVRDisplaysPromise&& promise)
+void NavigatorWebVR::getVRDisplays(Navigator& navigator, Document& document, GetVRDisplaysPromise&& promise)
 {
     if (!RuntimeEnabledFeatures::sharedFeatures().webVREnabled()) {
         promise.reject();
         return;
     }
+    NavigatorWebVR::from(&navigator)->getVRDisplays(document, WTFMove(promise));
+}
 
-    document.postTask([promise = WTFMove(promise)] (ScriptExecutionContext& context) mutable {
+void NavigatorWebVR::getVRDisplays(Document& document, GetVRDisplaysPromise&& promise)
+{
+    document.postTask([this, promise = WTFMove(promise)] (ScriptExecutionContext& context) mutable {
         std::optional<VRManager::VRDisplaysVector> platformDisplays = VRManager::singleton().getVRDisplays();
         if (!platformDisplays) {
             promise.reject();
+            m_displays.clear();
             return;
         }
 
-        Vector<Ref<VRDisplay>> displays;
-        for (auto& platformDisplay : platformDisplays.value())
-            displays.append(VRDisplay::create(context, WTFMove(platformDisplay)));
-        promise.resolve(displays);
+        Vector<Ref<VRDisplay>> oldDisplays = WTFMove(m_displays);
+
+        // Update the vector of displays. Note that although this is O(n^2) the amount of expected
+        // VRDisplays is so reduced that it'll run fast enough.
+        for (auto& platformDisplay : platformDisplays.value()) {
+            bool newDisplay = true;
+            for (auto& oldDisplay : oldDisplays) {
+                if (platformDisplay->getDisplayInfo().displayIdentifier() == oldDisplay->displayId()) {
+                    newDisplay = false;
+                    m_displays.append(oldDisplay.copyRef());
+                    break;
+                }
+            }
+            if (newDisplay) {
+                m_displays.append(VRDisplay::create(context, WTFMove(platformDisplay)));
+                m_displays.last()->platformDisplayConnected();
+            }
+        }
+        promise.resolve(WTF::map(m_displays, [](const Ref<VRDisplay>& display) {
+            return display.copyRef();
+        }));
     });
 }
 
@@ -62,4 +85,20 @@
     return mockVector;
 }
 
+const char* NavigatorWebVR::supplementName()
+{
+    return "NavigatorWebVR";
+}
+
+NavigatorWebVR* NavigatorWebVR::from(Navigator* navigator)
+{
+    NavigatorWebVR* supplement = static_cast<NavigatorWebVR*>(Supplement<Navigator>::from(navigator, supplementName()));
+    if (!supplement) {
+        auto newSupplement = std::make_unique<NavigatorWebVR>();
+        supplement = newSupplement.get();
+        provideTo(navigator, supplementName(), WTFMove(newSupplement));
+    }
+    return supplement;
+}
+
 } // namespace WebCore

Modified: trunk/Source/WebCore/Modules/webvr/NavigatorWebVR.h (233845 => 233846)


--- trunk/Source/WebCore/Modules/webvr/NavigatorWebVR.h	2018-07-16 12:24:08 UTC (rev 233845)
+++ trunk/Source/WebCore/Modules/webvr/NavigatorWebVR.h	2018-07-16 14:24:22 UTC (rev 233846)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 Igalia S.L. All rights reserved.
+ * Copyright (C) 2017-2018 Igalia S.L. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -36,7 +36,7 @@
 class Page;
 class VRDisplay;
 
-class NavigatorWebVR final : public Supplement<Page> {
+class NavigatorWebVR final : public Supplement<Navigator> {
 public:
     using GetVRDisplaysPromise = DOMPromiseDeferred<IDLSequence<IDLInterface<VRDisplay>>>;
 
@@ -43,6 +43,14 @@
     static void getVRDisplays(Navigator&, Document&, GetVRDisplaysPromise&&);
     static const Vector<RefPtr<VRDisplay>>& activeVRDisplays(Navigator&);
     static bool vrEnabled(Navigator&);
+
+    void getVRDisplays(Document&, GetVRDisplaysPromise&&);
+
+private:
+    static NavigatorWebVR* from(Navigator*);
+    static const char* supplementName();
+
+    Vector<Ref<VRDisplay>> m_displays;
 };
 
 } // namespace WebCore

Modified: trunk/Source/WebCore/Modules/webvr/VRDisplay.cpp (233845 => 233846)


--- trunk/Source/WebCore/Modules/webvr/VRDisplay.cpp	2018-07-16 12:24:08 UTC (rev 233845)
+++ trunk/Source/WebCore/Modules/webvr/VRDisplay.cpp	2018-07-16 14:24:22 UTC (rev 233846)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 Igalia S.L. All rights reserved.
+ * Copyright (C) 2017-2018 Igalia S.L. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -29,10 +29,12 @@
 #include "CanvasRenderingContext.h"
 #include "Chrome.h"
 #include "DOMException.h"
+#include "EventNames.h"
 #include "Page.h"
 #include "ScriptedAnimationController.h"
 #include "UserGestureIndicator.h"
 #include "VRDisplayCapabilities.h"
+#include "VRDisplayEvent.h"
 #include "VREyeParameters.h"
 #include "VRFrameData.h"
 #include "VRLayerInit.h"
@@ -44,9 +46,7 @@
 
 Ref<VRDisplay> VRDisplay::create(ScriptExecutionContext& context, WeakPtr<VRPlatformDisplay>&& platformDisplay)
 {
-    auto display = adoptRef(*new VRDisplay(context, WTFMove(platformDisplay)));
-    display->suspendIfNeeded();
-    return display;
+    return adoptRef(*new VRDisplay(context, WTFMove(platformDisplay)));
 }
 
 VRDisplay::VRDisplay(ScriptExecutionContext& context, WeakPtr<VRPlatformDisplay>&& platformDisplay)
@@ -59,9 +59,15 @@
     m_rightEyeParameters = VREyeParameters::create(displayInfo.eyeTranslation(VRPlatformDisplayInfo::EyeRight), displayInfo.eyeFieldOfView(VRPlatformDisplayInfo::EyeRight), displayInfo.renderSize());
     m_displayId = displayInfo.displayIdentifier();
     m_displayName = displayInfo.displayName();
+
+    m_display->setClient(this);
+    suspendIfNeeded();
 }
 
-VRDisplay::~VRDisplay() = default;
+VRDisplay::~VRDisplay()
+{
+    m_display->setClient(nullptr);
+}
 
 bool VRDisplay::isConnected() const
 {
@@ -198,6 +204,26 @@
 {
 }
 
+void VRDisplay::platformDisplayConnected()
+{
+    document()->domWindow()->dispatchEvent(VRDisplayEvent::create(eventNames().vrdisplayconnectEvent, makeRefPtr(this), std::nullopt));
+}
+
+void VRDisplay::platformDisplayDisconnected()
+{
+    document()->domWindow()->dispatchEvent(VRDisplayEvent::create(eventNames().vrdisplaydisconnectEvent, makeRefPtr(this), std::nullopt));
+}
+
+void VRDisplay::platformDisplayMounted()
+{
+    document()->domWindow()->dispatchEvent(VRDisplayEvent::create(eventNames().vrdisplayactivateEvent, makeRefPtr(this), VRDisplayEventReason::Mounted));
+}
+
+void VRDisplay::platformDisplayUnmounted()
+{
+    document()->domWindow()->dispatchEvent(VRDisplayEvent::create(eventNames().vrdisplaydeactivateEvent, makeRefPtr(this), VRDisplayEventReason::Unmounted));
+}
+
 bool VRDisplay::hasPendingActivity() const
 {
     return false;

Modified: trunk/Source/WebCore/Modules/webvr/VRDisplay.h (233845 => 233846)


--- trunk/Source/WebCore/Modules/webvr/VRDisplay.h	2018-07-16 12:24:08 UTC (rev 233845)
+++ trunk/Source/WebCore/Modules/webvr/VRDisplay.h	2018-07-16 14:24:22 UTC (rev 233846)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 Igalia S.L. All rights reserved.
+ * Copyright (C) 2017-2018 Igalia S.L. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -30,6 +30,7 @@
 #include "JSDOMPromiseDeferred.h"
 #include "VREye.h"
 #include "VRLayerInit.h"
+#include "VRPlatformDisplayClient.h"
 #include <wtf/RefCounted.h>
 
 namespace WebCore {
@@ -44,7 +45,7 @@
 class VRPose;
 class VRStageParameters;
 
-class VRDisplay : public RefCounted<VRDisplay>, public EventTargetWithInlineData, public ActiveDOMObject {
+class VRDisplay : public RefCounted<VRDisplay>, public VRPlatformDisplayClient, public EventTargetWithInlineData, public ActiveDOMObject {
 public:
     static Ref<VRDisplay> create(ScriptExecutionContext&, WeakPtr<VRPlatformDisplay>&&);
 
@@ -84,6 +85,12 @@
 
     void submitFrame();
 
+    // VRPlatformDisplayClient
+    void platformDisplayConnected() override;
+    void platformDisplayDisconnected() override;
+    void platformDisplayMounted() override;
+    void platformDisplayUnmounted() override;
+
 private:
     VRDisplay(ScriptExecutionContext&, WeakPtr<VRPlatformDisplay>&&);
 
@@ -101,6 +108,8 @@
 
     void stopPresenting();
 
+    Document* document() { return downcast<Document>(scriptExecutionContext()); }
+
     WeakPtr<VRPlatformDisplay> m_display;
 
     RefPtr<VRDisplayCapabilities> m_capabilities;

Modified: trunk/Source/WebCore/Modules/webvr/VRDisplayEvent.cpp (233845 => 233846)


--- trunk/Source/WebCore/Modules/webvr/VRDisplayEvent.cpp	2018-07-16 12:24:08 UTC (rev 233845)
+++ trunk/Source/WebCore/Modules/webvr/VRDisplayEvent.cpp	2018-07-16 14:24:22 UTC (rev 233846)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 Igalia S.L. All rights reserved.
+ * Copyright (C) 2017-2018 Igalia S.L. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions

Modified: trunk/Source/WebCore/Modules/webvr/VRDisplayEvent.h (233845 => 233846)


--- trunk/Source/WebCore/Modules/webvr/VRDisplayEvent.h	2018-07-16 12:24:08 UTC (rev 233845)
+++ trunk/Source/WebCore/Modules/webvr/VRDisplayEvent.h	2018-07-16 14:24:22 UTC (rev 233846)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 Igalia S.L. All rights reserved.
+ * Copyright (C) 2017-2018 Igalia S.L. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions

Modified: trunk/Source/WebCore/Sources.txt (233845 => 233846)


--- trunk/Source/WebCore/Sources.txt	2018-07-16 12:24:08 UTC (rev 233845)
+++ trunk/Source/WebCore/Sources.txt	2018-07-16 14:24:22 UTC (rev 233846)
@@ -1808,6 +1808,7 @@
 platform/text/TextEncodingRegistry.cpp
 
 platform/vr/VRManager.cpp
+platform/vr/VRPlatformDisplay.cpp
 platform/vr/openvr/VRPlatformDisplayOpenVR.cpp
 platform/vr/openvr/VRPlatformManagerOpenVR.cpp
 

Modified: trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj (233845 => 233846)


--- trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2018-07-16 12:24:08 UTC (rev 233845)
+++ trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj	2018-07-16 14:24:22 UTC (rev 233846)
@@ -2501,6 +2501,7 @@
 		8AF4E55C11DC5A63000ED3DE /* PerformanceTiming.h in Headers */ = {isa = PBXBuildFile; fileRef = 8AF4E55911DC5A63000ED3DE /* PerformanceTiming.h */; };
 		8BD37A68201BB39C0011734A /* ReadableStreamChunk.h in Headers */ = {isa = PBXBuildFile; fileRef = 8BD37A67201BB39C0011734A /* ReadableStreamChunk.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		8E4C96DD1AD4483500365A50 /* JSFetchResponse.h in Headers */ = {isa = PBXBuildFile; fileRef = 8E4C96D91AD4483500365A50 /* JSFetchResponse.h */; };
+		8E620E8120EE6024007BC5EF /* VRPlatformDisplayClient.h in Headers */ = {isa = PBXBuildFile; fileRef = 8E620E8020EE5FFC007BC5EF /* VRPlatformDisplayClient.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		8EC6C963201A251600FBFA53 /* GapLength.h in Headers */ = {isa = PBXBuildFile; fileRef = 8EC6C961201A250100FBFA53 /* GapLength.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		8F67561B1288B17B0047ACA3 /* EventQueue.h in Headers */ = {isa = PBXBuildFile; fileRef = 8F6756191288B17B0047ACA3 /* EventQueue.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		9001774012E0347800648462 /* OESStandardDerivatives.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9001773D12E0347800648462 /* OESStandardDerivatives.cpp */; };
@@ -10202,6 +10203,8 @@
 		8E33CD93201A29C100E39093 /* GapLength.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = GapLength.cpp; sourceTree = "<group>"; };
 		8E4C96D81AD4483500365A50 /* JSFetchResponse.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = JSFetchResponse.cpp; sourceTree = "<group>"; };
 		8E4C96D91AD4483500365A50 /* JSFetchResponse.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSFetchResponse.h; sourceTree = "<group>"; };
+		8E620E7E20EE5FE2007BC5EF /* VRPlatformDisplay.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = VRPlatformDisplay.cpp; sourceTree = "<group>"; };
+		8E620E8020EE5FFC007BC5EF /* VRPlatformDisplayClient.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = VRPlatformDisplayClient.h; sourceTree = "<group>"; };
 		8EC6C961201A250100FBFA53 /* GapLength.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = GapLength.h; sourceTree = "<group>"; };
 		8F6756191288B17B0047ACA3 /* EventQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EventQueue.h; sourceTree = "<group>"; };
 		8F934D831189F1EE00508D5D /* JSMainThreadExecState.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSMainThreadExecState.h; sourceTree = "<group>"; };
@@ -25040,7 +25043,9 @@
 			children = (
 				E18823622031F55C00B42DF3 /* VRManager.cpp */,
 				E18823632031F55D00B42DF3 /* VRManager.h */,
+				8E620E7E20EE5FE2007BC5EF /* VRPlatformDisplay.cpp */,
 				E18823602031F55A00B42DF3 /* VRPlatformDisplay.h */,
+				8E620E8020EE5FFC007BC5EF /* VRPlatformDisplayClient.h */,
 				E18823612031F55B00B42DF3 /* VRPlatformManager.h */,
 			);
 			path = vr;
@@ -30797,6 +30802,7 @@
 				E44613B60CD6344E00FADA75 /* VoidCallback.h in Headers */,
 				E18823642031F57100B42DF3 /* VRManager.h in Headers */,
 				E18823652031F57F00B42DF3 /* VRPlatformDisplay.h in Headers */,
+				8E620E8120EE6024007BC5EF /* VRPlatformDisplayClient.h in Headers */,
 				E18823662031F59400B42DF3 /* VRPlatformManager.h in Headers */,
 				BE20507A18A4586B0080647E /* VTTCue.h in Headers */,
 				7AF9B20318CFB2DF00C64BEF /* VTTRegion.h in Headers */,

Modified: trunk/Source/WebCore/platform/vr/VRManager.cpp (233845 => 233846)


--- trunk/Source/WebCore/platform/vr/VRManager.cpp	2018-07-16 12:24:08 UTC (rev 233845)
+++ trunk/Source/WebCore/platform/vr/VRManager.cpp	2018-07-16 14:24:22 UTC (rev 233846)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 Igalia, S.L. All right reserved.
+ * Copyright (C) 2017-2018 Igalia, S.L. All right reserved.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -53,7 +53,33 @@
     if (!m_platformManager)
         return std::nullopt;
 
-    return m_platformManager->getVRDisplays();
+    auto displays = m_platformManager->getVRDisplays();
+    VRDisplaysHashMap newDisplays;
+    for (auto& display : displays) {
+        ASSERT(display);
+        newDisplays.add(display->getDisplayInfo().displayIdentifier(), display);
+
+        VRPlatformDisplayInfo info = display->getDisplayInfo();
+        auto iterator = m_displays.find(info.displayIdentifier());
+        if (iterator == m_displays.end())
+            continue;
+
+        display->updateDisplayInfo();
+        VRPlatformDisplayInfo newInfo = display->getDisplayInfo();
+        if (info.isConnected() != newInfo.isConnected())
+            display->notifyVRPlatformDisplayEvent(newInfo.isConnected() ? VRPlatformDisplay::Event::Connected : VRPlatformDisplay::Event::Disconnected);
+        if (info.isMounted() != newInfo.isMounted())
+            display->notifyVRPlatformDisplayEvent(newInfo.isMounted() ? VRPlatformDisplay::Event::Mounted : VRPlatformDisplay::Event::Unmounted);
+    }
+
+    for (auto& iter : m_displays) {
+        ASSERT(iter.value);
+        if (!newDisplays.contains(iter.key))
+            iter.value->notifyVRPlatformDisplayEvent(VRPlatformDisplay::Event::Disconnected);
+    }
+
+    m_displays = WTFMove(newDisplays);
+    return displays;
 }
 
 }; // namespace WebCore

Modified: trunk/Source/WebCore/platform/vr/VRManager.h (233845 => 233846)


--- trunk/Source/WebCore/platform/vr/VRManager.h	2018-07-16 12:24:08 UTC (rev 233845)
+++ trunk/Source/WebCore/platform/vr/VRManager.h	2018-07-16 14:24:22 UTC (rev 233846)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 Igalia, S.L. All right reserved.
+ * Copyright (C) 2017-2018 Igalia, S.L. All right reserved.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -20,6 +20,7 @@
 
 #pragma once
 
+#include <wtf/HashMap.h>
 #include <wtf/NeverDestroyed.h>
 #include <wtf/Vector.h>
 #include <wtf/WeakPtr.h>
@@ -43,6 +44,8 @@
 private:
     VRManager();
 
+    using VRDisplaysHashMap = HashMap<uint32_t, WeakPtr<VRPlatformDisplay>>;
+    VRDisplaysHashMap m_displays;
     std::unique_ptr<VRPlatformManager> m_platformManager;
 };
 

Copied: trunk/Source/WebCore/platform/vr/VRPlatformDisplay.cpp (from rev 233845, trunk/Source/WebCore/platform/vr/VRManager.cpp) (0 => 233846)


--- trunk/Source/WebCore/platform/vr/VRPlatformDisplay.cpp	                        (rev 0)
+++ trunk/Source/WebCore/platform/vr/VRPlatformDisplay.cpp	2018-07-16 14:24:22 UTC (rev 233846)
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2018 Igalia, S.L. All right reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; see the file COPYING.LIB.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
+
+#include "config.h"
+#include "VRPlatformDisplay.h"
+
+namespace WebCore {
+
+void VRPlatformDisplay::setClient(VRPlatformDisplayClient* client)
+{
+    ASSERT(!client || !m_client);
+    m_client = client;
+}
+
+void VRPlatformDisplay::notifyVRPlatformDisplayEvent(Event event)
+{
+    if (!m_client)
+        return;
+
+    switch (event) {
+    case Event::Connected:
+        m_client->platformDisplayConnected();
+        break;
+    case Event::Disconnected:
+        m_client->platformDisplayDisconnected();
+        break;
+    case Event::Mounted:
+        m_client->platformDisplayMounted();
+        break;
+    case Event::Unmounted:
+        m_client->platformDisplayUnmounted();
+        break;
+    default:
+        ASSERT_NOT_REACHED();
+    };
+}
+
+} // namespace WebCore

Modified: trunk/Source/WebCore/platform/vr/VRPlatformDisplay.h (233845 => 233846)


--- trunk/Source/WebCore/platform/vr/VRPlatformDisplay.h	2018-07-16 12:24:08 UTC (rev 233845)
+++ trunk/Source/WebCore/platform/vr/VRPlatformDisplay.h	2018-07-16 14:24:22 UTC (rev 233846)
@@ -22,6 +22,7 @@
 
 #include "FloatPoint3D.h"
 #include "TransformationMatrix.h"
+#include "VRPlatformDisplayClient.h"
 
 #include <wtf/WeakPtr.h>
 #include <wtf/text/WTFString.h>
@@ -161,7 +162,22 @@
 public:
     virtual VRPlatformDisplayInfo getDisplayInfo() = 0;
     virtual VRPlatformTrackingInfo getTrackingInfo() = 0;
+    virtual void updateDisplayInfo() = 0;
     virtual ~VRPlatformDisplay() = default;
+
+    void setClient(VRPlatformDisplayClient*);
+
+    enum class Event {
+        Connected,
+        Disconnected,
+        Mounted,
+        Unmounted,
+    };
+
+    void notifyVRPlatformDisplayEvent(Event);
+
+private:
+    VRPlatformDisplayClient* m_client { nullptr };
 };
 
 }; // namespace WebCore

Copied: trunk/Source/WebCore/platform/vr/VRPlatformDisplayClient.h (from rev 233845, trunk/Source/WebCore/platform/vr/VRManager.h) (0 => 233846)


--- trunk/Source/WebCore/platform/vr/VRPlatformDisplayClient.h	                        (rev 0)
+++ trunk/Source/WebCore/platform/vr/VRPlatformDisplayClient.h	2018-07-16 14:24:22 UTC (rev 233846)
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2018 Igalia, S.L. All right reserved.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public License
+ * along with this library; see the file COPYING.LIB.  If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ *
+ */
+
+#pragma once
+
+namespace WebCore {
+
+class VRPlatformDisplayClient {
+public:
+    virtual ~VRPlatformDisplayClient() { }
+
+    virtual void platformDisplayConnected() { }
+    virtual void platformDisplayDisconnected() { }
+    virtual void platformDisplayMounted() { }
+    virtual void platformDisplayUnmounted() { }
+};
+
+}; // namespace WebCore

Modified: trunk/Source/WebCore/platform/vr/openvr/VRPlatformDisplayOpenVR.cpp (233845 => 233846)


--- trunk/Source/WebCore/platform/vr/openvr/VRPlatformDisplayOpenVR.cpp	2018-07-16 12:24:08 UTC (rev 233845)
+++ trunk/Source/WebCore/platform/vr/openvr/VRPlatformDisplayOpenVR.cpp	2018-07-16 14:24:22 UTC (rev 233846)
@@ -158,6 +158,36 @@
     return m_trackingInfo;
 }
 
+void VRPlatformDisplayOpenVR::updateDisplayInfo()
+{
+    if (!vr::VR_IsHmdPresent())
+        return;
+
+    vr::VREvent_t event;
+    while (m_system && m_system->PollNextEvent(&event, sizeof(event))) {
+        switch (event.eventType) {
+        case vr::VREvent_TrackedDeviceUserInteractionStarted:
+        case vr::VREvent_TrackedDeviceUserInteractionEnded:
+            if (event.trackedDeviceIndex == vr::k_unTrackedDeviceIndex_Hmd)
+                m_displayInfo.setIsMounted(event.eventType == vr::VREvent_TrackedDeviceUserInteractionStarted ? true : false);
+            break;
+        case vr::EVREventType::VREvent_TrackedDeviceActivated:
+        case vr::EVREventType::VREvent_TrackedDeviceDeactivated:
+            if (event.trackedDeviceIndex == vr::k_unTrackedDeviceIndex_Hmd)
+                m_displayInfo.setIsConnected(event.eventType == vr::VREvent_TrackedDeviceActivated ? true : false);
+            break;
+        case vr::EVREventType::VREvent_DriverRequestedQuit:
+        case vr::EVREventType::VREvent_Quit:
+        case vr::EVREventType::VREvent_ProcessQuit:
+        case vr::EVREventType::VREvent_QuitAcknowledged:
+        case vr::EVREventType::VREvent_QuitAborted_UserPrompt:
+            // FIXME: should we notify the platform manager about this and call VR_Shutdown().
+        default:
+            break;
+        }
+    }
+}
+
 }; // namespace WebCore
 
 #endif // USE(OPENVR)

Modified: trunk/Source/WebCore/platform/vr/openvr/VRPlatformDisplayOpenVR.h (233845 => 233846)


--- trunk/Source/WebCore/platform/vr/openvr/VRPlatformDisplayOpenVR.h	2018-07-16 12:24:08 UTC (rev 233845)
+++ trunk/Source/WebCore/platform/vr/openvr/VRPlatformDisplayOpenVR.h	2018-07-16 14:24:22 UTC (rev 233846)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2017 Igalia, S.L. All right reserved.
+ * Copyright (C) 2017-2018 Igalia, S.L. All right reserved.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -35,6 +35,7 @@
     ~VRPlatformDisplayOpenVR() = default;
     VRPlatformDisplayInfo getDisplayInfo() override { return m_displayInfo; }
     VRPlatformTrackingInfo getTrackingInfo() override;
+    void updateDisplayInfo() override;
 
 private:
     VRPlatformDisplayInfo::FieldOfView computeFieldOfView(vr::Hmd_Eye);
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to