Skip to content

Commit 992a56b

Browse files
committed
[wpiutil] Add a RawFrame JNI overload for byte[]
Allows avoiding two copies in wpilibsuite#7176 Signed-off-by: Jade Turner <[email protected]>
1 parent f150b36 commit 992a56b

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

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

+19
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,25 @@ public void setData(ByteBuffer data, int width, int height, int stride, PixelFor
8888
m_nativeObj, data, data.limit(), width, height, stride, pixelFormat.getValue());
8989
}
9090

91+
/**
92+
* Set frame data.
93+
*
94+
* @param data A Java byte[] pointing to the frame data.
95+
* @param width The width of the frame, in pixels
96+
* @param height The height of the frame, in pixels
97+
* @param stride The number of bytes in each row of image data
98+
* @param pixelFormat The PixelFormat of the frame
99+
*/
100+
public void setData(byte[] data, int width, int height, int stride, PixelFormat pixelFormat) {
101+
m_data = ByteBuffer.wrap(data);
102+
m_width = width;
103+
m_height = height;
104+
m_stride = stride;
105+
m_pixelFormat = pixelFormat;
106+
WPIUtilJNI.setRawFrameData(
107+
m_nativeObj, data, data.length, width, height, stride, pixelFormat.getValue());
108+
}
109+
91110
/**
92111
* Call to set frame information.
93112
*

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 = (jbyte *)env->GetByteArrayElements(jbBase, 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)