Bug 1880216 - Migrate Fenix docs into Sphinx. r=owlish,geckoview-reviewers,android...
[gecko.git] / dom / vr / XRView.cpp
blobc1ff18004e9ab424bb0d19f62bb0f700e1e14f83
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)
21 : mParent(aParent),
22 mEye(aEye),
24 mJSProjectionMatrix(nullptr) {
25 mozilla::HoldJSObjects(this);
28 XRView::~XRView() { mozilla::DropJSObjects(this); }
30 JSObject* XRView::WrapObject(JSContext* aCx,
31 JS::Handle<JSObject*> aGivenProto) {
32 return XRView_Binding::Wrap(aCx, this, aGivenProto);
35 void XRView::Update(const gfx::PointDouble3D& aPosition,
36 const gfx::QuaternionDouble& aOrientation,
37 const gfx::Matrix4x4& aProjectionMatrix) {
38 mPosition = aPosition;
39 mOrientation = aOrientation;
40 mProjectionMatrix = aProjectionMatrix;
41 if (mTransform) {
42 mTransform->Update(aPosition, aOrientation);
44 if (aProjectionMatrix != mProjectionMatrix) {
45 mProjectionNeedsUpdate = true;
46 mProjectionMatrix = aProjectionMatrix;
50 XREye XRView::Eye() const { return mEye; }
52 void XRView::GetProjectionMatrix(JSContext* aCx,
53 JS::MutableHandle<JSObject*> aRetval,
54 ErrorResult& aRv) {
55 if (!mJSProjectionMatrix || mProjectionNeedsUpdate) {
56 mProjectionNeedsUpdate = false;
57 gfx::Matrix4x4 mat;
59 Pose::SetFloat32Array(aCx, this, aRetval, mJSProjectionMatrix,
60 mProjectionMatrix.components, 16, aRv);
61 if (!mJSProjectionMatrix) {
62 return;
65 if (mJSProjectionMatrix) {
66 JS::ExposeObjectToActiveJS(mJSProjectionMatrix);
68 aRetval.set(mJSProjectionMatrix);
71 already_AddRefed<XRRigidTransform> XRView::GetTransform(ErrorResult& aRv) {
72 if (!mTransform) {
73 mTransform = new XRRigidTransform(mParent, mPosition, mOrientation);
75 RefPtr<XRRigidTransform> transform = mTransform;
76 return transform.forget();
79 } // namespace mozilla::dom