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 #7176

Signed-off-by: Jade Turner <spacey-sooty@proton.me>
  • Loading branch information
spacey-sooty committed Oct 10, 2024
1 parent f150b36 commit d2caa5e
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
19 changes: 19 additions & 0 deletions wpiutil/src/main/java/edu/wpi/first/util/RawFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,25 @@ public void setData(ByteBuffer data, int width, int height, int stride, PixelFor
m_nativeObj, data, data.limit(), width, height, stride, pixelFormat.getValue());
}

/**
* Set frame data.
*
* @param data A Java byte[] pointing to the frame data.
* @param width The width of the frame, in pixels
* @param height The height of the frame, in pixels
* @param stride The number of bytes in each row of image data
* @param pixelFormat The PixelFormat of the frame
*/
public void setData(byte[] data, int width, int height, int stride, PixelFormat pixelFormat) {
m_data = ByteBuffer.wrap(data);
m_width = width;
m_height = height;
m_stride = stride;
m_pixelFormat = pixelFormat;
WPIUtilJNI.setRawFrameData(
m_nativeObj, data, data.length, width, height, stride, pixelFormat.getValue());
}

/**
* Call to set frame information.
*
Expand Down
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 = (jbyte *)env->GetByteArrayElements(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 d2caa5e

Please sign in to comment.