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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/XRView.h"
9 #include "mozilla/HoldDropJSObjects.h"
10 #include "mozilla/dom/XRRigidTransform.h"
11 #include "mozilla/dom/Pose.h"
12 #include "nsWrapperCache.h"
14 namespace mozilla::dom
{
16 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_WITH_JS_MEMBERS(XRView
,
17 (mParent
, mTransform
),
18 (mJSProjectionMatrix
))
20 XRView::XRView(nsISupports
* aParent
, const XREye
& aEye
)
23 mPosition(gfx::PointDouble3D()),
24 mOrientation(gfx::QuaternionDouble()),
25 mJSProjectionMatrix(nullptr) {
26 mozilla::HoldJSObjects(this);
29 XRView::~XRView() { mozilla::DropJSObjects(this); }
31 JSObject
* XRView::WrapObject(JSContext
* aCx
,
32 JS::Handle
<JSObject
*> aGivenProto
) {
33 return XRView_Binding::Wrap(aCx
, this, aGivenProto
);
36 void XRView::Update(const gfx::PointDouble3D
& aPosition
,
37 const gfx::QuaternionDouble
& aOrientation
,
38 const gfx::Matrix4x4
& aProjectionMatrix
) {
39 mPosition
= aPosition
;
40 mOrientation
= aOrientation
;
41 mProjectionMatrix
= aProjectionMatrix
;
43 mTransform
->Update(aPosition
, aOrientation
);
45 if (aProjectionMatrix
!= mProjectionMatrix
) {
46 mProjectionNeedsUpdate
= true;
47 mProjectionMatrix
= aProjectionMatrix
;
51 XREye
XRView::Eye() const { return mEye
; }
53 void XRView::GetProjectionMatrix(JSContext
* aCx
,
54 JS::MutableHandle
<JSObject
*> aRetval
,
56 if (!mJSProjectionMatrix
|| mProjectionNeedsUpdate
) {
57 mProjectionNeedsUpdate
= false;
60 Pose::SetFloat32Array(aCx
, this, aRetval
, mJSProjectionMatrix
,
61 mProjectionMatrix
.components
, 16, aRv
);
62 if (!mJSProjectionMatrix
) {
66 if (mJSProjectionMatrix
) {
67 JS::ExposeObjectToActiveJS(mJSProjectionMatrix
);
69 aRetval
.set(mJSProjectionMatrix
);
72 already_AddRefed
<XRRigidTransform
> XRView::GetTransform(ErrorResult
& aRv
) {
74 mTransform
= new XRRigidTransform(mParent
, mPosition
, mOrientation
);
76 RefPtr
<XRRigidTransform
> transform
= mTransform
;
77 return transform
.forget();
80 } // namespace mozilla::dom