Skip to content

Commit

Permalink
Enable HardwareBufferPool only if MEDIAPIPE_TENSOR_USE_AHWB is enabled
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 604726411
  • Loading branch information
MediaPipe Team authored and copybara-github committed Feb 6, 2024
1 parent 97c1a39 commit a18ed2e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 27 deletions.
18 changes: 15 additions & 3 deletions mediapipe/framework/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,13 @@ cc_library(
"//mediapipe/framework:disable_rtti_and_exceptions": [
"MEDIAPIPE_HAS_RTTI=0",
],
# Excludes AHardwareBuffer features from vendor processes
"//mediapipe/framework:android_no_jni": ["MEDIAPIPE_NO_JNI"],
# unless they're linked against nativewindow.
"//mediapipe/framework/formats:android_link_native_window": [
"MEDIAPIPE_ANDROID_LINK_NATIVE_WINDOW",
"MEDIAPIPE_NO_JNI",
],
}),
visibility = [
"//fitbit/research/sensing/mobisense:__subpackages__",
Expand Down Expand Up @@ -1790,9 +1797,14 @@ cc_library(
hdrs = ["memory_manager.h"],
visibility = ["//visibility:public"],
deps = [
"//mediapipe/framework/formats:hardware_buffer_pool",
"//mediapipe/gpu:multi_pool",
],
"//mediapipe/framework:port",
] + select({
"//mediapipe:android": [
"//mediapipe/framework/formats:hardware_buffer_pool",
"//mediapipe/gpu:multi_pool",
],
"//conditions:default": [],
}),
)

cc_library(
Expand Down
12 changes: 1 addition & 11 deletions mediapipe/framework/formats/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ config_setting(
"MEDIAPIPE_NO_JNI": "1",
},
values = {"crosstool_top": "//external:android/crosstool"},
visibility = ["//visibility:private"],
visibility = ["//mediapipe/framework:__pkg__"],
)

cc_library(
Expand All @@ -511,16 +511,6 @@ 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({
"//mediapipe:apple": [
"-framework CoreVideo",
Expand Down
2 changes: 2 additions & 0 deletions mediapipe/framework/formats/hardware_buffer_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

#include <android/hardware_buffer.h>

#include <cstdint>
#include <memory>
#include <optional>

#include "absl/log/absl_check.h"
#include "absl/status/status.h"
Expand Down
12 changes: 1 addition & 11 deletions mediapipe/framework/formats/tensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,9 @@
#include "absl/synchronization/mutex.h"
#include "mediapipe/framework/formats/tensor/internal.h"
#include "mediapipe/framework/memory_manager.h"
// Exports MEDIAPIPE_TENSOR_USE_AHWB macro.
#include "mediapipe/framework/port.h"

// 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 // !defined(MEDIAPIPE_NO_JNI) ||
// defined(MEDIAPIPE_ANDROID_LINK_NATIVE_WINDOW)

#ifdef MEDIAPIPE_TENSOR_USE_AHWB
#include <EGL/egl.h>
#include <EGL/eglext.h>
Expand Down
18 changes: 16 additions & 2 deletions mediapipe/framework/memory_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,13 @@

#include <memory>

// Defines MEDIAPIPE_TENSOR_USE_AHWB
#include "mediapipe/framework/port.h"

#ifdef MEDIAPIPE_TENSOR_USE_AHWB
#include "mediapipe/framework/formats/hardware_buffer_pool.h"
#include "mediapipe/gpu/multi_pool.h"
#endif

namespace mediapipe {

Expand Down Expand Up @@ -49,19 +54,28 @@ namespace mediapipe {
// Tensor::Shape{kTensorSize}, &memory_manager_);
class MemoryManager {
public:
MemoryManager()
: hardware_buffer_pool_(std::make_shared<HardwareBufferPool>()) {}
MemoryManager() {
#ifdef MEDIAPIPE_TENSOR_USE_AHWB
hardware_buffer_pool_ = std::make_shared<HardwareBufferPool>();
#endif
}

#ifdef MEDIAPIPE_TENSOR_USE_AHWB
std::shared_ptr<HardwareBufferPool> GetAndroidHardwareBufferPool() const {
return hardware_buffer_pool_;
}
#endif

#ifdef MEDIAPIPE_TENSOR_USE_AHWB
// For testing only:
explicit MemoryManager(const MultiPoolOptions& options)
: hardware_buffer_pool_(std::make_shared<HardwareBufferPool>(options)) {}
#endif

private:
#ifdef MEDIAPIPE_TENSOR_USE_AHWB
std::shared_ptr<HardwareBufferPool> hardware_buffer_pool_;
#endif
};

} // namespace mediapipe
Expand Down
11 changes: 11 additions & 0 deletions mediapipe/framework/port.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,15 @@
#define MEDIAPIPE_GPU_BUFFER_USE_AHWB 1
#endif

// 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 // !defined(MEDIAPIPE_NO_JNI) ||
// defined(MEDIAPIPE_ANDROID_LINK_NATIVE_WINDOW)

#endif // MEDIAPIPE_FRAMEWORK_PORT_H_

0 comments on commit a18ed2e

Please sign in to comment.