Camera3Device.cpp revision 16a2ada049447c156648812b94d25be07869f284
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 "utils/CameraTraces.h"
47#include "device3/Camera3Device.h"
48#include "device3/Camera3OutputStream.h"
49#include "device3/Camera3InputStream.h"
50#include "device3/Camera3ZslStream.h"
51#include "device3/Camera3DummyStream.h"
52#include "CameraService.h"
53
54using namespace android::camera3;
55
56namespace android {
57
58Camera3Device::Camera3Device(int id):
59        mId(id),
60        mHal3Device(NULL),
61        mStatus(STATUS_UNINITIALIZED),
62        mUsePartialResult(false),
63        mNumPartialResults(1),
64        mNextResultFrameNumber(0),
65        mNextShutterFrameNumber(0),
66        mListener(NULL)
67{
68    ATRACE_CALL();
69    camera3_callback_ops::notify = &sNotify;
70    camera3_callback_ops::process_capture_result = &sProcessCaptureResult;
71    ALOGV("%s: Created device for camera %d", __FUNCTION__, id);
72}
73
74Camera3Device::~Camera3Device()
75{
76    ATRACE_CALL();
77    ALOGV("%s: Tearing down for camera id %d", __FUNCTION__, mId);
78    disconnect();
79}
80
81int Camera3Device::getId() const {
82    return mId;
83}
84
85/**
86 * CameraDeviceBase interface
87 */
88
89status_t Camera3Device::initialize(camera_module_t *module)
90{
91    ATRACE_CALL();
92    Mutex::Autolock il(mInterfaceLock);
93    Mutex::Autolock l(mLock);
94
95    ALOGV("%s: Initializing device for camera %d", __FUNCTION__, mId);
96    if (mStatus != STATUS_UNINITIALIZED) {
97        CLOGE("Already initialized!");
98        return INVALID_OPERATION;
99    }
100
101    /** Open HAL device */
102
103    status_t res;
104    String8 deviceName = String8::format("%d", mId);
105
106    camera3_device_t *device;
107
108    ATRACE_BEGIN("camera3->open");
109    res = CameraService::filterOpenErrorCode(module->common.methods->open(
110        &module->common, deviceName.string(),
111        reinterpret_cast<hw_device_t**>(&device)));
112    ATRACE_END();
113
114    if (res != OK) {
115        SET_ERR_L("Could not open camera: %s (%d)", strerror(-res), res);
116        return res;
117    }
118
119    /** Cross-check device version */
120    if (device->common.version < CAMERA_DEVICE_API_VERSION_3_0) {
121        SET_ERR_L("Could not open camera: "
122                "Camera device should be at least %x, reports %x instead",
123                CAMERA_DEVICE_API_VERSION_3_0,
124                device->common.version);
125        device->common.close(&device->common);
126        return BAD_VALUE;
127    }
128
129    camera_info info;
130    res = CameraService::filterGetInfoErrorCode(module->get_camera_info(
131        mId, &info));
132    if (res != OK) return res;
133
134    if (info.device_version != device->common.version) {
135        SET_ERR_L("HAL reporting mismatched camera_info version (%x)"
136                " and device version (%x).",
137                info.device_version, device->common.version);
138        device->common.close(&device->common);
139        return BAD_VALUE;
140    }
141
142    /** Initialize device with callback functions */
143
144    ATRACE_BEGIN("camera3->initialize");
145    res = device->ops->initialize(device, this);
146    ATRACE_END();
147
148    if (res != OK) {
149        SET_ERR_L("Unable to initialize HAL device: %s (%d)",
150                strerror(-res), res);
151        device->common.close(&device->common);
152        return BAD_VALUE;
153    }
154
155    /** Start up status tracker thread */
156    mStatusTracker = new StatusTracker(this);
157    res = mStatusTracker->run(String8::format("C3Dev-%d-Status", mId).string());
158    if (res != OK) {
159        SET_ERR_L("Unable to start status tracking thread: %s (%d)",
160                strerror(-res), res);
161        device->common.close(&device->common);
162        mStatusTracker.clear();
163        return res;
164    }
165
166    /** Start up request queue thread */
167
168    mRequestThread = new RequestThread(this, mStatusTracker, device);
169    res = mRequestThread->run(String8::format("C3Dev-%d-ReqQueue", mId).string());
170    if (res != OK) {
171        SET_ERR_L("Unable to start request queue thread: %s (%d)",
172                strerror(-res), res);
173        device->common.close(&device->common);
174        mRequestThread.clear();
175        return res;
176    }
177
178    /** Everything is good to go */
179
180    mDeviceVersion = device->common.version;
181    mDeviceInfo = info.static_camera_characteristics;
182    mHal3Device = device;
183    mStatus = STATUS_UNCONFIGURED;
184    mNextStreamId = 0;
185    mDummyStreamId = NO_STREAM;
186    mNeedConfig = true;
187    mPauseStateNotify = false;
188
189    // Will the HAL be sending in early partial result metadata?
190    if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
191        camera_metadata_entry partialResultsCount =
192                mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
193        if (partialResultsCount.count > 0) {
194            mNumPartialResults = partialResultsCount.data.i32[0];
195            mUsePartialResult = (mNumPartialResults > 1);
196        }
197    } else {
198        camera_metadata_entry partialResultsQuirk =
199                mDeviceInfo.find(ANDROID_QUIRKS_USE_PARTIAL_RESULT);
200        if (partialResultsQuirk.count > 0 && partialResultsQuirk.data.u8[0] == 1) {
201            mUsePartialResult = true;
202        }
203    }
204
205    return OK;
206}
207
208status_t Camera3Device::disconnect() {
209    ATRACE_CALL();
210    Mutex::Autolock il(mInterfaceLock);
211
212    ALOGV("%s: E", __FUNCTION__);
213
214    status_t res = OK;
215
216    {
217        Mutex::Autolock l(mLock);
218        if (mStatus == STATUS_UNINITIALIZED) return res;
219
220        if (mStatus == STATUS_ACTIVE ||
221                (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
222            res = mRequestThread->clearRepeatingRequests();
223            if (res != OK) {
224                SET_ERR_L("Can't stop streaming");
225                // Continue to close device even in case of error
226            } else {
227                res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
228                if (res != OK) {
229                    SET_ERR_L("Timeout waiting for HAL to drain");
230                    // Continue to close device even in case of error
231                }
232            }
233        }
234
235        if (mStatus == STATUS_ERROR) {
236            CLOGE("Shutting down in an error state");
237        }
238
239        if (mStatusTracker != NULL) {
240            mStatusTracker->requestExit();
241        }
242
243        if (mRequestThread != NULL) {
244            mRequestThread->requestExit();
245        }
246
247        mOutputStreams.clear();
248        mInputStream.clear();
249    }
250
251    // Joining done without holding mLock, otherwise deadlocks may ensue
252    // as the threads try to access parent state
253    if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
254        // HAL may be in a bad state, so waiting for request thread
255        // (which may be stuck in the HAL processCaptureRequest call)
256        // could be dangerous.
257        mRequestThread->join();
258    }
259
260    if (mStatusTracker != NULL) {
261        mStatusTracker->join();
262    }
263
264    {
265        Mutex::Autolock l(mLock);
266
267        mRequestThread.clear();
268        mStatusTracker.clear();
269
270        if (mHal3Device != NULL) {
271            ATRACE_BEGIN("camera3->close");
272            mHal3Device->common.close(&mHal3Device->common);
273            ATRACE_END();
274            mHal3Device = NULL;
275        }
276
277        mStatus = STATUS_UNINITIALIZED;
278    }
279
280    ALOGV("%s: X", __FUNCTION__);
281    return res;
282}
283
284// For dumping/debugging only -
285// try to acquire a lock a few times, eventually give up to proceed with
286// debug/dump operations
287bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
288    bool gotLock = false;
289    for (size_t i = 0; i < kDumpLockAttempts; ++i) {
290        if (lock.tryLock() == NO_ERROR) {
291            gotLock = true;
292            break;
293        } else {
294            usleep(kDumpSleepDuration);
295        }
296    }
297    return gotLock;
298}
299
300Camera3Device::Size Camera3Device::getMaxJpegResolution() const {
301    int32_t maxJpegWidth = 0, maxJpegHeight = 0;
302    if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
303        const int STREAM_CONFIGURATION_SIZE = 4;
304        const int STREAM_FORMAT_OFFSET = 0;
305        const int STREAM_WIDTH_OFFSET = 1;
306        const int STREAM_HEIGHT_OFFSET = 2;
307        const int STREAM_IS_INPUT_OFFSET = 3;
308        camera_metadata_ro_entry_t availableStreamConfigs =
309                mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
310        if (availableStreamConfigs.count == 0 ||
311                availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
312            return Size(0, 0);
313        }
314
315        // Get max jpeg size (area-wise).
316        for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
317            int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
318            int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
319            int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
320            int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
321            if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
322                    && format == HAL_PIXEL_FORMAT_BLOB &&
323                    (width * height > maxJpegWidth * maxJpegHeight)) {
324                maxJpegWidth = width;
325                maxJpegHeight = height;
326            }
327        }
328    } else {
329        camera_metadata_ro_entry availableJpegSizes =
330                mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
331        if (availableJpegSizes.count == 0 || availableJpegSizes.count % 2 != 0) {
332            return Size(0, 0);
333        }
334
335        // Get max jpeg size (area-wise).
336        for (size_t i = 0; i < availableJpegSizes.count; i += 2) {
337            if ((availableJpegSizes.data.i32[i] * availableJpegSizes.data.i32[i + 1])
338                    > (maxJpegWidth * maxJpegHeight)) {
339                maxJpegWidth = availableJpegSizes.data.i32[i];
340                maxJpegHeight = availableJpegSizes.data.i32[i + 1];
341            }
342        }
343    }
344    return Size(maxJpegWidth, maxJpegHeight);
345}
346
347ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
348    // Get max jpeg size (area-wise).
349    Size maxJpegResolution = getMaxJpegResolution();
350    if (maxJpegResolution.width == 0) {
351        ALOGE("%s: Camera %d: Can't find find valid available jpeg sizes in static metadata!",
352                __FUNCTION__, mId);
353        return BAD_VALUE;
354    }
355
356    // Get max jpeg buffer size
357    ssize_t maxJpegBufferSize = 0;
358    camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
359    if (jpegBufMaxSize.count == 0) {
360        ALOGE("%s: Camera %d: Can't find maximum JPEG size in static metadata!", __FUNCTION__, mId);
361        return BAD_VALUE;
362    }
363    maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
364
365    // Calculate final jpeg buffer size for the given resolution.
366    float scaleFactor = ((float) (width * height)) /
367            (maxJpegResolution.width * maxJpegResolution.height);
368    ssize_t jpegBufferSize = scaleFactor * maxJpegBufferSize;
369    // Bound the buffer size to [MIN_JPEG_BUFFER_SIZE, maxJpegBufferSize].
370    if (jpegBufferSize > maxJpegBufferSize) {
371        jpegBufferSize = maxJpegBufferSize;
372    } else if (jpegBufferSize < kMinJpegBufferSize) {
373        jpegBufferSize = kMinJpegBufferSize;
374    }
375
376    return jpegBufferSize;
377}
378
379status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
380    ATRACE_CALL();
381    (void)args;
382
383    // Try to lock, but continue in case of failure (to avoid blocking in
384    // deadlocks)
385    bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
386    bool gotLock = tryLockSpinRightRound(mLock);
387
388    ALOGW_IF(!gotInterfaceLock,
389            "Camera %d: %s: Unable to lock interface lock, proceeding anyway",
390            mId, __FUNCTION__);
391    ALOGW_IF(!gotLock,
392            "Camera %d: %s: Unable to lock main lock, proceeding anyway",
393            mId, __FUNCTION__);
394
395    String8 lines;
396
397    const char *status =
398            mStatus == STATUS_ERROR         ? "ERROR" :
399            mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
400            mStatus == STATUS_UNCONFIGURED  ? "UNCONFIGURED" :
401            mStatus == STATUS_CONFIGURED    ? "CONFIGURED" :
402            mStatus == STATUS_ACTIVE        ? "ACTIVE" :
403            "Unknown";
404
405    lines.appendFormat("    Device status: %s\n", status);
406    if (mStatus == STATUS_ERROR) {
407        lines.appendFormat("    Error cause: %s\n", mErrorCause.string());
408    }
409    lines.appendFormat("    Stream configuration:\n");
410
411    if (mInputStream != NULL) {
412        write(fd, lines.string(), lines.size());
413        mInputStream->dump(fd, args);
414    } else {
415        lines.appendFormat("      No input stream.\n");
416        write(fd, lines.string(), lines.size());
417    }
418    for (size_t i = 0; i < mOutputStreams.size(); i++) {
419        mOutputStreams[i]->dump(fd,args);
420    }
421
422    lines = String8("    In-flight requests:\n");
423    if (mInFlightMap.size() == 0) {
424        lines.append("      None\n");
425    } else {
426        for (size_t i = 0; i < mInFlightMap.size(); i++) {
427            InFlightRequest r = mInFlightMap.valueAt(i);
428            lines.appendFormat("      Frame %d |  Timestamp: %" PRId64 ", metadata"
429                    " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
430                    r.captureTimestamp, r.haveResultMetadata ? "true" : "false",
431                    r.numBuffersLeft);
432        }
433    }
434    write(fd, lines.string(), lines.size());
435
436    {
437        lines = String8("    Last request sent:\n");
438        write(fd, lines.string(), lines.size());
439
440        CameraMetadata lastRequest = getLatestRequestLocked();
441        lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
442    }
443
444    if (mHal3Device != NULL) {
445        lines = String8("    HAL device dump:\n");
446        write(fd, lines.string(), lines.size());
447        mHal3Device->ops->dump(mHal3Device, fd);
448    }
449
450    if (gotLock) mLock.unlock();
451    if (gotInterfaceLock) mInterfaceLock.unlock();
452
453    return OK;
454}
455
456const CameraMetadata& Camera3Device::info() const {
457    ALOGVV("%s: E", __FUNCTION__);
458    if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
459                    mStatus == STATUS_ERROR)) {
460        ALOGW("%s: Access to static info %s!", __FUNCTION__,
461                mStatus == STATUS_ERROR ?
462                "when in error state" : "before init");
463    }
464    return mDeviceInfo;
465}
466
467status_t Camera3Device::checkStatusOkToCaptureLocked() {
468    switch (mStatus) {
469        case STATUS_ERROR:
470            CLOGE("Device has encountered a serious error");
471            return INVALID_OPERATION;
472        case STATUS_UNINITIALIZED:
473            CLOGE("Device not initialized");
474            return INVALID_OPERATION;
475        case STATUS_UNCONFIGURED:
476        case STATUS_CONFIGURED:
477        case STATUS_ACTIVE:
478            // OK
479            break;
480        default:
481            SET_ERR_L("Unexpected status: %d", mStatus);
482            return INVALID_OPERATION;
483    }
484    return OK;
485}
486
487status_t Camera3Device::convertMetadataListToRequestListLocked(
488        const List<const CameraMetadata> &metadataList, RequestList *requestList) {
489    if (requestList == NULL) {
490        CLOGE("requestList cannot be NULL.");
491        return BAD_VALUE;
492    }
493
494    int32_t burstId = 0;
495    for (List<const CameraMetadata>::const_iterator it = metadataList.begin();
496            it != metadataList.end(); ++it) {
497        sp<CaptureRequest> newRequest = setUpRequestLocked(*it);
498        if (newRequest == 0) {
499            CLOGE("Can't create capture request");
500            return BAD_VALUE;
501        }
502
503        // Setup burst Id and request Id
504        newRequest->mResultExtras.burstId = burstId++;
505        if (it->exists(ANDROID_REQUEST_ID)) {
506            if (it->find(ANDROID_REQUEST_ID).count == 0) {
507                CLOGE("RequestID entry exists; but must not be empty in metadata");
508                return BAD_VALUE;
509            }
510            newRequest->mResultExtras.requestId = it->find(ANDROID_REQUEST_ID).data.i32[0];
511        } else {
512            CLOGE("RequestID does not exist in metadata");
513            return BAD_VALUE;
514        }
515
516        requestList->push_back(newRequest);
517
518        ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
519    }
520    return OK;
521}
522
523status_t Camera3Device::capture(CameraMetadata &request, int64_t* /*lastFrameNumber*/) {
524    ATRACE_CALL();
525
526    List<const CameraMetadata> requests;
527    requests.push_back(request);
528    return captureList(requests, /*lastFrameNumber*/NULL);
529}
530
531status_t Camera3Device::submitRequestsHelper(
532        const List<const CameraMetadata> &requests, bool repeating,
533        /*out*/
534        int64_t *lastFrameNumber) {
535    ATRACE_CALL();
536    Mutex::Autolock il(mInterfaceLock);
537    Mutex::Autolock l(mLock);
538
539    status_t res = checkStatusOkToCaptureLocked();
540    if (res != OK) {
541        // error logged by previous call
542        return res;
543    }
544
545    RequestList requestList;
546
547    res = convertMetadataListToRequestListLocked(requests, /*out*/&requestList);
548    if (res != OK) {
549        // error logged by previous call
550        return res;
551    }
552
553    if (repeating) {
554        res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
555    } else {
556        res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
557    }
558
559    if (res == OK) {
560        waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
561        if (res != OK) {
562            SET_ERR_L("Can't transition to active in %f seconds!",
563                    kActiveTimeout/1e9);
564        }
565        ALOGV("Camera %d: Capture request %" PRId32 " enqueued", mId,
566              (*(requestList.begin()))->mResultExtras.requestId);
567    } else {
568        CLOGE("Cannot queue request. Impossible.");
569        return BAD_VALUE;
570    }
571
572    return res;
573}
574
575status_t Camera3Device::captureList(const List<const CameraMetadata> &requests,
576                                    int64_t *lastFrameNumber) {
577    ATRACE_CALL();
578
579    return submitRequestsHelper(requests, /*repeating*/false, lastFrameNumber);
580}
581
582status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
583                                            int64_t* /*lastFrameNumber*/) {
584    ATRACE_CALL();
585
586    List<const CameraMetadata> requests;
587    requests.push_back(request);
588    return setStreamingRequestList(requests, /*lastFrameNumber*/NULL);
589}
590
591status_t Camera3Device::setStreamingRequestList(const List<const CameraMetadata> &requests,
592                                                int64_t *lastFrameNumber) {
593    ATRACE_CALL();
594
595    return submitRequestsHelper(requests, /*repeating*/true, lastFrameNumber);
596}
597
598sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
599        const CameraMetadata &request) {
600    status_t res;
601
602    if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
603        res = configureStreamsLocked();
604        if (res != OK) {
605            SET_ERR_L("Can't set up streams: %s (%d)", strerror(-res), res);
606            return NULL;
607        }
608        if (mStatus == STATUS_UNCONFIGURED) {
609            CLOGE("No streams configured");
610            return NULL;
611        }
612    }
613
614    sp<CaptureRequest> newRequest = createCaptureRequest(request);
615    return newRequest;
616}
617
618status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
619    ATRACE_CALL();
620    Mutex::Autolock il(mInterfaceLock);
621    Mutex::Autolock l(mLock);
622
623    switch (mStatus) {
624        case STATUS_ERROR:
625            CLOGE("Device has encountered a serious error");
626            return INVALID_OPERATION;
627        case STATUS_UNINITIALIZED:
628            CLOGE("Device not initialized");
629            return INVALID_OPERATION;
630        case STATUS_UNCONFIGURED:
631        case STATUS_CONFIGURED:
632        case STATUS_ACTIVE:
633            // OK
634            break;
635        default:
636            SET_ERR_L("Unexpected status: %d", mStatus);
637            return INVALID_OPERATION;
638    }
639    ALOGV("Camera %d: Clearing repeating request", mId);
640
641    return mRequestThread->clearRepeatingRequests(lastFrameNumber);
642}
643
644status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
645    ATRACE_CALL();
646    Mutex::Autolock il(mInterfaceLock);
647
648    return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
649}
650
651status_t Camera3Device::createInputStream(
652        uint32_t width, uint32_t height, int format, int *id) {
653    ATRACE_CALL();
654    Mutex::Autolock il(mInterfaceLock);
655    Mutex::Autolock l(mLock);
656    ALOGV("Camera %d: Creating new input stream %d: %d x %d, format %d",
657            mId, mNextStreamId, width, height, format);
658
659    status_t res;
660    bool wasActive = false;
661
662    switch (mStatus) {
663        case STATUS_ERROR:
664            ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
665            return INVALID_OPERATION;
666        case STATUS_UNINITIALIZED:
667            ALOGE("%s: Device not initialized", __FUNCTION__);
668            return INVALID_OPERATION;
669        case STATUS_UNCONFIGURED:
670        case STATUS_CONFIGURED:
671            // OK
672            break;
673        case STATUS_ACTIVE:
674            ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
675            res = internalPauseAndWaitLocked();
676            if (res != OK) {
677                SET_ERR_L("Can't pause captures to reconfigure streams!");
678                return res;
679            }
680            wasActive = true;
681            break;
682        default:
683            SET_ERR_L("%s: Unexpected status: %d", mStatus);
684            return INVALID_OPERATION;
685    }
686    assert(mStatus != STATUS_ACTIVE);
687
688    if (mInputStream != 0) {
689        ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
690        return INVALID_OPERATION;
691    }
692
693    sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
694                width, height, format);
695    newStream->setStatusTracker(mStatusTracker);
696
697    mInputStream = newStream;
698
699    *id = mNextStreamId++;
700
701    // Continue captures if active at start
702    if (wasActive) {
703        ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
704        res = configureStreamsLocked();
705        if (res != OK) {
706            ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
707                    __FUNCTION__, mNextStreamId, strerror(-res), res);
708            return res;
709        }
710        internalResumeLocked();
711    }
712
713    ALOGV("Camera %d: Created input stream", mId);
714    return OK;
715}
716
717
718status_t Camera3Device::createZslStream(
719            uint32_t width, uint32_t height,
720            int depth,
721            /*out*/
722            int *id,
723            sp<Camera3ZslStream>* zslStream) {
724    ATRACE_CALL();
725    Mutex::Autolock il(mInterfaceLock);
726    Mutex::Autolock l(mLock);
727    ALOGV("Camera %d: Creating ZSL stream %d: %d x %d, depth %d",
728            mId, mNextStreamId, width, height, depth);
729
730    status_t res;
731    bool wasActive = false;
732
733    switch (mStatus) {
734        case STATUS_ERROR:
735            ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
736            return INVALID_OPERATION;
737        case STATUS_UNINITIALIZED:
738            ALOGE("%s: Device not initialized", __FUNCTION__);
739            return INVALID_OPERATION;
740        case STATUS_UNCONFIGURED:
741        case STATUS_CONFIGURED:
742            // OK
743            break;
744        case STATUS_ACTIVE:
745            ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
746            res = internalPauseAndWaitLocked();
747            if (res != OK) {
748                SET_ERR_L("Can't pause captures to reconfigure streams!");
749                return res;
750            }
751            wasActive = true;
752            break;
753        default:
754            SET_ERR_L("Unexpected status: %d", mStatus);
755            return INVALID_OPERATION;
756    }
757    assert(mStatus != STATUS_ACTIVE);
758
759    if (mInputStream != 0) {
760        ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
761        return INVALID_OPERATION;
762    }
763
764    sp<Camera3ZslStream> newStream = new Camera3ZslStream(mNextStreamId,
765                width, height, depth);
766    newStream->setStatusTracker(mStatusTracker);
767
768    res = mOutputStreams.add(mNextStreamId, newStream);
769    if (res < 0) {
770        ALOGE("%s: Can't add new stream to set: %s (%d)",
771                __FUNCTION__, strerror(-res), res);
772        return res;
773    }
774    mInputStream = newStream;
775
776    mNeedConfig = true;
777
778    *id = mNextStreamId++;
779    *zslStream = newStream;
780
781    // Continue captures if active at start
782    if (wasActive) {
783        ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
784        res = configureStreamsLocked();
785        if (res != OK) {
786            ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
787                    __FUNCTION__, mNextStreamId, strerror(-res), res);
788            return res;
789        }
790        internalResumeLocked();
791    }
792
793    ALOGV("Camera %d: Created ZSL stream", mId);
794    return OK;
795}
796
797status_t Camera3Device::createStream(sp<ANativeWindow> consumer,
798        uint32_t width, uint32_t height, int format, int *id) {
799    ATRACE_CALL();
800    Mutex::Autolock il(mInterfaceLock);
801    Mutex::Autolock l(mLock);
802    ALOGV("Camera %d: Creating new stream %d: %d x %d, format %d",
803            mId, mNextStreamId, width, height, format);
804
805    status_t res;
806    bool wasActive = false;
807
808    switch (mStatus) {
809        case STATUS_ERROR:
810            CLOGE("Device has encountered a serious error");
811            return INVALID_OPERATION;
812        case STATUS_UNINITIALIZED:
813            CLOGE("Device not initialized");
814            return INVALID_OPERATION;
815        case STATUS_UNCONFIGURED:
816        case STATUS_CONFIGURED:
817            // OK
818            break;
819        case STATUS_ACTIVE:
820            ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
821            res = internalPauseAndWaitLocked();
822            if (res != OK) {
823                SET_ERR_L("Can't pause captures to reconfigure streams!");
824                return res;
825            }
826            wasActive = true;
827            break;
828        default:
829            SET_ERR_L("Unexpected status: %d", mStatus);
830            return INVALID_OPERATION;
831    }
832    assert(mStatus != STATUS_ACTIVE);
833
834    sp<Camera3OutputStream> newStream;
835    if (format == HAL_PIXEL_FORMAT_BLOB) {
836        ssize_t jpegBufferSize = getJpegBufferSize(width, height);
837        if (jpegBufferSize <= 0) {
838            SET_ERR_L("Invalid jpeg buffer size %zd", jpegBufferSize);
839            return BAD_VALUE;
840        }
841
842        newStream = new Camera3OutputStream(mNextStreamId, consumer,
843                width, height, jpegBufferSize, format);
844    } else {
845        newStream = new Camera3OutputStream(mNextStreamId, consumer,
846                width, height, format);
847    }
848    newStream->setStatusTracker(mStatusTracker);
849
850    res = mOutputStreams.add(mNextStreamId, newStream);
851    if (res < 0) {
852        SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
853        return res;
854    }
855
856    *id = mNextStreamId++;
857    mNeedConfig = true;
858
859    // Continue captures if active at start
860    if (wasActive) {
861        ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
862        res = configureStreamsLocked();
863        if (res != OK) {
864            CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
865                    mNextStreamId, strerror(-res), res);
866            return res;
867        }
868        internalResumeLocked();
869    }
870    ALOGV("Camera %d: Created new stream", mId);
871    return OK;
872}
873
874status_t Camera3Device::createReprocessStreamFromStream(int outputId, int *id) {
875    ATRACE_CALL();
876    (void)outputId; (void)id;
877
878    CLOGE("Unimplemented");
879    return INVALID_OPERATION;
880}
881
882
883status_t Camera3Device::getStreamInfo(int id,
884        uint32_t *width, uint32_t *height, uint32_t *format) {
885    ATRACE_CALL();
886    Mutex::Autolock il(mInterfaceLock);
887    Mutex::Autolock l(mLock);
888
889    switch (mStatus) {
890        case STATUS_ERROR:
891            CLOGE("Device has encountered a serious error");
892            return INVALID_OPERATION;
893        case STATUS_UNINITIALIZED:
894            CLOGE("Device not initialized!");
895            return INVALID_OPERATION;
896        case STATUS_UNCONFIGURED:
897        case STATUS_CONFIGURED:
898        case STATUS_ACTIVE:
899            // OK
900            break;
901        default:
902            SET_ERR_L("Unexpected status: %d", mStatus);
903            return INVALID_OPERATION;
904    }
905
906    ssize_t idx = mOutputStreams.indexOfKey(id);
907    if (idx == NAME_NOT_FOUND) {
908        CLOGE("Stream %d is unknown", id);
909        return idx;
910    }
911
912    if (width) *width  = mOutputStreams[idx]->getWidth();
913    if (height) *height = mOutputStreams[idx]->getHeight();
914    if (format) *format = mOutputStreams[idx]->getFormat();
915
916    return OK;
917}
918
919status_t Camera3Device::setStreamTransform(int id,
920        int transform) {
921    ATRACE_CALL();
922    Mutex::Autolock il(mInterfaceLock);
923    Mutex::Autolock l(mLock);
924
925    switch (mStatus) {
926        case STATUS_ERROR:
927            CLOGE("Device has encountered a serious error");
928            return INVALID_OPERATION;
929        case STATUS_UNINITIALIZED:
930            CLOGE("Device not initialized");
931            return INVALID_OPERATION;
932        case STATUS_UNCONFIGURED:
933        case STATUS_CONFIGURED:
934        case STATUS_ACTIVE:
935            // OK
936            break;
937        default:
938            SET_ERR_L("Unexpected status: %d", mStatus);
939            return INVALID_OPERATION;
940    }
941
942    ssize_t idx = mOutputStreams.indexOfKey(id);
943    if (idx == NAME_NOT_FOUND) {
944        CLOGE("Stream %d does not exist",
945                id);
946        return BAD_VALUE;
947    }
948
949    return mOutputStreams.editValueAt(idx)->setTransform(transform);
950}
951
952status_t Camera3Device::deleteStream(int id) {
953    ATRACE_CALL();
954    Mutex::Autolock il(mInterfaceLock);
955    Mutex::Autolock l(mLock);
956    status_t res;
957
958    ALOGV("%s: Camera %d: Deleting stream %d", __FUNCTION__, mId, id);
959
960    // CameraDevice semantics require device to already be idle before
961    // deleteStream is called, unlike for createStream.
962    if (mStatus == STATUS_ACTIVE) {
963        ALOGV("%s: Camera %d: Device not idle", __FUNCTION__, mId);
964        return -EBUSY;
965    }
966
967    sp<Camera3StreamInterface> deletedStream;
968    ssize_t outputStreamIdx = mOutputStreams.indexOfKey(id);
969    if (mInputStream != NULL && id == mInputStream->getId()) {
970        deletedStream = mInputStream;
971        mInputStream.clear();
972    } else {
973        if (outputStreamIdx == NAME_NOT_FOUND) {
974            CLOGE("Stream %d does not exist", id);
975            return BAD_VALUE;
976        }
977    }
978
979    // Delete output stream or the output part of a bi-directional stream.
980    if (outputStreamIdx != NAME_NOT_FOUND) {
981        deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
982        mOutputStreams.removeItem(id);
983    }
984
985    // Free up the stream endpoint so that it can be used by some other stream
986    res = deletedStream->disconnect();
987    if (res != OK) {
988        SET_ERR_L("Can't disconnect deleted stream %d", id);
989        // fall through since we want to still list the stream as deleted.
990    }
991    mDeletedStreams.add(deletedStream);
992    mNeedConfig = true;
993
994    return res;
995}
996
997status_t Camera3Device::deleteReprocessStream(int id) {
998    ATRACE_CALL();
999    (void)id;
1000
1001    CLOGE("Unimplemented");
1002    return INVALID_OPERATION;
1003}
1004
1005status_t Camera3Device::configureStreams() {
1006    ATRACE_CALL();
1007    ALOGV("%s: E", __FUNCTION__);
1008
1009    Mutex::Autolock il(mInterfaceLock);
1010    Mutex::Autolock l(mLock);
1011
1012    return configureStreamsLocked();
1013}
1014
1015status_t Camera3Device::createDefaultRequest(int templateId,
1016        CameraMetadata *request) {
1017    ATRACE_CALL();
1018    ALOGV("%s: for template %d", __FUNCTION__, templateId);
1019    Mutex::Autolock il(mInterfaceLock);
1020    Mutex::Autolock l(mLock);
1021
1022    switch (mStatus) {
1023        case STATUS_ERROR:
1024            CLOGE("Device has encountered a serious error");
1025            return INVALID_OPERATION;
1026        case STATUS_UNINITIALIZED:
1027            CLOGE("Device is not initialized!");
1028            return INVALID_OPERATION;
1029        case STATUS_UNCONFIGURED:
1030        case STATUS_CONFIGURED:
1031        case STATUS_ACTIVE:
1032            // OK
1033            break;
1034        default:
1035            SET_ERR_L("Unexpected status: %d", mStatus);
1036            return INVALID_OPERATION;
1037    }
1038
1039    const camera_metadata_t *rawRequest;
1040    ATRACE_BEGIN("camera3->construct_default_request_settings");
1041    rawRequest = mHal3Device->ops->construct_default_request_settings(
1042        mHal3Device, templateId);
1043    ATRACE_END();
1044    if (rawRequest == NULL) {
1045        SET_ERR_L("HAL is unable to construct default settings for template %d",
1046                templateId);
1047        return DEAD_OBJECT;
1048    }
1049    *request = rawRequest;
1050
1051    return OK;
1052}
1053
1054status_t Camera3Device::waitUntilDrained() {
1055    ATRACE_CALL();
1056    Mutex::Autolock il(mInterfaceLock);
1057    Mutex::Autolock l(mLock);
1058
1059    return waitUntilDrainedLocked();
1060}
1061
1062status_t Camera3Device::waitUntilDrainedLocked() {
1063    switch (mStatus) {
1064        case STATUS_UNINITIALIZED:
1065        case STATUS_UNCONFIGURED:
1066            ALOGV("%s: Already idle", __FUNCTION__);
1067            return OK;
1068        case STATUS_CONFIGURED:
1069            // To avoid race conditions, check with tracker to be sure
1070        case STATUS_ERROR:
1071        case STATUS_ACTIVE:
1072            // Need to verify shut down
1073            break;
1074        default:
1075            SET_ERR_L("Unexpected status: %d",mStatus);
1076            return INVALID_OPERATION;
1077    }
1078
1079    ALOGV("%s: Camera %d: Waiting until idle", __FUNCTION__, mId);
1080    status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1081    return res;
1082}
1083
1084// Pause to reconfigure
1085status_t Camera3Device::internalPauseAndWaitLocked() {
1086    mRequestThread->setPaused(true);
1087    mPauseStateNotify = true;
1088
1089    ALOGV("%s: Camera %d: Internal wait until idle", __FUNCTION__, mId);
1090    status_t res = waitUntilStateThenRelock(/*active*/ false, kShutdownTimeout);
1091    if (res != OK) {
1092        SET_ERR_L("Can't idle device in %f seconds!",
1093                kShutdownTimeout/1e9);
1094    }
1095
1096    return res;
1097}
1098
1099// Resume after internalPauseAndWaitLocked
1100status_t Camera3Device::internalResumeLocked() {
1101    status_t res;
1102
1103    mRequestThread->setPaused(false);
1104
1105    res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1106    if (res != OK) {
1107        SET_ERR_L("Can't transition to active in %f seconds!",
1108                kActiveTimeout/1e9);
1109    }
1110    mPauseStateNotify = false;
1111    return OK;
1112}
1113
1114status_t Camera3Device::waitUntilStateThenRelock(bool active,
1115        nsecs_t timeout) {
1116    status_t res = OK;
1117    if (active == (mStatus == STATUS_ACTIVE)) {
1118        // Desired state already reached
1119        return res;
1120    }
1121
1122    bool stateSeen = false;
1123    do {
1124        mRecentStatusUpdates.clear();
1125
1126        res = mStatusChanged.waitRelative(mLock, timeout);
1127        if (res != OK) break;
1128
1129        // Check state change history during wait
1130        for (size_t i = 0; i < mRecentStatusUpdates.size(); i++) {
1131            if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1132                stateSeen = true;
1133                break;
1134            }
1135        }
1136    } while (!stateSeen);
1137
1138    return res;
1139}
1140
1141
1142status_t Camera3Device::setNotifyCallback(NotificationListener *listener) {
1143    ATRACE_CALL();
1144    Mutex::Autolock l(mOutputLock);
1145
1146    if (listener != NULL && mListener != NULL) {
1147        ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1148    }
1149    mListener = listener;
1150    mRequestThread->setNotifyCallback(listener);
1151
1152    return OK;
1153}
1154
1155bool Camera3Device::willNotify3A() {
1156    return false;
1157}
1158
1159status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
1160    status_t res;
1161    Mutex::Autolock l(mOutputLock);
1162
1163    while (mResultQueue.empty()) {
1164        res = mResultSignal.waitRelative(mOutputLock, timeout);
1165        if (res == TIMED_OUT) {
1166            return res;
1167        } else if (res != OK) {
1168            ALOGW("%s: Camera %d: No frame in %" PRId64 " ns: %s (%d)",
1169                    __FUNCTION__, mId, timeout, strerror(-res), res);
1170            return res;
1171        }
1172    }
1173    return OK;
1174}
1175
1176status_t Camera3Device::getNextResult(CaptureResult *frame) {
1177    ATRACE_CALL();
1178    Mutex::Autolock l(mOutputLock);
1179
1180    if (mResultQueue.empty()) {
1181        return NOT_ENOUGH_DATA;
1182    }
1183
1184    if (frame == NULL) {
1185        ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1186        return BAD_VALUE;
1187    }
1188
1189    CaptureResult &result = *(mResultQueue.begin());
1190    frame->mResultExtras = result.mResultExtras;
1191    frame->mMetadata.acquire(result.mMetadata);
1192    mResultQueue.erase(mResultQueue.begin());
1193
1194    return OK;
1195}
1196
1197status_t Camera3Device::triggerAutofocus(uint32_t id) {
1198    ATRACE_CALL();
1199    Mutex::Autolock il(mInterfaceLock);
1200
1201    ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1202    // Mix-in this trigger into the next request and only the next request.
1203    RequestTrigger trigger[] = {
1204        {
1205            ANDROID_CONTROL_AF_TRIGGER,
1206            ANDROID_CONTROL_AF_TRIGGER_START
1207        },
1208        {
1209            ANDROID_CONTROL_AF_TRIGGER_ID,
1210            static_cast<int32_t>(id)
1211        }
1212    };
1213
1214    return mRequestThread->queueTrigger(trigger,
1215                                        sizeof(trigger)/sizeof(trigger[0]));
1216}
1217
1218status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1219    ATRACE_CALL();
1220    Mutex::Autolock il(mInterfaceLock);
1221
1222    ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1223    // Mix-in this trigger into the next request and only the next request.
1224    RequestTrigger trigger[] = {
1225        {
1226            ANDROID_CONTROL_AF_TRIGGER,
1227            ANDROID_CONTROL_AF_TRIGGER_CANCEL
1228        },
1229        {
1230            ANDROID_CONTROL_AF_TRIGGER_ID,
1231            static_cast<int32_t>(id)
1232        }
1233    };
1234
1235    return mRequestThread->queueTrigger(trigger,
1236                                        sizeof(trigger)/sizeof(trigger[0]));
1237}
1238
1239status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1240    ATRACE_CALL();
1241    Mutex::Autolock il(mInterfaceLock);
1242
1243    ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1244    // Mix-in this trigger into the next request and only the next request.
1245    RequestTrigger trigger[] = {
1246        {
1247            ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1248            ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1249        },
1250        {
1251            ANDROID_CONTROL_AE_PRECAPTURE_ID,
1252            static_cast<int32_t>(id)
1253        }
1254    };
1255
1256    return mRequestThread->queueTrigger(trigger,
1257                                        sizeof(trigger)/sizeof(trigger[0]));
1258}
1259
1260status_t Camera3Device::pushReprocessBuffer(int reprocessStreamId,
1261        buffer_handle_t *buffer, wp<BufferReleasedListener> listener) {
1262    ATRACE_CALL();
1263    (void)reprocessStreamId; (void)buffer; (void)listener;
1264
1265    CLOGE("Unimplemented");
1266    return INVALID_OPERATION;
1267}
1268
1269status_t Camera3Device::flush(int64_t *frameNumber) {
1270    ATRACE_CALL();
1271    ALOGV("%s: Camera %d: Flushing all requests", __FUNCTION__, mId);
1272    Mutex::Autolock il(mInterfaceLock);
1273
1274    NotificationListener* listener;
1275    {
1276        Mutex::Autolock l(mOutputLock);
1277        listener = mListener;
1278    }
1279
1280    {
1281        Mutex::Autolock l(mLock);
1282        mRequestThread->clear(listener, /*out*/frameNumber);
1283    }
1284
1285    status_t res;
1286    if (mHal3Device->common.version >= CAMERA_DEVICE_API_VERSION_3_1) {
1287        res = mHal3Device->ops->flush(mHal3Device);
1288    } else {
1289        Mutex::Autolock l(mLock);
1290        res = waitUntilDrainedLocked();
1291    }
1292
1293    return res;
1294}
1295
1296uint32_t Camera3Device::getDeviceVersion() {
1297    ATRACE_CALL();
1298    Mutex::Autolock il(mInterfaceLock);
1299    return mDeviceVersion;
1300}
1301
1302/**
1303 * Methods called by subclasses
1304 */
1305
1306void Camera3Device::notifyStatus(bool idle) {
1307    {
1308        // Need mLock to safely update state and synchronize to current
1309        // state of methods in flight.
1310        Mutex::Autolock l(mLock);
1311        // We can get various system-idle notices from the status tracker
1312        // while starting up. Only care about them if we've actually sent
1313        // in some requests recently.
1314        if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1315            return;
1316        }
1317        ALOGV("%s: Camera %d: Now %s", __FUNCTION__, mId,
1318                idle ? "idle" : "active");
1319        mStatus = idle ? STATUS_CONFIGURED : STATUS_ACTIVE;
1320        mRecentStatusUpdates.add(mStatus);
1321        mStatusChanged.signal();
1322
1323        // Skip notifying listener if we're doing some user-transparent
1324        // state changes
1325        if (mPauseStateNotify) return;
1326    }
1327    NotificationListener *listener;
1328    {
1329        Mutex::Autolock l(mOutputLock);
1330        listener = mListener;
1331    }
1332    if (idle && listener != NULL) {
1333        listener->notifyIdle();
1334    }
1335}
1336
1337/**
1338 * Camera3Device private methods
1339 */
1340
1341sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
1342        const CameraMetadata &request) {
1343    ATRACE_CALL();
1344    status_t res;
1345
1346    sp<CaptureRequest> newRequest = new CaptureRequest;
1347    newRequest->mSettings = request;
1348
1349    camera_metadata_entry_t inputStreams =
1350            newRequest->mSettings.find(ANDROID_REQUEST_INPUT_STREAMS);
1351    if (inputStreams.count > 0) {
1352        if (mInputStream == NULL ||
1353                mInputStream->getId() != inputStreams.data.i32[0]) {
1354            CLOGE("Request references unknown input stream %d",
1355                    inputStreams.data.u8[0]);
1356            return NULL;
1357        }
1358        // Lazy completion of stream configuration (allocation/registration)
1359        // on first use
1360        if (mInputStream->isConfiguring()) {
1361            res = mInputStream->finishConfiguration(mHal3Device);
1362            if (res != OK) {
1363                SET_ERR_L("Unable to finish configuring input stream %d:"
1364                        " %s (%d)",
1365                        mInputStream->getId(), strerror(-res), res);
1366                return NULL;
1367            }
1368        }
1369
1370        newRequest->mInputStream = mInputStream;
1371        newRequest->mSettings.erase(ANDROID_REQUEST_INPUT_STREAMS);
1372    }
1373
1374    camera_metadata_entry_t streams =
1375            newRequest->mSettings.find(ANDROID_REQUEST_OUTPUT_STREAMS);
1376    if (streams.count == 0) {
1377        CLOGE("Zero output streams specified!");
1378        return NULL;
1379    }
1380
1381    for (size_t i = 0; i < streams.count; i++) {
1382        int idx = mOutputStreams.indexOfKey(streams.data.i32[i]);
1383        if (idx == NAME_NOT_FOUND) {
1384            CLOGE("Request references unknown stream %d",
1385                    streams.data.u8[i]);
1386            return NULL;
1387        }
1388        sp<Camera3OutputStreamInterface> stream =
1389                mOutputStreams.editValueAt(idx);
1390
1391        // Lazy completion of stream configuration (allocation/registration)
1392        // on first use
1393        if (stream->isConfiguring()) {
1394            res = stream->finishConfiguration(mHal3Device);
1395            if (res != OK) {
1396                SET_ERR_L("Unable to finish configuring stream %d: %s (%d)",
1397                        stream->getId(), strerror(-res), res);
1398                return NULL;
1399            }
1400        }
1401
1402        newRequest->mOutputStreams.push(stream);
1403    }
1404    newRequest->mSettings.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
1405
1406    return newRequest;
1407}
1408
1409status_t Camera3Device::configureStreamsLocked() {
1410    ATRACE_CALL();
1411    status_t res;
1412
1413    if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
1414        CLOGE("Not idle");
1415        return INVALID_OPERATION;
1416    }
1417
1418    if (!mNeedConfig) {
1419        ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
1420        return OK;
1421    }
1422
1423    // Workaround for device HALv3.2 or older spec bug - zero streams requires
1424    // adding a dummy stream instead.
1425    // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
1426    if (mOutputStreams.size() == 0) {
1427        addDummyStreamLocked();
1428    } else {
1429        tryRemoveDummyStreamLocked();
1430    }
1431
1432    // Start configuring the streams
1433    ALOGV("%s: Camera %d: Starting stream configuration", __FUNCTION__, mId);
1434
1435    camera3_stream_configuration config;
1436
1437    config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
1438
1439    Vector<camera3_stream_t*> streams;
1440    streams.setCapacity(config.num_streams);
1441
1442    if (mInputStream != NULL) {
1443        camera3_stream_t *inputStream;
1444        inputStream = mInputStream->startConfiguration();
1445        if (inputStream == NULL) {
1446            SET_ERR_L("Can't start input stream configuration");
1447            return INVALID_OPERATION;
1448        }
1449        streams.add(inputStream);
1450    }
1451
1452    for (size_t i = 0; i < mOutputStreams.size(); i++) {
1453
1454        // Don't configure bidi streams twice, nor add them twice to the list
1455        if (mOutputStreams[i].get() ==
1456            static_cast<Camera3StreamInterface*>(mInputStream.get())) {
1457
1458            config.num_streams--;
1459            continue;
1460        }
1461
1462        camera3_stream_t *outputStream;
1463        outputStream = mOutputStreams.editValueAt(i)->startConfiguration();
1464        if (outputStream == NULL) {
1465            SET_ERR_L("Can't start output stream configuration");
1466            return INVALID_OPERATION;
1467        }
1468        streams.add(outputStream);
1469    }
1470
1471    config.streams = streams.editArray();
1472
1473    // Do the HAL configuration; will potentially touch stream
1474    // max_buffers, usage, priv fields.
1475    ATRACE_BEGIN("camera3->configure_streams");
1476    res = mHal3Device->ops->configure_streams(mHal3Device, &config);
1477    ATRACE_END();
1478
1479    if (res == BAD_VALUE) {
1480        // HAL rejected this set of streams as unsupported, clean up config
1481        // attempt and return to unconfigured state
1482        if (mInputStream != NULL && mInputStream->isConfiguring()) {
1483            res = mInputStream->cancelConfiguration();
1484            if (res != OK) {
1485                SET_ERR_L("Can't cancel configuring input stream %d: %s (%d)",
1486                        mInputStream->getId(), strerror(-res), res);
1487                return res;
1488            }
1489        }
1490
1491        for (size_t i = 0; i < mOutputStreams.size(); i++) {
1492            sp<Camera3OutputStreamInterface> outputStream =
1493                    mOutputStreams.editValueAt(i);
1494            if (outputStream->isConfiguring()) {
1495                res = outputStream->cancelConfiguration();
1496                if (res != OK) {
1497                    SET_ERR_L(
1498                        "Can't cancel configuring output stream %d: %s (%d)",
1499                        outputStream->getId(), strerror(-res), res);
1500                    return res;
1501                }
1502            }
1503        }
1504
1505        // Return state to that at start of call, so that future configures
1506        // properly clean things up
1507        mStatus = STATUS_UNCONFIGURED;
1508        mNeedConfig = true;
1509
1510        ALOGV("%s: Camera %d: Stream configuration failed", __FUNCTION__, mId);
1511        return BAD_VALUE;
1512    } else if (res != OK) {
1513        // Some other kind of error from configure_streams - this is not
1514        // expected
1515        SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
1516                strerror(-res), res);
1517        return res;
1518    }
1519
1520    // Finish all stream configuration immediately.
1521    // TODO: Try to relax this later back to lazy completion, which should be
1522    // faster
1523
1524    if (mInputStream != NULL && mInputStream->isConfiguring()) {
1525        res = mInputStream->finishConfiguration(mHal3Device);
1526        if (res != OK) {
1527            SET_ERR_L("Can't finish configuring input stream %d: %s (%d)",
1528                    mInputStream->getId(), strerror(-res), res);
1529            return res;
1530        }
1531    }
1532
1533    for (size_t i = 0; i < mOutputStreams.size(); i++) {
1534        sp<Camera3OutputStreamInterface> outputStream =
1535            mOutputStreams.editValueAt(i);
1536        if (outputStream->isConfiguring()) {
1537            res = outputStream->finishConfiguration(mHal3Device);
1538            if (res != OK) {
1539                SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
1540                        outputStream->getId(), strerror(-res), res);
1541                return res;
1542            }
1543        }
1544    }
1545
1546    // Request thread needs to know to avoid using repeat-last-settings protocol
1547    // across configure_streams() calls
1548    mRequestThread->configurationComplete();
1549
1550    // Update device state
1551
1552    mNeedConfig = false;
1553
1554    if (mDummyStreamId == NO_STREAM) {
1555        mStatus = STATUS_CONFIGURED;
1556    } else {
1557        mStatus = STATUS_UNCONFIGURED;
1558    }
1559
1560    ALOGV("%s: Camera %d: Stream configuration complete", __FUNCTION__, mId);
1561
1562    // tear down the deleted streams after configure streams.
1563    mDeletedStreams.clear();
1564
1565    return OK;
1566}
1567
1568status_t Camera3Device::addDummyStreamLocked() {
1569    ATRACE_CALL();
1570    status_t res;
1571
1572    if (mDummyStreamId != NO_STREAM) {
1573        // Should never be adding a second dummy stream when one is already
1574        // active
1575        SET_ERR_L("%s: Camera %d: A dummy stream already exists!",
1576                __FUNCTION__, mId);
1577        return INVALID_OPERATION;
1578    }
1579
1580    ALOGV("%s: Camera %d: Adding a dummy stream", __FUNCTION__, mId);
1581
1582    sp<Camera3OutputStreamInterface> dummyStream =
1583            new Camera3DummyStream(mNextStreamId);
1584
1585    res = mOutputStreams.add(mNextStreamId, dummyStream);
1586    if (res < 0) {
1587        SET_ERR_L("Can't add dummy stream to set: %s (%d)", strerror(-res), res);
1588        return res;
1589    }
1590
1591    mDummyStreamId = mNextStreamId;
1592    mNextStreamId++;
1593
1594    return OK;
1595}
1596
1597status_t Camera3Device::tryRemoveDummyStreamLocked() {
1598    ATRACE_CALL();
1599    status_t res;
1600
1601    if (mDummyStreamId == NO_STREAM) return OK;
1602    if (mOutputStreams.size() == 1) return OK;
1603
1604    ALOGV("%s: Camera %d: Removing the dummy stream", __FUNCTION__, mId);
1605
1606    // Ok, have a dummy stream and there's at least one other output stream,
1607    // so remove the dummy
1608
1609    sp<Camera3StreamInterface> deletedStream;
1610    ssize_t outputStreamIdx = mOutputStreams.indexOfKey(mDummyStreamId);
1611    if (outputStreamIdx == NAME_NOT_FOUND) {
1612        SET_ERR_L("Dummy stream %d does not appear to exist", mDummyStreamId);
1613        return INVALID_OPERATION;
1614    }
1615
1616    deletedStream = mOutputStreams.editValueAt(outputStreamIdx);
1617    mOutputStreams.removeItemsAt(outputStreamIdx);
1618
1619    // Free up the stream endpoint so that it can be used by some other stream
1620    res = deletedStream->disconnect();
1621    if (res != OK) {
1622        SET_ERR_L("Can't disconnect deleted dummy stream %d", mDummyStreamId);
1623        // fall through since we want to still list the stream as deleted.
1624    }
1625    mDeletedStreams.add(deletedStream);
1626    mDummyStreamId = NO_STREAM;
1627
1628    return res;
1629}
1630
1631void Camera3Device::setErrorState(const char *fmt, ...) {
1632    Mutex::Autolock l(mLock);
1633    va_list args;
1634    va_start(args, fmt);
1635
1636    setErrorStateLockedV(fmt, args);
1637
1638    va_end(args);
1639}
1640
1641void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
1642    Mutex::Autolock l(mLock);
1643    setErrorStateLockedV(fmt, args);
1644}
1645
1646void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
1647    va_list args;
1648    va_start(args, fmt);
1649
1650    setErrorStateLockedV(fmt, args);
1651
1652    va_end(args);
1653}
1654
1655void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
1656    // Print out all error messages to log
1657    String8 errorCause = String8::formatV(fmt, args);
1658    ALOGE("Camera %d: %s", mId, errorCause.string());
1659
1660    // But only do error state transition steps for the first error
1661    if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
1662
1663    mErrorCause = errorCause;
1664
1665    mRequestThread->setPaused(true);
1666    mStatus = STATUS_ERROR;
1667
1668    // Notify upstream about a device error
1669    if (mListener != NULL) {
1670        mListener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
1671                CaptureResultExtras());
1672    }
1673
1674    // Save stack trace. View by dumping it later.
1675    CameraTraces::saveTrace();
1676    // TODO: consider adding errorCause and client pid/procname
1677}
1678
1679/**
1680 * In-flight request management
1681 */
1682
1683status_t Camera3Device::registerInFlight(uint32_t frameNumber,
1684        int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput) {
1685    ATRACE_CALL();
1686    Mutex::Autolock l(mInFlightLock);
1687
1688    ssize_t res;
1689    res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput));
1690    if (res < 0) return res;
1691
1692    return OK;
1693}
1694
1695/**
1696 * Check if all 3A fields are ready, and send off a partial 3A-only result
1697 * to the output frame queue
1698 */
1699bool Camera3Device::processPartial3AResult(
1700        uint32_t frameNumber,
1701        const CameraMetadata& partial, const CaptureResultExtras& resultExtras) {
1702
1703    // Check if all 3A states are present
1704    // The full list of fields is
1705    //   android.control.afMode
1706    //   android.control.awbMode
1707    //   android.control.aeState
1708    //   android.control.awbState
1709    //   android.control.afState
1710    //   android.control.afTriggerID
1711    //   android.control.aePrecaptureID
1712    // TODO: Add android.control.aeMode
1713
1714    bool gotAllStates = true;
1715
1716    uint8_t afMode;
1717    uint8_t awbMode;
1718    uint8_t aeState;
1719    uint8_t afState;
1720    uint8_t awbState;
1721
1722    gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_MODE,
1723        &afMode, frameNumber);
1724
1725    gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_MODE,
1726        &awbMode, frameNumber);
1727
1728    gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AE_STATE,
1729        &aeState, frameNumber);
1730
1731    gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AF_STATE,
1732        &afState, frameNumber);
1733
1734    gotAllStates &= get3AResult(partial, ANDROID_CONTROL_AWB_STATE,
1735        &awbState, frameNumber);
1736
1737    if (!gotAllStates) return false;
1738
1739    ALOGVV("%s: Camera %d: Frame %d, Request ID %d: AF mode %d, AWB mode %d, "
1740        "AF state %d, AE state %d, AWB state %d, "
1741        "AF trigger %d, AE precapture trigger %d",
1742        __FUNCTION__, mId, frameNumber, resultExtras.requestId,
1743        afMode, awbMode,
1744        afState, aeState, awbState,
1745        resultExtras.afTriggerId, resultExtras.precaptureTriggerId);
1746
1747    // Got all states, so construct a minimal result to send
1748    // In addition to the above fields, this means adding in
1749    //   android.request.frameCount
1750    //   android.request.requestId
1751    //   android.quirks.partialResult (for HAL version below HAL3.2)
1752
1753    const size_t kMinimal3AResultEntries = 10;
1754
1755    Mutex::Autolock l(mOutputLock);
1756
1757    CaptureResult captureResult;
1758    captureResult.mResultExtras = resultExtras;
1759    captureResult.mMetadata = CameraMetadata(kMinimal3AResultEntries, /*dataCapacity*/ 0);
1760    // TODO: change this to sp<CaptureResult>. This will need other changes, including,
1761    // but not limited to CameraDeviceBase::getNextResult
1762    CaptureResult& min3AResult =
1763            *mResultQueue.insert(mResultQueue.end(), captureResult);
1764
1765    if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_FRAME_COUNT,
1766            // TODO: This is problematic casting. Need to fix CameraMetadata.
1767            reinterpret_cast<int32_t*>(&frameNumber), frameNumber)) {
1768        return false;
1769    }
1770
1771    int32_t requestId = resultExtras.requestId;
1772    if (!insert3AResult(min3AResult.mMetadata, ANDROID_REQUEST_ID,
1773            &requestId, frameNumber)) {
1774        return false;
1775    }
1776
1777    if (mDeviceVersion < CAMERA_DEVICE_API_VERSION_3_2) {
1778        static const uint8_t partialResult = ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL;
1779        if (!insert3AResult(min3AResult.mMetadata, ANDROID_QUIRKS_PARTIAL_RESULT,
1780                &partialResult, frameNumber)) {
1781            return false;
1782        }
1783    }
1784
1785    if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_MODE,
1786            &afMode, frameNumber)) {
1787        return false;
1788    }
1789
1790    if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_MODE,
1791            &awbMode, frameNumber)) {
1792        return false;
1793    }
1794
1795    if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_STATE,
1796            &aeState, frameNumber)) {
1797        return false;
1798    }
1799
1800    if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_STATE,
1801            &afState, frameNumber)) {
1802        return false;
1803    }
1804
1805    if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AWB_STATE,
1806            &awbState, frameNumber)) {
1807        return false;
1808    }
1809
1810    if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AF_TRIGGER_ID,
1811            &resultExtras.afTriggerId, frameNumber)) {
1812        return false;
1813    }
1814
1815    if (!insert3AResult(min3AResult.mMetadata, ANDROID_CONTROL_AE_PRECAPTURE_ID,
1816            &resultExtras.precaptureTriggerId, frameNumber)) {
1817        return false;
1818    }
1819
1820    // We only send the aggregated partial when all 3A related metadata are available
1821    // For both API1 and API2.
1822    // TODO: we probably should pass through all partials to API2 unconditionally.
1823    mResultSignal.signal();
1824
1825    return true;
1826}
1827
1828template<typename T>
1829bool Camera3Device::get3AResult(const CameraMetadata& result, int32_t tag,
1830        T* value, uint32_t frameNumber) {
1831    (void) frameNumber;
1832
1833    camera_metadata_ro_entry_t entry;
1834
1835    entry = result.find(tag);
1836    if (entry.count == 0) {
1837        ALOGVV("%s: Camera %d: Frame %d: No %s provided by HAL!", __FUNCTION__,
1838            mId, frameNumber, get_camera_metadata_tag_name(tag));
1839        return false;
1840    }
1841
1842    if (sizeof(T) == sizeof(uint8_t)) {
1843        *value = entry.data.u8[0];
1844    } else if (sizeof(T) == sizeof(int32_t)) {
1845        *value = entry.data.i32[0];
1846    } else {
1847        ALOGE("%s: Unexpected type", __FUNCTION__);
1848        return false;
1849    }
1850    return true;
1851}
1852
1853template<typename T>
1854bool Camera3Device::insert3AResult(CameraMetadata& result, int32_t tag,
1855        const T* value, uint32_t frameNumber) {
1856    if (result.update(tag, value, 1) != NO_ERROR) {
1857        mResultQueue.erase(--mResultQueue.end(), mResultQueue.end());
1858        SET_ERR("Frame %d: Failed to set %s in partial metadata",
1859                frameNumber, get_camera_metadata_tag_name(tag));
1860        return false;
1861    }
1862    return true;
1863}
1864
1865/**
1866 * Camera HAL device callback methods
1867 */
1868
1869void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
1870    ATRACE_CALL();
1871
1872    status_t res;
1873
1874    uint32_t frameNumber = result->frame_number;
1875    if (result->result == NULL && result->num_output_buffers == 0 &&
1876            result->input_buffer == NULL) {
1877        SET_ERR("No result data provided by HAL for frame %d",
1878                frameNumber);
1879        return;
1880    }
1881
1882    // For HAL3.2 or above, If HAL doesn't support partial, it must always set
1883    // partial_result to 1 when metadata is included in this result.
1884    if (!mUsePartialResult &&
1885            mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2 &&
1886            result->result != NULL &&
1887            result->partial_result != 1) {
1888        SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
1889                " if partial result is not supported",
1890                frameNumber, result->partial_result);
1891        return;
1892    }
1893
1894    bool isPartialResult = false;
1895    CameraMetadata collectedPartialResult;
1896    CaptureResultExtras resultExtras;
1897    bool hasInputBufferInRequest = false;
1898
1899    // Get capture timestamp and resultExtras from list of in-flight requests,
1900    // where it was added by the shutter notification for this frame.
1901    // Then update the in-flight status and remove the in-flight entry if
1902    // all result data has been received.
1903    nsecs_t timestamp = 0;
1904    {
1905        Mutex::Autolock l(mInFlightLock);
1906        ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
1907        if (idx == NAME_NOT_FOUND) {
1908            SET_ERR("Unknown frame number for capture result: %d",
1909                    frameNumber);
1910            return;
1911        }
1912        InFlightRequest &request = mInFlightMap.editValueAt(idx);
1913        ALOGVV("%s: got InFlightRequest requestId = %" PRId32 ", frameNumber = %" PRId64
1914                ", burstId = %" PRId32,
1915                __FUNCTION__, request.resultExtras.requestId, request.resultExtras.frameNumber,
1916                request.resultExtras.burstId);
1917        // Always update the partial count to the latest one. When framework aggregates adjacent
1918        // partial results into one, the latest partial count will be used.
1919        request.resultExtras.partialResultCount = result->partial_result;
1920
1921        // Check if this result carries only partial metadata
1922        if (mUsePartialResult && result->result != NULL) {
1923            if (mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
1924                if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
1925                    SET_ERR("Result is malformed for frame %d: partial_result %u must be  in"
1926                            " the range of [1, %d] when metadata is included in the result",
1927                            frameNumber, result->partial_result, mNumPartialResults);
1928                    return;
1929                }
1930                isPartialResult = (result->partial_result < mNumPartialResults);
1931                if (isPartialResult) {
1932                    request.partialResult.collectedResult.append(result->result);
1933                }
1934            } else {
1935                camera_metadata_ro_entry_t partialResultEntry;
1936                res = find_camera_metadata_ro_entry(result->result,
1937                        ANDROID_QUIRKS_PARTIAL_RESULT, &partialResultEntry);
1938                if (res != NAME_NOT_FOUND &&
1939                        partialResultEntry.count > 0 &&
1940                        partialResultEntry.data.u8[0] ==
1941                        ANDROID_QUIRKS_PARTIAL_RESULT_PARTIAL) {
1942                    // A partial result. Flag this as such, and collect this
1943                    // set of metadata into the in-flight entry.
1944                    isPartialResult = true;
1945                    request.partialResult.collectedResult.append(
1946                        result->result);
1947                    request.partialResult.collectedResult.erase(
1948                        ANDROID_QUIRKS_PARTIAL_RESULT);
1949                }
1950            }
1951
1952            if (isPartialResult) {
1953                // Fire off a 3A-only result if possible
1954                if (!request.partialResult.haveSent3A) {
1955                    request.partialResult.haveSent3A =
1956                            processPartial3AResult(frameNumber,
1957                                    request.partialResult.collectedResult,
1958                                    request.resultExtras);
1959                }
1960            }
1961        }
1962
1963        timestamp = request.captureTimestamp;
1964        resultExtras = request.resultExtras;
1965        hasInputBufferInRequest = request.hasInputBuffer;
1966
1967        /**
1968         * One of the following must happen before it's legal to call process_capture_result,
1969         * unless partial metadata is being provided:
1970         * - CAMERA3_MSG_SHUTTER (expected during normal operation)
1971         * - CAMERA3_MSG_ERROR (expected during flush)
1972         */
1973        if (request.requestStatus == OK && timestamp == 0 && !isPartialResult) {
1974            SET_ERR("Called before shutter notify for frame %d",
1975                    frameNumber);
1976            return;
1977        }
1978
1979        // Did we get the (final) result metadata for this capture?
1980        if (result->result != NULL && !isPartialResult) {
1981            if (request.haveResultMetadata) {
1982                SET_ERR("Called multiple times with metadata for frame %d",
1983                        frameNumber);
1984                return;
1985            }
1986            if (mUsePartialResult &&
1987                    !request.partialResult.collectedResult.isEmpty()) {
1988                collectedPartialResult.acquire(
1989                    request.partialResult.collectedResult);
1990            }
1991            request.haveResultMetadata = true;
1992        }
1993
1994        uint32_t numBuffersReturned = result->num_output_buffers;
1995        if (result->input_buffer != NULL) {
1996            if (hasInputBufferInRequest) {
1997                numBuffersReturned += 1;
1998            } else {
1999                ALOGW("%s: Input buffer should be NULL if there is no input"
2000                        " buffer sent in the request",
2001                        __FUNCTION__);
2002            }
2003        }
2004        request.numBuffersLeft -= numBuffersReturned;
2005        if (request.numBuffersLeft < 0) {
2006            SET_ERR("Too many buffers returned for frame %d",
2007                    frameNumber);
2008            return;
2009        }
2010
2011        // Check if everything has arrived for this result (buffers and metadata), remove it from
2012        // InFlightMap if both arrived or HAL reports error for this request (i.e. during flush).
2013        if ((request.requestStatus != OK) ||
2014                (request.haveResultMetadata && request.numBuffersLeft == 0)) {
2015            ATRACE_ASYNC_END("frame capture", frameNumber);
2016            mInFlightMap.removeItemsAt(idx, 1);
2017        }
2018
2019        // Sanity check - if we have too many in-flight frames, something has
2020        // likely gone wrong
2021        if (mInFlightMap.size() > kInFlightWarnLimit) {
2022            CLOGE("In-flight list too large: %zu", mInFlightMap.size());
2023        }
2024
2025    }
2026
2027    // Process the result metadata, if provided
2028    bool gotResult = false;
2029    if (result->result != NULL && !isPartialResult) {
2030        Mutex::Autolock l(mOutputLock);
2031
2032        gotResult = true;
2033
2034        // TODO: need to track errors for tighter bounds on expected frame number
2035        if (frameNumber < mNextResultFrameNumber) {
2036            SET_ERR("Out-of-order capture result metadata submitted! "
2037                    "(got frame number %d, expecting %d)",
2038                    frameNumber, mNextResultFrameNumber);
2039            return;
2040        }
2041        mNextResultFrameNumber = frameNumber + 1;
2042
2043        CaptureResult captureResult;
2044        captureResult.mResultExtras = resultExtras;
2045        captureResult.mMetadata = result->result;
2046
2047        if (captureResult.mMetadata.update(ANDROID_REQUEST_FRAME_COUNT,
2048                (int32_t*)&frameNumber, 1) != OK) {
2049            SET_ERR("Failed to set frame# in metadata (%d)",
2050                    frameNumber);
2051            gotResult = false;
2052        } else {
2053            ALOGVV("%s: Camera %d: Set frame# in metadata (%d)",
2054                    __FUNCTION__, mId, frameNumber);
2055        }
2056
2057        // Append any previous partials to form a complete result
2058        if (mUsePartialResult && !collectedPartialResult.isEmpty()) {
2059            captureResult.mMetadata.append(collectedPartialResult);
2060        }
2061
2062        captureResult.mMetadata.sort();
2063
2064        // Check that there's a timestamp in the result metadata
2065
2066        camera_metadata_entry entry =
2067                captureResult.mMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2068        if (entry.count == 0) {
2069            SET_ERR("No timestamp provided by HAL for frame %d!",
2070                    frameNumber);
2071            gotResult = false;
2072        } else if (timestamp != entry.data.i64[0]) {
2073            SET_ERR("Timestamp mismatch between shutter notify and result"
2074                    " metadata for frame %d (%" PRId64 " vs %" PRId64 " respectively)",
2075                    frameNumber, timestamp, entry.data.i64[0]);
2076            gotResult = false;
2077        }
2078
2079        if (gotResult) {
2080            // Valid result, insert into queue
2081            List<CaptureResult>::iterator queuedResult =
2082                    mResultQueue.insert(mResultQueue.end(), CaptureResult(captureResult));
2083            ALOGVV("%s: result requestId = %" PRId32 ", frameNumber = %" PRId64
2084                   ", burstId = %" PRId32, __FUNCTION__,
2085                   queuedResult->mResultExtras.requestId,
2086                   queuedResult->mResultExtras.frameNumber,
2087                   queuedResult->mResultExtras.burstId);
2088        }
2089    } // scope for mOutputLock
2090
2091    // Return completed buffers to their streams with the timestamp
2092
2093    for (size_t i = 0; i < result->num_output_buffers; i++) {
2094        Camera3Stream *stream =
2095                Camera3Stream::cast(result->output_buffers[i].stream);
2096        res = stream->returnBuffer(result->output_buffers[i], timestamp);
2097        // Note: stream may be deallocated at this point, if this buffer was the
2098        // last reference to it.
2099        if (res != OK) {
2100            ALOGE("Can't return buffer %zu for frame %d to its stream: "
2101                    " %s (%d)", i, frameNumber, strerror(-res), res);
2102        }
2103    }
2104
2105    if (result->input_buffer != NULL) {
2106        if (hasInputBufferInRequest) {
2107            Camera3Stream *stream =
2108                Camera3Stream::cast(result->input_buffer->stream);
2109            res = stream->returnInputBuffer(*(result->input_buffer));
2110            // Note: stream may be deallocated at this point, if this buffer was the
2111            // last reference to it.
2112            if (res != OK) {
2113                ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
2114                      "  its stream:%s (%d)",  __FUNCTION__,
2115                      frameNumber, strerror(-res), res);
2116            }
2117        } else {
2118            ALOGW("%s: Input buffer should be NULL if there is no input"
2119                    " buffer sent in the request, skipping input buffer return.",
2120                    __FUNCTION__);
2121        }
2122    }
2123
2124    // Finally, signal any waiters for new frames
2125
2126    if (gotResult) {
2127        mResultSignal.signal();
2128    }
2129
2130}
2131
2132void Camera3Device::notify(const camera3_notify_msg *msg) {
2133    ATRACE_CALL();
2134    NotificationListener *listener;
2135    {
2136        Mutex::Autolock l(mOutputLock);
2137        listener = mListener;
2138    }
2139
2140    if (msg == NULL) {
2141        SET_ERR("HAL sent NULL notify message!");
2142        return;
2143    }
2144
2145    switch (msg->type) {
2146        case CAMERA3_MSG_ERROR: {
2147            notifyError(msg->message.error, listener);
2148            break;
2149        }
2150        case CAMERA3_MSG_SHUTTER: {
2151            notifyShutter(msg->message.shutter, listener);
2152            break;
2153        }
2154        default:
2155            SET_ERR("Unknown notify message from HAL: %d",
2156                    msg->type);
2157    }
2158}
2159
2160void Camera3Device::notifyError(const camera3_error_msg_t &msg,
2161        NotificationListener *listener) {
2162
2163    // Map camera HAL error codes to ICameraDeviceCallback error codes
2164    // Index into this with the HAL error code
2165    static const ICameraDeviceCallbacks::CameraErrorCode
2166            halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
2167        // 0 = Unused error code
2168        ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
2169        // 1 = CAMERA3_MSG_ERROR_DEVICE
2170        ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2171        // 2 = CAMERA3_MSG_ERROR_REQUEST
2172        ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2173        // 3 = CAMERA3_MSG_ERROR_RESULT
2174        ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
2175        // 4 = CAMERA3_MSG_ERROR_BUFFER
2176        ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
2177    };
2178
2179    ICameraDeviceCallbacks::CameraErrorCode errorCode =
2180            ((msg.error_code >= 0) &&
2181                    (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
2182            halErrorMap[msg.error_code] :
2183            ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
2184
2185    int streamId = 0;
2186    if (msg.error_stream != NULL) {
2187        Camera3Stream *stream =
2188                Camera3Stream::cast(msg.error_stream);
2189        streamId = stream->getId();
2190    }
2191    ALOGV("Camera %d: %s: HAL error, frame %d, stream %d: %d",
2192            mId, __FUNCTION__, msg.frame_number,
2193            streamId, msg.error_code);
2194
2195    CaptureResultExtras resultExtras;
2196    switch (errorCode) {
2197        case ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
2198            // SET_ERR calls notifyError
2199            SET_ERR("Camera HAL reported serious device error");
2200            break;
2201        case ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
2202        case ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
2203        case ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
2204            {
2205                Mutex::Autolock l(mInFlightLock);
2206                ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
2207                if (idx >= 0) {
2208                    InFlightRequest &r = mInFlightMap.editValueAt(idx);
2209                    r.requestStatus = msg.error_code;
2210                    resultExtras = r.resultExtras;
2211                } else {
2212                    resultExtras.frameNumber = msg.frame_number;
2213                    ALOGE("Camera %d: %s: cannot find in-flight request on "
2214                            "frame %" PRId64 " error", mId, __FUNCTION__,
2215                            resultExtras.frameNumber);
2216                }
2217            }
2218            if (listener != NULL) {
2219                listener->notifyError(errorCode, resultExtras);
2220            } else {
2221                ALOGE("Camera %d: %s: no listener available", mId, __FUNCTION__);
2222            }
2223            break;
2224        default:
2225            // SET_ERR calls notifyError
2226            SET_ERR("Unknown error message from HAL: %d", msg.error_code);
2227            break;
2228    }
2229}
2230
2231void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
2232        NotificationListener *listener) {
2233    ssize_t idx;
2234    // Verify ordering of shutter notifications
2235    {
2236        Mutex::Autolock l(mOutputLock);
2237        // TODO: need to track errors for tighter bounds on expected frame number.
2238        if (msg.frame_number < mNextShutterFrameNumber) {
2239            SET_ERR("Shutter notification out-of-order. Expected "
2240                    "notification for frame %d, got frame %d",
2241                    mNextShutterFrameNumber, msg.frame_number);
2242            return;
2243        }
2244        mNextShutterFrameNumber = msg.frame_number + 1;
2245    }
2246
2247    CaptureResultExtras resultExtras;
2248
2249    // Set timestamp for the request in the in-flight tracking
2250    // and get the request ID to send upstream
2251    {
2252        Mutex::Autolock l(mInFlightLock);
2253        idx = mInFlightMap.indexOfKey(msg.frame_number);
2254        if (idx >= 0) {
2255            InFlightRequest &r = mInFlightMap.editValueAt(idx);
2256            r.captureTimestamp = msg.timestamp;
2257            resultExtras = r.resultExtras;
2258        }
2259    }
2260    if (idx < 0) {
2261        SET_ERR("Shutter notification for non-existent frame number %d",
2262                msg.frame_number);
2263        return;
2264    }
2265    ALOGVV("Camera %d: %s: Shutter fired for frame %d (id %d) at %" PRId64,
2266            mId, __FUNCTION__,
2267            msg.frame_number, resultExtras.requestId, msg.timestamp);
2268    // Call listener, if any
2269    if (listener != NULL) {
2270        listener->notifyShutter(resultExtras, msg.timestamp);
2271    }
2272}
2273
2274
2275CameraMetadata Camera3Device::getLatestRequestLocked() {
2276    ALOGV("%s", __FUNCTION__);
2277
2278    CameraMetadata retVal;
2279
2280    if (mRequestThread != NULL) {
2281        retVal = mRequestThread->getLatestRequest();
2282    }
2283
2284    return retVal;
2285}
2286
2287
2288/**
2289 * RequestThread inner class methods
2290 */
2291
2292Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
2293        sp<StatusTracker> statusTracker,
2294        camera3_device_t *hal3Device) :
2295        Thread(false),
2296        mParent(parent),
2297        mStatusTracker(statusTracker),
2298        mHal3Device(hal3Device),
2299        mId(getId(parent)),
2300        mReconfigured(false),
2301        mDoPause(false),
2302        mPaused(true),
2303        mFrameNumber(0),
2304        mLatestRequestId(NAME_NOT_FOUND),
2305        mCurrentAfTriggerId(0),
2306        mCurrentPreCaptureTriggerId(0),
2307        mRepeatingLastFrameNumber(NO_IN_FLIGHT_REPEATING_FRAMES) {
2308    mStatusId = statusTracker->addComponent();
2309}
2310
2311void Camera3Device::RequestThread::setNotifyCallback(
2312        NotificationListener *listener) {
2313    Mutex::Autolock l(mRequestLock);
2314    mListener = listener;
2315}
2316
2317void Camera3Device::RequestThread::configurationComplete() {
2318    Mutex::Autolock l(mRequestLock);
2319    mReconfigured = true;
2320}
2321
2322status_t Camera3Device::RequestThread::queueRequestList(
2323        List<sp<CaptureRequest> > &requests,
2324        /*out*/
2325        int64_t *lastFrameNumber) {
2326    Mutex::Autolock l(mRequestLock);
2327    for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
2328            ++it) {
2329        mRequestQueue.push_back(*it);
2330    }
2331
2332    if (lastFrameNumber != NULL) {
2333        *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
2334        ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
2335              __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
2336              *lastFrameNumber);
2337    }
2338
2339    unpauseForNewRequests();
2340
2341    return OK;
2342}
2343
2344
2345status_t Camera3Device::RequestThread::queueTrigger(
2346        RequestTrigger trigger[],
2347        size_t count) {
2348
2349    Mutex::Autolock l(mTriggerMutex);
2350    status_t ret;
2351
2352    for (size_t i = 0; i < count; ++i) {
2353        ret = queueTriggerLocked(trigger[i]);
2354
2355        if (ret != OK) {
2356            return ret;
2357        }
2358    }
2359
2360    return OK;
2361}
2362
2363int Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
2364    sp<Camera3Device> d = device.promote();
2365    if (d != NULL) return d->mId;
2366    return 0;
2367}
2368
2369status_t Camera3Device::RequestThread::queueTriggerLocked(
2370        RequestTrigger trigger) {
2371
2372    uint32_t tag = trigger.metadataTag;
2373    ssize_t index = mTriggerMap.indexOfKey(tag);
2374
2375    switch (trigger.getTagType()) {
2376        case TYPE_BYTE:
2377        // fall-through
2378        case TYPE_INT32:
2379            break;
2380        default:
2381            ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
2382                    trigger.getTagType());
2383            return INVALID_OPERATION;
2384    }
2385
2386    /**
2387     * Collect only the latest trigger, since we only have 1 field
2388     * in the request settings per trigger tag, and can't send more than 1
2389     * trigger per request.
2390     */
2391    if (index != NAME_NOT_FOUND) {
2392        mTriggerMap.editValueAt(index) = trigger;
2393    } else {
2394        mTriggerMap.add(tag, trigger);
2395    }
2396
2397    return OK;
2398}
2399
2400status_t Camera3Device::RequestThread::setRepeatingRequests(
2401        const RequestList &requests,
2402        /*out*/
2403        int64_t *lastFrameNumber) {
2404    Mutex::Autolock l(mRequestLock);
2405    if (lastFrameNumber != NULL) {
2406        *lastFrameNumber = mRepeatingLastFrameNumber;
2407    }
2408    mRepeatingRequests.clear();
2409    mRepeatingRequests.insert(mRepeatingRequests.begin(),
2410            requests.begin(), requests.end());
2411
2412    unpauseForNewRequests();
2413
2414    mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
2415    return OK;
2416}
2417
2418bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest> requestIn) {
2419    if (mRepeatingRequests.empty()) {
2420        return false;
2421    }
2422    int32_t requestId = requestIn->mResultExtras.requestId;
2423    const RequestList &repeatRequests = mRepeatingRequests;
2424    // All repeating requests are guaranteed to have same id so only check first quest
2425    const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
2426    return (firstRequest->mResultExtras.requestId == requestId);
2427}
2428
2429status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
2430    Mutex::Autolock l(mRequestLock);
2431    mRepeatingRequests.clear();
2432    if (lastFrameNumber != NULL) {
2433        *lastFrameNumber = mRepeatingLastFrameNumber;
2434    }
2435    mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
2436    return OK;
2437}
2438
2439status_t Camera3Device::RequestThread::clear(
2440        NotificationListener *listener,
2441        /*out*/int64_t *lastFrameNumber) {
2442    Mutex::Autolock l(mRequestLock);
2443    ALOGV("RequestThread::%s:", __FUNCTION__);
2444
2445    mRepeatingRequests.clear();
2446
2447    // Send errors for all requests pending in the request queue, including
2448    // pending repeating requests
2449    if (listener != NULL) {
2450        for (RequestList::iterator it = mRequestQueue.begin();
2451                 it != mRequestQueue.end(); ++it) {
2452            // Set the frame number this request would have had, if it
2453            // had been submitted; this frame number will not be reused.
2454            // The requestId and burstId fields were set when the request was
2455            // submitted originally (in convertMetadataListToRequestListLocked)
2456            (*it)->mResultExtras.frameNumber = mFrameNumber++;
2457            listener->notifyError(ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2458                    (*it)->mResultExtras);
2459        }
2460    }
2461    mRequestQueue.clear();
2462    mTriggerMap.clear();
2463    if (lastFrameNumber != NULL) {
2464        *lastFrameNumber = mRepeatingLastFrameNumber;
2465    }
2466    mRepeatingLastFrameNumber = NO_IN_FLIGHT_REPEATING_FRAMES;
2467    return OK;
2468}
2469
2470void Camera3Device::RequestThread::setPaused(bool paused) {
2471    Mutex::Autolock l(mPauseLock);
2472    mDoPause = paused;
2473    mDoPauseSignal.signal();
2474}
2475
2476status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
2477        int32_t requestId, nsecs_t timeout) {
2478    Mutex::Autolock l(mLatestRequestMutex);
2479    status_t res;
2480    while (mLatestRequestId != requestId) {
2481        nsecs_t startTime = systemTime();
2482
2483        res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
2484        if (res != OK) return res;
2485
2486        timeout -= (systemTime() - startTime);
2487    }
2488
2489    return OK;
2490}
2491
2492void Camera3Device::RequestThread::requestExit() {
2493    // Call parent to set up shutdown
2494    Thread::requestExit();
2495    // The exit from any possible waits
2496    mDoPauseSignal.signal();
2497    mRequestSignal.signal();
2498}
2499
2500bool Camera3Device::RequestThread::threadLoop() {
2501
2502    status_t res;
2503
2504    // Handle paused state.
2505    if (waitIfPaused()) {
2506        return true;
2507    }
2508
2509    // Get work to do
2510
2511    sp<CaptureRequest> nextRequest = waitForNextRequest();
2512    if (nextRequest == NULL) {
2513        return true;
2514    }
2515
2516    // Create request to HAL
2517    camera3_capture_request_t request = camera3_capture_request_t();
2518    request.frame_number = nextRequest->mResultExtras.frameNumber;
2519    Vector<camera3_stream_buffer_t> outputBuffers;
2520
2521    // Get the request ID, if any
2522    int requestId;
2523    camera_metadata_entry_t requestIdEntry =
2524            nextRequest->mSettings.find(ANDROID_REQUEST_ID);
2525    if (requestIdEntry.count > 0) {
2526        requestId = requestIdEntry.data.i32[0];
2527    } else {
2528        ALOGW("%s: Did not have android.request.id set in the request",
2529                __FUNCTION__);
2530        requestId = NAME_NOT_FOUND;
2531    }
2532
2533    // Insert any queued triggers (before metadata is locked)
2534    int32_t triggerCount;
2535    res = insertTriggers(nextRequest);
2536    if (res < 0) {
2537        SET_ERR("RequestThread: Unable to insert triggers "
2538                "(capture request %d, HAL device: %s (%d)",
2539                request.frame_number, strerror(-res), res);
2540        cleanUpFailedRequest(request, nextRequest, outputBuffers);
2541        return false;
2542    }
2543    triggerCount = res;
2544
2545    bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
2546
2547    // If the request is the same as last, or we had triggers last time
2548    if (mPrevRequest != nextRequest || triggersMixedIn) {
2549        /**
2550         * HAL workaround:
2551         * Insert a dummy trigger ID if a trigger is set but no trigger ID is
2552         */
2553        res = addDummyTriggerIds(nextRequest);
2554        if (res != OK) {
2555            SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
2556                    "(capture request %d, HAL device: %s (%d)",
2557                    request.frame_number, strerror(-res), res);
2558            cleanUpFailedRequest(request, nextRequest, outputBuffers);
2559            return false;
2560        }
2561
2562        /**
2563         * The request should be presorted so accesses in HAL
2564         *   are O(logn). Sidenote, sorting a sorted metadata is nop.
2565         */
2566        nextRequest->mSettings.sort();
2567        request.settings = nextRequest->mSettings.getAndLock();
2568        mPrevRequest = nextRequest;
2569        ALOGVV("%s: Request settings are NEW", __FUNCTION__);
2570
2571        IF_ALOGV() {
2572            camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
2573            find_camera_metadata_ro_entry(
2574                    request.settings,
2575                    ANDROID_CONTROL_AF_TRIGGER,
2576                    &e
2577            );
2578            if (e.count > 0) {
2579                ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
2580                      __FUNCTION__,
2581                      request.frame_number,
2582                      e.data.u8[0]);
2583            }
2584        }
2585    } else {
2586        // leave request.settings NULL to indicate 'reuse latest given'
2587        ALOGVV("%s: Request settings are REUSED",
2588               __FUNCTION__);
2589    }
2590
2591    camera3_stream_buffer_t inputBuffer;
2592    uint32_t totalNumBuffers = 0;
2593
2594    // Fill in buffers
2595
2596    if (nextRequest->mInputStream != NULL) {
2597        request.input_buffer = &inputBuffer;
2598        res = nextRequest->mInputStream->getInputBuffer(&inputBuffer);
2599        if (res != OK) {
2600            // Can't get input buffer from gralloc queue - this could be due to
2601            // disconnected queue or other producer misbehavior, so not a fatal
2602            // error
2603            ALOGE("RequestThread: Can't get input buffer, skipping request:"
2604                    " %s (%d)", strerror(-res), res);
2605            Mutex::Autolock l(mRequestLock);
2606            if (mListener != NULL) {
2607                mListener->notifyError(
2608                        ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2609                        nextRequest->mResultExtras);
2610            }
2611            cleanUpFailedRequest(request, nextRequest, outputBuffers);
2612            return true;
2613        }
2614        totalNumBuffers += 1;
2615    } else {
2616        request.input_buffer = NULL;
2617    }
2618
2619    outputBuffers.insertAt(camera3_stream_buffer_t(), 0,
2620            nextRequest->mOutputStreams.size());
2621    request.output_buffers = outputBuffers.array();
2622    for (size_t i = 0; i < nextRequest->mOutputStreams.size(); i++) {
2623        res = nextRequest->mOutputStreams.editItemAt(i)->
2624                getBuffer(&outputBuffers.editItemAt(i));
2625        if (res != OK) {
2626            // Can't get output buffer from gralloc queue - this could be due to
2627            // abandoned queue or other consumer misbehavior, so not a fatal
2628            // error
2629            ALOGE("RequestThread: Can't get output buffer, skipping request:"
2630                    " %s (%d)", strerror(-res), res);
2631            Mutex::Autolock l(mRequestLock);
2632            if (mListener != NULL) {
2633                mListener->notifyError(
2634                        ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
2635                        nextRequest->mResultExtras);
2636            }
2637            cleanUpFailedRequest(request, nextRequest, outputBuffers);
2638            return true;
2639        }
2640        request.num_output_buffers++;
2641    }
2642    totalNumBuffers += request.num_output_buffers;
2643
2644    // Log request in the in-flight queue
2645    sp<Camera3Device> parent = mParent.promote();
2646    if (parent == NULL) {
2647        // Should not happen, and nowhere to send errors to, so just log it
2648        CLOGE("RequestThread: Parent is gone");
2649        cleanUpFailedRequest(request, nextRequest, outputBuffers);
2650        return false;
2651    }
2652
2653    res = parent->registerInFlight(request.frame_number,
2654            totalNumBuffers, nextRequest->mResultExtras,
2655            /*hasInput*/request.input_buffer != NULL);
2656    ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
2657           ", burstId = %" PRId32 ".",
2658            __FUNCTION__,
2659            nextRequest->mResultExtras.requestId, nextRequest->mResultExtras.frameNumber,
2660            nextRequest->mResultExtras.burstId);
2661    if (res != OK) {
2662        SET_ERR("RequestThread: Unable to register new in-flight request:"
2663                " %s (%d)", strerror(-res), res);
2664        cleanUpFailedRequest(request, nextRequest, outputBuffers);
2665        return false;
2666    }
2667
2668    // Inform waitUntilRequestProcessed thread of a new request ID
2669    {
2670        Mutex::Autolock al(mLatestRequestMutex);
2671
2672        mLatestRequestId = requestId;
2673        mLatestRequestSignal.signal();
2674    }
2675
2676    // Submit request and block until ready for next one
2677    ATRACE_ASYNC_BEGIN("frame capture", request.frame_number);
2678    ATRACE_BEGIN("camera3->process_capture_request");
2679    res = mHal3Device->ops->process_capture_request(mHal3Device, &request);
2680    ATRACE_END();
2681
2682    if (res != OK) {
2683        // Should only get a failure here for malformed requests or device-level
2684        // errors, so consider all errors fatal.  Bad metadata failures should
2685        // come through notify.
2686        SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
2687                " device: %s (%d)", request.frame_number, strerror(-res), res);
2688        cleanUpFailedRequest(request, nextRequest, outputBuffers);
2689        return false;
2690    }
2691
2692    // Update the latest request sent to HAL
2693    if (request.settings != NULL) { // Don't update them if they were unchanged
2694        Mutex::Autolock al(mLatestRequestMutex);
2695
2696        camera_metadata_t* cloned = clone_camera_metadata(request.settings);
2697        mLatestRequest.acquire(cloned);
2698    }
2699
2700    if (request.settings != NULL) {
2701        nextRequest->mSettings.unlock(request.settings);
2702    }
2703
2704    // Remove any previously queued triggers (after unlock)
2705    res = removeTriggers(mPrevRequest);
2706    if (res != OK) {
2707        SET_ERR("RequestThread: Unable to remove triggers "
2708              "(capture request %d, HAL device: %s (%d)",
2709              request.frame_number, strerror(-res), res);
2710        return false;
2711    }
2712    mPrevTriggers = triggerCount;
2713
2714    return true;
2715}
2716
2717CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
2718    Mutex::Autolock al(mLatestRequestMutex);
2719
2720    ALOGV("RequestThread::%s", __FUNCTION__);
2721
2722    return mLatestRequest;
2723}
2724
2725
2726void Camera3Device::RequestThread::cleanUpFailedRequest(
2727        camera3_capture_request_t &request,
2728        sp<CaptureRequest> &nextRequest,
2729        Vector<camera3_stream_buffer_t> &outputBuffers) {
2730
2731    if (request.settings != NULL) {
2732        nextRequest->mSettings.unlock(request.settings);
2733    }
2734    if (request.input_buffer != NULL) {
2735        request.input_buffer->status = CAMERA3_BUFFER_STATUS_ERROR;
2736        nextRequest->mInputStream->returnInputBuffer(*(request.input_buffer));
2737    }
2738    for (size_t i = 0; i < request.num_output_buffers; i++) {
2739        outputBuffers.editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
2740        nextRequest->mOutputStreams.editItemAt(i)->returnBuffer(
2741            outputBuffers[i], 0);
2742    }
2743}
2744
2745sp<Camera3Device::CaptureRequest>
2746        Camera3Device::RequestThread::waitForNextRequest() {
2747    status_t res;
2748    sp<CaptureRequest> nextRequest;
2749
2750    // Optimized a bit for the simple steady-state case (single repeating
2751    // request), to avoid putting that request in the queue temporarily.
2752    Mutex::Autolock l(mRequestLock);
2753
2754    while (mRequestQueue.empty()) {
2755        if (!mRepeatingRequests.empty()) {
2756            // Always atomically enqueue all requests in a repeating request
2757            // list. Guarantees a complete in-sequence set of captures to
2758            // application.
2759            const RequestList &requests = mRepeatingRequests;
2760            RequestList::const_iterator firstRequest =
2761                    requests.begin();
2762            nextRequest = *firstRequest;
2763            mRequestQueue.insert(mRequestQueue.end(),
2764                    ++firstRequest,
2765                    requests.end());
2766            // No need to wait any longer
2767
2768            mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
2769
2770            break;
2771        }
2772
2773        res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
2774
2775        if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
2776                exitPending()) {
2777            Mutex::Autolock pl(mPauseLock);
2778            if (mPaused == false) {
2779                ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
2780                mPaused = true;
2781                // Let the tracker know
2782                sp<StatusTracker> statusTracker = mStatusTracker.promote();
2783                if (statusTracker != 0) {
2784                    statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2785                }
2786            }
2787            // Stop waiting for now and let thread management happen
2788            return NULL;
2789        }
2790    }
2791
2792    if (nextRequest == NULL) {
2793        // Don't have a repeating request already in hand, so queue
2794        // must have an entry now.
2795        RequestList::iterator firstRequest =
2796                mRequestQueue.begin();
2797        nextRequest = *firstRequest;
2798        mRequestQueue.erase(firstRequest);
2799    }
2800
2801    // In case we've been unpaused by setPaused clearing mDoPause, need to
2802    // update internal pause state (capture/setRepeatingRequest unpause
2803    // directly).
2804    Mutex::Autolock pl(mPauseLock);
2805    if (mPaused) {
2806        ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
2807        sp<StatusTracker> statusTracker = mStatusTracker.promote();
2808        if (statusTracker != 0) {
2809            statusTracker->markComponentActive(mStatusId);
2810        }
2811    }
2812    mPaused = false;
2813
2814    // Check if we've reconfigured since last time, and reset the preview
2815    // request if so. Can't use 'NULL request == repeat' across configure calls.
2816    if (mReconfigured) {
2817        mPrevRequest.clear();
2818        mReconfigured = false;
2819    }
2820
2821    if (nextRequest != NULL) {
2822        nextRequest->mResultExtras.frameNumber = mFrameNumber++;
2823        nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
2824        nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
2825    }
2826    return nextRequest;
2827}
2828
2829bool Camera3Device::RequestThread::waitIfPaused() {
2830    status_t res;
2831    Mutex::Autolock l(mPauseLock);
2832    while (mDoPause) {
2833        if (mPaused == false) {
2834            mPaused = true;
2835            ALOGV("%s: RequestThread: Paused", __FUNCTION__);
2836            // Let the tracker know
2837            sp<StatusTracker> statusTracker = mStatusTracker.promote();
2838            if (statusTracker != 0) {
2839                statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
2840            }
2841        }
2842
2843        res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
2844        if (res == TIMED_OUT || exitPending()) {
2845            return true;
2846        }
2847    }
2848    // We don't set mPaused to false here, because waitForNextRequest needs
2849    // to further manage the paused state in case of starvation.
2850    return false;
2851}
2852
2853void Camera3Device::RequestThread::unpauseForNewRequests() {
2854    // With work to do, mark thread as unpaused.
2855    // If paused by request (setPaused), don't resume, to avoid
2856    // extra signaling/waiting overhead to waitUntilPaused
2857    mRequestSignal.signal();
2858    Mutex::Autolock p(mPauseLock);
2859    if (!mDoPause) {
2860        ALOGV("%s: RequestThread: Going active", __FUNCTION__);
2861        if (mPaused) {
2862            sp<StatusTracker> statusTracker = mStatusTracker.promote();
2863            if (statusTracker != 0) {
2864                statusTracker->markComponentActive(mStatusId);
2865            }
2866        }
2867        mPaused = false;
2868    }
2869}
2870
2871void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
2872    sp<Camera3Device> parent = mParent.promote();
2873    if (parent != NULL) {
2874        va_list args;
2875        va_start(args, fmt);
2876
2877        parent->setErrorStateV(fmt, args);
2878
2879        va_end(args);
2880    }
2881}
2882
2883status_t Camera3Device::RequestThread::insertTriggers(
2884        const sp<CaptureRequest> &request) {
2885
2886    Mutex::Autolock al(mTriggerMutex);
2887
2888    sp<Camera3Device> parent = mParent.promote();
2889    if (parent == NULL) {
2890        CLOGE("RequestThread: Parent is gone");
2891        return DEAD_OBJECT;
2892    }
2893
2894    CameraMetadata &metadata = request->mSettings;
2895    size_t count = mTriggerMap.size();
2896
2897    for (size_t i = 0; i < count; ++i) {
2898        RequestTrigger trigger = mTriggerMap.valueAt(i);
2899        uint32_t tag = trigger.metadataTag;
2900
2901        if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
2902            bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
2903            uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
2904            if (isAeTrigger) {
2905                request->mResultExtras.precaptureTriggerId = triggerId;
2906                mCurrentPreCaptureTriggerId = triggerId;
2907            } else {
2908                request->mResultExtras.afTriggerId = triggerId;
2909                mCurrentAfTriggerId = triggerId;
2910            }
2911            if (parent->mDeviceVersion >= CAMERA_DEVICE_API_VERSION_3_2) {
2912                continue; // Trigger ID tag is deprecated since device HAL 3.2
2913            }
2914        }
2915
2916        camera_metadata_entry entry = metadata.find(tag);
2917
2918        if (entry.count > 0) {
2919            /**
2920             * Already has an entry for this trigger in the request.
2921             * Rewrite it with our requested trigger value.
2922             */
2923            RequestTrigger oldTrigger = trigger;
2924
2925            oldTrigger.entryValue = entry.data.u8[0];
2926
2927            mTriggerReplacedMap.add(tag, oldTrigger);
2928        } else {
2929            /**
2930             * More typical, no trigger entry, so we just add it
2931             */
2932            mTriggerRemovedMap.add(tag, trigger);
2933        }
2934
2935        status_t res;
2936
2937        switch (trigger.getTagType()) {
2938            case TYPE_BYTE: {
2939                uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2940                res = metadata.update(tag,
2941                                      &entryValue,
2942                                      /*count*/1);
2943                break;
2944            }
2945            case TYPE_INT32:
2946                res = metadata.update(tag,
2947                                      &trigger.entryValue,
2948                                      /*count*/1);
2949                break;
2950            default:
2951                ALOGE("%s: Type not supported: 0x%x",
2952                      __FUNCTION__,
2953                      trigger.getTagType());
2954                return INVALID_OPERATION;
2955        }
2956
2957        if (res != OK) {
2958            ALOGE("%s: Failed to update request metadata with trigger tag %s"
2959                  ", value %d", __FUNCTION__, trigger.getTagName(),
2960                  trigger.entryValue);
2961            return res;
2962        }
2963
2964        ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
2965              trigger.getTagName(),
2966              trigger.entryValue);
2967    }
2968
2969    mTriggerMap.clear();
2970
2971    return count;
2972}
2973
2974status_t Camera3Device::RequestThread::removeTriggers(
2975        const sp<CaptureRequest> &request) {
2976    Mutex::Autolock al(mTriggerMutex);
2977
2978    CameraMetadata &metadata = request->mSettings;
2979
2980    /**
2981     * Replace all old entries with their old values.
2982     */
2983    for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
2984        RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
2985
2986        status_t res;
2987
2988        uint32_t tag = trigger.metadataTag;
2989        switch (trigger.getTagType()) {
2990            case TYPE_BYTE: {
2991                uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
2992                res = metadata.update(tag,
2993                                      &entryValue,
2994                                      /*count*/1);
2995                break;
2996            }
2997            case TYPE_INT32:
2998                res = metadata.update(tag,
2999                                      &trigger.entryValue,
3000                                      /*count*/1);
3001                break;
3002            default:
3003                ALOGE("%s: Type not supported: 0x%x",
3004                      __FUNCTION__,
3005                      trigger.getTagType());
3006                return INVALID_OPERATION;
3007        }
3008
3009        if (res != OK) {
3010            ALOGE("%s: Failed to restore request metadata with trigger tag %s"
3011                  ", trigger value %d", __FUNCTION__,
3012                  trigger.getTagName(), trigger.entryValue);
3013            return res;
3014        }
3015    }
3016    mTriggerReplacedMap.clear();
3017
3018    /**
3019     * Remove all new entries.
3020     */
3021    for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
3022        RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
3023        status_t res = metadata.erase(trigger.metadataTag);
3024
3025        if (res != OK) {
3026            ALOGE("%s: Failed to erase metadata with trigger tag %s"
3027                  ", trigger value %d", __FUNCTION__,
3028                  trigger.getTagName(), trigger.entryValue);
3029            return res;
3030        }
3031    }
3032    mTriggerRemovedMap.clear();
3033
3034    return OK;
3035}
3036
3037status_t Camera3Device::RequestThread::addDummyTriggerIds(
3038        const sp<CaptureRequest> &request) {
3039    // Trigger ID 0 has special meaning in the HAL2 spec, so avoid it here
3040    static const int32_t dummyTriggerId = 1;
3041    status_t res;
3042
3043    CameraMetadata &metadata = request->mSettings;
3044
3045    // If AF trigger is active, insert a dummy AF trigger ID if none already
3046    // exists
3047    camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
3048    camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
3049    if (afTrigger.count > 0 &&
3050            afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
3051            afId.count == 0) {
3052        res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
3053        if (res != OK) return res;
3054    }
3055
3056    // If AE precapture trigger is active, insert a dummy precapture trigger ID
3057    // if none already exists
3058    camera_metadata_entry pcTrigger =
3059            metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
3060    camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
3061    if (pcTrigger.count > 0 &&
3062            pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
3063            pcId.count == 0) {
3064        res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
3065                &dummyTriggerId, 1);
3066        if (res != OK) return res;
3067    }
3068
3069    return OK;
3070}
3071
3072
3073/**
3074 * Static callback forwarding methods from HAL to instance
3075 */
3076
3077void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
3078        const camera3_capture_result *result) {
3079    Camera3Device *d =
3080            const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3081    d->processCaptureResult(result);
3082}
3083
3084void Camera3Device::sNotify(const camera3_callback_ops *cb,
3085        const camera3_notify_msg *msg) {
3086    Camera3Device *d =
3087            const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
3088    d->notify(msg);
3089}
3090
3091}; // namespace android
3092