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