Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 : * vim: sw=2 ts=8 et :
3 : */
4 : /* This Source Code Form is subject to the terms of the Mozilla Public
5 : * License, v. 2.0. If a copy of the MPL was not distributed with this
6 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 :
8 : #ifndef MOZILLA_GFX_VR_VRMANAGERCHILD_H
9 : #define MOZILLA_GFX_VR_VRMANAGERCHILD_H
10 :
11 : #include "mozilla/gfx/PVRManagerChild.h"
12 : #include "mozilla/ipc/SharedMemory.h" // for SharedMemory, etc
13 : #include "ThreadSafeRefcountingWithMainThreadDestruction.h"
14 : #include "mozilla/layers/ISurfaceAllocator.h" // for ISurfaceAllocator
15 : #include "mozilla/layers/LayersTypes.h" // for LayersBackend
16 : #include "mozilla/layers/TextureForwarder.h"
17 :
18 : namespace mozilla {
19 : namespace dom {
20 : class Promise;
21 : class GamepadManager;
22 : class Navigator;
23 : class VRDisplay;
24 : class VREventObserver;
25 : class VRMockDisplay;
26 : } // namespace dom
27 : namespace layers {
28 : class TextureClient;
29 : }
30 : namespace gfx {
31 : class VRLayerChild;
32 : class VRDisplayClient;
33 :
34 : class VRManagerChild : public PVRManagerChild
35 : , public layers::TextureForwarder
36 : , public layers::KnowsCompositor
37 : {
38 : public:
39 7 : NS_INLINE_DECL_THREADSAFE_REFCOUNTING(VRManagerChild, override);
40 :
41 0 : TextureForwarder* GetTextureForwarder() override { return this; }
42 0 : LayersIPCActor* GetLayersIPCActor() override { return this; }
43 :
44 : static VRManagerChild* Get();
45 :
46 : // Indicate that an observer wants to receive VR events.
47 : void AddListener(dom::VREventObserver* aObserver);
48 : // Indicate that an observer should no longer receive VR events.
49 : void RemoveListener(dom::VREventObserver* aObserver);
50 :
51 : bool GetVRDisplays(nsTArray<RefPtr<VRDisplayClient> >& aDisplays);
52 : bool RefreshVRDisplaysWithCallback(uint64_t aWindowId);
53 : void AddPromise(const uint32_t& aID, dom::Promise* aPromise);
54 :
55 : void CreateVRServiceTestDisplay(const nsCString& aID, dom::Promise* aPromise);
56 : void CreateVRServiceTestController(const nsCString& aID, dom::Promise* aPromise);
57 :
58 : static void InitSameProcess();
59 : static void InitWithGPUProcess(Endpoint<PVRManagerChild>&& aEndpoint);
60 : static bool InitForContent(Endpoint<PVRManagerChild>&& aEndpoint);
61 : static bool ReinitForContent(Endpoint<PVRManagerChild>&& aEndpoint);
62 : static void ShutDown();
63 :
64 : static bool IsCreated();
65 :
66 : virtual PTextureChild* CreateTexture(
67 : const SurfaceDescriptor& aSharedData,
68 : layers::LayersBackend aLayersBackend,
69 : TextureFlags aFlags,
70 : uint64_t aSerial,
71 : wr::MaybeExternalImageId& aExternalImageId,
72 : nsIEventTarget* aTarget = nullptr) override;
73 : virtual void CancelWaitForRecycle(uint64_t aTextureId) override;
74 :
75 : PVRLayerChild* CreateVRLayer(uint32_t aDisplayID,
76 : const Rect& aLeftEyeRect,
77 : const Rect& aRightEyeRect,
78 : nsIEventTarget* aTarget,
79 : uint32_t aGroup);
80 :
81 : static void IdentifyTextureHost(const layers::TextureFactoryIdentifier& aIdentifier);
82 : layers::LayersBackend GetBackendType() const;
83 0 : layers::SyncObject* GetSyncObject() { return mSyncObject; }
84 :
85 0 : virtual MessageLoop* GetMessageLoop() const override { return mMessageLoop; }
86 0 : virtual base::ProcessId GetParentPid() const override { return OtherPid(); }
87 :
88 : nsresult ScheduleFrameRequestCallback(mozilla::dom::FrameRequestCallback& aCallback,
89 : int32_t *aHandle);
90 : void CancelFrameRequestCallback(int32_t aHandle);
91 : void RunFrameRequestCallbacks();
92 :
93 : void UpdateDisplayInfo(nsTArray<VRDisplayInfo>& aDisplayUpdates);
94 : void FireDOMVRDisplayMountedEvent(uint32_t aDisplayID);
95 : void FireDOMVRDisplayUnmountedEvent(uint32_t aDisplayID);
96 : void FireDOMVRDisplayConnectEvent(uint32_t aDisplayID);
97 : void FireDOMVRDisplayDisconnectEvent(uint32_t aDisplayID);
98 : void FireDOMVRDisplayPresentChangeEvent(uint32_t aDisplayID);
99 :
100 : virtual void HandleFatalError(const char* aName, const char* aMsg) const override;
101 :
102 : protected:
103 : explicit VRManagerChild();
104 : ~VRManagerChild();
105 : void Destroy();
106 : static void DeferredDestroy(RefPtr<VRManagerChild> aVRManagerChild);
107 :
108 : virtual PTextureChild* AllocPTextureChild(const SurfaceDescriptor& aSharedData,
109 : const layers::LayersBackend& aLayersBackend,
110 : const TextureFlags& aFlags,
111 : const uint64_t& aSerial) override;
112 : virtual bool DeallocPTextureChild(PTextureChild* actor) override;
113 :
114 : virtual PVRLayerChild* AllocPVRLayerChild(const uint32_t& aDisplayID,
115 : const float& aLeftEyeX,
116 : const float& aLeftEyeY,
117 : const float& aLeftEyeWidth,
118 : const float& aLeftEyeHeight,
119 : const float& aRightEyeX,
120 : const float& aRightEyeY,
121 : const float& aRightEyeWidth,
122 : const float& aRightEyeHeight,
123 : const uint32_t& aGroup) override;
124 : virtual bool DeallocPVRLayerChild(PVRLayerChild* actor) override;
125 :
126 : virtual mozilla::ipc::IPCResult RecvUpdateDisplayInfo(nsTArray<VRDisplayInfo>&& aDisplayUpdates) override;
127 :
128 : virtual mozilla::ipc::IPCResult RecvParentAsyncMessages(InfallibleTArray<AsyncParentMessageData>&& aMessages) override;
129 :
130 : virtual mozilla::ipc::IPCResult RecvDispatchSubmitFrameResult(const uint32_t& aDisplayID, const VRSubmitFrameResultInfo& aResult) override;
131 : virtual mozilla::ipc::IPCResult RecvGamepadUpdate(const GamepadChangeEvent& aGamepadEvent) override;
132 : virtual mozilla::ipc::IPCResult RecvReplyGamepadVibrateHaptic(const uint32_t& aPromiseID) override;
133 :
134 : virtual mozilla::ipc::IPCResult RecvReplyCreateVRServiceTestDisplay(const nsCString& aID,
135 : const uint32_t& aPromiseID,
136 : const uint32_t& aDeviceID) override;
137 : virtual mozilla::ipc::IPCResult RecvReplyCreateVRServiceTestController(const nsCString& aID,
138 : const uint32_t& aPromiseID,
139 : const uint32_t& aDeviceID) override;
140 :
141 : // ShmemAllocator
142 :
143 : virtual bool AllocShmem(size_t aSize,
144 : ipc::SharedMemory::SharedMemoryType aType,
145 : ipc::Shmem* aShmem) override;
146 :
147 : virtual bool AllocUnsafeShmem(size_t aSize,
148 : ipc::SharedMemory::SharedMemoryType aType,
149 : ipc::Shmem* aShmem) override;
150 :
151 : virtual bool DeallocShmem(ipc::Shmem& aShmem) override;
152 :
153 0 : virtual bool IsSameProcess() const override
154 : {
155 0 : return OtherPid() == base::GetCurrentProcId();
156 : }
157 :
158 : friend class layers::CompositorBridgeChild;
159 :
160 : private:
161 :
162 : void FireDOMVRDisplayMountedEventInternal(uint32_t aDisplayID);
163 : void FireDOMVRDisplayUnmountedEventInternal(uint32_t aDisplayID);
164 : void FireDOMVRDisplayConnectEventInternal(uint32_t aDisplayID);
165 : void FireDOMVRDisplayDisconnectEventInternal(uint32_t aDisplayID);
166 : void FireDOMVRDisplayPresentChangeEventInternal(uint32_t aDisplayID);
167 : /**
168 : * Notify id of Texture When host side end its use. Transaction id is used to
169 : * make sure if there is no newer usage.
170 : */
171 : void NotifyNotUsed(uint64_t aTextureId, uint64_t aFwdTransactionId);
172 :
173 : nsTArray<RefPtr<VRDisplayClient> > mDisplays;
174 : bool mDisplaysInitialized;
175 : nsTArray<uint64_t> mNavigatorCallbacks;
176 :
177 : MessageLoop* mMessageLoop;
178 :
179 : struct FrameRequest;
180 :
181 : nsTArray<FrameRequest> mFrameRequestCallbacks;
182 : /**
183 : * The current frame request callback handle
184 : */
185 : int32_t mFrameRequestCallbackCounter;
186 : mozilla::TimeStamp mStartTimeStamp;
187 :
188 : nsTArray<RefPtr<dom::VREventObserver>> mListeners;
189 :
190 : /**
191 : * Hold TextureClients refs until end of their usages on host side.
192 : * It defer calling of TextureClient recycle callback.
193 : */
194 : nsDataHashtable<nsUint64HashKey, RefPtr<layers::TextureClient> > mTexturesWaitingRecycled;
195 :
196 : layers::LayersBackend mBackend;
197 : RefPtr<layers::SyncObject> mSyncObject;
198 : nsRefPtrHashtable<nsUint32HashKey, dom::Promise> mGamepadPromiseList; // TODO: check if it can merge into one list?
199 : uint32_t mPromiseID;
200 : nsRefPtrHashtable<nsUint32HashKey, dom::Promise> mPromiseList;
201 : RefPtr<dom::VRMockDisplay> mVRMockDisplay;
202 :
203 : DISALLOW_COPY_AND_ASSIGN(VRManagerChild);
204 : };
205 :
206 : } // namespace mozilla
207 : } // namespace gfx
208 :
209 : #endif // MOZILLA_GFX_VR_VRMANAGERCHILD_H
|