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