Commit message: None
CVE: CVE-2016-2449
CWE: CWE-264
Vulnerability description: services/camera/libcameraservice/device3/Camera3Device.cpp in mediaserver in Android 4.x before 4.4.4, 5.0.x before 5.0.2, 5.1.x before 5.1.1, and 6.x before 2016-05-01 does not validate template IDs, which allows attackers to gain privileges via a crafted application, as demonstrated by obtaining Signature or SignatureOrSystem access, aka internal bug 27568958.

Project: Android

Camera3Device::Camera3Device(int id):
        mId(id),
        mIsConstrainedHighSpeedConfiguration(false),
        mHal3Device(NULL),
        mStatus(STATUS_UNINITIALIZED),
        mStatusWaiters(0),
        mUsePartialResult(false),
        mNumPartialResults(1),
        mNextResultFrameNumber(0),
        mNextReprocessResultFrameNumber(0),
        mNextShutterFrameNumber(0),
        mNextReprocessShutterFrameNumber(0),
        mListener(NULL)
{
    ATRACE_CALL();
    camera3_callback_ops::notify = &sNotify;
    camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
    ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
}

Project: Android

Camera3Device::PreparerThread::PreparerThread() :
 Thread(/*canCallJava*/false), mActive(false), mCancelNow(false) {
}

Project: Android

Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
        sp<StatusTracker> statusTracker,
 camera3_device_t *hal3Device,
 bool aeLockAvailable) :
 Thread(/*canCallJava*/false),
        mParent(parent),
        mStatusTracker(statusTracker),
        mHal3Device(hal3Device),
        mId(getId(parent)),
        mReconfigured(false),
        mDoPause(false),
        mPaused(true),
        mFrameNumber(0),
        mLatestRequestId(NAME_NOT_FOUND),
        mCurrentAfTriggerId(0),
        mCurrentPreCaptureTriggerId(0),
        mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES),
        mAeLockAvailable(aeLockAvailable) {
    mStatusId = statusTracker->addComponent();
}

Project: Android

status_t Camera3Device::addDummyStreamLocked() {
    ATRACE_CALL();
 status_t res;

 if (mDummyStreamId != NO_STREAM) {
        SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
                __FUNCTION__, mId);
 return INVALID_OPERATION;
 }

    ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);

    sp<Camera3OutputStreamInterface> dummyStream =
 new Camera3DummyStream(mNextStreamId);

    res = mOutputStreams.add(mNextStreamId, dummyStream);
 if (res < 0) {
        SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
 return res;
 }

    mDummyStreamId = mNextStreamId;
    mNextStreamId++;

 return OK;
}

Project: Android

status_t Camera3Device::RequestThread::addDummyTriggerIds(
 const sp<CaptureRequest> &request) {
 static const int32_t dummyTriggerId = 1;
 status_t res;

 CameraMetadata &metadata = request->mSettings;

    camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
    camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
 if (afTrigger.count > 0 &&
            afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
            afId.count == 0) {
        res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
 if (res != OK) return res;
 }

    camera_metadata_entry pcTrigger =
            metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
    camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
 if (pcTrigger.count > 0 &&
            pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
            pcId.count == 0) {
        res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
 &dummyTriggerId, 1);
 if (res != OK) return res;
 }

 return OK;
}

Project: Android

status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
    ATRACE_CALL();

 List<const CameraMetadata> requests;
    requests.push_back(request);
 return captureList(requests, /*lastFrameNumber*/NULL);
}

Project: Android

status_t Camera3Device::checkStatusOkToCaptureLocked() {
 switch (mStatus) {
 case STATUS_ERROR:
            CLOGE("Device has encountered a serious error");
 return INVALID_OPERATION;
 case STATUS_UNINITIALIZED:
            CLOGE("Device not initialized");
 return INVALID_OPERATION;
 case STATUS_UNCONFIGURED:
 case STATUS_CONFIGURED:
 case STATUS_ACTIVE:
 break;
 default:
            SET_ERR_L("Unexpected status: %d", mStatus);
 return INVALID_OPERATION;
 }
 return OK;
}

Project: Android

void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
 if (mNextRequests.empty()) {
 return;
 }

 for (auto& nextRequest : mNextRequests) {
 if (nextRequest.submitted) {
 continue;
 }

        sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;

 if (halRequest->settings != NULL) {
            captureRequest->mSettings.unlock(halRequest->settings);
 }

 if (captureRequest->mInputStream != NULL) {
            captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
            captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
 }

 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
            outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
            captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
 }

 if (sendRequestError) {
 Mutex::Autolock l(mRequestLock);
 if (mListener != NULL) {
                mListener->notifyError(
 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
                        captureRequest->mResultExtras);
 }
 }
 }

 Mutex::Autolock l(mRequestLock);
    mNextRequests.clear();
}

Project: Android

status_t Camera3Device::PreparerThread::clear() {
 status_t res;

 Mutex::Autolock l(mLock);

 for (const auto& stream : mPendingStreams) {
        stream->cancelPrepare();
 }
    mPendingStreams.clear();
    mCancelNow = true;

 return OK;
}

Project: Android

status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
 Mutex::Autolock l(mRequestLock);
    mRepeatingRequests.clear();
 if (lastFrameNumber != NULL) {
 *lastFrameNumber = mRepeatingLastFrameNumber;
 }
    mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
 return OK;
}

Project: Android

status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
    ATRACE_CALL();
 Mutex::Autolock il(mInterfaceLock);
 Mutex::Autolock l(mLock);

 switch (mStatus) {
 case STATUS_ERROR:
            CLOGE("Device has encountered a serious error");
 return INVALID_OPERATION;
 case STATUS_UNINITIALIZED:
            CLOGE("Device not initialized");
 return INVALID_OPERATION;
 case STATUS_UNCONFIGURED:
 case STATUS_CONFIGURED:
 case STATUS_ACTIVE:
 break;
 default:
            SET_ERR_L("Unexpected status: %d", mStatus);
 return INVALID_OPERATION;
 }
    ALOGV("Camera %d: Clearing repeating request", mId);

 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
}

Project: Android

void Camera3Device::RequestThread::configurationComplete() {
 Mutex::Autolock l(mRequestLock);
    mReconfigured = true;
}

Project: Android

status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
    ATRACE_CALL();
    ALOGV("%s: E", __FUNCTION__);

 Mutex::Autolock il(mInterfaceLock);
 Mutex::Autolock l(mLock);

 if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
        mNeedConfig = true;
        mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
 }

 return configureStreamsLocked();
}

Project: Android

status_t Camera3Device::configureStreamsLocked() {
    ATRACE_CALL();
 status_t res;

 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
        CLOGE("Not idle");
 return INVALID_OPERATION;
 }

 if (!mNeedConfig) {
        ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
 return OK;
 }

 if (mOutputStreams.size() == 0) {
        addDummyStreamLocked();
 } else {
        tryRemoveDummyStreamLocked();
 }

    ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);

    camera3_stream_configuration config;
    config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
            CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
            CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
    config.num_streams = (mInputStream != NULL) + mOutputStreams.size();

 Vector<camera3_stream_t*> streams;
    streams.setCapacity(config.num_streams);

 if (mInputStream != NULL) {
 camera3_stream_t *inputStream;
        inputStream = mInputStream->startConfiguration();
 if (inputStream == NULL) {
            SET_ERR_L("Can't start input stream configuration");
 return INVALID_OPERATION;
 }
        streams.add(inputStream);
 }

 for (size_t i = 0; i < mOutputStreams.size(); i++) {

 if (mOutputStreams[i].get() ==
 static_cast<Camera3StreamInterface*>(mInputStream.get())) {

            config.num_streams--;
 continue;
 }

 camera3_stream_t *outputStream;
        outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
 if (outputStream == NULL) {
            SET_ERR_L("Can't start output stream configuration");
 return INVALID_OPERATION;
 }
        streams.add(outputStream);
 }

    config.streams = streams.editArray();

    ATRACE_BEGIN("camera3->configure_streams");
    res = mHal3Device->ops->configure_streams(mHal3Device, &config);
    ATRACE_END();

 if (res == BAD_VALUE) {
 if (mInputStream != NULL && mInputStream->isConfiguring()) {
            res = mInputStream->cancelConfiguration();
 if (res != OK) {
                SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
                        mInputStream->getId(), strerror(-res), res);
 return res;
 }
 }

 for (size_t i = 0; i < mOutputStreams.size(); i++) {
            sp<Camera3OutputStreamInterface> outputStream =
                    mOutputStreams.editValueAt(i);
 if (outputStream->isConfiguring()) {
                res = outputStream->cancelConfiguration();
 if (res != OK) {
                    SET_ERR_L(
 "Can't cancel configuring output stream %d: %s (%d)",
                        outputStream->getId(), strerror(-res), res);
 return res;
 }
 }
 }

        internalUpdateStatusLocked(STATUS_UNCONFIGURED);
        mNeedConfig = true;

        ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
 return BAD_VALUE;
 } else if (res != OK) {
        SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
                strerror(-res), res);
 return res;
 }


 if (mInputStream != NULL && mInputStream->isConfiguring()) {
        res = mInputStream->finishConfiguration(mHal3Device);
 if (res != OK) {
            SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
                    mInputStream->getId(), strerror(-res), res);
 return res;
 }
 }

 for (size_t i = 0; i < mOutputStreams.size(); i++) {
        sp<Camera3OutputStreamInterface> outputStream =
            mOutputStreams.editValueAt(i);
 if (outputStream->isConfiguring()) {
            res = outputStream->finishConfiguration(mHal3Device);
 if (res != OK) {
                SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
                        outputStream->getId(), strerror(-res), res);
 return res;
 }
 }
 }

    mRequestThread->configurationComplete();

 if (mIsConstrainedHighSpeedConfiguration) {
 pid_t requestThreadTid = mRequestThread->getTid();
        res = requestPriority(getpid(), requestThreadTid,
                kConstrainedHighSpeedThreadPriority, true);
 if (res != OK) {
            ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
                    strerror(-res), res);
 } else {
            ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
 }
 } else {
 }


    mNeedConfig = false;

    internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
            STATUS_CONFIGURED : STATUS_UNCONFIGURED);

    ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);

    mDeletedStreams.clear();

 return OK;
}

