<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>Hello,<br>
    </p>
    <div class="moz-cite-prefix">On 9/29/21 12:40 PM, Laurent Pinchart
      wrote:<br>
    </div>
    <blockquote type="cite"
      cite="mid:YVQRe8r51rkEIUIF@pendragon.ideasonboard.com">
      <pre class="moz-quote-pre" wrap="">Hi Hiro,

On Wed, Sep 29, 2021 at 08:45:24AM +0900, Hirokazu Honda wrote:
</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">On Wed, Sep 29, 2021 at 6:30 AM Laurent Pinchart wrote:
</pre>
        <blockquote type="cite">
          <pre class="moz-quote-pre" wrap="">On Tue, Sep 28, 2021 at 09:55:36PM +0530, Umang Jain wrote:
</pre>
          <blockquote type="cite">
            <pre class="moz-quote-pre" wrap="">There is a possibility that an out-of-order completion of capture
request happens by calling process_capture_result() directly on error
paths. The framework expects that errors should be notified as soon as
possible, but the request completion order should remain intact.
An existing instance of this is abortRequest(), which sends the capture
results on flushing state, without considering order-of-completion.

Since we have a queue of Camera3RequestDescriptor tracking each
capture request placed by framework to libcamera HAL, we should be only
sending back capture results from a single location, by inspecting
the queue. As per the patch, this now happens in
<a class="moz-txt-link-freetext" href="CameraDevice::sendCaptureResults()">CameraDevice::sendCaptureResults()</a>.

Each descriptor is now equipped with its own status to denote whether
the capture request is complete and ready to send back to the framework
or needs to be waited upon. This ensures that the order of completion is
respected for the requests.

Signed-off-by: Umang Jain <a class="moz-txt-link-rfc2396E" href="mailto:umang.jain@ideasonboard.com"><umang.jain@ideasonboard.com></a>
---
 src/android/camera_device.cpp | 44 +++++++++++++++++++++++++----------
 src/android/camera_device.h   | 15 +++++++++++-
 2 files changed, 46 insertions(+), 13 deletions(-)

diff --git a/src/android/camera_device.cpp b/src/android/camera_device.cpp
index a3b8a549..83030366 100644
--- a/src/android/camera_device.cpp
+++ b/src/android/camera_device.cpp
@@ -861,11 +861,12 @@ int <a class="moz-txt-link-freetext" href="CameraDevice::processControls(Camera3RequestDescriptor">CameraDevice::processControls(Camera3RequestDescriptor</a> *descriptor)
      return 0;
 }

-void <a class="moz-txt-link-freetext" href="CameraDevice::abortRequest(camera3_capture_request_t">CameraDevice::abortRequest(camera3_capture_request_t</a> *request)
+void <a class="moz-txt-link-freetext" href="CameraDevice::abortRequest(Camera3RequestDescriptor">CameraDevice::abortRequest(Camera3RequestDescriptor</a> *descriptor,
+                             camera3_capture_request_t *request)
</pre>
          </blockquote>
          <pre class="moz-quote-pre" wrap="">
I don't think I've seen a reply to my review comment in v1:

Could this function take a Camera3RequestDescriptor pointer only ? It
should contain all the needed data. This can be done as a patch before
this one if desired, or here as it shouldn't be much extra work.

This function uses the num_output_buffers, frame_number and
output_buffers members of camera3_capture_request_t. Those are copied to
the Camera3RequestDescriptor buffers_ and frameNumber_ members which you
can use here.

Reviewed-by: Laurent Pinchart <a class="moz-txt-link-rfc2396E" href="mailto:laurent.pinchart@ideasonboard.com"><laurent.pinchart@ideasonboard.com></a>

