1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef MOZILLA_GFX_VR_VRMANAGERCHILD_H
8 #define MOZILLA_GFX_VR_VRMANAGERCHILD_H
10 #include "nsISupportsImpl.h"
11 #include "mozilla/Attributes.h"
12 #include "mozilla/dom/AnimationFrameProviderBinding.h"
13 #include "mozilla/dom/WebXRBinding.h"
14 #include "mozilla/dom/XRFrame.h"
15 #include "mozilla/gfx/PVRManagerChild.h"
16 #include "mozilla/ipc/SharedMemory.h" // for SharedMemory, etc
17 #include "mozilla/layers/ISurfaceAllocator.h" // for ISurfaceAllocator
18 #include "mozilla/layers/LayersTypes.h" // for LayersBackend
26 class FrameRequestCallback
;
30 class VRDisplayClient
;
32 class VRManagerEventObserver
{
34 NS_INLINE_DECL_PURE_VIRTUAL_REFCOUNTING
35 virtual void NotifyVRDisplayMounted(uint32_t aDisplayID
) = 0;
36 virtual void NotifyVRDisplayUnmounted(uint32_t aDisplayID
) = 0;
37 virtual void NotifyVRDisplayConnect(uint32_t aDisplayID
) = 0;
38 virtual void NotifyVRDisplayDisconnect(uint32_t aDisplayID
) = 0;
39 virtual void NotifyVRDisplayPresentChange(uint32_t aDisplayID
) = 0;
40 virtual void NotifyPresentationGenerationChanged(uint32_t aDisplayID
) = 0;
41 virtual bool GetStopActivityStatus() const = 0;
42 virtual void NotifyEnumerationCompleted() = 0;
43 virtual void NotifyDetectRuntimesCompleted() = 0;
46 virtual ~VRManagerEventObserver() = default;
49 class VRManagerChild
: public PVRManagerChild
{
50 friend class PVRManagerChild
;
53 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(VRManagerChild
, override
);
55 static VRManagerChild
* Get();
57 // Indicate that an observer wants to receive VR events.
58 void AddListener(VRManagerEventObserver
* aObserver
);
59 // Indicate that an observer should no longer receive VR events.
60 void RemoveListener(VRManagerEventObserver
* aObserver
);
63 bool RuntimeSupportsVR() const;
64 bool RuntimeSupportsAR() const;
65 bool RuntimeSupportsInline() const;
67 void GetVRDisplays(nsTArray
<RefPtr
<VRDisplayClient
>>& aDisplays
);
68 bool RefreshVRDisplaysWithCallback(uint64_t aWindowId
);
69 bool EnumerateVRDisplays();
70 void DetectRuntimes();
71 void AddPromise(const uint32_t& aID
, dom::Promise
* aPromise
);
72 gfx::VRAPIMode
GetVRAPIMode(uint32_t aDisplayID
) const;
74 static void InitSameProcess();
75 static void InitWithGPUProcess(Endpoint
<PVRManagerChild
>&& aEndpoint
);
76 static bool InitForContent(Endpoint
<PVRManagerChild
>&& aEndpoint
);
77 static void ShutDown();
79 static bool IsCreated();
80 static bool IsPresenting();
81 static TimeStamp
GetIdleDeadlineHint(TimeStamp aDefault
);
83 PVRLayerChild
* CreateVRLayer(uint32_t aDisplayID
, uint32_t aGroup
);
85 static void IdentifyTextureHost(
86 const layers::TextureFactoryIdentifier
& aIdentifier
);
87 layers::LayersBackend
GetBackendType() const;
89 nsresult
ScheduleFrameRequestCallback(dom::FrameRequestCallback
& aCallback
,
91 void CancelFrameRequestCallback(int32_t aHandle
);
93 void RunFrameRequestCallbacks();
94 void NotifyPresentationGenerationChanged(uint32_t aDisplayID
);
97 void UpdateDisplayInfo(const VRDisplayInfo
& aDisplayInfo
);
98 void FireDOMVRDisplayMountedEvent(uint32_t aDisplayID
);
99 void FireDOMVRDisplayUnmountedEvent(uint32_t aDisplayID
);
100 void FireDOMVRDisplayConnectEvent(uint32_t aDisplayID
);
101 void FireDOMVRDisplayDisconnectEvent(uint32_t aDisplayID
);
102 void FireDOMVRDisplayPresentChangeEvent(uint32_t aDisplayID
);
103 void FireDOMVRDisplayConnectEventsForLoad(VRManagerEventObserver
* aObserver
);
105 void HandleFatalError(const char* aMsg
) override
;
106 void ActorDestroy(ActorDestroyReason aReason
) override
;
108 void RunPuppet(const nsTArray
<uint64_t>& aBuffer
, dom::Promise
* aPromise
,
110 void ResetPuppet(dom::Promise
* aPromise
, ErrorResult
& aRv
);
113 explicit VRManagerChild();
116 PVRLayerChild
* AllocPVRLayerChild(const uint32_t& aDisplayID
,
117 const uint32_t& aGroup
);
118 bool DeallocPVRLayerChild(PVRLayerChild
* actor
);
120 // MOZ_CAN_RUN_SCRIPT_BOUNDARY until we can mark ipdl-generated things as
121 // MOZ_CAN_RUN_SCRIPT.
122 MOZ_CAN_RUN_SCRIPT_BOUNDARY
123 mozilla::ipc::IPCResult
RecvUpdateDisplayInfo(
124 const VRDisplayInfo
& aDisplayInfo
);
125 mozilla::ipc::IPCResult
RecvUpdateRuntimeCapabilities(
126 const VRDisplayCapabilityFlags
& aCapabilities
);
127 mozilla::ipc::IPCResult
RecvReplyGamepadVibrateHaptic(
128 const uint32_t& aPromiseID
);
130 MOZ_CAN_RUN_SCRIPT_BOUNDARY
131 mozilla::ipc::IPCResult
RecvNotifyPuppetCommandBufferCompleted(bool aSuccess
);
132 MOZ_CAN_RUN_SCRIPT_BOUNDARY
133 mozilla::ipc::IPCResult
RecvNotifyPuppetResetComplete();
135 bool IsSameProcess() const { return OtherPid() == base::GetCurrentProcId(); }
138 void FireDOMVRDisplayMountedEventInternal(uint32_t aDisplayID
);
139 void FireDOMVRDisplayUnmountedEventInternal(uint32_t aDisplayID
);
140 void FireDOMVRDisplayConnectEventInternal(uint32_t aDisplayID
);
141 void FireDOMVRDisplayDisconnectEventInternal(uint32_t aDisplayID
);
142 void FireDOMVRDisplayPresentChangeEventInternal(uint32_t aDisplayID
);
143 void FireDOMVRDisplayConnectEventsForLoadInternal(
144 uint32_t aDisplayID
, VRManagerEventObserver
* aObserver
);
145 void NotifyPresentationGenerationChangedInternal(uint32_t aDisplayID
);
146 void NotifyEnumerationCompletedInternal();
147 void NotifyRuntimeCapabilitiesUpdatedInternal();
149 nsTArray
<RefPtr
<VRDisplayClient
>> mDisplays
;
150 VRDisplayCapabilityFlags mRuntimeCapabilities
;
151 bool mDisplaysInitialized
;
152 nsTArray
<uint64_t> mNavigatorCallbacks
;
154 struct XRFrameRequest
{
155 XRFrameRequest(mozilla::dom::FrameRequestCallback
& aCallback
,
157 : mCallback(&aCallback
), mHandle(aHandle
) {}
159 XRFrameRequest(mozilla::dom::XRFrameRequestCallback
& aCallback
,
160 mozilla::dom::XRFrame
& aFrame
, int32_t aHandle
)
161 : mXRCallback(&aCallback
), mXRFrame(&aFrame
), mHandle(aHandle
) {}
163 void Call(const DOMHighResTimeStamp
& aTimeStamp
);
165 // Comparator operators to allow RemoveElementSorted with an
166 // integer argument on arrays of XRFrameRequest
167 bool operator==(int32_t aHandle
) const { return mHandle
== aHandle
; }
168 bool operator<(int32_t aHandle
) const { return mHandle
< aHandle
; }
170 RefPtr
<mozilla::dom::FrameRequestCallback
> mCallback
;
171 RefPtr
<mozilla::dom::XRFrameRequestCallback
> mXRCallback
;
172 RefPtr
<mozilla::dom::XRFrame
> mXRFrame
;
176 nsTArray
<XRFrameRequest
> mFrameRequestCallbacks
;
178 * The current frame request callback handle
180 int32_t mFrameRequestCallbackCounter
;
181 mozilla::TimeStamp mStartTimeStamp
;
183 nsTArray
<RefPtr
<VRManagerEventObserver
>> mListeners
;
184 bool mWaitingForEnumeration
;
186 layers::LayersBackend mBackend
;
187 nsRefPtrHashtable
<nsUint32HashKey
, dom::Promise
> mGamepadPromiseList
;
188 RefPtr
<dom::Promise
> mRunPuppetPromise
;
189 nsTArray
<RefPtr
<dom::Promise
>> mResetPuppetPromises
;
191 DISALLOW_COPY_AND_ASSIGN(VRManagerChild
);
195 } // namespace mozilla
197 #endif // MOZILLA_GFX_VR_VRMANAGERCHILD_H