Skip to content

Commit 4eccfad

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

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
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
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,35 @@ Java_edu_wpi_first_util_WPIUtilJNI_setRawFrameData
395395
f->pixelFormat = pixelFormat;
396396
}
397397

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

0 commit comments

Comments
 (0)