Project: Android

status_t Camera3Device::convertMetadataListToRequestListLocked(
 const List<const CameraMetadata> &metadataList, RequestList *requestList) {
 if (requestList == NULL) {
        CLOGE("requestList cannot be NULL.");
 return BAD_VALUE;
 }

 int32_t burstId = 0;
 for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
            it != metadataList.end(); ++it) {
        sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
 if (newRequest == 0) {
            CLOGE("Can't create capture request");
 return BAD_VALUE;
 }

        newRequest->mResultExtras.burstId = burstId++;
 if (it->exists(ANDROID_REQUEST_ID)) {
 if (it->find(ANDROID_REQUEST_ID).count == 0) {
                CLOGE("RequestID entry exists; but must not be empty in metadata");
 return BAD_VALUE;
 }
            newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
 } else {
            CLOGE("RequestID does not exist in metadata");
 return BAD_VALUE;
 }

        requestList->push_back(newRequest);

        ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
 }

 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
 auto firstRequest = requestList->begin();
 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
 if (outputStream->isVideoStream()) {
 (*firstRequest)->mBatchSize = requestList->size();
 break;
 }
 }
 }

 return OK;
}

Project: Android

sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
 const CameraMetadata &request) {
    ATRACE_CALL();
 status_t res;

    sp<CaptureRequest> newRequest = new CaptureRequest;
    newRequest->mSettings = request;

 camera_metadata_entry_t inputStreams =
            newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
 if (inputStreams.count > 0) {
 if (mInputStream == NULL ||
                mInputStream->getId() != inputStreams.data.i32[0]) {
            CLOGE("Request references unknown input stream %d",
                    inputStreams.data.u8[0]);
 return NULL;
 }
 if (mInputStream->isConfiguring()) {
            res = mInputStream->finishConfiguration(mHal3Device);
 if (res != OK) {
                SET_ERR_L("Unable to finish configuring input stream %d:"
 " %s (%d)",
                        mInputStream->getId(), strerror(-res), res);
 return NULL;
 }
 }
 if (mInputStream->isPreparing()) {
            CLOGE("Request references an input stream that's being prepared!");
 return NULL;
 }

        newRequest->mInputStream = mInputStream;
        newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
 }

 camera_metadata_entry_t streams =
            newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
 if (streams.count == 0) {
        CLOGE("Zero output streams specified!");
 return NULL;
 }

 for (size_t i = 0; i < streams.count; i++) {
 int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
 if (idx == NAME_NOT_FOUND) {
            CLOGE("Request references unknown stream %d",
                    streams.data.u8[i]);
 return NULL;
 }
        sp<Camera3OutputStreamInterface> stream =
                mOutputStreams.editValueAt(idx);

 if (stream->isConfiguring()) {
            res = stream->finishConfiguration(mHal3Device);
 if (res != OK) {
                SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
                        stream->getId(), strerror(-res), res);
 return NULL;
 }
 }
 if (stream->isPreparing()) {
            CLOGE("Request references an output stream that's being prepared!");
 return NULL;
 }

        newRequest->mOutputStreams.push(stream);
 }
    newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
    newRequest->mBatchSize = 1;

 return newRequest;
}

Project: Android

status_t Camera3Device::createInputStream(
 uint32_t width, uint32_t height, int format, int *id) {
    ATRACE_CALL();
 Mutex::Autolock il(mInterfaceLock);
 Mutex::Autolock l(mLock);
    ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
            mId, mNextStreamId, width, height, format);

 status_t res;
 bool wasActive = false;

 switch (mStatus) {
 case STATUS_ERROR:
            ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
 return INVALID_OPERATION;
 case STATUS_UNINITIALIZED:
            ALOGE("%s: Device not initialized", __FUNCTION__);
 return INVALID_OPERATION;
 case STATUS_UNCONFIGURED:
 case STATUS_CONFIGURED:
 break;
 case STATUS_ACTIVE:
            ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
            res = internalPauseAndWaitLocked();
 if (res != OK) {
                SET_ERR_L("Can't pause captures to reconfigure streams!");
 return res;
 }
            wasActive = true;
 break;
 default:
            SET_ERR_L("%s: Unexpected status: %d", mStatus);
 return INVALID_OPERATION;
 }
    assert(mStatus != STATUS_ACTIVE);

 if (mInputStream != 0) {
        ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
 return INVALID_OPERATION;
 }

    sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
                width, height, format);
    newStream->setStatusTracker(mStatusTracker);

    mInputStream = newStream;

 *id = mNextStreamId++;

 if (wasActive) {
        ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
        res = configureStreamsLocked();
 if (res != OK) {
            ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
                    __FUNCTION__, mNextStreamId, strerror(-res), res);
 return res;
 }
        internalResumeLocked();
 }

    ALOGV("Camera %d: Created input stream", mId);
 return OK;
}

Project: Android

status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
    ATRACE_CALL();
 (void)outputId; (void)id;

    CLOGE("Unimplemented");
 return INVALID_OPERATION;
}

Project: Android

status_t Camera3Device::createStream(sp<Surface> consumer,
 uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
 camera3_stream_rotation_t rotation, int *id) {
    ATRACE_CALL();
 Mutex::Autolock il(mInterfaceLock);
 Mutex::Autolock l(mLock);
    ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d",
            mId, mNextStreamId, width, height, format, dataSpace, rotation);

 status_t res;
 bool wasActive = false;

 switch (mStatus) {
 case STATUS_ERROR:
            CLOGE("Device has encountered a serious error");
 return INVALID_OPERATION;
 case STATUS_UNINITIALIZED:
            CLOGE("Device not initialized");
 return INVALID_OPERATION;
 case STATUS_UNCONFIGURED:
 case STATUS_CONFIGURED:
 break;
 case STATUS_ACTIVE:
            ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
            res = internalPauseAndWaitLocked();
 if (res != OK) {
                SET_ERR_L("Can't pause captures to reconfigure streams!");
 return res;
 }
            wasActive = true;
 break;
 default:
            SET_ERR_L("Unexpected status: %d", mStatus);
 return INVALID_OPERATION;
 }
    assert(mStatus != STATUS_ACTIVE);

    sp<Camera3OutputStream> newStream;
 if (format == HAL_PIXEL_FORMAT_BLOB) {
 ssize_t blobBufferSize;
 if (dataSpace != HAL_DATASPACE_DEPTH) {
            blobBufferSize = getJpegBufferSize(width, height);
 if (blobBufferSize <= 0) {
                SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
 return BAD_VALUE;
 }
 } else {
            blobBufferSize = getPointCloudBufferSize();
 if (blobBufferSize <= 0) {
                SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
 return BAD_VALUE;
 }
 }
        newStream = new Camera3OutputStream(mNextStreamId, consumer,
                width, height, blobBufferSize, format, dataSpace, rotation);
 } else {
        newStream = new Camera3OutputStream(mNextStreamId, consumer,
                width, height, format, dataSpace, rotation);
 }
    newStream->setStatusTracker(mStatusTracker);

    res = mOutputStreams.add(mNextStreamId, newStream);
 if (res < 0) {
        SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
 return res;
 }

 *id = mNextStreamId++;
    mNeedConfig = true;

 if (wasActive) {
        ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
        res = configureStreamsLocked();
 if (res != OK) {
            CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
                    mNextStreamId, strerror(-res), res);
 return res;
 }
        internalResumeLocked();
 }
    ALOGV("Camera %d: Created new stream", mId);
 return OK;
}

Project: Android

status_t Camera3Device::createZslStream(
 uint32_t width, uint32_t height,
 int depth,
 /*out*/
 int *id,
            sp<Camera3ZslStream>* zslStream) {
    ATRACE_CALL();
 Mutex::Autolock il(mInterfaceLock);
 Mutex::Autolock l(mLock);
    ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
            mId, mNextStreamId, width, height, depth);

 status_t res;
 bool wasActive = false;

 switch (mStatus) {
 case STATUS_ERROR:
            ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
 return INVALID_OPERATION;
 case STATUS_UNINITIALIZED:
            ALOGE("%s: Device not initialized", __FUNCTION__);
 return INVALID_OPERATION;
 case STATUS_UNCONFIGURED:
 case STATUS_CONFIGURED:
 break;
 case STATUS_ACTIVE:
            ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
            res = internalPauseAndWaitLocked();
 if (res != OK) {
                SET_ERR_L("Can't pause captures to reconfigure streams!");
 return res;
 }
            wasActive = true;
 break;
 default:
            SET_ERR_L("Unexpected status: %d", mStatus);
 return INVALID_OPERATION;
 }
    assert(mStatus != STATUS_ACTIVE);

 if (mInputStream != 0) {
        ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
 return INVALID_OPERATION;
 }

    sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
                width, height, depth);
    newStream->setStatusTracker(mStatusTracker);

    res = mOutputStreams.add(mNextStreamId, newStream);
 if (res < 0) {
        ALOGE("%s: Can't add new stream to set: %s (%d)",
                __FUNCTION__, strerror(-res), res);
 return res;
 }
    mInputStream = newStream;

    mNeedConfig = true;

 *id = mNextStreamId++;
 *zslStream = newStream;

 if (wasActive) {
        ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
        res = configureStreamsLocked();
 if (res != OK) {
            ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
                    __FUNCTION__, mNextStreamId, strerror(-res), res);
 return res;
 }
        internalResumeLocked();
 }

    ALOGV("Camera %d: Created ZSL stream", mId);
 return OK;
}

Project: Android

