1 /*
2 * Copyright (C) 2013-2018 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 %s: %s: " fmt, mId.string(), __FUNCTION__, \
30 ##__VA_ARGS__)
31
32 #define CLOGW(fmt, ...) ALOGW("Camera %s: %s: " fmt, mId.string(), __FUNCTION__, \
33 ##__VA_ARGS__)
34
35 // Convenience macros for transitioning to the error state
36 #define SET_ERR(fmt, ...) setErrorState( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39 #define SET_ERR_L(fmt, ...) setErrorStateLocked( \
40 "%s: " fmt, __FUNCTION__, \
41 ##__VA_ARGS__)
42
43 #include <inttypes.h>
44
45 #include <utility>
46
47 #include <utils/Log.h>
48 #include <utils/Trace.h>
49 #include <utils/Timers.h>
50 #include <cutils/properties.h>
51
52 #include <android/hardware/camera/device/3.7/ICameraInjectionSession.h>
53 #include <android/hardware/camera2/ICameraDeviceUser.h>
54
55 #include "CameraService.h"
56 #include "aidl/android/hardware/graphics/common/Dataspace.h"
57 #include "aidl/AidlUtils.h"
58 #include "device3/Camera3Device.h"
59 #include "device3/Camera3FakeStream.h"
60 #include "device3/Camera3InputStream.h"
61 #include "device3/Camera3OutputStream.h"
62 #include "device3/Camera3SharedOutputStream.h"
63 #include "mediautils/SchedulingPolicyService.h"
64 #include "utils/CameraThreadState.h"
65 #include "utils/CameraTraces.h"
66 #include "utils/SessionConfigurationUtils.h"
67 #include "utils/TraceHFR.h"
68
69 #include <algorithm>
70 #include <optional>
71 #include <tuple>
72
73 using namespace android::camera3;
74 using namespace android::hardware::camera;
75
76 namespace android {
77
Camera3Device(std::shared_ptr<CameraServiceProxyWrapper> & cameraServiceProxyWrapper,const String8 & id,bool overrideForPerfClass,bool overrideToPortrait,bool legacyClient)78 Camera3Device::Camera3Device(std::shared_ptr<CameraServiceProxyWrapper>& cameraServiceProxyWrapper,
79 const String8 &id, bool overrideForPerfClass, bool overrideToPortrait, bool legacyClient):
80 mCameraServiceProxyWrapper(cameraServiceProxyWrapper),
81 mId(id),
82 mLegacyClient(legacyClient),
83 mOperatingMode(NO_MODE),
84 mIsConstrainedHighSpeedConfiguration(false),
85 mIsCompositeJpegRDisabled(false),
86 mStatus(STATUS_UNINITIALIZED),
87 mStatusWaiters(0),
88 mUsePartialResult(false),
89 mNumPartialResults(1),
90 mDeviceTimeBaseIsRealtime(false),
91 mTimestampOffset(0),
92 mNextResultFrameNumber(0),
93 mNextReprocessResultFrameNumber(0),
94 mNextZslStillResultFrameNumber(0),
95 mNextShutterFrameNumber(0),
96 mNextReprocessShutterFrameNumber(0),
97 mNextZslStillShutterFrameNumber(0),
98 mListener(NULL),
99 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID),
100 mLastTemplateId(-1),
101 mNeedFixupMonochromeTags(false),
102 mOverrideForPerfClass(overrideForPerfClass),
103 mOverrideToPortrait(overrideToPortrait),
104 mRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE),
105 mComposerOutput(false),
106 mAutoframingOverride(ANDROID_CONTROL_AUTOFRAMING_OFF),
107 mSettingsOverride(-1),
108 mActivePhysicalId("")
109 {
110 ATRACE_CALL();
111 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
112 }
113
~Camera3Device()114 Camera3Device::~Camera3Device()
115 {
116 ATRACE_CALL();
117 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
118 disconnectImpl();
119 }
120
getId() const121 const String8& Camera3Device::getId() const {
122 return mId;
123 }
124
initializeCommonLocked()125 status_t Camera3Device::initializeCommonLocked() {
126
127 /** Start up status tracker thread */
128 mStatusTracker = new StatusTracker(this);
129 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
130 if (res != OK) {
131 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
132 strerror(-res), res);
133 mInterface->close();
134 mStatusTracker.clear();
135 return res;
136 }
137
138 /** Register in-flight map to the status tracker */
139 mInFlightStatusId = mStatusTracker->addComponent("InflightRequests");
140
141 if (mUseHalBufManager) {
142 res = mRequestBufferSM.initialize(mStatusTracker);
143 if (res != OK) {
144 SET_ERR_L("Unable to start request buffer state machine: %s (%d)",
145 strerror(-res), res);
146 mInterface->close();
147 mStatusTracker.clear();
148 return res;
149 }
150 }
151
152 /** Create buffer manager */
153 mBufferManager = new Camera3BufferManager();
154
155 Vector<int32_t> sessionParamKeys;
156 camera_metadata_entry_t sessionKeysEntry = mDeviceInfo.find(
157 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
158 if (sessionKeysEntry.count > 0) {
159 sessionParamKeys.insertArrayAt(sessionKeysEntry.data.i32, 0, sessionKeysEntry.count);
160 }
161
162 camera_metadata_entry_t availableTestPatternModes = mDeviceInfo.find(
163 ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES);
164 for (size_t i = 0; i < availableTestPatternModes.count; i++) {
165 if (availableTestPatternModes.data.i32[i] ==
166 ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR) {
167 mSupportCameraMute = true;
168 mSupportTestPatternSolidColor = true;
169 break;
170 } else if (availableTestPatternModes.data.i32[i] ==
171 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK) {
172 mSupportCameraMute = true;
173 mSupportTestPatternSolidColor = false;
174 }
175 }
176
177 camera_metadata_entry_t availableSettingsOverrides = mDeviceInfo.find(
178 ANDROID_CONTROL_AVAILABLE_SETTINGS_OVERRIDES);
179 for (size_t i = 0; i < availableSettingsOverrides.count; i++) {
180 if (availableSettingsOverrides.data.i32[i] ==
181 ANDROID_CONTROL_SETTINGS_OVERRIDE_ZOOM) {
182 mSupportZoomOverride = true;
183 break;
184 }
185 }
186
187 /** Start up request queue thread */
188 mRequestThread = createNewRequestThread(
189 this, mStatusTracker, mInterface, sessionParamKeys,
190 mUseHalBufManager, mSupportCameraMute, mOverrideToPortrait,
191 mSupportZoomOverride);
192 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
193 if (res != OK) {
194 SET_ERR_L("Unable to start request queue thread: %s (%d)",
195 strerror(-res), res);
196 mInterface->close();
197 mRequestThread.clear();
198 return res;
199 }
200
201 mPreparerThread = new PreparerThread();
202
203 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
204 mNextStreamId = 0;
205 mFakeStreamId = NO_STREAM;
206 mNeedConfig = true;
207 mPauseStateNotify = false;
208 mIsInputStreamMultiResolution = false;
209
210 // Measure the clock domain offset between camera and video/hw_composer
211 mTimestampOffset = getMonoToBoottimeOffset();
212 camera_metadata_entry timestampSource =
213 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
214 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
215 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
216 mDeviceTimeBaseIsRealtime = true;
217 }
218
219 // Will the HAL be sending in early partial result metadata?
220 camera_metadata_entry partialResultsCount =
221 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
222 if (partialResultsCount.count > 0) {
223 mNumPartialResults = partialResultsCount.data.i32[0];
224 mUsePartialResult = (mNumPartialResults > 1);
225 }
226
227 bool usePrecorrectArray = DistortionMapper::isDistortionSupported(mDeviceInfo);
228 if (usePrecorrectArray) {
229 res = mDistortionMappers[mId.c_str()].setupStaticInfo(mDeviceInfo);
230 if (res != OK) {
231 SET_ERR_L("Unable to read necessary calibration fields for distortion correction");
232 return res;
233 }
234 }
235
236 mZoomRatioMappers[mId.c_str()] = ZoomRatioMapper(&mDeviceInfo,
237 mSupportNativeZoomRatio, usePrecorrectArray);
238
239 if (SessionConfigurationUtils::supportsUltraHighResolutionCapture(mDeviceInfo)) {
240 mUHRCropAndMeteringRegionMappers[mId.c_str()] =
241 UHRCropAndMeteringRegionMapper(mDeviceInfo, usePrecorrectArray);
242 }
243
244 if (RotateAndCropMapper::isNeeded(&mDeviceInfo)) {
245 mRotateAndCropMappers.emplace(mId.c_str(), &mDeviceInfo);
246 }
247
248 // Hidl/AidlCamera3DeviceInjectionMethods
249 mInjectionMethods = createCamera3DeviceInjectionMethods(this);
250
251 /** Start watchdog thread */
252 mCameraServiceWatchdog = new CameraServiceWatchdog(mId, mCameraServiceProxyWrapper);
253 res = mCameraServiceWatchdog->run("CameraServiceWatchdog");
254 if (res != OK) {
255 SET_ERR_L("Unable to start camera service watchdog thread: %s (%d)",
256 strerror(-res), res);
257 return res;
258 }
259
260 return OK;
261 }
262
disconnect()263 status_t Camera3Device::disconnect() {
264 return disconnectImpl();
265 }
266
disconnectImpl()267 status_t Camera3Device::disconnectImpl() {
268 ATRACE_CALL();
269 Mutex::Autolock il(mInterfaceLock);
270
271 ALOGI("%s: E", __FUNCTION__);
272
273 status_t res = OK;
274 std::vector<wp<Camera3StreamInterface>> streams;
275 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
276 {
277 Mutex::Autolock l(mLock);
278 if (mStatus == STATUS_UNINITIALIZED) return res;
279
280 if (mRequestThread != NULL) {
281 if (mStatus == STATUS_ACTIVE || mStatus == STATUS_ERROR) {
282 res = mRequestThread->clear();
283 if (res != OK) {
284 SET_ERR_L("Can't stop streaming");
285 // Continue to close device even in case of error
286 } else {
287 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration,
288 /*requestThreadInvocation*/ false);
289 if (res != OK) {
290 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
291 maxExpectedDuration);
292 // Continue to close device even in case of error
293 }
294 }
295 }
296 // Signal to request thread that we're not expecting any
297 // more requests. This will be true since once we're in
298 // disconnect and we've cleared off the request queue, the
299 // request thread can't receive any new requests through
300 // binder calls - since disconnect holds
301 // mBinderSerialization lock.
302 mRequestThread->setRequestClearing();
303 }
304
305 if (mStatus == STATUS_ERROR) {
306 CLOGE("Shutting down in an error state");
307 }
308
309 if (mStatusTracker != NULL) {
310 mStatusTracker->requestExit();
311 }
312
313 if (mRequestThread != NULL) {
314 mRequestThread->requestExit();
315 }
316
317 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
318 for (size_t i = 0; i < mOutputStreams.size(); i++) {
319 streams.push_back(mOutputStreams[i]);
320 }
321 if (mInputStream != nullptr) {
322 streams.push_back(mInputStream);
323 }
324 }
325
326 // Joining done without holding mLock, otherwise deadlocks may ensue
327 // as the threads try to access parent state
328 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
329 // HAL may be in a bad state, so waiting for request thread
330 // (which may be stuck in the HAL processCaptureRequest call)
331 // could be dangerous.
332 mRequestThread->join();
333 }
334
335 if (mStatusTracker != NULL) {
336 mStatusTracker->join();
337 }
338
339 if (mInjectionMethods->isInjecting()) {
340 mInjectionMethods->stopInjection();
341 }
342
343 HalInterface* interface;
344 {
345 Mutex::Autolock l(mLock);
346 mRequestThread.clear();
347 Mutex::Autolock stLock(mTrackerLock);
348 mStatusTracker.clear();
349 interface = mInterface.get();
350 }
351
352 // Call close without internal mutex held, as the HAL close may need to
353 // wait on assorted callbacks,etc, to complete before it can return.
354 mCameraServiceWatchdog->WATCH(interface->close());
355
356 flushInflightRequests();
357
358 {
359 Mutex::Autolock l(mLock);
360 mInterface->clear();
361 mOutputStreams.clear();
362 mInputStream.clear();
363 mDeletedStreams.clear();
364 mBufferManager.clear();
365 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
366 }
367
368 for (auto& weakStream : streams) {
369 sp<Camera3StreamInterface> stream = weakStream.promote();
370 if (stream != nullptr) {
371 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
372 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
373 }
374 }
375 ALOGI("%s: X", __FUNCTION__);
376
377 if (mCameraServiceWatchdog != NULL) {
378 mCameraServiceWatchdog->requestExit();
379 mCameraServiceWatchdog.clear();
380 }
381
382 return res;
383 }
384
385 // For dumping/debugging only -
386 // try to acquire a lock a few times, eventually give up to proceed with
387 // debug/dump operations
tryLockSpinRightRound(Mutex & lock)388 bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
389 bool gotLock = false;
390 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
391 if (lock.tryLock() == NO_ERROR) {
392 gotLock = true;
393 break;
394 } else {
395 usleep(kDumpSleepDuration);
396 }
397 }
398 return gotLock;
399 }
400
getMonoToBoottimeOffset()401 nsecs_t Camera3Device::getMonoToBoottimeOffset() {
402 // try three times to get the clock offset, choose the one
403 // with the minimum gap in measurements.
404 const int tries = 3;
405 nsecs_t bestGap, measured;
406 for (int i = 0; i < tries; ++i) {
407 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
408 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
409 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
410 const nsecs_t gap = tmono2 - tmono;
411 if (i == 0 || gap < bestGap) {
412 bestGap = gap;
413 measured = tbase - ((tmono + tmono2) >> 1);
414 }
415 }
416 return measured;
417 }
418
getJpegBufferSize(const CameraMetadata & info,uint32_t width,uint32_t height) const419 ssize_t Camera3Device::getJpegBufferSize(const CameraMetadata &info, uint32_t width,
420 uint32_t height) const {
421 // Get max jpeg size (area-wise) for default sensor pixel mode
422 camera3::Size maxDefaultJpegResolution =
423 SessionConfigurationUtils::getMaxJpegResolution(info,
424 /*supportsUltraHighResolutionCapture*/false);
425 // Get max jpeg size (area-wise) for max resolution sensor pixel mode / 0 if
426 // not ultra high res sensor
427 camera3::Size uhrMaxJpegResolution =
428 SessionConfigurationUtils::getMaxJpegResolution(info,
429 /*isUltraHighResolution*/true);
430 if (maxDefaultJpegResolution.width == 0) {
431 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
432 __FUNCTION__, mId.string());
433 return BAD_VALUE;
434 }
435 bool useMaxSensorPixelModeThreshold = false;
436 if (uhrMaxJpegResolution.width != 0 &&
437 width * height > maxDefaultJpegResolution.width * maxDefaultJpegResolution.height) {
438 // Use the ultra high res max jpeg size and max jpeg buffer size
439 useMaxSensorPixelModeThreshold = true;
440 }
441
442 // Get max jpeg buffer size
443 ssize_t maxJpegBufferSize = 0;
444 camera_metadata_ro_entry jpegBufMaxSize = info.find(ANDROID_JPEG_MAX_SIZE);
445 if (jpegBufMaxSize.count == 0) {
446 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
447 mId.string());
448 return BAD_VALUE;
449 }
450 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
451
452 camera3::Size chosenMaxJpegResolution = maxDefaultJpegResolution;
453 if (useMaxSensorPixelModeThreshold) {
454 maxJpegBufferSize =
455 SessionConfigurationUtils::getUHRMaxJpegBufferSize(uhrMaxJpegResolution,
456 maxDefaultJpegResolution, maxJpegBufferSize);
457 chosenMaxJpegResolution = uhrMaxJpegResolution;
458 }
459 assert(kMinJpegBufferSize < maxJpegBufferSize);
460
461 // Calculate final jpeg buffer size for the given resolution.
462 float scaleFactor = ((float) (width * height)) /
463 (chosenMaxJpegResolution.width * chosenMaxJpegResolution.height);
464 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
465 kMinJpegBufferSize;
466 if (jpegBufferSize > maxJpegBufferSize) {
467 ALOGI("%s: jpeg buffer size calculated is > maxJpeg bufferSize(%zd), clamping",
468 __FUNCTION__, maxJpegBufferSize);
469 jpegBufferSize = maxJpegBufferSize;
470 }
471 return jpegBufferSize;
472 }
473
getPointCloudBufferSize(const CameraMetadata & info) const474 ssize_t Camera3Device::getPointCloudBufferSize(const CameraMetadata &info) const {
475 const int FLOATS_PER_POINT=4;
476 camera_metadata_ro_entry maxPointCount = info.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
477 if (maxPointCount.count == 0) {
478 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
479 __FUNCTION__, mId.string());
480 return BAD_VALUE;
481 }
482 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
483 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
484 return maxBytesForPointCloud;
485 }
486
getRawOpaqueBufferSize(const CameraMetadata & info,int32_t width,int32_t height,bool maxResolution) const487 ssize_t Camera3Device::getRawOpaqueBufferSize(const CameraMetadata &info, int32_t width,
488 int32_t height, bool maxResolution) const {
489 const int PER_CONFIGURATION_SIZE = 3;
490 const int WIDTH_OFFSET = 0;
491 const int HEIGHT_OFFSET = 1;
492 const int SIZE_OFFSET = 2;
493 camera_metadata_ro_entry rawOpaqueSizes =
494 info.find(
495 camera3::SessionConfigurationUtils::getAppropriateModeTag(
496 ANDROID_SENSOR_OPAQUE_RAW_SIZE,
497 maxResolution));
498 size_t count = rawOpaqueSizes.count;
499 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
500 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
501 __FUNCTION__, mId.string(), count);
502 return BAD_VALUE;
503 }
504
505 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
506 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
507 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
508 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
509 }
510 }
511
512 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
513 __FUNCTION__, mId.string(), width, height);
514 return BAD_VALUE;
515 }
516
dump(int fd,const Vector<String16> & args)517 status_t Camera3Device::dump(int fd, [[maybe_unused]] const Vector<String16> &args) {
518 ATRACE_CALL();
519
520 // Try to lock, but continue in case of failure (to avoid blocking in
521 // deadlocks)
522 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
523 bool gotLock = tryLockSpinRightRound(mLock);
524
525 ALOGW_IF(!gotInterfaceLock,
526 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
527 mId.string(), __FUNCTION__);
528 ALOGW_IF(!gotLock,
529 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
530 mId.string(), __FUNCTION__);
531
532 bool dumpTemplates = false;
533
534 String16 templatesOption("-t");
535 int n = args.size();
536 for (int i = 0; i < n; i++) {
537 if (args[i] == templatesOption) {
538 dumpTemplates = true;
539 }
540 if (args[i] == TagMonitor::kMonitorOption) {
541 if (i + 1 < n) {
542 String8 monitorTags = String8(args[i + 1]);
543 if (monitorTags == "off") {
544 mTagMonitor.disableMonitoring();
545 } else {
546 mTagMonitor.parseTagsToMonitor(monitorTags);
547 }
548 } else {
549 mTagMonitor.disableMonitoring();
550 }
551 }
552 }
553
554 String8 lines;
555
556 const char *status =
557 mStatus == STATUS_ERROR ? "ERROR" :
558 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
559 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
560 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
561 mStatus == STATUS_ACTIVE ? "ACTIVE" :
562 "Unknown";
563
564 lines.appendFormat(" Device status: %s\n", status);
565 if (mStatus == STATUS_ERROR) {
566 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
567 }
568 lines.appendFormat(" Stream configuration:\n");
569 const char *mode =
570 mOperatingMode == CAMERA_STREAM_CONFIGURATION_NORMAL_MODE ? "NORMAL" :
571 mOperatingMode == CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE ?
572 "CONSTRAINED_HIGH_SPEED" : "CUSTOM";
573 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
574
575 if (mInputStream != NULL) {
576 write(fd, lines.string(), lines.size());
577 mInputStream->dump(fd, args);
578 } else {
579 lines.appendFormat(" No input stream.\n");
580 write(fd, lines.string(), lines.size());
581 }
582 for (size_t i = 0; i < mOutputStreams.size(); i++) {
583 mOutputStreams[i]->dump(fd,args);
584 }
585
586 if (mBufferManager != NULL) {
587 lines = String8(" Camera3 Buffer Manager:\n");
588 write(fd, lines.string(), lines.size());
589 mBufferManager->dump(fd, args);
590 }
591
592 lines = String8(" In-flight requests:\n");
593 if (mInFlightLock.try_lock()) {
594 if (mInFlightMap.size() == 0) {
595 lines.append(" None\n");
596 } else {
597 for (size_t i = 0; i < mInFlightMap.size(); i++) {
598 InFlightRequest r = mInFlightMap.valueAt(i);
599 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
600 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
601 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
602 r.numBuffersLeft);
603 }
604 }
605 mInFlightLock.unlock();
606 } else {
607 lines.append(" Failed to acquire In-flight lock!\n");
608 }
609 write(fd, lines.string(), lines.size());
610
611 if (mRequestThread != NULL) {
612 mRequestThread->dumpCaptureRequestLatency(fd,
613 " ProcessCaptureRequest latency histogram:");
614 }
615
616 {
617 lines = String8(" Last request sent:\n");
618 write(fd, lines.string(), lines.size());
619
620 CameraMetadata lastRequest = getLatestRequestLocked();
621 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
622 }
623
624 if (dumpTemplates) {
625 const char *templateNames[CAMERA_TEMPLATE_COUNT] = {
626 "TEMPLATE_PREVIEW",
627 "TEMPLATE_STILL_CAPTURE",
628 "TEMPLATE_VIDEO_RECORD",
629 "TEMPLATE_VIDEO_SNAPSHOT",
630 "TEMPLATE_ZERO_SHUTTER_LAG",
631 "TEMPLATE_MANUAL",
632 };
633
634 for (int i = 1; i < CAMERA_TEMPLATE_COUNT; i++) {
635 camera_metadata_t *templateRequest = nullptr;
636 mInterface->constructDefaultRequestSettings(
637 (camera_request_template_t) i, &templateRequest);
638 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
639 if (templateRequest == nullptr) {
640 lines.append(" Not supported\n");
641 write(fd, lines.string(), lines.size());
642 } else {
643 write(fd, lines.string(), lines.size());
644 dump_indented_camera_metadata(templateRequest,
645 fd, /*verbosity*/2, /*indentation*/8);
646 }
647 free_camera_metadata(templateRequest);
648 }
649 }
650
651 mTagMonitor.dumpMonitoredMetadata(fd);
652
653 if (mInterface->valid()) {
654 lines = String8(" HAL device dump:\n");
655 write(fd, lines.string(), lines.size());
656 mInterface->dump(fd);
657 }
658
659 if (gotLock) mLock.unlock();
660 if (gotInterfaceLock) mInterfaceLock.unlock();
661
662 return OK;
663 }
664
startWatchingTags(const String8 & tags)665 status_t Camera3Device::startWatchingTags(const String8 &tags) {
666 mTagMonitor.parseTagsToMonitor(tags);
667 return OK;
668 }
669
stopWatchingTags()670 status_t Camera3Device::stopWatchingTags() {
671 mTagMonitor.disableMonitoring();
672 return OK;
673 }
674
dumpWatchedEventsToVector(std::vector<std::string> & out)675 status_t Camera3Device::dumpWatchedEventsToVector(std::vector<std::string> &out) {
676 mTagMonitor.getLatestMonitoredTagEvents(out);
677 return OK;
678 }
679
infoPhysical(const String8 & physicalId) const680 const CameraMetadata& Camera3Device::infoPhysical(const String8& physicalId) const {
681 ALOGVV("%s: E", __FUNCTION__);
682 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
683 mStatus == STATUS_ERROR)) {
684 ALOGW("%s: Access to static info %s!", __FUNCTION__,
685 mStatus == STATUS_ERROR ?
686 "when in error state" : "before init");
687 }
688 if (physicalId.isEmpty()) {
689 return mDeviceInfo;
690 } else {
691 std::string id(physicalId.c_str());
692 if (mPhysicalDeviceInfoMap.find(id) != mPhysicalDeviceInfoMap.end()) {
693 return mPhysicalDeviceInfoMap.at(id);
694 } else {
695 ALOGE("%s: Invalid physical camera id %s", __FUNCTION__, physicalId.c_str());
696 return mDeviceInfo;
697 }
698 }
699 }
700
info() const701 const CameraMetadata& Camera3Device::info() const {
702 String8 emptyId;
703 return infoPhysical(emptyId);
704 }
705
checkStatusOkToCaptureLocked()706 status_t Camera3Device::checkStatusOkToCaptureLocked() {
707 switch (mStatus) {
708 case STATUS_ERROR:
709 CLOGE("Device has encountered a serious error");
710 return INVALID_OPERATION;
711 case STATUS_UNINITIALIZED:
712 CLOGE("Device not initialized");
713 return INVALID_OPERATION;
714 case STATUS_UNCONFIGURED:
715 case STATUS_CONFIGURED:
716 case STATUS_ACTIVE:
717 // OK
718 break;
719 default:
720 SET_ERR_L("Unexpected status: %d", mStatus);
721 return INVALID_OPERATION;
722 }
723 return OK;
724 }
725
convertMetadataListToRequestListLocked(const List<const PhysicalCameraSettingsList> & metadataList,const std::list<const SurfaceMap> & surfaceMaps,bool repeating,nsecs_t requestTimeNs,RequestList * requestList)726 status_t Camera3Device::convertMetadataListToRequestListLocked(
727 const List<const PhysicalCameraSettingsList> &metadataList,
728 const std::list<const SurfaceMap> &surfaceMaps,
729 bool repeating, nsecs_t requestTimeNs,
730 RequestList *requestList) {
731 if (requestList == NULL) {
732 CLOGE("requestList cannot be NULL.");
733 return BAD_VALUE;
734 }
735
736 int32_t burstId = 0;
737 List<const PhysicalCameraSettingsList>::const_iterator metadataIt = metadataList.begin();
738 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
739 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
740 ++metadataIt, ++surfaceMapIt) {
741 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
742 if (newRequest == 0) {
743 CLOGE("Can't create capture request");
744 return BAD_VALUE;
745 }
746
747 newRequest->mRepeating = repeating;
748 newRequest->mRequestTimeNs = requestTimeNs;
749
750 // Setup burst Id and request Id
751 newRequest->mResultExtras.burstId = burstId++;
752 auto requestIdEntry = metadataIt->begin()->metadata.find(ANDROID_REQUEST_ID);
753 if (requestIdEntry.count == 0) {
754 CLOGE("RequestID does not exist in metadata");
755 return BAD_VALUE;
756 }
757 newRequest->mResultExtras.requestId = requestIdEntry.data.i32[0];
758
759 requestList->push_back(newRequest);
760
761 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
762 }
763 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
764 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
765 return BAD_VALUE;
766 }
767
768 // Setup batch size if this is a high speed video recording request.
769 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
770 auto firstRequest = requestList->begin();
771 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
772 if (outputStream->isVideoStream()) {
773 (*firstRequest)->mBatchSize = requestList->size();
774 outputStream->setBatchSize(requestList->size());
775 break;
776 }
777 }
778 }
779
780 return OK;
781 }
782
capture(CameraMetadata & request,int64_t * lastFrameNumber)783 status_t Camera3Device::capture(CameraMetadata &request, int64_t* lastFrameNumber) {
784 ATRACE_CALL();
785
786 List<const PhysicalCameraSettingsList> requestsList;
787 std::list<const SurfaceMap> surfaceMaps;
788 convertToRequestList(requestsList, surfaceMaps, request);
789
790 return captureList(requestsList, surfaceMaps, lastFrameNumber);
791 }
792
convertToRequestList(List<const PhysicalCameraSettingsList> & requestsList,std::list<const SurfaceMap> & surfaceMaps,const CameraMetadata & request)793 void Camera3Device::convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
794 std::list<const SurfaceMap>& surfaceMaps,
795 const CameraMetadata& request) {
796 PhysicalCameraSettingsList requestList;
797 requestList.push_back({std::string(getId().string()), request});
798 requestsList.push_back(requestList);
799
800 SurfaceMap surfaceMap;
801 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
802 // With no surface list passed in, stream and surface will have 1-to-1
803 // mapping. So the surface index is 0 for each stream in the surfaceMap.
804 for (size_t i = 0; i < streams.count; i++) {
805 surfaceMap[streams.data.i32[i]].push_back(0);
806 }
807 surfaceMaps.push_back(surfaceMap);
808 }
809
submitRequestsHelper(const List<const PhysicalCameraSettingsList> & requests,const std::list<const SurfaceMap> & surfaceMaps,bool repeating,int64_t * lastFrameNumber)810 status_t Camera3Device::submitRequestsHelper(
811 const List<const PhysicalCameraSettingsList> &requests,
812 const std::list<const SurfaceMap> &surfaceMaps,
813 bool repeating,
814 /*out*/
815 int64_t *lastFrameNumber) {
816 ATRACE_CALL();
817 nsecs_t requestTimeNs = systemTime();
818
819 Mutex::Autolock il(mInterfaceLock);
820 Mutex::Autolock l(mLock);
821
822 status_t res = checkStatusOkToCaptureLocked();
823 if (res != OK) {
824 // error logged by previous call
825 return res;
826 }
827
828 RequestList requestList;
829
830 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
831 repeating, requestTimeNs, /*out*/&requestList);
832 if (res != OK) {
833 // error logged by previous call
834 return res;
835 }
836
837 if (repeating) {
838 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
839 } else {
840 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
841 }
842
843 if (res == OK) {
844 waitUntilStateThenRelock(/*active*/true, kActiveTimeout, /*requestThreadInvocation*/false);
845 if (res != OK) {
846 SET_ERR_L("Can't transition to active in %f seconds!",
847 kActiveTimeout/1e9);
848 }
849 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
850 (*(requestList.begin()))->mResultExtras.requestId);
851 } else {
852 CLOGE("Cannot queue request. Impossible.");
853 return BAD_VALUE;
854 }
855
856 return res;
857 }
858
captureList(const List<const PhysicalCameraSettingsList> & requestsList,const std::list<const SurfaceMap> & surfaceMaps,int64_t * lastFrameNumber)859 status_t Camera3Device::captureList(const List<const PhysicalCameraSettingsList> &requestsList,
860 const std::list<const SurfaceMap> &surfaceMaps,
861 int64_t *lastFrameNumber) {
862 ATRACE_CALL();
863
864 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/false, lastFrameNumber);
865 }
866
setStreamingRequest(const CameraMetadata & request,int64_t *)867 status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
868 int64_t* /*lastFrameNumber*/) {
869 ATRACE_CALL();
870
871 List<const PhysicalCameraSettingsList> requestsList;
872 std::list<const SurfaceMap> surfaceMaps;
873 convertToRequestList(requestsList, surfaceMaps, request);
874
875 return setStreamingRequestList(requestsList, /*surfaceMap*/surfaceMaps,
876 /*lastFrameNumber*/NULL);
877 }
878
setStreamingRequestList(const List<const PhysicalCameraSettingsList> & requestsList,const std::list<const SurfaceMap> & surfaceMaps,int64_t * lastFrameNumber)879 status_t Camera3Device::setStreamingRequestList(
880 const List<const PhysicalCameraSettingsList> &requestsList,
881 const std::list<const SurfaceMap> &surfaceMaps, int64_t *lastFrameNumber) {
882 ATRACE_CALL();
883
884 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/true, lastFrameNumber);
885 }
886
setUpRequestLocked(const PhysicalCameraSettingsList & request,const SurfaceMap & surfaceMap)887 sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
888 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
889 status_t res;
890
891 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
892 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
893 // so unilaterally select normal operating mode.
894 res = filterParamsAndConfigureLocked(request.begin()->metadata,
895 CAMERA_STREAM_CONFIGURATION_NORMAL_MODE);
896 // Stream configuration failed. Client might try other configuraitons.
897 if (res != OK) {
898 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
899 return NULL;
900 } else if (mStatus == STATUS_UNCONFIGURED) {
901 // Stream configuration successfully configure to empty stream configuration.
902 CLOGE("No streams configured");
903 return NULL;
904 }
905 }
906
907 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
908 return newRequest;
909 }
910
clearStreamingRequest(int64_t * lastFrameNumber)911 status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
912 ATRACE_CALL();
913 Mutex::Autolock il(mInterfaceLock);
914 Mutex::Autolock l(mLock);
915
916 switch (mStatus) {
917 case STATUS_ERROR:
918 CLOGE("Device has encountered a serious error");
919 return INVALID_OPERATION;
920 case STATUS_UNINITIALIZED:
921 CLOGE("Device not initialized");
922 return INVALID_OPERATION;
923 case STATUS_UNCONFIGURED:
924 case STATUS_CONFIGURED:
925 case STATUS_ACTIVE:
926 // OK
927 break;
928 default:
929 SET_ERR_L("Unexpected status: %d", mStatus);
930 return INVALID_OPERATION;
931 }
932 ALOGV("Camera %s: Clearing repeating request", mId.string());
933
934 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
935 }
936
waitUntilRequestReceived(int32_t requestId,nsecs_t timeout)937 status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
938 ATRACE_CALL();
939 Mutex::Autolock il(mInterfaceLock);
940
941 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
942 }
943
createInputStream(uint32_t width,uint32_t height,int format,bool isMultiResolution,int * id)944 status_t Camera3Device::createInputStream(
945 uint32_t width, uint32_t height, int format, bool isMultiResolution, int *id) {
946 ATRACE_CALL();
947 Mutex::Autolock il(mInterfaceLock);
948 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
949 Mutex::Autolock l(mLock);
950 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
951 mId.string(), mNextStreamId, width, height, format);
952
953 status_t res;
954 bool wasActive = false;
955
956 switch (mStatus) {
957 case STATUS_ERROR:
958 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
959 return INVALID_OPERATION;
960 case STATUS_UNINITIALIZED:
961 ALOGE("%s: Device not initialized", __FUNCTION__);
962 return INVALID_OPERATION;
963 case STATUS_UNCONFIGURED:
964 case STATUS_CONFIGURED:
965 // OK
966 break;
967 case STATUS_ACTIVE:
968 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
969 res = internalPauseAndWaitLocked(maxExpectedDuration,
970 /*requestThreadInvocation*/ false);
971 if (res != OK) {
972 SET_ERR_L("Can't pause captures to reconfigure streams!");
973 return res;
974 }
975 wasActive = true;
976 break;
977 default:
978 SET_ERR_L("%s: Unexpected status: %d", mStatus);
979 return INVALID_OPERATION;
980 }
981 assert(mStatus != STATUS_ACTIVE);
982
983 if (mInputStream != 0) {
984 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
985 return INVALID_OPERATION;
986 }
987
988 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
989 width, height, format);
990 newStream->setStatusTracker(mStatusTracker);
991
992 mInputStream = newStream;
993 mIsInputStreamMultiResolution = isMultiResolution;
994
995 *id = mNextStreamId++;
996
997 // Continue captures if active at start
998 if (wasActive) {
999 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1000 // Reuse current operating mode and session parameters for new stream config
1001 res = configureStreamsLocked(mOperatingMode, mSessionParams);
1002 if (res != OK) {
1003 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1004 __FUNCTION__, mNextStreamId, strerror(-res), res);
1005 return res;
1006 }
1007 internalResumeLocked();
1008 }
1009
1010 ALOGV("Camera %s: Created input stream", mId.string());
1011 return OK;
1012 }
1013
createStream(sp<Surface> consumer,uint32_t width,uint32_t height,int format,android_dataspace dataSpace,camera_stream_rotation_t rotation,int * id,const String8 & physicalCameraId,const std::unordered_set<int32_t> & sensorPixelModesUsed,std::vector<int> * surfaceIds,int streamSetId,bool isShared,bool isMultiResolution,uint64_t consumerUsage,int64_t dynamicRangeProfile,int64_t streamUseCase,int timestampBase,int mirrorMode,int32_t colorSpace,bool useReadoutTimestamp)1014 status_t Camera3Device::createStream(sp<Surface> consumer,
1015 uint32_t width, uint32_t height, int format,
1016 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
1017 const String8& physicalCameraId,
1018 const std::unordered_set<int32_t> &sensorPixelModesUsed,
1019 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
1020 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
1021 int timestampBase, int mirrorMode, int32_t colorSpace, bool useReadoutTimestamp) {
1022 ATRACE_CALL();
1023
1024 if (consumer == nullptr) {
1025 ALOGE("%s: consumer must not be null", __FUNCTION__);
1026 return BAD_VALUE;
1027 }
1028
1029 std::vector<sp<Surface>> consumers;
1030 consumers.push_back(consumer);
1031
1032 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
1033 format, dataSpace, rotation, id, physicalCameraId, sensorPixelModesUsed, surfaceIds,
1034 streamSetId, isShared, isMultiResolution, consumerUsage, dynamicRangeProfile,
1035 streamUseCase, timestampBase, mirrorMode, colorSpace, useReadoutTimestamp);
1036 }
1037
isRawFormat(int format)1038 static bool isRawFormat(int format) {
1039 switch (format) {
1040 case HAL_PIXEL_FORMAT_RAW16:
1041 case HAL_PIXEL_FORMAT_RAW12:
1042 case HAL_PIXEL_FORMAT_RAW10:
1043 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
1044 return true;
1045 default:
1046 return false;
1047 }
1048 }
1049
createStream(const std::vector<sp<Surface>> & consumers,bool hasDeferredConsumer,uint32_t width,uint32_t height,int format,android_dataspace dataSpace,camera_stream_rotation_t rotation,int * id,const String8 & physicalCameraId,const std::unordered_set<int32_t> & sensorPixelModesUsed,std::vector<int> * surfaceIds,int streamSetId,bool isShared,bool isMultiResolution,uint64_t consumerUsage,int64_t dynamicRangeProfile,int64_t streamUseCase,int timestampBase,int mirrorMode,int32_t colorSpace,bool useReadoutTimestamp)1050 status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1051 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
1052 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
1053 const String8& physicalCameraId, const std::unordered_set<int32_t> &sensorPixelModesUsed,
1054 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
1055 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
1056 int timestampBase, int mirrorMode, int32_t colorSpace, bool useReadoutTimestamp) {
1057 ATRACE_CALL();
1058
1059 Mutex::Autolock il(mInterfaceLock);
1060 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
1061 Mutex::Autolock l(mLock);
1062 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
1063 " consumer usage %" PRIu64 ", isShared %d, physicalCameraId %s, isMultiResolution %d"
1064 " dynamicRangeProfile 0x%" PRIx64 ", streamUseCase %" PRId64 ", timestampBase %d,"
1065 " mirrorMode %d, colorSpace %d, useReadoutTimestamp %d",
1066 mId.string(), mNextStreamId, width, height, format, dataSpace, rotation,
1067 consumerUsage, isShared, physicalCameraId.string(), isMultiResolution,
1068 dynamicRangeProfile, streamUseCase, timestampBase, mirrorMode, colorSpace,
1069 useReadoutTimestamp);
1070
1071 status_t res;
1072 bool wasActive = false;
1073
1074 switch (mStatus) {
1075 case STATUS_ERROR:
1076 CLOGE("Device has encountered a serious error");
1077 return INVALID_OPERATION;
1078 case STATUS_UNINITIALIZED:
1079 CLOGE("Device not initialized");
1080 return INVALID_OPERATION;
1081 case STATUS_UNCONFIGURED:
1082 case STATUS_CONFIGURED:
1083 // OK
1084 break;
1085 case STATUS_ACTIVE:
1086 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
1087 res = internalPauseAndWaitLocked(maxExpectedDuration,
1088 /*requestThreadInvocation*/ false);
1089 if (res != OK) {
1090 SET_ERR_L("Can't pause captures to reconfigure streams!");
1091 return res;
1092 }
1093 wasActive = true;
1094 break;
1095 default:
1096 SET_ERR_L("Unexpected status: %d", mStatus);
1097 return INVALID_OPERATION;
1098 }
1099 assert(mStatus != STATUS_ACTIVE);
1100
1101 sp<Camera3OutputStream> newStream;
1102
1103 if (consumers.size() == 0 && !hasDeferredConsumer) {
1104 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1105 return BAD_VALUE;
1106 }
1107
1108 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
1109 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1110 return BAD_VALUE;
1111 }
1112
1113 if (isRawFormat(format) && sensorPixelModesUsed.size() > 1) {
1114 // We can't use one stream with a raw format in both sensor pixel modes since its going to
1115 // be found in only one sensor pixel mode.
1116 ALOGE("%s: RAW opaque stream cannot be used with > 1 sensor pixel modes", __FUNCTION__);
1117 return BAD_VALUE;
1118 }
1119 IPCTransport transport = getTransportType();
1120 if (format == HAL_PIXEL_FORMAT_BLOB) {
1121 ssize_t blobBufferSize;
1122 if (dataSpace == HAL_DATASPACE_DEPTH) {
1123 blobBufferSize = getPointCloudBufferSize(infoPhysical(physicalCameraId));
1124 if (blobBufferSize <= 0) {
1125 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1126 return BAD_VALUE;
1127 }
1128 } else if (dataSpace == static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
1129 blobBufferSize = width * height;
1130 } else {
1131 blobBufferSize = getJpegBufferSize(infoPhysical(physicalCameraId), width, height);
1132 if (blobBufferSize <= 0) {
1133 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1134 return BAD_VALUE;
1135 }
1136 }
1137 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
1138 width, height, blobBufferSize, format, dataSpace, rotation,
1139 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
1140 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
1141 timestampBase, mirrorMode, colorSpace, useReadoutTimestamp);
1142 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1143 bool maxResolution =
1144 sensorPixelModesUsed.find(ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) !=
1145 sensorPixelModesUsed.end();
1146 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(infoPhysical(physicalCameraId), width,
1147 height, maxResolution);
1148 if (rawOpaqueBufferSize <= 0) {
1149 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1150 return BAD_VALUE;
1151 }
1152 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
1153 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
1154 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
1155 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
1156 timestampBase, mirrorMode, colorSpace, useReadoutTimestamp);
1157 } else if (isShared) {
1158 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1159 width, height, format, consumerUsage, dataSpace, rotation,
1160 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
1161 mUseHalBufManager, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
1162 timestampBase, mirrorMode, colorSpace, useReadoutTimestamp);
1163 } else if (consumers.size() == 0 && hasDeferredConsumer) {
1164 newStream = new Camera3OutputStream(mNextStreamId,
1165 width, height, format, consumerUsage, dataSpace, rotation,
1166 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
1167 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
1168 timestampBase, mirrorMode, colorSpace, useReadoutTimestamp);
1169 } else {
1170 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
1171 width, height, format, dataSpace, rotation,
1172 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
1173 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
1174 timestampBase, mirrorMode, colorSpace, useReadoutTimestamp);
1175 }
1176
1177 size_t consumerCount = consumers.size();
1178 for (size_t i = 0; i < consumerCount; i++) {
1179 int id = newStream->getSurfaceId(consumers[i]);
1180 if (id < 0) {
1181 SET_ERR_L("Invalid surface id");
1182 return BAD_VALUE;
1183 }
1184 if (surfaceIds != nullptr) {
1185 surfaceIds->push_back(id);
1186 }
1187 }
1188
1189 newStream->setStatusTracker(mStatusTracker);
1190
1191 newStream->setBufferManager(mBufferManager);
1192
1193 newStream->setImageDumpMask(mImageDumpMask);
1194
1195 res = mOutputStreams.add(mNextStreamId, newStream);
1196 if (res < 0) {
1197 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
1198 return res;
1199 }
1200
1201 mSessionStatsBuilder.addStream(mNextStreamId);
1202
1203 *id = mNextStreamId++;
1204 mNeedConfig = true;
1205
1206 // Continue captures if active at start
1207 if (wasActive) {
1208 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
1209 // Reuse current operating mode and session parameters for new stream config
1210 res = configureStreamsLocked(mOperatingMode, mSessionParams);
1211 if (res != OK) {
1212 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1213 mNextStreamId, strerror(-res), res);
1214 return res;
1215 }
1216 internalResumeLocked();
1217 }
1218 ALOGV("Camera %s: Created new stream", mId.string());
1219 return OK;
1220 }
1221
getStreamInfo(int id,StreamInfo * streamInfo)1222 status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
1223 ATRACE_CALL();
1224 if (nullptr == streamInfo) {
1225 return BAD_VALUE;
1226 }
1227 Mutex::Autolock il(mInterfaceLock);
1228 Mutex::Autolock l(mLock);
1229
1230 switch (mStatus) {
1231 case STATUS_ERROR:
1232 CLOGE("Device has encountered a serious error");
1233 return INVALID_OPERATION;
1234 case STATUS_UNINITIALIZED:
1235 CLOGE("Device not initialized!");
1236 return INVALID_OPERATION;
1237 case STATUS_UNCONFIGURED:
1238 case STATUS_CONFIGURED:
1239 case STATUS_ACTIVE:
1240 // OK
1241 break;
1242 default:
1243 SET_ERR_L("Unexpected status: %d", mStatus);
1244 return INVALID_OPERATION;
1245 }
1246
1247 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
1248 if (stream == nullptr) {
1249 CLOGE("Stream %d is unknown", id);
1250 return BAD_VALUE;
1251 }
1252
1253 streamInfo->width = stream->getWidth();
1254 streamInfo->height = stream->getHeight();
1255 streamInfo->format = stream->getFormat();
1256 streamInfo->dataSpace = stream->getDataSpace();
1257 streamInfo->formatOverridden = stream->isFormatOverridden();
1258 streamInfo->originalFormat = stream->getOriginalFormat();
1259 streamInfo->dataSpaceOverridden = stream->isDataSpaceOverridden();
1260 streamInfo->originalDataSpace = stream->getOriginalDataSpace();
1261 streamInfo->dynamicRangeProfile = stream->getDynamicRangeProfile();
1262 streamInfo->colorSpace = stream->getColorSpace();
1263 return OK;
1264 }
1265
setStreamTransform(int id,int transform)1266 status_t Camera3Device::setStreamTransform(int id,
1267 int transform) {
1268 ATRACE_CALL();
1269 Mutex::Autolock il(mInterfaceLock);
1270 Mutex::Autolock l(mLock);
1271
1272 switch (mStatus) {
1273 case STATUS_ERROR:
1274 CLOGE("Device has encountered a serious error");
1275 return INVALID_OPERATION;
1276 case STATUS_UNINITIALIZED:
1277 CLOGE("Device not initialized");
1278 return INVALID_OPERATION;
1279 case STATUS_UNCONFIGURED:
1280 case STATUS_CONFIGURED:
1281 case STATUS_ACTIVE:
1282 // OK
1283 break;
1284 default:
1285 SET_ERR_L("Unexpected status: %d", mStatus);
1286 return INVALID_OPERATION;
1287 }
1288
1289 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
1290 if (stream == nullptr) {
1291 CLOGE("Stream %d does not exist", id);
1292 return BAD_VALUE;
1293 }
1294 return stream->setTransform(transform, false /*mayChangeMirror*/);
1295 }
1296
deleteStream(int id)1297 status_t Camera3Device::deleteStream(int id) {
1298 ATRACE_CALL();
1299 Mutex::Autolock il(mInterfaceLock);
1300 Mutex::Autolock l(mLock);
1301 status_t res;
1302
1303 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
1304
1305 // CameraDevice semantics require device to already be idle before
1306 // deleteStream is called, unlike for createStream.
1307 if (mStatus == STATUS_ACTIVE) {
1308 ALOGW("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
1309 return -EBUSY;
1310 }
1311
1312 if (mStatus == STATUS_ERROR) {
1313 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1314 __FUNCTION__, mId.string());
1315 return -EBUSY;
1316 }
1317
1318 sp<Camera3StreamInterface> deletedStream;
1319 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
1320 if (mInputStream != NULL && id == mInputStream->getId()) {
1321 deletedStream = mInputStream;
1322 mInputStream.clear();
1323 } else {
1324 if (stream == nullptr) {
1325 CLOGE("Stream %d does not exist", id);
1326 return BAD_VALUE;
1327 }
1328 mSessionStatsBuilder.removeStream(id);
1329 }
1330
1331 // Delete output stream or the output part of a bi-directional stream.
1332 if (stream != nullptr) {
1333 deletedStream = stream;
1334 mOutputStreams.remove(id);
1335 }
1336
1337 // Free up the stream endpoint so that it can be used by some other stream
1338 res = deletedStream->disconnect();
1339 if (res != OK) {
1340 SET_ERR_L("Can't disconnect deleted stream %d", id);
1341 // fall through since we want to still list the stream as deleted.
1342 }
1343 mDeletedStreams.add(deletedStream);
1344 mNeedConfig = true;
1345
1346 return res;
1347 }
1348
configureStreams(const CameraMetadata & sessionParams,int operatingMode)1349 status_t Camera3Device::configureStreams(const CameraMetadata& sessionParams, int operatingMode) {
1350 ATRACE_CALL();
1351 ALOGV("%s: E", __FUNCTION__);
1352
1353 Mutex::Autolock il(mInterfaceLock);
1354 Mutex::Autolock l(mLock);
1355
1356 // In case the client doesn't include any session parameter, try a
1357 // speculative configuration using the values from the last cached
1358 // default request.
1359 if (sessionParams.isEmpty() &&
1360 ((mLastTemplateId > 0) && (mLastTemplateId < CAMERA_TEMPLATE_COUNT)) &&
1361 (!mRequestTemplateCache[mLastTemplateId].isEmpty())) {
1362 ALOGV("%s: Speculative session param configuration with template id: %d", __func__,
1363 mLastTemplateId);
1364 return filterParamsAndConfigureLocked(mRequestTemplateCache[mLastTemplateId],
1365 operatingMode);
1366 }
1367
1368 return filterParamsAndConfigureLocked(sessionParams, operatingMode);
1369 }
1370
filterParamsAndConfigureLocked(const CameraMetadata & sessionParams,int operatingMode)1371 status_t Camera3Device::filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
1372 int operatingMode) {
1373 //Filter out any incoming session parameters
1374 const CameraMetadata params(sessionParams);
1375 camera_metadata_entry_t availableSessionKeys = mDeviceInfo.find(
1376 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
1377 CameraMetadata filteredParams(availableSessionKeys.count);
1378 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
1379 filteredParams.getAndLock());
1380 set_camera_metadata_vendor_id(meta, mVendorTagId);
1381 filteredParams.unlock(meta);
1382 if (availableSessionKeys.count > 0) {
1383 bool rotateAndCropSessionKey = false;
1384 bool autoframingSessionKey = false;
1385 for (size_t i = 0; i < availableSessionKeys.count; i++) {
1386 camera_metadata_ro_entry entry = params.find(
1387 availableSessionKeys.data.i32[i]);
1388 if (entry.count > 0) {
1389 filteredParams.update(entry);
1390 }
1391 if (ANDROID_SCALER_ROTATE_AND_CROP == availableSessionKeys.data.i32[i]) {
1392 rotateAndCropSessionKey = true;
1393 }
1394 if (ANDROID_CONTROL_AUTOFRAMING == availableSessionKeys.data.i32[i]) {
1395 autoframingSessionKey = true;
1396 }
1397 }
1398
1399 if (rotateAndCropSessionKey || autoframingSessionKey) {
1400 sp<CaptureRequest> request = new CaptureRequest();
1401 PhysicalCameraSettings settingsList;
1402 settingsList.metadata = filteredParams;
1403 request->mSettingsList.push_back(settingsList);
1404
1405 if (rotateAndCropSessionKey) {
1406 auto rotateAndCropEntry = filteredParams.find(ANDROID_SCALER_ROTATE_AND_CROP);
1407 if (rotateAndCropEntry.count > 0 &&
1408 rotateAndCropEntry.data.u8[0] == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
1409 request->mRotateAndCropAuto = true;
1410 } else {
1411 request->mRotateAndCropAuto = false;
1412 }
1413
1414 overrideAutoRotateAndCrop(request, mOverrideToPortrait, mRotateAndCropOverride);
1415 }
1416
1417 if (autoframingSessionKey) {
1418 auto autoframingEntry = filteredParams.find(ANDROID_CONTROL_AUTOFRAMING);
1419 if (autoframingEntry.count > 0 &&
1420 autoframingEntry.data.u8[0] == ANDROID_CONTROL_AUTOFRAMING_AUTO) {
1421 overrideAutoframing(request, mAutoframingOverride);
1422 }
1423 }
1424
1425 filteredParams = request->mSettingsList.begin()->metadata;
1426 }
1427 }
1428
1429 return configureStreamsLocked(operatingMode, filteredParams);
1430 }
1431
getInputBufferProducer(sp<IGraphicBufferProducer> * producer)1432 status_t Camera3Device::getInputBufferProducer(
1433 sp<IGraphicBufferProducer> *producer) {
1434 ATRACE_CALL();
1435 Mutex::Autolock il(mInterfaceLock);
1436 Mutex::Autolock l(mLock);
1437
1438 if (producer == NULL) {
1439 return BAD_VALUE;
1440 } else if (mInputStream == NULL) {
1441 return INVALID_OPERATION;
1442 }
1443
1444 return mInputStream->getInputBufferProducer(producer);
1445 }
1446
createDefaultRequest(camera_request_template_t templateId,CameraMetadata * request)1447 status_t Camera3Device::createDefaultRequest(camera_request_template_t templateId,
1448 CameraMetadata *request) {
1449 ATRACE_CALL();
1450 ALOGV("%s: for template %d", __FUNCTION__, templateId);
1451
1452 if (templateId <= 0 || templateId >= CAMERA_TEMPLATE_COUNT) {
1453 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
1454 CameraThreadState::getCallingUid(), nullptr, 0);
1455 return BAD_VALUE;
1456 }
1457
1458 Mutex::Autolock il(mInterfaceLock);
1459
1460 {
1461 Mutex::Autolock l(mLock);
1462 switch (mStatus) {
1463 case STATUS_ERROR:
1464 CLOGE("Device has encountered a serious error");
1465 return INVALID_OPERATION;
1466 case STATUS_UNINITIALIZED:
1467 CLOGE("Device is not initialized!");
1468 return INVALID_OPERATION;
1469 case STATUS_UNCONFIGURED:
1470 case STATUS_CONFIGURED:
1471 case STATUS_ACTIVE:
1472 // OK
1473 break;
1474 default:
1475 SET_ERR_L("Unexpected status: %d", mStatus);
1476 return INVALID_OPERATION;
1477 }
1478
1479 if (!mRequestTemplateCache[templateId].isEmpty()) {
1480 *request = mRequestTemplateCache[templateId];
1481 mLastTemplateId = templateId;
1482 return OK;
1483 }
1484 }
1485
1486 camera_metadata_t *rawRequest;
1487 status_t res = mInterface->constructDefaultRequestSettings(
1488 (camera_request_template_t) templateId, &rawRequest);
1489
1490 {
1491 Mutex::Autolock l(mLock);
1492 if (res == BAD_VALUE) {
1493 ALOGI("%s: template %d is not supported on this camera device",
1494 __FUNCTION__, templateId);
1495 return res;
1496 } else if (res != OK) {
1497 CLOGE("Unable to construct request template %d: %s (%d)",
1498 templateId, strerror(-res), res);
1499 return res;
1500 }
1501
1502 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
1503 mRequestTemplateCache[templateId].acquire(rawRequest);
1504
1505 // Override the template request with zoomRatioMapper
1506 res = mZoomRatioMappers[mId.c_str()].initZoomRatioInTemplate(
1507 &mRequestTemplateCache[templateId]);
1508 if (res != OK) {
1509 CLOGE("Failed to update zoom ratio for template %d: %s (%d)",
1510 templateId, strerror(-res), res);
1511 return res;
1512 }
1513
1514 // Fill in JPEG_QUALITY if not available
1515 if (!mRequestTemplateCache[templateId].exists(ANDROID_JPEG_QUALITY)) {
1516 static const uint8_t kDefaultJpegQuality = 95;
1517 mRequestTemplateCache[templateId].update(ANDROID_JPEG_QUALITY,
1518 &kDefaultJpegQuality, 1);
1519 }
1520
1521 // Fill in AUTOFRAMING if not available
1522 if (!mRequestTemplateCache[templateId].exists(ANDROID_CONTROL_AUTOFRAMING)) {
1523 static const uint8_t kDefaultAutoframingMode = ANDROID_CONTROL_AUTOFRAMING_OFF;
1524 mRequestTemplateCache[templateId].update(ANDROID_CONTROL_AUTOFRAMING,
1525 &kDefaultAutoframingMode, 1);
1526 }
1527
1528 *request = mRequestTemplateCache[templateId];
1529 mLastTemplateId = templateId;
1530 }
1531 return OK;
1532 }
1533
waitUntilDrained()1534 status_t Camera3Device::waitUntilDrained() {
1535 ATRACE_CALL();
1536 Mutex::Autolock il(mInterfaceLock);
1537 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
1538 Mutex::Autolock l(mLock);
1539
1540 return waitUntilDrainedLocked(maxExpectedDuration);
1541 }
1542
waitUntilDrainedLocked(nsecs_t maxExpectedDuration)1543 status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
1544 switch (mStatus) {
1545 case STATUS_UNINITIALIZED:
1546 case STATUS_UNCONFIGURED:
1547 ALOGV("%s: Already idle", __FUNCTION__);
1548 return OK;
1549 case STATUS_CONFIGURED:
1550 // To avoid race conditions, check with tracker to be sure
1551 case STATUS_ERROR:
1552 case STATUS_ACTIVE:
1553 // Need to verify shut down
1554 break;
1555 default:
1556 SET_ERR_L("Unexpected status: %d",mStatus);
1557 return INVALID_OPERATION;
1558 }
1559 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
1560 maxExpectedDuration);
1561 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration,
1562 /*requestThreadInvocation*/ false);
1563 if (res != OK) {
1564 mStatusTracker->dumpActiveComponents();
1565 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1566 res);
1567 }
1568 return res;
1569 }
1570
internalUpdateStatusLocked(Status status)1571 void Camera3Device::internalUpdateStatusLocked(Status status) {
1572 mStatus = status;
1573 mStatusIsInternal = mPauseStateNotify ? true : false;
1574 mRecentStatusUpdates.add({mStatus, mStatusIsInternal});
1575 mStatusChanged.broadcast();
1576 }
1577
1578 // Pause to reconfigure
internalPauseAndWaitLocked(nsecs_t maxExpectedDuration,bool requestThreadInvocation)1579 status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration,
1580 bool requestThreadInvocation) {
1581 if (mRequestThread.get() != nullptr) {
1582 mRequestThread->setPaused(true);
1583 } else {
1584 return NO_INIT;
1585 }
1586
1587 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1588 maxExpectedDuration);
1589 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration,
1590 requestThreadInvocation);
1591 if (res != OK) {
1592 mStatusTracker->dumpActiveComponents();
1593 SET_ERR_L("Can't idle device in %f seconds!",
1594 maxExpectedDuration/1e9);
1595 }
1596
1597 return res;
1598 }
1599
1600 // Resume after internalPauseAndWaitLocked
internalResumeLocked()1601 status_t Camera3Device::internalResumeLocked() {
1602 status_t res;
1603
1604 mRequestThread->setPaused(false);
1605
1606 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1607 kActiveTimeout);
1608 // internalResumeLocked is always called from a binder thread.
1609 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout,
1610 /*requestThreadInvocation*/ false);
1611 if (res != OK) {
1612 SET_ERR_L("Can't transition to active in %f seconds!",
1613 kActiveTimeout/1e9);
1614 }
1615 mPauseStateNotify = false;
1616 return OK;
1617 }
1618
waitUntilStateThenRelock(bool active,nsecs_t timeout,bool requestThreadInvocation)1619 status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout,
1620 bool requestThreadInvocation) {
1621 status_t res = OK;
1622
1623 size_t startIndex = 0;
1624 if (mStatusWaiters == 0) {
1625 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1626 // this status list
1627 mRecentStatusUpdates.clear();
1628 } else {
1629 // If other threads are waiting on updates to this status list, set the position of the
1630 // first element that this list will check rather than clearing the list.
1631 startIndex = mRecentStatusUpdates.size();
1632 }
1633
1634 mStatusWaiters++;
1635
1636 bool signalPipelineDrain = false;
1637 if (!active && mUseHalBufManager) {
1638 auto streamIds = mOutputStreams.getStreamIds();
1639 if (mStatus == STATUS_ACTIVE) {
1640 mRequestThread->signalPipelineDrain(streamIds);
1641 signalPipelineDrain = true;
1642 }
1643 mRequestBufferSM.onWaitUntilIdle();
1644 }
1645
1646 bool stateSeen = false;
1647 nsecs_t startTime = systemTime();
1648 do {
1649 if (mStatus == STATUS_ERROR) {
1650 // Device in error state. Return right away.
1651 break;
1652 }
1653 if (active == (mStatus == STATUS_ACTIVE) &&
1654 (requestThreadInvocation || !mStatusIsInternal)) {
1655 // Desired state is current
1656 break;
1657 }
1658
1659 nsecs_t timeElapsed = systemTime() - startTime;
1660 nsecs_t timeToWait = timeout - timeElapsed;
1661 if (timeToWait <= 0) {
1662 // Thread woke up spuriously but has timed out since.
1663 // Force out of loop with TIMED_OUT result.
1664 res = TIMED_OUT;
1665 break;
1666 }
1667 res = mStatusChanged.waitRelative(mLock, timeToWait);
1668 if (res != OK) break;
1669
1670 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1671 // transitions.
1672 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1673 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1674 __FUNCTION__);
1675
1676 // Encountered desired state since we began waiting. Internal invocations coming from
1677 // request threads (such as reconfigureCamera) should be woken up immediately, whereas
1678 // invocations from binder threads (such as createInputStream) should only be woken up if
1679 // they are not paused. This avoids intermediate pause signals from reconfigureCamera as it
1680 // changes the status to active right after.
1681 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
1682 if (mRecentStatusUpdates[i].status == STATUS_ERROR) {
1683 // Device in error state. Return right away.
1684 stateSeen = true;
1685 break;
1686 }
1687 if (active == (mRecentStatusUpdates[i].status == STATUS_ACTIVE) &&
1688 (requestThreadInvocation || !mRecentStatusUpdates[i].isInternal)) {
1689 stateSeen = true;
1690 break;
1691 }
1692 }
1693 } while (!stateSeen);
1694
1695 if (signalPipelineDrain) {
1696 mRequestThread->resetPipelineDrain();
1697 }
1698
1699 mStatusWaiters--;
1700
1701 return res;
1702 }
1703
1704
setNotifyCallback(wp<NotificationListener> listener)1705 status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
1706 ATRACE_CALL();
1707 std::lock_guard<std::mutex> l(mOutputLock);
1708
1709 if (listener != NULL && mListener != NULL) {
1710 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1711 }
1712 mListener = listener;
1713 mRequestThread->setNotificationListener(listener);
1714 mPreparerThread->setNotificationListener(listener);
1715
1716 return OK;
1717 }
1718
willNotify3A()1719 bool Camera3Device::willNotify3A() {
1720 return false;
1721 }
1722
waitForNextFrame(nsecs_t timeout)1723 status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
1724 ATRACE_CALL();
1725 std::unique_lock<std::mutex> l(mOutputLock);
1726
1727 while (mResultQueue.empty()) {
1728 auto st = mResultSignal.wait_for(l, std::chrono::nanoseconds(timeout));
1729 if (st == std::cv_status::timeout) {
1730 return TIMED_OUT;
1731 }
1732 }
1733 return OK;
1734 }
1735
getNextResult(CaptureResult * frame)1736 status_t Camera3Device::getNextResult(CaptureResult *frame) {
1737 ATRACE_CALL();
1738 std::lock_guard<std::mutex> l(mOutputLock);
1739
1740 if (mResultQueue.empty()) {
1741 return NOT_ENOUGH_DATA;
1742 }
1743
1744 if (frame == NULL) {
1745 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1746 return BAD_VALUE;
1747 }
1748
1749 CaptureResult &result = *(mResultQueue.begin());
1750 frame->mResultExtras = result.mResultExtras;
1751 frame->mMetadata.acquire(result.mMetadata);
1752 frame->mPhysicalMetadatas = std::move(result.mPhysicalMetadatas);
1753 mResultQueue.erase(mResultQueue.begin());
1754
1755 return OK;
1756 }
1757
triggerAutofocus(uint32_t id)1758 status_t Camera3Device::triggerAutofocus(uint32_t id) {
1759 ATRACE_CALL();
1760 Mutex::Autolock il(mInterfaceLock);
1761
1762 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1763 // Mix-in this trigger into the next request and only the next request.
1764 RequestTrigger trigger[] = {
1765 {
1766 ANDROID_CONTROL_AF_TRIGGER,
1767 ANDROID_CONTROL_AF_TRIGGER_START
1768 },
1769 {
1770 ANDROID_CONTROL_AF_TRIGGER_ID,
1771 static_cast<int32_t>(id)
1772 }
1773 };
1774
1775 return mRequestThread->queueTrigger(trigger,
1776 sizeof(trigger)/sizeof(trigger[0]));
1777 }
1778
triggerCancelAutofocus(uint32_t id)1779 status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1780 ATRACE_CALL();
1781 Mutex::Autolock il(mInterfaceLock);
1782
1783 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1784 // Mix-in this trigger into the next request and only the next request.
1785 RequestTrigger trigger[] = {
1786 {
1787 ANDROID_CONTROL_AF_TRIGGER,
1788 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1789 },
1790 {
1791 ANDROID_CONTROL_AF_TRIGGER_ID,
1792 static_cast<int32_t>(id)
1793 }
1794 };
1795
1796 return mRequestThread->queueTrigger(trigger,
1797 sizeof(trigger)/sizeof(trigger[0]));
1798 }
1799
triggerPrecaptureMetering(uint32_t id)1800 status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1801 ATRACE_CALL();
1802 Mutex::Autolock il(mInterfaceLock);
1803
1804 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1805 // Mix-in this trigger into the next request and only the next request.
1806 RequestTrigger trigger[] = {
1807 {
1808 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1809 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1810 },
1811 {
1812 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1813 static_cast<int32_t>(id)
1814 }
1815 };
1816
1817 return mRequestThread->queueTrigger(trigger,
1818 sizeof(trigger)/sizeof(trigger[0]));
1819 }
1820
flush(int64_t * frameNumber)1821 status_t Camera3Device::flush(int64_t *frameNumber) {
1822 ATRACE_CALL();
1823 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
1824 Mutex::Autolock il(mInterfaceLock);
1825
1826 {
1827 Mutex::Autolock l(mLock);
1828
1829 // b/116514106 "disconnect()" can get called twice for the same device. The
1830 // camera device will not be initialized during the second run.
1831 if (mStatus == STATUS_UNINITIALIZED) {
1832 return OK;
1833 }
1834
1835 mRequestThread->clear(/*out*/frameNumber);
1836
1837 // Stop session and stream counter
1838 mSessionStatsBuilder.stopCounter();
1839 }
1840
1841 // Calculate expected duration for flush with additional buffer time in ms for watchdog
1842 uint64_t maxExpectedDuration = ns2ms(getExpectedInFlightDuration() + kBaseGetBufferWait);
1843 status_t res = mCameraServiceWatchdog->WATCH_CUSTOM_TIMER(mRequestThread->flush(),
1844 maxExpectedDuration / kCycleLengthMs, kCycleLengthMs);
1845
1846 return res;
1847 }
1848
prepare(int streamId)1849 status_t Camera3Device::prepare(int streamId) {
1850 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1851 }
1852
prepare(int maxCount,int streamId)1853 status_t Camera3Device::prepare(int maxCount, int streamId) {
1854 ATRACE_CALL();
1855 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
1856 Mutex::Autolock il(mInterfaceLock);
1857 Mutex::Autolock l(mLock);
1858
1859 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1860 if (stream == nullptr) {
1861 CLOGE("Stream %d does not exist", streamId);
1862 return BAD_VALUE;
1863 }
1864
1865 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
1866 CLOGE("Stream %d has already been a request target", streamId);
1867 return BAD_VALUE;
1868 }
1869
1870 if (mRequestThread->isStreamPending(stream)) {
1871 CLOGE("Stream %d is already a target in a pending request", streamId);
1872 return BAD_VALUE;
1873 }
1874
1875 return mPreparerThread->prepare(maxCount, stream);
1876 }
1877
tearDown(int streamId)1878 status_t Camera3Device::tearDown(int streamId) {
1879 ATRACE_CALL();
1880 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
1881 Mutex::Autolock il(mInterfaceLock);
1882 Mutex::Autolock l(mLock);
1883
1884 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1885 if (stream == nullptr) {
1886 CLOGE("Stream %d does not exist", streamId);
1887 return BAD_VALUE;
1888 }
1889
1890 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1891 CLOGE("Stream %d is a target of a in-progress request", streamId);
1892 return BAD_VALUE;
1893 }
1894
1895 return stream->tearDown();
1896 }
1897
addBufferListenerForStream(int streamId,wp<Camera3StreamBufferListener> listener)1898 status_t Camera3Device::addBufferListenerForStream(int streamId,
1899 wp<Camera3StreamBufferListener> listener) {
1900 ATRACE_CALL();
1901 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
1902 Mutex::Autolock il(mInterfaceLock);
1903 Mutex::Autolock l(mLock);
1904
1905 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1906 if (stream == nullptr) {
1907 CLOGE("Stream %d does not exist", streamId);
1908 return BAD_VALUE;
1909 }
1910 stream->addBufferListener(listener);
1911
1912 return OK;
1913 }
1914
getMaxPreviewFps(sp<camera3::Camera3OutputStreamInterface> stream)1915 float Camera3Device::getMaxPreviewFps(sp<camera3::Camera3OutputStreamInterface> stream) {
1916 camera_metadata_entry minDurations =
1917 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS);
1918 for (size_t i = 0; i < minDurations.count; i += 4) {
1919 if (minDurations.data.i64[i] == stream->getOriginalFormat()
1920 && minDurations.data.i64[i+1] == stream->getWidth()
1921 && minDurations.data.i64[i+2] == stream->getHeight()) {
1922 int64_t minFrameDuration = minDurations.data.i64[i+3];
1923 return 1e9f / minFrameDuration;
1924 }
1925 }
1926 return 0.0f;
1927 }
1928
1929 /**
1930 * Methods called by subclasses
1931 */
1932
notifyStatus(bool idle)1933 void Camera3Device::notifyStatus(bool idle) {
1934 ATRACE_CALL();
1935 std::vector<int> streamIds;
1936 std::vector<hardware::CameraStreamStats> streamStats;
1937 float sessionMaxPreviewFps = 0.0f;
1938
1939 {
1940 // Need mLock to safely update state and synchronize to current
1941 // state of methods in flight.
1942 Mutex::Autolock l(mLock);
1943 // We can get various system-idle notices from the status tracker
1944 // while starting up. Only care about them if we've actually sent
1945 // in some requests recently.
1946 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1947 return;
1948 }
1949 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
1950 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
1951 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
1952
1953 // Skip notifying listener if we're doing some user-transparent
1954 // state changes
1955 if (mPauseStateNotify) return;
1956
1957 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1958 auto stream = mOutputStreams[i];
1959 if (stream.get() == nullptr) continue;
1960
1961 float streamMaxPreviewFps = getMaxPreviewFps(stream);
1962 sessionMaxPreviewFps = std::max(sessionMaxPreviewFps, streamMaxPreviewFps);
1963
1964 // Populate stream statistics in case of Idle
1965 if (idle) {
1966 streamIds.push_back(stream->getId());
1967 Camera3Stream* camera3Stream = Camera3Stream::cast(stream->asHalStream());
1968 int64_t usage = 0LL;
1969 int64_t streamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
1970 if (camera3Stream != nullptr) {
1971 usage = camera3Stream->getUsage();
1972 streamUseCase = camera3Stream->getStreamUseCase();
1973 }
1974 streamStats.emplace_back(stream->getWidth(), stream->getHeight(),
1975 stream->getOriginalFormat(), streamMaxPreviewFps, stream->getDataSpace(), usage,
1976 stream->getMaxHalBuffers(),
1977 stream->getMaxTotalBuffers() - stream->getMaxHalBuffers(),
1978 stream->getDynamicRangeProfile(), streamUseCase,
1979 stream->getColorSpace());
1980 }
1981 }
1982 }
1983
1984 sp<NotificationListener> listener;
1985 {
1986 std::lock_guard<std::mutex> l(mOutputLock);
1987 listener = mListener.promote();
1988 }
1989 status_t res = OK;
1990 if (listener != nullptr) {
1991 if (idle) {
1992 // Get session stats from the builder, and notify the listener.
1993 int64_t requestCount, resultErrorCount;
1994 bool deviceError;
1995 std::map<int, StreamStats> streamStatsMap;
1996 mSessionStatsBuilder.buildAndReset(&requestCount, &resultErrorCount,
1997 &deviceError, &streamStatsMap);
1998 for (size_t i = 0; i < streamIds.size(); i++) {
1999 int streamId = streamIds[i];
2000 auto stats = streamStatsMap.find(streamId);
2001 if (stats != streamStatsMap.end()) {
2002 streamStats[i].mRequestCount = stats->second.mRequestedFrameCount;
2003 streamStats[i].mErrorCount = stats->second.mDroppedFrameCount;
2004 streamStats[i].mStartLatencyMs = stats->second.mStartLatencyMs;
2005 streamStats[i].mHistogramType =
2006 hardware::CameraStreamStats::HISTOGRAM_TYPE_CAPTURE_LATENCY;
2007 streamStats[i].mHistogramBins.assign(
2008 stats->second.mCaptureLatencyBins.begin(),
2009 stats->second.mCaptureLatencyBins.end());
2010 streamStats[i].mHistogramCounts.assign(
2011 stats->second.mCaptureLatencyHistogram.begin(),
2012 stats->second.mCaptureLatencyHistogram.end());
2013 }
2014 }
2015 listener->notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
2016 } else {
2017 res = listener->notifyActive(sessionMaxPreviewFps);
2018 }
2019 }
2020 if (res != OK) {
2021 SET_ERR("Camera access permission lost mid-operation: %s (%d)",
2022 strerror(-res), res);
2023 }
2024 }
2025
setConsumerSurfaces(int streamId,const std::vector<sp<Surface>> & consumers,std::vector<int> * surfaceIds)2026 status_t Camera3Device::setConsumerSurfaces(int streamId,
2027 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
2028 ATRACE_CALL();
2029 ALOGV("%s: Camera %s: set consumer surface for stream %d",
2030 __FUNCTION__, mId.string(), streamId);
2031
2032 if (surfaceIds == nullptr) {
2033 return BAD_VALUE;
2034 }
2035
2036 Mutex::Autolock il(mInterfaceLock);
2037 Mutex::Autolock l(mLock);
2038
2039 if (consumers.size() == 0) {
2040 CLOGE("No consumer is passed!");
2041 return BAD_VALUE;
2042 }
2043
2044 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2045 if (stream == nullptr) {
2046 CLOGE("Stream %d is unknown", streamId);
2047 return BAD_VALUE;
2048 }
2049
2050 // isConsumerConfigurationDeferred will be off after setConsumers
2051 bool isDeferred = stream->isConsumerConfigurationDeferred();
2052 status_t res = stream->setConsumers(consumers);
2053 if (res != OK) {
2054 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
2055 return res;
2056 }
2057
2058 for (auto &consumer : consumers) {
2059 int id = stream->getSurfaceId(consumer);
2060 if (id < 0) {
2061 CLOGE("Invalid surface id!");
2062 return BAD_VALUE;
2063 }
2064 surfaceIds->push_back(id);
2065 }
2066
2067 if (isDeferred) {
2068 if (!stream->isConfiguring()) {
2069 CLOGE("Stream %d was already fully configured.", streamId);
2070 return INVALID_OPERATION;
2071 }
2072
2073 res = stream->finishConfiguration();
2074 if (res != OK) {
2075 // If finishConfiguration fails due to abandoned surface, do not set
2076 // device to error state.
2077 bool isSurfaceAbandoned =
2078 (res == NO_INIT || res == DEAD_OBJECT) && stream->isAbandoned();
2079 if (!isSurfaceAbandoned) {
2080 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
2081 stream->getId(), strerror(-res), res);
2082 }
2083 return res;
2084 }
2085 }
2086
2087 return OK;
2088 }
2089
updateStream(int streamId,const std::vector<sp<Surface>> & newSurfaces,const std::vector<OutputStreamInfo> & outputInfo,const std::vector<size_t> & removedSurfaceIds,KeyedVector<sp<Surface>,size_t> * outputMap)2090 status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
2091 const std::vector<OutputStreamInfo> &outputInfo,
2092 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
2093 Mutex::Autolock il(mInterfaceLock);
2094 Mutex::Autolock l(mLock);
2095
2096 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2097 if (stream == nullptr) {
2098 CLOGE("Stream %d is unknown", streamId);
2099 return BAD_VALUE;
2100 }
2101
2102 for (const auto &it : removedSurfaceIds) {
2103 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2104 CLOGE("Shared surface still part of a pending request!");
2105 return -EBUSY;
2106 }
2107 }
2108
2109 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2110 if (res != OK) {
2111 CLOGE("Stream %d failed to update stream (error %d %s) ",
2112 streamId, res, strerror(-res));
2113 if (res == UNKNOWN_ERROR) {
2114 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2115 __FUNCTION__);
2116 }
2117 return res;
2118 }
2119
2120 return res;
2121 }
2122
dropStreamBuffers(bool dropping,int streamId)2123 status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2124 Mutex::Autolock il(mInterfaceLock);
2125 Mutex::Autolock l(mLock);
2126
2127 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2128 if (stream == nullptr) {
2129 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2130 return BAD_VALUE;
2131 }
2132
2133 if (dropping) {
2134 mSessionStatsBuilder.stopCounter(streamId);
2135 } else {
2136 mSessionStatsBuilder.startCounter(streamId);
2137 }
2138 return stream->dropBuffers(dropping);
2139 }
2140
2141 /**
2142 * Camera3Device private methods
2143 */
2144
createCaptureRequest(const PhysicalCameraSettingsList & request,const SurfaceMap & surfaceMap)2145 sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
2146 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
2147 ATRACE_CALL();
2148
2149 sp<CaptureRequest> newRequest = new CaptureRequest();
2150 newRequest->mSettingsList = request;
2151
2152 camera_metadata_entry_t inputStreams =
2153 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
2154 if (inputStreams.count > 0) {
2155 if (mInputStream == NULL ||
2156 mInputStream->getId() != inputStreams.data.i32[0]) {
2157 CLOGE("Request references unknown input stream %d",
2158 inputStreams.data.u8[0]);
2159 return NULL;
2160 }
2161
2162 if (mInputStream->isConfiguring()) {
2163 SET_ERR_L("%s: input stream %d is not configured!",
2164 __FUNCTION__, mInputStream->getId());
2165 return NULL;
2166 }
2167 // Check if stream prepare is blocking requests.
2168 if (mInputStream->isBlockedByPrepare()) {
2169 CLOGE("Request references an input stream that's being prepared!");
2170 return NULL;
2171 }
2172
2173 newRequest->mInputStream = mInputStream;
2174 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
2175 }
2176
2177 camera_metadata_entry_t streams =
2178 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
2179 if (streams.count == 0) {
2180 CLOGE("Zero output streams specified!");
2181 return NULL;
2182 }
2183
2184 for (size_t i = 0; i < streams.count; i++) {
2185 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streams.data.i32[i]);
2186 if (stream == nullptr) {
2187 CLOGE("Request references unknown stream %d",
2188 streams.data.i32[i]);
2189 return NULL;
2190 }
2191 // It is illegal to include a deferred consumer output stream into a request
2192 auto iter = surfaceMap.find(streams.data.i32[i]);
2193 if (iter != surfaceMap.end()) {
2194 const std::vector<size_t>& surfaces = iter->second;
2195 for (const auto& surface : surfaces) {
2196 if (stream->isConsumerConfigurationDeferred(surface)) {
2197 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2198 "due to deferred consumer", stream->getId(), surface);
2199 return NULL;
2200 }
2201 }
2202 newRequest->mOutputSurfaces[streams.data.i32[i]] = surfaces;
2203 }
2204
2205 if (stream->isConfiguring()) {
2206 SET_ERR_L("%s: stream %d is not configured!", __FUNCTION__, stream->getId());
2207 return NULL;
2208 }
2209 // Check if stream prepare is blocking requests.
2210 if (stream->isBlockedByPrepare()) {
2211 CLOGE("Request references an output stream that's being prepared!");
2212 return NULL;
2213 }
2214
2215 newRequest->mOutputStreams.push(stream);
2216 }
2217 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
2218 newRequest->mBatchSize = 1;
2219
2220 auto rotateAndCropEntry =
2221 newRequest->mSettingsList.begin()->metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
2222 if (rotateAndCropEntry.count > 0 &&
2223 rotateAndCropEntry.data.u8[0] == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
2224 newRequest->mRotateAndCropAuto = true;
2225 } else {
2226 newRequest->mRotateAndCropAuto = false;
2227 }
2228
2229 auto autoframingEntry =
2230 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_AUTOFRAMING);
2231 if (autoframingEntry.count > 0 &&
2232 autoframingEntry.data.u8[0] == ANDROID_CONTROL_AUTOFRAMING_AUTO) {
2233 newRequest->mAutoframingAuto = true;
2234 } else {
2235 newRequest->mAutoframingAuto = false;
2236 }
2237
2238 auto zoomRatioEntry =
2239 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_ZOOM_RATIO);
2240 if (zoomRatioEntry.count > 0 &&
2241 zoomRatioEntry.data.f[0] == 1.0f) {
2242 newRequest->mZoomRatioIs1x = true;
2243 } else {
2244 newRequest->mZoomRatioIs1x = false;
2245 }
2246
2247 if (mSupportCameraMute) {
2248 for (auto& settings : newRequest->mSettingsList) {
2249 auto testPatternModeEntry =
2250 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
2251 settings.mOriginalTestPatternMode = testPatternModeEntry.count > 0 ?
2252 testPatternModeEntry.data.i32[0] :
2253 ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;
2254
2255 auto testPatternDataEntry =
2256 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
2257 if (testPatternDataEntry.count >= 4) {
2258 memcpy(settings.mOriginalTestPatternData, testPatternDataEntry.data.i32,
2259 sizeof(PhysicalCameraSettings::mOriginalTestPatternData));
2260 } else {
2261 settings.mOriginalTestPatternData[0] = 0;
2262 settings.mOriginalTestPatternData[1] = 0;
2263 settings.mOriginalTestPatternData[2] = 0;
2264 settings.mOriginalTestPatternData[3] = 0;
2265 }
2266 }
2267 }
2268
2269 if (mSupportZoomOverride) {
2270 for (auto& settings : newRequest->mSettingsList) {
2271 auto settingsOverrideEntry =
2272 settings.metadata.find(ANDROID_CONTROL_SETTINGS_OVERRIDE);
2273 settings.mOriginalSettingsOverride = settingsOverrideEntry.count > 0 ?
2274 settingsOverrideEntry.data.i32[0] :
2275 ANDROID_CONTROL_SETTINGS_OVERRIDE_OFF;
2276 }
2277 }
2278
2279 return newRequest;
2280 }
2281
cancelStreamsConfigurationLocked()2282 void Camera3Device::cancelStreamsConfigurationLocked() {
2283 int res = OK;
2284 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2285 res = mInputStream->cancelConfiguration();
2286 if (res != OK) {
2287 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2288 mInputStream->getId(), strerror(-res), res);
2289 }
2290 }
2291
2292 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2293 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
2294 if (outputStream->isConfiguring()) {
2295 res = outputStream->cancelConfiguration();
2296 if (res != OK) {
2297 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2298 outputStream->getId(), strerror(-res), res);
2299 }
2300 }
2301 }
2302
2303 // Return state to that at start of call, so that future configures
2304 // properly clean things up
2305 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2306 mNeedConfig = true;
2307
2308 res = mPreparerThread->resume();
2309 if (res != OK) {
2310 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2311 }
2312 }
2313
checkAbandonedStreamsLocked()2314 bool Camera3Device::checkAbandonedStreamsLocked() {
2315 if ((mInputStream.get() != nullptr) && (mInputStream->isAbandoned())) {
2316 return true;
2317 }
2318
2319 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2320 auto stream = mOutputStreams[i];
2321 if ((stream.get() != nullptr) && (stream->isAbandoned())) {
2322 return true;
2323 }
2324 }
2325
2326 return false;
2327 }
2328
reconfigureCamera(const CameraMetadata & sessionParams,int clientStatusId)2329 bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams, int clientStatusId) {
2330 ATRACE_CALL();
2331 bool ret = false;
2332
2333 nsecs_t startTime = systemTime();
2334
2335 // We must not hold mInterfaceLock here since this function is called from
2336 // RequestThread::threadLoop and holding mInterfaceLock could lead to
2337 // deadlocks (http://b/143513518)
2338 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2339
2340 // Make sure status tracker is flushed
2341 mStatusTracker->flushPendingStates();
2342
2343 Mutex::Autolock l(mLock);
2344 if (checkAbandonedStreamsLocked()) {
2345 ALOGW("%s: Abandoned stream detected, session parameters can't be applied correctly!",
2346 __FUNCTION__);
2347 return true;
2348 }
2349
2350 status_t rc = NO_ERROR;
2351 bool markClientActive = false;
2352 if (mStatus == STATUS_ACTIVE) {
2353 markClientActive = true;
2354 mPauseStateNotify = true;
2355 mStatusTracker->markComponentIdle(clientStatusId, Fence::NO_FENCE);
2356
2357 // This is essentially the same as calling rc = internalPauseAndWaitLocked(..), except that
2358 // we don't want to call setPaused(true) to avoid it interfering with setPaused() called
2359 // from createInputStream/createStream.
2360 rc = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration,
2361 /*requestThreadInvocation*/ true);
2362 if (rc != OK) {
2363 mStatusTracker->dumpActiveComponents();
2364 SET_ERR_L("Can't idle device in %f seconds!",
2365 maxExpectedDuration/1e9);
2366 }
2367 }
2368
2369 if (rc == NO_ERROR) {
2370 mNeedConfig = true;
2371 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2372 if (rc == NO_ERROR) {
2373 ret = true;
2374 mPauseStateNotify = false;
2375 //Moving to active state while holding 'mLock' is important.
2376 //There could be pending calls to 'create-/deleteStream' which
2377 //will trigger another stream configuration while the already
2378 //present streams end up with outstanding buffers that will
2379 //not get drained.
2380 internalUpdateStatusLocked(STATUS_ACTIVE);
2381
2382 mCameraServiceProxyWrapper->logStreamConfigured(mId, mOperatingMode,
2383 true /*internalReconfig*/, ns2ms(systemTime() - startTime));
2384 } else if (rc == DEAD_OBJECT) {
2385 // DEAD_OBJECT can be returned if either the consumer surface is
2386 // abandoned, or the HAL has died.
2387 // - If the HAL has died, configureStreamsLocked call will set
2388 // device to error state,
2389 // - If surface is abandoned, we should not set device to error
2390 // state.
2391 ALOGE("Failed to re-configure camera due to abandoned surface");
2392 } else {
2393 SET_ERR_L("Failed to re-configure camera: %d", rc);
2394 }
2395 } else {
2396 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2397 }
2398
2399 if (markClientActive) {
2400 mStatusTracker->markComponentActive(clientStatusId);
2401 }
2402
2403 return ret;
2404 }
2405
configureStreamsLocked(int operatingMode,const CameraMetadata & sessionParams,bool notifyRequestThread)2406 status_t Camera3Device::configureStreamsLocked(int operatingMode,
2407 const CameraMetadata& sessionParams, bool notifyRequestThread) {
2408 ATRACE_CALL();
2409 status_t res;
2410
2411 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
2412 CLOGE("Not idle");
2413 return INVALID_OPERATION;
2414 }
2415
2416 if (operatingMode < 0) {
2417 CLOGE("Invalid operating mode: %d", operatingMode);
2418 return BAD_VALUE;
2419 }
2420
2421 bool isConstrainedHighSpeed =
2422 CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE == operatingMode;
2423
2424 if (mOperatingMode != operatingMode) {
2425 mNeedConfig = true;
2426 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2427 mOperatingMode = operatingMode;
2428 }
2429
2430 // Reset min expected duration when session is reconfigured.
2431 mMinExpectedDuration = 0;
2432
2433 // In case called from configureStreams, abort queued input buffers not belonging to
2434 // any pending requests.
2435 if (mInputStream != NULL && notifyRequestThread) {
2436 while (true) {
2437 camera_stream_buffer_t inputBuffer;
2438 camera3::Size inputBufferSize;
2439 status_t res = mInputStream->getInputBuffer(&inputBuffer,
2440 &inputBufferSize, /*respectHalLimit*/ false);
2441 if (res != OK) {
2442 // Exhausted acquiring all input buffers.
2443 break;
2444 }
2445
2446 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
2447 res = mInputStream->returnInputBuffer(inputBuffer);
2448 if (res != OK) {
2449 ALOGE("%s: %d: couldn't return input buffer while clearing input queue: "
2450 "%s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2451 }
2452 }
2453 }
2454
2455 if (!mNeedConfig) {
2456 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2457 return OK;
2458 }
2459
2460 // Workaround for device HALv3.2 or older spec bug - zero streams requires
2461 // adding a fake stream instead.
2462 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2463 if (mOutputStreams.size() == 0) {
2464 addFakeStreamLocked();
2465 } else {
2466 tryRemoveFakeStreamLocked();
2467 }
2468
2469 // Override stream use case based on "adb shell command"
2470 overrideStreamUseCaseLocked();
2471
2472 // Start configuring the streams
2473 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
2474
2475 mPreparerThread->pause();
2476
2477 camera_stream_configuration config;
2478 config.operation_mode = mOperatingMode;
2479 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
2480 config.input_is_multi_resolution = false;
2481
2482 Vector<camera3::camera_stream_t*> streams;
2483 streams.setCapacity(config.num_streams);
2484 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
2485
2486
2487 if (mInputStream != NULL) {
2488 camera3::camera_stream_t *inputStream;
2489 inputStream = mInputStream->startConfiguration();
2490 if (inputStream == NULL) {
2491 CLOGE("Can't start input stream configuration");
2492 cancelStreamsConfigurationLocked();
2493 return INVALID_OPERATION;
2494 }
2495 streams.add(inputStream);
2496
2497 config.input_is_multi_resolution = mIsInputStreamMultiResolution;
2498 }
2499
2500 mGroupIdPhysicalCameraMap.clear();
2501 mComposerOutput = false;
2502 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2503
2504 // Don't configure bidi streams twice, nor add them twice to the list
2505 if (mOutputStreams[i].get() ==
2506 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2507
2508 config.num_streams--;
2509 continue;
2510 }
2511
2512 camera3::camera_stream_t *outputStream;
2513 outputStream = mOutputStreams[i]->startConfiguration();
2514 if (outputStream == NULL) {
2515 CLOGE("Can't start output stream configuration");
2516 cancelStreamsConfigurationLocked();
2517 return INVALID_OPERATION;
2518 }
2519 streams.add(outputStream);
2520
2521 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB) {
2522 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2523 // always occupy the initial entry.
2524 if ((outputStream->data_space == HAL_DATASPACE_V0_JFIF) ||
2525 (outputStream->data_space ==
2526 static_cast<android_dataspace_t>(
2527 aidl::android::hardware::graphics::common::Dataspace::JPEG_R))) {
2528 bufferSizes[k] = static_cast<uint32_t>(
2529 getJpegBufferSize(infoPhysical(String8(outputStream->physical_camera_id)),
2530 outputStream->width, outputStream->height));
2531 } else if (outputStream->data_space ==
2532 static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
2533 bufferSizes[k] = outputStream->width * outputStream->height;
2534 } else {
2535 ALOGW("%s: Blob dataSpace %d not supported",
2536 __FUNCTION__, outputStream->data_space);
2537 }
2538 }
2539
2540 if (mOutputStreams[i]->isMultiResolution()) {
2541 int32_t streamGroupId = mOutputStreams[i]->getHalStreamGroupId();
2542 const String8& physicalCameraId = mOutputStreams[i]->getPhysicalCameraId();
2543 mGroupIdPhysicalCameraMap[streamGroupId].insert(physicalCameraId);
2544 }
2545
2546 if (outputStream->usage & GraphicBuffer::USAGE_HW_COMPOSER) {
2547 mComposerOutput = true;
2548 }
2549 }
2550
2551 config.streams = streams.editArray();
2552
2553 // Do the HAL configuration; will potentially touch stream
2554 // max_buffers, usage, and priv fields, as well as data_space and format
2555 // fields for IMPLEMENTATION_DEFINED formats.
2556
2557 int64_t logId = mCameraServiceProxyWrapper->getCurrentLogIdForCamera(mId);
2558 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
2559 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes, logId);
2560 sessionParams.unlock(sessionBuffer);
2561
2562 if (res == BAD_VALUE) {
2563 // HAL rejected this set of streams as unsupported, clean up config
2564 // attempt and return to unconfigured state
2565 CLOGE("Set of requested inputs/outputs not supported by HAL");
2566 cancelStreamsConfigurationLocked();
2567 return BAD_VALUE;
2568 } else if (res != OK) {
2569 // Some other kind of error from configure_streams - this is not
2570 // expected
2571 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2572 strerror(-res), res);
2573 return res;
2574 }
2575
2576 // Finish all stream configuration immediately.
2577 // TODO: Try to relax this later back to lazy completion, which should be
2578 // faster
2579
2580 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2581 bool streamReConfigured = false;
2582 res = mInputStream->finishConfiguration(&streamReConfigured);
2583 if (res != OK) {
2584 CLOGE("Can't finish configuring input stream %d: %s (%d)",
2585 mInputStream->getId(), strerror(-res), res);
2586 cancelStreamsConfigurationLocked();
2587 if ((res == NO_INIT || res == DEAD_OBJECT) && mInputStream->isAbandoned()) {
2588 return DEAD_OBJECT;
2589 }
2590 return BAD_VALUE;
2591 }
2592 if (streamReConfigured) {
2593 mInterface->onStreamReConfigured(mInputStream->getId());
2594 }
2595 }
2596
2597 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2598 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
2599 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
2600 bool streamReConfigured = false;
2601 res = outputStream->finishConfiguration(&streamReConfigured);
2602 if (res != OK) {
2603 CLOGE("Can't finish configuring output stream %d: %s (%d)",
2604 outputStream->getId(), strerror(-res), res);
2605 cancelStreamsConfigurationLocked();
2606 if ((res == NO_INIT || res == DEAD_OBJECT) && outputStream->isAbandoned()) {
2607 return DEAD_OBJECT;
2608 }
2609 return BAD_VALUE;
2610 }
2611 if (streamReConfigured) {
2612 mInterface->onStreamReConfigured(outputStream->getId());
2613 }
2614 }
2615 }
2616
2617 mRequestThread->setComposerSurface(mComposerOutput);
2618
2619 // Request thread needs to know to avoid using repeat-last-settings protocol
2620 // across configure_streams() calls
2621 if (notifyRequestThread) {
2622 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration,
2623 sessionParams, mGroupIdPhysicalCameraMap);
2624 }
2625
2626 char value[PROPERTY_VALUE_MAX];
2627 property_get("camera.fifo.disable", value, "0");
2628 int32_t disableFifo = atoi(value);
2629 if (disableFifo != 1) {
2630 // Boost priority of request thread to SCHED_FIFO.
2631 pid_t requestThreadTid = mRequestThread->getTid();
2632 res = requestPriority(getpid(), requestThreadTid,
2633 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
2634 if (res != OK) {
2635 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2636 strerror(-res), res);
2637 } else {
2638 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2639 }
2640 }
2641
2642 // Update device state
2643 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2644 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2645 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2646 sessionParams.unlock(newSessionParams);
2647 mSessionParams.unlock(currentSessionParams);
2648 if (updateSessionParams) {
2649 mSessionParams = sessionParams;
2650 }
2651
2652 mNeedConfig = false;
2653
2654 internalUpdateStatusLocked((mFakeStreamId == NO_STREAM) ?
2655 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
2656
2657 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
2658
2659 // tear down the deleted streams after configure streams.
2660 mDeletedStreams.clear();
2661
2662 auto rc = mPreparerThread->resume();
2663 if (rc != OK) {
2664 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2665 return rc;
2666 }
2667
2668 if (mFakeStreamId == NO_STREAM) {
2669 mRequestBufferSM.onStreamsConfigured();
2670 }
2671
2672 // First call injectCamera() and then run configureStreamsLocked() case:
2673 // Since the streams configuration of the injection camera is based on the internal camera, we
2674 // must wait until the internal camera configure streams before running the injection job to
2675 // configure the injection streams.
2676 if (mInjectionMethods->isInjecting()) {
2677 ALOGD("%s: Injection camera %s: Start to configure streams.",
2678 __FUNCTION__, mInjectionMethods->getInjectedCamId().string());
2679 res = mInjectionMethods->injectCamera(config, bufferSizes);
2680 if (res != OK) {
2681 ALOGE("Can't finish inject camera process!");
2682 return res;
2683 }
2684 } else {
2685 // First run configureStreamsLocked() and then call injectCamera() case:
2686 // If the stream configuration has been completed and camera deive is active, but the
2687 // injection camera has not been injected yet, we need to store the stream configuration of
2688 // the internal camera (because the stream configuration of the injection camera is based
2689 // on the internal camera). When injecting occurs later, this configuration can be used by
2690 // the injection camera.
2691 ALOGV("%s: The stream configuration is complete and the camera device is active, but the"
2692 " injection camera has not been injected yet.", __FUNCTION__);
2693 mInjectionMethods->storeInjectionConfig(config, bufferSizes);
2694 }
2695
2696 return OK;
2697 }
2698
addFakeStreamLocked()2699 status_t Camera3Device::addFakeStreamLocked() {
2700 ATRACE_CALL();
2701 status_t res;
2702
2703 if (mFakeStreamId != NO_STREAM) {
2704 // Should never be adding a second fake stream when one is already
2705 // active
2706 SET_ERR_L("%s: Camera %s: A fake stream already exists!",
2707 __FUNCTION__, mId.string());
2708 return INVALID_OPERATION;
2709 }
2710
2711 ALOGV("%s: Camera %s: Adding a fake stream", __FUNCTION__, mId.string());
2712
2713 sp<Camera3OutputStreamInterface> fakeStream =
2714 new Camera3FakeStream(mNextStreamId);
2715
2716 res = mOutputStreams.add(mNextStreamId, fakeStream);
2717 if (res < 0) {
2718 SET_ERR_L("Can't add fake stream to set: %s (%d)", strerror(-res), res);
2719 return res;
2720 }
2721
2722 mFakeStreamId = mNextStreamId;
2723 mNextStreamId++;
2724
2725 return OK;
2726 }
2727
tryRemoveFakeStreamLocked()2728 status_t Camera3Device::tryRemoveFakeStreamLocked() {
2729 ATRACE_CALL();
2730 status_t res;
2731
2732 if (mFakeStreamId == NO_STREAM) return OK;
2733 if (mOutputStreams.size() == 1) return OK;
2734
2735 ALOGV("%s: Camera %s: Removing the fake stream", __FUNCTION__, mId.string());
2736
2737 // Ok, have a fake stream and there's at least one other output stream,
2738 // so remove the fake
2739
2740 sp<Camera3StreamInterface> deletedStream = mOutputStreams.get(mFakeStreamId);
2741 if (deletedStream == nullptr) {
2742 SET_ERR_L("Fake stream %d does not appear to exist", mFakeStreamId);
2743 return INVALID_OPERATION;
2744 }
2745 mOutputStreams.remove(mFakeStreamId);
2746
2747 // Free up the stream endpoint so that it can be used by some other stream
2748 res = deletedStream->disconnect();
2749 if (res != OK) {
2750 SET_ERR_L("Can't disconnect deleted fake stream %d", mFakeStreamId);
2751 // fall through since we want to still list the stream as deleted.
2752 }
2753 mDeletedStreams.add(deletedStream);
2754 mFakeStreamId = NO_STREAM;
2755
2756 return res;
2757 }
2758
setErrorState(const char * fmt,...)2759 void Camera3Device::setErrorState(const char *fmt, ...) {
2760 ATRACE_CALL();
2761 Mutex::Autolock l(mLock);
2762 va_list args;
2763 va_start(args, fmt);
2764
2765 setErrorStateLockedV(fmt, args);
2766
2767 va_end(args);
2768 }
2769
setErrorStateV(const char * fmt,va_list args)2770 void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
2771 ATRACE_CALL();
2772 Mutex::Autolock l(mLock);
2773 setErrorStateLockedV(fmt, args);
2774 }
2775
setErrorStateLocked(const char * fmt,...)2776 void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2777 va_list args;
2778 va_start(args, fmt);
2779
2780 setErrorStateLockedV(fmt, args);
2781
2782 va_end(args);
2783 }
2784
setErrorStateLockedV(const char * fmt,va_list args)2785 void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
2786 // Print out all error messages to log
2787 String8 errorCause = String8::formatV(fmt, args);
2788 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
2789
2790 // But only do error state transition steps for the first error
2791 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
2792
2793 mErrorCause = errorCause;
2794
2795 if (mRequestThread != nullptr) {
2796 mRequestThread->setPaused(true);
2797 }
2798 internalUpdateStatusLocked(STATUS_ERROR);
2799
2800 // Notify upstream about a device error
2801 sp<NotificationListener> listener = mListener.promote();
2802 if (listener != NULL) {
2803 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
2804 CaptureResultExtras());
2805 mSessionStatsBuilder.onDeviceError();
2806 }
2807
2808 // Save stack trace. View by dumping it later.
2809 CameraTraces::saveTrace();
2810 // TODO: consider adding errorCause and client pid/procname
2811 }
2812
2813 /**
2814 * In-flight request management
2815 */
2816
registerInFlight(uint32_t frameNumber,int32_t numBuffers,CaptureResultExtras resultExtras,bool hasInput,bool hasAppCallback,nsecs_t minExpectedDuration,nsecs_t maxExpectedDuration,bool isFixedFps,const std::set<std::set<String8>> & physicalCameraIds,bool isStillCapture,bool isZslCapture,bool rotateAndCropAuto,bool autoframingAuto,const std::set<std::string> & cameraIdsWithZoom,const SurfaceMap & outputSurfaces,nsecs_t requestTimeNs)2817 status_t Camera3Device::registerInFlight(uint32_t frameNumber,
2818 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
2819 bool hasAppCallback, nsecs_t minExpectedDuration, nsecs_t maxExpectedDuration,
2820 bool isFixedFps, const std::set<std::set<String8>>& physicalCameraIds,
2821 bool isStillCapture, bool isZslCapture, bool rotateAndCropAuto, bool autoframingAuto,
2822 const std::set<std::string>& cameraIdsWithZoom,
2823 const SurfaceMap& outputSurfaces, nsecs_t requestTimeNs) {
2824 ATRACE_CALL();
2825 std::lock_guard<std::mutex> l(mInFlightLock);
2826
2827 ssize_t res;
2828 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
2829 hasAppCallback, minExpectedDuration, maxExpectedDuration, isFixedFps, physicalCameraIds,
2830 isStillCapture, isZslCapture, rotateAndCropAuto, autoframingAuto, cameraIdsWithZoom,
2831 requestTimeNs, outputSurfaces));
2832 if (res < 0) return res;
2833
2834 if (mInFlightMap.size() == 1) {
2835 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2836 // avoid a deadlock during reprocess requests.
2837 Mutex::Autolock l(mTrackerLock);
2838 if (mStatusTracker != nullptr) {
2839 mStatusTracker->markComponentActive(mInFlightStatusId);
2840 }
2841 }
2842
2843 mExpectedInflightDuration += maxExpectedDuration;
2844 return OK;
2845 }
2846
onInflightEntryRemovedLocked(nsecs_t duration)2847 void Camera3Device::onInflightEntryRemovedLocked(nsecs_t duration) {
2848 // Indicate idle inFlightMap to the status tracker
2849 if (mInFlightMap.size() == 0) {
2850 mRequestBufferSM.onInflightMapEmpty();
2851 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2852 // avoid a deadlock during reprocess requests.
2853 Mutex::Autolock l(mTrackerLock);
2854 if (mStatusTracker != nullptr) {
2855 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2856 }
2857 }
2858 mExpectedInflightDuration -= duration;
2859 }
2860
checkInflightMapLengthLocked()2861 void Camera3Device::checkInflightMapLengthLocked() {
2862 // Validation check - if we have too many in-flight frames with long total inflight duration,
2863 // something has likely gone wrong. This might still be legit only if application send in
2864 // a long burst of long exposure requests.
2865 if (mExpectedInflightDuration > kMinWarnInflightDuration) {
2866 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
2867 CLOGW("In-flight list too large: %zu, total inflight duration %" PRIu64,
2868 mInFlightMap.size(), mExpectedInflightDuration);
2869 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2870 kInFlightWarnLimitHighSpeed) {
2871 CLOGW("In-flight list too large for high speed configuration: %zu,"
2872 "total inflight duration %" PRIu64,
2873 mInFlightMap.size(), mExpectedInflightDuration);
2874 }
2875 }
2876 }
2877
onInflightMapFlushedLocked()2878 void Camera3Device::onInflightMapFlushedLocked() {
2879 mExpectedInflightDuration = 0;
2880 }
2881
removeInFlightMapEntryLocked(int idx)2882 void Camera3Device::removeInFlightMapEntryLocked(int idx) {
2883 ATRACE_HFR_CALL();
2884 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
2885 mInFlightMap.removeItemsAt(idx, 1);
2886
2887 onInflightEntryRemovedLocked(duration);
2888 }
2889
2890
flushInflightRequests()2891 void Camera3Device::flushInflightRequests() {
2892 ATRACE_CALL();
2893 sp<NotificationListener> listener;
2894 {
2895 std::lock_guard<std::mutex> l(mOutputLock);
2896 listener = mListener.promote();
2897 }
2898
2899 FlushInflightReqStates states {
2900 mId, mInFlightLock, mInFlightMap, mUseHalBufManager,
2901 listener, *this, *mInterface, *this, mSessionStatsBuilder};
2902
2903 camera3::flushInflightRequests(states);
2904 }
2905
getLatestRequestLocked()2906 CameraMetadata Camera3Device::getLatestRequestLocked() {
2907 ALOGV("%s", __FUNCTION__);
2908
2909 CameraMetadata retVal;
2910
2911 if (mRequestThread != NULL) {
2912 retVal = mRequestThread->getLatestRequest();
2913 }
2914
2915 return retVal;
2916 }
2917
monitorMetadata(TagMonitor::eventSource source,int64_t frameNumber,nsecs_t timestamp,const CameraMetadata & metadata,const std::unordered_map<std::string,CameraMetadata> & physicalMetadata,const camera_stream_buffer_t * outputBuffers,uint32_t numOutputBuffers,int32_t inputStreamId)2918 void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
2919 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata,
2920 const std::unordered_map<std::string, CameraMetadata>& physicalMetadata,
2921 const camera_stream_buffer_t *outputBuffers, uint32_t numOutputBuffers,
2922 int32_t inputStreamId) {
2923
2924 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata,
2925 physicalMetadata, outputBuffers, numOutputBuffers, inputStreamId);
2926 }
2927
cleanupNativeHandles(std::vector<native_handle_t * > * handles,bool closeFd)2928 void Camera3Device::cleanupNativeHandles(
2929 std::vector<native_handle_t*> *handles, bool closeFd) {
2930 if (handles == nullptr) {
2931 return;
2932 }
2933 if (closeFd) {
2934 for (auto& handle : *handles) {
2935 native_handle_close(handle);
2936 }
2937 }
2938 for (auto& handle : *handles) {
2939 native_handle_delete(handle);
2940 }
2941 handles->clear();
2942 return;
2943 }
2944
2945 /**
2946 * HalInterface inner class methods
2947 */
2948
getInflightBufferKeys(std::vector<std::pair<int32_t,int32_t>> * out)2949 void Camera3Device::HalInterface::getInflightBufferKeys(
2950 std::vector<std::pair<int32_t, int32_t>>* out) {
2951 mBufferRecords.getInflightBufferKeys(out);
2952 return;
2953 }
2954
getInflightRequestBufferKeys(std::vector<uint64_t> * out)2955 void Camera3Device::HalInterface::getInflightRequestBufferKeys(
2956 std::vector<uint64_t>* out) {
2957 mBufferRecords.getInflightRequestBufferKeys(out);
2958 return;
2959 }
2960
verifyBufferIds(int32_t streamId,std::vector<uint64_t> & bufIds)2961 bool Camera3Device::HalInterface::verifyBufferIds(
2962 int32_t streamId, std::vector<uint64_t>& bufIds) {
2963 return mBufferRecords.verifyBufferIds(streamId, bufIds);
2964 }
2965
popInflightBuffer(int32_t frameNumber,int32_t streamId,buffer_handle_t ** buffer)2966 status_t Camera3Device::HalInterface::popInflightBuffer(
2967 int32_t frameNumber, int32_t streamId,
2968 /*out*/ buffer_handle_t **buffer) {
2969 return mBufferRecords.popInflightBuffer(frameNumber, streamId, buffer);
2970 }
2971
pushInflightRequestBuffer(uint64_t bufferId,buffer_handle_t * buf,int32_t streamId)2972 status_t Camera3Device::HalInterface::pushInflightRequestBuffer(
2973 uint64_t bufferId, buffer_handle_t* buf, int32_t streamId) {
2974 return mBufferRecords.pushInflightRequestBuffer(bufferId, buf, streamId);
2975 }
2976
2977 // Find and pop a buffer_handle_t based on bufferId
popInflightRequestBuffer(uint64_t bufferId,buffer_handle_t ** buffer,int32_t * streamId)2978 status_t Camera3Device::HalInterface::popInflightRequestBuffer(
2979 uint64_t bufferId,
2980 /*out*/ buffer_handle_t** buffer,
2981 /*optional out*/ int32_t* streamId) {
2982 return mBufferRecords.popInflightRequestBuffer(bufferId, buffer, streamId);
2983 }
2984
getBufferId(const buffer_handle_t & buf,int streamId)2985 std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
2986 const buffer_handle_t& buf, int streamId) {
2987 return mBufferRecords.getBufferId(buf, streamId);
2988 }
2989
removeOneBufferCache(int streamId,const native_handle_t * handle)2990 uint64_t Camera3Device::HalInterface::removeOneBufferCache(int streamId,
2991 const native_handle_t* handle) {
2992 return mBufferRecords.removeOneBufferCache(streamId, handle);
2993 }
2994
onBufferFreed(int streamId,const native_handle_t * handle)2995 void Camera3Device::HalInterface::onBufferFreed(
2996 int streamId, const native_handle_t* handle) {
2997 uint32_t bufferId = mBufferRecords.removeOneBufferCache(streamId, handle);
2998 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2999 if (bufferId != BUFFER_ID_NO_BUFFER) {
3000 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
3001 }
3002 }
3003
onStreamReConfigured(int streamId)3004 void Camera3Device::HalInterface::onStreamReConfigured(int streamId) {
3005 std::vector<uint64_t> bufIds = mBufferRecords.clearBufferCaches(streamId);
3006 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
3007 for (auto bufferId : bufIds) {
3008 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
3009 }
3010 }
3011
3012 /**
3013 * RequestThread inner class methods
3014 */
3015
RequestThread(wp<Camera3Device> parent,sp<StatusTracker> statusTracker,sp<HalInterface> interface,const Vector<int32_t> & sessionParamKeys,bool useHalBufManager,bool supportCameraMute,bool overrideToPortrait,bool supportSettingsOverride)3016 Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
3017 sp<StatusTracker> statusTracker,
3018 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys,
3019 bool useHalBufManager,
3020 bool supportCameraMute,
3021 bool overrideToPortrait,
3022 bool supportSettingsOverride) :
3023 Thread(/*canCallJava*/false),
3024 mParent(parent),
3025 mStatusTracker(statusTracker),
3026 mInterface(interface),
3027 mListener(nullptr),
3028 mId(getId(parent)),
3029 mRequestClearing(false),
3030 mFirstRepeating(false),
3031 mReconfigured(false),
3032 mDoPause(false),
3033 mPaused(true),
3034 mNotifyPipelineDrain(false),
3035 mFrameNumber(0),
3036 mLatestRequestId(NAME_NOT_FOUND),
3037 mLatestFailedRequestId(NAME_NOT_FOUND),
3038 mCurrentAfTriggerId(0),
3039 mCurrentPreCaptureTriggerId(0),
3040 mRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE),
3041 mAutoframingOverride(ANDROID_CONTROL_AUTOFRAMING_OFF),
3042 mComposerOutput(false),
3043 mCameraMute(ANDROID_SENSOR_TEST_PATTERN_MODE_OFF),
3044 mCameraMuteChanged(false),
3045 mSettingsOverride(ANDROID_CONTROL_SETTINGS_OVERRIDE_OFF),
3046 mRepeatingLastFrameNumber(
3047 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
3048 mPrepareVideoStream(false),
3049 mConstrainedMode(false),
3050 mRequestLatency(kRequestLatencyBinSize),
3051 mSessionParamKeys(sessionParamKeys),
3052 mLatestSessionParams(sessionParamKeys.size()),
3053 mUseHalBufManager(useHalBufManager),
3054 mSupportCameraMute(supportCameraMute),
3055 mOverrideToPortrait(overrideToPortrait),
3056 mSupportSettingsOverride(supportSettingsOverride) {
3057 mStatusId = statusTracker->addComponent("RequestThread");
3058 mVndkVersion = property_get_int32("ro.vndk.version", __ANDROID_API_FUTURE__);
3059 }
3060
~RequestThread()3061 Camera3Device::RequestThread::~RequestThread() {}
3062
setNotificationListener(wp<NotificationListener> listener)3063 void Camera3Device::RequestThread::setNotificationListener(
3064 wp<NotificationListener> listener) {
3065 ATRACE_CALL();
3066 Mutex::Autolock l(mRequestLock);
3067 mListener = listener;
3068 }
3069
configurationComplete(bool isConstrainedHighSpeed,const CameraMetadata & sessionParams,const std::map<int32_t,std::set<String8>> & groupIdPhysicalCameraMap)3070 void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
3071 const CameraMetadata& sessionParams,
3072 const std::map<int32_t, std::set<String8>>& groupIdPhysicalCameraMap) {
3073 ATRACE_CALL();
3074 Mutex::Autolock l(mRequestLock);
3075 mReconfigured = true;
3076 mLatestSessionParams = sessionParams;
3077 mGroupIdPhysicalCameraMap = groupIdPhysicalCameraMap;
3078 // Prepare video stream for high speed recording.
3079 mPrepareVideoStream = isConstrainedHighSpeed;
3080 mConstrainedMode = isConstrainedHighSpeed;
3081 }
3082
queueRequestList(List<sp<CaptureRequest>> & requests,int64_t * lastFrameNumber)3083 status_t Camera3Device::RequestThread::queueRequestList(
3084 List<sp<CaptureRequest> > &requests,
3085 /*out*/
3086 int64_t *lastFrameNumber) {
3087 ATRACE_CALL();
3088 Mutex::Autolock l(mRequestLock);
3089 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
3090 ++it) {
3091 mRequestQueue.push_back(*it);
3092 }
3093
3094 if (lastFrameNumber != NULL) {
3095 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
3096 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
3097 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
3098 *lastFrameNumber);
3099 }
3100
3101 unpauseForNewRequests();
3102
3103 return OK;
3104 }
3105
3106
queueTrigger(RequestTrigger trigger[],size_t count)3107 status_t Camera3Device::RequestThread::queueTrigger(
3108 RequestTrigger trigger[],
3109 size_t count) {
3110 ATRACE_CALL();
3111 Mutex::Autolock l(mTriggerMutex);
3112 status_t ret;
3113
3114 for (size_t i = 0; i < count; ++i) {
3115 ret = queueTriggerLocked(trigger[i]);
3116
3117 if (ret != OK) {
3118 return ret;
3119 }
3120 }
3121
3122 return OK;
3123 }
3124
getId(const wp<Camera3Device> & device)3125 const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
3126 static String8 deadId("<DeadDevice>");
3127 sp<Camera3Device> d = device.promote();
3128 if (d != nullptr) return d->mId;
3129 return deadId;
3130 }
3131
queueTriggerLocked(RequestTrigger trigger)3132 status_t Camera3Device::RequestThread::queueTriggerLocked(
3133 RequestTrigger trigger) {
3134
3135 uint32_t tag = trigger.metadataTag;
3136 ssize_t index = mTriggerMap.indexOfKey(tag);
3137
3138 switch (trigger.getTagType()) {
3139 case TYPE_BYTE:
3140 // fall-through
3141 case TYPE_INT32:
3142 break;
3143 default:
3144 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
3145 trigger.getTagType());
3146 return INVALID_OPERATION;
3147 }
3148
3149 /**
3150 * Collect only the latest trigger, since we only have 1 field
3151 * in the request settings per trigger tag, and can't send more than 1
3152 * trigger per request.
3153 */
3154 if (index != NAME_NOT_FOUND) {
3155 mTriggerMap.editValueAt(index) = trigger;
3156 } else {
3157 mTriggerMap.add(tag, trigger);
3158 }
3159
3160 return OK;
3161 }
3162
setRepeatingRequests(const RequestList & requests,int64_t * lastFrameNumber)3163 status_t Camera3Device::RequestThread::setRepeatingRequests(
3164 const RequestList &requests,
3165 /*out*/
3166 int64_t *lastFrameNumber) {
3167 ATRACE_CALL();
3168 Mutex::Autolock l(mRequestLock);
3169 if (lastFrameNumber != NULL) {
3170 *lastFrameNumber = mRepeatingLastFrameNumber;
3171 }
3172 mRepeatingRequests.clear();
3173 mFirstRepeating = true;
3174 mRepeatingRequests.insert(mRepeatingRequests.begin(),
3175 requests.begin(), requests.end());
3176
3177 unpauseForNewRequests();
3178
3179 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
3180 return OK;
3181 }
3182
isRepeatingRequestLocked(const sp<CaptureRequest> & requestIn)3183 bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
3184 if (mRepeatingRequests.empty()) {
3185 return false;
3186 }
3187 int32_t requestId = requestIn->mResultExtras.requestId;
3188 const RequestList &repeatRequests = mRepeatingRequests;
3189 // All repeating requests are guaranteed to have same id so only check first quest
3190 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
3191 return (firstRequest->mResultExtras.requestId == requestId);
3192 }
3193
clearRepeatingRequests(int64_t * lastFrameNumber)3194 status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
3195 ATRACE_CALL();
3196 Mutex::Autolock l(mRequestLock);
3197 return clearRepeatingRequestsLocked(lastFrameNumber);
3198
3199 }
3200
clearRepeatingRequestsLocked(int64_t * lastFrameNumber)3201 status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(
3202 /*out*/int64_t *lastFrameNumber) {
3203 std::vector<int32_t> streamIds;
3204 for (const auto& request : mRepeatingRequests) {
3205 for (const auto& stream : request->mOutputStreams) {
3206 streamIds.push_back(stream->getId());
3207 }
3208 }
3209
3210 mRepeatingRequests.clear();
3211 if (lastFrameNumber != NULL) {
3212 *lastFrameNumber = mRepeatingLastFrameNumber;
3213 }
3214
3215 mInterface->repeatingRequestEnd(mRepeatingLastFrameNumber, streamIds);
3216
3217 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
3218 return OK;
3219 }
3220
clear(int64_t * lastFrameNumber)3221 status_t Camera3Device::RequestThread::clear(
3222 /*out*/int64_t *lastFrameNumber) {
3223 ATRACE_CALL();
3224 Mutex::Autolock l(mRequestLock);
3225 ALOGV("RequestThread::%s:", __FUNCTION__);
3226
3227 // Send errors for all requests pending in the request queue, including
3228 // pending repeating requests
3229 sp<NotificationListener> listener = mListener.promote();
3230 if (listener != NULL) {
3231 for (RequestList::iterator it = mRequestQueue.begin();
3232 it != mRequestQueue.end(); ++it) {
3233 // Abort the input buffers for reprocess requests.
3234 if ((*it)->mInputStream != NULL) {
3235 camera_stream_buffer_t inputBuffer;
3236 camera3::Size inputBufferSize;
3237 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
3238 &inputBufferSize, /*respectHalLimit*/ false);
3239 if (res != OK) {
3240 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
3241 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3242 } else {
3243 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
3244 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
3245 if (res != OK) {
3246 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
3247 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3248 }
3249 }
3250 }
3251 // Set the frame number this request would have had, if it
3252 // had been submitted; this frame number will not be reused.
3253 // The requestId and burstId fields were set when the request was
3254 // submitted originally (in convertMetadataListToRequestListLocked)
3255 (*it)->mResultExtras.frameNumber = mFrameNumber++;
3256 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3257 (*it)->mResultExtras);
3258 }
3259 }
3260 mRequestQueue.clear();
3261
3262 Mutex::Autolock al(mTriggerMutex);
3263 mTriggerMap.clear();
3264 clearRepeatingRequestsLocked(lastFrameNumber);
3265 mRequestClearing = true;
3266 mRequestSignal.signal();
3267 return OK;
3268 }
3269
flush()3270 status_t Camera3Device::RequestThread::flush() {
3271 ATRACE_CALL();
3272 Mutex::Autolock l(mFlushLock);
3273
3274 return mInterface->flush();
3275 }
3276
setPaused(bool paused)3277 void Camera3Device::RequestThread::setPaused(bool paused) {
3278 ATRACE_CALL();
3279 Mutex::Autolock l(mPauseLock);
3280 mDoPause = paused;
3281 mDoPauseSignal.signal();
3282 }
3283
waitUntilRequestProcessed(int32_t requestId,nsecs_t timeout)3284 status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
3285 int32_t requestId, nsecs_t timeout) {
3286 ATRACE_CALL();
3287 Mutex::Autolock l(mLatestRequestMutex);
3288 status_t res;
3289 while (mLatestRequestId != requestId && mLatestFailedRequestId != requestId) {
3290 nsecs_t startTime = systemTime();
3291
3292 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3293 if (res != OK) return res;
3294
3295 timeout -= (systemTime() - startTime);
3296 }
3297
3298 return OK;
3299 }
3300
requestExit()3301 void Camera3Device::RequestThread::requestExit() {
3302 // Call parent to set up shutdown
3303 Thread::requestExit();
3304 // The exit from any possible waits
3305 mDoPauseSignal.signal();
3306 mRequestSignal.signal();
3307
3308 mRequestLatency.log("ProcessCaptureRequest latency histogram");
3309 mRequestLatency.reset();
3310 }
3311
checkAndStopRepeatingRequest()3312 void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
3313 ATRACE_CALL();
3314 bool surfaceAbandoned = false;
3315 int64_t lastFrameNumber = 0;
3316 sp<NotificationListener> listener;
3317 {
3318 Mutex::Autolock l(mRequestLock);
3319 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3320 // repeating requests.
3321 for (const auto& request : mRepeatingRequests) {
3322 for (const auto& s : request->mOutputStreams) {
3323 if (s->isAbandoned()) {
3324 surfaceAbandoned = true;
3325 clearRepeatingRequestsLocked(&lastFrameNumber);
3326 break;
3327 }
3328 }
3329 if (surfaceAbandoned) {
3330 break;
3331 }
3332 }
3333 listener = mListener.promote();
3334 }
3335
3336 if (listener != NULL && surfaceAbandoned) {
3337 listener->notifyRepeatingRequestError(lastFrameNumber);
3338 }
3339 }
3340
sendRequestsBatch()3341 bool Camera3Device::RequestThread::sendRequestsBatch() {
3342 ATRACE_CALL();
3343 status_t res;
3344 size_t batchSize = mNextRequests.size();
3345 std::vector<camera_capture_request_t*> requests(batchSize);
3346 uint32_t numRequestProcessed = 0;
3347 for (size_t i = 0; i < batchSize; i++) {
3348 requests[i] = &mNextRequests.editItemAt(i).halRequest;
3349 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
3350 }
3351
3352 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
3353
3354 bool triggerRemoveFailed = false;
3355 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
3356 for (size_t i = 0; i < numRequestProcessed; i++) {
3357 NextRequest& nextRequest = mNextRequests.editItemAt(i);
3358 nextRequest.submitted = true;
3359
3360 updateNextRequest(nextRequest);
3361
3362 if (!triggerRemoveFailed) {
3363 // Remove any previously queued triggers (after unlock)
3364 status_t removeTriggerRes = removeTriggers(mPrevRequest);
3365 if (removeTriggerRes != OK) {
3366 triggerRemoveFailed = true;
3367 triggerFailedRequest = nextRequest;
3368 }
3369 }
3370 }
3371
3372 if (triggerRemoveFailed) {
3373 SET_ERR("RequestThread: Unable to remove triggers "
3374 "(capture request %d, HAL device: %s (%d)",
3375 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
3376 cleanUpFailedRequests(/*sendRequestError*/ false);
3377 return false;
3378 }
3379
3380 if (res != OK) {
3381 // Should only get a failure here for malformed requests or device-level
3382 // errors, so consider all errors fatal. Bad metadata failures should
3383 // come through notify.
3384 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
3385 mNextRequests[numRequestProcessed].halRequest.frame_number,
3386 strerror(-res), res);
3387 cleanUpFailedRequests(/*sendRequestError*/ false);
3388 return false;
3389 }
3390 return true;
3391 }
3392
3393 Camera3Device::RequestThread::ExpectedDurationInfo
calculateExpectedDurationRange(const camera_metadata_t * request)3394 Camera3Device::RequestThread::calculateExpectedDurationRange(
3395 const camera_metadata_t *request) {
3396 ExpectedDurationInfo expectedDurationInfo = {
3397 InFlightRequest::kDefaultMinExpectedDuration,
3398 InFlightRequest::kDefaultMaxExpectedDuration,
3399 /*isFixedFps*/false};
3400 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3401 find_camera_metadata_ro_entry(request,
3402 ANDROID_CONTROL_AE_MODE,
3403 &e);
3404 if (e.count == 0) return expectedDurationInfo;
3405
3406 switch (e.data.u8[0]) {
3407 case ANDROID_CONTROL_AE_MODE_OFF:
3408 find_camera_metadata_ro_entry(request,
3409 ANDROID_SENSOR_EXPOSURE_TIME,
3410 &e);
3411 if (e.count > 0) {
3412 expectedDurationInfo.minDuration = e.data.i64[0];
3413 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
3414 }
3415 find_camera_metadata_ro_entry(request,
3416 ANDROID_SENSOR_FRAME_DURATION,
3417 &e);
3418 if (e.count > 0) {
3419 expectedDurationInfo.minDuration =
3420 std::max(e.data.i64[0], expectedDurationInfo.minDuration);
3421 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
3422 }
3423 expectedDurationInfo.isFixedFps = false;
3424 break;
3425 default:
3426 find_camera_metadata_ro_entry(request,
3427 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
3428 &e);
3429 if (e.count > 1) {
3430 expectedDurationInfo.minDuration = 1e9 / e.data.i32[1];
3431 expectedDurationInfo.maxDuration = 1e9 / e.data.i32[0];
3432 }
3433 expectedDurationInfo.isFixedFps = (e.data.i32[1] == e.data.i32[0]);
3434 break;
3435 }
3436
3437 return expectedDurationInfo;
3438 }
3439
skipHFRTargetFPSUpdate(int32_t tag,const camera_metadata_ro_entry_t & newEntry,const camera_metadata_entry_t & currentEntry)3440 bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
3441 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
3442 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
3443 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
3444 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
3445 return true;
3446 }
3447
3448 return false;
3449 }
3450
updateNextRequest(NextRequest & nextRequest)3451 void Camera3Device::RequestThread::updateNextRequest(NextRequest& nextRequest) {
3452 // Update the latest request sent to HAL
3453 camera_capture_request_t& halRequest = nextRequest.halRequest;
3454 if (halRequest.settings != NULL) { // Don't update if they were unchanged
3455 Mutex::Autolock al(mLatestRequestMutex);
3456
3457 camera_metadata_t* cloned = clone_camera_metadata(halRequest.settings);
3458 mLatestRequest.acquire(cloned);
3459
3460 mLatestPhysicalRequest.clear();
3461 for (uint32_t i = 0; i < halRequest.num_physcam_settings; i++) {
3462 cloned = clone_camera_metadata(halRequest.physcam_settings[i]);
3463 mLatestPhysicalRequest.emplace(halRequest.physcam_id[i],
3464 CameraMetadata(cloned));
3465 }
3466
3467 sp<Camera3Device> parent = mParent.promote();
3468 if (parent != NULL) {
3469 int32_t inputStreamId = -1;
3470 if (halRequest.input_buffer != nullptr) {
3471 inputStreamId = Camera3Stream::cast(halRequest.input_buffer->stream)->getId();
3472 }
3473
3474 parent->monitorMetadata(TagMonitor::REQUEST,
3475 halRequest.frame_number,
3476 0, mLatestRequest, mLatestPhysicalRequest, halRequest.output_buffers,
3477 halRequest.num_output_buffers, inputStreamId);
3478 }
3479 }
3480
3481 if (halRequest.settings != NULL) {
3482 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
3483 halRequest.settings);
3484 }
3485
3486 cleanupPhysicalSettings(nextRequest.captureRequest, &halRequest);
3487 }
3488
updateSessionParameters(const CameraMetadata & settings)3489 bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
3490 ATRACE_CALL();
3491 bool updatesDetected = false;
3492
3493 CameraMetadata updatedParams(mLatestSessionParams);
3494 for (auto tag : mSessionParamKeys) {
3495 camera_metadata_ro_entry entry = settings.find(tag);
3496 camera_metadata_entry lastEntry = updatedParams.find(tag);
3497
3498 if (entry.count > 0) {
3499 bool isDifferent = false;
3500 if (lastEntry.count > 0) {
3501 // Have a last value, compare to see if changed
3502 if (lastEntry.type == entry.type &&
3503 lastEntry.count == entry.count) {
3504 // Same type and count, compare values
3505 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
3506 size_t entryBytes = bytesPerValue * lastEntry.count;
3507 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
3508 if (cmp != 0) {
3509 isDifferent = true;
3510 }
3511 } else {
3512 // Count or type has changed
3513 isDifferent = true;
3514 }
3515 } else {
3516 // No last entry, so always consider to be different
3517 isDifferent = true;
3518 }
3519
3520 if (isDifferent) {
3521 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
3522 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
3523 updatesDetected = true;
3524 }
3525 updatedParams.update(entry);
3526 }
3527 } else if (lastEntry.count > 0) {
3528 // Value has been removed
3529 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
3530 updatedParams.erase(tag);
3531 updatesDetected = true;
3532 }
3533 }
3534
3535 bool reconfigureRequired;
3536 if (updatesDetected) {
3537 reconfigureRequired = mInterface->isReconfigurationRequired(mLatestSessionParams,
3538 updatedParams);
3539 mLatestSessionParams = updatedParams;
3540 } else {
3541 reconfigureRequired = false;
3542 }
3543
3544 return reconfigureRequired;
3545 }
3546
threadLoop()3547 bool Camera3Device::RequestThread::threadLoop() {
3548 ATRACE_CALL();
3549 status_t res;
3550 // Any function called from threadLoop() must not hold mInterfaceLock since
3551 // it could lead to deadlocks (disconnect() -> hold mInterfaceMutex -> wait for request thread
3552 // to finish -> request thread waits on mInterfaceMutex) http://b/143513518
3553
3554 // Handle paused state.
3555 if (waitIfPaused()) {
3556 return true;
3557 }
3558
3559 // Wait for the next batch of requests.
3560 waitForNextRequestBatch();
3561 if (mNextRequests.size() == 0) {
3562 return true;
3563 }
3564
3565 // Get the latest request ID, if any
3566 int latestRequestId;
3567 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
3568 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
3569 if (requestIdEntry.count > 0) {
3570 latestRequestId = requestIdEntry.data.i32[0];
3571 } else {
3572 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3573 latestRequestId = NAME_NOT_FOUND;
3574 }
3575
3576 for (size_t i = 0; i < mNextRequests.size(); i++) {
3577 auto& nextRequest = mNextRequests.editItemAt(i);
3578 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3579 // Do not override rotate&crop for stream configurations that include
3580 // SurfaceViews(HW_COMPOSER) output, unless mOverrideToPortrait is set.
3581 // The display rotation there will be compensated by NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY
3582 captureRequest->mRotateAndCropChanged = (mComposerOutput && !mOverrideToPortrait) ? false :
3583 overrideAutoRotateAndCrop(captureRequest);
3584 captureRequest->mAutoframingChanged = overrideAutoframing(captureRequest);
3585 }
3586
3587 // 'mNextRequests' will at this point contain either a set of HFR batched requests
3588 // or a single request from streaming or burst. In either case the first element
3589 // should contain the latest camera settings that we need to check for any session
3590 // parameter updates.
3591 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
3592 res = OK;
3593
3594 //Input stream buffers are already acquired at this point so an input stream
3595 //will not be able to move to idle state unless we force it.
3596 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3597 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
3598 if (res != OK) {
3599 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
3600 cleanUpFailedRequests(/*sendRequestError*/ false);
3601 return false;
3602 }
3603 }
3604
3605 if (res == OK) {
3606 sp<Camera3Device> parent = mParent.promote();
3607 if (parent != nullptr) {
3608 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams, mStatusId);
3609 }
3610
3611 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3612 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
3613 if (res != OK) {
3614 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
3615 cleanUpFailedRequests(/*sendRequestError*/ false);
3616 return false;
3617 }
3618 }
3619 }
3620 }
3621
3622 // Prepare a batch of HAL requests and output buffers.
3623 res = prepareHalRequests();
3624 if (res == TIMED_OUT) {
3625 // Not a fatal error if getting output buffers time out.
3626 cleanUpFailedRequests(/*sendRequestError*/ true);
3627 // Check if any stream is abandoned.
3628 checkAndStopRepeatingRequest();
3629 return true;
3630 } else if (res != OK) {
3631 cleanUpFailedRequests(/*sendRequestError*/ false);
3632 return false;
3633 }
3634
3635 // Inform waitUntilRequestProcessed thread of a new request ID
3636 {
3637 Mutex::Autolock al(mLatestRequestMutex);
3638
3639 mLatestRequestId = latestRequestId;
3640 mLatestRequestSignal.signal();
3641 }
3642
3643 // Submit a batch of requests to HAL.
3644 // Use flush lock only when submitting multilple requests in a batch.
3645 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3646 // which may take a long time to finish so synchronizing flush() and
3647 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3648 // For now, only synchronize for high speed recording and we should figure something out for
3649 // removing the synchronization.
3650 bool useFlushLock = mNextRequests.size() > 1;
3651
3652 if (useFlushLock) {
3653 mFlushLock.lock();
3654 }
3655
3656 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
3657 mNextRequests.size());
3658
3659 sp<Camera3Device> parent = mParent.promote();
3660 if (parent != nullptr) {
3661 parent->mRequestBufferSM.onSubmittingRequest();
3662 }
3663
3664 bool submitRequestSuccess = false;
3665 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
3666 submitRequestSuccess = sendRequestsBatch();
3667
3668 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
3669 mRequestLatency.add(tRequestStart, tRequestEnd);
3670
3671 if (useFlushLock) {
3672 mFlushLock.unlock();
3673 }
3674
3675 // Unset as current request
3676 {
3677 Mutex::Autolock l(mRequestLock);
3678 mNextRequests.clear();
3679 }
3680 mRequestSubmittedSignal.signal();
3681
3682 return submitRequestSuccess;
3683 }
3684
removeFwkOnlyRegionKeys(CameraMetadata * request)3685 status_t Camera3Device::removeFwkOnlyRegionKeys(CameraMetadata *request) {
3686 static const std::array<uint32_t, 4> kFwkOnlyRegionKeys = {ANDROID_CONTROL_AF_REGIONS_SET,
3687 ANDROID_CONTROL_AE_REGIONS_SET, ANDROID_CONTROL_AWB_REGIONS_SET,
3688 ANDROID_SCALER_CROP_REGION_SET};
3689 if (request == nullptr) {
3690 ALOGE("%s request metadata nullptr", __FUNCTION__);
3691 return BAD_VALUE;
3692 }
3693 status_t res = OK;
3694 for (const auto &key : kFwkOnlyRegionKeys) {
3695 if (request->exists(key)) {
3696 res = request->erase(key);
3697 if (res != OK) {
3698 return res;
3699 }
3700 }
3701 }
3702 return OK;
3703 }
3704
prepareHalRequests()3705 status_t Camera3Device::RequestThread::prepareHalRequests() {
3706 ATRACE_CALL();
3707
3708 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
3709 for (size_t i = 0; i < mNextRequests.size(); i++) {
3710 auto& nextRequest = mNextRequests.editItemAt(i);
3711 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3712 camera_capture_request_t* halRequest = &nextRequest.halRequest;
3713 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
3714
3715 // Prepare a request to HAL
3716 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3717
3718 // Insert any queued triggers (before metadata is locked)
3719 status_t res = insertTriggers(captureRequest);
3720 if (res < 0) {
3721 SET_ERR("RequestThread: Unable to insert triggers "
3722 "(capture request %d, HAL device: %s (%d)",
3723 halRequest->frame_number, strerror(-res), res);
3724 return INVALID_OPERATION;
3725 }
3726
3727 int triggerCount = res;
3728 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3729 mPrevTriggers = triggerCount;
3730
3731 bool testPatternChanged = overrideTestPattern(captureRequest);
3732 bool settingsOverrideChanged = overrideSettingsOverride(captureRequest);
3733
3734 // If the request is the same as last, or we had triggers now or last time or
3735 // changing overrides this time
3736 bool newRequest =
3737 (mPrevRequest != captureRequest || triggersMixedIn ||
3738 captureRequest->mRotateAndCropChanged ||
3739 captureRequest->mAutoframingChanged ||
3740 testPatternChanged || settingsOverrideChanged) &&
3741 // Request settings are all the same within one batch, so only treat the first
3742 // request in a batch as new
3743 !(batchedRequest && i > 0);
3744 if (newRequest) {
3745 std::set<std::string> cameraIdsWithZoom;
3746 /**
3747 * HAL workaround:
3748 * Insert a fake trigger ID if a trigger is set but no trigger ID is
3749 */
3750 res = addFakeTriggerIds(captureRequest);
3751 if (res != OK) {
3752 SET_ERR("RequestThread: Unable to insert fake trigger IDs "
3753 "(capture request %d, HAL device: %s (%d)",
3754 halRequest->frame_number, strerror(-res), res);
3755 return INVALID_OPERATION;
3756 }
3757
3758 {
3759 sp<Camera3Device> parent = mParent.promote();
3760 if (parent != nullptr) {
3761 List<PhysicalCameraSettings>::iterator it;
3762 for (it = captureRequest->mSettingsList.begin();
3763 it != captureRequest->mSettingsList.end(); it++) {
3764 if (parent->mUHRCropAndMeteringRegionMappers.find(it->cameraId) ==
3765 parent->mUHRCropAndMeteringRegionMappers.end()) {
3766 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3767 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3768 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3769 res);
3770 return INVALID_OPERATION;
3771 }
3772 continue;
3773 }
3774
3775 if (!captureRequest->mUHRCropAndMeteringRegionsUpdated) {
3776 res = parent->mUHRCropAndMeteringRegionMappers[it->cameraId].
3777 updateCaptureRequest(&(it->metadata));
3778 if (res != OK) {
3779 SET_ERR("RequestThread: Unable to correct capture requests "
3780 "for scaler crop region and metering regions for request "
3781 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3782 res);
3783 return INVALID_OPERATION;
3784 }
3785 captureRequest->mUHRCropAndMeteringRegionsUpdated = true;
3786 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3787 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3788 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3789 res);
3790 return INVALID_OPERATION;
3791 }
3792 }
3793 }
3794
3795 // Correct metadata regions for distortion correction if enabled
3796 for (it = captureRequest->mSettingsList.begin();
3797 it != captureRequest->mSettingsList.end(); it++) {
3798 if (parent->mDistortionMappers.find(it->cameraId) ==
3799 parent->mDistortionMappers.end()) {
3800 continue;
3801 }
3802
3803 if (!captureRequest->mDistortionCorrectionUpdated) {
3804 res = parent->mDistortionMappers[it->cameraId].correctCaptureRequest(
3805 &(it->metadata));
3806 if (res != OK) {
3807 SET_ERR("RequestThread: Unable to correct capture requests "
3808 "for lens distortion for request %d: %s (%d)",
3809 halRequest->frame_number, strerror(-res), res);
3810 return INVALID_OPERATION;
3811 }
3812 captureRequest->mDistortionCorrectionUpdated = true;
3813 }
3814 }
3815
3816 for (it = captureRequest->mSettingsList.begin();
3817 it != captureRequest->mSettingsList.end(); it++) {
3818 if (parent->mZoomRatioMappers.find(it->cameraId) ==
3819 parent->mZoomRatioMappers.end()) {
3820 continue;
3821 }
3822
3823 if (!captureRequest->mZoomRatioIs1x) {
3824 cameraIdsWithZoom.insert(it->cameraId);
3825 }
3826
3827 if (!captureRequest->mZoomRatioUpdated) {
3828 res = parent->mZoomRatioMappers[it->cameraId].updateCaptureRequest(
3829 &(it->metadata));
3830 if (res != OK) {
3831 SET_ERR("RequestThread: Unable to correct capture requests "
3832 "for zoom ratio for request %d: %s (%d)",
3833 halRequest->frame_number, strerror(-res), res);
3834 return INVALID_OPERATION;
3835 }
3836 captureRequest->mZoomRatioUpdated = true;
3837 }
3838 }
3839 if (captureRequest->mRotateAndCropAuto &&
3840 !captureRequest->mRotationAndCropUpdated) {
3841 for (it = captureRequest->mSettingsList.begin();
3842 it != captureRequest->mSettingsList.end(); it++) {
3843 auto mapper = parent->mRotateAndCropMappers.find(it->cameraId);
3844 if (mapper != parent->mRotateAndCropMappers.end()) {
3845 res = mapper->second.updateCaptureRequest(&(it->metadata));
3846 if (res != OK) {
3847 SET_ERR("RequestThread: Unable to correct capture requests "
3848 "for rotate-and-crop for request %d: %s (%d)",
3849 halRequest->frame_number, strerror(-res), res);
3850 return INVALID_OPERATION;
3851 }
3852 }
3853 }
3854 captureRequest->mRotationAndCropUpdated = true;
3855 }
3856
3857 for (it = captureRequest->mSettingsList.begin();
3858 it != captureRequest->mSettingsList.end(); it++) {
3859 res = hardware::cameraservice::utils::conversion::aidl::filterVndkKeys(
3860 mVndkVersion, it->metadata, false /*isStatic*/);
3861 if (res != OK) {
3862 SET_ERR("RequestThread: Failed during VNDK filter of capture requests "
3863 "%d: %s (%d)", halRequest->frame_number, strerror(-res), res);
3864 return INVALID_OPERATION;
3865 }
3866 }
3867 }
3868 }
3869
3870 /**
3871 * The request should be presorted so accesses in HAL
3872 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3873 */
3874 captureRequest->mSettingsList.begin()->metadata.sort();
3875 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
3876 mPrevRequest = captureRequest;
3877 mPrevCameraIdsWithZoom = cameraIdsWithZoom;
3878 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3879
3880 IF_ALOGV() {
3881 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3882 find_camera_metadata_ro_entry(
3883 halRequest->settings,
3884 ANDROID_CONTROL_AF_TRIGGER,
3885 &e
3886 );
3887 if (e.count > 0) {
3888 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3889 __FUNCTION__,
3890 halRequest->frame_number,
3891 e.data.u8[0]);
3892 }
3893 }
3894 } else {
3895 // leave request.settings NULL to indicate 'reuse latest given'
3896 ALOGVV("%s: Request settings are REUSED",
3897 __FUNCTION__);
3898 }
3899
3900 if (captureRequest->mSettingsList.size() > 1) {
3901 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
3902 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
3903 if (newRequest) {
3904 halRequest->physcam_settings =
3905 new const camera_metadata* [halRequest->num_physcam_settings];
3906 } else {
3907 halRequest->physcam_settings = nullptr;
3908 }
3909 auto it = ++captureRequest->mSettingsList.begin();
3910 size_t i = 0;
3911 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
3912 halRequest->physcam_id[i] = it->cameraId.c_str();
3913 if (newRequest) {
3914 it->metadata.sort();
3915 halRequest->physcam_settings[i] = it->metadata.getAndLock();
3916 }
3917 }
3918 }
3919
3920 uint32_t totalNumBuffers = 0;
3921
3922 // Fill in buffers
3923 if (captureRequest->mInputStream != NULL) {
3924 halRequest->input_buffer = &captureRequest->mInputBuffer;
3925
3926 halRequest->input_width = captureRequest->mInputBufferSize.width;
3927 halRequest->input_height = captureRequest->mInputBufferSize.height;
3928 totalNumBuffers += 1;
3929 } else {
3930 halRequest->input_buffer = NULL;
3931 }
3932
3933 outputBuffers->insertAt(camera_stream_buffer_t(), 0,
3934 captureRequest->mOutputStreams.size());
3935 halRequest->output_buffers = outputBuffers->array();
3936 std::set<std::set<String8>> requestedPhysicalCameras;
3937
3938 sp<Camera3Device> parent = mParent.promote();
3939 if (parent == NULL) {
3940 // Should not happen, and nowhere to send errors to, so just log it
3941 CLOGE("RequestThread: Parent is gone");
3942 return INVALID_OPERATION;
3943 }
3944 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
3945
3946 SurfaceMap uniqueSurfaceIdMap;
3947 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
3948 sp<Camera3OutputStreamInterface> outputStream =
3949 captureRequest->mOutputStreams.editItemAt(j);
3950 int streamId = outputStream->getId();
3951
3952 // Prepare video buffers for high speed recording on the first video request.
3953 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3954 // Only try to prepare video stream on the first video request.
3955 mPrepareVideoStream = false;
3956
3957 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
3958 false /*blockRequest*/);
3959 while (res == NOT_ENOUGH_DATA) {
3960 res = outputStream->prepareNextBuffer();
3961 }
3962 if (res != OK) {
3963 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3964 __FUNCTION__, strerror(-res), res);
3965 outputStream->cancelPrepare();
3966 }
3967 }
3968
3969 std::vector<size_t> uniqueSurfaceIds;
3970 res = outputStream->getUniqueSurfaceIds(
3971 captureRequest->mOutputSurfaces[streamId],
3972 &uniqueSurfaceIds);
3973 // INVALID_OPERATION is normal output for streams not supporting surfaceIds
3974 if (res != OK && res != INVALID_OPERATION) {
3975 ALOGE("%s: failed to query stream %d unique surface IDs",
3976 __FUNCTION__, streamId);
3977 return res;
3978 }
3979 if (res == OK) {
3980 uniqueSurfaceIdMap.insert({streamId, std::move(uniqueSurfaceIds)});
3981 }
3982
3983 if (mUseHalBufManager) {
3984 if (outputStream->isAbandoned()) {
3985 ALOGV("%s: stream %d is abandoned, skipping request", __FUNCTION__, streamId);
3986 return TIMED_OUT;
3987 }
3988 // HAL will request buffer through requestStreamBuffer API
3989 camera_stream_buffer_t& buffer = outputBuffers->editItemAt(j);
3990 buffer.stream = outputStream->asHalStream();
3991 buffer.buffer = nullptr;
3992 buffer.status = CAMERA_BUFFER_STATUS_OK;
3993 buffer.acquire_fence = -1;
3994 buffer.release_fence = -1;
3995 // Mark the output stream as unpreparable to block clients from calling
3996 // 'prepare' after this request reaches CameraHal and before the respective
3997 // buffers are requested.
3998 outputStream->markUnpreparable();
3999 } else {
4000 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
4001 waitDuration,
4002 captureRequest->mOutputSurfaces[streamId]);
4003 if (res != OK) {
4004 // Can't get output buffer from gralloc queue - this could be due to
4005 // abandoned queue or other consumer misbehavior, so not a fatal
4006 // error
4007 ALOGV("RequestThread: Can't get output buffer, skipping request:"
4008 " %s (%d)", strerror(-res), res);
4009
4010 return TIMED_OUT;
4011 }
4012 }
4013
4014 {
4015 sp<Camera3Device> parent = mParent.promote();
4016 if (parent != nullptr) {
4017 const String8& streamCameraId = outputStream->getPhysicalCameraId();
4018 // Consider the case where clients are sending a single logical camera request
4019 // to physical output/outputs
4020 bool singleRequest = captureRequest->mSettingsList.size() == 1;
4021 for (const auto& settings : captureRequest->mSettingsList) {
4022 if (((streamCameraId.isEmpty() || singleRequest) &&
4023 parent->getId() == settings.cameraId.c_str()) ||
4024 streamCameraId == settings.cameraId.c_str()) {
4025 outputStream->fireBufferRequestForFrameNumber(
4026 captureRequest->mResultExtras.frameNumber,
4027 settings.metadata);
4028 }
4029 }
4030 }
4031 }
4032
4033 String8 physicalCameraId = outputStream->getPhysicalCameraId();
4034 int32_t streamGroupId = outputStream->getHalStreamGroupId();
4035 if (streamGroupId != -1 && mGroupIdPhysicalCameraMap.count(streamGroupId) == 1) {
4036 requestedPhysicalCameras.insert(mGroupIdPhysicalCameraMap[streamGroupId]);
4037 } else if (!physicalCameraId.isEmpty()) {
4038 requestedPhysicalCameras.insert(std::set<String8>({physicalCameraId}));
4039 }
4040 halRequest->num_output_buffers++;
4041 }
4042 totalNumBuffers += halRequest->num_output_buffers;
4043
4044 // Log request in the in-flight queue
4045 // If this request list is for constrained high speed recording (not
4046 // preview), and the current request is not the last one in the batch,
4047 // do not send callback to the app.
4048 bool hasCallback = true;
4049 if (batchedRequest && i != mNextRequests.size()-1) {
4050 hasCallback = false;
4051 }
4052 bool isStillCapture = false;
4053 bool isZslCapture = false;
4054 const camera_metadata_t* settings = halRequest->settings;
4055 bool shouldUnlockSettings = false;
4056 if (settings == nullptr) {
4057 shouldUnlockSettings = true;
4058 settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
4059 }
4060 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
4061 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4062 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
4063 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
4064 isStillCapture = true;
4065 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
4066 }
4067
4068 e = camera_metadata_ro_entry_t();
4069 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
4070 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
4071 isZslCapture = true;
4072 }
4073 }
4074 auto expectedDurationInfo = calculateExpectedDurationRange(settings);
4075 res = parent->registerInFlight(halRequest->frame_number,
4076 totalNumBuffers, captureRequest->mResultExtras,
4077 /*hasInput*/halRequest->input_buffer != NULL,
4078 hasCallback,
4079 expectedDurationInfo.minDuration,
4080 expectedDurationInfo.maxDuration,
4081 expectedDurationInfo.isFixedFps,
4082 requestedPhysicalCameras, isStillCapture, isZslCapture,
4083 captureRequest->mRotateAndCropAuto, captureRequest->mAutoframingAuto,
4084 mPrevCameraIdsWithZoom,
4085 (mUseHalBufManager) ? uniqueSurfaceIdMap :
4086 SurfaceMap{}, captureRequest->mRequestTimeNs);
4087 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
4088 ", burstId = %" PRId32 ".",
4089 __FUNCTION__,
4090 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
4091 captureRequest->mResultExtras.burstId);
4092
4093 if (shouldUnlockSettings) {
4094 captureRequest->mSettingsList.begin()->metadata.unlock(settings);
4095 }
4096
4097 if (res != OK) {
4098 SET_ERR("RequestThread: Unable to register new in-flight request:"
4099 " %s (%d)", strerror(-res), res);
4100 return INVALID_OPERATION;
4101 }
4102 }
4103
4104 return OK;
4105 }
4106
getLatestRequest() const4107 CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
4108 ATRACE_CALL();
4109 Mutex::Autolock al(mLatestRequestMutex);
4110
4111 ALOGV("RequestThread::%s", __FUNCTION__);
4112
4113 return mLatestRequest;
4114 }
4115
isStreamPending(sp<Camera3StreamInterface> & stream)4116 bool Camera3Device::RequestThread::isStreamPending(
4117 sp<Camera3StreamInterface>& stream) {
4118 ATRACE_CALL();
4119 Mutex::Autolock l(mRequestLock);
4120
4121 for (const auto& nextRequest : mNextRequests) {
4122 if (!nextRequest.submitted) {
4123 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
4124 if (stream == s) return true;
4125 }
4126 if (stream == nextRequest.captureRequest->mInputStream) return true;
4127 }
4128 }
4129
4130 for (const auto& request : mRequestQueue) {
4131 for (const auto& s : request->mOutputStreams) {
4132 if (stream == s) return true;
4133 }
4134 if (stream == request->mInputStream) return true;
4135 }
4136
4137 for (const auto& request : mRepeatingRequests) {
4138 for (const auto& s : request->mOutputStreams) {
4139 if (stream == s) return true;
4140 }
4141 if (stream == request->mInputStream) return true;
4142 }
4143
4144 return false;
4145 }
4146
isOutputSurfacePending(int streamId,size_t surfaceId)4147 bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
4148 ATRACE_CALL();
4149 Mutex::Autolock l(mRequestLock);
4150
4151 for (const auto& nextRequest : mNextRequests) {
4152 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
4153 if (s.first == streamId) {
4154 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4155 if (it != s.second.end()) {
4156 return true;
4157 }
4158 }
4159 }
4160 }
4161
4162 for (const auto& request : mRequestQueue) {
4163 for (const auto& s : request->mOutputSurfaces) {
4164 if (s.first == streamId) {
4165 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4166 if (it != s.second.end()) {
4167 return true;
4168 }
4169 }
4170 }
4171 }
4172
4173 for (const auto& request : mRepeatingRequests) {
4174 for (const auto& s : request->mOutputSurfaces) {
4175 if (s.first == streamId) {
4176 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4177 if (it != s.second.end()) {
4178 return true;
4179 }
4180 }
4181 }
4182 }
4183
4184 return false;
4185 }
4186
signalPipelineDrain(const std::vector<int> & streamIds)4187 void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
4188 if (!mUseHalBufManager) {
4189 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
4190 return;
4191 }
4192
4193 Mutex::Autolock pl(mPauseLock);
4194 if (mPaused) {
4195 mInterface->signalPipelineDrain(streamIds);
4196 return;
4197 }
4198 // If request thread is still busy, wait until paused then notify HAL
4199 mNotifyPipelineDrain = true;
4200 mStreamIdsToBeDrained = streamIds;
4201 }
4202
resetPipelineDrain()4203 void Camera3Device::RequestThread::resetPipelineDrain() {
4204 Mutex::Autolock pl(mPauseLock);
4205 mNotifyPipelineDrain = false;
4206 mStreamIdsToBeDrained.clear();
4207 }
4208
clearPreviousRequest()4209 void Camera3Device::RequestThread::clearPreviousRequest() {
4210 Mutex::Autolock l(mRequestLock);
4211 mPrevRequest.clear();
4212 }
4213
setRotateAndCropAutoBehavior(camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue)4214 status_t Camera3Device::RequestThread::setRotateAndCropAutoBehavior(
4215 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
4216 ATRACE_CALL();
4217 Mutex::Autolock l(mTriggerMutex);
4218 mRotateAndCropOverride = rotateAndCropValue;
4219 return OK;
4220 }
4221
setAutoframingAutoBehaviour(camera_metadata_enum_android_control_autoframing_t autoframingValue)4222 status_t Camera3Device::RequestThread::setAutoframingAutoBehaviour(
4223 camera_metadata_enum_android_control_autoframing_t autoframingValue) {
4224 ATRACE_CALL();
4225 Mutex::Autolock l(mTriggerMutex);
4226 mAutoframingOverride = autoframingValue;
4227 return OK;
4228 }
4229
setComposerSurface(bool composerSurfacePresent)4230 status_t Camera3Device::RequestThread::setComposerSurface(bool composerSurfacePresent) {
4231 ATRACE_CALL();
4232 Mutex::Autolock l(mTriggerMutex);
4233 mComposerOutput = composerSurfacePresent;
4234 return OK;
4235 }
4236
setCameraMute(int32_t muteMode)4237 status_t Camera3Device::RequestThread::setCameraMute(int32_t muteMode) {
4238 ATRACE_CALL();
4239 Mutex::Autolock l(mTriggerMutex);
4240 if (muteMode != mCameraMute) {
4241 mCameraMute = muteMode;
4242 mCameraMuteChanged = true;
4243 }
4244 return OK;
4245 }
4246
setZoomOverride(int32_t zoomOverride)4247 status_t Camera3Device::RequestThread::setZoomOverride(int32_t zoomOverride) {
4248 ATRACE_CALL();
4249 Mutex::Autolock l(mTriggerMutex);
4250 mSettingsOverride = zoomOverride;
4251 return OK;
4252 }
4253
getExpectedInFlightDuration()4254 nsecs_t Camera3Device::getExpectedInFlightDuration() {
4255 ATRACE_CALL();
4256 std::lock_guard<std::mutex> l(mInFlightLock);
4257 return mExpectedInflightDuration > kMinInflightDuration ?
4258 mExpectedInflightDuration : kMinInflightDuration;
4259 }
4260
cleanupPhysicalSettings(sp<CaptureRequest> request,camera_capture_request_t * halRequest)4261 void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
4262 camera_capture_request_t *halRequest) {
4263 if ((request == nullptr) || (halRequest == nullptr)) {
4264 ALOGE("%s: Invalid request!", __FUNCTION__);
4265 return;
4266 }
4267
4268 if (halRequest->num_physcam_settings > 0) {
4269 if (halRequest->physcam_id != nullptr) {
4270 delete [] halRequest->physcam_id;
4271 halRequest->physcam_id = nullptr;
4272 }
4273 if (halRequest->physcam_settings != nullptr) {
4274 auto it = ++(request->mSettingsList.begin());
4275 size_t i = 0;
4276 for (; it != request->mSettingsList.end(); it++, i++) {
4277 it->metadata.unlock(halRequest->physcam_settings[i]);
4278 }
4279 delete [] halRequest->physcam_settings;
4280 halRequest->physcam_settings = nullptr;
4281 }
4282 }
4283 }
4284
setCameraServiceWatchdog(bool enabled)4285 status_t Camera3Device::setCameraServiceWatchdog(bool enabled) {
4286 Mutex::Autolock il(mInterfaceLock);
4287 Mutex::Autolock l(mLock);
4288
4289 if (mCameraServiceWatchdog != NULL) {
4290 mCameraServiceWatchdog->setEnabled(enabled);
4291 }
4292
4293 return OK;
4294 }
4295
setStreamUseCaseOverrides(const std::vector<int64_t> & useCaseOverrides)4296 void Camera3Device::setStreamUseCaseOverrides(
4297 const std::vector<int64_t>& useCaseOverrides) {
4298 Mutex::Autolock il(mInterfaceLock);
4299 Mutex::Autolock l(mLock);
4300 mStreamUseCaseOverrides = useCaseOverrides;
4301 }
4302
clearStreamUseCaseOverrides()4303 void Camera3Device::clearStreamUseCaseOverrides() {
4304 Mutex::Autolock il(mInterfaceLock);
4305 Mutex::Autolock l(mLock);
4306 mStreamUseCaseOverrides.clear();
4307 }
4308
hasDeviceError()4309 bool Camera3Device::hasDeviceError() {
4310 Mutex::Autolock il(mInterfaceLock);
4311 Mutex::Autolock l(mLock);
4312 return mStatus == STATUS_ERROR;
4313 }
4314
cleanUpFailedRequests(bool sendRequestError)4315 void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4316 if (mNextRequests.empty()) {
4317 return;
4318 }
4319
4320 for (auto& nextRequest : mNextRequests) {
4321 // Skip the ones that have been submitted successfully.
4322 if (nextRequest.submitted) {
4323 continue;
4324 }
4325
4326 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
4327 camera_capture_request_t* halRequest = &nextRequest.halRequest;
4328 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
4329
4330 if (halRequest->settings != NULL) {
4331 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
4332 }
4333
4334 cleanupPhysicalSettings(captureRequest, halRequest);
4335
4336 if (captureRequest->mInputStream != NULL) {
4337 captureRequest->mInputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
4338 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4339 }
4340
4341 // No output buffer can be returned when using HAL buffer manager
4342 if (!mUseHalBufManager) {
4343 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
4344 //Buffers that failed processing could still have
4345 //valid acquire fence.
4346 int acquireFence = (*outputBuffers)[i].acquire_fence;
4347 if (0 <= acquireFence) {
4348 close(acquireFence);
4349 outputBuffers->editItemAt(i).acquire_fence = -1;
4350 }
4351 outputBuffers->editItemAt(i).status = CAMERA_BUFFER_STATUS_ERROR;
4352 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i],
4353 /*timestamp*/0, /*readoutTimestamp*/0,
4354 /*timestampIncreasing*/true, std::vector<size_t> (),
4355 captureRequest->mResultExtras.frameNumber);
4356 }
4357 }
4358
4359 if (sendRequestError) {
4360 Mutex::Autolock l(mRequestLock);
4361 sp<NotificationListener> listener = mListener.promote();
4362 if (listener != NULL) {
4363 listener->notifyError(
4364 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
4365 captureRequest->mResultExtras);
4366 }
4367 {
4368 Mutex::Autolock al(mLatestRequestMutex);
4369
4370 mLatestFailedRequestId = captureRequest->mResultExtras.requestId;
4371 mLatestRequestSignal.signal();
4372 }
4373 }
4374
4375 // Remove yet-to-be submitted inflight request from inflightMap
4376 {
4377 sp<Camera3Device> parent = mParent.promote();
4378 if (parent != NULL) {
4379 std::lock_guard<std::mutex> l(parent->mInFlightLock);
4380 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4381 if (idx >= 0) {
4382 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4383 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4384 parent->removeInFlightMapEntryLocked(idx);
4385 }
4386 }
4387 }
4388 }
4389
4390 Mutex::Autolock l(mRequestLock);
4391 mNextRequests.clear();
4392 }
4393
waitForNextRequestBatch()4394 void Camera3Device::RequestThread::waitForNextRequestBatch() {
4395 ATRACE_CALL();
4396 // Optimized a bit for the simple steady-state case (single repeating
4397 // request), to avoid putting that request in the queue temporarily.
4398 Mutex::Autolock l(mRequestLock);
4399
4400 assert(mNextRequests.empty());
4401
4402 NextRequest nextRequest;
4403 nextRequest.captureRequest = waitForNextRequestLocked();
4404 if (nextRequest.captureRequest == nullptr) {
4405 return;
4406 }
4407
4408 nextRequest.halRequest = camera_capture_request_t();
4409 nextRequest.submitted = false;
4410 mNextRequests.add(nextRequest);
4411
4412 // Wait for additional requests
4413 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4414
4415 for (size_t i = 1; i < batchSize; i++) {
4416 NextRequest additionalRequest;
4417 additionalRequest.captureRequest = waitForNextRequestLocked();
4418 if (additionalRequest.captureRequest == nullptr) {
4419 break;
4420 }
4421
4422 additionalRequest.halRequest = camera_capture_request_t();
4423 additionalRequest.submitted = false;
4424 mNextRequests.add(additionalRequest);
4425 }
4426
4427 if (mNextRequests.size() < batchSize) {
4428 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
4429 mNextRequests.size(), batchSize);
4430 cleanUpFailedRequests(/*sendRequestError*/true);
4431 }
4432
4433 return;
4434 }
4435
setRequestClearing()4436 void Camera3Device::RequestThread::setRequestClearing() {
4437 Mutex::Autolock l(mRequestLock);
4438 mRequestClearing = true;
4439 }
4440
4441 sp<Camera3Device::CaptureRequest>
waitForNextRequestLocked()4442 Camera3Device::RequestThread::waitForNextRequestLocked() {
4443 status_t res;
4444 sp<CaptureRequest> nextRequest;
4445
4446 while (mRequestQueue.empty()) {
4447 if (!mRepeatingRequests.empty()) {
4448 // Always atomically enqueue all requests in a repeating request
4449 // list. Guarantees a complete in-sequence set of captures to
4450 // application.
4451 const RequestList &requests = mRepeatingRequests;
4452 if (mFirstRepeating) {
4453 mFirstRepeating = false;
4454 } else {
4455 for (auto& request : requests) {
4456 // For repeating requests, override timestamp request using
4457 // the time a request is inserted into the request queue,
4458 // because the original repeating request will have an old
4459 // fixed timestamp.
4460 request->mRequestTimeNs = systemTime();
4461 }
4462 }
4463 RequestList::const_iterator firstRequest =
4464 requests.begin();
4465 nextRequest = *firstRequest;
4466 mRequestQueue.insert(mRequestQueue.end(),
4467 ++firstRequest,
4468 requests.end());
4469 // No need to wait any longer
4470
4471 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
4472
4473 break;
4474 }
4475
4476 if (!mRequestClearing) {
4477 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4478 }
4479
4480 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4481 exitPending()) {
4482 Mutex::Autolock pl(mPauseLock);
4483 if (mPaused == false) {
4484 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
4485 mPaused = true;
4486 if (mNotifyPipelineDrain) {
4487 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4488 mNotifyPipelineDrain = false;
4489 mStreamIdsToBeDrained.clear();
4490 }
4491 // Let the tracker know
4492 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4493 if (statusTracker != 0) {
4494 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4495 }
4496 sp<Camera3Device> parent = mParent.promote();
4497 if (parent != nullptr) {
4498 parent->mRequestBufferSM.onRequestThreadPaused();
4499 }
4500 }
4501 mRequestClearing = false;
4502 // Stop waiting for now and let thread management happen
4503 return NULL;
4504 }
4505 }
4506
4507 if (nextRequest == NULL) {
4508 // Don't have a repeating request already in hand, so queue
4509 // must have an entry now.
4510 RequestList::iterator firstRequest =
4511 mRequestQueue.begin();
4512 nextRequest = *firstRequest;
4513 mRequestQueue.erase(firstRequest);
4514 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4515 sp<NotificationListener> listener = mListener.promote();
4516 if (listener != NULL) {
4517 listener->notifyRequestQueueEmpty();
4518 }
4519 }
4520 }
4521
4522 // In case we've been unpaused by setPaused clearing mDoPause, need to
4523 // update internal pause state (capture/setRepeatingRequest unpause
4524 // directly).
4525 Mutex::Autolock pl(mPauseLock);
4526 if (mPaused) {
4527 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4528 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4529 if (statusTracker != 0) {
4530 statusTracker->markComponentActive(mStatusId);
4531 }
4532 }
4533 mPaused = false;
4534
4535 // Check if we've reconfigured since last time, and reset the preview
4536 // request if so. Can't use 'NULL request == repeat' across configure calls.
4537 if (mReconfigured) {
4538 mPrevRequest.clear();
4539 mReconfigured = false;
4540 }
4541
4542 if (nextRequest != NULL) {
4543 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
4544 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4545 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
4546
4547 // Since RequestThread::clear() removes buffers from the input stream,
4548 // get the right buffer here before unlocking mRequestLock
4549 if (nextRequest->mInputStream != NULL) {
4550 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer,
4551 &nextRequest->mInputBufferSize);
4552 if (res != OK) {
4553 // Can't get input buffer from gralloc queue - this could be due to
4554 // disconnected queue or other producer misbehavior, so not a fatal
4555 // error
4556 ALOGE("%s: Can't get input buffer, skipping request:"
4557 " %s (%d)", __FUNCTION__, strerror(-res), res);
4558
4559 sp<NotificationListener> listener = mListener.promote();
4560 if (listener != NULL) {
4561 listener->notifyError(
4562 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
4563 nextRequest->mResultExtras);
4564 }
4565 return NULL;
4566 }
4567 }
4568 }
4569
4570 return nextRequest;
4571 }
4572
waitIfPaused()4573 bool Camera3Device::RequestThread::waitIfPaused() {
4574 ATRACE_CALL();
4575 status_t res;
4576 Mutex::Autolock l(mPauseLock);
4577 while (mDoPause) {
4578 if (mPaused == false) {
4579 mPaused = true;
4580 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
4581 if (mNotifyPipelineDrain) {
4582 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4583 mNotifyPipelineDrain = false;
4584 mStreamIdsToBeDrained.clear();
4585 }
4586 // Let the tracker know
4587 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4588 if (statusTracker != 0) {
4589 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4590 }
4591 sp<Camera3Device> parent = mParent.promote();
4592 if (parent != nullptr) {
4593 parent->mRequestBufferSM.onRequestThreadPaused();
4594 }
4595 }
4596
4597 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
4598 if (res == TIMED_OUT || exitPending()) {
4599 return true;
4600 }
4601 }
4602 // We don't set mPaused to false here, because waitForNextRequest needs
4603 // to further manage the paused state in case of starvation.
4604 return false;
4605 }
4606
unpauseForNewRequests()4607 void Camera3Device::RequestThread::unpauseForNewRequests() {
4608 ATRACE_CALL();
4609 // With work to do, mark thread as unpaused.
4610 // If paused by request (setPaused), don't resume, to avoid
4611 // extra signaling/waiting overhead to waitUntilPaused
4612 mRequestSignal.signal();
4613 Mutex::Autolock p(mPauseLock);
4614 if (!mDoPause) {
4615 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4616 if (mPaused) {
4617 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4618 if (statusTracker != 0) {
4619 statusTracker->markComponentActive(mStatusId);
4620 }
4621 }
4622 mPaused = false;
4623 }
4624 }
4625
setErrorState(const char * fmt,...)4626 void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4627 sp<Camera3Device> parent = mParent.promote();
4628 if (parent != NULL) {
4629 va_list args;
4630 va_start(args, fmt);
4631
4632 parent->setErrorStateV(fmt, args);
4633
4634 va_end(args);
4635 }
4636 }
4637
insertTriggers(const sp<CaptureRequest> & request)4638 status_t Camera3Device::RequestThread::insertTriggers(
4639 const sp<CaptureRequest> &request) {
4640 ATRACE_CALL();
4641 Mutex::Autolock al(mTriggerMutex);
4642
4643 sp<Camera3Device> parent = mParent.promote();
4644 if (parent == NULL) {
4645 CLOGE("RequestThread: Parent is gone");
4646 return DEAD_OBJECT;
4647 }
4648
4649 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4650 size_t count = mTriggerMap.size();
4651
4652 for (size_t i = 0; i < count; ++i) {
4653 RequestTrigger trigger = mTriggerMap.valueAt(i);
4654 uint32_t tag = trigger.metadataTag;
4655
4656 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4657 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4658 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
4659 if (isAeTrigger) {
4660 request->mResultExtras.precaptureTriggerId = triggerId;
4661 mCurrentPreCaptureTriggerId = triggerId;
4662 } else {
4663 request->mResultExtras.afTriggerId = triggerId;
4664 mCurrentAfTriggerId = triggerId;
4665 }
4666 continue;
4667 }
4668
4669 camera_metadata_entry entry = metadata.find(tag);
4670
4671 if (entry.count > 0) {
4672 /**
4673 * Already has an entry for this trigger in the request.
4674 * Rewrite it with our requested trigger value.
4675 */
4676 RequestTrigger oldTrigger = trigger;
4677
4678 oldTrigger.entryValue = entry.data.u8[0];
4679
4680 mTriggerReplacedMap.add(tag, oldTrigger);
4681 } else {
4682 /**
4683 * More typical, no trigger entry, so we just add it
4684 */
4685 mTriggerRemovedMap.add(tag, trigger);
4686 }
4687
4688 status_t res;
4689
4690 switch (trigger.getTagType()) {
4691 case TYPE_BYTE: {
4692 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4693 res = metadata.update(tag,
4694 &entryValue,
4695 /*count*/1);
4696 break;
4697 }
4698 case TYPE_INT32:
4699 res = metadata.update(tag,
4700 &trigger.entryValue,
4701 /*count*/1);
4702 break;
4703 default:
4704 ALOGE("%s: Type not supported: 0x%x",
4705 __FUNCTION__,
4706 trigger.getTagType());
4707 return INVALID_OPERATION;
4708 }
4709
4710 if (res != OK) {
4711 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4712 ", value %d", __FUNCTION__, trigger.getTagName(),
4713 trigger.entryValue);
4714 return res;
4715 }
4716
4717 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4718 trigger.getTagName(),
4719 trigger.entryValue);
4720 }
4721
4722 mTriggerMap.clear();
4723
4724 return count;
4725 }
4726
removeTriggers(const sp<CaptureRequest> & request)4727 status_t Camera3Device::RequestThread::removeTriggers(
4728 const sp<CaptureRequest> &request) {
4729 ATRACE_CALL();
4730 Mutex::Autolock al(mTriggerMutex);
4731
4732 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4733
4734 /**
4735 * Replace all old entries with their old values.
4736 */
4737 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4738 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4739
4740 status_t res;
4741
4742 uint32_t tag = trigger.metadataTag;
4743 switch (trigger.getTagType()) {
4744 case TYPE_BYTE: {
4745 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4746 res = metadata.update(tag,
4747 &entryValue,
4748 /*count*/1);
4749 break;
4750 }
4751 case TYPE_INT32:
4752 res = metadata.update(tag,
4753 &trigger.entryValue,
4754 /*count*/1);
4755 break;
4756 default:
4757 ALOGE("%s: Type not supported: 0x%x",
4758 __FUNCTION__,
4759 trigger.getTagType());
4760 return INVALID_OPERATION;
4761 }
4762
4763 if (res != OK) {
4764 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4765 ", trigger value %d", __FUNCTION__,
4766 trigger.getTagName(), trigger.entryValue);
4767 return res;
4768 }
4769 }
4770 mTriggerReplacedMap.clear();
4771
4772 /**
4773 * Remove all new entries.
4774 */
4775 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4776 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4777 status_t res = metadata.erase(trigger.metadataTag);
4778
4779 if (res != OK) {
4780 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4781 ", trigger value %d", __FUNCTION__,
4782 trigger.getTagName(), trigger.entryValue);
4783 return res;
4784 }
4785 }
4786 mTriggerRemovedMap.clear();
4787
4788 return OK;
4789 }
4790
addFakeTriggerIds(const sp<CaptureRequest> & request)4791 status_t Camera3Device::RequestThread::addFakeTriggerIds(
4792 const sp<CaptureRequest> &request) {
4793 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
4794 static const int32_t fakeTriggerId = 1;
4795 status_t res;
4796
4797 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4798
4799 // If AF trigger is active, insert a fake AF trigger ID if none already
4800 // exists
4801 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4802 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4803 if (afTrigger.count > 0 &&
4804 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4805 afId.count == 0) {
4806 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &fakeTriggerId, 1);
4807 if (res != OK) return res;
4808 }
4809
4810 // If AE precapture trigger is active, insert a fake precapture trigger ID
4811 // if none already exists
4812 camera_metadata_entry pcTrigger =
4813 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4814 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4815 if (pcTrigger.count > 0 &&
4816 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4817 pcId.count == 0) {
4818 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
4819 &fakeTriggerId, 1);
4820 if (res != OK) return res;
4821 }
4822
4823 return OK;
4824 }
4825
overrideAutoRotateAndCrop(const sp<CaptureRequest> & request)4826 bool Camera3Device::RequestThread::overrideAutoRotateAndCrop(const sp<CaptureRequest> &request) {
4827 ATRACE_CALL();
4828 Mutex::Autolock l(mTriggerMutex);
4829 return Camera3Device::overrideAutoRotateAndCrop(request, this->mOverrideToPortrait,
4830 this->mRotateAndCropOverride);
4831 }
4832
overrideAutoRotateAndCrop(const sp<CaptureRequest> & request,bool overrideToPortrait,camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropOverride)4833 bool Camera3Device::overrideAutoRotateAndCrop(const sp<CaptureRequest> &request,
4834 bool overrideToPortrait,
4835 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropOverride) {
4836 ATRACE_CALL();
4837
4838 if (overrideToPortrait) {
4839 uint8_t rotateAndCrop_u8 = rotateAndCropOverride;
4840 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4841 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP,
4842 &rotateAndCrop_u8, 1);
4843 return true;
4844 }
4845
4846 if (request->mRotateAndCropAuto) {
4847 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4848
4849 auto rotateAndCropEntry = metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
4850 if (rotateAndCropEntry.count > 0) {
4851 if (rotateAndCropEntry.data.u8[0] == rotateAndCropOverride) {
4852 return false;
4853 } else {
4854 rotateAndCropEntry.data.u8[0] = rotateAndCropOverride;
4855 return true;
4856 }
4857 } else {
4858 uint8_t rotateAndCrop_u8 = rotateAndCropOverride;
4859 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP, &rotateAndCrop_u8, 1);
4860 return true;
4861 }
4862 }
4863
4864 return false;
4865 }
4866
overrideAutoframing(const sp<CaptureRequest> & request,camera_metadata_enum_android_control_autoframing_t autoframingOverride)4867 bool Camera3Device::overrideAutoframing(const sp<CaptureRequest> &request /*out*/,
4868 camera_metadata_enum_android_control_autoframing_t autoframingOverride) {
4869 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4870 auto autoframingEntry = metadata.find(ANDROID_CONTROL_AUTOFRAMING);
4871 if (autoframingEntry.count > 0) {
4872 if (autoframingEntry.data.u8[0] == autoframingOverride) {
4873 return false;
4874 } else {
4875 autoframingEntry.data.u8[0] = autoframingOverride;
4876 return true;
4877 }
4878 } else {
4879 uint8_t autoframing_u8 = autoframingOverride;
4880 metadata.update(ANDROID_CONTROL_AUTOFRAMING,
4881 &autoframing_u8, 1);
4882 return true;
4883 }
4884
4885 return false;
4886 }
4887
overrideAutoframing(const sp<CaptureRequest> & request)4888 bool Camera3Device::RequestThread::overrideAutoframing(const sp<CaptureRequest> &request) {
4889 ATRACE_CALL();
4890
4891 if (request->mAutoframingAuto) {
4892 Mutex::Autolock l(mTriggerMutex);
4893 return Camera3Device::overrideAutoframing(request, mAutoframingOverride);
4894 }
4895
4896 return false;
4897 }
4898
overrideTestPattern(const sp<CaptureRequest> & request)4899 bool Camera3Device::RequestThread::overrideTestPattern(
4900 const sp<CaptureRequest> &request) {
4901 ATRACE_CALL();
4902
4903 if (!mSupportCameraMute) return false;
4904
4905 Mutex::Autolock l(mTriggerMutex);
4906
4907 bool changed = false;
4908
4909 // For a multi-camera, the physical cameras support the same set of
4910 // test pattern modes as the logical camera.
4911 for (auto& settings : request->mSettingsList) {
4912 CameraMetadata &metadata = settings.metadata;
4913
4914 int32_t testPatternMode = settings.mOriginalTestPatternMode;
4915 int32_t testPatternData[4] = {
4916 settings.mOriginalTestPatternData[0],
4917 settings.mOriginalTestPatternData[1],
4918 settings.mOriginalTestPatternData[2],
4919 settings.mOriginalTestPatternData[3]
4920 };
4921 if (mCameraMute != ANDROID_SENSOR_TEST_PATTERN_MODE_OFF) {
4922 testPatternMode = mCameraMute;
4923 testPatternData[0] = 0;
4924 testPatternData[1] = 0;
4925 testPatternData[2] = 0;
4926 testPatternData[3] = 0;
4927 }
4928
4929 auto testPatternEntry = metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
4930 bool supportTestPatternModeKey = settings.mHasTestPatternModeTag;
4931 if (testPatternEntry.count > 0) {
4932 if (testPatternEntry.data.i32[0] != testPatternMode) {
4933 testPatternEntry.data.i32[0] = testPatternMode;
4934 changed = true;
4935 }
4936 } else if (supportTestPatternModeKey) {
4937 metadata.update(ANDROID_SENSOR_TEST_PATTERN_MODE,
4938 &testPatternMode, 1);
4939 changed = true;
4940 }
4941
4942 auto testPatternColor = metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
4943 bool supportTestPatternDataKey = settings.mHasTestPatternDataTag;
4944 if (testPatternColor.count >= 4) {
4945 for (size_t i = 0; i < 4; i++) {
4946 if (testPatternColor.data.i32[i] != testPatternData[i]) {
4947 testPatternColor.data.i32[i] = testPatternData[i];
4948 changed = true;
4949 }
4950 }
4951 } else if (supportTestPatternDataKey) {
4952 metadata.update(ANDROID_SENSOR_TEST_PATTERN_DATA,
4953 testPatternData, 4);
4954 changed = true;
4955 }
4956 }
4957
4958 return changed;
4959 }
4960
overrideSettingsOverride(const sp<CaptureRequest> & request)4961 bool Camera3Device::RequestThread::overrideSettingsOverride(
4962 const sp<CaptureRequest> &request) {
4963 ATRACE_CALL();
4964
4965 if (!mSupportSettingsOverride) return false;
4966
4967 Mutex::Autolock l(mTriggerMutex);
4968
4969 // For a multi-camera, only override the logical camera's metadata.
4970 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4971 camera_metadata_entry entry = metadata.find(ANDROID_CONTROL_SETTINGS_OVERRIDE);
4972 int32_t originalValue = request->mSettingsList.begin()->mOriginalSettingsOverride;
4973 if (mSettingsOverride != -1 &&
4974 (entry.count == 0 || entry.data.i32[0] != mSettingsOverride)) {
4975 metadata.update(ANDROID_CONTROL_SETTINGS_OVERRIDE,
4976 &mSettingsOverride, 1);
4977 return true;
4978 } else if (mSettingsOverride == -1 &&
4979 (entry.count == 0 || entry.data.i32[0] != originalValue)) {
4980 metadata.update(ANDROID_CONTROL_SETTINGS_OVERRIDE,
4981 &originalValue, 1);
4982 return true;
4983 }
4984
4985 return false;
4986 }
4987
setHalInterface(sp<HalInterface> newHalInterface)4988 status_t Camera3Device::RequestThread::setHalInterface(
4989 sp<HalInterface> newHalInterface) {
4990 if (newHalInterface.get() == nullptr) {
4991 ALOGE("%s: The newHalInterface does not exist!", __FUNCTION__);
4992 return DEAD_OBJECT;
4993 }
4994
4995 mInterface = newHalInterface;
4996
4997 return OK;
4998 }
4999
5000 /**
5001 * PreparerThread inner class methods
5002 */
5003
PreparerThread()5004 Camera3Device::PreparerThread::PreparerThread() :
5005 Thread(/*canCallJava*/false), mListener(nullptr),
5006 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
5007 }
5008
~PreparerThread()5009 Camera3Device::PreparerThread::~PreparerThread() {
5010 Thread::requestExitAndWait();
5011 if (mCurrentStream != nullptr) {
5012 mCurrentStream->cancelPrepare();
5013 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5014 mCurrentStream.clear();
5015 }
5016 clear();
5017 }
5018
prepare(int maxCount,sp<Camera3StreamInterface> & stream)5019 status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
5020 ATRACE_CALL();
5021 status_t res;
5022
5023 Mutex::Autolock l(mLock);
5024 sp<NotificationListener> listener = mListener.promote();
5025
5026 res = stream->startPrepare(maxCount, true /*blockRequest*/);
5027 if (res == OK) {
5028 // No preparation needed, fire listener right off
5029 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
5030 if (listener != NULL) {
5031 listener->notifyPrepared(stream->getId());
5032 }
5033 return OK;
5034 } else if (res != NOT_ENOUGH_DATA) {
5035 return res;
5036 }
5037
5038 // Need to prepare, start up thread if necessary
5039 if (!mActive) {
5040 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
5041 // isn't running
5042 Thread::requestExitAndWait();
5043 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5044 if (res != OK) {
5045 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
5046 if (listener != NULL) {
5047 listener->notifyPrepared(stream->getId());
5048 }
5049 return res;
5050 }
5051 mCancelNow = false;
5052 mActive = true;
5053 ALOGV("%s: Preparer stream started", __FUNCTION__);
5054 }
5055
5056 // queue up the work
5057 mPendingStreams.push_back(
5058 std::tuple<int, sp<camera3::Camera3StreamInterface>>(maxCount, stream));
5059 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
5060
5061 return OK;
5062 }
5063
pause()5064 void Camera3Device::PreparerThread::pause() {
5065 ATRACE_CALL();
5066
5067 Mutex::Autolock l(mLock);
5068
5069 std::list<std::tuple<int, sp<camera3::Camera3StreamInterface>>> pendingStreams;
5070 pendingStreams.insert(pendingStreams.begin(), mPendingStreams.begin(), mPendingStreams.end());
5071 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
5072 int currentMaxCount = mCurrentMaxCount;
5073 mPendingStreams.clear();
5074 mCancelNow = true;
5075 while (mActive) {
5076 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
5077 if (res == TIMED_OUT) {
5078 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
5079 return;
5080 } else if (res != OK) {
5081 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
5082 return;
5083 }
5084 }
5085
5086 //Check whether the prepare thread was able to complete the current
5087 //stream. In case work is still pending emplace it along with the rest
5088 //of the streams in the pending list.
5089 if (currentStream != nullptr) {
5090 if (!mCurrentPrepareComplete) {
5091 pendingStreams.push_back(std::tuple(currentMaxCount, currentStream));
5092 }
5093 }
5094
5095 mPendingStreams.insert(mPendingStreams.begin(), pendingStreams.begin(), pendingStreams.end());
5096 for (const auto& it : mPendingStreams) {
5097 std::get<1>(it)->cancelPrepare();
5098 }
5099 }
5100
resume()5101 status_t Camera3Device::PreparerThread::resume() {
5102 ATRACE_CALL();
5103 ALOGV("%s: PreparerThread", __FUNCTION__);
5104 status_t res;
5105
5106 Mutex::Autolock l(mLock);
5107 sp<NotificationListener> listener = mListener.promote();
5108
5109 if (mActive) {
5110 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
5111 return NO_INIT;
5112 }
5113
5114 auto it = mPendingStreams.begin();
5115 for (; it != mPendingStreams.end();) {
5116 res = std::get<1>(*it)->startPrepare(std::get<0>(*it), true /*blockRequest*/);
5117 if (res == OK) {
5118 if (listener != NULL) {
5119 listener->notifyPrepared(std::get<1>(*it)->getId());
5120 }
5121 it = mPendingStreams.erase(it);
5122 } else if (res != NOT_ENOUGH_DATA) {
5123 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
5124 res, strerror(-res));
5125 it = mPendingStreams.erase(it);
5126 } else {
5127 it++;
5128 }
5129 }
5130
5131 if (mPendingStreams.empty()) {
5132 return OK;
5133 }
5134
5135 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5136 if (res != OK) {
5137 ALOGE("%s: Unable to start preparer stream: %d (%s)",
5138 __FUNCTION__, res, strerror(-res));
5139 return res;
5140 }
5141 mCancelNow = false;
5142 mActive = true;
5143 ALOGV("%s: Preparer stream started", __FUNCTION__);
5144
5145 return OK;
5146 }
5147
clear()5148 status_t Camera3Device::PreparerThread::clear() {
5149 ATRACE_CALL();
5150 Mutex::Autolock l(mLock);
5151
5152 for (const auto& it : mPendingStreams) {
5153 std::get<1>(it)->cancelPrepare();
5154 }
5155 mPendingStreams.clear();
5156 mCancelNow = true;
5157
5158 return OK;
5159 }
5160
setNotificationListener(wp<NotificationListener> listener)5161 void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
5162 ATRACE_CALL();
5163 Mutex::Autolock l(mLock);
5164 mListener = listener;
5165 }
5166
threadLoop()5167 bool Camera3Device::PreparerThread::threadLoop() {
5168 status_t res;
5169 {
5170 Mutex::Autolock l(mLock);
5171 if (mCurrentStream == nullptr) {
5172 // End thread if done with work
5173 if (mPendingStreams.empty()) {
5174 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
5175 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
5176 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
5177 mActive = false;
5178 mThreadActiveSignal.signal();
5179 return false;
5180 }
5181
5182 // Get next stream to prepare
5183 auto it = mPendingStreams.begin();
5184 mCurrentMaxCount = std::get<0>(*it);
5185 mCurrentStream = std::get<1>(*it);
5186 mCurrentPrepareComplete = false;
5187 mPendingStreams.erase(it);
5188 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
5189 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
5190 } else if (mCancelNow) {
5191 mCurrentStream->cancelPrepare();
5192 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5193 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
5194 mCurrentStream.clear();
5195 mCancelNow = false;
5196 return true;
5197 }
5198 }
5199
5200 res = mCurrentStream->prepareNextBuffer();
5201 if (res == NOT_ENOUGH_DATA) return true;
5202 if (res != OK) {
5203 // Something bad happened; try to recover by cancelling prepare and
5204 // signalling listener anyway
5205 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
5206 mCurrentStream->getId(), res, strerror(-res));
5207 mCurrentStream->cancelPrepare();
5208 }
5209
5210 // This stream has finished, notify listener
5211 Mutex::Autolock l(mLock);
5212 sp<NotificationListener> listener = mListener.promote();
5213 if (listener != NULL) {
5214 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
5215 mCurrentStream->getId());
5216 listener->notifyPrepared(mCurrentStream->getId());
5217 }
5218
5219 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5220 mCurrentStream.clear();
5221 mCurrentPrepareComplete = true;
5222
5223 return true;
5224 }
5225
initialize(sp<camera3::StatusTracker> statusTracker)5226 status_t Camera3Device::RequestBufferStateMachine::initialize(
5227 sp<camera3::StatusTracker> statusTracker) {
5228 if (statusTracker == nullptr) {
5229 ALOGE("%s: statusTracker is null", __FUNCTION__);
5230 return BAD_VALUE;
5231 }
5232
5233 std::lock_guard<std::mutex> lock(mLock);
5234 mStatusTracker = statusTracker;
5235 mRequestBufferStatusId = statusTracker->addComponent("BufferRequestSM");
5236 return OK;
5237 }
5238
startRequestBuffer()5239 bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
5240 std::lock_guard<std::mutex> lock(mLock);
5241 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
5242 mRequestBufferOngoing = true;
5243 notifyTrackerLocked(/*active*/true);
5244 return true;
5245 }
5246 return false;
5247 }
5248
endRequestBuffer()5249 void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
5250 std::lock_guard<std::mutex> lock(mLock);
5251 if (!mRequestBufferOngoing) {
5252 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
5253 return;
5254 }
5255 mRequestBufferOngoing = false;
5256 if (mStatus == RB_STATUS_PENDING_STOP) {
5257 checkSwitchToStopLocked();
5258 }
5259 notifyTrackerLocked(/*active*/false);
5260 }
5261
onStreamsConfigured()5262 void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
5263 std::lock_guard<std::mutex> lock(mLock);
5264 mSwitchedToOffline = false;
5265 mStatus = RB_STATUS_READY;
5266 return;
5267 }
5268
onSubmittingRequest()5269 void Camera3Device::RequestBufferStateMachine::onSubmittingRequest() {
5270 std::lock_guard<std::mutex> lock(mLock);
5271 mRequestThreadPaused = false;
5272 // inflight map register actually happens in prepareHalRequest now, but it is close enough
5273 // approximation.
5274 mInflightMapEmpty = false;
5275 if (mStatus == RB_STATUS_STOPPED) {
5276 mStatus = RB_STATUS_READY;
5277 }
5278 return;
5279 }
5280
onRequestThreadPaused()5281 void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
5282 std::lock_guard<std::mutex> lock(mLock);
5283 mRequestThreadPaused = true;
5284 if (mStatus == RB_STATUS_PENDING_STOP) {
5285 checkSwitchToStopLocked();
5286 }
5287 return;
5288 }
5289
onInflightMapEmpty()5290 void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
5291 std::lock_guard<std::mutex> lock(mLock);
5292 mInflightMapEmpty = true;
5293 if (mStatus == RB_STATUS_PENDING_STOP) {
5294 checkSwitchToStopLocked();
5295 }
5296 return;
5297 }
5298
onWaitUntilIdle()5299 void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
5300 std::lock_guard<std::mutex> lock(mLock);
5301 if (!checkSwitchToStopLocked()) {
5302 mStatus = RB_STATUS_PENDING_STOP;
5303 }
5304 return;
5305 }
5306
onSwitchToOfflineSuccess()5307 bool Camera3Device::RequestBufferStateMachine::onSwitchToOfflineSuccess() {
5308 std::lock_guard<std::mutex> lock(mLock);
5309 if (mRequestBufferOngoing) {
5310 ALOGE("%s: HAL must not be requesting buffer after HAL returns switchToOffline!",
5311 __FUNCTION__);
5312 return false;
5313 }
5314 mSwitchedToOffline = true;
5315 mInflightMapEmpty = true;
5316 mRequestThreadPaused = true;
5317 mStatus = RB_STATUS_STOPPED;
5318 return true;
5319 }
5320
notifyTrackerLocked(bool active)5321 void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
5322 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5323 if (statusTracker != nullptr) {
5324 if (active) {
5325 statusTracker->markComponentActive(mRequestBufferStatusId);
5326 } else {
5327 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
5328 }
5329 }
5330 }
5331
checkSwitchToStopLocked()5332 bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
5333 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
5334 mStatus = RB_STATUS_STOPPED;
5335 return true;
5336 }
5337 return false;
5338 }
5339
startRequestBuffer()5340 bool Camera3Device::startRequestBuffer() {
5341 return mRequestBufferSM.startRequestBuffer();
5342 }
5343
endRequestBuffer()5344 void Camera3Device::endRequestBuffer() {
5345 mRequestBufferSM.endRequestBuffer();
5346 }
5347
getWaitDuration()5348 nsecs_t Camera3Device::getWaitDuration() {
5349 return kBaseGetBufferWait + getExpectedInFlightDuration();
5350 }
5351
getInflightBufferKeys(std::vector<std::pair<int32_t,int32_t>> * out)5352 void Camera3Device::getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) {
5353 mInterface->getInflightBufferKeys(out);
5354 }
5355
getInflightRequestBufferKeys(std::vector<uint64_t> * out)5356 void Camera3Device::getInflightRequestBufferKeys(std::vector<uint64_t>* out) {
5357 mInterface->getInflightRequestBufferKeys(out);
5358 }
5359
getAllStreams()5360 std::vector<sp<Camera3StreamInterface>> Camera3Device::getAllStreams() {
5361 std::vector<sp<Camera3StreamInterface>> ret;
5362 bool hasInputStream = mInputStream != nullptr;
5363 ret.reserve(mOutputStreams.size() + mDeletedStreams.size() + ((hasInputStream) ? 1 : 0));
5364 if (hasInputStream) {
5365 ret.push_back(mInputStream);
5366 }
5367 for (size_t i = 0; i < mOutputStreams.size(); i++) {
5368 ret.push_back(mOutputStreams[i]);
5369 }
5370 for (size_t i = 0; i < mDeletedStreams.size(); i++) {
5371 ret.push_back(mDeletedStreams[i]);
5372 }
5373 return ret;
5374 }
5375
getOfflineStreamIds(std::vector<int> * offlineStreamIds)5376 void Camera3Device::getOfflineStreamIds(std::vector<int> *offlineStreamIds) {
5377 ATRACE_CALL();
5378
5379 if (offlineStreamIds == nullptr) {
5380 return;
5381 }
5382
5383 Mutex::Autolock il(mInterfaceLock);
5384
5385 auto streamIds = mOutputStreams.getStreamIds();
5386 bool hasInputStream = mInputStream != nullptr;
5387 if (hasInputStream && mInputStream->getOfflineProcessingSupport()) {
5388 offlineStreamIds->push_back(mInputStream->getId());
5389 }
5390
5391 for (const auto & streamId : streamIds) {
5392 sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
5393 // Streams that use the camera buffer manager are currently not supported in
5394 // offline mode
5395 if (stream->getOfflineProcessingSupport() &&
5396 (stream->getStreamSetId() == CAMERA3_STREAM_SET_ID_INVALID)) {
5397 offlineStreamIds->push_back(streamId);
5398 }
5399 }
5400 }
5401
setRotateAndCropAutoBehavior(camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue,bool fromHal)5402 status_t Camera3Device::setRotateAndCropAutoBehavior(
5403 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue, bool fromHal) {
5404 ATRACE_CALL();
5405 // We shouldn't hold mInterfaceLock when called as an effect of a HAL
5406 // callback since this can lead to a deadlock : b/299348355.
5407 // mLock still protects state.
5408 std::optional<Mutex::Autolock> maybeMutex =
5409 fromHal ? std::nullopt : std::optional<Mutex::Autolock>(mInterfaceLock);
5410 Mutex::Autolock l(mLock);
5411 if (mRequestThread == nullptr) {
5412 return INVALID_OPERATION;
5413 }
5414 if (rotateAndCropValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
5415 return BAD_VALUE;
5416 }
5417 mRotateAndCropOverride = rotateAndCropValue;
5418 return mRequestThread->setRotateAndCropAutoBehavior(rotateAndCropValue);
5419 }
5420
setAutoframingAutoBehavior(camera_metadata_enum_android_control_autoframing_t autoframingValue)5421 status_t Camera3Device::setAutoframingAutoBehavior(
5422 camera_metadata_enum_android_control_autoframing_t autoframingValue) {
5423 ATRACE_CALL();
5424 Mutex::Autolock il(mInterfaceLock);
5425 Mutex::Autolock l(mLock);
5426 if (mRequestThread == nullptr) {
5427 return INVALID_OPERATION;
5428 }
5429 if (autoframingValue == ANDROID_CONTROL_AUTOFRAMING_AUTO) {
5430 return BAD_VALUE;
5431 }
5432 mAutoframingOverride = autoframingValue;
5433 return mRequestThread->setAutoframingAutoBehaviour(autoframingValue);
5434 }
5435
supportsCameraMute()5436 bool Camera3Device::supportsCameraMute() {
5437 Mutex::Autolock il(mInterfaceLock);
5438 Mutex::Autolock l(mLock);
5439
5440 return mSupportCameraMute;
5441 }
5442
setCameraMute(bool enabled)5443 status_t Camera3Device::setCameraMute(bool enabled) {
5444 ATRACE_CALL();
5445 Mutex::Autolock il(mInterfaceLock);
5446 Mutex::Autolock l(mLock);
5447
5448 if (mRequestThread == nullptr || !mSupportCameraMute) {
5449 return INVALID_OPERATION;
5450 }
5451 int32_t muteMode =
5452 !enabled ? ANDROID_SENSOR_TEST_PATTERN_MODE_OFF :
5453 mSupportTestPatternSolidColor ? ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR :
5454 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK;
5455 return mRequestThread->setCameraMute(muteMode);
5456 }
5457
supportsZoomOverride()5458 bool Camera3Device::supportsZoomOverride() {
5459 Mutex::Autolock il(mInterfaceLock);
5460 Mutex::Autolock l(mLock);
5461
5462 return mSupportZoomOverride;
5463 }
5464
setZoomOverride(int32_t zoomOverride)5465 status_t Camera3Device::setZoomOverride(int32_t zoomOverride) {
5466 ATRACE_CALL();
5467 Mutex::Autolock il(mInterfaceLock);
5468 Mutex::Autolock l(mLock);
5469
5470 if (mRequestThread == nullptr || !mSupportZoomOverride) {
5471 return INVALID_OPERATION;
5472 }
5473
5474 return mRequestThread->setZoomOverride(zoomOverride);
5475 }
5476
injectCamera(const String8 & injectedCamId,sp<CameraProviderManager> manager)5477 status_t Camera3Device::injectCamera(const String8& injectedCamId,
5478 sp<CameraProviderManager> manager) {
5479 ALOGI("%s Injection camera: injectedCamId = %s", __FUNCTION__, injectedCamId.string());
5480 ATRACE_CALL();
5481 Mutex::Autolock il(mInterfaceLock);
5482 // When the camera device is active, injectCamera() and stopInjection() will call
5483 // internalPauseAndWaitLocked() and internalResumeLocked(), and then they will call
5484 // mStatusChanged.waitRelative(mLock, timeout) of waitUntilStateThenRelock(). But
5485 // mStatusChanged.waitRelative(mLock, timeout)'s parameter: mutex "mLock" must be in the locked
5486 // state, so we need to add "Mutex::Autolock l(mLock)" to lock the "mLock" before calling
5487 // waitUntilStateThenRelock().
5488 Mutex::Autolock l(mLock);
5489
5490 status_t res = NO_ERROR;
5491 if (mInjectionMethods->isInjecting()) {
5492 if (injectedCamId == mInjectionMethods->getInjectedCamId()) {
5493 return OK;
5494 } else {
5495 res = mInjectionMethods->stopInjection();
5496 if (res != OK) {
5497 ALOGE("%s: Failed to stop the injection camera! ret != NO_ERROR: %d",
5498 __FUNCTION__, res);
5499 return res;
5500 }
5501 }
5502 }
5503
5504 res = injectionCameraInitialize(injectedCamId, manager);
5505 if (res != OK) {
5506 ALOGE("%s: Failed to initialize the injection camera! ret != NO_ERROR: %d",
5507 __FUNCTION__, res);
5508 return res;
5509 }
5510
5511 // When the second display of android is cast to the remote device, and the opened camera is
5512 // also cast to the second display, in this case, because the camera has configured the streams
5513 // at this time, we can directly call injectCamera() to replace the internal camera with
5514 // injection camera.
5515 if (mInjectionMethods->isStreamConfigCompleteButNotInjected()) {
5516 ALOGD("%s: The opened camera is directly cast to the remote device.", __FUNCTION__);
5517
5518 camera3::camera_stream_configuration injectionConfig;
5519 std::vector<uint32_t> injectionBufferSizes;
5520 mInjectionMethods->getInjectionConfig(&injectionConfig, &injectionBufferSizes);
5521 if (mOperatingMode < 0 || injectionConfig.num_streams <= 0
5522 || injectionBufferSizes.size() <= 0) {
5523 ALOGE("Failed to inject camera due to abandoned configuration! "
5524 "mOperatingMode: %d injectionConfig.num_streams: %d "
5525 "injectionBufferSizes.size(): %zu", mOperatingMode,
5526 injectionConfig.num_streams, injectionBufferSizes.size());
5527 return DEAD_OBJECT;
5528 }
5529
5530 res = mInjectionMethods->injectCamera(
5531 injectionConfig, injectionBufferSizes);
5532 if (res != OK) {
5533 ALOGE("Can't finish inject camera process!");
5534 return res;
5535 }
5536 }
5537
5538 return OK;
5539 }
5540
stopInjection()5541 status_t Camera3Device::stopInjection() {
5542 ALOGI("%s: Injection camera: stopInjection", __FUNCTION__);
5543 Mutex::Autolock il(mInterfaceLock);
5544 Mutex::Autolock l(mLock);
5545 return mInjectionMethods->stopInjection();
5546 }
5547
overrideStreamUseCaseLocked()5548 void Camera3Device::overrideStreamUseCaseLocked() {
5549 if (mStreamUseCaseOverrides.size() == 0) {
5550 return;
5551 }
5552
5553 // Start from an array of indexes in mStreamUseCaseOverrides, and sort them
5554 // based first on size, and second on formats of [JPEG, RAW, YUV, PRIV].
5555 // Refer to CameraService::printHelp for details.
5556 std::vector<int> outputStreamsIndices(mOutputStreams.size());
5557 for (size_t i = 0; i < outputStreamsIndices.size(); i++) {
5558 outputStreamsIndices[i] = i;
5559 }
5560
5561 std::sort(outputStreamsIndices.begin(), outputStreamsIndices.end(),
5562 [&](int a, int b) -> bool {
5563
5564 auto formatScore = [](int format) {
5565 switch (format) {
5566 case HAL_PIXEL_FORMAT_BLOB:
5567 return 4;
5568 case HAL_PIXEL_FORMAT_RAW16:
5569 case HAL_PIXEL_FORMAT_RAW10:
5570 case HAL_PIXEL_FORMAT_RAW12:
5571 return 3;
5572 case HAL_PIXEL_FORMAT_YCBCR_420_888:
5573 return 2;
5574 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
5575 return 1;
5576 default:
5577 return 0;
5578 }
5579 };
5580
5581 int sizeA = mOutputStreams[a]->getWidth() * mOutputStreams[a]->getHeight();
5582 int sizeB = mOutputStreams[a]->getWidth() * mOutputStreams[a]->getHeight();
5583 int formatAScore = formatScore(mOutputStreams[a]->getFormat());
5584 int formatBScore = formatScore(mOutputStreams[b]->getFormat());
5585 if (sizeA > sizeB ||
5586 (sizeA == sizeB && formatAScore >= formatBScore)) {
5587 return true;
5588 } else {
5589 return false;
5590 }
5591 });
5592
5593 size_t overlapSize = std::min(mStreamUseCaseOverrides.size(), mOutputStreams.size());
5594 for (size_t i = 0; i < mOutputStreams.size(); i++) {
5595 mOutputStreams[outputStreamsIndices[i]]->setStreamUseCase(
5596 mStreamUseCaseOverrides[std::min(i, overlapSize-1)]);
5597 }
5598 }
5599
5600 }; // namespace android
5601