</pre>
          <blockquote type="cite">
            <pre class="moz-quote-pre" wrap=""> {
      notifyError(request->frame_number, nullptr, CAMERA3_MSG_ERROR_REQUEST);

-     camera3_capture_result_t result = {};
+     camera3_capture_result_t &result = descriptor->captureResult_;
      result.num_output_buffers = request->num_output_buffers;
      result.frame_number = request->frame_number;
      result.partial_result = 0;
@@ -879,7 +880,7 @@ void <a class="moz-txt-link-freetext" href="CameraDevice::abortRequest(camera3_capture_request_t">CameraDevice::abortRequest(camera3_capture_request_t</a> *request)
      }
      result.output_buffers = resultBuffers.data();

-     callbacks_->process_capture_result(callbacks_, &result);
+     descriptor->status_ = <a class="moz-txt-link-freetext" href="Camera3RequestDescriptor::Status::Error">Camera3RequestDescriptor::Status::Error</a>;
 }

 bool <a class="moz-txt-link-freetext" href="CameraDevice::isValidRequest(camera3_capture_request_t">CameraDevice::isValidRequest(camera3_capture_request_t</a> *camera3Request) const
@@ -1052,13 +1053,19 @@ int <a class="moz-txt-link-freetext" href="CameraDevice::processCaptureRequest(camera3_capture_request_t">CameraDevice::processCaptureRequest(camera3_capture_request_t</a> *camera3Reques
              return ret;

      /*
-      * If flush is in progress abort the request. If the camera has been
-      * stopped we have to re-start it to be able to process the request.
+      * If flush is in progress abort the request and push the descriptor to
+      * the queue. If the camera has been stopped we have to re-start it to
+      * be able to process the request.
       */
      MutexLocker stateLock(stateMutex_);

      if (state_ == <a class="moz-txt-link-freetext" href="State::Flushing">State::Flushing</a>) {
-             abortRequest(camera3Request);
+             abortRequest(descriptors_.back().get(), camera3Request);
+             {
+                     MutexLocker descriptorsLock(descriptorsMutex_);
+                     descriptors_.push(<a class="moz-txt-link-freetext" href="std::move(descriptor))">std::move(descriptor))</a>;
+             }
</pre>
          </blockquote>
        </blockquote>
        <pre class="moz-quote-pre" wrap="">
Could you tell me what happens here?
I think the current request, camear3Request needs to be cancelled.
But abortRequest(descriptors_.back().get(), camera3Request) is called
before pushing.
So it aborts the last pushed request with the current request.
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
Oops indeed.

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">I wonder if the correct code is below.
if (state_ == <a class="moz-txt-link-freetext" href="State::Flushing">State::Flushing</a>)
{
  {
     MutexLocker descriptorsLock(descriptorsMutex_);
     descriptors_.push(<a class="moz-txt-link-freetext" href="std::move(descriptor))">std::move(descriptor))</a>;
     abortRequest(descriptors_.back().get(), camera3Request);
  }
  sendCaptureResults();
  return 0;
}
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
abortRequest() is supposed to operator on the request it receives as a
parameter, so I don't think it needs to be covered by the lock. I think

                abortRequest(descriptor.get(), camera3Request);
                {
                        MutexLocker descriptorsLock(descriptorsMutex_);
                        descriptors_.push(<a class="moz-txt-link-freetext" href="std::move(descriptor))">std::move(descriptor))</a>;
                }
                sendCaptureResults();

may be enough to fix the issue.</pre>
    </blockquote>
    <p><br>
    </p>
    <p>Yep, addressed in v3, thanks for pointing it out<br>
    </p>
    <blockquote type="cite"
      cite="mid:YVQRe8r51rkEIUIF@pendragon.ideasonboard.com">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <blockquote type="cite">
          <blockquote type="cite">
            <pre class="moz-quote-pre" wrap="">+             sendCaptureResults();
              return 0;
      }

@@ -1116,7 +1123,7 @@ void <a class="moz-txt-link-freetext" href="CameraDevice::requestComplete(Request">CameraDevice::requestComplete(Request</a> *request)
       * The buffer status is set to OK and later changed to ERROR if
       * post-processing/compression fails.
       */
-     camera3_capture_result_t captureResult = {};
+     camera3_capture_result_t &captureResult = descriptor->captureResult_;
      captureResult.frame_number = descriptor->frameNumber_;
      captureResult.num_output_buffers = descriptor->buffers_.size();
      for (camera3_stream_buffer_t &buffer : descriptor->buffers_) {
@@ -1166,9 +1173,9 @@ void <a class="moz-txt-link-freetext" href="CameraDevice::requestComplete(Request">CameraDevice::requestComplete(Request</a> *request)
                      buffer.status = CAMERA3_BUFFER_STATUS_ERROR;
              }

-             callbacks_->process_capture_result(callbacks_, &captureResult);
+             descriptor->status_ = <a class="moz-txt-link-freetext" href="Camera3RequestDescriptor::Status::Error">Camera3RequestDescriptor::Status::Error</a>;
+             sendCaptureResults();

-             descriptors_.pop();
              return;
      }

