2020-12-26 14:12:05 -08:00
|
|
|
// Copyright (c) FIRST and other WPILib contributors.
|
|
|
|
|
// Open Source Software; you can modify and/or share it under the terms of
|
|
|
|
|
// the WPILib BSD license file in the root directory of this project.
|
2019-09-26 22:53:21 -07:00
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "WPIUtilJNI.hpp"
|
2023-05-17 14:06:52 -07:00
|
|
|
|
2019-09-26 22:53:21 -07:00
|
|
|
#include <jni.h>
|
|
|
|
|
|
2025-11-07 19:55:43 -05:00
|
|
|
#include "org_wpilib_util_WPIUtilJNI.h"
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "wpi/util/RawFrame.h"
|
|
|
|
|
#include "wpi/util/RuntimeCheck.h"
|
|
|
|
|
#include "wpi/util/Synchronization.h"
|
|
|
|
|
#include "wpi/util/jni_util.hpp"
|
|
|
|
|
#include "wpi/util/print.hpp"
|
|
|
|
|
#include "wpi/util/timestamp.h"
|
2019-09-26 22:53:21 -07:00
|
|
|
|
|
|
|
|
using namespace wpi::java;
|
|
|
|
|
|
2021-05-31 10:35:54 -07:00
|
|
|
static bool mockTimeEnabled = false;
|
|
|
|
|
static uint64_t mockNow = 0;
|
|
|
|
|
|
2023-05-17 14:06:52 -07:00
|
|
|
static JException illegalArgEx;
|
|
|
|
|
static JException indexOobEx;
|
2021-08-03 00:05:47 -07:00
|
|
|
static JException interruptedEx;
|
2024-05-12 14:09:43 -07:00
|
|
|
static JException ioEx;
|
2023-05-17 14:06:52 -07:00
|
|
|
static JException nullPointerEx;
|
2024-11-05 08:51:48 -08:00
|
|
|
static JException msvcRuntimeEx;
|
2023-05-17 14:06:52 -07:00
|
|
|
|
|
|
|
|
static const JExceptionInit exceptions[] = {
|
|
|
|
|
{"java/lang/IllegalArgumentException", &illegalArgEx},
|
|
|
|
|
{"java/lang/IndexOutOfBoundsException", &indexOobEx},
|
|
|
|
|
{"java/lang/InterruptedException", &interruptedEx},
|
2024-05-12 14:09:43 -07:00
|
|
|
{"java/io/IOException", &ioEx},
|
2024-11-05 08:51:48 -08:00
|
|
|
{"java/lang/NullPointerException", &nullPointerEx},
|
2025-11-07 19:55:43 -05:00
|
|
|
{"org/wpilib/util/MsvcRuntimeException", &msvcRuntimeEx}};
|
2023-05-17 14:06:52 -07:00
|
|
|
|
|
|
|
|
void wpi::ThrowIllegalArgumentException(JNIEnv* env, std::string_view msg) {
|
|
|
|
|
illegalArgEx.Throw(env, msg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void wpi::ThrowIndexOobException(JNIEnv* env, std::string_view msg) {
|
|
|
|
|
indexOobEx.Throw(env, msg);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-12 14:09:43 -07:00
|
|
|
void wpi::ThrowIOException(JNIEnv* env, std::string_view msg) {
|
|
|
|
|
ioEx.Throw(env, msg);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-17 14:06:52 -07:00
|
|
|
void wpi::ThrowNullPointerException(JNIEnv* env, std::string_view msg) {
|
|
|
|
|
nullPointerEx.Throw(env, msg);
|
|
|
|
|
}
|
2021-08-03 00:05:47 -07:00
|
|
|
|
2019-09-26 22:53:21 -07:00
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
|
|
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved) {
|
|
|
|
|
JNIEnv* env;
|
2020-12-28 12:58:06 -08:00
|
|
|
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
|
2019-09-26 22:53:21 -07:00
|
|
|
return JNI_ERR;
|
2020-12-28 12:58:06 -08:00
|
|
|
}
|
2019-09-26 22:53:21 -07:00
|
|
|
|
2023-05-17 14:06:52 -07:00
|
|
|
for (auto& c : exceptions) {
|
|
|
|
|
*c.cls = JException(env, c.name);
|
|
|
|
|
if (!*c.cls) {
|
|
|
|
|
return JNI_ERR;
|
|
|
|
|
}
|
2021-08-03 00:05:47 -07:00
|
|
|
}
|
|
|
|
|
|
2019-09-26 22:53:21 -07:00
|
|
|
return JNI_VERSION_1_6;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 00:05:47 -07:00
|
|
|
JNIEXPORT void JNICALL JNI_OnUnload(JavaVM* vm, void* reserved) {
|
|
|
|
|
JNIEnv* env;
|
|
|
|
|
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-11-27 11:16:24 -08:00
|
|
|
|
2023-05-17 14:06:52 -07:00
|
|
|
for (auto& c : exceptions) {
|
|
|
|
|
c.cls->free(env);
|
|
|
|
|
}
|
2021-08-03 00:05:47 -07:00
|
|
|
}
|
2019-09-26 22:53:21 -07:00
|
|
|
|
2024-11-05 08:51:48 -08:00
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2024-11-05 08:51:48 -08:00
|
|
|
* Method: checkMsvcRuntime
|
|
|
|
|
* Signature: ()V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_checkMsvcRuntime
|
2024-11-05 08:51:48 -08:00
|
|
|
(JNIEnv* env, jclass)
|
|
|
|
|
{
|
|
|
|
|
uint32_t foundMajor;
|
|
|
|
|
uint32_t foundMinor;
|
|
|
|
|
uint32_t expectedMajor;
|
|
|
|
|
uint32_t expectedMinor;
|
|
|
|
|
WPI_String runtimePath;
|
|
|
|
|
|
|
|
|
|
if (!WPI_IsRuntimeValid(&foundMajor, &foundMinor, &expectedMajor,
|
|
|
|
|
&expectedMinor, &runtimePath)) {
|
|
|
|
|
static jmethodID ctor =
|
|
|
|
|
env->GetMethodID(msvcRuntimeEx, "<init>", "(IIIILjava/lang/String;)V");
|
|
|
|
|
jstring jmsvcruntime = MakeJString(env, wpi::to_string_view(&runtimePath));
|
|
|
|
|
jobject exception =
|
|
|
|
|
env->NewObject(msvcRuntimeEx, ctor, foundMajor, foundMinor,
|
|
|
|
|
expectedMajor, expectedMinor, jmsvcruntime);
|
|
|
|
|
WPI_FreeString(&runtimePath);
|
|
|
|
|
env->Throw(static_cast<jthrowable>(exception));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 20:40:16 -07:00
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2022-08-30 20:40:16 -07:00
|
|
|
* Method: writeStderr
|
|
|
|
|
* Signature: (Ljava/lang/String;)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_writeStderr
|
2022-08-30 20:40:16 -07:00
|
|
|
(JNIEnv* env, jclass, jstring str)
|
|
|
|
|
{
|
2024-05-12 06:25:42 -07:00
|
|
|
wpi::print(stderr, "{}", JStringRef{env, str}.str());
|
2022-08-30 20:40:16 -07:00
|
|
|
}
|
|
|
|
|
|
2021-05-31 10:35:54 -07:00
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2021-05-31 10:35:54 -07:00
|
|
|
* Method: enableMockTime
|
|
|
|
|
* Signature: ()V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_enableMockTime
|
2021-05-31 10:35:54 -07:00
|
|
|
(JNIEnv*, jclass)
|
|
|
|
|
{
|
2025-06-02 16:42:56 -07:00
|
|
|
#ifdef __FRC_SYSTEMCORE__
|
|
|
|
|
wpi::print(stderr, "WPIUtil: Mocking time is not available on systemcore\n");
|
2023-02-12 22:38:34 -08:00
|
|
|
#else
|
2021-05-31 10:35:54 -07:00
|
|
|
mockTimeEnabled = true;
|
|
|
|
|
wpi::SetNowImpl([] { return mockNow; });
|
2023-02-12 22:38:34 -08:00
|
|
|
#endif
|
2021-05-31 10:35:54 -07:00
|
|
|
}
|
|
|
|
|
|
2021-12-27 11:51:32 -06:00
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2021-12-27 11:51:32 -06:00
|
|
|
* Method: disableMockTime
|
|
|
|
|
* Signature: ()V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_disableMockTime
|
2021-12-27 11:51:32 -06:00
|
|
|
(JNIEnv*, jclass)
|
|
|
|
|
{
|
|
|
|
|
mockTimeEnabled = false;
|
|
|
|
|
wpi::SetNowImpl(nullptr);
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 10:35:54 -07:00
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2021-05-31 10:35:54 -07:00
|
|
|
* Method: setMockTime
|
|
|
|
|
* Signature: (J)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_setMockTime
|
2021-05-31 10:35:54 -07:00
|
|
|
(JNIEnv*, jclass, jlong time)
|
|
|
|
|
{
|
|
|
|
|
mockNow = time;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-06 21:37:38 -07:00
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2020-08-06 21:37:38 -07:00
|
|
|
* Method: now
|
|
|
|
|
* Signature: ()J
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jlong JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_now
|
2020-08-06 21:37:38 -07:00
|
|
|
(JNIEnv*, jclass)
|
|
|
|
|
{
|
2021-05-31 10:35:54 -07:00
|
|
|
if (mockTimeEnabled) {
|
|
|
|
|
return mockNow;
|
|
|
|
|
} else {
|
|
|
|
|
return wpi::Now();
|
|
|
|
|
}
|
2020-08-06 21:37:38 -07:00
|
|
|
}
|
|
|
|
|
|
2021-12-28 01:06:31 -06:00
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2021-12-28 01:06:31 -06:00
|
|
|
* Method: getSystemTime
|
|
|
|
|
* Signature: ()J
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jlong JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_getSystemTime
|
2021-12-28 01:06:31 -06:00
|
|
|
(JNIEnv*, jclass)
|
|
|
|
|
{
|
|
|
|
|
return wpi::GetSystemTime();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-03 00:05:47 -07:00
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2021-08-03 00:05:47 -07:00
|
|
|
* Method: createEvent
|
|
|
|
|
* Signature: (ZZ)I
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jint JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_createEvent
|
2021-08-03 00:05:47 -07:00
|
|
|
(JNIEnv*, jclass, jboolean manualReset, jboolean initialState)
|
|
|
|
|
{
|
|
|
|
|
return wpi::CreateEvent(manualReset, initialState);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2021-08-03 00:05:47 -07:00
|
|
|
* Method: destroyEvent
|
|
|
|
|
* Signature: (I)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_destroyEvent
|
2021-08-03 00:05:47 -07:00
|
|
|
(JNIEnv*, jclass, jint eventHandle)
|
|
|
|
|
{
|
|
|
|
|
wpi::DestroyEvent(eventHandle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2021-08-03 00:05:47 -07:00
|
|
|
* Method: setEvent
|
|
|
|
|
* Signature: (I)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_setEvent
|
2021-08-03 00:05:47 -07:00
|
|
|
(JNIEnv*, jclass, jint eventHandle)
|
|
|
|
|
{
|
|
|
|
|
wpi::SetEvent(eventHandle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2021-08-03 00:05:47 -07:00
|
|
|
* Method: resetEvent
|
|
|
|
|
* Signature: (I)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_resetEvent
|
2021-08-03 00:05:47 -07:00
|
|
|
(JNIEnv*, jclass, jint eventHandle)
|
|
|
|
|
{
|
|
|
|
|
wpi::ResetEvent(eventHandle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2021-08-03 00:05:47 -07:00
|
|
|
* Method: createSemaphore
|
|
|
|
|
* Signature: (II)I
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jint JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_createSemaphore
|
2021-08-03 00:05:47 -07:00
|
|
|
(JNIEnv*, jclass, jint initialCount, jint maximumCount)
|
|
|
|
|
{
|
|
|
|
|
return wpi::CreateSemaphore(initialCount, maximumCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2021-08-03 00:05:47 -07:00
|
|
|
* Method: destroySemaphore
|
|
|
|
|
* Signature: (I)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_destroySemaphore
|
2021-08-03 00:05:47 -07:00
|
|
|
(JNIEnv*, jclass, jint semHandle)
|
|
|
|
|
{
|
|
|
|
|
wpi::DestroySemaphore(semHandle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2021-08-03 00:05:47 -07:00
|
|
|
* Method: releaseSemaphore
|
|
|
|
|
* Signature: (II)Z
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jboolean JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_releaseSemaphore
|
2021-08-03 00:05:47 -07:00
|
|
|
(JNIEnv*, jclass, jint semHandle, jint releaseCount)
|
|
|
|
|
{
|
|
|
|
|
return wpi::ReleaseSemaphore(semHandle, releaseCount);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2021-08-03 00:05:47 -07:00
|
|
|
* Method: waitForObject
|
|
|
|
|
* Signature: (I)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_waitForObject
|
2021-08-03 00:05:47 -07:00
|
|
|
(JNIEnv* env, jclass, jint handle)
|
|
|
|
|
{
|
|
|
|
|
if (!wpi::WaitForObject(handle)) {
|
|
|
|
|
interruptedEx.Throw(env, "WaitForObject interrupted");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2021-08-03 00:05:47 -07:00
|
|
|
* Method: waitForObjectTimeout
|
|
|
|
|
* Signature: (ID)Z
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jboolean JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_waitForObjectTimeout
|
2021-08-03 00:05:47 -07:00
|
|
|
(JNIEnv* env, jclass, jint handle, jdouble timeout)
|
|
|
|
|
{
|
|
|
|
|
bool timedOut;
|
|
|
|
|
if (!wpi::WaitForObject(handle, timeout, &timedOut) && !timedOut) {
|
|
|
|
|
interruptedEx.Throw(env, "WaitForObject interrupted");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return timedOut;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2021-08-03 00:05:47 -07:00
|
|
|
* Method: waitForObjects
|
|
|
|
|
* Signature: ([I)[I
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jintArray JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_waitForObjects
|
2021-08-03 00:05:47 -07:00
|
|
|
(JNIEnv* env, jclass, jintArray handles)
|
|
|
|
|
{
|
2023-08-24 00:02:56 -07:00
|
|
|
JSpan<const jint> handlesArr{env, handles};
|
2021-08-03 00:05:47 -07:00
|
|
|
wpi::SmallVector<WPI_Handle, 8> signaledBuf;
|
|
|
|
|
signaledBuf.resize(handlesArr.size());
|
2022-10-15 16:33:14 -07:00
|
|
|
std::span<const WPI_Handle> handlesArr2{
|
2023-08-24 00:02:56 -07:00
|
|
|
reinterpret_cast<const WPI_Handle*>(handlesArr.data()),
|
2021-08-03 00:05:47 -07:00
|
|
|
handlesArr.size()};
|
|
|
|
|
|
|
|
|
|
auto signaled = wpi::WaitForObjects(handlesArr2, signaledBuf);
|
|
|
|
|
if (signaled.empty()) {
|
|
|
|
|
interruptedEx.Throw(env, "WaitForObjects interrupted");
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return MakeJIntArray(env, signaled);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2021-08-03 00:05:47 -07:00
|
|
|
* Method: waitForObjectsTimeout
|
|
|
|
|
* Signature: ([ID)[I
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jintArray JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_waitForObjectsTimeout
|
2021-08-03 00:05:47 -07:00
|
|
|
(JNIEnv* env, jclass, jintArray handles, jdouble timeout)
|
|
|
|
|
{
|
2023-08-24 00:02:56 -07:00
|
|
|
JSpan<const jint> handlesArr{env, handles};
|
2021-08-03 00:05:47 -07:00
|
|
|
wpi::SmallVector<WPI_Handle, 8> signaledBuf;
|
|
|
|
|
signaledBuf.resize(handlesArr.size());
|
2022-10-15 16:33:14 -07:00
|
|
|
std::span<const WPI_Handle> handlesArr2{
|
2023-08-24 00:02:56 -07:00
|
|
|
reinterpret_cast<const WPI_Handle*>(handlesArr.data()),
|
2021-08-03 00:05:47 -07:00
|
|
|
handlesArr.size()};
|
|
|
|
|
|
|
|
|
|
bool timedOut;
|
|
|
|
|
auto signaled =
|
|
|
|
|
wpi::WaitForObjects(handlesArr2, signaledBuf, timeout, &timedOut);
|
|
|
|
|
if (signaled.empty() && !timedOut) {
|
|
|
|
|
interruptedEx.Throw(env, "WaitForObjects interrupted");
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return MakeJIntArray(env, signaled);
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-23 13:55:10 -05:00
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2023-11-23 13:55:10 -05:00
|
|
|
* Method: allocateRawFrame
|
|
|
|
|
* Signature: ()J
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jlong JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_allocateRawFrame
|
2023-11-23 13:55:10 -05:00
|
|
|
(JNIEnv*, jclass)
|
|
|
|
|
{
|
2023-12-26 20:05:02 -08:00
|
|
|
return reinterpret_cast<jlong>(new wpi::RawFrame);
|
2023-11-23 13:55:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2023-11-23 13:55:10 -05:00
|
|
|
* Method: freeRawFrame
|
|
|
|
|
* Signature: (J)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_freeRawFrame
|
2023-12-26 20:05:02 -08:00
|
|
|
(JNIEnv*, jclass, jlong frame)
|
2023-11-23 13:55:10 -05:00
|
|
|
{
|
2023-12-26 20:05:02 -08:00
|
|
|
delete reinterpret_cast<wpi::RawFrame*>(frame);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2023-12-26 20:05:02 -08:00
|
|
|
* Method: getRawFrameDataPtr
|
|
|
|
|
* Signature: (J)J
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT jlong JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_getRawFrameDataPtr
|
2023-12-26 20:05:02 -08:00
|
|
|
(JNIEnv* env, jclass, jlong frame)
|
|
|
|
|
{
|
|
|
|
|
auto* f = reinterpret_cast<wpi::RawFrame*>(frame);
|
|
|
|
|
if (!f) {
|
|
|
|
|
wpi::ThrowNullPointerException(env, "frame is null");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
return reinterpret_cast<jlong>(f->data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2023-12-26 20:05:02 -08:00
|
|
|
* Method: setRawFrameData
|
|
|
|
|
* Signature: (JLjava/lang/Object;IIIII)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_setRawFrameData
|
2023-12-26 20:05:02 -08:00
|
|
|
(JNIEnv* env, jclass, jlong frame, jobject data, jint size, jint width,
|
|
|
|
|
jint height, jint stride, jint pixelFormat)
|
|
|
|
|
{
|
|
|
|
|
auto* f = reinterpret_cast<wpi::RawFrame*>(frame);
|
|
|
|
|
if (!f) {
|
|
|
|
|
wpi::ThrowNullPointerException(env, "frame is null");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
auto buf = env->GetDirectBufferAddress(data);
|
|
|
|
|
if (!buf) {
|
|
|
|
|
wpi::ThrowNullPointerException(env, "data is null");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
// there's no way to free a passed-in direct byte buffer
|
|
|
|
|
f->SetData(buf, size, env->GetDirectBufferCapacity(data), nullptr,
|
|
|
|
|
[](void*, void*, size_t) {});
|
|
|
|
|
f->width = width;
|
|
|
|
|
f->height = height;
|
|
|
|
|
f->stride = stride;
|
|
|
|
|
f->pixelFormat = pixelFormat;
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-07 09:33:20 -07:00
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2025-01-07 09:33:20 -07:00
|
|
|
* Method: setRawFrameTime
|
|
|
|
|
* Signature: (JJI)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_setRawFrameTime
|
2025-01-07 09:33:20 -07:00
|
|
|
(JNIEnv* env, jclass, jlong frame, jlong time, jint timeSource)
|
|
|
|
|
{
|
|
|
|
|
auto* f = reinterpret_cast<wpi::RawFrame*>(frame);
|
|
|
|
|
if (!f) {
|
|
|
|
|
wpi::ThrowNullPointerException(env, "frame is null");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
f->timestamp = time;
|
|
|
|
|
f->timestampSrc = timeSource;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-26 20:05:02 -08:00
|
|
|
/*
|
2025-11-07 19:55:43 -05:00
|
|
|
* Class: org_wpilib_util_WPIUtilJNI
|
2023-12-26 20:05:02 -08:00
|
|
|
* Method: setRawFrameInfo
|
|
|
|
|
* Signature: (JIIIII)V
|
|
|
|
|
*/
|
|
|
|
|
JNIEXPORT void JNICALL
|
2025-11-07 19:55:43 -05:00
|
|
|
Java_org_wpilib_util_WPIUtilJNI_setRawFrameInfo
|
2023-12-26 20:05:02 -08:00
|
|
|
(JNIEnv* env, jclass, jlong frame, jint size, jint width, jint height,
|
|
|
|
|
jint stride, jint pixelFormat)
|
|
|
|
|
{
|
|
|
|
|
auto* f = reinterpret_cast<wpi::RawFrame*>(frame);
|
|
|
|
|
if (!f) {
|
|
|
|
|
wpi::ThrowNullPointerException(env, "frame is null");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
f->width = width;
|
|
|
|
|
f->height = height;
|
|
|
|
|
f->stride = stride;
|
|
|
|
|
f->pixelFormat = pixelFormat;
|
2023-11-23 13:55:10 -05:00
|
|
|
}
|
2019-09-26 22:53:21 -07:00
|
|
|
} // extern "C"
|