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 %d: %s: " fmt, mId, __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/Camera3ZslStream.h"
     55 #include "device3/Camera3DummyStream.h"
     56 #include "CameraService.h"
     57 
     58 using namespace android::camera3;
     59 
     60 namespace android {
     61 
     62 Camera3Device::Camera3Device(int id):
     63         mId(id),
     64         mIsConstrainedHighSpeedConfiguration(false),
     65         mHal3Device(NULL),
     66         mStatus(STATUS_UNINITIALIZED),
     67         mStatusWaiters(0),
     68         mUsePartialResult(false),
     69         mNumPartialResults(1),
     70         mTimestampOffset(0),
     71         mNextResultFrameNumber(0),
     72         mNextReprocessResultFrameNumber(0),
     73         mNextShutterFrameNumber(0),
     74         mNextReprocessShutterFrameNumber(0),
     75         mListener(NULL)
     76 {
     77     ATRACE_CALL();
     78     camera3_callback_ops::notify = &sNotify;
     79     camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
     80     ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
     81 }
     82 
     83 Camera3Device::~Camera3Device()
     84 {
     85     ATRACE_CALL();
     86     ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
     87     disconnect();
     88 }
     89 
     90 int Camera3Device::getId() const {
     91     return mId;
     92 }
     93 
     94 /**
     95  * CameraDeviceBase interface
     96  */
     97 
     98 status_t Camera3Device::initialize(CameraModule *module)
     99 {
    100     ATRACE_CALL();
    101     Mutex::Autolock il(mInterfaceLock);
    102     Mutex::Autolock l(mLock);
    103 
    104     ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
    105     if (mStatus != STATUS_UNINITIALIZED) {
    106         CLOGE("Already initialized!");
    107         return INVALID_OPERATION;
    108     }
    109 
    110     /** Open HAL device */
    111 
    112     status_t res;
    113     String8 deviceName = String8::format("%d", mId);
    114 
    115     camera3_device_t *device;
    116 
    117     ATRACE_BEGIN("camera3->open");
    118     res = module->open(deviceName.string(),
    119             reinterpret_cast<hw_device_t**>(&device));
    120     ATRACE_END();
    121 
    122     if (res != OK) {
    123         SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
    124         return res;
    125     }
    126 
    127     /** Cross-check device version */
    128     if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
    129         SET_ERR_L("Could not open camera: "
    130                 "Camera device should be at least %x, reports %x instead",
    131                 CAMERA_DEVICE_API_VERSION_3_0,
    132                 device->common.version);
    133         device->common.close(&device->common);
    134         return BAD_VALUE;
    135     }
    136 
    137     camera_info info;
    138     res = module->getCameraInfo(mId, &info);
    139     if (res != OK) return res;
    140 
    141     if (info.device_version != device->common.version) {
    142         SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
    143                 " and device version (%x).",
    144                 info.device_version, device->common.version);
    145         device->common.close(&device->common);
    146         return BAD_VALUE;
    147     }
    148 
    149     /** Initialize device with callback functions */
    150 
    151     ATRACE_BEGIN("camera3->initialize");
    152     res = device->ops->initialize(device, this);
    153     ATRACE_END();
    154 
    155     if (res != OK) {
    156         SET_ERR_L("Unable to initialize HAL device: %s (%d)",
    157                 strerror(-res), res);
    158         device->common.close(&device->common);
    159         return BAD_VALUE;
    160     }
    161 
    162     /** Start up status tracker thread */
    163     mStatusTracker = new StatusTracker(this);
    164     res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
    165     if (res != OK) {
    166         SET_ERR_L("Unable to start status tracking thread: %s (%d)",
    167                 strerror(-res), res);
    168         device->common.close(&device->common);
    169         mStatusTracker.clear();
    170         return res;
    171     }
    172 
    173     /** Register in-flight map to the status tracker */
    174     mInFlightStatusId = mStatusTracker->addComponent();
    175 
    176     /** Create buffer manager */
    177     mBufferManager = new Camera3BufferManager();
    178 
    179     bool aeLockAvailable = false;
    180     camera_metadata_ro_entry aeLockAvailableEntry;
    181     res = find_camera_metadata_ro_entry(info.static_camera_characteristics,
    182             ANDROID_CONTROL_AE_LOCK_AVAILABLE, &aeLockAvailableEntry);
    183     if (res == OK && aeLockAvailableEntry.count > 0) {
    184         aeLockAvailable = (aeLockAvailableEntry.data.u8[0] ==
    185                 ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE);
    186     }
    187 
    188     /** Start up request queue thread */
    189     mRequestThread = new RequestThread(this, mStatusTracker, device, aeLockAvailable);
    190     res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
    191     if (res != OK) {
    192         SET_ERR_L("Unable to start request queue thread: %s (%d)",
    193                 strerror(-res), res);
    194         device->common.close(&device->common);
    195         mRequestThread.clear();
    196         return res;
    197     }
    198 
    199     mPreparerThread = new PreparerThread();
    200 
    201     /** Everything is good to go */
    202 
    203     mDeviceVersion = device->common.version;
    204     mDeviceInfo = info.static_camera_characteristics;
    205     mHal3Device = device;
    206 
    207     // Determine whether we need to derive sensitivity boost values for older devices.
    208     // If post-RAW sensitivity boost range is listed, so should post-raw sensitivity control
    209     // be listed (as the default value 100)
    210     if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_4 &&
    211             mDeviceInfo.exists(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST_RANGE)) {
    212         mDerivePostRawSensKey = true;
    213     }
    214 
    215     internalUpdateStatusLocked(STATUS_UNCONFIGURED);
    216     mNextStreamId = 0;
    217     mDummyStreamId = NO_STREAM;
    218     mNeedConfig = true;
    219     mPauseStateNotify = false;
    220 
    221     // Measure the clock domain offset between camera and video/hw_composer
    222     camera_metadata_entry timestampSource =
    223             mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
    224     if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
    225             ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
    226         mTimestampOffset = getMonoToBoottimeOffset();
    227     }
    228 
    229     // Will the HAL be sending in early partial result metadata?
    230     if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
    231         camera_metadata_entry partialResultsCount =
    232                 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
    233         if (partialResultsCount.count > 0) {
    234             mNumPartialResults = partialResultsCount.data.i32[0];
    235             mUsePartialResult = (mNumPartialResults > 1);
    236         }
    237     } else {
    238         camera_metadata_entry partialResultsQuirk =
    239                 mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
    240         if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
    241             mUsePartialResult = true;
    242         }
    243     }
    244 
    245     camera_metadata_entry configs =
    246             mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
    247     for (uint32_t i = 0; i < configs.count; i += 4) {
    248         if (configs.data.i32[i] == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED &&
    249                 configs.data.i32[i + 3] ==
    250                 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_INPUT) {
    251             mSupportedOpaqueInputSizes.add(Size(configs.data.i32[i + 1],
    252                     configs.data.i32[i + 2]));
    253         }
    254     }
    255 
    256     return OK;
    257 }
    258 
    259 status_t Camera3Device::disconnect() {
    260     ATRACE_CALL();
    261     Mutex::Autolock il(mInterfaceLock);
    262 
    263     ALOGI("%s: E", __FUNCTION__);
    264 
    265     status_t res = OK;
    266 
    267     {
    268         Mutex::Autolock l(mLock);
    269         if (mStatus == STATUS_UNINITIALIZED) return res;
    270 
    271         if (mStatus == STATUS_ACTIVE ||
    272                 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
    273             res = mRequestThread->clearRepeatingRequests();
    274             if (res != OK) {
    275                 SET_ERR_L("Can't stop streaming");
    276                 // Continue to close device even in case of error
    277             } else {
    278                 res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
    279                 if (res != OK) {
    280                     SET_ERR_L("Timeout waiting for HAL to drain");
    281                     // Continue to close device even in case of error
    282                 }
    283             }
    284         }
    285 
    286         if (mStatus == STATUS_ERROR) {
    287             CLOGE("Shutting down in an error state");
    288         }
    289 
    290         if (mStatusTracker != NULL) {
    291             mStatusTracker->requestExit();
    292         }
    293 
    294         if (mRequestThread != NULL) {
    295             mRequestThread->requestExit();
    296         }
    297 
    298         mOutputStreams.clear();
    299         mInputStream.clear();
    300     }
    301 
    302     // Joining done without holding mLock, otherwise deadlocks may ensue
    303     // as the threads try to access parent state
    304     if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
    305         // HAL may be in a bad state, so waiting for request thread
    306         // (which may be stuck in the HAL processCaptureRequest call)
    307         // could be dangerous.
    308         mRequestThread->join();
    309     }
    310 
    311     if (mStatusTracker != NULL) {
    312         mStatusTracker->join();
    313     }
    314 
    315     camera3_device_t *hal3Device;
    316     {
    317         Mutex::Autolock l(mLock);
    318 
    319         mRequestThread.clear();
    320         mStatusTracker.clear();
    321         mBufferManager.clear();
    322 
    323         hal3Device = mHal3Device;
    324     }
    325 
    326     // Call close without internal mutex held, as the HAL close may need to
    327     // wait on assorted callbacks,etc, to complete before it can return.
    328     if (hal3Device != NULL) {
    329         ATRACE_BEGIN("camera3->close");
    330         hal3Device->common.close(&hal3Device->common);
    331         ATRACE_END();
    332     }
    333 
    334     {
    335         Mutex::Autolock l(mLock);
    336         mHal3Device = NULL;
    337         internalUpdateStatusLocked(STATUS_UNINITIALIZED);
    338     }
    339 
    340     ALOGI("%s: X", __FUNCTION__);
    341     return res;
    342 }
    343 
    344 // For dumping/debugging only -
    345 // try to acquire a lock a few times, eventually give up to proceed with
    346 // debug/dump operations
    347 bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
    348     bool gotLock = false;
    349     for (size_t i = 0; i < kDumpLockAttempts; ++i) {
    350         if (lock.tryLock() == NO_ERROR) {
    351             gotLock = true;
    352             break;
    353         } else {
    354             usleep(kDumpSleepDuration);
    355         }
    356     }
    357     return gotLock;
    358 }
    359 
    360 Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
    361     int32_t maxJpegWidth = 0, maxJpegHeight = 0;
    362     if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
    363         const int STREAM_CONFIGURATION_SIZE = 4;
    364         const int STREAM_FORMAT_OFFSET = 0;
    365         const int STREAM_WIDTH_OFFSET = 1;
    366         const int STREAM_HEIGHT_OFFSET = 2;
    367         const int STREAM_IS_INPUT_OFFSET = 3;
    368         camera_metadata_ro_entry_t availableStreamConfigs =
    369                 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
    370         if (availableStreamConfigs.count == 0 ||
    371                 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
    372             return Size(0, 0);
    373         }
    374 
    375         // Get max jpeg size (area-wise).
    376         for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
    377             int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
    378             int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
    379             int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
    380             int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
    381             if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
    382                     && format == HAL_PIXEL_FORMAT_BLOB &&
    383                     (width * height > maxJpegWidth * maxJpegHeight)) {
    384                 maxJpegWidth = width;
    385                 maxJpegHeight = height;
    386             }
    387         }
    388     } else {
    389         camera_metadata_ro_entry availableJpegSizes =
    390                 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
    391         if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
    392             return Size(0, 0);
    393         }
    394 
    395         // Get max jpeg size (area-wise).
    396         for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
    397             if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
    398                     > (maxJpegWidth * maxJpegHeight)) {
    399                 maxJpegWidth = availableJpegSizes.data.i32[i];
    400                 maxJpegHeight = availableJpegSizes.data.i32[i + 1];
    401             }
    402         }
    403     }
    404     return Size(maxJpegWidth, maxJpegHeight);
    405 }
    406 
    407 nsecs_t Camera3Device::getMonoToBoottimeOffset() {
    408     // try three times to get the clock offset, choose the one
    409     // with the minimum gap in measurements.
    410     const int tries = 3;
    411     nsecs_t bestGap, measured;
    412     for (int i = 0; i < tries; ++i) {
    413         const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
    414         const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
    415         const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
    416         const nsecs_t gap = tmono2 - tmono;
    417         if (i == 0 || gap < bestGap) {
    418             bestGap = gap;
    419             measured = tbase - ((tmono + tmono2) >> 1);
    420         }
    421     }
    422     return measured;
    423 }
    424 
    425 /**
    426  * Map Android N dataspace definitions back to Android M definitions, for
    427  * use with HALv3.3 or older.
    428  *
    429  * Only map where correspondences exist, and otherwise preserve the value.
    430  */
    431 android_dataspace Camera3Device::mapToLegacyDataspace(android_dataspace dataSpace) {
    432     switch (dataSpace) {
    433         case HAL_DATASPACE_V0_SRGB_LINEAR:
    434             return HAL_DATASPACE_SRGB_LINEAR;
    435         case HAL_DATASPACE_V0_SRGB:
    436             return HAL_DATASPACE_SRGB;
    437         case HAL_DATASPACE_V0_JFIF:
    438             return HAL_DATASPACE_JFIF;
    439         case HAL_DATASPACE_V0_BT601_625:
    440             return HAL_DATASPACE_BT601_625;
    441         case HAL_DATASPACE_V0_BT601_525:
    442             return HAL_DATASPACE_BT601_525;
    443         case HAL_DATASPACE_V0_BT709:
    444             return HAL_DATASPACE_BT709;
    445         default:
    446             return dataSpace;
    447     }
    448 }
    449 
    450 ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
    451     // Get max jpeg size (area-wise).
    452     Size maxJpegResolution = getMaxJpegResolution();
    453     if (maxJpegResolution.width == 0) {
    454         ALOGE("%s: Camera %d: Can't find valid available jpeg sizes in static metadata!",
    455                 __FUNCTION__, mId);
    456         return BAD_VALUE;
    457     }
    458 
    459     // Get max jpeg buffer size
    460     ssize_t maxJpegBufferSize = 0;
    461     camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
    462     if (jpegBufMaxSize.count == 0) {
    463         ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
    464         return BAD_VALUE;
    465     }
    466     maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
    467     assert(kMinJpegBufferSize < maxJpegBufferSize);
    468 
    469     // Calculate final jpeg buffer size for the given resolution.
    470     float scaleFactor = ((float) (width * height)) /
    471             (maxJpegResolution.width * maxJpegResolution.height);
    472     ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
    473             kMinJpegBufferSize;
    474     if (jpegBufferSize > maxJpegBufferSize) {
    475         jpegBufferSize = maxJpegBufferSize;
    476     }
    477 
    478     return jpegBufferSize;
    479 }
    480 
    481 ssize_t Camera3Device::getPointCloudBufferSize() const {
    482     const int FLOATS_PER_POINT=4;
    483     camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
    484     if (maxPointCount.count == 0) {
    485         ALOGE("%s: Camera %d: Can't find maximum depth point cloud size in static metadata!",
    486                 __FUNCTION__, mId);
    487         return BAD_VALUE;
    488     }
    489     ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
    490             maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
    491     return maxBytesForPointCloud;
    492 }
    493 
    494 ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
    495     const int PER_CONFIGURATION_SIZE = 3;
    496     const int WIDTH_OFFSET = 0;
    497     const int HEIGHT_OFFSET = 1;
    498     const int SIZE_OFFSET = 2;
    499     camera_metadata_ro_entry rawOpaqueSizes =
    500         mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
    501     size_t count = rawOpaqueSizes.count;
    502     if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
    503         ALOGE("%s: Camera %d: bad opaque RAW size static metadata length(%zu)!",
    504                 __FUNCTION__, mId, count);
    505         return BAD_VALUE;
    506     }
    507 
    508     for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
    509         if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
    510                 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
    511             return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
    512         }
    513     }
    514 
    515     ALOGE("%s: Camera %d: cannot find size for %dx%d opaque RAW image!",
    516             __FUNCTION__, mId, width, height);
    517     return BAD_VALUE;
    518 }
    519 
    520 status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
    521     ATRACE_CALL();
    522     (void)args;
    523 
    524     // Try to lock, but continue in case of failure (to avoid blocking in
    525     // deadlocks)
    526     bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
    527     bool gotLock = tryLockSpinRightRound(mLock);
    528 
    529     ALOGW_IF(!gotInterfaceLock,
    530             "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
    531             mId, __FUNCTION__);
    532     ALOGW_IF(!gotLock,
    533             "Camera %d: %s: Unable to lock main lock, proceeding anyway",
    534             mId, __FUNCTION__);
    535 
    536     bool dumpTemplates = false;
    537 
    538     String16 templatesOption("-t");
    539     String16 monitorOption("-m");
    540     int n = args.size();
    541     for (int i = 0; i < n; i++) {
    542         if (args[i] == templatesOption) {
    543             dumpTemplates = true;
    544         }
    545         if (args[i] == monitorOption) {
    546             if (i + 1 < n) {
    547                 String8 monitorTags = String8(args[i + 1]);
    548                 if (monitorTags == "off") {
    549                     mTagMonitor.disableMonitoring();
    550                 } else {
    551                     mTagMonitor.parseTagsToMonitor(monitorTags);
    552                 }
    553             } else {
    554                 mTagMonitor.disableMonitoring();
    555             }
    556         }
    557     }
    558 
    559     String8 lines;
    560 
    561     const char *status =
    562             mStatus == STATUS_ERROR         ? "ERROR" :
    563             mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
    564             mStatus == STATUS_UNCONFIGURED  ? "UNCONFIGURED" :
    565             mStatus == STATUS_CONFIGURED    ? "CONFIGURED" :
    566             mStatus == STATUS_ACTIVE        ? "ACTIVE" :
    567             "Unknown";
    568 
    569     lines.appendFormat("    Device status: %s\n", status);
    570     if (mStatus == STATUS_ERROR) {
    571         lines.appendFormat("    Error cause: %s\n", mErrorCause.string());
    572     }
    573     lines.appendFormat("    Stream configuration:\n");
    574     lines.appendFormat("    Operation mode: %s \n", mIsConstrainedHighSpeedConfiguration ?
    575             "CONSTRAINED HIGH SPEED VIDEO" : "NORMAL");
    576 
    577     if (mInputStream != NULL) {
    578         write(fd, lines.string(), lines.size());
    579         mInputStream->dump(fd, args);
    580     } else {
    581         lines.appendFormat("      No input stream.\n");
    582         write(fd, lines.string(), lines.size());
    583     }
    584     for (size_t i = 0; i < mOutputStreams.size(); i++) {
    585         mOutputStreams[i]->dump(fd,args);
    586     }
    587 
    588     if (mBufferManager != NULL) {
    589         lines = String8("    Camera3 Buffer Manager:\n");
    590         write(fd, lines.string(), lines.size());
    591         mBufferManager->dump(fd, args);
    592     }
    593 
    594     lines = String8("    In-flight requests:\n");
    595     if (mInFlightMap.size() == 0) {
    596         lines.append("      None\n");
    597     } else {
    598         for (size_t i = 0; i < mInFlightMap.size(); i++) {
    599             InFlightRequest r = mInFlightMap.valueAt(i);
    600             lines.appendFormat("      Frame %d |  Timestamp: %" PRId64 ", metadata"
    601                     " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
    602                     r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
    603                     r.numBuffersLeft);
    604         }
    605     }
    606     write(fd, lines.string(), lines.size());
    607 
    608     {
    609         lines = String8("    Last request sent:\n");
    610         write(fd, lines.string(), lines.size());
    611 
    612         CameraMetadata lastRequest = getLatestRequestLocked();
    613         lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
    614     }
    615 
    616     if (dumpTemplates) {
    617         const char *templateNames[] = {
    618             "TEMPLATE_PREVIEW",
    619             "TEMPLATE_STILL_CAPTURE",
    620             "TEMPLATE_VIDEO_RECORD",
    621             "TEMPLATE_VIDEO_SNAPSHOT",
    622             "TEMPLATE_ZERO_SHUTTER_LAG",
    623             "TEMPLATE_MANUAL"
    624         };
    625 
    626         for (int i = 1; i < CAMERA3_TEMPLATE_COUNT; i++) {
    627             const camera_metadata_t *templateRequest;
    628             templateRequest =
    629                 mHal3Device->ops->construct_default_request_settings(
    630                     mHal3Device, i);
    631             lines = String8::format("    HAL Request %s:\n", templateNames[i-1]);
    632             if (templateRequest == NULL) {
    633                 lines.append("       Not supported\n");
    634                 write(fd, lines.string(), lines.size());
    635             } else {
    636                 write(fd, lines.string(), lines.size());
    637                 dump_indented_camera_metadata(templateRequest,
    638                         fd, /*verbosity*/2, /*indentation*/8);
    639             }
    640         }
    641     }
    642 
    643     mTagMonitor.dumpMonitoredMetadata(fd);
    644 
    645     if (mHal3Device != NULL) {
    646         lines = String8("    HAL device dump:\n");
    647         write(fd, lines.string(), lines.size());
    648         mHal3Device->ops->dump(mHal3Device, fd);
    649     }
    650 
    651     if (gotLock) mLock.unlock();
    652     if (gotInterfaceLock) mInterfaceLock.unlock();
    653 
    654     return OK;
    655 }
    656 
    657 const CameraMetadata& Camera3Device::info() const {
    658     ALOGVV("%s: E", __FUNCTION__);
    659     if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
    660                     mStatus == STATUS_ERROR)) {
    661         ALOGW("%s: Access to static info %s!", __FUNCTION__,
    662                 mStatus == STATUS_ERROR ?
    663                 "when in error state" : "before init");
    664     }
    665     return mDeviceInfo;
    666 }
    667 
    668 status_t Camera3Device::checkStatusOkToCaptureLocked() {
    669     switch (mStatus) {
    670         case STATUS_ERROR:
    671             CLOGE("Device has encountered a serious error");
    672             return INVALID_OPERATION;
    673         case STATUS_UNINITIALIZED:
    674             CLOGE("Device not initialized");
    675             return INVALID_OPERATION;
    676         case STATUS_UNCONFIGURED:
    677         case STATUS_CONFIGURED:
    678         case STATUS_ACTIVE:
    679             // OK
    680             break;
    681         default:
    682             SET_ERR_L("Unexpected status: %d", mStatus);
    683             return INVALID_OPERATION;
    684     }
    685     return OK;
    686 }
    687 
    688 status_t Camera3Device::convertMetadataListToRequestListLocked(
    689         const List<const CameraMetadata> &metadataList, RequestList *requestList) {
    690     if (requestList == NULL) {
    691         CLOGE("requestList cannot be NULL.");
    692         return BAD_VALUE;
    693     }
    694 
    695     int32_t burstId = 0;
    696     for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
    697             it != metadataList.end(); ++it) {
    698         sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
    699         if (newRequest == 0) {
    700             CLOGE("Can't create capture request");
    701             return BAD_VALUE;
    702         }
    703 
    704         // Setup burst Id and request Id
    705         newRequest->mResultExtras.burstId = burstId++;
    706         if (it->exists(ANDROID_REQUEST_ID)) {
    707             if (it->find(ANDROID_REQUEST_ID).count == 0) {
    708                 CLOGE("RequestID entry exists; but must not be empty in metadata");
    709                 return BAD_VALUE;
    710             }
    711             newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
    712         } else {
    713             CLOGE("RequestID does not exist in metadata");
    714             return BAD_VALUE;
    715         }
    716 
    717         requestList->push_back(newRequest);
    718 
    719         ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
    720     }
    721 
    722     // Setup batch size if this is a high speed video recording request.
    723     if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
    724         auto firstRequest = requestList->begin();
    725         for (auto& outputStream : (*firstRequest)->mOutputStreams) {
    726             if (outputStream->isVideoStream()) {
    727                 (*firstRequest)->mBatchSize = requestList->size();
    728                 break;
    729             }
    730         }
    731     }
    732 
    733     return OK;
    734 }
    735 
    736 status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
    737     ATRACE_CALL();
    738 
    739     List<const CameraMetadata> requests;
    740     requests.push_back(request);
    741     return captureList(requests, /*lastFrameNumber*/NULL);
    742 }
    743 
    744 status_t Camera3Device::submitRequestsHelper(
    745         const List<const CameraMetadata> &requests, bool repeating,
    746         /*out*/
    747         int64_t *lastFrameNumber) {
    748     ATRACE_CALL();
    749     Mutex::Autolock il(mInterfaceLock);
    750     Mutex::Autolock l(mLock);
    751 
    752     status_t res = checkStatusOkToCaptureLocked();
    753     if (res != OK) {
    754         // error logged by previous call
    755         return res;
    756     }
    757 
    758     RequestList requestList;
    759 
    760     res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
    761     if (res != OK) {
    762         // error logged by previous call
    763         return res;
    764     }
    765 
    766     if (repeating) {
    767         res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
    768     } else {
    769         res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
    770     }
    771 
    772     if (res == OK) {
    773         waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
    774         if (res != OK) {
    775             SET_ERR_L("Can't transition to active in %f seconds!",
    776                     kActiveTimeout/1e9);
    777         }
    778         ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
    779               (*(requestList.begin()))->mResultExtras.requestId);
    780     } else {
    781         CLOGE("Cannot queue request. Impossible.");
    782         return BAD_VALUE;
    783     }
    784 
    785     return res;
    786 }
    787 
    788 status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
    789                                     int64_t *lastFrameNumber) {
    790     ATRACE_CALL();
    791 
    792     return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
    793 }
    794 
    795 status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
    796                                             int64_t* /*lastFrameNumber*/) {
    797     ATRACE_CALL();
    798 
    799     List<const CameraMetadata> requests;
    800     requests.push_back(request);
    801     return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
    802 }
    803 
    804 status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
    805                                                 int64_t *lastFrameNumber) {
    806     ATRACE_CALL();
    807 
    808     return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
    809 }
    810 
    811 sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
    812         const CameraMetadata &request) {
    813     status_t res;
    814 
    815     if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
    816         res = configureStreamsLocked();
    817         // Stream configuration failed. Client might try other configuraitons.
    818         if (res != OK) {
    819             CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
    820             return NULL;
    821         } else if (mStatus == STATUS_UNCONFIGURED) {
    822             // Stream configuration successfully configure to empty stream configuration.
    823             CLOGE("No streams configured");
    824             return NULL;
    825         }
    826     }
    827 
    828     sp<CaptureRequest> newRequest = createCaptureRequest(request);
    829     return newRequest;
    830 }
    831 
    832 status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
    833     ATRACE_CALL();
    834     Mutex::Autolock il(mInterfaceLock);
    835     Mutex::Autolock l(mLock);
    836 
    837     switch (mStatus) {
    838         case STATUS_ERROR:
    839             CLOGE("Device has encountered a serious error");
    840             return INVALID_OPERATION;
    841         case STATUS_UNINITIALIZED:
    842             CLOGE("Device not initialized");
    843             return INVALID_OPERATION;
    844         case STATUS_UNCONFIGURED:
    845         case STATUS_CONFIGURED:
    846         case STATUS_ACTIVE:
    847             // OK
    848             break;
    849         default:
    850             SET_ERR_L("Unexpected status: %d", mStatus);
    851             return INVALID_OPERATION;
    852     }
    853     ALOGV("Camera %d: Clearing repeating request", mId);
    854 
    855     return mRequestThread->clearRepeatingRequests(lastFrameNumber);
    856 }
    857 
    858 status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
    859     ATRACE_CALL();
    860     Mutex::Autolock il(mInterfaceLock);
    861 
    862     return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
    863 }
    864 
    865 status_t Camera3Device::createInputStream(
    866         uint32_t width, uint32_t height, int format, int *id) {
    867     ATRACE_CALL();
    868     Mutex::Autolock il(mInterfaceLock);
    869     Mutex::Autolock l(mLock);
    870     ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
    871             mId, mNextStreamId, width, height, format);
    872 
    873     status_t res;
    874     bool wasActive = false;
    875 
    876     switch (mStatus) {
    877         case STATUS_ERROR:
    878             ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
    879             return INVALID_OPERATION;
    880         case STATUS_UNINITIALIZED:
    881             ALOGE("%s: Device not initialized", __FUNCTION__);
    882             return INVALID_OPERATION;
    883         case STATUS_UNCONFIGURED:
    884         case STATUS_CONFIGURED:
    885             // OK
    886             break;
    887         case STATUS_ACTIVE:
    888             ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
    889             res = internalPauseAndWaitLocked();
    890             if (res != OK) {
    891                 SET_ERR_L("Can't pause captures to reconfigure streams!");
    892                 return res;
    893             }
    894             wasActive = true;
    895             break;
    896         default:
    897             SET_ERR_L("%s: Unexpected status: %d", mStatus);
    898             return INVALID_OPERATION;
    899     }
    900     assert(mStatus != STATUS_ACTIVE);
    901 
    902     if (mInputStream != 0) {
    903         ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
    904         return INVALID_OPERATION;
    905     }
    906 
    907     sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
    908                 width, height, format);
    909     newStream->setStatusTracker(mStatusTracker);
    910 
    911     mInputStream = newStream;
    912 
    913     *id = mNextStreamId++;
    914 
    915     // Continue captures if active at start
    916     if (wasActive) {
    917         ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
    918         res = configureStreamsLocked();
    919         if (res != OK) {
    920             ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
    921                     __FUNCTION__, mNextStreamId, strerror(-res), res);
    922             return res;
    923         }
    924         internalResumeLocked();
    925     }
    926 
    927     ALOGV("Camera %d: Created input stream", mId);
    928     return OK;
    929 }
    930 
    931 
    932 status_t Camera3Device::createZslStream(
    933             uint32_t width, uint32_t height,
    934             int depth,
    935             /*out*/
    936             int *id,
    937             sp<Camera3ZslStream>* zslStream) {
    938     ATRACE_CALL();
    939     Mutex::Autolock il(mInterfaceLock);
    940     Mutex::Autolock l(mLock);
    941     ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
    942             mId, mNextStreamId, width, height, depth);
    943 
    944     status_t res;
    945     bool wasActive = false;
    946 
    947     switch (mStatus) {
    948         case STATUS_ERROR:
    949             ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
    950             return INVALID_OPERATION;
    951         case STATUS_UNINITIALIZED:
    952             ALOGE("%s: Device not initialized", __FUNCTION__);
    953             return INVALID_OPERATION;
    954         case STATUS_UNCONFIGURED:
    955         case STATUS_CONFIGURED:
    956             // OK
    957             break;
    958         case STATUS_ACTIVE:
    959             ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
    960             res = internalPauseAndWaitLocked();
    961             if (res != OK) {
    962                 SET_ERR_L("Can't pause captures to reconfigure streams!");
    963                 return res;
    964             }
    965             wasActive = true;
    966             break;
    967         default:
    968             SET_ERR_L("Unexpected status: %d", mStatus);
    969             return INVALID_OPERATION;
    970     }
    971     assert(mStatus != STATUS_ACTIVE);
    972 
    973     if (mInputStream != 0) {
    974         ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
    975         return INVALID_OPERATION;
    976     }
    977 
    978     sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
    979                 width, height, depth);
    980     newStream->setStatusTracker(mStatusTracker);
    981 
    982     res = mOutputStreams.add(mNextStreamId, newStream);
    983     if (res < 0) {
    984         ALOGE("%s: Can't add new stream to set: %s (%d)",
    985                 __FUNCTION__, strerror(-res), res);
    986         return res;
    987     }
    988     mInputStream = newStream;
    989 
    990     mNeedConfig = true;
    991 
    992     *id = mNextStreamId++;
    993     *zslStream = newStream;
    994 
    995     // Continue captures if active at start
    996     if (wasActive) {
    997         ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
    998         res = configureStreamsLocked();
    999         if (res != OK) {
   1000             ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
   1001                     __FUNCTION__, mNextStreamId, strerror(-res), res);
   1002             return res;
   1003         }
   1004         internalResumeLocked();
   1005     }
   1006 
   1007     ALOGV("Camera %d: Created ZSL stream", mId);
   1008     return OK;
   1009 }
   1010 
   1011 status_t Camera3Device::createStream(sp<Surface> consumer,
   1012         uint32_t width, uint32_t height, int format, android_dataspace dataSpace,
   1013         camera3_stream_rotation_t rotation, int *id, int streamSetId, uint32_t consumerUsage) {
   1014     ATRACE_CALL();
   1015     Mutex::Autolock il(mInterfaceLock);
   1016     Mutex::Autolock l(mLock);
   1017     ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
   1018             " consumer usage 0x%x", mId, mNextStreamId, width, height, format, dataSpace, rotation,
   1019             consumerUsage);
   1020 
   1021     status_t res;
   1022     bool wasActive = false;
   1023 
   1024     switch (mStatus) {
   1025         case STATUS_ERROR:
   1026             CLOGE("Device has encountered a serious error");
   1027             return INVALID_OPERATION;
   1028         case STATUS_UNINITIALIZED:
   1029             CLOGE("Device not initialized");
   1030             return INVALID_OPERATION;
   1031         case STATUS_UNCONFIGURED:
   1032         case STATUS_CONFIGURED:
   1033             // OK
   1034             break;
   1035         case STATUS_ACTIVE:
   1036             ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
   1037             res = internalPauseAndWaitLocked();
   1038             if (res != OK) {
   1039                 SET_ERR_L("Can't pause captures to reconfigure streams!");
   1040                 return res;
   1041             }
   1042             wasActive = true;
   1043             break;
   1044         default:
   1045             SET_ERR_L("Unexpected status: %d", mStatus);
   1046             return INVALID_OPERATION;
   1047     }
   1048     assert(mStatus != STATUS_ACTIVE);
   1049 
   1050     sp<Camera3OutputStream> newStream;
   1051     // Overwrite stream set id to invalid for HAL3.2 or lower, as buffer manager does support
   1052     // such devices.
   1053     if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2) {
   1054         streamSetId = CAMERA3_STREAM_SET_ID_INVALID;
   1055     }
   1056 
   1057     // HAL3.1 doesn't support deferred consumer stream creation as it requires buffer registration
   1058     // which requires a consumer surface to be available.
   1059     if (consumer == nullptr && mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
   1060         ALOGE("HAL3.1 doesn't support deferred consumer stream creation");
   1061         return BAD_VALUE;
   1062     }
   1063 
   1064     if (consumer == nullptr && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
   1065         ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
   1066         return BAD_VALUE;
   1067     }
   1068 
   1069     // Use legacy dataspace values for older HALs
   1070     if (mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_3) {
   1071         dataSpace = mapToLegacyDataspace(dataSpace);
   1072     }
   1073     if (format == HAL_PIXEL_FORMAT_BLOB) {
   1074         ssize_t blobBufferSize;
   1075         if (dataSpace != HAL_DATASPACE_DEPTH) {
   1076             blobBufferSize = getJpegBufferSize(width, height);
   1077             if (blobBufferSize <= 0) {
   1078                 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
   1079                 return BAD_VALUE;
   1080             }
   1081         } else {
   1082             blobBufferSize = getPointCloudBufferSize();
   1083             if (blobBufferSize <= 0) {
   1084                 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
   1085                 return BAD_VALUE;
   1086             }
   1087         }
   1088         newStream = new Camera3OutputStream(mNextStreamId, consumer,
   1089                 width, height, blobBufferSize, format, dataSpace, rotation,
   1090                 mTimestampOffset, streamSetId);
   1091     } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
   1092         ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
   1093         if (rawOpaqueBufferSize <= 0) {
   1094             SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
   1095             return BAD_VALUE;
   1096         }
   1097         newStream = new Camera3OutputStream(mNextStreamId, consumer,
   1098                 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
   1099                 mTimestampOffset, streamSetId);
   1100     } else if (consumer == nullptr) {
   1101         newStream = new Camera3OutputStream(mNextStreamId,
   1102                 width, height, format, consumerUsage, dataSpace, rotation,
   1103                 mTimestampOffset, streamSetId);
   1104     } else {
   1105         newStream = new Camera3OutputStream(mNextStreamId, consumer,
   1106                 width, height, format, dataSpace, rotation,
   1107                 mTimestampOffset, streamSetId);
   1108     }
   1109     newStream->setStatusTracker(mStatusTracker);
   1110 
   1111     /**
   1112      * Camera3 Buffer manager is only supported by HAL3.3 onwards, as the older HALs ( < HAL3.2)
   1113      * requires buffers to be statically allocated for internal static buffer registration, while
   1114      * the buffers provided by buffer manager are really dynamically allocated. For HAL3.2, because
   1115      * not all HAL implementation supports dynamic buffer registeration, exlude it as well.
   1116      */
   1117     if (mDeviceVersion > CAMERA_DEVICE_API_VERSION_3_2) {
   1118         newStream->setBufferManager(mBufferManager);
   1119     }
   1120 
   1121     res = mOutputStreams.add(mNextStreamId, newStream);
   1122     if (res < 0) {
   1123         SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
   1124         return res;
   1125     }
   1126 
   1127     *id = mNextStreamId++;
   1128     mNeedConfig = true;
   1129 
   1130     // Continue captures if active at start
   1131     if (wasActive) {
   1132         ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
   1133         res = configureStreamsLocked();
   1134         if (res != OK) {
   1135             CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
   1136                     mNextStreamId, strerror(-res), res);
   1137             return res;
   1138         }
   1139         internalResumeLocked();
   1140     }
   1141     ALOGV("Camera %d: Created new stream", mId);
   1142     return OK;
   1143 }
   1144 
   1145 status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
   1146     ATRACE_CALL();
   1147     (void)outputId; (void)id;
   1148 
   1149     CLOGE("Unimplemented");
   1150     return INVALID_OPERATION;
   1151 }
   1152 
   1153 
   1154 status_t Camera3Device::getStreamInfo(int id,
   1155         uint32_t *width, uint32_t *height,
   1156         uint32_t *format, android_dataspace *dataSpace) {
   1157     ATRACE_CALL();
   1158     Mutex::Autolock il(mInterfaceLock);
   1159     Mutex::Autolock l(mLock);
   1160 
   1161     switch (mStatus) {
   1162         case STATUS_ERROR:
   1163             CLOGE("Device has encountered a serious error");
   1164             return INVALID_OPERATION;
   1165         case STATUS_UNINITIALIZED:
   1166             CLOGE("Device not initialized!");
   1167             return INVALID_OPERATION;
   1168         case STATUS_UNCONFIGURED:
   1169         case STATUS_CONFIGURED:
   1170         case STATUS_ACTIVE:
   1171             // OK
   1172             break;
   1173         default:
   1174             SET_ERR_L("Unexpected status: %d", mStatus);
   1175             return INVALID_OPERATION;
   1176     }
   1177 
   1178     ssize_t idx = mOutputStreams.indexOfKey(id);
   1179     if (idx == NAME_NOT_FOUND) {
   1180         CLOGE("Stream %d is unknown", id);
   1181         return idx;
   1182     }
   1183 
   1184     if (width) *width  = mOutputStreams[idx]->getWidth();
   1185     if (height) *height = mOutputStreams[idx]->getHeight();
   1186     if (format) *format = mOutputStreams[idx]->getFormat();
   1187     if (dataSpace) *dataSpace = mOutputStreams[idx]->getDataSpace();
   1188     return OK;
   1189 }
   1190 
   1191 status_t Camera3Device::setStreamTransform(int id,
   1192         int transform) {
   1193     ATRACE_CALL();
   1194     Mutex::Autolock il(mInterfaceLock);
   1195     Mutex::Autolock l(mLock);
   1196 
   1197     switch (mStatus) {
   1198         case STATUS_ERROR:
   1199             CLOGE("Device has encountered a serious error");
   1200             return INVALID_OPERATION;
   1201         case STATUS_UNINITIALIZED:
   1202             CLOGE("Device not initialized");
   1203             return INVALID_OPERATION;
   1204         case STATUS_UNCONFIGURED:
   1205         case STATUS_CONFIGURED:
   1206         case STATUS_ACTIVE:
   1207             // OK
   1208             break;
   1209         default:
   1210             SET_ERR_L("Unexpected status: %d", mStatus);
   1211             return INVALID_OPERATION;
   1212     }
   1213 
   1214     ssize_t idx = mOutputStreams.indexOfKey(id);
   1215     if (idx == NAME_NOT_FOUND) {
   1216         CLOGE("Stream %d does not exist",
   1217                 id);
   1218         return BAD_VALUE;
   1219     }
   1220 
   1221     return mOutputStreams.editValueAt(idx)->setTransform(transform);
   1222 }
   1223 
   1224 status_t Camera3Device::deleteStream(int id) {
   1225     ATRACE_CALL();
   1226     Mutex::Autolock il(mInterfaceLock);
   1227     Mutex::Autolock l(mLock);
   1228     status_t res;
   1229 
   1230     ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
   1231 
   1232     // CameraDevice semantics require device to already be idle before
   1233     // deleteStream is called, unlike for createStream.
   1234     if (mStatus == STATUS_ACTIVE) {
   1235         ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
   1236         return -EBUSY;
   1237     }
   1238 
   1239     sp<Camera3StreamInterface> deletedStream;
   1240     ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
   1241     if (mInputStream != NULL && id == mInputStream->getId()) {
   1242         deletedStream = mInputStream;
   1243         mInputStream.clear();
   1244     } else {
   1245         if (outputStreamIdx == NAME_NOT_FOUND) {
   1246             CLOGE("Stream %d does not exist", id);
   1247             return BAD_VALUE;
   1248         }
   1249     }
   1250 
   1251     // Delete output stream or the output part of a bi-directional stream.
   1252     if (outputStreamIdx != NAME_NOT_FOUND) {
   1253         deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
   1254         mOutputStreams.removeItem(id);
   1255     }
   1256 
   1257     // Free up the stream endpoint so that it can be used by some other stream
   1258     res = deletedStream->disconnect();
   1259     if (res != OK) {
   1260         SET_ERR_L("Can't disconnect deleted stream %d", id);
   1261         // fall through since we want to still list the stream as deleted.
   1262     }
   1263     mDeletedStreams.add(deletedStream);
   1264     mNeedConfig = true;
   1265 
   1266     return res;
   1267 }
   1268 
   1269 status_t Camera3Device::deleteReprocessStream(int id) {
   1270     ATRACE_CALL();
   1271     (void)id;
   1272 
   1273     CLOGE("Unimplemented");
   1274     return INVALID_OPERATION;
   1275 }
   1276 
   1277 status_t Camera3Device::configureStreams(bool isConstrainedHighSpeed) {
   1278     ATRACE_CALL();
   1279     ALOGV("%s: E", __FUNCTION__);
   1280 
   1281     Mutex::Autolock il(mInterfaceLock);
   1282     Mutex::Autolock l(mLock);
   1283 
   1284     if (mIsConstrainedHighSpeedConfiguration != isConstrainedHighSpeed) {
   1285         mNeedConfig = true;
   1286         mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
   1287     }
   1288 
   1289     return configureStreamsLocked();
   1290 }
   1291 
   1292 status_t Camera3Device::getInputBufferProducer(
   1293         sp<IGraphicBufferProducer> *producer) {
   1294     Mutex::Autolock il(mInterfaceLock);
   1295     Mutex::Autolock l(mLock);
   1296 
   1297     if (producer == NULL) {
   1298         return BAD_VALUE;
   1299     } else if (mInputStream == NULL) {
   1300         return INVALID_OPERATION;
   1301     }
   1302 
   1303     return mInputStream->getInputBufferProducer(producer);
   1304 }
   1305 
   1306 status_t Camera3Device::createDefaultRequest(int templateId,
   1307         CameraMetadata *request) {
   1308     ATRACE_CALL();
   1309     ALOGV("%s: for template %d", __FUNCTION__, templateId);
   1310 
   1311     if (templateId <= 0 || templateId >= CAMERA3_TEMPLATE_COUNT) {
   1312         android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
   1313                 IPCThreadState::self()->getCallingUid(), nullptr, 0);
   1314         return BAD_VALUE;
   1315     }
   1316 
   1317     Mutex::Autolock il(mInterfaceLock);
   1318     Mutex::Autolock l(mLock);
   1319 
   1320     switch (mStatus) {
   1321         case STATUS_ERROR:
   1322             CLOGE("Device has encountered a serious error");
   1323             return INVALID_OPERATION;
   1324         case STATUS_UNINITIALIZED:
   1325             CLOGE("Device is not initialized!");
   1326             return INVALID_OPERATION;
   1327         case STATUS_UNCONFIGURED:
   1328         case STATUS_CONFIGURED:
   1329         case STATUS_ACTIVE:
   1330             // OK
   1331             break;
   1332         default:
   1333             SET_ERR_L("Unexpected status: %d", mStatus);
   1334             return INVALID_OPERATION;
   1335     }
   1336 
   1337     if (!mRequestTemplateCache[templateId].isEmpty()) {
   1338         *request = mRequestTemplateCache[templateId];
   1339         return OK;
   1340     }
   1341 
   1342     const camera_metadata_t *rawRequest;
   1343     ATRACE_BEGIN("camera3->construct_default_request_settings");
   1344     rawRequest = mHal3Device->ops->construct_default_request_settings(
   1345         mHal3Device, templateId);
   1346     ATRACE_END();
   1347     if (rawRequest == NULL) {
   1348         ALOGI("%s: template %d is not supported on this camera device",
   1349               __FUNCTION__, templateId);
   1350         return BAD_VALUE;
   1351     }
   1352 
   1353     mRequestTemplateCache[templateId] = rawRequest;
   1354 
   1355     // Derive some new keys for backward compatibility
   1356     if (mDerivePostRawSensKey && !mRequestTemplateCache[templateId].exists(
   1357             ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
   1358         int32_t defaultBoost[1] = {100};
   1359         mRequestTemplateCache[templateId].update(
   1360                 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
   1361                 defaultBoost, 1);
   1362     }
   1363 
   1364     *request = mRequestTemplateCache[templateId];
   1365     return OK;
   1366 }
   1367 
   1368 status_t Camera3Device::waitUntilDrained() {
   1369     ATRACE_CALL();
   1370     Mutex::Autolock il(mInterfaceLock);
   1371     Mutex::Autolock l(mLock);
   1372 
   1373     return waitUntilDrainedLocked();
   1374 }
   1375 
   1376 status_t Camera3Device::waitUntilDrainedLocked() {
   1377     switch (mStatus) {
   1378         case STATUS_UNINITIALIZED:
   1379         case STATUS_UNCONFIGURED:
   1380             ALOGV("%s: Already idle", __FUNCTION__);
   1381             return OK;
   1382         case STATUS_CONFIGURED:
   1383             // To avoid race conditions, check with tracker to be sure
   1384         case STATUS_ERROR:
   1385         case STATUS_ACTIVE:
   1386             // Need to verify shut down
   1387             break;
   1388         default:
   1389             SET_ERR_L("Unexpected status: %d",mStatus);
   1390             return INVALID_OPERATION;
   1391     }
   1392 
   1393     ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
   1394     status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
   1395     if (res != OK) {
   1396         SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
   1397                 res);
   1398     }
   1399     return res;
   1400 }
   1401 
   1402 
   1403 void Camera3Device::internalUpdateStatusLocked(Status status) {
   1404     mStatus = status;
   1405     mRecentStatusUpdates.add(mStatus);
   1406     mStatusChanged.broadcast();
   1407 }
   1408 
   1409 // Pause to reconfigure
   1410 status_t Camera3Device::internalPauseAndWaitLocked() {
   1411     mRequestThread->setPaused(true);
   1412     mPauseStateNotify = true;
   1413 
   1414     ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
   1415     status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
   1416     if (res != OK) {
   1417         SET_ERR_L("Can't idle device in %f seconds!",
   1418                 kShutdownTimeout/1e9);
   1419     }
   1420 
   1421     return res;
   1422 }
   1423 
   1424 // Resume after internalPauseAndWaitLocked
   1425 status_t Camera3Device::internalResumeLocked() {
   1426     status_t res;
   1427 
   1428     mRequestThread->setPaused(false);
   1429 
   1430     res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
   1431     if (res != OK) {
   1432         SET_ERR_L("Can't transition to active in %f seconds!",
   1433                 kActiveTimeout/1e9);
   1434     }
   1435     mPauseStateNotify = false;
   1436     return OK;
   1437 }
   1438 
   1439 status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
   1440     status_t res = OK;
   1441 
   1442     size_t startIndex = 0;
   1443     if (mStatusWaiters == 0) {
   1444         // Clear the list of recent statuses if there are no existing threads waiting on updates to
   1445         // this status list
   1446         mRecentStatusUpdates.clear();
   1447     } else {
   1448         // If other threads are waiting on updates to this status list, set the position of the
   1449         // first element that this list will check rather than clearing the list.
   1450         startIndex = mRecentStatusUpdates.size();
   1451     }
   1452 
   1453     mStatusWaiters++;
   1454 
   1455     bool stateSeen = false;
   1456     do {
   1457         if (active == (mStatus == STATUS_ACTIVE)) {
   1458             // Desired state is current
   1459             break;
   1460         }
   1461 
   1462         res = mStatusChanged.waitRelative(mLock, timeout);
   1463         if (res != OK) break;
   1464 
   1465         // This is impossible, but if not, could result in subtle deadlocks and invalid state
   1466         // transitions.
   1467         LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
   1468                 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
   1469                 __FUNCTION__);
   1470 
   1471         // Encountered desired state since we began waiting
   1472         for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
   1473             if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
   1474                 stateSeen = true;
   1475                 break;
   1476             }
   1477         }
   1478     } while (!stateSeen);
   1479 
   1480     mStatusWaiters--;
   1481 
   1482     return res;
   1483 }
   1484 
   1485 
   1486 status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
   1487     ATRACE_CALL();
   1488     Mutex::Autolock l(mOutputLock);
   1489 
   1490     if (listener != NULL && mListener != NULL) {
   1491         ALOGW("%s: Replacing old callback listener", __FUNCTION__);
   1492     }
   1493     mListener = listener;
   1494     mRequestThread->setNotificationListener(listener);
   1495     mPreparerThread->setNotificationListener(listener);
   1496 
   1497     return OK;
   1498 }
   1499 
   1500 bool Camera3Device::willNotify3A() {
   1501     return false;
   1502 }
   1503 
   1504 status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
   1505     status_t res;
   1506     Mutex::Autolock l(mOutputLock);
   1507 
   1508     while (mResultQueue.empty()) {
   1509         res = mResultSignal.waitRelative(mOutputLock, timeout);
   1510         if (res == TIMED_OUT) {
   1511             return res;
   1512         } else if (res != OK) {
   1513             ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
   1514                     __FUNCTION__, mId, timeout, strerror(-res), res);
   1515             return res;
   1516         }
   1517     }
   1518     return OK;
   1519 }
   1520 
   1521 status_t Camera3Device::getNextResult(CaptureResult *frame) {
   1522     ATRACE_CALL();
   1523     Mutex::Autolock l(mOutputLock);
   1524 
   1525     if (mResultQueue.empty()) {
   1526         return NOT_ENOUGH_DATA;
   1527     }
   1528 
   1529     if (frame == NULL) {
   1530         ALOGE("%s: argument cannot be NULL", __FUNCTION__);
   1531         return BAD_VALUE;
   1532     }
   1533 
   1534     CaptureResult &result = *(mResultQueue.begin());
   1535     frame->mResultExtras = result.mResultExtras;
   1536     frame->mMetadata.acquire(result.mMetadata);
   1537     mResultQueue.erase(mResultQueue.begin());
   1538 
   1539     return OK;
   1540 }
   1541 
   1542 status_t Camera3Device::triggerAutofocus(uint32_t id) {
   1543     ATRACE_CALL();
   1544     Mutex::Autolock il(mInterfaceLock);
   1545 
   1546     ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
   1547     // Mix-in this trigger into the next request and only the next request.
   1548     RequestTrigger trigger[] = {
   1549         {
   1550             ANDROID_CONTROL_AF_TRIGGER,
   1551             ANDROID_CONTROL_AF_TRIGGER_START
   1552         },
   1553         {
   1554             ANDROID_CONTROL_AF_TRIGGER_ID,
   1555             static_cast<int32_t>(id)
   1556         }
   1557     };
   1558 
   1559     return mRequestThread->queueTrigger(trigger,
   1560                                         sizeof(trigger)/sizeof(trigger[0]));
   1561 }
   1562 
   1563 status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
   1564     ATRACE_CALL();
   1565     Mutex::Autolock il(mInterfaceLock);
   1566 
   1567     ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
   1568     // Mix-in this trigger into the next request and only the next request.
   1569     RequestTrigger trigger[] = {
   1570         {
   1571             ANDROID_CONTROL_AF_TRIGGER,
   1572             ANDROID_CONTROL_AF_TRIGGER_CANCEL
   1573         },
   1574         {
   1575             ANDROID_CONTROL_AF_TRIGGER_ID,
   1576             static_cast<int32_t>(id)
   1577         }
   1578     };
   1579 
   1580     return mRequestThread->queueTrigger(trigger,
   1581                                         sizeof(trigger)/sizeof(trigger[0]));
   1582 }
   1583 
   1584 status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
   1585     ATRACE_CALL();
   1586     Mutex::Autolock il(mInterfaceLock);
   1587 
   1588     ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
   1589     // Mix-in this trigger into the next request and only the next request.
   1590     RequestTrigger trigger[] = {
   1591         {
   1592             ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
   1593             ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
   1594         },
   1595         {
   1596             ANDROID_CONTROL_AE_PRECAPTURE_ID,
   1597             static_cast<int32_t>(id)
   1598         }
   1599     };
   1600 
   1601     return mRequestThread->queueTrigger(trigger,
   1602                                         sizeof(trigger)/sizeof(trigger[0]));
   1603 }
   1604 
   1605 status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
   1606         buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
   1607     ATRACE_CALL();
   1608     (void)reprocessStreamId; (void)buffer; (void)listener;
   1609 
   1610     CLOGE("Unimplemented");
   1611     return INVALID_OPERATION;
   1612 }
   1613 
   1614 status_t Camera3Device::flush(int64_t *frameNumber) {
   1615     ATRACE_CALL();
   1616     ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
   1617     Mutex::Autolock il(mInterfaceLock);
   1618 
   1619     {
   1620         Mutex::Autolock l(mLock);
   1621         mRequestThread->clear(/*out*/frameNumber);
   1622     }
   1623 
   1624     status_t res;
   1625     if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
   1626         res = mRequestThread->flush();
   1627     } else {
   1628         Mutex::Autolock l(mLock);
   1629         res = waitUntilDrainedLocked();
   1630     }
   1631 
   1632     return res;
   1633 }
   1634 
   1635 status_t Camera3Device::prepare(int streamId) {
   1636     return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
   1637 }
   1638 
   1639 status_t Camera3Device::prepare(int maxCount, int streamId) {
   1640     ATRACE_CALL();
   1641     ALOGV("%s: Camera %d: Preparing stream %d", __FUNCTION__, mId, streamId);
   1642     Mutex::Autolock il(mInterfaceLock);
   1643     Mutex::Autolock l(mLock);
   1644 
   1645     sp<Camera3StreamInterface> stream;
   1646     ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
   1647     if (outputStreamIdx == NAME_NOT_FOUND) {
   1648         CLOGE("Stream %d does not exist", streamId);
   1649         return BAD_VALUE;
   1650     }
   1651 
   1652     stream = mOutputStreams.editValueAt(outputStreamIdx);
   1653 
   1654     if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
   1655         CLOGE("Stream %d has already been a request target", streamId);
   1656         return BAD_VALUE;
   1657     }
   1658 
   1659     if (mRequestThread->isStreamPending(stream)) {
   1660         CLOGE("Stream %d is already a target in a pending request", streamId);
   1661         return BAD_VALUE;
   1662     }
   1663 
   1664     return mPreparerThread->prepare(maxCount, stream);
   1665 }
   1666 
   1667 status_t Camera3Device::tearDown(int streamId) {
   1668     ATRACE_CALL();
   1669     ALOGV("%s: Camera %d: Tearing down stream %d", __FUNCTION__, mId, streamId);
   1670     Mutex::Autolock il(mInterfaceLock);
   1671     Mutex::Autolock l(mLock);
   1672 
   1673     // Teardown can only be accomplished on devices that don't require register_stream_buffers,
   1674     // since we cannot call register_stream_buffers except right after configure_streams.
   1675     if (mHal3Device->common.version < CAMERA_DEVICE_API_VERSION_3_2) {
   1676         ALOGE("%s: Unable to tear down streams on device HAL v%x",
   1677                 __FUNCTION__, mHal3Device->common.version);
   1678         return NO_INIT;
   1679     }
   1680 
   1681     sp<Camera3StreamInterface> stream;
   1682     ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
   1683     if (outputStreamIdx == NAME_NOT_FOUND) {
   1684         CLOGE("Stream %d does not exist", streamId);
   1685         return BAD_VALUE;
   1686     }
   1687 
   1688     stream = mOutputStreams.editValueAt(outputStreamIdx);
   1689 
   1690     if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
   1691         CLOGE("Stream %d is a target of a in-progress request", streamId);
   1692         return BAD_VALUE;
   1693     }
   1694 
   1695     return stream->tearDown();
   1696 }
   1697 
   1698 status_t Camera3Device::addBufferListenerForStream(int streamId,
   1699         wp<Camera3StreamBufferListener> listener) {
   1700     ATRACE_CALL();
   1701     ALOGV("%s: Camera %d: Adding buffer listener for stream %d", __FUNCTION__, mId, streamId);
   1702     Mutex::Autolock il(mInterfaceLock);
   1703     Mutex::Autolock l(mLock);
   1704 
   1705     sp<Camera3StreamInterface> stream;
   1706     ssize_t outputStreamIdx = mOutputStreams.indexOfKey(streamId);
   1707     if (outputStreamIdx == NAME_NOT_FOUND) {
   1708         CLOGE("Stream %d does not exist", streamId);
   1709         return BAD_VALUE;
   1710     }
   1711 
   1712     stream = mOutputStreams.editValueAt(outputStreamIdx);
   1713     stream->addBufferListener(listener);
   1714 
   1715     return OK;
   1716 }
   1717 
   1718 uint32_t Camera3Device::getDeviceVersion() {
   1719     ATRACE_CALL();
   1720     Mutex::Autolock il(mInterfaceLock);
   1721     return mDeviceVersion;
   1722 }
   1723 
   1724 /**
   1725  * Methods called by subclasses
   1726  */
   1727 
   1728 void Camera3Device::notifyStatus(bool idle) {
   1729     {
   1730         // Need mLock to safely update state and synchronize to current
   1731         // state of methods in flight.
   1732         Mutex::Autolock l(mLock);
   1733         // We can get various system-idle notices from the status tracker
   1734         // while starting up. Only care about them if we've actually sent
   1735         // in some requests recently.
   1736         if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
   1737             return;
   1738         }
   1739         ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
   1740                 idle ? "idle" : "active");
   1741         internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
   1742 
   1743         // Skip notifying listener if we're doing some user-transparent
   1744         // state changes
   1745         if (mPauseStateNotify) return;
   1746     }
   1747 
   1748     sp<NotificationListener> listener;
   1749     {
   1750         Mutex::Autolock l(mOutputLock);
   1751         listener = mListener.promote();
   1752     }
   1753     if (idle && listener != NULL) {
   1754         listener->notifyIdle();
   1755     }
   1756 }
   1757 
   1758 status_t Camera3Device::setConsumerSurface(int streamId, sp<Surface> consumer) {
   1759     ATRACE_CALL();
   1760     ALOGV("%s: Camera %d: set consumer surface for stream %d", __FUNCTION__, mId, streamId);
   1761     Mutex::Autolock il(mInterfaceLock);
   1762     Mutex::Autolock l(mLock);
   1763 
   1764     if (consumer == nullptr) {
   1765         CLOGE("Null consumer is passed!");
   1766         return BAD_VALUE;
   1767     }
   1768 
   1769     ssize_t idx = mOutputStreams.indexOfKey(streamId);
   1770     if (idx == NAME_NOT_FOUND) {
   1771         CLOGE("Stream %d is unknown", streamId);
   1772         return idx;
   1773     }
   1774     sp<Camera3OutputStreamInterface> stream = mOutputStreams[idx];
   1775     status_t res = stream->setConsumer(consumer);
   1776     if (res != OK) {
   1777         CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
   1778         return res;
   1779     }
   1780 
   1781     if (!stream->isConfiguring()) {
   1782         CLOGE("Stream %d was already fully configured.", streamId);
   1783         return INVALID_OPERATION;
   1784     }
   1785 
   1786     res = stream->finishConfiguration(mHal3Device);
   1787     if (res != OK) {
   1788         SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
   1789                 stream->getId(), strerror(-res), res);
   1790         return res;
   1791     }
   1792 
   1793     return OK;
   1794 }
   1795 
   1796 /**
   1797  * Camera3Device private methods
   1798  */
   1799 
   1800 sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
   1801         const CameraMetadata &request) {
   1802     ATRACE_CALL();
   1803     status_t res;
   1804 
   1805     sp<CaptureRequest> newRequest = new CaptureRequest;
   1806     newRequest->mSettings = request;
   1807 
   1808     camera_metadata_entry_t inputStreams =
   1809             newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
   1810     if (inputStreams.count > 0) {
   1811         if (mInputStream == NULL ||
   1812                 mInputStream->getId() != inputStreams.data.i32[0]) {
   1813             CLOGE("Request references unknown input stream %d",
   1814                     inputStreams.data.u8[0]);
   1815             return NULL;
   1816         }
   1817         // Lazy completion of stream configuration (allocation/registration)
   1818         // on first use
   1819         if (mInputStream->isConfiguring()) {
   1820             res = mInputStream->finishConfiguration(mHal3Device);
   1821             if (res != OK) {
   1822                 SET_ERR_L("Unable to finish configuring input stream %d:"
   1823                         " %s (%d)",
   1824                         mInputStream->getId(), strerror(-res), res);
   1825                 return NULL;
   1826             }
   1827         }
   1828         // Check if stream is being prepared
   1829         if (mInputStream->isPreparing()) {
   1830             CLOGE("Request references an input stream that's being prepared!");
   1831             return NULL;
   1832         }
   1833 
   1834         newRequest->mInputStream = mInputStream;
   1835         newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
   1836     }
   1837 
   1838     camera_metadata_entry_t streams =
   1839             newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
   1840     if (streams.count == 0) {
   1841         CLOGE("Zero output streams specified!");
   1842         return NULL;
   1843     }
   1844 
   1845     for (size_t i = 0; i < streams.count; i++) {
   1846         int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
   1847         if (idx == NAME_NOT_FOUND) {
   1848             CLOGE("Request references unknown stream %d",
   1849                     streams.data.u8[i]);
   1850             return NULL;
   1851         }
   1852         sp<Camera3OutputStreamInterface> stream =
   1853                 mOutputStreams.editValueAt(idx);
   1854 
   1855         // It is illegal to include a deferred consumer output stream into a request
   1856         if (stream->isConsumerConfigurationDeferred()) {
   1857             CLOGE("Stream %d hasn't finished configuration yet due to deferred consumer",
   1858                     stream->getId());
   1859             return NULL;
   1860         }
   1861 
   1862         // Lazy completion of stream configuration (allocation/registration)
   1863         // on first use
   1864         if (stream->isConfiguring()) {
   1865             res = stream->finishConfiguration(mHal3Device);
   1866             if (res != OK) {
   1867                 SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
   1868                         stream->getId(), strerror(-res), res);
   1869                 return NULL;
   1870             }
   1871         }
   1872         // Check if stream is being prepared
   1873         if (stream->isPreparing()) {
   1874             CLOGE("Request references an output stream that's being prepared!");
   1875             return NULL;
   1876         }
   1877 
   1878         newRequest->mOutputStreams.push(stream);
   1879     }
   1880     newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
   1881     newRequest->mBatchSize = 1;
   1882 
   1883     return newRequest;
   1884 }
   1885 
   1886 bool Camera3Device::isOpaqueInputSizeSupported(uint32_t width, uint32_t height) {
   1887     for (uint32_t i = 0; i < mSupportedOpaqueInputSizes.size(); i++) {
   1888         Size size = mSupportedOpaqueInputSizes[i];
   1889         if (size.width == width && size.height == height) {
   1890             return true;
   1891         }
   1892     }
   1893 
   1894     return false;
   1895 }
   1896 
   1897 void Camera3Device::cancelStreamsConfigurationLocked() {
   1898     int res = OK;
   1899     if (mInputStream != NULL && mInputStream->isConfiguring()) {
   1900         res = mInputStream->cancelConfiguration();
   1901         if (res != OK) {
   1902             CLOGE("Can't cancel configuring input stream %d: %s (%d)",
   1903                     mInputStream->getId(), strerror(-res), res);
   1904         }
   1905     }
   1906 
   1907     for (size_t i = 0; i < mOutputStreams.size(); i++) {
   1908         sp<Camera3OutputStreamInterface> outputStream = mOutputStreams.editValueAt(i);
   1909         if (outputStream->isConfiguring()) {
   1910             res = outputStream->cancelConfiguration();
   1911             if (res != OK) {
   1912                 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
   1913                         outputStream->getId(), strerror(-res), res);
   1914             }
   1915         }
   1916     }
   1917 
   1918     // Return state to that at start of call, so that future configures
   1919     // properly clean things up
   1920     internalUpdateStatusLocked(STATUS_UNCONFIGURED);
   1921     mNeedConfig = true;
   1922 }
   1923 
   1924 status_t Camera3Device::configureStreamsLocked() {
   1925     ATRACE_CALL();
   1926     status_t res;
   1927 
   1928     if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
   1929         CLOGE("Not idle");
   1930         return INVALID_OPERATION;
   1931     }
   1932 
   1933     if (!mNeedConfig) {
   1934         ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
   1935         return OK;
   1936     }
   1937 
   1938     // Workaround for device HALv3.2 or older spec bug - zero streams requires
   1939     // adding a dummy stream instead.
   1940     // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
   1941     if (mOutputStreams.size() == 0) {
   1942         addDummyStreamLocked();
   1943     } else {
   1944         tryRemoveDummyStreamLocked();
   1945     }
   1946 
   1947     // Start configuring the streams
   1948     ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
   1949 
   1950     camera3_stream_configuration config;
   1951     config.operation_mode = mIsConstrainedHighSpeedConfiguration ?
   1952             CAMERA3_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE :
   1953             CAMERA3_STREAM_CONFIGURATION_NORMAL_MODE;
   1954     config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
   1955 
   1956     Vector<camera3_stream_t*> streams;
   1957     streams.setCapacity(config.num_streams);
   1958 
   1959     if (mInputStream != NULL) {
   1960         camera3_stream_t *inputStream;
   1961         inputStream = mInputStream->startConfiguration();
   1962         if (inputStream == NULL) {
   1963             CLOGE("Can't start input stream configuration");
   1964             cancelStreamsConfigurationLocked();
   1965             return INVALID_OPERATION;
   1966         }
   1967         streams.add(inputStream);
   1968     }
   1969 
   1970     for (size_t i = 0; i < mOutputStreams.size(); i++) {
   1971 
   1972         // Don't configure bidi streams twice, nor add them twice to the list
   1973         if (mOutputStreams[i].get() ==
   1974             static_cast<Camera3StreamInterface*>(mInputStream.get())) {
   1975 
   1976             config.num_streams--;
   1977             continue;
   1978         }
   1979 
   1980         camera3_stream_t *outputStream;
   1981         outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
   1982         if (outputStream == NULL) {
   1983             CLOGE("Can't start output stream configuration");
   1984             cancelStreamsConfigurationLocked();
   1985             return INVALID_OPERATION;
   1986         }
   1987         streams.add(outputStream);
   1988     }
   1989 
   1990     config.streams = streams.editArray();
   1991 
   1992     // Do the HAL configuration; will potentially touch stream
   1993     // max_buffers, usage, priv fields.
   1994     ATRACE_BEGIN("camera3->configure_streams");
   1995     res = mHal3Device->ops->configure_streams(mHal3Device, &config);
   1996     ATRACE_END();
   1997 
   1998     if (res == BAD_VALUE) {
   1999         // HAL rejected this set of streams as unsupported, clean up config
   2000         // attempt and return to unconfigured state
   2001         CLOGE("Set of requested inputs/outputs not supported by HAL");
   2002         cancelStreamsConfigurationLocked();
   2003         return BAD_VALUE;
   2004     } else if (res != OK) {
   2005         // Some other kind of error from configure_streams - this is not
   2006         // expected
   2007         SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
   2008                 strerror(-res), res);
   2009         return res;
   2010     }
   2011 
   2012     // Finish all stream configuration immediately.
   2013     // TODO: Try to relax this later back to lazy completion, which should be
   2014     // faster
   2015 
   2016     if (mInputStream != NULL && mInputStream->isConfiguring()) {
   2017         res = mInputStream->finishConfiguration(mHal3Device);
   2018         if (res != OK) {
   2019             CLOGE("Can't finish configuring input stream %d: %s (%d)",
   2020                     mInputStream->getId(), strerror(-res), res);
   2021             cancelStreamsConfigurationLocked();
   2022             return BAD_VALUE;
   2023         }
   2024     }
   2025 
   2026     for (size_t i = 0; i < mOutputStreams.size(); i++) {
   2027         sp<Camera3OutputStreamInterface> outputStream =
   2028             mOutputStreams.editValueAt(i);
   2029         if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
   2030             res = outputStream->finishConfiguration(mHal3Device);
   2031             if (res != OK) {
   2032                 CLOGE("Can't finish configuring output stream %d: %s (%d)",
   2033                         outputStream->getId(), strerror(-res), res);
   2034                 cancelStreamsConfigurationLocked();
   2035                 return BAD_VALUE;
   2036             }
   2037         }
   2038     }
   2039 
   2040     // Request thread needs to know to avoid using repeat-last-settings protocol
   2041     // across configure_streams() calls
   2042     mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration);
   2043 
   2044     char value[PROPERTY_VALUE_MAX];
   2045     property_get("camera.fifo.disable", value, "0");
   2046     int32_t disableFifo = atoi(value);
   2047     if (disableFifo != 1) {
   2048         // Boost priority of request thread to SCHED_FIFO.
   2049         pid_t requestThreadTid = mRequestThread->getTid();
   2050         res = requestPriority(getpid(), requestThreadTid,
   2051                 kRequestThreadPriority, /*asynchronous*/ false);
   2052         if (res != OK) {
   2053             ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
   2054                     strerror(-res), res);
   2055         } else {
   2056             ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
   2057         }
   2058     }
   2059 
   2060     // Update device state
   2061 
   2062     mNeedConfig = false;
   2063 
   2064     internalUpdateStatusLocked((mDummyStreamId == NO_STREAM) ?
   2065             STATUS_CONFIGURED : STATUS_UNCONFIGURED);
   2066 
   2067     ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
   2068 
   2069     // tear down the deleted streams after configure streams.
   2070     mDeletedStreams.clear();
   2071 
   2072     return OK;
   2073 }
   2074 
   2075 status_t Camera3Device::addDummyStreamLocked() {
   2076     ATRACE_CALL();
   2077     status_t res;
   2078 
   2079     if (mDummyStreamId != NO_STREAM) {
   2080         // Should never be adding a second dummy stream when one is already
   2081         // active
   2082         SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
   2083                 __FUNCTION__, mId);
   2084         return INVALID_OPERATION;
   2085     }
   2086 
   2087     ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
   2088 
   2089     sp<Camera3OutputStreamInterface> dummyStream =
   2090             new Camera3DummyStream(mNextStreamId);
   2091 
   2092     res = mOutputStreams.add(mNextStreamId, dummyStream);
   2093     if (res < 0) {
   2094         SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
   2095         return res;
   2096     }
   2097 
   2098     mDummyStreamId = mNextStreamId;
   2099     mNextStreamId++;
   2100 
   2101     return OK;
   2102 }
   2103 
   2104 status_t Camera3Device::tryRemoveDummyStreamLocked() {
   2105     ATRACE_CALL();
   2106     status_t res;
   2107 
   2108     if (mDummyStreamId == NO_STREAM) return OK;
   2109     if (mOutputStreams.size() == 1) return OK;
   2110 
   2111     ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
   2112 
   2113     // Ok, have a dummy stream and there's at least one other output stream,
   2114     // so remove the dummy
   2115 
   2116     sp<Camera3StreamInterface> deletedStream;
   2117     ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
   2118     if (outputStreamIdx == NAME_NOT_FOUND) {
   2119         SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
   2120         return INVALID_OPERATION;
   2121     }
   2122 
   2123     deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
   2124     mOutputStreams.removeItemsAt(outputStreamIdx);
   2125 
   2126     // Free up the stream endpoint so that it can be used by some other stream
   2127     res = deletedStream->disconnect();
   2128     if (res != OK) {
   2129         SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
   2130         // fall through since we want to still list the stream as deleted.
   2131     }
   2132     mDeletedStreams.add(deletedStream);
   2133     mDummyStreamId = NO_STREAM;
   2134 
   2135     return res;
   2136 }
   2137 
   2138 void Camera3Device::setErrorState(const char *fmt, ...) {
   2139     Mutex::Autolock l(mLock);
   2140     va_list args;
   2141     va_start(args, fmt);
   2142 
   2143     setErrorStateLockedV(fmt, args);
   2144 
   2145     va_end(args);
   2146 }
   2147 
   2148 void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
   2149     Mutex::Autolock l(mLock);
   2150     setErrorStateLockedV(fmt, args);
   2151 }
   2152 
   2153 void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
   2154     va_list args;
   2155     va_start(args, fmt);
   2156 
   2157     setErrorStateLockedV(fmt, args);
   2158 
   2159     va_end(args);
   2160 }
   2161 
   2162 void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
   2163     // Print out all error messages to log
   2164     String8 errorCause = String8::formatV(fmt, args);
   2165     ALOGE("Camera %d: %s", mId, errorCause.string());
   2166 
   2167     // But only do error state transition steps for the first error
   2168     if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
   2169 
   2170     mErrorCause = errorCause;
   2171 
   2172     mRequestThread->setPaused(true);
   2173     internalUpdateStatusLocked(STATUS_ERROR);
   2174 
   2175     // Notify upstream about a device error
   2176     sp<NotificationListener> listener = mListener.promote();
   2177     if (listener != NULL) {
   2178         listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
   2179                 CaptureResultExtras());
   2180     }
   2181 
   2182     // Save stack trace. View by dumping it later.
   2183     CameraTraces::saveTrace();
   2184     // TODO: consider adding errorCause and client pid/procname
   2185 }
   2186 
   2187 /**
   2188  * In-flight request management
   2189  */
   2190 
   2191 status_t Camera3Device::registerInFlight(uint32_t frameNumber,
   2192         int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
   2193         const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
   2194     ATRACE_CALL();
   2195     Mutex::Autolock l(mInFlightLock);
   2196 
   2197     ssize_t res;
   2198     res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
   2199             aeTriggerCancelOverride));
   2200     if (res < 0) return res;
   2201 
   2202     if (mInFlightMap.size() == 1) {
   2203         mStatusTracker->markComponentActive(mInFlightStatusId);
   2204     }
   2205 
   2206     return OK;
   2207 }
   2208 
   2209 void Camera3Device::returnOutputBuffers(
   2210         const camera3_stream_buffer_t *outputBuffers, size_t numBuffers,
   2211         nsecs_t timestamp) {
   2212     for (size_t i = 0; i < numBuffers; i++)
   2213     {
   2214         Camera3Stream *stream = Camera3Stream::cast(outputBuffers[i].stream);
   2215         status_t res = stream->returnBuffer(outputBuffers[i], timestamp);
   2216         // Note: stream may be deallocated at this point, if this buffer was
   2217         // the last reference to it.
   2218         if (res != OK) {
   2219             ALOGE("Can't return buffer to its stream: %s (%d)",
   2220                 strerror(-res), res);
   2221         }
   2222     }
   2223 }
   2224 
   2225 
   2226 void Camera3Device::removeInFlightRequestIfReadyLocked(int idx) {
   2227 
   2228     const InFlightRequest &request = mInFlightMap.valueAt(idx);
   2229     const uint32_t frameNumber = mInFlightMap.keyAt(idx);
   2230 
   2231     nsecs_t sensorTimestamp = request.sensorTimestamp;
   2232     nsecs_t shutterTimestamp = request.shutterTimestamp;
   2233 
   2234     // Check if it's okay to remove the request from InFlightMap:
   2235     // In the case of a successful request:
   2236     //      all input and output buffers, all result metadata, shutter callback
   2237     //      arrived.
   2238     // In the case of a unsuccessful request:
   2239     //      all input and output buffers arrived.
   2240     if (request.numBuffersLeft == 0 &&
   2241             (request.requestStatus != OK ||
   2242             (request.haveResultMetadata && shutterTimestamp != 0))) {
   2243         ATRACE_ASYNC_END("frame capture", frameNumber);
   2244 
   2245         // Sanity check - if sensor timestamp matches shutter timestamp
   2246         if (request.requestStatus == OK &&
   2247                 sensorTimestamp != shutterTimestamp) {
   2248             SET_ERR("sensor timestamp (%" PRId64
   2249                 ") for frame %d doesn't match shutter timestamp (%" PRId64 ")",
   2250                 sensorTimestamp, frameNumber, shutterTimestamp);
   2251         }
   2252 
   2253         // for an unsuccessful request, it may have pending output buffers to
   2254         // return.
   2255         assert(request.requestStatus != OK ||
   2256                request.pendingOutputBuffers.size() == 0);
   2257         returnOutputBuffers(request.pendingOutputBuffers.array(),
   2258             request.pendingOutputBuffers.size(), 0);
   2259 
   2260         mInFlightMap.removeItemsAt(idx, 1);
   2261 
   2262         // Indicate idle inFlightMap to the status tracker
   2263         if (mInFlightMap.size() == 0) {
   2264             mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
   2265         }
   2266 
   2267         ALOGVV("%s: removed frame %d from InFlightMap", __FUNCTION__, frameNumber);
   2268      }
   2269 
   2270     // Sanity check - if we have too many in-flight frames, something has
   2271     // likely gone wrong
   2272     if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
   2273         CLOGE("In-flight list too large: %zu", mInFlightMap.size());
   2274     } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
   2275             kInFlightWarnLimitHighSpeed) {
   2276         CLOGE("In-flight list too large for high speed configuration: %zu",
   2277                 mInFlightMap.size());
   2278     }
   2279 }
   2280 
   2281 void Camera3Device::insertResultLocked(CaptureResult *result, uint32_t frameNumber,
   2282             const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
   2283     if (result == nullptr) return;
   2284 
   2285     if (result->mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
   2286             (int32_t*)&frameNumber, 1) != OK) {
   2287         SET_ERR("Failed to set frame number %d in metadata", frameNumber);
   2288         return;
   2289     }
   2290 
   2291     if (result->mMetadata.update(ANDROID_REQUEST_ID, &result->mResultExtras.requestId, 1) != OK) {
   2292         SET_ERR("Failed to set request ID in metadata for frame %d", frameNumber);
   2293         return;
   2294     }
   2295 
   2296     overrideResultForPrecaptureCancel(&result->mMetadata, aeTriggerCancelOverride);
   2297 
   2298     // Valid result, insert into queue
   2299     List<CaptureResult>::iterator queuedResult =
   2300             mResultQueue.insert(mResultQueue.end(), CaptureResult(*result));
   2301     ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
   2302            ", burstId = %" PRId32, __FUNCTION__,
   2303            queuedResult->mResultExtras.requestId,
   2304            queuedResult->mResultExtras.frameNumber,
   2305            queuedResult->mResultExtras.burstId);
   2306 
   2307     mResultSignal.signal();
   2308 }
   2309 
   2310 
   2311 void Camera3Device::sendPartialCaptureResult(const camera_metadata_t * partialResult,
   2312         const CaptureResultExtras &resultExtras, uint32_t frameNumber,
   2313         const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
   2314     Mutex::Autolock l(mOutputLock);
   2315 
   2316     CaptureResult captureResult;
   2317     captureResult.mResultExtras = resultExtras;
   2318     captureResult.mMetadata = partialResult;
   2319 
   2320     insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
   2321 }
   2322 
   2323 
   2324 void Camera3Device::sendCaptureResult(CameraMetadata &pendingMetadata,
   2325         CaptureResultExtras &resultExtras,
   2326         CameraMetadata &collectedPartialResult,
   2327         uint32_t frameNumber,
   2328         bool reprocess,
   2329         const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
   2330     if (pendingMetadata.isEmpty())
   2331         return;
   2332 
   2333     Mutex::Autolock l(mOutputLock);
   2334 
   2335     // TODO: need to track errors for tighter bounds on expected frame number
   2336     if (reprocess) {
   2337         if (frameNumber < mNextReprocessResultFrameNumber) {
   2338             SET_ERR("Out-of-order reprocess capture result metadata submitted! "
   2339                 "(got frame number %d, expecting %d)",
   2340                 frameNumber, mNextReprocessResultFrameNumber);
   2341             return;
   2342         }
   2343         mNextReprocessResultFrameNumber = frameNumber + 1;
   2344     } else {
   2345         if (frameNumber < mNextResultFrameNumber) {
   2346             SET_ERR("Out-of-order capture result metadata submitted! "
   2347                     "(got frame number %d, expecting %d)",
   2348                     frameNumber, mNextResultFrameNumber);
   2349             return;
   2350         }
   2351         mNextResultFrameNumber = frameNumber + 1;
   2352     }
   2353 
   2354     CaptureResult captureResult;
   2355     captureResult.mResultExtras = resultExtras;
   2356     captureResult.mMetadata = pendingMetadata;
   2357 
   2358     // Append any previous partials to form a complete result
   2359     if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
   2360         captureResult.mMetadata.append(collectedPartialResult);
   2361     }
   2362 
   2363     // Derive some new keys for backward compaibility
   2364     if (mDerivePostRawSensKey && !captureResult.mMetadata.exists(
   2365             ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST)) {
   2366         int32_t defaultBoost[1] = {100};
   2367         captureResult.mMetadata.update(
   2368                 ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
   2369                 defaultBoost, 1);
   2370     }
   2371 
   2372     captureResult.mMetadata.sort();
   2373 
   2374     // Check that there's a timestamp in the result metadata
   2375     camera_metadata_entry timestamp = captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
   2376     if (timestamp.count == 0) {
   2377         SET_ERR("No timestamp provided by HAL for frame %d!",
   2378                 frameNumber);
   2379         return;
   2380     }
   2381 
   2382     mTagMonitor.monitorMetadata(TagMonitor::RESULT,
   2383             frameNumber, timestamp.data.i64[0], captureResult.mMetadata);
   2384 
   2385     insertResultLocked(&captureResult, frameNumber, aeTriggerCancelOverride);
   2386 }
   2387 
   2388 /**
   2389  * Camera HAL device callback methods
   2390  */
   2391 
   2392 void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
   2393     ATRACE_CALL();
   2394 
   2395     status_t res;
   2396 
   2397     uint32_t frameNumber = result->frame_number;
   2398     if (result->result == NULL && result->num_output_buffers == 0 &&
   2399             result->input_buffer == NULL) {
   2400         SET_ERR("No result data provided by HAL for frame %d",
   2401                 frameNumber);
   2402         return;
   2403     }
   2404 
   2405     // For HAL3.2 or above, If HAL doesn't support partial, it must always set
   2406     // partial_result to 1 when metadata is included in this result.
   2407     if (!mUsePartialResult &&
   2408             mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
   2409             result->result != NULL &&
   2410             result->partial_result != 1) {
   2411         SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
   2412                 " if partial result is not supported",
   2413                 frameNumber, result->partial_result);
   2414         return;
   2415     }
   2416 
   2417     bool isPartialResult = false;
   2418     CameraMetadata collectedPartialResult;
   2419     CaptureResultExtras resultExtras;
   2420     bool hasInputBufferInRequest = false;
   2421 
   2422     // Get shutter timestamp and resultExtras from list of in-flight requests,
   2423     // where it was added by the shutter notification for this frame. If the
   2424     // shutter timestamp isn't received yet, append the output buffers to the
   2425     // in-flight request and they will be returned when the shutter timestamp
   2426     // arrives. Update the in-flight status and remove the in-flight entry if
   2427     // all result data and shutter timestamp have been received.
   2428     nsecs_t shutterTimestamp = 0;
   2429 
   2430     {
   2431         Mutex::Autolock l(mInFlightLock);
   2432         ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
   2433         if (idx == NAME_NOT_FOUND) {
   2434             SET_ERR("Unknown frame number for capture result: %d",
   2435                     frameNumber);
   2436             return;
   2437         }
   2438         InFlightRequest &request = mInFlightMap.editValueAt(idx);
   2439         ALOGVV("%s: got InFlightRequest requestId = %" PRId32
   2440                 ", frameNumber = %" PRId64 ", burstId = %" PRId32
   2441                 ", partialResultCount = %d",
   2442                 __FUNCTION__, request.resultExtras.requestId,
   2443                 request.resultExtras.frameNumber, request.resultExtras.burstId,
   2444                 result->partial_result);
   2445         // Always update the partial count to the latest one if it's not 0
   2446         // (buffers only). When framework aggregates adjacent partial results
   2447         // into one, the latest partial count will be used.
   2448         if (result->partial_result != 0)
   2449             request.resultExtras.partialResultCount = result->partial_result;
   2450 
   2451         // Check if this result carries only partial metadata
   2452         if (mUsePartialResult && result->result != NULL) {
   2453             if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
   2454                 if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
   2455                     SET_ERR("Result is malformed for frame %d: partial_result %u must be  in"
   2456                             " the range of [1, %d] when metadata is included in the result",
   2457                             frameNumber, result->partial_result, mNumPartialResults);
   2458                     return;
   2459                 }
   2460                 isPartialResult = (result->partial_result < mNumPartialResults);
   2461                 if (isPartialResult) {
   2462                     request.collectedPartialResult.append(result->result);
   2463                 }
   2464             } else {
   2465                 camera_metadata_ro_entry_t partialResultEntry;
   2466                 res = find_camera_metadata_ro_entry(result->result,
   2467                         ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
   2468                 if (res != NAME_NOT_FOUND &&
   2469                         partialResultEntry.count > 0 &&
   2470                         partialResultEntry.data.u8[0] ==
   2471                         ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
   2472                     // A partial result. Flag this as such, and collect this
   2473                     // set of metadata into the in-flight entry.
   2474                     isPartialResult = true;
   2475                     request.collectedPartialResult.append(
   2476                         result->result);
   2477                     request.collectedPartialResult.erase(
   2478                         ANDROID_QUIRKS_PARTIAL_RESULT);
   2479                 }
   2480             }
   2481 
   2482             if (isPartialResult) {
   2483                 // Send partial capture result
   2484                 sendPartialCaptureResult(result->result, request.resultExtras, frameNumber,
   2485                         request.aeTriggerCancelOverride);
   2486             }
   2487         }
   2488 
   2489         shutterTimestamp = request.shutterTimestamp;
   2490         hasInputBufferInRequest = request.hasInputBuffer;
   2491 
   2492         // Did we get the (final) result metadata for this capture?
   2493         if (result->result != NULL && !isPartialResult) {
   2494             if (request.haveResultMetadata) {
   2495                 SET_ERR("Called multiple times with metadata for frame %d",
   2496                         frameNumber);
   2497                 return;
   2498             }
   2499             if (mUsePartialResult &&
   2500                     !request.collectedPartialResult.isEmpty()) {
   2501                 collectedPartialResult.acquire(
   2502                     request.collectedPartialResult);
   2503             }
   2504             request.haveResultMetadata = true;
   2505         }
   2506 
   2507         uint32_t numBuffersReturned = result->num_output_buffers;
   2508         if (result->input_buffer != NULL) {
   2509             if (hasInputBufferInRequest) {
   2510                 numBuffersReturned += 1;
   2511             } else {
   2512                 ALOGW("%s: Input buffer should be NULL if there is no input"
   2513                         " buffer sent in the request",
   2514                         __FUNCTION__);
   2515             }
   2516         }
   2517         request.numBuffersLeft -= numBuffersReturned;
   2518         if (request.numBuffersLeft < 0) {
   2519             SET_ERR("Too many buffers returned for frame %d",
   2520                     frameNumber);
   2521             return;
   2522         }
   2523 
   2524         camera_metadata_ro_entry_t entry;
   2525         res = find_camera_metadata_ro_entry(result->result,
   2526                 ANDROID_SENSOR_TIMESTAMP, &entry);
   2527         if (res == OK && entry.count == 1) {
   2528             request.sensorTimestamp = entry.data.i64[0];
   2529         }
   2530 
   2531         // If shutter event isn't received yet, append the output buffers to
   2532         // the in-flight request. Otherwise, return the output buffers to
   2533         // streams.
   2534         if (shutterTimestamp == 0) {
   2535             request.pendingOutputBuffers.appendArray(result->output_buffers,
   2536                 result->num_output_buffers);
   2537         } else {
   2538             returnOutputBuffers(result->output_buffers,
   2539                 result->num_output_buffers, shutterTimestamp);
   2540         }
   2541 
   2542         if (result->result != NULL && !isPartialResult) {
   2543             if (shutterTimestamp == 0) {
   2544                 request.pendingMetadata = result->result;
   2545                 request.collectedPartialResult = collectedPartialResult;
   2546             } else {
   2547                 CameraMetadata metadata;
   2548                 metadata = result->result;
   2549                 sendCaptureResult(metadata, request.resultExtras,
   2550                     collectedPartialResult, frameNumber, hasInputBufferInRequest,
   2551                     request.aeTriggerCancelOverride);
   2552             }
   2553         }
   2554 
   2555         removeInFlightRequestIfReadyLocked(idx);
   2556     } // scope for mInFlightLock
   2557 
   2558     if (result->input_buffer != NULL) {
   2559         if (hasInputBufferInRequest) {
   2560             Camera3Stream *stream =
   2561                 Camera3Stream::cast(result->input_buffer->stream);
   2562             res = stream->returnInputBuffer(*(result->input_buffer));
   2563             // Note: stream may be deallocated at this point, if this buffer was the
   2564             // last reference to it.
   2565             if (res != OK) {
   2566                 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
   2567                       "  its stream:%s (%d)",  __FUNCTION__,
   2568                       frameNumber, strerror(-res), res);
   2569             }
   2570         } else {
   2571             ALOGW("%s: Input buffer should be NULL if there is no input"
   2572                     " buffer sent in the request, skipping input buffer return.",
   2573                     __FUNCTION__);
   2574         }
   2575     }
   2576 }
   2577 
   2578 void Camera3Device::notify(const camera3_notify_msg *msg) {
   2579     ATRACE_CALL();
   2580     sp<NotificationListener> listener;
   2581     {
   2582         Mutex::Autolock l(mOutputLock);
   2583         listener = mListener.promote();
   2584     }
   2585 
   2586     if (msg == NULL) {
   2587         SET_ERR("HAL sent NULL notify message!");
   2588         return;
   2589     }
   2590 
   2591     switch (msg->type) {
   2592         case CAMERA3_MSG_ERROR: {
   2593             notifyError(msg->message.error, listener);
   2594             break;
   2595         }
   2596         case CAMERA3_MSG_SHUTTER: {
   2597             notifyShutter(msg->message.shutter, listener);
   2598             break;
   2599         }
   2600         default:
   2601             SET_ERR("Unknown notify message from HAL: %d",
   2602                     msg->type);
   2603     }
   2604 }
   2605 
   2606 void Camera3Device::notifyError(const camera3_error_msg_t &msg,
   2607         sp<NotificationListener> listener) {
   2608 
   2609     // Map camera HAL error codes to ICameraDeviceCallback error codes
   2610     // Index into this with the HAL error code
   2611     static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
   2612         // 0 = Unused error code
   2613         hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
   2614         // 1 = CAMERA3_MSG_ERROR_DEVICE
   2615         hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
   2616         // 2 = CAMERA3_MSG_ERROR_REQUEST
   2617         hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
   2618         // 3 = CAMERA3_MSG_ERROR_RESULT
   2619         hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
   2620         // 4 = CAMERA3_MSG_ERROR_BUFFER
   2621         hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
   2622     };
   2623 
   2624     int32_t errorCode =
   2625             ((msg.error_code >= 0) &&
   2626                     (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
   2627             halErrorMap[msg.error_code] :
   2628             hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
   2629 
   2630     int streamId = 0;
   2631     if (msg.error_stream != NULL) {
   2632         Camera3Stream *stream =
   2633                 Camera3Stream::cast(msg.error_stream);
   2634         streamId = stream->getId();
   2635     }
   2636     ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
   2637             mId, __FUNCTION__, msg.frame_number,
   2638             streamId, msg.error_code);
   2639 
   2640     CaptureResultExtras resultExtras;
   2641     switch (errorCode) {
   2642         case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
   2643             // SET_ERR calls notifyError
   2644             SET_ERR("Camera HAL reported serious device error");
   2645             break;
   2646         case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
   2647         case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
   2648         case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
   2649             {
   2650                 Mutex::Autolock l(mInFlightLock);
   2651                 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
   2652                 if (idx >= 0) {
   2653                     InFlightRequest &r = mInFlightMap.editValueAt(idx);
   2654                     r.requestStatus = msg.error_code;
   2655                     resultExtras = r.resultExtras;
   2656                 } else {
   2657                     resultExtras.frameNumber = msg.frame_number;
   2658                     ALOGE("Camera %d: %s: cannot find in-flight request on "
   2659                             "frame %" PRId64 " error", mId, __FUNCTION__,
   2660                             resultExtras.frameNumber);
   2661                 }
   2662             }
   2663             resultExtras.errorStreamId = streamId;
   2664             if (listener != NULL) {
   2665                 listener->notifyError(errorCode, resultExtras);
   2666             } else {
   2667                 ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
   2668             }
   2669             break;
   2670         default:
   2671             // SET_ERR calls notifyError
   2672             SET_ERR("Unknown error message from HAL: %d", msg.error_code);
   2673             break;
   2674     }
   2675 }
   2676 
   2677 void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
   2678         sp<NotificationListener> listener) {
   2679     ssize_t idx;
   2680 
   2681     // Set timestamp for the request in the in-flight tracking
   2682     // and get the request ID to send upstream
   2683     {
   2684         Mutex::Autolock l(mInFlightLock);
   2685         idx = mInFlightMap.indexOfKey(msg.frame_number);
   2686         if (idx >= 0) {
   2687             InFlightRequest &r = mInFlightMap.editValueAt(idx);
   2688 
   2689             // Verify ordering of shutter notifications
   2690             {
   2691                 Mutex::Autolock l(mOutputLock);
   2692                 // TODO: need to track errors for tighter bounds on expected frame number.
   2693                 if (r.hasInputBuffer) {
   2694                     if (msg.frame_number < mNextReprocessShutterFrameNumber) {
   2695                         SET_ERR("Shutter notification out-of-order. Expected "
   2696                                 "notification for frame %d, got frame %d",
   2697                                 mNextReprocessShutterFrameNumber, msg.frame_number);
   2698                         return;
   2699                     }
   2700                     mNextReprocessShutterFrameNumber = msg.frame_number + 1;
   2701                 } else {
   2702                     if (msg.frame_number < mNextShutterFrameNumber) {
   2703                         SET_ERR("Shutter notification out-of-order. Expected "
   2704                                 "notification for frame %d, got frame %d",
   2705                                 mNextShutterFrameNumber, msg.frame_number);
   2706                         return;
   2707                     }
   2708                     mNextShutterFrameNumber = msg.frame_number + 1;
   2709                 }
   2710             }
   2711 
   2712             ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
   2713                     mId, __FUNCTION__,
   2714                     msg.frame_number, r.resultExtras.requestId, msg.timestamp);
   2715             // Call listener, if any
   2716             if (listener != NULL) {
   2717                 listener->notifyShutter(r.resultExtras, msg.timestamp);
   2718             }
   2719 
   2720             r.shutterTimestamp = msg.timestamp;
   2721 
   2722             // send pending result and buffers
   2723             sendCaptureResult(r.pendingMetadata, r.resultExtras,
   2724                 r.collectedPartialResult, msg.frame_number,
   2725                 r.hasInputBuffer, r.aeTriggerCancelOverride);
   2726             returnOutputBuffers(r.pendingOutputBuffers.array(),
   2727                 r.pendingOutputBuffers.size(), r.shutterTimestamp);
   2728             r.pendingOutputBuffers.clear();
   2729 
   2730             removeInFlightRequestIfReadyLocked(idx);
   2731         }
   2732     }
   2733     if (idx < 0) {
   2734         SET_ERR("Shutter notification for non-existent frame number %d",
   2735                 msg.frame_number);
   2736     }
   2737 }
   2738 
   2739 
   2740 CameraMetadata Camera3Device::getLatestRequestLocked() {
   2741     ALOGV("%s", __FUNCTION__);
   2742 
   2743     CameraMetadata retVal;
   2744 
   2745     if (mRequestThread != NULL) {
   2746         retVal = mRequestThread->getLatestRequest();
   2747     }
   2748 
   2749     return retVal;
   2750 }
   2751 
   2752 
   2753 void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
   2754         int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
   2755     mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
   2756 }
   2757 
   2758 /**
   2759  * RequestThread inner class methods
   2760  */
   2761 
   2762 Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
   2763         sp<StatusTracker> statusTracker,
   2764         camera3_device_t *hal3Device,
   2765         bool aeLockAvailable) :
   2766         Thread(/*canCallJava*/false),
   2767         mParent(parent),
   2768         mStatusTracker(statusTracker),
   2769         mHal3Device(hal3Device),
   2770         mListener(nullptr),
   2771         mId(getId(parent)),
   2772         mReconfigured(false),
   2773         mDoPause(false),
   2774         mPaused(true),
   2775         mFrameNumber(0),
   2776         mLatestRequestId(NAME_NOT_FOUND),
   2777         mCurrentAfTriggerId(0),
   2778         mCurrentPreCaptureTriggerId(0),
   2779         mRepeatingLastFrameNumber(
   2780             hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
   2781         mAeLockAvailable(aeLockAvailable),
   2782         mPrepareVideoStream(false) {
   2783     mStatusId = statusTracker->addComponent();
   2784 }
   2785 
   2786 void Camera3Device::RequestThread::setNotificationListener(
   2787         wp<NotificationListener> listener) {
   2788     Mutex::Autolock l(mRequestLock);
   2789     mListener = listener;
   2790 }
   2791 
   2792 void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed) {
   2793     Mutex::Autolock l(mRequestLock);
   2794     mReconfigured = true;
   2795     // Prepare video stream for high speed recording.
   2796     mPrepareVideoStream = isConstrainedHighSpeed;
   2797 }
   2798 
   2799 status_t Camera3Device::RequestThread::queueRequestList(
   2800         List<sp<CaptureRequest> > &requests,
   2801         /*out*/
   2802         int64_t *lastFrameNumber) {
   2803     Mutex::Autolock l(mRequestLock);
   2804     for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
   2805             ++it) {
   2806         mRequestQueue.push_back(*it);
   2807     }
   2808 
   2809     if (lastFrameNumber != NULL) {
   2810         *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
   2811         ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
   2812               __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
   2813               *lastFrameNumber);
   2814     }
   2815 
   2816     unpauseForNewRequests();
   2817 
   2818     return OK;
   2819 }
   2820 
   2821 
   2822 status_t Camera3Device::RequestThread::queueTrigger(
   2823         RequestTrigger trigger[],
   2824         size_t count) {
   2825 
   2826     Mutex::Autolock l(mTriggerMutex);
   2827     status_t ret;
   2828 
   2829     for (size_t i = 0; i < count; ++i) {
   2830         ret = queueTriggerLocked(trigger[i]);
   2831 
   2832         if (ret != OK) {
   2833             return ret;
   2834         }
   2835     }
   2836 
   2837     return OK;
   2838 }
   2839 
   2840 int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
   2841     sp<Camera3Device> d = device.promote();
   2842     if (d != NULL) return d->mId;
   2843     return 0;
   2844 }
   2845 
   2846 status_t Camera3Device::RequestThread::queueTriggerLocked(
   2847         RequestTrigger trigger) {
   2848 
   2849     uint32_t tag = trigger.metadataTag;
   2850     ssize_t index = mTriggerMap.indexOfKey(tag);
   2851 
   2852     switch (trigger.getTagType()) {
   2853         case TYPE_BYTE:
   2854         // fall-through
   2855         case TYPE_INT32:
   2856             break;
   2857         default:
   2858             ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
   2859                     trigger.getTagType());
   2860             return INVALID_OPERATION;
   2861     }
   2862 
   2863     /**
   2864      * Collect only the latest trigger, since we only have 1 field
   2865      * in the request settings per trigger tag, and can't send more than 1
   2866      * trigger per request.
   2867      */
   2868     if (index != NAME_NOT_FOUND) {
   2869         mTriggerMap.editValueAt(index) = trigger;
   2870     } else {
   2871         mTriggerMap.add(tag, trigger);
   2872     }
   2873 
   2874     return OK;
   2875 }
   2876 
   2877 status_t Camera3Device::RequestThread::setRepeatingRequests(
   2878         const RequestList &requests,
   2879         /*out*/
   2880         int64_t *lastFrameNumber) {
   2881     Mutex::Autolock l(mRequestLock);
   2882     if (lastFrameNumber != NULL) {
   2883         *lastFrameNumber = mRepeatingLastFrameNumber;
   2884     }
   2885     mRepeatingRequests.clear();
   2886     mRepeatingRequests.insert(mRepeatingRequests.begin(),
   2887             requests.begin(), requests.end());
   2888 
   2889     unpauseForNewRequests();
   2890 
   2891     mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
   2892     return OK;
   2893 }
   2894 
   2895 bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
   2896     if (mRepeatingRequests.empty()) {
   2897         return false;
   2898     }
   2899     int32_t requestId = requestIn->mResultExtras.requestId;
   2900     const RequestList &repeatRequests = mRepeatingRequests;
   2901     // All repeating requests are guaranteed to have same id so only check first quest
   2902     const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
   2903     return (firstRequest->mResultExtras.requestId == requestId);
   2904 }
   2905 
   2906 status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
   2907     Mutex::Autolock l(mRequestLock);
   2908     return clearRepeatingRequestsLocked(lastFrameNumber);
   2909 
   2910 }
   2911 
   2912 status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
   2913     mRepeatingRequests.clear();
   2914     if (lastFrameNumber != NULL) {
   2915         *lastFrameNumber = mRepeatingLastFrameNumber;
   2916     }
   2917     mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
   2918     return OK;
   2919 }
   2920 
   2921 status_t Camera3Device::RequestThread::clear(
   2922         /*out*/int64_t *lastFrameNumber) {
   2923     Mutex::Autolock l(mRequestLock);
   2924     ALOGV("RequestThread::%s:", __FUNCTION__);
   2925 
   2926     mRepeatingRequests.clear();
   2927 
   2928     // Send errors for all requests pending in the request queue, including
   2929     // pending repeating requests
   2930     sp<NotificationListener> listener = mListener.promote();
   2931     if (listener != NULL) {
   2932         for (RequestList::iterator it = mRequestQueue.begin();
   2933                  it != mRequestQueue.end(); ++it) {
   2934             // Abort the input buffers for reprocess requests.
   2935             if ((*it)->mInputStream != NULL) {
   2936                 camera3_stream_buffer_t inputBuffer;
   2937                 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer);
   2938                 if (res != OK) {
   2939                     ALOGW("%s: %d: couldn't get input buffer while clearing the request "
   2940                             "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
   2941                 } else {
   2942                     res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
   2943                     if (res != OK) {
   2944                         ALOGE("%s: %d: couldn't return input buffer while clearing the request "
   2945                                 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
   2946                     }
   2947                 }
   2948             }
   2949             // Set the frame number this request would have had, if it
   2950             // had been submitted; this frame number will not be reused.
   2951             // The requestId and burstId fields were set when the request was
   2952             // submitted originally (in convertMetadataListToRequestListLocked)
   2953             (*it)->mResultExtras.frameNumber = mFrameNumber++;
   2954             listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
   2955                     (*it)->mResultExtras);
   2956         }
   2957     }
   2958     mRequestQueue.clear();
   2959     mTriggerMap.clear();
   2960     if (lastFrameNumber != NULL) {
   2961         *lastFrameNumber = mRepeatingLastFrameNumber;
   2962     }
   2963     mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
   2964     return OK;
   2965 }
   2966 
   2967 status_t Camera3Device::RequestThread::flush() {
   2968     ATRACE_CALL();
   2969     Mutex::Autolock l(mFlushLock);
   2970 
   2971     if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
   2972         return mHal3Device->ops->flush(mHal3Device);
   2973     }
   2974 
   2975     return -ENOTSUP;
   2976 }
   2977 
   2978 void Camera3Device::RequestThread::setPaused(bool paused) {
   2979     Mutex::Autolock l(mPauseLock);
   2980     mDoPause = paused;
   2981     mDoPauseSignal.signal();
   2982 }
   2983 
   2984 status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
   2985         int32_t requestId, nsecs_t timeout) {
   2986     Mutex::Autolock l(mLatestRequestMutex);
   2987     status_t res;
   2988     while (mLatestRequestId != requestId) {
   2989         nsecs_t startTime = systemTime();
   2990 
   2991         res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
   2992         if (res != OK) return res;
   2993 
   2994         timeout -= (systemTime() - startTime);
   2995     }
   2996 
   2997     return OK;
   2998 }
   2999 
   3000 void Camera3Device::RequestThread::requestExit() {
   3001     // Call parent to set up shutdown
   3002     Thread::requestExit();
   3003     // The exit from any possible waits
   3004     mDoPauseSignal.signal();
   3005     mRequestSignal.signal();
   3006 }
   3007 
   3008 
   3009 /**
   3010  * For devices <= CAMERA_DEVICE_API_VERSION_3_2, AE_PRECAPTURE_TRIGGER_CANCEL is not supported so
   3011  * we need to override AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE and AE_LOCK_OFF
   3012  * to AE_LOCK_ON to start cancelling AE precapture. If AE lock is not available, it still overrides
   3013  * AE_PRECAPTURE_TRIGGER_CANCEL to AE_PRECAPTURE_TRIGGER_IDLE but doesn't add AE_LOCK_ON to the
   3014  * request.
   3015  */
   3016 void Camera3Device::RequestThread::handleAePrecaptureCancelRequest(sp<CaptureRequest> request) {
   3017     request->mAeTriggerCancelOverride.applyAeLock = false;
   3018     request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = false;
   3019 
   3020     if (mHal3Device->common.version > CAMERA_DEVICE_API_VERSION_3_2) {
   3021         return;
   3022     }
   3023 
   3024     camera_metadata_entry_t aePrecaptureTrigger =
   3025             request->mSettings.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
   3026     if (aePrecaptureTrigger.count > 0 &&
   3027             aePrecaptureTrigger.data.u8[0] == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
   3028         // Always override CANCEL to IDLE
   3029         uint8_t aePrecaptureTrigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
   3030         request->mSettings.update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &aePrecaptureTrigger, 1);
   3031         request->mAeTriggerCancelOverride.applyAePrecaptureTrigger = true;
   3032         request->mAeTriggerCancelOverride.aePrecaptureTrigger =
   3033                 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
   3034 
   3035         if (mAeLockAvailable == true) {
   3036             camera_metadata_entry_t aeLock = request->mSettings.find(ANDROID_CONTROL_AE_LOCK);
   3037             if (aeLock.count == 0 ||  aeLock.data.u8[0] == ANDROID_CONTROL_AE_LOCK_OFF) {
   3038                 uint8_t aeLock = ANDROID_CONTROL_AE_LOCK_ON;
   3039                 request->mSettings.update(ANDROID_CONTROL_AE_LOCK, &aeLock, 1);
   3040                 request->mAeTriggerCancelOverride.applyAeLock = true;
   3041                 request->mAeTriggerCancelOverride.aeLock = ANDROID_CONTROL_AE_LOCK_OFF;
   3042             }
   3043         }
   3044     }
   3045 }
   3046 
   3047 /**
   3048  * Override result metadata for cancelling AE precapture trigger applied in
   3049  * handleAePrecaptureCancelRequest().
   3050  */
   3051 void Camera3Device::overrideResultForPrecaptureCancel(
   3052         CameraMetadata *result, const AeTriggerCancelOverride_t &aeTriggerCancelOverride) {
   3053     if (aeTriggerCancelOverride.applyAeLock) {
   3054         // Only devices <= v3.2 should have this override
   3055         assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
   3056         result->update(ANDROID_CONTROL_AE_LOCK, &aeTriggerCancelOverride.aeLock, 1);
   3057     }
   3058 
   3059     if (aeTriggerCancelOverride.applyAePrecaptureTrigger) {
   3060         // Only devices <= v3.2 should have this override
   3061         assert(mDeviceVersion <= CAMERA_DEVICE_API_VERSION_3_2);
   3062         result->update(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
   3063                 &aeTriggerCancelOverride.aePrecaptureTrigger, 1);
   3064     }
   3065 }
   3066 
   3067 void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
   3068     bool surfaceAbandoned = false;
   3069     int64_t lastFrameNumber = 0;
   3070     sp<NotificationListener> listener;
   3071     {
   3072         Mutex::Autolock l(mRequestLock);
   3073         // Check all streams needed by repeating requests are still valid. Otherwise, stop
   3074         // repeating requests.
   3075         for (const auto& request : mRepeatingRequests) {
   3076             for (const auto& s : request->mOutputStreams) {
   3077                 if (s->isAbandoned()) {
   3078                     surfaceAbandoned = true;
   3079                     clearRepeatingRequestsLocked(&lastFrameNumber);
   3080                     break;
   3081                 }
   3082             }
   3083             if (surfaceAbandoned) {
   3084                 break;
   3085             }
   3086         }
   3087         listener = mListener.promote();
   3088     }
   3089 
   3090     if (listener != NULL && surfaceAbandoned) {
   3091         listener->notifyRepeatingRequestError(lastFrameNumber);
   3092     }
   3093 }
   3094 
   3095 bool Camera3Device::RequestThread::threadLoop() {
   3096     ATRACE_CALL();
   3097     status_t res;
   3098 
   3099     // Handle paused state.
   3100     if (waitIfPaused()) {
   3101         return true;
   3102     }
   3103 
   3104     // Wait for the next batch of requests.
   3105     waitForNextRequestBatch();
   3106     if (mNextRequests.size() == 0) {
   3107         return true;
   3108     }
   3109 
   3110     // Get the latest request ID, if any
   3111     int latestRequestId;
   3112     camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
   3113             captureRequest->mSettings.find(ANDROID_REQUEST_ID);
   3114     if (requestIdEntry.count > 0) {
   3115         latestRequestId = requestIdEntry.data.i32[0];
   3116     } else {
   3117         ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
   3118         latestRequestId = NAME_NOT_FOUND;
   3119     }
   3120 
   3121     // Prepare a batch of HAL requests and output buffers.
   3122     res = prepareHalRequests();
   3123     if (res == TIMED_OUT) {
   3124         // Not a fatal error if getting output buffers time out.
   3125         cleanUpFailedRequests(/*sendRequestError*/ true);
   3126         // Check if any stream is abandoned.
   3127         checkAndStopRepeatingRequest();
   3128         return true;
   3129     } else if (res != OK) {
   3130         cleanUpFailedRequests(/*sendRequestError*/ false);
   3131         return false;
   3132     }
   3133 
   3134     // Inform waitUntilRequestProcessed thread of a new request ID
   3135     {
   3136         Mutex::Autolock al(mLatestRequestMutex);
   3137 
   3138         mLatestRequestId = latestRequestId;
   3139         mLatestRequestSignal.signal();
   3140     }
   3141 
   3142     // Submit a batch of requests to HAL.
   3143     // Use flush lock only when submitting multilple requests in a batch.
   3144     // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
   3145     // which may take a long time to finish so synchronizing flush() and
   3146     // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
   3147     // For now, only synchronize for high speed recording and we should figure something out for
   3148     // removing the synchronization.
   3149     bool useFlushLock = mNextRequests.size() > 1;
   3150 
   3151     if (useFlushLock) {
   3152         mFlushLock.lock();
   3153     }
   3154 
   3155     ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
   3156             mNextRequests.size());
   3157     for (auto& nextRequest : mNextRequests) {
   3158         // Submit request and block until ready for next one
   3159         ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
   3160         ATRACE_BEGIN("camera3->process_capture_request");
   3161         res = mHal3Device->ops->process_capture_request(mHal3Device, &nextRequest.halRequest);
   3162         ATRACE_END();
   3163 
   3164         if (res != OK) {
   3165             // Should only get a failure here for malformed requests or device-level
   3166             // errors, so consider all errors fatal.  Bad metadata failures should
   3167             // come through notify.
   3168             SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
   3169                     " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
   3170                     res);
   3171             cleanUpFailedRequests(/*sendRequestError*/ false);
   3172             if (useFlushLock) {
   3173                 mFlushLock.unlock();
   3174             }
   3175             return false;
   3176         }
   3177 
   3178         // Mark that the request has be submitted successfully.
   3179         nextRequest.submitted = true;
   3180 
   3181         // Update the latest request sent to HAL
   3182         if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
   3183             Mutex::Autolock al(mLatestRequestMutex);
   3184 
   3185             camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
   3186             mLatestRequest.acquire(cloned);
   3187 
   3188             sp<Camera3Device> parent = mParent.promote();
   3189             if (parent != NULL) {
   3190                 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
   3191                         0, mLatestRequest);
   3192             }
   3193         }
   3194 
   3195         if (nextRequest.halRequest.settings != NULL) {
   3196             nextRequest.captureRequest->mSettings.unlock(nextRequest.halRequest.settings);
   3197         }
   3198 
   3199         // Remove any previously queued triggers (after unlock)
   3200         res = removeTriggers(mPrevRequest);
   3201         if (res != OK) {
   3202             SET_ERR("RequestThread: Unable to remove triggers "
   3203                   "(capture request %d, HAL device: %s (%d)",
   3204                   nextRequest.halRequest.frame_number, strerror(-res), res);
   3205             cleanUpFailedRequests(/*sendRequestError*/ false);
   3206             if (useFlushLock) {
   3207                 mFlushLock.unlock();
   3208             }
   3209             return false;
   3210         }
   3211     }
   3212 
   3213     if (useFlushLock) {
   3214         mFlushLock.unlock();
   3215     }
   3216 
   3217     // Unset as current request
   3218     {
   3219         Mutex::Autolock l(mRequestLock);
   3220         mNextRequests.clear();
   3221     }
   3222 
   3223     return true;
   3224 }
   3225 
   3226 status_t Camera3Device::RequestThread::prepareHalRequests() {
   3227     ATRACE_CALL();
   3228 
   3229     for (auto& nextRequest : mNextRequests) {
   3230         sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
   3231         camera3_capture_request_t* halRequest = &nextRequest.halRequest;
   3232         Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
   3233 
   3234         // Prepare a request to HAL
   3235         halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
   3236 
   3237         // Insert any queued triggers (before metadata is locked)
   3238         status_t res = insertTriggers(captureRequest);
   3239 
   3240         if (res < 0) {
   3241             SET_ERR("RequestThread: Unable to insert triggers "
   3242                     "(capture request %d, HAL device: %s (%d)",
   3243                     halRequest->frame_number, strerror(-res), res);
   3244             return INVALID_OPERATION;
   3245         }
   3246         int triggerCount = res;
   3247         bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
   3248         mPrevTriggers = triggerCount;
   3249 
   3250         // If the request is the same as last, or we had triggers last time
   3251         if (mPrevRequest != captureRequest || triggersMixedIn) {
   3252             /**
   3253              * HAL workaround:
   3254              * Insert a dummy trigger ID if a trigger is set but no trigger ID is
   3255              */
   3256             res = addDummyTriggerIds(captureRequest);
   3257             if (res != OK) {
   3258                 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
   3259                         "(capture request %d, HAL device: %s (%d)",
   3260                         halRequest->frame_number, strerror(-res), res);
   3261                 return INVALID_OPERATION;
   3262             }
   3263 
   3264             /**
   3265              * The request should be presorted so accesses in HAL
   3266              *   are O(logn). Sidenote, sorting a sorted metadata is nop.
   3267              */
   3268             captureRequest->mSettings.sort();
   3269             halRequest->settings = captureRequest->mSettings.getAndLock();
   3270             mPrevRequest = captureRequest;
   3271             ALOGVV("%s: Request settings are NEW", __FUNCTION__);
   3272 
   3273             IF_ALOGV() {
   3274                 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
   3275                 find_camera_metadata_ro_entry(
   3276                         halRequest->settings,
   3277                         ANDROID_CONTROL_AF_TRIGGER,
   3278                         &e
   3279                 );
   3280                 if (e.count > 0) {
   3281                     ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
   3282                           __FUNCTION__,
   3283                           halRequest->frame_number,
   3284                           e.data.u8[0]);
   3285                 }
   3286             }
   3287         } else {
   3288             // leave request.settings NULL to indicate 'reuse latest given'
   3289             ALOGVV("%s: Request settings are REUSED",
   3290                    __FUNCTION__);
   3291         }
   3292 
   3293         uint32_t totalNumBuffers = 0;
   3294 
   3295         // Fill in buffers
   3296         if (captureRequest->mInputStream != NULL) {
   3297             halRequest->input_buffer = &captureRequest->mInputBuffer;
   3298             totalNumBuffers += 1;
   3299         } else {
   3300             halRequest->input_buffer = NULL;
   3301         }
   3302 
   3303         outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
   3304                 captureRequest->mOutputStreams.size());
   3305         halRequest->output_buffers = outputBuffers->array();
   3306         for (size_t i = 0; i < captureRequest->mOutputStreams.size(); i++) {
   3307             sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(i);
   3308 
   3309             // Prepare video buffers for high speed recording on the first video request.
   3310             if (mPrepareVideoStream && outputStream->isVideoStream()) {
   3311                 // Only try to prepare video stream on the first video request.
   3312                 mPrepareVideoStream = false;
   3313 
   3314                 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX);
   3315                 while (res == NOT_ENOUGH_DATA) {
   3316                     res = outputStream->prepareNextBuffer();
   3317                 }
   3318                 if (res != OK) {
   3319                     ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
   3320                         __FUNCTION__, strerror(-res), res);
   3321                     outputStream->cancelPrepare();
   3322                 }
   3323             }
   3324 
   3325             res = outputStream->getBuffer(&outputBuffers->editItemAt(i));
   3326             if (res != OK) {
   3327                 // Can't get output buffer from gralloc queue - this could be due to
   3328                 // abandoned queue or other consumer misbehavior, so not a fatal
   3329                 // error
   3330                 ALOGE("RequestThread: Can't get output buffer, skipping request:"
   3331                         " %s (%d)", strerror(-res), res);
   3332 
   3333                 return TIMED_OUT;
   3334             }
   3335             halRequest->num_output_buffers++;
   3336         }
   3337         totalNumBuffers += halRequest->num_output_buffers;
   3338 
   3339         // Log request in the in-flight queue
   3340         sp<Camera3Device> parent = mParent.promote();
   3341         if (parent == NULL) {
   3342             // Should not happen, and nowhere to send errors to, so just log it
   3343             CLOGE("RequestThread: Parent is gone");
   3344             return INVALID_OPERATION;
   3345         }
   3346         res = parent->registerInFlight(halRequest->frame_number,
   3347                 totalNumBuffers, captureRequest->mResultExtras,
   3348                 /*hasInput*/halRequest->input_buffer != NULL,
   3349                 captureRequest->mAeTriggerCancelOverride);
   3350         ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
   3351                ", burstId = %" PRId32 ".",
   3352                 __FUNCTION__,
   3353                 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
   3354                 captureRequest->mResultExtras.burstId);
   3355         if (res != OK) {
   3356             SET_ERR("RequestThread: Unable to register new in-flight request:"
   3357                     " %s (%d)", strerror(-res), res);
   3358             return INVALID_OPERATION;
   3359         }
   3360     }
   3361 
   3362     return OK;
   3363 }
   3364 
   3365 CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
   3366     Mutex::Autolock al(mLatestRequestMutex);
   3367 
   3368     ALOGV("RequestThread::%s", __FUNCTION__);
   3369 
   3370     return mLatestRequest;
   3371 }
   3372 
   3373 bool Camera3Device::RequestThread::isStreamPending(
   3374         sp<Camera3StreamInterface>& stream) {
   3375     Mutex::Autolock l(mRequestLock);
   3376 
   3377     for (const auto& nextRequest : mNextRequests) {
   3378         if (!nextRequest.submitted) {
   3379             for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
   3380                 if (stream == s) return true;
   3381             }
   3382             if (stream == nextRequest.captureRequest->mInputStream) return true;
   3383         }
   3384     }
   3385 
   3386     for (const auto& request : mRequestQueue) {
   3387         for (const auto& s : request->mOutputStreams) {
   3388             if (stream == s) return true;
   3389         }
   3390         if (stream == request->mInputStream) return true;
   3391     }
   3392 
   3393     for (const auto& request : mRepeatingRequests) {
   3394         for (const auto& s : request->mOutputStreams) {
   3395             if (stream == s) return true;
   3396         }
   3397         if (stream == request->mInputStream) return true;
   3398     }
   3399 
   3400     return false;
   3401 }
   3402 
   3403 void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
   3404     if (mNextRequests.empty()) {
   3405         return;
   3406     }
   3407 
   3408     for (auto& nextRequest : mNextRequests) {
   3409         // Skip the ones that have been submitted successfully.
   3410         if (nextRequest.submitted) {
   3411             continue;
   3412         }
   3413 
   3414         sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
   3415         camera3_capture_request_t* halRequest = &nextRequest.halRequest;
   3416         Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
   3417 
   3418         if (halRequest->settings != NULL) {
   3419             captureRequest->mSettings.unlock(halRequest->settings);
   3420         }
   3421 
   3422         if (captureRequest->mInputStream != NULL) {
   3423             captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
   3424             captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
   3425         }
   3426 
   3427         for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
   3428             outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
   3429             captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
   3430         }
   3431 
   3432         if (sendRequestError) {
   3433             Mutex::Autolock l(mRequestLock);
   3434             sp<NotificationListener> listener = mListener.promote();
   3435             if (listener != NULL) {
   3436                 listener->notifyError(
   3437                         hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
   3438                         captureRequest->mResultExtras);
   3439             }
   3440         }
   3441     }
   3442 
   3443     Mutex::Autolock l(mRequestLock);
   3444     mNextRequests.clear();
   3445 }
   3446 
   3447 void Camera3Device::RequestThread::waitForNextRequestBatch() {
   3448     // Optimized a bit for the simple steady-state case (single repeating
   3449     // request), to avoid putting that request in the queue temporarily.
   3450     Mutex::Autolock l(mRequestLock);
   3451 
   3452     assert(mNextRequests.empty());
   3453 
   3454     NextRequest nextRequest;
   3455     nextRequest.captureRequest = waitForNextRequestLocked();
   3456     if (nextRequest.captureRequest == nullptr) {
   3457         return;
   3458     }
   3459 
   3460     nextRequest.halRequest = camera3_capture_request_t();
   3461     nextRequest.submitted = false;
   3462     mNextRequests.add(nextRequest);
   3463 
   3464     // Wait for additional requests
   3465     const size_t batchSize = nextRequest.captureRequest->mBatchSize;
   3466 
   3467     for (size_t i = 1; i < batchSize; i++) {
   3468         NextRequest additionalRequest;
   3469         additionalRequest.captureRequest = waitForNextRequestLocked();
   3470         if (additionalRequest.captureRequest == nullptr) {
   3471             break;
   3472         }
   3473 
   3474         additionalRequest.halRequest = camera3_capture_request_t();
   3475         additionalRequest.submitted = false;
   3476         mNextRequests.add(additionalRequest);
   3477     }
   3478 
   3479     if (mNextRequests.size() < batchSize) {
   3480         ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
   3481                 mNextRequests.size(), batchSize);
   3482         cleanUpFailedRequests(/*sendRequestError*/true);
   3483     }
   3484 
   3485     return;
   3486 }
   3487 
   3488 sp<Camera3Device::CaptureRequest>
   3489         Camera3Device::RequestThread::waitForNextRequestLocked() {
   3490     status_t res;
   3491     sp<CaptureRequest> nextRequest;
   3492 
   3493     while (mRequestQueue.empty()) {
   3494         if (!mRepeatingRequests.empty()) {
   3495             // Always atomically enqueue all requests in a repeating request
   3496             // list. Guarantees a complete in-sequence set of captures to
   3497             // application.
   3498             const RequestList &requests = mRepeatingRequests;
   3499             RequestList::const_iterator firstRequest =
   3500                     requests.begin();
   3501             nextRequest = *firstRequest;
   3502             mRequestQueue.insert(mRequestQueue.end(),
   3503                     ++firstRequest,
   3504                     requests.end());
   3505             // No need to wait any longer
   3506 
   3507             mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
   3508 
   3509             break;
   3510         }
   3511 
   3512         res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
   3513 
   3514         if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
   3515                 exitPending()) {
   3516             Mutex::Autolock pl(mPauseLock);
   3517             if (mPaused == false) {
   3518                 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
   3519                 mPaused = true;
   3520                 // Let the tracker know
   3521                 sp<StatusTracker> statusTracker = mStatusTracker.promote();
   3522                 if (statusTracker != 0) {
   3523                     statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
   3524                 }
   3525             }
   3526             // Stop waiting for now and let thread management happen
   3527             return NULL;
   3528         }
   3529     }
   3530 
   3531     if (nextRequest == NULL) {
   3532         // Don't have a repeating request already in hand, so queue
   3533         // must have an entry now.
   3534         RequestList::iterator firstRequest =
   3535                 mRequestQueue.begin();
   3536         nextRequest = *firstRequest;
   3537         mRequestQueue.erase(firstRequest);
   3538     }
   3539 
   3540     // In case we've been unpaused by setPaused clearing mDoPause, need to
   3541     // update internal pause state (capture/setRepeatingRequest unpause
   3542     // directly).
   3543     Mutex::Autolock pl(mPauseLock);
   3544     if (mPaused) {
   3545         ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
   3546         sp<StatusTracker> statusTracker = mStatusTracker.promote();
   3547         if (statusTracker != 0) {
   3548             statusTracker->markComponentActive(mStatusId);
   3549         }
   3550     }
   3551     mPaused = false;
   3552 
   3553     // Check if we've reconfigured since last time, and reset the preview
   3554     // request if so. Can't use 'NULL request == repeat' across configure calls.
   3555     if (mReconfigured) {
   3556         mPrevRequest.clear();
   3557         mReconfigured = false;
   3558     }
   3559 
   3560     if (nextRequest != NULL) {
   3561         nextRequest->mResultExtras.frameNumber = mFrameNumber++;
   3562         nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
   3563         nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
   3564 
   3565         // Since RequestThread::clear() removes buffers from the input stream,
   3566         // get the right buffer here before unlocking mRequestLock
   3567         if (nextRequest->mInputStream != NULL) {
   3568             res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
   3569             if (res != OK) {
   3570                 // Can't get input buffer from gralloc queue - this could be due to
   3571                 // disconnected queue or other producer misbehavior, so not a fatal
   3572                 // error
   3573                 ALOGE("%s: Can't get input buffer, skipping request:"
   3574                         " %s (%d)", __FUNCTION__, strerror(-res), res);
   3575 
   3576                 sp<NotificationListener> listener = mListener.promote();
   3577                 if (listener != NULL) {
   3578                     listener->notifyError(
   3579                             hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
   3580                             nextRequest->mResultExtras);
   3581                 }
   3582                 return NULL;
   3583             }
   3584         }
   3585     }
   3586 
   3587     handleAePrecaptureCancelRequest(nextRequest);
   3588 
   3589     return nextRequest;
   3590 }
   3591 
   3592 bool Camera3Device::RequestThread::waitIfPaused() {
   3593     status_t res;
   3594     Mutex::Autolock l(mPauseLock);
   3595     while (mDoPause) {
   3596         if (mPaused == false) {
   3597             mPaused = true;
   3598             ALOGV("%s: RequestThread: Paused", __FUNCTION__);
   3599             // Let the tracker know
   3600             sp<StatusTracker> statusTracker = mStatusTracker.promote();
   3601             if (statusTracker != 0) {
   3602                 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
   3603             }
   3604         }
   3605 
   3606         res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
   3607         if (res == TIMED_OUT || exitPending()) {
   3608             return true;
   3609         }
   3610     }
   3611     // We don't set mPaused to false here, because waitForNextRequest needs
   3612     // to further manage the paused state in case of starvation.
   3613     return false;
   3614 }
   3615 
   3616 void Camera3Device::RequestThread::unpauseForNewRequests() {
   3617     // With work to do, mark thread as unpaused.
   3618     // If paused by request (setPaused), don't resume, to avoid
   3619     // extra signaling/waiting overhead to waitUntilPaused
   3620     mRequestSignal.signal();
   3621     Mutex::Autolock p(mPauseLock);
   3622     if (!mDoPause) {
   3623         ALOGV("%s: RequestThread: Going active", __FUNCTION__);
   3624         if (mPaused) {
   3625             sp<StatusTracker> statusTracker = mStatusTracker.promote();
   3626             if (statusTracker != 0) {
   3627                 statusTracker->markComponentActive(mStatusId);
   3628             }
   3629         }
   3630         mPaused = false;
   3631     }
   3632 }
   3633 
   3634 void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
   3635     sp<Camera3Device> parent = mParent.promote();
   3636     if (parent != NULL) {
   3637         va_list args;
   3638         va_start(args, fmt);
   3639 
   3640         parent->setErrorStateV(fmt, args);
   3641 
   3642         va_end(args);
   3643     }
   3644 }
   3645 
   3646 status_t Camera3Device::RequestThread::insertTriggers(
   3647         const sp<CaptureRequest> &request) {
   3648 
   3649     Mutex::Autolock al(mTriggerMutex);
   3650 
   3651     sp<Camera3Device> parent = mParent.promote();
   3652     if (parent == NULL) {
   3653         CLOGE("RequestThread: Parent is gone");
   3654         return DEAD_OBJECT;
   3655     }
   3656 
   3657     CameraMetadata &metadata = request->mSettings;
   3658     size_t count = mTriggerMap.size();
   3659 
   3660     for (size_t i = 0; i < count; ++i) {
   3661         RequestTrigger trigger = mTriggerMap.valueAt(i);
   3662         uint32_t tag = trigger.metadataTag;
   3663 
   3664         if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
   3665             bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
   3666             uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
   3667             if (isAeTrigger) {
   3668                 request->mResultExtras.precaptureTriggerId = triggerId;
   3669                 mCurrentPreCaptureTriggerId = triggerId;
   3670             } else {
   3671                 request->mResultExtras.afTriggerId = triggerId;
   3672                 mCurrentAfTriggerId = triggerId;
   3673             }
   3674             if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
   3675                 continue; // Trigger ID tag is deprecated since device HAL 3.2
   3676             }
   3677         }
   3678 
   3679         camera_metadata_entry entry = metadata.find(tag);
   3680 
   3681         if (entry.count > 0) {
   3682             /**
   3683              * Already has an entry for this trigger in the request.
   3684              * Rewrite it with our requested trigger value.
   3685              */
   3686             RequestTrigger oldTrigger = trigger;
   3687 
   3688             oldTrigger.entryValue = entry.data.u8[0];
   3689 
   3690             mTriggerReplacedMap.add(tag, oldTrigger);
   3691         } else {
   3692             /**
   3693              * More typical, no trigger entry, so we just add it
   3694              */
   3695             mTriggerRemovedMap.add(tag, trigger);
   3696         }
   3697 
   3698         status_t res;
   3699 
   3700         switch (trigger.getTagType()) {
   3701             case TYPE_BYTE: {
   3702                 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
   3703                 res = metadata.update(tag,
   3704                                       &entryValue,
   3705                                       /*count*/1);
   3706                 break;
   3707             }
   3708             case TYPE_INT32:
   3709                 res = metadata.update(tag,
   3710                                       &trigger.entryValue,
   3711                                       /*count*/1);
   3712                 break;
   3713             default:
   3714                 ALOGE("%s: Type not supported: 0x%x",
   3715                       __FUNCTION__,
   3716                       trigger.getTagType());
   3717                 return INVALID_OPERATION;
   3718         }
   3719 
   3720         if (res != OK) {
   3721             ALOGE("%s: Failed to update request metadata with trigger tag %s"
   3722                   ", value %d", __FUNCTION__, trigger.getTagName(),
   3723                   trigger.entryValue);
   3724             return res;
   3725         }
   3726 
   3727         ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
   3728               trigger.getTagName(),
   3729               trigger.entryValue);
   3730     }
   3731 
   3732     mTriggerMap.clear();
   3733 
   3734     return count;
   3735 }
   3736 
   3737 status_t Camera3Device::RequestThread::removeTriggers(
   3738         const sp<CaptureRequest> &request) {
   3739     Mutex::Autolock al(mTriggerMutex);
   3740 
   3741     CameraMetadata &metadata = request->mSettings;
   3742 
   3743     /**
   3744      * Replace all old entries with their old values.
   3745      */
   3746     for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
   3747         RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
   3748 
   3749         status_t res;
   3750 
   3751         uint32_t tag = trigger.metadataTag;
   3752         switch (trigger.getTagType()) {
   3753             case TYPE_BYTE: {
   3754                 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
   3755                 res = metadata.update(tag,
   3756                                       &entryValue,
   3757                                       /*count*/1);
   3758                 break;
   3759             }
   3760             case TYPE_INT32:
   3761                 res = metadata.update(tag,
   3762                                       &trigger.entryValue,
   3763                                       /*count*/1);
   3764                 break;
   3765             default:
   3766                 ALOGE("%s: Type not supported: 0x%x",
   3767                       __FUNCTION__,
   3768                       trigger.getTagType());
   3769                 return INVALID_OPERATION;
   3770         }
   3771 
   3772         if (res != OK) {
   3773             ALOGE("%s: Failed to restore request metadata with trigger tag %s"
   3774                   ", trigger value %d", __FUNCTION__,
   3775                   trigger.getTagName(), trigger.entryValue);
   3776             return res;
   3777         }
   3778     }
   3779     mTriggerReplacedMap.clear();
   3780 
   3781     /**
   3782      * Remove all new entries.
   3783      */
   3784     for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
   3785         RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
   3786         status_t res = metadata.erase(trigger.metadataTag);
   3787 
   3788         if (res != OK) {
   3789             ALOGE("%s: Failed to erase metadata with trigger tag %s"
   3790                   ", trigger value %d", __FUNCTION__,
   3791                   trigger.getTagName(), trigger.entryValue);
   3792             return res;
   3793         }
   3794     }
   3795     mTriggerRemovedMap.clear();
   3796 
   3797     return OK;
   3798 }
   3799 
   3800 status_t Camera3Device::RequestThread::addDummyTriggerIds(
   3801         const sp<CaptureRequest> &request) {
   3802     // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
   3803     static const int32_t dummyTriggerId = 1;
   3804     status_t res;
   3805 
   3806     CameraMetadata &metadata = request->mSettings;
   3807 
   3808     // If AF trigger is active, insert a dummy AF trigger ID if none already
   3809     // exists
   3810     camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
   3811     camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
   3812     if (afTrigger.count > 0 &&
   3813             afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
   3814             afId.count == 0) {
   3815         res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
   3816         if (res != OK) return res;
   3817     }
   3818 
   3819     // If AE precapture trigger is active, insert a dummy precapture trigger ID
   3820     // if none already exists
   3821     camera_metadata_entry pcTrigger =
   3822             metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
   3823     camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
   3824     if (pcTrigger.count > 0 &&
   3825             pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
   3826             pcId.count == 0) {
   3827         res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
   3828                 &dummyTriggerId, 1);
   3829         if (res != OK) return res;
   3830     }
   3831 
   3832     return OK;
   3833 }
   3834 
   3835 /**
   3836  * PreparerThread inner class methods
   3837  */
   3838 
   3839 Camera3Device::PreparerThread::PreparerThread() :
   3840         Thread(/*canCallJava*/false), mListener(nullptr),
   3841         mActive(false), mCancelNow(false) {
   3842 }
   3843 
   3844 Camera3Device::PreparerThread::~PreparerThread() {
   3845     Thread::requestExitAndWait();
   3846     if (mCurrentStream != nullptr) {
   3847         mCurrentStream->cancelPrepare();
   3848         ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
   3849         mCurrentStream.clear();
   3850     }
   3851     clear();
   3852 }
   3853 
   3854 status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
   3855     status_t res;
   3856 
   3857     Mutex::Autolock l(mLock);
   3858     sp<NotificationListener> listener = mListener.promote();
   3859 
   3860     res = stream->startPrepare(maxCount);
   3861     if (res == OK) {
   3862         // No preparation needed, fire listener right off
   3863         ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
   3864         if (listener != NULL) {
   3865             listener->notifyPrepared(stream->getId());
   3866         }
   3867         return OK;
   3868     } else if (res != NOT_ENOUGH_DATA) {
   3869         return res;
   3870     }
   3871 
   3872     // Need to prepare, start up thread if necessary
   3873     if (!mActive) {
   3874         // mRunning will change to false before the thread fully shuts down, so wait to be sure it
   3875         // isn't running
   3876         Thread::requestExitAndWait();
   3877         res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
   3878         if (res != OK) {
   3879             ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
   3880             if (listener != NULL) {
   3881                 listener->notifyPrepared(stream->getId());
   3882             }
   3883             return res;
   3884         }
   3885         mCancelNow = false;
   3886         mActive = true;
   3887         ALOGV("%s: Preparer stream started", __FUNCTION__);
   3888     }
   3889 
   3890     // queue up the work
   3891     mPendingStreams.push_back(stream);
   3892     ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
   3893 
   3894     return OK;
   3895 }
   3896 
   3897 status_t Camera3Device::PreparerThread::clear() {
   3898     Mutex::Autolock l(mLock);
   3899 
   3900     for (const auto& stream : mPendingStreams) {
   3901         stream->cancelPrepare();
   3902     }
   3903     mPendingStreams.clear();
   3904     mCancelNow = true;
   3905 
   3906     return OK;
   3907 }
   3908 
   3909 void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
   3910     Mutex::Autolock l(mLock);
   3911     mListener = listener;
   3912 }
   3913 
   3914 bool Camera3Device::PreparerThread::threadLoop() {
   3915     status_t res;
   3916     {
   3917         Mutex::Autolock l(mLock);
   3918         if (mCurrentStream == nullptr) {
   3919             // End thread if done with work
   3920             if (mPendingStreams.empty()) {
   3921                 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
   3922                 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
   3923                 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
   3924                 mActive = false;
   3925                 return false;
   3926             }
   3927 
   3928             // Get next stream to prepare
   3929             auto it = mPendingStreams.begin();
   3930             mCurrentStream = *it;
   3931             mPendingStreams.erase(it);
   3932             ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
   3933             ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
   3934         } else if (mCancelNow) {
   3935             mCurrentStream->cancelPrepare();
   3936             ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
   3937             ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
   3938             mCurrentStream.clear();
   3939             mCancelNow = false;
   3940             return true;
   3941         }
   3942     }
   3943 
   3944     res = mCurrentStream->prepareNextBuffer();
   3945     if (res == NOT_ENOUGH_DATA) return true;
   3946     if (res != OK) {
   3947         // Something bad happened; try to recover by cancelling prepare and
   3948         // signalling listener anyway
   3949         ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
   3950                 mCurrentStream->getId(), res, strerror(-res));
   3951         mCurrentStream->cancelPrepare();
   3952     }
   3953 
   3954     // This stream has finished, notify listener
   3955     Mutex::Autolock l(mLock);
   3956     sp<NotificationListener> listener = mListener.promote();
   3957     if (listener != NULL) {
   3958         ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
   3959                 mCurrentStream->getId());
   3960         listener->notifyPrepared(mCurrentStream->getId());
   3961     }
   3962 
   3963     ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
   3964     mCurrentStream.clear();
   3965 
   3966     return true;
   3967 }
   3968 
   3969 /**
   3970  * Static callback forwarding methods from HAL to instance
   3971  */
   3972 
   3973 void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
   3974         const camera3_capture_result *result) {
   3975     Camera3Device *d =
   3976             const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
   3977 
   3978     d->processCaptureResult(result);
   3979 }
   3980 
   3981 void Camera3Device::sNotify(const camera3_callback_ops *cb,
   3982         const camera3_notify_msg *msg) {
   3983     Camera3Device *d =
   3984             const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
   3985     d->notify(msg);
   3986 }
   3987 
   3988 }; // namespace android
   3989