@@ -1234,10 +1241,23 @@ void <a class="moz-txt-link-freetext" href="CameraDevice::requestComplete(Request">CameraDevice::requestComplete(Request</a> *request)
      }

      captureResult.result = resultMetadata->get();
-     callbacks_->process_capture_result(callbacks_, &captureResult);
+     descriptor->status_ = <a class="moz-txt-link-freetext" href="Camera3RequestDescriptor::Status::Success">Camera3RequestDescriptor::Status::Success</a>;
+     sendCaptureResults();
+}

-     MutexLocker descriptorsLock(descriptorsMutex_);
-     descriptors_.pop();
+void <a class="moz-txt-link-freetext" href="CameraDevice::sendCaptureResults()">CameraDevice::sendCaptureResults()</a>
+{
+     MutexLocker lock(descriptorsMutex_);
+     while (!descriptors_.empty() && !descriptors_.front()->isPending()) {
+             <a class="moz-txt-link-freetext" href="std::unique_ptr">std::unique_ptr</a><Camera3RequestDescriptor> descriptor =
+                     <a class="moz-txt-link-freetext" href="std::move(descriptors_.front())">std::move(descriptors_.front())</a>;
</pre>
          </blockquote>
        </blockquote>
        <pre class="moz-quote-pre" wrap="">
nit: I would use auto here.

</pre>
        <blockquote type="cite">
          <blockquote type="cite">
            <pre class="moz-quote-pre" wrap="">+             descriptors_.pop();
+
+             lock.unlock();
+             callbacks_->process_capture_result(callbacks_,
+                                                &descriptor->captureResult_);
+             lock.lock();
</pre>
          </blockquote>
        </blockquote>
        <pre class="moz-quote-pre" wrap="">
Why is lock released during calling the callback? Is there any deadlock here?
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
The Android camera HAL API doesn't specify this as far as I know, so we
decided to minmize lock contention as the default rule.

</pre>
      <blockquote type="cite">
        <pre class="moz-quote-pre" wrap="">I wonder if it might be less efficient to release and re-acquire lock
every call than just holding a lock entirely.
</pre>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
That's a good question, the only way to know would be to measure
performances for both options I suppose.</pre>
    </blockquote>
    <p><br>
    </p>
    <p>The <span class="pun"></span><span class="pln">process_capture_result</span><span
        class="pun"> performance is specified as:   <br>
      </span></p>
    <p><span class="pun">    * This is a non-blocking call. The
        framework will return this call in 5ms.<br>
            *<br>
            * The pipeline latency (see S7 for definition) should be
        less than or equal to<br>
            * 4 frame intervals, and must be less than or equal to 8
        frame intervals.</span></p>
    <p><span class="pun"><a class="moz-txt-link-freetext" href="https://android.googlesource.com/platform/hardware/libhardware/+/master/include/hardware/camera3.h#2781">https://android.googlesource.com/platform/hardware/libhardware/+/master/include/hardware/camera3.h#2781</a></span></p>
    <p><span class="pun">I don't think bring callback under the lock
        would have much impact on the performance, fps-wise?<br>
      </span></p>
    <blockquote type="cite"
      cite="mid:YVQRe8r51rkEIUIF@pendragon.ideasonboard.com">
      <pre class="moz-quote-pre" wrap="">

</pre>
      <blockquote type="cite">
        <blockquote type="cite">
          <blockquote type="cite">
            <pre class="moz-quote-pre" wrap="">+     }
 }

 <a class="moz-txt-link-freetext" href="std::string">std::string</a> <a class="moz-txt-link-freetext" href="CameraDevice::logPrefix()">CameraDevice::logPrefix()</a> const
