v4l2 camera: port ARC++ image processing code

Utiltizes ARC++ image processing code to perform the conversion between
the camera format and the requested format from the framework. In order
to support USB devices which may not implement the required formats,
conversion between YUYV/MJPEG to the request format is done on-the-fly.

TEST: All existing unit tests pass. Tested manually with the camera
sample APK, preview/video/capture in both JPEG and YUV formats. Tested
on the RPi v2 Camera, the Logitech C270 and the 180 degree Fisheye USB
camera.
Bug: 37708856, Bug: 69983918
Exempt-From-Owner-Approval: This HAL is owned by AT team.
Change-Id: I1a54581d1170fa3270036ec366fb613fe587a094
diff --git a/modules/camera/3_4/v4l2_camera.cpp b/modules/camera/3_4/v4l2_camera.cpp
index 22406c9..98b8062 100644
--- a/modules/camera/3_4/v4l2_camera.cpp
+++ b/modules/camera/3_4/v4l2_camera.cpp
@@ -123,16 +123,7 @@
 
 int V4L2Camera::flushBuffers() {
   HAL_LOG_ENTER();
-
-  int res = device_->StreamOff();
-
-  // This is not strictly necessary, but prevents a buildup of aborted
-  // requests in the in_flight_ map. These should be cleared
-  // whenever the stream is turned off.
-  std::lock_guard<std::mutex> guard(in_flight_lock_);
-  in_flight_.clear();
-
-  return res;
+  return device_->StreamOff();
 }
 
 int V4L2Camera::initStaticInfo(android::CameraMetadata* out) {
@@ -262,55 +253,41 @@
   }
 
   // Actually enqueue the buffer for capture.
-  {
-    std::lock_guard<std::mutex> guard(in_flight_lock_);
-
-    uint32_t index;
-    res = device_->EnqueueBuffer(&request->output_buffers[0], &index);
-    if (res) {
-      HAL_LOGE("Device failed to enqueue buffer.");
-      completeRequest(request, res);
-      return true;
-    }
-
-    // Make sure the stream is on (no effect if already on).
-    res = device_->StreamOn();
-    if (res) {
-      HAL_LOGE("Device failed to turn on stream.");
-      // Don't really want to send an error for only the request here,
-      // since this is a full device error.
-      // TODO: Should trigger full flush.
-      return true;
-    }
-
-    // Note: the request should be dequeued/flushed from the device
-    // before removal from in_flight_.
-    in_flight_.emplace(index, request);
-    buffers_in_flight_.notify_one();
+  res = device_->EnqueueRequest(request);
+  if (res) {
+    HAL_LOGE("Device failed to enqueue buffer.");
+    completeRequest(request, res);
+    return true;
   }
 
+  // Make sure the stream is on (no effect if already on).
+  res = device_->StreamOn();
+  if (res) {
+    HAL_LOGE("Device failed to turn on stream.");
+    // Don't really want to send an error for only the request here,
+    // since this is a full device error.
+    // TODO: Should trigger full flush.
+    return true;
+  }
+
+  std::unique_lock<std::mutex> lock(in_flight_lock_);
+  in_flight_buffer_count_++;
+  buffers_in_flight_.notify_one();
   return true;
 }
 
 bool V4L2Camera::dequeueRequestBuffers() {
   // Dequeue a buffer.
-  uint32_t result_index;
+  std::shared_ptr<default_camera_hal::CaptureRequest> request;
   int res;
 
   {
-    std::lock_guard<std::mutex> guard(in_flight_lock_);
-    res = device_->DequeueBuffer(&result_index);
+    std::unique_lock<std::mutex> lock(in_flight_lock_);
+    res = device_->DequeueRequest(&request);
     if (!res) {
-      // Find the associated request and complete it.
-      auto index_request = in_flight_.find(result_index);
-      if (index_request != in_flight_.end()) {
-        completeRequest(index_request->second, 0);
-        in_flight_.erase(index_request);
-      } else {
-        HAL_LOGW(
-            "Dequeued non in-flight buffer index %d. "
-            "This buffer may have been flushed from the HAL but not the device.",
-            index_request->first);
+      if (request) {
+        completeRequest(request, res);
+        in_flight_buffer_count_--;
       }
       return true;
     }
@@ -320,7 +297,7 @@
     // EAGAIN just means nothing to dequeue right now.
     // Wait until something is available before looping again.
     std::unique_lock<std::mutex> lock(in_flight_lock_);
-    while (in_flight_.empty()) {
+    while (in_flight_buffer_count_ == 0) {
       buffers_in_flight_.wait(lock);
     }
   } else {
@@ -350,10 +327,11 @@
 
   std::lock_guard<std::mutex> guard(in_flight_lock_);
   // The framework should be enforcing this, but doesn't hurt to be safe.
-  if (!in_flight_.empty()) {
+  if (device_->GetInFlightBufferCount() != 0) {
     HAL_LOGE("Can't set device format while frames are in flight.");
     return -EINVAL;
   }
+  in_flight_buffer_count_ = 0;
 
   // stream_config should have been validated; assume at least 1 stream.
   camera3_stream_t* stream = stream_config->streams[0];