Home | History | Annotate | Download | only in api2
      1 /*
      2  * Copyright (C) 2013-2018 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #define LOG_TAG "CameraDeviceClient"
     18 #define ATRACE_TAG ATRACE_TAG_CAMERA
     19 //#define LOG_NDEBUG 0
     20 
     21 #include <cutils/properties.h>
     22 #include <utils/Log.h>
     23 #include <utils/Trace.h>
     24 #include <gui/Surface.h>
     25 #include <camera/camera2/CaptureRequest.h>
     26 #include <camera/CameraUtils.h>
     27 
     28 #include "common/CameraDeviceBase.h"
     29 #include "api2/CameraDeviceClient.h"
     30 
     31 #include <camera_metadata_hidden.h>
     32 
     33 // Convenience methods for constructing binder::Status objects for error returns
     34 
     35 #define STATUS_ERROR(errorCode, errorString) \
     36     binder::Status::fromServiceSpecificError(errorCode, \
     37             String8::format("%s:%d: %s", __FUNCTION__, __LINE__, errorString))
     38 
     39 #define STATUS_ERROR_FMT(errorCode, errorString, ...) \
     40     binder::Status::fromServiceSpecificError(errorCode, \
     41             String8::format("%s:%d: " errorString, __FUNCTION__, __LINE__, \
     42                     __VA_ARGS__))
     43 
     44 namespace android {
     45 using namespace camera2;
     46 
     47 CameraDeviceClientBase::CameraDeviceClientBase(
     48         const sp<CameraService>& cameraService,
     49         const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
     50         const String16& clientPackageName,
     51         const String8& cameraId,
     52         int api1CameraId,
     53         int cameraFacing,
     54         int clientPid,
     55         uid_t clientUid,
     56         int servicePid) :
     57     BasicClient(cameraService,
     58             IInterface::asBinder(remoteCallback),
     59             clientPackageName,
     60             cameraId,
     61             cameraFacing,
     62             clientPid,
     63             clientUid,
     64             servicePid),
     65     mRemoteCallback(remoteCallback) {
     66     // We don't need it for API2 clients, but Camera2ClientBase requires it.
     67     (void) api1CameraId;
     68 }
     69 
     70 // Interface used by CameraService
     71 
     72 CameraDeviceClient::CameraDeviceClient(const sp<CameraService>& cameraService,
     73         const sp<hardware::camera2::ICameraDeviceCallbacks>& remoteCallback,
     74         const String16& clientPackageName,
     75         const String8& cameraId,
     76         int cameraFacing,
     77         int clientPid,
     78         uid_t clientUid,
     79         int servicePid) :
     80     Camera2ClientBase(cameraService, remoteCallback, clientPackageName,
     81                 cameraId, /*API1 camera ID*/ -1,
     82                 cameraFacing, clientPid, clientUid, servicePid),
     83     mInputStream(),
     84     mStreamingRequestId(REQUEST_ID_NONE),
     85     mRequestIdCounter(0) {
     86 
     87     ATRACE_CALL();
     88     ALOGI("CameraDeviceClient %s: Opened", cameraId.string());
     89 }
     90 
     91 status_t CameraDeviceClient::initialize(sp<CameraProviderManager> manager,
     92         const String8& monitorTags) {
     93     return initializeImpl(manager, monitorTags);
     94 }
     95 
     96 template<typename TProviderPtr>
     97 status_t CameraDeviceClient::initializeImpl(TProviderPtr providerPtr, const String8& monitorTags) {
     98     ATRACE_CALL();
     99     status_t res;
    100 
    101     res = Camera2ClientBase::initialize(providerPtr, monitorTags);
    102     if (res != OK) {
    103         return res;
    104     }
    105 
    106     String8 threadName;
    107     mFrameProcessor = new FrameProcessorBase(mDevice);
    108     threadName = String8::format("CDU-%s-FrameProc", mCameraIdStr.string());
    109     mFrameProcessor->run(threadName.string());
    110 
    111     mFrameProcessor->registerListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
    112                                       FRAME_PROCESSOR_LISTENER_MAX_ID,
    113                                       /*listener*/this,
    114                                       /*sendPartials*/true);
    115 
    116     auto deviceInfo = mDevice->info();
    117     camera_metadata_entry_t physicalKeysEntry = deviceInfo.find(
    118             ANDROID_REQUEST_AVAILABLE_PHYSICAL_CAMERA_REQUEST_KEYS);
    119     if (physicalKeysEntry.count > 0) {
    120         mSupportedPhysicalRequestKeys.insert(mSupportedPhysicalRequestKeys.begin(),
    121                 physicalKeysEntry.data.i32,
    122                 physicalKeysEntry.data.i32 + physicalKeysEntry.count);
    123     }
    124 
    125     return OK;
    126 }
    127 
    128 CameraDeviceClient::~CameraDeviceClient() {
    129 }
    130 
    131 binder::Status CameraDeviceClient::submitRequest(
    132         const hardware::camera2::CaptureRequest& request,
    133         bool streaming,
    134         /*out*/
    135         hardware::camera2::utils::SubmitInfo *submitInfo) {
    136     std::vector<hardware::camera2::CaptureRequest> requestList = { request };
    137     return submitRequestList(requestList, streaming, submitInfo);
    138 }
    139 
    140 binder::Status CameraDeviceClient::insertGbpLocked(const sp<IGraphicBufferProducer>& gbp,
    141         SurfaceMap* outSurfaceMap, Vector<int32_t>* outputStreamIds, int32_t *currentStreamId) {
    142     int idx = mStreamMap.indexOfKey(IInterface::asBinder(gbp));
    143 
    144     // Trying to submit request with surface that wasn't created
    145     if (idx == NAME_NOT_FOUND) {
    146         ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
    147                 " we have not called createStream on",
    148                 __FUNCTION__, mCameraIdStr.string());
    149         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
    150                 "Request targets Surface that is not part of current capture session");
    151     }
    152 
    153     const StreamSurfaceId& streamSurfaceId = mStreamMap.valueAt(idx);
    154     if (outSurfaceMap->find(streamSurfaceId.streamId()) == outSurfaceMap->end()) {
    155         (*outSurfaceMap)[streamSurfaceId.streamId()] = std::vector<size_t>();
    156         outputStreamIds->push_back(streamSurfaceId.streamId());
    157     }
    158     (*outSurfaceMap)[streamSurfaceId.streamId()].push_back(streamSurfaceId.surfaceId());
    159 
    160     ALOGV("%s: Camera %s: Appending output stream %d surface %d to request",
    161             __FUNCTION__, mCameraIdStr.string(), streamSurfaceId.streamId(),
    162             streamSurfaceId.surfaceId());
    163 
    164     if (currentStreamId != nullptr) {
    165         *currentStreamId = streamSurfaceId.streamId();
    166     }
    167 
    168     return binder::Status::ok();
    169 }
    170 
    171 binder::Status CameraDeviceClient::submitRequestList(
    172         const std::vector<hardware::camera2::CaptureRequest>& requests,
    173         bool streaming,
    174         /*out*/
    175         hardware::camera2::utils::SubmitInfo *submitInfo) {
    176     ATRACE_CALL();
    177     ALOGV("%s-start of function. Request list size %zu", __FUNCTION__, requests.size());
    178 
    179     binder::Status res = binder::Status::ok();
    180     status_t err;
    181     if ( !(res = checkPidStatus(__FUNCTION__) ).isOk()) {
    182         return res;
    183     }
    184 
    185     Mutex::Autolock icl(mBinderSerializationLock);
    186 
    187     if (!mDevice.get()) {
    188         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
    189     }
    190 
    191     if (requests.empty()) {
    192         ALOGE("%s: Camera %s: Sent null request. Rejecting request.",
    193               __FUNCTION__, mCameraIdStr.string());
    194         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Empty request list");
    195     }
    196 
    197     List<const CameraDeviceBase::PhysicalCameraSettingsList> metadataRequestList;
    198     std::list<const SurfaceMap> surfaceMapList;
    199     submitInfo->mRequestId = mRequestIdCounter;
    200     uint32_t loopCounter = 0;
    201 
    202     for (auto&& request: requests) {
    203         if (request.mIsReprocess) {
    204             if (!mInputStream.configured) {
    205                 ALOGE("%s: Camera %s: no input stream is configured.", __FUNCTION__,
    206                         mCameraIdStr.string());
    207                 return STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
    208                         "No input configured for camera %s but request is for reprocessing",
    209                         mCameraIdStr.string());
    210             } else if (streaming) {
    211                 ALOGE("%s: Camera %s: streaming reprocess requests not supported.", __FUNCTION__,
    212                         mCameraIdStr.string());
    213                 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
    214                         "Repeating reprocess requests not supported");
    215             } else if (request.mPhysicalCameraSettings.size() > 1) {
    216                 ALOGE("%s: Camera %s: reprocess requests not supported for "
    217                         "multiple physical cameras.", __FUNCTION__,
    218                         mCameraIdStr.string());
    219                 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
    220                         "Reprocess requests not supported for multiple cameras");
    221             }
    222         }
    223 
    224         if (request.mPhysicalCameraSettings.empty()) {
    225             ALOGE("%s: Camera %s: request doesn't contain any settings.", __FUNCTION__,
    226                     mCameraIdStr.string());
    227             return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
    228                     "Request doesn't contain any settings");
    229         }
    230 
    231         //The first capture settings should always match the logical camera id
    232         String8 logicalId(request.mPhysicalCameraSettings.begin()->id.c_str());
    233         if (mDevice->getId() != logicalId) {
    234             ALOGE("%s: Camera %s: Invalid camera request settings.", __FUNCTION__,
    235                     mCameraIdStr.string());
    236             return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
    237                     "Invalid camera request settings");
    238         }
    239 
    240         if (request.mSurfaceList.isEmpty() && request.mStreamIdxList.size() == 0) {
    241             ALOGE("%s: Camera %s: Requests must have at least one surface target. "
    242                     "Rejecting request.", __FUNCTION__, mCameraIdStr.string());
    243             return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
    244                     "Request has no output targets");
    245         }
    246 
    247         /**
    248          * Write in the output stream IDs and map from stream ID to surface ID
    249          * which we calculate from the capture request's list of surface target
    250          */
    251         SurfaceMap surfaceMap;
    252         Vector<int32_t> outputStreamIds;
    253         std::vector<std::string> requestedPhysicalIds;
    254         if (request.mSurfaceList.size() > 0) {
    255             for (sp<Surface> surface : request.mSurfaceList) {
    256                 if (surface == 0) continue;
    257 
    258                 int32_t streamId;
    259                 sp<IGraphicBufferProducer> gbp = surface->getIGraphicBufferProducer();
    260                 res = insertGbpLocked(gbp, &surfaceMap, &outputStreamIds, &streamId);
    261                 if (!res.isOk()) {
    262                     return res;
    263                 }
    264 
    265                 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
    266                 if (index >= 0) {
    267                     String8 requestedPhysicalId(
    268                             mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
    269                     requestedPhysicalIds.push_back(requestedPhysicalId.string());
    270                 } else {
    271                     ALOGW("%s: Output stream Id not found among configured outputs!", __FUNCTION__);
    272                 }
    273             }
    274         } else {
    275             for (size_t i = 0; i < request.mStreamIdxList.size(); i++) {
    276                 int streamId = request.mStreamIdxList.itemAt(i);
    277                 int surfaceIdx = request.mSurfaceIdxList.itemAt(i);
    278 
    279                 ssize_t index = mConfiguredOutputs.indexOfKey(streamId);
    280                 if (index < 0) {
    281                     ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
    282                             " we have not called createStream on: stream %d",
    283                             __FUNCTION__, mCameraIdStr.string(), streamId);
    284                     return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
    285                             "Request targets Surface that is not part of current capture session");
    286                 }
    287 
    288                 const auto& gbps = mConfiguredOutputs.valueAt(index).getGraphicBufferProducers();
    289                 if ((size_t)surfaceIdx >= gbps.size()) {
    290                     ALOGE("%s: Camera %s: Tried to submit a request with a surface that"
    291                             " we have not called createStream on: stream %d, surfaceIdx %d",
    292                             __FUNCTION__, mCameraIdStr.string(), streamId, surfaceIdx);
    293                     return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
    294                             "Request targets Surface has invalid surface index");
    295                 }
    296 
    297                 res = insertGbpLocked(gbps[surfaceIdx], &surfaceMap, &outputStreamIds, nullptr);
    298                 if (!res.isOk()) {
    299                     return res;
    300                 }
    301 
    302                 String8 requestedPhysicalId(
    303                         mConfiguredOutputs.valueAt(index).getPhysicalCameraId());
    304                 requestedPhysicalIds.push_back(requestedPhysicalId.string());
    305             }
    306         }
    307 
    308         CameraDeviceBase::PhysicalCameraSettingsList physicalSettingsList;
    309         for (const auto& it : request.mPhysicalCameraSettings) {
    310             if (it.settings.isEmpty()) {
    311                 ALOGE("%s: Camera %s: Sent empty metadata packet. Rejecting request.",
    312                         __FUNCTION__, mCameraIdStr.string());
    313                 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
    314                         "Request settings are empty");
    315             }
    316 
    317             String8 physicalId(it.id.c_str());
    318             if (physicalId != mDevice->getId()) {
    319                 auto found = std::find(requestedPhysicalIds.begin(), requestedPhysicalIds.end(),
    320                         it.id);
    321                 if (found == requestedPhysicalIds.end()) {
    322                     ALOGE("%s: Camera %s: Physical camera id: %s not part of attached outputs.",
    323                             __FUNCTION__, mCameraIdStr.string(), physicalId.string());
    324                     return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
    325                             "Invalid physical camera id");
    326                 }
    327 
    328                 if (!mSupportedPhysicalRequestKeys.empty()) {
    329                     // Filter out any unsupported physical request keys.
    330                     CameraMetadata filteredParams(mSupportedPhysicalRequestKeys.size());
    331                     camera_metadata_t *meta = const_cast<camera_metadata_t *>(
    332                             filteredParams.getAndLock());
    333                     set_camera_metadata_vendor_id(meta, mDevice->getVendorTagId());
    334                     filteredParams.unlock(meta);
    335 
    336                     for (const auto& keyIt : mSupportedPhysicalRequestKeys) {
    337                         camera_metadata_ro_entry entry = it.settings.find(keyIt);
    338                         if (entry.count > 0) {
    339                             filteredParams.update(entry);
    340                         }
    341                     }
    342 
    343                     physicalSettingsList.push_back({it.id, filteredParams});
    344                 }
    345             } else {
    346                 physicalSettingsList.push_back({it.id, it.settings});
    347             }
    348         }
    349 
    350         if (!enforceRequestPermissions(physicalSettingsList.begin()->metadata)) {
    351             // Callee logs
    352             return STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
    353                     "Caller does not have permission to change restricted controls");
    354         }
    355 
    356         physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_OUTPUT_STREAMS,
    357                 &outputStreamIds[0], outputStreamIds.size());
    358 
    359         if (request.mIsReprocess) {
    360             physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_INPUT_STREAMS,
    361                     &mInputStream.id, 1);
    362         }
    363 
    364         physicalSettingsList.begin()->metadata.update(ANDROID_REQUEST_ID,
    365                 &(submitInfo->mRequestId), /*size*/1);
    366         loopCounter++; // loopCounter starts from 1
    367         ALOGV("%s: Camera %s: Creating request with ID %d (%d of %zu)",
    368                 __FUNCTION__, mCameraIdStr.string(), submitInfo->mRequestId,
    369                 loopCounter, requests.size());
    370 
    371         metadataRequestList.push_back(physicalSettingsList);
    372         surfaceMapList.push_back(surfaceMap);
    373     }
    374     mRequestIdCounter++;
    375 
    376     if (streaming) {
    377         err = mDevice->setStreamingRequestList(metadataRequestList, surfaceMapList,
    378                 &(submitInfo->mLastFrameNumber));
    379         if (err != OK) {
    380             String8 msg = String8::format(
    381                 "Camera %s:  Got error %s (%d) after trying to set streaming request",
    382                 mCameraIdStr.string(), strerror(-err), err);
    383             ALOGE("%s: %s", __FUNCTION__, msg.string());
    384             res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
    385                     msg.string());
    386         } else {
    387             Mutex::Autolock idLock(mStreamingRequestIdLock);
    388             mStreamingRequestId = submitInfo->mRequestId;
    389         }
    390     } else {
    391         err = mDevice->captureList(metadataRequestList, surfaceMapList,
    392                 &(submitInfo->mLastFrameNumber));
    393         if (err != OK) {
    394             String8 msg = String8::format(
    395                 "Camera %s: Got error %s (%d) after trying to submit capture request",
    396                 mCameraIdStr.string(), strerror(-err), err);
    397             ALOGE("%s: %s", __FUNCTION__, msg.string());
    398             res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
    399                     msg.string());
    400         }
    401         ALOGV("%s: requestId = %d ", __FUNCTION__, submitInfo->mRequestId);
    402     }
    403 
    404     ALOGV("%s: Camera %s: End of function", __FUNCTION__, mCameraIdStr.string());
    405     return res;
    406 }
    407 
    408 binder::Status CameraDeviceClient::cancelRequest(
    409         int requestId,
    410         /*out*/
    411         int64_t* lastFrameNumber) {
    412     ATRACE_CALL();
    413     ALOGV("%s, requestId = %d", __FUNCTION__, requestId);
    414 
    415     status_t err;
    416     binder::Status res;
    417 
    418     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
    419 
    420     Mutex::Autolock icl(mBinderSerializationLock);
    421 
    422     if (!mDevice.get()) {
    423         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
    424     }
    425 
    426     Mutex::Autolock idLock(mStreamingRequestIdLock);
    427     if (mStreamingRequestId != requestId) {
    428         String8 msg = String8::format("Camera %s: Canceling request ID %d doesn't match "
    429                 "current request ID %d", mCameraIdStr.string(), requestId, mStreamingRequestId);
    430         ALOGE("%s: %s", __FUNCTION__, msg.string());
    431         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
    432     }
    433 
    434     err = mDevice->clearStreamingRequest(lastFrameNumber);
    435 
    436     if (err == OK) {
    437         ALOGV("%s: Camera %s: Successfully cleared streaming request",
    438                 __FUNCTION__, mCameraIdStr.string());
    439         mStreamingRequestId = REQUEST_ID_NONE;
    440     } else {
    441         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
    442                 "Camera %s: Error clearing streaming request: %s (%d)",
    443                 mCameraIdStr.string(), strerror(-err), err);
    444     }
    445 
    446     return res;
    447 }
    448 
    449 binder::Status CameraDeviceClient::beginConfigure() {
    450     // TODO: Implement this.
    451     ATRACE_CALL();
    452     ALOGV("%s: Not implemented yet.", __FUNCTION__);
    453     return binder::Status::ok();
    454 }
    455 
    456 binder::Status CameraDeviceClient::endConfigure(int operatingMode,
    457         const hardware::camera2::impl::CameraMetadataNative& sessionParams) {
    458     ATRACE_CALL();
    459     ALOGV("%s: ending configure (%d input stream, %zu output surfaces)",
    460             __FUNCTION__, mInputStream.configured ? 1 : 0,
    461             mStreamMap.size());
    462 
    463     binder::Status res;
    464     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
    465 
    466     Mutex::Autolock icl(mBinderSerializationLock);
    467 
    468     if (!mDevice.get()) {
    469         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
    470     }
    471 
    472     if (operatingMode < 0) {
    473         String8 msg = String8::format(
    474             "Camera %s: Invalid operating mode %d requested", mCameraIdStr.string(), operatingMode);
    475         ALOGE("%s: %s", __FUNCTION__, msg.string());
    476         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
    477                 msg.string());
    478     }
    479 
    480     // Sanitize the high speed session against necessary capability bit.
    481     bool isConstrainedHighSpeed = (operatingMode == ICameraDeviceUser::CONSTRAINED_HIGH_SPEED_MODE);
    482     if (isConstrainedHighSpeed) {
    483         CameraMetadata staticInfo = mDevice->info();
    484         camera_metadata_entry_t entry = staticInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
    485         bool isConstrainedHighSpeedSupported = false;
    486         for(size_t i = 0; i < entry.count; ++i) {
    487             uint8_t capability = entry.data.u8[i];
    488             if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_CONSTRAINED_HIGH_SPEED_VIDEO) {
    489                 isConstrainedHighSpeedSupported = true;
    490                 break;
    491             }
    492         }
    493         if (!isConstrainedHighSpeedSupported) {
    494             String8 msg = String8::format(
    495                 "Camera %s: Try to create a constrained high speed configuration on a device"
    496                 " that doesn't support it.", mCameraIdStr.string());
    497             ALOGE("%s: %s", __FUNCTION__, msg.string());
    498             return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
    499                     msg.string());
    500         }
    501     }
    502 
    503     status_t err = mDevice->configureStreams(sessionParams, operatingMode);
    504     if (err == BAD_VALUE) {
    505         String8 msg = String8::format("Camera %s: Unsupported set of inputs/outputs provided",
    506                 mCameraIdStr.string());
    507         ALOGE("%s: %s", __FUNCTION__, msg.string());
    508         res = STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
    509     } else if (err != OK) {
    510         String8 msg = String8::format("Camera %s: Error configuring streams: %s (%d)",
    511                 mCameraIdStr.string(), strerror(-err), err);
    512         ALOGE("%s: %s", __FUNCTION__, msg.string());
    513         res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
    514     }
    515 
    516     return res;
    517 }
    518 
    519 binder::Status CameraDeviceClient::deleteStream(int streamId) {
    520     ATRACE_CALL();
    521     ALOGV("%s (streamId = 0x%x)", __FUNCTION__, streamId);
    522 
    523     binder::Status res;
    524     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
    525 
    526     Mutex::Autolock icl(mBinderSerializationLock);
    527 
    528     if (!mDevice.get()) {
    529         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
    530     }
    531 
    532     bool isInput = false;
    533     std::vector<sp<IBinder>> surfaces;
    534     ssize_t dIndex = NAME_NOT_FOUND;
    535 
    536     if (mInputStream.configured && mInputStream.id == streamId) {
    537         isInput = true;
    538     } else {
    539         // Guard against trying to delete non-created streams
    540         for (size_t i = 0; i < mStreamMap.size(); ++i) {
    541             if (streamId == mStreamMap.valueAt(i).streamId()) {
    542                 surfaces.push_back(mStreamMap.keyAt(i));
    543             }
    544         }
    545 
    546         // See if this stream is one of the deferred streams.
    547         for (size_t i = 0; i < mDeferredStreams.size(); ++i) {
    548             if (streamId == mDeferredStreams[i]) {
    549                 dIndex = i;
    550                 break;
    551             }
    552         }
    553 
    554         if (surfaces.empty() && dIndex == NAME_NOT_FOUND) {
    555             String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no such"
    556                     " stream created yet", mCameraIdStr.string(), streamId);
    557             ALOGW("%s: %s", __FUNCTION__, msg.string());
    558             return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
    559         }
    560     }
    561 
    562     // Also returns BAD_VALUE if stream ID was not valid
    563     status_t err = mDevice->deleteStream(streamId);
    564 
    565     if (err != OK) {
    566         String8 msg = String8::format("Camera %s: Unexpected error %s (%d) when deleting stream %d",
    567                 mCameraIdStr.string(), strerror(-err), err, streamId);
    568         ALOGE("%s: %s", __FUNCTION__, msg.string());
    569         res = STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
    570     } else {
    571         if (isInput) {
    572             mInputStream.configured = false;
    573         } else {
    574             for (auto& surface : surfaces) {
    575                 mStreamMap.removeItem(surface);
    576             }
    577 
    578             mConfiguredOutputs.removeItem(streamId);
    579 
    580             if (dIndex != NAME_NOT_FOUND) {
    581                 mDeferredStreams.removeItemsAt(dIndex);
    582             }
    583         }
    584     }
    585 
    586     return res;
    587 }
    588 
    589 binder::Status CameraDeviceClient::createStream(
    590         const hardware::camera2::params::OutputConfiguration &outputConfiguration,
    591         /*out*/
    592         int32_t* newStreamId) {
    593     ATRACE_CALL();
    594 
    595     binder::Status res;
    596     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
    597 
    598     Mutex::Autolock icl(mBinderSerializationLock);
    599 
    600     const std::vector<sp<IGraphicBufferProducer>>& bufferProducers =
    601             outputConfiguration.getGraphicBufferProducers();
    602     size_t numBufferProducers = bufferProducers.size();
    603     bool deferredConsumer = outputConfiguration.isDeferred();
    604     bool isShared = outputConfiguration.isShared();
    605     String8 physicalCameraId = String8(outputConfiguration.getPhysicalCameraId());
    606 
    607     if (numBufferProducers > MAX_SURFACES_PER_STREAM) {
    608         ALOGE("%s: GraphicBufferProducer count %zu for stream exceeds limit of %d",
    609               __FUNCTION__, bufferProducers.size(), MAX_SURFACES_PER_STREAM);
    610         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Surface count is too high");
    611     }
    612     bool deferredConsumerOnly = deferredConsumer && numBufferProducers == 0;
    613     int surfaceType = outputConfiguration.getSurfaceType();
    614     bool validSurfaceType = ((surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) ||
    615             (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_TEXTURE));
    616 
    617     if (deferredConsumer && !validSurfaceType) {
    618         ALOGE("%s: Target surface is invalid: bufferProducer = %p, surfaceType = %d.",
    619                 __FUNCTION__, bufferProducers[0].get(), surfaceType);
    620         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
    621     }
    622 
    623     if (!mDevice.get()) {
    624         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
    625     }
    626 
    627     if (physicalCameraId.size() > 0) {
    628         std::vector<std::string> physicalCameraIds;
    629         std::string physicalId(physicalCameraId.string());
    630         bool logicalCamera =
    631                 CameraProviderManager::isLogicalCamera(mDevice->info(), &physicalCameraIds);
    632         if (!logicalCamera ||
    633                 std::find(physicalCameraIds.begin(), physicalCameraIds.end(), physicalId) ==
    634                 physicalCameraIds.end()) {
    635             String8 msg = String8::format("Camera %s: Camera doesn't support physicalCameraId %s.",
    636                     mCameraIdStr.string(), physicalCameraId.string());
    637             ALOGE("%s: %s", __FUNCTION__, msg.string());
    638             return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
    639         }
    640     }
    641     std::vector<sp<Surface>> surfaces;
    642     std::vector<sp<IBinder>> binders;
    643     status_t err;
    644 
    645     // Create stream for deferred surface case.
    646     if (deferredConsumerOnly) {
    647         return createDeferredSurfaceStreamLocked(outputConfiguration, isShared, newStreamId);
    648     }
    649 
    650     OutputStreamInfo streamInfo;
    651     bool isStreamInfoValid = false;
    652     for (auto& bufferProducer : bufferProducers) {
    653         // Don't create multiple streams for the same target surface
    654         sp<IBinder> binder = IInterface::asBinder(bufferProducer);
    655         ssize_t index = mStreamMap.indexOfKey(binder);
    656         if (index != NAME_NOT_FOUND) {
    657             String8 msg = String8::format("Camera %s: Surface already has a stream created for it "
    658                     "(ID %zd)", mCameraIdStr.string(), index);
    659             ALOGW("%s: %s", __FUNCTION__, msg.string());
    660             return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
    661         }
    662 
    663         sp<Surface> surface;
    664         res = createSurfaceFromGbp(streamInfo, isStreamInfoValid, surface, bufferProducer);
    665 
    666         if (!res.isOk())
    667             return res;
    668 
    669         if (!isStreamInfoValid) {
    670             isStreamInfoValid = true;
    671         }
    672 
    673         binders.push_back(IInterface::asBinder(bufferProducer));
    674         surfaces.push_back(surface);
    675     }
    676 
    677     int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
    678     std::vector<int> surfaceIds;
    679     err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
    680             streamInfo.height, streamInfo.format, streamInfo.dataSpace,
    681             static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
    682             &streamId, physicalCameraId, &surfaceIds, outputConfiguration.getSurfaceSetID(),
    683             isShared);
    684 
    685     if (err != OK) {
    686         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
    687                 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
    688                 mCameraIdStr.string(), streamInfo.width, streamInfo.height, streamInfo.format,
    689                 streamInfo.dataSpace, strerror(-err), err);
    690     } else {
    691         int i = 0;
    692         for (auto& binder : binders) {
    693             ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d",
    694                     __FUNCTION__, binder.get(), streamId, i);
    695             mStreamMap.add(binder, StreamSurfaceId(streamId, surfaceIds[i]));
    696             i++;
    697         }
    698 
    699         mConfiguredOutputs.add(streamId, outputConfiguration);
    700         mStreamInfoMap[streamId] = streamInfo;
    701 
    702         ALOGV("%s: Camera %s: Successfully created a new stream ID %d for output surface"
    703                     " (%d x %d) with format 0x%x.",
    704                   __FUNCTION__, mCameraIdStr.string(), streamId, streamInfo.width,
    705                   streamInfo.height, streamInfo.format);
    706 
    707         // Set transform flags to ensure preview to be rotated correctly.
    708         res = setStreamTransformLocked(streamId);
    709 
    710         *newStreamId = streamId;
    711     }
    712 
    713     return res;
    714 }
    715 
    716 binder::Status CameraDeviceClient::createDeferredSurfaceStreamLocked(
    717         const hardware::camera2::params::OutputConfiguration &outputConfiguration,
    718         bool isShared,
    719         /*out*/
    720         int* newStreamId) {
    721     int width, height, format, surfaceType;
    722     uint64_t consumerUsage;
    723     android_dataspace dataSpace;
    724     status_t err;
    725     binder::Status res;
    726 
    727     if (!mDevice.get()) {
    728         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
    729     }
    730 
    731     // Infer the surface info for deferred surface stream creation.
    732     width = outputConfiguration.getWidth();
    733     height = outputConfiguration.getHeight();
    734     surfaceType = outputConfiguration.getSurfaceType();
    735     format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
    736     dataSpace = android_dataspace_t::HAL_DATASPACE_UNKNOWN;
    737     // Hardcode consumer usage flags: SurfaceView--0x900, SurfaceTexture--0x100.
    738     consumerUsage = GraphicBuffer::USAGE_HW_TEXTURE;
    739     if (surfaceType == OutputConfiguration::SURFACE_TYPE_SURFACE_VIEW) {
    740         consumerUsage |= GraphicBuffer::USAGE_HW_COMPOSER;
    741     }
    742     int streamId = camera3::CAMERA3_STREAM_ID_INVALID;
    743     std::vector<sp<Surface>> noSurface;
    744     std::vector<int> surfaceIds;
    745     String8 physicalCameraId(outputConfiguration.getPhysicalCameraId());
    746     err = mDevice->createStream(noSurface, /*hasDeferredConsumer*/true, width,
    747             height, format, dataSpace,
    748             static_cast<camera3_stream_rotation_t>(outputConfiguration.getRotation()),
    749             &streamId, physicalCameraId, &surfaceIds,
    750             outputConfiguration.getSurfaceSetID(), isShared,
    751             consumerUsage);
    752 
    753     if (err != OK) {
    754         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
    755                 "Camera %s: Error creating output stream (%d x %d, fmt %x, dataSpace %x): %s (%d)",
    756                 mCameraIdStr.string(), width, height, format, dataSpace, strerror(-err), err);
    757     } else {
    758         // Can not add streamId to mStreamMap here, as the surface is deferred. Add it to
    759         // a separate list to track. Once the deferred surface is set, this id will be
    760         // relocated to mStreamMap.
    761         mDeferredStreams.push_back(streamId);
    762 
    763         mStreamInfoMap.emplace(std::piecewise_construct, std::forward_as_tuple(streamId),
    764                 std::forward_as_tuple(width, height, format, dataSpace, consumerUsage));
    765 
    766         ALOGV("%s: Camera %s: Successfully created a new stream ID %d for a deferred surface"
    767                 " (%d x %d) stream with format 0x%x.",
    768               __FUNCTION__, mCameraIdStr.string(), streamId, width, height, format);
    769 
    770         // Set transform flags to ensure preview to be rotated correctly.
    771         res = setStreamTransformLocked(streamId);
    772 
    773         *newStreamId = streamId;
    774     }
    775     return res;
    776 }
    777 
    778 binder::Status CameraDeviceClient::setStreamTransformLocked(int streamId) {
    779     int32_t transform = 0;
    780     status_t err;
    781     binder::Status res;
    782 
    783     if (!mDevice.get()) {
    784         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
    785     }
    786 
    787     err = getRotationTransformLocked(&transform);
    788     if (err != OK) {
    789         // Error logged by getRotationTransformLocked.
    790         return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION,
    791                 "Unable to calculate rotation transform for new stream");
    792     }
    793 
    794     err = mDevice->setStreamTransform(streamId, transform);
    795     if (err != OK) {
    796         String8 msg = String8::format("Failed to set stream transform (stream id %d)",
    797                 streamId);
    798         ALOGE("%s: %s", __FUNCTION__, msg.string());
    799         return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
    800     }
    801 
    802     return res;
    803 }
    804 
    805 binder::Status CameraDeviceClient::createInputStream(
    806         int width, int height, int format,
    807         /*out*/
    808         int32_t* newStreamId) {
    809 
    810     ATRACE_CALL();
    811     ALOGV("%s (w = %d, h = %d, f = 0x%x)", __FUNCTION__, width, height, format);
    812 
    813     binder::Status res;
    814     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
    815 
    816     Mutex::Autolock icl(mBinderSerializationLock);
    817 
    818     if (!mDevice.get()) {
    819         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
    820     }
    821 
    822     if (mInputStream.configured) {
    823         String8 msg = String8::format("Camera %s: Already has an input stream "
    824                 "configured (ID %d)", mCameraIdStr.string(), mInputStream.id);
    825         ALOGE("%s: %s", __FUNCTION__, msg.string() );
    826         return STATUS_ERROR(CameraService::ERROR_ALREADY_EXISTS, msg.string());
    827     }
    828 
    829     int streamId = -1;
    830     status_t err = mDevice->createInputStream(width, height, format, &streamId);
    831     if (err == OK) {
    832         mInputStream.configured = true;
    833         mInputStream.width = width;
    834         mInputStream.height = height;
    835         mInputStream.format = format;
    836         mInputStream.id = streamId;
    837 
    838         ALOGV("%s: Camera %s: Successfully created a new input stream ID %d",
    839                 __FUNCTION__, mCameraIdStr.string(), streamId);
    840 
    841         *newStreamId = streamId;
    842     } else {
    843         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
    844                 "Camera %s: Error creating new input stream: %s (%d)", mCameraIdStr.string(),
    845                 strerror(-err), err);
    846     }
    847 
    848     return res;
    849 }
    850 
    851 binder::Status CameraDeviceClient::getInputSurface(/*out*/ view::Surface *inputSurface) {
    852 
    853     binder::Status res;
    854     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
    855 
    856     if (inputSurface == NULL) {
    857         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Null input surface");
    858     }
    859 
    860     Mutex::Autolock icl(mBinderSerializationLock);
    861     if (!mDevice.get()) {
    862         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
    863     }
    864     sp<IGraphicBufferProducer> producer;
    865     status_t err = mDevice->getInputBufferProducer(&producer);
    866     if (err != OK) {
    867         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
    868                 "Camera %s: Error getting input Surface: %s (%d)",
    869                 mCameraIdStr.string(), strerror(-err), err);
    870     } else {
    871         inputSurface->name = String16("CameraInput");
    872         inputSurface->graphicBufferProducer = producer;
    873     }
    874     return res;
    875 }
    876 
    877 binder::Status CameraDeviceClient::updateOutputConfiguration(int streamId,
    878         const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
    879     ATRACE_CALL();
    880 
    881     binder::Status res;
    882     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
    883 
    884     Mutex::Autolock icl(mBinderSerializationLock);
    885 
    886     if (!mDevice.get()) {
    887         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
    888     }
    889 
    890     const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
    891             outputConfiguration.getGraphicBufferProducers();
    892     auto producerCount = bufferProducers.size();
    893     if (producerCount == 0) {
    894         ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
    895         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
    896                 "bufferProducers must not be empty");
    897     }
    898 
    899     // The first output is the one associated with the output configuration.
    900     // It should always be present, valid and the corresponding stream id should match.
    901     sp<IBinder> binder = IInterface::asBinder(bufferProducers[0]);
    902     ssize_t index = mStreamMap.indexOfKey(binder);
    903     if (index == NAME_NOT_FOUND) {
    904         ALOGE("%s: Outputconfiguration is invalid", __FUNCTION__);
    905         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
    906                 "OutputConfiguration is invalid");
    907     }
    908     if (mStreamMap.valueFor(binder).streamId() != streamId) {
    909         ALOGE("%s: Stream Id: %d provided doesn't match the id: %d in the stream map",
    910                 __FUNCTION__, streamId, mStreamMap.valueFor(binder).streamId());
    911         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
    912                 "Stream id is invalid");
    913     }
    914 
    915     std::vector<size_t> removedSurfaceIds;
    916     std::vector<sp<IBinder>> removedOutputs;
    917     std::vector<sp<Surface>> newOutputs;
    918     std::vector<OutputStreamInfo> streamInfos;
    919     KeyedVector<sp<IBinder>, sp<IGraphicBufferProducer>> newOutputsMap;
    920     for (auto &it : bufferProducers) {
    921         newOutputsMap.add(IInterface::asBinder(it), it);
    922     }
    923 
    924     for (size_t i = 0; i < mStreamMap.size(); i++) {
    925         ssize_t idx = newOutputsMap.indexOfKey(mStreamMap.keyAt(i));
    926         if (idx == NAME_NOT_FOUND) {
    927             if (mStreamMap[i].streamId() == streamId) {
    928                 removedSurfaceIds.push_back(mStreamMap[i].surfaceId());
    929                 removedOutputs.push_back(mStreamMap.keyAt(i));
    930             }
    931         } else {
    932             if (mStreamMap[i].streamId() != streamId) {
    933                 ALOGE("%s: Output surface already part of a different stream", __FUNCTION__);
    934                 return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT,
    935                         "Target Surface is invalid");
    936             }
    937             newOutputsMap.removeItemsAt(idx);
    938         }
    939     }
    940 
    941     for (size_t i = 0; i < newOutputsMap.size(); i++) {
    942         OutputStreamInfo outInfo;
    943         sp<Surface> surface;
    944         res = createSurfaceFromGbp(outInfo, /*isStreamInfoValid*/ false, surface,
    945                 newOutputsMap.valueAt(i));
    946         if (!res.isOk())
    947             return res;
    948 
    949         streamInfos.push_back(outInfo);
    950         newOutputs.push_back(surface);
    951     }
    952 
    953     //Trivial case no changes required
    954     if (removedSurfaceIds.empty() && newOutputs.empty()) {
    955         return binder::Status::ok();
    956     }
    957 
    958     KeyedVector<sp<Surface>, size_t> outputMap;
    959     auto ret = mDevice->updateStream(streamId, newOutputs, streamInfos, removedSurfaceIds,
    960             &outputMap);
    961     if (ret != OK) {
    962         switch (ret) {
    963             case NAME_NOT_FOUND:
    964             case BAD_VALUE:
    965             case -EBUSY:
    966                 res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
    967                         "Camera %s: Error updating stream: %s (%d)",
    968                         mCameraIdStr.string(), strerror(ret), ret);
    969                 break;
    970             default:
    971                 res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
    972                         "Camera %s: Error updating stream: %s (%d)",
    973                         mCameraIdStr.string(), strerror(ret), ret);
    974                 break;
    975         }
    976     } else {
    977         for (const auto &it : removedOutputs) {
    978             mStreamMap.removeItem(it);
    979         }
    980 
    981         for (size_t i = 0; i < outputMap.size(); i++) {
    982             mStreamMap.add(IInterface::asBinder(outputMap.keyAt(i)->getIGraphicBufferProducer()),
    983                     StreamSurfaceId(streamId, outputMap.valueAt(i)));
    984         }
    985 
    986         mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
    987 
    988         ALOGV("%s: Camera %s: Successful stream ID %d update",
    989                   __FUNCTION__, mCameraIdStr.string(), streamId);
    990     }
    991 
    992     return res;
    993 }
    994 
    995 bool CameraDeviceClient::isPublicFormat(int32_t format)
    996 {
    997     switch(format) {
    998         case HAL_PIXEL_FORMAT_RGBA_8888:
    999         case HAL_PIXEL_FORMAT_RGBX_8888:
   1000         case HAL_PIXEL_FORMAT_RGB_888:
   1001         case HAL_PIXEL_FORMAT_RGB_565:
   1002         case HAL_PIXEL_FORMAT_BGRA_8888:
   1003         case HAL_PIXEL_FORMAT_YV12:
   1004         case HAL_PIXEL_FORMAT_Y8:
   1005         case HAL_PIXEL_FORMAT_Y16:
   1006         case HAL_PIXEL_FORMAT_RAW16:
   1007         case HAL_PIXEL_FORMAT_RAW10:
   1008         case HAL_PIXEL_FORMAT_RAW12:
   1009         case HAL_PIXEL_FORMAT_RAW_OPAQUE:
   1010         case HAL_PIXEL_FORMAT_BLOB:
   1011         case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
   1012         case HAL_PIXEL_FORMAT_YCbCr_420_888:
   1013         case HAL_PIXEL_FORMAT_YCbCr_422_SP:
   1014         case HAL_PIXEL_FORMAT_YCrCb_420_SP:
   1015         case HAL_PIXEL_FORMAT_YCbCr_422_I:
   1016             return true;
   1017         default:
   1018             return false;
   1019     }
   1020 }
   1021 
   1022 binder::Status CameraDeviceClient::createSurfaceFromGbp(
   1023         OutputStreamInfo& streamInfo, bool isStreamInfoValid,
   1024         sp<Surface>& surface, const sp<IGraphicBufferProducer>& gbp) {
   1025 
   1026     // bufferProducer must be non-null
   1027     if (gbp == nullptr) {
   1028         String8 msg = String8::format("Camera %s: Surface is NULL", mCameraIdStr.string());
   1029         ALOGW("%s: %s", __FUNCTION__, msg.string());
   1030         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
   1031     }
   1032     // HACK b/10949105
   1033     // Query consumer usage bits to set async operation mode for
   1034     // GLConsumer using controlledByApp parameter.
   1035     bool useAsync = false;
   1036     uint64_t consumerUsage = 0;
   1037     status_t err;
   1038     if ((err = gbp->getConsumerUsage(&consumerUsage)) != OK) {
   1039         String8 msg = String8::format("Camera %s: Failed to query Surface consumer usage: %s (%d)",
   1040                 mCameraIdStr.string(), strerror(-err), err);
   1041         ALOGE("%s: %s", __FUNCTION__, msg.string());
   1042         return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
   1043     }
   1044     if (consumerUsage & GraphicBuffer::USAGE_HW_TEXTURE) {
   1045         ALOGW("%s: Camera %s with consumer usage flag: %" PRIu64 ": Forcing asynchronous mode for stream",
   1046                 __FUNCTION__, mCameraIdStr.string(), consumerUsage);
   1047         useAsync = true;
   1048     }
   1049 
   1050     uint64_t disallowedFlags = GraphicBuffer::USAGE_HW_VIDEO_ENCODER |
   1051                               GRALLOC_USAGE_RENDERSCRIPT;
   1052     uint64_t allowedFlags = GraphicBuffer::USAGE_SW_READ_MASK |
   1053                            GraphicBuffer::USAGE_HW_TEXTURE |
   1054                            GraphicBuffer::USAGE_HW_COMPOSER;
   1055     bool flexibleConsumer = (consumerUsage & disallowedFlags) == 0 &&
   1056             (consumerUsage & allowedFlags) != 0;
   1057 
   1058     surface = new Surface(gbp, useAsync);
   1059     ANativeWindow *anw = surface.get();
   1060 
   1061     int width, height, format;
   1062     android_dataspace dataSpace;
   1063     if ((err = anw->query(anw, NATIVE_WINDOW_WIDTH, &width)) != OK) {
   1064         String8 msg = String8::format("Camera %s: Failed to query Surface width: %s (%d)",
   1065                  mCameraIdStr.string(), strerror(-err), err);
   1066         ALOGE("%s: %s", __FUNCTION__, msg.string());
   1067         return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
   1068     }
   1069     if ((err = anw->query(anw, NATIVE_WINDOW_HEIGHT, &height)) != OK) {
   1070         String8 msg = String8::format("Camera %s: Failed to query Surface height: %s (%d)",
   1071                 mCameraIdStr.string(), strerror(-err), err);
   1072         ALOGE("%s: %s", __FUNCTION__, msg.string());
   1073         return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
   1074     }
   1075     if ((err = anw->query(anw, NATIVE_WINDOW_FORMAT, &format)) != OK) {
   1076         String8 msg = String8::format("Camera %s: Failed to query Surface format: %s (%d)",
   1077                 mCameraIdStr.string(), strerror(-err), err);
   1078         ALOGE("%s: %s", __FUNCTION__, msg.string());
   1079         return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
   1080     }
   1081     if ((err = anw->query(anw, NATIVE_WINDOW_DEFAULT_DATASPACE,
   1082             reinterpret_cast<int*>(&dataSpace))) != OK) {
   1083         String8 msg = String8::format("Camera %s: Failed to query Surface dataspace: %s (%d)",
   1084                 mCameraIdStr.string(), strerror(-err), err);
   1085         ALOGE("%s: %s", __FUNCTION__, msg.string());
   1086         return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
   1087     }
   1088 
   1089     // FIXME: remove this override since the default format should be
   1090     //       IMPLEMENTATION_DEFINED. b/9487482 & b/35317944
   1091     if ((format >= HAL_PIXEL_FORMAT_RGBA_8888 && format <= HAL_PIXEL_FORMAT_BGRA_8888) &&
   1092             ((consumerUsage & GRALLOC_USAGE_HW_MASK) &&
   1093              ((consumerUsage & GRALLOC_USAGE_SW_READ_MASK) == 0))) {
   1094         ALOGW("%s: Camera %s: Overriding format %#x to IMPLEMENTATION_DEFINED",
   1095                 __FUNCTION__, mCameraIdStr.string(), format);
   1096         format = HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
   1097     }
   1098     // Round dimensions to the nearest dimensions available for this format
   1099     if (flexibleConsumer && isPublicFormat(format) &&
   1100             !CameraDeviceClient::roundBufferDimensionNearest(width, height,
   1101             format, dataSpace, mDevice->info(), /*out*/&width, /*out*/&height)) {
   1102         String8 msg = String8::format("Camera %s: No supported stream configurations with "
   1103                 "format %#x defined, failed to create output stream",
   1104                 mCameraIdStr.string(), format);
   1105         ALOGE("%s: %s", __FUNCTION__, msg.string());
   1106         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
   1107     }
   1108 
   1109     if (!isStreamInfoValid) {
   1110         streamInfo.width = width;
   1111         streamInfo.height = height;
   1112         streamInfo.format = format;
   1113         streamInfo.dataSpace = dataSpace;
   1114         streamInfo.consumerUsage = consumerUsage;
   1115         return binder::Status::ok();
   1116     }
   1117     if (width != streamInfo.width) {
   1118         String8 msg = String8::format("Camera %s:Surface width doesn't match: %d vs %d",
   1119                 mCameraIdStr.string(), width, streamInfo.width);
   1120         ALOGE("%s: %s", __FUNCTION__, msg.string());
   1121         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
   1122     }
   1123     if (height != streamInfo.height) {
   1124         String8 msg = String8::format("Camera %s:Surface height doesn't match: %d vs %d",
   1125                  mCameraIdStr.string(), height, streamInfo.height);
   1126         ALOGE("%s: %s", __FUNCTION__, msg.string());
   1127         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
   1128     }
   1129     if (format != streamInfo.format) {
   1130         String8 msg = String8::format("Camera %s:Surface format doesn't match: %d vs %d",
   1131                  mCameraIdStr.string(), format, streamInfo.format);
   1132         ALOGE("%s: %s", __FUNCTION__, msg.string());
   1133         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
   1134     }
   1135     if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
   1136         if (dataSpace != streamInfo.dataSpace) {
   1137             String8 msg = String8::format("Camera %s:Surface dataSpace doesn't match: %d vs %d",
   1138                     mCameraIdStr.string(), dataSpace, streamInfo.dataSpace);
   1139             ALOGE("%s: %s", __FUNCTION__, msg.string());
   1140             return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
   1141         }
   1142         //At the native side, there isn't a way to check whether 2 surfaces come from the same
   1143         //surface class type. Use usage flag to approximate the comparison.
   1144         if (consumerUsage != streamInfo.consumerUsage) {
   1145             String8 msg = String8::format(
   1146                     "Camera %s:Surface usage flag doesn't match %" PRIu64 " vs %" PRIu64 "",
   1147                     mCameraIdStr.string(), consumerUsage, streamInfo.consumerUsage);
   1148             ALOGE("%s: %s", __FUNCTION__, msg.string());
   1149             return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
   1150         }
   1151     }
   1152     return binder::Status::ok();
   1153 }
   1154 
   1155 bool CameraDeviceClient::roundBufferDimensionNearest(int32_t width, int32_t height,
   1156         int32_t format, android_dataspace dataSpace, const CameraMetadata& info,
   1157         /*out*/int32_t* outWidth, /*out*/int32_t* outHeight) {
   1158 
   1159     camera_metadata_ro_entry streamConfigs =
   1160             (dataSpace == HAL_DATASPACE_DEPTH) ?
   1161             info.find(ANDROID_DEPTH_AVAILABLE_DEPTH_STREAM_CONFIGURATIONS) :
   1162             info.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
   1163 
   1164     int32_t bestWidth = -1;
   1165     int32_t bestHeight = -1;
   1166 
   1167     // Iterate through listed stream configurations and find the one with the smallest euclidean
   1168     // distance from the given dimensions for the given format.
   1169     for (size_t i = 0; i < streamConfigs.count; i += 4) {
   1170         int32_t fmt = streamConfigs.data.i32[i];
   1171         int32_t w = streamConfigs.data.i32[i + 1];
   1172         int32_t h = streamConfigs.data.i32[i + 2];
   1173 
   1174         // Ignore input/output type for now
   1175         if (fmt == format) {
   1176             if (w == width && h == height) {
   1177                 bestWidth = width;
   1178                 bestHeight = height;
   1179                 break;
   1180             } else if (w <= ROUNDING_WIDTH_CAP && (bestWidth == -1 ||
   1181                     CameraDeviceClient::euclidDistSquare(w, h, width, height) <
   1182                     CameraDeviceClient::euclidDistSquare(bestWidth, bestHeight, width, height))) {
   1183                 bestWidth = w;
   1184                 bestHeight = h;
   1185             }
   1186         }
   1187     }
   1188 
   1189     if (bestWidth == -1) {
   1190         // Return false if no configurations for this format were listed
   1191         return false;
   1192     }
   1193 
   1194     // Set the outputs to the closet width/height
   1195     if (outWidth != NULL) {
   1196         *outWidth = bestWidth;
   1197     }
   1198     if (outHeight != NULL) {
   1199         *outHeight = bestHeight;
   1200     }
   1201 
   1202     // Return true if at least one configuration for this format was listed
   1203     return true;
   1204 }
   1205 
   1206 int64_t CameraDeviceClient::euclidDistSquare(int32_t x0, int32_t y0, int32_t x1, int32_t y1) {
   1207     int64_t d0 = x0 - x1;
   1208     int64_t d1 = y0 - y1;
   1209     return d0 * d0 + d1 * d1;
   1210 }
   1211 
   1212 // Create a request object from a template.
   1213 binder::Status CameraDeviceClient::createDefaultRequest(int templateId,
   1214         /*out*/
   1215         hardware::camera2::impl::CameraMetadataNative* request)
   1216 {
   1217     ATRACE_CALL();
   1218     ALOGV("%s (templateId = 0x%x)", __FUNCTION__, templateId);
   1219 
   1220     binder::Status res;
   1221     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
   1222 
   1223     Mutex::Autolock icl(mBinderSerializationLock);
   1224 
   1225     if (!mDevice.get()) {
   1226         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
   1227     }
   1228 
   1229     CameraMetadata metadata;
   1230     status_t err;
   1231     if ( (err = mDevice->createDefaultRequest(templateId, &metadata) ) == OK &&
   1232         request != NULL) {
   1233 
   1234         request->swap(metadata);
   1235     } else if (err == BAD_VALUE) {
   1236         res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
   1237                 "Camera %s: Template ID %d is invalid or not supported: %s (%d)",
   1238                 mCameraIdStr.string(), templateId, strerror(-err), err);
   1239 
   1240     } else {
   1241         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
   1242                 "Camera %s: Error creating default request for template %d: %s (%d)",
   1243                 mCameraIdStr.string(), templateId, strerror(-err), err);
   1244     }
   1245     return res;
   1246 }
   1247 
   1248 binder::Status CameraDeviceClient::getCameraInfo(
   1249         /*out*/
   1250         hardware::camera2::impl::CameraMetadataNative* info)
   1251 {
   1252     ATRACE_CALL();
   1253     ALOGV("%s", __FUNCTION__);
   1254 
   1255     binder::Status res;
   1256 
   1257     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
   1258 
   1259     Mutex::Autolock icl(mBinderSerializationLock);
   1260 
   1261     if (!mDevice.get()) {
   1262         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
   1263     }
   1264 
   1265     if (info != NULL) {
   1266         *info = mDevice->info(); // static camera metadata
   1267         // TODO: merge with device-specific camera metadata
   1268     }
   1269 
   1270     return res;
   1271 }
   1272 
   1273 binder::Status CameraDeviceClient::waitUntilIdle()
   1274 {
   1275     ATRACE_CALL();
   1276     ALOGV("%s", __FUNCTION__);
   1277 
   1278     binder::Status res;
   1279     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
   1280 
   1281     Mutex::Autolock icl(mBinderSerializationLock);
   1282 
   1283     if (!mDevice.get()) {
   1284         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
   1285     }
   1286 
   1287     // FIXME: Also need check repeating burst.
   1288     Mutex::Autolock idLock(mStreamingRequestIdLock);
   1289     if (mStreamingRequestId != REQUEST_ID_NONE) {
   1290         String8 msg = String8::format(
   1291             "Camera %s: Try to waitUntilIdle when there are active streaming requests",
   1292             mCameraIdStr.string());
   1293         ALOGE("%s: %s", __FUNCTION__, msg.string());
   1294         return STATUS_ERROR(CameraService::ERROR_INVALID_OPERATION, msg.string());
   1295     }
   1296     status_t err = mDevice->waitUntilDrained();
   1297     if (err != OK) {
   1298         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
   1299                 "Camera %s: Error waiting to drain: %s (%d)",
   1300                 mCameraIdStr.string(), strerror(-err), err);
   1301     }
   1302     ALOGV("%s Done", __FUNCTION__);
   1303     return res;
   1304 }
   1305 
   1306 binder::Status CameraDeviceClient::flush(
   1307         /*out*/
   1308         int64_t* lastFrameNumber) {
   1309     ATRACE_CALL();
   1310     ALOGV("%s", __FUNCTION__);
   1311 
   1312     binder::Status res;
   1313     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
   1314 
   1315     Mutex::Autolock icl(mBinderSerializationLock);
   1316 
   1317     if (!mDevice.get()) {
   1318         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
   1319     }
   1320 
   1321     Mutex::Autolock idLock(mStreamingRequestIdLock);
   1322     mStreamingRequestId = REQUEST_ID_NONE;
   1323     status_t err = mDevice->flush(lastFrameNumber);
   1324     if (err != OK) {
   1325         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
   1326                 "Camera %s: Error flushing device: %s (%d)", mCameraIdStr.string(), strerror(-err), err);
   1327     }
   1328     return res;
   1329 }
   1330 
   1331 binder::Status CameraDeviceClient::prepare(int streamId) {
   1332     ATRACE_CALL();
   1333     ALOGV("%s", __FUNCTION__);
   1334 
   1335     binder::Status res;
   1336     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
   1337 
   1338     Mutex::Autolock icl(mBinderSerializationLock);
   1339 
   1340     // Guard against trying to prepare non-created streams
   1341     ssize_t index = NAME_NOT_FOUND;
   1342     for (size_t i = 0; i < mStreamMap.size(); ++i) {
   1343         if (streamId == mStreamMap.valueAt(i).streamId()) {
   1344             index = i;
   1345             break;
   1346         }
   1347     }
   1348 
   1349     if (index == NAME_NOT_FOUND) {
   1350         String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
   1351               "with that ID exists", mCameraIdStr.string(), streamId);
   1352         ALOGW("%s: %s", __FUNCTION__, msg.string());
   1353         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
   1354     }
   1355 
   1356     // Also returns BAD_VALUE if stream ID was not valid, or stream already
   1357     // has been used
   1358     status_t err = mDevice->prepare(streamId);
   1359     if (err == BAD_VALUE) {
   1360         res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
   1361                 "Camera %s: Stream %d has already been used, and cannot be prepared",
   1362                 mCameraIdStr.string(), streamId);
   1363     } else if (err != OK) {
   1364         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
   1365                 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
   1366                 strerror(-err), err);
   1367     }
   1368     return res;
   1369 }
   1370 
   1371 binder::Status CameraDeviceClient::prepare2(int maxCount, int streamId) {
   1372     ATRACE_CALL();
   1373     ALOGV("%s", __FUNCTION__);
   1374 
   1375     binder::Status res;
   1376     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
   1377 
   1378     Mutex::Autolock icl(mBinderSerializationLock);
   1379 
   1380     // Guard against trying to prepare non-created streams
   1381     ssize_t index = NAME_NOT_FOUND;
   1382     for (size_t i = 0; i < mStreamMap.size(); ++i) {
   1383         if (streamId == mStreamMap.valueAt(i).streamId()) {
   1384             index = i;
   1385             break;
   1386         }
   1387     }
   1388 
   1389     if (index == NAME_NOT_FOUND) {
   1390         String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
   1391               "with that ID exists", mCameraIdStr.string(), streamId);
   1392         ALOGW("%s: %s", __FUNCTION__, msg.string());
   1393         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
   1394     }
   1395 
   1396     if (maxCount <= 0) {
   1397         String8 msg = String8::format("Camera %s: maxCount (%d) must be greater than 0",
   1398                 mCameraIdStr.string(), maxCount);
   1399         ALOGE("%s: %s", __FUNCTION__, msg.string());
   1400         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
   1401     }
   1402 
   1403     // Also returns BAD_VALUE if stream ID was not valid, or stream already
   1404     // has been used
   1405     status_t err = mDevice->prepare(maxCount, streamId);
   1406     if (err == BAD_VALUE) {
   1407         res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
   1408                 "Camera %s: Stream %d has already been used, and cannot be prepared",
   1409                 mCameraIdStr.string(), streamId);
   1410     } else if (err != OK) {
   1411         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
   1412                 "Camera %s: Error preparing stream %d: %s (%d)", mCameraIdStr.string(), streamId,
   1413                 strerror(-err), err);
   1414     }
   1415 
   1416     return res;
   1417 }
   1418 
   1419 binder::Status CameraDeviceClient::tearDown(int streamId) {
   1420     ATRACE_CALL();
   1421     ALOGV("%s", __FUNCTION__);
   1422 
   1423     binder::Status res;
   1424     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
   1425 
   1426     Mutex::Autolock icl(mBinderSerializationLock);
   1427 
   1428     // Guard against trying to prepare non-created streams
   1429     ssize_t index = NAME_NOT_FOUND;
   1430     for (size_t i = 0; i < mStreamMap.size(); ++i) {
   1431         if (streamId == mStreamMap.valueAt(i).streamId()) {
   1432             index = i;
   1433             break;
   1434         }
   1435     }
   1436 
   1437     if (index == NAME_NOT_FOUND) {
   1438         String8 msg = String8::format("Camera %s: Invalid stream ID (%d) specified, no stream "
   1439               "with that ID exists", mCameraIdStr.string(), streamId);
   1440         ALOGW("%s: %s", __FUNCTION__, msg.string());
   1441         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
   1442     }
   1443 
   1444     // Also returns BAD_VALUE if stream ID was not valid or if the stream is in
   1445     // use
   1446     status_t err = mDevice->tearDown(streamId);
   1447     if (err == BAD_VALUE) {
   1448         res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
   1449                 "Camera %s: Stream %d is still in use, cannot be torn down",
   1450                 mCameraIdStr.string(), streamId);
   1451     } else if (err != OK) {
   1452         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
   1453                 "Camera %s: Error tearing down stream %d: %s (%d)", mCameraIdStr.string(), streamId,
   1454                 strerror(-err), err);
   1455     }
   1456 
   1457     return res;
   1458 }
   1459 
   1460 binder::Status CameraDeviceClient::finalizeOutputConfigurations(int32_t streamId,
   1461         const hardware::camera2::params::OutputConfiguration &outputConfiguration) {
   1462     ATRACE_CALL();
   1463 
   1464     binder::Status res;
   1465     if (!(res = checkPidStatus(__FUNCTION__)).isOk()) return res;
   1466 
   1467     Mutex::Autolock icl(mBinderSerializationLock);
   1468 
   1469     const std::vector<sp<IGraphicBufferProducer> >& bufferProducers =
   1470             outputConfiguration.getGraphicBufferProducers();
   1471 
   1472     if (bufferProducers.size() == 0) {
   1473         ALOGE("%s: bufferProducers must not be empty", __FUNCTION__);
   1474         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, "Target Surface is invalid");
   1475     }
   1476 
   1477     // streamId should be in mStreamMap if this stream already has a surface attached
   1478     // to it. Otherwise, it should be in mDeferredStreams.
   1479     bool streamIdConfigured = false;
   1480     ssize_t deferredStreamIndex = NAME_NOT_FOUND;
   1481     for (size_t i = 0; i < mStreamMap.size(); i++) {
   1482         if (mStreamMap.valueAt(i).streamId() == streamId) {
   1483             streamIdConfigured = true;
   1484             break;
   1485         }
   1486     }
   1487     for (size_t i = 0; i < mDeferredStreams.size(); i++) {
   1488         if (streamId == mDeferredStreams[i]) {
   1489             deferredStreamIndex = i;
   1490             break;
   1491         }
   1492 
   1493     }
   1494     if (deferredStreamIndex == NAME_NOT_FOUND && !streamIdConfigured) {
   1495         String8 msg = String8::format("Camera %s: deferred surface is set to a unknown stream"
   1496                 "(ID %d)", mCameraIdStr.string(), streamId);
   1497         ALOGW("%s: %s", __FUNCTION__, msg.string());
   1498         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
   1499     }
   1500 
   1501     if (mStreamInfoMap[streamId].finalized) {
   1502         String8 msg = String8::format("Camera %s: finalizeOutputConfigurations has been called"
   1503                 " on stream ID %d", mCameraIdStr.string(), streamId);
   1504         ALOGW("%s: %s", __FUNCTION__, msg.string());
   1505         return STATUS_ERROR(CameraService::ERROR_ILLEGAL_ARGUMENT, msg.string());
   1506     }
   1507 
   1508     if (!mDevice.get()) {
   1509         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED, "Camera device no longer alive");
   1510     }
   1511 
   1512     std::vector<sp<Surface>> consumerSurfaces;
   1513     for (auto& bufferProducer : bufferProducers) {
   1514         // Don't create multiple streams for the same target surface
   1515         ssize_t index = mStreamMap.indexOfKey(IInterface::asBinder(bufferProducer));
   1516         if (index != NAME_NOT_FOUND) {
   1517             ALOGV("Camera %s: Surface already has a stream created "
   1518                     " for it (ID %zd)", mCameraIdStr.string(), index);
   1519             continue;
   1520         }
   1521 
   1522         sp<Surface> surface;
   1523         res = createSurfaceFromGbp(mStreamInfoMap[streamId], true /*isStreamInfoValid*/,
   1524                 surface, bufferProducer);
   1525 
   1526         if (!res.isOk())
   1527             return res;
   1528 
   1529         consumerSurfaces.push_back(surface);
   1530     }
   1531 
   1532     // Gracefully handle case where finalizeOutputConfigurations is called
   1533     // without any new surface.
   1534     if (consumerSurfaces.size() == 0) {
   1535         mStreamInfoMap[streamId].finalized = true;
   1536         return res;
   1537     }
   1538 
   1539     // Finish the deferred stream configuration with the surface.
   1540     status_t err;
   1541     std::vector<int> consumerSurfaceIds;
   1542     err = mDevice->setConsumerSurfaces(streamId, consumerSurfaces, &consumerSurfaceIds);
   1543     if (err == OK) {
   1544         for (size_t i = 0; i < consumerSurfaces.size(); i++) {
   1545             sp<IBinder> binder = IInterface::asBinder(
   1546                     consumerSurfaces[i]->getIGraphicBufferProducer());
   1547             ALOGV("%s: mStreamMap add binder %p streamId %d, surfaceId %d", __FUNCTION__,
   1548                     binder.get(), streamId, consumerSurfaceIds[i]);
   1549             mStreamMap.add(binder, StreamSurfaceId(streamId, consumerSurfaceIds[i]));
   1550         }
   1551         if (deferredStreamIndex != NAME_NOT_FOUND) {
   1552             mDeferredStreams.removeItemsAt(deferredStreamIndex);
   1553         }
   1554         mStreamInfoMap[streamId].finalized = true;
   1555         mConfiguredOutputs.replaceValueFor(streamId, outputConfiguration);
   1556     } else if (err == NO_INIT) {
   1557         res = STATUS_ERROR_FMT(CameraService::ERROR_ILLEGAL_ARGUMENT,
   1558                 "Camera %s: Deferred surface is invalid: %s (%d)",
   1559                 mCameraIdStr.string(), strerror(-err), err);
   1560     } else {
   1561         res = STATUS_ERROR_FMT(CameraService::ERROR_INVALID_OPERATION,
   1562                 "Camera %s: Error setting output stream deferred surface: %s (%d)",
   1563                 mCameraIdStr.string(), strerror(-err), err);
   1564     }
   1565 
   1566     return res;
   1567 }
   1568 
   1569 status_t CameraDeviceClient::dump(int fd, const Vector<String16>& args) {
   1570     return BasicClient::dump(fd, args);
   1571 }
   1572 
   1573 status_t CameraDeviceClient::dumpClient(int fd, const Vector<String16>& args) {
   1574     dprintf(fd, "  CameraDeviceClient[%s] (%p) dump:\n",
   1575             mCameraIdStr.string(),
   1576             (getRemoteCallback() != NULL ?
   1577                     IInterface::asBinder(getRemoteCallback()).get() : NULL) );
   1578     dprintf(fd, "    Current client UID %u\n", mClientUid);
   1579 
   1580     dprintf(fd, "    State:\n");
   1581     dprintf(fd, "      Request ID counter: %d\n", mRequestIdCounter);
   1582     if (mInputStream.configured) {
   1583         dprintf(fd, "      Current input stream ID: %d\n", mInputStream.id);
   1584     } else {
   1585         dprintf(fd, "      No input stream configured.\n");
   1586     }
   1587     if (!mStreamMap.isEmpty()) {
   1588         dprintf(fd, "      Current output stream/surface IDs:\n");
   1589         for (size_t i = 0; i < mStreamMap.size(); i++) {
   1590             dprintf(fd, "        Stream %d Surface %d\n",
   1591                                 mStreamMap.valueAt(i).streamId(),
   1592                                 mStreamMap.valueAt(i).surfaceId());
   1593         }
   1594     } else if (!mDeferredStreams.isEmpty()) {
   1595         dprintf(fd, "      Current deferred surface output stream IDs:\n");
   1596         for (auto& streamId : mDeferredStreams) {
   1597             dprintf(fd, "        Stream %d\n", streamId);
   1598         }
   1599     } else {
   1600         dprintf(fd, "      No output streams configured.\n");
   1601     }
   1602     // TODO: print dynamic/request section from most recent requests
   1603     mFrameProcessor->dump(fd, args);
   1604 
   1605     return dumpDevice(fd, args);
   1606 }
   1607 
   1608 void CameraDeviceClient::notifyError(int32_t errorCode,
   1609                                      const CaptureResultExtras& resultExtras) {
   1610     // Thread safe. Don't bother locking.
   1611     sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
   1612 
   1613     if (remoteCb != 0) {
   1614         remoteCb->onDeviceError(errorCode, resultExtras);
   1615     }
   1616 }
   1617 
   1618 void CameraDeviceClient::notifyRepeatingRequestError(long lastFrameNumber) {
   1619     sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
   1620 
   1621     if (remoteCb != 0) {
   1622         remoteCb->onRepeatingRequestError(lastFrameNumber, mStreamingRequestId);
   1623     }
   1624 
   1625     Mutex::Autolock idLock(mStreamingRequestIdLock);
   1626     mStreamingRequestId = REQUEST_ID_NONE;
   1627 }
   1628 
   1629 void CameraDeviceClient::notifyIdle() {
   1630     // Thread safe. Don't bother locking.
   1631     sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
   1632 
   1633     if (remoteCb != 0) {
   1634         remoteCb->onDeviceIdle();
   1635     }
   1636     Camera2ClientBase::notifyIdle();
   1637 }
   1638 
   1639 void CameraDeviceClient::notifyShutter(const CaptureResultExtras& resultExtras,
   1640         nsecs_t timestamp) {
   1641     // Thread safe. Don't bother locking.
   1642     sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
   1643     if (remoteCb != 0) {
   1644         remoteCb->onCaptureStarted(resultExtras, timestamp);
   1645     }
   1646     Camera2ClientBase::notifyShutter(resultExtras, timestamp);
   1647 }
   1648 
   1649 void CameraDeviceClient::notifyPrepared(int streamId) {
   1650     // Thread safe. Don't bother locking.
   1651     sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
   1652     if (remoteCb != 0) {
   1653         remoteCb->onPrepared(streamId);
   1654     }
   1655 }
   1656 
   1657 void CameraDeviceClient::notifyRequestQueueEmpty() {
   1658     // Thread safe. Don't bother locking.
   1659     sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = getRemoteCallback();
   1660     if (remoteCb != 0) {
   1661         remoteCb->onRequestQueueEmpty();
   1662     }
   1663 }
   1664 
   1665 void CameraDeviceClient::detachDevice() {
   1666     if (mDevice == 0) return;
   1667 
   1668     ALOGV("Camera %s: Stopping processors", mCameraIdStr.string());
   1669 
   1670     mFrameProcessor->removeListener(FRAME_PROCESSOR_LISTENER_MIN_ID,
   1671                                     FRAME_PROCESSOR_LISTENER_MAX_ID,
   1672                                     /*listener*/this);
   1673     mFrameProcessor->requestExit();
   1674     ALOGV("Camera %s: Waiting for threads", mCameraIdStr.string());
   1675     mFrameProcessor->join();
   1676     ALOGV("Camera %s: Disconnecting device", mCameraIdStr.string());
   1677 
   1678     // WORKAROUND: HAL refuses to disconnect while there's streams in flight
   1679     {
   1680         mDevice->clearStreamingRequest();
   1681 
   1682         status_t code;
   1683         if ((code = mDevice->waitUntilDrained()) != OK) {
   1684             ALOGE("%s: waitUntilDrained failed with code 0x%x", __FUNCTION__,
   1685                   code);
   1686         }
   1687     }
   1688 
   1689     Camera2ClientBase::detachDevice();
   1690 }
   1691 
   1692 /** Device-related methods */
   1693 void CameraDeviceClient::onResultAvailable(const CaptureResult& result) {
   1694     ATRACE_CALL();
   1695     ALOGV("%s", __FUNCTION__);
   1696 
   1697     // Thread-safe. No lock necessary.
   1698     sp<hardware::camera2::ICameraDeviceCallbacks> remoteCb = mRemoteCallback;
   1699     if (remoteCb != NULL) {
   1700         remoteCb->onResultReceived(result.mMetadata, result.mResultExtras,
   1701                 result.mPhysicalMetadatas);
   1702     }
   1703 }
   1704 
   1705 binder::Status CameraDeviceClient::checkPidStatus(const char* checkLocation) {
   1706     if (mDisconnected) {
   1707         return STATUS_ERROR(CameraService::ERROR_DISCONNECTED,
   1708                 "The camera device has been disconnected");
   1709     }
   1710     status_t res = checkPid(checkLocation);
   1711     return (res == OK) ? binder::Status::ok() :
   1712             STATUS_ERROR(CameraService::ERROR_PERMISSION_DENIED,
   1713                     "Attempt to use camera from a different process than original client");
   1714 }
   1715 
   1716 // TODO: move to Camera2ClientBase
   1717 bool CameraDeviceClient::enforceRequestPermissions(CameraMetadata& metadata) {
   1718 
   1719     const int pid = IPCThreadState::self()->getCallingPid();
   1720     const int selfPid = getpid();
   1721     camera_metadata_entry_t entry;
   1722 
   1723     /**
   1724      * Mixin default important security values
   1725      * - android.led.transmit = defaulted ON
   1726      */
   1727     CameraMetadata staticInfo = mDevice->info();
   1728     entry = staticInfo.find(ANDROID_LED_AVAILABLE_LEDS);
   1729     for(size_t i = 0; i < entry.count; ++i) {
   1730         uint8_t led = entry.data.u8[i];
   1731 
   1732         switch(led) {
   1733             case ANDROID_LED_AVAILABLE_LEDS_TRANSMIT: {
   1734                 uint8_t transmitDefault = ANDROID_LED_TRANSMIT_ON;
   1735                 if (!metadata.exists(ANDROID_LED_TRANSMIT)) {
   1736                     metadata.update(ANDROID_LED_TRANSMIT,
   1737                                     &transmitDefault, 1);
   1738                 }
   1739                 break;
   1740             }
   1741         }
   1742     }
   1743 
   1744     // We can do anything!
   1745     if (pid == selfPid) {
   1746         return true;
   1747     }
   1748 
   1749     /**
   1750      * Permission check special fields in the request
   1751      * - android.led.transmit = android.permission.CAMERA_DISABLE_TRANSMIT
   1752      */
   1753     entry = metadata.find(ANDROID_LED_TRANSMIT);
   1754     if (entry.count > 0 && entry.data.u8[0] != ANDROID_LED_TRANSMIT_ON) {
   1755         String16 permissionString =
   1756             String16("android.permission.CAMERA_DISABLE_TRANSMIT_LED");
   1757         if (!checkCallingPermission(permissionString)) {
   1758             const int uid = IPCThreadState::self()->getCallingUid();
   1759             ALOGE("Permission Denial: "
   1760                   "can't disable transmit LED pid=%d, uid=%d", pid, uid);
   1761             return false;
   1762         }
   1763     }
   1764 
   1765     return true;
   1766 }
   1767 
   1768 status_t CameraDeviceClient::getRotationTransformLocked(int32_t* transform) {
   1769     ALOGV("%s: begin", __FUNCTION__);
   1770 
   1771     const CameraMetadata& staticInfo = mDevice->info();
   1772     return CameraUtils::getRotationTransform(staticInfo, transform);
   1773 }
   1774 
   1775 } // namespace android
   1776