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