diff --git a/src/android/camera_device.h b/src/android/camera_device.h
index 9ec510d5..dbfa7431 100644
--- a/src/android/camera_device.h
+++ b/src/android/camera_device.h
@@ -74,17 +74,28 @@ private:
      CameraDevice(unsigned int id, <a class="moz-txt-link-freetext" href="std::shared_ptr">std::shared_ptr</a><a class="moz-txt-link-rfc2396E" href="libcamera::Camera"><libcamera::Camera></a> camera);

      struct Camera3RequestDescriptor {
+             enum class Status {
+                     Pending,
+                     Success,
+                     Error,
+             };
+
              Camera3RequestDescriptor() = default;
              ~Camera3RequestDescriptor() = default;
              Camera3RequestDescriptor(<a class="moz-txt-link-freetext" href="libcamera::Camera">libcamera::Camera</a> *camera,
                                       const camera3_capture_request_t *camera3Request);
              Camera3RequestDescriptor &operator=(Camera3RequestDescriptor &&) = default;

+             bool isPending() const { return status_ == <a class="moz-txt-link-freetext" href="Status::Pending">Status::Pending</a>; }
+
              uint32_t frameNumber_ = 0;
              <a class="moz-txt-link-freetext" href="std::vector">std::vector</a><camera3_stream_buffer_t> buffers_;
              <a class="moz-txt-link-freetext" href="std::vector">std::vector</a><<a class="moz-txt-link-freetext" href="std::unique_ptr">std::unique_ptr</a><a class="moz-txt-link-rfc2396E" href="libcamera::FrameBuffer"><libcamera::FrameBuffer></a>> frameBuffers_;
              CameraMetadata settings_;
              <a class="moz-txt-link-freetext" href="std::unique_ptr">std::unique_ptr</a><CaptureRequest> request_;
+
+             camera3_capture_result_t captureResult_ = {};
+             Status status_ = <a class="moz-txt-link-freetext" href="Status::Pending">Status::Pending</a>;
      };

      enum class State {
@@ -99,12 +110,14 @@ private:
      createFrameBuffer(const buffer_handle_t camera3buffer,
                        <a class="moz-txt-link-freetext" href="libcamera::PixelFormat">libcamera::PixelFormat</a> pixelFormat,
                        const <a class="moz-txt-link-freetext" href="libcamera::Size">libcamera::Size</a> &size);
-     void abortRequest(camera3_capture_request_t *request);
+     void abortRequest(Camera3RequestDescriptor *descriptor,
+                       camera3_capture_request_t *request);
      bool isValidRequest(camera3_capture_request_t *request) const;
      void notifyShutter(uint32_t frameNumber, uint64_t timestamp);
      void notifyError(uint32_t frameNumber, camera3_stream_t *stream,
                       camera3_error_msg_code code);
      int processControls(Camera3RequestDescriptor *descriptor);
+     void sendCaptureResults();
      <a class="moz-txt-link-freetext" href="std::unique_ptr">std::unique_ptr</a><CameraMetadata> getResultMetadata(
              const Camera3RequestDescriptor &descriptor) const;

</pre>
          </blockquote>
        </blockquote>
      </blockquote>
      <pre class="moz-quote-pre" wrap="">
</pre>
    </blockquote>
  </body>
</html>