From 7d26daf7236c60b1bc1e04c291b632e59b45db8d Mon Sep 17 00:00:00 2001 From: MediaPipe Team Date: Tue, 21 Mar 2023 13:09:43 -0700 Subject: [PATCH] Make sure calling GraphTextureFrame::getTextureName()+::release() on a non-GL thread doesn't result in a crash. PiperOrigin-RevId: 518359778 --- .../mediapipe/framework/GraphTextureFrame.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/mediapipe/java/com/google/mediapipe/framework/GraphTextureFrame.java b/mediapipe/java/com/google/mediapipe/framework/GraphTextureFrame.java index 6a2c97b94..a253a8289 100644 --- a/mediapipe/java/com/google/mediapipe/framework/GraphTextureFrame.java +++ b/mediapipe/java/com/google/mediapipe/framework/GraphTextureFrame.java @@ -66,7 +66,8 @@ public class GraphTextureFrame implements TextureFrame { if (nativeBufferHandle == 0) { return 0; } - if (activeConsumerContextHandleSet.add(nativeGetCurrentExternalContextHandle())) { + long contextHandle = nativeGetCurrentExternalContextHandle(); + if (contextHandle != 0 && activeConsumerContextHandleSet.add(contextHandle)) { // Gpu wait only if deferredSync is true, such as when this GraphTextureFrame is created using // PacketGetter.getTextureFrameDeferredSync(). if (deferredSync) { @@ -116,7 +117,14 @@ public class GraphTextureFrame implements TextureFrame { GlSyncToken consumerToken = null; // Note that this remove should be moved to the other overload of release when b/68808951 is // addressed. - if (activeConsumerContextHandleSet.remove(nativeGetCurrentExternalContextHandle())) { + final long contextHandle = nativeGetCurrentExternalContextHandle(); + if (contextHandle == 0 && !activeConsumerContextHandleSet.isEmpty()) { + logger.atWarning().log( + "GraphTextureFrame is being released on non GL thread while having active consumers," + + " which may lead to external / internal GL contexts synchronization issues."); + } + + if (contextHandle != 0 && activeConsumerContextHandleSet.remove(contextHandle)) { consumerToken = new GraphGlSyncToken(nativeCreateSyncTokenForCurrentExternalContext(nativeBufferHandle)); } @@ -169,7 +177,9 @@ public class GraphTextureFrame implements TextureFrame { private native void nativeReleaseBuffer(long nativeHandle); private native int nativeGetTextureName(long nativeHandle); + private native int nativeGetWidth(long nativeHandle); + private native int nativeGetHeight(long nativeHandle); private native void nativeGpuWait(long nativeHandle);