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