EmulatedCamera: Add support for session hal buffer manager

The EmulatedCameraDeviceSessionHWLImpl::ShouldUseHalBufferManager()
method lets GCH know if HAL buffer manager should be used for a session
configured.

Bug: 311263114

Test: Camera CTS with session_hal_buf_manager flag

Change-Id: I1f05d15a644d9998cb7351817cc57804611b138d
Signed-off-by: Jayant Chowdhary <jchowdhary@google.com>
diff --git a/devices/EmulatedCamera/hwl/EmulatedCameraDeviceSessionHWLImpl.cpp b/devices/EmulatedCamera/hwl/EmulatedCameraDeviceSessionHWLImpl.cpp
index ca6660d..6ad921d 100644
--- a/devices/EmulatedCamera/hwl/EmulatedCameraDeviceSessionHWLImpl.cpp
+++ b/devices/EmulatedCamera/hwl/EmulatedCameraDeviceSessionHWLImpl.cpp
@@ -169,6 +169,14 @@
     return ret;
   }
 
+  ret = SupportsSessionHalBufManager(static_metadata_.get(),
+                                     &supports_session_hal_buf_manager_);
+  if (ret != OK) {
+    ALOGE("%s: Unable to get sensor hal buffer manager support %s (%d)",
+          __FUNCTION__, strerror(-ret), ret);
+    return ret;
+  }
+
   logical_chars_.emplace(camera_id_, sensor_chars_);
   for (const auto& it : *physical_device_map_) {
     SensorCharacteristics physical_chars;
@@ -373,6 +381,27 @@
   return OK;
 }
 
+status_t EmulatedCameraDeviceSessionHwlImpl::ShouldUseHalBufferManager(
+    bool* result) {
+  if (result == nullptr) {
+    ALOGE("%s result is nullptr", __FUNCTION__);
+    return BAD_VALUE;
+  }
+  *result = false;
+  if (!pipelines_built_) {
+    ALOGE("%s: Pipelines haven't been built yet", __FUNCTION__);
+    return INVALID_OPERATION;
+  }
+  if (!supports_session_hal_buf_manager_) {
+    return OK;
+  }
+  // Heuristic which doesn't necessarily correspond to real scenarios
+  if (pipelines_.size() >= 1 && pipelines_[0].streams.size() >= 2) {
+    *result = true;
+  }
+  return OK;
+}
+
 void EmulatedCameraDeviceSessionHwlImpl::DestroyPipelines() {
   ATRACE_CALL();
   std::lock_guard<std::mutex> lock(api_mutex_);
diff --git a/devices/EmulatedCamera/hwl/EmulatedCameraDeviceSessionHWLImpl.h b/devices/EmulatedCamera/hwl/EmulatedCameraDeviceSessionHWLImpl.h
index 81631cf..06c3609 100644
--- a/devices/EmulatedCamera/hwl/EmulatedCameraDeviceSessionHWLImpl.h
+++ b/devices/EmulatedCamera/hwl/EmulatedCameraDeviceSessionHWLImpl.h
@@ -103,6 +103,8 @@
 
   status_t BuildPipelines() override;
 
+  status_t ShouldUseHalBufferManager(bool* result) override;
+
   status_t PreparePipeline(uint32_t /*pipeline_id*/,
                            uint32_t /*frame_number*/) override {
     return OK;
@@ -202,6 +204,7 @@
   bool error_state_ = false;
   bool pipelines_built_ = false;
   bool has_raw_stream_ = false;
+  bool supports_session_hal_buf_manager_ = false;
   std::unique_ptr<HalCameraMetadata> static_metadata_;
   std::vector<EmulatedPipeline> pipelines_;
   std::shared_ptr<EmulatedRequestProcessor> request_processor_;
diff --git a/devices/EmulatedCamera/hwl/configs/emu_camera_front.json b/devices/EmulatedCamera/hwl/configs/emu_camera_front.json
index 7feca73..69188e9 100644
--- a/devices/EmulatedCamera/hwl/configs/emu_camera_front.json
+++ b/devices/EmulatedCamera/hwl/configs/emu_camera_front.json
@@ -1502,7 +1502,7 @@
    "10"
   ],
   "android.info.supportedBufferManagementVersion" : [
-    "HIDL_DEVICE_3_5"
+    "SESSION_CONFIGURABLE"
   ],
   "android.sync.maxLatency": [
    "PER_FRAME_CONTROL"
@@ -2985,7 +2985,7 @@
    "10"
   ],
   "android.info.supportedBufferManagementVersion" : [
-    "HIDL_DEVICE_3_5"
+    "SESSION_CONFIGURABLE"
   ],
   "android.sync.maxLatency": [
    "PER_FRAME_CONTROL"
@@ -4513,7 +4513,7 @@
    "10"
   ],
   "android.info.supportedBufferManagementVersion" : [
-    "HIDL_DEVICE_3_5"
+    "SESSION_CONFIGURABLE"
   ],
   "android.sync.maxLatency": [
    "PER_FRAME_CONTROL"
diff --git a/devices/EmulatedCamera/hwl/utils/HWLUtils.cpp b/devices/EmulatedCamera/hwl/utils/HWLUtils.cpp
index f034a7a..2d156ab 100644
--- a/devices/EmulatedCamera/hwl/utils/HWLUtils.cpp
+++ b/devices/EmulatedCamera/hwl/utils/HWLUtils.cpp
@@ -45,6 +45,29 @@
   }
   return video_call_use_case;
 }
+status_t SupportsSessionHalBufManager(const HalCameraMetadata* metadata,
+                                      bool* result /*out*/) {
+  if ((metadata == nullptr) || (result == nullptr)) {
+    return BAD_VALUE;
+  }
+
+  status_t ret = OK;
+  camera_metadata_ro_entry_t entry;
+  *result = false;
+  ret = metadata->Get(ANDROID_INFO_SUPPORTED_BUFFER_MANAGEMENT_VERSION, &entry);
+  if (ret != OK) {
+    return OK;
+  }
+  if ((ret == OK) && (entry.count != 1)) {
+    ALOGE("%s: Invalid ANDROID_INFO_SUPPORTED_BUFFER_MANAGEMENT_VERSION!",
+          __FUNCTION__);
+    return BAD_VALUE;
+  }
+  *result =
+      (entry.data.u8[0] ==
+       ANDROID_INFO_SUPPORTED_BUFFER_MANAGEMENT_VERSION_SESSION_CONFIGURABLE);
+  return OK;
+}
 
 status_t GetSensorCharacteristics(const HalCameraMetadata* metadata,
                                   SensorCharacteristics* sensor_chars /*out*/) {
diff --git a/devices/EmulatedCamera/hwl/utils/HWLUtils.h b/devices/EmulatedCamera/hwl/utils/HWLUtils.h
index 4ee0054..5c3a281 100644
--- a/devices/EmulatedCamera/hwl/utils/HWLUtils.h
+++ b/devices/EmulatedCamera/hwl/utils/HWLUtils.h
@@ -43,6 +43,9 @@
 typedef std::unique_ptr<PhysicalDeviceMap> PhysicalDeviceMapPtr;
 
 // Metadata utility functions start
+
+status_t SupportsSessionHalBufManager(const HalCameraMetadata* metadata,
+                                      bool* result /*out*/);
 status_t GetSensorCharacteristics(const HalCameraMetadata* metadata,
                                   SensorCharacteristics* sensor_chars /*out*/);
 PhysicalDeviceMapPtr ClonePhysicalDeviceMap(const PhysicalDeviceMapPtr& src);