summaryrefslogtreecommitdiffstats
path: root/dom/vr/VRDisplay.h
blob: 866edc98461696a8ef8546747cc2b5e308254a13 (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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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_dom_VRDisplay_h_
#define mozilla_dom_VRDisplay_h_

#include <stdint.h>

#include "mozilla/dom/TypedArray.h"
#include "mozilla/dom/VRDisplayBinding.h"
#include "mozilla/DOMEventTargetHelper.h"
#include "mozilla/dom/DOMPoint.h"
#include "mozilla/dom/DOMRect.h"
#include "mozilla/dom/Pose.h"
#include "mozilla/TimeStamp.h"

#include "nsCOMPtr.h"
#include "nsString.h"
#include "nsTArray.h"

#include "gfxVR.h"

namespace mozilla {
class ErrorResult;

namespace gfx {
class VRDisplayClient;
class VRDisplayPresentation;
struct VRFieldOfView;
enum class VRDisplayCapabilityFlags : uint16_t;
struct VRHMDSensorState;
}  // namespace gfx
namespace dom {
class Navigator;

class VRFieldOfView final : public nsWrapperCache {
 public:
  VRFieldOfView(nsISupports* aParent, double aUpDegrees, double aRightDegrees,
                double aDownDegrees, double aLeftDegrees);
  VRFieldOfView(nsISupports* aParent, const gfx::VRFieldOfView& aSrc);

  NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(VRFieldOfView)
  NS_DECL_CYCLE_COLLECTION_NATIVE_WRAPPERCACHE_CLASS(VRFieldOfView)

  double UpDegrees() const { return mUpDegrees; }
  double RightDegrees() const { return mRightDegrees; }
  double DownDegrees() const { return mDownDegrees; }
  double LeftDegrees() const { return mLeftDegrees; }

  nsISupports* GetParentObject() const { return mParent; }
  virtual JSObject* WrapObject(JSContext* aCx,
                               JS::Handle<JSObject*> aGivenProto) override;

 protected:
  virtual ~VRFieldOfView() = default;

  nsCOMPtr<nsISupports> mParent;

  double mUpDegrees;
  double mRightDegrees;
  double mDownDegrees;
  double mLeftDegrees;
};

class VRDisplayCapabilities final : public nsWrapperCache {
 public:
  VRDisplayCapabilities(nsISupports* aParent,
                        const gfx::VRDisplayCapabilityFlags& aFlags)
      : mParent(aParent), mFlags(aFlags) {}

  NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(VRDisplayCapabilities)
  NS_DECL_CYCLE_COLLECTION_NATIVE_WRAPPERCACHE_CLASS(VRDisplayCapabilities)

  nsISupports* GetParentObject() const { return mParent; }

  virtual JSObject* WrapObject(JSContext* aCx,
                               JS::Handle<JSObject*> aGivenProto) override;

  bool HasPosition() const;
  bool HasOrientation() const;
  bool HasExternalDisplay() const;
  bool CanPresent() const;
  uint32_t MaxLayers() const;

 protected:
  ~VRDisplayCapabilities() = default;
  nsCOMPtr<nsISupports> mParent;
  gfx::VRDisplayCapabilityFlags mFlags;
};

class VRPose final : public Pose {
 public:
  VRPose(nsISupports* aParent, const gfx::VRHMDSensorState& aState);
  explicit VRPose(nsISupports* aParent);

  virtual void GetPosition(JSContext* aCx, JS::MutableHandle<JSObject*> aRetval,
                           ErrorResult& aRv) override;
  virtual void GetLinearVelocity(JSContext* aCx,
                                 JS::MutableHandle<JSObject*> aRetval,
                                 ErrorResult& aRv) override;
  virtual void GetLinearAcceleration(JSContext* aCx,
                                     JS::MutableHandle<JSObject*> aRetval,
                                     ErrorResult& aRv) override;
  virtual void GetOrientation(JSContext* aCx,
                              JS::MutableHandle<JSObject*> aRetval,
                              ErrorResult& aRv) override;
  virtual void GetAngularVelocity(JSContext* aCx,
                                  JS::MutableHandle<JSObject*> aRetval,
                                  ErrorResult& aRv) override;
  virtual void GetAngularAcceleration(JSContext* aCx,
                                      JS::MutableHandle<JSObject*> aRetval,
                                      ErrorResult& aRv) override;

  virtual JSObject* WrapObject(JSContext* aCx,
                               JS::Handle<JSObject*> aGivenProto) override;

  void Update(const gfx::VRHMDSensorState& aState);

 protected:
  ~VRPose();

  gfx::VRHMDSensorState mVRState;
};

struct VRFrameInfo {
  VRFrameInfo();

  void Update(const gfx::VRDisplayInfo& aInfo,
              const gfx::VRHMDSensorState& aState, float aDepthNear,
              float aDepthFar);

  void Clear();
  bool IsDirty();

  gfx::VRHMDSensorState mVRState;
  gfx::Matrix4x4 mLeftProjection;
  gfx::Matrix4x4 mLeftView;
  gfx::Matrix4x4 mRightProjection;
  gfx::Matrix4x4 mRightView;

  /**
   * In order to avoid leaking information related to the duration of
   * the user's VR session, we re-base timestamps.
   * mTimeStampOffset is added to the actual timestamp returned by the
   * underlying VR platform API when returned through WebVR API's.
   */
  double mTimeStampOffset;
};

class VRFrameData final : public nsWrapperCache {
 public:
  NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(VRFrameData)
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(VRFrameData)

  explicit VRFrameData(nsISupports* aParent);
  static already_AddRefed<VRFrameData> Constructor(const GlobalObject& aGlobal);

  void Update(const VRFrameInfo& aFrameInfo);

  // WebIDL Members
  double Timestamp() const;
  void GetLeftProjectionMatrix(JSContext* aCx,
                               JS::MutableHandle<JSObject*> aRetval,
                               ErrorResult& aRv);
  void GetLeftViewMatrix(JSContext* aCx, JS::MutableHandle<JSObject*> aRetval,
                         ErrorResult& aRv);
  void GetRightProjectionMatrix(JSContext* aCx,
                                JS::MutableHandle<JSObject*> aRetval,
                                ErrorResult& aRv);
  void GetRightViewMatrix(JSContext* aCx, JS::MutableHandle<JSObject*> aRetval,
                          ErrorResult& aRv);

  VRPose* Pose();

  // WebIDL Boilerplate
  nsISupports* GetParentObject() const { return mParent; }
  virtual JSObject* WrapObject(JSContext* aCx,
                               JS::Handle<JSObject*> aGivenProto) override;

 protected:
  ~VRFrameData();
  nsCOMPtr<nsISupports> mParent;

  VRFrameInfo mFrameInfo;
  RefPtr<VRPose> mPose;
  JS::Heap<JSObject*> mLeftProjectionMatrix;
  JS::Heap<JSObject*> mLeftViewMatrix;
  JS::Heap<JSObject*> mRightProjectionMatrix;
  JS::Heap<JSObject*> mRightViewMatrix;

  void LazyCreateMatrix(JS::Heap<JSObject*>& aArray, gfx::Matrix4x4& aMat,
                        JSContext* aCx, JS::MutableHandle<JSObject*> aRetval,
                        ErrorResult& aRv);
};

class VRStageParameters final : public nsWrapperCache {
 public:
  VRStageParameters(nsISupports* aParent,
                    const gfx::Matrix4x4& aSittingToStandingTransform,
                    const gfx::Size& aSize);

  NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(VRStageParameters)
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(VRStageParameters)

  void GetSittingToStandingTransform(JSContext* aCx,
                                     JS::MutableHandle<JSObject*> aRetval,
                                     ErrorResult& aRv);
  float SizeX() const { return mSize.width; }
  float SizeZ() const { return mSize.height; }

  nsISupports* GetParentObject() const { return mParent; }
  virtual JSObject* WrapObject(JSContext* aCx,
                               JS::Handle<JSObject*> aGivenProto) override;

 protected:
  ~VRStageParameters();

  nsCOMPtr<nsISupports> mParent;

  gfx::Matrix4x4 mSittingToStandingTransform;
  JS::Heap<JSObject*> mSittingToStandingTransformArray;
  gfx::Size mSize;
};

class VREyeParameters final : public nsWrapperCache {
 public:
  VREyeParameters(nsISupports* aParent, const gfx::Point3D& aEyeTranslation,
                  const gfx::VRFieldOfView& aFOV,
                  const gfx::IntSize& aRenderSize);

  NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(VREyeParameters)
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(VREyeParameters)

  void GetOffset(JSContext* aCx, JS::MutableHandle<JSObject*> aRetVal,
                 ErrorResult& aRv);

  VRFieldOfView* FieldOfView();

  uint32_t RenderWidth() const { return mRenderSize.width; }
  uint32_t RenderHeight() const { return mRenderSize.height; }

  nsISupports* GetParentObject() const { return mParent; }
  virtual JSObject* WrapObject(JSContext* aCx,
                               JS::Handle<JSObject*> aGivenProto) override;

 protected:
  ~VREyeParameters();

  nsCOMPtr<nsISupports> mParent;

  gfx::Point3D mEyeTranslation;
  gfx::IntSize mRenderSize;
  JS::Heap<JSObject*> mOffset;
  RefPtr<VRFieldOfView> mFOV;
};

class VRDisplay final : public DOMEventTargetHelper, public nsIObserver {
 public:
  NS_DECL_ISUPPORTS_INHERITED
  NS_DECL_NSIOBSERVER
  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(VRDisplay, DOMEventTargetHelper)

  virtual JSObject* WrapObject(JSContext* aCx,
                               JS::Handle<JSObject*> aGivenProto) override;

  uint32_t PresentingGroups() const;
  uint32_t GroupMask() const;
  void SetGroupMask(const uint32_t& aGroupMask);
  bool IsAnyPresenting(uint32_t aGroupMask) const;
  bool IsPresenting() const;
  bool IsConnected() const;

  VRDisplayCapabilities* Capabilities();
  VRStageParameters* GetStageParameters();

  uint32_t DisplayId() const;
  void GetDisplayName(nsAString& aDisplayName) const;
  // Replacing the old VRDisplayClient with the newest one to avoid
  // JS needs to reload to recover VRDisplay when VRService is shutdown at the
  // backend.
  void UpdateDisplayClient(already_AddRefed<gfx::VRDisplayClient> aClient);

  static bool RefreshVRDisplays(uint64_t aWindowId);
  static void UpdateVRDisplays(nsTArray<RefPtr<VRDisplay> >& aDisplays,
                               nsPIDOMWindowInner* aWindow);

  gfx::VRDisplayClient* GetClient() { return mClient; }

  virtual already_AddRefed<VREyeParameters> GetEyeParameters(VREye aEye);

  bool GetFrameData(VRFrameData& aFrameData);
  already_AddRefed<VRPose> GetPose();
  void ResetPose();

  double DepthNear() { return mDepthNear; }

  double DepthFar() { return mDepthFar; }

  void SetDepthNear(double aDepthNear) {
    // XXX When we start sending depth buffers to VRLayer's we will want
    // to communicate this with the VRDisplayHost
    mDepthNear = aDepthNear;
  }

  void SetDepthFar(double aDepthFar) {
    // XXX When we start sending depth buffers to VRLayer's we will want
    // to communicate this with the VRDisplayHost
    mDepthFar = aDepthFar;
  }

  already_AddRefed<Promise> RequestPresent(const nsTArray<VRLayer>& aLayers,
                                           CallerType aCallerType,
                                           ErrorResult& aRv);
  already_AddRefed<Promise> ExitPresent(ErrorResult& aRv);
  void GetLayers(nsTArray<VRLayer>& result);
  void SubmitFrame();

  int32_t RequestAnimationFrame(mozilla::dom::FrameRequestCallback& aCallback,
                                mozilla::ErrorResult& aError);
  void CancelAnimationFrame(int32_t aHandle, mozilla::ErrorResult& aError);
  void StartVRNavigation();
  void StartHandlingVRNavigationEvent();
  void StopHandlingVRNavigationEvent();
  bool IsHandlingVRNavigationEvent();
  void OnPresentationGenerationChanged();

 protected:
  VRDisplay(nsPIDOMWindowInner* aWindow, gfx::VRDisplayClient* aClient);
  virtual ~VRDisplay();
  virtual void LastRelease() override;

  void ExitPresentInternal();
  void Shutdown();
  void UpdateFrameInfo();

  RefPtr<gfx::VRDisplayClient> mClient;

  RefPtr<VRDisplayCapabilities> mCapabilities;
  RefPtr<VRStageParameters> mStageParameters;

  double mDepthNear;
  double mDepthFar;

  RefPtr<gfx::VRDisplayPresentation> mPresentation;

  /**
   * The WebVR 1.1 spec Requires that VRDisplay.getPose and
   * VRDisplay.getFrameData must return the same values until the next
   * VRDisplay.submitFrame. mFrameInfo is updated only on the first call to
   * either function within one frame.  Subsequent calls before the next
   * SubmitFrame or ExitPresent call will use these cached values.
   */
  VRFrameInfo mFrameInfo;

  // Time at which we began expecting VR navigation.
  TimeStamp mHandlingVRNavigationEventStart;
  int32_t mVRNavigationEventDepth;
  bool mShutdown;
};

}  // namespace dom
}  // namespace mozilla

#endif