Skip to content

Commit 7cd87c5

Browse files
committed
[wpiutil] Add a RawFrame JNI overload for byte[]
Allows avoiding two copies in wpilibsuite#7176
1 parent f150b36 commit 7cd87c5

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

wpiutil/src/main/java/edu/wpi/first/util/WPIUtilJNI.java

+3
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ public static synchronized void forceLoad() throws IOException {
169169
static native void setRawFrameData(
170170
long frame, ByteBuffer data, int size, int width, int height, int stride, int pixelFormat);
171171

172+
static native void setRawFrameData(
173+
long frame, byte[] data, int size, int width, int height, int stride, int pixelFormat);
174+
172175
static native void setRawFrameInfo(
173176
long frame, int size, int width, int height, int stride, int pixelFormat);
174177

wpiutil/src/main/native/cpp/jni/WPIUtilJNI.cpp

+29-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "wpi/timestamp.h"
1717

1818
using namespace wpi::java;
19-
2019
static bool mockTimeEnabled = false;
2120
static uint64_t mockNow = 0;
2221

@@ -395,6 +394,35 @@ Java_edu_wpi_first_util_WPIUtilJNI_setRawFrameData
395394
f->pixelFormat = pixelFormat;
396395
}
397396

397+
/*
398+
* Class: edu_wpi_first_util_WPIUtilJNI
399+
* Method: setRawFrameData
400+
* Signature: (J[BIIIII)V
401+
*/
402+
JNIEXPORT void JNICALL
403+
Java_edu_wpi_first_util_WPIUtilJNI_setRawFrameData
404+
(JNIEnv* env, jclass, jlong frame, jbyteArray data, jint size, jint width,
405+
jint height, jint stride, jint pixelFormat)
406+
{
407+
auto* f = reinterpret_cast<wpi::RawFrame*>(frame);
408+
if (!f) {
409+
wpi::ThrowNullPointerException(env, "frame is null");
410+
return;
411+
}
412+
auto buf = env->GetByteArrayElements(env, data, size);
413+
if (!buf) {
414+
wpi::ThrowNullPointerException(env, "data is null");
415+
return;
416+
}
417+
// there's no way to free a passed-in direct byte buffer
418+
f->SetData(buf, size, env->GetDirectBufferCapacity(data), nullptr,
419+
[](void*, void*, size_t) {});
420+
f->width = width;
421+
f->height = height;
422+
f->stride = stride;
423+
f->pixelFormat = pixelFormat;
424+
}
425+
398426
/*
399427
* Class: edu_wpi_first_util_WPIUtilJNI
400428
* Method: setRawFrameInfo

0 commit comments

Comments
 (0)