blob: 299c1e557eff46fd348bc90103eb69c848ed6a75 [file] [log] [blame]
dino@apple.com1cc2a442021-10-18 08:05:05 +00001/*
2 * Copyright (C) 2021 Apple, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "WebXRHand.h"
28
29#if ENABLE(WEBXR) && ENABLE(WEBXR_HANDS)
30
31#include "WebXRInputSource.h"
32#include <wtf/IsoMallocInlines.h>
33
34namespace WebCore {
35
36WTF_MAKE_ISO_ALLOCATED_IMPL(WebXRHand);
37
38Ref<WebXRHand> WebXRHand::create(const WebXRInputSource& inputSource)
39{
40 return adoptRef(*new WebXRHand(inputSource));
41}
42
43WebXRHand::WebXRHand(const WebXRInputSource& inputSource)
44 : m_inputSource(inputSource)
45{
commit-queue@webkit.org54a6cea2022-04-12 18:52:27 +000046 auto* session = this->session();
47 auto* document = session ? downcast<Document>(session->scriptExecutionContext()) : nullptr;
48 if (!document)
49 return;
50
51 size_t jointCount = static_cast<size_t>(XRHandJoint::Count);
52 Vector<Ref<WebXRJointSpace>> joints;
53 joints.reserveInitialCapacity(jointCount);
54 for (size_t i = 0; i < jointCount; ++i)
55 joints.uncheckedAppend(WebXRJointSpace::create(*document, *this, static_cast<XRHandJoint>(i)));
56 m_joints = WTFMove(joints);
dino@apple.com1cc2a442021-10-18 08:05:05 +000057}
58
59WebXRHand::~WebXRHand() = default;
60
61RefPtr<WebXRJointSpace> WebXRHand::get(const XRHandJoint& key)
62{
commit-queue@webkit.org54a6cea2022-04-12 18:52:27 +000063 size_t jointIndex = static_cast<size_t>(key);
64 if (jointIndex >= m_joints.size())
65 return nullptr;
66
67 return m_joints[jointIndex].ptr();
dino@apple.com1cc2a442021-10-18 08:05:05 +000068}
69
70WebXRHand::Iterator::Iterator(WebXRHand& hand)
71 : m_hand(hand)
72{
73}
74
Hironori.Fujii@sony.com6e2fa9f2021-10-26 07:23:01 +000075std::optional<KeyValuePair<XRHandJoint, RefPtr<WebXRJointSpace>>> WebXRHand::Iterator::next()
dino@apple.com1cc2a442021-10-18 08:05:05 +000076{
commit-queue@webkit.org54a6cea2022-04-12 18:52:27 +000077 if (m_index >= m_hand->m_joints.size())
dino@apple.com1cc2a442021-10-18 08:05:05 +000078 return std::nullopt;
79
commit-queue@webkit.org54a6cea2022-04-12 18:52:27 +000080 size_t index = m_index++;
81 return KeyValuePair<XRHandJoint, RefPtr<WebXRJointSpace>> { static_cast<XRHandJoint>(index), m_hand->m_joints[index].ptr() };
dino@apple.com1cc2a442021-10-18 08:05:05 +000082}
83
84WebXRSession* WebXRHand::session()
85{
86 if (!m_inputSource)
87 return nullptr;
commit-queue@webkit.org54a6cea2022-04-12 18:52:27 +000088
dino@apple.com1cc2a442021-10-18 08:05:05 +000089 return m_inputSource.get()->session();
90}
91
commit-queue@webkit.org54a6cea2022-04-12 18:52:27 +000092void WebXRHand::updateFromInputSource(const PlatformXR::Device::FrameData::InputSource& inputSource)
93{
94 if (!inputSource.handJoints) {
95 m_hasMissingPoses = true;
96 return;
97 }
98
99 auto& handJoints = *(inputSource.handJoints);
100 if (handJoints.size() != m_joints.size()) {
101 m_hasMissingPoses = true;
102 return;
103 }
104
105 bool hasMissingPoses = false;
106 for (size_t i = 0; i < handJoints.size(); ++i) {
107 if (!handJoints[i])
108 hasMissingPoses = true;
109
110 m_joints[i]->updateFromJoint(handJoints[i]);
111 }
112 m_hasMissingPoses = hasMissingPoses;
113}
114
dino@apple.com1cc2a442021-10-18 08:05:05 +0000115}
116
117#endif