Skip to content

Commit

Permalink
Updated release for Jetpack XR Natives
Browse files Browse the repository at this point in the history
Release-Id: jetpack_xr_1.0.0-alpha02_RC03
  • Loading branch information
Googler authored and copybara-github committed Feb 12, 2025
1 parent 9e14657 commit 9d58fa8
Show file tree
Hide file tree
Showing 23 changed files with 2,603 additions and 1,073 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Jetpack XR Natives

Jetpack XR Natives is a collection of native libraries that are used by the
[Jetpack XR SDK](https://developer.android.com/develop/devices/xr/jetpack-xr-sdk).
[Jetpack XR SDK](https://developer.android.com/develop/xr/jetpack-xr-sdk).

## How to Build

Expand Down
2 changes: 1 addition & 1 deletion openxr/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ cc_library(
deps = [
":openxr_manager_clock",
":openxr_manager_utils",
"//third_party/OpenXR_KHR:openxr_headers_androidxr",
"//third_party/OpenXR_KHR:openxr_headers_androidxr", # build_cleaner: keep
"@abseil-cpp//absl/base:core_headers",
"@abseil-cpp//absl/base:no_destructor",
"@abseil-cpp//absl/container:flat_hash_map",
Expand Down
53 changes: 53 additions & 0 deletions openxr/hand_jni.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2025 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <jni.h>
#include <openxr/openxr.h>

#include <cstdint>

#include "openxr/jobject_creator.h"
#include "openxr/openxr_manager.h"

static jobject NativeLocateHandJoints(JNIEnv* env, bool is_left_hand,
jlong monotonic_time_ns) {
androidx::xr::openxr::OpenXrManager& xr_manager =
androidx::xr::openxr::OpenXrManager::GetOpenXrManager();

XrHandJointLocationsEXT hand_joints = {};
if (!xr_manager.LocateHandJoints(is_left_hand,
static_cast<int64_t>(monotonic_time_ns),
&hand_joints)) {
return nullptr;
}

return androidx::xr::openxr::CreateJavaHandState(env, hand_joints);
}

extern "C" {
JNIEXPORT jobject JNICALL
Java_androidx_xr_openxr_OpenXrHand_nativeLocateHandJoints(
JNIEnv* env, jclass /*clazz*/, jboolean is_left_hand,
jlong monotonic_time_ns) {
return NativeLocateHandJoints(env, is_left_hand, monotonic_time_ns);
}

JNIEXPORT jobject JNICALL
Java_androidx_xr_runtime_openxr_OpenXrHand_nativeLocateHandJoints(
JNIEnv* env, jclass /*clazz*/, jboolean is_left_hand,
jlong monotonic_time_ns) {
return NativeLocateHandJoints(env, is_left_hand, monotonic_time_ns);
}

} // extern "C"
37 changes: 32 additions & 5 deletions openxr/jobject_creator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,38 @@ jobject CreateJavaAnchorPersistenceState(

jlong CreateJavaAnchorHandle(const XrSpace& xr_space) {
static_assert(sizeof(XrSpace) <= sizeof(uint64_t));
#if defined(__aarch64__)
return reinterpret_cast<jlong>(xr_space);
#else
return static_cast<jlong>(reinterpret_cast<intptr_t>(&xr_space));
#endif
// jlong is signed, uint64_t is not, and xr_space originally starts as a
// pointer. Reinterpret into a value type, then static for the sign.
return static_cast<jlong>(reinterpret_cast<uint64_t>(xr_space));
}

jobject CreateJavaHandState(
JNIEnv* env, const XrHandJointLocationsEXT& xr_hand_joint_locations) {
jclass hand_state_class = GetJxrClass(env, PACKAGE_OPENXR, "HandState");
jmethodID hand_state_constructor =
env->GetMethodID(hand_state_class, "<init>", "(ZLjava/util/List;)V");

jclass array_list_class = env->FindClass("java/util/ArrayList");
jmethodID array_list_constructor =
env->GetMethodID(array_list_class, "<init>", "()V");
jobject array_list = env->NewObject(array_list_class, array_list_constructor);

jmethodID list_add_method =
env->GetMethodID(array_list_class, "add", "(Ljava/lang/Object;)Z");

for (uint32_t i = 0; i < xr_hand_joint_locations.jointCount; ++i) {
const XrHandJointLocationEXT& joint =
xr_hand_joint_locations.jointLocations[i];

if (joint.locationFlags & XR_SPACE_LOCATION_POSITION_VALID_BIT) {
jobject poseObject = CreateJavaPose(env, joint.pose);
env->CallBooleanMethod(array_list, list_add_method, poseObject);
env->DeleteLocalRef(poseObject);
}
}

return env->NewObject(hand_state_class, hand_state_constructor,
xr_hand_joint_locations.isActive, array_list);
}

} // namespace androidx::xr::openxr
5 changes: 5 additions & 0 deletions openxr/jobject_creator.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ jobject CreateJavaAnchorPersistenceState(
// Returns a JVM object of type `long` from an `XrSpace`.
jlong CreateJavaAnchorHandle(const XrSpace& xr_space);

// Returns a JVM object of type `androidx.xr.openxr.HandState` from an
// `XrHandJointLocationsEXT`.
jobject CreateJavaHandState(
JNIEnv* env, const XrHandJointLocationsEXT& xr_hand_joint_locations);

} // namespace androidx::xr::openxr

#endif // JETPACK_XR_NATIVES_OPENXR_JOBJECT_CREATOR_H_
Loading

0 comments on commit 9d58fa8

Please sign in to comment.