status_t Camera3Device::deleteReprocessStream(int id) {
    ATRACE_CALL();
 (void)id;

    CLOGE("Unimplemented");
 return INVALID_OPERATION;
}

Project: Android

status_t Camera3Device::deleteStream(int id) {
    ATRACE_CALL();
 Mutex::Autolock il(mInterfaceLock);
 Mutex::Autolock l(mLock);
 status_t res;

    ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);

 if (mStatus == STATUS_ACTIVE) {
        ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
 return -EBUSY;
 }

    sp<Camera3StreamInterface> deletedStream;
 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
 if (mInputStream != NULL && id == mInputStream->getId()) {
        deletedStream = mInputStream;
        mInputStream.clear();
 } else {
 if (outputStreamIdx == NAME_NOT_FOUND) {
            CLOGE("Stream %d does not exist", id);
 return BAD_VALUE;
 }
 }

 if (outputStreamIdx != NAME_NOT_FOUND) {
        deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
        mOutputStreams.removeItem(id);
 }

    res = deletedStream->disconnect();
 if (res != OK) {
        SET_ERR_L("Can't disconnect deleted stream %d", id);
 }
    mDeletedStreams.add(deletedStream);
    mNeedConfig = true;

 return res;
}

Project: Android

status_t Camera3Device::disconnect() {
    ATRACE_CALL();
 Mutex::Autolock il(mInterfaceLock);

    ALOGV("%s: E", __FUNCTION__);

 status_t res = OK;

 {
 Mutex::Autolock l(mLock);
 if (mStatus == STATUS_UNINITIALIZED) return res;

 if (mStatus == STATUS_ACTIVE ||
 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
            res = mRequestThread->clearRepeatingRequests();
 if (res != OK) {
                SET_ERR_L("Can't stop streaming");
 } else {
                res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
 if (res != OK) {
                    SET_ERR_L("Timeout waiting for HAL to drain");
 }
 }
 }

 if (mStatus == STATUS_ERROR) {
            CLOGE("Shutting down in an error state");
 }

 if (mStatusTracker != NULL) {
            mStatusTracker->requestExit();
 }

 if (mRequestThread != NULL) {
            mRequestThread->requestExit();
 }

        mOutputStreams.clear();
        mInputStream.clear();
 }

 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
        mRequestThread->join();
 }

 if (mStatusTracker != NULL) {
        mStatusTracker->join();
 }

 camera3_device_t *hal3Device;
 {
 Mutex::Autolock l(mLock);

        mRequestThread.clear();
        mStatusTracker.clear();

        hal3Device = mHal3Device;
 }

 if (hal3Device != NULL) {
        ATRACE_BEGIN("camera3->close");
        hal3Device->common.close(&hal3Device->common);
        ATRACE_END();
 }

 {
 Mutex::Autolock l(mLock);
        mHal3Device = NULL;
        internalUpdateStatusLocked(STATUS_UNINITIALIZED);
 }

    ALOGV("%s: X", __FUNCTION__);
 return res;
}

Project: Android

status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
    ATRACE_CALL();
 (void)args;

 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
 bool gotLock = tryLockSpinRightRound(mLock);

    ALOGW_IF(!gotInterfaceLock,
 "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
            mId, __FUNCTION__);
    ALOGW_IF(!gotLock,
 "Camera %d: %s: Unable to lock main lock, proceeding anyway",
            mId, __FUNCTION__);

 String8 lines;

 const char *status =
            mStatus == STATUS_ERROR         ? "ERROR" :
            mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
            mStatus == STATUS_UNCONFIGURED  ? "UNCONFIGURED" :
            mStatus == STATUS_CONFIGURED    ? "CONFIGURED" :
            mStatus == STATUS_ACTIVE        ? "ACTIVE" :
 "Unknown";

    lines.appendFormat("    Device status: %s\n", status);
 if (mStatus == STATUS_ERROR) {
        lines.appendFormat("    Error cause: %s\n", mErrorCause.string());
 }
    lines.appendFormat("    Stream configuration:\n");
    lines.appendFormat("    Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
 "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");

 if (mInputStream != NULL) {
        write(fd, lines.string(), lines.size());
        mInputStream->dump(fd, args);
 } else {
        lines.appendFormat("      No input stream.\n");
        write(fd, lines.string(), lines.size());
 }
 for (size_t i = 0; i < mOutputStreams.size(); i++) {
        mOutputStreams[i]->dump(fd,args);
 }

    lines = String8("    In-flight requests:\n");
 if (mInFlightMap.size() == 0) {
        lines.append("      None\n");
 } else {
 for (size_t i = 0; i < mInFlightMap.size(); i++) {
 InFlightRequest r = mInFlightMap.valueAt(i);
            lines.appendFormat("      Frame %d |  Timestamp: %" PRId64 ", metadata"
 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
                    r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
                    r.numBuffersLeft);
 }
 }
    write(fd, lines.string(), lines.size());

 {
        lines = String8("    Last request sent:\n");
        write(fd, lines.string(), lines.size());

 CameraMetadata lastRequest = getLatestRequestLocked();
        lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
 }

 if (mHal3Device != NULL) {
        lines = String8("    HAL device dump:\n");
        write(fd, lines.string(), lines.size());
        mHal3Device->ops->dump(mHal3Device, fd);
 }

 if (gotLock) mLock.unlock();
 if (gotInterfaceLock) mInterfaceLock.unlock();

 return OK;
}

Project: Android

status_t Camera3Device::flush(int64_t *frameNumber) {
    ATRACE_CALL();
    ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
 Mutex::Autolock il(mInterfaceLock);

 NotificationListener* listener;
 {
 Mutex::Autolock l(mOutputLock);
        listener = mListener;
 }

 {
 Mutex::Autolock l(mLock);
        mRequestThread->clear(listener, /*out*/frameNumber);
 }

 status_t res;
 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
        res = mRequestThread->flush();
 } else {
 Mutex::Autolock l(mLock);
        res = waitUntilDrainedLocked();
 }

 return res;
}

Project: Android

status_t Camera3Device::RequestThread::flush() {
    ATRACE_CALL();
 Mutex::Autolock l(mFlushLock);

 if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
 return mHal3Device->ops->flush(mHal3Device);
 }

 return -ENOTSUP;
}

Project: Android

bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
        T* value, uint32_t frameNumber) {
 (void) frameNumber;

 camera_metadata_ro_entry_t entry;

    entry = result.find(tag);
 if (entry.count == 0) {
        ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
            mId, frameNumber, get_camera_metadata_tag_name(tag));
 return false;
 }

 if (sizeof(T) == sizeof(uint8_t)) {
 *value = entry.data.u8[0];
 } else if (sizeof(T) == sizeof(int32_t)) {
 *value = entry.data.i32[0];
 } else {
        ALOGE("%s: Unexpected type", __FUNCTION__);
 return false;
 }
 return true;
}

Project: Android

uint32_t Camera3Device::getDeviceVersion() {
    ATRACE_CALL();
 Mutex::Autolock il(mInterfaceLock);
 return mDeviceVersion;
}

Project: Android

int Camera3Device::getId() const {
 return mId;
}

Project: Android

int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
    sp<Camera3Device> d = device.promote();
 if (d != NULL) return d->mId;
 return 0;
}

Project: Android

status_t Camera3Device::getInputBufferProducer(
        sp<IGraphicBufferProducer> *producer) {
 Mutex::Autolock il(mInterfaceLock);
 Mutex::Autolock l(mLock);

 if (producer == NULL) {
 return BAD_VALUE;
 } else if (mInputStream == NULL) {
 return INVALID_OPERATION;
 }

 return mInputStream->getInputBufferProducer(producer);
}

Project: Android

ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
 Size maxJpegResolution = getMaxJpegResolution();
 if (maxJpegResolution.width == 0) {
        ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
                __FUNCTION__, mId);
 return BAD_VALUE;
 }

 ssize_t maxJpegBufferSize = 0;
    camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
 if (jpegBufMaxSize.count == 0) {
        ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
 return BAD_VALUE;
 }
    maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
    assert(kMinJpegBufferSize < maxJpegBufferSize);

 float scaleFactor = ((float) (width * height)) /
 (maxJpegResolution.width * maxJpegResolution.height);
 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
            kMinJpegBufferSize;
 if (jpegBufferSize > maxJpegBufferSize) {
        jpegBufferSize = maxJpegBufferSize;
 }

 return jpegBufferSize;
}

Project: Android

CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
 Mutex::Autolock al(mLatestRequestMutex);

    ALOGV("RequestThread::%s", __FUNCTION__);

 return mLatestRequest;
}

Project: Android

CameraMetadata Camera3Device::getLatestRequestLocked() {
    ALOGV("%s", __FUNCTION__);

 CameraMetadata retVal;

 if (mRequestThread != NULL) {
        retVal = mRequestThread->getLatestRequest();
 }

 return retVal;
}

Project: Android

Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
 const int STREAM_CONFIGURATION_SIZE = 4;
 const int STREAM_FORMAT_OFFSET = 0;
 const int STREAM_WIDTH_OFFSET = 1;
 const int STREAM_HEIGHT_OFFSET = 2;
 const int STREAM_IS_INPUT_OFFSET = 3;
 camera_metadata_ro_entry_t availableStreamConfigs =
                mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
 if (availableStreamConfigs.count == 0 ||
                availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
 return Size(0, 0);
 }

 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
 && format == HAL_PIXEL_FORMAT_BLOB &&
 (width * height > maxJpegWidth * maxJpegHeight)) {
                maxJpegWidth = width;
                maxJpegHeight = height;
 }
 }
 } else {
        camera_metadata_ro_entry availableJpegSizes =
                mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
 if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
 return Size(0, 0);
 }

 for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
 if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
 > (maxJpegWidth * maxJpegHeight)) {
                maxJpegWidth = availableJpegSizes.data.i32[i];
                maxJpegHeight = availableJpegSizes.data.i32[i + 1];
 }
 }
 }
 return Size(maxJpegWidth, maxJpegHeight);
}

