/**
 * returnResult
 *
 * Returns a partial result metadata buffer, just one.
 *
 * \param reqState[IN]: Request State control structure
 * \param returnIndex[IN]: index of the result buffer in the array of result
 *                         buffers stored in the request
 */
status_t ResultProcessor::returnResult(RequestState_t* reqState, int returnIndex)
{
    status_t status = NO_ERROR;
    camera3_capture_result result;
    CameraMetadata *resultMetadata;
    CLEAR(result);
    resultMetadata = reqState->request->getPartialResultBuffer(returnIndex);
    if (resultMetadata == NULL) {
        LOGE("Cannot get partial result buffer");
        return UNKNOWN_ERROR;
    }
    // This value should be between 1 and android.request.partialResultCount
    // The index goes between 0-partialResultCount -1
    result.partial_result = returnIndex + 1;
    result.frame_number = reqState->reqId;
    result.result = resultMetadata->getAndLock();
    result.num_output_buffers = 0;

    mCallbackOps->process_capture_result(mCallbackOps, &result);

    resultMetadata->unlock(result.result);

    reqState->partialResultReturned += 1;
    LOGR("<Request %d> result cb done", reqState->reqId);
    return status;
}
/**
 * Returns the single partial result stored in the vector.
 * In the future we will have more than one.
 */
void ResultProcessor::returnPendingPartials(RequestState_t* reqState)
{
    camera3_capture_result result;
    CLEAR(result);

    // it must be 1 for >= CAMERA_DEVICE_API_VERSION_3_2 if we don't support partial metadata
    result.partial_result = mPartialResultCount;

    //TODO: combine them all in one metadata buffer and return
    result.frame_number = reqState->reqId;
    // check if metadata result of the previous request is returned
    int pre_reqId = reqState->reqId - 1;
    int index = mRequestsInTransit.indexOfKey(pre_reqId);
    if (index != NAME_NOT_FOUND) {
        RequestState_t *pre_reqState = mRequestsInTransit.valueAt(index);
        if (pre_reqState->partialResultReturned == 0) {
            LOGR("wait the metadata of the previous request return");
            mRequestsPendingMetaReturn.add(reqState->reqId);
            return;
        }
    }

    CameraMetadata * settings = reqState->pendingPartialResults[0];

    result.result = settings->getAndLock();
    result.num_output_buffers = 0;

    mCallbackOps->process_capture_result(mCallbackOps, &result);

    settings->unlock(result.result);

    reqState->partialResultReturned += 1;
    LOGR("<Request %d> result cb done",reqState->reqId);
    reqState->pendingPartialResults.clear();
}