From fda0d19337ef7924069bf7d2758596f4cc3b1651 Mon Sep 17 00:00:00 2001 From: MediaPipe Team Date: Fri, 18 Aug 2023 02:28:03 -0700 Subject: [PATCH] Adds option to use tensor_ahwb in Android vendor processes PiperOrigin-RevId: 558086646 --- mediapipe/framework/formats/BUILD | 20 ++++++++++++++++++++ mediapipe/framework/formats/tensor.h | 8 ++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/mediapipe/framework/formats/BUILD b/mediapipe/framework/formats/BUILD index b23209f7d..242de6ff9 100644 --- a/mediapipe/framework/formats/BUILD +++ b/mediapipe/framework/formats/BUILD @@ -427,6 +427,17 @@ cc_test( ], ) +# Used by vendor processes that don't have access to libandroid.so, but want to use AHardwareBuffer. +config_setting( + name = "android_link_native_window", + define_values = { + "MEDIAPIPE_ANDROID_LINK_NATIVE_WINDOW": "1", + "MEDIAPIPE_NO_JNI": "1", + }, + values = {"crosstool_top": "//external:android/crosstool"}, + visibility = ["//visibility:private"], +) + cc_library( name = "tensor", srcs = @@ -449,7 +460,13 @@ cc_library( "//conditions:default": [], }), defines = select({ + # Excludes AHardwareBuffer features from vendor processes "//mediapipe/framework:android_no_jni": ["MEDIAPIPE_NO_JNI"], + # unless they're linked against nativewindow. + ":android_link_native_window": [ + "MEDIAPIPE_ANDROID_LINK_NATIVE_WINDOW", + "MEDIAPIPE_NO_JNI", + ], "//conditions:default": [], }), linkopts = select({ @@ -462,6 +479,9 @@ cc_library( "//mediapipe:android": [ "-landroid", ], + ":android_link_native_window": [ + "-lnativewindow", # Provides to vendor processes on Android API >= 26. + ], }), deps = [ "//mediapipe/framework:port", diff --git a/mediapipe/framework/formats/tensor.h b/mediapipe/framework/formats/tensor.h index 4f95eb27b..fea200f94 100644 --- a/mediapipe/framework/formats/tensor.h +++ b/mediapipe/framework/formats/tensor.h @@ -29,12 +29,16 @@ #include "mediapipe/framework/formats/tensor/internal.h" #include "mediapipe/framework/port.h" -#ifndef MEDIAPIPE_NO_JNI +// Supported use cases for tensor_ahwb: +// 1. Native code running in Android apps. +// 2. Android vendor processes linked against nativewindow. +#if !defined(MEDIAPIPE_NO_JNI) || defined(MEDIAPIPE_ANDROID_LINK_NATIVE_WINDOW) #if __ANDROID_API__ >= 26 || defined(__ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__) #define MEDIAPIPE_TENSOR_USE_AHWB 1 #endif // __ANDROID_API__ >= 26 || // defined(__ANDROID_UNAVAILABLE_SYMBOLS_ARE_WEAK__) -#endif // MEDIAPIPE_NO_JNI +#endif // !defined(MEDIAPIPE_NO_JNI) || + // defined(MEDIAPIPE_ANDROID_LINK_NATIVE_WINDOW) #ifdef MEDIAPIPE_TENSOR_USE_AHWB #include