DXR will be turned off on Tuesday, December 29th. It will redirect to Searchfox.
See the announcement on Discourse.

DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (c68fe15a81fc)

VCS Links

Line Code
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* -*- 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,
 * 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/. */
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/dom/XRFrame.h"
#include "mozilla/dom/XRRenderState.h"
#include "mozilla/dom/XRRenderState.h"
#include "mozilla/dom/XRViewerPose.h"
#include "mozilla/dom/XRView.h"

namespace mozilla {
namespace dom {
namespace dom {

NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(XRFrame, mParent, mSession)
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(XRFrame, AddRef)
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(XRFrame, Release)


XRFrame::XRFrame(nsISupports* aParent, XRSession* aXRSession)
    : mParent(aParent),
      mSession(aXRSession),
      mActive(false),
      mAnimationFrame(false) {}
      mAnimationFrame(false) {}

JSObject* XRFrame::WrapObject(JSContext* aCx,
                              JS::Handle<JSObject*> aGivenProto) {
  return XRFrame_Binding::Wrap(aCx, this, aGivenProto);
}
}

XRSession* XRFrame::Session() { return mSession; }

already_AddRefed<XRViewerPose> XRFrame::GetViewerPose(
already_AddRefed<XRViewerPose> XRFrame::GetViewerPose(
    const XRReferenceSpace& aReferenceSpace, ErrorResult& aRv) {
  if (!mActive || !mAnimationFrame) {
    aRv.ThrowInvalidStateError(
        "GetViewerPose can only be called on an XRFrame during an "
        "XRSession.requestAnimationFrame callback.");
        "XRSession.requestAnimationFrame callback.");
    return nullptr;
  }

  if (aReferenceSpace.GetSession() != mSession) {
    aRv.ThrowInvalidStateError(
    aRv.ThrowInvalidStateError(
        "The XRReferenceSpace passed to GetViewerPose must belong to the "
        "XRSession that GetViewerPose is called on.");
    return nullptr;
  }


  // TODO (Bug 1616390) - Validate that poses may be reported:
  // https://immersive-web.github.io/webxr/#poses-may-be-reported

  // TODO (Bug 1616393) - Check if poses must be limited:
  // https://immersive-web.github.io/webxr/#poses-must-be-limited
  // https://immersive-web.github.io/webxr/#poses-must-be-limited

  bool emulatedPosition = aReferenceSpace.IsPositionEmulated();

  XRRenderState* renderState = mSession->GetActiveRenderState();

  float depthNear = (float)renderState->DepthNear();
  float depthFar = (float)renderState->DepthFar();

  RefPtr<XRViewerPose> viewerPose;

  RefPtr<XRViewerPose> viewerPose;
  gfx::VRDisplayClient* display = mSession->GetDisplayClient();
  if (display) {
    // Have a VRDisplayClient
    const VRDisplayInfo& displayInfo =
        mSession->GetDisplayClient()->GetDisplayInfo();
    const gfx::VRHMDSensorState& sensorState = display->GetSensorState();

        mSession->GetDisplayClient()->GetDisplayInfo();
    const gfx::VRHMDSensorState& sensorState = display->GetSensorState();
    gfx::PointDouble3D viewerPosition = gfx::PointDouble3D(
        sensorState.pose.position[0], sensorState.pose.position[1],
        sensorState.pose.position[2]);
    gfx::QuaternionDouble viewerOrientation = gfx::QuaternionDouble(
                                        sensorState.pose.position[1],
        sensorState.pose.orientation[0], sensorState.pose.orientation[1],
        sensorState.pose.orientation[2], sensorState.pose.orientation[3]);

    // Quaternion was inverted for WebVR. We need to invert it here again.
    // TODO: Remove those extra inverts when WebVR support is disabled.

    viewerOrientation.Invert();

    gfx::Matrix4x4Double headTransform;
    headTransform.SetRotationFromQuaternion(viewerOrientation);
    headTransform.PostTranslate(viewerPosition);
    viewerPose = mSession->PooledViewerPose(viewerPosition, viewerOrientation,

    gfx::Matrix4x4Double originTransform;
    originTransform.SetRotationFromQuaternion(
        aReferenceSpace.GetEffectiveOriginOrientation());
    originTransform.PreTranslate(-aReferenceSpace.GetEffectiveOriginPosition());
    headTransform.PostTranslate(viewerPosition);

    headTransform *= originTransform;

    viewerPose = mSession->PooledViewerPose(headTransform, emulatedPosition);

    originTransform.PreTranslate(-aReferenceSpace.GetEffectiveOriginPosition());
    auto updateEye = [&](int32_t viewIndex, VRDisplayState::Eye eye) {
      auto offset = displayInfo.GetEyeTranslation(eye);
      auto eyeFromHead = gfx::Matrix4x4Double::Translation(
          gfx::PointDouble3D(offset.x, offset.y, offset.z));
    auto updateEye = [&](int32_t viewIndex, VRDisplayState::Eye eye) {
      auto eyeTransform = eyeFromHead * headTransform;
      gfx::PointDouble3D eyePosition;
      gfx::QuaternionDouble eyeRotation;
      gfx::PointDouble3D eyeScale;
      eyeTransform.Decompose(eyePosition, eyeRotation, eyeScale);
      gfx::PointDouble3D eyePosition;

      const gfx::VRFieldOfView fov = displayInfo.mDisplayState.eyeFOV[eye];
      Matrix4x4 projection =
          fov.ConstructProjectionMatrix(depthNear, depthFar, true);
      viewerPose->GetEye(viewIndex)->Update(eyePosition, eyeRotation,
                                            projection);
          fov.ConstructProjectionMatrix(depthNear, depthFar, true);
    };

    updateEye(0, gfx::VRDisplayState::Eye_Left);
    updateEye(1, gfx::VRDisplayState::Eye_Right);
  } else {
    updateEye(0, gfx::VRDisplayState::Eye_Left);
    auto inlineVerticalFov = renderState->GetInlineVerticalFieldOfView();
    const double fov =
        inlineVerticalFov.IsNull() ? M_PI * 0.5f : inlineVerticalFov.Value();
    HTMLCanvasElement* canvas = renderState->GetOutputCanvas();
    float aspect = 1.0f;
        inlineVerticalFov.IsNull() ? M_PI * 0.5f : inlineVerticalFov.Value();
    if (canvas) {
      aspect = (float)canvas->Width() / (float)canvas->Height();
    }
    Matrix4x4 projection =
        ConstructInlineProjection((float)fov, aspect, depthNear, depthFar);

    }
    viewerPose =
        mSession->PooledViewerPose(gfx::Matrix4x4Double(), emulatedPosition);
    viewerPose->GetEye(0)->Update(gfx::PointDouble3D(), gfx::QuaternionDouble(),
                                  projection);
  }
                                            emulatedPosition);

  return viewerPose.forget();
  }
}


already_AddRefed<XRPose> XRFrame::GetPose(const XRSpace& aSpace,
                                          const XRSpace& aBaseSpace,

                                          ErrorResult& aRv) {
  if (!mActive) {
    aRv.ThrowInvalidStateError(
        "GetPose can not be called on an XRFrame that is not active.");
    return nullptr;
    aRv.ThrowInvalidStateError(
  }

  if (aSpace.GetSession() != mSession || aBaseSpace.GetSession() != mSession) {
    aRv.ThrowInvalidStateError(
        "The XRSpace passed to GetPose must belong to the "
        "XRSession that GetPose is called on.");
    return nullptr;
  }
        "XRSession that GetPose is called on.");

  // TODO (Bug 1616390) - Validate that poses may be reported:
  // https://immersive-web.github.io/webxr/#poses-may-be-reported
  if (aSpace.GetSession()->VisibilityState() != XRVisibilityState::Visible) {
    aRv.ThrowInvalidStateError(
  // https://immersive-web.github.io/webxr/#poses-may-be-reported
        "An XRSpace ’s visibilityState in not 'visible'.");
    return nullptr;
  }

  // TODO (Bug 1616393) - Check if poses must be limited:
  }
  // https://immersive-web.github.io/webxr/#poses-must-be-limited

