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
|
/*
* Copyright (C) 2025 Igalia, S.L.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* aint with this library; see the file COPYING.LIB. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "OpenXRExtensions.h"
#if ENABLE(WEBXR) && USE(OPENXR)
#include "OpenXRUtils.h"
#if USE(LIBEPOXY)
#define __GBM__ 1
#include <epoxy/egl.h>
#else
#include <EGL/egl.h>
#endif
#include <openxr/openxr_platform.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/TZoneMallocInlines.h>
#include <wtf/text/WTFString.h>
namespace WebKit {
WTF_MAKE_TZONE_ALLOCATED_IMPL(OpenXRExtensions);
OpenXRExtensions& OpenXRExtensions::singleton()
{
static NeverDestroyed<OpenXRExtensions> sharedExtensions;
return sharedExtensions;
}
OpenXRExtensions::OpenXRExtensions()
: m_methods(WTF::makeUnique<OpenXRExtensionMethods>())
{
uint32_t extensionCount;
XrResult result = xrEnumerateInstanceExtensionProperties(nullptr, 0, &extensionCount, nullptr);
if (XR_FAILED(result) || !extensionCount) {
LOG(XR, "xrEnumerateInstanceExtensionProperties(): no extensions\n");
return;
}
m_extensions.fill(createOpenXRStruct<XrExtensionProperties, XR_TYPE_EXTENSION_PROPERTIES>(), extensionCount);
result = xrEnumerateInstanceExtensionProperties(nullptr, extensionCount, &extensionCount, m_extensions.mutableSpan().data());
if (XR_FAILED(result)) {
LOG(XR, "xrEnumerateInstanceExtensionProperties() failed: %d\n", result);
return;
}
}
// Destructor must be explicitly defined here because at this point OpenXRExtensionMethods is already defined.
// If we don't do this, the compiler will try to generate the default destructor for this class the first time
// it finds it which might be too early, in the sense that the struct is not defined yet and thus it will fail.
OpenXRExtensions::~OpenXRExtensions() = default;
bool OpenXRExtensions::loadMethods(XrInstance instance)
{
#if defined(XR_USE_PLATFORM_EGL)
m_methods->getProcAddressFunc = eglGetProcAddress;
if (!m_methods->getProcAddressFunc) {
LOG(XR, "Failed to load eglGetProcAddress");
return false;
}
#endif
#if defined(XR_USE_GRAPHICS_API_OPENGL_ES)
xrGetInstanceProcAddr(instance, "xrGetOpenGLESGraphicsRequirementsKHR", reinterpret_cast<PFN_xrVoidFunction*>(&m_methods->xrGetOpenGLESGraphicsRequirementsKHR));
if (!m_methods->xrGetOpenGLESGraphicsRequirementsKHR) {
LOG(XR, "Failed to load xrGetOpenGLESGraphicsRequirementsKHR");
return false;
}
#endif
#if defined(XR_EXT_hand_tracking)
if (isExtensionSupported(XR_EXT_HAND_TRACKING_EXTENSION_NAME ""_span)) {
xrGetInstanceProcAddr(instance, "xrCreateHandTrackerEXT", reinterpret_cast<PFN_xrVoidFunction*>(&m_methods->xrCreateHandTrackerEXT));
xrGetInstanceProcAddr(instance, "xrDestroyHandTrackerEXT", reinterpret_cast<PFN_xrVoidFunction*>(&m_methods->xrDestroyHandTrackerEXT));
xrGetInstanceProcAddr(instance, "xrLocateHandJointsEXT", reinterpret_cast<PFN_xrVoidFunction*>(&m_methods->xrLocateHandJointsEXT));
RELEASE_ASSERT(m_methods->xrCreateHandTrackerEXT);
RELEASE_ASSERT(m_methods->xrDestroyHandTrackerEXT);
RELEASE_ASSERT(m_methods->xrLocateHandJointsEXT);
}
#endif
return true;
}
bool OpenXRExtensions::isExtensionSupported(std::span<const char> name) const
{
auto position = m_extensions.findIf([name](auto& property) {
return equalSpans(unsafeSpan(property.extensionName), name);
});
return position != notFound;
}
} // namespace WebKit
#endif // ENABLE(WEBXR) && USE(OPENXR)
|