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