Home | History | Annotate | Download | only in device3
      1 /*
      2  * Copyright (C) 2013 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 "Camera3-Device"
     18 #define ATRACE_TAG ATRACE_TAG_CAMERA
     19 //#define LOG_NDEBUG 0
     20 //#define LOG_NNDEBUG 0  // Per-frame verbose logging
     21 
     22 #ifdef LOG_NNDEBUG
     23 #define ALOGVV(...) ALOGV(__VA_ARGS__)
     24 #else
     25 #define ALOGVV(...) ((void)0)
     26 #endif
     27 
     28 // Convenience macro for transient errors
     29 #define CLOGE(fmt, ...) ALOGE("Camera %s: %s: " fmt, mId.string(), __FUNCTION__, \
     30             ##__VA_ARGS__)
     31 
     32 // Convenience macros for transitioning to the error state
     33 #define SET_ERR(fmt, ...) setErrorState(   \
     34     "%s: " fmt, __FUNCTION__,              \
     35     ##__VA_ARGS__)
     36 #define SET_ERR_L(fmt, ...) setErrorStateLocked( \
     37     "%s: " fmt, __FUNCTION__,                    \
     38     ##__VA_ARGS__)
     39 
     40 #include <inttypes.h>
     41 
     42 #include <utils/Log.h>
     43 #include <utils/Trace.h>
     44 #include <utils/Timers.h>
     45 #include <cutils/properties.h>
     46 
     47 #include <android/hardware/camera2/ICameraDeviceUser.h>
     48 
     49 #include "utils/CameraTraces.h"
     50 #include "mediautils/SchedulingPolicyService.h"
     51 #include "device3/Camera3Device.h"
     52 #include "device3/Camera3OutputStream.h"
     53 #include "device3/Camera3InputStream.h"
     54 #include "device3/Camera3DummyStream.h"
     55 #include "device3/Camera3SharedOutputStream.h"
     56 #include "CameraService.h"
     57 
     58 using namespace android::camera3;
     59 using namespace android::hardware::camera;
     60 using namespace android::hardware::camera::device::V3_2;
     61 
     62 namespace android {
     63 
     64 Camera3Device::Camera3Device(const String8 &id):
     65         mId(id),
     66         mOperatingMode(NO_MODE),
     67         mIsConstrainedHighSpeedConfiguration(false),
     68         mStatus(STATUS_UNINITIALIZED),
     69         mStatusWaiters(0),
     70         mUsePartialResult(false),
     71         mNumPartialResults(1),
     72         mTimestampOffset(0),
     73         mNextResultFrameNumber(0),
     74         mNextReprocessResultFrameNumber(0),
     75         mNextShutterFrameNumber(0),
     76         mNextReprocessShutterFrameNumber(0),
     77         mListener(NULL),
     78         mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID)
     79 {
     80     ATRACE_CALL();
     81     camera3_callback_ops::notify = &sNotify;
     82     camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
     83     ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
     84 }
     85 
     86 Camera3Device::~Camera3Device()
     87 {
     88     ATRACE_CALL();
     89     ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
     90     disconnect();
     91 }
     92 
     93 const String8& Camera3Device::getId() const {
     94     return mId;
     95 }
     96 
     97 status_t Camera3Device::initialize(sp<CameraProviderManager> manager) {
     98     ATRACE_CALL();
     99     Mutex::Autolock il(mInterfaceLock);
    100     Mutex::Autolock l(mLock);
    101 
    102     ALOGV("%s: Initializing HIDL device for camera %s", __FUNCTION__, mId.string());
    103     if (mStatus != STATUS_UNINITIALIZED) {
    104         CLOGE("Already initialized!");
    105         return INVALID_OPERATION;
    106     }
    107     if (manager == nullptr) return INVALID_OPERATION;
    108 
    109     sp<ICameraDeviceSession> session;
    110     ATRACE_BEGIN("CameraHal::openSession");
    111     status_t res = manager->openSession(mId.string(), this,
    112             /*out*/ &session);
    113     ATRACE_END();
    114     if (res != OK) {
    115         SET_ERR_L("Could not open camera session: %s (%d)", strerror(-res), res);
    116         return res;
    117     }
    118 
    119     res = manager->getCameraCharacteristics(mId.string(), &mDeviceInfo);
    120     if (res != OK) {
    121         SET_ERR_L("Could not retrive camera characteristics: %s (%d)", strerror(-res), res);
    122         session->close();
    123         return res;
    124     }
    125 
    126     std::shared_ptr<RequestMetadataQueue> queue;
    127     auto requestQueueRet = session->getCaptureRequestMetadataQueue(
    128         [&queue](const auto& descriptor) {
    129             queue = std::make_shared<RequestMetadataQueue>(descriptor);
    130             if (!queue->isValid() || queue->availableToWrite() <= 0) {
    131                 ALOGE("HAL returns empty request metadata fmq, not use it");
    132                 queue = nullptr;
    133                 // don't use the queue onwards.
    134             }
    135         });
    136     if (!requestQueueRet.isOk()) {
    137         ALOGE("Transaction error when getting request metadata fmq: %s, not use it",
    138                 requestQueueRet.description().c_str());
    139         return DEAD_OBJECT;
    140     }
    141 
    142     std::unique_ptr<ResultMetadataQueue>& resQueue = mResultMetadataQueue;
    143     auto resultQueueRet = session->getCaptureResultMetadataQueue(
    144         [&resQueue](const auto& descriptor) {
    145             resQueue = std::make_unique<ResultMetadataQueue>(descriptor);
    146             if (!resQueue->isValid() || resQueue->availableToWrite() <= 0) {
    147                 ALOGE("HAL returns empty result metadata fmq, not use it");
    148                 resQueue = nullptr;
    149                 // Don't use the resQueue onwards.
    150             }
    151         });
    152     if (!resultQueueRet.isOk()) {
    153         ALOGE("Transaction error when getting result metadata queue from camera session: %s",
    154                 resultQueueRet.description().c_str());
    155         return DEAD_OBJECT;
    156     }
    157     IF_ALOGV() {
    158         session->interfaceChain([](
    159             ::android::hardware::hidl_vec<::android::hardware::hidl_string> interfaceChain) {
    160                 ALOGV("Session interface chain:");
    161                 for (auto iface : interfaceChain) {
    162                     ALOGV("  %s", iface.c_str());
    163                 }
    164             });
    165     }
    166 
    167     mInterface = new HalInterface(session, queue);
    168     std::string providerType;
    169     mVendorTagId = manager->getProviderTagIdLocked(mId.string());
    170 
    171     return initializeCommonLocked();
    172 }
    173 
    174 status_t Camera3Device::initializeCommonLocked() {
    175 
    176     /** Start up status tracker thread */
    177     mStatusTracker = new StatusTracker(this);
    178     status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
    179     if (res != OK) {
    180         SET_ERR_L("Unable to start status tracking thread: %s (%d)",
    181                 strerror(-res), res);
    182         mInterface->close();
    183         mStatusTracker.clear();
    184         return res;
    185     }
    186 
    187     /** Register in-flight map to the status tracker */
    188     mInFlightStatusId = mStatusTracker->addComponent();
    189 
    190     /** Create buffer manager */
    191     mBufferManager = new Camera3BufferManager();
    192 
    193     mTagMonitor.initialize(mVendorTagId);
    194 
    195     /** Start up request queue thread */
    196     mRequestThread = new RequestThread(this, mStatusTracker, mInterface);
    197     res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
    198     if (res != OK) {
    199         SET_ERR_L("Unable to start request queue thread: %s (%d)",
    200                 strerror(-res), res);
    201         mInterface->close();
    202         mRequestThread.clear();
    203         return res;
    204     }
    205 
    206     mPreparerThread = new PreparerThread();
    207 
    208     internalUpdateStatusLocked(STATUS_UNCONFIGURED);
    209     mNextStreamId = 0;
    210     mDummyStreamId = NO_STREAM;
    211     mNeedConfig = true;
    212     mPauseStateNotify = false;
    213 
    214     // Measure the clock domain offset between camera and video/hw_composer
    215     camera_metadata_entry timestampSource =
    216             mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
    217     if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
    218             ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
    219         mTimestampOffset = getMonoToBoottimeOffset();
    220     }
    221 
    222     // Will the HAL be sending in early partial result metadata?
    223     camera_metadata_entry partialResultsCount =
    224             mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
    225     if (partialResultsCount.count > 0) {
    226         mNumPartialResults = partialResultsCount.data.i32[0];
    227         mUsePartialResult = (mNumPartialResults > 1);
    228     }
    229 
    230     camera_metadata_entry configs =
    231             mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
    232     for (uint32_t i = 0; i < configs.count; i += 4) {
    233         if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
    234                 configs.data.i32[i + 3] ==
    235                 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
    236             mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
    237                     configs.data.i32[i + 2]));
    238         }
    239     }
    240 
    241     return OK;
    242 }
    243 
    244 status_t Camera3Device::disconnect() {
    245     ATRACE_CALL();
    246     Mutex::Autolock il(mInterfaceLock);
    247 
    248     ALOGI("%s: E", __FUNCTION__);
    249 
    250     status_t res = OK;
    251     std::vector<wp<Camera3StreamInterface>> streams;
    252     nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
    253     {
    254         Mutex::Autolock l(mLock);
    255         if (mStatus == STATUS_UNINITIALIZED) return res;
    256 
    257         if (mStatus == STATUS_ACTIVE ||
    258                 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
    259             res = mRequestThread->clearRepeatingRequests();
    260             if (res != OK) {
    261                 SET_ERR_L("Can't stop streaming");
    262                 // Continue to close device even in case of error
    263             } else {
    264                 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
    265                 if (res != OK) {
    266                     SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
    267                             maxExpectedDuration);
    268                     // Continue to close device even in case of error
    269                 }
    270             }
    271         }
    272 
    273         if (mStatus == STATUS_ERROR) {
    274             CLOGE("Shutting down in an error state");
    275         }
    276 
    277         if (mStatusTracker != NULL) {
    278             mStatusTracker->requestExit();
    279         }
    280 
    281         if (mRequestThread != NULL) {
    282             mRequestThread->requestExit();
    283         }
    284 
    285         streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
    286         for (size_t i = 0; i < mOutputStreams.size(); i++) {
    287             streams.push_back(mOutputStreams[i]);
    288         }
    289         if (mInputStream != nullptr) {
    290             streams.push_back(mInputStream);
    291         }
    292     }
    293 
    294     // Joining done without holding mLock, otherwise deadlocks may ensue
    295     // as the threads try to access parent state
    296     if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
    297         // HAL may be in a bad state, so waiting for request thread
    298         // (which may be stuck in the HAL processCaptureRequest call)
    299         // could be dangerous.
    300         mRequestThread->join();
    301     }
    302 
    303     if (mStatusTracker != NULL) {
    304         mStatusTracker->join();
    305     }
    306 
    307     HalInterface* interface;
    308     {
    309         Mutex::Autolock l(mLock);
    310         mRequestThread.clear();
    311         mStatusTracker.clear();
    312         interface = mInterface.get();
    313     }
    314 
    315     // Call close without internal mutex held, as the HAL close may need to
    316     // wait on assorted callbacks,etc, to complete before it can return.
    317     interface->close();
    318 
    319     flushInflightRequests();
    320 
    321     {
    322         Mutex::Autolock l(mLock);
    323         mInterface->clear();
    324         mOutputStreams.clear();
    325         mInputStream.clear();
    326         mDeletedStreams.clear();
    327         mBufferManager.clear();
    328         internalUpdateStatusLocked(STATUS_UNINITIALIZED);
    329     }
    330 
    331     for (auto& weakStream : streams) {
    332         sp<Camera3StreamInterface> stream = weakStream.promote();
    333         if (stream != nullptr) {
    334             ALOGE("%s: Stream %d leaked! strong reference (%d)!",
    335                     __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
    336         }
    337     }
    338 
    339     ALOGI("%s: X", __FUNCTION__);
    340     return res;
    341 }
    342 
    343 // For dumping/debugging only -
    344 // try to acquire a lock a few times, eventually give up to proceed with
    345 // debug/dump operations
    346 bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
    347     bool gotLock = false;
    348     for (size_t i = 0; i < kDumpLockAttempts; ++i) {
    349         if (lock.tryLock() == NO_ERROR) {
    350             gotLock = true;
    351             break;
    352         } else {
    353             usleep(kDumpSleepDuration);
    354         }
    355     }
    356     return gotLock;
    357 }
    358 
    359 Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
    360     int32_t maxJpegWidth = 0, maxJpegHeight = 0;
    361     const int STREAM_CONFIGURATION_SIZE = 4;
    362     const int STREAM_FORMAT_OFFSET = 0;
    363     const int STREAM_WIDTH_OFFSET = 1;
    364     const int STREAM_HEIGHT_OFFSET = 2;
    365     const int STREAM_IS_INPUT_OFFSET = 3;
    366     camera_metadata_ro_entry_t availableStreamConfigs =
    367             mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
    368     if (availableStreamConfigs.count == 0 ||
    369             availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
    370         return Size(0, 0);
    371     }
    372 
    373     // Get max jpeg size (area-wise).
    374     for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
    375         int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
    376         int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
    377         int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
    378         int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
    379         if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
    380                 && format == HAL_PIXEL_FORMAT_BLOB &&
    381                 (width * height > maxJpegWidth * maxJpegHeight)) {
    382             maxJpegWidth = width;
    383             maxJpegHeight = height;
    384         }
    385     }
    386 
    387     return Size(maxJpegWidth, maxJpegHeight);
    388 }
    389 
    390 nsecs_t Camera3Device::getMonoToBoottimeOffset() {
    391     // try three times to get the clock offset, choose the one
    392     // with the minimum gap in measurements.
    393     const int tries = 3;
    394     nsecs_t bestGap, measured;
    395     for (int i = 0; i < tries; ++i) {
    396         const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
    397         const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
    398         const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
    399         const nsecs_t gap = tmono2 - tmono;
    400         if (i == 0 || gap < bestGap) {
    401             bestGap = gap;
    402             measured = tbase - ((tmono + tmono2) >> 1);
    403         }
    404     }
    405     return measured;
    406 }
    407 
    408 hardware::graphics::common::V1_0::PixelFormat Camera3Device::mapToPixelFormat(
    409         int frameworkFormat) {
    410     return (hardware::graphics::common::V1_0::PixelFormat) frameworkFormat;
    411 }
    412 
    413 DataspaceFlags Camera3Device::mapToHidlDataspace(
    414         android_dataspace dataSpace) {
    415     return dataSpace;
    416 }
    417 
    418 BufferUsageFlags Camera3Device::mapToConsumerUsage(
    419         uint64_t usage) {
    420     return usage;
    421 }
    422 
    423 StreamRotation Camera3Device::mapToStreamRotation(camera3_stream_rotation_t rotation) {
    424     switch (rotation) {
    425         case CAMERA3_STREAM_ROTATION_0:
    426             return StreamRotation::ROTATION_0;
    427         case CAMERA3_STREAM_ROTATION_90:
    428             return StreamRotation::ROTATION_90;
    429         case CAMERA3_STREAM_ROTATION_180:
    430             return StreamRotation::ROTATION_180;
    431         case CAMERA3_STREAM_ROTATION_270:
    432             return StreamRotation::ROTATION_270;
    433     }
    434     ALOGE("%s: Unknown stream rotation %d", __FUNCTION__, rotation);
    435     return StreamRotation::ROTATION_0;
    436 }
    437 
    438 status_t Camera3Device::mapToStreamConfigurationMode(
    439         camera3_stream_configuration_mode_t operationMode, StreamConfigurationMode *mode) {
    440     if (mode == nullptr) return BAD_VALUE;
    441     if (operationMode < CAMERA3_VENDOR_STREAM_CONFIGURATION_MODE_START) {
    442         switch(operationMode) {
    443             case CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE:
    444                 *mode = StreamConfigurationMode::NORMAL_MODE;
    445                 break;
    446             case CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE:
    447                 *mode = StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE;
    448                 break;
    449             default:
    450                 ALOGE("%s: Unknown stream configuration mode %d", __FUNCTION__, operationMode);
    451                 return BAD_VALUE;
    452         }
    453     } else {
    454         *mode = static_cast<StreamConfigurationMode>(operationMode);
    455     }
    456     return OK;
    457 }
    458 
    459 camera3_buffer_status_t Camera3Device::mapHidlBufferStatus(BufferStatus status) {
    460     switch (status) {
    461         case BufferStatus::OK: return CAMERA3_BUFFER_STATUS_OK;
    462         case BufferStatus::ERROR: return CAMERA3_BUFFER_STATUS_ERROR;
    463     }
    464     return CAMERA3_BUFFER_STATUS_ERROR;
    465 }
    466 
    467 int Camera3Device::mapToFrameworkFormat(
    468         hardware::graphics::common::V1_0::PixelFormat pixelFormat) {
    469     return static_cast<uint32_t>(pixelFormat);
    470 }
    471 
    472 android_dataspace Camera3Device::mapToFrameworkDataspace(
    473         DataspaceFlags dataSpace) {
    474     return static_cast<android_dataspace>(dataSpace);
    475 }
    476 
    477 uint64_t Camera3Device::mapConsumerToFrameworkUsage(
    478         BufferUsageFlags usage) {
    479     return usage;
    480 }
    481 
    482 uint64_t Camera3Device::mapProducerToFrameworkUsage(
    483         BufferUsageFlags usage) {
    484     return usage;
    485 }
    486 
    487 ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
    488     // Get max jpeg size (area-wise).
    489     Size maxJpegResolution = getMaxJpegResolution();
    490     if (maxJpegResolution.width == 0) {
    491         ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
    492                 __FUNCTION__, mId.string());
    493         return BAD_VALUE;
    494     }
    495 
    496     // Get max jpeg buffer size
    497     ssize_t maxJpegBufferSize = 0;
    498     camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
    499     if (jpegBufMaxSize.count == 0) {
    500         ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
    501                 mId.string());
    502         return BAD_VALUE;
    503     }
    504     maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
    505     assert(kMinJpegBufferSize < maxJpegBufferSize);
    506 
    507     // Calculate final jpeg buffer size for the given resolution.
    508     float scaleFactor = ((float) (width * height)) /
    509             (maxJpegResolution.width * maxJpegResolution.height);
    510     ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
    511             kMinJpegBufferSize;
    512     if (jpegBufferSize > maxJpegBufferSize) {
    513         jpegBufferSize = maxJpegBufferSize;
    514     }
    515 
    516     return jpegBufferSize;
    517 }
    518 
    519 ssize_t Camera3Device::getPointCloudBufferSize() const {
    520     const int FLOATS_PER_POINT=4;
    521     camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
    522     if (maxPointCount.count == 0) {
    523         ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
    524                 __FUNCTION__, mId.string());
    525         return BAD_VALUE;
    526     }
    527     ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
    528             maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
    529     return maxBytesForPointCloud;
    530 }
    531 
    532 ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
    533     const int PER_CONFIGURATION_SIZE = 3;
    534     const int WIDTH_OFFSET = 0;
    535     const int HEIGHT_OFFSET = 1;
    536     const int SIZE_OFFSET = 2;
    537     camera_metadata_ro_entry rawOpaqueSizes =
    538         mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
    539     size_t count = rawOpaqueSizes.count;
    540     if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
    541         ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
    542                 __FUNCTION__, mId.string(), count);
    543         return BAD_VALUE;
    544     }
    545 
    546     for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
    547         if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
    548                 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
    549             return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
    550         }
    551     }
    552 
    553     ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
    554             __FUNCTION__, mId.string(), width, height);
    555     return BAD_VALUE;
    556 }
    557 
    558 status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
    559     ATRACE_CALL();
    560     (void)args;
    561 
    562     // Try to lock, but continue in case of failure (to avoid blocking in
    563     // deadlocks)
    564     bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
    565     bool gotLock = tryLockSpinRightRound(mLock);
    566 
    567     ALOGW_IF(!gotInterfaceLock,
    568             "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
    569             mId.string(), __FUNCTION__);
    570     ALOGW_IF(!gotLock,
    571             "Camera %s: %s: Unable to lock main lock, proceeding anyway",
    572             mId.string(), __FUNCTION__);
    573 
    574     bool dumpTemplates = false;
    575 
    576     String16 templatesOption("-t");
    577     String16 monitorOption("-m");
    578     int n = args.size();
    579     for (int i = 0; i < n; i++) {
    580         if (args[i] == templatesOption) {
    581             dumpTemplates = true;
    582         }
    583         if (args[i] == monitorOption) {
    584             if (i + 1 < n) {
    585                 String8 monitorTags = String8(args[i + 1]);
    586                 if (monitorTags == "off") {
    587                     mTagMonitor.disableMonitoring();
    588                 } else {
    589                     mTagMonitor.parseTagsToMonitor(monitorTags);
    590                 }
    591             } else {
    592                 mTagMonitor.disableMonitoring();
    593             }
    594         }
    595     }
    596 
    597     String8 lines;
    598 
    599     const char *status =
    600             mStatus == STATUS_ERROR         ? "ERROR" :
    601             mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
    602             mStatus == STATUS_UNCONFIGURED  ? "UNCONFIGURED" :
    603             mStatus == STATUS_CONFIGURED    ? "CONFIGURED" :
    604             mStatus == STATUS_ACTIVE        ? "ACTIVE" :
    605             "Unknown";
    606 
    607     lines.appendFormat("    Device status: %s\n", status);
    608     if (mStatus == STATUS_ERROR) {
    609         lines.appendFormat("    Error cause: %s\n", mErrorCause.string());
    610     }
    611     lines.appendFormat("    Stream configuration:\n");
    612     const char *mode =
    613             mOperatingMode == static_cast<int>(StreamConfigurationMode::NORMAL_MODE) ? "NORMAL" :
    614             mOperatingMode == static_cast<int>(
    615                 StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ? "CONSTRAINED_HIGH_SPEED" :
    616             "CUSTOM";
    617     lines.appendFormat("    Operation mode: %s (%d) \n", mode, mOperatingMode);
    618 
    619     if (mInputStream != NULL) {
    620         write(fd, lines.string(), lines.size());
    621         mInputStream->dump(fd, args);
    622     } else {
    623         lines.appendFormat("      No input stream.\n");
    624         write(fd, lines.string(), lines.size());
    625     }
    626     for (size_t i = 0; i < mOutputStreams.size(); i++) {
    627         mOutputStreams[i]->dump(fd,args);
    628     }
    629 
    630     if (mBufferManager != NULL) {
    631         lines = String8("    Camera3 Buffer Manager:\n");
    632         write(fd, lines.string(), lines.size());
    633         mBufferManager->dump(fd, args);
    634     }
    635 
    636     lines = String8("    In-flight requests:\n");
    637     if (mInFlightMap.size() == 0) {
    638         lines.append("      None\n");
    639     } else {
    640         for (size_t i = 0; i < mInFlightMap.size(); i++) {
    641             InFlightRequest r = mInFlightMap.valueAt(i);
    642             lines.appendFormat("      Frame %d |  Timestamp: %" PRId64 ", metadata"
    643                     " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
    644                     r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
    645                     r.numBuffersLeft);
    646         }
    647     }
    648     write(fd, lines.string(), lines.size());
    649 
    650     if (mRequestThread != NULL) {
    651         mRequestThread->dumpCaptureRequestLatency(fd,
    652                 "    ProcessCaptureRequest latency histogram:");
    653     }
    654 
    655     {
    656         lines = String8("    Last request sent:\n");
    657         write(fd, lines.string(), lines.size());
    658 
    659         CameraMetadata lastRequest = getLatestRequestLocked();
    660         lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
    661     }
    662 
    663     if (dumpTemplates) {
    664         const char *templateNames[] = {
    665             "TEMPLATE_PREVIEW",
    666             "TEMPLATE_STILL_CAPTURE",
    667             "TEMPLATE_VIDEO_RECORD",
    668             "TEMPLATE_VIDEO_SNAPSHOT",
    669             "TEMPLATE_ZERO_SHUTTER_LAG",
    670             "TEMPLATE_MANUAL"
    671         };
    672 
    673         for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
    674             camera_metadata_t *templateRequest = nullptr;
    675             mInterface->constructDefaultRequestSettings(
    676                     (camera3_request_template_t) i, &templateRequest);
    677             lines = String8::format("    HAL Request %s:\n", templateNames[i-1]);
    678             if (templateRequest == nullptr) {
    679                 lines.append("       Not supported\n");
    680                 write(fd, lines.string(), lines.size());
    681             } else {
    682                 write(fd, lines.string(), lines.size());
    683                 dump_indented_camera_metadata(templateRequest,
    684                         fd, /*verbosity*/2, /*indentation*/8);
    685             }
    686             free_camera_metadata(templateRequest);
    687         }
    688     }
    689 
    690     mTagMonitor.dumpMonitoredMetadata(fd);
    691 
    692     if (mInterface->valid()) {
    693         lines = String8("     HAL device dump:\n");
    694         write(fd, lines.string(), lines.size());
    695         mInterface->dump(fd);
    696     }
    697 
    698     if (gotLock) mLock.unlock();
    699     if (gotInterfaceLock) mInterfaceLock.unlock();
    700 
    701     return OK;
    702 }
    703 
    704 const CameraMetadata& Camera3Device::info() const {
    705     ALOGVV("%s: E", __FUNCTION__);
    706     if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
    707                     mStatus == STATUS_ERROR)) {
    708         ALOGW("%s: Access to static info %s!", __FUNCTION__,
    709                 mStatus == STATUS_ERROR ?
    710                 "when in error state" : "before init");
    711     }
    712     return mDeviceInfo;
    713 }
    714 
    715 status_t Camera3Device::checkStatusOkToCaptureLocked() {
    716     switch (mStatus) {
    717         case STATUS_ERROR:
    718             CLOGE("Device has encountered a serious error");
    719             return INVALID_OPERATION;
    720         case STATUS_UNINITIALIZED:
    721             CLOGE("Device not initialized");
    722             return INVALID_OPERATION;
    723         case STATUS_UNCONFIGURED:
    724         case STATUS_CONFIGURED:
    725         case STATUS_ACTIVE:
    726             // OK
    727             break;
    728         default:
    729             SET_ERR_L("Unexpected status: %d", mStatus);
    730             return INVALID_OPERATION;
    731     }
    732     return OK;
    733 }
    734 
    735 status_t Camera3Device::convertMetadataListToRequestListLocked(
    736         const List<const CameraMetadata> &metadataList,
    737         const std::list<const SurfaceMap> &surfaceMaps,
    738         bool repeating,
    739         RequestList *requestList) {
    740     if (requestList == NULL) {
    741         CLOGE("requestList cannot be NULL.");
    742         return BAD_VALUE;
    743     }
    744 
    745     int32_t burstId = 0;
    746     List<const CameraMetadata>::const_iterator metadataIt = metadataList.begin();
    747     std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
    748     for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
    749             ++metadataIt, ++surfaceMapIt) {
    750         sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
    751         if (newRequest == 0) {
    752             CLOGE("Can't create capture request");
    753             return BAD_VALUE;
    754         }
    755 
    756         newRequest->mRepeating = repeating;
    757 
    758         // Setup burst Id and request Id
    759         newRequest->mResultExtras.burstId = burstId++;
    760         if (metadataIt->exists(ANDROID_REQUEST_ID)) {
    761             if (metadataIt->find(ANDROID_REQUEST_ID).count == 0) {
    762                 CLOGE("RequestID entry exists; but must not be empty in metadata");
    763                 return BAD_VALUE;
    764             }
    765             newRequest->mResultExtras.requestId = metadataIt->find(ANDROID_REQUEST_ID).data.i32[0];
    766         } else {
    767             CLOGE("RequestID does not exist in metadata");
    768             return BAD_VALUE;
    769         }
    770 
    771         requestList->push_back(newRequest);
    772 
    773         ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
    774     }
    775     if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
    776         ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
    777         return BAD_VALUE;
    778     }
    779 
    780     // Setup batch size if this is a high speed video recording request.
    781     if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
    782         auto firstRequest = requestList->begin();
    783         for (auto& outputStream : (*firstRequest)->mOutputStreams) {
    784             if (outputStream->isVideoStream()) {
    785                 (*firstRequest)->mBatchSize = requestList->size();
    786                 break;
    787             }
    788         }
    789     }
    790 
    791     return OK;
    792 }
    793 
    794 status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
    795     ATRACE_CALL();
    796 
    797     List<const CameraMetadata> requests;
    798     std::list<const SurfaceMap> surfaceMaps;
    799     convertToRequestList(requests, surfaceMaps, request);
    800 
    801     return captureList(requests, surfaceMaps, /*lastFrameNumber*/NULL);
    802 }
    803 
    804 void Camera3Device::convertToRequestList(List<const CameraMetadata>& requests,
    805         std::list<const SurfaceMap>& surfaceMaps,
    806         const CameraMetadata& request) {
    807     requests.push_back(request);
    808 
    809     SurfaceMap surfaceMap;
    810     camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
    811     // With no surface list passed in, stream and surface will have 1-to-1
    812     // mapping. So the surface index is 0 for each stream in the surfaceMap.
    813     for (size_t i = 0; i < streams.count; i++) {
    814         surfaceMap[streams.data.i32[i]].push_back(0);
    815     }
    816     surfaceMaps.push_back(surfaceMap);
    817 }
    818 
    819 status_t Camera3Device::submitRequestsHelper(
    820         const List<const CameraMetadata> &requests,
    821         const std::list<const SurfaceMap> &surfaceMaps,
    822         bool repeating,
    823         /*out*/
    824         int64_t *lastFrameNumber) {
    825     ATRACE_CALL();
    826     Mutex::Autolock il(mInterfaceLock);
    827     Mutex::Autolock l(mLock);
    828 
    829     status_t res = checkStatusOkToCaptureLocked();
    830     if (res != OK) {
    831         // error logged by previous call
    832         return res;
    833     }
    834 
    835     RequestList requestList;
    836 
    837     res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
    838             repeating, /*out*/&requestList);
    839     if (res != OK) {
    840         // error logged by previous call
    841         return res;
    842     }
    843 
    844     if (repeating) {
    845         res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
    846     } else {
    847         res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
    848     }
    849 
    850     if (res == OK) {
    851         waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
    852         if (res != OK) {
    853             SET_ERR_L("Can't transition to active in %f seconds!",
    854                     kActiveTimeout/1e9);
    855         }
    856         ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
    857               (*(requestList.begin()))->mResultExtras.requestId);
    858     } else {
    859         CLOGE("Cannot queue request. Impossible.");
    860         return BAD_VALUE;
    861     }
    862 
    863     return res;
    864 }
    865 
    866 // Only one processCaptureResult should be called at a time, so
    867 // the locks won't block. The locks are present here simply to enforce this.
    868 hardware::Return<void> Camera3Device::processCaptureResult(
    869         const hardware::hidl_vec<
    870                 hardware::camera::device::V3_2::CaptureResult>& results) {
    871     // Ideally we should grab mLock, but that can lead to deadlock, and
    872     // it's not super important to get up to date value of mStatus for this
    873     // warning print, hence skipping the lock here
    874     if (mStatus == STATUS_ERROR) {
    875         // Per API contract, HAL should act as closed after device error
    876         // But mStatus can be set to error by framework as well, so just log
    877         // a warning here.
    878         ALOGW("%s: received capture result in error state.", __FUNCTION__);
    879     }
    880 
    881     if (mProcessCaptureResultLock.tryLock() != OK) {
    882         // This should never happen; it indicates a wrong client implementation
    883         // that doesn't follow the contract. But, we can be tolerant here.
    884         ALOGE("%s: callback overlapped! waiting 1s...",
    885                 __FUNCTION__);
    886         if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
    887             ALOGE("%s: cannot acquire lock in 1s, dropping results",
    888                     __FUNCTION__);
    889             // really don't know what to do, so bail out.
    890             return hardware::Void();
    891         }
    892     }
    893     for (const auto& result : results) {
    894         processOneCaptureResultLocked(result);
    895     }
    896     mProcessCaptureResultLock.unlock();
    897     return hardware::Void();
    898 }
    899 
    900 void Camera3Device::processOneCaptureResultLocked(
    901         const hardware::camera::device::V3_2::CaptureResult& result) {
    902     camera3_capture_result r;
    903     status_t res;
    904     r.frame_number = result.frameNumber;
    905 
    906     hardware::camera::device::V3_2::CameraMetadata resultMetadata;
    907     if (result.fmqResultSize > 0) {
    908         resultMetadata.resize(result.fmqResultSize);
    909         if (mResultMetadataQueue == nullptr) {
    910             return; // logged in initialize()
    911         }
    912         if (!mResultMetadataQueue->read(resultMetadata.data(), result.fmqResultSize)) {
    913             ALOGE("%s: Frame %d: Cannot read camera metadata from fmq, size = %" PRIu64,
    914                     __FUNCTION__, result.frameNumber, result.fmqResultSize);
    915             return;
    916         }
    917     } else {
    918         resultMetadata.setToExternal(const_cast<uint8_t *>(result.result.data()),
    919                 result.result.size());
    920     }
    921 
    922     if (resultMetadata.size() != 0) {
    923         r.result = reinterpret_cast<const camera_metadata_t*>(resultMetadata.data());
    924         size_t expected_metadata_size = resultMetadata.size();
    925         if ((res = validate_camera_metadata_structure(r.result, &expected_metadata_size)) != OK) {
    926             ALOGE("%s: Frame %d: Invalid camera metadata received by camera service from HAL: %s (%d)",
    927                     __FUNCTION__, result.frameNumber, strerror(-res), res);
    928             return;
    929         }
    930     } else {
    931         r.result = nullptr;
    932     }
    933 
    934     std::vector<camera3_stream_buffer_t> outputBuffers(result.outputBuffers.size());
    935     std::vector<buffer_handle_t> outputBufferHandles(result.outputBuffers.size());
    936     for (size_t i = 0; i < result.outputBuffers.size(); i++) {
    937         auto& bDst = outputBuffers[i];
    938         const StreamBuffer &bSrc = result.outputBuffers[i];
    939 
    940         ssize_t idx = mOutputStreams.indexOfKey(bSrc.streamId);
    941         if (idx == NAME_NOT_FOUND) {
    942             ALOGE("%s: Frame %d: Buffer %zu: Invalid output stream id %d",
    943                     __FUNCTION__, result.frameNumber, i, bSrc.streamId);
    944             return;
    945         }
    946         bDst.stream = mOutputStreams.valueAt(idx)->asHalStream();
    947 
    948         buffer_handle_t *buffer;
    949         res = mInterface->popInflightBuffer(result.frameNumber, bSrc.streamId, &buffer);
    950         if (res != OK) {
    951             ALOGE("%s: Frame %d: Buffer %zu: No in-flight buffer for stream %d",
    952                     __FUNCTION__, result.frameNumber, i, bSrc.streamId);
    953             return;
    954         }
    955         bDst.buffer = buffer;
    956         bDst.status = mapHidlBufferStatus(bSrc.status);
    957         bDst.acquire_fence = -1;
    958         if (bSrc.releaseFence == nullptr) {
    959             bDst.release_fence = -1;
    960         } else if (bSrc.releaseFence->numFds == 1) {
    961             bDst.release_fence = dup(bSrc.releaseFence->data[0]);
    962         } else {
    963             ALOGE("%s: Frame %d: Invalid release fence for buffer %zu, fd count is %d, not 1",
    964                     __FUNCTION__, result.frameNumber, i, bSrc.releaseFence->numFds);
    965             return;
    966         }
    967     }
    968     r.num_output_buffers = outputBuffers.size();
    969     r.output_buffers = outputBuffers.data();
    970 
    971     camera3_stream_buffer_t inputBuffer;
    972     if (result.inputBuffer.streamId == -1) {
    973         r.input_buffer = nullptr;
    974     } else {
    975         if (mInputStream->getId() != result.inputBuffer.streamId) {
    976             ALOGE("%s: Frame %d: Invalid input stream id %d", __FUNCTION__,
    977                     result.frameNumber, result.inputBuffer.streamId);
    978             return;
    979         }
    980         inputBuffer.stream = mInputStream->asHalStream();
    981         buffer_handle_t *buffer;
    982         res = mInterface->popInflightBuffer(result.frameNumber, result.inputBuffer.streamId,
    983                 &buffer);
    984         if (res != OK) {
    985             ALOGE("%s: Frame %d: Input buffer: No in-flight buffer for stream %d",
    986                     __FUNCTION__, result.frameNumber, result.inputBuffer.streamId);
    987             return;
    988         }
    989         inputBuffer.buffer = buffer;
    990         inputBuffer.status = mapHidlBufferStatus(result.inputBuffer.status);
    991         inputBuffer.acquire_fence = -1;
    992         if (result.inputBuffer.releaseFence == nullptr) {
    993             inputBuffer.release_fence = -1;
    994         } else if (result.inputBuffer.releaseFence->numFds == 1) {
    995             inputBuffer.release_fence = dup(result.inputBuffer.releaseFence->data[0]);
    996         } else {
    997             ALOGE("%s: Frame %d: Invalid release fence for input buffer, fd count is %d, not 1",
    998                     __FUNCTION__, result.frameNumber, result.inputBuffer.releaseFence->numFds);
    999             return;
   1000         }
   1001         r.input_buffer = &inputBuffer;
   1002     }
   1003 
   1004     r.partial_result = result.partialResult;
   1005 
   1006     processCaptureResult(&r);
   1007 }
   1008 
   1009 hardware::Return<void> Camera3Device::notify(
   1010         const hardware::hidl_vec<hardware::camera::device::V3_2::NotifyMsg>& msgs) {
   1011     // Ideally we should grab mLock, but that can lead to deadlock, and
   1012     // it's not super important to get up to date value of mStatus for this
   1013     // warning print, hence skipping the lock here
   1014     if (mStatus == STATUS_ERROR) {
   1015         // Per API contract, HAL should act as closed after device error
   1016         // But mStatus can be set to error by framework as well, so just log
   1017         // a warning here.
   1018         ALOGW("%s: received notify message in error state.", __FUNCTION__);
   1019     }
   1020 
   1021     for (const auto& msg : msgs) {
   1022         notify(msg);
   1023     }
   1024     return hardware::Void();
   1025 }
   1026 
   1027 void Camera3Device::notify(
   1028         const hardware::camera::device::V3_2::NotifyMsg& msg) {
   1029 
   1030     camera3_notify_msg m;
   1031     switch (msg.type) {
   1032         case MsgType::ERROR:
   1033             m.type = CAMERA3_MSG_ERROR;
   1034             m.message.error.frame_number = msg.msg.error.frameNumber;
   1035             if (msg.msg.error.errorStreamId >= 0) {
   1036                 ssize_t idx = mOutputStreams.indexOfKey(msg.msg.error.errorStreamId);
   1037                 if (idx == NAME_NOT_FOUND) {
   1038                     ALOGE("%s: Frame %d: Invalid error stream id %d",
   1039                             __FUNCTION__, m.message.error.frame_number, msg.msg.error.errorStreamId);
   1040                     return;
   1041                 }
   1042                 m.message.error.error_stream = mOutputStreams.valueAt(idx)->asHalStream();
   1043             } else {
   1044                 m.message.error.error_stream = nullptr;
   1045             }
   1046             switch (msg.msg.error.errorCode) {
   1047                 case ErrorCode::ERROR_DEVICE:
   1048                     m.message.error.error_code = CAMERA3_MSG_ERROR_DEVICE;
   1049                     break;
   1050                 case ErrorCode::ERROR_REQUEST:
   1051                     m.message.error.error_code = CAMERA3_MSG_ERROR_REQUEST;
   1052                     break;
   1053                 case ErrorCode::ERROR_RESULT:
   1054                     m.message.error.error_code = CAMERA3_MSG_ERROR_RESULT;
   1055                     break;
   1056                 case ErrorCode::ERROR_BUFFER:
   1057                     m.message.error.error_code = CAMERA3_MSG_ERROR_BUFFER;
   1058                     break;
   1059             }
   1060             break;
   1061         case MsgType::SHUTTER:
   1062             m.type = CAMERA3_MSG_SHUTTER;
   1063             m.message.shutter.frame_number = msg.msg.shutter.frameNumber;
   1064             m.message.shutter.timestamp = msg.msg.shutter.timestamp;
   1065             break;
   1066     }
   1067     notify(&m);
   1068 }
   1069 
   1070 status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
   1071                                     const std::list<const SurfaceMap> &surfaceMaps,
   1072                                     int64_t *lastFrameNumber) {
   1073     ATRACE_CALL();
   1074 
   1075     return submitRequestsHelper(requests, surfaceMaps, /*repeating*/false, lastFrameNumber);
   1076 }
   1077 
   1078 status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
   1079                                             int64_t* /*lastFrameNumber*/) {
   1080     ATRACE_CALL();
   1081 
   1082     List<const CameraMetadata> requests;
   1083     std::list<const SurfaceMap> surfaceMaps;
   1084     convertToRequestList(requests, surfaceMaps, request);
   1085 
   1086     return setStreamingRequestList(requests, /*surfaceMap*/surfaceMaps,
   1087                                    /*lastFrameNumber*/NULL);
   1088 }
   1089 
   1090 status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
   1091                                                 const std::list<const SurfaceMap> &surfaceMaps,
   1092                                                 int64_t *lastFrameNumber) {
   1093     ATRACE_CALL();
   1094 
   1095     return submitRequestsHelper(requests, surfaceMaps, /*repeating*/true, lastFrameNumber);
   1096 }
   1097 
   1098 sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
   1099         const CameraMetadata &request, const SurfaceMap &surfaceMap) {
   1100     status_t res;
   1101 
   1102     if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
   1103         // This point should only be reached via API1 (API2 must explicitly call configureStreams)
   1104         // so unilaterally select normal operating mode.
   1105         res = configureStreamsLocked(CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE);
   1106         // Stream configuration failed. Client might try other configuraitons.
   1107         if (res != OK) {
   1108             CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
   1109             return NULL;
   1110         } else if (mStatus == STATUS_UNCONFIGURED) {
   1111             // Stream configuration successfully configure to empty stream configuration.
   1112             CLOGE("No streams configured");
   1113             return NULL;
   1114         }
   1115     }
   1116 
   1117     sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
   1118     return newRequest;
   1119 }
   1120 
   1121 status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
   1122     ATRACE_CALL();
   1123     Mutex::Autolock il(mInterfaceLock);
   1124     Mutex::Autolock l(mLock);
   1125 
   1126     switch (mStatus) {
   1127         case STATUS_ERROR:
   1128             CLOGE("Device has encountered a serious error");
   1129             return INVALID_OPERATION;
   1130         case STATUS_UNINITIALIZED:
   1131             CLOGE("Device not initialized");
   1132             return INVALID_OPERATION;
   1133         case STATUS_UNCONFIGURED:
   1134         case STATUS_CONFIGURED:
   1135         case STATUS_ACTIVE:
   1136             // OK
   1137             break;
   1138         default:
   1139             SET_ERR_L("Unexpected status: %d", mStatus);
   1140             return INVALID_OPERATION;
   1141     }
   1142     ALOGV("Camera %s: Clearing repeating request", mId.string());
   1143 
   1144     return mRequestThread->clearRepeatingRequests(lastFrameNumber);
   1145 }
   1146 
   1147 status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
   1148     ATRACE_CALL();
   1149     Mutex::Autolock il(mInterfaceLock);
   1150 
   1151     return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
   1152 }
   1153 
   1154 status_t Camera3Device::createInputStream(
   1155         uint32_t width, uint32_t height, int format, int *id) {
   1156     ATRACE_CALL();
   1157     Mutex::Autolock il(mInterfaceLock);
   1158     nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
   1159     Mutex::Autolock l(mLock);
   1160     ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
   1161             mId.string(), mNextStreamId, width, height, format);
   1162 
   1163     status_t res;
   1164     bool wasActive = false;
   1165 
   1166     switch (mStatus) {
   1167         case STATUS_ERROR:
   1168             ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
   1169             return INVALID_OPERATION;
   1170         case STATUS_UNINITIALIZED:
   1171             ALOGE("%s: Device not initialized", __FUNCTION__);
   1172             return INVALID_OPERATION;
   1173         case STATUS_UNCONFIGURED:
   1174         case STATUS_CONFIGURED:
   1175             // OK
   1176             break;
   1177         case STATUS_ACTIVE:
   1178             ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
   1179             res = internalPauseAndWaitLocked(maxExpectedDuration);
   1180             if (res != OK) {
   1181                 SET_ERR_L("Can't pause captures to reconfigure streams!");
   1182                 return res;
   1183             }
   1184             wasActive = true;
   1185             break;
   1186         default:
   1187             SET_ERR_L("%s: Unexpected status: %d", mStatus);
   1188             return INVALID_OPERATION;
   1189     }
   1190     assert(mStatus != STATUS_ACTIVE);
   1191 
   1192     if (mInputStream != 0) {
   1193         ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
   1194         return INVALID_OPERATION;
   1195     }
   1196 
   1197     sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
   1198                 width, height, format);
   1199     newStream->setStatusTracker(mStatusTracker);
   1200 
   1201     mInputStream = newStream;
   1202 
   1203     *id = mNextStreamId++;
   1204 
   1205     // Continue captures if active at start
   1206     if (wasActive) {
   1207         ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
   1208         // Reuse current operating mode for new stream config
   1209         res = configureStreamsLocked(mOperatingMode);
   1210         if (res != OK) {
   1211             ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
   1212                     __FUNCTION__, mNextStreamId, strerror(-res), res);
   1213             return res;
   1214         }
   1215         internalResumeLocked();
   1216     }
   1217 
   1218     ALOGV("Camera %s: Created input stream", mId.string());
   1219     return OK;
   1220 }
   1221 
   1222 status_t Camera3Device::createStream(sp<Surface> consumer,
   1223             uint32_t width, uint32_t height, int format,
   1224             android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
   1225             int streamSetId, bool isShared, uint64_t consumerUsage) {
   1226     ATRACE_CALL();
   1227 
   1228     if (consumer == nullptr) {
   1229         ALOGE("%s: consumer must not be null", __FUNCTION__);
   1230         return BAD_VALUE;
   1231     }
   1232 
   1233     std::vector<sp<Surface>> consumers;
   1234     consumers.push_back(consumer);
   1235 
   1236     return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
   1237             format, dataSpace, rotation, id, streamSetId, isShared, consumerUsage);
   1238 }
   1239 
   1240 status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
   1241         bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
   1242         android_dataspace dataSpace, camera3_stream_rotation_t rotation, int *id,
   1243         int streamSetId, bool isShared, uint64_t consumerUsage) {
   1244     ATRACE_CALL();
   1245     Mutex::Autolock il(mInterfaceLock);
   1246     nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
   1247     Mutex::Autolock l(mLock);
   1248     ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
   1249             " consumer usage %" PRIu64 ", isShared %d", mId.string(), mNextStreamId, width, height, format,
   1250             dataSpace, rotation, consumerUsage, isShared);
   1251 
   1252     status_t res;
   1253     bool wasActive = false;
   1254 
   1255     switch (mStatus) {
   1256         case STATUS_ERROR:
   1257             CLOGE("Device has encountered a serious error");
   1258             return INVALID_OPERATION;
   1259         case STATUS_UNINITIALIZED:
   1260             CLOGE("Device not initialized");
   1261             return INVALID_OPERATION;
   1262         case STATUS_UNCONFIGURED:
   1263         case STATUS_CONFIGURED:
   1264             // OK
   1265             break;
   1266         case STATUS_ACTIVE:
   1267             ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
   1268             res = internalPauseAndWaitLocked(maxExpectedDuration);
   1269             if (res != OK) {
   1270                 SET_ERR_L("Can't pause captures to reconfigure streams!");
   1271                 return res;
   1272             }
   1273             wasActive = true;
   1274             break;
   1275         default:
   1276             SET_ERR_L("Unexpected status: %d", mStatus);
   1277             return INVALID_OPERATION;
   1278     }
   1279     assert(mStatus != STATUS_ACTIVE);
   1280 
   1281     sp<Camera3OutputStream> newStream;
   1282 
   1283     if (consumers.size() == 0 && !hasDeferredConsumer) {
   1284         ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
   1285         return BAD_VALUE;
   1286     }
   1287 
   1288     if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
   1289         ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
   1290         return BAD_VALUE;
   1291     }
   1292 
   1293     if (format == HAL_PIXEL_FORMAT_BLOB) {
   1294         ssize_t blobBufferSize;
   1295         if (dataSpace != HAL_DATASPACE_DEPTH) {
   1296             blobBufferSize = getJpegBufferSize(width, height);
   1297             if (blobBufferSize <= 0) {
   1298                 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
   1299                 return BAD_VALUE;
   1300             }
   1301         } else {
   1302             blobBufferSize = getPointCloudBufferSize();
   1303             if (blobBufferSize <= 0) {
   1304                 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
   1305                 return BAD_VALUE;
   1306             }
   1307         }
   1308         newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
   1309                 width, height, blobBufferSize, format, dataSpace, rotation,
   1310                 mTimestampOffset, streamSetId);
   1311     } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
   1312         ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
   1313         if (rawOpaqueBufferSize <= 0) {
   1314             SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
   1315             return BAD_VALUE;
   1316         }
   1317         newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
   1318                 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
   1319                 mTimestampOffset, streamSetId);
   1320     } else if (isShared) {
   1321         newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
   1322                 width, height, format, consumerUsage, dataSpace, rotation,
   1323                 mTimestampOffset, streamSetId);
   1324     } else if (consumers.size() == 0 && hasDeferredConsumer) {
   1325         newStream = new Camera3OutputStream(mNextStreamId,
   1326                 width, height, format, consumerUsage, dataSpace, rotation,
   1327                 mTimestampOffset, streamSetId);
   1328     } else {
   1329         newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
   1330                 width, height, format, dataSpace, rotation,
   1331                 mTimestampOffset, streamSetId);
   1332     }
   1333     newStream->setStatusTracker(mStatusTracker);
   1334 
   1335     newStream->setBufferManager(mBufferManager);
   1336 
   1337     res = mOutputStreams.add(mNextStreamId, newStream);
   1338     if (res < 0) {
   1339         SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
   1340         return res;
   1341     }
   1342 
   1343     *id = mNextStreamId++;
   1344     mNeedConfig = true;
   1345 
   1346     // Continue captures if active at start
   1347     if (wasActive) {
   1348         ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
   1349         // Reuse current operating mode for new stream config
   1350         res = configureStreamsLocked(mOperatingMode);
   1351         if (res != OK) {
   1352             CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
   1353                     mNextStreamId, strerror(-res), res);
   1354             return res;
   1355         }
   1356         internalResumeLocked();
   1357     }
   1358     ALOGV("Camera %s: Created new stream", mId.string());
   1359     return OK;
   1360 }
   1361 
   1362 status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
   1363     ATRACE_CALL();
   1364     if (nullptr == streamInfo) {
   1365         return BAD_VALUE;
   1366     }
   1367     Mutex::Autolock il(mInterfaceLock);
   1368     Mutex::Autolock l(mLock);
   1369 
   1370     switch (mStatus) {
   1371         case STATUS_ERROR:
   1372             CLOGE("Device has encountered a serious error");
   1373             return INVALID_OPERATION;
   1374         case STATUS_UNINITIALIZED:
   1375             CLOGE("Device not initialized!");
   1376             return INVALID_OPERATION;
   1377         case STATUS_UNCONFIGURED:
   1378         case STATUS_CONFIGURED:
   1379         case STATUS_ACTIVE:
   1380             // OK
   1381             break;
   1382         default:
   1383             SET_ERR_L("Unexpected status: %d", mStatus);
   1384             return INVALID_OPERATION;
   1385     }
   1386 
   1387     ssize_t idx = mOutputStreams.indexOfKey(id);
   1388     if (idx == NAME_NOT_FOUND) {
   1389         CLOGE("Stream %d is unknown", id);
   1390         return idx;
   1391     }
   1392 
   1393     streamInfo->width  = mOutputStreams[idx]->getWidth();
   1394     streamInfo->height = mOutputStreams[idx]->getHeight();
   1395     streamInfo->format = mOutputStreams[idx]->getFormat();
   1396     streamInfo->dataSpace = mOutputStreams[idx]->getDataSpace();
   1397     streamInfo->formatOverridden = mOutputStreams[idx]->isFormatOverridden();
   1398     streamInfo->originalFormat = mOutputStreams[idx]->getOriginalFormat();
   1399     streamInfo->dataSpaceOverridden = mOutputStreams[idx]->isDataSpaceOverridden();
   1400     streamInfo->originalDataSpace = mOutputStreams[idx]->getOriginalDataSpace();
   1401     return OK;
   1402 }
   1403 
   1404 status_t Camera3Device::setStreamTransform(int id,
   1405         int transform) {
   1406     ATRACE_CALL();
   1407     Mutex::Autolock il(mInterfaceLock);
   1408     Mutex::Autolock l(mLock);
   1409 
   1410     switch (mStatus) {
   1411         case STATUS_ERROR:
   1412             CLOGE("Device has encountered a serious error");
   1413             return INVALID_OPERATION;
   1414         case STATUS_UNINITIALIZED:
   1415             CLOGE("Device not initialized");
   1416             return INVALID_OPERATION;
   1417         case STATUS_UNCONFIGURED:
   1418         case STATUS_CONFIGURED:
   1419         case STATUS_ACTIVE:
   1420             // OK
   1421             break;
   1422         default:
   1423             SET_ERR_L("Unexpected status: %d", mStatus);
   1424             return INVALID_OPERATION;
   1425     }
   1426 
   1427     ssize_t idx = mOutputStreams.indexOfKey(id);
   1428     if (idx == NAME_NOT_FOUND) {
   1429         CLOGE("Stream %d does not exist",
   1430                 id);
   1431         return BAD_VALUE;
   1432     }
   1433 
   1434     return mOutputStreams.editValueAt(idx)->setTransform(transform);
   1435 }
   1436 
   1437 status_t Camera3Device::deleteStream(int id) {
   1438     ATRACE_CALL();
   1439     Mutex::Autolock il(mInterfaceLock);
   1440     Mutex::Autolock l(mLock);
   1441     status_t res;
   1442 
   1443     ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
   1444 
   1445     // CameraDevice semantics require device to already be idle before
   1446     // deleteStream is called, unlike for createStream.
   1447     if (mStatus == STATUS_ACTIVE) {
   1448         ALOGV("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
   1449         return -EBUSY;
   1450     }
   1451 
   1452     if (mStatus == STATUS_ERROR) {
   1453         ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
   1454                 __FUNCTION__, mId.string());
   1455         return -EBUSY;
   1456     }
   1457 
   1458     sp<Camera3StreamInterface> deletedStream;
   1459     ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
   1460     if (mInputStream != NULL && id == mInputStream->getId()) {
   1461         deletedStream = mInputStream;
   1462         mInputStream.clear();
   1463     } else {
   1464         if (outputStreamIdx == NAME_NOT_FOUND) {
   1465             CLOGE("Stream %d does not exist", id);
   1466             return BAD_VALUE;
   1467         }
   1468     }
   1469 
   1470     // Delete output stream or the output part of a bi-directional stream.
   1471     if (outputStreamIdx != NAME_NOT_FOUND) {
   1472         deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
   1473         mOutputStreams.removeItem(id);
   1474     }
   1475 
   1476     // Free up the stream endpoint so that it can be used by some other stream
   1477     res = deletedStream->disconnect();
   1478     if (res != OK) {
   1479         SET_ERR_L("Can't disconnect deleted stream %d", id);
   1480         // fall through since we want to still list the stream as deleted.
   1481     }
   1482     mDeletedStreams.add(deletedStream);
   1483     mNeedConfig = true;
   1484 
   1485     return res;
   1486 }
   1487 
   1488 status_t Camera3Device::configureStreams(int operatingMode) {
   1489     ATRACE_CALL();
   1490     ALOGV("%s: E", __FUNCTION__);
   1491 
   1492     Mutex::Autolock il(mInterfaceLock);
   1493     Mutex::Autolock l(mLock);
   1494 
   1495     return configureStreamsLocked(operatingMode);
   1496 }
   1497 
   1498 status_t Camera3Device::getInputBufferProducer(
   1499         sp<IGraphicBufferProducer> *producer) {
   1500     ATRACE_CALL();
   1501     Mutex::Autolock il(mInterfaceLock);
   1502     Mutex::Autolock l(mLock);
   1503 
   1504     if (producer == NULL) {
   1505         return BAD_VALUE;
   1506     } else if (mInputStream == NULL) {
   1507         return INVALID_OPERATION;
   1508     }
   1509 
   1510     return mInputStream->getInputBufferProducer(producer);
   1511 }
   1512 
   1513 status_t Camera3Device::createDefaultRequest(int templateId,
   1514         CameraMetadata *request) {
   1515     ATRACE_CALL();
   1516     ALOGV("%s: for template %d", __FUNCTION__, templateId);
   1517 
   1518     if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
   1519         android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
   1520                 IPCThreadState::self()->getCallingUid(), nullptr, 0);
   1521         return BAD_VALUE;
   1522     }
   1523 
   1524     Mutex::Autolock il(mInterfaceLock);
   1525 
   1526     {
   1527         Mutex::Autolock l(mLock);
   1528         switch (mStatus) {
   1529             case STATUS_ERROR:
   1530                 CLOGE("Device has encountered a serious error");
   1531                 return INVALID_OPERATION;
   1532             case STATUS_UNINITIALIZED:
   1533                 CLOGE("Device is not initialized!");
   1534                 return INVALID_OPERATION;
   1535             case STATUS_UNCONFIGURED:
   1536             case STATUS_CONFIGURED:
   1537             case STATUS_ACTIVE:
   1538                 // OK
   1539                 break;
   1540             default:
   1541                 SET_ERR_L("Unexpected status: %d", mStatus);
   1542                 return INVALID_OPERATION;
   1543         }
   1544 
   1545         if (!mRequestTemplateCache[templateId].isEmpty()) {
   1546             *request = mRequestTemplateCache[templateId];
   1547             return OK;
   1548         }
   1549     }
   1550 
   1551     camera_metadata_t *rawRequest;
   1552     status_t res = mInterface->constructDefaultRequestSettings(
   1553             (camera3_request_template_t) templateId, &rawRequest);
   1554 
   1555     {
   1556         Mutex::Autolock l(mLock);
   1557         if (res == BAD_VALUE) {
   1558             ALOGI("%s: template %d is not supported on this camera device",
   1559                   __FUNCTION__, templateId);
   1560             return res;
   1561         } else if (res != OK) {
   1562             CLOGE("Unable to construct request template %d: %s (%d)",
   1563                     templateId, strerror(-res), res);
   1564             return res;
   1565         }
   1566 
   1567         set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
   1568         mRequestTemplateCache[templateId].acquire(rawRequest);
   1569 
   1570         *request = mRequestTemplateCache[templateId];
   1571     }
   1572     return OK;
   1573 }
   1574 
   1575 status_t Camera3Device::waitUntilDrained() {
   1576     ATRACE_CALL();
   1577     Mutex::Autolock il(mInterfaceLock);
   1578     nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
   1579     Mutex::Autolock l(mLock);
   1580 
   1581     return waitUntilDrainedLocked(maxExpectedDuration);
   1582 }
   1583 
   1584 status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
   1585     switch (mStatus) {
   1586         case STATUS_UNINITIALIZED:
   1587         case STATUS_UNCONFIGURED:
   1588             ALOGV("%s: Already idle", __FUNCTION__);
   1589             return OK;
   1590         case STATUS_CONFIGURED:
   1591             // To avoid race conditions, check with tracker to be sure
   1592         case STATUS_ERROR:
   1593         case STATUS_ACTIVE:
   1594             // Need to verify shut down
   1595             break;
   1596         default:
   1597             SET_ERR_L("Unexpected status: %d",mStatus);
   1598             return INVALID_OPERATION;
   1599     }
   1600     ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
   1601             maxExpectedDuration);
   1602     status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
   1603     if (res != OK) {
   1604         SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
   1605                 res);
   1606     }
   1607     return res;
   1608 }
   1609 
   1610 
   1611 void Camera3Device::internalUpdateStatusLocked(Status status) {
   1612     mStatus = status;
   1613     mRecentStatusUpdates.add(mStatus);
   1614     mStatusChanged.broadcast();
   1615 }
   1616 
   1617 // Pause to reconfigure
   1618 status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
   1619     mRequestThread->setPaused(true);
   1620     mPauseStateNotify = true;
   1621 
   1622     ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
   1623           maxExpectedDuration);
   1624     status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
   1625     if (res != OK) {
   1626         SET_ERR_L("Can't idle device in %f seconds!",
   1627                 maxExpectedDuration/1e9);
   1628     }
   1629 
   1630     return res;
   1631 }
   1632 
   1633 // Resume after internalPauseAndWaitLocked
   1634 status_t Camera3Device::internalResumeLocked() {
   1635     status_t res;
   1636 
   1637     mRequestThread->setPaused(false);
   1638 
   1639     res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
   1640     if (res != OK) {
   1641         SET_ERR_L("Can't transition to active in %f seconds!",
   1642                 kActiveTimeout/1e9);
   1643     }
   1644     mPauseStateNotify = false;
   1645     return OK;
   1646 }
   1647 
   1648 status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
   1649     status_t res = OK;
   1650 
   1651     size_t startIndex = 0;
   1652     if (mStatusWaiters == 0) {
   1653         // Clear the list of recent statuses if there are no existing threads waiting on updates to
   1654         // this status list
   1655         mRecentStatusUpdates.clear();
   1656     } else {
   1657         // If other threads are waiting on updates to this status list, set the position of the
   1658         // first element that this list will check rather than clearing the list.
   1659         startIndex = mRecentStatusUpdates.size();
   1660     }
   1661 
   1662     mStatusWaiters++;
   1663 
   1664     bool stateSeen = false;
   1665     do {
   1666         if (active == (mStatus == STATUS_ACTIVE)) {
   1667             // Desired state is current
   1668             break;
   1669         }
   1670 
   1671         res = mStatusChanged.waitRelative(mLock, timeout);
   1672         if (res != OK) break;
   1673 
   1674         // This is impossible, but if not, could result in subtle deadlocks and invalid state
   1675         // transitions.
   1676         LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
   1677                 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
   1678                 __FUNCTION__);
   1679 
   1680         // Encountered desired state since we began waiting
   1681         for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
   1682             if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
   1683                 stateSeen = true;
   1684                 break;
   1685             }
   1686         }
   1687     } while (!stateSeen);
   1688 
   1689     mStatusWaiters--;
   1690 
   1691     return res;
   1692 }
   1693 
   1694 
   1695 status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
   1696     ATRACE_CALL();
   1697     Mutex::Autolock l(mOutputLock);
   1698 
   1699     if (listener != NULL && mListener != NULL) {
   1700         ALOGW("%s: Replacing old callback listener", __FUNCTION__);
   1701     }
   1702     mListener = listener;
   1703     mRequestThread->setNotificationListener(listener);
   1704     mPreparerThread->setNotificationListener(listener);
   1705 
   1706     return OK;
   1707 }
   1708 
   1709 bool Camera3Device::willNotify3A() {
   1710     return false;
   1711 }
   1712 
   1713 status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
   1714     ATRACE_CALL();
   1715     status_t res;
   1716     Mutex::Autolock l(mOutputLock);
   1717 
   1718     while (mResultQueue.empty()) {
   1719         res = mResultSignal.waitRelative(mOutputLock, timeout);
   1720         if (res == TIMED_OUT) {
   1721             return res;
   1722         } else if (res != OK) {
   1723             ALOGW("%s: Camera %s: No frame in %" PRId64 " ns: %s (%d)",
   1724                     __FUNCTION__, mId.string(), timeout, strerror(-res), res);
   1725             return res;
   1726         }
   1727     }
   1728     return OK;
   1729 }
   1730 
   1731 status_t Camera3Device::getNextResult(CaptureResult *frame) {
   1732     ATRACE_CALL();
   1733     Mutex::Autolock l(mOutputLock);
   1734 
   1735     if (mResultQueue.empty()) {
   1736         return NOT_ENOUGH_DATA;
   1737     }
   1738 
   1739     if (frame == NULL) {
   1740         ALOGE("%s: argument cannot be NULL", __FUNCTION__);
   1741         return BAD_VALUE;
   1742     }
   1743 
   1744     CaptureResult &result = *(mResultQueue.begin());
   1745     frame->mResultExtras = result.mResultExtras;
   1746     frame->mMetadata.acquire(result.mMetadata);
   1747     mResultQueue.erase(mResultQueue.begin());
   1748 
   1749     return OK;
   1750 }
   1751 
   1752 status_t Camera3Device::triggerAutofocus(uint32_t id) {
   1753     ATRACE_CALL();
   1754     Mutex::Autolock il(mInterfaceLock);
   1755 
   1756     ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
   1757     // Mix-in this trigger into the next request and only the next request.
   1758     RequestTrigger trigger[] = {
   1759         {
   1760             ANDROID_CONTROL_AF_TRIGGER,
   1761             ANDROID_CONTROL_AF_TRIGGER_START
   1762         },
   1763         {
   1764             ANDROID_CONTROL_AF_TRIGGER_ID,
   1765             static_cast<int32_t>(id)
   1766         }
   1767     };
   1768 
   1769     return mRequestThread->queueTrigger(trigger,
   1770                                         sizeof(trigger)/sizeof(trigger[0]));
   1771 }
   1772 
   1773 status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
   1774     ATRACE_CALL();
   1775     Mutex::Autolock il(mInterfaceLock);
   1776 
   1777     ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
   1778     // Mix-in this trigger into the next request and only the next request.
   1779     RequestTrigger trigger[] = {
   1780         {
   1781             ANDROID_CONTROL_AF_TRIGGER,
   1782             ANDROID_CONTROL_AF_TRIGGER_CANCEL
   1783         },
   1784         {
   1785             ANDROID_CONTROL_AF_TRIGGER_ID,
   1786             static_cast<int32_t>(id)
   1787         }
   1788     };
   1789 
   1790     return mRequestThread->queueTrigger(trigger,
   1791                                         sizeof(trigger)/sizeof(trigger[0]));
   1792 }
   1793 
   1794 status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
   1795     ATRACE_CALL();
   1796     Mutex::Autolock il(mInterfaceLock);
   1797 
   1798     ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
   1799     // Mix-in this trigger into the next request and only the next request.
   1800     RequestTrigger trigger[] = {
   1801         {
   1802             ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
   1803             ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
   1804         },
   1805         {
   1806             ANDROID_CONTROL_AE_PRECAPTURE_ID,
   1807             static_cast<int32_t>(id)
   1808         }
   1809     };
   1810 
   1811     return mRequestThread->queueTrigger(trigger,
   1812                                         sizeof(trigger)/sizeof(trigger[0]));
   1813 }
   1814 
   1815 status_t Camera3Device::flush(int64_t *frameNumber) {
   1816     ATRACE_CALL();
   1817     ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
   1818     Mutex::Autolock il(mInterfaceLock);
   1819 
   1820     {
   1821         Mutex::Autolock l(mLock);
   1822         mRequestThread->clear(/*out*/frameNumber);
   1823     }
   1824 
   1825     return mRequestThread->flush();
   1826 }
   1827 
   1828 status_t Camera3Device::prepare(int streamId) {
   1829     return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
   1830 }
   1831 
   1832 status_t Camera3Device::prepare(int maxCount, int streamId) {
   1833     ATRACE_CALL();
   1834     ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
   1835     Mutex::Autolock il(mInterfaceLock);
   1836     Mutex::Autolock l(mLock);
   1837 
   1838     sp<Camera3StreamInterface> stream;
   1839     ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
   1840     if (outputStreamIdx == NAME_NOT_FOUND) {
   1841         CLOGE("Stream %d does not exist", streamId);
   1842         return BAD_VALUE;
   1843     }
   1844 
   1845     stream = mOutputStreams.editValueAt(outputStreamIdx);
   1846 
   1847     if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
   1848         CLOGE("Stream %d has already been a request target", streamId);
   1849         return BAD_VALUE;
   1850     }
   1851 
   1852     if (mRequestThread->isStreamPending(stream)) {
   1853         CLOGE("Stream %d is already a target in a pending request", streamId);
   1854         return BAD_VALUE;
   1855     }
   1856 
   1857     return mPreparerThread->prepare(maxCount, stream);
   1858 }
   1859 
   1860 status_t Camera3Device::tearDown(int streamId) {
   1861     ATRACE_CALL();
   1862     ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
   1863     Mutex::Autolock il(mInterfaceLock);
   1864     Mutex::Autolock l(mLock);
   1865 
   1866     sp<Camera3StreamInterface> stream;
   1867     ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
   1868     if (outputStreamIdx == NAME_NOT_FOUND) {
   1869         CLOGE("Stream %d does not exist", streamId);
   1870         return BAD_VALUE;
   1871     }
   1872 
   1873     stream = mOutputStreams.editValueAt(outputStreamIdx);
   1874 
   1875     if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
   1876         CLOGE("Stream %d is a target of a in-progress request", streamId);
   1877         return BAD_VALUE;
   1878     }
   1879 
   1880     return stream->tearDown();
   1881 }
   1882 
   1883 status_t Camera3Device::addBufferListenerForStream(int streamId,
   1884         wp<Camera3StreamBufferListener> listener) {
   1885     ATRACE_CALL();
   1886     ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
   1887     Mutex::Autolock il(mInterfaceLock);
   1888     Mutex::Autolock l(mLock);
   1889 
   1890     sp<Camera3StreamInterface> stream;
   1891     ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
   1892     if (outputStreamIdx == NAME_NOT_FOUND) {
   1893         CLOGE("Stream %d does not exist", streamId);
   1894         return BAD_VALUE;
   1895     }
   1896 
   1897     stream = mOutputStreams.editValueAt(outputStreamIdx);
   1898     stream->addBufferListener(listener);
   1899 
   1900     return OK;
   1901 }
   1902 
   1903 /**
   1904  * Methods called by subclasses
   1905  */
   1906 
   1907 void Camera3Device::notifyStatus(bool idle) {
   1908     ATRACE_CALL();
   1909     {
   1910         // Need mLock to safely update state and synchronize to current
   1911         // state of methods in flight.
   1912         Mutex::Autolock l(mLock);
   1913         // We can get various system-idle notices from the status tracker
   1914         // while starting up. Only care about them if we've actually sent
   1915         // in some requests recently.
   1916         if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
   1917             return;
   1918         }
   1919         ALOGV("%s: Camera %s: Now %s", __FUNCTION__, mId.string(),
   1920                 idle ? "idle" : "active");
   1921         internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
   1922 
   1923         // Skip notifying listener if we're doing some user-transparent
   1924         // state changes
   1925         if (mPauseStateNotify) return;
   1926     }
   1927 
   1928     sp<NotificationListener> listener;
   1929     {
   1930         Mutex::Autolock l(mOutputLock);
   1931         listener = mListener.promote();
   1932     }
   1933     if (idle && listener != NULL) {
   1934         listener->notifyIdle();
   1935     }
   1936 }
   1937 
   1938 status_t Camera3Device::setConsumerSurfaces(int streamId,
   1939         const std::vector<sp<Surface>>& consumers) {
   1940     ATRACE_CALL();
   1941     ALOGV("%s: Camera %s: set consumer surface for stream %d",
   1942             __FUNCTION__, mId.string(), streamId);
   1943     Mutex::Autolock il(mInterfaceLock);
   1944     Mutex::Autolock l(mLock);
   1945 
   1946     if (consumers.size() == 0) {
   1947         CLOGE("No consumer is passed!");
   1948         return BAD_VALUE;
   1949     }
   1950 
   1951     ssize_t idx = mOutputStreams.indexOfKey(streamId);
   1952     if (idx == NAME_NOT_FOUND) {
   1953         CLOGE("Stream %d is unknown", streamId);
   1954         return idx;
   1955     }
   1956     sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
   1957     status_t res = stream->setConsumers(consumers);
   1958     if (res != OK) {
   1959         CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
   1960         return res;
   1961     }
   1962 
   1963     if (stream->isConsumerConfigurationDeferred()) {
   1964         if (!stream->isConfiguring()) {
   1965             CLOGE("Stream %d was already fully configured.", streamId);
   1966             return INVALID_OPERATION;
   1967         }
   1968 
   1969         res = stream->finishConfiguration();
   1970         if (res != OK) {
   1971             SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
   1972                    stream->getId(), strerror(-res), res);
   1973             return res;
   1974         }
   1975     }
   1976 
   1977     return OK;
   1978 }
   1979 
   1980 /**
   1981  * Camera3Device private methods
   1982  */
   1983 
   1984 sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
   1985         const CameraMetadata &request, const SurfaceMap &surfaceMap) {
   1986     ATRACE_CALL();
   1987     status_t res;
   1988 
   1989     sp<CaptureRequest> newRequest = new CaptureRequest;
   1990     newRequest->mSettings = request;
   1991 
   1992     camera_metadata_entry_t inputStreams =
   1993             newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
   1994     if (inputStreams.count > 0) {
   1995         if (mInputStream == NULL ||
   1996                 mInputStream->getId() != inputStreams.data.i32[0]) {
   1997             CLOGE("Request references unknown input stream %d",
   1998                     inputStreams.data.u8[0]);
   1999             return NULL;
   2000         }
   2001         // Lazy completion of stream configuration (allocation/registration)
   2002         // on first use
   2003         if (mInputStream->isConfiguring()) {
   2004             res = mInputStream->finishConfiguration();
   2005             if (res != OK) {
   2006                 SET_ERR_L("Unable to finish configuring input stream %d:"
   2007                         " %s (%d)",
   2008                         mInputStream->getId(), strerror(-res), res);
   2009                 return NULL;
   2010             }
   2011         }
   2012         // Check if stream is being prepared
   2013         if (mInputStream->isPreparing()) {
   2014             CLOGE("Request references an input stream that's being prepared!");
   2015             return NULL;
   2016         }
   2017 
   2018         newRequest->mInputStream = mInputStream;
   2019         newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
   2020     }
   2021 
   2022     camera_metadata_entry_t streams =
   2023             newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
   2024     if (streams.count == 0) {
   2025         CLOGE("Zero output streams specified!");
   2026         return NULL;
   2027     }
   2028 
   2029     for (size_t i = 0; i < streams.count; i++) {
   2030         int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
   2031         if (idx == NAME_NOT_FOUND) {
   2032             CLOGE("Request references unknown stream %d",
   2033                     streams.data.u8[i]);
   2034             return NULL;
   2035         }
   2036         sp<Camera3OutputStreamInterface> stream =
   2037                 mOutputStreams.editValueAt(idx);
   2038 
   2039         // It is illegal to include a deferred consumer output stream into a request
   2040         auto iter = surfaceMap.find(streams.data.i32[i]);
   2041         if (iter != surfaceMap.end()) {
   2042             const std::vector<size_t>& surfaces = iter->second;
   2043             for (const auto& surface : surfaces) {
   2044                 if (stream->isConsumerConfigurationDeferred(surface)) {
   2045                     CLOGE("Stream %d surface %zu hasn't finished configuration yet "
   2046                           "due to deferred consumer", stream->getId(), surface);
   2047                     return NULL;
   2048                 }
   2049             }
   2050             newRequest->mOutputSurfaces[i] = surfaces;
   2051         }
   2052 
   2053         // Lazy completion of stream configuration (allocation/registration)
   2054         // on first use
   2055         if (stream->isConfiguring()) {
   2056             res = stream->finishConfiguration();
   2057             if (res != OK) {
   2058                 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
   2059                         stream->getId(), strerror(-res), res);
   2060                 return NULL;
   2061             }
   2062         }
   2063         // Check if stream is being prepared
   2064         if (stream->isPreparing()) {
   2065             CLOGE("Request references an output stream that's being prepared!");
   2066             return NULL;
   2067         }
   2068 
   2069         newRequest->mOutputStreams.push(stream);
   2070     }
   2071     newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
   2072     newRequest->mBatchSize = 1;
   2073 
   2074     return newRequest;
   2075 }
   2076 
   2077 bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
   2078     for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
   2079         Size size = mSupportedOpaqueInputSizes[i];
   2080         if (size.width == width && size.height == height) {
   2081             return true;
   2082         }
   2083     }
   2084 
   2085     return false;
   2086 }
   2087 
   2088 void Camera3Device::cancelStreamsConfigurationLocked() {
   2089     int res = OK;
   2090     if (mInputStream != NULL && mInputStream->isConfiguring()) {
   2091         res = mInputStream->cancelConfiguration();
   2092         if (res != OK) {
   2093             CLOGE("Can't cancel configuring input stream %d: %s (%d)",
   2094                     mInputStream->getId(), strerror(-res), res);
   2095         }
   2096     }
   2097 
   2098     for (size_t i = 0; i < mOutputStreams.size(); i++) {
   2099         sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i);
   2100         if (outputStream->isConfiguring()) {
   2101             res = outputStream->cancelConfiguration();
   2102             if (res != OK) {
   2103                 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
   2104                         outputStream->getId(), strerror(-res), res);
   2105             }
   2106         }
   2107     }
   2108 
   2109     // Return state to that at start of call, so that future configures
   2110     // properly clean things up
   2111     internalUpdateStatusLocked(STATUS_UNCONFIGURED);
   2112     mNeedConfig = true;
   2113 }
   2114 
   2115 status_t Camera3Device::configureStreamsLocked(int operatingMode) {
   2116     ATRACE_CALL();
   2117     status_t res;
   2118 
   2119     if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
   2120         CLOGE("Not idle");
   2121         return INVALID_OPERATION;
   2122     }
   2123 
   2124     if (operatingMode < 0) {
   2125         CLOGE("Invalid operating mode: %d", operatingMode);
   2126         return BAD_VALUE;
   2127     }
   2128 
   2129     bool isConstrainedHighSpeed =
   2130             static_cast<int>(StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ==
   2131             operatingMode;
   2132 
   2133     if (mOperatingMode != operatingMode) {
   2134         mNeedConfig = true;
   2135         mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
   2136         mOperatingMode = operatingMode;
   2137     }
   2138 
   2139     if (!mNeedConfig) {
   2140         ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
   2141         return OK;
   2142     }
   2143 
   2144     // Workaround for device HALv3.2 or older spec bug - zero streams requires
   2145     // adding a dummy stream instead.
   2146     // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
   2147     if (mOutputStreams.size() == 0) {
   2148         addDummyStreamLocked();
   2149     } else {
   2150         tryRemoveDummyStreamLocked();
   2151     }
   2152 
   2153     // Start configuring the streams
   2154     ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
   2155 
   2156     camera3_stream_configuration config;
   2157     config.operation_mode = mOperatingMode;
   2158     config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
   2159 
   2160     Vector<camera3_stream_t*> streams;
   2161     streams.setCapacity(config.num_streams);
   2162 
   2163     if (mInputStream != NULL) {
   2164         camera3_stream_t *inputStream;
   2165         inputStream = mInputStream->startConfiguration();
   2166         if (inputStream == NULL) {
   2167             CLOGE("Can't start input stream configuration");
   2168             cancelStreamsConfigurationLocked();
   2169             return INVALID_OPERATION;
   2170         }
   2171         streams.add(inputStream);
   2172     }
   2173 
   2174     for (size_t i = 0; i < mOutputStreams.size(); i++) {
   2175 
   2176         // Don't configure bidi streams twice, nor add them twice to the list
   2177         if (mOutputStreams[i].get() ==
   2178             static_cast<Camera3StreamInterface*>(mInputStream.get())) {
   2179 
   2180             config.num_streams--;
   2181             continue;
   2182         }
   2183 
   2184         camera3_stream_t *outputStream;
   2185         outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
   2186         if (outputStream == NULL) {
   2187             CLOGE("Can't start output stream configuration");
   2188             cancelStreamsConfigurationLocked();
   2189             return INVALID_OPERATION;
   2190         }
   2191         streams.add(outputStream);
   2192     }
   2193 
   2194     config.streams = streams.editArray();
   2195 
   2196     // Do the HAL configuration; will potentially touch stream
   2197     // max_buffers, usage, priv fields.
   2198 
   2199     res = mInterface->configureStreams(&config);
   2200 
   2201     if (res == BAD_VALUE) {
   2202         // HAL rejected this set of streams as unsupported, clean up config
   2203         // attempt and return to unconfigured state
   2204         CLOGE("Set of requested inputs/outputs not supported by HAL");
   2205         cancelStreamsConfigurationLocked();
   2206         return BAD_VALUE;
   2207     } else if (res != OK) {
   2208         // Some other kind of error from configure_streams - this is not
   2209         // expected
   2210         SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
   2211                 strerror(-res), res);
   2212         return res;
   2213     }
   2214 
   2215     // Finish all stream configuration immediately.
   2216     // TODO: Try to relax this later back to lazy completion, which should be
   2217     // faster
   2218 
   2219     if (mInputStream != NULL && mInputStream->isConfiguring()) {
   2220         res = mInputStream->finishConfiguration();
   2221         if (res != OK) {
   2222             CLOGE("Can't finish configuring input stream %d: %s (%d)",
   2223                     mInputStream->getId(), strerror(-res), res);
   2224             cancelStreamsConfigurationLocked();
   2225             return BAD_VALUE;
   2226         }
   2227     }
   2228 
   2229     for (size_t i = 0; i < mOutputStreams.size(); i++) {
   2230         sp<Camera3OutputStreamInterface> outputStream =
   2231             mOutputStreams.editValueAt(i);
   2232         if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
   2233             res = outputStream->finishConfiguration();
   2234             if (res != OK) {
   2235                 CLOGE("Can't finish configuring output stream %d: %s (%d)",
   2236                         outputStream->getId(), strerror(-res), res);
   2237                 cancelStreamsConfigurationLocked();
   2238                 return BAD_VALUE;
   2239             }
   2240         }
   2241     }
   2242 
   2243     // Request thread needs to know to avoid using repeat-last-settings protocol
   2244     // across configure_streams() calls
   2245     mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration);
   2246 
   2247     char value[PROPERTY_VALUE_MAX];
   2248     property_get("camera.fifo.disable", value, "0");
   2249     int32_t disableFifo = atoi(value);
   2250     if (disableFifo != 1) {
   2251         // Boost priority of request thread to SCHED_FIFO.
   2252         pid_t requestThreadTid = mRequestThread->getTid();
   2253         res = requestPriority(getpid(), requestThreadTid,
   2254                 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
   2255         if (res != OK) {
   2256             ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
   2257                     strerror(-res), res);
   2258         } else {
   2259             ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
   2260         }
   2261     }
   2262 
   2263     // Update device state
   2264 
   2265     mNeedConfig = false;
   2266 
   2267     internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
   2268             STATUS_CONFIGURED : STATUS_UNCONFIGURED);
   2269 
   2270     ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
   2271 
   2272     // tear down the deleted streams after configure streams.
   2273     mDeletedStreams.clear();
   2274 
   2275     return OK;
   2276 }
   2277 
   2278 status_t Camera3Device::addDummyStreamLocked() {
   2279     ATRACE_CALL();
   2280     status_t res;
   2281 
   2282     if (mDummyStreamId != NO_STREAM) {
   2283         // Should never be adding a second dummy stream when one is already
   2284         // active
   2285         SET_ERR_L("%s: Camera %s: A dummy stream already exists!",
   2286                 __FUNCTION__, mId.string());
   2287         return INVALID_OPERATION;
   2288     }
   2289 
   2290     ALOGV("%s: Camera %s: Adding a dummy stream", __FUNCTION__, mId.string());
   2291 
   2292     sp<Camera3OutputStreamInterface> dummyStream =
   2293             new Camera3DummyStream(mNextStreamId);
   2294 
   2295     res = mOutputStreams.add(mNextStreamId, dummyStream);
   2296     if (res < 0) {
   2297         SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
   2298         return res;
   2299     }
   2300 
   2301     mDummyStreamId = mNextStreamId;
   2302     mNextStreamId++;
   2303 
   2304     return OK;
   2305 }
   2306 
   2307 status_t Camera3Device::tryRemoveDummyStreamLocked() {
   2308     ATRACE_CALL();
   2309     status_t res;
   2310 
   2311     if (mDummyStreamId == NO_STREAM) return OK;
   2312     if (mOutputStreams.size() == 1) return OK;
   2313 
   2314     ALOGV("%s: Camera %s: Removing the dummy stream", __FUNCTION__, mId.string());
   2315 
   2316     // Ok, have a dummy stream and there's at least one other output stream,
   2317     // so remove the dummy
   2318 
   2319     sp<Camera3StreamInterface> deletedStream;
   2320     ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
   2321     if (outputStreamIdx == NAME_NOT_FOUND) {
   2322         SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
   2323         return INVALID_OPERATION;
   2324     }
   2325 
   2326     deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
   2327     mOutputStreams.removeItemsAt(outputStreamIdx);
   2328 
   2329     // Free up the stream endpoint so that it can be used by some other stream
   2330     res = deletedStream->disconnect();
   2331     if (res != OK) {
   2332         SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
   2333         // fall through since we want to still list the stream as deleted.
   2334     }
   2335     mDeletedStreams.add(deletedStream);
   2336     mDummyStreamId = NO_STREAM;
   2337 
   2338     return res;
   2339 }
   2340 
   2341 void Camera3Device::setErrorState(const char *fmt, ...) {
   2342     ATRACE_CALL();
   2343     Mutex::Autolock l(mLock);
   2344     va_list args;
   2345     va_start(args, fmt);
   2346 
   2347     setErrorStateLockedV(fmt, args);
   2348 
   2349     va_end(args);
   2350 }
   2351 
   2352 void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
   2353     ATRACE_CALL();
   2354     Mutex::Autolock l(mLock);
   2355     setErrorStateLockedV(fmt, args);
   2356 }
   2357 
   2358 void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
   2359     va_list args;
   2360     va_start(args, fmt);
   2361 
   2362     setErrorStateLockedV(fmt, args);
   2363 
   2364     va_end(args);
   2365 }
   2366 
   2367 void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
   2368     // Print out all error messages to log
   2369     String8 errorCause = String8::formatV(fmt, args);
   2370     ALOGE("Camera %s: %s", mId.string(), errorCause.string());
   2371 
   2372     // But only do error state transition steps for the first error
   2373     if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
   2374 
   2375     mErrorCause = errorCause;
   2376 
   2377     if (mRequestThread != nullptr) {
   2378         mRequestThread->setPaused(true);
   2379     }
   2380     internalUpdateStatusLocked(STATUS_ERROR);
   2381 
   2382     // Notify upstream about a device error
   2383     sp<NotificationListener> listener = mListener.promote();
   2384     if (listener != NULL) {
   2385         listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
   2386                 CaptureResultExtras());
   2387     }
   2388 
   2389     // Save stack trace. View by dumping it later.
   2390     CameraTraces::saveTrace();
   2391     // TODO: consider adding errorCause and client pid/procname
   2392 }
   2393 
   2394 /**
   2395  * In-flight request management
   2396  */
   2397 
   2398 status_t Camera3Device::registerInFlight(uint32_t frameNumber,
   2399         int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
   2400         bool hasAppCallback, nsecs_t maxExpectedDuration) {
   2401     ATRACE_CALL();
   2402     Mutex::Autolock l(mInFlightLock);
   2403 
   2404     ssize_t res;
   2405     res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
   2406             hasAppCallback, maxExpectedDuration));
   2407     if (res < 0) return res;
   2408 
   2409     if (mInFlightMap.size() == 1) {
   2410         // hold mLock to prevent race with disconnect
   2411         Mutex::Autolock l(mLock);
   2412         if (mStatusTracker != nullptr) {
   2413             mStatusTracker->markComponentActive(mInFlightStatusId);
   2414         }
   2415     }
   2416 
   2417     mExpectedInflightDuration += maxExpectedDuration;
   2418     return OK;
   2419 }
   2420 
   2421 void Camera3Device::returnOutputBuffers(
   2422         const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
   2423         nsecs_t timestamp) {
   2424     for (size_t i = 0; i < numBuffers; i++)
   2425     {
   2426         Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
   2427         status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
   2428         // Note: stream may be deallocated at this point, if this buffer was
   2429         // the last reference to it.
   2430         if (res != OK) {
   2431             ALOGE("Can't return buffer to its stream: %s (%d)",
   2432                 strerror(-res), res);
   2433         }
   2434     }
   2435 }
   2436 
   2437 void Camera3Device::removeInFlightMapEntryLocked(int idx) {
   2438     ATRACE_CALL();
   2439     nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
   2440     mInFlightMap.removeItemsAt(idx, 1);
   2441 
   2442     // Indicate idle inFlightMap to the status tracker
   2443     if (mInFlightMap.size() == 0) {
   2444         // hold mLock to prevent race with disconnect
   2445         Mutex::Autolock l(mLock);
   2446         if (mStatusTracker != nullptr) {
   2447             mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
   2448         }
   2449     }
   2450     mExpectedInflightDuration -= duration;
   2451 }
   2452 
   2453 void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
   2454 
   2455     const InFlightRequest &request = mInFlightMap.valueAt(idx);
   2456     const uint32_t frameNumber = mInFlightMap.keyAt(idx);
   2457 
   2458     nsecs_t sensorTimestamp = request.sensorTimestamp;
   2459     nsecs_t shutterTimestamp = request.shutterTimestamp;
   2460 
   2461     // Check if it's okay to remove the request from InFlightMap:
   2462     // In the case of a successful request:
   2463     //      all input and output buffers, all result metadata, shutter callback
   2464     //      arrived.
   2465     // In the case of a unsuccessful request:
   2466     //      all input and output buffers arrived.
   2467     if (request.numBuffersLeft == 0 &&
   2468             (request.skipResultMetadata ||
   2469             (request.haveResultMetadata && shutterTimestamp != 0))) {
   2470         ATRACE_ASYNC_END("frame capture", frameNumber);
   2471 
   2472         // Sanity check - if sensor timestamp matches shutter timestamp in the
   2473         // case of request having callback.
   2474         if (request.hasCallback && request.requestStatus == OK &&
   2475                 sensorTimestamp != shutterTimestamp) {
   2476             SET_ERR("sensor timestamp (%" PRId64
   2477                 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
   2478                 sensorTimestamp, frameNumber, shutterTimestamp);
   2479         }
   2480 
   2481         // for an unsuccessful request, it may have pending output buffers to
   2482         // return.
   2483         assert(request.requestStatus != OK ||
   2484                request.pendingOutputBuffers.size() == 0);
   2485         returnOutputBuffers(request.pendingOutputBuffers.array(),
   2486             request.pendingOutputBuffers.size(), 0);
   2487 
   2488         removeInFlightMapEntryLocked(idx);
   2489         ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
   2490      }
   2491 
   2492     // Sanity check - if we have too many in-flight frames, something has
   2493     // likely gone wrong
   2494     if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
   2495         CLOGE("In-flight list too large: %zu", mInFlightMap.size());
   2496     } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
   2497             kInFlightWarnLimitHighSpeed) {
   2498         CLOGE("In-flight list too large for high speed configuration: %zu",
   2499                 mInFlightMap.size());
   2500     }
   2501 }
   2502 
   2503 void Camera3Device::flushInflightRequests() {
   2504     ATRACE_CALL();
   2505     { // First return buffers cached in mInFlightMap
   2506         Mutex::Autolock l(mInFlightLock);
   2507         for (size_t idx = 0; idx < mInFlightMap.size(); idx++) {
   2508             const InFlightRequest &request = mInFlightMap.valueAt(idx);
   2509             returnOutputBuffers(request.pendingOutputBuffers.array(),
   2510                 request.pendingOutputBuffers.size(), 0);
   2511         }
   2512         mInFlightMap.clear();
   2513         mExpectedInflightDuration = 0;
   2514     }
   2515 
   2516     // Then return all inflight buffers not returned by HAL
   2517     std::vector<std::pair<int32_t, int32_t>> inflightKeys;
   2518     mInterface->getInflightBufferKeys(&inflightKeys);
   2519 
   2520     int32_t inputStreamId = (mInputStream != nullptr) ? mInputStream->getId() : -1;
   2521     for (auto& pair : inflightKeys) {
   2522         int32_t frameNumber = pair.first;
   2523         int32_t streamId = pair.second;
   2524         buffer_handle_t* buffer;
   2525         status_t res = mInterface->popInflightBuffer(frameNumber, streamId, &buffer);
   2526         if (res != OK) {
   2527             ALOGE("%s: Frame %d: No in-flight buffer for stream %d",
   2528                     __FUNCTION__, frameNumber, streamId);
   2529             continue;
   2530         }
   2531 
   2532         camera3_stream_buffer_t streamBuffer;
   2533         streamBuffer.buffer = buffer;
   2534         streamBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
   2535         streamBuffer.acquire_fence = -1;
   2536         streamBuffer.release_fence = -1;
   2537 
   2538         // First check if the buffer belongs to deleted stream
   2539         bool streamDeleted = false;
   2540         for (auto& stream : mDeletedStreams) {
   2541             if (streamId == stream->getId()) {
   2542                 streamDeleted = true;
   2543                 // Return buffer to deleted stream
   2544                 camera3_stream* halStream = stream->asHalStream();
   2545                 streamBuffer.stream = halStream;
   2546                 switch (halStream->stream_type) {
   2547                     case CAMERA3_STREAM_OUTPUT:
   2548                         res = stream->returnBuffer(streamBuffer, /*timestamp*/ 0);
   2549                         if (res != OK) {
   2550                             ALOGE("%s: Can't return output buffer for frame %d to"
   2551                                   " stream %d: %s (%d)",  __FUNCTION__,
   2552                                   frameNumber, streamId, strerror(-res), res);
   2553                         }
   2554                         break;
   2555                     case CAMERA3_STREAM_INPUT:
   2556                         res = stream->returnInputBuffer(streamBuffer);
   2557                         if (res != OK) {
   2558                             ALOGE("%s: Can't return input buffer for frame %d to"
   2559                                   " stream %d: %s (%d)",  __FUNCTION__,
   2560                                   frameNumber, streamId, strerror(-res), res);
   2561                         }
   2562                         break;
   2563                     default: // Bi-direcitonal stream is deprecated
   2564                         ALOGE("%s: stream %d has unknown stream type %d",
   2565                                 __FUNCTION__, streamId, halStream->stream_type);
   2566                         break;
   2567                 }
   2568                 break;
   2569             }
   2570         }
   2571         if (streamDeleted) {
   2572             continue;
   2573         }
   2574 
   2575         // Then check against configured streams
   2576         if (streamId == inputStreamId) {
   2577             streamBuffer.stream = mInputStream->asHalStream();
   2578             res = mInputStream->returnInputBuffer(streamBuffer);
   2579             if (res != OK) {
   2580                 ALOGE("%s: Can't return input buffer for frame %d to"
   2581                       " stream %d: %s (%d)",  __FUNCTION__,
   2582                       frameNumber, streamId, strerror(-res), res);
   2583             }
   2584         } else {
   2585             ssize_t idx = mOutputStreams.indexOfKey(streamId);
   2586             if (idx == NAME_NOT_FOUND) {
   2587                 ALOGE("%s: Output stream id %d not found!", __FUNCTION__, streamId);
   2588                 continue;
   2589             }
   2590             streamBuffer.stream = mOutputStreams.valueAt(idx)->asHalStream();
   2591             returnOutputBuffers(&streamBuffer, /*size*/1, /*timestamp*/ 0);
   2592         }
   2593     }
   2594 }
   2595 
   2596 void Camera3Device::insertResultLocked(CaptureResult *result,
   2597         uint32_t frameNumber) {
   2598     if (result == nullptr) return;
   2599 
   2600     camera_metadata_t *meta = const_cast<camera_metadata_t *>(
   2601             result->mMetadata.getAndLock());
   2602     set_camera_metadata_vendor_id(meta, mVendorTagId);
   2603     result->mMetadata.unlock(meta);
   2604 
   2605     if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
   2606             (int32_t*)&frameNumber, 1) != OK) {
   2607         SET_ERR("Failed to set frame number %d in metadata", frameNumber);
   2608         return;
   2609     }
   2610 
   2611     if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
   2612         SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
   2613         return;
   2614     }
   2615 
   2616     // Valid result, insert into queue
   2617     List<CaptureResult>::iterator queuedResult =
   2618             mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
   2619     ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
   2620            ", burstId = %" PRId32, __FUNCTION__,
   2621            queuedResult->mResultExtras.requestId,
   2622            queuedResult->mResultExtras.frameNumber,
   2623            queuedResult->mResultExtras.burstId);
   2624 
   2625     mResultSignal.signal();
   2626 }
   2627 
   2628 
   2629 void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
   2630         const CaptureResultExtras &resultExtras, uint32_t frameNumber) {
   2631     ATRACE_CALL();
   2632     Mutex::Autolock l(mOutputLock);
   2633 
   2634     CaptureResult captureResult;
   2635     captureResult.mResultExtras = resultExtras;
   2636     captureResult.mMetadata = partialResult;
   2637 
   2638     insertResultLocked(&captureResult, frameNumber);
   2639 }
   2640 
   2641 
   2642 void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
   2643         CaptureResultExtras &resultExtras,
   2644         CameraMetadata &collectedPartialResult,
   2645         uint32_t frameNumber,
   2646         bool reprocess) {
   2647     ATRACE_CALL();
   2648     if (pendingMetadata.isEmpty())
   2649         return;
   2650 
   2651     Mutex::Autolock l(mOutputLock);
   2652 
   2653     // TODO: need to track errors for tighter bounds on expected frame number
   2654     if (reprocess) {
   2655         if (frameNumber < mNextReprocessResultFrameNumber) {
   2656             SET_ERR("Out-of-order reprocess capture result metadata submitted! "
   2657                 "(got frame number %d, expecting %d)",
   2658                 frameNumber, mNextReprocessResultFrameNumber);
   2659             return;
   2660         }
   2661         mNextReprocessResultFrameNumber = frameNumber + 1;
   2662     } else {
   2663         if (frameNumber < mNextResultFrameNumber) {
   2664             SET_ERR("Out-of-order capture result metadata submitted! "
   2665                     "(got frame number %d, expecting %d)",
   2666                     frameNumber, mNextResultFrameNumber);
   2667             return;
   2668         }
   2669         mNextResultFrameNumber = frameNumber + 1;
   2670     }
   2671 
   2672     CaptureResult captureResult;
   2673     captureResult.mResultExtras = resultExtras;
   2674     captureResult.mMetadata = pendingMetadata;
   2675 
   2676     // Append any previous partials to form a complete result
   2677     if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
   2678         captureResult.mMetadata.append(collectedPartialResult);
   2679     }
   2680 
   2681     captureResult.mMetadata.sort();
   2682 
   2683     // Check that there's a timestamp in the result metadata
   2684     camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
   2685     if (timestamp.count == 0) {
   2686         SET_ERR("No timestamp provided by HAL for frame %d!",
   2687                 frameNumber);
   2688         return;
   2689     }
   2690 
   2691     mTagMonitor.monitorMetadata(TagMonitor::RESULT,
   2692             frameNumber, timestamp.data.i64[0], captureResult.mMetadata);
   2693 
   2694     insertResultLocked(&captureResult, frameNumber);
   2695 }
   2696 
   2697 /**
   2698  * Camera HAL device callback methods
   2699  */
   2700 
   2701 void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
   2702     ATRACE_CALL();
   2703 
   2704     status_t res;
   2705 
   2706     uint32_t frameNumber = result->frame_number;
   2707     if (result->result == NULL && result->num_output_buffers == 0 &&
   2708             result->input_buffer == NULL) {
   2709         SET_ERR("No result data provided by HAL for frame %d",
   2710                 frameNumber);
   2711         return;
   2712     }
   2713 
   2714     if (!mUsePartialResult &&
   2715             result->result != NULL &&
   2716             result->partial_result != 1) {
   2717         SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
   2718                 " if partial result is not supported",
   2719                 frameNumber, result->partial_result);
   2720         return;
   2721     }
   2722 
   2723     bool isPartialResult = false;
   2724     CameraMetadata collectedPartialResult;
   2725     CaptureResultExtras resultExtras;
   2726     bool hasInputBufferInRequest = false;
   2727 
   2728     // Get shutter timestamp and resultExtras from list of in-flight requests,
   2729     // where it was added by the shutter notification for this frame. If the
   2730     // shutter timestamp isn't received yet, append the output buffers to the
   2731     // in-flight request and they will be returned when the shutter timestamp
   2732     // arrives. Update the in-flight status and remove the in-flight entry if
   2733     // all result data and shutter timestamp have been received.
   2734     nsecs_t shutterTimestamp = 0;
   2735 
   2736     {
   2737         Mutex::Autolock l(mInFlightLock);
   2738         ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
   2739         if (idx == NAME_NOT_FOUND) {
   2740             SET_ERR("Unknown frame number for capture result: %d",
   2741                     frameNumber);
   2742             return;
   2743         }
   2744         InFlightRequest &request = mInFlightMap.editValueAt(idx);
   2745         ALOGVV("%s: got InFlightRequest requestId = %" PRId32
   2746                 ", frameNumber = %" PRId64 ", burstId = %" PRId32
   2747                 ", partialResultCount = %d, hasCallback = %d",
   2748                 __FUNCTION__, request.resultExtras.requestId,
   2749                 request.resultExtras.frameNumber, request.resultExtras.burstId,
   2750                 result->partial_result, request.hasCallback);
   2751         // Always update the partial count to the latest one if it's not 0
   2752         // (buffers only). When framework aggregates adjacent partial results
   2753         // into one, the latest partial count will be used.
   2754         if (result->partial_result != 0)
   2755             request.resultExtras.partialResultCount = result->partial_result;
   2756 
   2757         // Check if this result carries only partial metadata
   2758         if (mUsePartialResult && result->result != NULL) {
   2759             if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
   2760                 SET_ERR("Result is malformed for frame %d: partial_result %u must be  in"
   2761                         " the range of [1, %d] when metadata is included in the result",
   2762                         frameNumber, result->partial_result, mNumPartialResults);
   2763                 return;
   2764             }
   2765             isPartialResult = (result->partial_result < mNumPartialResults);
   2766             if (isPartialResult) {
   2767                 request.collectedPartialResult.append(result->result);
   2768             }
   2769 
   2770             if (isPartialResult && request.hasCallback) {
   2771                 // Send partial capture result
   2772                 sendPartialCaptureResult(result->result, request.resultExtras,
   2773                         frameNumber);
   2774             }
   2775         }
   2776 
   2777         shutterTimestamp = request.shutterTimestamp;
   2778         hasInputBufferInRequest = request.hasInputBuffer;
   2779 
   2780         // Did we get the (final) result metadata for this capture?
   2781         if (result->result != NULL && !isPartialResult) {
   2782             if (request.haveResultMetadata) {
   2783                 SET_ERR("Called multiple times with metadata for frame %d",
   2784                         frameNumber);
   2785                 return;
   2786             }
   2787             if (mUsePartialResult &&
   2788                     !request.collectedPartialResult.isEmpty()) {
   2789                 collectedPartialResult.acquire(
   2790                     request.collectedPartialResult);
   2791             }
   2792             request.haveResultMetadata = true;
   2793         }
   2794 
   2795         uint32_t numBuffersReturned = result->num_output_buffers;
   2796         if (result->input_buffer != NULL) {
   2797             if (hasInputBufferInRequest) {
   2798                 numBuffersReturned += 1;
   2799             } else {
   2800                 ALOGW("%s: Input buffer should be NULL if there is no input"
   2801                         " buffer sent in the request",
   2802                         __FUNCTION__);
   2803             }
   2804         }
   2805         request.numBuffersLeft -= numBuffersReturned;
   2806         if (request.numBuffersLeft < 0) {
   2807             SET_ERR("Too many buffers returned for frame %d",
   2808                     frameNumber);
   2809             return;
   2810         }
   2811 
   2812         camera_metadata_ro_entry_t entry;
   2813         res = find_camera_metadata_ro_entry(result->result,
   2814                 ANDROID_SENSOR_TIMESTAMP, &entry);
   2815         if (res == OK && entry.count == 1) {
   2816             request.sensorTimestamp = entry.data.i64[0];
   2817         }
   2818 
   2819         // If shutter event isn't received yet, append the output buffers to
   2820         // the in-flight request. Otherwise, return the output buffers to
   2821         // streams.
   2822         if (shutterTimestamp == 0) {
   2823             request.pendingOutputBuffers.appendArray(result->output_buffers,
   2824                 result->num_output_buffers);
   2825         } else {
   2826             returnOutputBuffers(result->output_buffers,
   2827                 result->num_output_buffers, shutterTimestamp);
   2828         }
   2829 
   2830         if (result->result != NULL && !isPartialResult) {
   2831             if (shutterTimestamp == 0) {
   2832                 request.pendingMetadata = result->result;
   2833                 request.collectedPartialResult = collectedPartialResult;
   2834             } else if (request.hasCallback) {
   2835                 CameraMetadata metadata;
   2836                 metadata = result->result;
   2837                 sendCaptureResult(metadata, request.resultExtras,
   2838                     collectedPartialResult, frameNumber,
   2839                     hasInputBufferInRequest);
   2840             }
   2841         }
   2842 
   2843         removeInFlightRequestIfReadyLocked(idx);
   2844     } // scope for mInFlightLock
   2845 
   2846     if (result->input_buffer != NULL) {
   2847         if (hasInputBufferInRequest) {
   2848             Camera3Stream *stream =
   2849                 Camera3Stream::cast(result->input_buffer->stream);
   2850             res = stream->returnInputBuffer(*(result->input_buffer));
   2851             // Note: stream may be deallocated at this point, if this buffer was the
   2852             // last reference to it.
   2853             if (res != OK) {
   2854                 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
   2855                       "  its stream:%s (%d)",  __FUNCTION__,
   2856                       frameNumber, strerror(-res), res);
   2857             }
   2858         } else {
   2859             ALOGW("%s: Input buffer should be NULL if there is no input"
   2860                     " buffer sent in the request, skipping input buffer return.",
   2861                     __FUNCTION__);
   2862         }
   2863     }
   2864 }
   2865 
   2866 void Camera3Device::notify(const camera3_notify_msg *msg) {
   2867     ATRACE_CALL();
   2868     sp<NotificationListener> listener;
   2869     {
   2870         Mutex::Autolock l(mOutputLock);
   2871         listener = mListener.promote();
   2872     }
   2873 
   2874     if (msg == NULL) {
   2875         SET_ERR("HAL sent NULL notify message!");
   2876         return;
   2877     }
   2878 
   2879     switch (msg->type) {
   2880         case CAMERA3_MSG_ERROR: {
   2881             notifyError(msg->message.error, listener);
   2882             break;
   2883         }
   2884         case CAMERA3_MSG_SHUTTER: {
   2885             notifyShutter(msg->message.shutter, listener);
   2886             break;
   2887         }
   2888         default:
   2889             SET_ERR("Unknown notify message from HAL: %d",
   2890                     msg->type);
   2891     }
   2892 }
   2893 
   2894 void Camera3Device::notifyError(const camera3_error_msg_t &msg,
   2895         sp<NotificationListener> listener) {
   2896     ATRACE_CALL();
   2897     // Map camera HAL error codes to ICameraDeviceCallback error codes
   2898     // Index into this with the HAL error code
   2899     static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
   2900         // 0 = Unused error code
   2901         hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
   2902         // 1 = CAMERA3_MSG_ERROR_DEVICE
   2903         hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
   2904         // 2 = CAMERA3_MSG_ERROR_REQUEST
   2905         hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
   2906         // 3 = CAMERA3_MSG_ERROR_RESULT
   2907         hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
   2908         // 4 = CAMERA3_MSG_ERROR_BUFFER
   2909         hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
   2910     };
   2911 
   2912     int32_t errorCode =
   2913             ((msg.error_code >= 0) &&
   2914                     (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
   2915             halErrorMap[msg.error_code] :
   2916             hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
   2917 
   2918     int streamId = 0;
   2919     if (msg.error_stream != NULL) {
   2920         Camera3Stream *stream =
   2921                 Camera3Stream::cast(msg.error_stream);
   2922         streamId = stream->getId();
   2923     }
   2924     ALOGV("Camera %s: %s: HAL error, frame %d, stream %d: %d",
   2925             mId.string(), __FUNCTION__, msg.frame_number,
   2926             streamId, msg.error_code);
   2927 
   2928     CaptureResultExtras resultExtras;
   2929     switch (errorCode) {
   2930         case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
   2931             // SET_ERR calls notifyError
   2932             SET_ERR("Camera HAL reported serious device error");
   2933             break;
   2934         case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
   2935         case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
   2936         case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
   2937             {
   2938                 Mutex::Autolock l(mInFlightLock);
   2939                 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
   2940                 if (idx >= 0) {
   2941                     InFlightRequest &r = mInFlightMap.editValueAt(idx);
   2942                     r.requestStatus = msg.error_code;
   2943                     resultExtras = r.resultExtras;
   2944                     if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT == errorCode
   2945                             ||  hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST ==
   2946                             errorCode) {
   2947                         r.skipResultMetadata = true;
   2948                     }
   2949                     if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT ==
   2950                             errorCode) {
   2951                         // In case of missing result check whether the buffers
   2952                         // returned. If they returned, then remove inflight
   2953                         // request.
   2954                         removeInFlightRequestIfReadyLocked(idx);
   2955                     }
   2956                 } else {
   2957                     resultExtras.frameNumber = msg.frame_number;
   2958                     ALOGE("Camera %s: %s: cannot find in-flight request on "
   2959                             "frame %" PRId64 " error", mId.string(), __FUNCTION__,
   2960                             resultExtras.frameNumber);
   2961                 }
   2962             }
   2963             resultExtras.errorStreamId = streamId;
   2964             if (listener != NULL) {
   2965                 listener->notifyError(errorCode, resultExtras);
   2966             } else {
   2967                 ALOGE("Camera %s: %s: no listener available", mId.string(), __FUNCTION__);
   2968             }
   2969             break;
   2970         default:
   2971             // SET_ERR calls notifyError
   2972             SET_ERR("Unknown error message from HAL: %d", msg.error_code);
   2973             break;
   2974     }
   2975 }
   2976 
   2977 void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
   2978         sp<NotificationListener> listener) {
   2979     ATRACE_CALL();
   2980     ssize_t idx;
   2981 
   2982     // Set timestamp for the request in the in-flight tracking
   2983     // and get the request ID to send upstream
   2984     {
   2985         Mutex::Autolock l(mInFlightLock);
   2986         idx = mInFlightMap.indexOfKey(msg.frame_number);
   2987         if (idx >= 0) {
   2988             InFlightRequest &r = mInFlightMap.editValueAt(idx);
   2989 
   2990             // Verify ordering of shutter notifications
   2991             {
   2992                 Mutex::Autolock l(mOutputLock);
   2993                 // TODO: need to track errors for tighter bounds on expected frame number.
   2994                 if (r.hasInputBuffer) {
   2995                     if (msg.frame_number < mNextReprocessShutterFrameNumber) {
   2996                         SET_ERR("Shutter notification out-of-order. Expected "
   2997                                 "notification for frame %d, got frame %d",
   2998                                 mNextReprocessShutterFrameNumber, msg.frame_number);
   2999                         return;
   3000                     }
   3001                     mNextReprocessShutterFrameNumber = msg.frame_number + 1;
   3002                 } else {
   3003                     if (msg.frame_number < mNextShutterFrameNumber) {
   3004                         SET_ERR("Shutter notification out-of-order. Expected "
   3005                                 "notification for frame %d, got frame %d",
   3006                                 mNextShutterFrameNumber, msg.frame_number);
   3007                         return;
   3008                     }
   3009                     mNextShutterFrameNumber = msg.frame_number + 1;
   3010                 }
   3011             }
   3012 
   3013             r.shutterTimestamp = msg.timestamp;
   3014             if (r.hasCallback) {
   3015                 ALOGVV("Camera %s: %s: Shutter fired for frame %d (id %d) at %" PRId64,
   3016                     mId.string(), __FUNCTION__,
   3017                     msg.frame_number, r.resultExtras.requestId, msg.timestamp);
   3018                 // Call listener, if any
   3019                 if (listener != NULL) {
   3020                     listener->notifyShutter(r.resultExtras, msg.timestamp);
   3021                 }
   3022                 // send pending result and buffers
   3023                 sendCaptureResult(r.pendingMetadata, r.resultExtras,
   3024                     r.collectedPartialResult, msg.frame_number,
   3025                     r.hasInputBuffer);
   3026             }
   3027             returnOutputBuffers(r.pendingOutputBuffers.array(),
   3028                 r.pendingOutputBuffers.size(), r.shutterTimestamp);
   3029             r.pendingOutputBuffers.clear();
   3030 
   3031             removeInFlightRequestIfReadyLocked(idx);
   3032         }
   3033     }
   3034     if (idx < 0) {
   3035         SET_ERR("Shutter notification for non-existent frame number %d",
   3036                 msg.frame_number);
   3037     }
   3038 }
   3039 
   3040 
   3041 CameraMetadata Camera3Device::getLatestRequestLocked() {
   3042     ALOGV("%s", __FUNCTION__);
   3043 
   3044     CameraMetadata retVal;
   3045 
   3046     if (mRequestThread != NULL) {
   3047         retVal = mRequestThread->getLatestRequest();
   3048     }
   3049 
   3050     return retVal;
   3051 }
   3052 
   3053 
   3054 void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
   3055         int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
   3056     mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
   3057 }
   3058 
   3059 /**
   3060  * HalInterface inner class methods
   3061  */
   3062 
   3063 Camera3Device::HalInterface::HalInterface(
   3064             sp<ICameraDeviceSession> &session,
   3065             std::shared_ptr<RequestMetadataQueue> queue) :
   3066         mHidlSession(session),
   3067         mRequestMetadataQueue(queue) {}
   3068 
   3069 Camera3Device::HalInterface::HalInterface() {}
   3070 
   3071 Camera3Device::HalInterface::HalInterface(const HalInterface& other) :
   3072         mHidlSession(other.mHidlSession),
   3073         mRequestMetadataQueue(other.mRequestMetadataQueue) {}
   3074 
   3075 bool Camera3Device::HalInterface::valid() {
   3076     return (mHidlSession != nullptr);
   3077 }
   3078 
   3079 void Camera3Device::HalInterface::clear() {
   3080     mHidlSession.clear();
   3081 }
   3082 
   3083 bool Camera3Device::HalInterface::supportBatchRequest() {
   3084     return mHidlSession != nullptr;
   3085 }
   3086 
   3087 status_t Camera3Device::HalInterface::constructDefaultRequestSettings(
   3088         camera3_request_template_t templateId,
   3089         /*out*/ camera_metadata_t **requestTemplate) {
   3090     ATRACE_NAME("CameraHal::constructDefaultRequestSettings");
   3091     if (!valid()) return INVALID_OPERATION;
   3092     status_t res = OK;
   3093 
   3094     common::V1_0::Status status;
   3095     RequestTemplate id;
   3096     switch (templateId) {
   3097         case CAMERA3_TEMPLATE_PREVIEW:
   3098             id = RequestTemplate::PREVIEW;
   3099             break;
   3100         case CAMERA3_TEMPLATE_STILL_CAPTURE:
   3101             id = RequestTemplate::STILL_CAPTURE;
   3102             break;
   3103         case CAMERA3_TEMPLATE_VIDEO_RECORD:
   3104             id = RequestTemplate::VIDEO_RECORD;
   3105             break;
   3106         case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
   3107             id = RequestTemplate::VIDEO_SNAPSHOT;
   3108             break;
   3109         case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
   3110             id = RequestTemplate::ZERO_SHUTTER_LAG;
   3111             break;
   3112         case CAMERA3_TEMPLATE_MANUAL:
   3113             id = RequestTemplate::MANUAL;
   3114             break;
   3115         default:
   3116             // Unknown template ID
   3117             return BAD_VALUE;
   3118     }
   3119     auto err = mHidlSession->constructDefaultRequestSettings(id,
   3120             [&status, &requestTemplate]
   3121             (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) {
   3122                 status = s;
   3123                 if (status == common::V1_0::Status::OK) {
   3124                     const camera_metadata *r =
   3125                             reinterpret_cast<const camera_metadata_t*>(request.data());
   3126                     size_t expectedSize = request.size();
   3127                     int ret = validate_camera_metadata_structure(r, &expectedSize);
   3128                     if (ret == OK || ret == CAMERA_METADATA_VALIDATION_SHIFTED) {
   3129                         *requestTemplate = clone_camera_metadata(r);
   3130                         if (*requestTemplate == nullptr) {
   3131                             ALOGE("%s: Unable to clone camera metadata received from HAL",
   3132                                     __FUNCTION__);
   3133                             status = common::V1_0::Status::INTERNAL_ERROR;
   3134                         }
   3135                     } else {
   3136                         ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
   3137                         status = common::V1_0::Status::INTERNAL_ERROR;
   3138                     }
   3139                 }
   3140             });
   3141     if (!err.isOk()) {
   3142         ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
   3143         res = DEAD_OBJECT;
   3144     } else {
   3145         res = CameraProviderManager::mapToStatusT(status);
   3146     }
   3147 
   3148     return res;
   3149 }
   3150 
   3151 status_t Camera3Device::HalInterface::configureStreams(camera3_stream_configuration *config) {
   3152     ATRACE_NAME("CameraHal::configureStreams");
   3153     if (!valid()) return INVALID_OPERATION;
   3154     status_t res = OK;
   3155 
   3156     // Convert stream config to HIDL
   3157     std::set<int> activeStreams;
   3158     StreamConfiguration requestedConfiguration;
   3159     requestedConfiguration.streams.resize(config->num_streams);
   3160     for (size_t i = 0; i < config->num_streams; i++) {
   3161         Stream &dst = requestedConfiguration.streams[i];
   3162         camera3_stream_t *src = config->streams[i];
   3163 
   3164         Camera3Stream* cam3stream = Camera3Stream::cast(src);
   3165         cam3stream->setBufferFreedListener(this);
   3166         int streamId = cam3stream->getId();
   3167         StreamType streamType;
   3168         switch (src->stream_type) {
   3169             case CAMERA3_STREAM_OUTPUT:
   3170                 streamType = StreamType::OUTPUT;
   3171                 break;
   3172             case CAMERA3_STREAM_INPUT:
   3173                 streamType = StreamType::INPUT;
   3174                 break;
   3175             default:
   3176                 ALOGE("%s: Stream %d: Unsupported stream type %d",
   3177                         __FUNCTION__, streamId, config->streams[i]->stream_type);
   3178                 return BAD_VALUE;
   3179         }
   3180         dst.id = streamId;
   3181         dst.streamType = streamType;
   3182         dst.width = src->width;
   3183         dst.height = src->height;
   3184         dst.format = mapToPixelFormat(src->format);
   3185         dst.usage = mapToConsumerUsage(cam3stream->getUsage());
   3186         dst.dataSpace = mapToHidlDataspace(src->data_space);
   3187         dst.rotation = mapToStreamRotation((camera3_stream_rotation_t) src->rotation);
   3188 
   3189         activeStreams.insert(streamId);
   3190         // Create Buffer ID map if necessary
   3191         if (mBufferIdMaps.count(streamId) == 0) {
   3192             mBufferIdMaps.emplace(streamId, BufferIdMap{});
   3193         }
   3194     }
   3195     // remove BufferIdMap for deleted streams
   3196     for(auto it = mBufferIdMaps.begin(); it != mBufferIdMaps.end();) {
   3197         int streamId = it->first;
   3198         bool active = activeStreams.count(streamId) > 0;
   3199         if (!active) {
   3200             it = mBufferIdMaps.erase(it);
   3201         } else {
   3202             ++it;
   3203         }
   3204     }
   3205 
   3206     res = mapToStreamConfigurationMode(
   3207             (camera3_stream_configuration_mode_t) config->operation_mode,
   3208             /*out*/ &requestedConfiguration.operationMode);
   3209     if (res != OK) {
   3210         return res;
   3211     }
   3212 
   3213     // Invoke configureStreams
   3214 
   3215     device::V3_3::HalStreamConfiguration finalConfiguration;
   3216     common::V1_0::Status status;
   3217 
   3218     // See if we have v3.3 HAL
   3219     sp<device::V3_3::ICameraDeviceSession> hidlSession_3_3;
   3220     auto castResult = device::V3_3::ICameraDeviceSession::castFrom(mHidlSession);
   3221     if (castResult.isOk()) {
   3222         hidlSession_3_3 = castResult;
   3223     } else {
   3224         ALOGE("%s: Transaction error when casting ICameraDeviceSession: %s", __FUNCTION__,
   3225                 castResult.description().c_str());
   3226     }
   3227     if (hidlSession_3_3 != nullptr) {
   3228         // We do; use v3.3 for the call
   3229         ALOGV("%s: v3.3 device found", __FUNCTION__);
   3230         auto err = hidlSession_3_3->configureStreams_3_3(requestedConfiguration,
   3231             [&status, &finalConfiguration]
   3232             (common::V1_0::Status s, const device::V3_3::HalStreamConfiguration& halConfiguration) {
   3233                 finalConfiguration = halConfiguration;
   3234                 status = s;
   3235             });
   3236         if (!err.isOk()) {
   3237             ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
   3238             return DEAD_OBJECT;
   3239         }
   3240     } else {
   3241         // We don't; use v3.2 call and construct a v3.3 HalStreamConfiguration
   3242         ALOGV("%s: v3.2 device found", __FUNCTION__);
   3243         HalStreamConfiguration finalConfiguration_3_2;
   3244         auto err = mHidlSession->configureStreams(requestedConfiguration,
   3245                 [&status, &finalConfiguration_3_2]
   3246                 (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) {
   3247                     finalConfiguration_3_2 = halConfiguration;
   3248                     status = s;
   3249                 });
   3250         if (!err.isOk()) {
   3251             ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
   3252             return DEAD_OBJECT;
   3253         }
   3254         finalConfiguration.streams.resize(finalConfiguration_3_2.streams.size());
   3255         for (size_t i = 0; i < finalConfiguration_3_2.streams.size(); i++) {
   3256             finalConfiguration.streams[i].v3_2 = finalConfiguration_3_2.streams[i];
   3257             finalConfiguration.streams[i].overrideDataSpace =
   3258                     requestedConfiguration.streams[i].dataSpace;
   3259         }
   3260     }
   3261 
   3262     if (status != common::V1_0::Status::OK ) {
   3263         return CameraProviderManager::mapToStatusT(status);
   3264     }
   3265 
   3266     // And convert output stream configuration from HIDL
   3267 
   3268     for (size_t i = 0; i < config->num_streams; i++) {
   3269         camera3_stream_t *dst = config->streams[i];
   3270         int streamId = Camera3Stream::cast(dst)->getId();
   3271 
   3272         // Start scan at i, with the assumption that the stream order matches
   3273         size_t realIdx = i;
   3274         bool found = false;
   3275         for (size_t idx = 0; idx < finalConfiguration.streams.size(); idx++) {
   3276             if (finalConfiguration.streams[realIdx].v3_2.id == streamId) {
   3277                 found = true;
   3278                 break;
   3279             }
   3280             realIdx = (realIdx >= finalConfiguration.streams.size()) ? 0 : realIdx + 1;
   3281         }
   3282         if (!found) {
   3283             ALOGE("%s: Stream %d not found in stream configuration response from HAL",
   3284                     __FUNCTION__, streamId);
   3285             return INVALID_OPERATION;
   3286         }
   3287         device::V3_3::HalStream &src = finalConfiguration.streams[realIdx];
   3288 
   3289         Camera3Stream* dstStream = Camera3Stream::cast(dst);
   3290         dstStream->setFormatOverride(false);
   3291         dstStream->setDataSpaceOverride(false);
   3292         int overrideFormat = mapToFrameworkFormat(src.v3_2.overrideFormat);
   3293         android_dataspace overrideDataSpace = mapToFrameworkDataspace(src.overrideDataSpace);
   3294 
   3295         if (dst->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
   3296             if (dst->format != overrideFormat) {
   3297                 ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__,
   3298                         streamId, dst->format);
   3299             }
   3300             if (dst->data_space != overrideDataSpace) {
   3301                 ALOGE("%s: Stream %d: DataSpace override not allowed for format 0x%x", __FUNCTION__,
   3302                         streamId, dst->format);
   3303             }
   3304         } else {
   3305             dstStream->setFormatOverride((dst->format != overrideFormat) ? true : false);
   3306             dstStream->setDataSpaceOverride((dst->data_space != overrideDataSpace) ? true : false);
   3307 
   3308             // Override allowed with IMPLEMENTATION_DEFINED
   3309             dst->format = overrideFormat;
   3310             dst->data_space = overrideDataSpace;
   3311         }
   3312 
   3313         if (dst->stream_type == CAMERA3_STREAM_INPUT) {
   3314             if (src.v3_2.producerUsage != 0) {
   3315                 ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage",
   3316                         __FUNCTION__, streamId);
   3317                 return INVALID_OPERATION;
   3318             }
   3319             dstStream->setUsage(
   3320                     mapConsumerToFrameworkUsage(src.v3_2.consumerUsage));
   3321         } else {
   3322             // OUTPUT
   3323             if (src.v3_2.consumerUsage != 0) {
   3324                 ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage",
   3325                         __FUNCTION__, streamId);
   3326                 return INVALID_OPERATION;
   3327             }
   3328             dstStream->setUsage(
   3329                     mapProducerToFrameworkUsage(src.v3_2.producerUsage));
   3330         }
   3331         dst->max_buffers = src.v3_2.maxBuffers;
   3332     }
   3333 
   3334     return res;
   3335 }
   3336 
   3337 void Camera3Device::HalInterface::wrapAsHidlRequest(camera3_capture_request_t* request,
   3338         /*out*/device::V3_2::CaptureRequest* captureRequest,
   3339         /*out*/std::vector<native_handle_t*>* handlesCreated) {
   3340     ATRACE_CALL();
   3341     if (captureRequest == nullptr || handlesCreated == nullptr) {
   3342         ALOGE("%s: captureRequest (%p) and handlesCreated (%p) must not be null",
   3343                 __FUNCTION__, captureRequest, handlesCreated);
   3344         return;
   3345     }
   3346 
   3347     captureRequest->frameNumber = request->frame_number;
   3348 
   3349     captureRequest->fmqSettingsSize = 0;
   3350 
   3351     {
   3352         std::lock_guard<std::mutex> lock(mInflightLock);
   3353         if (request->input_buffer != nullptr) {
   3354             int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId();
   3355             buffer_handle_t buf = *(request->input_buffer->buffer);
   3356             auto pair = getBufferId(buf, streamId);
   3357             bool isNewBuffer = pair.first;
   3358             uint64_t bufferId = pair.second;
   3359             captureRequest->inputBuffer.streamId = streamId;
   3360             captureRequest->inputBuffer.bufferId = bufferId;
   3361             captureRequest->inputBuffer.buffer = (isNewBuffer) ? buf : nullptr;
   3362             captureRequest->inputBuffer.status = BufferStatus::OK;
   3363             native_handle_t *acquireFence = nullptr;
   3364             if (request->input_buffer->acquire_fence != -1) {
   3365                 acquireFence = native_handle_create(1,0);
   3366                 acquireFence->data[0] = request->input_buffer->acquire_fence;
   3367                 handlesCreated->push_back(acquireFence);
   3368             }
   3369             captureRequest->inputBuffer.acquireFence = acquireFence;
   3370             captureRequest->inputBuffer.releaseFence = nullptr;
   3371 
   3372             pushInflightBufferLocked(captureRequest->frameNumber, streamId,
   3373                     request->input_buffer->buffer,
   3374                     request->input_buffer->acquire_fence);
   3375         } else {
   3376             captureRequest->inputBuffer.streamId = -1;
   3377             captureRequest->inputBuffer.bufferId = BUFFER_ID_NO_BUFFER;
   3378         }
   3379 
   3380         captureRequest->outputBuffers.resize(request->num_output_buffers);
   3381         for (size_t i = 0; i < request->num_output_buffers; i++) {
   3382             const camera3_stream_buffer_t *src = request->output_buffers + i;
   3383             StreamBuffer &dst = captureRequest->outputBuffers[i];
   3384             int32_t streamId = Camera3Stream::cast(src->stream)->getId();
   3385             buffer_handle_t buf = *(src->buffer);
   3386             auto pair = getBufferId(buf, streamId);
   3387             bool isNewBuffer = pair.first;
   3388             dst.streamId = streamId;
   3389             dst.bufferId = pair.second;
   3390             dst.buffer = isNewBuffer ? buf : nullptr;
   3391             dst.status = BufferStatus::OK;
   3392             native_handle_t *acquireFence = nullptr;
   3393             if (src->acquire_fence != -1) {
   3394                 acquireFence = native_handle_create(1,0);
   3395                 acquireFence->data[0] = src->acquire_fence;
   3396                 handlesCreated->push_back(acquireFence);
   3397             }
   3398             dst.acquireFence = acquireFence;
   3399             dst.releaseFence = nullptr;
   3400 
   3401             pushInflightBufferLocked(captureRequest->frameNumber, streamId,
   3402                     src->buffer, src->acquire_fence);
   3403         }
   3404     }
   3405 }
   3406 
   3407 status_t Camera3Device::HalInterface::processBatchCaptureRequests(
   3408         std::vector<camera3_capture_request_t*>& requests,/*out*/uint32_t* numRequestProcessed) {
   3409     ATRACE_NAME("CameraHal::processBatchCaptureRequests");
   3410     if (!valid()) return INVALID_OPERATION;
   3411 
   3412     hardware::hidl_vec<device::V3_2::CaptureRequest> captureRequests;
   3413     size_t batchSize = requests.size();
   3414     captureRequests.resize(batchSize);
   3415     std::vector<native_handle_t*> handlesCreated;
   3416 
   3417     for (size_t i = 0; i < batchSize; i++) {
   3418         wrapAsHidlRequest(requests[i], /*out*/&captureRequests[i], /*out*/&handlesCreated);
   3419     }
   3420 
   3421     std::vector<device::V3_2::BufferCache> cachesToRemove;
   3422     {
   3423         std::lock_guard<std::mutex> lock(mBufferIdMapLock);
   3424         for (auto& pair : mFreedBuffers) {
   3425             // The stream might have been removed since onBufferFreed
   3426             if (mBufferIdMaps.find(pair.first) != mBufferIdMaps.end()) {
   3427                 cachesToRemove.push_back({pair.first, pair.second});
   3428             }
   3429         }
   3430         mFreedBuffers.clear();
   3431     }
   3432 
   3433     common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
   3434     *numRequestProcessed = 0;
   3435 
   3436     // Write metadata to FMQ.
   3437     for (size_t i = 0; i < batchSize; i++) {
   3438         camera3_capture_request_t* request = requests[i];
   3439         device::V3_2::CaptureRequest* captureRequest = &captureRequests[i];
   3440 
   3441         if (request->settings != nullptr) {
   3442             size_t settingsSize = get_camera_metadata_size(request->settings);
   3443             if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
   3444                     reinterpret_cast<const uint8_t*>(request->settings), settingsSize)) {
   3445                 captureRequest->settings.resize(0);
   3446                 captureRequest->fmqSettingsSize = settingsSize;
   3447             } else {
   3448                 if (mRequestMetadataQueue != nullptr) {
   3449                     ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
   3450                 }
   3451                 captureRequest->settings.setToExternal(
   3452                         reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(request->settings)),
   3453                         get_camera_metadata_size(request->settings));
   3454                 captureRequest->fmqSettingsSize = 0u;
   3455             }
   3456         } else {
   3457             // A null request settings maps to a size-0 CameraMetadata
   3458             captureRequest->settings.resize(0);
   3459             captureRequest->fmqSettingsSize = 0u;
   3460         }
   3461     }
   3462     auto err = mHidlSession->processCaptureRequest(captureRequests, cachesToRemove,
   3463             [&status, &numRequestProcessed] (auto s, uint32_t n) {
   3464                 status = s;
   3465                 *numRequestProcessed = n;
   3466             });
   3467     if (!err.isOk()) {
   3468         ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
   3469         return DEAD_OBJECT;
   3470     }
   3471     if (status == common::V1_0::Status::OK && *numRequestProcessed != batchSize) {
   3472         ALOGE("%s: processCaptureRequest returns OK but processed %d/%zu requests",
   3473                 __FUNCTION__, *numRequestProcessed, batchSize);
   3474         status = common::V1_0::Status::INTERNAL_ERROR;
   3475     }
   3476 
   3477     for (auto& handle : handlesCreated) {
   3478         native_handle_delete(handle);
   3479     }
   3480     return CameraProviderManager::mapToStatusT(status);
   3481 }
   3482 
   3483 status_t Camera3Device::HalInterface::processCaptureRequest(
   3484         camera3_capture_request_t *request) {
   3485     ATRACE_NAME("CameraHal::processCaptureRequest");
   3486     if (!valid()) return INVALID_OPERATION;
   3487     status_t res = OK;
   3488 
   3489     uint32_t numRequestProcessed = 0;
   3490     std::vector<camera3_capture_request_t*> requests(1);
   3491     requests[0] = request;
   3492     res = processBatchCaptureRequests(requests, &numRequestProcessed);
   3493 
   3494     return res;
   3495 }
   3496 
   3497 status_t Camera3Device::HalInterface::flush() {
   3498     ATRACE_NAME("CameraHal::flush");
   3499     if (!valid()) return INVALID_OPERATION;
   3500     status_t res = OK;
   3501 
   3502     auto err = mHidlSession->flush();
   3503     if (!err.isOk()) {
   3504         ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
   3505         res = DEAD_OBJECT;
   3506     } else {
   3507         res = CameraProviderManager::mapToStatusT(err);
   3508     }
   3509 
   3510     return res;
   3511 }
   3512 
   3513 status_t Camera3Device::HalInterface::dump(int /*fd*/) {
   3514     ATRACE_NAME("CameraHal::dump");
   3515     if (!valid()) return INVALID_OPERATION;
   3516 
   3517     // Handled by CameraProviderManager::dump
   3518 
   3519     return OK;
   3520 }
   3521 
   3522 status_t Camera3Device::HalInterface::close() {
   3523     ATRACE_NAME("CameraHal::close()");
   3524     if (!valid()) return INVALID_OPERATION;
   3525     status_t res = OK;
   3526 
   3527     auto err = mHidlSession->close();
   3528     // Interface will be dead shortly anyway, so don't log errors
   3529     if (!err.isOk()) {
   3530         res = DEAD_OBJECT;
   3531     }
   3532 
   3533     return res;
   3534 }
   3535 
   3536 void Camera3Device::HalInterface::getInflightBufferKeys(
   3537         std::vector<std::pair<int32_t, int32_t>>* out) {
   3538     std::lock_guard<std::mutex> lock(mInflightLock);
   3539     out->clear();
   3540     out->reserve(mInflightBufferMap.size());
   3541     for (auto& pair : mInflightBufferMap) {
   3542         uint64_t key = pair.first;
   3543         int32_t streamId = key & 0xFFFFFFFF;
   3544         int32_t frameNumber = (key >> 32) & 0xFFFFFFFF;
   3545         out->push_back(std::make_pair(frameNumber, streamId));
   3546     }
   3547     return;
   3548 }
   3549 
   3550 status_t Camera3Device::HalInterface::pushInflightBufferLocked(
   3551         int32_t frameNumber, int32_t streamId, buffer_handle_t *buffer, int acquireFence) {
   3552     uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
   3553     auto pair = std::make_pair(buffer, acquireFence);
   3554     mInflightBufferMap[key] = pair;
   3555     return OK;
   3556 }
   3557 
   3558 status_t Camera3Device::HalInterface::popInflightBuffer(
   3559         int32_t frameNumber, int32_t streamId,
   3560         /*out*/ buffer_handle_t **buffer) {
   3561     std::lock_guard<std::mutex> lock(mInflightLock);
   3562 
   3563     uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
   3564     auto it = mInflightBufferMap.find(key);
   3565     if (it == mInflightBufferMap.end()) return NAME_NOT_FOUND;
   3566     auto pair = it->second;
   3567     *buffer = pair.first;
   3568     int acquireFence = pair.second;
   3569     if (acquireFence > 0) {
   3570         ::close(acquireFence);
   3571     }
   3572     mInflightBufferMap.erase(it);
   3573     return OK;
   3574 }
   3575 
   3576 std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
   3577         const buffer_handle_t& buf, int streamId) {
   3578     std::lock_guard<std::mutex> lock(mBufferIdMapLock);
   3579 
   3580     BufferIdMap& bIdMap = mBufferIdMaps.at(streamId);
   3581     auto it = bIdMap.find(buf);
   3582     if (it == bIdMap.end()) {
   3583         bIdMap[buf] = mNextBufferId++;
   3584         ALOGV("stream %d now have %zu buffer caches, buf %p",
   3585                 streamId, bIdMap.size(), buf);
   3586         return std::make_pair(true, mNextBufferId - 1);
   3587     } else {
   3588         return std::make_pair(false, it->second);
   3589     }
   3590 }
   3591 
   3592 void Camera3Device::HalInterface::onBufferFreed(
   3593         int streamId, const native_handle_t* handle) {
   3594     std::lock_guard<std::mutex> lock(mBufferIdMapLock);
   3595     uint64_t bufferId = BUFFER_ID_NO_BUFFER;
   3596     auto mapIt = mBufferIdMaps.find(streamId);
   3597     if (mapIt == mBufferIdMaps.end()) {
   3598         // streamId might be from a deleted stream here
   3599         ALOGI("%s: stream %d has been removed",
   3600                 __FUNCTION__, streamId);
   3601         return;
   3602     }
   3603     BufferIdMap& bIdMap = mapIt->second;
   3604     auto it = bIdMap.find(handle);
   3605     if (it == bIdMap.end()) {
   3606         ALOGW("%s: cannot find buffer %p in stream %d",
   3607                 __FUNCTION__, handle, streamId);
   3608         return;
   3609     } else {
   3610         bufferId =  it->second;
   3611         bIdMap.erase(it);
   3612         ALOGV("%s: stream %d now have %zu buffer caches after removing buf %p",
   3613                 __FUNCTION__, streamId, bIdMap.size(), handle);
   3614     }
   3615     mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
   3616 }
   3617 
   3618 /**
   3619  * RequestThread inner class methods
   3620  */
   3621 
   3622 Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
   3623         sp<StatusTracker> statusTracker,
   3624         sp<HalInterface> interface) :
   3625         Thread(/*canCallJava*/false),
   3626         mParent(parent),
   3627         mStatusTracker(statusTracker),
   3628         mInterface(interface),
   3629         mListener(nullptr),
   3630         mId(getId(parent)),
   3631         mReconfigured(false),
   3632         mDoPause(false),
   3633         mPaused(true),
   3634         mFrameNumber(0),
   3635         mLatestRequestId(NAME_NOT_FOUND),
   3636         mCurrentAfTriggerId(0),
   3637         mCurrentPreCaptureTriggerId(0),
   3638         mRepeatingLastFrameNumber(
   3639             hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
   3640         mPrepareVideoStream(false),
   3641         mRequestLatency(kRequestLatencyBinSize) {
   3642     mStatusId = statusTracker->addComponent();
   3643 }
   3644 
   3645 Camera3Device::RequestThread::~RequestThread() {}
   3646 
   3647 void Camera3Device::RequestThread::setNotificationListener(
   3648         wp<NotificationListener> listener) {
   3649     ATRACE_CALL();
   3650     Mutex::Autolock l(mRequestLock);
   3651     mListener = listener;
   3652 }
   3653 
   3654 void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed) {
   3655     ATRACE_CALL();
   3656     Mutex::Autolock l(mRequestLock);
   3657     mReconfigured = true;
   3658     // Prepare video stream for high speed recording.
   3659     mPrepareVideoStream = isConstrainedHighSpeed;
   3660 }
   3661 
   3662 status_t Camera3Device::RequestThread::queueRequestList(
   3663         List<sp<CaptureRequest> > &requests,
   3664         /*out*/
   3665         int64_t *lastFrameNumber) {
   3666     ATRACE_CALL();
   3667     Mutex::Autolock l(mRequestLock);
   3668     for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
   3669             ++it) {
   3670         mRequestQueue.push_back(*it);
   3671     }
   3672 
   3673     if (lastFrameNumber != NULL) {
   3674         *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
   3675         ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
   3676               __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
   3677               *lastFrameNumber);
   3678     }
   3679 
   3680     unpauseForNewRequests();
   3681 
   3682     return OK;
   3683 }
   3684 
   3685 
   3686 status_t Camera3Device::RequestThread::queueTrigger(
   3687         RequestTrigger trigger[],
   3688         size_t count) {
   3689     ATRACE_CALL();
   3690     Mutex::Autolock l(mTriggerMutex);
   3691     status_t ret;
   3692 
   3693     for (size_t i = 0; i < count; ++i) {
   3694         ret = queueTriggerLocked(trigger[i]);
   3695 
   3696         if (ret != OK) {
   3697             return ret;
   3698         }
   3699     }
   3700 
   3701     return OK;
   3702 }
   3703 
   3704 const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
   3705     static String8 deadId("<DeadDevice>");
   3706     sp<Camera3Device> d = device.promote();
   3707     if (d != nullptr) return d->mId;
   3708     return deadId;
   3709 }
   3710 
   3711 status_t Camera3Device::RequestThread::queueTriggerLocked(
   3712         RequestTrigger trigger) {
   3713 
   3714     uint32_t tag = trigger.metadataTag;
   3715     ssize_t index = mTriggerMap.indexOfKey(tag);
   3716 
   3717     switch (trigger.getTagType()) {
   3718         case TYPE_BYTE:
   3719         // fall-through
   3720         case TYPE_INT32:
   3721             break;
   3722         default:
   3723             ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
   3724                     trigger.getTagType());
   3725             return INVALID_OPERATION;
   3726     }
   3727 
   3728     /**
   3729      * Collect only the latest trigger, since we only have 1 field
   3730      * in the request settings per trigger tag, and can't send more than 1
   3731      * trigger per request.
   3732      */
   3733     if (index != NAME_NOT_FOUND) {
   3734         mTriggerMap.editValueAt(index) = trigger;
   3735     } else {
   3736         mTriggerMap.add(tag, trigger);
   3737     }
   3738 
   3739     return OK;
   3740 }
   3741 
   3742 status_t Camera3Device::RequestThread::setRepeatingRequests(
   3743         const RequestList &requests,
   3744         /*out*/
   3745         int64_t *lastFrameNumber) {
   3746     ATRACE_CALL();
   3747     Mutex::Autolock l(mRequestLock);
   3748     if (lastFrameNumber != NULL) {
   3749         *lastFrameNumber = mRepeatingLastFrameNumber;
   3750     }
   3751     mRepeatingRequests.clear();
   3752     mRepeatingRequests.insert(mRepeatingRequests.begin(),
   3753             requests.begin(), requests.end());
   3754 
   3755     unpauseForNewRequests();
   3756 
   3757     mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
   3758     return OK;
   3759 }
   3760 
   3761 bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
   3762     if (mRepeatingRequests.empty()) {
   3763         return false;
   3764     }
   3765     int32_t requestId = requestIn->mResultExtras.requestId;
   3766     const RequestList &repeatRequests = mRepeatingRequests;
   3767     // All repeating requests are guaranteed to have same id so only check first quest
   3768     const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
   3769     return (firstRequest->mResultExtras.requestId == requestId);
   3770 }
   3771 
   3772 status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
   3773     ATRACE_CALL();
   3774     Mutex::Autolock l(mRequestLock);
   3775     return clearRepeatingRequestsLocked(lastFrameNumber);
   3776 
   3777 }
   3778 
   3779 status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
   3780     mRepeatingRequests.clear();
   3781     if (lastFrameNumber != NULL) {
   3782         *lastFrameNumber = mRepeatingLastFrameNumber;
   3783     }
   3784     mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
   3785     return OK;
   3786 }
   3787 
   3788 status_t Camera3Device::RequestThread::clear(
   3789         /*out*/int64_t *lastFrameNumber) {
   3790     ATRACE_CALL();
   3791     Mutex::Autolock l(mRequestLock);
   3792     ALOGV("RequestThread::%s:", __FUNCTION__);
   3793 
   3794     mRepeatingRequests.clear();
   3795 
   3796     // Send errors for all requests pending in the request queue, including
   3797     // pending repeating requests
   3798     sp<NotificationListener> listener = mListener.promote();
   3799     if (listener != NULL) {
   3800         for (RequestList::iterator it = mRequestQueue.begin();
   3801                  it != mRequestQueue.end(); ++it) {
   3802             // Abort the input buffers for reprocess requests.
   3803             if ((*it)->mInputStream != NULL) {
   3804                 camera3_stream_buffer_t inputBuffer;
   3805                 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
   3806                         /*respectHalLimit*/ false);
   3807                 if (res != OK) {
   3808                     ALOGW("%s: %d: couldn't get input buffer while clearing the request "
   3809                             "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
   3810                 } else {
   3811                     res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
   3812                     if (res != OK) {
   3813                         ALOGE("%s: %d: couldn't return input buffer while clearing the request "
   3814                                 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
   3815                     }
   3816                 }
   3817             }
   3818             // Set the frame number this request would have had, if it
   3819             // had been submitted; this frame number will not be reused.
   3820             // The requestId and burstId fields were set when the request was
   3821             // submitted originally (in convertMetadataListToRequestListLocked)
   3822             (*it)->mResultExtras.frameNumber = mFrameNumber++;
   3823             listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
   3824                     (*it)->mResultExtras);
   3825         }
   3826     }
   3827     mRequestQueue.clear();
   3828 
   3829     Mutex::Autolock al(mTriggerMutex);
   3830     mTriggerMap.clear();
   3831     if (lastFrameNumber != NULL) {
   3832         *lastFrameNumber = mRepeatingLastFrameNumber;
   3833     }
   3834     mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
   3835     return OK;
   3836 }
   3837 
   3838 status_t Camera3Device::RequestThread::flush() {
   3839     ATRACE_CALL();
   3840     Mutex::Autolock l(mFlushLock);
   3841 
   3842     return mInterface->flush();
   3843 }
   3844 
   3845 void Camera3Device::RequestThread::setPaused(bool paused) {
   3846     ATRACE_CALL();
   3847     Mutex::Autolock l(mPauseLock);
   3848     mDoPause = paused;
   3849     mDoPauseSignal.signal();
   3850 }
   3851 
   3852 status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
   3853         int32_t requestId, nsecs_t timeout) {
   3854     ATRACE_CALL();
   3855     Mutex::Autolock l(mLatestRequestMutex);
   3856     status_t res;
   3857     while (mLatestRequestId != requestId) {
   3858         nsecs_t startTime = systemTime();
   3859 
   3860         res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
   3861         if (res != OK) return res;
   3862 
   3863         timeout -= (systemTime() - startTime);
   3864     }
   3865 
   3866     return OK;
   3867 }
   3868 
   3869 void Camera3Device::RequestThread::requestExit() {
   3870     // Call parent to set up shutdown
   3871     Thread::requestExit();
   3872     // The exit from any possible waits
   3873     mDoPauseSignal.signal();
   3874     mRequestSignal.signal();
   3875 
   3876     mRequestLatency.log("ProcessCaptureRequest latency histogram");
   3877     mRequestLatency.reset();
   3878 }
   3879 
   3880 void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
   3881     ATRACE_CALL();
   3882     bool surfaceAbandoned = false;
   3883     int64_t lastFrameNumber = 0;
   3884     sp<NotificationListener> listener;
   3885     {
   3886         Mutex::Autolock l(mRequestLock);
   3887         // Check all streams needed by repeating requests are still valid. Otherwise, stop
   3888         // repeating requests.
   3889         for (const auto& request : mRepeatingRequests) {
   3890             for (const auto& s : request->mOutputStreams) {
   3891                 if (s->isAbandoned()) {
   3892                     surfaceAbandoned = true;
   3893                     clearRepeatingRequestsLocked(&lastFrameNumber);
   3894                     break;
   3895                 }
   3896             }
   3897             if (surfaceAbandoned) {
   3898                 break;
   3899             }
   3900         }
   3901         listener = mListener.promote();
   3902     }
   3903 
   3904     if (listener != NULL && surfaceAbandoned) {
   3905         listener->notifyRepeatingRequestError(lastFrameNumber);
   3906     }
   3907 }
   3908 
   3909 bool Camera3Device::RequestThread::sendRequestsBatch() {
   3910     ATRACE_CALL();
   3911     status_t res;
   3912     size_t batchSize = mNextRequests.size();
   3913     std::vector<camera3_capture_request_t*> requests(batchSize);
   3914     uint32_t numRequestProcessed = 0;
   3915     for (size_t i = 0; i < batchSize; i++) {
   3916         requests[i] = &mNextRequests.editItemAt(i).halRequest;
   3917     }
   3918 
   3919     ATRACE_ASYNC_BEGIN("batch frame capture", mNextRequests[0].halRequest.frame_number);
   3920     res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
   3921 
   3922     bool triggerRemoveFailed = false;
   3923     NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
   3924     for (size_t i = 0; i < numRequestProcessed; i++) {
   3925         NextRequest& nextRequest = mNextRequests.editItemAt(i);
   3926         nextRequest.submitted = true;
   3927 
   3928 
   3929         // Update the latest request sent to HAL
   3930         if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
   3931             Mutex::Autolock al(mLatestRequestMutex);
   3932 
   3933             camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
   3934             mLatestRequest.acquire(cloned);
   3935 
   3936             sp<Camera3Device> parent = mParent.promote();
   3937             if (parent != NULL) {
   3938                 parent->monitorMetadata(TagMonitor::REQUEST,
   3939                         nextRequest.halRequest.frame_number,
   3940                         0, mLatestRequest);
   3941             }
   3942         }
   3943 
   3944         if (nextRequest.halRequest.settings != NULL) {
   3945             nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
   3946         }
   3947 
   3948         if (!triggerRemoveFailed) {
   3949             // Remove any previously queued triggers (after unlock)
   3950             status_t removeTriggerRes = removeTriggers(mPrevRequest);
   3951             if (removeTriggerRes != OK) {
   3952                 triggerRemoveFailed = true;
   3953                 triggerFailedRequest = nextRequest;
   3954             }
   3955         }
   3956     }
   3957 
   3958     if (triggerRemoveFailed) {
   3959         SET_ERR("RequestThread: Unable to remove triggers "
   3960               "(capture request %d, HAL device: %s (%d)",
   3961               triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
   3962         cleanUpFailedRequests(/*sendRequestError*/ false);
   3963         return false;
   3964     }
   3965 
   3966     if (res != OK) {
   3967         // Should only get a failure here for malformed requests or device-level
   3968         // errors, so consider all errors fatal.  Bad metadata failures should
   3969         // come through notify.
   3970         SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
   3971                 mNextRequests[numRequestProcessed].halRequest.frame_number,
   3972                 strerror(-res), res);
   3973         cleanUpFailedRequests(/*sendRequestError*/ false);
   3974         return false;
   3975     }
   3976     return true;
   3977 }
   3978 
   3979 bool Camera3Device::RequestThread::sendRequestsOneByOne() {
   3980     status_t res;
   3981 
   3982     for (auto& nextRequest : mNextRequests) {
   3983         // Submit request and block until ready for next one
   3984         ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
   3985         res = mInterface->processCaptureRequest(&nextRequest.halRequest);
   3986 
   3987         if (res != OK) {
   3988             // Should only get a failure here for malformed requests or device-level
   3989             // errors, so consider all errors fatal.  Bad metadata failures should
   3990             // come through notify.
   3991             SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
   3992                     " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
   3993                     res);
   3994             cleanUpFailedRequests(/*sendRequestError*/ false);
   3995             return false;
   3996         }
   3997 
   3998         // Mark that the request has be submitted successfully.
   3999         nextRequest.submitted = true;
   4000 
   4001         // Update the latest request sent to HAL
   4002         if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
   4003             Mutex::Autolock al(mLatestRequestMutex);
   4004 
   4005             camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
   4006             mLatestRequest.acquire(cloned);
   4007 
   4008             sp<Camera3Device> parent = mParent.promote();
   4009             if (parent != NULL) {
   4010                 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
   4011                         0, mLatestRequest);
   4012             }
   4013         }
   4014 
   4015         if (nextRequest.halRequest.settings != NULL) {
   4016             nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
   4017         }
   4018 
   4019         // Remove any previously queued triggers (after unlock)
   4020         res = removeTriggers(mPrevRequest);
   4021         if (res != OK) {
   4022             SET_ERR("RequestThread: Unable to remove triggers "
   4023                   "(capture request %d, HAL device: %s (%d)",
   4024                   nextRequest.halRequest.frame_number, strerror(-res), res);
   4025             cleanUpFailedRequests(/*sendRequestError*/ false);
   4026             return false;
   4027         }
   4028     }
   4029     return true;
   4030 }
   4031 
   4032 nsecs_t Camera3Device::RequestThread::calculateMaxExpectedDuration(const camera_metadata_t *request) {
   4033     nsecs_t maxExpectedDuration = kDefaultExpectedDuration;
   4034     camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
   4035     find_camera_metadata_ro_entry(request,
   4036             ANDROID_CONTROL_AE_MODE,
   4037             &e);
   4038     if (e.count == 0) return maxExpectedDuration;
   4039 
   4040     switch (e.data.u8[0]) {
   4041         case ANDROID_CONTROL_AE_MODE_OFF:
   4042             find_camera_metadata_ro_entry(request,
   4043                     ANDROID_SENSOR_EXPOSURE_TIME,
   4044                     &e);
   4045             if (e.count > 0) {
   4046                 maxExpectedDuration = e.data.i64[0];
   4047             }
   4048             find_camera_metadata_ro_entry(request,
   4049                     ANDROID_SENSOR_FRAME_DURATION,
   4050                     &e);
   4051             if (e.count > 0) {
   4052                 maxExpectedDuration = std::max(e.data.i64[0], maxExpectedDuration);
   4053             }
   4054             break;
   4055         default:
   4056             find_camera_metadata_ro_entry(request,
   4057                     ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
   4058                     &e);
   4059             if (e.count > 1) {
   4060                 maxExpectedDuration = 1e9 / e.data.u8[0];
   4061             }
   4062             break;
   4063     }
   4064 
   4065     return maxExpectedDuration;
   4066 }
   4067 
   4068 bool Camera3Device::RequestThread::threadLoop() {
   4069     ATRACE_CALL();
   4070     status_t res;
   4071 
   4072     // Handle paused state.
   4073     if (waitIfPaused()) {
   4074         return true;
   4075     }
   4076 
   4077     // Wait for the next batch of requests.
   4078     waitForNextRequestBatch();
   4079     if (mNextRequests.size() == 0) {
   4080         return true;
   4081     }
   4082 
   4083     // Get the latest request ID, if any
   4084     int latestRequestId;
   4085     camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
   4086             captureRequest->mSettings.find(ANDROID_REQUEST_ID);
   4087     if (requestIdEntry.count > 0) {
   4088         latestRequestId = requestIdEntry.data.i32[0];
   4089     } else {
   4090         ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
   4091         latestRequestId = NAME_NOT_FOUND;
   4092     }
   4093 
   4094     // Prepare a batch of HAL requests and output buffers.
   4095     res = prepareHalRequests();
   4096     if (res == TIMED_OUT) {
   4097         // Not a fatal error if getting output buffers time out.
   4098         cleanUpFailedRequests(/*sendRequestError*/ true);
   4099         // Check if any stream is abandoned.
   4100         checkAndStopRepeatingRequest();
   4101         return true;
   4102     } else if (res != OK) {
   4103         cleanUpFailedRequests(/*sendRequestError*/ false);
   4104         return false;
   4105     }
   4106 
   4107     // Inform waitUntilRequestProcessed thread of a new request ID
   4108     {
   4109         Mutex::Autolock al(mLatestRequestMutex);
   4110 
   4111         mLatestRequestId = latestRequestId;
   4112         mLatestRequestSignal.signal();
   4113     }
   4114 
   4115     // Submit a batch of requests to HAL.
   4116     // Use flush lock only when submitting multilple requests in a batch.
   4117     // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
   4118     // which may take a long time to finish so synchronizing flush() and
   4119     // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
   4120     // For now, only synchronize for high speed recording and we should figure something out for
   4121     // removing the synchronization.
   4122     bool useFlushLock = mNextRequests.size() > 1;
   4123 
   4124     if (useFlushLock) {
   4125         mFlushLock.lock();
   4126     }
   4127 
   4128     ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
   4129             mNextRequests.size());
   4130 
   4131     bool submitRequestSuccess = false;
   4132     nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
   4133     if (mInterface->supportBatchRequest()) {
   4134         submitRequestSuccess = sendRequestsBatch();
   4135     } else {
   4136         submitRequestSuccess = sendRequestsOneByOne();
   4137     }
   4138     nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
   4139     mRequestLatency.add(tRequestStart, tRequestEnd);
   4140 
   4141     if (useFlushLock) {
   4142         mFlushLock.unlock();
   4143     }
   4144 
   4145     // Unset as current request
   4146     {
   4147         Mutex::Autolock l(mRequestLock);
   4148         mNextRequests.clear();
   4149     }
   4150 
   4151     return submitRequestSuccess;
   4152 }
   4153 
   4154 status_t Camera3Device::RequestThread::prepareHalRequests() {
   4155     ATRACE_CALL();
   4156 
   4157     for (size_t i = 0; i < mNextRequests.size(); i++) {
   4158         auto& nextRequest = mNextRequests.editItemAt(i);
   4159         sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
   4160         camera3_capture_request_t* halRequest = &nextRequest.halRequest;
   4161         Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
   4162 
   4163         // Prepare a request to HAL
   4164         halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
   4165 
   4166         // Insert any queued triggers (before metadata is locked)
   4167         status_t res = insertTriggers(captureRequest);
   4168 
   4169         if (res < 0) {
   4170             SET_ERR("RequestThread: Unable to insert triggers "
   4171                     "(capture request %d, HAL device: %s (%d)",
   4172                     halRequest->frame_number, strerror(-res), res);
   4173             return INVALID_OPERATION;
   4174         }
   4175         int triggerCount = res;
   4176         bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
   4177         mPrevTriggers = triggerCount;
   4178 
   4179         // If the request is the same as last, or we had triggers last time
   4180         if (mPrevRequest != captureRequest || triggersMixedIn) {
   4181             /**
   4182              * HAL workaround:
   4183              * Insert a dummy trigger ID if a trigger is set but no trigger ID is
   4184              */
   4185             res = addDummyTriggerIds(captureRequest);
   4186             if (res != OK) {
   4187                 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
   4188                         "(capture request %d, HAL device: %s (%d)",
   4189                         halRequest->frame_number, strerror(-res), res);
   4190                 return INVALID_OPERATION;
   4191             }
   4192 
   4193             /**
   4194              * The request should be presorted so accesses in HAL
   4195              *   are O(logn). Sidenote, sorting a sorted metadata is nop.
   4196              */
   4197             captureRequest->mSettings.sort();
   4198             halRequest->settings = captureRequest->mSettings.getAndLock();
   4199             mPrevRequest = captureRequest;
   4200             ALOGVV("%s: Request settings are NEW", __FUNCTION__);
   4201 
   4202             IF_ALOGV() {
   4203                 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
   4204                 find_camera_metadata_ro_entry(
   4205                         halRequest->settings,
   4206                         ANDROID_CONTROL_AF_TRIGGER,
   4207                         &e
   4208                 );
   4209                 if (e.count > 0) {
   4210                     ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
   4211                           __FUNCTION__,
   4212                           halRequest->frame_number,
   4213                           e.data.u8[0]);
   4214                 }
   4215             }
   4216         } else {
   4217             // leave request.settings NULL to indicate 'reuse latest given'
   4218             ALOGVV("%s: Request settings are REUSED",
   4219                    __FUNCTION__);
   4220         }
   4221 
   4222         uint32_t totalNumBuffers = 0;
   4223 
   4224         // Fill in buffers
   4225         if (captureRequest->mInputStream != NULL) {
   4226             halRequest->input_buffer = &captureRequest->mInputBuffer;
   4227             totalNumBuffers += 1;
   4228         } else {
   4229             halRequest->input_buffer = NULL;
   4230         }
   4231 
   4232         outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
   4233                 captureRequest->mOutputStreams.size());
   4234         halRequest->output_buffers = outputBuffers->array();
   4235         for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
   4236             sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(j);
   4237 
   4238             // Prepare video buffers for high speed recording on the first video request.
   4239             if (mPrepareVideoStream && outputStream->isVideoStream()) {
   4240                 // Only try to prepare video stream on the first video request.
   4241                 mPrepareVideoStream = false;
   4242 
   4243                 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX);
   4244                 while (res == NOT_ENOUGH_DATA) {
   4245                     res = outputStream->prepareNextBuffer();
   4246                 }
   4247                 if (res != OK) {
   4248                     ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
   4249                         __FUNCTION__, strerror(-res), res);
   4250                     outputStream->cancelPrepare();
   4251                 }
   4252             }
   4253 
   4254             res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
   4255                     captureRequest->mOutputSurfaces[j]);
   4256             if (res != OK) {
   4257                 // Can't get output buffer from gralloc queue - this could be due to
   4258                 // abandoned queue or other consumer misbehavior, so not a fatal
   4259                 // error
   4260                 ALOGE("RequestThread: Can't get output buffer, skipping request:"
   4261                         " %s (%d)", strerror(-res), res);
   4262 
   4263                 return TIMED_OUT;
   4264             }
   4265             halRequest->num_output_buffers++;
   4266 
   4267         }
   4268         totalNumBuffers += halRequest->num_output_buffers;
   4269 
   4270         // Log request in the in-flight queue
   4271         sp<Camera3Device> parent = mParent.promote();
   4272         if (parent == NULL) {
   4273             // Should not happen, and nowhere to send errors to, so just log it
   4274             CLOGE("RequestThread: Parent is gone");
   4275             return INVALID_OPERATION;
   4276         }
   4277 
   4278         // If this request list is for constrained high speed recording (not
   4279         // preview), and the current request is not the last one in the batch,
   4280         // do not send callback to the app.
   4281         bool hasCallback = true;
   4282         if (mNextRequests[0].captureRequest->mBatchSize > 1 && i != mNextRequests.size()-1) {
   4283             hasCallback = false;
   4284         }
   4285         res = parent->registerInFlight(halRequest->frame_number,
   4286                 totalNumBuffers, captureRequest->mResultExtras,
   4287                 /*hasInput*/halRequest->input_buffer != NULL,
   4288                 hasCallback,
   4289                 calculateMaxExpectedDuration(halRequest->settings));
   4290         ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
   4291                ", burstId = %" PRId32 ".",
   4292                 __FUNCTION__,
   4293                 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
   4294                 captureRequest->mResultExtras.burstId);
   4295         if (res != OK) {
   4296             SET_ERR("RequestThread: Unable to register new in-flight request:"
   4297                     " %s (%d)", strerror(-res), res);
   4298             return INVALID_OPERATION;
   4299         }
   4300     }
   4301 
   4302     return OK;
   4303 }
   4304 
   4305 CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
   4306     ATRACE_CALL();
   4307     Mutex::Autolock al(mLatestRequestMutex);
   4308 
   4309     ALOGV("RequestThread::%s", __FUNCTION__);
   4310 
   4311     return mLatestRequest;
   4312 }
   4313 
   4314 bool Camera3Device::RequestThread::isStreamPending(
   4315         sp<Camera3StreamInterface>& stream) {
   4316     ATRACE_CALL();
   4317     Mutex::Autolock l(mRequestLock);
   4318 
   4319     for (const auto& nextRequest : mNextRequests) {
   4320         if (!nextRequest.submitted) {
   4321             for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
   4322                 if (stream == s) return true;
   4323             }
   4324             if (stream == nextRequest.captureRequest->mInputStream) return true;
   4325         }
   4326     }
   4327 
   4328     for (const auto& request : mRequestQueue) {
   4329         for (const auto& s : request->mOutputStreams) {
   4330             if (stream == s) return true;
   4331         }
   4332         if (stream == request->mInputStream) return true;
   4333     }
   4334 
   4335     for (const auto& request : mRepeatingRequests) {
   4336         for (const auto& s : request->mOutputStreams) {
   4337             if (stream == s) return true;
   4338         }
   4339         if (stream == request->mInputStream) return true;
   4340     }
   4341 
   4342     return false;
   4343 }
   4344 
   4345 nsecs_t Camera3Device::getExpectedInFlightDuration() {
   4346     ATRACE_CALL();
   4347     Mutex::Autolock al(mInFlightLock);
   4348     return mExpectedInflightDuration > kMinInflightDuration ?
   4349             mExpectedInflightDuration : kMinInflightDuration;
   4350 }
   4351 
   4352 void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
   4353     if (mNextRequests.empty()) {
   4354         return;
   4355     }
   4356 
   4357     for (auto& nextRequest : mNextRequests) {
   4358         // Skip the ones that have been submitted successfully.
   4359         if (nextRequest.submitted) {
   4360             continue;
   4361         }
   4362 
   4363         sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
   4364         camera3_capture_request_t* halRequest = &nextRequest.halRequest;
   4365         Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
   4366 
   4367         if (halRequest->settings != NULL) {
   4368             captureRequest->mSettings.unlock(halRequest->settings);
   4369         }
   4370 
   4371         if (captureRequest->mInputStream != NULL) {
   4372             captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
   4373             captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
   4374         }
   4375 
   4376         for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
   4377             //Buffers that failed processing could still have
   4378             //valid acquire fence.
   4379             int acquireFence = (*outputBuffers)[i].acquire_fence;
   4380             if (0 <= acquireFence) {
   4381                 close(acquireFence);
   4382                 outputBuffers->editItemAt(i).acquire_fence = -1;
   4383             }
   4384             outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
   4385             captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
   4386         }
   4387 
   4388         if (sendRequestError) {
   4389             Mutex::Autolock l(mRequestLock);
   4390             sp<NotificationListener> listener = mListener.promote();
   4391             if (listener != NULL) {
   4392                 listener->notifyError(
   4393                         hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
   4394                         captureRequest->mResultExtras);
   4395             }
   4396         }
   4397 
   4398         // Remove yet-to-be submitted inflight request from inflightMap
   4399         {
   4400           sp<Camera3Device> parent = mParent.promote();
   4401           if (parent != NULL) {
   4402               Mutex::Autolock l(parent->mInFlightLock);
   4403               ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
   4404               if (idx >= 0) {
   4405                   ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
   4406                         __FUNCTION__, captureRequest->mResultExtras.frameNumber);
   4407                   parent->removeInFlightMapEntryLocked(idx);
   4408               }
   4409           }
   4410         }
   4411     }
   4412 
   4413     Mutex::Autolock l(mRequestLock);
   4414     mNextRequests.clear();
   4415 }
   4416 
   4417 void Camera3Device::RequestThread::waitForNextRequestBatch() {
   4418     ATRACE_CALL();
   4419     // Optimized a bit for the simple steady-state case (single repeating
   4420     // request), to avoid putting that request in the queue temporarily.
   4421     Mutex::Autolock l(mRequestLock);
   4422 
   4423     assert(mNextRequests.empty());
   4424 
   4425     NextRequest nextRequest;
   4426     nextRequest.captureRequest = waitForNextRequestLocked();
   4427     if (nextRequest.captureRequest == nullptr) {
   4428         return;
   4429     }
   4430 
   4431     nextRequest.halRequest = camera3_capture_request_t();
   4432     nextRequest.submitted = false;
   4433     mNextRequests.add(nextRequest);
   4434 
   4435     // Wait for additional requests
   4436     const size_t batchSize = nextRequest.captureRequest->mBatchSize;
   4437 
   4438     for (size_t i = 1; i < batchSize; i++) {
   4439         NextRequest additionalRequest;
   4440         additionalRequest.captureRequest = waitForNextRequestLocked();
   4441         if (additionalRequest.captureRequest == nullptr) {
   4442             break;
   4443         }
   4444 
   4445         additionalRequest.halRequest = camera3_capture_request_t();
   4446         additionalRequest.submitted = false;
   4447         mNextRequests.add(additionalRequest);
   4448     }
   4449 
   4450     if (mNextRequests.size() < batchSize) {
   4451         ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
   4452                 mNextRequests.size(), batchSize);
   4453         cleanUpFailedRequests(/*sendRequestError*/true);
   4454     }
   4455 
   4456     return;
   4457 }
   4458 
   4459 sp<Camera3Device::CaptureRequest>
   4460         Camera3Device::RequestThread::waitForNextRequestLocked() {
   4461     status_t res;
   4462     sp<CaptureRequest> nextRequest;
   4463 
   4464     while (mRequestQueue.empty()) {
   4465         if (!mRepeatingRequests.empty()) {
   4466             // Always atomically enqueue all requests in a repeating request
   4467             // list. Guarantees a complete in-sequence set of captures to
   4468             // application.
   4469             const RequestList &requests = mRepeatingRequests;
   4470             RequestList::const_iterator firstRequest =
   4471                     requests.begin();
   4472             nextRequest = *firstRequest;
   4473             mRequestQueue.insert(mRequestQueue.end(),
   4474                     ++firstRequest,
   4475                     requests.end());
   4476             // No need to wait any longer
   4477 
   4478             mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
   4479 
   4480             break;
   4481         }
   4482 
   4483         res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
   4484 
   4485         if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
   4486                 exitPending()) {
   4487             Mutex::Autolock pl(mPauseLock);
   4488             if (mPaused == false) {
   4489                 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
   4490                 mPaused = true;
   4491                 // Let the tracker know
   4492                 sp<StatusTracker> statusTracker = mStatusTracker.promote();
   4493                 if (statusTracker != 0) {
   4494                     statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
   4495                 }
   4496             }
   4497             // Stop waiting for now and let thread management happen
   4498             return NULL;
   4499         }
   4500     }
   4501 
   4502     if (nextRequest == NULL) {
   4503         // Don't have a repeating request already in hand, so queue
   4504         // must have an entry now.
   4505         RequestList::iterator firstRequest =
   4506                 mRequestQueue.begin();
   4507         nextRequest = *firstRequest;
   4508         mRequestQueue.erase(firstRequest);
   4509         if (mRequestQueue.empty() && !nextRequest->mRepeating) {
   4510             sp<NotificationListener> listener = mListener.promote();
   4511             if (listener != NULL) {
   4512                 listener->notifyRequestQueueEmpty();
   4513             }
   4514         }
   4515     }
   4516 
   4517     // In case we've been unpaused by setPaused clearing mDoPause, need to
   4518     // update internal pause state (capture/setRepeatingRequest unpause
   4519     // directly).
   4520     Mutex::Autolock pl(mPauseLock);
   4521     if (mPaused) {
   4522         ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
   4523         sp<StatusTracker> statusTracker = mStatusTracker.promote();
   4524         if (statusTracker != 0) {
   4525             statusTracker->markComponentActive(mStatusId);
   4526         }
   4527     }
   4528     mPaused = false;
   4529 
   4530     // Check if we've reconfigured since last time, and reset the preview
   4531     // request if so. Can't use 'NULL request == repeat' across configure calls.
   4532     if (mReconfigured) {
   4533         mPrevRequest.clear();
   4534         mReconfigured = false;
   4535     }
   4536 
   4537     if (nextRequest != NULL) {
   4538         nextRequest->mResultExtras.frameNumber = mFrameNumber++;
   4539         nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
   4540         nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
   4541 
   4542         // Since RequestThread::clear() removes buffers from the input stream,
   4543         // get the right buffer here before unlocking mRequestLock
   4544         if (nextRequest->mInputStream != NULL) {
   4545             res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
   4546             if (res != OK) {
   4547                 // Can't get input buffer from gralloc queue - this could be due to
   4548                 // disconnected queue or other producer misbehavior, so not a fatal
   4549                 // error
   4550                 ALOGE("%s: Can't get input buffer, skipping request:"
   4551                         " %s (%d)", __FUNCTION__, strerror(-res), res);
   4552 
   4553                 sp<NotificationListener> listener = mListener.promote();
   4554                 if (listener != NULL) {
   4555                     listener->notifyError(
   4556                             hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
   4557                             nextRequest->mResultExtras);
   4558                 }
   4559                 return NULL;
   4560             }
   4561         }
   4562     }
   4563 
   4564     return nextRequest;
   4565 }
   4566 
   4567 bool Camera3Device::RequestThread::waitIfPaused() {
   4568     ATRACE_CALL();
   4569     status_t res;
   4570     Mutex::Autolock l(mPauseLock);
   4571     while (mDoPause) {
   4572         if (mPaused == false) {
   4573             mPaused = true;
   4574             ALOGV("%s: RequestThread: Paused", __FUNCTION__);
   4575             // Let the tracker know
   4576             sp<StatusTracker> statusTracker = mStatusTracker.promote();
   4577             if (statusTracker != 0) {
   4578                 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
   4579             }
   4580         }
   4581 
   4582         res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
   4583         if (res == TIMED_OUT || exitPending()) {
   4584             return true;
   4585         }
   4586     }
   4587     // We don't set mPaused to false here, because waitForNextRequest needs
   4588     // to further manage the paused state in case of starvation.
   4589     return false;
   4590 }
   4591 
   4592 void Camera3Device::RequestThread::unpauseForNewRequests() {
   4593     ATRACE_CALL();
   4594     // With work to do, mark thread as unpaused.
   4595     // If paused by request (setPaused), don't resume, to avoid
   4596     // extra signaling/waiting overhead to waitUntilPaused
   4597     mRequestSignal.signal();
   4598     Mutex::Autolock p(mPauseLock);
   4599     if (!mDoPause) {
   4600         ALOGV("%s: RequestThread: Going active", __FUNCTION__);
   4601         if (mPaused) {
   4602             sp<StatusTracker> statusTracker = mStatusTracker.promote();
   4603             if (statusTracker != 0) {
   4604                 statusTracker->markComponentActive(mStatusId);
   4605             }
   4606         }
   4607         mPaused = false;
   4608     }
   4609 }
   4610 
   4611 void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
   4612     sp<Camera3Device> parent = mParent.promote();
   4613     if (parent != NULL) {
   4614         va_list args;
   4615         va_start(args, fmt);
   4616 
   4617         parent->setErrorStateV(fmt, args);
   4618 
   4619         va_end(args);
   4620     }
   4621 }
   4622 
   4623 status_t Camera3Device::RequestThread::insertTriggers(
   4624         const sp<CaptureRequest> &request) {
   4625     ATRACE_CALL();
   4626     Mutex::Autolock al(mTriggerMutex);
   4627 
   4628     sp<Camera3Device> parent = mParent.promote();
   4629     if (parent == NULL) {
   4630         CLOGE("RequestThread: Parent is gone");
   4631         return DEAD_OBJECT;
   4632     }
   4633 
   4634     CameraMetadata &metadata = request->mSettings;
   4635     size_t count = mTriggerMap.size();
   4636 
   4637     for (size_t i = 0; i < count; ++i) {
   4638         RequestTrigger trigger = mTriggerMap.valueAt(i);
   4639         uint32_t tag = trigger.metadataTag;
   4640 
   4641         if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
   4642             bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
   4643             uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
   4644             if (isAeTrigger) {
   4645                 request->mResultExtras.precaptureTriggerId = triggerId;
   4646                 mCurrentPreCaptureTriggerId = triggerId;
   4647             } else {
   4648                 request->mResultExtras.afTriggerId = triggerId;
   4649                 mCurrentAfTriggerId = triggerId;
   4650             }
   4651             continue;
   4652         }
   4653 
   4654         camera_metadata_entry entry = metadata.find(tag);
   4655 
   4656         if (entry.count > 0) {
   4657             /**
   4658              * Already has an entry for this trigger in the request.
   4659              * Rewrite it with our requested trigger value.
   4660              */
   4661             RequestTrigger oldTrigger = trigger;
   4662 
   4663             oldTrigger.entryValue = entry.data.u8[0];
   4664 
   4665             mTriggerReplacedMap.add(tag, oldTrigger);
   4666         } else {
   4667             /**
   4668              * More typical, no trigger entry, so we just add it
   4669              */
   4670             mTriggerRemovedMap.add(tag, trigger);
   4671         }
   4672 
   4673         status_t res;
   4674 
   4675         switch (trigger.getTagType()) {
   4676             case TYPE_BYTE: {
   4677                 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
   4678                 res = metadata.update(tag,
   4679                                       &entryValue,
   4680                                       /*count*/1);
   4681                 break;
   4682             }
   4683             case TYPE_INT32:
   4684                 res = metadata.update(tag,
   4685                                       &trigger.entryValue,
   4686                                       /*count*/1);
   4687                 break;
   4688             default:
   4689                 ALOGE("%s: Type not supported: 0x%x",
   4690                       __FUNCTION__,
   4691                       trigger.getTagType());
   4692                 return INVALID_OPERATION;
   4693         }
   4694 
   4695         if (res != OK) {
   4696             ALOGE("%s: Failed to update request metadata with trigger tag %s"
   4697                   ", value %d", __FUNCTION__, trigger.getTagName(),
   4698                   trigger.entryValue);
   4699             return res;
   4700         }
   4701 
   4702         ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
   4703               trigger.getTagName(),
   4704               trigger.entryValue);
   4705     }
   4706 
   4707     mTriggerMap.clear();
   4708 
   4709     return count;
   4710 }
   4711 
   4712 status_t Camera3Device::RequestThread::removeTriggers(
   4713         const sp<CaptureRequest> &request) {
   4714     ATRACE_CALL();
   4715     Mutex::Autolock al(mTriggerMutex);
   4716 
   4717     CameraMetadata &metadata = request->mSettings;
   4718 
   4719     /**
   4720      * Replace all old entries with their old values.
   4721      */
   4722     for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
   4723         RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
   4724 
   4725         status_t res;
   4726 
   4727         uint32_t tag = trigger.metadataTag;
   4728         switch (trigger.getTagType()) {
   4729             case TYPE_BYTE: {
   4730                 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
   4731                 res = metadata.update(tag,
   4732                                       &entryValue,
   4733                                       /*count*/1);
   4734                 break;
   4735             }
   4736             case TYPE_INT32:
   4737                 res = metadata.update(tag,
   4738                                       &trigger.entryValue,
   4739                                       /*count*/1);
   4740                 break;
   4741             default:
   4742                 ALOGE("%s: Type not supported: 0x%x",
   4743                       __FUNCTION__,
   4744                       trigger.getTagType());
   4745                 return INVALID_OPERATION;
   4746         }
   4747 
   4748         if (res != OK) {
   4749             ALOGE("%s: Failed to restore request metadata with trigger tag %s"
   4750                   ", trigger value %d", __FUNCTION__,
   4751                   trigger.getTagName(), trigger.entryValue);
   4752             return res;
   4753         }
   4754     }
   4755     mTriggerReplacedMap.clear();
   4756 
   4757     /**
   4758      * Remove all new entries.
   4759      */
   4760     for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
   4761         RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
   4762         status_t res = metadata.erase(trigger.metadataTag);
   4763 
   4764         if (res != OK) {
   4765             ALOGE("%s: Failed to erase metadata with trigger tag %s"
   4766                   ", trigger value %d", __FUNCTION__,
   4767                   trigger.getTagName(), trigger.entryValue);
   4768             return res;
   4769         }
   4770     }
   4771     mTriggerRemovedMap.clear();
   4772 
   4773     return OK;
   4774 }
   4775 
   4776 status_t Camera3Device::RequestThread::addDummyTriggerIds(
   4777         const sp<CaptureRequest> &request) {
   4778     // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
   4779     static const int32_t dummyTriggerId = 1;
   4780     status_t res;
   4781 
   4782     CameraMetadata &metadata = request->mSettings;
   4783 
   4784     // If AF trigger is active, insert a dummy AF trigger ID if none already
   4785     // exists
   4786     camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
   4787     camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
   4788     if (afTrigger.count > 0 &&
   4789             afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
   4790             afId.count == 0) {
   4791         res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
   4792         if (res != OK) return res;
   4793     }
   4794 
   4795     // If AE precapture trigger is active, insert a dummy precapture trigger ID
   4796     // if none already exists
   4797     camera_metadata_entry pcTrigger =
   4798             metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
   4799     camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
   4800     if (pcTrigger.count > 0 &&
   4801             pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
   4802             pcId.count == 0) {
   4803         res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
   4804                 &dummyTriggerId, 1);
   4805         if (res != OK) return res;
   4806     }
   4807 
   4808     return OK;
   4809 }
   4810 
   4811 /**
   4812  * PreparerThread inner class methods
   4813  */
   4814 
   4815 Camera3Device::PreparerThread::PreparerThread() :
   4816         Thread(/*canCallJava*/false), mListener(nullptr),
   4817         mActive(false), mCancelNow(false) {
   4818 }
   4819 
   4820 Camera3Device::PreparerThread::~PreparerThread() {
   4821     Thread::requestExitAndWait();
   4822     if (mCurrentStream != nullptr) {
   4823         mCurrentStream->cancelPrepare();
   4824         ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
   4825         mCurrentStream.clear();
   4826     }
   4827     clear();
   4828 }
   4829 
   4830 status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
   4831     ATRACE_CALL();
   4832     status_t res;
   4833 
   4834     Mutex::Autolock l(mLock);
   4835     sp<NotificationListener> listener = mListener.promote();
   4836 
   4837     res = stream->startPrepare(maxCount);
   4838     if (res == OK) {
   4839         // No preparation needed, fire listener right off
   4840         ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
   4841         if (listener != NULL) {
   4842             listener->notifyPrepared(stream->getId());
   4843         }
   4844         return OK;
   4845     } else if (res != NOT_ENOUGH_DATA) {
   4846         return res;
   4847     }
   4848 
   4849     // Need to prepare, start up thread if necessary
   4850     if (!mActive) {
   4851         // mRunning will change to false before the thread fully shuts down, so wait to be sure it
   4852         // isn't running
   4853         Thread::requestExitAndWait();
   4854         res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
   4855         if (res != OK) {
   4856             ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
   4857             if (listener != NULL) {
   4858                 listener->notifyPrepared(stream->getId());
   4859             }
   4860             return res;
   4861         }
   4862         mCancelNow = false;
   4863         mActive = true;
   4864         ALOGV("%s: Preparer stream started", __FUNCTION__);
   4865     }
   4866 
   4867     // queue up the work
   4868     mPendingStreams.push_back(stream);
   4869     ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
   4870 
   4871     return OK;
   4872 }
   4873 
   4874 status_t Camera3Device::PreparerThread::clear() {
   4875     ATRACE_CALL();
   4876     Mutex::Autolock l(mLock);
   4877 
   4878     for (const auto& stream : mPendingStreams) {
   4879         stream->cancelPrepare();
   4880     }
   4881     mPendingStreams.clear();
   4882     mCancelNow = true;
   4883 
   4884     return OK;
   4885 }
   4886 
   4887 void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
   4888     ATRACE_CALL();
   4889     Mutex::Autolock l(mLock);
   4890     mListener = listener;
   4891 }
   4892 
   4893 bool Camera3Device::PreparerThread::threadLoop() {
   4894     status_t res;
   4895     {
   4896         Mutex::Autolock l(mLock);
   4897         if (mCurrentStream == nullptr) {
   4898             // End thread if done with work
   4899             if (mPendingStreams.empty()) {
   4900                 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
   4901                 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
   4902                 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
   4903                 mActive = false;
   4904                 return false;
   4905             }
   4906 
   4907             // Get next stream to prepare
   4908             auto it = mPendingStreams.begin();
   4909             mCurrentStream = *it;
   4910             mPendingStreams.erase(it);
   4911             ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
   4912             ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
   4913         } else if (mCancelNow) {
   4914             mCurrentStream->cancelPrepare();
   4915             ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
   4916             ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
   4917             mCurrentStream.clear();
   4918             mCancelNow = false;
   4919             return true;
   4920         }
   4921     }
   4922 
   4923     res = mCurrentStream->prepareNextBuffer();
   4924     if (res == NOT_ENOUGH_DATA) return true;
   4925     if (res != OK) {
   4926         // Something bad happened; try to recover by cancelling prepare and
   4927         // signalling listener anyway
   4928         ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
   4929                 mCurrentStream->getId(), res, strerror(-res));
   4930         mCurrentStream->cancelPrepare();
   4931     }
   4932 
   4933     // This stream has finished, notify listener
   4934     Mutex::Autolock l(mLock);
   4935     sp<NotificationListener> listener = mListener.promote();
   4936     if (listener != NULL) {
   4937         ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
   4938                 mCurrentStream->getId());
   4939         listener->notifyPrepared(mCurrentStream->getId());
   4940     }
   4941 
   4942     ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
   4943     mCurrentStream.clear();
   4944 
   4945     return true;
   4946 }
   4947 
   4948 /**
   4949  * Static callback forwarding methods from HAL to instance
   4950  */
   4951 
   4952 void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
   4953         const camera3_capture_result *result) {
   4954     Camera3Device *d =
   4955             const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
   4956 
   4957     d->processCaptureResult(result);
   4958 }
   4959 
   4960 void Camera3Device::sNotify(const camera3_callback_ops *cb,
   4961         const camera3_notify_msg *msg) {
   4962     Camera3Device *d =
   4963             const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
   4964     d->notify(msg);
   4965 }
   4966 
   4967 }; // namespace android
   4968