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