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
|
/*
* Copyright (C) 2020 Igalia S.L. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "WebXRFrame.h"
#if ENABLE(WEBXR)
#include "WebXRBoundedReferenceSpace.h"
#include "WebXRJointPose.h"
#include "WebXRJointSpace.h"
#include "WebXRReferenceSpace.h"
#include "WebXRSession.h"
#include "WebXRViewerPose.h"
#include <wtf/IsoMallocInlines.h>
namespace WebCore {
WTF_MAKE_ISO_ALLOCATED_IMPL(WebXRFrame);
Ref<WebXRFrame> WebXRFrame::create(WebXRSession& session, IsAnimationFrame isAnimationFrame)
{
return adoptRef(*new WebXRFrame(session, isAnimationFrame));
}
WebXRFrame::WebXRFrame(WebXRSession& session, IsAnimationFrame isAnimationFrame)
: m_isAnimationFrame(isAnimationFrame == IsAnimationFrame::Yes)
, m_session(session)
{
}
WebXRFrame::~WebXRFrame() = default;
bool WebXRFrame::isOutsideNativeBoundsOfBoundedReferenceSpace(const WebXRSpace& space, const WebXRSpace&) const
{
if (!is<WebXRBoundedReferenceSpace>(space))
return false;
// FIXME: return true whenever the distance from the bounded geometry of
// |space| to the native origin of |other| space is greater than 1m
// (suggested by specs).
return false;
}
bool WebXRFrame::isLocalReferenceSpace(const WebXRSpace& space) const
{
if (!is<WebXRReferenceSpace>(space))
return false;
auto type = downcast<WebXRReferenceSpace>(space).type();
if (type == XRReferenceSpaceType::Local || type == XRReferenceSpaceType::LocalFloor)
return true;
return false;
}
// https://immersive-web.github.io/webxr/#poses-must-be-limited
bool WebXRFrame::mustPosesBeLimited(const WebXRSpace& space, const WebXRSpace& baseSpace) const
{
if (isOutsideNativeBoundsOfBoundedReferenceSpace(space, baseSpace)
|| isOutsideNativeBoundsOfBoundedReferenceSpace(baseSpace, space))
return true;
if (isLocalReferenceSpace(space) || isLocalReferenceSpace(baseSpace)) {
// FIXME: If the distance between native origins of spaces is greater
// than 15m (suggested by specs) return true.
}
return false;
}
struct WebXRFrame::PopulatedPose {
TransformationMatrix transform;
bool emulatedPosition { false };
};
// https://immersive-web.github.io/webxr/#populate-the-pose
ExceptionOr<std::optional<WebXRFrame::PopulatedPose>> WebXRFrame::populatePose(const Document& document, const WebXRSpace& space, const WebXRSpace& baseSpace)
{
// 1. If frame’s active boolean is false, throw an InvalidStateError and abort these steps.
if (!m_active)
return Exception { InvalidStateError };
// 2. Let session be frame’s session object.
// 3. If space’s session does not equal session, throw an InvalidStateError and abort these steps.
if (space.session() != m_session.ptr())
return Exception { InvalidStateError };
// 4. If baseSpace’s session does not equal session, throw an InvalidStateError and abort these steps.
if (baseSpace.session() != m_session.ptr())
return Exception { InvalidStateError };
// 5. Check if poses may be reported and, if not, throw a SecurityError and abort these steps.
if (!m_session->posesCanBeReported(document))
return Exception { SecurityError };
// 6. Let limit be the result of whether poses must be limited between space and baseSpace.
// 7. Let transform be pose’s transform.
// 8. Query the XR device's tracking system for space’s pose relative to baseSpace at the frame’s time.
if (m_isAnimationFrame && !m_session->frameData().isTrackingValid) {
// FIXME: check if space’s pose relative to baseSpace has been determined in the past.
// Anyway this emulation is usually provided by the system in the pose (e.g. OpenXR)
// so we shouldn't hit this path in most XRPlatform ports.
return { std::nullopt };
}
auto baseTransform = baseSpace.effectiveOrigin();
if (!baseTransform)
return Exception { InvalidStateError };
if (!baseTransform.value().isInvertible())
return { std::nullopt };
auto effectiveOrigin = space.effectiveOrigin();
if (!effectiveOrigin)
return Exception { InvalidStateError };
auto transform = *baseTransform.value().inverse() * effectiveOrigin.value();
auto isPositionEmulated = space.isPositionEmulated();
if (!isPositionEmulated)
return Exception { InvalidStateError };
auto baseSpaceIsPositionEmulated = baseSpace.isPositionEmulated();
if (!baseSpaceIsPositionEmulated)
return Exception { InvalidStateError };
bool emulatedPosition = isPositionEmulated.value() || baseSpaceIsPositionEmulated.value();
bool limit = mustPosesBeLimited(space, baseSpace);
if (limit) {
// FIXME: apply pose limits logic
// https://immersive-web.github.io/webxr/#poses-must-be-limited
}
return { PopulatedPose { transform, emulatedPosition } };
}
// https://immersive-web.github.io/webxr/#dom-xrframe-getviewerpose
ExceptionOr<RefPtr<WebXRViewerPose>> WebXRFrame::getViewerPose(const Document& document, const WebXRReferenceSpace& referenceSpace)
{
// 1. Let frame be this.
// 2. Let session be frame’s session object.
// 3. If frame’s animationFrame boolean is false, throw an InvalidStateError and abort these steps.
if (!m_isAnimationFrame)
return Exception { InvalidStateError };
// 4. Let pose be a new XRViewerPose object in the relevant realm of session.
// 5. Populate the pose of session’s viewer reference space in referenceSpace at the time represented by frame into pose.
auto populatePoseResult = populatePose(document, m_session->viewerReferenceSpace(), referenceSpace);
if (populatePoseResult.hasException())
return populatePoseResult.releaseException();
// 6. If pose is null return null.
auto populateValue = populatePoseResult.releaseReturnValue();
if (!populateValue)
return nullptr;
RefPtr<WebXRViewerPose> pose = WebXRViewerPose::create(WebXRRigidTransform::create(populateValue->transform), populateValue->emulatedPosition);
// 7. Let xrviews be an empty list.
Vector<Ref<WebXRView>> xrViews;
// 8. For each active view view in the list of views on session, perform the following steps:
const auto& frameData = m_session->frameData();
for (auto& view : m_session->views()) {
auto index = xrViews.size();
if (!view.active || frameData.views.size() <= index)
continue;
// 8.1 Let xrview be a new XRView object in the relevant realm of session.
// 8.2 Initialize xrview’s underlying view to view.
// 8.3 Initialize xrview’s eye to view’s eye.
// 8.4 Initialize xrview’s frame time to frame’s time.
// 8.5 Initialize xrview’s session to session.
// 8.6. Let offset be an new XRRigidTransform object equal to the view offset of view in the relevant realm of session.
// 8.7. Set xrview’s transform property to the result of multiplying the XRViewerPose's transform by the offset transform in the relevant realm of session
auto offset = matrixFromPose(frameData.views[index].offset);
auto transform = WebXRRigidTransform::create(pose->transform().rawTransform() * offset);
// Set projection matrix for each view
std::array<float, 16> projection = WTF::switchOn(frameData.views[index].projection, [&](const PlatformXR::Device::FrameData::Fov& fov) {
double near = m_session->renderState().depthNear();
double far = m_session->renderState().depthFar();
return TransformationMatrix::fromProjection(fov.up, fov.down, fov.left, fov.right, near, far).toColumnMajorFloatArray();
}, [&](const std::array<float, 16>& matrix) {
return matrix;
}, [&](const std::nullptr_t&) {
// Use aspect projection for inline sessions
double fov = m_session->renderState().inlineVerticalFieldOfView().value_or(piOverTwoDouble);
float aspect = 1;
auto layer = m_session->renderState().baseLayer();
if (layer)
aspect = static_cast<double>(layer->framebufferWidth()) / static_cast<double>(layer->framebufferHeight());
double near = m_session->renderState().depthNear();
double far = m_session->renderState().depthFar();
return TransformationMatrix::fromProjection(fov, aspect, near, far).toColumnMajorFloatArray();
});
auto xrView = WebXRView::create(Ref { *this }, view.eye, WTFMove(transform), Float32Array::create(projection.data(), projection.size()));
xrView->setViewportModifiable(m_session->supportsViewportScaling());
// 8.8. Append xrview to xrviews
xrViews.append(WTFMove(xrView));
}
// 9. Set pose’s views to xrviews
pose->setViews(WTFMove(xrViews));
// 10. Return pose.
return pose;
}
ExceptionOr<RefPtr<WebXRPose>> WebXRFrame::getPose(const Document& document, const WebXRSpace& space, const WebXRSpace& baseSpace)
{
// 1. Let frame be this.
// 2. Let pose be a new XRPose object in the relevant realm of frame.
// 3. Populate the pose of space in baseSpace at the time represented by frame into pose.
auto populatePoseResult = populatePose(document, space, baseSpace);
if (populatePoseResult.hasException())
return populatePoseResult.releaseException();
auto populateValue = populatePoseResult.releaseReturnValue();
if (!populateValue)
return nullptr;
// 4. Return pose.
return RefPtr<WebXRPose>(WebXRPose::create(WebXRRigidTransform::create(populateValue->transform), populateValue->emulatedPosition));
}
TransformationMatrix WebXRFrame::matrixFromPose(const PlatformXR::Device::FrameData::Pose& pose)
{
TransformationMatrix matrix;
matrix.translate3d(pose.position.x(), pose.position.y(), pose.position.z());
matrix.multiply(TransformationMatrix::fromQuaternion(pose.orientation.x, pose.orientation.y, pose.orientation.z, pose.orientation.w));
return matrix;
}
#if ENABLE(WEBXR_HANDS)
// https://immersive-web.github.io/webxr-hand-input/#dom-xrframe-getjointpose
ExceptionOr<RefPtr<WebXRJointPose>> WebXRFrame::getJointPose(const WebXRJointSpace& joint, const WebXRSpace& baseSpace)
{
UNUSED_PARAM(joint);
UNUSED_PARAM(baseSpace);
return nullptr;
}
// https://immersive-web.github.io/webxr-hand-input/#dom-xrframe-filljointradii
ExceptionOr<bool> WebXRFrame::fillJointRadii(const Vector<RefPtr<WebXRJointSpace>>, const Float32Array&)
{
return true;
}
// https://immersive-web.github.io/webxr-hand-input/#dom-xrframe-fillposes
ExceptionOr<bool> WebXRFrame::fillPoses(const Vector<RefPtr<WebXRSpace>>, const WebXRSpace&, const Float32Array&)
{
return true;
}
#endif
} // namespace WebCore
#endif // ENABLE(WEBXR)
|