summaryrefslogtreecommitdiffstats
path: root/gfx/vr/ipc/VRManagerChild.h
blob: c898cd2f881c0941905b2661a9d19b51a66c545b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: sw=2 ts=8 et :
 */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef MOZILLA_GFX_VR_VRMANAGERCHILD_H
#define MOZILLA_GFX_VR_VRMANAGERCHILD_H

#include "mozilla/gfx/PVRManagerChild.h"
#include "mozilla/ipc/SharedMemory.h"   // for SharedMemory, etc
#include "ThreadSafeRefcountingWithMainThreadDestruction.h"
#include "mozilla/layers/ISurfaceAllocator.h"  // for ISurfaceAllocator
#include "mozilla/layers/LayersTypes.h"  // for LayersBackend
#include "mozilla/layers/TextureForwarder.h"

namespace mozilla {
namespace dom {
class GamepadManager;
class Navigator;
class VRDisplay;
class VREventObserver;
} // namespace dom
namespace layers {
class PCompositableChild;
class TextureClient;
}
namespace gfx {
class VRLayerChild;
class VRDisplayClient;

class VRManagerChild : public PVRManagerChild
                     , public layers::TextureForwarder
                     , public layers::KnowsCompositor
{
public:
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(VRManagerChild, override);

  TextureForwarder* GetTextureForwarder() override { return this; }
  LayersIPCActor* GetLayersIPCActor() override { return this; }

  static VRManagerChild* Get();

  // Indicate that an observer wants to receive VR events.
  void AddListener(dom::VREventObserver* aObserver);
  // Indicate that an observer should no longer receive VR events.
  void RemoveListener(dom::VREventObserver* aObserver);

  int GetInputFrameID();
  bool GetVRDisplays(nsTArray<RefPtr<VRDisplayClient> >& aDisplays);
  bool RefreshVRDisplaysWithCallback(uint64_t aWindowId);

  static void InitSameProcess();
  static void InitWithGPUProcess(Endpoint<PVRManagerChild>&& aEndpoint);
  static bool InitForContent(Endpoint<PVRManagerChild>&& aEndpoint);
  static bool ReinitForContent(Endpoint<PVRManagerChild>&& aEndpoint);
  static void ShutDown();

  static bool IsCreated();

  virtual PTextureChild* CreateTexture(const SurfaceDescriptor& aSharedData,
                                       layers::LayersBackend aLayersBackend,
                                       TextureFlags aFlags,
                                       uint64_t aSerial) override;
  virtual void CancelWaitForRecycle(uint64_t aTextureId) override;

  PVRLayerChild* CreateVRLayer(uint32_t aDisplayID, const Rect& aLeftEyeRect, const Rect& aRightEyeRect);

  static void IdentifyTextureHost(const layers::TextureFactoryIdentifier& aIdentifier);
  layers::LayersBackend GetBackendType() const;
  layers::SyncObject* GetSyncObject() { return mSyncObject; }

  virtual MessageLoop* GetMessageLoop() const override { return mMessageLoop; }
  virtual base::ProcessId GetParentPid() const override { return OtherPid(); }

  nsresult ScheduleFrameRequestCallback(mozilla::dom::FrameRequestCallback& aCallback,
    int32_t *aHandle);
  void CancelFrameRequestCallback(int32_t aHandle);
  void RunFrameRequestCallbacks();

  void UpdateDisplayInfo(nsTArray<VRDisplayInfo>& aDisplayUpdates);
  void FireDOMVRDisplayConnectEvent();
  void FireDOMVRDisplayDisconnectEvent();
  void FireDOMVRDisplayPresentChangeEvent();

  virtual void HandleFatalError(const char* aName, const char* aMsg) const override;

protected:
  explicit VRManagerChild();
  ~VRManagerChild();
  void Destroy();
  static void DeferredDestroy(RefPtr<VRManagerChild> aVRManagerChild);

  virtual PTextureChild* AllocPTextureChild(const SurfaceDescriptor& aSharedData,
                                            const layers::LayersBackend& aLayersBackend,
                                            const TextureFlags& aFlags,
                                            const uint64_t& aSerial) override;
  virtual bool DeallocPTextureChild(PTextureChild* actor) override;

  virtual PVRLayerChild* AllocPVRLayerChild(const uint32_t& aDisplayID,
                                            const float& aLeftEyeX,
                                            const float& aLeftEyeY,
                                            const float& aLeftEyeWidth,
                                            const float& aLeftEyeHeight,
                                            const float& aRightEyeX,
                                            const float& aRightEyeY,
                                            const float& aRightEyeWidth,
                                            const float& aRightEyeHeight) override;
  virtual bool DeallocPVRLayerChild(PVRLayerChild* actor) override;

  virtual bool RecvUpdateDisplayInfo(nsTArray<VRDisplayInfo>&& aDisplayUpdates) override;

  virtual bool RecvParentAsyncMessages(InfallibleTArray<AsyncParentMessageData>&& aMessages) override;

  virtual bool RecvNotifyVSync() override;
  virtual bool RecvNotifyVRVSync(const uint32_t& aDisplayID) override;
  virtual bool RecvGamepadUpdate(const GamepadChangeEvent& aGamepadEvent) override;

  // ShmemAllocator

  virtual bool AllocShmem(size_t aSize,
                          ipc::SharedMemory::SharedMemoryType aType,
                          ipc::Shmem* aShmem) override;

  virtual bool AllocUnsafeShmem(size_t aSize,
                                ipc::SharedMemory::SharedMemoryType aType,
                                ipc::Shmem* aShmem) override;

  virtual bool DeallocShmem(ipc::Shmem& aShmem) override;

  virtual bool IsSameProcess() const override
  {
    return OtherPid() == base::GetCurrentProcId();
  }

  friend class layers::CompositorBridgeChild;

private:

  void FireDOMVRDisplayConnectEventInternal();
  void FireDOMVRDisplayDisconnectEventInternal();
  void FireDOMVRDisplayPresentChangeEventInternal();
  /**
  * Notify id of Texture When host side end its use. Transaction id is used to
  * make sure if there is no newer usage.
  */
  void NotifyNotUsed(uint64_t aTextureId, uint64_t aFwdTransactionId);

  nsTArray<RefPtr<VRDisplayClient> > mDisplays;
  bool mDisplaysInitialized;
  nsTArray<uint64_t> mNavigatorCallbacks;

  int32_t mInputFrameID;

  MessageLoop* mMessageLoop;

  struct FrameRequest;

  nsTArray<FrameRequest> mFrameRequestCallbacks;
  /**
  * The current frame request callback handle
  */
  int32_t mFrameRequestCallbackCounter;
  mozilla::TimeStamp mStartTimeStamp;

  // Array of Weak pointers, instance is owned by nsGlobalWindow::mVREventObserver.
  nsTArray<dom::VREventObserver*> mListeners;

  /**
  * Hold TextureClients refs until end of their usages on host side.
  * It defer calling of TextureClient recycle callback.
  */
  nsDataHashtable<nsUint64HashKey, RefPtr<layers::TextureClient> > mTexturesWaitingRecycled;

  layers::LayersBackend mBackend;
  RefPtr<layers::SyncObject> mSyncObject;

  DISALLOW_COPY_AND_ASSIGN(VRManagerChild);
};

} // namespace mozilla
} // namespace gfx

#endif // MOZILLA_GFX_VR_VRMANAGERCHILD_H