Project: Android

status_t Camera3Device::getNextResult(CaptureResult *frame) {
    ATRACE_CALL();
 Mutex::Autolock l(mOutputLock);

 if (mResultQueue.empty()) {
 return NOT_ENOUGH_DATA;
 }

 if (frame == NULL) {
        ALOGE("%s: argument cannot be NULL", __FUNCTION__);
 return BAD_VALUE;
 }

 CaptureResult &result = *(mResultQueue.begin());
    frame->mResultExtras = result.mResultExtras;
    frame->mMetadata.acquire(result.mMetadata);
    mResultQueue.erase(mResultQueue.begin());

 return OK;
}

Project: Android

ssize_t Camera3Device::getPointCloudBufferSize() const {
 const int FLOATS_PER_POINT=4;
    camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
 if (maxPointCount.count == 0) {
        ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
                __FUNCTION__, mId);
 return BAD_VALUE;
 }
 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
            maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
 return maxBytesForPointCloud;
}

Project: Android

status_t Camera3Device::getStreamInfo(int id,
 uint32_t *width, uint32_t *height,
 uint32_t *format, android_dataspace *dataSpace) {
    ATRACE_CALL();
 Mutex::Autolock il(mInterfaceLock);
 Mutex::Autolock l(mLock);

 switch (mStatus) {
 case STATUS_ERROR:
            CLOGE("Device has encountered a serious error");
 return INVALID_OPERATION;
 case STATUS_UNINITIALIZED:
            CLOGE("Device not initialized!");
 return INVALID_OPERATION;
 case STATUS_UNCONFIGURED:
 case STATUS_CONFIGURED:
 case STATUS_ACTIVE:
 break;
 default:
            SET_ERR_L("Unexpected status: %d", mStatus);
 return INVALID_OPERATION;
 }

 ssize_t idx = mOutputStreams.indexOfKey(id);
 if (idx == NAME_NOT_FOUND) {
        CLOGE("Stream %d is unknown", id);
 return idx;
 }

 if (width) *width  = mOutputStreams[idx]->getWidth();
 if (height) *height = mOutputStreams[idx]->getHeight();
 if (format) *format = mOutputStreams[idx]->getFormat();
 if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
 return OK;
}

Project: Android

void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
    request->mAeTriggerCancelOverride.applyAeLock = false;
    request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;

 if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
 return;
 }

 camera_metadata_entry_t aePrecaptureTrigger =
            request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
 if (aePrecaptureTrigger.count > 0 &&
            aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
 uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
        request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
        request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
        request->mAeTriggerCancelOverride.aePrecaptureTrigger =
                ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;

 if (mAeLockAvailable == true) {
 camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
 if (aeLock.count == 0 ||  aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
                request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
                request->mAeTriggerCancelOverride.applyAeLock = true;
                request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
 }
 }
 }
}

Project: Android

const CameraMetadata& Camera3Device::info() const {
    ALOGVV("%s: E", __FUNCTION__);
 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
                    mStatus == STATUS_ERROR)) {
        ALOGW("%s: Access to static info %s!", __FUNCTION__,
                mStatus == STATUS_ERROR ?
 "when in error state" : "before init");
 }
 return mDeviceInfo;
}

Project: Android

status_t Camera3Device::initialize(CameraModule *module)
{
    ATRACE_CALL();
 Mutex::Autolock il(mInterfaceLock);
 Mutex::Autolock l(mLock);

    ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
 if (mStatus != STATUS_UNINITIALIZED) {
        CLOGE("Already initialized!");
 return INVALID_OPERATION;
 }

 /** Open HAL device */

 status_t res;
 String8 deviceName = String8::format("%d", mId);

 camera3_device_t *device;

    ATRACE_BEGIN("camera3->open");
    res = module->open(deviceName.string(),
 reinterpret_cast<hw_device_t**>(&device));
    ATRACE_END();

 if (res != OK) {
        SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
 return res;
 }

 /** Cross-check device version */
 if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
        SET_ERR_L("Could not open camera: "
 "Camera device should be at least %x, reports %x instead",
                CAMERA_DEVICE_API_VERSION_3_0,
                device->common.version);
        device->common.close(&device->common);
 return BAD_VALUE;
 }

    camera_info info;
    res = CameraService::filterGetInfoErrorCode(module->getCameraInfo(
        mId, &info));
 if (res != OK) return res;

 if (info.device_version != device->common.version) {
        SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
 " and device version (%x).",
                info.device_version, device->common.version);
        device->common.close(&device->common);
 return BAD_VALUE;
 }

 /** Initialize device with callback functions */

    ATRACE_BEGIN("camera3->initialize");
    res = device->ops->initialize(device, this);
    ATRACE_END();

 if (res != OK) {
        SET_ERR_L("Unable to initialize HAL device: %s (%d)",
                strerror(-res), res);
        device->common.close(&device->common);
 return BAD_VALUE;
 }

 /** Start up status tracker thread */
    mStatusTracker = new StatusTracker(this);
    res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
 if (res != OK) {
        SET_ERR_L("Unable to start status tracking thread: %s (%d)",
                strerror(-res), res);
        device->common.close(&device->common);
        mStatusTracker.clear();
 return res;
 }

 bool aeLockAvailable = false;
    camera_metadata_ro_entry aeLockAvailableEntry;
    res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
            ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
 if (res == OK && aeLockAvailableEntry.count > 0) {
        aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
                ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
 }

 /** Start up request queue thread */
    mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
    res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
 if (res != OK) {
        SET_ERR_L("Unable to start request queue thread: %s (%d)",
                strerror(-res), res);
        device->common.close(&device->common);
        mRequestThread.clear();
 return res;
 }

    mPreparerThread = new PreparerThread();

 /** Everything is good to go */

    mDeviceVersion = device->common.version;
    mDeviceInfo = info.static_camera_characteristics;
    mHal3Device = device;

    internalUpdateStatusLocked(STATUS_UNCONFIGURED);
    mNextStreamId = 0;
    mDummyStreamId = NO_STREAM;
    mNeedConfig = true;
    mPauseStateNotify = false;

 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
        camera_metadata_entry partialResultsCount =
                mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
 if (partialResultsCount.count > 0) {
            mNumPartialResults = partialResultsCount.data.i32[0];
            mUsePartialResult = (mNumPartialResults > 1);
 }
 } else {
        camera_metadata_entry partialResultsQuirk =
                mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
 if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
            mUsePartialResult = true;
 }
 }

    camera_metadata_entry configs =
            mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
 for (uint32_t i = 0; i < configs.count; i += 4) {
 if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
                configs.data.i32[i + 3] ==
                ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
            mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
                    configs.data.i32[i + 2]));
 }
 }

 return OK;
}

Project: Android

status_t Camera3Device::RequestThread::insertTriggers(
 const sp<CaptureRequest> &request) {

 Mutex::Autolock al(mTriggerMutex);

    sp<Camera3Device> parent = mParent.promote();
 if (parent == NULL) {
        CLOGE("RequestThread: Parent is gone");
 return DEAD_OBJECT;
 }

 CameraMetadata &metadata = request->mSettings;
 size_t count = mTriggerMap.size();

 for (size_t i = 0; i < count; ++i) {
 RequestTrigger trigger = mTriggerMap.valueAt(i);
 uint32_t tag = trigger.metadataTag;

 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
 if (isAeTrigger) {
                request->mResultExtras.precaptureTriggerId = triggerId;
                mCurrentPreCaptureTriggerId = triggerId;
 } else {
                request->mResultExtras.afTriggerId = triggerId;
                mCurrentAfTriggerId = triggerId;
 }
 if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
 continue; // Trigger ID tag is deprecated since device HAL 3.2
 }
 }

        camera_metadata_entry entry = metadata.find(tag);

 if (entry.count > 0) {
 /**
             * Already has an entry for this trigger in the request.
             * Rewrite it with our requested trigger value.
             */
 RequestTrigger oldTrigger = trigger;

            oldTrigger.entryValue = entry.data.u8[0];

            mTriggerReplacedMap.add(tag, oldTrigger);
 } else {
 /**
             * More typical, no trigger entry, so we just add it
             */
            mTriggerRemovedMap.add(tag, trigger);
 }

 status_t res;

 switch (trigger.getTagType()) {
 case TYPE_BYTE: {
 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
                res = metadata.update(tag,
 &entryValue,
 /*count*/1);
 break;
 }
 case TYPE_INT32:
                res = metadata.update(tag,
 &trigger.entryValue,
 /*count*/1);
 break;
 default:
                ALOGE("%s: Type not supported: 0x%x",
                      __FUNCTION__,
                      trigger.getTagType());
 return INVALID_OPERATION;
 }

 if (res != OK) {
            ALOGE("%s: Failed to update request metadata with trigger tag %s"
 ", value %d", __FUNCTION__, trigger.getTagName(),
                  trigger.entryValue);
 return res;
 }

        ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
              trigger.getTagName(),
              trigger.entryValue);
 }

    mTriggerMap.clear();

 return count;
}

Project: Android

status_t Camera3Device::internalPauseAndWaitLocked() {
    mRequestThread->setPaused(true);
    mPauseStateNotify = true;

    ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
 if (res != OK) {
        SET_ERR_L("Can't idle device in %f seconds!",
                kShutdownTimeout/1e9);
 }

 return res;
}

Project: Android

status_t Camera3Device::internalResumeLocked() {
 status_t res;

    mRequestThread->setPaused(false);

    res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
 if (res != OK) {
        SET_ERR_L("Can't transition to active in %f seconds!",
                kActiveTimeout/1e9);
 }
    mPauseStateNotify = false;
 return OK;
}

Project: Android

bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
 if (mRepeatingRequests.empty()) {
 return false;
 }
 int32_t requestId = requestIn->mResultExtras.requestId;
 const RequestList &repeatRequests = mRepeatingRequests;
 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
 return (firstRequest->mResultExtras.requestId == requestId);
}

Project: Android

void Camera3Device::notify(const camera3_notify_msg *msg) {
    ATRACE_CALL();
 NotificationListener *listener;
 {
 Mutex::Autolock l(mOutputLock);
        listener = mListener;
 }

 if (msg == NULL) {
        SET_ERR("HAL sent NULL notify message!");
 return;
 }

 switch (msg->type) {
 case CAMERA3_MSG_ERROR: {
            notifyError(msg->message.error, listener);
 break;
 }
 case CAMERA3_MSG_SHUTTER: {
            notifyShutter(msg->message.shutter, listener);
 break;
 }
 default:
            SET_ERR("Unknown notify message from HAL: %d",
                    msg->type);
 }
}

Project: Android

void Camera3Device::notifyError(const camera3_error_msg_t &msg,
 NotificationListener *listener) {

 static const ICameraDeviceCallbacks::CameraErrorCode
            halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
 ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
 ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
 ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
 };

 ICameraDeviceCallbacks::CameraErrorCode errorCode =
 ((msg.error_code >= 0) &&
 (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
            halErrorMap[msg.error_code] :
 ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;

 int streamId = 0;
 if (msg.error_stream != NULL) {
 Camera3Stream *stream =
 Camera3Stream::cast(msg.error_stream);
        streamId = stream->getId();
 }
    ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
            mId, __FUNCTION__, msg.frame_number,
            streamId, msg.error_code);

 CaptureResultExtras resultExtras;
 switch (errorCode) {
 case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
            SET_ERR("Camera HAL reported serious device error");
 break;
 case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
 case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
 case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
 {
 Mutex::Autolock l(mInFlightLock);
 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
 if (idx >= 0) {
 InFlightRequest &r = mInFlightMap.editValueAt(idx);
                    r.requestStatus = msg.error_code;
                    resultExtras = r.resultExtras;
 } else {
                    resultExtras.frameNumber = msg.frame_number;
                    ALOGE("Camera %d: %s: cannot find in-flight request on "
 "frame %" PRId64 " error", mId, __FUNCTION__,
                            resultExtras.frameNumber);
 }
 }
 if (listener != NULL) {
                listener->notifyError(errorCode, resultExtras);
 } else {
                ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
 }
 break;
 default:
            SET_ERR("Unknown error message from HAL: %d", msg.error_code);
 break;
 }
}

Project: Android

void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
 NotificationListener *listener) {
 ssize_t idx;

 {
 Mutex::Autolock l(mInFlightLock);
        idx = mInFlightMap.indexOfKey(msg.frame_number);
 if (idx >= 0) {
 InFlightRequest &r = mInFlightMap.editValueAt(idx);

 {
 Mutex::Autolock l(mOutputLock);
 if (r.hasInputBuffer) {
 if (msg.frame_number < mNextReprocessShutterFrameNumber) {
                        SET_ERR("Shutter notification out-of-order. Expected "
 "notification for frame %d, got frame %d",
                                mNextReprocessShutterFrameNumber, msg.frame_number);
 return;
 }
                    mNextReprocessShutterFrameNumber = msg.frame_number + 1;
 } else {
 if (msg.frame_number < mNextShutterFrameNumber) {
                        SET_ERR("Shutter notification out-of-order. Expected "
 "notification for frame %d, got frame %d",
                                mNextShutterFrameNumber, msg.frame_number);
 return;
 }
                    mNextShutterFrameNumber = msg.frame_number + 1;
 }
 }

            ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
                    mId, __FUNCTION__,
                    msg.frame_number, r.resultExtras.requestId, msg.timestamp);
 if (listener != NULL) {
                listener->notifyShutter(r.resultExtras, msg.timestamp);
 }

            r.shutterTimestamp = msg.timestamp;

            sendCaptureResult(r.pendingMetadata, r.resultExtras,
                r.partialResult.collectedResult, msg.frame_number,
                r.hasInputBuffer, r.aeTriggerCancelOverride);
            returnOutputBuffers(r.pendingOutputBuffers.array(),
                r.pendingOutputBuffers.size(), r.shutterTimestamp);
            r.pendingOutputBuffers.clear();

            removeInFlightRequestIfReadyLocked(idx);
 }
 }
 if (idx < 0) {
        SET_ERR("Shutter notification for non-existent frame number %d",
                msg.frame_number);
 }
}

Project: Android

void Camera3Device::notifyStatus(bool idle) {
 {
 Mutex::Autolock l(mLock);
 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
 return;
 }
        ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
                idle ? "idle" : "active");
        internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);

 if (mPauseStateNotify) return;
 }
 NotificationListener *listener;
 {
 Mutex::Autolock l(mOutputLock);
        listener = mListener;
 }
 if (idle && listener != NULL) {
        listener->notifyIdle();
 }
}

Project: Android

void Camera3Device::overrideResultForPrecaptureCancel(
 CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
 if (aeTriggerCancelOverride.applyAeLock) {
        assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
        result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
 }

 if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
        assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
        result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
 }
}

Project: Android

status_t Camera3Device::prepare(int streamId) {
 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
}

Project: Android

status_t Camera3Device::prepare(int maxCount, int streamId) {
    ATRACE_CALL();
    ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
 Mutex::Autolock il(mInterfaceLock);
 Mutex::Autolock l(mLock);

    sp<Camera3StreamInterface> stream;
 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
 if (outputStreamIdx == NAME_NOT_FOUND) {
        CLOGE("Stream %d does not exist", streamId);
 return BAD_VALUE;
 }

    stream = mOutputStreams.editValueAt(outputStreamIdx);

 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
        CLOGE("Stream %d has already been a request target", streamId);
 return BAD_VALUE;
 }

 if (mRequestThread->isStreamPending(stream)) {
        CLOGE("Stream %d is already a target in a pending request", streamId);
 return BAD_VALUE;
 }

 return mPreparerThread->prepare(maxCount, stream);
}

Project: Android

status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
 status_t res;

 Mutex::Autolock l(mLock);

    res = stream->startPrepare(maxCount);
 if (res == OK) {
        ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
 if (mListener) {
            mListener->notifyPrepared(stream->getId());
 }
 return OK;
 } else if (res != NOT_ENOUGH_DATA) {
 return res;
 }

 if (!mActive) {
 Thread::requestExitAndWait();
        res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
 if (res != OK) {
            ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
 if (mListener) {
                mListener->notifyPrepared(stream->getId());
 }
 return res;
 }
        mCancelNow = false;
        mActive = true;
        ALOGV("%s: Preparer stream started", __FUNCTION__);
 }

    mPendingStreams.push_back(stream);
    ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());

 return OK;
}

Project: Android

status_t Camera3Device::RequestThread::prepareHalRequests() {
    ATRACE_CALL();

 for (auto& nextRequest : mNextRequests) {
        sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
 camera3_capture_request_t* halRequest = &nextRequest.halRequest;
 Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;

        halRequest->frame_number = captureRequest->mResultExtras.frameNumber;

 status_t res = insertTriggers(captureRequest);

 if (res < 0) {
            SET_ERR("RequestThread: Unable to insert triggers "
 "(capture request %d, HAL device: %s (%d)",
                    halRequest->frame_number, strerror(-res), res);
 return INVALID_OPERATION;
 }
 int triggerCount = res;
 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
        mPrevTriggers = triggerCount;

 if (mPrevRequest != captureRequest || triggersMixedIn) {
 /**
             * HAL workaround:
             * Insert a dummy trigger ID if a trigger is set but no trigger ID is
             */
            res = addDummyTriggerIds(captureRequest);
 if (res != OK) {
                SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
 "(capture request %d, HAL device: %s (%d)",
                        halRequest->frame_number, strerror(-res), res);
 return INVALID_OPERATION;
 }

 /**
             * The request should be presorted so accesses in HAL
             *   are O(logn). Sidenote, sorting a sorted metadata is nop.
             */
            captureRequest->mSettings.sort();
            halRequest->settings = captureRequest->mSettings.getAndLock();
            mPrevRequest = captureRequest;
            ALOGVV("%s: Request settings are NEW", __FUNCTION__);

            IF_ALOGV() {
 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
                find_camera_metadata_ro_entry(
                        halRequest->settings,
                        ANDROID_CONTROL_AF_TRIGGER,
 &e
 );
 if (e.count > 0) {
                    ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
                          __FUNCTION__,
                          halRequest->frame_number,
                          e.data.u8[0]);
 }
 }
 } else {
            ALOGVV("%s: Request settings are REUSED",
                   __FUNCTION__);
 }

 uint32_t totalNumBuffers = 0;

 if (captureRequest->mInputStream != NULL) {
            halRequest->input_buffer = &captureRequest->mInputBuffer;
            totalNumBuffers += 1;
 } else {
            halRequest->input_buffer = NULL;
 }

        outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
                captureRequest->mOutputStreams.size());
        halRequest->output_buffers = outputBuffers->array();
 for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
            res = captureRequest->mOutputStreams.editItemAt(i)->
                    getBuffer(&outputBuffers->editItemAt(i));
 if (res != OK) {
                ALOGE("RequestThread: Can't get output buffer, skipping request:"
 " %s (%d)", strerror(-res), res);

 return TIMED_OUT;
 }
            halRequest->num_output_buffers++;
 }
        totalNumBuffers += halRequest->num_output_buffers;

        sp<Camera3Device> parent = mParent.promote();
 if (parent == NULL) {
            CLOGE("RequestThread: Parent is gone");
 return INVALID_OPERATION;
 }
        res = parent->registerInFlight(halRequest->frame_number,
                totalNumBuffers, captureRequest->mResultExtras,
 /*hasInput*/halRequest->input_buffer != NULL,
                captureRequest->mAeTriggerCancelOverride);
        ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
 ", burstId = %" PRId32 ".",
                __FUNCTION__,
                captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
                captureRequest->mResultExtras.burstId);
 if (res != OK) {
            SET_ERR("RequestThread: Unable to register new in-flight request:"
 " %s (%d)", strerror(-res), res);
 return INVALID_OPERATION;
 }
 }

 return OK;
}

