Skip to content

Commit

Permalink
[wpiutil] Add a RawFrame JNI overload for byte[]
Browse files Browse the repository at this point in the history
Allows avoiding two copies in wpilibsuite#7176
  • Loading branch information
spacey-sooty committed Oct 10, 2024
1 parent f150b36 commit 4eccfad
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions wpiutil/src/main/java/edu/wpi/first/util/WPIUtilJNI.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ public static synchronized void forceLoad() throws IOException {
static native void setRawFrameData(
long frame, ByteBuffer data, int size, int width, int height, int stride, int pixelFormat);

static native void setRawFrameData(
long frame, byte[] data, int size, int width, int height, int stride, int pixelFormat);

static native void setRawFrameInfo(
long frame, int size, int width, int height, int stride, int pixelFormat);

Expand Down
29 changes: 29 additions & 0 deletions wpiutil/src/main/native/cpp/jni/WPIUtilJNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,35 @@ Java_edu_wpi_first_util_WPIUtilJNI_setRawFrameData
f->pixelFormat = pixelFormat;
}

/*
* Class: edu_wpi_first_util_WPIUtilJNI
* Method: setRawFrameData
* Signature: (J[BIIIII)V
*/
JNIEXPORT void JNICALL
Java_edu_wpi_first_util_WPIUtilJNI_setRawFrameData
(JNIEnv* env, jclass, jlong frame, jbyteArray 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->GetByteArrayElements(env, data, size);
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;
}

/*
* Class: edu_wpi_first_util_WPIUtilJNI
* Method: setRawFrameInfo
Expand Down

0 comments on commit 4eccfad

Please sign in to comment.