dino@apple.com | 1cc2a44 | 2021-10-18 08:05:05 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 34 | namespace WebCore { |
| 35 | |
| 36 | WTF_MAKE_ISO_ALLOCATED_IMPL(WebXRHand); |
| 37 | |
| 38 | Ref<WebXRHand> WebXRHand::create(const WebXRInputSource& inputSource) |
| 39 | { |
| 40 | return adoptRef(*new WebXRHand(inputSource)); |
| 41 | } |
| 42 | |
| 43 | WebXRHand::WebXRHand(const WebXRInputSource& inputSource) |
| 44 | : m_inputSource(inputSource) |
| 45 | { |
commit-queue@webkit.org | 54a6cea | 2022-04-12 18:52:27 +0000 | [diff] [blame] | 46 | 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.com | 1cc2a44 | 2021-10-18 08:05:05 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | WebXRHand::~WebXRHand() = default; |
| 60 | |
| 61 | RefPtr<WebXRJointSpace> WebXRHand::get(const XRHandJoint& key) |
| 62 | { |
commit-queue@webkit.org | 54a6cea | 2022-04-12 18:52:27 +0000 | [diff] [blame] | 63 | 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.com | 1cc2a44 | 2021-10-18 08:05:05 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | WebXRHand::Iterator::Iterator(WebXRHand& hand) |
| 71 | : m_hand(hand) |
| 72 | { |
| 73 | } |
| 74 | |
Hironori.Fujii@sony.com | 6e2fa9f | 2021-10-26 07:23:01 +0000 | [diff] [blame] | 75 | std::optional<KeyValuePair<XRHandJoint, RefPtr<WebXRJointSpace>>> WebXRHand::Iterator::next() |
dino@apple.com | 1cc2a44 | 2021-10-18 08:05:05 +0000 | [diff] [blame] | 76 | { |
commit-queue@webkit.org | 54a6cea | 2022-04-12 18:52:27 +0000 | [diff] [blame] | 77 | if (m_index >= m_hand->m_joints.size()) |
dino@apple.com | 1cc2a44 | 2021-10-18 08:05:05 +0000 | [diff] [blame] | 78 | return std::nullopt; |
| 79 | |
commit-queue@webkit.org | 54a6cea | 2022-04-12 18:52:27 +0000 | [diff] [blame] | 80 | size_t index = m_index++; |
| 81 | return KeyValuePair<XRHandJoint, RefPtr<WebXRJointSpace>> { static_cast<XRHandJoint>(index), m_hand->m_joints[index].ptr() }; |
dino@apple.com | 1cc2a44 | 2021-10-18 08:05:05 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | WebXRSession* WebXRHand::session() |
| 85 | { |
| 86 | if (!m_inputSource) |
| 87 | return nullptr; |
commit-queue@webkit.org | 54a6cea | 2022-04-12 18:52:27 +0000 | [diff] [blame] | 88 | |
dino@apple.com | 1cc2a44 | 2021-10-18 08:05:05 +0000 | [diff] [blame] | 89 | return m_inputSource.get()->session(); |
| 90 | } |
| 91 | |
commit-queue@webkit.org | 54a6cea | 2022-04-12 18:52:27 +0000 | [diff] [blame] | 92 | void 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.com | 1cc2a44 | 2021-10-18 08:05:05 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | #endif |