blob: 00983f27fcc69097141932b3a3c18fc12aac04bd [file] [log] [blame]
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001/*
Shuzhen Wangc28189a2017-11-27 23:05:10 -08002 * Copyright (C) 2013-2018 The Android Open Source Project
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003 *
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
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070028// Convenience macro for transient errors
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080029#define CLOGE(fmt, ...) ALOGE("Camera %s: %s: " fmt, mId.string(), __FUNCTION__, \
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070030 ##__VA_ARGS__)
31
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -070032#define CLOGW(fmt, ...) ALOGW("Camera %s: %s: " fmt, mId.string(), __FUNCTION__, \
33 ##__VA_ARGS__)
34
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -070035// Convenience macros for transitioning to the error state
36#define SET_ERR(fmt, ...) setErrorState( \
37 "%s: " fmt, __FUNCTION__, \
38 ##__VA_ARGS__)
39#define SET_ERR_L(fmt, ...) setErrorStateLocked( \
40 "%s: " fmt, __FUNCTION__, \
41 ##__VA_ARGS__)
42
Colin Crosse5729fa2014-03-21 15:04:25 -070043#include <inttypes.h>
44
Shuzhen Wang5c22c152017-12-31 17:12:25 -080045#include <utility>
46
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080047#include <utils/Log.h>
48#include <utils/Trace.h>
49#include <utils/Timers.h>
Zhijun He90f7c372016-08-16 16:19:43 -070050#include <cutils/properties.h>
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070051
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080052#include <android/hardware/camera2/ICameraDeviceUser.h>
53
Igor Murashkinff3e31d2013-10-23 16:40:06 -070054#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070055#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070056#include "device3/Camera3Device.h"
57#include "device3/Camera3OutputStream.h"
58#include "device3/Camera3InputStream.h"
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -040059#include "device3/Camera3FakeStream.h"
Shuzhen Wang0129d522016-10-30 22:43:41 -070060#include "device3/Camera3SharedOutputStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070061#include "CameraService.h"
Jayant Chowdhary12361932018-08-27 14:46:13 -070062#include "utils/CameraThreadState.h"
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080063#include "utils/SessionConfigurationUtils.h"
Jayant Chowdharyd4776262020-06-23 23:45:57 -070064#include "utils/TraceHFR.h"
Shuzhen Wang316781a2020-08-18 18:11:01 -070065#include "utils/CameraServiceProxyWrapper.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080066
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080067#include <algorithm>
Yin-Chia Yeh84be5782019-03-01 11:47:02 -080068#include <tuple>
69
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080070using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080071using namespace android::hardware::camera;
72using namespace android::hardware::camera::device::V3_2;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080073using android::hardware::camera::metadata::V3_6::CameraMetadataEnumAndroidSensorPixelMode;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080074
75namespace android {
76
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080077Camera3Device::Camera3Device(const String8 &id):
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080078 mId(id),
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080079 mOperatingMode(NO_MODE),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070080 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070081 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070082 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070083 mUsePartialResult(false),
84 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080085 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070086 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070087 mNextReprocessResultFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070088 mNextZslStillResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070089 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070090 mNextReprocessShutterFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070091 mNextZslStillShutterFrameNumber(0),
Emilian Peev71c73a22017-03-21 16:35:51 +000092 mListener(NULL),
Emilian Peev811d2952018-05-25 11:08:40 +010093 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID),
Shuzhen Wang268a1362018-10-16 16:32:59 -070094 mLastTemplateId(-1),
95 mNeedFixupMonochromeTags(false)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080096{
97 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080098 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080099}
100
101Camera3Device::~Camera3Device()
102{
103 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800104 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700105 disconnectImpl();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800106}
107
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800108const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -0800109 return mId;
110}
111
Emilian Peevbd8c5032018-02-14 23:05:40 +0000112status_t Camera3Device::initialize(sp<CameraProviderManager> manager, const String8& monitorTags) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800113 ATRACE_CALL();
114 Mutex::Autolock il(mInterfaceLock);
115 Mutex::Autolock l(mLock);
116
117 ALOGV("%s: Initializing HIDL device for camera %s", __FUNCTION__, mId.string());
118 if (mStatus != STATUS_UNINITIALIZED) {
119 CLOGE("Already initialized!");
120 return INVALID_OPERATION;
121 }
122 if (manager == nullptr) return INVALID_OPERATION;
123
124 sp<ICameraDeviceSession> session;
125 ATRACE_BEGIN("CameraHal::openSession");
Steven Moreland5ff9c912017-03-09 23:13:00 -0800126 status_t res = manager->openSession(mId.string(), this,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800127 /*out*/ &session);
128 ATRACE_END();
129 if (res != OK) {
130 SET_ERR_L("Could not open camera session: %s (%d)", strerror(-res), res);
131 return res;
132 }
133
Steven Moreland5ff9c912017-03-09 23:13:00 -0800134 res = manager->getCameraCharacteristics(mId.string(), &mDeviceInfo);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800135 if (res != OK) {
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700136 SET_ERR_L("Could not retrieve camera characteristics: %s (%d)", strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800137 session->close();
138 return res;
139 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800140 mSupportNativeZoomRatio = manager->supportNativeZoomRatio(mId.string());
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800141
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700142 std::vector<std::string> physicalCameraIds;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700143 bool isLogical = manager->isLogicalCamera(mId.string(), &physicalCameraIds);
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700144 if (isLogical) {
145 for (auto& physicalId : physicalCameraIds) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700146 res = manager->getCameraCharacteristics(
147 physicalId, &mPhysicalDeviceInfoMap[physicalId]);
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700148 if (res != OK) {
149 SET_ERR_L("Could not retrieve camera %s characteristics: %s (%d)",
150 physicalId.c_str(), strerror(-res), res);
151 session->close();
152 return res;
153 }
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700154
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800155 bool usePrecorrectArray =
156 DistortionMapper::isDistortionSupported(mPhysicalDeviceInfoMap[physicalId]);
157 if (usePrecorrectArray) {
158 res = mDistortionMappers[physicalId].setupStaticInfo(
159 mPhysicalDeviceInfoMap[physicalId]);
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700160 if (res != OK) {
161 SET_ERR_L("Unable to read camera %s's calibration fields for distortion "
162 "correction", physicalId.c_str());
163 session->close();
164 return res;
165 }
166 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800167
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800168 mZoomRatioMappers[physicalId] = ZoomRatioMapper(
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800169 &mPhysicalDeviceInfoMap[physicalId],
170 mSupportNativeZoomRatio, usePrecorrectArray);
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700171 }
172 }
173
Yifan Hongf79b5542017-04-11 14:44:25 -0700174 std::shared_ptr<RequestMetadataQueue> queue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700175 auto requestQueueRet = session->getCaptureRequestMetadataQueue(
176 [&queue](const auto& descriptor) {
177 queue = std::make_shared<RequestMetadataQueue>(descriptor);
178 if (!queue->isValid() || queue->availableToWrite() <= 0) {
179 ALOGE("HAL returns empty request metadata fmq, not use it");
180 queue = nullptr;
181 // don't use the queue onwards.
182 }
183 });
184 if (!requestQueueRet.isOk()) {
185 ALOGE("Transaction error when getting request metadata fmq: %s, not use it",
186 requestQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700187 return DEAD_OBJECT;
Yifan Hongf79b5542017-04-11 14:44:25 -0700188 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700189
190 std::unique_ptr<ResultMetadataQueue>& resQueue = mResultMetadataQueue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700191 auto resultQueueRet = session->getCaptureResultMetadataQueue(
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700192 [&resQueue](const auto& descriptor) {
193 resQueue = std::make_unique<ResultMetadataQueue>(descriptor);
194 if (!resQueue->isValid() || resQueue->availableToWrite() <= 0) {
Yifan Honga640c5a2017-04-12 16:30:31 -0700195 ALOGE("HAL returns empty result metadata fmq, not use it");
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700196 resQueue = nullptr;
197 // Don't use the resQueue onwards.
Yifan Honga640c5a2017-04-12 16:30:31 -0700198 }
199 });
200 if (!resultQueueRet.isOk()) {
201 ALOGE("Transaction error when getting result metadata queue from camera session: %s",
202 resultQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700203 return DEAD_OBJECT;
Yifan Honga640c5a2017-04-12 16:30:31 -0700204 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700205 IF_ALOGV() {
206 session->interfaceChain([](
207 ::android::hardware::hidl_vec<::android::hardware::hidl_string> interfaceChain) {
208 ALOGV("Session interface chain:");
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800209 for (const auto& iface : interfaceChain) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700210 ALOGV(" %s", iface.c_str());
211 }
212 });
213 }
Yifan Hongf79b5542017-04-11 14:44:25 -0700214
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -0800215 camera_metadata_entry bufMgrMode =
216 mDeviceInfo.find(ANDROID_INFO_SUPPORTED_BUFFER_MANAGEMENT_VERSION);
217 if (bufMgrMode.count > 0) {
218 mUseHalBufManager = (bufMgrMode.data.u8[0] ==
219 ANDROID_INFO_SUPPORTED_BUFFER_MANAGEMENT_VERSION_HIDL_DEVICE_3_5);
220 }
221
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700222 camera_metadata_entry_t capabilities = mDeviceInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
223 for (size_t i = 0; i < capabilities.count; i++) {
224 uint8_t capability = capabilities.data.u8[i];
225 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_OFFLINE_PROCESSING) {
226 mSupportOfflineProcessing = true;
227 }
228 }
229
230 mInterface = new HalInterface(session, queue, mUseHalBufManager, mSupportOfflineProcessing);
Emilian Peev71c73a22017-03-21 16:35:51 +0000231 std::string providerType;
232 mVendorTagId = manager->getProviderTagIdLocked(mId.string());
Emilian Peevbd8c5032018-02-14 23:05:40 +0000233 mTagMonitor.initialize(mVendorTagId);
234 if (!monitorTags.isEmpty()) {
235 mTagMonitor.parseTagsToMonitor(String8(monitorTags));
236 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800237
Shuzhen Wang268a1362018-10-16 16:32:59 -0700238 // Metadata tags needs fixup for monochrome camera device version less
239 // than 3.5.
240 hardware::hidl_version maxVersion{0,0};
241 res = manager->getHighestSupportedVersion(mId.string(), &maxVersion);
242 if (res != OK) {
243 ALOGE("%s: Error in getting camera device version id: %s (%d)",
244 __FUNCTION__, strerror(-res), res);
245 return res;
246 }
247 int deviceVersion = HARDWARE_DEVICE_API_VERSION(
248 maxVersion.get_major(), maxVersion.get_minor());
249
250 bool isMonochrome = false;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700251 for (size_t i = 0; i < capabilities.count; i++) {
252 uint8_t capability = capabilities.data.u8[i];
Shuzhen Wang268a1362018-10-16 16:32:59 -0700253 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_MONOCHROME) {
254 isMonochrome = true;
255 }
256 }
257 mNeedFixupMonochromeTags = (isMonochrome && deviceVersion < CAMERA_DEVICE_API_VERSION_3_5);
258
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800259 return initializeCommonLocked();
260}
261
262status_t Camera3Device::initializeCommonLocked() {
263
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700264 /** Start up status tracker thread */
265 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800266 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700267 if (res != OK) {
268 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
269 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800270 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700271 mStatusTracker.clear();
272 return res;
273 }
274
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700275 /** Register in-flight map to the status tracker */
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -0700276 mInFlightStatusId = mStatusTracker->addComponent("InflightRequests");
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700277
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -0700278 if (mUseHalBufManager) {
279 res = mRequestBufferSM.initialize(mStatusTracker);
280 if (res != OK) {
281 SET_ERR_L("Unable to start request buffer state machine: %s (%d)",
282 strerror(-res), res);
283 mInterface->close();
284 mStatusTracker.clear();
285 return res;
286 }
287 }
288
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -0800289 /** Create buffer manager */
290 mBufferManager = new Camera3BufferManager();
291
292 Vector<int32_t> sessionParamKeys;
293 camera_metadata_entry_t sessionKeysEntry = mDeviceInfo.find(
294 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
295 if (sessionKeysEntry.count > 0) {
296 sessionParamKeys.insertArrayAt(sessionKeysEntry.data.i32, 0, sessionKeysEntry.count);
297 }
298
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700299 /** Start up request queue thread */
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -0700300 mRequestThread = new RequestThread(
301 this, mStatusTracker, mInterface, sessionParamKeys, mUseHalBufManager);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800302 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800303 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700304 SET_ERR_L("Unable to start request queue thread: %s (%d)",
305 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800306 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800307 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800308 return res;
309 }
310
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700311 mPreparerThread = new PreparerThread();
312
Ruben Brunk183f0562015-08-12 12:55:02 -0700313 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800314 mNextStreamId = 0;
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -0400315 mFakeStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700316 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700317 mPauseStateNotify = false;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800318 mIsInputStreamMultiResolution = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800319
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800320 // Measure the clock domain offset between camera and video/hw_composer
321 camera_metadata_entry timestampSource =
322 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
323 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
324 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
325 mTimestampOffset = getMonoToBoottimeOffset();
326 }
327
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700328 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100329 camera_metadata_entry partialResultsCount =
330 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
331 if (partialResultsCount.count > 0) {
332 mNumPartialResults = partialResultsCount.data.i32[0];
333 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700334 }
335
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800336 bool usePrecorrectArray = DistortionMapper::isDistortionSupported(mDeviceInfo);
337 if (usePrecorrectArray) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700338 res = mDistortionMappers[mId.c_str()].setupStaticInfo(mDeviceInfo);
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -0700339 if (res != OK) {
340 SET_ERR_L("Unable to read necessary calibration fields for distortion correction");
341 return res;
342 }
343 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800344
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800345 mZoomRatioMappers[mId.c_str()] = ZoomRatioMapper(&mDeviceInfo,
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800346 mSupportNativeZoomRatio, usePrecorrectArray);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800347
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800348 if (RotateAndCropMapper::isNeeded(&mDeviceInfo)) {
349 mRotateAndCropMappers.emplace(mId.c_str(), &mDeviceInfo);
350 }
351
Eino-Ville Talvala305cec62020-11-12 14:18:17 -0800352 camera_metadata_entry_t availableTestPatternModes = mDeviceInfo.find(
353 ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES);
354 for (size_t i = 0; i < availableTestPatternModes.count; i++) {
355 if (availableTestPatternModes.data.i32[i] == ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR) {
356 mSupportCameraMute = true;
357 break;
358 }
359 }
360
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800361 return OK;
362}
363
364status_t Camera3Device::disconnect() {
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700365 return disconnectImpl();
366}
367
368status_t Camera3Device::disconnectImpl() {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800369 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700370 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800371
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700372 status_t res = OK;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700373 std::vector<wp<Camera3StreamInterface>> streams;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700374 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800375 Mutex::Autolock il(mInterfaceLock);
376 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
377 {
378 Mutex::Autolock l(mLock);
379 if (mStatus == STATUS_UNINITIALIZED) return res;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700380
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800381 if (mStatus == STATUS_ACTIVE ||
382 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
383 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700384 if (res != OK) {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800385 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700386 // Continue to close device even in case of error
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800387 } else {
388 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
389 if (res != OK) {
390 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
391 maxExpectedDuration);
392 // Continue to close device even in case of error
393 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700394 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700395 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800396
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800397 if (mStatus == STATUS_ERROR) {
398 CLOGE("Shutting down in an error state");
399 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700400
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800401 if (mStatusTracker != NULL) {
402 mStatusTracker->requestExit();
403 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700404
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800405 if (mRequestThread != NULL) {
406 mRequestThread->requestExit();
407 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700408
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800409 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
410 for (size_t i = 0; i < mOutputStreams.size(); i++) {
411 streams.push_back(mOutputStreams[i]);
412 }
413 if (mInputStream != nullptr) {
414 streams.push_back(mInputStream);
415 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700416 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700417 }
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800418 // Joining done without holding mLock and mInterfaceLock, otherwise deadlocks may ensue
419 // as the threads try to access parent state (b/143513518)
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700420 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
421 // HAL may be in a bad state, so waiting for request thread
422 // (which may be stuck in the HAL processCaptureRequest call)
423 // could be dangerous.
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800424 // give up mInterfaceLock here and then lock it again. Could this lead
425 // to other deadlocks
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700426 mRequestThread->join();
427 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700428 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800429 Mutex::Autolock il(mInterfaceLock);
430 if (mStatusTracker != NULL) {
431 mStatusTracker->join();
432 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800433
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800434 HalInterface* interface;
435 {
436 Mutex::Autolock l(mLock);
437 mRequestThread.clear();
438 Mutex::Autolock stLock(mTrackerLock);
439 mStatusTracker.clear();
440 interface = mInterface.get();
441 }
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700442
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800443 // Call close without internal mutex held, as the HAL close may need to
444 // wait on assorted callbacks,etc, to complete before it can return.
445 interface->close();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700446
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800447 flushInflightRequests();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800448
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800449 {
450 Mutex::Autolock l(mLock);
451 mInterface->clear();
452 mOutputStreams.clear();
453 mInputStream.clear();
454 mDeletedStreams.clear();
455 mBufferManager.clear();
456 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
457 }
458
459 for (auto& weakStream : streams) {
460 sp<Camera3StreamInterface> stream = weakStream.promote();
461 if (stream != nullptr) {
462 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
463 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
464 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700465 }
466 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700467 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700468 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800469}
470
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700471// For dumping/debugging only -
472// try to acquire a lock a few times, eventually give up to proceed with
473// debug/dump operations
474bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
475 bool gotLock = false;
476 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
477 if (lock.tryLock() == NO_ERROR) {
478 gotLock = true;
479 break;
480 } else {
481 usleep(kDumpSleepDuration);
482 }
483 }
484 return gotLock;
485}
486
Shuzhen Wang83bff122020-11-20 15:51:39 -0800487camera3::Size Camera3Device::getMaxJpegResolution() const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700488 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
Emilian Peev08dd2452017-04-06 16:55:14 +0100489 const int STREAM_CONFIGURATION_SIZE = 4;
490 const int STREAM_FORMAT_OFFSET = 0;
491 const int STREAM_WIDTH_OFFSET = 1;
492 const int STREAM_HEIGHT_OFFSET = 2;
493 const int STREAM_IS_INPUT_OFFSET = 3;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800494 bool isHighResolutionSensor =
495 camera3::SessionConfigurationUtils::isUltraHighResolutionSensor(mDeviceInfo);
496 int32_t scalerSizesTag = isHighResolutionSensor ?
497 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_MAXIMUM_RESOLUTION :
498 ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS;
Emilian Peev08dd2452017-04-06 16:55:14 +0100499 camera_metadata_ro_entry_t availableStreamConfigs =
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800500 mDeviceInfo.find(scalerSizesTag);
Emilian Peev08dd2452017-04-06 16:55:14 +0100501 if (availableStreamConfigs.count == 0 ||
502 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
Shuzhen Wang83bff122020-11-20 15:51:39 -0800503 return camera3::Size(0, 0);
Emilian Peev08dd2452017-04-06 16:55:14 +0100504 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700505
Emilian Peev08dd2452017-04-06 16:55:14 +0100506 // Get max jpeg size (area-wise).
507 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
508 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
509 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
510 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
511 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
512 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
513 && format == HAL_PIXEL_FORMAT_BLOB &&
514 (width * height > maxJpegWidth * maxJpegHeight)) {
515 maxJpegWidth = width;
516 maxJpegHeight = height;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700517 }
518 }
Emilian Peev08dd2452017-04-06 16:55:14 +0100519
Shuzhen Wang83bff122020-11-20 15:51:39 -0800520 return camera3::Size(maxJpegWidth, maxJpegHeight);
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700521}
522
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800523nsecs_t Camera3Device::getMonoToBoottimeOffset() {
524 // try three times to get the clock offset, choose the one
525 // with the minimum gap in measurements.
526 const int tries = 3;
527 nsecs_t bestGap, measured;
528 for (int i = 0; i < tries; ++i) {
529 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
530 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
531 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
532 const nsecs_t gap = tmono2 - tmono;
533 if (i == 0 || gap < bestGap) {
534 bestGap = gap;
535 measured = tbase - ((tmono + tmono2) >> 1);
536 }
537 }
538 return measured;
539}
540
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800541hardware::graphics::common::V1_0::PixelFormat Camera3Device::mapToPixelFormat(
542 int frameworkFormat) {
543 return (hardware::graphics::common::V1_0::PixelFormat) frameworkFormat;
544}
545
546DataspaceFlags Camera3Device::mapToHidlDataspace(
547 android_dataspace dataSpace) {
548 return dataSpace;
549}
550
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700551BufferUsageFlags Camera3Device::mapToConsumerUsage(
Emilian Peev050f5dc2017-05-18 14:43:56 +0100552 uint64_t usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700553 return usage;
554}
555
Emilian Peevf4816702020-04-03 15:44:51 -0700556StreamRotation Camera3Device::mapToStreamRotation(camera_stream_rotation_t rotation) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800557 switch (rotation) {
Emilian Peevf4816702020-04-03 15:44:51 -0700558 case CAMERA_STREAM_ROTATION_0:
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800559 return StreamRotation::ROTATION_0;
Emilian Peevf4816702020-04-03 15:44:51 -0700560 case CAMERA_STREAM_ROTATION_90:
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800561 return StreamRotation::ROTATION_90;
Emilian Peevf4816702020-04-03 15:44:51 -0700562 case CAMERA_STREAM_ROTATION_180:
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800563 return StreamRotation::ROTATION_180;
Emilian Peevf4816702020-04-03 15:44:51 -0700564 case CAMERA_STREAM_ROTATION_270:
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800565 return StreamRotation::ROTATION_270;
566 }
567 ALOGE("%s: Unknown stream rotation %d", __FUNCTION__, rotation);
568 return StreamRotation::ROTATION_0;
569}
570
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800571status_t Camera3Device::mapToStreamConfigurationMode(
Emilian Peevf4816702020-04-03 15:44:51 -0700572 camera_stream_configuration_mode_t operationMode, StreamConfigurationMode *mode) {
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800573 if (mode == nullptr) return BAD_VALUE;
Emilian Peevf4816702020-04-03 15:44:51 -0700574 if (operationMode < CAMERA_VENDOR_STREAM_CONFIGURATION_MODE_START) {
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800575 switch(operationMode) {
Emilian Peevf4816702020-04-03 15:44:51 -0700576 case CAMERA_STREAM_CONFIGURATION_NORMAL_MODE:
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800577 *mode = StreamConfigurationMode::NORMAL_MODE;
578 break;
Emilian Peevf4816702020-04-03 15:44:51 -0700579 case CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE:
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800580 *mode = StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE;
581 break;
582 default:
583 ALOGE("%s: Unknown stream configuration mode %d", __FUNCTION__, operationMode);
584 return BAD_VALUE;
585 }
586 } else {
587 *mode = static_cast<StreamConfigurationMode>(operationMode);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800588 }
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800589 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800590}
591
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800592int Camera3Device::mapToFrameworkFormat(
593 hardware::graphics::common::V1_0::PixelFormat pixelFormat) {
594 return static_cast<uint32_t>(pixelFormat);
595}
596
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700597android_dataspace Camera3Device::mapToFrameworkDataspace(
598 DataspaceFlags dataSpace) {
599 return static_cast<android_dataspace>(dataSpace);
600}
601
Emilian Peev050f5dc2017-05-18 14:43:56 +0100602uint64_t Camera3Device::mapConsumerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700603 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700604 return usage;
605}
606
Emilian Peev050f5dc2017-05-18 14:43:56 +0100607uint64_t Camera3Device::mapProducerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700608 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700609 return usage;
610}
611
Zhijun Hef7da0962014-04-24 13:27:56 -0700612ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700613 // Get max jpeg size (area-wise).
Shuzhen Wang83bff122020-11-20 15:51:39 -0800614 camera3::Size maxJpegResolution = getMaxJpegResolution();
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700615 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800616 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
617 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700618 return BAD_VALUE;
619 }
620
Zhijun Hef7da0962014-04-24 13:27:56 -0700621 // Get max jpeg buffer size
622 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700623 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
624 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800625 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
626 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700627 return BAD_VALUE;
628 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700629 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800630 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700631
632 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700633 float scaleFactor = ((float) (width * height)) /
634 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800635 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
636 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700637 if (jpegBufferSize > maxJpegBufferSize) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800638 ALOGI("%s: jpeg buffer size calculated is > maxJpeg bufferSize(%zd), clamping",
639 __FUNCTION__, maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700640 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700641 }
642
643 return jpegBufferSize;
644}
645
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700646ssize_t Camera3Device::getPointCloudBufferSize() const {
647 const int FLOATS_PER_POINT=4;
648 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
649 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800650 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
651 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700652 return BAD_VALUE;
653 }
654 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
655 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
656 return maxBytesForPointCloud;
657}
658
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800659ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height,
660 bool maxResolution) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800661 const int PER_CONFIGURATION_SIZE = 3;
662 const int WIDTH_OFFSET = 0;
663 const int HEIGHT_OFFSET = 1;
664 const int SIZE_OFFSET = 2;
665 camera_metadata_ro_entry rawOpaqueSizes =
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800666 mDeviceInfo.find(
667 camera3::SessionConfigurationUtils::getAppropriateModeTag(
668 ANDROID_SENSOR_OPAQUE_RAW_SIZE,
669 maxResolution));
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800670 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800671 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800672 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
673 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800674 return BAD_VALUE;
675 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700676
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800677 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
678 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
679 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
680 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
681 }
682 }
683
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800684 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
685 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800686 return BAD_VALUE;
687}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700688
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800689status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
690 ATRACE_CALL();
691 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700692
693 // Try to lock, but continue in case of failure (to avoid blocking in
694 // deadlocks)
695 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
696 bool gotLock = tryLockSpinRightRound(mLock);
697
698 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800699 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
700 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700701 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800702 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
703 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700704
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800705 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700706
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800707 String16 templatesOption("-t");
708 int n = args.size();
709 for (int i = 0; i < n; i++) {
710 if (args[i] == templatesOption) {
711 dumpTemplates = true;
712 }
Emilian Peevbd8c5032018-02-14 23:05:40 +0000713 if (args[i] == TagMonitor::kMonitorOption) {
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700714 if (i + 1 < n) {
715 String8 monitorTags = String8(args[i + 1]);
716 if (monitorTags == "off") {
717 mTagMonitor.disableMonitoring();
718 } else {
719 mTagMonitor.parseTagsToMonitor(monitorTags);
720 }
721 } else {
722 mTagMonitor.disableMonitoring();
723 }
724 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800725 }
726
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800727 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800728
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800729 const char *status =
730 mStatus == STATUS_ERROR ? "ERROR" :
731 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700732 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
733 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800734 mStatus == STATUS_ACTIVE ? "ACTIVE" :
735 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700736
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800737 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700738 if (mStatus == STATUS_ERROR) {
739 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
740 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800741 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800742 const char *mode =
743 mOperatingMode == static_cast<int>(StreamConfigurationMode::NORMAL_MODE) ? "NORMAL" :
744 mOperatingMode == static_cast<int>(
745 StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ? "CONSTRAINED_HIGH_SPEED" :
746 "CUSTOM";
747 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800748
749 if (mInputStream != NULL) {
750 write(fd, lines.string(), lines.size());
751 mInputStream->dump(fd, args);
752 } else {
753 lines.appendFormat(" No input stream.\n");
754 write(fd, lines.string(), lines.size());
755 }
756 for (size_t i = 0; i < mOutputStreams.size(); i++) {
757 mOutputStreams[i]->dump(fd,args);
758 }
759
Zhijun He431503c2016-03-07 17:30:16 -0800760 if (mBufferManager != NULL) {
761 lines = String8(" Camera3 Buffer Manager:\n");
762 write(fd, lines.string(), lines.size());
763 mBufferManager->dump(fd, args);
764 }
Zhijun He125684a2015-12-26 15:07:30 -0800765
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700766 lines = String8(" In-flight requests:\n");
767 if (mInFlightMap.size() == 0) {
768 lines.append(" None\n");
769 } else {
770 for (size_t i = 0; i < mInFlightMap.size(); i++) {
771 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700772 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Greg Kaiser51b882c2020-06-10 05:41:44 +0000773 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800774 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Greg Kaiser51b882c2020-06-10 05:41:44 +0000775 r.numBuffersLeft);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700776 }
777 }
778 write(fd, lines.string(), lines.size());
779
Shuzhen Wang686f6442017-06-20 16:16:04 -0700780 if (mRequestThread != NULL) {
781 mRequestThread->dumpCaptureRequestLatency(fd,
782 " ProcessCaptureRequest latency histogram:");
783 }
784
Igor Murashkin1e479c02013-09-06 16:55:14 -0700785 {
786 lines = String8(" Last request sent:\n");
787 write(fd, lines.string(), lines.size());
788
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700789 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700790 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
791 }
792
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800793 if (dumpTemplates) {
Emilian Peevf4816702020-04-03 15:44:51 -0700794 const char *templateNames[CAMERA_TEMPLATE_COUNT] = {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800795 "TEMPLATE_PREVIEW",
796 "TEMPLATE_STILL_CAPTURE",
797 "TEMPLATE_VIDEO_RECORD",
798 "TEMPLATE_VIDEO_SNAPSHOT",
799 "TEMPLATE_ZERO_SHUTTER_LAG",
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800800 "TEMPLATE_MANUAL",
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800801 };
802
Emilian Peevf4816702020-04-03 15:44:51 -0700803 for (int i = 1; i < CAMERA_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800804 camera_metadata_t *templateRequest = nullptr;
805 mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -0700806 (camera_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800807 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800808 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800809 lines.append(" Not supported\n");
810 write(fd, lines.string(), lines.size());
811 } else {
812 write(fd, lines.string(), lines.size());
813 dump_indented_camera_metadata(templateRequest,
814 fd, /*verbosity*/2, /*indentation*/8);
815 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800816 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800817 }
818 }
819
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700820 mTagMonitor.dumpMonitoredMetadata(fd);
821
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800822 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800823 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800824 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800825 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800826 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800827
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700828 if (gotLock) mLock.unlock();
829 if (gotInterfaceLock) mInterfaceLock.unlock();
830
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800831 return OK;
832}
833
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800834const CameraMetadata& Camera3Device::infoPhysical(const String8& physicalId) const {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800835 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800836 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
837 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700838 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800839 mStatus == STATUS_ERROR ?
840 "when in error state" : "before init");
841 }
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700842 if (physicalId.isEmpty()) {
843 return mDeviceInfo;
844 } else {
845 std::string id(physicalId.c_str());
846 if (mPhysicalDeviceInfoMap.find(id) != mPhysicalDeviceInfoMap.end()) {
847 return mPhysicalDeviceInfoMap.at(id);
848 } else {
849 ALOGE("%s: Invalid physical camera id %s", __FUNCTION__, physicalId.c_str());
850 return mDeviceInfo;
851 }
852 }
853}
854
855const CameraMetadata& Camera3Device::info() const {
856 String8 emptyId;
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800857 return infoPhysical(emptyId);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800858}
859
Jianing Wei90e59c92014-03-12 18:29:36 -0700860status_t Camera3Device::checkStatusOkToCaptureLocked() {
861 switch (mStatus) {
862 case STATUS_ERROR:
863 CLOGE("Device has encountered a serious error");
864 return INVALID_OPERATION;
865 case STATUS_UNINITIALIZED:
866 CLOGE("Device not initialized");
867 return INVALID_OPERATION;
868 case STATUS_UNCONFIGURED:
869 case STATUS_CONFIGURED:
870 case STATUS_ACTIVE:
871 // OK
872 break;
873 default:
874 SET_ERR_L("Unexpected status: %d", mStatus);
875 return INVALID_OPERATION;
876 }
877 return OK;
878}
879
880status_t Camera3Device::convertMetadataListToRequestListLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000881 const List<const PhysicalCameraSettingsList> &metadataList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700882 const std::list<const SurfaceMap> &surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700883 bool repeating, nsecs_t requestTimeNs,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700884 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700885 if (requestList == NULL) {
886 CLOGE("requestList cannot be NULL.");
887 return BAD_VALUE;
888 }
889
Jianing Weicb0652e2014-03-12 18:29:36 -0700890 int32_t burstId = 0;
Emilian Peevaebbe412018-01-15 13:53:24 +0000891 List<const PhysicalCameraSettingsList>::const_iterator metadataIt = metadataList.begin();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700892 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
893 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
894 ++metadataIt, ++surfaceMapIt) {
895 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700896 if (newRequest == 0) {
897 CLOGE("Can't create capture request");
898 return BAD_VALUE;
899 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700900
Shuzhen Wang9d066012016-09-30 11:30:20 -0700901 newRequest->mRepeating = repeating;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700902 newRequest->mRequestTimeNs = requestTimeNs;
Shuzhen Wang9d066012016-09-30 11:30:20 -0700903
Jianing Weicb0652e2014-03-12 18:29:36 -0700904 // Setup burst Id and request Id
905 newRequest->mResultExtras.burstId = burstId++;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800906 auto requestIdEntry = metadataIt->begin()->metadata.find(ANDROID_REQUEST_ID);
907 if (requestIdEntry.count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700908 CLOGE("RequestID does not exist in metadata");
909 return BAD_VALUE;
910 }
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800911 newRequest->mResultExtras.requestId = requestIdEntry.data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700912
Jianing Wei90e59c92014-03-12 18:29:36 -0700913 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700914
915 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700916 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700917 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
918 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
919 return BAD_VALUE;
920 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700921
922 // Setup batch size if this is a high speed video recording request.
923 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
924 auto firstRequest = requestList->begin();
925 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
926 if (outputStream->isVideoStream()) {
927 (*firstRequest)->mBatchSize = requestList->size();
Yin-Chia Yeh14ef48d2020-02-10 15:06:37 -0800928 outputStream->setBatchSize(requestList->size());
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700929 break;
930 }
931 }
932 }
933
Jianing Wei90e59c92014-03-12 18:29:36 -0700934 return OK;
935}
936
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800937status_t Camera3Device::capture(CameraMetadata &request, int64_t* lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800938 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800939
Emilian Peevaebbe412018-01-15 13:53:24 +0000940 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700941 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000942 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700943
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800944 return captureList(requestsList, surfaceMaps, lastFrameNumber);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700945}
946
Emilian Peevaebbe412018-01-15 13:53:24 +0000947void Camera3Device::convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700948 std::list<const SurfaceMap>& surfaceMaps,
949 const CameraMetadata& request) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000950 PhysicalCameraSettingsList requestList;
951 requestList.push_back({std::string(getId().string()), request});
952 requestsList.push_back(requestList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700953
954 SurfaceMap surfaceMap;
955 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
956 // With no surface list passed in, stream and surface will have 1-to-1
957 // mapping. So the surface index is 0 for each stream in the surfaceMap.
958 for (size_t i = 0; i < streams.count; i++) {
959 surfaceMap[streams.data.i32[i]].push_back(0);
960 }
961 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800962}
963
Jianing Wei90e59c92014-03-12 18:29:36 -0700964status_t Camera3Device::submitRequestsHelper(
Emilian Peevaebbe412018-01-15 13:53:24 +0000965 const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700966 const std::list<const SurfaceMap> &surfaceMaps,
967 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700968 /*out*/
969 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700970 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -0700971 nsecs_t requestTimeNs = systemTime();
972
Jianing Wei90e59c92014-03-12 18:29:36 -0700973 Mutex::Autolock il(mInterfaceLock);
974 Mutex::Autolock l(mLock);
975
976 status_t res = checkStatusOkToCaptureLocked();
977 if (res != OK) {
978 // error logged by previous call
979 return res;
980 }
981
982 RequestList requestList;
983
Shuzhen Wang0129d522016-10-30 22:43:41 -0700984 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700985 repeating, requestTimeNs, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700986 if (res != OK) {
987 // error logged by previous call
988 return res;
989 }
990
991 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700992 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700993 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700994 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700995 }
996
997 if (res == OK) {
998 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
999 if (res != OK) {
1000 SET_ERR_L("Can't transition to active in %f seconds!",
1001 kActiveTimeout/1e9);
1002 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001003 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001004 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -07001005 } else {
1006 CLOGE("Cannot queue request. Impossible.");
1007 return BAD_VALUE;
1008 }
1009
1010 return res;
1011}
1012
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001013hardware::Return<void> Camera3Device::requestStreamBuffers(
1014 const hardware::hidl_vec<hardware::camera::device::V3_5::BufferRequest>& bufReqs,
1015 requestStreamBuffers_cb _hidl_cb) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001016 RequestBufferStates states {
Shuzhen Wang316781a2020-08-18 18:11:01 -07001017 mId, mRequestBufferInterfaceLock, mUseHalBufManager, mOutputStreams, mSessionStatsBuilder,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001018 *this, *mInterface, *this};
1019 camera3::requestStreamBuffers(states, bufReqs, _hidl_cb);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001020 return hardware::Void();
1021}
1022
1023hardware::Return<void> Camera3Device::returnStreamBuffers(
1024 const hardware::hidl_vec<hardware::camera::device::V3_2::StreamBuffer>& buffers) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001025 ReturnBufferStates states {
Shuzhen Wang316781a2020-08-18 18:11:01 -07001026 mId, mUseHalBufManager, mOutputStreams, mSessionStatsBuilder, *mInterface};
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001027 camera3::returnStreamBuffers(states, buffers);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001028 return hardware::Void();
1029}
1030
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001031hardware::Return<void> Camera3Device::processCaptureResult_3_4(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001032 const hardware::hidl_vec<
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001033 hardware::camera::device::V3_4::CaptureResult>& results) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001034 // Ideally we should grab mLock, but that can lead to deadlock, and
1035 // it's not super important to get up to date value of mStatus for this
1036 // warning print, hence skipping the lock here
1037 if (mStatus == STATUS_ERROR) {
1038 // Per API contract, HAL should act as closed after device error
1039 // But mStatus can be set to error by framework as well, so just log
1040 // a warning here.
1041 ALOGW("%s: received capture result in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07001042 }
Yifan Honga640c5a2017-04-12 16:30:31 -07001043
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001044 sp<NotificationListener> listener;
1045 {
1046 std::lock_guard<std::mutex> l(mOutputLock);
1047 listener = mListener.promote();
1048 }
1049
Yifan Honga640c5a2017-04-12 16:30:31 -07001050 if (mProcessCaptureResultLock.tryLock() != OK) {
1051 // This should never happen; it indicates a wrong client implementation
1052 // that doesn't follow the contract. But, we can be tolerant here.
1053 ALOGE("%s: callback overlapped! waiting 1s...",
1054 __FUNCTION__);
1055 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
1056 ALOGE("%s: cannot acquire lock in 1s, dropping results",
1057 __FUNCTION__);
1058 // really don't know what to do, so bail out.
1059 return hardware::Void();
1060 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001061 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001062 CaptureOutputStates states {
1063 mId,
Shuzhen Wangb7b42652020-05-07 11:59:02 -07001064 mInFlightLock, mLastCompletedRegularFrameNumber,
1065 mLastCompletedReprocessFrameNumber, mLastCompletedZslFrameNumber,
1066 mInFlightMap, mOutputLock, mResultQueue, mResultSignal,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001067 mNextShutterFrameNumber,
1068 mNextReprocessShutterFrameNumber, mNextZslStillShutterFrameNumber,
1069 mNextResultFrameNumber,
1070 mNextReprocessResultFrameNumber, mNextZslStillResultFrameNumber,
1071 mUseHalBufManager, mUsePartialResult, mNeedFixupMonochromeTags,
1072 mNumPartialResults, mVendorTagId, mDeviceInfo, mPhysicalDeviceInfoMap,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001073 mResultMetadataQueue, mDistortionMappers, mZoomRatioMappers, mRotateAndCropMappers,
Shuzhen Wang316781a2020-08-18 18:11:01 -07001074 mTagMonitor, mInputStream, mOutputStreams, mSessionStatsBuilder, listener, *this, *this,
1075 *mInterface
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001076 };
1077
Yifan Honga640c5a2017-04-12 16:30:31 -07001078 for (const auto& result : results) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001079 processOneCaptureResultLocked(states, result.v3_2, result.physicalCameraMetadata);
Yifan Honga640c5a2017-04-12 16:30:31 -07001080 }
1081 mProcessCaptureResultLock.unlock();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001082 return hardware::Void();
1083}
1084
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001085// Only one processCaptureResult should be called at a time, so
1086// the locks won't block. The locks are present here simply to enforce this.
1087hardware::Return<void> Camera3Device::processCaptureResult(
1088 const hardware::hidl_vec<
1089 hardware::camera::device::V3_2::CaptureResult>& results) {
1090 hardware::hidl_vec<hardware::camera::device::V3_4::PhysicalCameraMetadata> noPhysMetadata;
1091
1092 // Ideally we should grab mLock, but that can lead to deadlock, and
1093 // it's not super important to get up to date value of mStatus for this
1094 // warning print, hence skipping the lock here
1095 if (mStatus == STATUS_ERROR) {
1096 // Per API contract, HAL should act as closed after device error
1097 // But mStatus can be set to error by framework as well, so just log
1098 // a warning here.
1099 ALOGW("%s: received capture result in error state.", __FUNCTION__);
1100 }
1101
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001102 sp<NotificationListener> listener;
1103 {
1104 std::lock_guard<std::mutex> l(mOutputLock);
1105 listener = mListener.promote();
1106 }
1107
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001108 if (mProcessCaptureResultLock.tryLock() != OK) {
1109 // This should never happen; it indicates a wrong client implementation
1110 // that doesn't follow the contract. But, we can be tolerant here.
1111 ALOGE("%s: callback overlapped! waiting 1s...",
1112 __FUNCTION__);
1113 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
1114 ALOGE("%s: cannot acquire lock in 1s, dropping results",
1115 __FUNCTION__);
1116 // really don't know what to do, so bail out.
1117 return hardware::Void();
1118 }
1119 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001120
1121 CaptureOutputStates states {
1122 mId,
Shuzhen Wangb7b42652020-05-07 11:59:02 -07001123 mInFlightLock, mLastCompletedRegularFrameNumber,
1124 mLastCompletedReprocessFrameNumber, mLastCompletedZslFrameNumber,
1125 mInFlightMap, mOutputLock, mResultQueue, mResultSignal,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001126 mNextShutterFrameNumber,
1127 mNextReprocessShutterFrameNumber, mNextZslStillShutterFrameNumber,
1128 mNextResultFrameNumber,
1129 mNextReprocessResultFrameNumber, mNextZslStillResultFrameNumber,
1130 mUseHalBufManager, mUsePartialResult, mNeedFixupMonochromeTags,
1131 mNumPartialResults, mVendorTagId, mDeviceInfo, mPhysicalDeviceInfoMap,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001132 mResultMetadataQueue, mDistortionMappers, mZoomRatioMappers, mRotateAndCropMappers,
Shuzhen Wang316781a2020-08-18 18:11:01 -07001133 mTagMonitor, mInputStream, mOutputStreams, mSessionStatsBuilder, listener, *this, *this,
1134 *mInterface
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001135 };
1136
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001137 for (const auto& result : results) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001138 processOneCaptureResultLocked(states, result, noPhysMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001139 }
1140 mProcessCaptureResultLock.unlock();
1141 return hardware::Void();
1142}
1143
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001144hardware::Return<void> Camera3Device::notify(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001145 const hardware::hidl_vec<hardware::camera::device::V3_2::NotifyMsg>& msgs) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001146 // Ideally we should grab mLock, but that can lead to deadlock, and
1147 // it's not super important to get up to date value of mStatus for this
1148 // warning print, hence skipping the lock here
1149 if (mStatus == STATUS_ERROR) {
1150 // Per API contract, HAL should act as closed after device error
1151 // But mStatus can be set to error by framework as well, so just log
1152 // a warning here.
1153 ALOGW("%s: received notify message in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07001154 }
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001155
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001156 sp<NotificationListener> listener;
1157 {
1158 std::lock_guard<std::mutex> l(mOutputLock);
1159 listener = mListener.promote();
1160 }
1161
1162 CaptureOutputStates states {
1163 mId,
Shuzhen Wangb7b42652020-05-07 11:59:02 -07001164 mInFlightLock, mLastCompletedRegularFrameNumber,
1165 mLastCompletedReprocessFrameNumber, mLastCompletedZslFrameNumber,
1166 mInFlightMap, mOutputLock, mResultQueue, mResultSignal,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001167 mNextShutterFrameNumber,
1168 mNextReprocessShutterFrameNumber, mNextZslStillShutterFrameNumber,
1169 mNextResultFrameNumber,
1170 mNextReprocessResultFrameNumber, mNextZslStillResultFrameNumber,
1171 mUseHalBufManager, mUsePartialResult, mNeedFixupMonochromeTags,
1172 mNumPartialResults, mVendorTagId, mDeviceInfo, mPhysicalDeviceInfoMap,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001173 mResultMetadataQueue, mDistortionMappers, mZoomRatioMappers, mRotateAndCropMappers,
Shuzhen Wang316781a2020-08-18 18:11:01 -07001174 mTagMonitor, mInputStream, mOutputStreams, mSessionStatsBuilder, listener, *this, *this,
1175 *mInterface
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001176 };
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001177 for (const auto& msg : msgs) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001178 camera3::notify(states, msg);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001179 }
1180 return hardware::Void();
1181}
1182
Emilian Peevaebbe412018-01-15 13:53:24 +00001183status_t Camera3Device::captureList(const List<const PhysicalCameraSettingsList> &requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001184 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -07001185 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001186 ATRACE_CALL();
1187
Emilian Peevaebbe412018-01-15 13:53:24 +00001188 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001189}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001190
Jianing Weicb0652e2014-03-12 18:29:36 -07001191status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
1192 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001193 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001194
Emilian Peevaebbe412018-01-15 13:53:24 +00001195 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001196 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +00001197 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001198
Emilian Peevaebbe412018-01-15 13:53:24 +00001199 return setStreamingRequestList(requestsList, /*surfaceMap*/surfaceMaps,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001200 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001201}
1202
Emilian Peevaebbe412018-01-15 13:53:24 +00001203status_t Camera3Device::setStreamingRequestList(
1204 const List<const PhysicalCameraSettingsList> &requestsList,
1205 const std::list<const SurfaceMap> &surfaceMaps, int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001206 ATRACE_CALL();
1207
Emilian Peevaebbe412018-01-15 13:53:24 +00001208 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001209}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001210
1211sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +00001212 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001213 status_t res;
1214
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001215 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001216 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
1217 // so unilaterally select normal operating mode.
Emilian Peevaebbe412018-01-15 13:53:24 +00001218 res = filterParamsAndConfigureLocked(request.begin()->metadata,
Emilian Peevf4816702020-04-03 15:44:51 -07001219 CAMERA_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001220 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001221 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001222 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001223 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001224 } else if (mStatus == STATUS_UNCONFIGURED) {
1225 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001226 CLOGE("No streams configured");
1227 return NULL;
1228 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001229 }
1230
Shuzhen Wang0129d522016-10-30 22:43:41 -07001231 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001232 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001233}
1234
Jianing Weicb0652e2014-03-12 18:29:36 -07001235status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001236 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001237 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001238 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001239
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001240 switch (mStatus) {
1241 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001242 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001243 return INVALID_OPERATION;
1244 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001245 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001246 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001247 case STATUS_UNCONFIGURED:
1248 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001249 case STATUS_ACTIVE:
1250 // OK
1251 break;
1252 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001253 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001254 return INVALID_OPERATION;
1255 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001256 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -07001257
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001258 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001259}
1260
1261status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
1262 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001263 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001264
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001265 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001266}
1267
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001268status_t Camera3Device::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -08001269 uint32_t width, uint32_t height, int format, bool isMultiResolution, int *id) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001270 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001271 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001272 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001273 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001274 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
1275 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001276
1277 status_t res;
1278 bool wasActive = false;
1279
1280 switch (mStatus) {
1281 case STATUS_ERROR:
1282 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
1283 return INVALID_OPERATION;
1284 case STATUS_UNINITIALIZED:
1285 ALOGE("%s: Device not initialized", __FUNCTION__);
1286 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001287 case STATUS_UNCONFIGURED:
1288 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001289 // OK
1290 break;
1291 case STATUS_ACTIVE:
1292 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001293 res = internalPauseAndWaitLocked(maxExpectedDuration);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001294 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001295 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001296 return res;
1297 }
1298 wasActive = true;
1299 break;
1300 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001301 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001302 return INVALID_OPERATION;
1303 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001304 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001305
1306 if (mInputStream != 0) {
1307 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
1308 return INVALID_OPERATION;
1309 }
1310
1311 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
1312 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001313 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001314
1315 mInputStream = newStream;
Shuzhen Wang83bff122020-11-20 15:51:39 -08001316 mIsInputStreamMultiResolution = isMultiResolution;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001317
1318 *id = mNextStreamId++;
1319
1320 // Continue captures if active at start
1321 if (wasActive) {
1322 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001323 // Reuse current operating mode and session parameters for new stream config
1324 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001325 if (res != OK) {
1326 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1327 __FUNCTION__, mNextStreamId, strerror(-res), res);
1328 return res;
1329 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001330 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001331 }
1332
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001333 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001334 return OK;
1335}
1336
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001337status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001338 uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -07001339 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001340 const String8& physicalCameraId,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001341 const std::unordered_set<int32_t> &sensorPixelModesUsed,
1342 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
1343 uint64_t consumerUsage) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001344 ATRACE_CALL();
1345
1346 if (consumer == nullptr) {
1347 ALOGE("%s: consumer must not be null", __FUNCTION__);
1348 return BAD_VALUE;
1349 }
1350
1351 std::vector<sp<Surface>> consumers;
1352 consumers.push_back(consumer);
1353
1354 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001355 format, dataSpace, rotation, id, physicalCameraId, sensorPixelModesUsed, surfaceIds,
1356 streamSetId, isShared, isMultiResolution, consumerUsage);
1357}
1358
1359static bool isRawFormat(int format) {
1360 switch (format) {
1361 case HAL_PIXEL_FORMAT_RAW16:
1362 case HAL_PIXEL_FORMAT_RAW12:
1363 case HAL_PIXEL_FORMAT_RAW10:
1364 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
1365 return true;
1366 default:
1367 return false;
1368 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001369}
1370
1371status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1372 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -07001373 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001374 const String8& physicalCameraId, const std::unordered_set<int32_t> &sensorPixelModesUsed,
Shuzhen Wang83bff122020-11-20 15:51:39 -08001375 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
1376 uint64_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001377 ATRACE_CALL();
Emilian Peev40ead602017-09-26 15:46:36 +01001378
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001379 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001380 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001381 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001382 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wang83bff122020-11-20 15:51:39 -08001383 " consumer usage %" PRIu64 ", isShared %d, physicalCameraId %s, isMultiResolution %d",
1384 mId.string(), mNextStreamId, width, height, format, dataSpace, rotation,
1385 consumerUsage, isShared, physicalCameraId.string(), isMultiResolution);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001386
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001387 status_t res;
1388 bool wasActive = false;
1389
1390 switch (mStatus) {
1391 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001392 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001393 return INVALID_OPERATION;
1394 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001395 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001396 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001397 case STATUS_UNCONFIGURED:
1398 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001399 // OK
1400 break;
1401 case STATUS_ACTIVE:
1402 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001403 res = internalPauseAndWaitLocked(maxExpectedDuration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001404 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001405 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001406 return res;
1407 }
1408 wasActive = true;
1409 break;
1410 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001411 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001412 return INVALID_OPERATION;
1413 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001414 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001415
1416 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001417
Shuzhen Wang0129d522016-10-30 22:43:41 -07001418 if (consumers.size() == 0 && !hasDeferredConsumer) {
1419 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1420 return BAD_VALUE;
1421 }
Zhijun He5d677d12016-05-29 16:52:39 -07001422
Shuzhen Wang0129d522016-10-30 22:43:41 -07001423 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001424 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1425 return BAD_VALUE;
1426 }
1427
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001428 if (isRawFormat(format) && sensorPixelModesUsed.size() > 1) {
1429 // We can't use one stream with a raw format in both sensor pixel modes since its going to
1430 // be found in only one sensor pixel mode.
1431 ALOGE("%s: RAW opaque stream cannot be used with > 1 sensor pixel modes", __FUNCTION__);
1432 return BAD_VALUE;
1433 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001434 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001435 ssize_t blobBufferSize;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001436 if (dataSpace == HAL_DATASPACE_DEPTH) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001437 blobBufferSize = getPointCloudBufferSize();
1438 if (blobBufferSize <= 0) {
1439 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1440 return BAD_VALUE;
1441 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001442 } else if (dataSpace == static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
1443 blobBufferSize = width * height;
1444 } else {
1445 blobBufferSize = getJpegBufferSize(width, height);
1446 if (blobBufferSize <= 0) {
1447 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1448 return BAD_VALUE;
1449 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001450 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001451 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001452 width, height, blobBufferSize, format, dataSpace, rotation,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001453 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, streamSetId,
1454 isMultiResolution);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001455 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001456 bool maxResolution =
1457 sensorPixelModesUsed.find(ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) !=
1458 sensorPixelModesUsed.end();
1459 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height, maxResolution);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001460 if (rawOpaqueBufferSize <= 0) {
1461 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1462 return BAD_VALUE;
1463 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001464 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001465 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001466 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, streamSetId,
1467 isMultiResolution);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001468 } else if (isShared) {
1469 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1470 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001471 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, streamSetId,
1472 mUseHalBufManager);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001473 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001474 newStream = new Camera3OutputStream(mNextStreamId,
1475 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001476 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, streamSetId,
1477 isMultiResolution);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001478 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001479 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001480 width, height, format, dataSpace, rotation,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001481 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, streamSetId,
1482 isMultiResolution);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001483 }
Emilian Peev40ead602017-09-26 15:46:36 +01001484
1485 size_t consumerCount = consumers.size();
1486 for (size_t i = 0; i < consumerCount; i++) {
1487 int id = newStream->getSurfaceId(consumers[i]);
1488 if (id < 0) {
1489 SET_ERR_L("Invalid surface id");
1490 return BAD_VALUE;
1491 }
1492 if (surfaceIds != nullptr) {
1493 surfaceIds->push_back(id);
1494 }
1495 }
1496
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001497 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001498
Emilian Peev08dd2452017-04-06 16:55:14 +01001499 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001500
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -08001501 newStream->setImageDumpMask(mImageDumpMask);
1502
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001503 res = mOutputStreams.add(mNextStreamId, newStream);
1504 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001505 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001506 return res;
1507 }
1508
Shuzhen Wang316781a2020-08-18 18:11:01 -07001509 mSessionStatsBuilder.addStream(mNextStreamId);
1510
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001511 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001512 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001513
1514 // Continue captures if active at start
1515 if (wasActive) {
1516 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001517 // Reuse current operating mode and session parameters for new stream config
1518 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001519 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001520 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1521 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001522 return res;
1523 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001524 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001525 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001526 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001527 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001528}
1529
Emilian Peev710c1422017-08-30 11:19:38 +01001530status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001531 ATRACE_CALL();
Emilian Peev710c1422017-08-30 11:19:38 +01001532 if (nullptr == streamInfo) {
1533 return BAD_VALUE;
1534 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001535 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001536 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001537
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001538 switch (mStatus) {
1539 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001540 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001541 return INVALID_OPERATION;
1542 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001543 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001544 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001545 case STATUS_UNCONFIGURED:
1546 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001547 case STATUS_ACTIVE:
1548 // OK
1549 break;
1550 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001551 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001552 return INVALID_OPERATION;
1553 }
1554
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001555 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
1556 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001557 CLOGE("Stream %d is unknown", id);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001558 return BAD_VALUE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001559 }
1560
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001561 streamInfo->width = stream->getWidth();
1562 streamInfo->height = stream->getHeight();
1563 streamInfo->format = stream->getFormat();
1564 streamInfo->dataSpace = stream->getDataSpace();
1565 streamInfo->formatOverridden = stream->isFormatOverridden();
1566 streamInfo->originalFormat = stream->getOriginalFormat();
1567 streamInfo->dataSpaceOverridden = stream->isDataSpaceOverridden();
1568 streamInfo->originalDataSpace = stream->getOriginalDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001569 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001570}
1571
1572status_t Camera3Device::setStreamTransform(int id,
1573 int transform) {
1574 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001575 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001576 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001577
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001578 switch (mStatus) {
1579 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001580 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001581 return INVALID_OPERATION;
1582 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001583 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001584 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001585 case STATUS_UNCONFIGURED:
1586 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001587 case STATUS_ACTIVE:
1588 // OK
1589 break;
1590 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001591 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001592 return INVALID_OPERATION;
1593 }
1594
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001595 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
1596 if (stream == nullptr) {
1597 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001598 return BAD_VALUE;
1599 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001600 return stream->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001601}
1602
1603status_t Camera3Device::deleteStream(int id) {
1604 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001605 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001606 Mutex::Autolock l(mLock);
1607 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001608
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001609 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001610
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001611 // CameraDevice semantics require device to already be idle before
1612 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001613 if (mStatus == STATUS_ACTIVE) {
Yin-Chia Yeh693047d2018-03-08 12:14:19 -08001614 ALOGW("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001615 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001616 }
1617
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001618 if (mStatus == STATUS_ERROR) {
1619 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1620 __FUNCTION__, mId.string());
1621 return -EBUSY;
1622 }
1623
Igor Murashkin2fba5842013-04-22 14:03:54 -07001624 sp<Camera3StreamInterface> deletedStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001625 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001626 if (mInputStream != NULL && id == mInputStream->getId()) {
1627 deletedStream = mInputStream;
1628 mInputStream.clear();
1629 } else {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001630 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001631 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001632 return BAD_VALUE;
1633 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001634 mSessionStatsBuilder.removeStream(id);
Zhijun He5f446352014-01-22 09:49:33 -08001635 }
1636
1637 // Delete output stream or the output part of a bi-directional stream.
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001638 if (stream != nullptr) {
1639 deletedStream = stream;
1640 mOutputStreams.remove(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001641 }
1642
1643 // Free up the stream endpoint so that it can be used by some other stream
1644 res = deletedStream->disconnect();
1645 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001646 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001647 // fall through since we want to still list the stream as deleted.
1648 }
1649 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001650 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001651
1652 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001653}
1654
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001655status_t Camera3Device::configureStreams(const CameraMetadata& sessionParams, int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001656 ATRACE_CALL();
1657 ALOGV("%s: E", __FUNCTION__);
1658
1659 Mutex::Autolock il(mInterfaceLock);
1660 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001661
Emilian Peev811d2952018-05-25 11:08:40 +01001662 // In case the client doesn't include any session parameter, try a
1663 // speculative configuration using the values from the last cached
1664 // default request.
1665 if (sessionParams.isEmpty() &&
Emilian Peevf4816702020-04-03 15:44:51 -07001666 ((mLastTemplateId > 0) && (mLastTemplateId < CAMERA_TEMPLATE_COUNT)) &&
Emilian Peev811d2952018-05-25 11:08:40 +01001667 (!mRequestTemplateCache[mLastTemplateId].isEmpty())) {
1668 ALOGV("%s: Speculative session param configuration with template id: %d", __func__,
1669 mLastTemplateId);
1670 return filterParamsAndConfigureLocked(mRequestTemplateCache[mLastTemplateId],
1671 operatingMode);
1672 }
1673
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001674 return filterParamsAndConfigureLocked(sessionParams, operatingMode);
1675}
1676
1677status_t Camera3Device::filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
1678 int operatingMode) {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001679 //Filter out any incoming session parameters
1680 const CameraMetadata params(sessionParams);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001681 camera_metadata_entry_t availableSessionKeys = mDeviceInfo.find(
1682 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001683 CameraMetadata filteredParams(availableSessionKeys.count);
1684 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
1685 filteredParams.getAndLock());
1686 set_camera_metadata_vendor_id(meta, mVendorTagId);
1687 filteredParams.unlock(meta);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001688 if (availableSessionKeys.count > 0) {
1689 for (size_t i = 0; i < availableSessionKeys.count; i++) {
1690 camera_metadata_ro_entry entry = params.find(
1691 availableSessionKeys.data.i32[i]);
1692 if (entry.count > 0) {
1693 filteredParams.update(entry);
1694 }
1695 }
1696 }
1697
1698 return configureStreamsLocked(operatingMode, filteredParams);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001699}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001700
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001701status_t Camera3Device::getInputBufferProducer(
1702 sp<IGraphicBufferProducer> *producer) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001703 ATRACE_CALL();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001704 Mutex::Autolock il(mInterfaceLock);
1705 Mutex::Autolock l(mLock);
1706
1707 if (producer == NULL) {
1708 return BAD_VALUE;
1709 } else if (mInputStream == NULL) {
1710 return INVALID_OPERATION;
1711 }
1712
1713 return mInputStream->getInputBufferProducer(producer);
1714}
1715
Emilian Peevf4816702020-04-03 15:44:51 -07001716status_t Camera3Device::createDefaultRequest(camera_request_template_t templateId,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001717 CameraMetadata *request) {
1718 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001719 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001720
Emilian Peevf4816702020-04-03 15:44:51 -07001721 if (templateId <= 0 || templateId >= CAMERA_TEMPLATE_COUNT) {
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001722 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
Jayant Chowdhary12361932018-08-27 14:46:13 -07001723 CameraThreadState::getCallingUid(), nullptr, 0);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001724 return BAD_VALUE;
1725 }
1726
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001727 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001728
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001729 {
1730 Mutex::Autolock l(mLock);
1731 switch (mStatus) {
1732 case STATUS_ERROR:
1733 CLOGE("Device has encountered a serious error");
1734 return INVALID_OPERATION;
1735 case STATUS_UNINITIALIZED:
1736 CLOGE("Device is not initialized!");
1737 return INVALID_OPERATION;
1738 case STATUS_UNCONFIGURED:
1739 case STATUS_CONFIGURED:
1740 case STATUS_ACTIVE:
1741 // OK
1742 break;
1743 default:
1744 SET_ERR_L("Unexpected status: %d", mStatus);
1745 return INVALID_OPERATION;
1746 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001747
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001748 if (!mRequestTemplateCache[templateId].isEmpty()) {
1749 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001750 mLastTemplateId = templateId;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001751 return OK;
1752 }
Zhijun Hea1530f12014-09-14 12:44:20 -07001753 }
1754
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001755 camera_metadata_t *rawRequest;
1756 status_t res = mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -07001757 (camera_request_template_t) templateId, &rawRequest);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001758
1759 {
1760 Mutex::Autolock l(mLock);
1761 if (res == BAD_VALUE) {
1762 ALOGI("%s: template %d is not supported on this camera device",
1763 __FUNCTION__, templateId);
1764 return res;
1765 } else if (res != OK) {
1766 CLOGE("Unable to construct request template %d: %s (%d)",
1767 templateId, strerror(-res), res);
1768 return res;
1769 }
1770
1771 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
1772 mRequestTemplateCache[templateId].acquire(rawRequest);
1773
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08001774 // Override the template request with zoomRatioMapper
1775 res = mZoomRatioMappers[mId.c_str()].initZoomRatioInTemplate(
1776 &mRequestTemplateCache[templateId]);
1777 if (res != OK) {
1778 CLOGE("Failed to update zoom ratio for template %d: %s (%d)",
1779 templateId, strerror(-res), res);
1780 return res;
1781 }
1782
Shuzhen Wangd25dc972020-03-24 17:11:43 -07001783 // Fill in JPEG_QUALITY if not available
1784 if (!mRequestTemplateCache[templateId].exists(ANDROID_JPEG_QUALITY)) {
1785 static const uint8_t kDefaultJpegQuality = 95;
1786 mRequestTemplateCache[templateId].update(ANDROID_JPEG_QUALITY,
1787 &kDefaultJpegQuality, 1);
1788 }
1789
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001790 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001791 mLastTemplateId = templateId;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001792 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001793 return OK;
1794}
1795
1796status_t Camera3Device::waitUntilDrained() {
1797 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001798 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001799 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001800 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001801
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001802 return waitUntilDrainedLocked(maxExpectedDuration);
Zhijun He69a37482014-03-23 18:44:49 -07001803}
1804
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001805status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001806 switch (mStatus) {
1807 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001808 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001809 ALOGV("%s: Already idle", __FUNCTION__);
1810 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001811 case STATUS_CONFIGURED:
1812 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001813 case STATUS_ERROR:
1814 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001815 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001816 break;
1817 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001818 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001819 return INVALID_OPERATION;
1820 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001821 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
1822 maxExpectedDuration);
1823 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001824 if (res != OK) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07001825 mStatusTracker->dumpActiveComponents();
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001826 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1827 res);
1828 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001829 return res;
1830}
1831
Ruben Brunk183f0562015-08-12 12:55:02 -07001832
1833void Camera3Device::internalUpdateStatusLocked(Status status) {
1834 mStatus = status;
1835 mRecentStatusUpdates.add(mStatus);
1836 mStatusChanged.broadcast();
1837}
1838
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001839// Pause to reconfigure
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001840status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
Emilian Peeve86358b2019-02-15 13:51:39 -08001841 if (mRequestThread.get() != nullptr) {
1842 mRequestThread->setPaused(true);
1843 } else {
1844 return NO_INIT;
1845 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001846
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001847 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1848 maxExpectedDuration);
1849 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001850 if (res != OK) {
1851 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001852 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001853 }
1854
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001855 return res;
1856}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001857
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001858// Resume after internalPauseAndWaitLocked
1859status_t Camera3Device::internalResumeLocked() {
1860 status_t res;
1861
1862 mRequestThread->setPaused(false);
1863
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001864 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1865 kActiveTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001866 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1867 if (res != OK) {
1868 SET_ERR_L("Can't transition to active in %f seconds!",
1869 kActiveTimeout/1e9);
1870 }
1871 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001872 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001873}
1874
Ruben Brunk183f0562015-08-12 12:55:02 -07001875status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001876 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001877
1878 size_t startIndex = 0;
1879 if (mStatusWaiters == 0) {
1880 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1881 // this status list
1882 mRecentStatusUpdates.clear();
1883 } else {
1884 // If other threads are waiting on updates to this status list, set the position of the
1885 // first element that this list will check rather than clearing the list.
1886 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001887 }
1888
Ruben Brunk183f0562015-08-12 12:55:02 -07001889 mStatusWaiters++;
1890
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001891 bool signalPipelineDrain = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001892 if (!active && mUseHalBufManager) {
1893 auto streamIds = mOutputStreams.getStreamIds();
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001894 if (mStatus == STATUS_ACTIVE) {
1895 mRequestThread->signalPipelineDrain(streamIds);
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001896 signalPipelineDrain = true;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001897 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001898 mRequestBufferSM.onWaitUntilIdle();
1899 }
1900
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001901 bool stateSeen = false;
1902 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001903 if (active == (mStatus == STATUS_ACTIVE)) {
1904 // Desired state is current
1905 break;
1906 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001907
1908 res = mStatusChanged.waitRelative(mLock, timeout);
1909 if (res != OK) break;
1910
Ruben Brunk183f0562015-08-12 12:55:02 -07001911 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1912 // transitions.
1913 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1914 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1915 __FUNCTION__);
1916
1917 // Encountered desired state since we began waiting
1918 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001919 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1920 stateSeen = true;
1921 break;
1922 }
1923 }
1924 } while (!stateSeen);
1925
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001926 if (signalPipelineDrain) {
1927 mRequestThread->resetPipelineDrain();
1928 }
1929
Ruben Brunk183f0562015-08-12 12:55:02 -07001930 mStatusWaiters--;
1931
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001932 return res;
1933}
1934
1935
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001936status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001937 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001938 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001939
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001940 if (listener != NULL && mListener != NULL) {
1941 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1942 }
1943 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001944 mRequestThread->setNotificationListener(listener);
1945 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001946
1947 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001948}
1949
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001950bool Camera3Device::willNotify3A() {
1951 return false;
1952}
1953
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001954status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001955 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001956 std::unique_lock<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001957
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001958 while (mResultQueue.empty()) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001959 auto st = mResultSignal.wait_for(l, std::chrono::nanoseconds(timeout));
1960 if (st == std::cv_status::timeout) {
1961 return TIMED_OUT;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001962 }
1963 }
1964 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001965}
1966
Jianing Weicb0652e2014-03-12 18:29:36 -07001967status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001968 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001969 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001970
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001971 if (mResultQueue.empty()) {
1972 return NOT_ENOUGH_DATA;
1973 }
1974
Jianing Weicb0652e2014-03-12 18:29:36 -07001975 if (frame == NULL) {
1976 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1977 return BAD_VALUE;
1978 }
1979
1980 CaptureResult &result = *(mResultQueue.begin());
1981 frame->mResultExtras = result.mResultExtras;
1982 frame->mMetadata.acquire(result.mMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001983 frame->mPhysicalMetadatas = std::move(result.mPhysicalMetadatas);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001984 mResultQueue.erase(mResultQueue.begin());
1985
1986 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001987}
1988
1989status_t Camera3Device::triggerAutofocus(uint32_t id) {
1990 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001991 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001992
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001993 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1994 // Mix-in this trigger into the next request and only the next request.
1995 RequestTrigger trigger[] = {
1996 {
1997 ANDROID_CONTROL_AF_TRIGGER,
1998 ANDROID_CONTROL_AF_TRIGGER_START
1999 },
2000 {
2001 ANDROID_CONTROL_AF_TRIGGER_ID,
2002 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002003 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002004 };
2005
2006 return mRequestThread->queueTrigger(trigger,
2007 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002008}
2009
2010status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
2011 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002012 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002013
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002014 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
2015 // Mix-in this trigger into the next request and only the next request.
2016 RequestTrigger trigger[] = {
2017 {
2018 ANDROID_CONTROL_AF_TRIGGER,
2019 ANDROID_CONTROL_AF_TRIGGER_CANCEL
2020 },
2021 {
2022 ANDROID_CONTROL_AF_TRIGGER_ID,
2023 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002024 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002025 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002026
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002027 return mRequestThread->queueTrigger(trigger,
2028 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002029}
2030
2031status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
2032 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002033 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002034
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002035 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
2036 // Mix-in this trigger into the next request and only the next request.
2037 RequestTrigger trigger[] = {
2038 {
2039 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2040 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
2041 },
2042 {
2043 ANDROID_CONTROL_AE_PRECAPTURE_ID,
2044 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002045 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002046 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002047
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002048 return mRequestThread->queueTrigger(trigger,
2049 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002050}
2051
Jianing Weicb0652e2014-03-12 18:29:36 -07002052status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002053 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002054 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002055 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002056
Zhijun He7ef20392014-04-21 16:04:17 -07002057 {
2058 Mutex::Autolock l(mLock);
Emilian Peeved2ebe42018-09-25 16:59:09 +01002059
2060 // b/116514106 "disconnect()" can get called twice for the same device. The
2061 // camera device will not be initialized during the second run.
2062 if (mStatus == STATUS_UNINITIALIZED) {
2063 return OK;
2064 }
2065
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002066 mRequestThread->clear(/*out*/frameNumber);
Shuzhen Wang316781a2020-08-18 18:11:01 -07002067
2068 // Stop session and stream counter
2069 mSessionStatsBuilder.stopCounter();
Zhijun He7ef20392014-04-21 16:04:17 -07002070 }
2071
Emilian Peev08dd2452017-04-06 16:55:14 +01002072 return mRequestThread->flush();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002073}
2074
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002075status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07002076 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
2077}
2078
2079status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002080 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002081 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002082 Mutex::Autolock il(mInterfaceLock);
2083 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002084
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002085 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
2086 if (stream == nullptr) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002087 CLOGE("Stream %d does not exist", streamId);
2088 return BAD_VALUE;
2089 }
2090
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002091 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002092 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002093 return BAD_VALUE;
2094 }
2095
2096 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002097 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002098 return BAD_VALUE;
2099 }
2100
Ruben Brunkc78ac262015-08-13 17:58:46 -07002101 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002102}
2103
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002104status_t Camera3Device::tearDown(int streamId) {
2105 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002106 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002107 Mutex::Autolock il(mInterfaceLock);
2108 Mutex::Autolock l(mLock);
2109
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002110 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
2111 if (stream == nullptr) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002112 CLOGE("Stream %d does not exist", streamId);
2113 return BAD_VALUE;
2114 }
2115
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002116 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
2117 CLOGE("Stream %d is a target of a in-progress request", streamId);
2118 return BAD_VALUE;
2119 }
2120
2121 return stream->tearDown();
2122}
2123
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002124status_t Camera3Device::addBufferListenerForStream(int streamId,
2125 wp<Camera3StreamBufferListener> listener) {
2126 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002127 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002128 Mutex::Autolock il(mInterfaceLock);
2129 Mutex::Autolock l(mLock);
2130
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002131 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
2132 if (stream == nullptr) {
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002133 CLOGE("Stream %d does not exist", streamId);
2134 return BAD_VALUE;
2135 }
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002136 stream->addBufferListener(listener);
2137
2138 return OK;
2139}
2140
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002141/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002142 * Methods called by subclasses
2143 */
2144
2145void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002146 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -07002147 std::vector<int> streamIds;
2148 std::vector<hardware::CameraStreamStats> streamStats;
2149
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002150 {
2151 // Need mLock to safely update state and synchronize to current
2152 // state of methods in flight.
2153 Mutex::Autolock l(mLock);
2154 // We can get various system-idle notices from the status tracker
2155 // while starting up. Only care about them if we've actually sent
2156 // in some requests recently.
2157 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
2158 return;
2159 }
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08002160 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
2161 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
Ruben Brunk183f0562015-08-12 12:55:02 -07002162 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002163
2164 // Skip notifying listener if we're doing some user-transparent
2165 // state changes
2166 if (mPauseStateNotify) return;
Shuzhen Wang316781a2020-08-18 18:11:01 -07002167
2168 // Populate stream statistics in case of Idle
2169 if (idle) {
2170 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2171 auto stream = mOutputStreams[i];
2172 if (stream.get() == nullptr) continue;
2173 streamIds.push_back(stream->getId());
2174 Camera3Stream* camera3Stream = Camera3Stream::cast(stream->asHalStream());
2175 int64_t usage = 0LL;
2176 if (camera3Stream != nullptr) {
2177 usage = camera3Stream->getUsage();
2178 }
2179 streamStats.emplace_back(stream->getWidth(), stream->getHeight(),
2180 stream->getFormat(), stream->getDataSpace(), usage,
2181 stream->getMaxHalBuffers(),
2182 stream->getMaxTotalBuffers() - stream->getMaxHalBuffers());
2183 }
2184 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002185 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002186
2187 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002188 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002189 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002190 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002191 }
2192 if (idle && listener != NULL) {
Shuzhen Wang316781a2020-08-18 18:11:01 -07002193 // Get session stats from the builder, and notify the listener.
2194 int64_t requestCount, resultErrorCount;
2195 bool deviceError;
2196 std::map<int, StreamStats> streamStatsMap;
2197 mSessionStatsBuilder.buildAndReset(&requestCount, &resultErrorCount,
2198 &deviceError, &streamStatsMap);
2199 for (size_t i = 0; i < streamIds.size(); i++) {
2200 int streamId = streamIds[i];
2201 auto stats = streamStatsMap.find(streamId);
2202 if (stats != streamStatsMap.end()) {
2203 streamStats[i].mRequestCount = stats->second.mRequestedFrameCount;
2204 streamStats[i].mErrorCount = stats->second.mDroppedFrameCount;
2205 streamStats[i].mStartLatencyMs = stats->second.mStartLatencyMs;
Shuzhen Wangdb8f2782020-10-30 08:43:37 -07002206 streamStats[i].mHistogramType =
2207 hardware::CameraStreamStats::HISTOGRAM_TYPE_CAPTURE_LATENCY;
2208 streamStats[i].mHistogramBins.assign(
2209 stats->second.mCaptureLatencyBins.begin(),
2210 stats->second.mCaptureLatencyBins.end());
2211 streamStats[i].mHistogramCounts.assign(
2212 stats->second.mCaptureLatencyHistogram.begin(),
2213 stats->second.mCaptureLatencyHistogram.end());
Shuzhen Wang316781a2020-08-18 18:11:01 -07002214 }
2215 }
2216 listener->notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002217 }
2218}
2219
Shuzhen Wang758c2152017-01-10 18:26:18 -08002220status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01002221 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07002222 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08002223 ALOGV("%s: Camera %s: set consumer surface for stream %d",
2224 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01002225
2226 if (surfaceIds == nullptr) {
2227 return BAD_VALUE;
2228 }
2229
Zhijun He5d677d12016-05-29 16:52:39 -07002230 Mutex::Autolock il(mInterfaceLock);
2231 Mutex::Autolock l(mLock);
2232
Shuzhen Wang758c2152017-01-10 18:26:18 -08002233 if (consumers.size() == 0) {
2234 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07002235 return BAD_VALUE;
2236 }
2237
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002238 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2239 if (stream == nullptr) {
Zhijun He5d677d12016-05-29 16:52:39 -07002240 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002241 return BAD_VALUE;
Zhijun He5d677d12016-05-29 16:52:39 -07002242 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002243
2244 // isConsumerConfigurationDeferred will be off after setConsumers
2245 bool isDeferred = stream->isConsumerConfigurationDeferred();
Shuzhen Wang758c2152017-01-10 18:26:18 -08002246 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07002247 if (res != OK) {
2248 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
2249 return res;
2250 }
2251
Emilian Peev40ead602017-09-26 15:46:36 +01002252 for (auto &consumer : consumers) {
2253 int id = stream->getSurfaceId(consumer);
2254 if (id < 0) {
2255 CLOGE("Invalid surface id!");
2256 return BAD_VALUE;
2257 }
2258 surfaceIds->push_back(id);
2259 }
2260
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002261 if (isDeferred) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07002262 if (!stream->isConfiguring()) {
2263 CLOGE("Stream %d was already fully configured.", streamId);
2264 return INVALID_OPERATION;
2265 }
Zhijun He5d677d12016-05-29 16:52:39 -07002266
Shuzhen Wang0129d522016-10-30 22:43:41 -07002267 res = stream->finishConfiguration();
2268 if (res != OK) {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002269 // If finishConfiguration fails due to abandoned surface, do not set
2270 // device to error state.
2271 bool isSurfaceAbandoned =
2272 (res == NO_INIT || res == DEAD_OBJECT) && stream->isAbandoned();
2273 if (!isSurfaceAbandoned) {
2274 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
2275 stream->getId(), strerror(-res), res);
2276 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07002277 return res;
2278 }
Zhijun He5d677d12016-05-29 16:52:39 -07002279 }
2280
2281 return OK;
2282}
2283
Emilian Peev40ead602017-09-26 15:46:36 +01002284status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
2285 const std::vector<OutputStreamInfo> &outputInfo,
2286 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
2287 Mutex::Autolock il(mInterfaceLock);
2288 Mutex::Autolock l(mLock);
2289
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002290 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2291 if (stream == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +01002292 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002293 return BAD_VALUE;
Emilian Peev40ead602017-09-26 15:46:36 +01002294 }
2295
2296 for (const auto &it : removedSurfaceIds) {
2297 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2298 CLOGE("Shared surface still part of a pending request!");
2299 return -EBUSY;
2300 }
2301 }
2302
Emilian Peev40ead602017-09-26 15:46:36 +01002303 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2304 if (res != OK) {
2305 CLOGE("Stream %d failed to update stream (error %d %s) ",
2306 streamId, res, strerror(-res));
2307 if (res == UNKNOWN_ERROR) {
2308 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2309 __FUNCTION__);
2310 }
2311 return res;
2312 }
2313
2314 return res;
2315}
2316
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002317status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2318 Mutex::Autolock il(mInterfaceLock);
2319 Mutex::Autolock l(mLock);
2320
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002321 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2322 if (stream == nullptr) {
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002323 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2324 return BAD_VALUE;
2325 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07002326
2327 if (dropping) {
2328 mSessionStatsBuilder.stopCounter(streamId);
2329 } else {
2330 mSessionStatsBuilder.startCounter(streamId);
2331 }
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002332 return stream->dropBuffers(dropping);
2333}
2334
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002335/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002336 * Camera3Device private methods
2337 */
2338
2339sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002340 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002341 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002342
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002343 sp<CaptureRequest> newRequest = new CaptureRequest();
Emilian Peevaebbe412018-01-15 13:53:24 +00002344 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002345
2346 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002347 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002348 if (inputStreams.count > 0) {
2349 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002350 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002351 CLOGE("Request references unknown input stream %d",
2352 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002353 return NULL;
2354 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002355
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002356 if (mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002357 SET_ERR_L("%s: input stream %d is not configured!",
2358 __FUNCTION__, mInputStream->getId());
2359 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002360 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002361 // Check if stream prepare is blocking requests.
2362 if (mInputStream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002363 CLOGE("Request references an input stream that's being prepared!");
2364 return NULL;
2365 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002366
2367 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002368 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002369 }
2370
2371 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002372 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002373 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002374 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002375 return NULL;
2376 }
2377
2378 for (size_t i = 0; i < streams.count; i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002379 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streams.data.i32[i]);
2380 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002381 CLOGE("Request references unknown stream %d",
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002382 streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002383 return NULL;
2384 }
Zhijun He5d677d12016-05-29 16:52:39 -07002385 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002386 auto iter = surfaceMap.find(streams.data.i32[i]);
2387 if (iter != surfaceMap.end()) {
2388 const std::vector<size_t>& surfaces = iter->second;
2389 for (const auto& surface : surfaces) {
2390 if (stream->isConsumerConfigurationDeferred(surface)) {
2391 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2392 "due to deferred consumer", stream->getId(), surface);
2393 return NULL;
2394 }
2395 }
Yin-Chia Yeh0b287572018-10-15 12:38:13 -07002396 newRequest->mOutputSurfaces[streams.data.i32[i]] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002397 }
2398
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002399 if (stream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002400 SET_ERR_L("%s: stream %d is not configured!", __FUNCTION__, stream->getId());
2401 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002402 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002403 // Check if stream prepare is blocking requests.
2404 if (stream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002405 CLOGE("Request references an output stream that's being prepared!");
2406 return NULL;
2407 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002408
2409 newRequest->mOutputStreams.push(stream);
2410 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002411 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002412 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002413
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002414 auto rotateAndCropEntry =
2415 newRequest->mSettingsList.begin()->metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
2416 if (rotateAndCropEntry.count > 0 &&
2417 rotateAndCropEntry.data.u8[0] == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
2418 newRequest->mRotateAndCropAuto = true;
2419 } else {
2420 newRequest->mRotateAndCropAuto = false;
2421 }
2422
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07002423 auto zoomRatioEntry =
2424 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_ZOOM_RATIO);
2425 if (zoomRatioEntry.count > 0 &&
2426 zoomRatioEntry.data.f[0] == 1.0f) {
2427 newRequest->mZoomRatioIs1x = true;
2428 } else {
2429 newRequest->mZoomRatioIs1x = false;
2430 }
2431
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002432 if (mSupportCameraMute) {
2433 auto testPatternModeEntry =
2434 newRequest->mSettingsList.begin()->metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
2435 newRequest->mOriginalTestPatternMode = testPatternModeEntry.count > 0 ?
2436 testPatternModeEntry.data.i32[0] :
2437 ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;
2438
2439 auto testPatternDataEntry =
2440 newRequest->mSettingsList.begin()->metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
2441 if (testPatternDataEntry.count > 0) {
2442 memcpy(newRequest->mOriginalTestPatternData, testPatternModeEntry.data.i32,
2443 sizeof(newRequest->mOriginalTestPatternData));
2444 } else {
2445 newRequest->mOriginalTestPatternData[0] = 0;
2446 newRequest->mOriginalTestPatternData[1] = 0;
2447 newRequest->mOriginalTestPatternData[2] = 0;
2448 newRequest->mOriginalTestPatternData[3] = 0;
2449 }
2450 }
2451
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002452 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002453}
2454
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002455void Camera3Device::cancelStreamsConfigurationLocked() {
2456 int res = OK;
2457 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2458 res = mInputStream->cancelConfiguration();
2459 if (res != OK) {
2460 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2461 mInputStream->getId(), strerror(-res), res);
2462 }
2463 }
2464
2465 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002466 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002467 if (outputStream->isConfiguring()) {
2468 res = outputStream->cancelConfiguration();
2469 if (res != OK) {
2470 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2471 outputStream->getId(), strerror(-res), res);
2472 }
2473 }
2474 }
2475
2476 // Return state to that at start of call, so that future configures
2477 // properly clean things up
2478 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2479 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002480
2481 res = mPreparerThread->resume();
2482 if (res != OK) {
2483 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2484 }
2485}
2486
Emilian Peev0d0191e2020-04-21 17:01:18 -07002487bool Camera3Device::checkAbandonedStreamsLocked() {
2488 if ((mInputStream.get() != nullptr) && (mInputStream->isAbandoned())) {
2489 return true;
2490 }
2491
2492 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2493 auto stream = mOutputStreams[i];
2494 if ((stream.get() != nullptr) && (stream->isAbandoned())) {
2495 return true;
2496 }
2497 }
2498
2499 return false;
2500}
2501
Emilian Peev3bead5f2020-05-28 17:29:08 -07002502bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams, int clientStatusId) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002503 ATRACE_CALL();
2504 bool ret = false;
2505
Shuzhen Wang316781a2020-08-18 18:11:01 -07002506 nsecs_t startTime = systemTime();
2507
Jayant Chowdhary646c31b2020-01-30 13:09:59 -08002508 Mutex::Autolock il(mInterfaceLock);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002509 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2510
2511 Mutex::Autolock l(mLock);
Emilian Peev0d0191e2020-04-21 17:01:18 -07002512 if (checkAbandonedStreamsLocked()) {
2513 ALOGW("%s: Abandoned stream detected, session parameters can't be applied correctly!",
2514 __FUNCTION__);
2515 return true;
2516 }
2517
Emilian Peev3bead5f2020-05-28 17:29:08 -07002518 status_t rc = NO_ERROR;
2519 bool markClientActive = false;
2520 if (mStatus == STATUS_ACTIVE) {
2521 markClientActive = true;
2522 mPauseStateNotify = true;
2523 mStatusTracker->markComponentIdle(clientStatusId, Fence::NO_FENCE);
2524
2525 rc = internalPauseAndWaitLocked(maxExpectedDuration);
2526 }
2527
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002528 if (rc == NO_ERROR) {
2529 mNeedConfig = true;
2530 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2531 if (rc == NO_ERROR) {
2532 ret = true;
2533 mPauseStateNotify = false;
2534 //Moving to active state while holding 'mLock' is important.
2535 //There could be pending calls to 'create-/deleteStream' which
2536 //will trigger another stream configuration while the already
2537 //present streams end up with outstanding buffers that will
2538 //not get drained.
2539 internalUpdateStatusLocked(STATUS_ACTIVE);
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002540 } else if (rc == DEAD_OBJECT) {
2541 // DEAD_OBJECT can be returned if either the consumer surface is
2542 // abandoned, or the HAL has died.
2543 // - If the HAL has died, configureStreamsLocked call will set
2544 // device to error state,
2545 // - If surface is abandoned, we should not set device to error
2546 // state.
2547 ALOGE("Failed to re-configure camera due to abandoned surface");
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002548 } else {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002549 SET_ERR_L("Failed to re-configure camera: %d", rc);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002550 }
2551 } else {
2552 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2553 }
2554
Shuzhen Wang316781a2020-08-18 18:11:01 -07002555 CameraServiceProxyWrapper::logStreamConfigured(mId, mOperatingMode, true /*internalReconfig*/,
2556 ns2ms(systemTime() - startTime));
2557
Emilian Peev3bead5f2020-05-28 17:29:08 -07002558 if (markClientActive) {
2559 mStatusTracker->markComponentActive(clientStatusId);
2560 }
2561
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002562 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002563}
2564
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002565status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002566 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002567 ATRACE_CALL();
2568 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002569
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002570 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002571 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002572 return INVALID_OPERATION;
2573 }
2574
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002575 if (operatingMode < 0) {
2576 CLOGE("Invalid operating mode: %d", operatingMode);
2577 return BAD_VALUE;
2578 }
2579
2580 bool isConstrainedHighSpeed =
2581 static_cast<int>(StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ==
2582 operatingMode;
2583
2584 if (mOperatingMode != operatingMode) {
2585 mNeedConfig = true;
2586 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2587 mOperatingMode = operatingMode;
2588 }
2589
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002590 // In case called from configureStreams, abort queued input buffers not belonging to
2591 // any pending requests.
2592 if (mInputStream != NULL && notifyRequestThread) {
2593 while (true) {
Emilian Peevf4816702020-04-03 15:44:51 -07002594 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08002595 camera3::Size inputBufferSize;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002596 status_t res = mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08002597 &inputBufferSize, /*respectHalLimit*/ false);
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002598 if (res != OK) {
2599 // Exhausted acquiring all input buffers.
2600 break;
2601 }
2602
Emilian Peevf4816702020-04-03 15:44:51 -07002603 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002604 res = mInputStream->returnInputBuffer(inputBuffer);
2605 if (res != OK) {
2606 ALOGE("%s: %d: couldn't return input buffer while clearing input queue: "
2607 "%s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2608 }
2609 }
2610 }
2611
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002612 if (!mNeedConfig) {
2613 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2614 return OK;
2615 }
2616
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002617 // Workaround for device HALv3.2 or older spec bug - zero streams requires
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002618 // adding a fake stream instead.
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002619 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2620 if (mOutputStreams.size() == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002621 addFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002622 } else {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002623 tryRemoveFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002624 }
2625
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002626 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002627 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002628
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002629 mPreparerThread->pause();
2630
Emilian Peevf4816702020-04-03 15:44:51 -07002631 camera_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002632 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002633 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
Shuzhen Wang83bff122020-11-20 15:51:39 -08002634 config.input_is_multi_resolution = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002635
Emilian Peevf4816702020-04-03 15:44:51 -07002636 Vector<camera3::camera_stream_t*> streams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002637 streams.setCapacity(config.num_streams);
Emilian Peev192ee832018-01-31 14:46:47 +00002638 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002639
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002640
2641 if (mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07002642 camera3::camera_stream_t *inputStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002643 inputStream = mInputStream->startConfiguration();
2644 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002645 CLOGE("Can't start input stream configuration");
2646 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002647 return INVALID_OPERATION;
2648 }
2649 streams.add(inputStream);
Shuzhen Wang83bff122020-11-20 15:51:39 -08002650
2651 config.input_is_multi_resolution = mIsInputStreamMultiResolution;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002652 }
2653
2654 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002655
2656 // Don't configure bidi streams twice, nor add them twice to the list
2657 if (mOutputStreams[i].get() ==
2658 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2659
2660 config.num_streams--;
2661 continue;
2662 }
2663
Emilian Peevf4816702020-04-03 15:44:51 -07002664 camera3::camera_stream_t *outputStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002665 outputStream = mOutputStreams[i]->startConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002666 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002667 CLOGE("Can't start output stream configuration");
2668 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002669 return INVALID_OPERATION;
2670 }
2671 streams.add(outputStream);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002672
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002673 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB) {
Emilian Peev192ee832018-01-31 14:46:47 +00002674 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2675 // always occupy the initial entry.
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002676 if (outputStream->data_space == HAL_DATASPACE_V0_JFIF) {
2677 bufferSizes[k] = static_cast<uint32_t>(
2678 getJpegBufferSize(outputStream->width, outputStream->height));
2679 } else if (outputStream->data_space ==
2680 static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
2681 bufferSizes[k] = outputStream->width * outputStream->height;
2682 } else {
2683 ALOGW("%s: Blob dataSpace %d not supported",
2684 __FUNCTION__, outputStream->data_space);
2685 }
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002686 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002687 }
2688
2689 config.streams = streams.editArray();
2690
2691 // Do the HAL configuration; will potentially touch stream
Shuzhen Wang92653952019-05-07 15:11:43 -07002692 // max_buffers, usage, and priv fields, as well as data_space and format
2693 // fields for IMPLEMENTATION_DEFINED formats.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002694
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002695 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
Emilian Peev192ee832018-01-31 14:46:47 +00002696 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002697 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002698
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002699 if (res == BAD_VALUE) {
2700 // HAL rejected this set of streams as unsupported, clean up config
2701 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002702 CLOGE("Set of requested inputs/outputs not supported by HAL");
2703 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002704 return BAD_VALUE;
2705 } else if (res != OK) {
2706 // Some other kind of error from configure_streams - this is not
2707 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002708 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2709 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002710 return res;
2711 }
2712
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002713 // Finish all stream configuration immediately.
2714 // TODO: Try to relax this later back to lazy completion, which should be
2715 // faster
2716
Igor Murashkin073f8572013-05-02 14:59:28 -07002717 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002718 bool streamReConfigured = false;
2719 res = mInputStream->finishConfiguration(&streamReConfigured);
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002720 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002721 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002722 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002723 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002724 if ((res == NO_INIT || res == DEAD_OBJECT) && mInputStream->isAbandoned()) {
2725 return DEAD_OBJECT;
2726 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002727 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002728 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002729 if (streamReConfigured) {
2730 mInterface->onStreamReConfigured(mInputStream->getId());
2731 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002732 }
2733
2734 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002735 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Zhijun He5d677d12016-05-29 16:52:39 -07002736 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002737 bool streamReConfigured = false;
2738 res = outputStream->finishConfiguration(&streamReConfigured);
Igor Murashkin073f8572013-05-02 14:59:28 -07002739 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002740 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002741 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002742 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002743 if ((res == NO_INIT || res == DEAD_OBJECT) && outputStream->isAbandoned()) {
2744 return DEAD_OBJECT;
2745 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002746 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002747 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002748 if (streamReConfigured) {
2749 mInterface->onStreamReConfigured(outputStream->getId());
2750 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002751 }
2752 }
2753
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002754 // Request thread needs to know to avoid using repeat-last-settings protocol
2755 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002756 if (notifyRequestThread) {
2757 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration, sessionParams);
2758 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002759
Zhijun He90f7c372016-08-16 16:19:43 -07002760 char value[PROPERTY_VALUE_MAX];
2761 property_get("camera.fifo.disable", value, "0");
2762 int32_t disableFifo = atoi(value);
2763 if (disableFifo != 1) {
2764 // Boost priority of request thread to SCHED_FIFO.
2765 pid_t requestThreadTid = mRequestThread->getTid();
2766 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002767 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002768 if (res != OK) {
2769 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2770 strerror(-res), res);
2771 } else {
2772 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2773 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002774 }
2775
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002776 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002777 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2778 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2779 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2780 sessionParams.unlock(newSessionParams);
2781 mSessionParams.unlock(currentSessionParams);
2782 if (updateSessionParams) {
2783 mSessionParams = sessionParams;
2784 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002785
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002786 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002787
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002788 internalUpdateStatusLocked((mFakeStreamId == NO_STREAM) ?
Ruben Brunk183f0562015-08-12 12:55:02 -07002789 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002790
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002791 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002792
Zhijun He0a210512014-07-24 13:45:15 -07002793 // tear down the deleted streams after configure streams.
2794 mDeletedStreams.clear();
2795
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002796 auto rc = mPreparerThread->resume();
2797 if (rc != OK) {
2798 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2799 return rc;
2800 }
2801
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002802 if (mFakeStreamId == NO_STREAM) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002803 mRequestBufferSM.onStreamsConfigured();
2804 }
2805
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002806 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002807}
2808
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002809status_t Camera3Device::addFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002810 ATRACE_CALL();
2811 status_t res;
2812
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002813 if (mFakeStreamId != NO_STREAM) {
2814 // Should never be adding a second fake stream when one is already
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002815 // active
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002816 SET_ERR_L("%s: Camera %s: A fake stream already exists!",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002817 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002818 return INVALID_OPERATION;
2819 }
2820
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002821 ALOGV("%s: Camera %s: Adding a fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002822
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002823 sp<Camera3OutputStreamInterface> fakeStream =
2824 new Camera3FakeStream(mNextStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002825
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002826 res = mOutputStreams.add(mNextStreamId, fakeStream);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002827 if (res < 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002828 SET_ERR_L("Can't add fake stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002829 return res;
2830 }
2831
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002832 mFakeStreamId = mNextStreamId;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002833 mNextStreamId++;
2834
2835 return OK;
2836}
2837
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002838status_t Camera3Device::tryRemoveFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002839 ATRACE_CALL();
2840 status_t res;
2841
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002842 if (mFakeStreamId == NO_STREAM) return OK;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002843 if (mOutputStreams.size() == 1) return OK;
2844
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002845 ALOGV("%s: Camera %s: Removing the fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002846
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002847 // Ok, have a fake stream and there's at least one other output stream,
2848 // so remove the fake
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002849
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002850 sp<Camera3StreamInterface> deletedStream = mOutputStreams.get(mFakeStreamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002851 if (deletedStream == nullptr) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002852 SET_ERR_L("Fake stream %d does not appear to exist", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002853 return INVALID_OPERATION;
2854 }
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002855 mOutputStreams.remove(mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002856
2857 // Free up the stream endpoint so that it can be used by some other stream
2858 res = deletedStream->disconnect();
2859 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002860 SET_ERR_L("Can't disconnect deleted fake stream %d", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002861 // fall through since we want to still list the stream as deleted.
2862 }
2863 mDeletedStreams.add(deletedStream);
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002864 mFakeStreamId = NO_STREAM;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002865
2866 return res;
2867}
2868
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002869void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002870 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002871 Mutex::Autolock l(mLock);
2872 va_list args;
2873 va_start(args, fmt);
2874
2875 setErrorStateLockedV(fmt, args);
2876
2877 va_end(args);
2878}
2879
2880void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002881 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002882 Mutex::Autolock l(mLock);
2883 setErrorStateLockedV(fmt, args);
2884}
2885
2886void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2887 va_list args;
2888 va_start(args, fmt);
2889
2890 setErrorStateLockedV(fmt, args);
2891
2892 va_end(args);
2893}
2894
2895void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002896 // Print out all error messages to log
2897 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002898 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002899
2900 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002901 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002902
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002903 mErrorCause = errorCause;
2904
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07002905 if (mRequestThread != nullptr) {
2906 mRequestThread->setPaused(true);
2907 }
Ruben Brunk183f0562015-08-12 12:55:02 -07002908 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002909
2910 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002911 sp<NotificationListener> listener = mListener.promote();
2912 if (listener != NULL) {
2913 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002914 CaptureResultExtras());
Shuzhen Wang316781a2020-08-18 18:11:01 -07002915 mSessionStatsBuilder.onDeviceError();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002916 }
2917
2918 // Save stack trace. View by dumping it later.
2919 CameraTraces::saveTrace();
2920 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002921}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002922
2923/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002924 * In-flight request management
2925 */
2926
Jianing Weicb0652e2014-03-12 18:29:36 -07002927status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002928 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002929 bool hasAppCallback, nsecs_t maxExpectedDuration,
Shuzhen Wang26abaf42018-08-28 15:41:20 -07002930 std::set<String8>& physicalCameraIds, bool isStillCapture,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002931 bool isZslCapture, bool rotateAndCropAuto, const std::set<std::string>& cameraIdsWithZoom,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002932 const SurfaceMap& outputSurfaces, nsecs_t requestTimeNs) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002933 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002934 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002935
2936 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002937 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07002938 hasAppCallback, maxExpectedDuration, physicalCameraIds, isStillCapture, isZslCapture,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002939 rotateAndCropAuto, cameraIdsWithZoom, requestTimeNs, outputSurfaces));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002940 if (res < 0) return res;
2941
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002942 if (mInFlightMap.size() == 1) {
Emilian Peev26d975d2018-07-05 14:52:57 +01002943 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2944 // avoid a deadlock during reprocess requests.
2945 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002946 if (mStatusTracker != nullptr) {
2947 mStatusTracker->markComponentActive(mInFlightStatusId);
2948 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002949 }
2950
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002951 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002952 return OK;
2953}
2954
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002955void Camera3Device::onInflightEntryRemovedLocked(nsecs_t duration) {
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002956 // Indicate idle inFlightMap to the status tracker
2957 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002958 mRequestBufferSM.onInflightMapEmpty();
Emilian Peev26d975d2018-07-05 14:52:57 +01002959 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2960 // avoid a deadlock during reprocess requests.
2961 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002962 if (mStatusTracker != nullptr) {
2963 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2964 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002965 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002966 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002967}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002968
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002969void Camera3Device::checkInflightMapLengthLocked() {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002970 // Validation check - if we have too many in-flight frames with long total inflight duration,
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -07002971 // something has likely gone wrong. This might still be legit only if application send in
2972 // a long burst of long exposure requests.
2973 if (mExpectedInflightDuration > kMinWarnInflightDuration) {
2974 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
2975 CLOGW("In-flight list too large: %zu, total inflight duration %" PRIu64,
2976 mInFlightMap.size(), mExpectedInflightDuration);
2977 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2978 kInFlightWarnLimitHighSpeed) {
2979 CLOGW("In-flight list too large for high speed configuration: %zu,"
2980 "total inflight duration %" PRIu64,
2981 mInFlightMap.size(), mExpectedInflightDuration);
2982 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002983 }
2984}
2985
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002986void Camera3Device::onInflightMapFlushedLocked() {
2987 mExpectedInflightDuration = 0;
2988}
2989
2990void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Jayant Chowdharyd4776262020-06-23 23:45:57 -07002991 ATRACE_HFR_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002992 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
2993 mInFlightMap.removeItemsAt(idx, 1);
2994
2995 onInflightEntryRemovedLocked(duration);
2996}
2997
2998
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002999void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003000 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003001 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003002 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003003 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003004 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003005 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003006
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003007 FlushInflightReqStates states {
3008 mId, mInFlightLock, mInFlightMap, mUseHalBufManager,
Shuzhen Wang316781a2020-08-18 18:11:01 -07003009 listener, *this, *mInterface, *this, mSessionStatsBuilder};
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003010
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003011 camera3::flushInflightRequests(states);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003012}
3013
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003014CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07003015 ALOGV("%s", __FUNCTION__);
3016
Igor Murashkin1e479c02013-09-06 16:55:14 -07003017 CameraMetadata retVal;
3018
3019 if (mRequestThread != NULL) {
3020 retVal = mRequestThread->getLatestRequest();
3021 }
3022
Igor Murashkin1e479c02013-09-06 16:55:14 -07003023 return retVal;
3024}
3025
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003026void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003027 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata,
3028 const std::unordered_map<std::string, CameraMetadata>& physicalMetadata) {
3029
3030 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata,
3031 physicalMetadata);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003032}
3033
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003034/**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003035 * HalInterface inner class methods
3036 */
3037
Yifan Hongf79b5542017-04-11 14:44:25 -07003038Camera3Device::HalInterface::HalInterface(
3039 sp<ICameraDeviceSession> &session,
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003040 std::shared_ptr<RequestMetadataQueue> queue,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003041 bool useHalBufManager, bool supportOfflineProcessing) :
Yifan Hongf79b5542017-04-11 14:44:25 -07003042 mHidlSession(session),
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003043 mRequestMetadataQueue(queue),
Emilian Peev4ec17882019-01-24 17:16:58 -08003044 mUseHalBufManager(useHalBufManager),
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003045 mIsReconfigurationQuerySupported(true),
3046 mSupportOfflineProcessing(supportOfflineProcessing) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003047 // Check with hardware service manager if we can downcast these interfaces
3048 // Somewhat expensive, so cache the results at startup
Shuzhen Wang83bff122020-11-20 15:51:39 -08003049 auto castResult_3_7 = device::V3_7::ICameraDeviceSession::castFrom(mHidlSession);
3050 if (castResult_3_7.isOk()) {
3051 mHidlSession_3_7 = castResult_3_7;
3052 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003053 auto castResult_3_6 = device::V3_6::ICameraDeviceSession::castFrom(mHidlSession);
3054 if (castResult_3_6.isOk()) {
3055 mHidlSession_3_6 = castResult_3_6;
3056 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003057 auto castResult_3_5 = device::V3_5::ICameraDeviceSession::castFrom(mHidlSession);
3058 if (castResult_3_5.isOk()) {
3059 mHidlSession_3_5 = castResult_3_5;
3060 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003061 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
3062 if (castResult_3_4.isOk()) {
3063 mHidlSession_3_4 = castResult_3_4;
3064 }
3065 auto castResult_3_3 = device::V3_3::ICameraDeviceSession::castFrom(mHidlSession);
3066 if (castResult_3_3.isOk()) {
3067 mHidlSession_3_3 = castResult_3_3;
3068 }
3069}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003070
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003071Camera3Device::HalInterface::HalInterface() :
3072 mUseHalBufManager(false),
3073 mSupportOfflineProcessing(false) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003074
3075Camera3Device::HalInterface::HalInterface(const HalInterface& other) :
Yifan Hongf79b5542017-04-11 14:44:25 -07003076 mHidlSession(other.mHidlSession),
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003077 mRequestMetadataQueue(other.mRequestMetadataQueue),
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003078 mUseHalBufManager(other.mUseHalBufManager),
3079 mSupportOfflineProcessing(other.mSupportOfflineProcessing) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003080
3081bool Camera3Device::HalInterface::valid() {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003082 return (mHidlSession != nullptr);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003083}
3084
3085void Camera3Device::HalInterface::clear() {
Shuzhen Wang83bff122020-11-20 15:51:39 -08003086 mHidlSession_3_7.clear();
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003087 mHidlSession_3_6.clear();
Emilian Peev644a3e12018-11-23 13:52:39 +00003088 mHidlSession_3_5.clear();
Emilian Peev9e740b02018-01-30 18:28:03 +00003089 mHidlSession_3_4.clear();
3090 mHidlSession_3_3.clear();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003091 mHidlSession.clear();
3092}
3093
3094status_t Camera3Device::HalInterface::constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -07003095 camera_request_template_t templateId,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003096 /*out*/ camera_metadata_t **requestTemplate) {
3097 ATRACE_NAME("CameraHal::constructDefaultRequestSettings");
3098 if (!valid()) return INVALID_OPERATION;
3099 status_t res = OK;
3100
Emilian Peev31abd0a2017-05-11 18:37:46 +01003101 common::V1_0::Status status;
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003102
3103 auto requestCallback = [&status, &requestTemplate]
Emilian Peev31abd0a2017-05-11 18:37:46 +01003104 (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003105 status = s;
3106 if (status == common::V1_0::Status::OK) {
3107 const camera_metadata *r =
3108 reinterpret_cast<const camera_metadata_t*>(request.data());
3109 size_t expectedSize = request.size();
3110 int ret = validate_camera_metadata_structure(r, &expectedSize);
3111 if (ret == OK || ret == CAMERA_METADATA_VALIDATION_SHIFTED) {
3112 *requestTemplate = clone_camera_metadata(r);
3113 if (*requestTemplate == nullptr) {
3114 ALOGE("%s: Unable to clone camera metadata received from HAL",
3115 __FUNCTION__);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003116 status = common::V1_0::Status::INTERNAL_ERROR;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003117 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003118 } else {
3119 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
3120 status = common::V1_0::Status::INTERNAL_ERROR;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003121 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003122 }
3123 };
3124 hardware::Return<void> err;
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003125 RequestTemplate id;
3126 switch (templateId) {
Emilian Peevf4816702020-04-03 15:44:51 -07003127 case CAMERA_TEMPLATE_PREVIEW:
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003128 id = RequestTemplate::PREVIEW;
3129 break;
Emilian Peevf4816702020-04-03 15:44:51 -07003130 case CAMERA_TEMPLATE_STILL_CAPTURE:
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003131 id = RequestTemplate::STILL_CAPTURE;
3132 break;
Emilian Peevf4816702020-04-03 15:44:51 -07003133 case CAMERA_TEMPLATE_VIDEO_RECORD:
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003134 id = RequestTemplate::VIDEO_RECORD;
3135 break;
Emilian Peevf4816702020-04-03 15:44:51 -07003136 case CAMERA_TEMPLATE_VIDEO_SNAPSHOT:
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003137 id = RequestTemplate::VIDEO_SNAPSHOT;
3138 break;
Emilian Peevf4816702020-04-03 15:44:51 -07003139 case CAMERA_TEMPLATE_ZERO_SHUTTER_LAG:
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003140 id = RequestTemplate::ZERO_SHUTTER_LAG;
3141 break;
Emilian Peevf4816702020-04-03 15:44:51 -07003142 case CAMERA_TEMPLATE_MANUAL:
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003143 id = RequestTemplate::MANUAL;
3144 break;
3145 default:
3146 // Unknown template ID, or this HAL is too old to support it
3147 return BAD_VALUE;
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003148 }
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003149 err = mHidlSession->constructDefaultRequestSettings(id, requestCallback);
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003150
Emilian Peev31abd0a2017-05-11 18:37:46 +01003151 if (!err.isOk()) {
3152 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3153 res = DEAD_OBJECT;
3154 } else {
3155 res = CameraProviderManager::mapToStatusT(status);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003156 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003157
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003158 return res;
3159}
3160
Emilian Peev4ec17882019-01-24 17:16:58 -08003161bool Camera3Device::HalInterface::isReconfigurationRequired(CameraMetadata& oldSessionParams,
3162 CameraMetadata& newSessionParams) {
3163 // We do reconfiguration by default;
3164 bool ret = true;
3165 if ((mHidlSession_3_5 != nullptr) && mIsReconfigurationQuerySupported) {
3166 android::hardware::hidl_vec<uint8_t> oldParams, newParams;
3167 camera_metadata_t* oldSessioMeta = const_cast<camera_metadata_t*>(
3168 oldSessionParams.getAndLock());
3169 camera_metadata_t* newSessioMeta = const_cast<camera_metadata_t*>(
3170 newSessionParams.getAndLock());
3171 oldParams.setToExternal(reinterpret_cast<uint8_t*>(oldSessioMeta),
3172 get_camera_metadata_size(oldSessioMeta));
3173 newParams.setToExternal(reinterpret_cast<uint8_t*>(newSessioMeta),
3174 get_camera_metadata_size(newSessioMeta));
3175 hardware::camera::common::V1_0::Status callStatus;
3176 bool required;
3177 auto hidlCb = [&callStatus, &required] (hardware::camera::common::V1_0::Status s,
3178 bool requiredFlag) {
3179 callStatus = s;
3180 required = requiredFlag;
3181 };
3182 auto err = mHidlSession_3_5->isReconfigurationRequired(oldParams, newParams, hidlCb);
3183 oldSessionParams.unlock(oldSessioMeta);
3184 newSessionParams.unlock(newSessioMeta);
3185 if (err.isOk()) {
3186 switch (callStatus) {
3187 case hardware::camera::common::V1_0::Status::OK:
3188 ret = required;
3189 break;
3190 case hardware::camera::common::V1_0::Status::METHOD_NOT_SUPPORTED:
3191 mIsReconfigurationQuerySupported = false;
3192 ret = true;
3193 break;
3194 default:
3195 ALOGV("%s: Reconfiguration query failed: %d", __FUNCTION__, callStatus);
3196 ret = true;
3197 }
3198 } else {
3199 ALOGE("%s: Unexpected binder error: %s", __FUNCTION__, err.description().c_str());
3200 ret = true;
3201 }
3202 }
3203
3204 return ret;
3205}
3206
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003207status_t Camera3Device::HalInterface::configureStreams(const camera_metadata_t *sessionParams,
Emilian Peevf4816702020-04-03 15:44:51 -07003208 camera_stream_configuration *config, const std::vector<uint32_t>& bufferSizes) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003209 ATRACE_NAME("CameraHal::configureStreams");
3210 if (!valid()) return INVALID_OPERATION;
3211 status_t res = OK;
3212
Shuzhen Wang83bff122020-11-20 15:51:39 -08003213 if (config->input_is_multi_resolution && mHidlSession_3_7 == nullptr) {
3214 ALOGE("%s: Camera device doesn't support multi-resolution input stream", __FUNCTION__);
3215 return BAD_VALUE;
3216 }
3217
Emilian Peev31abd0a2017-05-11 18:37:46 +01003218 // Convert stream config to HIDL
3219 std::set<int> activeStreams;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003220 device::V3_2::StreamConfiguration requestedConfiguration3_2;
3221 device::V3_4::StreamConfiguration requestedConfiguration3_4;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003222 device::V3_7::StreamConfiguration requestedConfiguration3_7;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003223 requestedConfiguration3_2.streams.resize(config->num_streams);
3224 requestedConfiguration3_4.streams.resize(config->num_streams);
Shuzhen Wang83bff122020-11-20 15:51:39 -08003225 requestedConfiguration3_7.streams.resize(config->num_streams);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003226 for (size_t i = 0; i < config->num_streams; i++) {
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003227 device::V3_2::Stream &dst3_2 = requestedConfiguration3_2.streams[i];
3228 device::V3_4::Stream &dst3_4 = requestedConfiguration3_4.streams[i];
Shuzhen Wang83bff122020-11-20 15:51:39 -08003229 device::V3_7::Stream &dst3_7 = requestedConfiguration3_7.streams[i];
Emilian Peevf4816702020-04-03 15:44:51 -07003230 camera3::camera_stream_t *src = config->streams[i];
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003231
Emilian Peev31abd0a2017-05-11 18:37:46 +01003232 Camera3Stream* cam3stream = Camera3Stream::cast(src);
3233 cam3stream->setBufferFreedListener(this);
3234 int streamId = cam3stream->getId();
3235 StreamType streamType;
3236 switch (src->stream_type) {
Emilian Peevf4816702020-04-03 15:44:51 -07003237 case CAMERA_STREAM_OUTPUT:
Emilian Peev31abd0a2017-05-11 18:37:46 +01003238 streamType = StreamType::OUTPUT;
3239 break;
Emilian Peevf4816702020-04-03 15:44:51 -07003240 case CAMERA_STREAM_INPUT:
Emilian Peev31abd0a2017-05-11 18:37:46 +01003241 streamType = StreamType::INPUT;
3242 break;
3243 default:
3244 ALOGE("%s: Stream %d: Unsupported stream type %d",
3245 __FUNCTION__, streamId, config->streams[i]->stream_type);
3246 return BAD_VALUE;
3247 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003248 dst3_2.id = streamId;
3249 dst3_2.streamType = streamType;
3250 dst3_2.width = src->width;
3251 dst3_2.height = src->height;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003252 dst3_2.usage = mapToConsumerUsage(cam3stream->getUsage());
Emilian Peevf4816702020-04-03 15:44:51 -07003253 dst3_2.rotation = mapToStreamRotation((camera_stream_rotation_t) src->rotation);
Shuzhen Wang92653952019-05-07 15:11:43 -07003254 // For HidlSession version 3.5 or newer, the format and dataSpace sent
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08003255 // to HAL are original, not the overridden ones.
Shuzhen Wang92653952019-05-07 15:11:43 -07003256 if (mHidlSession_3_5 != nullptr) {
3257 dst3_2.format = mapToPixelFormat(cam3stream->isFormatOverridden() ?
3258 cam3stream->getOriginalFormat() : src->format);
3259 dst3_2.dataSpace = mapToHidlDataspace(cam3stream->isDataSpaceOverridden() ?
3260 cam3stream->getOriginalDataSpace() : src->data_space);
3261 } else {
3262 dst3_2.format = mapToPixelFormat(src->format);
3263 dst3_2.dataSpace = mapToHidlDataspace(src->data_space);
3264 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003265 dst3_4.v3_2 = dst3_2;
Emilian Peev192ee832018-01-31 14:46:47 +00003266 dst3_4.bufferSize = bufferSizes[i];
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003267 if (src->physical_camera_id != nullptr) {
3268 dst3_4.physicalCameraId = src->physical_camera_id;
3269 }
Shuzhen Wang83bff122020-11-20 15:51:39 -08003270 dst3_7.v3_4 = dst3_4;
3271 dst3_7.groupId = cam3stream->getHalStreamGroupId();
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08003272 dst3_7.sensorPixelModesUsed.resize(src->sensor_pixel_modes_used.size());
3273 size_t j = 0;
3274 for (int mode : src->sensor_pixel_modes_used) {
3275 dst3_7.sensorPixelModesUsed[j++] =
3276 static_cast<CameraMetadataEnumAndroidSensorPixelMode>(mode);
3277 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003278 activeStreams.insert(streamId);
3279 // Create Buffer ID map if necessary
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003280 mBufferRecords.tryCreateBufferCache(streamId);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003281 }
3282 // remove BufferIdMap for deleted streams
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003283 mBufferRecords.removeInactiveBufferCaches(activeStreams);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003284
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003285 StreamConfigurationMode operationMode;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003286 res = mapToStreamConfigurationMode(
Emilian Peevf4816702020-04-03 15:44:51 -07003287 (camera_stream_configuration_mode_t) config->operation_mode,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003288 /*out*/ &operationMode);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003289 if (res != OK) {
3290 return res;
3291 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003292 requestedConfiguration3_2.operationMode = operationMode;
3293 requestedConfiguration3_4.operationMode = operationMode;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08003294 requestedConfiguration3_7.operationMode = operationMode;
3295 size_t sessionParamSize = get_camera_metadata_size(sessionParams);
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003296 requestedConfiguration3_4.sessionParams.setToExternal(
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003297 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(sessionParams)),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08003298 sessionParamSize);
Shuzhen Wang83bff122020-11-20 15:51:39 -08003299 requestedConfiguration3_7.operationMode = operationMode;
3300 requestedConfiguration3_7.sessionParams.setToExternal(
3301 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(sessionParams)),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08003302 sessionParamSize);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003303
Emilian Peev31abd0a2017-05-11 18:37:46 +01003304 // Invoke configureStreams
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003305 device::V3_3::HalStreamConfiguration finalConfiguration;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003306 device::V3_4::HalStreamConfiguration finalConfiguration3_4;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003307 device::V3_6::HalStreamConfiguration finalConfiguration3_6;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003308 common::V1_0::Status status;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003309
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003310 auto configStream34Cb = [&status, &finalConfiguration3_4]
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003311 (common::V1_0::Status s, const device::V3_4::HalStreamConfiguration& halConfiguration) {
3312 finalConfiguration3_4 = halConfiguration;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003313 status = s;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003314 };
3315
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003316 auto configStream36Cb = [&status, &finalConfiguration3_6]
3317 (common::V1_0::Status s, const device::V3_6::HalStreamConfiguration& halConfiguration) {
3318 finalConfiguration3_6 = halConfiguration;
3319 status = s;
3320 };
3321
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003322 auto postprocConfigStream34 = [&finalConfiguration, &finalConfiguration3_4]
3323 (hardware::Return<void>& err) -> status_t {
3324 if (!err.isOk()) {
3325 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3326 return DEAD_OBJECT;
3327 }
3328 finalConfiguration.streams.resize(finalConfiguration3_4.streams.size());
3329 for (size_t i = 0; i < finalConfiguration3_4.streams.size(); i++) {
3330 finalConfiguration.streams[i] = finalConfiguration3_4.streams[i].v3_3;
3331 }
3332 return OK;
3333 };
3334
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003335 auto postprocConfigStream36 = [&finalConfiguration, &finalConfiguration3_6]
3336 (hardware::Return<void>& err) -> status_t {
3337 if (!err.isOk()) {
3338 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3339 return DEAD_OBJECT;
3340 }
3341 finalConfiguration.streams.resize(finalConfiguration3_6.streams.size());
3342 for (size_t i = 0; i < finalConfiguration3_6.streams.size(); i++) {
3343 finalConfiguration.streams[i] = finalConfiguration3_6.streams[i].v3_4.v3_3;
3344 }
3345 return OK;
3346 };
3347
Shuzhen Wang92653952019-05-07 15:11:43 -07003348 // See which version of HAL we have
Shuzhen Wang83bff122020-11-20 15:51:39 -08003349 if (mHidlSession_3_7 != nullptr) {
3350 ALOGV("%s: v3.7 device found", __FUNCTION__);
3351 requestedConfiguration3_7.streamConfigCounter = mNextStreamConfigCounter++;
3352 requestedConfiguration3_7.multiResolutionInputImage = config->input_is_multi_resolution;
3353 auto err = mHidlSession_3_7->configureStreams_3_7(
3354 requestedConfiguration3_7, configStream36Cb);
3355 res = postprocConfigStream36(err);
3356 if (res != OK) {
3357 return res;
3358 }
3359 } else if (mHidlSession_3_6 != nullptr) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003360 ALOGV("%s: v3.6 device found", __FUNCTION__);
3361 device::V3_5::StreamConfiguration requestedConfiguration3_5;
3362 requestedConfiguration3_5.v3_4 = requestedConfiguration3_4;
3363 requestedConfiguration3_5.streamConfigCounter = mNextStreamConfigCounter++;
3364 auto err = mHidlSession_3_6->configureStreams_3_6(
3365 requestedConfiguration3_5, configStream36Cb);
3366 res = postprocConfigStream36(err);
3367 if (res != OK) {
3368 return res;
3369 }
3370 } else if (mHidlSession_3_5 != nullptr) {
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003371 ALOGV("%s: v3.5 device found", __FUNCTION__);
3372 device::V3_5::StreamConfiguration requestedConfiguration3_5;
3373 requestedConfiguration3_5.v3_4 = requestedConfiguration3_4;
3374 requestedConfiguration3_5.streamConfigCounter = mNextStreamConfigCounter++;
3375 auto err = mHidlSession_3_5->configureStreams_3_5(
3376 requestedConfiguration3_5, configStream34Cb);
3377 res = postprocConfigStream34(err);
3378 if (res != OK) {
3379 return res;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003380 }
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003381 } else if (mHidlSession_3_4 != nullptr) {
3382 // We do; use v3.4 for the call
3383 ALOGV("%s: v3.4 device found", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003384 auto err = mHidlSession_3_4->configureStreams_3_4(
3385 requestedConfiguration3_4, configStream34Cb);
3386 res = postprocConfigStream34(err);
3387 if (res != OK) {
3388 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003389 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003390 } else if (mHidlSession_3_3 != nullptr) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003391 // We do; use v3.3 for the call
3392 ALOGV("%s: v3.3 device found", __FUNCTION__);
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003393 auto err = mHidlSession_3_3->configureStreams_3_3(requestedConfiguration3_2,
Emilian Peev31abd0a2017-05-11 18:37:46 +01003394 [&status, &finalConfiguration]
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003395 (common::V1_0::Status s, const device::V3_3::HalStreamConfiguration& halConfiguration) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003396 finalConfiguration = halConfiguration;
3397 status = s;
3398 });
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003399 if (!err.isOk()) {
3400 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3401 return DEAD_OBJECT;
3402 }
3403 } else {
3404 // We don't; use v3.2 call and construct a v3.3 HalStreamConfiguration
3405 ALOGV("%s: v3.2 device found", __FUNCTION__);
3406 HalStreamConfiguration finalConfiguration_3_2;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003407 auto err = mHidlSession->configureStreams(requestedConfiguration3_2,
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003408 [&status, &finalConfiguration_3_2]
3409 (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) {
3410 finalConfiguration_3_2 = halConfiguration;
3411 status = s;
3412 });
3413 if (!err.isOk()) {
3414 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3415 return DEAD_OBJECT;
3416 }
3417 finalConfiguration.streams.resize(finalConfiguration_3_2.streams.size());
3418 for (size_t i = 0; i < finalConfiguration_3_2.streams.size(); i++) {
3419 finalConfiguration.streams[i].v3_2 = finalConfiguration_3_2.streams[i];
3420 finalConfiguration.streams[i].overrideDataSpace =
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003421 requestedConfiguration3_2.streams[i].dataSpace;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003422 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003423 }
3424
3425 if (status != common::V1_0::Status::OK ) {
3426 return CameraProviderManager::mapToStatusT(status);
3427 }
3428
3429 // And convert output stream configuration from HIDL
3430
3431 for (size_t i = 0; i < config->num_streams; i++) {
Emilian Peevf4816702020-04-03 15:44:51 -07003432 camera3::camera_stream_t *dst = config->streams[i];
Emilian Peev31abd0a2017-05-11 18:37:46 +01003433 int streamId = Camera3Stream::cast(dst)->getId();
3434
3435 // Start scan at i, with the assumption that the stream order matches
3436 size_t realIdx = i;
3437 bool found = false;
Eino-Ville Talvala370875f2019-04-12 12:40:27 -07003438 size_t halStreamCount = finalConfiguration.streams.size();
3439 for (size_t idx = 0; idx < halStreamCount; idx++) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003440 if (finalConfiguration.streams[realIdx].v3_2.id == streamId) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003441 found = true;
3442 break;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003443 }
Eino-Ville Talvala370875f2019-04-12 12:40:27 -07003444 realIdx = (realIdx >= halStreamCount - 1) ? 0 : realIdx + 1;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003445 }
3446 if (!found) {
3447 ALOGE("%s: Stream %d not found in stream configuration response from HAL",
3448 __FUNCTION__, streamId);
3449 return INVALID_OPERATION;
3450 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003451 device::V3_3::HalStream &src = finalConfiguration.streams[realIdx];
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003452 device::V3_6::HalStream &src_36 = finalConfiguration3_6.streams[realIdx];
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003453
Emilian Peev710c1422017-08-30 11:19:38 +01003454 Camera3Stream* dstStream = Camera3Stream::cast(dst);
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003455 int overrideFormat = mapToFrameworkFormat(src.v3_2.overrideFormat);
3456 android_dataspace overrideDataSpace = mapToFrameworkDataspace(src.overrideDataSpace);
3457
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003458 if (mHidlSession_3_6 != nullptr) {
3459 dstStream->setOfflineProcessingSupport(src_36.supportOffline);
3460 }
3461
Yin-Chia Yeh90667662019-07-01 15:45:00 -07003462 if (dstStream->getOriginalFormat() != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Shuzhen Wang92653952019-05-07 15:11:43 -07003463 dstStream->setFormatOverride(false);
3464 dstStream->setDataSpaceOverride(false);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003465 if (dst->format != overrideFormat) {
3466 ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__,
3467 streamId, dst->format);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003468 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003469 if (dst->data_space != overrideDataSpace) {
3470 ALOGE("%s: Stream %d: DataSpace override not allowed for format 0x%x", __FUNCTION__,
3471 streamId, dst->format);
3472 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003473 } else {
Shuzhen Wang92653952019-05-07 15:11:43 -07003474 bool needFormatOverride =
3475 requestedConfiguration3_2.streams[i].format != src.v3_2.overrideFormat;
3476 bool needDataspaceOverride =
3477 requestedConfiguration3_2.streams[i].dataSpace != src.overrideDataSpace;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003478 // Override allowed with IMPLEMENTATION_DEFINED
Shuzhen Wang92653952019-05-07 15:11:43 -07003479 dstStream->setFormatOverride(needFormatOverride);
3480 dstStream->setDataSpaceOverride(needDataspaceOverride);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003481 dst->format = overrideFormat;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003482 dst->data_space = overrideDataSpace;
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003483 }
3484
Emilian Peevf4816702020-04-03 15:44:51 -07003485 if (dst->stream_type == CAMERA_STREAM_INPUT) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003486 if (src.v3_2.producerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003487 ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003488 __FUNCTION__, streamId);
3489 return INVALID_OPERATION;
3490 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003491 dstStream->setUsage(
3492 mapConsumerToFrameworkUsage(src.v3_2.consumerUsage));
Emilian Peev31abd0a2017-05-11 18:37:46 +01003493 } else {
3494 // OUTPUT
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003495 if (src.v3_2.consumerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003496 ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage",
3497 __FUNCTION__, streamId);
3498 return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003499 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003500 dstStream->setUsage(
3501 mapProducerToFrameworkUsage(src.v3_2.producerUsage));
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003502 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003503 dst->max_buffers = src.v3_2.maxBuffers;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003504 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003505
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003506 return res;
3507}
3508
Emilian Peevf4816702020-04-03 15:44:51 -07003509status_t Camera3Device::HalInterface::wrapAsHidlRequest(camera_capture_request_t* request,
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003510 /*out*/device::V3_2::CaptureRequest* captureRequest,
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003511 /*out*/std::vector<native_handle_t*>* handlesCreated,
3512 /*out*/std::vector<std::pair<int32_t, int32_t>>* inflightBuffers) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003513 ATRACE_CALL();
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003514 if (captureRequest == nullptr || handlesCreated == nullptr || inflightBuffers == nullptr) {
3515 ALOGE("%s: captureRequest (%p), handlesCreated (%p), and inflightBuffers(%p) "
3516 "must not be null", __FUNCTION__, captureRequest, handlesCreated, inflightBuffers);
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003517 return BAD_VALUE;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003518 }
3519
3520 captureRequest->frameNumber = request->frame_number;
Yifan Hongf79b5542017-04-11 14:44:25 -07003521
3522 captureRequest->fmqSettingsSize = 0;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003523
3524 {
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003525 if (request->input_buffer != nullptr) {
3526 int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId();
3527 buffer_handle_t buf = *(request->input_buffer->buffer);
3528 auto pair = getBufferId(buf, streamId);
3529 bool isNewBuffer = pair.first;
3530 uint64_t bufferId = pair.second;
3531 captureRequest->inputBuffer.streamId = streamId;
3532 captureRequest->inputBuffer.bufferId = bufferId;
3533 captureRequest->inputBuffer.buffer = (isNewBuffer) ? buf : nullptr;
3534 captureRequest->inputBuffer.status = BufferStatus::OK;
3535 native_handle_t *acquireFence = nullptr;
3536 if (request->input_buffer->acquire_fence != -1) {
3537 acquireFence = native_handle_create(1,0);
3538 acquireFence->data[0] = request->input_buffer->acquire_fence;
3539 handlesCreated->push_back(acquireFence);
3540 }
3541 captureRequest->inputBuffer.acquireFence = acquireFence;
3542 captureRequest->inputBuffer.releaseFence = nullptr;
3543
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003544 mBufferRecords.pushInflightBuffer(captureRequest->frameNumber, streamId,
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003545 request->input_buffer->buffer);
3546 inflightBuffers->push_back(std::make_pair(captureRequest->frameNumber, streamId));
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003547 } else {
3548 captureRequest->inputBuffer.streamId = -1;
3549 captureRequest->inputBuffer.bufferId = BUFFER_ID_NO_BUFFER;
3550 }
3551
3552 captureRequest->outputBuffers.resize(request->num_output_buffers);
3553 for (size_t i = 0; i < request->num_output_buffers; i++) {
Emilian Peevf4816702020-04-03 15:44:51 -07003554 const camera_stream_buffer_t *src = request->output_buffers + i;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003555 StreamBuffer &dst = captureRequest->outputBuffers[i];
3556 int32_t streamId = Camera3Stream::cast(src->stream)->getId();
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003557 if (src->buffer != nullptr) {
3558 buffer_handle_t buf = *(src->buffer);
3559 auto pair = getBufferId(buf, streamId);
3560 bool isNewBuffer = pair.first;
3561 dst.bufferId = pair.second;
3562 dst.buffer = isNewBuffer ? buf : nullptr;
3563 native_handle_t *acquireFence = nullptr;
3564 if (src->acquire_fence != -1) {
3565 acquireFence = native_handle_create(1,0);
3566 acquireFence->data[0] = src->acquire_fence;
3567 handlesCreated->push_back(acquireFence);
3568 }
3569 dst.acquireFence = acquireFence;
3570 } else if (mUseHalBufManager) {
3571 // HAL buffer management path
3572 dst.bufferId = BUFFER_ID_NO_BUFFER;
3573 dst.buffer = nullptr;
3574 dst.acquireFence = nullptr;
3575 } else {
3576 ALOGE("%s: cannot send a null buffer in capture request!", __FUNCTION__);
3577 return BAD_VALUE;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003578 }
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003579 dst.streamId = streamId;
3580 dst.status = BufferStatus::OK;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003581 dst.releaseFence = nullptr;
3582
Yin-Chia Yeh5f840f82019-03-05 11:59:04 -08003583 // Output buffers are empty when using HAL buffer manager
3584 if (!mUseHalBufManager) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003585 mBufferRecords.pushInflightBuffer(
3586 captureRequest->frameNumber, streamId, src->buffer);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003587 inflightBuffers->push_back(std::make_pair(captureRequest->frameNumber, streamId));
Yin-Chia Yeh5f840f82019-03-05 11:59:04 -08003588 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003589 }
3590 }
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003591 return OK;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003592}
3593
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003594void Camera3Device::HalInterface::cleanupNativeHandles(
3595 std::vector<native_handle_t*> *handles, bool closeFd) {
3596 if (handles == nullptr) {
3597 return;
3598 }
3599 if (closeFd) {
3600 for (auto& handle : *handles) {
3601 native_handle_close(handle);
3602 }
3603 }
3604 for (auto& handle : *handles) {
3605 native_handle_delete(handle);
3606 }
3607 handles->clear();
3608 return;
3609}
3610
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003611status_t Camera3Device::HalInterface::processBatchCaptureRequests(
Emilian Peevf4816702020-04-03 15:44:51 -07003612 std::vector<camera_capture_request_t*>& requests,/*out*/uint32_t* numRequestProcessed) {
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003613 ATRACE_NAME("CameraHal::processBatchCaptureRequests");
3614 if (!valid()) return INVALID_OPERATION;
3615
Emilian Peevaebbe412018-01-15 13:53:24 +00003616 sp<device::V3_4::ICameraDeviceSession> hidlSession_3_4;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003617 sp<device::V3_7::ICameraDeviceSession> hidlSession_3_7;
3618 auto castResult_3_7 = device::V3_7::ICameraDeviceSession::castFrom(mHidlSession);
3619 if (castResult_3_7.isOk()) {
3620 hidlSession_3_7 = castResult_3_7;
3621 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003622 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
3623 if (castResult_3_4.isOk()) {
3624 hidlSession_3_4 = castResult_3_4;
3625 }
3626
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003627 hardware::hidl_vec<device::V3_2::CaptureRequest> captureRequests;
Emilian Peevaebbe412018-01-15 13:53:24 +00003628 hardware::hidl_vec<device::V3_4::CaptureRequest> captureRequests_3_4;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003629 hardware::hidl_vec<device::V3_7::CaptureRequest> captureRequests_3_7;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003630 size_t batchSize = requests.size();
Shuzhen Wang83bff122020-11-20 15:51:39 -08003631 if (hidlSession_3_7 != nullptr) {
3632 captureRequests_3_7.resize(batchSize);
3633 } else if (hidlSession_3_4 != nullptr) {
Emilian Peevaebbe412018-01-15 13:53:24 +00003634 captureRequests_3_4.resize(batchSize);
3635 } else {
3636 captureRequests.resize(batchSize);
3637 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003638 std::vector<native_handle_t*> handlesCreated;
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003639 std::vector<std::pair<int32_t, int32_t>> inflightBuffers;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003640
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003641 status_t res = OK;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003642 for (size_t i = 0; i < batchSize; i++) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08003643 if (hidlSession_3_7 != nullptr) {
3644 res = wrapAsHidlRequest(requests[i], /*out*/&captureRequests_3_7[i].v3_4.v3_2,
3645 /*out*/&handlesCreated, /*out*/&inflightBuffers);
3646 } else if (hidlSession_3_4 != nullptr) {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003647 res = wrapAsHidlRequest(requests[i], /*out*/&captureRequests_3_4[i].v3_2,
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003648 /*out*/&handlesCreated, /*out*/&inflightBuffers);
Emilian Peevaebbe412018-01-15 13:53:24 +00003649 } else {
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003650 res = wrapAsHidlRequest(requests[i], /*out*/&captureRequests[i],
3651 /*out*/&handlesCreated, /*out*/&inflightBuffers);
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003652 }
3653 if (res != OK) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003654 mBufferRecords.popInflightBuffers(inflightBuffers);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003655 cleanupNativeHandles(&handlesCreated);
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003656 return res;
Emilian Peevaebbe412018-01-15 13:53:24 +00003657 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003658 }
3659
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003660 std::vector<device::V3_2::BufferCache> cachesToRemove;
3661 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003662 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003663 for (auto& pair : mFreedBuffers) {
3664 // The stream might have been removed since onBufferFreed
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003665 if (mBufferRecords.isStreamCached(pair.first)) {
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003666 cachesToRemove.push_back({pair.first, pair.second});
3667 }
3668 }
3669 mFreedBuffers.clear();
3670 }
3671
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003672 common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
3673 *numRequestProcessed = 0;
Yifan Hongf79b5542017-04-11 14:44:25 -07003674
3675 // Write metadata to FMQ.
3676 for (size_t i = 0; i < batchSize; i++) {
Emilian Peevf4816702020-04-03 15:44:51 -07003677 camera_capture_request_t* request = requests[i];
Emilian Peevaebbe412018-01-15 13:53:24 +00003678 device::V3_2::CaptureRequest* captureRequest;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003679 if (hidlSession_3_7 != nullptr) {
3680 captureRequest = &captureRequests_3_7[i].v3_4.v3_2;
3681 } else if (hidlSession_3_4 != nullptr) {
Emilian Peevaebbe412018-01-15 13:53:24 +00003682 captureRequest = &captureRequests_3_4[i].v3_2;
3683 } else {
3684 captureRequest = &captureRequests[i];
3685 }
Yifan Hongf79b5542017-04-11 14:44:25 -07003686
3687 if (request->settings != nullptr) {
3688 size_t settingsSize = get_camera_metadata_size(request->settings);
3689 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
3690 reinterpret_cast<const uint8_t*>(request->settings), settingsSize)) {
3691 captureRequest->settings.resize(0);
3692 captureRequest->fmqSettingsSize = settingsSize;
3693 } else {
3694 if (mRequestMetadataQueue != nullptr) {
3695 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
3696 }
3697 captureRequest->settings.setToExternal(
3698 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(request->settings)),
3699 get_camera_metadata_size(request->settings));
3700 captureRequest->fmqSettingsSize = 0u;
3701 }
3702 } else {
3703 // A null request settings maps to a size-0 CameraMetadata
3704 captureRequest->settings.resize(0);
3705 captureRequest->fmqSettingsSize = 0u;
3706 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003707
Shuzhen Wang83bff122020-11-20 15:51:39 -08003708 // hidl session 3.7 specific handling.
3709 if (hidlSession_3_7 != nullptr) {
3710 captureRequests_3_7[i].inputWidth = request->input_width;
3711 captureRequests_3_7[i].inputHeight = request->input_height;
3712 }
3713
3714 // hidl session 3.7 and 3.4 specific handling.
3715 if (hidlSession_3_7 != nullptr || hidlSession_3_4 != nullptr) {
3716 hardware::hidl_vec<device::V3_4::PhysicalCameraSetting>& physicalCameraSettings =
3717 (hidlSession_3_7 != nullptr) ?
3718 captureRequests_3_7[i].v3_4.physicalCameraSettings :
3719 captureRequests_3_4[i].physicalCameraSettings;
3720 physicalCameraSettings.resize(request->num_physcam_settings);
Emilian Peevaebbe412018-01-15 13:53:24 +00003721 for (size_t j = 0; j < request->num_physcam_settings; j++) {
Emilian Peev00420d22018-02-05 21:33:13 +00003722 if (request->physcam_settings != nullptr) {
3723 size_t settingsSize = get_camera_metadata_size(request->physcam_settings[j]);
3724 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
3725 reinterpret_cast<const uint8_t*>(request->physcam_settings[j]),
3726 settingsSize)) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08003727 physicalCameraSettings[j].settings.resize(0);
3728 physicalCameraSettings[j].fmqSettingsSize = settingsSize;
Emilian Peev00420d22018-02-05 21:33:13 +00003729 } else {
3730 if (mRequestMetadataQueue != nullptr) {
3731 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
3732 }
Shuzhen Wang83bff122020-11-20 15:51:39 -08003733 physicalCameraSettings[j].settings.setToExternal(
Emilian Peev00420d22018-02-05 21:33:13 +00003734 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(
3735 request->physcam_settings[j])),
3736 get_camera_metadata_size(request->physcam_settings[j]));
Shuzhen Wang83bff122020-11-20 15:51:39 -08003737 physicalCameraSettings[j].fmqSettingsSize = 0u;
Emilian Peevaebbe412018-01-15 13:53:24 +00003738 }
Emilian Peev00420d22018-02-05 21:33:13 +00003739 } else {
Yin-Chia Yeha2849702021-03-10 10:11:33 -08003740 physicalCameraSettings[j].fmqSettingsSize = 0u;
3741 physicalCameraSettings[j].settings.resize(0);
Emilian Peevaebbe412018-01-15 13:53:24 +00003742 }
Shuzhen Wang83bff122020-11-20 15:51:39 -08003743 physicalCameraSettings[j].physicalCameraId = request->physcam_id[j];
Emilian Peevaebbe412018-01-15 13:53:24 +00003744 }
3745 }
Yifan Hongf79b5542017-04-11 14:44:25 -07003746 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003747
3748 hardware::details::return_status err;
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07003749 auto resultCallback =
3750 [&status, &numRequestProcessed] (auto s, uint32_t n) {
3751 status = s;
3752 *numRequestProcessed = n;
3753 };
Shuzhen Wang83bff122020-11-20 15:51:39 -08003754 if (hidlSession_3_7 != nullptr) {
3755 err = hidlSession_3_7->processCaptureRequest_3_7(captureRequests_3_7, cachesToRemove,
3756 resultCallback);
3757 } else if (hidlSession_3_4 != nullptr) {
Emilian Peevaebbe412018-01-15 13:53:24 +00003758 err = hidlSession_3_4->processCaptureRequest_3_4(captureRequests_3_4, cachesToRemove,
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07003759 resultCallback);
Emilian Peevaebbe412018-01-15 13:53:24 +00003760 } else {
3761 err = mHidlSession->processCaptureRequest(captureRequests, cachesToRemove,
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07003762 resultCallback);
Emilian Peevaebbe412018-01-15 13:53:24 +00003763 }
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003764 if (!err.isOk()) {
3765 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003766 status = common::V1_0::Status::CAMERA_DISCONNECTED;
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003767 }
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003768
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003769 if (status == common::V1_0::Status::OK && *numRequestProcessed != batchSize) {
3770 ALOGE("%s: processCaptureRequest returns OK but processed %d/%zu requests",
3771 __FUNCTION__, *numRequestProcessed, batchSize);
3772 status = common::V1_0::Status::INTERNAL_ERROR;
3773 }
3774
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003775 res = CameraProviderManager::mapToStatusT(status);
3776 if (res == OK) {
3777 if (mHidlSession->isRemote()) {
3778 // Only close acquire fence FDs when the HIDL transaction succeeds (so the FDs have been
3779 // sent to camera HAL processes)
3780 cleanupNativeHandles(&handlesCreated, /*closeFd*/true);
3781 } else {
3782 // In passthrough mode the FDs are now owned by HAL
3783 cleanupNativeHandles(&handlesCreated);
3784 }
3785 } else {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003786 mBufferRecords.popInflightBuffers(inflightBuffers);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003787 cleanupNativeHandles(&handlesCreated);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003788 }
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003789 return res;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003790}
3791
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003792status_t Camera3Device::HalInterface::flush() {
3793 ATRACE_NAME("CameraHal::flush");
3794 if (!valid()) return INVALID_OPERATION;
3795 status_t res = OK;
3796
Emilian Peev31abd0a2017-05-11 18:37:46 +01003797 auto err = mHidlSession->flush();
3798 if (!err.isOk()) {
3799 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3800 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003801 } else {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003802 res = CameraProviderManager::mapToStatusT(err);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003803 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003804
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003805 return res;
3806}
3807
Emilian Peev31abd0a2017-05-11 18:37:46 +01003808status_t Camera3Device::HalInterface::dump(int /*fd*/) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003809 ATRACE_NAME("CameraHal::dump");
3810 if (!valid()) return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003811
Emilian Peev31abd0a2017-05-11 18:37:46 +01003812 // Handled by CameraProviderManager::dump
3813
3814 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003815}
3816
3817status_t Camera3Device::HalInterface::close() {
3818 ATRACE_NAME("CameraHal::close()");
3819 if (!valid()) return INVALID_OPERATION;
3820 status_t res = OK;
3821
Emilian Peev31abd0a2017-05-11 18:37:46 +01003822 auto err = mHidlSession->close();
3823 // Interface will be dead shortly anyway, so don't log errors
3824 if (!err.isOk()) {
3825 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003826 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003827
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003828 return res;
3829}
3830
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003831void Camera3Device::HalInterface::signalPipelineDrain(const std::vector<int>& streamIds) {
3832 ATRACE_NAME("CameraHal::signalPipelineDrain");
3833 if (!valid() || mHidlSession_3_5 == nullptr) {
3834 ALOGE("%s called on invalid camera!", __FUNCTION__);
3835 return;
3836 }
3837
Yin-Chia Yehc300a072019-02-13 14:56:57 -08003838 auto err = mHidlSession_3_5->signalStreamFlush(streamIds, mNextStreamConfigCounter - 1);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003839 if (!err.isOk()) {
3840 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3841 return;
3842 }
3843}
3844
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003845status_t Camera3Device::HalInterface::switchToOffline(
3846 const std::vector<int32_t>& streamsToKeep,
3847 /*out*/hardware::camera::device::V3_6::CameraOfflineSessionInfo* offlineSessionInfo,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003848 /*out*/sp<hardware::camera::device::V3_6::ICameraOfflineSession>* offlineSession,
3849 /*out*/camera3::BufferRecords* bufferRecords) {
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003850 ATRACE_NAME("CameraHal::switchToOffline");
3851 if (!valid() || mHidlSession_3_6 == nullptr) {
3852 ALOGE("%s called on invalid camera!", __FUNCTION__);
3853 return INVALID_OPERATION;
3854 }
3855
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003856 if (offlineSessionInfo == nullptr || offlineSession == nullptr || bufferRecords == nullptr) {
3857 ALOGE("%s: output arguments must not be null!", __FUNCTION__);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003858 return INVALID_OPERATION;
3859 }
3860
3861 common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003862 auto resultCallback =
3863 [&status, &offlineSessionInfo, &offlineSession] (auto s, auto info, auto session) {
3864 status = s;
3865 *offlineSessionInfo = info;
3866 *offlineSession = session;
3867 };
3868 auto err = mHidlSession_3_6->switchToOffline(streamsToKeep, resultCallback);
3869
3870 if (!err.isOk()) {
3871 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3872 return DEAD_OBJECT;
3873 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003874
3875 status_t ret = CameraProviderManager::mapToStatusT(status);
3876 if (ret != OK) {
3877 return ret;
3878 }
3879
3880 // TODO: assert no ongoing requestBuffer/returnBuffer call here
3881 // TODO: update RequestBufferStateMachine to block requestBuffer/returnBuffer once HAL
3882 // returns from switchToOffline.
3883
3884
3885 // Validate buffer caches
3886 std::vector<int32_t> streams;
3887 streams.reserve(offlineSessionInfo->offlineStreams.size());
3888 for (auto offlineStream : offlineSessionInfo->offlineStreams) {
3889 int32_t id = offlineStream.id;
3890 streams.push_back(id);
3891 // Verify buffer caches
3892 std::vector<uint64_t> bufIds(offlineStream.circulatingBufferIds.begin(),
3893 offlineStream.circulatingBufferIds.end());
3894 if (!verifyBufferIds(id, bufIds)) {
3895 ALOGE("%s: stream ID %d buffer cache records mismatch!", __FUNCTION__, id);
3896 return UNKNOWN_ERROR;
3897 }
3898 }
3899
3900 // Move buffer records
3901 bufferRecords->takeBufferCaches(mBufferRecords, streams);
3902 bufferRecords->takeInflightBufferMap(mBufferRecords);
3903 bufferRecords->takeRequestedBufferMap(mBufferRecords);
3904 return ret;
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003905}
3906
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003907void Camera3Device::HalInterface::getInflightBufferKeys(
3908 std::vector<std::pair<int32_t, int32_t>>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003909 mBufferRecords.getInflightBufferKeys(out);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003910 return;
3911}
3912
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08003913void Camera3Device::HalInterface::getInflightRequestBufferKeys(
3914 std::vector<uint64_t>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003915 mBufferRecords.getInflightRequestBufferKeys(out);
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08003916 return;
3917}
3918
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003919bool Camera3Device::HalInterface::verifyBufferIds(
3920 int32_t streamId, std::vector<uint64_t>& bufIds) {
3921 return mBufferRecords.verifyBufferIds(streamId, bufIds);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003922}
3923
3924status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003925 int32_t frameNumber, int32_t streamId,
3926 /*out*/ buffer_handle_t **buffer) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003927 return mBufferRecords.popInflightBuffer(frameNumber, streamId, buffer);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003928}
3929
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003930status_t Camera3Device::HalInterface::pushInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08003931 uint64_t bufferId, buffer_handle_t* buf, int32_t streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003932 return mBufferRecords.pushInflightRequestBuffer(bufferId, buf, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003933}
3934
3935// Find and pop a buffer_handle_t based on bufferId
3936status_t Camera3Device::HalInterface::popInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08003937 uint64_t bufferId,
3938 /*out*/ buffer_handle_t** buffer,
3939 /*optional out*/ int32_t* streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003940 return mBufferRecords.popInflightRequestBuffer(bufferId, buffer, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003941}
3942
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003943std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
3944 const buffer_handle_t& buf, int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003945 return mBufferRecords.getBufferId(buf, streamId);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003946}
3947
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003948void Camera3Device::HalInterface::onBufferFreed(
3949 int streamId, const native_handle_t* handle) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003950 uint32_t bufferId = mBufferRecords.removeOneBufferCache(streamId, handle);
3951 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
3952 if (bufferId != BUFFER_ID_NO_BUFFER) {
3953 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003954 }
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003955}
3956
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07003957void Camera3Device::HalInterface::onStreamReConfigured(int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003958 std::vector<uint64_t> bufIds = mBufferRecords.clearBufferCaches(streamId);
3959 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
3960 for (auto bufferId : bufIds) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07003961 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
3962 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07003963}
3964
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003965/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003966 * RequestThread inner class methods
3967 */
3968
3969Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003970 sp<StatusTracker> statusTracker,
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003971 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys,
3972 bool useHalBufManager) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003973 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003974 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003975 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003976 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07003977 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003978 mId(getId(parent)),
Shuzhen Wang316781a2020-08-18 18:11:01 -07003979 mFirstRepeating(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003980 mReconfigured(false),
3981 mDoPause(false),
3982 mPaused(true),
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003983 mNotifyPipelineDrain(false),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003984 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07003985 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003986 mCurrentAfTriggerId(0),
3987 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003988 mRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE),
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003989 mCameraMute(false),
3990 mCameraMuteChanged(false),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003991 mRepeatingLastFrameNumber(
3992 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07003993 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003994 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003995 mRequestLatency(kRequestLatencyBinSize),
3996 mSessionParamKeys(sessionParamKeys),
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003997 mLatestSessionParams(sessionParamKeys.size()),
3998 mUseHalBufManager(useHalBufManager) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07003999 mStatusId = statusTracker->addComponent("RequestThread");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004000}
4001
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004002Camera3Device::RequestThread::~RequestThread() {}
4003
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004004void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004005 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004006 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004007 Mutex::Autolock l(mRequestLock);
4008 mListener = listener;
4009}
4010
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004011void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
4012 const CameraMetadata& sessionParams) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004013 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004014 Mutex::Autolock l(mRequestLock);
4015 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004016 mLatestSessionParams = sessionParams;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07004017 // Prepare video stream for high speed recording.
4018 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01004019 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004020}
4021
Jianing Wei90e59c92014-03-12 18:29:36 -07004022status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004023 List<sp<CaptureRequest> > &requests,
4024 /*out*/
4025 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004026 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07004027 Mutex::Autolock l(mRequestLock);
4028 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
4029 ++it) {
4030 mRequestQueue.push_back(*it);
4031 }
4032
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004033 if (lastFrameNumber != NULL) {
4034 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
4035 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
4036 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
4037 *lastFrameNumber);
4038 }
Jianing Weicb0652e2014-03-12 18:29:36 -07004039
Jianing Wei90e59c92014-03-12 18:29:36 -07004040 unpauseForNewRequests();
4041
4042 return OK;
4043}
4044
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004045
4046status_t Camera3Device::RequestThread::queueTrigger(
4047 RequestTrigger trigger[],
4048 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004049 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004050 Mutex::Autolock l(mTriggerMutex);
4051 status_t ret;
4052
4053 for (size_t i = 0; i < count; ++i) {
4054 ret = queueTriggerLocked(trigger[i]);
4055
4056 if (ret != OK) {
4057 return ret;
4058 }
4059 }
4060
4061 return OK;
4062}
4063
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004064const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
4065 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004066 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004067 if (d != nullptr) return d->mId;
4068 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004069}
4070
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004071status_t Camera3Device::RequestThread::queueTriggerLocked(
4072 RequestTrigger trigger) {
4073
4074 uint32_t tag = trigger.metadataTag;
4075 ssize_t index = mTriggerMap.indexOfKey(tag);
4076
4077 switch (trigger.getTagType()) {
4078 case TYPE_BYTE:
4079 // fall-through
4080 case TYPE_INT32:
4081 break;
4082 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004083 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
4084 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004085 return INVALID_OPERATION;
4086 }
4087
4088 /**
4089 * Collect only the latest trigger, since we only have 1 field
4090 * in the request settings per trigger tag, and can't send more than 1
4091 * trigger per request.
4092 */
4093 if (index != NAME_NOT_FOUND) {
4094 mTriggerMap.editValueAt(index) = trigger;
4095 } else {
4096 mTriggerMap.add(tag, trigger);
4097 }
4098
4099 return OK;
4100}
4101
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004102status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004103 const RequestList &requests,
4104 /*out*/
4105 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004106 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004107 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004108 if (lastFrameNumber != NULL) {
4109 *lastFrameNumber = mRepeatingLastFrameNumber;
4110 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004111 mRepeatingRequests.clear();
Shuzhen Wang316781a2020-08-18 18:11:01 -07004112 mFirstRepeating = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004113 mRepeatingRequests.insert(mRepeatingRequests.begin(),
4114 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004115
4116 unpauseForNewRequests();
4117
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004118 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004119 return OK;
4120}
4121
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07004122bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004123 if (mRepeatingRequests.empty()) {
4124 return false;
4125 }
4126 int32_t requestId = requestIn->mResultExtras.requestId;
4127 const RequestList &repeatRequests = mRepeatingRequests;
4128 // All repeating requests are guaranteed to have same id so only check first quest
4129 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
4130 return (firstRequest->mResultExtras.requestId == requestId);
4131}
4132
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004133status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004134 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004135 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004136 return clearRepeatingRequestsLocked(lastFrameNumber);
4137
4138}
4139
4140status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004141 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004142 if (lastFrameNumber != NULL) {
4143 *lastFrameNumber = mRepeatingLastFrameNumber;
4144 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004145 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004146 return OK;
4147}
4148
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004149status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004150 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004151 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004152 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004153 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004154
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004155 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004156
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004157 // Send errors for all requests pending in the request queue, including
4158 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004159 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004160 if (listener != NULL) {
4161 for (RequestList::iterator it = mRequestQueue.begin();
4162 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004163 // Abort the input buffers for reprocess requests.
4164 if ((*it)->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07004165 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08004166 camera3::Size inputBufferSize;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07004167 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08004168 &inputBufferSize, /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004169 if (res != OK) {
4170 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
4171 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
4172 } else {
Emilian Peevf4816702020-04-03 15:44:51 -07004173 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004174 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
4175 if (res != OK) {
4176 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
4177 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
4178 }
4179 }
4180 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004181 // Set the frame number this request would have had, if it
4182 // had been submitted; this frame number will not be reused.
4183 // The requestId and burstId fields were set when the request was
4184 // submitted originally (in convertMetadataListToRequestListLocked)
4185 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004186 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004187 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004188 }
4189 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004190 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08004191
4192 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004193 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004194 if (lastFrameNumber != NULL) {
4195 *lastFrameNumber = mRepeatingLastFrameNumber;
4196 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004197 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Emilian Peev8dae54c2019-12-02 15:17:17 -08004198 mRequestSignal.signal();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004199 return OK;
4200}
4201
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004202status_t Camera3Device::RequestThread::flush() {
4203 ATRACE_CALL();
4204 Mutex::Autolock l(mFlushLock);
4205
Emilian Peev08dd2452017-04-06 16:55:14 +01004206 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004207}
4208
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004209void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004210 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004211 Mutex::Autolock l(mPauseLock);
4212 mDoPause = paused;
4213 mDoPauseSignal.signal();
4214}
4215
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004216status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
4217 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004218 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004219 Mutex::Autolock l(mLatestRequestMutex);
4220 status_t res;
4221 while (mLatestRequestId != requestId) {
4222 nsecs_t startTime = systemTime();
4223
4224 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
4225 if (res != OK) return res;
4226
4227 timeout -= (systemTime() - startTime);
4228 }
4229
4230 return OK;
4231}
4232
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004233void Camera3Device::RequestThread::requestExit() {
4234 // Call parent to set up shutdown
4235 Thread::requestExit();
4236 // The exit from any possible waits
4237 mDoPauseSignal.signal();
4238 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07004239
4240 mRequestLatency.log("ProcessCaptureRequest latency histogram");
4241 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004242}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004243
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004244void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004245 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004246 bool surfaceAbandoned = false;
4247 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004248 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004249 {
4250 Mutex::Autolock l(mRequestLock);
4251 // Check all streams needed by repeating requests are still valid. Otherwise, stop
4252 // repeating requests.
4253 for (const auto& request : mRepeatingRequests) {
4254 for (const auto& s : request->mOutputStreams) {
4255 if (s->isAbandoned()) {
4256 surfaceAbandoned = true;
4257 clearRepeatingRequestsLocked(&lastFrameNumber);
4258 break;
4259 }
4260 }
4261 if (surfaceAbandoned) {
4262 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004263 }
4264 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004265 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004266 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004267
4268 if (listener != NULL && surfaceAbandoned) {
4269 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004270 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004271}
4272
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004273bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004274 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004275 status_t res;
4276 size_t batchSize = mNextRequests.size();
Emilian Peevf4816702020-04-03 15:44:51 -07004277 std::vector<camera_capture_request_t*> requests(batchSize);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004278 uint32_t numRequestProcessed = 0;
4279 for (size_t i = 0; i < batchSize; i++) {
4280 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07004281 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004282 }
4283
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004284 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
4285
4286 bool triggerRemoveFailed = false;
4287 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
4288 for (size_t i = 0; i < numRequestProcessed; i++) {
4289 NextRequest& nextRequest = mNextRequests.editItemAt(i);
4290 nextRequest.submitted = true;
4291
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004292 updateNextRequest(nextRequest);
Emilian Peevaebbe412018-01-15 13:53:24 +00004293
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004294 if (!triggerRemoveFailed) {
4295 // Remove any previously queued triggers (after unlock)
4296 status_t removeTriggerRes = removeTriggers(mPrevRequest);
4297 if (removeTriggerRes != OK) {
4298 triggerRemoveFailed = true;
4299 triggerFailedRequest = nextRequest;
4300 }
4301 }
4302 }
4303
4304 if (triggerRemoveFailed) {
4305 SET_ERR("RequestThread: Unable to remove triggers "
4306 "(capture request %d, HAL device: %s (%d)",
4307 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
4308 cleanUpFailedRequests(/*sendRequestError*/ false);
4309 return false;
4310 }
4311
4312 if (res != OK) {
4313 // Should only get a failure here for malformed requests or device-level
4314 // errors, so consider all errors fatal. Bad metadata failures should
4315 // come through notify.
4316 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
4317 mNextRequests[numRequestProcessed].halRequest.frame_number,
4318 strerror(-res), res);
4319 cleanUpFailedRequests(/*sendRequestError*/ false);
4320 return false;
4321 }
4322 return true;
4323}
4324
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004325nsecs_t Camera3Device::RequestThread::calculateMaxExpectedDuration(const camera_metadata_t *request) {
4326 nsecs_t maxExpectedDuration = kDefaultExpectedDuration;
4327 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4328 find_camera_metadata_ro_entry(request,
4329 ANDROID_CONTROL_AE_MODE,
4330 &e);
4331 if (e.count == 0) return maxExpectedDuration;
4332
4333 switch (e.data.u8[0]) {
4334 case ANDROID_CONTROL_AE_MODE_OFF:
4335 find_camera_metadata_ro_entry(request,
4336 ANDROID_SENSOR_EXPOSURE_TIME,
4337 &e);
4338 if (e.count > 0) {
4339 maxExpectedDuration = e.data.i64[0];
4340 }
4341 find_camera_metadata_ro_entry(request,
4342 ANDROID_SENSOR_FRAME_DURATION,
4343 &e);
4344 if (e.count > 0) {
4345 maxExpectedDuration = std::max(e.data.i64[0], maxExpectedDuration);
4346 }
4347 break;
4348 default:
4349 find_camera_metadata_ro_entry(request,
4350 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
4351 &e);
4352 if (e.count > 1) {
4353 maxExpectedDuration = 1e9 / e.data.u8[0];
4354 }
4355 break;
4356 }
4357
4358 return maxExpectedDuration;
4359}
4360
Emilian Peeva14b4dd2018-05-15 11:00:31 +01004361bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
4362 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
4363 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
4364 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
4365 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
4366 return true;
4367 }
4368
4369 return false;
4370}
4371
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004372void Camera3Device::RequestThread::updateNextRequest(NextRequest& nextRequest) {
4373 // Update the latest request sent to HAL
Shuzhen Wang83bff122020-11-20 15:51:39 -08004374 camera_capture_request_t& halRequest = nextRequest.halRequest;
4375 if (halRequest.settings != NULL) { // Don't update if they were unchanged
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004376 Mutex::Autolock al(mLatestRequestMutex);
4377
Shuzhen Wang83bff122020-11-20 15:51:39 -08004378 camera_metadata_t* cloned = clone_camera_metadata(halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004379 mLatestRequest.acquire(cloned);
4380
4381 mLatestPhysicalRequest.clear();
Shuzhen Wang83bff122020-11-20 15:51:39 -08004382 for (uint32_t i = 0; i < halRequest.num_physcam_settings; i++) {
4383 cloned = clone_camera_metadata(halRequest.physcam_settings[i]);
4384 mLatestPhysicalRequest.emplace(halRequest.physcam_id[i],
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004385 CameraMetadata(cloned));
4386 }
4387
4388 sp<Camera3Device> parent = mParent.promote();
4389 if (parent != NULL) {
4390 parent->monitorMetadata(TagMonitor::REQUEST,
Shuzhen Wang83bff122020-11-20 15:51:39 -08004391 halRequest.frame_number,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004392 0, mLatestRequest, mLatestPhysicalRequest);
4393 }
4394 }
4395
Shuzhen Wang83bff122020-11-20 15:51:39 -08004396 if (halRequest.settings != NULL) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004397 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
Shuzhen Wang83bff122020-11-20 15:51:39 -08004398 halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004399 }
4400
Shuzhen Wang83bff122020-11-20 15:51:39 -08004401 cleanupPhysicalSettings(nextRequest.captureRequest, &halRequest);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004402}
4403
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004404bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
4405 ATRACE_CALL();
4406 bool updatesDetected = false;
4407
Emilian Peev4ec17882019-01-24 17:16:58 -08004408 CameraMetadata updatedParams(mLatestSessionParams);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004409 for (auto tag : mSessionParamKeys) {
4410 camera_metadata_ro_entry entry = settings.find(tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08004411 camera_metadata_entry lastEntry = updatedParams.find(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004412
4413 if (entry.count > 0) {
4414 bool isDifferent = false;
4415 if (lastEntry.count > 0) {
4416 // Have a last value, compare to see if changed
4417 if (lastEntry.type == entry.type &&
4418 lastEntry.count == entry.count) {
4419 // Same type and count, compare values
4420 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
4421 size_t entryBytes = bytesPerValue * lastEntry.count;
4422 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
4423 if (cmp != 0) {
4424 isDifferent = true;
4425 }
4426 } else {
4427 // Count or type has changed
4428 isDifferent = true;
4429 }
4430 } else {
4431 // No last entry, so always consider to be different
4432 isDifferent = true;
4433 }
4434
4435 if (isDifferent) {
4436 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01004437 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
4438 updatesDetected = true;
4439 }
Emilian Peev4ec17882019-01-24 17:16:58 -08004440 updatedParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004441 }
4442 } else if (lastEntry.count > 0) {
4443 // Value has been removed
4444 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08004445 updatedParams.erase(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004446 updatesDetected = true;
4447 }
4448 }
4449
Emilian Peev4ec17882019-01-24 17:16:58 -08004450 bool reconfigureRequired;
4451 if (updatesDetected) {
4452 reconfigureRequired = mInterface->isReconfigurationRequired(mLatestSessionParams,
4453 updatedParams);
4454 mLatestSessionParams = updatedParams;
4455 } else {
4456 reconfigureRequired = false;
4457 }
4458
4459 return reconfigureRequired;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004460}
4461
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004462bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004463 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004464 status_t res;
Jayant Chowdhary37eca242019-11-18 08:55:56 -08004465 // Any function called from threadLoop() must not hold mInterfaceLock since
4466 // it could lead to deadlocks (disconnect() -> hold mInterfaceMutex -> wait for request thread
4467 // to finish -> request thread waits on mInterfaceMutex) http://b/143513518
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004468
4469 // Handle paused state.
4470 if (waitIfPaused()) {
4471 return true;
4472 }
4473
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004474 // Wait for the next batch of requests.
4475 waitForNextRequestBatch();
4476 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004477 return true;
4478 }
4479
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004480 // Get the latest request ID, if any
4481 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004482 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00004483 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004484 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004485 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004486 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004487 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
4488 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004489 }
4490
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004491 // 'mNextRequests' will at this point contain either a set of HFR batched requests
4492 // or a single request from streaming or burst. In either case the first element
4493 // should contain the latest camera settings that we need to check for any session
4494 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00004495 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004496 res = OK;
4497
4498 //Input stream buffers are already acquired at this point so an input stream
4499 //will not be able to move to idle state unless we force it.
4500 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
4501 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
4502 if (res != OK) {
4503 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
4504 cleanUpFailedRequests(/*sendRequestError*/ false);
4505 return false;
4506 }
4507 }
4508
4509 if (res == OK) {
Emilian Peev3bead5f2020-05-28 17:29:08 -07004510 sp<Camera3Device> parent = mParent.promote();
4511 if (parent != nullptr) {
4512 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams, mStatusId);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004513 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07004514 setPaused(false);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004515
4516 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
4517 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
4518 if (res != OK) {
4519 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
4520 cleanUpFailedRequests(/*sendRequestError*/ false);
4521 return false;
4522 }
4523 }
4524 }
4525 }
4526
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004527 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004528 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004529 if (res == TIMED_OUT) {
4530 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004531 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004532 // Check if any stream is abandoned.
4533 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004534 return true;
4535 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004536 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004537 return false;
4538 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004539
Zhijun Hecc27e112013-10-03 16:12:43 -07004540 // Inform waitUntilRequestProcessed thread of a new request ID
4541 {
4542 Mutex::Autolock al(mLatestRequestMutex);
4543
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004544 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07004545 mLatestRequestSignal.signal();
4546 }
4547
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004548 // Submit a batch of requests to HAL.
4549 // Use flush lock only when submitting multilple requests in a batch.
4550 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
4551 // which may take a long time to finish so synchronizing flush() and
4552 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
4553 // For now, only synchronize for high speed recording and we should figure something out for
4554 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004555 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07004556
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004557 if (useFlushLock) {
4558 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004559 }
4560
Zhijun Hef0645c12016-08-02 00:58:11 -07004561 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004562 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07004563
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08004564 sp<Camera3Device> parent = mParent.promote();
4565 if (parent != nullptr) {
4566 parent->mRequestBufferSM.onSubmittingRequest();
4567 }
4568
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004569 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07004570 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh11648852019-05-16 10:42:54 -07004571 submitRequestSuccess = sendRequestsBatch();
4572
Shuzhen Wang686f6442017-06-20 16:16:04 -07004573 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
4574 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07004575
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004576 if (useFlushLock) {
4577 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004578 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004579
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004580 // Unset as current request
4581 {
4582 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004583 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004584 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07004585 mRequestSubmittedSignal.signal();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004586
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004587 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004588}
4589
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004590status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004591 ATRACE_CALL();
4592
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07004593 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
Shuzhen Wang4a472662017-02-26 23:29:04 -08004594 for (size_t i = 0; i < mNextRequests.size(); i++) {
4595 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004596 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07004597 camera_capture_request_t* halRequest = &nextRequest.halRequest;
4598 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004599
4600 // Prepare a request to HAL
4601 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
4602
4603 // Insert any queued triggers (before metadata is locked)
4604 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004605 if (res < 0) {
4606 SET_ERR("RequestThread: Unable to insert triggers "
4607 "(capture request %d, HAL device: %s (%d)",
4608 halRequest->frame_number, strerror(-res), res);
4609 return INVALID_OPERATION;
4610 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07004611
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004612 int triggerCount = res;
4613 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
4614 mPrevTriggers = triggerCount;
4615
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004616 bool rotateAndCropChanged = overrideAutoRotateAndCrop(captureRequest);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004617 bool testPatternChanged = overrideTestPattern(captureRequest);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004618
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004619 // If the request is the same as last, or we had triggers now or last time or
4620 // changing overrides this time
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004621 bool newRequest =
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004622 (mPrevRequest != captureRequest || triggersMixedIn ||
4623 rotateAndCropChanged || testPatternChanged) &&
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07004624 // Request settings are all the same within one batch, so only treat the first
4625 // request in a batch as new
Zhijun He54c36822018-07-18 09:33:39 -07004626 !(batchedRequest && i > 0);
Emilian Peev00420d22018-02-05 21:33:13 +00004627 if (newRequest) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08004628 std::set<std::string> cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004629 /**
4630 * HAL workaround:
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004631 * Insert a fake trigger ID if a trigger is set but no trigger ID is
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004632 */
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004633 res = addFakeTriggerIds(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004634 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004635 SET_ERR("RequestThread: Unable to insert fake trigger IDs "
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004636 "(capture request %d, HAL device: %s (%d)",
4637 halRequest->frame_number, strerror(-res), res);
4638 return INVALID_OPERATION;
4639 }
4640
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07004641 {
4642 // Correct metadata regions for distortion correction if enabled
4643 sp<Camera3Device> parent = mParent.promote();
4644 if (parent != nullptr) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07004645 List<PhysicalCameraSettings>::iterator it;
4646 for (it = captureRequest->mSettingsList.begin();
4647 it != captureRequest->mSettingsList.end(); it++) {
4648 if (parent->mDistortionMappers.find(it->cameraId) ==
4649 parent->mDistortionMappers.end()) {
4650 continue;
4651 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07004652
4653 if (!captureRequest->mDistortionCorrectionUpdated) {
4654 res = parent->mDistortionMappers[it->cameraId].correctCaptureRequest(
4655 &(it->metadata));
4656 if (res != OK) {
4657 SET_ERR("RequestThread: Unable to correct capture requests "
4658 "for lens distortion for request %d: %s (%d)",
4659 halRequest->frame_number, strerror(-res), res);
4660 return INVALID_OPERATION;
4661 }
4662 captureRequest->mDistortionCorrectionUpdated = true;
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07004663 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07004664 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08004665
4666 for (it = captureRequest->mSettingsList.begin();
4667 it != captureRequest->mSettingsList.end(); it++) {
4668 if (parent->mZoomRatioMappers.find(it->cameraId) ==
4669 parent->mZoomRatioMappers.end()) {
4670 continue;
4671 }
4672
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07004673 if (!captureRequest->mZoomRatioIs1x) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08004674 cameraIdsWithZoom.insert(it->cameraId);
4675 }
4676
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07004677 if (!captureRequest->mZoomRatioUpdated) {
4678 res = parent->mZoomRatioMappers[it->cameraId].updateCaptureRequest(
4679 &(it->metadata));
4680 if (res != OK) {
4681 SET_ERR("RequestThread: Unable to correct capture requests "
4682 "for zoom ratio for request %d: %s (%d)",
4683 halRequest->frame_number, strerror(-res), res);
4684 return INVALID_OPERATION;
4685 }
4686 captureRequest->mZoomRatioUpdated = true;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08004687 }
4688 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07004689 if (captureRequest->mRotateAndCropAuto &&
4690 !captureRequest->mRotationAndCropUpdated) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004691 for (it = captureRequest->mSettingsList.begin();
4692 it != captureRequest->mSettingsList.end(); it++) {
4693 auto mapper = parent->mRotateAndCropMappers.find(it->cameraId);
4694 if (mapper != parent->mRotateAndCropMappers.end()) {
4695 res = mapper->second.updateCaptureRequest(&(it->metadata));
4696 if (res != OK) {
4697 SET_ERR("RequestThread: Unable to correct capture requests "
4698 "for rotate-and-crop for request %d: %s (%d)",
4699 halRequest->frame_number, strerror(-res), res);
4700 return INVALID_OPERATION;
4701 }
4702 }
4703 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07004704 captureRequest->mRotationAndCropUpdated = true;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004705 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07004706 }
4707 }
4708
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004709 /**
4710 * The request should be presorted so accesses in HAL
4711 * are O(logn). Sidenote, sorting a sorted metadata is nop.
4712 */
Emilian Peevaebbe412018-01-15 13:53:24 +00004713 captureRequest->mSettingsList.begin()->metadata.sort();
4714 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004715 mPrevRequest = captureRequest;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08004716 mPrevCameraIdsWithZoom = cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004717 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
4718
4719 IF_ALOGV() {
4720 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4721 find_camera_metadata_ro_entry(
4722 halRequest->settings,
4723 ANDROID_CONTROL_AF_TRIGGER,
4724 &e
4725 );
4726 if (e.count > 0) {
4727 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
4728 __FUNCTION__,
4729 halRequest->frame_number,
4730 e.data.u8[0]);
4731 }
4732 }
4733 } else {
4734 // leave request.settings NULL to indicate 'reuse latest given'
4735 ALOGVV("%s: Request settings are REUSED",
4736 __FUNCTION__);
4737 }
4738
Emilian Peevaebbe412018-01-15 13:53:24 +00004739 if (captureRequest->mSettingsList.size() > 1) {
4740 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
4741 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00004742 if (newRequest) {
4743 halRequest->physcam_settings =
4744 new const camera_metadata* [halRequest->num_physcam_settings];
4745 } else {
4746 halRequest->physcam_settings = nullptr;
4747 }
Emilian Peevaebbe412018-01-15 13:53:24 +00004748 auto it = ++captureRequest->mSettingsList.begin();
4749 size_t i = 0;
4750 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
4751 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00004752 if (newRequest) {
4753 it->metadata.sort();
4754 halRequest->physcam_settings[i] = it->metadata.getAndLock();
4755 }
Emilian Peevaebbe412018-01-15 13:53:24 +00004756 }
4757 }
4758
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004759 uint32_t totalNumBuffers = 0;
4760
4761 // Fill in buffers
4762 if (captureRequest->mInputStream != NULL) {
4763 halRequest->input_buffer = &captureRequest->mInputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08004764
4765 halRequest->input_width = captureRequest->mInputBufferSize.width;
4766 halRequest->input_height = captureRequest->mInputBufferSize.height;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004767 totalNumBuffers += 1;
4768 } else {
4769 halRequest->input_buffer = NULL;
4770 }
4771
Emilian Peevf4816702020-04-03 15:44:51 -07004772 outputBuffers->insertAt(camera_stream_buffer_t(), 0,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004773 captureRequest->mOutputStreams.size());
4774 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang5c22c152017-12-31 17:12:25 -08004775 std::set<String8> requestedPhysicalCameras;
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07004776
4777 sp<Camera3Device> parent = mParent.promote();
4778 if (parent == NULL) {
4779 // Should not happen, and nowhere to send errors to, so just log it
4780 CLOGE("RequestThread: Parent is gone");
4781 return INVALID_OPERATION;
4782 }
4783 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
4784
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004785 SurfaceMap uniqueSurfaceIdMap;
Shuzhen Wang4a472662017-02-26 23:29:04 -08004786 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004787 sp<Camera3OutputStreamInterface> outputStream =
4788 captureRequest->mOutputStreams.editItemAt(j);
4789 int streamId = outputStream->getId();
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07004790
4791 // Prepare video buffers for high speed recording on the first video request.
4792 if (mPrepareVideoStream && outputStream->isVideoStream()) {
4793 // Only try to prepare video stream on the first video request.
4794 mPrepareVideoStream = false;
4795
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004796 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
4797 false /*blockRequest*/);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07004798 while (res == NOT_ENOUGH_DATA) {
4799 res = outputStream->prepareNextBuffer();
4800 }
4801 if (res != OK) {
4802 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
4803 __FUNCTION__, strerror(-res), res);
4804 outputStream->cancelPrepare();
4805 }
4806 }
4807
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004808 std::vector<size_t> uniqueSurfaceIds;
4809 res = outputStream->getUniqueSurfaceIds(
4810 captureRequest->mOutputSurfaces[streamId],
4811 &uniqueSurfaceIds);
4812 // INVALID_OPERATION is normal output for streams not supporting surfaceIds
4813 if (res != OK && res != INVALID_OPERATION) {
4814 ALOGE("%s: failed to query stream %d unique surface IDs",
4815 __FUNCTION__, streamId);
4816 return res;
4817 }
4818 if (res == OK) {
4819 uniqueSurfaceIdMap.insert({streamId, std::move(uniqueSurfaceIds)});
4820 }
4821
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004822 if (mUseHalBufManager) {
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08004823 if (outputStream->isAbandoned()) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07004824 ALOGV("%s: stream %d is abandoned, skipping request", __FUNCTION__, streamId);
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08004825 return TIMED_OUT;
4826 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004827 // HAL will request buffer through requestStreamBuffer API
Emilian Peevf4816702020-04-03 15:44:51 -07004828 camera_stream_buffer_t& buffer = outputBuffers->editItemAt(j);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004829 buffer.stream = outputStream->asHalStream();
4830 buffer.buffer = nullptr;
Emilian Peevf4816702020-04-03 15:44:51 -07004831 buffer.status = CAMERA_BUFFER_STATUS_OK;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004832 buffer.acquire_fence = -1;
4833 buffer.release_fence = -1;
Emilian Peevf0348ae2021-01-13 13:39:45 -08004834 // Mark the output stream as unpreparable to block clients from calling
4835 // 'prepare' after this request reaches CameraHal and before the respective
4836 // buffers are requested.
4837 outputStream->markUnpreparable();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004838 } else {
4839 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
4840 waitDuration,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004841 captureRequest->mOutputSurfaces[streamId]);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004842 if (res != OK) {
4843 // Can't get output buffer from gralloc queue - this could be due to
4844 // abandoned queue or other consumer misbehavior, so not a fatal
4845 // error
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07004846 ALOGV("RequestThread: Can't get output buffer, skipping request:"
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004847 " %s (%d)", strerror(-res), res);
4848
4849 return TIMED_OUT;
4850 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004851 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08004852
4853 {
4854 sp<Camera3Device> parent = mParent.promote();
4855 if (parent != nullptr) {
4856 const String8& streamCameraId = outputStream->getPhysicalCameraId();
4857 for (const auto& settings : captureRequest->mSettingsList) {
4858 if ((streamCameraId.isEmpty() &&
4859 parent->getId() == settings.cameraId.c_str()) ||
4860 streamCameraId == settings.cameraId.c_str()) {
4861 outputStream->fireBufferRequestForFrameNumber(
4862 captureRequest->mResultExtras.frameNumber,
4863 settings.metadata);
4864 }
4865 }
4866 }
4867 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07004868
Shuzhen Wang5c22c152017-12-31 17:12:25 -08004869 String8 physicalCameraId = outputStream->getPhysicalCameraId();
Shuzhen Wang5c22c152017-12-31 17:12:25 -08004870 if (!physicalCameraId.isEmpty()) {
Shuzhen Wang5c22c152017-12-31 17:12:25 -08004871 requestedPhysicalCameras.insert(physicalCameraId);
4872 }
4873 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004874 }
4875 totalNumBuffers += halRequest->num_output_buffers;
4876
4877 // Log request in the in-flight queue
Shuzhen Wang4a472662017-02-26 23:29:04 -08004878 // If this request list is for constrained high speed recording (not
4879 // preview), and the current request is not the last one in the batch,
4880 // do not send callback to the app.
4881 bool hasCallback = true;
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07004882 if (batchedRequest && i != mNextRequests.size()-1) {
Shuzhen Wang4a472662017-02-26 23:29:04 -08004883 hasCallback = false;
4884 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01004885 bool isStillCapture = false;
Shuzhen Wang26abaf42018-08-28 15:41:20 -07004886 bool isZslCapture = false;
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004887 const camera_metadata_t* settings = halRequest->settings;
4888 bool shouldUnlockSettings = false;
4889 if (settings == nullptr) {
4890 shouldUnlockSettings = true;
4891 settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
4892 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01004893 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
4894 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004895 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
Emilian Peev9dd21f42018-08-03 13:39:29 +01004896 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
4897 isStillCapture = true;
4898 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
4899 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07004900
Emilian Peevaf8416e2021-02-04 17:52:43 -08004901 e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004902 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
Shuzhen Wang26abaf42018-08-28 15:41:20 -07004903 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
4904 isZslCapture = true;
4905 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01004906 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004907 res = parent->registerInFlight(halRequest->frame_number,
4908 totalNumBuffers, captureRequest->mResultExtras,
4909 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004910 hasCallback,
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004911 calculateMaxExpectedDuration(settings),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004912 requestedPhysicalCameras, isStillCapture, isZslCapture,
4913 captureRequest->mRotateAndCropAuto, mPrevCameraIdsWithZoom,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004914 (mUseHalBufManager) ? uniqueSurfaceIdMap :
Shuzhen Wang316781a2020-08-18 18:11:01 -07004915 SurfaceMap{}, captureRequest->mRequestTimeNs);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004916 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
4917 ", burstId = %" PRId32 ".",
4918 __FUNCTION__,
4919 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
4920 captureRequest->mResultExtras.burstId);
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004921
4922 if (shouldUnlockSettings) {
4923 captureRequest->mSettingsList.begin()->metadata.unlock(settings);
4924 }
4925
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004926 if (res != OK) {
4927 SET_ERR("RequestThread: Unable to register new in-flight request:"
4928 " %s (%d)", strerror(-res), res);
4929 return INVALID_OPERATION;
4930 }
4931 }
4932
4933 return OK;
4934}
4935
Igor Murashkin1e479c02013-09-06 16:55:14 -07004936CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004937 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07004938 Mutex::Autolock al(mLatestRequestMutex);
4939
4940 ALOGV("RequestThread::%s", __FUNCTION__);
4941
4942 return mLatestRequest;
4943}
4944
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004945bool Camera3Device::RequestThread::isStreamPending(
4946 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004947 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004948 Mutex::Autolock l(mRequestLock);
4949
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004950 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004951 if (!nextRequest.submitted) {
4952 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
4953 if (stream == s) return true;
4954 }
4955 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004956 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004957 }
4958
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004959 for (const auto& request : mRequestQueue) {
4960 for (const auto& s : request->mOutputStreams) {
4961 if (stream == s) return true;
4962 }
4963 if (stream == request->mInputStream) return true;
4964 }
4965
4966 for (const auto& request : mRepeatingRequests) {
4967 for (const auto& s : request->mOutputStreams) {
4968 if (stream == s) return true;
4969 }
4970 if (stream == request->mInputStream) return true;
4971 }
4972
4973 return false;
4974}
Jianing Weicb0652e2014-03-12 18:29:36 -07004975
Emilian Peev40ead602017-09-26 15:46:36 +01004976bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
4977 ATRACE_CALL();
4978 Mutex::Autolock l(mRequestLock);
4979
4980 for (const auto& nextRequest : mNextRequests) {
4981 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
4982 if (s.first == streamId) {
4983 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4984 if (it != s.second.end()) {
4985 return true;
4986 }
4987 }
4988 }
4989 }
4990
4991 for (const auto& request : mRequestQueue) {
4992 for (const auto& s : request->mOutputSurfaces) {
4993 if (s.first == streamId) {
4994 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4995 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004996 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004997 }
4998 }
4999 }
5000 }
5001
5002 for (const auto& request : mRepeatingRequests) {
5003 for (const auto& s : request->mOutputSurfaces) {
5004 if (s.first == streamId) {
5005 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
5006 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005007 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01005008 }
5009 }
5010 }
5011 }
5012
5013 return false;
5014}
5015
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07005016void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
5017 if (!mUseHalBufManager) {
5018 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
5019 return;
5020 }
5021
5022 Mutex::Autolock pl(mPauseLock);
5023 if (mPaused) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005024 mInterface->signalPipelineDrain(streamIds);
5025 return;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07005026 }
5027 // If request thread is still busy, wait until paused then notify HAL
5028 mNotifyPipelineDrain = true;
5029 mStreamIdsToBeDrained = streamIds;
5030}
5031
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07005032void Camera3Device::RequestThread::resetPipelineDrain() {
5033 Mutex::Autolock pl(mPauseLock);
5034 mNotifyPipelineDrain = false;
5035 mStreamIdsToBeDrained.clear();
5036}
5037
Emilian Peevc0fe54c2020-03-11 14:05:07 -07005038void Camera3Device::RequestThread::clearPreviousRequest() {
5039 Mutex::Autolock l(mRequestLock);
5040 mPrevRequest.clear();
5041}
5042
Yin-Chia Yehb978c382019-10-30 00:22:37 -07005043status_t Camera3Device::RequestThread::switchToOffline(
5044 const std::vector<int32_t>& streamsToKeep,
5045 /*out*/hardware::camera::device::V3_6::CameraOfflineSessionInfo* offlineSessionInfo,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005046 /*out*/sp<hardware::camera::device::V3_6::ICameraOfflineSession>* offlineSession,
5047 /*out*/camera3::BufferRecords* bufferRecords) {
Yin-Chia Yehb978c382019-10-30 00:22:37 -07005048 Mutex::Autolock l(mRequestLock);
5049 clearRepeatingRequestsLocked(/*lastFrameNumber*/nullptr);
5050
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005051 // Wait until request thread is fully stopped
5052 // TBD: check if request thread is being paused by other APIs (shouldn't be)
5053
5054 // We could also check for mRepeatingRequests.empty(), but the API interface
5055 // is serialized by Camera3Device::mInterfaceLock so no one should be able to submit any
5056 // new requests during the call; hence skip that check.
Yin-Chia Yehb978c382019-10-30 00:22:37 -07005057 bool queueEmpty = mNextRequests.empty() && mRequestQueue.empty();
5058 while (!queueEmpty) {
5059 status_t res = mRequestSubmittedSignal.waitRelative(mRequestLock, kRequestSubmitTimeout);
5060 if (res == TIMED_OUT) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005061 ALOGE("%s: request thread failed to submit one request within timeout!", __FUNCTION__);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07005062 return res;
5063 } else if (res != OK) {
5064 ALOGE("%s: request thread failed to submit a request: %s (%d)!",
5065 __FUNCTION__, strerror(-res), res);
5066 return res;
5067 }
5068 queueEmpty = mNextRequests.empty() && mRequestQueue.empty();
5069 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005070
Yin-Chia Yehb978c382019-10-30 00:22:37 -07005071 return mInterface->switchToOffline(
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005072 streamsToKeep, offlineSessionInfo, offlineSession, bufferRecords);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07005073}
5074
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005075status_t Camera3Device::RequestThread::setRotateAndCropAutoBehavior(
5076 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
5077 ATRACE_CALL();
5078 Mutex::Autolock l(mTriggerMutex);
5079 if (rotateAndCropValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
5080 return BAD_VALUE;
5081 }
5082 mRotateAndCropOverride = rotateAndCropValue;
5083 return OK;
5084}
5085
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005086status_t Camera3Device::RequestThread::setCameraMute(bool enabled) {
5087 ATRACE_CALL();
5088 Mutex::Autolock l(mTriggerMutex);
5089 if (enabled != mCameraMute) {
5090 mCameraMute = enabled;
5091 mCameraMuteChanged = true;
5092 }
5093 return OK;
5094}
5095
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07005096nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005097 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005098 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07005099 return mExpectedInflightDuration > kMinInflightDuration ?
5100 mExpectedInflightDuration : kMinInflightDuration;
5101}
5102
Emilian Peevaebbe412018-01-15 13:53:24 +00005103void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
Emilian Peevf4816702020-04-03 15:44:51 -07005104 camera_capture_request_t *halRequest) {
Emilian Peevaebbe412018-01-15 13:53:24 +00005105 if ((request == nullptr) || (halRequest == nullptr)) {
5106 ALOGE("%s: Invalid request!", __FUNCTION__);
5107 return;
5108 }
5109
5110 if (halRequest->num_physcam_settings > 0) {
5111 if (halRequest->physcam_id != nullptr) {
5112 delete [] halRequest->physcam_id;
5113 halRequest->physcam_id = nullptr;
5114 }
5115 if (halRequest->physcam_settings != nullptr) {
5116 auto it = ++(request->mSettingsList.begin());
5117 size_t i = 0;
5118 for (; it != request->mSettingsList.end(); it++, i++) {
5119 it->metadata.unlock(halRequest->physcam_settings[i]);
5120 }
5121 delete [] halRequest->physcam_settings;
5122 halRequest->physcam_settings = nullptr;
5123 }
5124 }
5125}
5126
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005127void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
5128 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005129 return;
5130 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005131
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005132 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005133 // Skip the ones that have been submitted successfully.
5134 if (nextRequest.submitted) {
5135 continue;
5136 }
5137
5138 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07005139 camera_capture_request_t* halRequest = &nextRequest.halRequest;
5140 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005141
5142 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00005143 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005144 }
5145
Emilian Peevaebbe412018-01-15 13:53:24 +00005146 cleanupPhysicalSettings(captureRequest, halRequest);
5147
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005148 if (captureRequest->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07005149 captureRequest->mInputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005150 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
5151 }
5152
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08005153 // No output buffer can be returned when using HAL buffer manager
5154 if (!mUseHalBufManager) {
5155 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
5156 //Buffers that failed processing could still have
5157 //valid acquire fence.
5158 int acquireFence = (*outputBuffers)[i].acquire_fence;
5159 if (0 <= acquireFence) {
5160 close(acquireFence);
5161 outputBuffers->editItemAt(i).acquire_fence = -1;
5162 }
Emilian Peevf4816702020-04-03 15:44:51 -07005163 outputBuffers->editItemAt(i).status = CAMERA_BUFFER_STATUS_ERROR;
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08005164 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0,
5165 /*timestampIncreasing*/true, std::vector<size_t> (),
5166 captureRequest->mResultExtras.frameNumber);
Emilian Peevc58cf4c2017-05-11 17:23:41 +01005167 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005168 }
5169
5170 if (sendRequestError) {
5171 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005172 sp<NotificationListener> listener = mListener.promote();
5173 if (listener != NULL) {
5174 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005175 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005176 captureRequest->mResultExtras);
5177 }
5178 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07005179
5180 // Remove yet-to-be submitted inflight request from inflightMap
5181 {
5182 sp<Camera3Device> parent = mParent.promote();
5183 if (parent != NULL) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005184 std::lock_guard<std::mutex> l(parent->mInFlightLock);
Shuzhen Wangcadb3302016-11-04 14:17:56 -07005185 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
5186 if (idx >= 0) {
5187 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
5188 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
5189 parent->removeInFlightMapEntryLocked(idx);
5190 }
5191 }
5192 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005193 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005194
5195 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005196 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005197}
5198
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005199void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005200 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005201 // Optimized a bit for the simple steady-state case (single repeating
5202 // request), to avoid putting that request in the queue temporarily.
5203 Mutex::Autolock l(mRequestLock);
5204
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005205 assert(mNextRequests.empty());
5206
5207 NextRequest nextRequest;
5208 nextRequest.captureRequest = waitForNextRequestLocked();
5209 if (nextRequest.captureRequest == nullptr) {
5210 return;
5211 }
5212
Emilian Peevf4816702020-04-03 15:44:51 -07005213 nextRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005214 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005215 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005216
5217 // Wait for additional requests
5218 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
5219
5220 for (size_t i = 1; i < batchSize; i++) {
5221 NextRequest additionalRequest;
5222 additionalRequest.captureRequest = waitForNextRequestLocked();
5223 if (additionalRequest.captureRequest == nullptr) {
5224 break;
5225 }
5226
Emilian Peevf4816702020-04-03 15:44:51 -07005227 additionalRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005228 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005229 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005230 }
5231
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005232 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08005233 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005234 mNextRequests.size(), batchSize);
5235 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005236 }
5237
5238 return;
5239}
5240
5241sp<Camera3Device::CaptureRequest>
5242 Camera3Device::RequestThread::waitForNextRequestLocked() {
5243 status_t res;
5244 sp<CaptureRequest> nextRequest;
5245
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005246 while (mRequestQueue.empty()) {
5247 if (!mRepeatingRequests.empty()) {
5248 // Always atomically enqueue all requests in a repeating request
5249 // list. Guarantees a complete in-sequence set of captures to
5250 // application.
5251 const RequestList &requests = mRepeatingRequests;
Shuzhen Wang316781a2020-08-18 18:11:01 -07005252 if (mFirstRepeating) {
5253 mFirstRepeating = false;
5254 } else {
5255 for (auto& request : requests) {
5256 // For repeating requests, override timestamp request using
5257 // the time a request is inserted into the request queue,
5258 // because the original repeating request will have an old
5259 // fixed timestamp.
5260 request->mRequestTimeNs = systemTime();
5261 }
5262 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005263 RequestList::const_iterator firstRequest =
5264 requests.begin();
5265 nextRequest = *firstRequest;
5266 mRequestQueue.insert(mRequestQueue.end(),
5267 ++firstRequest,
5268 requests.end());
5269 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07005270
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005271 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07005272
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005273 break;
5274 }
5275
5276 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
5277
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005278 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
5279 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005280 Mutex::Autolock pl(mPauseLock);
5281 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005282 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005283 mPaused = true;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07005284 if (mNotifyPipelineDrain) {
5285 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
5286 mNotifyPipelineDrain = false;
5287 mStreamIdsToBeDrained.clear();
5288 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08005289 // Let the tracker know
5290 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5291 if (statusTracker != 0) {
5292 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
5293 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005294 sp<Camera3Device> parent = mParent.promote();
5295 if (parent != nullptr) {
5296 parent->mRequestBufferSM.onRequestThreadPaused();
5297 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005298 }
5299 // Stop waiting for now and let thread management happen
5300 return NULL;
5301 }
5302 }
5303
5304 if (nextRequest == NULL) {
5305 // Don't have a repeating request already in hand, so queue
5306 // must have an entry now.
5307 RequestList::iterator firstRequest =
5308 mRequestQueue.begin();
5309 nextRequest = *firstRequest;
5310 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07005311 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
5312 sp<NotificationListener> listener = mListener.promote();
5313 if (listener != NULL) {
5314 listener->notifyRequestQueueEmpty();
5315 }
5316 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005317 }
5318
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005319 // In case we've been unpaused by setPaused clearing mDoPause, need to
5320 // update internal pause state (capture/setRepeatingRequest unpause
5321 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005322 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005323 if (mPaused) {
5324 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
5325 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5326 if (statusTracker != 0) {
5327 statusTracker->markComponentActive(mStatusId);
5328 }
5329 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005330 mPaused = false;
5331
5332 // Check if we've reconfigured since last time, and reset the preview
5333 // request if so. Can't use 'NULL request == repeat' across configure calls.
5334 if (mReconfigured) {
5335 mPrevRequest.clear();
5336 mReconfigured = false;
5337 }
5338
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005339 if (nextRequest != NULL) {
5340 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07005341 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
5342 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005343
5344 // Since RequestThread::clear() removes buffers from the input stream,
5345 // get the right buffer here before unlocking mRequestLock
5346 if (nextRequest->mInputStream != NULL) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08005347 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer,
5348 &nextRequest->mInputBufferSize);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005349 if (res != OK) {
5350 // Can't get input buffer from gralloc queue - this could be due to
5351 // disconnected queue or other producer misbehavior, so not a fatal
5352 // error
5353 ALOGE("%s: Can't get input buffer, skipping request:"
5354 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005355
5356 sp<NotificationListener> listener = mListener.promote();
5357 if (listener != NULL) {
5358 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005359 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005360 nextRequest->mResultExtras);
5361 }
5362 return NULL;
5363 }
5364 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005365 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07005366
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005367 return nextRequest;
5368}
5369
5370bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005371 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005372 status_t res;
5373 Mutex::Autolock l(mPauseLock);
5374 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005375 if (mPaused == false) {
5376 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005377 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07005378 if (mNotifyPipelineDrain) {
5379 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
5380 mNotifyPipelineDrain = false;
5381 mStreamIdsToBeDrained.clear();
5382 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08005383 // Let the tracker know
5384 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5385 if (statusTracker != 0) {
5386 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
5387 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005388 sp<Camera3Device> parent = mParent.promote();
5389 if (parent != nullptr) {
5390 parent->mRequestBufferSM.onRequestThreadPaused();
5391 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005392 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005393
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005394 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005395 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005396 return true;
5397 }
5398 }
5399 // We don't set mPaused to false here, because waitForNextRequest needs
5400 // to further manage the paused state in case of starvation.
5401 return false;
5402}
5403
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005404void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005405 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005406 // With work to do, mark thread as unpaused.
5407 // If paused by request (setPaused), don't resume, to avoid
5408 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005409 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005410 Mutex::Autolock p(mPauseLock);
5411 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005412 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
5413 if (mPaused) {
5414 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5415 if (statusTracker != 0) {
5416 statusTracker->markComponentActive(mStatusId);
5417 }
5418 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005419 mPaused = false;
5420 }
5421}
5422
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07005423void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
5424 sp<Camera3Device> parent = mParent.promote();
5425 if (parent != NULL) {
5426 va_list args;
5427 va_start(args, fmt);
5428
5429 parent->setErrorStateV(fmt, args);
5430
5431 va_end(args);
5432 }
5433}
5434
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005435status_t Camera3Device::RequestThread::insertTriggers(
5436 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005437 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005438 Mutex::Autolock al(mTriggerMutex);
5439
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005440 sp<Camera3Device> parent = mParent.promote();
5441 if (parent == NULL) {
5442 CLOGE("RequestThread: Parent is gone");
5443 return DEAD_OBJECT;
5444 }
5445
Emilian Peevaebbe412018-01-15 13:53:24 +00005446 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005447 size_t count = mTriggerMap.size();
5448
5449 for (size_t i = 0; i < count; ++i) {
5450 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005451 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005452
5453 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
5454 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
5455 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07005456 if (isAeTrigger) {
5457 request->mResultExtras.precaptureTriggerId = triggerId;
5458 mCurrentPreCaptureTriggerId = triggerId;
5459 } else {
5460 request->mResultExtras.afTriggerId = triggerId;
5461 mCurrentAfTriggerId = triggerId;
5462 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01005463 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005464 }
5465
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005466 camera_metadata_entry entry = metadata.find(tag);
5467
5468 if (entry.count > 0) {
5469 /**
5470 * Already has an entry for this trigger in the request.
5471 * Rewrite it with our requested trigger value.
5472 */
5473 RequestTrigger oldTrigger = trigger;
5474
5475 oldTrigger.entryValue = entry.data.u8[0];
5476
5477 mTriggerReplacedMap.add(tag, oldTrigger);
5478 } else {
5479 /**
5480 * More typical, no trigger entry, so we just add it
5481 */
5482 mTriggerRemovedMap.add(tag, trigger);
5483 }
5484
5485 status_t res;
5486
5487 switch (trigger.getTagType()) {
5488 case TYPE_BYTE: {
5489 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
5490 res = metadata.update(tag,
5491 &entryValue,
5492 /*count*/1);
5493 break;
5494 }
5495 case TYPE_INT32:
5496 res = metadata.update(tag,
5497 &trigger.entryValue,
5498 /*count*/1);
5499 break;
5500 default:
5501 ALOGE("%s: Type not supported: 0x%x",
5502 __FUNCTION__,
5503 trigger.getTagType());
5504 return INVALID_OPERATION;
5505 }
5506
5507 if (res != OK) {
5508 ALOGE("%s: Failed to update request metadata with trigger tag %s"
5509 ", value %d", __FUNCTION__, trigger.getTagName(),
5510 trigger.entryValue);
5511 return res;
5512 }
5513
5514 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
5515 trigger.getTagName(),
5516 trigger.entryValue);
5517 }
5518
5519 mTriggerMap.clear();
5520
5521 return count;
5522}
5523
5524status_t Camera3Device::RequestThread::removeTriggers(
5525 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005526 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005527 Mutex::Autolock al(mTriggerMutex);
5528
Emilian Peevaebbe412018-01-15 13:53:24 +00005529 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005530
5531 /**
5532 * Replace all old entries with their old values.
5533 */
5534 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
5535 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
5536
5537 status_t res;
5538
5539 uint32_t tag = trigger.metadataTag;
5540 switch (trigger.getTagType()) {
5541 case TYPE_BYTE: {
5542 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
5543 res = metadata.update(tag,
5544 &entryValue,
5545 /*count*/1);
5546 break;
5547 }
5548 case TYPE_INT32:
5549 res = metadata.update(tag,
5550 &trigger.entryValue,
5551 /*count*/1);
5552 break;
5553 default:
5554 ALOGE("%s: Type not supported: 0x%x",
5555 __FUNCTION__,
5556 trigger.getTagType());
5557 return INVALID_OPERATION;
5558 }
5559
5560 if (res != OK) {
5561 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
5562 ", trigger value %d", __FUNCTION__,
5563 trigger.getTagName(), trigger.entryValue);
5564 return res;
5565 }
5566 }
5567 mTriggerReplacedMap.clear();
5568
5569 /**
5570 * Remove all new entries.
5571 */
5572 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
5573 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
5574 status_t res = metadata.erase(trigger.metadataTag);
5575
5576 if (res != OK) {
5577 ALOGE("%s: Failed to erase metadata with trigger tag %s"
5578 ", trigger value %d", __FUNCTION__,
5579 trigger.getTagName(), trigger.entryValue);
5580 return res;
5581 }
5582 }
5583 mTriggerRemovedMap.clear();
5584
5585 return OK;
5586}
5587
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04005588status_t Camera3Device::RequestThread::addFakeTriggerIds(
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005589 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08005590 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04005591 static const int32_t fakeTriggerId = 1;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005592 status_t res;
5593
Emilian Peevaebbe412018-01-15 13:53:24 +00005594 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005595
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04005596 // If AF trigger is active, insert a fake AF trigger ID if none already
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005597 // exists
5598 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
5599 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
5600 if (afTrigger.count > 0 &&
5601 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
5602 afId.count == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04005603 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005604 if (res != OK) return res;
5605 }
5606
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04005607 // If AE precapture trigger is active, insert a fake precapture trigger ID
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005608 // if none already exists
5609 camera_metadata_entry pcTrigger =
5610 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
5611 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
5612 if (pcTrigger.count > 0 &&
5613 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
5614 pcId.count == 0) {
5615 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04005616 &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005617 if (res != OK) return res;
5618 }
5619
5620 return OK;
5621}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005622
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005623bool Camera3Device::RequestThread::overrideAutoRotateAndCrop(
5624 const sp<CaptureRequest> &request) {
5625 ATRACE_CALL();
5626
5627 if (request->mRotateAndCropAuto) {
5628 Mutex::Autolock l(mTriggerMutex);
5629 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
5630
5631 auto rotateAndCropEntry = metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
5632 if (rotateAndCropEntry.count > 0) {
5633 if (rotateAndCropEntry.data.u8[0] == mRotateAndCropOverride) {
5634 return false;
5635 } else {
5636 rotateAndCropEntry.data.u8[0] = mRotateAndCropOverride;
5637 return true;
5638 }
5639 } else {
5640 uint8_t rotateAndCrop_u8 = mRotateAndCropOverride;
5641 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP,
5642 &rotateAndCrop_u8, 1);
5643 return true;
5644 }
5645 }
5646 return false;
5647}
5648
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005649bool Camera3Device::RequestThread::overrideTestPattern(
5650 const sp<CaptureRequest> &request) {
5651 ATRACE_CALL();
5652
5653 Mutex::Autolock l(mTriggerMutex);
5654
5655 bool changed = false;
5656
5657 int32_t testPatternMode = request->mOriginalTestPatternMode;
5658 int32_t testPatternData[4] = {
5659 request->mOriginalTestPatternData[0],
5660 request->mOriginalTestPatternData[1],
5661 request->mOriginalTestPatternData[2],
5662 request->mOriginalTestPatternData[3]
5663 };
5664
5665 if (mCameraMute) {
5666 testPatternMode = ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR;
5667 testPatternData[0] = 0;
5668 testPatternData[1] = 0;
5669 testPatternData[2] = 0;
5670 testPatternData[3] = 0;
5671 }
5672
5673 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
5674
5675 auto testPatternEntry = metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
5676 if (testPatternEntry.count > 0) {
5677 if (testPatternEntry.data.i32[0] != testPatternMode) {
5678 testPatternEntry.data.i32[0] = testPatternMode;
5679 changed = true;
5680 }
5681 } else {
5682 metadata.update(ANDROID_SENSOR_TEST_PATTERN_MODE,
5683 &testPatternMode, 1);
5684 changed = true;
5685 }
5686
5687 auto testPatternColor = metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
5688 if (testPatternColor.count > 0) {
5689 for (size_t i = 0; i < 4; i++) {
5690 if (testPatternColor.data.i32[i] != (int32_t)testPatternData[i]) {
5691 testPatternColor.data.i32[i] = testPatternData[i];
5692 changed = true;
5693 }
5694 }
5695 } else {
5696 metadata.update(ANDROID_SENSOR_TEST_PATTERN_DATA,
5697 (int32_t*)testPatternData, 4);
5698 changed = true;
5699 }
5700
5701 return changed;
5702}
5703
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005704/**
5705 * PreparerThread inner class methods
5706 */
5707
5708Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07005709 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005710 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005711}
5712
5713Camera3Device::PreparerThread::~PreparerThread() {
5714 Thread::requestExitAndWait();
5715 if (mCurrentStream != nullptr) {
5716 mCurrentStream->cancelPrepare();
5717 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5718 mCurrentStream.clear();
5719 }
5720 clear();
5721}
5722
Ruben Brunkc78ac262015-08-13 17:58:46 -07005723status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005724 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005725 status_t res;
5726
5727 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005728 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005729
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07005730 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005731 if (res == OK) {
5732 // No preparation needed, fire listener right off
5733 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005734 if (listener != NULL) {
5735 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005736 }
5737 return OK;
5738 } else if (res != NOT_ENOUGH_DATA) {
5739 return res;
5740 }
5741
5742 // Need to prepare, start up thread if necessary
5743 if (!mActive) {
5744 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
5745 // isn't running
5746 Thread::requestExitAndWait();
5747 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5748 if (res != OK) {
5749 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005750 if (listener != NULL) {
5751 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005752 }
5753 return res;
5754 }
5755 mCancelNow = false;
5756 mActive = true;
5757 ALOGV("%s: Preparer stream started", __FUNCTION__);
5758 }
5759
5760 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005761 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005762 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
5763
5764 return OK;
5765}
5766
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005767void Camera3Device::PreparerThread::pause() {
5768 ATRACE_CALL();
5769
5770 Mutex::Autolock l(mLock);
5771
5772 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
5773 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
5774 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
5775 int currentMaxCount = mCurrentMaxCount;
5776 mPendingStreams.clear();
5777 mCancelNow = true;
5778 while (mActive) {
5779 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
5780 if (res == TIMED_OUT) {
5781 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
5782 return;
5783 } else if (res != OK) {
5784 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
5785 return;
5786 }
5787 }
5788
5789 //Check whether the prepare thread was able to complete the current
5790 //stream. In case work is still pending emplace it along with the rest
5791 //of the streams in the pending list.
5792 if (currentStream != nullptr) {
5793 if (!mCurrentPrepareComplete) {
5794 pendingStreams.emplace(currentMaxCount, currentStream);
5795 }
5796 }
5797
5798 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
5799 for (const auto& it : mPendingStreams) {
5800 it.second->cancelPrepare();
5801 }
5802}
5803
5804status_t Camera3Device::PreparerThread::resume() {
5805 ATRACE_CALL();
5806 status_t res;
5807
5808 Mutex::Autolock l(mLock);
5809 sp<NotificationListener> listener = mListener.promote();
5810
5811 if (mActive) {
5812 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
5813 return NO_INIT;
5814 }
5815
5816 auto it = mPendingStreams.begin();
5817 for (; it != mPendingStreams.end();) {
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07005818 res = it->second->startPrepare(it->first, true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005819 if (res == OK) {
5820 if (listener != NULL) {
5821 listener->notifyPrepared(it->second->getId());
5822 }
5823 it = mPendingStreams.erase(it);
5824 } else if (res != NOT_ENOUGH_DATA) {
5825 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
5826 res, strerror(-res));
5827 it = mPendingStreams.erase(it);
5828 } else {
5829 it++;
5830 }
5831 }
5832
5833 if (mPendingStreams.empty()) {
5834 return OK;
5835 }
5836
5837 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5838 if (res != OK) {
5839 ALOGE("%s: Unable to start preparer stream: %d (%s)",
5840 __FUNCTION__, res, strerror(-res));
5841 return res;
5842 }
5843 mCancelNow = false;
5844 mActive = true;
5845 ALOGV("%s: Preparer stream started", __FUNCTION__);
5846
5847 return OK;
5848}
5849
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005850status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005851 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005852 Mutex::Autolock l(mLock);
5853
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005854 for (const auto& it : mPendingStreams) {
5855 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005856 }
5857 mPendingStreams.clear();
5858 mCancelNow = true;
5859
5860 return OK;
5861}
5862
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005863void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005864 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005865 Mutex::Autolock l(mLock);
5866 mListener = listener;
5867}
5868
5869bool Camera3Device::PreparerThread::threadLoop() {
5870 status_t res;
5871 {
5872 Mutex::Autolock l(mLock);
5873 if (mCurrentStream == nullptr) {
5874 // End thread if done with work
5875 if (mPendingStreams.empty()) {
5876 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
5877 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
5878 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
5879 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005880 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005881 return false;
5882 }
5883
5884 // Get next stream to prepare
5885 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005886 mCurrentStream = it->second;
5887 mCurrentMaxCount = it->first;
5888 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005889 mPendingStreams.erase(it);
5890 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
5891 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
5892 } else if (mCancelNow) {
5893 mCurrentStream->cancelPrepare();
5894 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5895 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
5896 mCurrentStream.clear();
5897 mCancelNow = false;
5898 return true;
5899 }
5900 }
5901
5902 res = mCurrentStream->prepareNextBuffer();
5903 if (res == NOT_ENOUGH_DATA) return true;
5904 if (res != OK) {
5905 // Something bad happened; try to recover by cancelling prepare and
5906 // signalling listener anyway
5907 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
5908 mCurrentStream->getId(), res, strerror(-res));
5909 mCurrentStream->cancelPrepare();
5910 }
5911
5912 // This stream has finished, notify listener
5913 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005914 sp<NotificationListener> listener = mListener.promote();
5915 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005916 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
5917 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005918 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005919 }
5920
5921 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5922 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005923 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005924
5925 return true;
5926}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005927
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005928status_t Camera3Device::RequestBufferStateMachine::initialize(
5929 sp<camera3::StatusTracker> statusTracker) {
5930 if (statusTracker == nullptr) {
5931 ALOGE("%s: statusTracker is null", __FUNCTION__);
5932 return BAD_VALUE;
5933 }
5934
5935 std::lock_guard<std::mutex> lock(mLock);
5936 mStatusTracker = statusTracker;
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07005937 mRequestBufferStatusId = statusTracker->addComponent("BufferRequestSM");
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005938 return OK;
5939}
5940
5941bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
5942 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005943 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005944 mRequestBufferOngoing = true;
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005945 notifyTrackerLocked(/*active*/true);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005946 return true;
5947 }
5948 return false;
5949}
5950
5951void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
5952 std::lock_guard<std::mutex> lock(mLock);
5953 if (!mRequestBufferOngoing) {
5954 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
5955 return;
5956 }
5957 mRequestBufferOngoing = false;
5958 if (mStatus == RB_STATUS_PENDING_STOP) {
5959 checkSwitchToStopLocked();
5960 }
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005961 notifyTrackerLocked(/*active*/false);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005962}
5963
5964void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
5965 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005966 mSwitchedToOffline = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005967 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005968 return;
5969}
5970
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005971void Camera3Device::RequestBufferStateMachine::onSubmittingRequest() {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005972 std::lock_guard<std::mutex> lock(mLock);
5973 mRequestThreadPaused = false;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005974 // inflight map register actually happens in prepareHalRequest now, but it is close enough
5975 // approximation.
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005976 mInflightMapEmpty = false;
5977 if (mStatus == RB_STATUS_STOPPED) {
5978 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005979 }
5980 return;
5981}
5982
5983void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
5984 std::lock_guard<std::mutex> lock(mLock);
5985 mRequestThreadPaused = true;
5986 if (mStatus == RB_STATUS_PENDING_STOP) {
5987 checkSwitchToStopLocked();
5988 }
5989 return;
5990}
5991
5992void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
5993 std::lock_guard<std::mutex> lock(mLock);
5994 mInflightMapEmpty = true;
5995 if (mStatus == RB_STATUS_PENDING_STOP) {
5996 checkSwitchToStopLocked();
5997 }
5998 return;
5999}
6000
6001void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
6002 std::lock_guard<std::mutex> lock(mLock);
6003 if (!checkSwitchToStopLocked()) {
6004 mStatus = RB_STATUS_PENDING_STOP;
6005 }
6006 return;
6007}
6008
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006009bool Camera3Device::RequestBufferStateMachine::onSwitchToOfflineSuccess() {
6010 std::lock_guard<std::mutex> lock(mLock);
6011 if (mRequestBufferOngoing) {
6012 ALOGE("%s: HAL must not be requesting buffer after HAL returns switchToOffline!",
6013 __FUNCTION__);
6014 return false;
6015 }
6016 mSwitchedToOffline = true;
6017 mInflightMapEmpty = true;
6018 mRequestThreadPaused = true;
6019 mStatus = RB_STATUS_STOPPED;
6020 return true;
6021}
6022
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006023void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
6024 sp<StatusTracker> statusTracker = mStatusTracker.promote();
6025 if (statusTracker != nullptr) {
6026 if (active) {
6027 statusTracker->markComponentActive(mRequestBufferStatusId);
6028 } else {
6029 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
6030 }
6031 }
6032}
6033
6034bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
6035 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
6036 mStatus = RB_STATUS_STOPPED;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006037 return true;
6038 }
6039 return false;
6040}
6041
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006042bool Camera3Device::startRequestBuffer() {
6043 return mRequestBufferSM.startRequestBuffer();
6044}
Shuzhen Wang268a1362018-10-16 16:32:59 -07006045
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006046void Camera3Device::endRequestBuffer() {
6047 mRequestBufferSM.endRequestBuffer();
6048}
Shuzhen Wang268a1362018-10-16 16:32:59 -07006049
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006050nsecs_t Camera3Device::getWaitDuration() {
6051 return kBaseGetBufferWait + getExpectedInFlightDuration();
6052}
Shuzhen Wang268a1362018-10-16 16:32:59 -07006053
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006054void Camera3Device::getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) {
6055 mInterface->getInflightBufferKeys(out);
6056}
Shuzhen Wang268a1362018-10-16 16:32:59 -07006057
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006058void Camera3Device::getInflightRequestBufferKeys(std::vector<uint64_t>* out) {
6059 mInterface->getInflightRequestBufferKeys(out);
6060}
Shuzhen Wang268a1362018-10-16 16:32:59 -07006061
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006062std::vector<sp<Camera3StreamInterface>> Camera3Device::getAllStreams() {
6063 std::vector<sp<Camera3StreamInterface>> ret;
6064 bool hasInputStream = mInputStream != nullptr;
6065 ret.reserve(mOutputStreams.size() + mDeletedStreams.size() + ((hasInputStream) ? 1 : 0));
6066 if (hasInputStream) {
6067 ret.push_back(mInputStream);
Shuzhen Wang268a1362018-10-16 16:32:59 -07006068 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006069 for (size_t i = 0; i < mOutputStreams.size(); i++) {
6070 ret.push_back(mOutputStreams[i]);
6071 }
6072 for (size_t i = 0; i < mDeletedStreams.size(); i++) {
6073 ret.push_back(mDeletedStreams[i]);
6074 }
6075 return ret;
Shuzhen Wang268a1362018-10-16 16:32:59 -07006076}
6077
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006078status_t Camera3Device::switchToOffline(
6079 const std::vector<int32_t>& streamsToKeep,
6080 /*out*/ sp<CameraOfflineSessionBase>* session) {
6081 ATRACE_CALL();
6082 if (session == nullptr) {
6083 ALOGE("%s: session must not be null", __FUNCTION__);
6084 return BAD_VALUE;
6085 }
6086
6087 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006088
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006089 bool hasInputStream = mInputStream != nullptr;
6090 int32_t inputStreamId = hasInputStream ? mInputStream->getId() : -1;
6091 bool inputStreamSupportsOffline = hasInputStream ?
6092 mInputStream->getOfflineProcessingSupport() : false;
6093 auto outputStreamIds = mOutputStreams.getStreamIds();
6094 auto streamIds = outputStreamIds;
6095 if (hasInputStream) {
6096 streamIds.push_back(mInputStream->getId());
6097 }
6098
6099 // Check all streams in streamsToKeep supports offline mode
6100 for (auto id : streamsToKeep) {
6101 if (std::find(streamIds.begin(), streamIds.end(), id) == streamIds.end()) {
6102 ALOGE("%s: Unknown stream ID %d", __FUNCTION__, id);
6103 return BAD_VALUE;
6104 } else if (id == inputStreamId) {
6105 if (!inputStreamSupportsOffline) {
6106 ALOGE("%s: input stream %d cannot be switched to offline",
6107 __FUNCTION__, id);
6108 return BAD_VALUE;
6109 }
6110 } else {
6111 sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
6112 if (!stream->getOfflineProcessingSupport()) {
6113 ALOGE("%s: output stream %d cannot be switched to offline",
6114 __FUNCTION__, id);
6115 return BAD_VALUE;
6116 }
6117 }
6118 }
6119
6120 // TODO: block surface sharing and surface group streams until we can support them
6121
6122 // Stop repeating request, wait until all remaining requests are submitted, then call into
6123 // HAL switchToOffline
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006124 hardware::camera::device::V3_6::CameraOfflineSessionInfo offlineSessionInfo;
6125 sp<hardware::camera::device::V3_6::ICameraOfflineSession> offlineSession;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006126 camera3::BufferRecords bufferRecords;
6127 status_t ret = mRequestThread->switchToOffline(
6128 streamsToKeep, &offlineSessionInfo, &offlineSession, &bufferRecords);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006129
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006130 if (ret != OK) {
6131 SET_ERR("Switch to offline failed: %s (%d)", strerror(-ret), ret);
6132 return ret;
6133 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006134
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006135 bool succ = mRequestBufferSM.onSwitchToOfflineSuccess();
6136 if (!succ) {
6137 SET_ERR("HAL must not be calling requestStreamBuffers call");
6138 // TODO: block ALL callbacks from HAL till app configured new streams?
6139 return UNKNOWN_ERROR;
6140 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006141
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006142 // Verify offlineSessionInfo
6143 std::vector<int32_t> offlineStreamIds;
6144 offlineStreamIds.reserve(offlineSessionInfo.offlineStreams.size());
6145 for (auto offlineStream : offlineSessionInfo.offlineStreams) {
6146 // verify stream IDs
6147 int32_t id = offlineStream.id;
6148 if (std::find(streamIds.begin(), streamIds.end(), id) == streamIds.end()) {
6149 SET_ERR("stream ID %d not found!", id);
6150 return UNKNOWN_ERROR;
6151 }
6152
6153 // When not using HAL buf manager, only allow streams requested by app to be preserved
6154 if (!mUseHalBufManager) {
6155 if (std::find(streamsToKeep.begin(), streamsToKeep.end(), id) == streamsToKeep.end()) {
6156 SET_ERR("stream ID %d must not be switched to offline!", id);
6157 return UNKNOWN_ERROR;
6158 }
6159 }
6160
6161 offlineStreamIds.push_back(id);
6162 sp<Camera3StreamInterface> stream = (id == inputStreamId) ?
6163 static_cast<sp<Camera3StreamInterface>>(mInputStream) :
6164 static_cast<sp<Camera3StreamInterface>>(mOutputStreams.get(id));
6165 // Verify number of outstanding buffers
6166 if (stream->getOutstandingBuffersCount() != offlineStream.numOutstandingBuffers) {
6167 SET_ERR("Offline stream %d # of remaining buffer mismatch: (%zu,%d) (service/HAL)",
6168 id, stream->getOutstandingBuffersCount(), offlineStream.numOutstandingBuffers);
6169 return UNKNOWN_ERROR;
6170 }
6171 }
6172
6173 // Verify all streams to be deleted don't have any outstanding buffers
6174 if (hasInputStream && std::find(offlineStreamIds.begin(), offlineStreamIds.end(),
6175 inputStreamId) == offlineStreamIds.end()) {
6176 if (mInputStream->hasOutstandingBuffers()) {
6177 SET_ERR("Input stream %d still has %zu outstanding buffer!",
6178 inputStreamId, mInputStream->getOutstandingBuffersCount());
6179 return UNKNOWN_ERROR;
6180 }
6181 }
6182
6183 for (const auto& outStreamId : outputStreamIds) {
6184 if (std::find(offlineStreamIds.begin(), offlineStreamIds.end(),
6185 outStreamId) == offlineStreamIds.end()) {
6186 auto outStream = mOutputStreams.get(outStreamId);
6187 if (outStream->hasOutstandingBuffers()) {
6188 SET_ERR("Output stream %d still has %zu outstanding buffer!",
6189 outStreamId, outStream->getOutstandingBuffersCount());
6190 return UNKNOWN_ERROR;
6191 }
6192 }
6193 }
6194
6195 InFlightRequestMap offlineReqs;
6196 // Verify inflight requests and their pending buffers
6197 {
6198 std::lock_guard<std::mutex> l(mInFlightLock);
6199 for (auto offlineReq : offlineSessionInfo.offlineRequests) {
6200 int idx = mInFlightMap.indexOfKey(offlineReq.frameNumber);
6201 if (idx == NAME_NOT_FOUND) {
6202 SET_ERR("Offline request frame number %d not found!", offlineReq.frameNumber);
6203 return UNKNOWN_ERROR;
6204 }
6205
6206 const auto& inflightReq = mInFlightMap.valueAt(idx);
6207 // TODO: check specific stream IDs
6208 size_t numBuffersLeft = static_cast<size_t>(inflightReq.numBuffersLeft);
6209 if (numBuffersLeft != offlineReq.pendingStreams.size()) {
6210 SET_ERR("Offline request # of remaining buffer mismatch: (%d,%d) (service/HAL)",
6211 inflightReq.numBuffersLeft, offlineReq.pendingStreams.size());
6212 return UNKNOWN_ERROR;
6213 }
6214 offlineReqs.add(offlineReq.frameNumber, inflightReq);
6215 }
6216 }
6217
6218 // Create Camera3OfflineSession and transfer object ownership
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006219 // (streams, inflight requests, buffer caches)
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006220 camera3::StreamSet offlineStreamSet;
6221 sp<camera3::Camera3Stream> inputStream;
6222 for (auto offlineStream : offlineSessionInfo.offlineStreams) {
6223 int32_t id = offlineStream.id;
6224 if (mInputStream != nullptr && id == mInputStream->getId()) {
6225 inputStream = mInputStream;
6226 } else {
6227 offlineStreamSet.add(id, mOutputStreams.get(id));
6228 }
6229 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006230
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006231 // TODO: check if we need to lock before copying states
6232 // though technically no other thread should be talking to Camera3Device at this point
6233 Camera3OfflineStates offlineStates(
6234 mTagMonitor, mVendorTagId, mUseHalBufManager, mNeedFixupMonochromeTags,
Shuzhen Wangb7b42652020-05-07 11:59:02 -07006235 mUsePartialResult, mNumPartialResults, mLastCompletedRegularFrameNumber,
6236 mLastCompletedReprocessFrameNumber, mLastCompletedZslFrameNumber,
6237 mNextResultFrameNumber, mNextReprocessResultFrameNumber,
6238 mNextZslStillResultFrameNumber, mNextShutterFrameNumber,
6239 mNextReprocessShutterFrameNumber, mNextZslStillShutterFrameNumber,
6240 mDeviceInfo, mPhysicalDeviceInfoMap, mDistortionMappers,
6241 mZoomRatioMappers, mRotateAndCropMappers);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006242
6243 *session = new Camera3OfflineSession(mId, inputStream, offlineStreamSet,
6244 std::move(bufferRecords), offlineReqs, offlineStates, offlineSession);
6245
6246 // Delete all streams that has been transferred to offline session
6247 Mutex::Autolock l(mLock);
6248 for (auto offlineStream : offlineSessionInfo.offlineStreams) {
6249 int32_t id = offlineStream.id;
6250 if (mInputStream != nullptr && id == mInputStream->getId()) {
6251 mInputStream.clear();
6252 } else {
6253 mOutputStreams.remove(id);
6254 }
6255 }
6256
6257 // disconnect all other streams and switch to UNCONFIGURED state
6258 if (mInputStream != nullptr) {
6259 ret = mInputStream->disconnect();
6260 if (ret != OK) {
6261 SET_ERR_L("disconnect input stream failed!");
6262 return UNKNOWN_ERROR;
6263 }
6264 }
6265
6266 for (auto streamId : mOutputStreams.getStreamIds()) {
6267 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
6268 ret = stream->disconnect();
6269 if (ret != OK) {
6270 SET_ERR_L("disconnect output stream %d failed!", streamId);
6271 return UNKNOWN_ERROR;
6272 }
6273 }
6274
6275 mInputStream.clear();
6276 mOutputStreams.clear();
6277 mNeedConfig = true;
6278 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
6279 mOperatingMode = NO_MODE;
6280 mIsConstrainedHighSpeedConfiguration = false;
Emilian Peevc0fe54c2020-03-11 14:05:07 -07006281 mRequestThread->clearPreviousRequest();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006282
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006283 return OK;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006284 // TO be done by CameraDeviceClient/Camera3OfflineSession
6285 // register the offline client to camera service
6286 // Setup result passthing threads etc
6287 // Initialize offline session so HAL can start sending callback to it (result Fmq)
6288 // TODO: check how many onIdle callback will be sent
6289 // Java side to make sure the CameraCaptureSession is properly closed
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006290}
6291
Emilian Peevcc0b7952020-01-07 13:54:47 -08006292void Camera3Device::getOfflineStreamIds(std::vector<int> *offlineStreamIds) {
6293 ATRACE_CALL();
6294
6295 if (offlineStreamIds == nullptr) {
6296 return;
6297 }
6298
6299 Mutex::Autolock il(mInterfaceLock);
6300
6301 auto streamIds = mOutputStreams.getStreamIds();
6302 bool hasInputStream = mInputStream != nullptr;
6303 if (hasInputStream && mInputStream->getOfflineProcessingSupport()) {
6304 offlineStreamIds->push_back(mInputStream->getId());
6305 }
6306
6307 for (const auto & streamId : streamIds) {
6308 sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
6309 // Streams that use the camera buffer manager are currently not supported in
6310 // offline mode
6311 if (stream->getOfflineProcessingSupport() &&
6312 (stream->getStreamSetId() == CAMERA3_STREAM_SET_ID_INVALID)) {
6313 offlineStreamIds->push_back(streamId);
6314 }
6315 }
6316}
6317
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08006318status_t Camera3Device::setRotateAndCropAutoBehavior(
6319 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
6320 ATRACE_CALL();
6321 Mutex::Autolock il(mInterfaceLock);
6322 Mutex::Autolock l(mLock);
6323 if (mRequestThread == nullptr) {
6324 return INVALID_OPERATION;
6325 }
6326 return mRequestThread->setRotateAndCropAutoBehavior(rotateAndCropValue);
6327}
6328
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08006329bool Camera3Device::supportsCameraMute() {
6330 Mutex::Autolock il(mInterfaceLock);
6331 Mutex::Autolock l(mLock);
6332
6333 return mSupportCameraMute;
6334}
6335
6336status_t Camera3Device::setCameraMute(bool enabled) {
6337 ATRACE_CALL();
6338 Mutex::Autolock il(mInterfaceLock);
6339 Mutex::Autolock l(mLock);
6340
6341 if (mRequestThread == nullptr || !mSupportCameraMute) {
6342 return INVALID_OPERATION;
6343 }
6344 return mRequestThread->setCameraMute(enabled);
6345}
6346
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08006347}; // namespace android