  const bool emulatedPosition = aSpace.IsPositionEmulated();
  gfx::Matrix4x4Double base;

  base.SetRotationFromQuaternion(aBaseSpace.GetEffectiveOriginOrientation());
  base.PreTranslate(-aBaseSpace.GetEffectiveOriginPosition());

  gfx::Matrix4x4Double matrix = aSpace.GetEffectiveOriginTransform() * base;


  RefPtr<XRRigidTransform> transform = new XRRigidTransform(mParent, matrix);
  RefPtr<XRPose> pose = new XRPose(mParent, transform, emulatedPosition);

  return pose.forget();
}


void XRFrame::StartAnimationFrame() {
  mActive = true;
  mAnimationFrame = true;
}

  mActive = true;
  mAnimationFrame = true;
void XRFrame::EndAnimationFrame() { mActive = false; }

void XRFrame::StartInputSourceEvent() { mActive = true; }

void XRFrame::EndInputSourceEvent() { mActive = false; }
void XRFrame::StartInputSourceEvent() { mActive = true; }

gfx::Matrix4x4 XRFrame::ConstructInlineProjection(float aFov, float aAspect,
                                                  float aNear, float aFar) {
  Matrix4x4 m;
  const float depth = aFar - aNear;
                                                  float aNear, float aFar) {
  const float invDepth = 1 / depth;
  if (aFov == 0) {
    aFov = 0.5f * M_PI;
  }

    aFov = 0.5f * M_PI;
  m._22 = 1.0f / tan(0.5f * aFov);
  m._11 = -m._22 / aAspect;
  m._33 = depth * invDepth;
  m._43 = (-aFar * aNear) * invDepth;
  m._34 = 1.0f;
  m._11 = -m._22 / aAspect;

  return m;
}

}  // namespace dom
  return m;
}  // namespace mozilla