• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
2985     nsecs_t sensorTimestamp = timestamp.data.i64[0];
2986 
2987     for (auto& physicalMetadata : captureResult.mPhysicalMetadatas) {
2988         camera_metadata_entry timestamp =
2989                 physicalMetadata.mPhysicalCameraMetadata.find(ANDROID_SENSOR_TIMESTAMP);
2990         if (timestamp.count == 0) {
2991             SET_ERR("No timestamp provided by HAL for physical camera %s frame %d!",
2992                     String8(physicalMetadata.mPhysicalCameraId).c_str(), frameNumber);
2993             return;
2994         }
2995     }
2996 
2997     // Fix up some result metadata to account for HAL-level distortion correction
2998     status_t res = mDistortionMapper.correctCaptureResult(&captureResult.mMetadata);
2999     if (res != OK) {
3000         SET_ERR("Unable to correct capture result metadata for frame %d: %s (%d)",
3001                 frameNumber, strerror(res), res);
3002         return;
3003     }
3004 
3005     mTagMonitor.monitorMetadata(TagMonitor::RESULT,
3006             frameNumber, sensorTimestamp, captureResult.mMetadata);
3007 
3008     insertResultLocked(&captureResult, frameNumber);
3009 }
3010 
3011 /**
3012  * Camera HAL device callback methods
3013  */
3014 
processCaptureResult(const camera3_capture_result * result)3015 void Camera3Device::processCaptureResult(const camera3_capture_result *result) {
3016     ATRACE_CALL();
3017 
3018     status_t res;
3019 
3020     uint32_t frameNumber = result->frame_number;
3021     if (result->result == NULL && result->num_output_buffers == 0 &&
3022             result->input_buffer == NULL) {
3023         SET_ERR("No result data provided by HAL for frame %d",
3024                 frameNumber);
3025         return;
3026     }
3027 
3028     if (!mUsePartialResult &&
3029             result->result != NULL &&
3030             result->partial_result != 1) {
3031         SET_ERR("Result is malformed for frame %d: partial_result %u must be 1"
3032                 " if partial result is not supported",
3033                 frameNumber, result->partial_result);
3034         return;
3035     }
3036 
3037     bool isPartialResult = false;
3038     CameraMetadata collectedPartialResult;
3039     bool hasInputBufferInRequest = false;
3040 
3041     // Get shutter timestamp and resultExtras from list of in-flight requests,
3042     // where it was added by the shutter notification for this frame. If the
3043     // shutter timestamp isn't received yet, append the output buffers to the
3044     // in-flight request and they will be returned when the shutter timestamp
3045     // arrives. Update the in-flight status and remove the in-flight entry if
3046     // all result data and shutter timestamp have been received.
3047     nsecs_t shutterTimestamp = 0;
3048 
3049     {
3050         Mutex::Autolock l(mInFlightLock);
3051         ssize_t idx = mInFlightMap.indexOfKey(frameNumber);
3052         if (idx == NAME_NOT_FOUND) {
3053             SET_ERR("Unknown frame number for capture result: %d",
3054                     frameNumber);
3055             return;
3056         }
3057         InFlightRequest &request = mInFlightMap.editValueAt(idx);
3058         ALOGVV("%s: got InFlightRequest requestId = %" PRId32
3059                 ", frameNumber = %" PRId64 ", burstId = %" PRId32
3060                 ", partialResultCount = %d, hasCallback = %d",
3061                 __FUNCTION__, request.resultExtras.requestId,
3062                 request.resultExtras.frameNumber, request.resultExtras.burstId,
3063                 result->partial_result, request.hasCallback);
3064         // Always update the partial count to the latest one if it's not 0
3065         // (buffers only). When framework aggregates adjacent partial results
3066         // into one, the latest partial count will be used.
3067         if (result->partial_result != 0)
3068             request.resultExtras.partialResultCount = result->partial_result;
3069 
3070         // Check if this result carries only partial metadata
3071         if (mUsePartialResult && result->result != NULL) {
3072             if (result->partial_result > mNumPartialResults || result->partial_result < 1) {
3073                 SET_ERR("Result is malformed for frame %d: partial_result %u must be  in"
3074                         " the range of [1, %d] when metadata is included in the result",
3075                         frameNumber, result->partial_result, mNumPartialResults);
3076                 return;
3077             }
3078             isPartialResult = (result->partial_result < mNumPartialResults);
3079             if (isPartialResult && result->num_physcam_metadata) {
3080                 SET_ERR("Result is malformed for frame %d: partial_result not allowed for"
3081                         " physical camera result", frameNumber);
3082                 return;
3083             }
3084             if (isPartialResult) {
3085                 request.collectedPartialResult.append(result->result);
3086             }
3087 
3088             if (isPartialResult && request.hasCallback) {
3089                 // Send partial capture result
3090                 sendPartialCaptureResult(result->result, request.resultExtras,
3091                         frameNumber);
3092             }
3093         }
3094 
3095         shutterTimestamp = request.shutterTimestamp;
3096         hasInputBufferInRequest = request.hasInputBuffer;
3097 
3098         // Did we get the (final) result metadata for this capture?
3099         if (result->result != NULL && !isPartialResult) {
3100             if (request.physicalCameraIds.size() != result->num_physcam_metadata) {
3101                 SET_ERR("Requested physical Camera Ids %d not equal to number of metadata %d",
3102                         request.physicalCameraIds.size(), result->num_physcam_metadata);
3103                 return;
3104             }
3105             if (request.haveResultMetadata) {
3106                 SET_ERR("Called multiple times with metadata for frame %d",
3107                         frameNumber);
3108                 return;
3109             }
3110             for (uint32_t i = 0; i < result->num_physcam_metadata; i++) {
3111                 String8 physicalId(result->physcam_ids[i]);
3112                 std::set<String8>::iterator cameraIdIter =
3113                         request.physicalCameraIds.find(physicalId);
3114                 if (cameraIdIter != request.physicalCameraIds.end()) {
3115                     request.physicalCameraIds.erase(cameraIdIter);
3116                 } else {
3117                     SET_ERR("Total result for frame %d has already returned for camera %s",
3118                             frameNumber, physicalId.c_str());
3119                     return;
3120                 }
3121             }
3122             if (mUsePartialResult &&
3123                     !request.collectedPartialResult.isEmpty()) {
3124                 collectedPartialResult.acquire(
3125                     request.collectedPartialResult);
3126             }
3127             request.haveResultMetadata = true;
3128         }
3129 
3130         uint32_t numBuffersReturned = result->num_output_buffers;
3131         if (result->input_buffer != NULL) {
3132             if (hasInputBufferInRequest) {
3133                 numBuffersReturned += 1;
3134             } else {
3135                 ALOGW("%s: Input buffer should be NULL if there is no input"
3136                         " buffer sent in the request",
3137                         __FUNCTION__);
3138             }
3139         }
3140         request.numBuffersLeft -= numBuffersReturned;
3141         if (request.numBuffersLeft < 0) {
3142             SET_ERR("Too many buffers returned for frame %d",
3143                     frameNumber);
3144             return;
3145         }
3146 
3147         camera_metadata_ro_entry_t entry;
3148         res = find_camera_metadata_ro_entry(result->result,
3149                 ANDROID_SENSOR_TIMESTAMP, &entry);
3150         if (res == OK && entry.count == 1) {
3151             request.sensorTimestamp = entry.data.i64[0];
3152         }
3153 
3154         // If shutter event isn't received yet, append the output buffers to
3155         // the in-flight request. Otherwise, return the output buffers to
3156         // streams.
3157         if (shutterTimestamp == 0) {
3158             request.pendingOutputBuffers.appendArray(result->output_buffers,
3159                 result->num_output_buffers);
3160         } else {
3161             returnOutputBuffers(result->output_buffers,
3162                 result->num_output_buffers, shutterTimestamp);
3163         }
3164 
3165         if (result->result != NULL && !isPartialResult) {
3166             for (uint32_t i = 0; i < result->num_physcam_metadata; i++) {
3167                 CameraMetadata physicalMetadata;
3168                 physicalMetadata.append(result->physcam_metadata[i]);
3169                 request.physicalMetadatas.push_back({String16(result->physcam_ids[i]),
3170                         physicalMetadata});
3171             }
3172             if (shutterTimestamp == 0) {
3173                 request.pendingMetadata = result->result;
3174                 request.collectedPartialResult = collectedPartialResult;
3175            } else if (request.hasCallback) {
3176                 CameraMetadata metadata;
3177                 metadata = result->result;
3178                 sendCaptureResult(metadata, request.resultExtras,
3179                     collectedPartialResult, frameNumber,
3180                     hasInputBufferInRequest, request.physicalMetadatas);
3181             }
3182         }
3183 
3184         removeInFlightRequestIfReadyLocked(idx);
3185     } // scope for mInFlightLock
3186 
3187     if (result->input_buffer != NULL) {
3188         if (hasInputBufferInRequest) {
3189             Camera3Stream *stream =
3190                 Camera3Stream::cast(result->input_buffer->stream);
3191             res = stream->returnInputBuffer(*(result->input_buffer));
3192             // Note: stream may be deallocated at this point, if this buffer was the
3193             // last reference to it.
3194             if (res != OK) {
3195                 ALOGE("%s: RequestThread: Can't return input buffer for frame %d to"
3196                       "  its stream:%s (%d)",  __FUNCTION__,
3197                       frameNumber, strerror(-res), res);
3198             }
3199         } else {
3200             ALOGW("%s: Input buffer should be NULL if there is no input"
3201                     " buffer sent in the request, skipping input buffer return.",
3202                     __FUNCTION__);
3203         }
3204     }
3205 }
3206 
notify(const camera3_notify_msg * msg)3207 void Camera3Device::notify(const camera3_notify_msg *msg) {
3208     ATRACE_CALL();
3209     sp<NotificationListener> listener;
3210     {
3211         Mutex::Autolock l(mOutputLock);
3212         listener = mListener.promote();
3213     }
3214 
3215     if (msg == NULL) {
3216         SET_ERR("HAL sent NULL notify message!");
3217         return;
3218     }
3219 
3220     switch (msg->type) {
3221         case CAMERA3_MSG_ERROR: {
3222             notifyError(msg->message.error, listener);
3223             break;
3224         }
3225         case CAMERA3_MSG_SHUTTER: {
3226             notifyShutter(msg->message.shutter, listener);
3227             break;
3228         }
3229         default:
3230             SET_ERR("Unknown notify message from HAL: %d",
3231                     msg->type);
3232     }
3233 }
3234 
notifyError(const camera3_error_msg_t & msg,sp<NotificationListener> listener)3235 void Camera3Device::notifyError(const camera3_error_msg_t &msg,
3236         sp<NotificationListener> listener) {
3237     ATRACE_CALL();
3238     // Map camera HAL error codes to ICameraDeviceCallback error codes
3239     // Index into this with the HAL error code
3240     static const int32_t halErrorMap[CAMERA3_MSG_NUM_ERRORS] = {
3241         // 0 = Unused error code
3242         hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR,
3243         // 1 = CAMERA3_MSG_ERROR_DEVICE
3244         hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
3245         // 2 = CAMERA3_MSG_ERROR_REQUEST
3246         hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
3247         // 3 = CAMERA3_MSG_ERROR_RESULT
3248         hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT,
3249         // 4 = CAMERA3_MSG_ERROR_BUFFER
3250         hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER
3251     };
3252 
3253     int32_t errorCode =
3254             ((msg.error_code >= 0) &&
3255                     (msg.error_code < CAMERA3_MSG_NUM_ERRORS)) ?
3256             halErrorMap[msg.error_code] :
3257             hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR;
3258 
3259     int streamId = 0;
3260     if (msg.error_stream != NULL) {
3261         Camera3Stream *stream =
3262                 Camera3Stream::cast(msg.error_stream);
3263         streamId = stream->getId();
3264     }
3265     ALOGV("Camera %s: %s: HAL error, frame %d, stream %d: %d",
3266             mId.string(), __FUNCTION__, msg.frame_number,
3267             streamId, msg.error_code);
3268 
3269     CaptureResultExtras resultExtras;
3270     switch (errorCode) {
3271         case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE:
3272             // SET_ERR calls notifyError
3273             SET_ERR("Camera HAL reported serious device error");
3274             break;
3275         case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
3276         case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
3277         case hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
3278             {
3279                 Mutex::Autolock l(mInFlightLock);
3280                 ssize_t idx = mInFlightMap.indexOfKey(msg.frame_number);
3281                 if (idx >= 0) {
3282                     InFlightRequest &r = mInFlightMap.editValueAt(idx);
3283                     r.requestStatus = msg.error_code;
3284                     resultExtras = r.resultExtras;
3285                     if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT == errorCode
3286                             ||  hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST ==
3287                             errorCode) {
3288                         r.skipResultMetadata = true;
3289                     }
3290                     if (hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT ==
3291                             errorCode) {
3292                         // In case of missing result check whether the buffers
3293                         // returned. If they returned, then remove inflight
3294                         // request.
3295                         removeInFlightRequestIfReadyLocked(idx);
3296                     }
3297                 } else {
3298                     resultExtras.frameNumber = msg.frame_number;
3299                     ALOGE("Camera %s: %s: cannot find in-flight request on "
3300                             "frame %" PRId64 " error", mId.string(), __FUNCTION__,
3301                             resultExtras.frameNumber);
3302                 }
3303             }
3304             resultExtras.errorStreamId = streamId;
3305             if (listener != NULL) {
3306                 listener->notifyError(errorCode, resultExtras);
3307             } else {
3308                 ALOGE("Camera %s: %s: no listener available", mId.string(), __FUNCTION__);
3309             }
3310             break;
3311         default:
3312             // SET_ERR calls notifyError
3313             SET_ERR("Unknown error message from HAL: %d", msg.error_code);
3314             break;
3315     }
3316 }
3317 
notifyShutter(const camera3_shutter_msg_t & msg,sp<NotificationListener> listener)3318 void Camera3Device::notifyShutter(const camera3_shutter_msg_t &msg,
3319         sp<NotificationListener> listener) {
3320     ATRACE_CALL();
3321     ssize_t idx;
3322 
3323     // Set timestamp for the request in the in-flight tracking
3324     // and get the request ID to send upstream
3325     {
3326         Mutex::Autolock l(mInFlightLock);
3327         idx = mInFlightMap.indexOfKey(msg.frame_number);
3328         if (idx >= 0) {
3329             InFlightRequest &r = mInFlightMap.editValueAt(idx);
3330 
3331             // Verify ordering of shutter notifications
3332             {
3333                 Mutex::Autolock l(mOutputLock);
3334                 // TODO: need to track errors for tighter bounds on expected frame number.
3335                 if (r.hasInputBuffer) {
3336                     if (msg.frame_number < mNextReprocessShutterFrameNumber) {
3337                         SET_ERR("Shutter notification out-of-order. Expected "
3338                                 "notification for frame %d, got frame %d",
3339                                 mNextReprocessShutterFrameNumber, msg.frame_number);
3340                         return;
3341                     }
3342                     mNextReprocessShutterFrameNumber = msg.frame_number + 1;
3343                 } else {
3344                     if (msg.frame_number < mNextShutterFrameNumber) {
3345                         SET_ERR("Shutter notification out-of-order. Expected "
3346                                 "notification for frame %d, got frame %d",
3347                                 mNextShutterFrameNumber, msg.frame_number);
3348                         return;
3349                     }
3350                     mNextShutterFrameNumber = msg.frame_number + 1;
3351                 }
3352             }
3353 
3354             r.shutterTimestamp = msg.timestamp;
3355             if (r.hasCallback) {
3356                 ALOGVV("Camera %s: %s: Shutter fired for frame %d (id %d) at %" PRId64,
3357                     mId.string(), __FUNCTION__,
3358                     msg.frame_number, r.resultExtras.requestId, msg.timestamp);
3359                 // Call listener, if any
3360                 if (listener != NULL) {
3361                     listener->notifyShutter(r.resultExtras, msg.timestamp);
3362                 }
3363                 // send pending result and buffers
3364                 sendCaptureResult(r.pendingMetadata, r.resultExtras,
3365                     r.collectedPartialResult, msg.frame_number,
3366                     r.hasInputBuffer, r.physicalMetadatas);
3367             }
3368             returnOutputBuffers(r.pendingOutputBuffers.array(),
3369                 r.pendingOutputBuffers.size(), r.shutterTimestamp);
3370             r.pendingOutputBuffers.clear();
3371 
3372             removeInFlightRequestIfReadyLocked(idx);
3373         }
3374     }
3375     if (idx < 0) {
3376         SET_ERR("Shutter notification for non-existent frame number %d",
3377                 msg.frame_number);
3378     }
3379 }
3380 
getLatestRequestLocked()3381 CameraMetadata Camera3Device::getLatestRequestLocked() {
3382     ALOGV("%s", __FUNCTION__);
3383 
3384     CameraMetadata retVal;
3385 
3386     if (mRequestThread != NULL) {
3387         retVal = mRequestThread->getLatestRequest();
3388     }
3389 
3390     return retVal;
3391 }
3392 
3393 
monitorMetadata(TagMonitor::eventSource source,int64_t frameNumber,nsecs_t timestamp,const CameraMetadata & metadata)3394 void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
3395         int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata) {
3396     mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata);
3397 }
3398 
3399 /**
3400  * HalInterface inner class methods
3401  */
3402 
HalInterface(sp<ICameraDeviceSession> & session,std::shared_ptr<RequestMetadataQueue> queue)3403 Camera3Device::HalInterface::HalInterface(
3404             sp<ICameraDeviceSession> &session,
3405             std::shared_ptr<RequestMetadataQueue> queue) :
3406         mHidlSession(session),
3407         mRequestMetadataQueue(queue) {
3408     // Check with hardware service manager if we can downcast these interfaces
3409     // Somewhat expensive, so cache the results at startup
3410     auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
3411     if (castResult_3_4.isOk()) {
3412         mHidlSession_3_4 = castResult_3_4;
3413     }
3414     auto castResult_3_3 = device::V3_3::ICameraDeviceSession::castFrom(mHidlSession);
3415     if (castResult_3_3.isOk()) {
3416         mHidlSession_3_3 = castResult_3_3;
3417     }
3418 }
3419 
HalInterface()3420 Camera3Device::HalInterface::HalInterface() {}
3421 
HalInterface(const HalInterface & other)3422 Camera3Device::HalInterface::HalInterface(const HalInterface& other) :
3423         mHidlSession(other.mHidlSession),
3424         mRequestMetadataQueue(other.mRequestMetadataQueue) {}
3425 
valid()3426 bool Camera3Device::HalInterface::valid() {
3427     return (mHidlSession != nullptr);
3428 }
3429 
clear()3430 void Camera3Device::HalInterface::clear() {
3431     mHidlSession_3_4.clear();
3432     mHidlSession_3_3.clear();
3433     mHidlSession.clear();
3434 }
3435 
supportBatchRequest()3436 bool Camera3Device::HalInterface::supportBatchRequest() {
3437     return mHidlSession != nullptr;
3438 }
3439 
constructDefaultRequestSettings(camera3_request_template_t templateId,camera_metadata_t ** requestTemplate)3440 status_t Camera3Device::HalInterface::constructDefaultRequestSettings(
3441         camera3_request_template_t templateId,
3442         /*out*/ camera_metadata_t **requestTemplate) {
3443     ATRACE_NAME("CameraHal::constructDefaultRequestSettings");
3444     if (!valid()) return INVALID_OPERATION;
3445     status_t res = OK;
3446 
3447     common::V1_0::Status status;
3448 
3449     auto requestCallback = [&status, &requestTemplate]
3450             (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) {
3451             status = s;
3452             if (status == common::V1_0::Status::OK) {
3453                 const camera_metadata *r =
3454                         reinterpret_cast<const camera_metadata_t*>(request.data());
3455                 size_t expectedSize = request.size();
3456                 int ret = validate_camera_metadata_structure(r, &expectedSize);
3457                 if (ret == OK || ret == CAMERA_METADATA_VALIDATION_SHIFTED) {
3458                     *requestTemplate = clone_camera_metadata(r);
3459                     if (*requestTemplate == nullptr) {
3460                         ALOGE("%s: Unable to clone camera metadata received from HAL",
3461                                 __FUNCTION__);
3462                         status = common::V1_0::Status::INTERNAL_ERROR;
3463                     }
3464                 } else {
3465                     ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
3466                     status = common::V1_0::Status::INTERNAL_ERROR;
3467                 }
3468             }
3469         };
3470     hardware::Return<void> err;
3471     RequestTemplate id;
3472     switch (templateId) {
3473         case CAMERA3_TEMPLATE_PREVIEW:
3474             id = RequestTemplate::PREVIEW;
3475             break;
3476         case CAMERA3_TEMPLATE_STILL_CAPTURE:
3477             id = RequestTemplate::STILL_CAPTURE;
3478             break;
3479         case CAMERA3_TEMPLATE_VIDEO_RECORD:
3480             id = RequestTemplate::VIDEO_RECORD;
3481             break;
3482         case CAMERA3_TEMPLATE_VIDEO_SNAPSHOT:
3483             id = RequestTemplate::VIDEO_SNAPSHOT;
3484             break;
3485         case CAMERA3_TEMPLATE_ZERO_SHUTTER_LAG:
3486             id = RequestTemplate::ZERO_SHUTTER_LAG;
3487             break;
3488         case CAMERA3_TEMPLATE_MANUAL:
3489             id = RequestTemplate::MANUAL;
3490             break;
3491         default:
3492             // Unknown template ID, or this HAL is too old to support it
3493             return BAD_VALUE;
3494     }
3495     err = mHidlSession->constructDefaultRequestSettings(id, requestCallback);
3496 
3497     if (!err.isOk()) {
3498         ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3499         res = DEAD_OBJECT;
3500     } else {
3501         res = CameraProviderManager::mapToStatusT(status);
3502     }
3503 
3504     return res;
3505 }
3506 
configureStreams(const camera_metadata_t * sessionParams,camera3_stream_configuration * config,const std::vector<uint32_t> & bufferSizes)3507 status_t Camera3Device::HalInterface::configureStreams(const camera_metadata_t *sessionParams,
3508         camera3_stream_configuration *config, const std::vector<uint32_t>& bufferSizes) {
3509     ATRACE_NAME("CameraHal::configureStreams");
3510     if (!valid()) return INVALID_OPERATION;
3511     status_t res = OK;
3512 
3513     // Convert stream config to HIDL
3514     std::set<int> activeStreams;
3515     device::V3_2::StreamConfiguration requestedConfiguration3_2;
3516     device::V3_4::StreamConfiguration requestedConfiguration3_4;
3517     requestedConfiguration3_2.streams.resize(config->num_streams);
3518     requestedConfiguration3_4.streams.resize(config->num_streams);
3519     for (size_t i = 0; i < config->num_streams; i++) {
3520         device::V3_2::Stream &dst3_2 = requestedConfiguration3_2.streams[i];
3521         device::V3_4::Stream &dst3_4 = requestedConfiguration3_4.streams[i];
3522         camera3_stream_t *src = config->streams[i];
3523 
3524         Camera3Stream* cam3stream = Camera3Stream::cast(src);
3525         cam3stream->setBufferFreedListener(this);
3526         int streamId = cam3stream->getId();
3527         StreamType streamType;
3528         switch (src->stream_type) {
3529             case CAMERA3_STREAM_OUTPUT:
3530                 streamType = StreamType::OUTPUT;
3531                 break;
3532             case CAMERA3_STREAM_INPUT:
3533                 streamType = StreamType::INPUT;
3534                 break;
3535             default:
3536                 ALOGE("%s: Stream %d: Unsupported stream type %d",
3537                         __FUNCTION__, streamId, config->streams[i]->stream_type);
3538                 return BAD_VALUE;
3539         }
3540         dst3_2.id = streamId;
3541         dst3_2.streamType = streamType;
3542         dst3_2.width = src->width;
3543         dst3_2.height = src->height;
3544         dst3_2.format = mapToPixelFormat(src->format);
3545         dst3_2.usage = mapToConsumerUsage(cam3stream->getUsage());
3546         dst3_2.dataSpace = mapToHidlDataspace(src->data_space);
3547         dst3_2.rotation = mapToStreamRotation((camera3_stream_rotation_t) src->rotation);
3548         dst3_4.v3_2 = dst3_2;
3549         dst3_4.bufferSize = bufferSizes[i];
3550         if (src->physical_camera_id != nullptr) {
3551             dst3_4.physicalCameraId = src->physical_camera_id;
3552         }
3553 
3554         activeStreams.insert(streamId);
3555         // Create Buffer ID map if necessary
3556         if (mBufferIdMaps.count(streamId) == 0) {
3557             mBufferIdMaps.emplace(streamId, BufferIdMap{});
3558         }
3559     }
3560     // remove BufferIdMap for deleted streams
3561     for(auto it = mBufferIdMaps.begin(); it != mBufferIdMaps.end();) {
3562         int streamId = it->first;
3563         bool active = activeStreams.count(streamId) > 0;
3564         if (!active) {
3565             it = mBufferIdMaps.erase(it);
3566         } else {
3567             ++it;
3568         }
3569     }
3570 
3571     StreamConfigurationMode operationMode;
3572     res = mapToStreamConfigurationMode(
3573             (camera3_stream_configuration_mode_t) config->operation_mode,
3574             /*out*/ &operationMode);
3575     if (res != OK) {
3576         return res;
3577     }
3578     requestedConfiguration3_2.operationMode = operationMode;
3579     requestedConfiguration3_4.operationMode = operationMode;
3580     requestedConfiguration3_4.sessionParams.setToExternal(
3581             reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(sessionParams)),
3582             get_camera_metadata_size(sessionParams));
3583 
3584     // Invoke configureStreams
3585     device::V3_3::HalStreamConfiguration finalConfiguration;
3586     common::V1_0::Status status;
3587 
3588     // See if we have v3.4 or v3.3 HAL
3589     if (mHidlSession_3_4 != nullptr) {
3590         // We do; use v3.4 for the call
3591         ALOGV("%s: v3.4 device found", __FUNCTION__);
3592         device::V3_4::HalStreamConfiguration finalConfiguration3_4;
3593         auto err = mHidlSession_3_4->configureStreams_3_4(requestedConfiguration3_4,
3594             [&status, &finalConfiguration3_4]
3595             (common::V1_0::Status s, const device::V3_4::HalStreamConfiguration& halConfiguration) {
3596                 finalConfiguration3_4 = halConfiguration;
3597                 status = s;
3598             });
3599         if (!err.isOk()) {
3600             ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3601             return DEAD_OBJECT;
3602         }
3603         finalConfiguration.streams.resize(finalConfiguration3_4.streams.size());
3604         for (size_t i = 0; i < finalConfiguration3_4.streams.size(); i++) {
3605             finalConfiguration.streams[i] = finalConfiguration3_4.streams[i].v3_3;
3606         }
3607     } else if (mHidlSession_3_3 != nullptr) {
3608         // We do; use v3.3 for the call
3609         ALOGV("%s: v3.3 device found", __FUNCTION__);
3610         auto err = mHidlSession_3_3->configureStreams_3_3(requestedConfiguration3_2,
3611             [&status, &finalConfiguration]
3612             (common::V1_0::Status s, const device::V3_3::HalStreamConfiguration& halConfiguration) {
3613                 finalConfiguration = halConfiguration;
3614                 status = s;
3615             });
3616         if (!err.isOk()) {
3617             ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3618             return DEAD_OBJECT;
3619         }
3620     } else {
3621         // We don't; use v3.2 call and construct a v3.3 HalStreamConfiguration
3622         ALOGV("%s: v3.2 device found", __FUNCTION__);
3623         HalStreamConfiguration finalConfiguration_3_2;
3624         auto err = mHidlSession->configureStreams(requestedConfiguration3_2,
3625                 [&status, &finalConfiguration_3_2]
3626                 (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) {
3627                     finalConfiguration_3_2 = halConfiguration;
3628                     status = s;
3629                 });
3630         if (!err.isOk()) {
3631             ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3632             return DEAD_OBJECT;
3633         }
3634         finalConfiguration.streams.resize(finalConfiguration_3_2.streams.size());
3635         for (size_t i = 0; i < finalConfiguration_3_2.streams.size(); i++) {
3636             finalConfiguration.streams[i].v3_2 = finalConfiguration_3_2.streams[i];
3637             finalConfiguration.streams[i].overrideDataSpace =
3638                     requestedConfiguration3_2.streams[i].dataSpace;
3639         }
3640     }
3641 
3642     if (status != common::V1_0::Status::OK ) {
3643         return CameraProviderManager::mapToStatusT(status);
3644     }
3645 
3646     // And convert output stream configuration from HIDL
3647 
3648     for (size_t i = 0; i < config->num_streams; i++) {
3649         camera3_stream_t *dst = config->streams[i];
3650         int streamId = Camera3Stream::cast(dst)->getId();
3651 
3652         // Start scan at i, with the assumption that the stream order matches
3653         size_t realIdx = i;
3654         bool found = false;
3655         for (size_t idx = 0; idx < finalConfiguration.streams.size(); idx++) {
3656             if (finalConfiguration.streams[realIdx].v3_2.id == streamId) {
3657                 found = true;
3658                 break;
3659             }
3660             realIdx = (realIdx >= finalConfiguration.streams.size()) ? 0 : realIdx + 1;
3661         }
3662         if (!found) {
3663             ALOGE("%s: Stream %d not found in stream configuration response from HAL",
3664                     __FUNCTION__, streamId);
3665             return INVALID_OPERATION;
3666         }
3667         device::V3_3::HalStream &src = finalConfiguration.streams[realIdx];
3668 
3669         Camera3Stream* dstStream = Camera3Stream::cast(dst);
3670         dstStream->setFormatOverride(false);
3671         dstStream->setDataSpaceOverride(false);
3672         int overrideFormat = mapToFrameworkFormat(src.v3_2.overrideFormat);
3673         android_dataspace overrideDataSpace = mapToFrameworkDataspace(src.overrideDataSpace);
3674 
3675         if (dst->format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
3676             if (dst->format != overrideFormat) {
3677                 ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__,
3678                         streamId, dst->format);
3679             }
3680             if (dst->data_space != overrideDataSpace) {
3681                 ALOGE("%s: Stream %d: DataSpace override not allowed for format 0x%x", __FUNCTION__,
3682                         streamId, dst->format);
3683             }
3684         } else {
3685             dstStream->setFormatOverride((dst->format != overrideFormat) ? true : false);
3686             dstStream->setDataSpaceOverride((dst->data_space != overrideDataSpace) ? true : false);
3687 
3688             // Override allowed with IMPLEMENTATION_DEFINED
3689             dst->format = overrideFormat;
3690             dst->data_space = overrideDataSpace;
3691         }
3692 
3693         if (dst->stream_type == CAMERA3_STREAM_INPUT) {
3694             if (src.v3_2.producerUsage != 0) {
3695                 ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage",
3696                         __FUNCTION__, streamId);
3697                 return INVALID_OPERATION;
3698             }
3699             dstStream->setUsage(
3700                     mapConsumerToFrameworkUsage(src.v3_2.consumerUsage));
3701         } else {
3702             // OUTPUT
3703             if (src.v3_2.consumerUsage != 0) {
3704                 ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage",
3705                         __FUNCTION__, streamId);
3706                 return INVALID_OPERATION;
3707             }
3708             dstStream->setUsage(
3709                     mapProducerToFrameworkUsage(src.v3_2.producerUsage));
3710         }
3711         dst->max_buffers = src.v3_2.maxBuffers;
3712     }
3713 
3714     return res;
3715 }
3716 
wrapAsHidlRequest(camera3_capture_request_t * request,device::V3_2::CaptureRequest * captureRequest,std::vector<native_handle_t * > * handlesCreated)3717 void Camera3Device::HalInterface::wrapAsHidlRequest(camera3_capture_request_t* request,
3718         /*out*/device::V3_2::CaptureRequest* captureRequest,
3719         /*out*/std::vector<native_handle_t*>* handlesCreated) {
3720     ATRACE_CALL();
3721     if (captureRequest == nullptr || handlesCreated == nullptr) {
3722         ALOGE("%s: captureRequest (%p) and handlesCreated (%p) must not be null",
3723                 __FUNCTION__, captureRequest, handlesCreated);
3724         return;
3725     }
3726 
3727     captureRequest->frameNumber = request->frame_number;
3728 
3729     captureRequest->fmqSettingsSize = 0;
3730 
3731     {
3732         std::lock_guard<std::mutex> lock(mInflightLock);
3733         if (request->input_buffer != nullptr) {
3734             int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId();
3735             buffer_handle_t buf = *(request->input_buffer->buffer);
3736             auto pair = getBufferId(buf, streamId);
3737             bool isNewBuffer = pair.first;
3738             uint64_t bufferId = pair.second;
3739             captureRequest->inputBuffer.streamId = streamId;
3740             captureRequest->inputBuffer.bufferId = bufferId;
3741             captureRequest->inputBuffer.buffer = (isNewBuffer) ? buf : nullptr;
3742             captureRequest->inputBuffer.status = BufferStatus::OK;
3743             native_handle_t *acquireFence = nullptr;
3744             if (request->input_buffer->acquire_fence != -1) {
3745                 acquireFence = native_handle_create(1,0);
3746                 acquireFence->data[0] = request->input_buffer->acquire_fence;
3747                 handlesCreated->push_back(acquireFence);
3748             }
3749             captureRequest->inputBuffer.acquireFence = acquireFence;
3750             captureRequest->inputBuffer.releaseFence = nullptr;
3751 
3752             pushInflightBufferLocked(captureRequest->frameNumber, streamId,
3753                     request->input_buffer->buffer,
3754                     request->input_buffer->acquire_fence);
3755         } else {
3756             captureRequest->inputBuffer.streamId = -1;
3757             captureRequest->inputBuffer.bufferId = BUFFER_ID_NO_BUFFER;
3758         }
3759 
3760         captureRequest->outputBuffers.resize(request->num_output_buffers);
3761         for (size_t i = 0; i < request->num_output_buffers; i++) {
3762             const camera3_stream_buffer_t *src = request->output_buffers + i;
3763             StreamBuffer &dst = captureRequest->outputBuffers[i];
3764             int32_t streamId = Camera3Stream::cast(src->stream)->getId();
3765             buffer_handle_t buf = *(src->buffer);
3766             auto pair = getBufferId(buf, streamId);
3767             bool isNewBuffer = pair.first;
3768             dst.streamId = streamId;
3769             dst.bufferId = pair.second;
3770             dst.buffer = isNewBuffer ? buf : nullptr;
3771             dst.status = BufferStatus::OK;
3772             native_handle_t *acquireFence = nullptr;
3773             if (src->acquire_fence != -1) {
3774                 acquireFence = native_handle_create(1,0);
3775                 acquireFence->data[0] = src->acquire_fence;
3776                 handlesCreated->push_back(acquireFence);
3777             }
3778             dst.acquireFence = acquireFence;
3779             dst.releaseFence = nullptr;
3780 
3781             pushInflightBufferLocked(captureRequest->frameNumber, streamId,
3782                     src->buffer, src->acquire_fence);
3783         }
3784     }
3785 }
3786 
processBatchCaptureRequests(std::vector<camera3_capture_request_t * > & requests,uint32_t * numRequestProcessed)3787 status_t Camera3Device::HalInterface::processBatchCaptureRequests(
3788         std::vector<camera3_capture_request_t*>& requests,/*out*/uint32_t* numRequestProcessed) {
3789     ATRACE_NAME("CameraHal::processBatchCaptureRequests");
3790     if (!valid()) return INVALID_OPERATION;
3791 
3792     sp<device::V3_4::ICameraDeviceSession> hidlSession_3_4;
3793     auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
3794     if (castResult_3_4.isOk()) {
3795         hidlSession_3_4 = castResult_3_4;
3796     }
3797 
3798     hardware::hidl_vec<device::V3_2::CaptureRequest> captureRequests;
3799     hardware::hidl_vec<device::V3_4::CaptureRequest> captureRequests_3_4;
3800     size_t batchSize = requests.size();
3801     if (hidlSession_3_4 != nullptr) {
3802         captureRequests_3_4.resize(batchSize);
3803     } else {
3804         captureRequests.resize(batchSize);
3805     }
3806     std::vector<native_handle_t*> handlesCreated;
3807 
3808     for (size_t i = 0; i < batchSize; i++) {
3809         if (hidlSession_3_4 != nullptr) {
3810             wrapAsHidlRequest(requests[i], /*out*/&captureRequests_3_4[i].v3_2,
3811                     /*out*/&handlesCreated);
3812         } else {
3813             wrapAsHidlRequest(requests[i], /*out*/&captureRequests[i], /*out*/&handlesCreated);
3814         }
3815     }
3816 
3817     std::vector<device::V3_2::BufferCache> cachesToRemove;
3818     {
3819         std::lock_guard<std::mutex> lock(mBufferIdMapLock);
3820         for (auto& pair : mFreedBuffers) {
3821             // The stream might have been removed since onBufferFreed
3822             if (mBufferIdMaps.find(pair.first) != mBufferIdMaps.end()) {
3823                 cachesToRemove.push_back({pair.first, pair.second});
3824             }
3825         }
3826         mFreedBuffers.clear();
3827     }
3828 
3829     common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
3830     *numRequestProcessed = 0;
3831 
3832     // Write metadata to FMQ.
3833     for (size_t i = 0; i < batchSize; i++) {
3834         camera3_capture_request_t* request = requests[i];
3835         device::V3_2::CaptureRequest* captureRequest;
3836         if (hidlSession_3_4 != nullptr) {
3837             captureRequest = &captureRequests_3_4[i].v3_2;
3838         } else {
3839             captureRequest = &captureRequests[i];
3840         }
3841 
3842         if (request->settings != nullptr) {
3843             size_t settingsSize = get_camera_metadata_size(request->settings);
3844             if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
3845                     reinterpret_cast<const uint8_t*>(request->settings), settingsSize)) {
3846                 captureRequest->settings.resize(0);
3847                 captureRequest->fmqSettingsSize = settingsSize;
3848             } else {
3849                 if (mRequestMetadataQueue != nullptr) {
3850                     ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
3851                 }
3852                 captureRequest->settings.setToExternal(
3853                         reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(request->settings)),
3854                         get_camera_metadata_size(request->settings));
3855                 captureRequest->fmqSettingsSize = 0u;
3856             }
3857         } else {
3858             // A null request settings maps to a size-0 CameraMetadata
3859             captureRequest->settings.resize(0);
3860             captureRequest->fmqSettingsSize = 0u;
3861         }
3862 
3863         if (hidlSession_3_4 != nullptr) {
3864             captureRequests_3_4[i].physicalCameraSettings.resize(request->num_physcam_settings);
3865             for (size_t j = 0; j < request->num_physcam_settings; j++) {
3866                 if (request->physcam_settings != nullptr) {
3867                     size_t settingsSize = get_camera_metadata_size(request->physcam_settings[j]);
3868                     if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
3869                                 reinterpret_cast<const uint8_t*>(request->physcam_settings[j]),
3870                                 settingsSize)) {
3871                         captureRequests_3_4[i].physicalCameraSettings[j].settings.resize(0);
3872                         captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize =
3873                             settingsSize;
3874                     } else {
3875                         if (mRequestMetadataQueue != nullptr) {
3876                             ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
3877                         }
3878                         captureRequests_3_4[i].physicalCameraSettings[j].settings.setToExternal(
3879                                 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(
3880                                         request->physcam_settings[j])),
3881                                 get_camera_metadata_size(request->physcam_settings[j]));
3882                         captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize = 0u;
3883                     }
3884                 } else {
3885                     captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize = 0u;
3886                     captureRequests_3_4[i].physicalCameraSettings[j].settings.resize(0);
3887                 }
3888                 captureRequests_3_4[i].physicalCameraSettings[j].physicalCameraId =
3889                     request->physcam_id[j];
3890             }
3891         }
3892     }
3893 
3894     hardware::details::return_status err;
3895     if (hidlSession_3_4 != nullptr) {
3896         err = hidlSession_3_4->processCaptureRequest_3_4(captureRequests_3_4, cachesToRemove,
3897             [&status, &numRequestProcessed] (auto s, uint32_t n) {
3898                 status = s;
3899                 *numRequestProcessed = n;
3900             });
3901     } else {
3902         err = mHidlSession->processCaptureRequest(captureRequests, cachesToRemove,
3903             [&status, &numRequestProcessed] (auto s, uint32_t n) {
3904                 status = s;
3905                 *numRequestProcessed = n;
3906             });
3907     }
3908     if (!err.isOk()) {
3909         ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3910         return DEAD_OBJECT;
3911     }
3912     if (status == common::V1_0::Status::OK && *numRequestProcessed != batchSize) {
3913         ALOGE("%s: processCaptureRequest returns OK but processed %d/%zu requests",
3914                 __FUNCTION__, *numRequestProcessed, batchSize);
3915         status = common::V1_0::Status::INTERNAL_ERROR;
3916     }
3917 
3918     for (auto& handle : handlesCreated) {
3919         native_handle_delete(handle);
3920     }
3921     return CameraProviderManager::mapToStatusT(status);
3922 }
3923 
processCaptureRequest(camera3_capture_request_t * request)3924 status_t Camera3Device::HalInterface::processCaptureRequest(
3925         camera3_capture_request_t *request) {
3926     ATRACE_NAME("CameraHal::processCaptureRequest");
3927     if (!valid()) return INVALID_OPERATION;
3928     status_t res = OK;
3929 
3930     uint32_t numRequestProcessed = 0;
3931     std::vector<camera3_capture_request_t*> requests(1);
3932     requests[0] = request;
3933     res = processBatchCaptureRequests(requests, &numRequestProcessed);
3934 
3935     return res;
3936 }
3937 
flush()3938 status_t Camera3Device::HalInterface::flush() {
3939     ATRACE_NAME("CameraHal::flush");
3940     if (!valid()) return INVALID_OPERATION;
3941     status_t res = OK;
3942 
3943     auto err = mHidlSession->flush();
3944     if (!err.isOk()) {
3945         ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3946         res = DEAD_OBJECT;
3947     } else {
3948         res = CameraProviderManager::mapToStatusT(err);
3949     }
3950 
3951     return res;
3952 }
3953 
dump(int)3954 status_t Camera3Device::HalInterface::dump(int /*fd*/) {
3955     ATRACE_NAME("CameraHal::dump");
3956     if (!valid()) return INVALID_OPERATION;
3957 
3958     // Handled by CameraProviderManager::dump
3959 
3960     return OK;
3961 }
3962 
close()3963 status_t Camera3Device::HalInterface::close() {
3964     ATRACE_NAME("CameraHal::close()");
3965     if (!valid()) return INVALID_OPERATION;
3966     status_t res = OK;
3967 
3968     auto err = mHidlSession->close();
3969     // Interface will be dead shortly anyway, so don't log errors
3970     if (!err.isOk()) {
3971         res = DEAD_OBJECT;
3972     }
3973 
3974     return res;
3975 }
3976 
getInflightBufferKeys(std::vector<std::pair<int32_t,int32_t>> * out)3977 void Camera3Device::HalInterface::getInflightBufferKeys(
3978         std::vector<std::pair<int32_t, int32_t>>* out) {
3979     std::lock_guard<std::mutex> lock(mInflightLock);
3980     out->clear();
3981     out->reserve(mInflightBufferMap.size());
3982     for (auto& pair : mInflightBufferMap) {
3983         uint64_t key = pair.first;
3984         int32_t streamId = key & 0xFFFFFFFF;
3985         int32_t frameNumber = (key >> 32) & 0xFFFFFFFF;
3986         out->push_back(std::make_pair(frameNumber, streamId));
3987     }
3988     return;
3989 }
3990 
pushInflightBufferLocked(int32_t frameNumber,int32_t streamId,buffer_handle_t * buffer,int acquireFence)3991 status_t Camera3Device::HalInterface::pushInflightBufferLocked(
3992         int32_t frameNumber, int32_t streamId, buffer_handle_t *buffer, int acquireFence) {
3993     uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
3994     auto pair = std::make_pair(buffer, acquireFence);
3995     mInflightBufferMap[key] = pair;
3996     return OK;
3997 }
3998 
popInflightBuffer(int32_t frameNumber,int32_t streamId,buffer_handle_t ** buffer)3999 status_t Camera3Device::HalInterface::popInflightBuffer(
4000         int32_t frameNumber, int32_t streamId,
4001         /*out*/ buffer_handle_t **buffer) {
4002     std::lock_guard<std::mutex> lock(mInflightLock);
4003 
4004     uint64_t key = static_cast<uint64_t>(frameNumber) << 32 | static_cast<uint64_t>(streamId);
4005     auto it = mInflightBufferMap.find(key);
4006     if (it == mInflightBufferMap.end()) return NAME_NOT_FOUND;
4007     auto pair = it->second;
4008     *buffer = pair.first;
4009     int acquireFence = pair.second;
4010     if (acquireFence > 0) {
4011         ::close(acquireFence);
4012     }
4013     mInflightBufferMap.erase(it);
4014     return OK;
4015 }
4016 
getBufferId(const buffer_handle_t & buf,int streamId)4017 std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
4018         const buffer_handle_t& buf, int streamId) {
4019     std::lock_guard<std::mutex> lock(mBufferIdMapLock);
4020 
4021     BufferIdMap& bIdMap = mBufferIdMaps.at(streamId);
4022     auto it = bIdMap.find(buf);
4023     if (it == bIdMap.end()) {
4024         bIdMap[buf] = mNextBufferId++;
4025         ALOGV("stream %d now have %zu buffer caches, buf %p",
4026                 streamId, bIdMap.size(), buf);
4027         return std::make_pair(true, mNextBufferId - 1);
4028     } else {
4029         return std::make_pair(false, it->second);
4030     }
4031 }
4032 
onBufferFreed(int streamId,const native_handle_t * handle)4033 void Camera3Device::HalInterface::onBufferFreed(
4034         int streamId, const native_handle_t* handle) {
4035     std::lock_guard<std::mutex> lock(mBufferIdMapLock);
4036     uint64_t bufferId = BUFFER_ID_NO_BUFFER;
4037     auto mapIt = mBufferIdMaps.find(streamId);
4038     if (mapIt == mBufferIdMaps.end()) {
4039         // streamId might be from a deleted stream here
4040         ALOGI("%s: stream %d has been removed",
4041                 __FUNCTION__, streamId);
4042         return;
4043     }
4044     BufferIdMap& bIdMap = mapIt->second;
4045     auto it = bIdMap.find(handle);
4046     if (it == bIdMap.end()) {
4047         ALOGW("%s: cannot find buffer %p in stream %d",
4048                 __FUNCTION__, handle, streamId);
4049         return;
4050     } else {
4051         bufferId =  it->second;
4052         bIdMap.erase(it);
4053         ALOGV("%s: stream %d now have %zu buffer caches after removing buf %p",
4054                 __FUNCTION__, streamId, bIdMap.size(), handle);
4055     }
4056     mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
4057 }
4058 
4059 /**
4060  * RequestThread inner class methods
4061  */
4062 
RequestThread(wp<Camera3Device> parent,sp<StatusTracker> statusTracker,sp<HalInterface> interface,const Vector<int32_t> & sessionParamKeys)4063 Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
4064         sp<StatusTracker> statusTracker,
4065         sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys) :
4066         Thread(/*canCallJava*/false),
4067         mParent(parent),
4068         mStatusTracker(statusTracker),
4069         mInterface(interface),
4070         mListener(nullptr),
4071         mId(getId(parent)),
4072         mReconfigured(false),
4073         mDoPause(false),
4074         mPaused(true),
4075         mFrameNumber(0),
4076         mLatestRequestId(NAME_NOT_FOUND),
4077         mCurrentAfTriggerId(0),
4078         mCurrentPreCaptureTriggerId(0),
4079         mRepeatingLastFrameNumber(
4080             hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
4081         mPrepareVideoStream(false),
4082         mConstrainedMode(false),
4083         mRequestLatency(kRequestLatencyBinSize),
4084         mSessionParamKeys(sessionParamKeys),
4085         mLatestSessionParams(sessionParamKeys.size()) {
4086     mStatusId = statusTracker->addComponent();
4087 }
4088 
~RequestThread()4089 Camera3Device::RequestThread::~RequestThread() {}
4090 
setNotificationListener(wp<NotificationListener> listener)4091 void Camera3Device::RequestThread::setNotificationListener(
4092         wp<NotificationListener> listener) {
4093     ATRACE_CALL();
4094     Mutex::Autolock l(mRequestLock);
4095     mListener = listener;
4096 }
4097 
configurationComplete(bool isConstrainedHighSpeed,const CameraMetadata & sessionParams)4098 void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
4099         const CameraMetadata& sessionParams) {
4100     ATRACE_CALL();
4101     Mutex::Autolock l(mRequestLock);
4102     mReconfigured = true;
4103     mLatestSessionParams = sessionParams;
4104     // Prepare video stream for high speed recording.
4105     mPrepareVideoStream = isConstrainedHighSpeed;
4106     mConstrainedMode = isConstrainedHighSpeed;
4107 }
4108 
queueRequestList(List<sp<CaptureRequest>> & requests,int64_t * lastFrameNumber)4109 status_t Camera3Device::RequestThread::queueRequestList(
4110         List<sp<CaptureRequest> > &requests,
4111         /*out*/
4112         int64_t *lastFrameNumber) {
4113     ATRACE_CALL();
4114     Mutex::Autolock l(mRequestLock);
4115     for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
4116             ++it) {
4117         mRequestQueue.push_back(*it);
4118     }
4119 
4120     if (lastFrameNumber != NULL) {
4121         *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
4122         ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
4123               __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
4124               *lastFrameNumber);
4125     }
4126 
4127     unpauseForNewRequests();
4128 
4129     return OK;
4130 }
4131 
4132 
queueTrigger(RequestTrigger trigger[],size_t count)4133 status_t Camera3Device::RequestThread::queueTrigger(
4134         RequestTrigger trigger[],
4135         size_t count) {
4136     ATRACE_CALL();
4137     Mutex::Autolock l(mTriggerMutex);
4138     status_t ret;
4139 
4140     for (size_t i = 0; i < count; ++i) {
4141         ret = queueTriggerLocked(trigger[i]);
4142 
4143         if (ret != OK) {
4144             return ret;
4145         }
4146     }
4147 
4148     return OK;
4149 }
4150 
getId(const wp<Camera3Device> & device)4151 const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
4152     static String8 deadId("<DeadDevice>");
4153     sp<Camera3Device> d = device.promote();
4154     if (d != nullptr) return d->mId;
4155     return deadId;
4156 }
4157 
queueTriggerLocked(RequestTrigger trigger)4158 status_t Camera3Device::RequestThread::queueTriggerLocked(
4159         RequestTrigger trigger) {
4160 
4161     uint32_t tag = trigger.metadataTag;
4162     ssize_t index = mTriggerMap.indexOfKey(tag);
4163 
4164     switch (trigger.getTagType()) {
4165         case TYPE_BYTE:
4166         // fall-through
4167         case TYPE_INT32:
4168             break;
4169         default:
4170             ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
4171                     trigger.getTagType());
4172             return INVALID_OPERATION;
4173     }
4174 
4175     /**
4176      * Collect only the latest trigger, since we only have 1 field
4177      * in the request settings per trigger tag, and can't send more than 1
4178      * trigger per request.
4179      */
4180     if (index != NAME_NOT_FOUND) {
4181         mTriggerMap.editValueAt(index) = trigger;
4182     } else {
4183         mTriggerMap.add(tag, trigger);
4184     }
4185 
4186     return OK;
4187 }
4188 
setRepeatingRequests(const RequestList & requests,int64_t * lastFrameNumber)4189 status_t Camera3Device::RequestThread::setRepeatingRequests(
4190         const RequestList &requests,
4191         /*out*/
4192         int64_t *lastFrameNumber) {
4193     ATRACE_CALL();
4194     Mutex::Autolock l(mRequestLock);
4195     if (lastFrameNumber != NULL) {
4196         *lastFrameNumber = mRepeatingLastFrameNumber;
4197     }
4198     mRepeatingRequests.clear();
4199     mRepeatingRequests.insert(mRepeatingRequests.begin(),
4200             requests.begin(), requests.end());
4201 
4202     unpauseForNewRequests();
4203 
4204     mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
4205     return OK;
4206 }
4207 
isRepeatingRequestLocked(const sp<CaptureRequest> & requestIn)4208 bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
4209     if (mRepeatingRequests.empty()) {
4210         return false;
4211     }
4212     int32_t requestId = requestIn->mResultExtras.requestId;
4213     const RequestList &repeatRequests = mRepeatingRequests;
4214     // All repeating requests are guaranteed to have same id so only check first quest
4215     const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
4216     return (firstRequest->mResultExtras.requestId == requestId);
4217 }
4218 
clearRepeatingRequests(int64_t * lastFrameNumber)4219 status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
4220     ATRACE_CALL();
4221     Mutex::Autolock l(mRequestLock);
4222     return clearRepeatingRequestsLocked(lastFrameNumber);
4223 
4224 }
4225 
clearRepeatingRequestsLocked(int64_t * lastFrameNumber)4226 status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
4227     mRepeatingRequests.clear();
4228     if (lastFrameNumber != NULL) {
4229         *lastFrameNumber = mRepeatingLastFrameNumber;
4230     }
4231     mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
4232     return OK;
4233 }
4234 
clear(int64_t * lastFrameNumber)4235 status_t Camera3Device::RequestThread::clear(
4236         /*out*/int64_t *lastFrameNumber) {
4237     ATRACE_CALL();
4238     Mutex::Autolock l(mRequestLock);
4239     ALOGV("RequestThread::%s:", __FUNCTION__);
4240 
4241     mRepeatingRequests.clear();
4242 
4243     // Send errors for all requests pending in the request queue, including
4244     // pending repeating requests
4245     sp<NotificationListener> listener = mListener.promote();
4246     if (listener != NULL) {
4247         for (RequestList::iterator it = mRequestQueue.begin();
4248                  it != mRequestQueue.end(); ++it) {
4249             // Abort the input buffers for reprocess requests.
4250             if ((*it)->mInputStream != NULL) {
4251                 camera3_stream_buffer_t inputBuffer;
4252                 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
4253                         /*respectHalLimit*/ false);
4254                 if (res != OK) {
4255                     ALOGW("%s: %d: couldn't get input buffer while clearing the request "
4256                             "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
4257                 } else {
4258                     res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
4259                     if (res != OK) {
4260                         ALOGE("%s: %d: couldn't return input buffer while clearing the request "
4261                                 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
4262                     }
4263                 }
4264             }
4265             // Set the frame number this request would have had, if it
4266             // had been submitted; this frame number will not be reused.
4267             // The requestId and burstId fields were set when the request was
4268             // submitted originally (in convertMetadataListToRequestListLocked)
4269             (*it)->mResultExtras.frameNumber = mFrameNumber++;
4270             listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
4271                     (*it)->mResultExtras);
4272         }
4273     }
4274     mRequestQueue.clear();
4275 
4276     Mutex::Autolock al(mTriggerMutex);
4277     mTriggerMap.clear();
4278     if (lastFrameNumber != NULL) {
4279         *lastFrameNumber = mRepeatingLastFrameNumber;
4280     }
4281     mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
4282     return OK;
4283 }
4284 
flush()4285 status_t Camera3Device::RequestThread::flush() {
4286     ATRACE_CALL();
4287     Mutex::Autolock l(mFlushLock);
4288 
4289     return mInterface->flush();
4290 }
4291 
setPaused(bool paused)4292 void Camera3Device::RequestThread::setPaused(bool paused) {
4293     ATRACE_CALL();
4294     Mutex::Autolock l(mPauseLock);
4295     mDoPause = paused;
4296     mDoPauseSignal.signal();
4297 }
4298 
waitUntilRequestProcessed(int32_t requestId,nsecs_t timeout)4299 status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
4300         int32_t requestId, nsecs_t timeout) {
4301     ATRACE_CALL();
4302     Mutex::Autolock l(mLatestRequestMutex);
4303     status_t res;
4304     while (mLatestRequestId != requestId) {
4305         nsecs_t startTime = systemTime();
4306 
4307         res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
4308         if (res != OK) return res;
4309 
4310         timeout -= (systemTime() - startTime);
4311     }
4312 
4313     return OK;
4314 }
4315 
requestExit()4316 void Camera3Device::RequestThread::requestExit() {
4317     // Call parent to set up shutdown
4318     Thread::requestExit();
4319     // The exit from any possible waits
4320     mDoPauseSignal.signal();
4321     mRequestSignal.signal();
4322 
4323     mRequestLatency.log("ProcessCaptureRequest latency histogram");
4324     mRequestLatency.reset();
4325 }
4326 
checkAndStopRepeatingRequest()4327 void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
4328     ATRACE_CALL();
4329     bool surfaceAbandoned = false;
4330     int64_t lastFrameNumber = 0;
4331     sp<NotificationListener> listener;
4332     {
4333         Mutex::Autolock l(mRequestLock);
4334         // Check all streams needed by repeating requests are still valid. Otherwise, stop
4335         // repeating requests.
4336         for (const auto& request : mRepeatingRequests) {
4337             for (const auto& s : request->mOutputStreams) {
4338                 if (s->isAbandoned()) {
4339                     surfaceAbandoned = true;
4340                     clearRepeatingRequestsLocked(&lastFrameNumber);
4341                     break;
4342                 }
4343             }
4344             if (surfaceAbandoned) {
4345                 break;
4346             }
4347         }
4348         listener = mListener.promote();
4349     }
4350 
4351     if (listener != NULL && surfaceAbandoned) {
4352         listener->notifyRepeatingRequestError(lastFrameNumber);
4353     }
4354 }
4355 
sendRequestsBatch()4356 bool Camera3Device::RequestThread::sendRequestsBatch() {
4357     ATRACE_CALL();
4358     status_t res;
4359     size_t batchSize = mNextRequests.size();
4360     std::vector<camera3_capture_request_t*> requests(batchSize);
4361     uint32_t numRequestProcessed = 0;
4362     for (size_t i = 0; i < batchSize; i++) {
4363         requests[i] = &mNextRequests.editItemAt(i).halRequest;
4364         ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
4365     }
4366 
4367     res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
4368 
4369     bool triggerRemoveFailed = false;
4370     NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
4371     for (size_t i = 0; i < numRequestProcessed; i++) {
4372         NextRequest& nextRequest = mNextRequests.editItemAt(i);
4373         nextRequest.submitted = true;
4374 
4375 
4376         // Update the latest request sent to HAL
4377         if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
4378             Mutex::Autolock al(mLatestRequestMutex);
4379 
4380             camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
4381             mLatestRequest.acquire(cloned);
4382 
4383             sp<Camera3Device> parent = mParent.promote();
4384             if (parent != NULL) {
4385                 parent->monitorMetadata(TagMonitor::REQUEST,
4386                         nextRequest.halRequest.frame_number,
4387                         0, mLatestRequest);
4388             }
4389         }
4390 
4391         if (nextRequest.halRequest.settings != NULL) {
4392             nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
4393                     nextRequest.halRequest.settings);
4394         }
4395 
4396         cleanupPhysicalSettings(nextRequest.captureRequest, &nextRequest.halRequest);
4397 
4398         if (!triggerRemoveFailed) {
4399             // Remove any previously queued triggers (after unlock)
4400             status_t removeTriggerRes = removeTriggers(mPrevRequest);
4401             if (removeTriggerRes != OK) {
4402                 triggerRemoveFailed = true;
4403                 triggerFailedRequest = nextRequest;
4404             }
4405         }
4406     }
4407 
4408     if (triggerRemoveFailed) {
4409         SET_ERR("RequestThread: Unable to remove triggers "
4410               "(capture request %d, HAL device: %s (%d)",
4411               triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
4412         cleanUpFailedRequests(/*sendRequestError*/ false);
4413         return false;
4414     }
4415 
4416     if (res != OK) {
4417         // Should only get a failure here for malformed requests or device-level
4418         // errors, so consider all errors fatal.  Bad metadata failures should
4419         // come through notify.
4420         SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
4421                 mNextRequests[numRequestProcessed].halRequest.frame_number,
4422                 strerror(-res), res);
4423         cleanUpFailedRequests(/*sendRequestError*/ false);
4424         return false;
4425     }
4426     return true;
4427 }
4428 
sendRequestsOneByOne()4429 bool Camera3Device::RequestThread::sendRequestsOneByOne() {
4430     status_t res;
4431 
4432     for (auto& nextRequest : mNextRequests) {
4433         // Submit request and block until ready for next one
4434         ATRACE_ASYNC_BEGIN("frame capture", nextRequest.halRequest.frame_number);
4435         res = mInterface->processCaptureRequest(&nextRequest.halRequest);
4436 
4437         if (res != OK) {
4438             // Should only get a failure here for malformed requests or device-level
4439             // errors, so consider all errors fatal.  Bad metadata failures should
4440             // come through notify.
4441             SET_ERR("RequestThread: Unable to submit capture request %d to HAL"
4442                     " device: %s (%d)", nextRequest.halRequest.frame_number, strerror(-res),
4443                     res);
4444             cleanUpFailedRequests(/*sendRequestError*/ false);
4445             return false;
4446         }
4447 
4448         // Mark that the request has be submitted successfully.
4449         nextRequest.submitted = true;
4450 
4451         // Update the latest request sent to HAL
4452         if (nextRequest.halRequest.settings != NULL) { // Don't update if they were unchanged
4453             Mutex::Autolock al(mLatestRequestMutex);
4454 
4455             camera_metadata_t* cloned = clone_camera_metadata(nextRequest.halRequest.settings);
4456             mLatestRequest.acquire(cloned);
4457 
4458             sp<Camera3Device> parent = mParent.promote();
4459             if (parent != NULL) {
4460                 parent->monitorMetadata(TagMonitor::REQUEST, nextRequest.halRequest.frame_number,
4461                         0, mLatestRequest);
4462             }
4463         }
4464 
4465         if (nextRequest.halRequest.settings != NULL) {
4466             nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
4467                     nextRequest.halRequest.settings);
4468         }
4469 
4470         cleanupPhysicalSettings(nextRequest.captureRequest, &nextRequest.halRequest);
4471 
4472         // Remove any previously queued triggers (after unlock)
4473         res = removeTriggers(mPrevRequest);
4474         if (res != OK) {
4475             SET_ERR("RequestThread: Unable to remove triggers "
4476                   "(capture request %d, HAL device: %s (%d)",
4477                   nextRequest.halRequest.frame_number, strerror(-res), res);
4478             cleanUpFailedRequests(/*sendRequestError*/ false);
4479             return false;
4480         }
4481     }
4482     return true;
4483 }
4484 
calculateMaxExpectedDuration(const camera_metadata_t * request)4485 nsecs_t Camera3Device::RequestThread::calculateMaxExpectedDuration(const camera_metadata_t *request) {
4486     nsecs_t maxExpectedDuration = kDefaultExpectedDuration;
4487     camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4488     find_camera_metadata_ro_entry(request,
4489             ANDROID_CONTROL_AE_MODE,
4490             &e);
4491     if (e.count == 0) return maxExpectedDuration;
4492 
4493     switch (e.data.u8[0]) {
4494         case ANDROID_CONTROL_AE_MODE_OFF:
4495             find_camera_metadata_ro_entry(request,
4496                     ANDROID_SENSOR_EXPOSURE_TIME,
4497                     &e);
4498             if (e.count > 0) {
4499                 maxExpectedDuration = e.data.i64[0];
4500             }
4501             find_camera_metadata_ro_entry(request,
4502                     ANDROID_SENSOR_FRAME_DURATION,
4503                     &e);
4504             if (e.count > 0) {
4505                 maxExpectedDuration = std::max(e.data.i64[0], maxExpectedDuration);
4506             }
4507             break;
4508         default:
4509             find_camera_metadata_ro_entry(request,
4510                     ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
4511                     &e);
4512             if (e.count > 1) {
4513                 maxExpectedDuration = 1e9 / e.data.u8[0];
4514             }
4515             break;
4516     }
4517 
4518     return maxExpectedDuration;
4519 }
4520 
skipHFRTargetFPSUpdate(int32_t tag,const camera_metadata_ro_entry_t & newEntry,const camera_metadata_entry_t & currentEntry)4521 bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
4522         const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
4523     if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
4524             (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
4525             (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
4526         return true;
4527     }
4528 
4529     return false;
4530 }
4531 
updateSessionParameters(const CameraMetadata & settings)4532 bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
4533     ATRACE_CALL();
4534     bool updatesDetected = false;
4535 
4536     for (auto tag : mSessionParamKeys) {
4537         camera_metadata_ro_entry entry = settings.find(tag);
4538         camera_metadata_entry lastEntry = mLatestSessionParams.find(tag);
4539 
4540         if (entry.count > 0) {
4541             bool isDifferent = false;
4542             if (lastEntry.count > 0) {
4543                 // Have a last value, compare to see if changed
4544                 if (lastEntry.type == entry.type &&
4545                         lastEntry.count == entry.count) {
4546                     // Same type and count, compare values
4547                     size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
4548                     size_t entryBytes = bytesPerValue * lastEntry.count;
4549                     int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
4550                     if (cmp != 0) {
4551                         isDifferent = true;
4552                     }
4553                 } else {
4554                     // Count or type has changed
4555                     isDifferent = true;
4556                 }
4557             } else {
4558                 // No last entry, so always consider to be different
4559                 isDifferent = true;
4560             }
4561 
4562             if (isDifferent) {
4563                 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
4564                 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
4565                     updatesDetected = true;
4566                 }
4567                 mLatestSessionParams.update(entry);
4568             }
4569         } else if (lastEntry.count > 0) {
4570             // Value has been removed
4571             ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
4572             mLatestSessionParams.erase(tag);
4573             updatesDetected = true;
4574         }
4575     }
4576 
4577     return updatesDetected;
4578 }
4579 
threadLoop()4580 bool Camera3Device::RequestThread::threadLoop() {
4581     ATRACE_CALL();
4582     status_t res;
4583 
4584     // Handle paused state.
4585     if (waitIfPaused()) {
4586         return true;
4587     }
4588 
4589     // Wait for the next batch of requests.
4590     waitForNextRequestBatch();
4591     if (mNextRequests.size() == 0) {
4592         return true;
4593     }
4594 
4595     // Get the latest request ID, if any
4596     int latestRequestId;
4597     camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
4598             captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
4599     if (requestIdEntry.count > 0) {
4600         latestRequestId = requestIdEntry.data.i32[0];
4601     } else {
4602         ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
4603         latestRequestId = NAME_NOT_FOUND;
4604     }
4605 
4606     // 'mNextRequests' will at this point contain either a set of HFR batched requests
4607     //  or a single request from streaming or burst. In either case the first element
4608     //  should contain the latest camera settings that we need to check for any session
4609     //  parameter updates.
4610     if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
4611         res = OK;
4612 
4613         //Input stream buffers are already acquired at this point so an input stream
4614         //will not be able to move to idle state unless we force it.
4615         if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
4616             res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
4617             if (res != OK) {
4618                 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
4619                 cleanUpFailedRequests(/*sendRequestError*/ false);
4620                 return false;
4621             }
4622         }
4623 
4624         if (res == OK) {
4625             sp<StatusTracker> statusTracker = mStatusTracker.promote();
4626             if (statusTracker != 0) {
4627                 sp<Camera3Device> parent = mParent.promote();
4628                 if (parent != nullptr) {
4629                     parent->pauseStateNotify(true);
4630                 }
4631 
4632                 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4633 
4634                 if (parent != nullptr) {
4635                     mReconfigured |= parent->reconfigureCamera(mLatestSessionParams);
4636                 }
4637 
4638                 statusTracker->markComponentActive(mStatusId);
4639                 setPaused(false);
4640             }
4641 
4642             if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
4643                 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
4644                 if (res != OK) {
4645                     ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
4646                     cleanUpFailedRequests(/*sendRequestError*/ false);
4647                     return false;
4648                 }
4649             }
4650         }
4651     }
4652 
4653     // Prepare a batch of HAL requests and output buffers.
4654     res = prepareHalRequests();
4655     if (res == TIMED_OUT) {
4656         // Not a fatal error if getting output buffers time out.
4657         cleanUpFailedRequests(/*sendRequestError*/ true);
4658         // Check if any stream is abandoned.
4659         checkAndStopRepeatingRequest();
4660         return true;
4661     } else if (res != OK) {
4662         cleanUpFailedRequests(/*sendRequestError*/ false);
4663         return false;
4664     }
4665 
4666     // Inform waitUntilRequestProcessed thread of a new request ID
4667     {
4668         Mutex::Autolock al(mLatestRequestMutex);
4669 
4670         mLatestRequestId = latestRequestId;
4671         mLatestRequestSignal.signal();
4672     }
4673 
4674     // Submit a batch of requests to HAL.
4675     // Use flush lock only when submitting multilple requests in a batch.
4676     // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
4677     // which may take a long time to finish so synchronizing flush() and
4678     // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
4679     // For now, only synchronize for high speed recording and we should figure something out for
4680     // removing the synchronization.
4681     bool useFlushLock = mNextRequests.size() > 1;
4682 
4683     if (useFlushLock) {
4684         mFlushLock.lock();
4685     }
4686 
4687     ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
4688             mNextRequests.size());
4689 
4690     bool submitRequestSuccess = false;
4691     nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
4692     if (mInterface->supportBatchRequest()) {
4693         submitRequestSuccess = sendRequestsBatch();
4694     } else {
4695         submitRequestSuccess = sendRequestsOneByOne();
4696     }
4697     nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
4698     mRequestLatency.add(tRequestStart, tRequestEnd);
4699 
4700     if (useFlushLock) {
4701         mFlushLock.unlock();
4702     }
4703 
4704     // Unset as current request
4705     {
4706         Mutex::Autolock l(mRequestLock);
4707         mNextRequests.clear();
4708     }
4709 
4710     return submitRequestSuccess;
4711 }
4712 
prepareHalRequests()4713 status_t Camera3Device::RequestThread::prepareHalRequests() {
4714     ATRACE_CALL();
4715 
4716     for (size_t i = 0; i < mNextRequests.size(); i++) {
4717         auto& nextRequest = mNextRequests.editItemAt(i);
4718         sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
4719         camera3_capture_request_t* halRequest = &nextRequest.halRequest;
4720         Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
4721 
4722         // Prepare a request to HAL
4723         halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
4724 
4725         // Insert any queued triggers (before metadata is locked)
4726         status_t res = insertTriggers(captureRequest);
4727         if (res < 0) {
4728             SET_ERR("RequestThread: Unable to insert triggers "
4729                     "(capture request %d, HAL device: %s (%d)",
4730                     halRequest->frame_number, strerror(-res), res);
4731             return INVALID_OPERATION;
4732         }
4733 
4734         int triggerCount = res;
4735         bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
4736         mPrevTriggers = triggerCount;
4737 
4738         // If the request is the same as last, or we had triggers last time
4739         bool newRequest = mPrevRequest != captureRequest || triggersMixedIn;
4740         if (newRequest) {
4741             /**
4742              * HAL workaround:
4743              * Insert a dummy trigger ID if a trigger is set but no trigger ID is
4744              */
4745             res = addDummyTriggerIds(captureRequest);
4746             if (res != OK) {
4747                 SET_ERR("RequestThread: Unable to insert dummy trigger IDs "
4748                         "(capture request %d, HAL device: %s (%d)",
4749                         halRequest->frame_number, strerror(-res), res);
4750                 return INVALID_OPERATION;
4751             }
4752 
4753             {
4754                 // Correct metadata regions for distortion correction if enabled
4755                 sp<Camera3Device> parent = mParent.promote();
4756                 if (parent != nullptr) {
4757                     res = parent->mDistortionMapper.correctCaptureRequest(
4758                         &(captureRequest->mSettingsList.begin()->metadata));
4759                     if (res != OK) {
4760                         SET_ERR("RequestThread: Unable to correct capture requests "
4761                                 "for lens distortion for request %d: %s (%d)",
4762                                 halRequest->frame_number, strerror(-res), res);
4763                         return INVALID_OPERATION;
4764                     }
4765                 }
4766             }
4767 
4768             /**
4769              * The request should be presorted so accesses in HAL
4770              *   are O(logn). Sidenote, sorting a sorted metadata is nop.
4771              */
4772             captureRequest->mSettingsList.begin()->metadata.sort();
4773             halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
4774             mPrevRequest = captureRequest;
4775             ALOGVV("%s: Request settings are NEW", __FUNCTION__);
4776 
4777             IF_ALOGV() {
4778                 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4779                 find_camera_metadata_ro_entry(
4780                         halRequest->settings,
4781                         ANDROID_CONTROL_AF_TRIGGER,
4782                         &e
4783                 );
4784                 if (e.count > 0) {
4785                     ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
4786                           __FUNCTION__,
4787                           halRequest->frame_number,
4788                           e.data.u8[0]);
4789                 }
4790             }
4791         } else {
4792             // leave request.settings NULL to indicate 'reuse latest given'
4793             ALOGVV("%s: Request settings are REUSED",
4794                    __FUNCTION__);
4795         }
4796 
4797         if (captureRequest->mSettingsList.size() > 1) {
4798             halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
4799             halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
4800             if (newRequest) {
4801                 halRequest->physcam_settings =
4802                     new const camera_metadata* [halRequest->num_physcam_settings];
4803             } else {
4804                 halRequest->physcam_settings = nullptr;
4805             }
4806             auto it = ++captureRequest->mSettingsList.begin();
4807             size_t i = 0;
4808             for (; it != captureRequest->mSettingsList.end(); it++, i++) {
4809                 halRequest->physcam_id[i] = it->cameraId.c_str();
4810                 if (newRequest) {
4811                     it->metadata.sort();
4812                     halRequest->physcam_settings[i] = it->metadata.getAndLock();
4813                 }
4814             }
4815         }
4816 
4817         uint32_t totalNumBuffers = 0;
4818 
4819         // Fill in buffers
4820         if (captureRequest->mInputStream != NULL) {
4821             halRequest->input_buffer = &captureRequest->mInputBuffer;
4822             totalNumBuffers += 1;
4823         } else {
4824             halRequest->input_buffer = NULL;
4825         }
4826 
4827         outputBuffers->insertAt(camera3_stream_buffer_t(), 0,
4828                 captureRequest->mOutputStreams.size());
4829         halRequest->output_buffers = outputBuffers->array();
4830         std::set<String8> requestedPhysicalCameras;
4831         for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
4832             sp<Camera3OutputStreamInterface> outputStream = captureRequest->mOutputStreams.editItemAt(j);
4833 
4834             // Prepare video buffers for high speed recording on the first video request.
4835             if (mPrepareVideoStream && outputStream->isVideoStream()) {
4836                 // Only try to prepare video stream on the first video request.
4837                 mPrepareVideoStream = false;
4838 
4839                 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX);
4840                 while (res == NOT_ENOUGH_DATA) {
4841                     res = outputStream->prepareNextBuffer();
4842                 }
4843                 if (res != OK) {
4844                     ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
4845                         __FUNCTION__, strerror(-res), res);
4846                     outputStream->cancelPrepare();
4847                 }
4848             }
4849 
4850             res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
4851                     captureRequest->mOutputSurfaces[j]);
4852             if (res != OK) {
4853                 // Can't get output buffer from gralloc queue - this could be due to
4854                 // abandoned queue or other consumer misbehavior, so not a fatal
4855                 // error
4856                 ALOGE("RequestThread: Can't get output buffer, skipping request:"
4857                         " %s (%d)", strerror(-res), res);
4858 
4859                 return TIMED_OUT;
4860             }
4861 
4862             String8 physicalCameraId = outputStream->getPhysicalCameraId();
4863 
4864             if (!physicalCameraId.isEmpty()) {
4865                 // Physical stream isn't supported for input request.
4866                 if (halRequest->input_buffer) {
4867                     CLOGE("Physical stream is not supported for input request");
4868                     return INVALID_OPERATION;
4869                 }
4870                 requestedPhysicalCameras.insert(physicalCameraId);
4871             }
4872             halRequest->num_output_buffers++;
4873         }
4874         totalNumBuffers += halRequest->num_output_buffers;
4875 
4876         // Log request in the in-flight queue
4877         sp<Camera3Device> parent = mParent.promote();
4878         if (parent == NULL) {
4879             // Should not happen, and nowhere to send errors to, so just log it
4880             CLOGE("RequestThread: Parent is gone");
4881             return INVALID_OPERATION;
4882         }
4883 
4884         // If this request list is for constrained high speed recording (not
4885         // preview), and the current request is not the last one in the batch,
4886         // do not send callback to the app.
4887         bool hasCallback = true;
4888         if (mNextRequests[0].captureRequest->mBatchSize > 1 && i != mNextRequests.size()-1) {
4889             hasCallback = false;
4890         }
4891         res = parent->registerInFlight(halRequest->frame_number,
4892                 totalNumBuffers, captureRequest->mResultExtras,
4893                 /*hasInput*/halRequest->input_buffer != NULL,
4894                 hasCallback,
4895                 calculateMaxExpectedDuration(halRequest->settings),
4896                 requestedPhysicalCameras);
4897         ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
4898                ", burstId = %" PRId32 ".",
4899                 __FUNCTION__,
4900                 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
4901                 captureRequest->mResultExtras.burstId);
4902         if (res != OK) {
4903             SET_ERR("RequestThread: Unable to register new in-flight request:"
4904                     " %s (%d)", strerror(-res), res);
4905             return INVALID_OPERATION;
4906         }
4907     }
4908 
4909     return OK;
4910 }
4911 
getLatestRequest() const4912 CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
4913     ATRACE_CALL();
4914     Mutex::Autolock al(mLatestRequestMutex);
4915 
4916     ALOGV("RequestThread::%s", __FUNCTION__);
4917 
4918     return mLatestRequest;
4919 }
4920 
isStreamPending(sp<Camera3StreamInterface> & stream)4921 bool Camera3Device::RequestThread::isStreamPending(
4922         sp<Camera3StreamInterface>& stream) {
4923     ATRACE_CALL();
4924     Mutex::Autolock l(mRequestLock);
4925 
4926     for (const auto& nextRequest : mNextRequests) {
4927         if (!nextRequest.submitted) {
4928             for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
4929                 if (stream == s) return true;
4930             }
4931             if (stream == nextRequest.captureRequest->mInputStream) return true;
4932         }
4933     }
4934 
4935     for (const auto& request : mRequestQueue) {
4936         for (const auto& s : request->mOutputStreams) {
4937             if (stream == s) return true;
4938         }
4939         if (stream == request->mInputStream) return true;
4940     }
4941 
4942     for (const auto& request : mRepeatingRequests) {
4943         for (const auto& s : request->mOutputStreams) {
4944             if (stream == s) return true;
4945         }
4946         if (stream == request->mInputStream) return true;
4947     }
4948 
4949     return false;
4950 }
4951 
isOutputSurfacePending(int streamId,size_t surfaceId)4952 bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
4953     ATRACE_CALL();
4954     Mutex::Autolock l(mRequestLock);
4955 
4956     for (const auto& nextRequest : mNextRequests) {
4957         for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
4958             if (s.first == streamId) {
4959                 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4960                 if (it != s.second.end()) {
4961                     return true;
4962                 }
4963             }
4964         }
4965     }
4966 
4967     for (const auto& request : mRequestQueue) {
4968         for (const auto& s : request->mOutputSurfaces) {
4969             if (s.first == streamId) {
4970                 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4971                 if (it != s.second.end()) {
4972                   return true;
4973                 }
4974             }
4975         }
4976     }
4977 
4978     for (const auto& request : mRepeatingRequests) {
4979         for (const auto& s : request->mOutputSurfaces) {
4980             if (s.first == streamId) {
4981                 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4982                 if (it != s.second.end()) {
4983                   return true;
4984                 }
4985             }
4986         }
4987     }
4988 
4989     return false;
4990 }
4991 
getExpectedInFlightDuration()4992 nsecs_t Camera3Device::getExpectedInFlightDuration() {
4993     ATRACE_CALL();
4994     Mutex::Autolock al(mInFlightLock);
4995     return mExpectedInflightDuration > kMinInflightDuration ?
4996             mExpectedInflightDuration : kMinInflightDuration;
4997 }
4998 
cleanupPhysicalSettings(sp<CaptureRequest> request,camera3_capture_request_t * halRequest)4999 void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
5000         camera3_capture_request_t *halRequest) {
5001     if ((request == nullptr) || (halRequest == nullptr)) {
5002         ALOGE("%s: Invalid request!", __FUNCTION__);
5003         return;
5004     }
5005 
5006     if (halRequest->num_physcam_settings > 0) {
5007         if (halRequest->physcam_id != nullptr) {
5008             delete [] halRequest->physcam_id;
5009             halRequest->physcam_id = nullptr;
5010         }
5011         if (halRequest->physcam_settings != nullptr) {
5012             auto it = ++(request->mSettingsList.begin());
5013             size_t i = 0;
5014             for (; it != request->mSettingsList.end(); it++, i++) {
5015                 it->metadata.unlock(halRequest->physcam_settings[i]);
5016             }
5017             delete [] halRequest->physcam_settings;
5018             halRequest->physcam_settings = nullptr;
5019         }
5020     }
5021 }
5022 
cleanUpFailedRequests(bool sendRequestError)5023 void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
5024     if (mNextRequests.empty()) {
5025         return;
5026     }
5027 
5028     for (auto& nextRequest : mNextRequests) {
5029         // Skip the ones that have been submitted successfully.
5030         if (nextRequest.submitted) {
5031             continue;
5032         }
5033 
5034         sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
5035         camera3_capture_request_t* halRequest = &nextRequest.halRequest;
5036         Vector<camera3_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
5037 
5038         if (halRequest->settings != NULL) {
5039             captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
5040         }
5041 
5042         cleanupPhysicalSettings(captureRequest, halRequest);
5043 
5044         if (captureRequest->mInputStream != NULL) {
5045             captureRequest->mInputBuffer.status = CAMERA3_BUFFER_STATUS_ERROR;
5046             captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
5047         }
5048 
5049         for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
5050             //Buffers that failed processing could still have
5051             //valid acquire fence.
5052             int acquireFence = (*outputBuffers)[i].acquire_fence;
5053             if (0 <= acquireFence) {
5054                 close(acquireFence);
5055                 outputBuffers->editItemAt(i).acquire_fence = -1;
5056             }
5057             outputBuffers->editItemAt(i).status = CAMERA3_BUFFER_STATUS_ERROR;
5058             captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0);
5059         }
5060 
5061         if (sendRequestError) {
5062             Mutex::Autolock l(mRequestLock);
5063             sp<NotificationListener> listener = mListener.promote();
5064             if (listener != NULL) {
5065                 listener->notifyError(
5066                         hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
5067                         captureRequest->mResultExtras);
5068             }
5069         }
5070 
5071         // Remove yet-to-be submitted inflight request from inflightMap
5072         {
5073           sp<Camera3Device> parent = mParent.promote();
5074           if (parent != NULL) {
5075               Mutex::Autolock l(parent->mInFlightLock);
5076               ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
5077               if (idx >= 0) {
5078                   ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
5079                         __FUNCTION__, captureRequest->mResultExtras.frameNumber);
5080                   parent->removeInFlightMapEntryLocked(idx);
5081               }
5082           }
5083         }
5084     }
5085 
5086     Mutex::Autolock l(mRequestLock);
5087     mNextRequests.clear();
5088 }
5089 
waitForNextRequestBatch()5090 void Camera3Device::RequestThread::waitForNextRequestBatch() {
5091     ATRACE_CALL();
5092     // Optimized a bit for the simple steady-state case (single repeating
5093     // request), to avoid putting that request in the queue temporarily.
5094     Mutex::Autolock l(mRequestLock);
5095 
5096     assert(mNextRequests.empty());
5097 
5098     NextRequest nextRequest;
5099     nextRequest.captureRequest = waitForNextRequestLocked();
5100     if (nextRequest.captureRequest == nullptr) {
5101         return;
5102     }
5103 
5104     nextRequest.halRequest = camera3_capture_request_t();
5105     nextRequest.submitted = false;
5106     mNextRequests.add(nextRequest);
5107 
5108     // Wait for additional requests
5109     const size_t batchSize = nextRequest.captureRequest->mBatchSize;
5110 
5111     for (size_t i = 1; i < batchSize; i++) {
5112         NextRequest additionalRequest;
5113         additionalRequest.captureRequest = waitForNextRequestLocked();
5114         if (additionalRequest.captureRequest == nullptr) {
5115             break;
5116         }
5117 
5118         additionalRequest.halRequest = camera3_capture_request_t();
5119         additionalRequest.submitted = false;
5120         mNextRequests.add(additionalRequest);
5121     }
5122 
5123     if (mNextRequests.size() < batchSize) {
5124         ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
5125                 mNextRequests.size(), batchSize);
5126         cleanUpFailedRequests(/*sendRequestError*/true);
5127     }
5128 
5129     return;
5130 }
5131 
5132 sp<Camera3Device::CaptureRequest>
waitForNextRequestLocked()5133         Camera3Device::RequestThread::waitForNextRequestLocked() {
5134     status_t res;
5135     sp<CaptureRequest> nextRequest;
5136 
5137     while (mRequestQueue.empty()) {
5138         if (!mRepeatingRequests.empty()) {
5139             // Always atomically enqueue all requests in a repeating request
5140             // list. Guarantees a complete in-sequence set of captures to
5141             // application.
5142             const RequestList &requests = mRepeatingRequests;
5143             RequestList::const_iterator firstRequest =
5144                     requests.begin();
5145             nextRequest = *firstRequest;
5146             mRequestQueue.insert(mRequestQueue.end(),
5147                     ++firstRequest,
5148                     requests.end());
5149             // No need to wait any longer
5150 
5151             mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
5152 
5153             break;
5154         }
5155 
5156         res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
5157 
5158         if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
5159                 exitPending()) {
5160             Mutex::Autolock pl(mPauseLock);
5161             if (mPaused == false) {
5162                 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
5163                 mPaused = true;
5164                 // Let the tracker know
5165                 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5166                 if (statusTracker != 0) {
5167                     statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
5168                 }
5169             }
5170             // Stop waiting for now and let thread management happen
5171             return NULL;
5172         }
5173     }
5174 
5175     if (nextRequest == NULL) {
5176         // Don't have a repeating request already in hand, so queue
5177         // must have an entry now.
5178         RequestList::iterator firstRequest =
5179                 mRequestQueue.begin();
5180         nextRequest = *firstRequest;
5181         mRequestQueue.erase(firstRequest);
5182         if (mRequestQueue.empty() && !nextRequest->mRepeating) {
5183             sp<NotificationListener> listener = mListener.promote();
5184             if (listener != NULL) {
5185                 listener->notifyRequestQueueEmpty();
5186             }
5187         }
5188     }
5189 
5190     // In case we've been unpaused by setPaused clearing mDoPause, need to
5191     // update internal pause state (capture/setRepeatingRequest unpause
5192     // directly).
5193     Mutex::Autolock pl(mPauseLock);
5194     if (mPaused) {
5195         ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
5196         sp<StatusTracker> statusTracker = mStatusTracker.promote();
5197         if (statusTracker != 0) {
5198             statusTracker->markComponentActive(mStatusId);
5199         }
5200     }
5201     mPaused = false;
5202 
5203     // Check if we've reconfigured since last time, and reset the preview
5204     // request if so. Can't use 'NULL request == repeat' across configure calls.
5205     if (mReconfigured) {
5206         mPrevRequest.clear();
5207         mReconfigured = false;
5208     }
5209 
5210     if (nextRequest != NULL) {
5211         nextRequest->mResultExtras.frameNumber = mFrameNumber++;
5212         nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
5213         nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
5214 
5215         // Since RequestThread::clear() removes buffers from the input stream,
5216         // get the right buffer here before unlocking mRequestLock
5217         if (nextRequest->mInputStream != NULL) {
5218             res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer);
5219             if (res != OK) {
5220                 // Can't get input buffer from gralloc queue - this could be due to
5221                 // disconnected queue or other producer misbehavior, so not a fatal
5222                 // error
5223                 ALOGE("%s: Can't get input buffer, skipping request:"
5224                         " %s (%d)", __FUNCTION__, strerror(-res), res);
5225 
5226                 sp<NotificationListener> listener = mListener.promote();
5227                 if (listener != NULL) {
5228                     listener->notifyError(
5229                             hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
5230                             nextRequest->mResultExtras);
5231                 }
5232                 return NULL;
5233             }
5234         }
5235     }
5236 
5237     return nextRequest;
5238 }
5239 
waitIfPaused()5240 bool Camera3Device::RequestThread::waitIfPaused() {
5241     ATRACE_CALL();
5242     status_t res;
5243     Mutex::Autolock l(mPauseLock);
5244     while (mDoPause) {
5245         if (mPaused == false) {
5246             mPaused = true;
5247             ALOGV("%s: RequestThread: Paused", __FUNCTION__);
5248             // Let the tracker know
5249             sp<StatusTracker> statusTracker = mStatusTracker.promote();
5250             if (statusTracker != 0) {
5251                 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
5252             }
5253         }
5254 
5255         res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
5256         if (res == TIMED_OUT || exitPending()) {
5257             return true;
5258         }
5259     }
5260     // We don't set mPaused to false here, because waitForNextRequest needs
5261     // to further manage the paused state in case of starvation.
5262     return false;
5263 }
5264 
unpauseForNewRequests()5265 void Camera3Device::RequestThread::unpauseForNewRequests() {
5266     ATRACE_CALL();
5267     // With work to do, mark thread as unpaused.
5268     // If paused by request (setPaused), don't resume, to avoid
5269     // extra signaling/waiting overhead to waitUntilPaused
5270     mRequestSignal.signal();
5271     Mutex::Autolock p(mPauseLock);
5272     if (!mDoPause) {
5273         ALOGV("%s: RequestThread: Going active", __FUNCTION__);
5274         if (mPaused) {
5275             sp<StatusTracker> statusTracker = mStatusTracker.promote();
5276             if (statusTracker != 0) {
5277                 statusTracker->markComponentActive(mStatusId);
5278             }
5279         }
5280         mPaused = false;
5281     }
5282 }
5283 
setErrorState(const char * fmt,...)5284 void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
5285     sp<Camera3Device> parent = mParent.promote();
5286     if (parent != NULL) {
5287         va_list args;
5288         va_start(args, fmt);
5289 
5290         parent->setErrorStateV(fmt, args);
5291 
5292         va_end(args);
5293     }
5294 }
5295 
insertTriggers(const sp<CaptureRequest> & request)5296 status_t Camera3Device::RequestThread::insertTriggers(
5297         const sp<CaptureRequest> &request) {
5298     ATRACE_CALL();
5299     Mutex::Autolock al(mTriggerMutex);
5300 
5301     sp<Camera3Device> parent = mParent.promote();
5302     if (parent == NULL) {
5303         CLOGE("RequestThread: Parent is gone");
5304         return DEAD_OBJECT;
5305     }
5306 
5307     CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
5308     size_t count = mTriggerMap.size();
5309 
5310     for (size_t i = 0; i < count; ++i) {
5311         RequestTrigger trigger = mTriggerMap.valueAt(i);
5312         uint32_t tag = trigger.metadataTag;
5313 
5314         if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
5315             bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
5316             uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
5317             if (isAeTrigger) {
5318                 request->mResultExtras.precaptureTriggerId = triggerId;
5319                 mCurrentPreCaptureTriggerId = triggerId;
5320             } else {
5321                 request->mResultExtras.afTriggerId = triggerId;
5322                 mCurrentAfTriggerId = triggerId;
5323             }
5324             continue;
5325         }
5326 
5327         camera_metadata_entry entry = metadata.find(tag);
5328 
5329         if (entry.count > 0) {
5330             /**
5331              * Already has an entry for this trigger in the request.
5332              * Rewrite it with our requested trigger value.
5333              */
5334             RequestTrigger oldTrigger = trigger;
5335 
5336             oldTrigger.entryValue = entry.data.u8[0];
5337 
5338             mTriggerReplacedMap.add(tag, oldTrigger);
5339         } else {
5340             /**
5341              * More typical, no trigger entry, so we just add it
5342              */
5343             mTriggerRemovedMap.add(tag, trigger);
5344         }
5345 
5346         status_t res;
5347 
5348         switch (trigger.getTagType()) {
5349             case TYPE_BYTE: {
5350                 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
5351                 res = metadata.update(tag,
5352                                       &entryValue,
5353                                       /*count*/1);
5354                 break;
5355             }
5356             case TYPE_INT32:
5357                 res = metadata.update(tag,
5358                                       &trigger.entryValue,
5359                                       /*count*/1);
5360                 break;
5361             default:
5362                 ALOGE("%s: Type not supported: 0x%x",
5363                       __FUNCTION__,
5364                       trigger.getTagType());
5365                 return INVALID_OPERATION;
5366         }
5367 
5368         if (res != OK) {
5369             ALOGE("%s: Failed to update request metadata with trigger tag %s"
5370                   ", value %d", __FUNCTION__, trigger.getTagName(),
5371                   trigger.entryValue);
5372             return res;
5373         }
5374 
5375         ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
5376               trigger.getTagName(),
5377               trigger.entryValue);
5378     }
5379 
5380     mTriggerMap.clear();
5381 
5382     return count;
5383 }
5384 
removeTriggers(const sp<CaptureRequest> & request)5385 status_t Camera3Device::RequestThread::removeTriggers(
5386         const sp<CaptureRequest> &request) {
5387     ATRACE_CALL();
5388     Mutex::Autolock al(mTriggerMutex);
5389 
5390     CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
5391 
5392     /**
5393      * Replace all old entries with their old values.
5394      */
5395     for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
5396         RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
5397 
5398         status_t res;
5399 
5400         uint32_t tag = trigger.metadataTag;
5401         switch (trigger.getTagType()) {
5402             case TYPE_BYTE: {
5403                 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
5404                 res = metadata.update(tag,
5405                                       &entryValue,
5406                                       /*count*/1);
5407                 break;
5408             }
5409             case TYPE_INT32:
5410                 res = metadata.update(tag,
5411                                       &trigger.entryValue,
5412                                       /*count*/1);
5413                 break;
5414             default:
5415                 ALOGE("%s: Type not supported: 0x%x",
5416                       __FUNCTION__,
5417                       trigger.getTagType());
5418                 return INVALID_OPERATION;
5419         }
5420 
5421         if (res != OK) {
5422             ALOGE("%s: Failed to restore request metadata with trigger tag %s"
5423                   ", trigger value %d", __FUNCTION__,
5424                   trigger.getTagName(), trigger.entryValue);
5425             return res;
5426         }
5427     }
5428     mTriggerReplacedMap.clear();
5429 
5430     /**
5431      * Remove all new entries.
5432      */
5433     for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
5434         RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
5435         status_t res = metadata.erase(trigger.metadataTag);
5436 
5437         if (res != OK) {
5438             ALOGE("%s: Failed to erase metadata with trigger tag %s"
5439                   ", trigger value %d", __FUNCTION__,
5440                   trigger.getTagName(), trigger.entryValue);
5441             return res;
5442         }
5443     }
5444     mTriggerRemovedMap.clear();
5445 
5446     return OK;
5447 }
5448 
addDummyTriggerIds(const sp<CaptureRequest> & request)5449 status_t Camera3Device::RequestThread::addDummyTriggerIds(
5450         const sp<CaptureRequest> &request) {
5451     // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
5452     static const int32_t dummyTriggerId = 1;
5453     status_t res;
5454 
5455     CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
5456 
5457     // If AF trigger is active, insert a dummy AF trigger ID if none already
5458     // exists
5459     camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
5460     camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
5461     if (afTrigger.count > 0 &&
5462             afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
5463             afId.count == 0) {
5464         res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &dummyTriggerId, 1);
5465         if (res != OK) return res;
5466     }
5467 
5468     // If AE precapture trigger is active, insert a dummy precapture trigger ID
5469     // if none already exists
5470     camera_metadata_entry pcTrigger =
5471             metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
5472     camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
5473     if (pcTrigger.count > 0 &&
5474             pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
5475             pcId.count == 0) {
5476         res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
5477                 &dummyTriggerId, 1);
5478         if (res != OK) return res;
5479     }
5480 
5481     return OK;
5482 }
5483 
5484 /**
5485  * PreparerThread inner class methods
5486  */
5487 
PreparerThread()5488 Camera3Device::PreparerThread::PreparerThread() :
5489         Thread(/*canCallJava*/false), mListener(nullptr),
5490         mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
5491 }
5492 
~PreparerThread()5493 Camera3Device::PreparerThread::~PreparerThread() {
5494     Thread::requestExitAndWait();
5495     if (mCurrentStream != nullptr) {
5496         mCurrentStream->cancelPrepare();
5497         ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5498         mCurrentStream.clear();
5499     }
5500     clear();
5501 }
5502 
prepare(int maxCount,sp<Camera3StreamInterface> & stream)5503 status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
5504     ATRACE_CALL();
5505     status_t res;
5506 
5507     Mutex::Autolock l(mLock);
5508     sp<NotificationListener> listener = mListener.promote();
5509 
5510     res = stream->startPrepare(maxCount);
5511     if (res == OK) {
5512         // No preparation needed, fire listener right off
5513         ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
5514         if (listener != NULL) {
5515             listener->notifyPrepared(stream->getId());
5516         }
5517         return OK;
5518     } else if (res != NOT_ENOUGH_DATA) {
5519         return res;
5520     }
5521 
5522     // Need to prepare, start up thread if necessary
5523     if (!mActive) {
5524         // mRunning will change to false before the thread fully shuts down, so wait to be sure it
5525         // isn't running
5526         Thread::requestExitAndWait();
5527         res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5528         if (res != OK) {
5529             ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
5530             if (listener != NULL) {
5531                 listener->notifyPrepared(stream->getId());
5532             }
5533             return res;
5534         }
5535         mCancelNow = false;
5536         mActive = true;
5537         ALOGV("%s: Preparer stream started", __FUNCTION__);
5538     }
5539 
5540     // queue up the work
5541     mPendingStreams.emplace(maxCount, stream);
5542     ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
5543 
5544     return OK;
5545 }
5546 
pause()5547 void Camera3Device::PreparerThread::pause() {
5548     ATRACE_CALL();
5549 
5550     Mutex::Autolock l(mLock);
5551 
5552     std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
5553     pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
5554     sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
5555     int currentMaxCount = mCurrentMaxCount;
5556     mPendingStreams.clear();
5557     mCancelNow = true;
5558     while (mActive) {
5559         auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
5560         if (res == TIMED_OUT) {
5561             ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
5562             return;
5563         } else if (res != OK) {
5564             ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
5565             return;
5566         }
5567     }
5568 
5569     //Check whether the prepare thread was able to complete the current
5570     //stream. In case work is still pending emplace it along with the rest
5571     //of the streams in the pending list.
5572     if (currentStream != nullptr) {
5573         if (!mCurrentPrepareComplete) {
5574             pendingStreams.emplace(currentMaxCount, currentStream);
5575         }
5576     }
5577 
5578     mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
5579     for (const auto& it : mPendingStreams) {
5580         it.second->cancelPrepare();
5581     }
5582 }
5583 
resume()5584 status_t Camera3Device::PreparerThread::resume() {
5585     ATRACE_CALL();
5586     status_t res;
5587 
5588     Mutex::Autolock l(mLock);
5589     sp<NotificationListener> listener = mListener.promote();
5590 
5591     if (mActive) {
5592         ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
5593         return NO_INIT;
5594     }
5595 
5596     auto it = mPendingStreams.begin();
5597     for (; it != mPendingStreams.end();) {
5598         res = it->second->startPrepare(it->first);
5599         if (res == OK) {
5600             if (listener != NULL) {
5601                 listener->notifyPrepared(it->second->getId());
5602             }
5603             it = mPendingStreams.erase(it);
5604         } else if (res != NOT_ENOUGH_DATA) {
5605             ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
5606                     res, strerror(-res));
5607             it = mPendingStreams.erase(it);
5608         } else {
5609             it++;
5610         }
5611     }
5612 
5613     if (mPendingStreams.empty()) {
5614         return OK;
5615     }
5616 
5617     res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5618     if (res != OK) {
5619         ALOGE("%s: Unable to start preparer stream: %d (%s)",
5620                 __FUNCTION__, res, strerror(-res));
5621         return res;
5622     }
5623     mCancelNow = false;
5624     mActive = true;
5625     ALOGV("%s: Preparer stream started", __FUNCTION__);
5626 
5627     return OK;
5628 }
5629 
clear()5630 status_t Camera3Device::PreparerThread::clear() {
5631     ATRACE_CALL();
5632     Mutex::Autolock l(mLock);
5633 
5634     for (const auto& it : mPendingStreams) {
5635         it.second->cancelPrepare();
5636     }
5637     mPendingStreams.clear();
5638     mCancelNow = true;
5639 
5640     return OK;
5641 }
5642 
setNotificationListener(wp<NotificationListener> listener)5643 void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
5644     ATRACE_CALL();
5645     Mutex::Autolock l(mLock);
5646     mListener = listener;
5647 }
5648 
threadLoop()5649 bool Camera3Device::PreparerThread::threadLoop() {
5650     status_t res;
5651     {
5652         Mutex::Autolock l(mLock);
5653         if (mCurrentStream == nullptr) {
5654             // End thread if done with work
5655             if (mPendingStreams.empty()) {
5656                 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
5657                 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
5658                 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
5659                 mActive = false;
5660                 mThreadActiveSignal.signal();
5661                 return false;
5662             }
5663 
5664             // Get next stream to prepare
5665             auto it = mPendingStreams.begin();
5666             mCurrentStream = it->second;
5667             mCurrentMaxCount = it->first;
5668             mCurrentPrepareComplete = false;
5669             mPendingStreams.erase(it);
5670             ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
5671             ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
5672         } else if (mCancelNow) {
5673             mCurrentStream->cancelPrepare();
5674             ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5675             ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
5676             mCurrentStream.clear();
5677             mCancelNow = false;
5678             return true;
5679         }
5680     }
5681 
5682     res = mCurrentStream->prepareNextBuffer();
5683     if (res == NOT_ENOUGH_DATA) return true;
5684     if (res != OK) {
5685         // Something bad happened; try to recover by cancelling prepare and
5686         // signalling listener anyway
5687         ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
5688                 mCurrentStream->getId(), res, strerror(-res));
5689         mCurrentStream->cancelPrepare();
5690     }
5691 
5692     // This stream has finished, notify listener
5693     Mutex::Autolock l(mLock);
5694     sp<NotificationListener> listener = mListener.promote();
5695     if (listener != NULL) {
5696         ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
5697                 mCurrentStream->getId());
5698         listener->notifyPrepared(mCurrentStream->getId());
5699     }
5700 
5701     ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5702     mCurrentStream.clear();
5703     mCurrentPrepareComplete = true;
5704 
5705     return true;
5706 }
5707 
5708 /**
5709  * Static callback forwarding methods from HAL to instance
5710  */
5711 
sProcessCaptureResult(const camera3_callback_ops * cb,const camera3_capture_result * result)5712 void Camera3Device::sProcessCaptureResult(const camera3_callback_ops *cb,
5713         const camera3_capture_result *result) {
5714     Camera3Device *d =
5715             const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
5716 
5717     d->processCaptureResult(result);
5718 }
5719 
sNotify(const camera3_callback_ops * cb,const camera3_notify_msg * msg)5720 void Camera3Device::sNotify(const camera3_callback_ops *cb,
5721         const camera3_notify_msg *msg) {
5722     Camera3Device *d =
5723             const_cast<Camera3Device*>(static_cast<const Camera3Device*>(cb));
5724     d->notify(msg);
5725 }
5726 
5727 }; // namespace android
5728