tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

XRView.cpp (2556B)


      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/. */
      6 
      7 #include "mozilla/dom/XRView.h"
      8 
      9 #include "mozilla/HoldDropJSObjects.h"
     10 #include "mozilla/dom/Pose.h"
     11 #include "mozilla/dom/XRRigidTransform.h"
     12 #include "nsWrapperCache.h"
     13 
     14 namespace mozilla::dom {
     15 
     16 NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_WITH_JS_MEMBERS(XRView,
     17                                                      (mParent, mTransform),
     18                                                      (mJSProjectionMatrix))
     19 
     20 XRView::XRView(nsISupports* aParent, const XREye& aEye)
     21    : mParent(aParent),
     22      mEye(aEye),
     23 
     24      mJSProjectionMatrix(nullptr) {
     25  mozilla::HoldJSObjects(this);
     26 }
     27 
     28 XRView::~XRView() { mozilla::DropJSObjects(this); }
     29 
     30 JSObject* XRView::WrapObject(JSContext* aCx,
     31                             JS::Handle<JSObject*> aGivenProto) {
     32  return XRView_Binding::Wrap(aCx, this, aGivenProto);
     33 }
     34 
     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);
     43  }
     44  if (aProjectionMatrix != mProjectionMatrix) {
     45    mProjectionNeedsUpdate = true;
     46    mProjectionMatrix = aProjectionMatrix;
     47  }
     48 }
     49 
     50 XREye XRView::Eye() const { return mEye; }
     51 
     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;
     58 
     59    Pose::SetFloat32Array(aCx, this, aRetval, mJSProjectionMatrix,
     60                          mProjectionMatrix.components, 16, aRv);
     61    if (!mJSProjectionMatrix) {
     62      return;
     63    }
     64  }
     65  if (mJSProjectionMatrix) {
     66    JS::ExposeObjectToActiveJS(mJSProjectionMatrix);
     67  }
     68  aRetval.set(mJSProjectionMatrix);
     69 }
     70 
     71 already_AddRefed<XRRigidTransform> XRView::GetTransform(ErrorResult& aRv) {
     72  if (!mTransform) {
     73    mTransform = new XRRigidTransform(mParent, mPosition, mOrientation);
     74  }
     75  RefPtr<XRRigidTransform> transform = mTransform;
     76  return transform.forget();
     77 }
     78 
     79 }  // namespace mozilla::dom