Project: Android

void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
    ATRACE_CALL();

 status_t res;

 uint32_t frameNumber = result->frame_number;
 if (result->result == NULL && result->num_output_buffers == 0 &&
            result->input_buffer == NULL) {
        SET_ERR("No result data provided by HAL for frame %d",
                frameNumber);
 return;
 }

 if (!mUsePartialResult &&
            mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
            result->result != NULL &&
            result->partial_result != 1) {
        SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
 " if partial result is not supported",
                frameNumber, result->partial_result);
 return;
 }

 bool isPartialResult = false;
 CameraMetadata collectedPartialResult;
 CaptureResultExtras resultExtras;
 bool hasInputBufferInRequest = false;

 nsecs_t shutterTimestamp = 0;

 {
 Mutex::Autolock l(mInFlightLock);
 ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
 if (idx == NAME_NOT_FOUND) {
            SET_ERR("Unknown frame number for capture result: %d",
                    frameNumber);
 return;
 }
 InFlightRequest &request = mInFlightMap.editValueAt(idx);
        ALOGVV("%s: got InFlightRequest requestId = %" PRId32
 ", frameNumber = %" PRId64 ", burstId = %" PRId32
 ", partialResultCount = %d",
                __FUNCTION__, request.resultExtras.requestId,
                request.resultExtras.frameNumber, request.resultExtras.burstId,
                result->partial_result);
 if (result->partial_result != 0)
            request.resultExtras.partialResultCount = result->partial_result;

 if (mUsePartialResult && result->result != NULL) {
 if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
                    SET_ERR("Result is malformed for frame %d: partial_result %u must be  in"
 " the range of [1, %d] when metadata is included in the result",
                            frameNumber, result->partial_result, mNumPartialResults);
 return;
 }
                isPartialResult = (result->partial_result < mNumPartialResults);
 if (isPartialResult) {
                    request.partialResult.collectedResult.append(result->result);
 }
 } else {
 camera_metadata_ro_entry_t partialResultEntry;
                res = find_camera_metadata_ro_entry(result->result,
                        ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
 if (res != NAME_NOT_FOUND &&
                        partialResultEntry.count > 0 &&
                        partialResultEntry.data.u8[0] ==
                        ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
                    isPartialResult = true;
                    request.partialResult.collectedResult.append(
                        result->result);
                    request.partialResult.collectedResult.erase(
                        ANDROID_QUIRKS_PARTIAL_RESULT);
 }
 }

 if (isPartialResult) {
 if (!request.partialResult.haveSent3A) {
                    request.partialResult.haveSent3A =
                            processPartial3AResult(frameNumber,
                                    request.partialResult.collectedResult,
                                    request.resultExtras);
 }
 }
 }

        shutterTimestamp = request.shutterTimestamp;
        hasInputBufferInRequest = request.hasInputBuffer;

 if (result->result != NULL && !isPartialResult) {
 if (request.haveResultMetadata) {
                SET_ERR("Called multiple times with metadata for frame %d",
                        frameNumber);
 return;
 }
 if (mUsePartialResult &&
 !request.partialResult.collectedResult.isEmpty()) {
                collectedPartialResult.acquire(
                    request.partialResult.collectedResult);
 }
            request.haveResultMetadata = true;
 }

 uint32_t numBuffersReturned = result->num_output_buffers;
 if (result->input_buffer != NULL) {
 if (hasInputBufferInRequest) {
                numBuffersReturned += 1;
 } else {
                ALOGW("%s: Input buffer should be NULL if there is no input"
 " buffer sent in the request",
                        __FUNCTION__);
 }
 }
        request.numBuffersLeft -= numBuffersReturned;
 if (request.numBuffersLeft < 0) {
            SET_ERR("Too many buffers returned for frame %d",
                    frameNumber);
 return;
 }

 camera_metadata_ro_entry_t entry;
        res = find_camera_metadata_ro_entry(result->result,
                ANDROID_SENSOR_TIMESTAMP, &entry);
 if (res == OK && entry.count == 1) {
            request.sensorTimestamp = entry.data.i64[0];
 }

 if (shutterTimestamp == 0) {
            request.pendingOutputBuffers.appendArray(result->output_buffers,
                result->num_output_buffers);
 } else {
            returnOutputBuffers(result->output_buffers,
                result->num_output_buffers, shutterTimestamp);
 }

 if (result->result != NULL && !isPartialResult) {
 if (shutterTimestamp == 0) {
                request.pendingMetadata = result->result;
                request.partialResult.collectedResult = collectedPartialResult;
 } else {
 CameraMetadata metadata;
                metadata = result->result;
                sendCaptureResult(metadata, request.resultExtras,
                    collectedPartialResult, frameNumber, hasInputBufferInRequest,
                    request.aeTriggerCancelOverride);
 }
 }

        removeInFlightRequestIfReadyLocked(idx);
 } // scope for mInFlightLock

 if (result->input_buffer != NULL) {
 if (hasInputBufferInRequest) {
 Camera3Stream *stream =
 Camera3Stream::cast(result->input_buffer->stream);
            res = stream->returnInputBuffer(*(result->input_buffer));
 if (res != OK) {
                ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
 "  its stream:%s (%d)",  __FUNCTION__,
                      frameNumber, strerror(-res), res);
 }
 } else {
            ALOGW("%s: Input buffer should be NULL if there is no input"
 " buffer sent in the request, skipping input buffer return.",
                    __FUNCTION__);
 }
 }
}

Project: Android

bool Camera3Device::processPartial3AResult(
 uint32_t frameNumber,
 const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {


 bool gotAllStates = true;

 uint8_t afMode;
 uint8_t awbMode;
 uint8_t aeState;
 uint8_t afState;
 uint8_t awbState;

    gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
 &afMode, frameNumber);

    gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
 &awbMode, frameNumber);

    gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
 &aeState, frameNumber);

    gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
 &afState, frameNumber);

    gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
 &awbState, frameNumber);

 if (!gotAllStates) return false;

    ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
 "AF state %d, AE state %d, AWB state %d, "
 "AF trigger %d, AE precapture trigger %d",
        __FUNCTION__, mId, frameNumber, resultExtras.requestId,
        afMode, awbMode,
        afState, aeState, awbState,
        resultExtras.afTriggerId, resultExtras.precaptureTriggerId);


 const size_t kMinimal3AResultEntries = 10;

 Mutex::Autolock l(mOutputLock);

 CaptureResult captureResult;
    captureResult.mResultExtras = resultExtras;
    captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
 CaptureResult& min3AResult =
 *mResultQueue.insert(mResultQueue.end(), captureResult);

 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
 reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
 return false;
 }

 int32_t requestId = resultExtras.requestId;
 if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
 &requestId, frameNumber)) {
 return false;
 }

 if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
 static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
 if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
 &partialResult, frameNumber)) {
 return false;
 }
 }

 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
 &afMode, frameNumber)) {
 return false;
 }

 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
 &awbMode, frameNumber)) {
 return false;
 }

 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
 &aeState, frameNumber)) {
 return false;
 }

 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
 &afState, frameNumber)) {
 return false;
 }

 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
 &awbState, frameNumber)) {
 return false;
 }

 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
 &resultExtras.afTriggerId, frameNumber)) {
 return false;
 }

 if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
 &resultExtras.precaptureTriggerId, frameNumber)) {
 return false;
 }

    mResultSignal.signal();

 return true;
}

Project: Android

status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
 buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
    ATRACE_CALL();
 (void)reprocessStreamId; (void)buffer; (void)listener;

    CLOGE("Unimplemented");
 return INVALID_OPERATION;
}

Project: Android

status_t Camera3Device::RequestThread::queueRequestList(
 List<sp<CaptureRequest> > &requests,
 /*out*/
 int64_t *lastFrameNumber) {
 Mutex::Autolock l(mRequestLock);
 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
 ++it) {
        mRequestQueue.push_back(*it);
 }

 if (lastFrameNumber != NULL) {
 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
        ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
              __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
 *lastFrameNumber);
 }

    unpauseForNewRequests();

 return OK;
}

Project: Android

status_t Camera3Device::RequestThread::queueTrigger(
 RequestTrigger trigger[],
 size_t count) {

 Mutex::Autolock l(mTriggerMutex);
 status_t ret;

 for (size_t i = 0; i < count; ++i) {
        ret = queueTriggerLocked(trigger[i]);

 if (ret != OK) {
 return ret;
 }
 }

 return OK;
}

Project: Android

status_t Camera3Device::RequestThread::queueTriggerLocked(
 RequestTrigger trigger) {

 uint32_t tag = trigger.metadataTag;
 ssize_t index = mTriggerMap.indexOfKey(tag);

 switch (trigger.getTagType()) {
 case TYPE_BYTE:
 case TYPE_INT32:
 break;
 default:
            ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
                    trigger.getTagType());
 return INVALID_OPERATION;
 }

 /**
     * Collect only the latest trigger, since we only have 1 field
     * in the request settings per trigger tag, and can't send more than 1
     * trigger per request.
     */
 if (index != NAME_NOT_FOUND) {
        mTriggerMap.editValueAt(index) = trigger;
 } else {
        mTriggerMap.add(tag, trigger);
 }

 return OK;
}

Project: Android

status_t Camera3Device::registerInFlight(uint32_t frameNumber,
 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
    ATRACE_CALL();
 Mutex::Autolock l(mInFlightLock);

 ssize_t res;
    res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
            aeTriggerCancelOverride));
 if (res < 0) return res;

 return OK;
}

