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