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