Project: Android

status_t Camera3Device::RequestThread::removeTriggers(
 const sp<CaptureRequest> &request) {
 Mutex::Autolock al(mTriggerMutex);

 CameraMetadata &metadata = request->mSettings;

 /**
     * Replace all old entries with their old values.
     */
 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);

 status_t res;

 uint32_t tag = trigger.metadataTag;
 switch (trigger.getTagType()) {
 case TYPE_BYTE: {
 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
                res = metadata.update(tag,
 &entryValue,
 /*count*/1);
 break;
 }
 case TYPE_INT32:
                res = metadata.update(tag,
 &trigger.entryValue,
 /*count*/1);
 break;
 default:
                ALOGE("%s: Type not supported: 0x%x",
                      __FUNCTION__,
                      trigger.getTagType());
 return INVALID_OPERATION;
 }

 if (res != OK) {
            ALOGE("%s: Failed to restore request metadata with trigger tag %s"
 ", trigger value %d", __FUNCTION__,
                  trigger.getTagName(), trigger.entryValue);
 return res;
 }
 }
    mTriggerReplacedMap.clear();

 /**
     * Remove all new entries.
     */
 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
 status_t res = metadata.erase(trigger.metadataTag);

 if (res != OK) {
            ALOGE("%s: Failed to erase metadata with trigger tag %s"
 ", trigger value %d", __FUNCTION__,
                  trigger.getTagName(), trigger.entryValue);
 return res;
 }
 }
    mTriggerRemovedMap.clear();

 return OK;
}

Project: Android

void Camera3Device::RequestThread::requestExit() {
 Thread::requestExit();
    mDoPauseSignal.signal();
    mRequestSignal.signal();
}

Project: Android

void Camera3Device::returnOutputBuffers(
 const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
 nsecs_t timestamp) {
 for (size_t i = 0; i < numBuffers; i++)
 {
 Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
 status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
 if (res != OK) {
            ALOGE("Can't return buffer to its stream: %s (%d)",
                strerror(-res), res);
 }
 }
}

Project: Android

void Camera3Device::sNotify(const camera3_callback_ops *cb,
 const camera3_notify_msg *msg) {
 Camera3Device *d =
 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
    d->notify(msg);
}

Project: Android

void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
 const camera3_capture_result *result) {
 Camera3Device *d =
 const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));

    d->processCaptureResult(result);
}

Project: Android

void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
 CaptureResultExtras &resultExtras,
 CameraMetadata &collectedPartialResult,
 uint32_t frameNumber,
 bool reprocess,
 const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
 if (pendingMetadata.isEmpty())
 return;

 Mutex::Autolock l(mOutputLock);

 if (reprocess) {
 if (frameNumber < mNextReprocessResultFrameNumber) {
            SET_ERR("Out-of-order reprocess capture result metadata submitted! "
 "(got frame number %d, expecting %d)",
                frameNumber, mNextReprocessResultFrameNumber);
 return;
 }
        mNextReprocessResultFrameNumber = frameNumber + 1;
 } else {
 if (frameNumber < mNextResultFrameNumber) {
            SET_ERR("Out-of-order capture result metadata submitted! "
 "(got frame number %d, expecting %d)",
                    frameNumber, mNextResultFrameNumber);
 return;
 }
        mNextResultFrameNumber = frameNumber + 1;
 }

 CaptureResult captureResult;
    captureResult.mResultExtras = resultExtras;
    captureResult.mMetadata = pendingMetadata;

 if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
 (int32_t*)&frameNumber, 1) != OK) {
        SET_ERR("Failed to set frame# in metadata (%d)",
                frameNumber);
 return;
 } else {
        ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
                __FUNCTION__, mId, frameNumber);
 }

 if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
        captureResult.mMetadata.append(collectedPartialResult);
 }

    captureResult.mMetadata.sort();

    camera_metadata_entry entry =
            captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
 if (entry.count == 0) {
        SET_ERR("No timestamp provided by HAL for frame %d!",
                frameNumber);
 return;
 }

    overrideResultForPrecaptureCancel(&captureResult.mMetadata, aeTriggerCancelOverride);

 List<CaptureResult>::iterator queuedResult =
            mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
    ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
 ", burstId = %" PRId32, __FUNCTION__,
           queuedResult->mResultExtras.requestId,
           queuedResult->mResultExtras.frameNumber,
           queuedResult->mResultExtras.burstId);

    mResultSignal.signal();
}

Project: Android

void Camera3Device::setErrorState(const char *fmt, ...) {
 Mutex::Autolock l(mLock);
    va_list args;
    va_start(args, fmt);

    setErrorStateLockedV(fmt, args);

    va_end(args);
}

Project: Android

void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
    sp<Camera3Device> parent = mParent.promote();
 if (parent != NULL) {
        va_list args;
        va_start(args, fmt);

        parent->setErrorStateV(fmt, args);

        va_end(args);
 }
}

Project: Android

void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
    va_list args;
    va_start(args, fmt);

    setErrorStateLockedV(fmt, args);

    va_end(args);
}

Project: Android

void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
 String8 errorCause = String8::formatV(fmt, args);
    ALOGE("Camera %d: %s", mId, errorCause.string());

 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;

    mErrorCause = errorCause;

    mRequestThread->setPaused(true);
    internalUpdateStatusLocked(STATUS_ERROR);

 if (mListener != NULL) {
        mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
 CaptureResultExtras());
 }

 CameraTraces::saveTrace();
}

Project: Android

void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
 Mutex::Autolock l(mLock);
    setErrorStateLockedV(fmt, args);
}

Project: Android

void Camera3Device::PreparerThread::setNotificationListener(NotificationListener *listener) {
 Mutex::Autolock l(mLock);
    mListener = listener;
}

Project: Android

status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
    ATRACE_CALL();
 Mutex::Autolock l(mOutputLock);

 if (listener != NULL && mListener != NULL) {
        ALOGW("%s: Replacing old callback listener", __FUNCTION__);
 }
    mListener = listener;
    mRequestThread->setNotificationListener(listener);
    mPreparerThread->setNotificationListener(listener);

 return OK;
}

Project: Android

void Camera3Device::RequestThread::setPaused(bool paused) {
 Mutex::Autolock l(mPauseLock);
    mDoPause = paused;
    mDoPauseSignal.signal();
}

Project: Android

status_t Camera3Device::RequestThread::setRepeatingRequests(
 const RequestList &requests,
 /*out*/
 int64_t *lastFrameNumber) {
 Mutex::Autolock l(mRequestLock);
 if (lastFrameNumber != NULL) {
 *lastFrameNumber = mRepeatingLastFrameNumber;
 }
    mRepeatingRequests.clear();
    mRepeatingRequests.insert(mRepeatingRequests.begin(),
            requests.begin(), requests.end());

    unpauseForNewRequests();

    mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
 return OK;
}

Project: Android

status_t Camera3Device::setStreamTransform(int id,
 int transform) {
    ATRACE_CALL();
 Mutex::Autolock il(mInterfaceLock);
 Mutex::Autolock l(mLock);

 switch (mStatus) {
 case STATUS_ERROR:
            CLOGE("Device has encountered a serious error");
 return INVALID_OPERATION;
 case STATUS_UNINITIALIZED:
            CLOGE("Device not initialized");
 return INVALID_OPERATION;
 case STATUS_UNCONFIGURED:
 case STATUS_CONFIGURED:
 case STATUS_ACTIVE:
 break;
 default:
            SET_ERR_L("Unexpected status: %d", mStatus);
 return INVALID_OPERATION;
 }

 ssize_t idx = mOutputStreams.indexOfKey(id);
 if (idx == NAME_NOT_FOUND) {
        CLOGE("Stream %d does not exist",
                id);
 return BAD_VALUE;
 }

 return mOutputStreams.editValueAt(idx)->setTransform(transform);
}

Project: Android

status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
 int64_t* /*lastFrameNumber*/) {
    ATRACE_CALL();

 List<const CameraMetadata> requests;
    requests.push_back(request);
 return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
}

Project: Android

status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
 int64_t *lastFrameNumber) {
    ATRACE_CALL();

 return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
}

Project: Android

sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
 const CameraMetadata &request) {
 status_t res;

 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
        res = configureStreamsLocked();
 if (res == BAD_VALUE && mStatus == STATUS_UNCONFIGURED) {
            CLOGE("No streams configured");
 return NULL;
 }
 if (res != OK) {
            SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
 return NULL;
 }
 if (mStatus == STATUS_UNCONFIGURED) {
            CLOGE("No streams configured");
 return NULL;
 }
 }

    sp<CaptureRequest> newRequest = createCaptureRequest(request);
 return newRequest;
}

Project: Android

status_t Camera3Device::submitRequestsHelper(
 const List<const CameraMetadata> &requests, bool repeating,
 /*out*/
 int64_t *lastFrameNumber) {
    ATRACE_CALL();
 Mutex::Autolock il(mInterfaceLock);
 Mutex::Autolock l(mLock);

 status_t res = checkStatusOkToCaptureLocked();
 if (res != OK) {
 return res;
 }

 RequestList requestList;

    res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
 if (res != OK) {
 return res;
 }

 if (repeating) {
        res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
 } else {
        res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
 }

 if (res == OK) {
        waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
 if (res != OK) {
            SET_ERR_L("Can't transition to active in %f seconds!",
                    kActiveTimeout/1e9);
 }
        ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
 (*(requestList.begin()))->mResultExtras.requestId);
 } else {
        CLOGE("Cannot queue request. Impossible.");
 return BAD_VALUE;
 }

 return res;
}

Project: Android

