Internal change

PiperOrigin-RevId: 495235951
This commit is contained in:
Camillo Lugaresi 2022-12-14 00:34:22 -08:00 committed by Copybara-Service
parent b9d020cb7d
commit 6fa0a58529
3 changed files with 47 additions and 10 deletions

View File

@ -14,6 +14,10 @@
package com.google.mediapipe.framework; package com.google.mediapipe.framework;
import com.google.common.flogger.FluentLogger;
import java.util.HashSet;
import java.util.Set;
/** /**
* A {@link TextureFrame} that represents a texture produced by MediaPipe. * A {@link TextureFrame} that represents a texture produced by MediaPipe.
* *
@ -21,6 +25,7 @@ package com.google.mediapipe.framework;
* method. * method.
*/ */
public class GraphTextureFrame implements TextureFrame { public class GraphTextureFrame implements TextureFrame {
private static final FluentLogger logger = FluentLogger.forEnclosingClass();
private long nativeBufferHandle; private long nativeBufferHandle;
// We cache these to be able to get them without a JNI call. // We cache these to be able to get them without a JNI call.
private int textureName; private int textureName;
@ -30,6 +35,7 @@ public class GraphTextureFrame implements TextureFrame {
// True when created with PacketGetter.getTextureFrameDeferredSync(). This will result in gpuWait // True when created with PacketGetter.getTextureFrameDeferredSync(). This will result in gpuWait
// when calling getTextureName(). // when calling getTextureName().
private final boolean deferredSync; private final boolean deferredSync;
private final Set<Long> activeConsumerContextHandleSet = new HashSet<>();
GraphTextureFrame(long nativeHandle, long timestamp) { GraphTextureFrame(long nativeHandle, long timestamp) {
this(nativeHandle, timestamp, false); this(nativeHandle, timestamp, false);
@ -54,17 +60,19 @@ public class GraphTextureFrame implements TextureFrame {
* condition if release() is called after the if-check for nativeBufferHandle is already passed. * condition if release() is called after the if-check for nativeBufferHandle is already passed.
*/ */
@Override @Override
public int getTextureName() { public synchronized int getTextureName() {
// Return special texture id 0 if handle is 0 i.e. frame is already released. // Return special texture id 0 if handle is 0 i.e. frame is already released.
if (nativeBufferHandle == 0) { if (nativeBufferHandle == 0) {
return 0; return 0;
} }
// Gpu wait only if deferredSync is true, such as when this GraphTextureFrame is created using if (activeConsumerContextHandleSet.add(nativeGetCurrentExternalContextHandle())) {
// PacketGetter.getTextureFrameDeferredSync(). // Gpu wait only if deferredSync is true, such as when this GraphTextureFrame is created using
if (deferredSync) { // PacketGetter.getTextureFrameDeferredSync().
// Note that, if a CPU wait has already been done, the sync point will have been if (deferredSync) {
// cleared and this will turn into a no-op. See GlFenceSyncPoint::Wait. // Note that, if a CPU wait has already been done, the sync point will have been
nativeGpuWait(nativeBufferHandle); // cleared and this will turn into a no-op. See GlFenceSyncPoint::Wait.
nativeGpuWait(nativeBufferHandle);
}
} }
return textureName; return textureName;
} }
@ -92,9 +100,14 @@ public class GraphTextureFrame implements TextureFrame {
* <p>The consumer calls this when it is done using the texture. * <p>The consumer calls this when it is done using the texture.
*/ */
@Override @Override
public void release() { public synchronized void release() {
GlSyncToken consumerToken = GlSyncToken consumerToken = null;
new GraphGlSyncToken(nativeCreateSyncTokenForCurrentExternalContext(nativeBufferHandle)); // Note that this remove should be moved to the other overload of release when b/68808951 is
// addressed.
if (activeConsumerContextHandleSet.remove(nativeGetCurrentExternalContextHandle())) {
consumerToken =
new GraphGlSyncToken(nativeCreateSyncTokenForCurrentExternalContext(nativeBufferHandle));
}
release(consumerToken); release(consumerToken);
} }
@ -113,12 +126,24 @@ public class GraphTextureFrame implements TextureFrame {
long token = consumerSyncToken == null ? 0 : consumerSyncToken.nativeToken(); long token = consumerSyncToken == null ? 0 : consumerSyncToken.nativeToken();
nativeReleaseBuffer(nativeBufferHandle, token); nativeReleaseBuffer(nativeBufferHandle, token);
nativeBufferHandle = 0; nativeBufferHandle = 0;
} else if (consumerSyncToken != null) {
logger.atWarning().log("release with sync token, but handle is 0");
} }
if (consumerSyncToken != null) { if (consumerSyncToken != null) {
consumerSyncToken.release(); consumerSyncToken.release();
} }
} }
@Override
protected void finalize() throws Throwable {
if (nativeBufferHandle != 0) {
logger.atWarning().log("release was not called before finalize");
}
if (!activeConsumerContextHandleSet.isEmpty()) {
logger.atWarning().log("active consumers did not release with sync before finalize");
}
}
private native void nativeReleaseBuffer(long nativeHandle, long consumerSyncToken); private native void nativeReleaseBuffer(long nativeHandle, long consumerSyncToken);
private native int nativeGetTextureName(long nativeHandle); private native int nativeGetTextureName(long nativeHandle);
@ -128,4 +153,6 @@ public class GraphTextureFrame implements TextureFrame {
private native void nativeGpuWait(long nativeHandle); private native void nativeGpuWait(long nativeHandle);
private native long nativeCreateSyncTokenForCurrentExternalContext(long nativeHandle); private native long nativeCreateSyncTokenForCurrentExternalContext(long nativeHandle);
private native long nativeGetCurrentExternalContextHandle();
} }

View File

@ -15,6 +15,7 @@
#include "mediapipe/java/com/google/mediapipe/framework/jni/graph_texture_frame_jni.h" #include "mediapipe/java/com/google/mediapipe/framework/jni/graph_texture_frame_jni.h"
#include "mediapipe/gpu/gl_calculator_helper.h" #include "mediapipe/gpu/gl_calculator_helper.h"
#include "mediapipe/gpu/gl_context.h"
#include "mediapipe/gpu/gl_texture_buffer.h" #include "mediapipe/gpu/gl_texture_buffer.h"
#include "mediapipe/java/com/google/mediapipe/framework/jni/jni_util.h" #include "mediapipe/java/com/google/mediapipe/framework/jni/jni_util.h"
@ -84,3 +85,9 @@ JNIEXPORT jlong JNICALL GRAPH_TEXTURE_FRAME_METHOD(
} }
return reinterpret_cast<jlong>(token); return reinterpret_cast<jlong>(token);
} }
JNIEXPORT jlong JNICALL GRAPH_TEXTURE_FRAME_METHOD(
nativeGetCurrentExternalContextHandle)(JNIEnv* env, jobject thiz) {
return reinterpret_cast<jlong>(
mediapipe::GlContext::GetCurrentNativeContext());
}

View File

@ -44,6 +44,9 @@ JNIEXPORT jlong JNICALL GRAPH_TEXTURE_FRAME_METHOD(
nativeCreateSyncTokenForCurrentExternalContext)(JNIEnv* env, jobject thiz, nativeCreateSyncTokenForCurrentExternalContext)(JNIEnv* env, jobject thiz,
jlong nativeHandle); jlong nativeHandle);
JNIEXPORT jlong JNICALL GRAPH_TEXTURE_FRAME_METHOD(
nativeGetCurrentExternalContextHandle)(JNIEnv* env, jobject thiz);
#ifdef __cplusplus #ifdef __cplusplus
} // extern "C" } // extern "C"
#endif // __cplusplus #endif // __cplusplus