status_t Camera3Device::tearDown(int streamId) {
    ATRACE_CALL();
    ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
 Mutex::Autolock il(mInterfaceLock);
 Mutex::Autolock l(mLock);

 if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
        ALOGE("%s: Unable to tear down streams on device HAL v%x",
                __FUNCTION__, mHal3Device->common.version);
 return NO_INIT;
 }

    sp<Camera3StreamInterface> stream;
 ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
 if (outputStreamIdx == NAME_NOT_FOUND) {
        CLOGE("Stream %d does not exist", streamId);
 return BAD_VALUE;
 }

    stream = mOutputStreams.editValueAt(outputStreamIdx);

 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
        CLOGE("Stream %d is a target of a in-progress request", streamId);
 return BAD_VALUE;
 }

 return stream->tearDown();
}

Project: Android

bool Camera3Device::PreparerThread::threadLoop() {
 status_t res;
 {
 Mutex::Autolock l(mLock);
 if (mCurrentStream == nullptr) {
 if (mPendingStreams.empty()) {
                ALOGV("%s: Preparer stream out of work", __FUNCTION__);
                mActive = false;
 return false;
 }

 auto it = mPendingStreams.begin();
            mCurrentStream = *it;
            mPendingStreams.erase(it);
            ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
            ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
 } else if (mCancelNow) {
            mCurrentStream->cancelPrepare();
            ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
            ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
            mCurrentStream.clear();
            mCancelNow = false;
 return true;
 }
 }

    res = mCurrentStream->prepareNextBuffer();
 if (res == NOT_ENOUGH_DATA) return true;
 if (res != OK) {
        ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
                mCurrentStream->getId(), res, strerror(-res));
        mCurrentStream->cancelPrepare();
 }

 Mutex::Autolock l(mLock);
 if (mListener) {
        ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
                mCurrentStream->getId());
        mListener->notifyPrepared(mCurrentStream->getId());
 }

    ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
    mCurrentStream.clear();

 return true;
}

Project: Android

status_t Camera3Device::triggerAutofocus(uint32_t id) {
    ATRACE_CALL();
 Mutex::Autolock il(mInterfaceLock);

    ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
 RequestTrigger trigger[] = {
 {
            ANDROID_CONTROL_AF_TRIGGER,
            ANDROID_CONTROL_AF_TRIGGER_START
 },
 {
            ANDROID_CONTROL_AF_TRIGGER_ID,
 static_cast<int32_t>(id)
 }
 };

 return mRequestThread->queueTrigger(trigger,
 sizeof(trigger)/sizeof(trigger[0]));
}

Project: Android

status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
    ATRACE_CALL();
 Mutex::Autolock il(mInterfaceLock);

    ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
 RequestTrigger trigger[] = {
 {
            ANDROID_CONTROL_AF_TRIGGER,
            ANDROID_CONTROL_AF_TRIGGER_CANCEL
 },
 {
            ANDROID_CONTROL_AF_TRIGGER_ID,
 static_cast<int32_t>(id)
 }
 };

 return mRequestThread->queueTrigger(trigger,
 sizeof(trigger)/sizeof(trigger[0]));
}

Project: Android

status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
    ATRACE_CALL();
 Mutex::Autolock il(mInterfaceLock);

    ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
 RequestTrigger trigger[] = {
 {
            ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
            ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
 },
 {
            ANDROID_CONTROL_AE_PRECAPTURE_ID,
 static_cast<int32_t>(id)
 }
 };

 return mRequestThread->queueTrigger(trigger,
 sizeof(trigger)/sizeof(trigger[0]));
}

Project: Android

bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
 bool gotLock = false;
 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
 if (lock.tryLock() == NO_ERROR) {
            gotLock = true;
 break;
 } else {
            usleep(kDumpSleepDuration);
 }
 }
 return gotLock;
}

Project: Android

void Camera3Device::RequestThread::unpauseForNewRequests() {
    mRequestSignal.signal();
 Mutex::Autolock p(mPauseLock);
 if (!mDoPause) {
        ALOGV("%s: RequestThread: Going active", __FUNCTION__);
 if (mPaused) {
            sp<StatusTracker> statusTracker = mStatusTracker.promote();
 if (statusTracker != 0) {
                statusTracker->markComponentActive(mStatusId);
 }
 }
        mPaused = false;
 }
}

Project: Android

status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
 status_t res;
 Mutex::Autolock l(mOutputLock);

 while (mResultQueue.empty()) {
        res = mResultSignal.waitRelative(mOutputLock, timeout);
 if (res == TIMED_OUT) {
 return res;
 } else if (res != OK) {
            ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
                    __FUNCTION__, mId, timeout, strerror(-res), res);
 return res;
 }
 }
 return OK;
}

Project: Android

void Camera3Device::RequestThread::waitForNextRequestBatch() {
 Mutex::Autolock l(mRequestLock);

    assert(mNextRequests.empty());

 NextRequest nextRequest;
    nextRequest.captureRequest = waitForNextRequestLocked();
 if (nextRequest.captureRequest == nullptr) {
 return;
 }

    nextRequest.halRequest = camera3_capture_request_t();
    nextRequest.submitted = false;
    mNextRequests.add(nextRequest);

 const size_t batchSize = nextRequest.captureRequest->mBatchSize;

 for (size_t i = 1; i < batchSize; i++) {
 NextRequest additionalRequest;
        additionalRequest.captureRequest = waitForNextRequestLocked();
 if (additionalRequest.captureRequest == nullptr) {
 break;
 }

        additionalRequest.halRequest = camera3_capture_request_t();
        additionalRequest.submitted = false;
        mNextRequests.add(additionalRequest);
 }

 if (mNextRequests.size() < batchSize) {
        ALOGE("RequestThread: only get %d out of %d requests. Skipping requests.",
                mNextRequests.size(), batchSize);
        cleanUpFailedRequests(/*sendRequestError*/true);
 }

 return;
}

Project: Android

 Camera3Device::RequestThread::waitForNextRequestLocked() {
 status_t res;
    sp<CaptureRequest> nextRequest;

 while (mRequestQueue.empty()) {
 if (!mRepeatingRequests.empty()) {
 const RequestList &requests = mRepeatingRequests;
 RequestList::const_iterator firstRequest =
                    requests.begin();
            nextRequest = *firstRequest;
            mRequestQueue.insert(mRequestQueue.end(),
 ++firstRequest,
                    requests.end());

            mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;

 break;
 }

        res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);

 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
                exitPending()) {
 Mutex::Autolock pl(mPauseLock);
 if (mPaused == false) {
                ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
                mPaused = true;
                sp<StatusTracker> statusTracker = mStatusTracker.promote();
 if (statusTracker != 0) {
                    statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
 }
 }
 return NULL;
 }
 }

 if (nextRequest == NULL) {
 RequestList::iterator firstRequest =
                mRequestQueue.begin();
        nextRequest = *firstRequest;
        mRequestQueue.erase(firstRequest);
 }

 Mutex::Autolock pl(mPauseLock);
 if (mPaused) {
        ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
        sp<StatusTracker> statusTracker = mStatusTracker.promote();
 if (statusTracker != 0) {
            statusTracker->markComponentActive(mStatusId);
 }
 }
    mPaused = false;

 if (mReconfigured) {
        mPrevRequest.clear();
        mReconfigured = false;
 }

 if (nextRequest != NULL) {
        nextRequest->mResultExtras.frameNumber = mFrameNumber++;
        nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
        nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;

 if (nextRequest->mInputStream != NULL) {
            res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
 if (res != OK) {
                ALOGE("%s: Can't get input buffer, skipping request:"
 " %s (%d)", __FUNCTION__, strerror(-res), res);
 if (mListener != NULL) {
                    mListener->notifyError(
 ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
                            nextRequest->mResultExtras);
 }
 return NULL;
 }
 }
 }

    handleAePrecaptureCancelRequest(nextRequest);

 return nextRequest;
}

Project: Android

bool Camera3Device::RequestThread::waitIfPaused() {
 status_t res;
 Mutex::Autolock l(mPauseLock);
 while (mDoPause) {
 if (mPaused == false) {
            mPaused = true;
            ALOGV("%s: RequestThread: Paused", __FUNCTION__);
            sp<StatusTracker> statusTracker = mStatusTracker.promote();
 if (statusTracker != 0) {
                statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
 }
 }

        res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
 if (res == TIMED_OUT || exitPending()) {
 return true;
 }
 }
 return false;
}

Project: Android

status_t Camera3Device::waitUntilDrained() {
    ATRACE_CALL();
 Mutex::Autolock il(mInterfaceLock);
 Mutex::Autolock l(mLock);

 return waitUntilDrainedLocked();
}

Project: Android

status_t Camera3Device::waitUntilDrainedLocked() {
 switch (mStatus) {
 case STATUS_UNINITIALIZED:
 case STATUS_UNCONFIGURED:
            ALOGV("%s: Already idle", __FUNCTION__);
 return OK;
 case STATUS_CONFIGURED:
 case STATUS_ERROR:
 case STATUS_ACTIVE:
 break;
 default:
            SET_ERR_L("Unexpected status: %d",mStatus);
 return INVALID_OPERATION;
 }

    ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
 status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
 if (res != OK) {
        SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
                res);
 }
 return res;
}

Project: Android

status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
 int32_t requestId, nsecs_t timeout) {
 Mutex::Autolock l(mLatestRequestMutex);
 status_t res;
 while (mLatestRequestId != requestId) {
 nsecs_t startTime = systemTime();

        res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
 if (res != OK) return res;

        timeout -= (systemTime() - startTime);
 }

 return OK;
}

Project: Android

status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
    ATRACE_CALL();
 Mutex::Autolock il(mInterfaceLock);

 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
}

Project: Android

Camera3Device::~Camera3Device()
{
    ATRACE_CALL();
    ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
    disconnect();
}

Project: Android

Camera3Device::PreparerThread::~PreparerThread() {
 Thread::requestExitAndWait();
 if (mCurrentStream != nullptr) {
        mCurrentStream->cancelPrepare();
        ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
        mCurrentStream.clear();
 }
    clear();
}