blob: 8cc4f55cabc80c8df6710bb3daf87f5a5984e8ef [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 Chowdharyd4776262020-06-23 23:45:57 -070063#include "utils/TraceHFR.h"
Shuzhen Wang316781a2020-08-18 18:11:01 -070064#include "utils/CameraServiceProxyWrapper.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080065
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080066#include <algorithm>
Yin-Chia Yeh84be5782019-03-01 11:47:02 -080067#include <tuple>
68
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080069using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080070using namespace android::hardware::camera;
71using namespace android::hardware::camera::device::V3_2;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080072
73namespace android {
74
Eino-Ville Talvala2f09bac2016-12-13 11:29:54 -080075Camera3Device::Camera3Device(const String8 &id):
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080076 mId(id),
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080077 mOperatingMode(NO_MODE),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070078 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070079 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070080 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070081 mUsePartialResult(false),
82 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080083 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070084 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070085 mNextReprocessResultFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070086 mNextZslStillResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070087 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070088 mNextReprocessShutterFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070089 mNextZslStillShutterFrameNumber(0),
Emilian Peev71c73a22017-03-21 16:35:51 +000090 mListener(NULL),
Emilian Peev811d2952018-05-25 11:08:40 +010091 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID),
Shuzhen Wang268a1362018-10-16 16:32:59 -070092 mLastTemplateId(-1),
93 mNeedFixupMonochromeTags(false)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080094{
95 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080096 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080097}
98
99Camera3Device::~Camera3Device()
100{
101 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800102 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700103 disconnectImpl();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800104}
105
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800106const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -0800107 return mId;
108}
109
Emilian Peevbd8c5032018-02-14 23:05:40 +0000110status_t Camera3Device::initialize(sp<CameraProviderManager> manager, const String8& monitorTags) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800111 ATRACE_CALL();
112 Mutex::Autolock il(mInterfaceLock);
113 Mutex::Autolock l(mLock);
114
115 ALOGV("%s: Initializing HIDL device for camera %s", __FUNCTION__, mId.string());
116 if (mStatus != STATUS_UNINITIALIZED) {
117 CLOGE("Already initialized!");
118 return INVALID_OPERATION;
119 }
120 if (manager == nullptr) return INVALID_OPERATION;
121
122 sp<ICameraDeviceSession> session;
123 ATRACE_BEGIN("CameraHal::openSession");
Steven Moreland5ff9c912017-03-09 23:13:00 -0800124 status_t res = manager->openSession(mId.string(), this,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800125 /*out*/ &session);
126 ATRACE_END();
127 if (res != OK) {
128 SET_ERR_L("Could not open camera session: %s (%d)", strerror(-res), res);
129 return res;
130 }
131
Steven Moreland5ff9c912017-03-09 23:13:00 -0800132 res = manager->getCameraCharacteristics(mId.string(), &mDeviceInfo);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800133 if (res != OK) {
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700134 SET_ERR_L("Could not retrieve camera characteristics: %s (%d)", strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800135 session->close();
136 return res;
137 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800138 mSupportNativeZoomRatio = manager->supportNativeZoomRatio(mId.string());
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800139
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700140 std::vector<std::string> physicalCameraIds;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700141 bool isLogical = manager->isLogicalCamera(mId.string(), &physicalCameraIds);
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700142 if (isLogical) {
143 for (auto& physicalId : physicalCameraIds) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700144 res = manager->getCameraCharacteristics(
145 physicalId, &mPhysicalDeviceInfoMap[physicalId]);
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700146 if (res != OK) {
147 SET_ERR_L("Could not retrieve camera %s characteristics: %s (%d)",
148 physicalId.c_str(), strerror(-res), res);
149 session->close();
150 return res;
151 }
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700152
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800153 bool usePrecorrectArray =
154 DistortionMapper::isDistortionSupported(mPhysicalDeviceInfoMap[physicalId]);
155 if (usePrecorrectArray) {
156 res = mDistortionMappers[physicalId].setupStaticInfo(
157 mPhysicalDeviceInfoMap[physicalId]);
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700158 if (res != OK) {
159 SET_ERR_L("Unable to read camera %s's calibration fields for distortion "
160 "correction", physicalId.c_str());
161 session->close();
162 return res;
163 }
164 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800165
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800166 mZoomRatioMappers[physicalId] = ZoomRatioMapper(
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800167 &mPhysicalDeviceInfoMap[physicalId],
168 mSupportNativeZoomRatio, usePrecorrectArray);
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700169 }
170 }
171
Yifan Hongf79b5542017-04-11 14:44:25 -0700172 std::shared_ptr<RequestMetadataQueue> queue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700173 auto requestQueueRet = session->getCaptureRequestMetadataQueue(
174 [&queue](const auto& descriptor) {
175 queue = std::make_shared<RequestMetadataQueue>(descriptor);
176 if (!queue->isValid() || queue->availableToWrite() <= 0) {
177 ALOGE("HAL returns empty request metadata fmq, not use it");
178 queue = nullptr;
179 // don't use the queue onwards.
180 }
181 });
182 if (!requestQueueRet.isOk()) {
183 ALOGE("Transaction error when getting request metadata fmq: %s, not use it",
184 requestQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700185 return DEAD_OBJECT;
Yifan Hongf79b5542017-04-11 14:44:25 -0700186 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700187
188 std::unique_ptr<ResultMetadataQueue>& resQueue = mResultMetadataQueue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700189 auto resultQueueRet = session->getCaptureResultMetadataQueue(
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700190 [&resQueue](const auto& descriptor) {
191 resQueue = std::make_unique<ResultMetadataQueue>(descriptor);
192 if (!resQueue->isValid() || resQueue->availableToWrite() <= 0) {
Yifan Honga640c5a2017-04-12 16:30:31 -0700193 ALOGE("HAL returns empty result metadata fmq, not use it");
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700194 resQueue = nullptr;
195 // Don't use the resQueue onwards.
Yifan Honga640c5a2017-04-12 16:30:31 -0700196 }
197 });
198 if (!resultQueueRet.isOk()) {
199 ALOGE("Transaction error when getting result metadata queue from camera session: %s",
200 resultQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700201 return DEAD_OBJECT;
Yifan Honga640c5a2017-04-12 16:30:31 -0700202 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700203 IF_ALOGV() {
204 session->interfaceChain([](
205 ::android::hardware::hidl_vec<::android::hardware::hidl_string> interfaceChain) {
206 ALOGV("Session interface chain:");
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800207 for (const auto& iface : interfaceChain) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700208 ALOGV(" %s", iface.c_str());
209 }
210 });
211 }
Yifan Hongf79b5542017-04-11 14:44:25 -0700212
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -0800213 camera_metadata_entry bufMgrMode =
214 mDeviceInfo.find(ANDROID_INFO_SUPPORTED_BUFFER_MANAGEMENT_VERSION);
215 if (bufMgrMode.count > 0) {
216 mUseHalBufManager = (bufMgrMode.data.u8[0] ==
217 ANDROID_INFO_SUPPORTED_BUFFER_MANAGEMENT_VERSION_HIDL_DEVICE_3_5);
218 }
219
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700220 camera_metadata_entry_t capabilities = mDeviceInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
221 for (size_t i = 0; i < capabilities.count; i++) {
222 uint8_t capability = capabilities.data.u8[i];
223 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_OFFLINE_PROCESSING) {
224 mSupportOfflineProcessing = true;
225 }
226 }
227
228 mInterface = new HalInterface(session, queue, mUseHalBufManager, mSupportOfflineProcessing);
Emilian Peev71c73a22017-03-21 16:35:51 +0000229 std::string providerType;
230 mVendorTagId = manager->getProviderTagIdLocked(mId.string());
Emilian Peevbd8c5032018-02-14 23:05:40 +0000231 mTagMonitor.initialize(mVendorTagId);
232 if (!monitorTags.isEmpty()) {
233 mTagMonitor.parseTagsToMonitor(String8(monitorTags));
234 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800235
Shuzhen Wang268a1362018-10-16 16:32:59 -0700236 // Metadata tags needs fixup for monochrome camera device version less
237 // than 3.5.
238 hardware::hidl_version maxVersion{0,0};
239 res = manager->getHighestSupportedVersion(mId.string(), &maxVersion);
240 if (res != OK) {
241 ALOGE("%s: Error in getting camera device version id: %s (%d)",
242 __FUNCTION__, strerror(-res), res);
243 return res;
244 }
245 int deviceVersion = HARDWARE_DEVICE_API_VERSION(
246 maxVersion.get_major(), maxVersion.get_minor());
247
248 bool isMonochrome = false;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700249 for (size_t i = 0; i < capabilities.count; i++) {
250 uint8_t capability = capabilities.data.u8[i];
Shuzhen Wang268a1362018-10-16 16:32:59 -0700251 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_MONOCHROME) {
252 isMonochrome = true;
253 }
254 }
255 mNeedFixupMonochromeTags = (isMonochrome && deviceVersion < CAMERA_DEVICE_API_VERSION_3_5);
256
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800257 return initializeCommonLocked();
258}
259
260status_t Camera3Device::initializeCommonLocked() {
261
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700262 /** Start up status tracker thread */
263 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800264 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700265 if (res != OK) {
266 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
267 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800268 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700269 mStatusTracker.clear();
270 return res;
271 }
272
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700273 /** Register in-flight map to the status tracker */
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -0700274 mInFlightStatusId = mStatusTracker->addComponent("InflightRequests");
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700275
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -0700276 if (mUseHalBufManager) {
277 res = mRequestBufferSM.initialize(mStatusTracker);
278 if (res != OK) {
279 SET_ERR_L("Unable to start request buffer state machine: %s (%d)",
280 strerror(-res), res);
281 mInterface->close();
282 mStatusTracker.clear();
283 return res;
284 }
285 }
286
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -0800287 /** Create buffer manager */
288 mBufferManager = new Camera3BufferManager();
289
290 Vector<int32_t> sessionParamKeys;
291 camera_metadata_entry_t sessionKeysEntry = mDeviceInfo.find(
292 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
293 if (sessionKeysEntry.count > 0) {
294 sessionParamKeys.insertArrayAt(sessionKeysEntry.data.i32, 0, sessionKeysEntry.count);
295 }
296
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700297 /** Start up request queue thread */
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -0700298 mRequestThread = new RequestThread(
299 this, mStatusTracker, mInterface, sessionParamKeys, mUseHalBufManager);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800300 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800301 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700302 SET_ERR_L("Unable to start request queue thread: %s (%d)",
303 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800304 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800305 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800306 return res;
307 }
308
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700309 mPreparerThread = new PreparerThread();
310
Ruben Brunk183f0562015-08-12 12:55:02 -0700311 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800312 mNextStreamId = 0;
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -0400313 mFakeStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700314 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700315 mPauseStateNotify = false;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800316 mIsInputStreamMultiResolution = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800317
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800318 // Measure the clock domain offset between camera and video/hw_composer
319 camera_metadata_entry timestampSource =
320 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
321 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
322 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
323 mTimestampOffset = getMonoToBoottimeOffset();
324 }
325
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700326 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100327 camera_metadata_entry partialResultsCount =
328 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
329 if (partialResultsCount.count > 0) {
330 mNumPartialResults = partialResultsCount.data.i32[0];
331 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700332 }
333
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800334 bool usePrecorrectArray = DistortionMapper::isDistortionSupported(mDeviceInfo);
335 if (usePrecorrectArray) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700336 res = mDistortionMappers[mId.c_str()].setupStaticInfo(mDeviceInfo);
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -0700337 if (res != OK) {
338 SET_ERR_L("Unable to read necessary calibration fields for distortion correction");
339 return res;
340 }
341 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800342
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800343 mZoomRatioMappers[mId.c_str()] = ZoomRatioMapper(&mDeviceInfo,
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800344 mSupportNativeZoomRatio, usePrecorrectArray);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800345
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800346 if (RotateAndCropMapper::isNeeded(&mDeviceInfo)) {
347 mRotateAndCropMappers.emplace(mId.c_str(), &mDeviceInfo);
348 }
349
Eino-Ville Talvala305cec62020-11-12 14:18:17 -0800350 camera_metadata_entry_t availableTestPatternModes = mDeviceInfo.find(
351 ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES);
352 for (size_t i = 0; i < availableTestPatternModes.count; i++) {
353 if (availableTestPatternModes.data.i32[i] == ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR) {
354 mSupportCameraMute = true;
355 break;
356 }
357 }
358
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800359 return OK;
360}
361
362status_t Camera3Device::disconnect() {
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700363 return disconnectImpl();
364}
365
366status_t Camera3Device::disconnectImpl() {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800367 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700368 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800369
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700370 status_t res = OK;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700371 std::vector<wp<Camera3StreamInterface>> streams;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700372 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800373 Mutex::Autolock il(mInterfaceLock);
374 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
375 {
376 Mutex::Autolock l(mLock);
377 if (mStatus == STATUS_UNINITIALIZED) return res;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700378
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800379 if (mStatus == STATUS_ACTIVE ||
380 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
381 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700382 if (res != OK) {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800383 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700384 // Continue to close device even in case of error
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800385 } else {
386 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
387 if (res != OK) {
388 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
389 maxExpectedDuration);
390 // Continue to close device even in case of error
391 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700392 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700393 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800394
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800395 if (mStatus == STATUS_ERROR) {
396 CLOGE("Shutting down in an error state");
397 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700398
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800399 if (mStatusTracker != NULL) {
400 mStatusTracker->requestExit();
401 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700402
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800403 if (mRequestThread != NULL) {
404 mRequestThread->requestExit();
405 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700406
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800407 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
408 for (size_t i = 0; i < mOutputStreams.size(); i++) {
409 streams.push_back(mOutputStreams[i]);
410 }
411 if (mInputStream != nullptr) {
412 streams.push_back(mInputStream);
413 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700414 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700415 }
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800416 // Joining done without holding mLock and mInterfaceLock, otherwise deadlocks may ensue
417 // as the threads try to access parent state (b/143513518)
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700418 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
419 // HAL may be in a bad state, so waiting for request thread
420 // (which may be stuck in the HAL processCaptureRequest call)
421 // could be dangerous.
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800422 // give up mInterfaceLock here and then lock it again. Could this lead
423 // to other deadlocks
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700424 mRequestThread->join();
425 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700426 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800427 Mutex::Autolock il(mInterfaceLock);
428 if (mStatusTracker != NULL) {
429 mStatusTracker->join();
430 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800431
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800432 HalInterface* interface;
433 {
434 Mutex::Autolock l(mLock);
435 mRequestThread.clear();
436 Mutex::Autolock stLock(mTrackerLock);
437 mStatusTracker.clear();
438 interface = mInterface.get();
439 }
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700440
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800441 // Call close without internal mutex held, as the HAL close may need to
442 // wait on assorted callbacks,etc, to complete before it can return.
443 interface->close();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700444
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800445 flushInflightRequests();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800446
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800447 {
448 Mutex::Autolock l(mLock);
449 mInterface->clear();
450 mOutputStreams.clear();
451 mInputStream.clear();
452 mDeletedStreams.clear();
453 mBufferManager.clear();
454 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
455 }
456
457 for (auto& weakStream : streams) {
458 sp<Camera3StreamInterface> stream = weakStream.promote();
459 if (stream != nullptr) {
460 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
461 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
462 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700463 }
464 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700465 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700466 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800467}
468
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700469// For dumping/debugging only -
470// try to acquire a lock a few times, eventually give up to proceed with
471// debug/dump operations
472bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
473 bool gotLock = false;
474 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
475 if (lock.tryLock() == NO_ERROR) {
476 gotLock = true;
477 break;
478 } else {
479 usleep(kDumpSleepDuration);
480 }
481 }
482 return gotLock;
483}
484
Shuzhen Wang83bff122020-11-20 15:51:39 -0800485camera3::Size Camera3Device::getMaxJpegResolution() const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700486 int32_t maxJpegWidth = 0, maxJpegHeight = 0;
Emilian Peev08dd2452017-04-06 16:55:14 +0100487 const int STREAM_CONFIGURATION_SIZE = 4;
488 const int STREAM_FORMAT_OFFSET = 0;
489 const int STREAM_WIDTH_OFFSET = 1;
490 const int STREAM_HEIGHT_OFFSET = 2;
491 const int STREAM_IS_INPUT_OFFSET = 3;
492 camera_metadata_ro_entry_t availableStreamConfigs =
493 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS);
494 if (availableStreamConfigs.count == 0 ||
495 availableStreamConfigs.count % STREAM_CONFIGURATION_SIZE != 0) {
Shuzhen Wang83bff122020-11-20 15:51:39 -0800496 return camera3::Size(0, 0);
Emilian Peev08dd2452017-04-06 16:55:14 +0100497 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700498
Emilian Peev08dd2452017-04-06 16:55:14 +0100499 // Get max jpeg size (area-wise).
500 for (size_t i=0; i < availableStreamConfigs.count; i+= STREAM_CONFIGURATION_SIZE) {
501 int32_t format = availableStreamConfigs.data.i32[i + STREAM_FORMAT_OFFSET];
502 int32_t width = availableStreamConfigs.data.i32[i + STREAM_WIDTH_OFFSET];
503 int32_t height = availableStreamConfigs.data.i32[i + STREAM_HEIGHT_OFFSET];
504 int32_t isInput = availableStreamConfigs.data.i32[i + STREAM_IS_INPUT_OFFSET];
505 if (isInput == ANDROID_SCALER_AVAILABLE_STREAM_CONFIGURATIONS_OUTPUT
506 && format == HAL_PIXEL_FORMAT_BLOB &&
507 (width * height > maxJpegWidth * maxJpegHeight)) {
508 maxJpegWidth = width;
509 maxJpegHeight = height;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700510 }
511 }
Emilian Peev08dd2452017-04-06 16:55:14 +0100512
Shuzhen Wang83bff122020-11-20 15:51:39 -0800513 return camera3::Size(maxJpegWidth, maxJpegHeight);
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700514}
515
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800516nsecs_t Camera3Device::getMonoToBoottimeOffset() {
517 // try three times to get the clock offset, choose the one
518 // with the minimum gap in measurements.
519 const int tries = 3;
520 nsecs_t bestGap, measured;
521 for (int i = 0; i < tries; ++i) {
522 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
523 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
524 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
525 const nsecs_t gap = tmono2 - tmono;
526 if (i == 0 || gap < bestGap) {
527 bestGap = gap;
528 measured = tbase - ((tmono + tmono2) >> 1);
529 }
530 }
531 return measured;
532}
533
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800534hardware::graphics::common::V1_0::PixelFormat Camera3Device::mapToPixelFormat(
535 int frameworkFormat) {
536 return (hardware::graphics::common::V1_0::PixelFormat) frameworkFormat;
537}
538
539DataspaceFlags Camera3Device::mapToHidlDataspace(
540 android_dataspace dataSpace) {
541 return dataSpace;
542}
543
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700544BufferUsageFlags Camera3Device::mapToConsumerUsage(
Emilian Peev050f5dc2017-05-18 14:43:56 +0100545 uint64_t usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700546 return usage;
547}
548
Emilian Peevf4816702020-04-03 15:44:51 -0700549StreamRotation Camera3Device::mapToStreamRotation(camera_stream_rotation_t rotation) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800550 switch (rotation) {
Emilian Peevf4816702020-04-03 15:44:51 -0700551 case CAMERA_STREAM_ROTATION_0:
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800552 return StreamRotation::ROTATION_0;
Emilian Peevf4816702020-04-03 15:44:51 -0700553 case CAMERA_STREAM_ROTATION_90:
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800554 return StreamRotation::ROTATION_90;
Emilian Peevf4816702020-04-03 15:44:51 -0700555 case CAMERA_STREAM_ROTATION_180:
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800556 return StreamRotation::ROTATION_180;
Emilian Peevf4816702020-04-03 15:44:51 -0700557 case CAMERA_STREAM_ROTATION_270:
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800558 return StreamRotation::ROTATION_270;
559 }
560 ALOGE("%s: Unknown stream rotation %d", __FUNCTION__, rotation);
561 return StreamRotation::ROTATION_0;
562}
563
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800564status_t Camera3Device::mapToStreamConfigurationMode(
Emilian Peevf4816702020-04-03 15:44:51 -0700565 camera_stream_configuration_mode_t operationMode, StreamConfigurationMode *mode) {
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800566 if (mode == nullptr) return BAD_VALUE;
Emilian Peevf4816702020-04-03 15:44:51 -0700567 if (operationMode < CAMERA_VENDOR_STREAM_CONFIGURATION_MODE_START) {
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800568 switch(operationMode) {
Emilian Peevf4816702020-04-03 15:44:51 -0700569 case CAMERA_STREAM_CONFIGURATION_NORMAL_MODE:
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800570 *mode = StreamConfigurationMode::NORMAL_MODE;
571 break;
Emilian Peevf4816702020-04-03 15:44:51 -0700572 case CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE:
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800573 *mode = StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE;
574 break;
575 default:
576 ALOGE("%s: Unknown stream configuration mode %d", __FUNCTION__, operationMode);
577 return BAD_VALUE;
578 }
579 } else {
580 *mode = static_cast<StreamConfigurationMode>(operationMode);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800581 }
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800582 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800583}
584
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800585int Camera3Device::mapToFrameworkFormat(
586 hardware::graphics::common::V1_0::PixelFormat pixelFormat) {
587 return static_cast<uint32_t>(pixelFormat);
588}
589
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700590android_dataspace Camera3Device::mapToFrameworkDataspace(
591 DataspaceFlags dataSpace) {
592 return static_cast<android_dataspace>(dataSpace);
593}
594
Emilian Peev050f5dc2017-05-18 14:43:56 +0100595uint64_t Camera3Device::mapConsumerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700596 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700597 return usage;
598}
599
Emilian Peev050f5dc2017-05-18 14:43:56 +0100600uint64_t Camera3Device::mapProducerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700601 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700602 return usage;
603}
604
Zhijun Hef7da0962014-04-24 13:27:56 -0700605ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700606 // Get max jpeg size (area-wise).
Shuzhen Wang83bff122020-11-20 15:51:39 -0800607 camera3::Size maxJpegResolution = getMaxJpegResolution();
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700608 if (maxJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800609 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
610 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700611 return BAD_VALUE;
612 }
613
Zhijun Hef7da0962014-04-24 13:27:56 -0700614 // Get max jpeg buffer size
615 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700616 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
617 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800618 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
619 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700620 return BAD_VALUE;
621 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700622 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800623 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700624
625 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700626 float scaleFactor = ((float) (width * height)) /
627 (maxJpegResolution.width * maxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800628 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
629 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700630 if (jpegBufferSize > maxJpegBufferSize) {
631 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700632 }
633
634 return jpegBufferSize;
635}
636
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700637ssize_t Camera3Device::getPointCloudBufferSize() const {
638 const int FLOATS_PER_POINT=4;
639 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
640 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800641 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
642 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700643 return BAD_VALUE;
644 }
645 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
646 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
647 return maxBytesForPointCloud;
648}
649
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -0800650ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800651 const int PER_CONFIGURATION_SIZE = 3;
652 const int WIDTH_OFFSET = 0;
653 const int HEIGHT_OFFSET = 1;
654 const int SIZE_OFFSET = 2;
655 camera_metadata_ro_entry rawOpaqueSizes =
656 mDeviceInfo.find(ANDROID_SENSOR_OPAQUE_RAW_SIZE);
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800657 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800658 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800659 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
660 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800661 return BAD_VALUE;
662 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700663
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800664 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
665 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
666 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
667 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
668 }
669 }
670
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800671 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
672 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800673 return BAD_VALUE;
674}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700675
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800676status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
677 ATRACE_CALL();
678 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700679
680 // Try to lock, but continue in case of failure (to avoid blocking in
681 // deadlocks)
682 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
683 bool gotLock = tryLockSpinRightRound(mLock);
684
685 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800686 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
687 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700688 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800689 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
690 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700691
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800692 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700693
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800694 String16 templatesOption("-t");
695 int n = args.size();
696 for (int i = 0; i < n; i++) {
697 if (args[i] == templatesOption) {
698 dumpTemplates = true;
699 }
Emilian Peevbd8c5032018-02-14 23:05:40 +0000700 if (args[i] == TagMonitor::kMonitorOption) {
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700701 if (i + 1 < n) {
702 String8 monitorTags = String8(args[i + 1]);
703 if (monitorTags == "off") {
704 mTagMonitor.disableMonitoring();
705 } else {
706 mTagMonitor.parseTagsToMonitor(monitorTags);
707 }
708 } else {
709 mTagMonitor.disableMonitoring();
710 }
711 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800712 }
713
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800714 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800715
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800716 const char *status =
717 mStatus == STATUS_ERROR ? "ERROR" :
718 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700719 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
720 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800721 mStatus == STATUS_ACTIVE ? "ACTIVE" :
722 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700723
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800724 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700725 if (mStatus == STATUS_ERROR) {
726 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
727 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800728 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800729 const char *mode =
730 mOperatingMode == static_cast<int>(StreamConfigurationMode::NORMAL_MODE) ? "NORMAL" :
731 mOperatingMode == static_cast<int>(
732 StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ? "CONSTRAINED_HIGH_SPEED" :
733 "CUSTOM";
734 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800735
736 if (mInputStream != NULL) {
737 write(fd, lines.string(), lines.size());
738 mInputStream->dump(fd, args);
739 } else {
740 lines.appendFormat(" No input stream.\n");
741 write(fd, lines.string(), lines.size());
742 }
743 for (size_t i = 0; i < mOutputStreams.size(); i++) {
744 mOutputStreams[i]->dump(fd,args);
745 }
746
Zhijun He431503c2016-03-07 17:30:16 -0800747 if (mBufferManager != NULL) {
748 lines = String8(" Camera3 Buffer Manager:\n");
749 write(fd, lines.string(), lines.size());
750 mBufferManager->dump(fd, args);
751 }
Zhijun He125684a2015-12-26 15:07:30 -0800752
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700753 lines = String8(" In-flight requests:\n");
754 if (mInFlightMap.size() == 0) {
755 lines.append(" None\n");
756 } else {
757 for (size_t i = 0; i < mInFlightMap.size(); i++) {
758 InFlightRequest r = mInFlightMap.valueAt(i);
Colin Crosse5729fa2014-03-21 15:04:25 -0700759 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
Greg Kaiser51b882c2020-06-10 05:41:44 +0000760 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
Chien-Yu Chen43e69a62014-11-25 16:38:33 -0800761 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
Greg Kaiser51b882c2020-06-10 05:41:44 +0000762 r.numBuffersLeft);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700763 }
764 }
765 write(fd, lines.string(), lines.size());
766
Shuzhen Wang686f6442017-06-20 16:16:04 -0700767 if (mRequestThread != NULL) {
768 mRequestThread->dumpCaptureRequestLatency(fd,
769 " ProcessCaptureRequest latency histogram:");
770 }
771
Igor Murashkin1e479c02013-09-06 16:55:14 -0700772 {
773 lines = String8(" Last request sent:\n");
774 write(fd, lines.string(), lines.size());
775
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700776 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700777 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
778 }
779
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800780 if (dumpTemplates) {
Emilian Peevf4816702020-04-03 15:44:51 -0700781 const char *templateNames[CAMERA_TEMPLATE_COUNT] = {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800782 "TEMPLATE_PREVIEW",
783 "TEMPLATE_STILL_CAPTURE",
784 "TEMPLATE_VIDEO_RECORD",
785 "TEMPLATE_VIDEO_SNAPSHOT",
786 "TEMPLATE_ZERO_SHUTTER_LAG",
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800787 "TEMPLATE_MANUAL",
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800788 };
789
Emilian Peevf4816702020-04-03 15:44:51 -0700790 for (int i = 1; i < CAMERA_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800791 camera_metadata_t *templateRequest = nullptr;
792 mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -0700793 (camera_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800794 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800795 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800796 lines.append(" Not supported\n");
797 write(fd, lines.string(), lines.size());
798 } else {
799 write(fd, lines.string(), lines.size());
800 dump_indented_camera_metadata(templateRequest,
801 fd, /*verbosity*/2, /*indentation*/8);
802 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800803 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800804 }
805 }
806
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700807 mTagMonitor.dumpMonitoredMetadata(fd);
808
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800809 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800810 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800811 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800812 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800813 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800814
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700815 if (gotLock) mLock.unlock();
816 if (gotInterfaceLock) mInterfaceLock.unlock();
817
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800818 return OK;
819}
820
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800821const CameraMetadata& Camera3Device::infoPhysical(const String8& physicalId) const {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800822 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800823 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
824 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700825 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800826 mStatus == STATUS_ERROR ?
827 "when in error state" : "before init");
828 }
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700829 if (physicalId.isEmpty()) {
830 return mDeviceInfo;
831 } else {
832 std::string id(physicalId.c_str());
833 if (mPhysicalDeviceInfoMap.find(id) != mPhysicalDeviceInfoMap.end()) {
834 return mPhysicalDeviceInfoMap.at(id);
835 } else {
836 ALOGE("%s: Invalid physical camera id %s", __FUNCTION__, physicalId.c_str());
837 return mDeviceInfo;
838 }
839 }
840}
841
842const CameraMetadata& Camera3Device::info() const {
843 String8 emptyId;
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800844 return infoPhysical(emptyId);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800845}
846
Jianing Wei90e59c92014-03-12 18:29:36 -0700847status_t Camera3Device::checkStatusOkToCaptureLocked() {
848 switch (mStatus) {
849 case STATUS_ERROR:
850 CLOGE("Device has encountered a serious error");
851 return INVALID_OPERATION;
852 case STATUS_UNINITIALIZED:
853 CLOGE("Device not initialized");
854 return INVALID_OPERATION;
855 case STATUS_UNCONFIGURED:
856 case STATUS_CONFIGURED:
857 case STATUS_ACTIVE:
858 // OK
859 break;
860 default:
861 SET_ERR_L("Unexpected status: %d", mStatus);
862 return INVALID_OPERATION;
863 }
864 return OK;
865}
866
867status_t Camera3Device::convertMetadataListToRequestListLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000868 const List<const PhysicalCameraSettingsList> &metadataList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700869 const std::list<const SurfaceMap> &surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700870 bool repeating, nsecs_t requestTimeNs,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700871 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700872 if (requestList == NULL) {
873 CLOGE("requestList cannot be NULL.");
874 return BAD_VALUE;
875 }
876
Jianing Weicb0652e2014-03-12 18:29:36 -0700877 int32_t burstId = 0;
Emilian Peevaebbe412018-01-15 13:53:24 +0000878 List<const PhysicalCameraSettingsList>::const_iterator metadataIt = metadataList.begin();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700879 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
880 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
881 ++metadataIt, ++surfaceMapIt) {
882 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700883 if (newRequest == 0) {
884 CLOGE("Can't create capture request");
885 return BAD_VALUE;
886 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700887
Shuzhen Wang9d066012016-09-30 11:30:20 -0700888 newRequest->mRepeating = repeating;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700889 newRequest->mRequestTimeNs = requestTimeNs;
Shuzhen Wang9d066012016-09-30 11:30:20 -0700890
Jianing Weicb0652e2014-03-12 18:29:36 -0700891 // Setup burst Id and request Id
892 newRequest->mResultExtras.burstId = burstId++;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800893 auto requestIdEntry = metadataIt->begin()->metadata.find(ANDROID_REQUEST_ID);
894 if (requestIdEntry.count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700895 CLOGE("RequestID does not exist in metadata");
896 return BAD_VALUE;
897 }
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800898 newRequest->mResultExtras.requestId = requestIdEntry.data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700899
Jianing Wei90e59c92014-03-12 18:29:36 -0700900 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700901
902 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700903 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700904 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
905 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
906 return BAD_VALUE;
907 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700908
909 // Setup batch size if this is a high speed video recording request.
910 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
911 auto firstRequest = requestList->begin();
912 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
913 if (outputStream->isVideoStream()) {
914 (*firstRequest)->mBatchSize = requestList->size();
Yin-Chia Yeh14ef48d2020-02-10 15:06:37 -0800915 outputStream->setBatchSize(requestList->size());
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700916 break;
917 }
918 }
919 }
920
Jianing Wei90e59c92014-03-12 18:29:36 -0700921 return OK;
922}
923
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800924status_t Camera3Device::capture(CameraMetadata &request, int64_t* lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800925 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800926
Emilian Peevaebbe412018-01-15 13:53:24 +0000927 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700928 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000929 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700930
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800931 return captureList(requestsList, surfaceMaps, lastFrameNumber);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700932}
933
Emilian Peevaebbe412018-01-15 13:53:24 +0000934void Camera3Device::convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700935 std::list<const SurfaceMap>& surfaceMaps,
936 const CameraMetadata& request) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000937 PhysicalCameraSettingsList requestList;
938 requestList.push_back({std::string(getId().string()), request});
939 requestsList.push_back(requestList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700940
941 SurfaceMap surfaceMap;
942 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
943 // With no surface list passed in, stream and surface will have 1-to-1
944 // mapping. So the surface index is 0 for each stream in the surfaceMap.
945 for (size_t i = 0; i < streams.count; i++) {
946 surfaceMap[streams.data.i32[i]].push_back(0);
947 }
948 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800949}
950
Jianing Wei90e59c92014-03-12 18:29:36 -0700951status_t Camera3Device::submitRequestsHelper(
Emilian Peevaebbe412018-01-15 13:53:24 +0000952 const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700953 const std::list<const SurfaceMap> &surfaceMaps,
954 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700955 /*out*/
956 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700957 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -0700958 nsecs_t requestTimeNs = systemTime();
959
Jianing Wei90e59c92014-03-12 18:29:36 -0700960 Mutex::Autolock il(mInterfaceLock);
961 Mutex::Autolock l(mLock);
962
963 status_t res = checkStatusOkToCaptureLocked();
964 if (res != OK) {
965 // error logged by previous call
966 return res;
967 }
968
969 RequestList requestList;
970
Shuzhen Wang0129d522016-10-30 22:43:41 -0700971 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700972 repeating, requestTimeNs, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700973 if (res != OK) {
974 // error logged by previous call
975 return res;
976 }
977
978 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700979 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700980 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700981 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700982 }
983
984 if (res == OK) {
985 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
986 if (res != OK) {
987 SET_ERR_L("Can't transition to active in %f seconds!",
988 kActiveTimeout/1e9);
989 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800990 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700991 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700992 } else {
993 CLOGE("Cannot queue request. Impossible.");
994 return BAD_VALUE;
995 }
996
997 return res;
998}
999
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001000hardware::Return<void> Camera3Device::requestStreamBuffers(
1001 const hardware::hidl_vec<hardware::camera::device::V3_5::BufferRequest>& bufReqs,
1002 requestStreamBuffers_cb _hidl_cb) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001003 RequestBufferStates states {
Shuzhen Wang316781a2020-08-18 18:11:01 -07001004 mId, mRequestBufferInterfaceLock, mUseHalBufManager, mOutputStreams, mSessionStatsBuilder,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001005 *this, *mInterface, *this};
1006 camera3::requestStreamBuffers(states, bufReqs, _hidl_cb);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001007 return hardware::Void();
1008}
1009
1010hardware::Return<void> Camera3Device::returnStreamBuffers(
1011 const hardware::hidl_vec<hardware::camera::device::V3_2::StreamBuffer>& buffers) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001012 ReturnBufferStates states {
Shuzhen Wang316781a2020-08-18 18:11:01 -07001013 mId, mUseHalBufManager, mOutputStreams, mSessionStatsBuilder, *mInterface};
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001014 camera3::returnStreamBuffers(states, buffers);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001015 return hardware::Void();
1016}
1017
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001018hardware::Return<void> Camera3Device::processCaptureResult_3_4(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001019 const hardware::hidl_vec<
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001020 hardware::camera::device::V3_4::CaptureResult>& results) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001021 // Ideally we should grab mLock, but that can lead to deadlock, and
1022 // it's not super important to get up to date value of mStatus for this
1023 // warning print, hence skipping the lock here
1024 if (mStatus == STATUS_ERROR) {
1025 // Per API contract, HAL should act as closed after device error
1026 // But mStatus can be set to error by framework as well, so just log
1027 // a warning here.
1028 ALOGW("%s: received capture result in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07001029 }
Yifan Honga640c5a2017-04-12 16:30:31 -07001030
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001031 sp<NotificationListener> listener;
1032 {
1033 std::lock_guard<std::mutex> l(mOutputLock);
1034 listener = mListener.promote();
1035 }
1036
Yifan Honga640c5a2017-04-12 16:30:31 -07001037 if (mProcessCaptureResultLock.tryLock() != OK) {
1038 // This should never happen; it indicates a wrong client implementation
1039 // that doesn't follow the contract. But, we can be tolerant here.
1040 ALOGE("%s: callback overlapped! waiting 1s...",
1041 __FUNCTION__);
1042 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
1043 ALOGE("%s: cannot acquire lock in 1s, dropping results",
1044 __FUNCTION__);
1045 // really don't know what to do, so bail out.
1046 return hardware::Void();
1047 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001048 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001049 CaptureOutputStates states {
1050 mId,
Shuzhen Wangb7b42652020-05-07 11:59:02 -07001051 mInFlightLock, mLastCompletedRegularFrameNumber,
1052 mLastCompletedReprocessFrameNumber, mLastCompletedZslFrameNumber,
1053 mInFlightMap, mOutputLock, mResultQueue, mResultSignal,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001054 mNextShutterFrameNumber,
1055 mNextReprocessShutterFrameNumber, mNextZslStillShutterFrameNumber,
1056 mNextResultFrameNumber,
1057 mNextReprocessResultFrameNumber, mNextZslStillResultFrameNumber,
1058 mUseHalBufManager, mUsePartialResult, mNeedFixupMonochromeTags,
1059 mNumPartialResults, mVendorTagId, mDeviceInfo, mPhysicalDeviceInfoMap,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001060 mResultMetadataQueue, mDistortionMappers, mZoomRatioMappers, mRotateAndCropMappers,
Shuzhen Wang316781a2020-08-18 18:11:01 -07001061 mTagMonitor, mInputStream, mOutputStreams, mSessionStatsBuilder, listener, *this, *this,
1062 *mInterface
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001063 };
1064
Yifan Honga640c5a2017-04-12 16:30:31 -07001065 for (const auto& result : results) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001066 processOneCaptureResultLocked(states, result.v3_2, result.physicalCameraMetadata);
Yifan Honga640c5a2017-04-12 16:30:31 -07001067 }
1068 mProcessCaptureResultLock.unlock();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001069 return hardware::Void();
1070}
1071
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001072// Only one processCaptureResult should be called at a time, so
1073// the locks won't block. The locks are present here simply to enforce this.
1074hardware::Return<void> Camera3Device::processCaptureResult(
1075 const hardware::hidl_vec<
1076 hardware::camera::device::V3_2::CaptureResult>& results) {
1077 hardware::hidl_vec<hardware::camera::device::V3_4::PhysicalCameraMetadata> noPhysMetadata;
1078
1079 // Ideally we should grab mLock, but that can lead to deadlock, and
1080 // it's not super important to get up to date value of mStatus for this
1081 // warning print, hence skipping the lock here
1082 if (mStatus == STATUS_ERROR) {
1083 // Per API contract, HAL should act as closed after device error
1084 // But mStatus can be set to error by framework as well, so just log
1085 // a warning here.
1086 ALOGW("%s: received capture result in error state.", __FUNCTION__);
1087 }
1088
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001089 sp<NotificationListener> listener;
1090 {
1091 std::lock_guard<std::mutex> l(mOutputLock);
1092 listener = mListener.promote();
1093 }
1094
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001095 if (mProcessCaptureResultLock.tryLock() != OK) {
1096 // This should never happen; it indicates a wrong client implementation
1097 // that doesn't follow the contract. But, we can be tolerant here.
1098 ALOGE("%s: callback overlapped! waiting 1s...",
1099 __FUNCTION__);
1100 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
1101 ALOGE("%s: cannot acquire lock in 1s, dropping results",
1102 __FUNCTION__);
1103 // really don't know what to do, so bail out.
1104 return hardware::Void();
1105 }
1106 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001107
1108 CaptureOutputStates states {
1109 mId,
Shuzhen Wangb7b42652020-05-07 11:59:02 -07001110 mInFlightLock, mLastCompletedRegularFrameNumber,
1111 mLastCompletedReprocessFrameNumber, mLastCompletedZslFrameNumber,
1112 mInFlightMap, mOutputLock, mResultQueue, mResultSignal,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001113 mNextShutterFrameNumber,
1114 mNextReprocessShutterFrameNumber, mNextZslStillShutterFrameNumber,
1115 mNextResultFrameNumber,
1116 mNextReprocessResultFrameNumber, mNextZslStillResultFrameNumber,
1117 mUseHalBufManager, mUsePartialResult, mNeedFixupMonochromeTags,
1118 mNumPartialResults, mVendorTagId, mDeviceInfo, mPhysicalDeviceInfoMap,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001119 mResultMetadataQueue, mDistortionMappers, mZoomRatioMappers, mRotateAndCropMappers,
Shuzhen Wang316781a2020-08-18 18:11:01 -07001120 mTagMonitor, mInputStream, mOutputStreams, mSessionStatsBuilder, listener, *this, *this,
1121 *mInterface
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001122 };
1123
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001124 for (const auto& result : results) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001125 processOneCaptureResultLocked(states, result, noPhysMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001126 }
1127 mProcessCaptureResultLock.unlock();
1128 return hardware::Void();
1129}
1130
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001131hardware::Return<void> Camera3Device::notify(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001132 const hardware::hidl_vec<hardware::camera::device::V3_2::NotifyMsg>& msgs) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001133 // Ideally we should grab mLock, but that can lead to deadlock, and
1134 // it's not super important to get up to date value of mStatus for this
1135 // warning print, hence skipping the lock here
1136 if (mStatus == STATUS_ERROR) {
1137 // Per API contract, HAL should act as closed after device error
1138 // But mStatus can be set to error by framework as well, so just log
1139 // a warning here.
1140 ALOGW("%s: received notify message in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07001141 }
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001142
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001143 sp<NotificationListener> listener;
1144 {
1145 std::lock_guard<std::mutex> l(mOutputLock);
1146 listener = mListener.promote();
1147 }
1148
1149 CaptureOutputStates states {
1150 mId,
Shuzhen Wangb7b42652020-05-07 11:59:02 -07001151 mInFlightLock, mLastCompletedRegularFrameNumber,
1152 mLastCompletedReprocessFrameNumber, mLastCompletedZslFrameNumber,
1153 mInFlightMap, mOutputLock, mResultQueue, mResultSignal,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001154 mNextShutterFrameNumber,
1155 mNextReprocessShutterFrameNumber, mNextZslStillShutterFrameNumber,
1156 mNextResultFrameNumber,
1157 mNextReprocessResultFrameNumber, mNextZslStillResultFrameNumber,
1158 mUseHalBufManager, mUsePartialResult, mNeedFixupMonochromeTags,
1159 mNumPartialResults, mVendorTagId, mDeviceInfo, mPhysicalDeviceInfoMap,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001160 mResultMetadataQueue, mDistortionMappers, mZoomRatioMappers, mRotateAndCropMappers,
Shuzhen Wang316781a2020-08-18 18:11:01 -07001161 mTagMonitor, mInputStream, mOutputStreams, mSessionStatsBuilder, listener, *this, *this,
1162 *mInterface
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001163 };
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001164 for (const auto& msg : msgs) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001165 camera3::notify(states, msg);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001166 }
1167 return hardware::Void();
1168}
1169
Emilian Peevaebbe412018-01-15 13:53:24 +00001170status_t Camera3Device::captureList(const List<const PhysicalCameraSettingsList> &requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001171 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -07001172 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001173 ATRACE_CALL();
1174
Emilian Peevaebbe412018-01-15 13:53:24 +00001175 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001176}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001177
Jianing Weicb0652e2014-03-12 18:29:36 -07001178status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
1179 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001180 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001181
Emilian Peevaebbe412018-01-15 13:53:24 +00001182 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001183 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +00001184 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001185
Emilian Peevaebbe412018-01-15 13:53:24 +00001186 return setStreamingRequestList(requestsList, /*surfaceMap*/surfaceMaps,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001187 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001188}
1189
Emilian Peevaebbe412018-01-15 13:53:24 +00001190status_t Camera3Device::setStreamingRequestList(
1191 const List<const PhysicalCameraSettingsList> &requestsList,
1192 const std::list<const SurfaceMap> &surfaceMaps, int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001193 ATRACE_CALL();
1194
Emilian Peevaebbe412018-01-15 13:53:24 +00001195 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001196}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001197
1198sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +00001199 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001200 status_t res;
1201
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001202 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001203 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
1204 // so unilaterally select normal operating mode.
Emilian Peevaebbe412018-01-15 13:53:24 +00001205 res = filterParamsAndConfigureLocked(request.begin()->metadata,
Emilian Peevf4816702020-04-03 15:44:51 -07001206 CAMERA_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001207 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001208 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001209 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001210 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001211 } else if (mStatus == STATUS_UNCONFIGURED) {
1212 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001213 CLOGE("No streams configured");
1214 return NULL;
1215 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001216 }
1217
Shuzhen Wang0129d522016-10-30 22:43:41 -07001218 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001219 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001220}
1221
Jianing Weicb0652e2014-03-12 18:29:36 -07001222status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001223 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001224 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001225 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001226
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001227 switch (mStatus) {
1228 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001229 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001230 return INVALID_OPERATION;
1231 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001232 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001233 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001234 case STATUS_UNCONFIGURED:
1235 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001236 case STATUS_ACTIVE:
1237 // OK
1238 break;
1239 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001240 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001241 return INVALID_OPERATION;
1242 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001243 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -07001244
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001245 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001246}
1247
1248status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
1249 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001250 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001251
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001252 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001253}
1254
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001255status_t Camera3Device::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -08001256 uint32_t width, uint32_t height, int format, bool isMultiResolution, int *id) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001257 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001258 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001259 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001260 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001261 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
1262 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001263
1264 status_t res;
1265 bool wasActive = false;
1266
1267 switch (mStatus) {
1268 case STATUS_ERROR:
1269 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
1270 return INVALID_OPERATION;
1271 case STATUS_UNINITIALIZED:
1272 ALOGE("%s: Device not initialized", __FUNCTION__);
1273 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001274 case STATUS_UNCONFIGURED:
1275 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001276 // OK
1277 break;
1278 case STATUS_ACTIVE:
1279 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001280 res = internalPauseAndWaitLocked(maxExpectedDuration);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001281 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001282 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001283 return res;
1284 }
1285 wasActive = true;
1286 break;
1287 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001288 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001289 return INVALID_OPERATION;
1290 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001291 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001292
1293 if (mInputStream != 0) {
1294 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
1295 return INVALID_OPERATION;
1296 }
1297
1298 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
1299 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001300 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001301
1302 mInputStream = newStream;
Shuzhen Wang83bff122020-11-20 15:51:39 -08001303 mIsInputStreamMultiResolution = isMultiResolution;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001304
1305 *id = mNextStreamId++;
1306
1307 // Continue captures if active at start
1308 if (wasActive) {
1309 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001310 // Reuse current operating mode and session parameters for new stream config
1311 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001312 if (res != OK) {
1313 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1314 __FUNCTION__, mNextStreamId, strerror(-res), res);
1315 return res;
1316 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001317 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001318 }
1319
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001320 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001321 return OK;
1322}
1323
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001324status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001325 uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -07001326 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001327 const String8& physicalCameraId,
Shuzhen Wang83bff122020-11-20 15:51:39 -08001328 std::vector<int> *surfaceIds, int streamSetId, bool isShared,
1329 bool isMultiResolution, uint64_t consumerUsage) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001330 ATRACE_CALL();
1331
1332 if (consumer == nullptr) {
1333 ALOGE("%s: consumer must not be null", __FUNCTION__);
1334 return BAD_VALUE;
1335 }
1336
1337 std::vector<sp<Surface>> consumers;
1338 consumers.push_back(consumer);
1339
1340 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001341 format, dataSpace, rotation, id, physicalCameraId, surfaceIds, streamSetId,
Shuzhen Wang83bff122020-11-20 15:51:39 -08001342 isShared, isMultiResolution, consumerUsage);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001343}
1344
1345status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1346 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -07001347 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001348 const String8& physicalCameraId,
Shuzhen Wang83bff122020-11-20 15:51:39 -08001349 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
1350 uint64_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001351 ATRACE_CALL();
Emilian Peev40ead602017-09-26 15:46:36 +01001352
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001353 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001354 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001355 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001356 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wang83bff122020-11-20 15:51:39 -08001357 " consumer usage %" PRIu64 ", isShared %d, physicalCameraId %s, isMultiResolution %d",
1358 mId.string(), mNextStreamId, width, height, format, dataSpace, rotation,
1359 consumerUsage, isShared, physicalCameraId.string(), isMultiResolution);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001360
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001361 status_t res;
1362 bool wasActive = false;
1363
1364 switch (mStatus) {
1365 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001366 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001367 return INVALID_OPERATION;
1368 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001369 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001370 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001371 case STATUS_UNCONFIGURED:
1372 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001373 // OK
1374 break;
1375 case STATUS_ACTIVE:
1376 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001377 res = internalPauseAndWaitLocked(maxExpectedDuration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001378 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001379 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001380 return res;
1381 }
1382 wasActive = true;
1383 break;
1384 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001385 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001386 return INVALID_OPERATION;
1387 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001388 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001389
1390 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001391
Shuzhen Wang0129d522016-10-30 22:43:41 -07001392 if (consumers.size() == 0 && !hasDeferredConsumer) {
1393 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1394 return BAD_VALUE;
1395 }
Zhijun He5d677d12016-05-29 16:52:39 -07001396
Shuzhen Wang0129d522016-10-30 22:43:41 -07001397 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001398 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1399 return BAD_VALUE;
1400 }
1401
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001402 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001403 ssize_t blobBufferSize;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001404 if (dataSpace == HAL_DATASPACE_DEPTH) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001405 blobBufferSize = getPointCloudBufferSize();
1406 if (blobBufferSize <= 0) {
1407 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1408 return BAD_VALUE;
1409 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001410 } else if (dataSpace == static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
1411 blobBufferSize = width * height;
1412 } else {
1413 blobBufferSize = getJpegBufferSize(width, height);
1414 if (blobBufferSize <= 0) {
1415 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1416 return BAD_VALUE;
1417 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001418 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001419 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001420 width, height, blobBufferSize, format, dataSpace, rotation,
Shuzhen Wang83bff122020-11-20 15:51:39 -08001421 mTimestampOffset, physicalCameraId, streamSetId, isMultiResolution);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001422 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
1423 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height);
1424 if (rawOpaqueBufferSize <= 0) {
1425 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1426 return BAD_VALUE;
1427 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001428 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001429 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
Shuzhen Wang83bff122020-11-20 15:51:39 -08001430 mTimestampOffset, physicalCameraId, streamSetId, isMultiResolution);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001431 } else if (isShared) {
1432 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1433 width, height, format, consumerUsage, dataSpace, rotation,
Shuzhen Wang83bff122020-11-20 15:51:39 -08001434 mTimestampOffset, physicalCameraId, streamSetId, mUseHalBufManager);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001435 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001436 newStream = new Camera3OutputStream(mNextStreamId,
1437 width, height, format, consumerUsage, dataSpace, rotation,
Shuzhen Wang83bff122020-11-20 15:51:39 -08001438 mTimestampOffset, physicalCameraId, streamSetId, isMultiResolution);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001439 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001440 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001441 width, height, format, dataSpace, rotation,
Shuzhen Wang83bff122020-11-20 15:51:39 -08001442 mTimestampOffset, physicalCameraId, streamSetId, isMultiResolution);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001443 }
Emilian Peev40ead602017-09-26 15:46:36 +01001444
1445 size_t consumerCount = consumers.size();
1446 for (size_t i = 0; i < consumerCount; i++) {
1447 int id = newStream->getSurfaceId(consumers[i]);
1448 if (id < 0) {
1449 SET_ERR_L("Invalid surface id");
1450 return BAD_VALUE;
1451 }
1452 if (surfaceIds != nullptr) {
1453 surfaceIds->push_back(id);
1454 }
1455 }
1456
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001457 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001458
Emilian Peev08dd2452017-04-06 16:55:14 +01001459 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001460
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -08001461 newStream->setImageDumpMask(mImageDumpMask);
1462
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001463 res = mOutputStreams.add(mNextStreamId, newStream);
1464 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001465 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001466 return res;
1467 }
1468
Shuzhen Wang316781a2020-08-18 18:11:01 -07001469 mSessionStatsBuilder.addStream(mNextStreamId);
1470
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001471 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001472 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001473
1474 // Continue captures if active at start
1475 if (wasActive) {
1476 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001477 // Reuse current operating mode and session parameters for new stream config
1478 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001479 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001480 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1481 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001482 return res;
1483 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001484 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001485 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001486 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001487 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001488}
1489
Emilian Peev710c1422017-08-30 11:19:38 +01001490status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001491 ATRACE_CALL();
Emilian Peev710c1422017-08-30 11:19:38 +01001492 if (nullptr == streamInfo) {
1493 return BAD_VALUE;
1494 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001495 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001496 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001497
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001498 switch (mStatus) {
1499 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001500 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001501 return INVALID_OPERATION;
1502 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001503 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001504 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001505 case STATUS_UNCONFIGURED:
1506 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001507 case STATUS_ACTIVE:
1508 // OK
1509 break;
1510 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001511 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001512 return INVALID_OPERATION;
1513 }
1514
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001515 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
1516 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001517 CLOGE("Stream %d is unknown", id);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001518 return BAD_VALUE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001519 }
1520
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001521 streamInfo->width = stream->getWidth();
1522 streamInfo->height = stream->getHeight();
1523 streamInfo->format = stream->getFormat();
1524 streamInfo->dataSpace = stream->getDataSpace();
1525 streamInfo->formatOverridden = stream->isFormatOverridden();
1526 streamInfo->originalFormat = stream->getOriginalFormat();
1527 streamInfo->dataSpaceOverridden = stream->isDataSpaceOverridden();
1528 streamInfo->originalDataSpace = stream->getOriginalDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001529 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001530}
1531
1532status_t Camera3Device::setStreamTransform(int id,
1533 int transform) {
1534 ATRACE_CALL();
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<Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
1556 if (stream == nullptr) {
1557 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001558 return BAD_VALUE;
1559 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001560 return stream->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001561}
1562
1563status_t Camera3Device::deleteStream(int id) {
1564 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001565 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001566 Mutex::Autolock l(mLock);
1567 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001568
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001569 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001570
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001571 // CameraDevice semantics require device to already be idle before
1572 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001573 if (mStatus == STATUS_ACTIVE) {
Yin-Chia Yeh693047d2018-03-08 12:14:19 -08001574 ALOGW("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001575 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001576 }
1577
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001578 if (mStatus == STATUS_ERROR) {
1579 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1580 __FUNCTION__, mId.string());
1581 return -EBUSY;
1582 }
1583
Igor Murashkin2fba5842013-04-22 14:03:54 -07001584 sp<Camera3StreamInterface> deletedStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001585 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001586 if (mInputStream != NULL && id == mInputStream->getId()) {
1587 deletedStream = mInputStream;
1588 mInputStream.clear();
1589 } else {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001590 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001591 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001592 return BAD_VALUE;
1593 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001594 mSessionStatsBuilder.removeStream(id);
Zhijun He5f446352014-01-22 09:49:33 -08001595 }
1596
1597 // Delete output stream or the output part of a bi-directional stream.
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001598 if (stream != nullptr) {
1599 deletedStream = stream;
1600 mOutputStreams.remove(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001601 }
1602
1603 // Free up the stream endpoint so that it can be used by some other stream
1604 res = deletedStream->disconnect();
1605 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001606 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001607 // fall through since we want to still list the stream as deleted.
1608 }
1609 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001610 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001611
1612 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001613}
1614
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001615status_t Camera3Device::configureStreams(const CameraMetadata& sessionParams, int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001616 ATRACE_CALL();
1617 ALOGV("%s: E", __FUNCTION__);
1618
1619 Mutex::Autolock il(mInterfaceLock);
1620 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001621
Emilian Peev811d2952018-05-25 11:08:40 +01001622 // In case the client doesn't include any session parameter, try a
1623 // speculative configuration using the values from the last cached
1624 // default request.
1625 if (sessionParams.isEmpty() &&
Emilian Peevf4816702020-04-03 15:44:51 -07001626 ((mLastTemplateId > 0) && (mLastTemplateId < CAMERA_TEMPLATE_COUNT)) &&
Emilian Peev811d2952018-05-25 11:08:40 +01001627 (!mRequestTemplateCache[mLastTemplateId].isEmpty())) {
1628 ALOGV("%s: Speculative session param configuration with template id: %d", __func__,
1629 mLastTemplateId);
1630 return filterParamsAndConfigureLocked(mRequestTemplateCache[mLastTemplateId],
1631 operatingMode);
1632 }
1633
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001634 return filterParamsAndConfigureLocked(sessionParams, operatingMode);
1635}
1636
1637status_t Camera3Device::filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
1638 int operatingMode) {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001639 //Filter out any incoming session parameters
1640 const CameraMetadata params(sessionParams);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001641 camera_metadata_entry_t availableSessionKeys = mDeviceInfo.find(
1642 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001643 CameraMetadata filteredParams(availableSessionKeys.count);
1644 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
1645 filteredParams.getAndLock());
1646 set_camera_metadata_vendor_id(meta, mVendorTagId);
1647 filteredParams.unlock(meta);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001648 if (availableSessionKeys.count > 0) {
1649 for (size_t i = 0; i < availableSessionKeys.count; i++) {
1650 camera_metadata_ro_entry entry = params.find(
1651 availableSessionKeys.data.i32[i]);
1652 if (entry.count > 0) {
1653 filteredParams.update(entry);
1654 }
1655 }
1656 }
1657
1658 return configureStreamsLocked(operatingMode, filteredParams);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001659}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001660
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001661status_t Camera3Device::getInputBufferProducer(
1662 sp<IGraphicBufferProducer> *producer) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001663 ATRACE_CALL();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001664 Mutex::Autolock il(mInterfaceLock);
1665 Mutex::Autolock l(mLock);
1666
1667 if (producer == NULL) {
1668 return BAD_VALUE;
1669 } else if (mInputStream == NULL) {
1670 return INVALID_OPERATION;
1671 }
1672
1673 return mInputStream->getInputBufferProducer(producer);
1674}
1675
Emilian Peevf4816702020-04-03 15:44:51 -07001676status_t Camera3Device::createDefaultRequest(camera_request_template_t templateId,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001677 CameraMetadata *request) {
1678 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001679 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001680
Emilian Peevf4816702020-04-03 15:44:51 -07001681 if (templateId <= 0 || templateId >= CAMERA_TEMPLATE_COUNT) {
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001682 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
Jayant Chowdhary12361932018-08-27 14:46:13 -07001683 CameraThreadState::getCallingUid(), nullptr, 0);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001684 return BAD_VALUE;
1685 }
1686
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001687 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001688
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001689 {
1690 Mutex::Autolock l(mLock);
1691 switch (mStatus) {
1692 case STATUS_ERROR:
1693 CLOGE("Device has encountered a serious error");
1694 return INVALID_OPERATION;
1695 case STATUS_UNINITIALIZED:
1696 CLOGE("Device is not initialized!");
1697 return INVALID_OPERATION;
1698 case STATUS_UNCONFIGURED:
1699 case STATUS_CONFIGURED:
1700 case STATUS_ACTIVE:
1701 // OK
1702 break;
1703 default:
1704 SET_ERR_L("Unexpected status: %d", mStatus);
1705 return INVALID_OPERATION;
1706 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001707
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001708 if (!mRequestTemplateCache[templateId].isEmpty()) {
1709 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001710 mLastTemplateId = templateId;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001711 return OK;
1712 }
Zhijun Hea1530f12014-09-14 12:44:20 -07001713 }
1714
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001715 camera_metadata_t *rawRequest;
1716 status_t res = mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -07001717 (camera_request_template_t) templateId, &rawRequest);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001718
1719 {
1720 Mutex::Autolock l(mLock);
1721 if (res == BAD_VALUE) {
1722 ALOGI("%s: template %d is not supported on this camera device",
1723 __FUNCTION__, templateId);
1724 return res;
1725 } else if (res != OK) {
1726 CLOGE("Unable to construct request template %d: %s (%d)",
1727 templateId, strerror(-res), res);
1728 return res;
1729 }
1730
1731 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
1732 mRequestTemplateCache[templateId].acquire(rawRequest);
1733
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08001734 // Override the template request with zoomRatioMapper
1735 res = mZoomRatioMappers[mId.c_str()].initZoomRatioInTemplate(
1736 &mRequestTemplateCache[templateId]);
1737 if (res != OK) {
1738 CLOGE("Failed to update zoom ratio for template %d: %s (%d)",
1739 templateId, strerror(-res), res);
1740 return res;
1741 }
1742
Shuzhen Wangd25dc972020-03-24 17:11:43 -07001743 // Fill in JPEG_QUALITY if not available
1744 if (!mRequestTemplateCache[templateId].exists(ANDROID_JPEG_QUALITY)) {
1745 static const uint8_t kDefaultJpegQuality = 95;
1746 mRequestTemplateCache[templateId].update(ANDROID_JPEG_QUALITY,
1747 &kDefaultJpegQuality, 1);
1748 }
1749
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001750 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001751 mLastTemplateId = templateId;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001752 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001753 return OK;
1754}
1755
1756status_t Camera3Device::waitUntilDrained() {
1757 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001758 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001759 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001760 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001761
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001762 return waitUntilDrainedLocked(maxExpectedDuration);
Zhijun He69a37482014-03-23 18:44:49 -07001763}
1764
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001765status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001766 switch (mStatus) {
1767 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001768 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001769 ALOGV("%s: Already idle", __FUNCTION__);
1770 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001771 case STATUS_CONFIGURED:
1772 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001773 case STATUS_ERROR:
1774 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001775 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001776 break;
1777 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001778 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001779 return INVALID_OPERATION;
1780 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001781 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
1782 maxExpectedDuration);
1783 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001784 if (res != OK) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07001785 mStatusTracker->dumpActiveComponents();
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001786 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1787 res);
1788 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001789 return res;
1790}
1791
Ruben Brunk183f0562015-08-12 12:55:02 -07001792
1793void Camera3Device::internalUpdateStatusLocked(Status status) {
1794 mStatus = status;
1795 mRecentStatusUpdates.add(mStatus);
1796 mStatusChanged.broadcast();
1797}
1798
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001799// Pause to reconfigure
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001800status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
Emilian Peeve86358b2019-02-15 13:51:39 -08001801 if (mRequestThread.get() != nullptr) {
1802 mRequestThread->setPaused(true);
1803 } else {
1804 return NO_INIT;
1805 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001806
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001807 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1808 maxExpectedDuration);
1809 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001810 if (res != OK) {
1811 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001812 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001813 }
1814
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001815 return res;
1816}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001817
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001818// Resume after internalPauseAndWaitLocked
1819status_t Camera3Device::internalResumeLocked() {
1820 status_t res;
1821
1822 mRequestThread->setPaused(false);
1823
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001824 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1825 kActiveTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001826 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1827 if (res != OK) {
1828 SET_ERR_L("Can't transition to active in %f seconds!",
1829 kActiveTimeout/1e9);
1830 }
1831 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001832 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001833}
1834
Ruben Brunk183f0562015-08-12 12:55:02 -07001835status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001836 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001837
1838 size_t startIndex = 0;
1839 if (mStatusWaiters == 0) {
1840 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1841 // this status list
1842 mRecentStatusUpdates.clear();
1843 } else {
1844 // If other threads are waiting on updates to this status list, set the position of the
1845 // first element that this list will check rather than clearing the list.
1846 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001847 }
1848
Ruben Brunk183f0562015-08-12 12:55:02 -07001849 mStatusWaiters++;
1850
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001851 bool signalPipelineDrain = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001852 if (!active && mUseHalBufManager) {
1853 auto streamIds = mOutputStreams.getStreamIds();
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001854 if (mStatus == STATUS_ACTIVE) {
1855 mRequestThread->signalPipelineDrain(streamIds);
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001856 signalPipelineDrain = true;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001857 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001858 mRequestBufferSM.onWaitUntilIdle();
1859 }
1860
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001861 bool stateSeen = false;
1862 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001863 if (active == (mStatus == STATUS_ACTIVE)) {
1864 // Desired state is current
1865 break;
1866 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001867
1868 res = mStatusChanged.waitRelative(mLock, timeout);
1869 if (res != OK) break;
1870
Ruben Brunk183f0562015-08-12 12:55:02 -07001871 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1872 // transitions.
1873 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1874 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1875 __FUNCTION__);
1876
1877 // Encountered desired state since we began waiting
1878 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001879 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1880 stateSeen = true;
1881 break;
1882 }
1883 }
1884 } while (!stateSeen);
1885
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001886 if (signalPipelineDrain) {
1887 mRequestThread->resetPipelineDrain();
1888 }
1889
Ruben Brunk183f0562015-08-12 12:55:02 -07001890 mStatusWaiters--;
1891
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001892 return res;
1893}
1894
1895
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001896status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001897 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001898 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001899
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001900 if (listener != NULL && mListener != NULL) {
1901 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1902 }
1903 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001904 mRequestThread->setNotificationListener(listener);
1905 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001906
1907 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001908}
1909
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001910bool Camera3Device::willNotify3A() {
1911 return false;
1912}
1913
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001914status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001915 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001916 std::unique_lock<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001917
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001918 while (mResultQueue.empty()) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001919 auto st = mResultSignal.wait_for(l, std::chrono::nanoseconds(timeout));
1920 if (st == std::cv_status::timeout) {
1921 return TIMED_OUT;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001922 }
1923 }
1924 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001925}
1926
Jianing Weicb0652e2014-03-12 18:29:36 -07001927status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001928 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001929 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001930
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001931 if (mResultQueue.empty()) {
1932 return NOT_ENOUGH_DATA;
1933 }
1934
Jianing Weicb0652e2014-03-12 18:29:36 -07001935 if (frame == NULL) {
1936 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1937 return BAD_VALUE;
1938 }
1939
1940 CaptureResult &result = *(mResultQueue.begin());
1941 frame->mResultExtras = result.mResultExtras;
1942 frame->mMetadata.acquire(result.mMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001943 frame->mPhysicalMetadatas = std::move(result.mPhysicalMetadatas);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001944 mResultQueue.erase(mResultQueue.begin());
1945
1946 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001947}
1948
1949status_t Camera3Device::triggerAutofocus(uint32_t id) {
1950 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001951 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001952
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001953 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1954 // Mix-in this trigger into the next request and only the next request.
1955 RequestTrigger trigger[] = {
1956 {
1957 ANDROID_CONTROL_AF_TRIGGER,
1958 ANDROID_CONTROL_AF_TRIGGER_START
1959 },
1960 {
1961 ANDROID_CONTROL_AF_TRIGGER_ID,
1962 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001963 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001964 };
1965
1966 return mRequestThread->queueTrigger(trigger,
1967 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001968}
1969
1970status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1971 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001972 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001973
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001974 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1975 // Mix-in this trigger into the next request and only the next request.
1976 RequestTrigger trigger[] = {
1977 {
1978 ANDROID_CONTROL_AF_TRIGGER,
1979 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1980 },
1981 {
1982 ANDROID_CONTROL_AF_TRIGGER_ID,
1983 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001984 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001985 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001986
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001987 return mRequestThread->queueTrigger(trigger,
1988 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001989}
1990
1991status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1992 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001993 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001994
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001995 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1996 // Mix-in this trigger into the next request and only the next request.
1997 RequestTrigger trigger[] = {
1998 {
1999 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2000 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
2001 },
2002 {
2003 ANDROID_CONTROL_AE_PRECAPTURE_ID,
2004 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002005 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002006 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002007
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002008 return mRequestThread->queueTrigger(trigger,
2009 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002010}
2011
Jianing Weicb0652e2014-03-12 18:29:36 -07002012status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002013 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002014 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002015 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002016
Zhijun He7ef20392014-04-21 16:04:17 -07002017 {
2018 Mutex::Autolock l(mLock);
Emilian Peeved2ebe42018-09-25 16:59:09 +01002019
2020 // b/116514106 "disconnect()" can get called twice for the same device. The
2021 // camera device will not be initialized during the second run.
2022 if (mStatus == STATUS_UNINITIALIZED) {
2023 return OK;
2024 }
2025
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002026 mRequestThread->clear(/*out*/frameNumber);
Shuzhen Wang316781a2020-08-18 18:11:01 -07002027
2028 // Stop session and stream counter
2029 mSessionStatsBuilder.stopCounter();
Zhijun He7ef20392014-04-21 16:04:17 -07002030 }
2031
Emilian Peev08dd2452017-04-06 16:55:14 +01002032 return mRequestThread->flush();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002033}
2034
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002035status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07002036 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
2037}
2038
2039status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002040 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002041 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002042 Mutex::Autolock il(mInterfaceLock);
2043 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002044
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002045 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
2046 if (stream == nullptr) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002047 CLOGE("Stream %d does not exist", streamId);
2048 return BAD_VALUE;
2049 }
2050
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002051 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002052 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002053 return BAD_VALUE;
2054 }
2055
2056 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002057 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002058 return BAD_VALUE;
2059 }
2060
Ruben Brunkc78ac262015-08-13 17:58:46 -07002061 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002062}
2063
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002064status_t Camera3Device::tearDown(int streamId) {
2065 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002066 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002067 Mutex::Autolock il(mInterfaceLock);
2068 Mutex::Autolock l(mLock);
2069
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002070 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
2071 if (stream == nullptr) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002072 CLOGE("Stream %d does not exist", streamId);
2073 return BAD_VALUE;
2074 }
2075
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002076 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
2077 CLOGE("Stream %d is a target of a in-progress request", streamId);
2078 return BAD_VALUE;
2079 }
2080
2081 return stream->tearDown();
2082}
2083
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002084status_t Camera3Device::addBufferListenerForStream(int streamId,
2085 wp<Camera3StreamBufferListener> listener) {
2086 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002087 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002088 Mutex::Autolock il(mInterfaceLock);
2089 Mutex::Autolock l(mLock);
2090
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002091 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
2092 if (stream == nullptr) {
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002093 CLOGE("Stream %d does not exist", streamId);
2094 return BAD_VALUE;
2095 }
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002096 stream->addBufferListener(listener);
2097
2098 return OK;
2099}
2100
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002101/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002102 * Methods called by subclasses
2103 */
2104
2105void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002106 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -07002107 std::vector<int> streamIds;
2108 std::vector<hardware::CameraStreamStats> streamStats;
2109
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002110 {
2111 // Need mLock to safely update state and synchronize to current
2112 // state of methods in flight.
2113 Mutex::Autolock l(mLock);
2114 // We can get various system-idle notices from the status tracker
2115 // while starting up. Only care about them if we've actually sent
2116 // in some requests recently.
2117 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
2118 return;
2119 }
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08002120 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
2121 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
Ruben Brunk183f0562015-08-12 12:55:02 -07002122 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002123
2124 // Skip notifying listener if we're doing some user-transparent
2125 // state changes
2126 if (mPauseStateNotify) return;
Shuzhen Wang316781a2020-08-18 18:11:01 -07002127
2128 // Populate stream statistics in case of Idle
2129 if (idle) {
2130 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2131 auto stream = mOutputStreams[i];
2132 if (stream.get() == nullptr) continue;
2133 streamIds.push_back(stream->getId());
2134 Camera3Stream* camera3Stream = Camera3Stream::cast(stream->asHalStream());
2135 int64_t usage = 0LL;
2136 if (camera3Stream != nullptr) {
2137 usage = camera3Stream->getUsage();
2138 }
2139 streamStats.emplace_back(stream->getWidth(), stream->getHeight(),
2140 stream->getFormat(), stream->getDataSpace(), usage,
2141 stream->getMaxHalBuffers(),
2142 stream->getMaxTotalBuffers() - stream->getMaxHalBuffers());
2143 }
2144 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002145 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002146
2147 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002148 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002149 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002150 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002151 }
2152 if (idle && listener != NULL) {
Shuzhen Wang316781a2020-08-18 18:11:01 -07002153 // Get session stats from the builder, and notify the listener.
2154 int64_t requestCount, resultErrorCount;
2155 bool deviceError;
2156 std::map<int, StreamStats> streamStatsMap;
2157 mSessionStatsBuilder.buildAndReset(&requestCount, &resultErrorCount,
2158 &deviceError, &streamStatsMap);
2159 for (size_t i = 0; i < streamIds.size(); i++) {
2160 int streamId = streamIds[i];
2161 auto stats = streamStatsMap.find(streamId);
2162 if (stats != streamStatsMap.end()) {
2163 streamStats[i].mRequestCount = stats->second.mRequestedFrameCount;
2164 streamStats[i].mErrorCount = stats->second.mDroppedFrameCount;
2165 streamStats[i].mStartLatencyMs = stats->second.mStartLatencyMs;
Shuzhen Wangdb8f2782020-10-30 08:43:37 -07002166 streamStats[i].mHistogramType =
2167 hardware::CameraStreamStats::HISTOGRAM_TYPE_CAPTURE_LATENCY;
2168 streamStats[i].mHistogramBins.assign(
2169 stats->second.mCaptureLatencyBins.begin(),
2170 stats->second.mCaptureLatencyBins.end());
2171 streamStats[i].mHistogramCounts.assign(
2172 stats->second.mCaptureLatencyHistogram.begin(),
2173 stats->second.mCaptureLatencyHistogram.end());
Shuzhen Wang316781a2020-08-18 18:11:01 -07002174 }
2175 }
2176 listener->notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002177 }
2178}
2179
Shuzhen Wang758c2152017-01-10 18:26:18 -08002180status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01002181 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07002182 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08002183 ALOGV("%s: Camera %s: set consumer surface for stream %d",
2184 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01002185
2186 if (surfaceIds == nullptr) {
2187 return BAD_VALUE;
2188 }
2189
Zhijun He5d677d12016-05-29 16:52:39 -07002190 Mutex::Autolock il(mInterfaceLock);
2191 Mutex::Autolock l(mLock);
2192
Shuzhen Wang758c2152017-01-10 18:26:18 -08002193 if (consumers.size() == 0) {
2194 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07002195 return BAD_VALUE;
2196 }
2197
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002198 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2199 if (stream == nullptr) {
Zhijun He5d677d12016-05-29 16:52:39 -07002200 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002201 return BAD_VALUE;
Zhijun He5d677d12016-05-29 16:52:39 -07002202 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002203
2204 // isConsumerConfigurationDeferred will be off after setConsumers
2205 bool isDeferred = stream->isConsumerConfigurationDeferred();
Shuzhen Wang758c2152017-01-10 18:26:18 -08002206 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07002207 if (res != OK) {
2208 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
2209 return res;
2210 }
2211
Emilian Peev40ead602017-09-26 15:46:36 +01002212 for (auto &consumer : consumers) {
2213 int id = stream->getSurfaceId(consumer);
2214 if (id < 0) {
2215 CLOGE("Invalid surface id!");
2216 return BAD_VALUE;
2217 }
2218 surfaceIds->push_back(id);
2219 }
2220
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002221 if (isDeferred) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07002222 if (!stream->isConfiguring()) {
2223 CLOGE("Stream %d was already fully configured.", streamId);
2224 return INVALID_OPERATION;
2225 }
Zhijun He5d677d12016-05-29 16:52:39 -07002226
Shuzhen Wang0129d522016-10-30 22:43:41 -07002227 res = stream->finishConfiguration();
2228 if (res != OK) {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002229 // If finishConfiguration fails due to abandoned surface, do not set
2230 // device to error state.
2231 bool isSurfaceAbandoned =
2232 (res == NO_INIT || res == DEAD_OBJECT) && stream->isAbandoned();
2233 if (!isSurfaceAbandoned) {
2234 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
2235 stream->getId(), strerror(-res), res);
2236 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07002237 return res;
2238 }
Zhijun He5d677d12016-05-29 16:52:39 -07002239 }
2240
2241 return OK;
2242}
2243
Emilian Peev40ead602017-09-26 15:46:36 +01002244status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
2245 const std::vector<OutputStreamInfo> &outputInfo,
2246 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
2247 Mutex::Autolock il(mInterfaceLock);
2248 Mutex::Autolock l(mLock);
2249
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002250 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2251 if (stream == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +01002252 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002253 return BAD_VALUE;
Emilian Peev40ead602017-09-26 15:46:36 +01002254 }
2255
2256 for (const auto &it : removedSurfaceIds) {
2257 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2258 CLOGE("Shared surface still part of a pending request!");
2259 return -EBUSY;
2260 }
2261 }
2262
Emilian Peev40ead602017-09-26 15:46:36 +01002263 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2264 if (res != OK) {
2265 CLOGE("Stream %d failed to update stream (error %d %s) ",
2266 streamId, res, strerror(-res));
2267 if (res == UNKNOWN_ERROR) {
2268 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2269 __FUNCTION__);
2270 }
2271 return res;
2272 }
2273
2274 return res;
2275}
2276
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002277status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2278 Mutex::Autolock il(mInterfaceLock);
2279 Mutex::Autolock l(mLock);
2280
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002281 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2282 if (stream == nullptr) {
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002283 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2284 return BAD_VALUE;
2285 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07002286
2287 if (dropping) {
2288 mSessionStatsBuilder.stopCounter(streamId);
2289 } else {
2290 mSessionStatsBuilder.startCounter(streamId);
2291 }
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002292 return stream->dropBuffers(dropping);
2293}
2294
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002295/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002296 * Camera3Device private methods
2297 */
2298
2299sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002300 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002301 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002302
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002303 sp<CaptureRequest> newRequest = new CaptureRequest();
Emilian Peevaebbe412018-01-15 13:53:24 +00002304 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002305
2306 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002307 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002308 if (inputStreams.count > 0) {
2309 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002310 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002311 CLOGE("Request references unknown input stream %d",
2312 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002313 return NULL;
2314 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002315
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002316 if (mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002317 SET_ERR_L("%s: input stream %d is not configured!",
2318 __FUNCTION__, mInputStream->getId());
2319 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002320 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002321 // Check if stream prepare is blocking requests.
2322 if (mInputStream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002323 CLOGE("Request references an input stream that's being prepared!");
2324 return NULL;
2325 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002326
2327 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002328 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002329 }
2330
2331 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002332 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002333 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002334 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002335 return NULL;
2336 }
2337
2338 for (size_t i = 0; i < streams.count; i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002339 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streams.data.i32[i]);
2340 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002341 CLOGE("Request references unknown stream %d",
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002342 streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002343 return NULL;
2344 }
Zhijun He5d677d12016-05-29 16:52:39 -07002345 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002346 auto iter = surfaceMap.find(streams.data.i32[i]);
2347 if (iter != surfaceMap.end()) {
2348 const std::vector<size_t>& surfaces = iter->second;
2349 for (const auto& surface : surfaces) {
2350 if (stream->isConsumerConfigurationDeferred(surface)) {
2351 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2352 "due to deferred consumer", stream->getId(), surface);
2353 return NULL;
2354 }
2355 }
Yin-Chia Yeh0b287572018-10-15 12:38:13 -07002356 newRequest->mOutputSurfaces[streams.data.i32[i]] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002357 }
2358
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002359 if (stream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002360 SET_ERR_L("%s: stream %d is not configured!", __FUNCTION__, stream->getId());
2361 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002362 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002363 // Check if stream prepare is blocking requests.
2364 if (stream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002365 CLOGE("Request references an output stream that's being prepared!");
2366 return NULL;
2367 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002368
2369 newRequest->mOutputStreams.push(stream);
2370 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002371 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002372 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002373
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002374 auto rotateAndCropEntry =
2375 newRequest->mSettingsList.begin()->metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
2376 if (rotateAndCropEntry.count > 0 &&
2377 rotateAndCropEntry.data.u8[0] == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
2378 newRequest->mRotateAndCropAuto = true;
2379 } else {
2380 newRequest->mRotateAndCropAuto = false;
2381 }
2382
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07002383 auto zoomRatioEntry =
2384 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_ZOOM_RATIO);
2385 if (zoomRatioEntry.count > 0 &&
2386 zoomRatioEntry.data.f[0] == 1.0f) {
2387 newRequest->mZoomRatioIs1x = true;
2388 } else {
2389 newRequest->mZoomRatioIs1x = false;
2390 }
2391
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002392 if (mSupportCameraMute) {
2393 auto testPatternModeEntry =
2394 newRequest->mSettingsList.begin()->metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
2395 newRequest->mOriginalTestPatternMode = testPatternModeEntry.count > 0 ?
2396 testPatternModeEntry.data.i32[0] :
2397 ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;
2398
2399 auto testPatternDataEntry =
2400 newRequest->mSettingsList.begin()->metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
2401 if (testPatternDataEntry.count > 0) {
2402 memcpy(newRequest->mOriginalTestPatternData, testPatternModeEntry.data.i32,
2403 sizeof(newRequest->mOriginalTestPatternData));
2404 } else {
2405 newRequest->mOriginalTestPatternData[0] = 0;
2406 newRequest->mOriginalTestPatternData[1] = 0;
2407 newRequest->mOriginalTestPatternData[2] = 0;
2408 newRequest->mOriginalTestPatternData[3] = 0;
2409 }
2410 }
2411
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002412 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002413}
2414
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002415void Camera3Device::cancelStreamsConfigurationLocked() {
2416 int res = OK;
2417 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2418 res = mInputStream->cancelConfiguration();
2419 if (res != OK) {
2420 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2421 mInputStream->getId(), strerror(-res), res);
2422 }
2423 }
2424
2425 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002426 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002427 if (outputStream->isConfiguring()) {
2428 res = outputStream->cancelConfiguration();
2429 if (res != OK) {
2430 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2431 outputStream->getId(), strerror(-res), res);
2432 }
2433 }
2434 }
2435
2436 // Return state to that at start of call, so that future configures
2437 // properly clean things up
2438 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2439 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002440
2441 res = mPreparerThread->resume();
2442 if (res != OK) {
2443 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2444 }
2445}
2446
Emilian Peev0d0191e2020-04-21 17:01:18 -07002447bool Camera3Device::checkAbandonedStreamsLocked() {
2448 if ((mInputStream.get() != nullptr) && (mInputStream->isAbandoned())) {
2449 return true;
2450 }
2451
2452 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2453 auto stream = mOutputStreams[i];
2454 if ((stream.get() != nullptr) && (stream->isAbandoned())) {
2455 return true;
2456 }
2457 }
2458
2459 return false;
2460}
2461
Emilian Peev3bead5f2020-05-28 17:29:08 -07002462bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams, int clientStatusId) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002463 ATRACE_CALL();
2464 bool ret = false;
2465
Shuzhen Wang316781a2020-08-18 18:11:01 -07002466 nsecs_t startTime = systemTime();
2467
Jayant Chowdhary646c31b2020-01-30 13:09:59 -08002468 Mutex::Autolock il(mInterfaceLock);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002469 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2470
2471 Mutex::Autolock l(mLock);
Emilian Peev0d0191e2020-04-21 17:01:18 -07002472 if (checkAbandonedStreamsLocked()) {
2473 ALOGW("%s: Abandoned stream detected, session parameters can't be applied correctly!",
2474 __FUNCTION__);
2475 return true;
2476 }
2477
Emilian Peev3bead5f2020-05-28 17:29:08 -07002478 status_t rc = NO_ERROR;
2479 bool markClientActive = false;
2480 if (mStatus == STATUS_ACTIVE) {
2481 markClientActive = true;
2482 mPauseStateNotify = true;
2483 mStatusTracker->markComponentIdle(clientStatusId, Fence::NO_FENCE);
2484
2485 rc = internalPauseAndWaitLocked(maxExpectedDuration);
2486 }
2487
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002488 if (rc == NO_ERROR) {
2489 mNeedConfig = true;
2490 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2491 if (rc == NO_ERROR) {
2492 ret = true;
2493 mPauseStateNotify = false;
2494 //Moving to active state while holding 'mLock' is important.
2495 //There could be pending calls to 'create-/deleteStream' which
2496 //will trigger another stream configuration while the already
2497 //present streams end up with outstanding buffers that will
2498 //not get drained.
2499 internalUpdateStatusLocked(STATUS_ACTIVE);
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002500 } else if (rc == DEAD_OBJECT) {
2501 // DEAD_OBJECT can be returned if either the consumer surface is
2502 // abandoned, or the HAL has died.
2503 // - If the HAL has died, configureStreamsLocked call will set
2504 // device to error state,
2505 // - If surface is abandoned, we should not set device to error
2506 // state.
2507 ALOGE("Failed to re-configure camera due to abandoned surface");
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002508 } else {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002509 SET_ERR_L("Failed to re-configure camera: %d", rc);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002510 }
2511 } else {
2512 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2513 }
2514
Shuzhen Wang316781a2020-08-18 18:11:01 -07002515 CameraServiceProxyWrapper::logStreamConfigured(mId, mOperatingMode, true /*internalReconfig*/,
2516 ns2ms(systemTime() - startTime));
2517
Emilian Peev3bead5f2020-05-28 17:29:08 -07002518 if (markClientActive) {
2519 mStatusTracker->markComponentActive(clientStatusId);
2520 }
2521
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002522 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002523}
2524
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002525status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002526 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002527 ATRACE_CALL();
2528 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002529
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002530 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002531 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002532 return INVALID_OPERATION;
2533 }
2534
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002535 if (operatingMode < 0) {
2536 CLOGE("Invalid operating mode: %d", operatingMode);
2537 return BAD_VALUE;
2538 }
2539
2540 bool isConstrainedHighSpeed =
2541 static_cast<int>(StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ==
2542 operatingMode;
2543
2544 if (mOperatingMode != operatingMode) {
2545 mNeedConfig = true;
2546 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2547 mOperatingMode = operatingMode;
2548 }
2549
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002550 // In case called from configureStreams, abort queued input buffers not belonging to
2551 // any pending requests.
2552 if (mInputStream != NULL && notifyRequestThread) {
2553 while (true) {
Emilian Peevf4816702020-04-03 15:44:51 -07002554 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08002555 camera3::Size inputBufferSize;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002556 status_t res = mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08002557 &inputBufferSize, /*respectHalLimit*/ false);
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002558 if (res != OK) {
2559 // Exhausted acquiring all input buffers.
2560 break;
2561 }
2562
Emilian Peevf4816702020-04-03 15:44:51 -07002563 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002564 res = mInputStream->returnInputBuffer(inputBuffer);
2565 if (res != OK) {
2566 ALOGE("%s: %d: couldn't return input buffer while clearing input queue: "
2567 "%s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2568 }
2569 }
2570 }
2571
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002572 if (!mNeedConfig) {
2573 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2574 return OK;
2575 }
2576
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002577 // Workaround for device HALv3.2 or older spec bug - zero streams requires
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002578 // adding a fake stream instead.
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002579 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2580 if (mOutputStreams.size() == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002581 addFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002582 } else {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002583 tryRemoveFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002584 }
2585
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002586 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002587 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002588
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002589 mPreparerThread->pause();
2590
Emilian Peevf4816702020-04-03 15:44:51 -07002591 camera_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002592 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002593 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
Shuzhen Wang83bff122020-11-20 15:51:39 -08002594 config.input_is_multi_resolution = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002595
Emilian Peevf4816702020-04-03 15:44:51 -07002596 Vector<camera3::camera_stream_t*> streams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002597 streams.setCapacity(config.num_streams);
Emilian Peev192ee832018-01-31 14:46:47 +00002598 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002599
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002600
2601 if (mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07002602 camera3::camera_stream_t *inputStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002603 inputStream = mInputStream->startConfiguration();
2604 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002605 CLOGE("Can't start input stream configuration");
2606 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002607 return INVALID_OPERATION;
2608 }
2609 streams.add(inputStream);
Shuzhen Wang83bff122020-11-20 15:51:39 -08002610
2611 config.input_is_multi_resolution = mIsInputStreamMultiResolution;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002612 }
2613
Shuzhen Wang99080502021-03-07 21:08:20 -08002614 mGroupIdPhysicalCameraMap.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002615 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002616
2617 // Don't configure bidi streams twice, nor add them twice to the list
2618 if (mOutputStreams[i].get() ==
2619 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2620
2621 config.num_streams--;
2622 continue;
2623 }
2624
Emilian Peevf4816702020-04-03 15:44:51 -07002625 camera3::camera_stream_t *outputStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002626 outputStream = mOutputStreams[i]->startConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002627 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002628 CLOGE("Can't start output stream configuration");
2629 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002630 return INVALID_OPERATION;
2631 }
2632 streams.add(outputStream);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002633
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002634 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB) {
Emilian Peev192ee832018-01-31 14:46:47 +00002635 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2636 // always occupy the initial entry.
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002637 if (outputStream->data_space == HAL_DATASPACE_V0_JFIF) {
2638 bufferSizes[k] = static_cast<uint32_t>(
2639 getJpegBufferSize(outputStream->width, outputStream->height));
2640 } else if (outputStream->data_space ==
2641 static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
2642 bufferSizes[k] = outputStream->width * outputStream->height;
2643 } else {
2644 ALOGW("%s: Blob dataSpace %d not supported",
2645 __FUNCTION__, outputStream->data_space);
2646 }
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002647 }
Shuzhen Wang99080502021-03-07 21:08:20 -08002648
2649 if (mOutputStreams[i]->isMultiResolution()) {
2650 int32_t streamGroupId = mOutputStreams[i]->getHalStreamGroupId();
2651 const String8& physicalCameraId = mOutputStreams[i]->getPhysicalCameraId();
2652 mGroupIdPhysicalCameraMap[streamGroupId].insert(physicalCameraId);
2653 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002654 }
2655
2656 config.streams = streams.editArray();
2657
2658 // Do the HAL configuration; will potentially touch stream
Shuzhen Wang92653952019-05-07 15:11:43 -07002659 // max_buffers, usage, and priv fields, as well as data_space and format
2660 // fields for IMPLEMENTATION_DEFINED formats.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002661
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002662 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
Emilian Peev192ee832018-01-31 14:46:47 +00002663 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002664 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002665
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002666 if (res == BAD_VALUE) {
2667 // HAL rejected this set of streams as unsupported, clean up config
2668 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002669 CLOGE("Set of requested inputs/outputs not supported by HAL");
2670 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002671 return BAD_VALUE;
2672 } else if (res != OK) {
2673 // Some other kind of error from configure_streams - this is not
2674 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002675 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2676 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002677 return res;
2678 }
2679
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002680 // Finish all stream configuration immediately.
2681 // TODO: Try to relax this later back to lazy completion, which should be
2682 // faster
2683
Igor Murashkin073f8572013-05-02 14:59:28 -07002684 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002685 bool streamReConfigured = false;
2686 res = mInputStream->finishConfiguration(&streamReConfigured);
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002687 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002688 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002689 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002690 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002691 if ((res == NO_INIT || res == DEAD_OBJECT) && mInputStream->isAbandoned()) {
2692 return DEAD_OBJECT;
2693 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002694 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002695 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002696 if (streamReConfigured) {
2697 mInterface->onStreamReConfigured(mInputStream->getId());
2698 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002699 }
2700
2701 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002702 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Zhijun He5d677d12016-05-29 16:52:39 -07002703 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002704 bool streamReConfigured = false;
2705 res = outputStream->finishConfiguration(&streamReConfigured);
Igor Murashkin073f8572013-05-02 14:59:28 -07002706 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002707 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002708 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002709 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002710 if ((res == NO_INIT || res == DEAD_OBJECT) && outputStream->isAbandoned()) {
2711 return DEAD_OBJECT;
2712 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002713 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002714 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002715 if (streamReConfigured) {
2716 mInterface->onStreamReConfigured(outputStream->getId());
2717 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002718 }
2719 }
2720
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002721 // Request thread needs to know to avoid using repeat-last-settings protocol
2722 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002723 if (notifyRequestThread) {
Shuzhen Wang99080502021-03-07 21:08:20 -08002724 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration,
2725 sessionParams, mGroupIdPhysicalCameraMap);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002726 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002727
Zhijun He90f7c372016-08-16 16:19:43 -07002728 char value[PROPERTY_VALUE_MAX];
2729 property_get("camera.fifo.disable", value, "0");
2730 int32_t disableFifo = atoi(value);
2731 if (disableFifo != 1) {
2732 // Boost priority of request thread to SCHED_FIFO.
2733 pid_t requestThreadTid = mRequestThread->getTid();
2734 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002735 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002736 if (res != OK) {
2737 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2738 strerror(-res), res);
2739 } else {
2740 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2741 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002742 }
2743
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002744 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002745 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2746 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2747 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2748 sessionParams.unlock(newSessionParams);
2749 mSessionParams.unlock(currentSessionParams);
2750 if (updateSessionParams) {
2751 mSessionParams = sessionParams;
2752 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002753
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002754 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002755
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002756 internalUpdateStatusLocked((mFakeStreamId == NO_STREAM) ?
Ruben Brunk183f0562015-08-12 12:55:02 -07002757 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002758
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002759 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002760
Zhijun He0a210512014-07-24 13:45:15 -07002761 // tear down the deleted streams after configure streams.
2762 mDeletedStreams.clear();
2763
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002764 auto rc = mPreparerThread->resume();
2765 if (rc != OK) {
2766 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2767 return rc;
2768 }
2769
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002770 if (mFakeStreamId == NO_STREAM) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002771 mRequestBufferSM.onStreamsConfigured();
2772 }
2773
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002774 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002775}
2776
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002777status_t Camera3Device::addFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002778 ATRACE_CALL();
2779 status_t res;
2780
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002781 if (mFakeStreamId != NO_STREAM) {
2782 // Should never be adding a second fake stream when one is already
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002783 // active
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002784 SET_ERR_L("%s: Camera %s: A fake stream already exists!",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002785 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002786 return INVALID_OPERATION;
2787 }
2788
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002789 ALOGV("%s: Camera %s: Adding a fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002790
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002791 sp<Camera3OutputStreamInterface> fakeStream =
2792 new Camera3FakeStream(mNextStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002793
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002794 res = mOutputStreams.add(mNextStreamId, fakeStream);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002795 if (res < 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002796 SET_ERR_L("Can't add fake stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002797 return res;
2798 }
2799
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002800 mFakeStreamId = mNextStreamId;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002801 mNextStreamId++;
2802
2803 return OK;
2804}
2805
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002806status_t Camera3Device::tryRemoveFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002807 ATRACE_CALL();
2808 status_t res;
2809
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002810 if (mFakeStreamId == NO_STREAM) return OK;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002811 if (mOutputStreams.size() == 1) return OK;
2812
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002813 ALOGV("%s: Camera %s: Removing the fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002814
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002815 // Ok, have a fake stream and there's at least one other output stream,
2816 // so remove the fake
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002817
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002818 sp<Camera3StreamInterface> deletedStream = mOutputStreams.get(mFakeStreamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002819 if (deletedStream == nullptr) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002820 SET_ERR_L("Fake stream %d does not appear to exist", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002821 return INVALID_OPERATION;
2822 }
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002823 mOutputStreams.remove(mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002824
2825 // Free up the stream endpoint so that it can be used by some other stream
2826 res = deletedStream->disconnect();
2827 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002828 SET_ERR_L("Can't disconnect deleted fake stream %d", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002829 // fall through since we want to still list the stream as deleted.
2830 }
2831 mDeletedStreams.add(deletedStream);
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002832 mFakeStreamId = NO_STREAM;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002833
2834 return res;
2835}
2836
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002837void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002838 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002839 Mutex::Autolock l(mLock);
2840 va_list args;
2841 va_start(args, fmt);
2842
2843 setErrorStateLockedV(fmt, args);
2844
2845 va_end(args);
2846}
2847
2848void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002849 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002850 Mutex::Autolock l(mLock);
2851 setErrorStateLockedV(fmt, args);
2852}
2853
2854void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2855 va_list args;
2856 va_start(args, fmt);
2857
2858 setErrorStateLockedV(fmt, args);
2859
2860 va_end(args);
2861}
2862
2863void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002864 // Print out all error messages to log
2865 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002866 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002867
2868 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002869 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002870
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002871 mErrorCause = errorCause;
2872
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07002873 if (mRequestThread != nullptr) {
2874 mRequestThread->setPaused(true);
2875 }
Ruben Brunk183f0562015-08-12 12:55:02 -07002876 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002877
2878 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002879 sp<NotificationListener> listener = mListener.promote();
2880 if (listener != NULL) {
2881 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002882 CaptureResultExtras());
Shuzhen Wang316781a2020-08-18 18:11:01 -07002883 mSessionStatsBuilder.onDeviceError();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002884 }
2885
2886 // Save stack trace. View by dumping it later.
2887 CameraTraces::saveTrace();
2888 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002889}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002890
2891/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002892 * In-flight request management
2893 */
2894
Jianing Weicb0652e2014-03-12 18:29:36 -07002895status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002896 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002897 bool hasAppCallback, nsecs_t maxExpectedDuration,
Shuzhen Wang99080502021-03-07 21:08:20 -08002898 const std::set<std::set<String8>>& physicalCameraIds,
2899 bool isStillCapture, bool isZslCapture, bool rotateAndCropAuto,
2900 const std::set<std::string>& cameraIdsWithZoom,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002901 const SurfaceMap& outputSurfaces, nsecs_t requestTimeNs) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002902 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002903 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002904
2905 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002906 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07002907 hasAppCallback, maxExpectedDuration, physicalCameraIds, isStillCapture, isZslCapture,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002908 rotateAndCropAuto, cameraIdsWithZoom, requestTimeNs, outputSurfaces));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002909 if (res < 0) return res;
2910
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002911 if (mInFlightMap.size() == 1) {
Emilian Peev26d975d2018-07-05 14:52:57 +01002912 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2913 // avoid a deadlock during reprocess requests.
2914 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002915 if (mStatusTracker != nullptr) {
2916 mStatusTracker->markComponentActive(mInFlightStatusId);
2917 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002918 }
2919
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002920 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002921 return OK;
2922}
2923
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002924void Camera3Device::onInflightEntryRemovedLocked(nsecs_t duration) {
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002925 // Indicate idle inFlightMap to the status tracker
2926 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002927 mRequestBufferSM.onInflightMapEmpty();
Emilian Peev26d975d2018-07-05 14:52:57 +01002928 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2929 // avoid a deadlock during reprocess requests.
2930 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002931 if (mStatusTracker != nullptr) {
2932 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2933 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002934 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002935 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002936}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002937
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002938void Camera3Device::checkInflightMapLengthLocked() {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002939 // Validation check - if we have too many in-flight frames with long total inflight duration,
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -07002940 // something has likely gone wrong. This might still be legit only if application send in
2941 // a long burst of long exposure requests.
2942 if (mExpectedInflightDuration > kMinWarnInflightDuration) {
2943 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
2944 CLOGW("In-flight list too large: %zu, total inflight duration %" PRIu64,
2945 mInFlightMap.size(), mExpectedInflightDuration);
2946 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2947 kInFlightWarnLimitHighSpeed) {
2948 CLOGW("In-flight list too large for high speed configuration: %zu,"
2949 "total inflight duration %" PRIu64,
2950 mInFlightMap.size(), mExpectedInflightDuration);
2951 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002952 }
2953}
2954
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002955void Camera3Device::onInflightMapFlushedLocked() {
2956 mExpectedInflightDuration = 0;
2957}
2958
2959void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Jayant Chowdharyd4776262020-06-23 23:45:57 -07002960 ATRACE_HFR_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002961 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
2962 mInFlightMap.removeItemsAt(idx, 1);
2963
2964 onInflightEntryRemovedLocked(duration);
2965}
2966
2967
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002968void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002969 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002970 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002971 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002972 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002973 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002974 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002975
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002976 FlushInflightReqStates states {
2977 mId, mInFlightLock, mInFlightMap, mUseHalBufManager,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002978 listener, *this, *mInterface, *this, mSessionStatsBuilder};
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002979
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002980 camera3::flushInflightRequests(states);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002981}
2982
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002983CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002984 ALOGV("%s", __FUNCTION__);
2985
Igor Murashkin1e479c02013-09-06 16:55:14 -07002986 CameraMetadata retVal;
2987
2988 if (mRequestThread != NULL) {
2989 retVal = mRequestThread->getLatestRequest();
2990 }
2991
Igor Murashkin1e479c02013-09-06 16:55:14 -07002992 return retVal;
2993}
2994
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002995void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002996 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata,
2997 const std::unordered_map<std::string, CameraMetadata>& physicalMetadata) {
2998
2999 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata,
3000 physicalMetadata);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003001}
3002
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003003/**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003004 * HalInterface inner class methods
3005 */
3006
Yifan Hongf79b5542017-04-11 14:44:25 -07003007Camera3Device::HalInterface::HalInterface(
3008 sp<ICameraDeviceSession> &session,
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003009 std::shared_ptr<RequestMetadataQueue> queue,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003010 bool useHalBufManager, bool supportOfflineProcessing) :
Yifan Hongf79b5542017-04-11 14:44:25 -07003011 mHidlSession(session),
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003012 mRequestMetadataQueue(queue),
Emilian Peev4ec17882019-01-24 17:16:58 -08003013 mUseHalBufManager(useHalBufManager),
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003014 mIsReconfigurationQuerySupported(true),
3015 mSupportOfflineProcessing(supportOfflineProcessing) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003016 // Check with hardware service manager if we can downcast these interfaces
3017 // Somewhat expensive, so cache the results at startup
Shuzhen Wang83bff122020-11-20 15:51:39 -08003018 auto castResult_3_7 = device::V3_7::ICameraDeviceSession::castFrom(mHidlSession);
3019 if (castResult_3_7.isOk()) {
3020 mHidlSession_3_7 = castResult_3_7;
3021 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003022 auto castResult_3_6 = device::V3_6::ICameraDeviceSession::castFrom(mHidlSession);
3023 if (castResult_3_6.isOk()) {
3024 mHidlSession_3_6 = castResult_3_6;
3025 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003026 auto castResult_3_5 = device::V3_5::ICameraDeviceSession::castFrom(mHidlSession);
3027 if (castResult_3_5.isOk()) {
3028 mHidlSession_3_5 = castResult_3_5;
3029 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003030 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
3031 if (castResult_3_4.isOk()) {
3032 mHidlSession_3_4 = castResult_3_4;
3033 }
3034 auto castResult_3_3 = device::V3_3::ICameraDeviceSession::castFrom(mHidlSession);
3035 if (castResult_3_3.isOk()) {
3036 mHidlSession_3_3 = castResult_3_3;
3037 }
3038}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003039
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003040Camera3Device::HalInterface::HalInterface() :
3041 mUseHalBufManager(false),
3042 mSupportOfflineProcessing(false) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003043
3044Camera3Device::HalInterface::HalInterface(const HalInterface& other) :
Yifan Hongf79b5542017-04-11 14:44:25 -07003045 mHidlSession(other.mHidlSession),
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003046 mRequestMetadataQueue(other.mRequestMetadataQueue),
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003047 mUseHalBufManager(other.mUseHalBufManager),
3048 mSupportOfflineProcessing(other.mSupportOfflineProcessing) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003049
3050bool Camera3Device::HalInterface::valid() {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003051 return (mHidlSession != nullptr);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003052}
3053
3054void Camera3Device::HalInterface::clear() {
Shuzhen Wang83bff122020-11-20 15:51:39 -08003055 mHidlSession_3_7.clear();
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003056 mHidlSession_3_6.clear();
Emilian Peev644a3e12018-11-23 13:52:39 +00003057 mHidlSession_3_5.clear();
Emilian Peev9e740b02018-01-30 18:28:03 +00003058 mHidlSession_3_4.clear();
3059 mHidlSession_3_3.clear();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003060 mHidlSession.clear();
3061}
3062
3063status_t Camera3Device::HalInterface::constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -07003064 camera_request_template_t templateId,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003065 /*out*/ camera_metadata_t **requestTemplate) {
3066 ATRACE_NAME("CameraHal::constructDefaultRequestSettings");
3067 if (!valid()) return INVALID_OPERATION;
3068 status_t res = OK;
3069
Emilian Peev31abd0a2017-05-11 18:37:46 +01003070 common::V1_0::Status status;
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003071
3072 auto requestCallback = [&status, &requestTemplate]
Emilian Peev31abd0a2017-05-11 18:37:46 +01003073 (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003074 status = s;
3075 if (status == common::V1_0::Status::OK) {
3076 const camera_metadata *r =
3077 reinterpret_cast<const camera_metadata_t*>(request.data());
3078 size_t expectedSize = request.size();
3079 int ret = validate_camera_metadata_structure(r, &expectedSize);
3080 if (ret == OK || ret == CAMERA_METADATA_VALIDATION_SHIFTED) {
3081 *requestTemplate = clone_camera_metadata(r);
3082 if (*requestTemplate == nullptr) {
3083 ALOGE("%s: Unable to clone camera metadata received from HAL",
3084 __FUNCTION__);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003085 status = common::V1_0::Status::INTERNAL_ERROR;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003086 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003087 } else {
3088 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
3089 status = common::V1_0::Status::INTERNAL_ERROR;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003090 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003091 }
3092 };
3093 hardware::Return<void> err;
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003094 RequestTemplate id;
3095 switch (templateId) {
Emilian Peevf4816702020-04-03 15:44:51 -07003096 case CAMERA_TEMPLATE_PREVIEW:
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003097 id = RequestTemplate::PREVIEW;
3098 break;
Emilian Peevf4816702020-04-03 15:44:51 -07003099 case CAMERA_TEMPLATE_STILL_CAPTURE:
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003100 id = RequestTemplate::STILL_CAPTURE;
3101 break;
Emilian Peevf4816702020-04-03 15:44:51 -07003102 case CAMERA_TEMPLATE_VIDEO_RECORD:
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003103 id = RequestTemplate::VIDEO_RECORD;
3104 break;
Emilian Peevf4816702020-04-03 15:44:51 -07003105 case CAMERA_TEMPLATE_VIDEO_SNAPSHOT:
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003106 id = RequestTemplate::VIDEO_SNAPSHOT;
3107 break;
Emilian Peevf4816702020-04-03 15:44:51 -07003108 case CAMERA_TEMPLATE_ZERO_SHUTTER_LAG:
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003109 id = RequestTemplate::ZERO_SHUTTER_LAG;
3110 break;
Emilian Peevf4816702020-04-03 15:44:51 -07003111 case CAMERA_TEMPLATE_MANUAL:
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003112 id = RequestTemplate::MANUAL;
3113 break;
3114 default:
3115 // Unknown template ID, or this HAL is too old to support it
3116 return BAD_VALUE;
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003117 }
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003118 err = mHidlSession->constructDefaultRequestSettings(id, requestCallback);
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003119
Emilian Peev31abd0a2017-05-11 18:37:46 +01003120 if (!err.isOk()) {
3121 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3122 res = DEAD_OBJECT;
3123 } else {
3124 res = CameraProviderManager::mapToStatusT(status);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003125 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003126
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003127 return res;
3128}
3129
Emilian Peev4ec17882019-01-24 17:16:58 -08003130bool Camera3Device::HalInterface::isReconfigurationRequired(CameraMetadata& oldSessionParams,
3131 CameraMetadata& newSessionParams) {
3132 // We do reconfiguration by default;
3133 bool ret = true;
3134 if ((mHidlSession_3_5 != nullptr) && mIsReconfigurationQuerySupported) {
3135 android::hardware::hidl_vec<uint8_t> oldParams, newParams;
3136 camera_metadata_t* oldSessioMeta = const_cast<camera_metadata_t*>(
3137 oldSessionParams.getAndLock());
3138 camera_metadata_t* newSessioMeta = const_cast<camera_metadata_t*>(
3139 newSessionParams.getAndLock());
3140 oldParams.setToExternal(reinterpret_cast<uint8_t*>(oldSessioMeta),
3141 get_camera_metadata_size(oldSessioMeta));
3142 newParams.setToExternal(reinterpret_cast<uint8_t*>(newSessioMeta),
3143 get_camera_metadata_size(newSessioMeta));
3144 hardware::camera::common::V1_0::Status callStatus;
3145 bool required;
3146 auto hidlCb = [&callStatus, &required] (hardware::camera::common::V1_0::Status s,
3147 bool requiredFlag) {
3148 callStatus = s;
3149 required = requiredFlag;
3150 };
3151 auto err = mHidlSession_3_5->isReconfigurationRequired(oldParams, newParams, hidlCb);
3152 oldSessionParams.unlock(oldSessioMeta);
3153 newSessionParams.unlock(newSessioMeta);
3154 if (err.isOk()) {
3155 switch (callStatus) {
3156 case hardware::camera::common::V1_0::Status::OK:
3157 ret = required;
3158 break;
3159 case hardware::camera::common::V1_0::Status::METHOD_NOT_SUPPORTED:
3160 mIsReconfigurationQuerySupported = false;
3161 ret = true;
3162 break;
3163 default:
3164 ALOGV("%s: Reconfiguration query failed: %d", __FUNCTION__, callStatus);
3165 ret = true;
3166 }
3167 } else {
3168 ALOGE("%s: Unexpected binder error: %s", __FUNCTION__, err.description().c_str());
3169 ret = true;
3170 }
3171 }
3172
3173 return ret;
3174}
3175
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003176status_t Camera3Device::HalInterface::configureStreams(const camera_metadata_t *sessionParams,
Emilian Peevf4816702020-04-03 15:44:51 -07003177 camera_stream_configuration *config, const std::vector<uint32_t>& bufferSizes) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003178 ATRACE_NAME("CameraHal::configureStreams");
3179 if (!valid()) return INVALID_OPERATION;
3180 status_t res = OK;
3181
Shuzhen Wang83bff122020-11-20 15:51:39 -08003182 if (config->input_is_multi_resolution && mHidlSession_3_7 == nullptr) {
3183 ALOGE("%s: Camera device doesn't support multi-resolution input stream", __FUNCTION__);
3184 return BAD_VALUE;
3185 }
3186
Emilian Peev31abd0a2017-05-11 18:37:46 +01003187 // Convert stream config to HIDL
3188 std::set<int> activeStreams;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003189 device::V3_2::StreamConfiguration requestedConfiguration3_2;
3190 device::V3_4::StreamConfiguration requestedConfiguration3_4;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003191 device::V3_7::StreamConfiguration requestedConfiguration3_7;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003192 requestedConfiguration3_2.streams.resize(config->num_streams);
3193 requestedConfiguration3_4.streams.resize(config->num_streams);
Shuzhen Wang83bff122020-11-20 15:51:39 -08003194 requestedConfiguration3_7.streams.resize(config->num_streams);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003195 for (size_t i = 0; i < config->num_streams; i++) {
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003196 device::V3_2::Stream &dst3_2 = requestedConfiguration3_2.streams[i];
3197 device::V3_4::Stream &dst3_4 = requestedConfiguration3_4.streams[i];
Shuzhen Wang83bff122020-11-20 15:51:39 -08003198 device::V3_7::Stream &dst3_7 = requestedConfiguration3_7.streams[i];
Emilian Peevf4816702020-04-03 15:44:51 -07003199 camera3::camera_stream_t *src = config->streams[i];
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003200
Emilian Peev31abd0a2017-05-11 18:37:46 +01003201 Camera3Stream* cam3stream = Camera3Stream::cast(src);
3202 cam3stream->setBufferFreedListener(this);
3203 int streamId = cam3stream->getId();
3204 StreamType streamType;
3205 switch (src->stream_type) {
Emilian Peevf4816702020-04-03 15:44:51 -07003206 case CAMERA_STREAM_OUTPUT:
Emilian Peev31abd0a2017-05-11 18:37:46 +01003207 streamType = StreamType::OUTPUT;
3208 break;
Emilian Peevf4816702020-04-03 15:44:51 -07003209 case CAMERA_STREAM_INPUT:
Emilian Peev31abd0a2017-05-11 18:37:46 +01003210 streamType = StreamType::INPUT;
3211 break;
3212 default:
3213 ALOGE("%s: Stream %d: Unsupported stream type %d",
3214 __FUNCTION__, streamId, config->streams[i]->stream_type);
3215 return BAD_VALUE;
3216 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003217 dst3_2.id = streamId;
3218 dst3_2.streamType = streamType;
3219 dst3_2.width = src->width;
3220 dst3_2.height = src->height;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003221 dst3_2.usage = mapToConsumerUsage(cam3stream->getUsage());
Emilian Peevf4816702020-04-03 15:44:51 -07003222 dst3_2.rotation = mapToStreamRotation((camera_stream_rotation_t) src->rotation);
Shuzhen Wang92653952019-05-07 15:11:43 -07003223 // For HidlSession version 3.5 or newer, the format and dataSpace sent
3224 // to HAL are original, not the overriden ones.
3225 if (mHidlSession_3_5 != nullptr) {
3226 dst3_2.format = mapToPixelFormat(cam3stream->isFormatOverridden() ?
3227 cam3stream->getOriginalFormat() : src->format);
3228 dst3_2.dataSpace = mapToHidlDataspace(cam3stream->isDataSpaceOverridden() ?
3229 cam3stream->getOriginalDataSpace() : src->data_space);
3230 } else {
3231 dst3_2.format = mapToPixelFormat(src->format);
3232 dst3_2.dataSpace = mapToHidlDataspace(src->data_space);
3233 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003234 dst3_4.v3_2 = dst3_2;
Emilian Peev192ee832018-01-31 14:46:47 +00003235 dst3_4.bufferSize = bufferSizes[i];
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003236 if (src->physical_camera_id != nullptr) {
3237 dst3_4.physicalCameraId = src->physical_camera_id;
3238 }
Shuzhen Wang83bff122020-11-20 15:51:39 -08003239 dst3_7.v3_4 = dst3_4;
3240 dst3_7.groupId = cam3stream->getHalStreamGroupId();
Emilian Peev31abd0a2017-05-11 18:37:46 +01003241
3242 activeStreams.insert(streamId);
3243 // Create Buffer ID map if necessary
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003244 mBufferRecords.tryCreateBufferCache(streamId);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003245 }
3246 // remove BufferIdMap for deleted streams
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003247 mBufferRecords.removeInactiveBufferCaches(activeStreams);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003248
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003249 StreamConfigurationMode operationMode;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003250 res = mapToStreamConfigurationMode(
Emilian Peevf4816702020-04-03 15:44:51 -07003251 (camera_stream_configuration_mode_t) config->operation_mode,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003252 /*out*/ &operationMode);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003253 if (res != OK) {
3254 return res;
3255 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003256 requestedConfiguration3_2.operationMode = operationMode;
3257 requestedConfiguration3_4.operationMode = operationMode;
3258 requestedConfiguration3_4.sessionParams.setToExternal(
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003259 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(sessionParams)),
3260 get_camera_metadata_size(sessionParams));
Shuzhen Wang83bff122020-11-20 15:51:39 -08003261 requestedConfiguration3_7.operationMode = operationMode;
3262 requestedConfiguration3_7.sessionParams.setToExternal(
3263 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(sessionParams)),
3264 get_camera_metadata_size(sessionParams));
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003265
Emilian Peev31abd0a2017-05-11 18:37:46 +01003266 // Invoke configureStreams
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003267 device::V3_3::HalStreamConfiguration finalConfiguration;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003268 device::V3_4::HalStreamConfiguration finalConfiguration3_4;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003269 device::V3_6::HalStreamConfiguration finalConfiguration3_6;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003270 common::V1_0::Status status;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003271
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003272 auto configStream34Cb = [&status, &finalConfiguration3_4]
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003273 (common::V1_0::Status s, const device::V3_4::HalStreamConfiguration& halConfiguration) {
3274 finalConfiguration3_4 = halConfiguration;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003275 status = s;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003276 };
3277
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003278 auto configStream36Cb = [&status, &finalConfiguration3_6]
3279 (common::V1_0::Status s, const device::V3_6::HalStreamConfiguration& halConfiguration) {
3280 finalConfiguration3_6 = halConfiguration;
3281 status = s;
3282 };
3283
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003284 auto postprocConfigStream34 = [&finalConfiguration, &finalConfiguration3_4]
3285 (hardware::Return<void>& err) -> status_t {
3286 if (!err.isOk()) {
3287 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3288 return DEAD_OBJECT;
3289 }
3290 finalConfiguration.streams.resize(finalConfiguration3_4.streams.size());
3291 for (size_t i = 0; i < finalConfiguration3_4.streams.size(); i++) {
3292 finalConfiguration.streams[i] = finalConfiguration3_4.streams[i].v3_3;
3293 }
3294 return OK;
3295 };
3296
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003297 auto postprocConfigStream36 = [&finalConfiguration, &finalConfiguration3_6]
3298 (hardware::Return<void>& err) -> status_t {
3299 if (!err.isOk()) {
3300 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3301 return DEAD_OBJECT;
3302 }
3303 finalConfiguration.streams.resize(finalConfiguration3_6.streams.size());
3304 for (size_t i = 0; i < finalConfiguration3_6.streams.size(); i++) {
3305 finalConfiguration.streams[i] = finalConfiguration3_6.streams[i].v3_4.v3_3;
3306 }
3307 return OK;
3308 };
3309
Shuzhen Wang92653952019-05-07 15:11:43 -07003310 // See which version of HAL we have
Shuzhen Wang83bff122020-11-20 15:51:39 -08003311 if (mHidlSession_3_7 != nullptr) {
3312 ALOGV("%s: v3.7 device found", __FUNCTION__);
3313 requestedConfiguration3_7.streamConfigCounter = mNextStreamConfigCounter++;
3314 requestedConfiguration3_7.multiResolutionInputImage = config->input_is_multi_resolution;
3315 auto err = mHidlSession_3_7->configureStreams_3_7(
3316 requestedConfiguration3_7, configStream36Cb);
3317 res = postprocConfigStream36(err);
3318 if (res != OK) {
3319 return res;
3320 }
3321 } else if (mHidlSession_3_6 != nullptr) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003322 ALOGV("%s: v3.6 device found", __FUNCTION__);
3323 device::V3_5::StreamConfiguration requestedConfiguration3_5;
3324 requestedConfiguration3_5.v3_4 = requestedConfiguration3_4;
3325 requestedConfiguration3_5.streamConfigCounter = mNextStreamConfigCounter++;
3326 auto err = mHidlSession_3_6->configureStreams_3_6(
3327 requestedConfiguration3_5, configStream36Cb);
3328 res = postprocConfigStream36(err);
3329 if (res != OK) {
3330 return res;
3331 }
3332 } else if (mHidlSession_3_5 != nullptr) {
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003333 ALOGV("%s: v3.5 device found", __FUNCTION__);
3334 device::V3_5::StreamConfiguration requestedConfiguration3_5;
3335 requestedConfiguration3_5.v3_4 = requestedConfiguration3_4;
3336 requestedConfiguration3_5.streamConfigCounter = mNextStreamConfigCounter++;
3337 auto err = mHidlSession_3_5->configureStreams_3_5(
3338 requestedConfiguration3_5, configStream34Cb);
3339 res = postprocConfigStream34(err);
3340 if (res != OK) {
3341 return res;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003342 }
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003343 } else if (mHidlSession_3_4 != nullptr) {
3344 // We do; use v3.4 for the call
3345 ALOGV("%s: v3.4 device found", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003346 auto err = mHidlSession_3_4->configureStreams_3_4(
3347 requestedConfiguration3_4, configStream34Cb);
3348 res = postprocConfigStream34(err);
3349 if (res != OK) {
3350 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003351 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003352 } else if (mHidlSession_3_3 != nullptr) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003353 // We do; use v3.3 for the call
3354 ALOGV("%s: v3.3 device found", __FUNCTION__);
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003355 auto err = mHidlSession_3_3->configureStreams_3_3(requestedConfiguration3_2,
Emilian Peev31abd0a2017-05-11 18:37:46 +01003356 [&status, &finalConfiguration]
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003357 (common::V1_0::Status s, const device::V3_3::HalStreamConfiguration& halConfiguration) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003358 finalConfiguration = halConfiguration;
3359 status = s;
3360 });
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003361 if (!err.isOk()) {
3362 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3363 return DEAD_OBJECT;
3364 }
3365 } else {
3366 // We don't; use v3.2 call and construct a v3.3 HalStreamConfiguration
3367 ALOGV("%s: v3.2 device found", __FUNCTION__);
3368 HalStreamConfiguration finalConfiguration_3_2;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003369 auto err = mHidlSession->configureStreams(requestedConfiguration3_2,
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003370 [&status, &finalConfiguration_3_2]
3371 (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) {
3372 finalConfiguration_3_2 = halConfiguration;
3373 status = s;
3374 });
3375 if (!err.isOk()) {
3376 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3377 return DEAD_OBJECT;
3378 }
3379 finalConfiguration.streams.resize(finalConfiguration_3_2.streams.size());
3380 for (size_t i = 0; i < finalConfiguration_3_2.streams.size(); i++) {
3381 finalConfiguration.streams[i].v3_2 = finalConfiguration_3_2.streams[i];
3382 finalConfiguration.streams[i].overrideDataSpace =
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003383 requestedConfiguration3_2.streams[i].dataSpace;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003384 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003385 }
3386
3387 if (status != common::V1_0::Status::OK ) {
3388 return CameraProviderManager::mapToStatusT(status);
3389 }
3390
3391 // And convert output stream configuration from HIDL
3392
3393 for (size_t i = 0; i < config->num_streams; i++) {
Emilian Peevf4816702020-04-03 15:44:51 -07003394 camera3::camera_stream_t *dst = config->streams[i];
Emilian Peev31abd0a2017-05-11 18:37:46 +01003395 int streamId = Camera3Stream::cast(dst)->getId();
3396
3397 // Start scan at i, with the assumption that the stream order matches
3398 size_t realIdx = i;
3399 bool found = false;
Eino-Ville Talvala370875f2019-04-12 12:40:27 -07003400 size_t halStreamCount = finalConfiguration.streams.size();
3401 for (size_t idx = 0; idx < halStreamCount; idx++) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003402 if (finalConfiguration.streams[realIdx].v3_2.id == streamId) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003403 found = true;
3404 break;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003405 }
Eino-Ville Talvala370875f2019-04-12 12:40:27 -07003406 realIdx = (realIdx >= halStreamCount - 1) ? 0 : realIdx + 1;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003407 }
3408 if (!found) {
3409 ALOGE("%s: Stream %d not found in stream configuration response from HAL",
3410 __FUNCTION__, streamId);
3411 return INVALID_OPERATION;
3412 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003413 device::V3_3::HalStream &src = finalConfiguration.streams[realIdx];
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003414 device::V3_6::HalStream &src_36 = finalConfiguration3_6.streams[realIdx];
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003415
Emilian Peev710c1422017-08-30 11:19:38 +01003416 Camera3Stream* dstStream = Camera3Stream::cast(dst);
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003417 int overrideFormat = mapToFrameworkFormat(src.v3_2.overrideFormat);
3418 android_dataspace overrideDataSpace = mapToFrameworkDataspace(src.overrideDataSpace);
3419
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003420 if (mHidlSession_3_6 != nullptr) {
3421 dstStream->setOfflineProcessingSupport(src_36.supportOffline);
3422 }
3423
Yin-Chia Yeh90667662019-07-01 15:45:00 -07003424 if (dstStream->getOriginalFormat() != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Shuzhen Wang92653952019-05-07 15:11:43 -07003425 dstStream->setFormatOverride(false);
3426 dstStream->setDataSpaceOverride(false);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003427 if (dst->format != overrideFormat) {
3428 ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__,
3429 streamId, dst->format);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003430 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003431 if (dst->data_space != overrideDataSpace) {
3432 ALOGE("%s: Stream %d: DataSpace override not allowed for format 0x%x", __FUNCTION__,
3433 streamId, dst->format);
3434 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003435 } else {
Shuzhen Wang92653952019-05-07 15:11:43 -07003436 bool needFormatOverride =
3437 requestedConfiguration3_2.streams[i].format != src.v3_2.overrideFormat;
3438 bool needDataspaceOverride =
3439 requestedConfiguration3_2.streams[i].dataSpace != src.overrideDataSpace;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003440 // Override allowed with IMPLEMENTATION_DEFINED
Shuzhen Wang92653952019-05-07 15:11:43 -07003441 dstStream->setFormatOverride(needFormatOverride);
3442 dstStream->setDataSpaceOverride(needDataspaceOverride);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003443 dst->format = overrideFormat;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003444 dst->data_space = overrideDataSpace;
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003445 }
3446
Emilian Peevf4816702020-04-03 15:44:51 -07003447 if (dst->stream_type == CAMERA_STREAM_INPUT) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003448 if (src.v3_2.producerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003449 ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003450 __FUNCTION__, streamId);
3451 return INVALID_OPERATION;
3452 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003453 dstStream->setUsage(
3454 mapConsumerToFrameworkUsage(src.v3_2.consumerUsage));
Emilian Peev31abd0a2017-05-11 18:37:46 +01003455 } else {
3456 // OUTPUT
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003457 if (src.v3_2.consumerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003458 ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage",
3459 __FUNCTION__, streamId);
3460 return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003461 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003462 dstStream->setUsage(
3463 mapProducerToFrameworkUsage(src.v3_2.producerUsage));
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003464 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003465 dst->max_buffers = src.v3_2.maxBuffers;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003466 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003467
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003468 return res;
3469}
3470
Emilian Peevf4816702020-04-03 15:44:51 -07003471status_t Camera3Device::HalInterface::wrapAsHidlRequest(camera_capture_request_t* request,
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003472 /*out*/device::V3_2::CaptureRequest* captureRequest,
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003473 /*out*/std::vector<native_handle_t*>* handlesCreated,
3474 /*out*/std::vector<std::pair<int32_t, int32_t>>* inflightBuffers) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003475 ATRACE_CALL();
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003476 if (captureRequest == nullptr || handlesCreated == nullptr || inflightBuffers == nullptr) {
3477 ALOGE("%s: captureRequest (%p), handlesCreated (%p), and inflightBuffers(%p) "
3478 "must not be null", __FUNCTION__, captureRequest, handlesCreated, inflightBuffers);
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003479 return BAD_VALUE;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003480 }
3481
3482 captureRequest->frameNumber = request->frame_number;
Yifan Hongf79b5542017-04-11 14:44:25 -07003483
3484 captureRequest->fmqSettingsSize = 0;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003485
3486 {
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003487 if (request->input_buffer != nullptr) {
3488 int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId();
3489 buffer_handle_t buf = *(request->input_buffer->buffer);
3490 auto pair = getBufferId(buf, streamId);
3491 bool isNewBuffer = pair.first;
3492 uint64_t bufferId = pair.second;
3493 captureRequest->inputBuffer.streamId = streamId;
3494 captureRequest->inputBuffer.bufferId = bufferId;
3495 captureRequest->inputBuffer.buffer = (isNewBuffer) ? buf : nullptr;
3496 captureRequest->inputBuffer.status = BufferStatus::OK;
3497 native_handle_t *acquireFence = nullptr;
3498 if (request->input_buffer->acquire_fence != -1) {
3499 acquireFence = native_handle_create(1,0);
3500 acquireFence->data[0] = request->input_buffer->acquire_fence;
3501 handlesCreated->push_back(acquireFence);
3502 }
3503 captureRequest->inputBuffer.acquireFence = acquireFence;
3504 captureRequest->inputBuffer.releaseFence = nullptr;
3505
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003506 mBufferRecords.pushInflightBuffer(captureRequest->frameNumber, streamId,
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003507 request->input_buffer->buffer);
3508 inflightBuffers->push_back(std::make_pair(captureRequest->frameNumber, streamId));
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003509 } else {
3510 captureRequest->inputBuffer.streamId = -1;
3511 captureRequest->inputBuffer.bufferId = BUFFER_ID_NO_BUFFER;
3512 }
3513
3514 captureRequest->outputBuffers.resize(request->num_output_buffers);
3515 for (size_t i = 0; i < request->num_output_buffers; i++) {
Emilian Peevf4816702020-04-03 15:44:51 -07003516 const camera_stream_buffer_t *src = request->output_buffers + i;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003517 StreamBuffer &dst = captureRequest->outputBuffers[i];
3518 int32_t streamId = Camera3Stream::cast(src->stream)->getId();
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003519 if (src->buffer != nullptr) {
3520 buffer_handle_t buf = *(src->buffer);
3521 auto pair = getBufferId(buf, streamId);
3522 bool isNewBuffer = pair.first;
3523 dst.bufferId = pair.second;
3524 dst.buffer = isNewBuffer ? buf : nullptr;
3525 native_handle_t *acquireFence = nullptr;
3526 if (src->acquire_fence != -1) {
3527 acquireFence = native_handle_create(1,0);
3528 acquireFence->data[0] = src->acquire_fence;
3529 handlesCreated->push_back(acquireFence);
3530 }
3531 dst.acquireFence = acquireFence;
3532 } else if (mUseHalBufManager) {
3533 // HAL buffer management path
3534 dst.bufferId = BUFFER_ID_NO_BUFFER;
3535 dst.buffer = nullptr;
3536 dst.acquireFence = nullptr;
3537 } else {
3538 ALOGE("%s: cannot send a null buffer in capture request!", __FUNCTION__);
3539 return BAD_VALUE;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003540 }
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003541 dst.streamId = streamId;
3542 dst.status = BufferStatus::OK;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003543 dst.releaseFence = nullptr;
3544
Yin-Chia Yeh5f840f82019-03-05 11:59:04 -08003545 // Output buffers are empty when using HAL buffer manager
3546 if (!mUseHalBufManager) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003547 mBufferRecords.pushInflightBuffer(
3548 captureRequest->frameNumber, streamId, src->buffer);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003549 inflightBuffers->push_back(std::make_pair(captureRequest->frameNumber, streamId));
Yin-Chia Yeh5f840f82019-03-05 11:59:04 -08003550 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003551 }
3552 }
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003553 return OK;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003554}
3555
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003556void Camera3Device::HalInterface::cleanupNativeHandles(
3557 std::vector<native_handle_t*> *handles, bool closeFd) {
3558 if (handles == nullptr) {
3559 return;
3560 }
3561 if (closeFd) {
3562 for (auto& handle : *handles) {
3563 native_handle_close(handle);
3564 }
3565 }
3566 for (auto& handle : *handles) {
3567 native_handle_delete(handle);
3568 }
3569 handles->clear();
3570 return;
3571}
3572
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003573status_t Camera3Device::HalInterface::processBatchCaptureRequests(
Emilian Peevf4816702020-04-03 15:44:51 -07003574 std::vector<camera_capture_request_t*>& requests,/*out*/uint32_t* numRequestProcessed) {
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003575 ATRACE_NAME("CameraHal::processBatchCaptureRequests");
3576 if (!valid()) return INVALID_OPERATION;
3577
Emilian Peevaebbe412018-01-15 13:53:24 +00003578 sp<device::V3_4::ICameraDeviceSession> hidlSession_3_4;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003579 sp<device::V3_7::ICameraDeviceSession> hidlSession_3_7;
3580 auto castResult_3_7 = device::V3_7::ICameraDeviceSession::castFrom(mHidlSession);
3581 if (castResult_3_7.isOk()) {
3582 hidlSession_3_7 = castResult_3_7;
3583 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003584 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
3585 if (castResult_3_4.isOk()) {
3586 hidlSession_3_4 = castResult_3_4;
3587 }
3588
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003589 hardware::hidl_vec<device::V3_2::CaptureRequest> captureRequests;
Emilian Peevaebbe412018-01-15 13:53:24 +00003590 hardware::hidl_vec<device::V3_4::CaptureRequest> captureRequests_3_4;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003591 hardware::hidl_vec<device::V3_7::CaptureRequest> captureRequests_3_7;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003592 size_t batchSize = requests.size();
Shuzhen Wang83bff122020-11-20 15:51:39 -08003593 if (hidlSession_3_7 != nullptr) {
3594 captureRequests_3_7.resize(batchSize);
3595 } else if (hidlSession_3_4 != nullptr) {
Emilian Peevaebbe412018-01-15 13:53:24 +00003596 captureRequests_3_4.resize(batchSize);
3597 } else {
3598 captureRequests.resize(batchSize);
3599 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003600 std::vector<native_handle_t*> handlesCreated;
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003601 std::vector<std::pair<int32_t, int32_t>> inflightBuffers;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003602
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003603 status_t res = OK;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003604 for (size_t i = 0; i < batchSize; i++) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08003605 if (hidlSession_3_7 != nullptr) {
3606 res = wrapAsHidlRequest(requests[i], /*out*/&captureRequests_3_7[i].v3_4.v3_2,
3607 /*out*/&handlesCreated, /*out*/&inflightBuffers);
3608 } else if (hidlSession_3_4 != nullptr) {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003609 res = wrapAsHidlRequest(requests[i], /*out*/&captureRequests_3_4[i].v3_2,
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003610 /*out*/&handlesCreated, /*out*/&inflightBuffers);
Emilian Peevaebbe412018-01-15 13:53:24 +00003611 } else {
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003612 res = wrapAsHidlRequest(requests[i], /*out*/&captureRequests[i],
3613 /*out*/&handlesCreated, /*out*/&inflightBuffers);
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003614 }
3615 if (res != OK) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003616 mBufferRecords.popInflightBuffers(inflightBuffers);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003617 cleanupNativeHandles(&handlesCreated);
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003618 return res;
Emilian Peevaebbe412018-01-15 13:53:24 +00003619 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003620 }
3621
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003622 std::vector<device::V3_2::BufferCache> cachesToRemove;
3623 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003624 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003625 for (auto& pair : mFreedBuffers) {
3626 // The stream might have been removed since onBufferFreed
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003627 if (mBufferRecords.isStreamCached(pair.first)) {
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003628 cachesToRemove.push_back({pair.first, pair.second});
3629 }
3630 }
3631 mFreedBuffers.clear();
3632 }
3633
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003634 common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
3635 *numRequestProcessed = 0;
Yifan Hongf79b5542017-04-11 14:44:25 -07003636
3637 // Write metadata to FMQ.
3638 for (size_t i = 0; i < batchSize; i++) {
Emilian Peevf4816702020-04-03 15:44:51 -07003639 camera_capture_request_t* request = requests[i];
Emilian Peevaebbe412018-01-15 13:53:24 +00003640 device::V3_2::CaptureRequest* captureRequest;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003641 if (hidlSession_3_7 != nullptr) {
3642 captureRequest = &captureRequests_3_7[i].v3_4.v3_2;
3643 } else if (hidlSession_3_4 != nullptr) {
Emilian Peevaebbe412018-01-15 13:53:24 +00003644 captureRequest = &captureRequests_3_4[i].v3_2;
3645 } else {
3646 captureRequest = &captureRequests[i];
3647 }
Yifan Hongf79b5542017-04-11 14:44:25 -07003648
3649 if (request->settings != nullptr) {
3650 size_t settingsSize = get_camera_metadata_size(request->settings);
3651 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
3652 reinterpret_cast<const uint8_t*>(request->settings), settingsSize)) {
3653 captureRequest->settings.resize(0);
3654 captureRequest->fmqSettingsSize = settingsSize;
3655 } else {
3656 if (mRequestMetadataQueue != nullptr) {
3657 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
3658 }
3659 captureRequest->settings.setToExternal(
3660 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(request->settings)),
3661 get_camera_metadata_size(request->settings));
3662 captureRequest->fmqSettingsSize = 0u;
3663 }
3664 } else {
3665 // A null request settings maps to a size-0 CameraMetadata
3666 captureRequest->settings.resize(0);
3667 captureRequest->fmqSettingsSize = 0u;
3668 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003669
Shuzhen Wang83bff122020-11-20 15:51:39 -08003670 // hidl session 3.7 specific handling.
3671 if (hidlSession_3_7 != nullptr) {
3672 captureRequests_3_7[i].inputWidth = request->input_width;
3673 captureRequests_3_7[i].inputHeight = request->input_height;
3674 }
3675
3676 // hidl session 3.7 and 3.4 specific handling.
3677 if (hidlSession_3_7 != nullptr || hidlSession_3_4 != nullptr) {
3678 hardware::hidl_vec<device::V3_4::PhysicalCameraSetting>& physicalCameraSettings =
3679 (hidlSession_3_7 != nullptr) ?
3680 captureRequests_3_7[i].v3_4.physicalCameraSettings :
3681 captureRequests_3_4[i].physicalCameraSettings;
3682 physicalCameraSettings.resize(request->num_physcam_settings);
Emilian Peevaebbe412018-01-15 13:53:24 +00003683 for (size_t j = 0; j < request->num_physcam_settings; j++) {
Emilian Peev00420d22018-02-05 21:33:13 +00003684 if (request->physcam_settings != nullptr) {
3685 size_t settingsSize = get_camera_metadata_size(request->physcam_settings[j]);
3686 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
3687 reinterpret_cast<const uint8_t*>(request->physcam_settings[j]),
3688 settingsSize)) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08003689 physicalCameraSettings[j].settings.resize(0);
3690 physicalCameraSettings[j].fmqSettingsSize = settingsSize;
Emilian Peev00420d22018-02-05 21:33:13 +00003691 } else {
3692 if (mRequestMetadataQueue != nullptr) {
3693 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
3694 }
Shuzhen Wang83bff122020-11-20 15:51:39 -08003695 physicalCameraSettings[j].settings.setToExternal(
Emilian Peev00420d22018-02-05 21:33:13 +00003696 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(
3697 request->physcam_settings[j])),
3698 get_camera_metadata_size(request->physcam_settings[j]));
Shuzhen Wang83bff122020-11-20 15:51:39 -08003699 physicalCameraSettings[j].fmqSettingsSize = 0u;
Emilian Peevaebbe412018-01-15 13:53:24 +00003700 }
Emilian Peev00420d22018-02-05 21:33:13 +00003701 } else {
Emilian Peevaebbe412018-01-15 13:53:24 +00003702 captureRequests_3_4[i].physicalCameraSettings[j].fmqSettingsSize = 0u;
Emilian Peev00420d22018-02-05 21:33:13 +00003703 captureRequests_3_4[i].physicalCameraSettings[j].settings.resize(0);
Emilian Peevaebbe412018-01-15 13:53:24 +00003704 }
Shuzhen Wang83bff122020-11-20 15:51:39 -08003705 physicalCameraSettings[j].physicalCameraId = request->physcam_id[j];
Emilian Peevaebbe412018-01-15 13:53:24 +00003706 }
3707 }
Yifan Hongf79b5542017-04-11 14:44:25 -07003708 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003709
3710 hardware::details::return_status err;
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07003711 auto resultCallback =
3712 [&status, &numRequestProcessed] (auto s, uint32_t n) {
3713 status = s;
3714 *numRequestProcessed = n;
3715 };
Shuzhen Wang83bff122020-11-20 15:51:39 -08003716 if (hidlSession_3_7 != nullptr) {
3717 err = hidlSession_3_7->processCaptureRequest_3_7(captureRequests_3_7, cachesToRemove,
3718 resultCallback);
3719 } else if (hidlSession_3_4 != nullptr) {
Emilian Peevaebbe412018-01-15 13:53:24 +00003720 err = hidlSession_3_4->processCaptureRequest_3_4(captureRequests_3_4, cachesToRemove,
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07003721 resultCallback);
Emilian Peevaebbe412018-01-15 13:53:24 +00003722 } else {
3723 err = mHidlSession->processCaptureRequest(captureRequests, cachesToRemove,
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07003724 resultCallback);
Emilian Peevaebbe412018-01-15 13:53:24 +00003725 }
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003726 if (!err.isOk()) {
3727 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003728 status = common::V1_0::Status::CAMERA_DISCONNECTED;
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003729 }
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003730
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003731 if (status == common::V1_0::Status::OK && *numRequestProcessed != batchSize) {
3732 ALOGE("%s: processCaptureRequest returns OK but processed %d/%zu requests",
3733 __FUNCTION__, *numRequestProcessed, batchSize);
3734 status = common::V1_0::Status::INTERNAL_ERROR;
3735 }
3736
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003737 res = CameraProviderManager::mapToStatusT(status);
3738 if (res == OK) {
3739 if (mHidlSession->isRemote()) {
3740 // Only close acquire fence FDs when the HIDL transaction succeeds (so the FDs have been
3741 // sent to camera HAL processes)
3742 cleanupNativeHandles(&handlesCreated, /*closeFd*/true);
3743 } else {
3744 // In passthrough mode the FDs are now owned by HAL
3745 cleanupNativeHandles(&handlesCreated);
3746 }
3747 } else {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003748 mBufferRecords.popInflightBuffers(inflightBuffers);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003749 cleanupNativeHandles(&handlesCreated);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003750 }
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003751 return res;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003752}
3753
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003754status_t Camera3Device::HalInterface::flush() {
3755 ATRACE_NAME("CameraHal::flush");
3756 if (!valid()) return INVALID_OPERATION;
3757 status_t res = OK;
3758
Emilian Peev31abd0a2017-05-11 18:37:46 +01003759 auto err = mHidlSession->flush();
3760 if (!err.isOk()) {
3761 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3762 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003763 } else {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003764 res = CameraProviderManager::mapToStatusT(err);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003765 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003766
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003767 return res;
3768}
3769
Emilian Peev31abd0a2017-05-11 18:37:46 +01003770status_t Camera3Device::HalInterface::dump(int /*fd*/) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003771 ATRACE_NAME("CameraHal::dump");
3772 if (!valid()) return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003773
Emilian Peev31abd0a2017-05-11 18:37:46 +01003774 // Handled by CameraProviderManager::dump
3775
3776 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003777}
3778
3779status_t Camera3Device::HalInterface::close() {
3780 ATRACE_NAME("CameraHal::close()");
3781 if (!valid()) return INVALID_OPERATION;
3782 status_t res = OK;
3783
Emilian Peev31abd0a2017-05-11 18:37:46 +01003784 auto err = mHidlSession->close();
3785 // Interface will be dead shortly anyway, so don't log errors
3786 if (!err.isOk()) {
3787 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003788 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003789
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003790 return res;
3791}
3792
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003793void Camera3Device::HalInterface::signalPipelineDrain(const std::vector<int>& streamIds) {
3794 ATRACE_NAME("CameraHal::signalPipelineDrain");
3795 if (!valid() || mHidlSession_3_5 == nullptr) {
3796 ALOGE("%s called on invalid camera!", __FUNCTION__);
3797 return;
3798 }
3799
Yin-Chia Yehc300a072019-02-13 14:56:57 -08003800 auto err = mHidlSession_3_5->signalStreamFlush(streamIds, mNextStreamConfigCounter - 1);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003801 if (!err.isOk()) {
3802 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3803 return;
3804 }
3805}
3806
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003807status_t Camera3Device::HalInterface::switchToOffline(
3808 const std::vector<int32_t>& streamsToKeep,
3809 /*out*/hardware::camera::device::V3_6::CameraOfflineSessionInfo* offlineSessionInfo,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003810 /*out*/sp<hardware::camera::device::V3_6::ICameraOfflineSession>* offlineSession,
3811 /*out*/camera3::BufferRecords* bufferRecords) {
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003812 ATRACE_NAME("CameraHal::switchToOffline");
3813 if (!valid() || mHidlSession_3_6 == nullptr) {
3814 ALOGE("%s called on invalid camera!", __FUNCTION__);
3815 return INVALID_OPERATION;
3816 }
3817
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003818 if (offlineSessionInfo == nullptr || offlineSession == nullptr || bufferRecords == nullptr) {
3819 ALOGE("%s: output arguments must not be null!", __FUNCTION__);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003820 return INVALID_OPERATION;
3821 }
3822
3823 common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003824 auto resultCallback =
3825 [&status, &offlineSessionInfo, &offlineSession] (auto s, auto info, auto session) {
3826 status = s;
3827 *offlineSessionInfo = info;
3828 *offlineSession = session;
3829 };
3830 auto err = mHidlSession_3_6->switchToOffline(streamsToKeep, resultCallback);
3831
3832 if (!err.isOk()) {
3833 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3834 return DEAD_OBJECT;
3835 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003836
3837 status_t ret = CameraProviderManager::mapToStatusT(status);
3838 if (ret != OK) {
3839 return ret;
3840 }
3841
3842 // TODO: assert no ongoing requestBuffer/returnBuffer call here
3843 // TODO: update RequestBufferStateMachine to block requestBuffer/returnBuffer once HAL
3844 // returns from switchToOffline.
3845
3846
3847 // Validate buffer caches
3848 std::vector<int32_t> streams;
3849 streams.reserve(offlineSessionInfo->offlineStreams.size());
3850 for (auto offlineStream : offlineSessionInfo->offlineStreams) {
3851 int32_t id = offlineStream.id;
3852 streams.push_back(id);
3853 // Verify buffer caches
3854 std::vector<uint64_t> bufIds(offlineStream.circulatingBufferIds.begin(),
3855 offlineStream.circulatingBufferIds.end());
3856 if (!verifyBufferIds(id, bufIds)) {
3857 ALOGE("%s: stream ID %d buffer cache records mismatch!", __FUNCTION__, id);
3858 return UNKNOWN_ERROR;
3859 }
3860 }
3861
3862 // Move buffer records
3863 bufferRecords->takeBufferCaches(mBufferRecords, streams);
3864 bufferRecords->takeInflightBufferMap(mBufferRecords);
3865 bufferRecords->takeRequestedBufferMap(mBufferRecords);
3866 return ret;
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003867}
3868
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003869void Camera3Device::HalInterface::getInflightBufferKeys(
3870 std::vector<std::pair<int32_t, int32_t>>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003871 mBufferRecords.getInflightBufferKeys(out);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003872 return;
3873}
3874
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08003875void Camera3Device::HalInterface::getInflightRequestBufferKeys(
3876 std::vector<uint64_t>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003877 mBufferRecords.getInflightRequestBufferKeys(out);
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08003878 return;
3879}
3880
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003881bool Camera3Device::HalInterface::verifyBufferIds(
3882 int32_t streamId, std::vector<uint64_t>& bufIds) {
3883 return mBufferRecords.verifyBufferIds(streamId, bufIds);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003884}
3885
3886status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08003887 int32_t frameNumber, int32_t streamId,
3888 /*out*/ buffer_handle_t **buffer) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003889 return mBufferRecords.popInflightBuffer(frameNumber, streamId, buffer);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003890}
3891
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003892status_t Camera3Device::HalInterface::pushInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08003893 uint64_t bufferId, buffer_handle_t* buf, int32_t streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003894 return mBufferRecords.pushInflightRequestBuffer(bufferId, buf, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003895}
3896
3897// Find and pop a buffer_handle_t based on bufferId
3898status_t Camera3Device::HalInterface::popInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08003899 uint64_t bufferId,
3900 /*out*/ buffer_handle_t** buffer,
3901 /*optional out*/ int32_t* streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003902 return mBufferRecords.popInflightRequestBuffer(bufferId, buffer, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003903}
3904
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003905std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
3906 const buffer_handle_t& buf, int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003907 return mBufferRecords.getBufferId(buf, streamId);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003908}
3909
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003910void Camera3Device::HalInterface::onBufferFreed(
3911 int streamId, const native_handle_t* handle) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003912 uint32_t bufferId = mBufferRecords.removeOneBufferCache(streamId, handle);
3913 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
3914 if (bufferId != BUFFER_ID_NO_BUFFER) {
3915 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003916 }
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003917}
3918
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07003919void Camera3Device::HalInterface::onStreamReConfigured(int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003920 std::vector<uint64_t> bufIds = mBufferRecords.clearBufferCaches(streamId);
3921 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
3922 for (auto bufferId : bufIds) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07003923 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
3924 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07003925}
3926
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003927/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003928 * RequestThread inner class methods
3929 */
3930
3931Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003932 sp<StatusTracker> statusTracker,
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003933 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys,
3934 bool useHalBufManager) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003935 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003936 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003937 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003938 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07003939 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003940 mId(getId(parent)),
Shuzhen Wang316781a2020-08-18 18:11:01 -07003941 mFirstRepeating(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003942 mReconfigured(false),
3943 mDoPause(false),
3944 mPaused(true),
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003945 mNotifyPipelineDrain(false),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003946 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07003947 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003948 mCurrentAfTriggerId(0),
3949 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003950 mRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE),
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003951 mCameraMute(false),
3952 mCameraMuteChanged(false),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003953 mRepeatingLastFrameNumber(
3954 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07003955 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003956 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003957 mRequestLatency(kRequestLatencyBinSize),
3958 mSessionParamKeys(sessionParamKeys),
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003959 mLatestSessionParams(sessionParamKeys.size()),
3960 mUseHalBufManager(useHalBufManager) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07003961 mStatusId = statusTracker->addComponent("RequestThread");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003962}
3963
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003964Camera3Device::RequestThread::~RequestThread() {}
3965
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003966void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003967 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003968 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003969 Mutex::Autolock l(mRequestLock);
3970 mListener = listener;
3971}
3972
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003973void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
Shuzhen Wang99080502021-03-07 21:08:20 -08003974 const CameraMetadata& sessionParams,
3975 const std::map<int32_t, std::set<String8>>& groupIdPhysicalCameraMap) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003976 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003977 Mutex::Autolock l(mRequestLock);
3978 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003979 mLatestSessionParams = sessionParams;
Shuzhen Wang99080502021-03-07 21:08:20 -08003980 mGroupIdPhysicalCameraMap = groupIdPhysicalCameraMap;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003981 // Prepare video stream for high speed recording.
3982 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003983 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003984}
3985
Jianing Wei90e59c92014-03-12 18:29:36 -07003986status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003987 List<sp<CaptureRequest> > &requests,
3988 /*out*/
3989 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003990 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07003991 Mutex::Autolock l(mRequestLock);
3992 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
3993 ++it) {
3994 mRequestQueue.push_back(*it);
3995 }
3996
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003997 if (lastFrameNumber != NULL) {
3998 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
3999 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
4000 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
4001 *lastFrameNumber);
4002 }
Jianing Weicb0652e2014-03-12 18:29:36 -07004003
Jianing Wei90e59c92014-03-12 18:29:36 -07004004 unpauseForNewRequests();
4005
4006 return OK;
4007}
4008
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004009
4010status_t Camera3Device::RequestThread::queueTrigger(
4011 RequestTrigger trigger[],
4012 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004013 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004014 Mutex::Autolock l(mTriggerMutex);
4015 status_t ret;
4016
4017 for (size_t i = 0; i < count; ++i) {
4018 ret = queueTriggerLocked(trigger[i]);
4019
4020 if (ret != OK) {
4021 return ret;
4022 }
4023 }
4024
4025 return OK;
4026}
4027
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004028const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
4029 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004030 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004031 if (d != nullptr) return d->mId;
4032 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004033}
4034
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004035status_t Camera3Device::RequestThread::queueTriggerLocked(
4036 RequestTrigger trigger) {
4037
4038 uint32_t tag = trigger.metadataTag;
4039 ssize_t index = mTriggerMap.indexOfKey(tag);
4040
4041 switch (trigger.getTagType()) {
4042 case TYPE_BYTE:
4043 // fall-through
4044 case TYPE_INT32:
4045 break;
4046 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004047 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
4048 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004049 return INVALID_OPERATION;
4050 }
4051
4052 /**
4053 * Collect only the latest trigger, since we only have 1 field
4054 * in the request settings per trigger tag, and can't send more than 1
4055 * trigger per request.
4056 */
4057 if (index != NAME_NOT_FOUND) {
4058 mTriggerMap.editValueAt(index) = trigger;
4059 } else {
4060 mTriggerMap.add(tag, trigger);
4061 }
4062
4063 return OK;
4064}
4065
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004066status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004067 const RequestList &requests,
4068 /*out*/
4069 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004070 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004071 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004072 if (lastFrameNumber != NULL) {
4073 *lastFrameNumber = mRepeatingLastFrameNumber;
4074 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004075 mRepeatingRequests.clear();
Shuzhen Wang316781a2020-08-18 18:11:01 -07004076 mFirstRepeating = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004077 mRepeatingRequests.insert(mRepeatingRequests.begin(),
4078 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004079
4080 unpauseForNewRequests();
4081
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004082 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004083 return OK;
4084}
4085
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07004086bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004087 if (mRepeatingRequests.empty()) {
4088 return false;
4089 }
4090 int32_t requestId = requestIn->mResultExtras.requestId;
4091 const RequestList &repeatRequests = mRepeatingRequests;
4092 // All repeating requests are guaranteed to have same id so only check first quest
4093 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
4094 return (firstRequest->mResultExtras.requestId == requestId);
4095}
4096
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004097status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004098 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004099 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004100 return clearRepeatingRequestsLocked(lastFrameNumber);
4101
4102}
4103
4104status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004105 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004106 if (lastFrameNumber != NULL) {
4107 *lastFrameNumber = mRepeatingLastFrameNumber;
4108 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004109 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004110 return OK;
4111}
4112
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004113status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004114 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004115 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004116 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004117 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004118
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004119 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004120
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004121 // Send errors for all requests pending in the request queue, including
4122 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004123 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004124 if (listener != NULL) {
4125 for (RequestList::iterator it = mRequestQueue.begin();
4126 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004127 // Abort the input buffers for reprocess requests.
4128 if ((*it)->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07004129 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08004130 camera3::Size inputBufferSize;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07004131 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08004132 &inputBufferSize, /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004133 if (res != OK) {
4134 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
4135 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
4136 } else {
Emilian Peevf4816702020-04-03 15:44:51 -07004137 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004138 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
4139 if (res != OK) {
4140 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
4141 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
4142 }
4143 }
4144 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004145 // Set the frame number this request would have had, if it
4146 // had been submitted; this frame number will not be reused.
4147 // The requestId and burstId fields were set when the request was
4148 // submitted originally (in convertMetadataListToRequestListLocked)
4149 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004150 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004151 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004152 }
4153 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004154 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08004155
4156 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004157 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004158 if (lastFrameNumber != NULL) {
4159 *lastFrameNumber = mRepeatingLastFrameNumber;
4160 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004161 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Emilian Peev8dae54c2019-12-02 15:17:17 -08004162 mRequestSignal.signal();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004163 return OK;
4164}
4165
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004166status_t Camera3Device::RequestThread::flush() {
4167 ATRACE_CALL();
4168 Mutex::Autolock l(mFlushLock);
4169
Emilian Peev08dd2452017-04-06 16:55:14 +01004170 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004171}
4172
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004173void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004174 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004175 Mutex::Autolock l(mPauseLock);
4176 mDoPause = paused;
4177 mDoPauseSignal.signal();
4178}
4179
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004180status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
4181 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004182 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004183 Mutex::Autolock l(mLatestRequestMutex);
4184 status_t res;
4185 while (mLatestRequestId != requestId) {
4186 nsecs_t startTime = systemTime();
4187
4188 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
4189 if (res != OK) return res;
4190
4191 timeout -= (systemTime() - startTime);
4192 }
4193
4194 return OK;
4195}
4196
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004197void Camera3Device::RequestThread::requestExit() {
4198 // Call parent to set up shutdown
4199 Thread::requestExit();
4200 // The exit from any possible waits
4201 mDoPauseSignal.signal();
4202 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07004203
4204 mRequestLatency.log("ProcessCaptureRequest latency histogram");
4205 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004206}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004207
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004208void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004209 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004210 bool surfaceAbandoned = false;
4211 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004212 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004213 {
4214 Mutex::Autolock l(mRequestLock);
4215 // Check all streams needed by repeating requests are still valid. Otherwise, stop
4216 // repeating requests.
4217 for (const auto& request : mRepeatingRequests) {
4218 for (const auto& s : request->mOutputStreams) {
4219 if (s->isAbandoned()) {
4220 surfaceAbandoned = true;
4221 clearRepeatingRequestsLocked(&lastFrameNumber);
4222 break;
4223 }
4224 }
4225 if (surfaceAbandoned) {
4226 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004227 }
4228 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004229 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004230 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004231
4232 if (listener != NULL && surfaceAbandoned) {
4233 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004234 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004235}
4236
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004237bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004238 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004239 status_t res;
4240 size_t batchSize = mNextRequests.size();
Emilian Peevf4816702020-04-03 15:44:51 -07004241 std::vector<camera_capture_request_t*> requests(batchSize);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004242 uint32_t numRequestProcessed = 0;
4243 for (size_t i = 0; i < batchSize; i++) {
4244 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07004245 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004246 }
4247
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004248 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
4249
4250 bool triggerRemoveFailed = false;
4251 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
4252 for (size_t i = 0; i < numRequestProcessed; i++) {
4253 NextRequest& nextRequest = mNextRequests.editItemAt(i);
4254 nextRequest.submitted = true;
4255
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004256 updateNextRequest(nextRequest);
Emilian Peevaebbe412018-01-15 13:53:24 +00004257
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004258 if (!triggerRemoveFailed) {
4259 // Remove any previously queued triggers (after unlock)
4260 status_t removeTriggerRes = removeTriggers(mPrevRequest);
4261 if (removeTriggerRes != OK) {
4262 triggerRemoveFailed = true;
4263 triggerFailedRequest = nextRequest;
4264 }
4265 }
4266 }
4267
4268 if (triggerRemoveFailed) {
4269 SET_ERR("RequestThread: Unable to remove triggers "
4270 "(capture request %d, HAL device: %s (%d)",
4271 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
4272 cleanUpFailedRequests(/*sendRequestError*/ false);
4273 return false;
4274 }
4275
4276 if (res != OK) {
4277 // Should only get a failure here for malformed requests or device-level
4278 // errors, so consider all errors fatal. Bad metadata failures should
4279 // come through notify.
4280 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
4281 mNextRequests[numRequestProcessed].halRequest.frame_number,
4282 strerror(-res), res);
4283 cleanUpFailedRequests(/*sendRequestError*/ false);
4284 return false;
4285 }
4286 return true;
4287}
4288
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004289nsecs_t Camera3Device::RequestThread::calculateMaxExpectedDuration(const camera_metadata_t *request) {
4290 nsecs_t maxExpectedDuration = kDefaultExpectedDuration;
4291 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4292 find_camera_metadata_ro_entry(request,
4293 ANDROID_CONTROL_AE_MODE,
4294 &e);
4295 if (e.count == 0) return maxExpectedDuration;
4296
4297 switch (e.data.u8[0]) {
4298 case ANDROID_CONTROL_AE_MODE_OFF:
4299 find_camera_metadata_ro_entry(request,
4300 ANDROID_SENSOR_EXPOSURE_TIME,
4301 &e);
4302 if (e.count > 0) {
4303 maxExpectedDuration = e.data.i64[0];
4304 }
4305 find_camera_metadata_ro_entry(request,
4306 ANDROID_SENSOR_FRAME_DURATION,
4307 &e);
4308 if (e.count > 0) {
4309 maxExpectedDuration = std::max(e.data.i64[0], maxExpectedDuration);
4310 }
4311 break;
4312 default:
4313 find_camera_metadata_ro_entry(request,
4314 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
4315 &e);
4316 if (e.count > 1) {
4317 maxExpectedDuration = 1e9 / e.data.u8[0];
4318 }
4319 break;
4320 }
4321
4322 return maxExpectedDuration;
4323}
4324
Emilian Peeva14b4dd2018-05-15 11:00:31 +01004325bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
4326 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
4327 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
4328 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
4329 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
4330 return true;
4331 }
4332
4333 return false;
4334}
4335
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004336void Camera3Device::RequestThread::updateNextRequest(NextRequest& nextRequest) {
4337 // Update the latest request sent to HAL
Shuzhen Wang83bff122020-11-20 15:51:39 -08004338 camera_capture_request_t& halRequest = nextRequest.halRequest;
4339 if (halRequest.settings != NULL) { // Don't update if they were unchanged
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004340 Mutex::Autolock al(mLatestRequestMutex);
4341
Shuzhen Wang83bff122020-11-20 15:51:39 -08004342 camera_metadata_t* cloned = clone_camera_metadata(halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004343 mLatestRequest.acquire(cloned);
4344
4345 mLatestPhysicalRequest.clear();
Shuzhen Wang83bff122020-11-20 15:51:39 -08004346 for (uint32_t i = 0; i < halRequest.num_physcam_settings; i++) {
4347 cloned = clone_camera_metadata(halRequest.physcam_settings[i]);
4348 mLatestPhysicalRequest.emplace(halRequest.physcam_id[i],
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004349 CameraMetadata(cloned));
4350 }
4351
4352 sp<Camera3Device> parent = mParent.promote();
4353 if (parent != NULL) {
4354 parent->monitorMetadata(TagMonitor::REQUEST,
Shuzhen Wang83bff122020-11-20 15:51:39 -08004355 halRequest.frame_number,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004356 0, mLatestRequest, mLatestPhysicalRequest);
4357 }
4358 }
4359
Shuzhen Wang83bff122020-11-20 15:51:39 -08004360 if (halRequest.settings != NULL) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004361 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
Shuzhen Wang83bff122020-11-20 15:51:39 -08004362 halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004363 }
4364
Shuzhen Wang83bff122020-11-20 15:51:39 -08004365 cleanupPhysicalSettings(nextRequest.captureRequest, &halRequest);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004366}
4367
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004368bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
4369 ATRACE_CALL();
4370 bool updatesDetected = false;
4371
Emilian Peev4ec17882019-01-24 17:16:58 -08004372 CameraMetadata updatedParams(mLatestSessionParams);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004373 for (auto tag : mSessionParamKeys) {
4374 camera_metadata_ro_entry entry = settings.find(tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08004375 camera_metadata_entry lastEntry = updatedParams.find(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004376
4377 if (entry.count > 0) {
4378 bool isDifferent = false;
4379 if (lastEntry.count > 0) {
4380 // Have a last value, compare to see if changed
4381 if (lastEntry.type == entry.type &&
4382 lastEntry.count == entry.count) {
4383 // Same type and count, compare values
4384 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
4385 size_t entryBytes = bytesPerValue * lastEntry.count;
4386 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
4387 if (cmp != 0) {
4388 isDifferent = true;
4389 }
4390 } else {
4391 // Count or type has changed
4392 isDifferent = true;
4393 }
4394 } else {
4395 // No last entry, so always consider to be different
4396 isDifferent = true;
4397 }
4398
4399 if (isDifferent) {
4400 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01004401 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
4402 updatesDetected = true;
4403 }
Emilian Peev4ec17882019-01-24 17:16:58 -08004404 updatedParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004405 }
4406 } else if (lastEntry.count > 0) {
4407 // Value has been removed
4408 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08004409 updatedParams.erase(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004410 updatesDetected = true;
4411 }
4412 }
4413
Emilian Peev4ec17882019-01-24 17:16:58 -08004414 bool reconfigureRequired;
4415 if (updatesDetected) {
4416 reconfigureRequired = mInterface->isReconfigurationRequired(mLatestSessionParams,
4417 updatedParams);
4418 mLatestSessionParams = updatedParams;
4419 } else {
4420 reconfigureRequired = false;
4421 }
4422
4423 return reconfigureRequired;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004424}
4425
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004426bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004427 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004428 status_t res;
Jayant Chowdhary37eca242019-11-18 08:55:56 -08004429 // Any function called from threadLoop() must not hold mInterfaceLock since
4430 // it could lead to deadlocks (disconnect() -> hold mInterfaceMutex -> wait for request thread
4431 // to finish -> request thread waits on mInterfaceMutex) http://b/143513518
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004432
4433 // Handle paused state.
4434 if (waitIfPaused()) {
4435 return true;
4436 }
4437
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004438 // Wait for the next batch of requests.
4439 waitForNextRequestBatch();
4440 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004441 return true;
4442 }
4443
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004444 // Get the latest request ID, if any
4445 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004446 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00004447 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004448 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004449 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004450 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004451 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
4452 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004453 }
4454
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004455 // 'mNextRequests' will at this point contain either a set of HFR batched requests
4456 // or a single request from streaming or burst. In either case the first element
4457 // should contain the latest camera settings that we need to check for any session
4458 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00004459 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004460 res = OK;
4461
4462 //Input stream buffers are already acquired at this point so an input stream
4463 //will not be able to move to idle state unless we force it.
4464 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
4465 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
4466 if (res != OK) {
4467 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
4468 cleanUpFailedRequests(/*sendRequestError*/ false);
4469 return false;
4470 }
4471 }
4472
4473 if (res == OK) {
Emilian Peev3bead5f2020-05-28 17:29:08 -07004474 sp<Camera3Device> parent = mParent.promote();
4475 if (parent != nullptr) {
4476 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams, mStatusId);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004477 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07004478 setPaused(false);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004479
4480 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
4481 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
4482 if (res != OK) {
4483 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
4484 cleanUpFailedRequests(/*sendRequestError*/ false);
4485 return false;
4486 }
4487 }
4488 }
4489 }
4490
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004491 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004492 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004493 if (res == TIMED_OUT) {
4494 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004495 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004496 // Check if any stream is abandoned.
4497 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004498 return true;
4499 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004500 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004501 return false;
4502 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004503
Zhijun Hecc27e112013-10-03 16:12:43 -07004504 // Inform waitUntilRequestProcessed thread of a new request ID
4505 {
4506 Mutex::Autolock al(mLatestRequestMutex);
4507
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004508 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07004509 mLatestRequestSignal.signal();
4510 }
4511
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004512 // Submit a batch of requests to HAL.
4513 // Use flush lock only when submitting multilple requests in a batch.
4514 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
4515 // which may take a long time to finish so synchronizing flush() and
4516 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
4517 // For now, only synchronize for high speed recording and we should figure something out for
4518 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004519 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07004520
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004521 if (useFlushLock) {
4522 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004523 }
4524
Zhijun Hef0645c12016-08-02 00:58:11 -07004525 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004526 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07004527
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08004528 sp<Camera3Device> parent = mParent.promote();
4529 if (parent != nullptr) {
4530 parent->mRequestBufferSM.onSubmittingRequest();
4531 }
4532
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004533 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07004534 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh11648852019-05-16 10:42:54 -07004535 submitRequestSuccess = sendRequestsBatch();
4536
Shuzhen Wang686f6442017-06-20 16:16:04 -07004537 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
4538 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07004539
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004540 if (useFlushLock) {
4541 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004542 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004543
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004544 // Unset as current request
4545 {
4546 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004547 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004548 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07004549 mRequestSubmittedSignal.signal();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004550
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004551 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004552}
4553
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004554status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004555 ATRACE_CALL();
4556
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07004557 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
Shuzhen Wang4a472662017-02-26 23:29:04 -08004558 for (size_t i = 0; i < mNextRequests.size(); i++) {
4559 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004560 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07004561 camera_capture_request_t* halRequest = &nextRequest.halRequest;
4562 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004563
4564 // Prepare a request to HAL
4565 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
4566
4567 // Insert any queued triggers (before metadata is locked)
4568 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004569 if (res < 0) {
4570 SET_ERR("RequestThread: Unable to insert triggers "
4571 "(capture request %d, HAL device: %s (%d)",
4572 halRequest->frame_number, strerror(-res), res);
4573 return INVALID_OPERATION;
4574 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07004575
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004576 int triggerCount = res;
4577 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
4578 mPrevTriggers = triggerCount;
4579
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004580 bool rotateAndCropChanged = overrideAutoRotateAndCrop(captureRequest);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004581 bool testPatternChanged = overrideTestPattern(captureRequest);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004582
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004583 // If the request is the same as last, or we had triggers now or last time or
4584 // changing overrides this time
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004585 bool newRequest =
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004586 (mPrevRequest != captureRequest || triggersMixedIn ||
4587 rotateAndCropChanged || testPatternChanged) &&
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07004588 // Request settings are all the same within one batch, so only treat the first
4589 // request in a batch as new
Zhijun He54c36822018-07-18 09:33:39 -07004590 !(batchedRequest && i > 0);
Emilian Peev00420d22018-02-05 21:33:13 +00004591 if (newRequest) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08004592 std::set<std::string> cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004593 /**
4594 * HAL workaround:
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004595 * Insert a fake trigger ID if a trigger is set but no trigger ID is
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004596 */
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004597 res = addFakeTriggerIds(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004598 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004599 SET_ERR("RequestThread: Unable to insert fake trigger IDs "
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004600 "(capture request %d, HAL device: %s (%d)",
4601 halRequest->frame_number, strerror(-res), res);
4602 return INVALID_OPERATION;
4603 }
4604
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07004605 {
4606 // Correct metadata regions for distortion correction if enabled
4607 sp<Camera3Device> parent = mParent.promote();
4608 if (parent != nullptr) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07004609 List<PhysicalCameraSettings>::iterator it;
4610 for (it = captureRequest->mSettingsList.begin();
4611 it != captureRequest->mSettingsList.end(); it++) {
4612 if (parent->mDistortionMappers.find(it->cameraId) ==
4613 parent->mDistortionMappers.end()) {
4614 continue;
4615 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07004616
4617 if (!captureRequest->mDistortionCorrectionUpdated) {
4618 res = parent->mDistortionMappers[it->cameraId].correctCaptureRequest(
4619 &(it->metadata));
4620 if (res != OK) {
4621 SET_ERR("RequestThread: Unable to correct capture requests "
4622 "for lens distortion for request %d: %s (%d)",
4623 halRequest->frame_number, strerror(-res), res);
4624 return INVALID_OPERATION;
4625 }
4626 captureRequest->mDistortionCorrectionUpdated = true;
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07004627 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07004628 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08004629
4630 for (it = captureRequest->mSettingsList.begin();
4631 it != captureRequest->mSettingsList.end(); it++) {
4632 if (parent->mZoomRatioMappers.find(it->cameraId) ==
4633 parent->mZoomRatioMappers.end()) {
4634 continue;
4635 }
4636
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07004637 if (!captureRequest->mZoomRatioIs1x) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08004638 cameraIdsWithZoom.insert(it->cameraId);
4639 }
4640
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07004641 if (!captureRequest->mZoomRatioUpdated) {
4642 res = parent->mZoomRatioMappers[it->cameraId].updateCaptureRequest(
4643 &(it->metadata));
4644 if (res != OK) {
4645 SET_ERR("RequestThread: Unable to correct capture requests "
4646 "for zoom ratio for request %d: %s (%d)",
4647 halRequest->frame_number, strerror(-res), res);
4648 return INVALID_OPERATION;
4649 }
4650 captureRequest->mZoomRatioUpdated = true;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08004651 }
4652 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07004653 if (captureRequest->mRotateAndCropAuto &&
4654 !captureRequest->mRotationAndCropUpdated) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004655 for (it = captureRequest->mSettingsList.begin();
4656 it != captureRequest->mSettingsList.end(); it++) {
4657 auto mapper = parent->mRotateAndCropMappers.find(it->cameraId);
4658 if (mapper != parent->mRotateAndCropMappers.end()) {
4659 res = mapper->second.updateCaptureRequest(&(it->metadata));
4660 if (res != OK) {
4661 SET_ERR("RequestThread: Unable to correct capture requests "
4662 "for rotate-and-crop for request %d: %s (%d)",
4663 halRequest->frame_number, strerror(-res), res);
4664 return INVALID_OPERATION;
4665 }
4666 }
4667 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07004668 captureRequest->mRotationAndCropUpdated = true;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004669 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07004670 }
4671 }
4672
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004673 /**
4674 * The request should be presorted so accesses in HAL
4675 * are O(logn). Sidenote, sorting a sorted metadata is nop.
4676 */
Emilian Peevaebbe412018-01-15 13:53:24 +00004677 captureRequest->mSettingsList.begin()->metadata.sort();
4678 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004679 mPrevRequest = captureRequest;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08004680 mPrevCameraIdsWithZoom = cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004681 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
4682
4683 IF_ALOGV() {
4684 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4685 find_camera_metadata_ro_entry(
4686 halRequest->settings,
4687 ANDROID_CONTROL_AF_TRIGGER,
4688 &e
4689 );
4690 if (e.count > 0) {
4691 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
4692 __FUNCTION__,
4693 halRequest->frame_number,
4694 e.data.u8[0]);
4695 }
4696 }
4697 } else {
4698 // leave request.settings NULL to indicate 'reuse latest given'
4699 ALOGVV("%s: Request settings are REUSED",
4700 __FUNCTION__);
4701 }
4702
Emilian Peevaebbe412018-01-15 13:53:24 +00004703 if (captureRequest->mSettingsList.size() > 1) {
4704 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
4705 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00004706 if (newRequest) {
4707 halRequest->physcam_settings =
4708 new const camera_metadata* [halRequest->num_physcam_settings];
4709 } else {
4710 halRequest->physcam_settings = nullptr;
4711 }
Emilian Peevaebbe412018-01-15 13:53:24 +00004712 auto it = ++captureRequest->mSettingsList.begin();
4713 size_t i = 0;
4714 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
4715 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00004716 if (newRequest) {
4717 it->metadata.sort();
4718 halRequest->physcam_settings[i] = it->metadata.getAndLock();
4719 }
Emilian Peevaebbe412018-01-15 13:53:24 +00004720 }
4721 }
4722
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004723 uint32_t totalNumBuffers = 0;
4724
4725 // Fill in buffers
4726 if (captureRequest->mInputStream != NULL) {
4727 halRequest->input_buffer = &captureRequest->mInputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08004728
4729 halRequest->input_width = captureRequest->mInputBufferSize.width;
4730 halRequest->input_height = captureRequest->mInputBufferSize.height;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004731 totalNumBuffers += 1;
4732 } else {
4733 halRequest->input_buffer = NULL;
4734 }
4735
Emilian Peevf4816702020-04-03 15:44:51 -07004736 outputBuffers->insertAt(camera_stream_buffer_t(), 0,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004737 captureRequest->mOutputStreams.size());
4738 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang99080502021-03-07 21:08:20 -08004739 std::set<std::set<String8>> requestedPhysicalCameras;
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07004740
4741 sp<Camera3Device> parent = mParent.promote();
4742 if (parent == NULL) {
4743 // Should not happen, and nowhere to send errors to, so just log it
4744 CLOGE("RequestThread: Parent is gone");
4745 return INVALID_OPERATION;
4746 }
4747 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
4748
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004749 SurfaceMap uniqueSurfaceIdMap;
Shuzhen Wang4a472662017-02-26 23:29:04 -08004750 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004751 sp<Camera3OutputStreamInterface> outputStream =
4752 captureRequest->mOutputStreams.editItemAt(j);
4753 int streamId = outputStream->getId();
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07004754
4755 // Prepare video buffers for high speed recording on the first video request.
4756 if (mPrepareVideoStream && outputStream->isVideoStream()) {
4757 // Only try to prepare video stream on the first video request.
4758 mPrepareVideoStream = false;
4759
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07004760 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
4761 false /*blockRequest*/);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07004762 while (res == NOT_ENOUGH_DATA) {
4763 res = outputStream->prepareNextBuffer();
4764 }
4765 if (res != OK) {
4766 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
4767 __FUNCTION__, strerror(-res), res);
4768 outputStream->cancelPrepare();
4769 }
4770 }
4771
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004772 std::vector<size_t> uniqueSurfaceIds;
4773 res = outputStream->getUniqueSurfaceIds(
4774 captureRequest->mOutputSurfaces[streamId],
4775 &uniqueSurfaceIds);
4776 // INVALID_OPERATION is normal output for streams not supporting surfaceIds
4777 if (res != OK && res != INVALID_OPERATION) {
4778 ALOGE("%s: failed to query stream %d unique surface IDs",
4779 __FUNCTION__, streamId);
4780 return res;
4781 }
4782 if (res == OK) {
4783 uniqueSurfaceIdMap.insert({streamId, std::move(uniqueSurfaceIds)});
4784 }
4785
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004786 if (mUseHalBufManager) {
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08004787 if (outputStream->isAbandoned()) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07004788 ALOGV("%s: stream %d is abandoned, skipping request", __FUNCTION__, streamId);
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08004789 return TIMED_OUT;
4790 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004791 // HAL will request buffer through requestStreamBuffer API
Emilian Peevf4816702020-04-03 15:44:51 -07004792 camera_stream_buffer_t& buffer = outputBuffers->editItemAt(j);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004793 buffer.stream = outputStream->asHalStream();
4794 buffer.buffer = nullptr;
Emilian Peevf4816702020-04-03 15:44:51 -07004795 buffer.status = CAMERA_BUFFER_STATUS_OK;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004796 buffer.acquire_fence = -1;
4797 buffer.release_fence = -1;
Emilian Peevf0348ae2021-01-13 13:39:45 -08004798 // Mark the output stream as unpreparable to block clients from calling
4799 // 'prepare' after this request reaches CameraHal and before the respective
4800 // buffers are requested.
4801 outputStream->markUnpreparable();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004802 } else {
4803 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
4804 waitDuration,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004805 captureRequest->mOutputSurfaces[streamId]);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004806 if (res != OK) {
4807 // Can't get output buffer from gralloc queue - this could be due to
4808 // abandoned queue or other consumer misbehavior, so not a fatal
4809 // error
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07004810 ALOGV("RequestThread: Can't get output buffer, skipping request:"
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004811 " %s (%d)", strerror(-res), res);
4812
4813 return TIMED_OUT;
4814 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004815 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08004816
4817 {
4818 sp<Camera3Device> parent = mParent.promote();
4819 if (parent != nullptr) {
4820 const String8& streamCameraId = outputStream->getPhysicalCameraId();
4821 for (const auto& settings : captureRequest->mSettingsList) {
4822 if ((streamCameraId.isEmpty() &&
4823 parent->getId() == settings.cameraId.c_str()) ||
4824 streamCameraId == settings.cameraId.c_str()) {
4825 outputStream->fireBufferRequestForFrameNumber(
4826 captureRequest->mResultExtras.frameNumber,
4827 settings.metadata);
4828 }
4829 }
4830 }
4831 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07004832
Shuzhen Wang5c22c152017-12-31 17:12:25 -08004833 String8 physicalCameraId = outputStream->getPhysicalCameraId();
Shuzhen Wang99080502021-03-07 21:08:20 -08004834 int32_t streamGroupId = outputStream->getHalStreamGroupId();
4835 if (streamGroupId != -1 && mGroupIdPhysicalCameraMap.count(streamGroupId) == 1) {
4836 requestedPhysicalCameras.insert(mGroupIdPhysicalCameraMap[streamGroupId]);
4837 } else if (!physicalCameraId.isEmpty()) {
4838 requestedPhysicalCameras.insert(std::set<String8>({physicalCameraId}));
Shuzhen Wang5c22c152017-12-31 17:12:25 -08004839 }
4840 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004841 }
4842 totalNumBuffers += halRequest->num_output_buffers;
4843
4844 // Log request in the in-flight queue
Shuzhen Wang4a472662017-02-26 23:29:04 -08004845 // If this request list is for constrained high speed recording (not
4846 // preview), and the current request is not the last one in the batch,
4847 // do not send callback to the app.
4848 bool hasCallback = true;
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07004849 if (batchedRequest && i != mNextRequests.size()-1) {
Shuzhen Wang4a472662017-02-26 23:29:04 -08004850 hasCallback = false;
4851 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01004852 bool isStillCapture = false;
Shuzhen Wang26abaf42018-08-28 15:41:20 -07004853 bool isZslCapture = false;
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004854 const camera_metadata_t* settings = halRequest->settings;
4855 bool shouldUnlockSettings = false;
4856 if (settings == nullptr) {
4857 shouldUnlockSettings = true;
4858 settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
4859 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01004860 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
4861 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004862 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
Emilian Peev9dd21f42018-08-03 13:39:29 +01004863 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
4864 isStillCapture = true;
4865 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
4866 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07004867
Emilian Peevaf8416e2021-02-04 17:52:43 -08004868 e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004869 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
Shuzhen Wang26abaf42018-08-28 15:41:20 -07004870 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
4871 isZslCapture = true;
4872 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01004873 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004874 res = parent->registerInFlight(halRequest->frame_number,
4875 totalNumBuffers, captureRequest->mResultExtras,
4876 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004877 hasCallback,
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004878 calculateMaxExpectedDuration(settings),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004879 requestedPhysicalCameras, isStillCapture, isZslCapture,
4880 captureRequest->mRotateAndCropAuto, mPrevCameraIdsWithZoom,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004881 (mUseHalBufManager) ? uniqueSurfaceIdMap :
Shuzhen Wang316781a2020-08-18 18:11:01 -07004882 SurfaceMap{}, captureRequest->mRequestTimeNs);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004883 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
4884 ", burstId = %" PRId32 ".",
4885 __FUNCTION__,
4886 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
4887 captureRequest->mResultExtras.burstId);
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004888
4889 if (shouldUnlockSettings) {
4890 captureRequest->mSettingsList.begin()->metadata.unlock(settings);
4891 }
4892
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004893 if (res != OK) {
4894 SET_ERR("RequestThread: Unable to register new in-flight request:"
4895 " %s (%d)", strerror(-res), res);
4896 return INVALID_OPERATION;
4897 }
4898 }
4899
4900 return OK;
4901}
4902
Igor Murashkin1e479c02013-09-06 16:55:14 -07004903CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004904 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07004905 Mutex::Autolock al(mLatestRequestMutex);
4906
4907 ALOGV("RequestThread::%s", __FUNCTION__);
4908
4909 return mLatestRequest;
4910}
4911
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004912bool Camera3Device::RequestThread::isStreamPending(
4913 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004914 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004915 Mutex::Autolock l(mRequestLock);
4916
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004917 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004918 if (!nextRequest.submitted) {
4919 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
4920 if (stream == s) return true;
4921 }
4922 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004923 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004924 }
4925
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004926 for (const auto& request : mRequestQueue) {
4927 for (const auto& s : request->mOutputStreams) {
4928 if (stream == s) return true;
4929 }
4930 if (stream == request->mInputStream) return true;
4931 }
4932
4933 for (const auto& request : mRepeatingRequests) {
4934 for (const auto& s : request->mOutputStreams) {
4935 if (stream == s) return true;
4936 }
4937 if (stream == request->mInputStream) return true;
4938 }
4939
4940 return false;
4941}
Jianing Weicb0652e2014-03-12 18:29:36 -07004942
Emilian Peev40ead602017-09-26 15:46:36 +01004943bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
4944 ATRACE_CALL();
4945 Mutex::Autolock l(mRequestLock);
4946
4947 for (const auto& nextRequest : mNextRequests) {
4948 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
4949 if (s.first == streamId) {
4950 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4951 if (it != s.second.end()) {
4952 return true;
4953 }
4954 }
4955 }
4956 }
4957
4958 for (const auto& request : mRequestQueue) {
4959 for (const auto& s : request->mOutputSurfaces) {
4960 if (s.first == streamId) {
4961 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4962 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004963 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004964 }
4965 }
4966 }
4967 }
4968
4969 for (const auto& request : mRepeatingRequests) {
4970 for (const auto& s : request->mOutputSurfaces) {
4971 if (s.first == streamId) {
4972 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4973 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004974 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004975 }
4976 }
4977 }
4978 }
4979
4980 return false;
4981}
4982
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004983void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
4984 if (!mUseHalBufManager) {
4985 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
4986 return;
4987 }
4988
4989 Mutex::Autolock pl(mPauseLock);
4990 if (mPaused) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004991 mInterface->signalPipelineDrain(streamIds);
4992 return;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004993 }
4994 // If request thread is still busy, wait until paused then notify HAL
4995 mNotifyPipelineDrain = true;
4996 mStreamIdsToBeDrained = streamIds;
4997}
4998
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07004999void Camera3Device::RequestThread::resetPipelineDrain() {
5000 Mutex::Autolock pl(mPauseLock);
5001 mNotifyPipelineDrain = false;
5002 mStreamIdsToBeDrained.clear();
5003}
5004
Emilian Peevc0fe54c2020-03-11 14:05:07 -07005005void Camera3Device::RequestThread::clearPreviousRequest() {
5006 Mutex::Autolock l(mRequestLock);
5007 mPrevRequest.clear();
5008}
5009
Yin-Chia Yehb978c382019-10-30 00:22:37 -07005010status_t Camera3Device::RequestThread::switchToOffline(
5011 const std::vector<int32_t>& streamsToKeep,
5012 /*out*/hardware::camera::device::V3_6::CameraOfflineSessionInfo* offlineSessionInfo,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005013 /*out*/sp<hardware::camera::device::V3_6::ICameraOfflineSession>* offlineSession,
5014 /*out*/camera3::BufferRecords* bufferRecords) {
Yin-Chia Yehb978c382019-10-30 00:22:37 -07005015 Mutex::Autolock l(mRequestLock);
5016 clearRepeatingRequestsLocked(/*lastFrameNumber*/nullptr);
5017
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005018 // Wait until request thread is fully stopped
5019 // TBD: check if request thread is being paused by other APIs (shouldn't be)
5020
5021 // We could also check for mRepeatingRequests.empty(), but the API interface
5022 // is serialized by Camera3Device::mInterfaceLock so no one should be able to submit any
5023 // new requests during the call; hence skip that check.
Yin-Chia Yehb978c382019-10-30 00:22:37 -07005024 bool queueEmpty = mNextRequests.empty() && mRequestQueue.empty();
5025 while (!queueEmpty) {
5026 status_t res = mRequestSubmittedSignal.waitRelative(mRequestLock, kRequestSubmitTimeout);
5027 if (res == TIMED_OUT) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005028 ALOGE("%s: request thread failed to submit one request within timeout!", __FUNCTION__);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07005029 return res;
5030 } else if (res != OK) {
5031 ALOGE("%s: request thread failed to submit a request: %s (%d)!",
5032 __FUNCTION__, strerror(-res), res);
5033 return res;
5034 }
5035 queueEmpty = mNextRequests.empty() && mRequestQueue.empty();
5036 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005037
Yin-Chia Yehb978c382019-10-30 00:22:37 -07005038 return mInterface->switchToOffline(
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005039 streamsToKeep, offlineSessionInfo, offlineSession, bufferRecords);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07005040}
5041
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005042status_t Camera3Device::RequestThread::setRotateAndCropAutoBehavior(
5043 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
5044 ATRACE_CALL();
5045 Mutex::Autolock l(mTriggerMutex);
5046 if (rotateAndCropValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
5047 return BAD_VALUE;
5048 }
5049 mRotateAndCropOverride = rotateAndCropValue;
5050 return OK;
5051}
5052
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005053status_t Camera3Device::RequestThread::setCameraMute(bool enabled) {
5054 ATRACE_CALL();
5055 Mutex::Autolock l(mTriggerMutex);
5056 if (enabled != mCameraMute) {
5057 mCameraMute = enabled;
5058 mCameraMuteChanged = true;
5059 }
5060 return OK;
5061}
5062
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07005063nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005064 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005065 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07005066 return mExpectedInflightDuration > kMinInflightDuration ?
5067 mExpectedInflightDuration : kMinInflightDuration;
5068}
5069
Emilian Peevaebbe412018-01-15 13:53:24 +00005070void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
Emilian Peevf4816702020-04-03 15:44:51 -07005071 camera_capture_request_t *halRequest) {
Emilian Peevaebbe412018-01-15 13:53:24 +00005072 if ((request == nullptr) || (halRequest == nullptr)) {
5073 ALOGE("%s: Invalid request!", __FUNCTION__);
5074 return;
5075 }
5076
5077 if (halRequest->num_physcam_settings > 0) {
5078 if (halRequest->physcam_id != nullptr) {
5079 delete [] halRequest->physcam_id;
5080 halRequest->physcam_id = nullptr;
5081 }
5082 if (halRequest->physcam_settings != nullptr) {
5083 auto it = ++(request->mSettingsList.begin());
5084 size_t i = 0;
5085 for (; it != request->mSettingsList.end(); it++, i++) {
5086 it->metadata.unlock(halRequest->physcam_settings[i]);
5087 }
5088 delete [] halRequest->physcam_settings;
5089 halRequest->physcam_settings = nullptr;
5090 }
5091 }
5092}
5093
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005094void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
5095 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005096 return;
5097 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005098
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005099 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005100 // Skip the ones that have been submitted successfully.
5101 if (nextRequest.submitted) {
5102 continue;
5103 }
5104
5105 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07005106 camera_capture_request_t* halRequest = &nextRequest.halRequest;
5107 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005108
5109 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00005110 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005111 }
5112
Emilian Peevaebbe412018-01-15 13:53:24 +00005113 cleanupPhysicalSettings(captureRequest, halRequest);
5114
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005115 if (captureRequest->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07005116 captureRequest->mInputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005117 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
5118 }
5119
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08005120 // No output buffer can be returned when using HAL buffer manager
5121 if (!mUseHalBufManager) {
5122 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
5123 //Buffers that failed processing could still have
5124 //valid acquire fence.
5125 int acquireFence = (*outputBuffers)[i].acquire_fence;
5126 if (0 <= acquireFence) {
5127 close(acquireFence);
5128 outputBuffers->editItemAt(i).acquire_fence = -1;
5129 }
Emilian Peevf4816702020-04-03 15:44:51 -07005130 outputBuffers->editItemAt(i).status = CAMERA_BUFFER_STATUS_ERROR;
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08005131 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0,
5132 /*timestampIncreasing*/true, std::vector<size_t> (),
5133 captureRequest->mResultExtras.frameNumber);
Emilian Peevc58cf4c2017-05-11 17:23:41 +01005134 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005135 }
5136
5137 if (sendRequestError) {
5138 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005139 sp<NotificationListener> listener = mListener.promote();
5140 if (listener != NULL) {
5141 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005142 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005143 captureRequest->mResultExtras);
5144 }
5145 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07005146
5147 // Remove yet-to-be submitted inflight request from inflightMap
5148 {
5149 sp<Camera3Device> parent = mParent.promote();
5150 if (parent != NULL) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005151 std::lock_guard<std::mutex> l(parent->mInFlightLock);
Shuzhen Wangcadb3302016-11-04 14:17:56 -07005152 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
5153 if (idx >= 0) {
5154 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
5155 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
5156 parent->removeInFlightMapEntryLocked(idx);
5157 }
5158 }
5159 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005160 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005161
5162 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005163 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005164}
5165
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005166void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005167 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005168 // Optimized a bit for the simple steady-state case (single repeating
5169 // request), to avoid putting that request in the queue temporarily.
5170 Mutex::Autolock l(mRequestLock);
5171
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005172 assert(mNextRequests.empty());
5173
5174 NextRequest nextRequest;
5175 nextRequest.captureRequest = waitForNextRequestLocked();
5176 if (nextRequest.captureRequest == nullptr) {
5177 return;
5178 }
5179
Emilian Peevf4816702020-04-03 15:44:51 -07005180 nextRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005181 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005182 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005183
5184 // Wait for additional requests
5185 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
5186
5187 for (size_t i = 1; i < batchSize; i++) {
5188 NextRequest additionalRequest;
5189 additionalRequest.captureRequest = waitForNextRequestLocked();
5190 if (additionalRequest.captureRequest == nullptr) {
5191 break;
5192 }
5193
Emilian Peevf4816702020-04-03 15:44:51 -07005194 additionalRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005195 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005196 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005197 }
5198
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005199 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08005200 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005201 mNextRequests.size(), batchSize);
5202 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005203 }
5204
5205 return;
5206}
5207
5208sp<Camera3Device::CaptureRequest>
5209 Camera3Device::RequestThread::waitForNextRequestLocked() {
5210 status_t res;
5211 sp<CaptureRequest> nextRequest;
5212
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005213 while (mRequestQueue.empty()) {
5214 if (!mRepeatingRequests.empty()) {
5215 // Always atomically enqueue all requests in a repeating request
5216 // list. Guarantees a complete in-sequence set of captures to
5217 // application.
5218 const RequestList &requests = mRepeatingRequests;
Shuzhen Wang316781a2020-08-18 18:11:01 -07005219 if (mFirstRepeating) {
5220 mFirstRepeating = false;
5221 } else {
5222 for (auto& request : requests) {
5223 // For repeating requests, override timestamp request using
5224 // the time a request is inserted into the request queue,
5225 // because the original repeating request will have an old
5226 // fixed timestamp.
5227 request->mRequestTimeNs = systemTime();
5228 }
5229 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005230 RequestList::const_iterator firstRequest =
5231 requests.begin();
5232 nextRequest = *firstRequest;
5233 mRequestQueue.insert(mRequestQueue.end(),
5234 ++firstRequest,
5235 requests.end());
5236 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07005237
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005238 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07005239
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005240 break;
5241 }
5242
5243 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
5244
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005245 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
5246 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005247 Mutex::Autolock pl(mPauseLock);
5248 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005249 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005250 mPaused = true;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07005251 if (mNotifyPipelineDrain) {
5252 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
5253 mNotifyPipelineDrain = false;
5254 mStreamIdsToBeDrained.clear();
5255 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08005256 // Let the tracker know
5257 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5258 if (statusTracker != 0) {
5259 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
5260 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005261 sp<Camera3Device> parent = mParent.promote();
5262 if (parent != nullptr) {
5263 parent->mRequestBufferSM.onRequestThreadPaused();
5264 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005265 }
5266 // Stop waiting for now and let thread management happen
5267 return NULL;
5268 }
5269 }
5270
5271 if (nextRequest == NULL) {
5272 // Don't have a repeating request already in hand, so queue
5273 // must have an entry now.
5274 RequestList::iterator firstRequest =
5275 mRequestQueue.begin();
5276 nextRequest = *firstRequest;
5277 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07005278 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
5279 sp<NotificationListener> listener = mListener.promote();
5280 if (listener != NULL) {
5281 listener->notifyRequestQueueEmpty();
5282 }
5283 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005284 }
5285
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005286 // In case we've been unpaused by setPaused clearing mDoPause, need to
5287 // update internal pause state (capture/setRepeatingRequest unpause
5288 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005289 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005290 if (mPaused) {
5291 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
5292 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5293 if (statusTracker != 0) {
5294 statusTracker->markComponentActive(mStatusId);
5295 }
5296 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005297 mPaused = false;
5298
5299 // Check if we've reconfigured since last time, and reset the preview
5300 // request if so. Can't use 'NULL request == repeat' across configure calls.
5301 if (mReconfigured) {
5302 mPrevRequest.clear();
5303 mReconfigured = false;
5304 }
5305
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005306 if (nextRequest != NULL) {
5307 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07005308 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
5309 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005310
5311 // Since RequestThread::clear() removes buffers from the input stream,
5312 // get the right buffer here before unlocking mRequestLock
5313 if (nextRequest->mInputStream != NULL) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08005314 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer,
5315 &nextRequest->mInputBufferSize);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005316 if (res != OK) {
5317 // Can't get input buffer from gralloc queue - this could be due to
5318 // disconnected queue or other producer misbehavior, so not a fatal
5319 // error
5320 ALOGE("%s: Can't get input buffer, skipping request:"
5321 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005322
5323 sp<NotificationListener> listener = mListener.promote();
5324 if (listener != NULL) {
5325 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005326 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005327 nextRequest->mResultExtras);
5328 }
5329 return NULL;
5330 }
5331 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005332 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07005333
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005334 return nextRequest;
5335}
5336
5337bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005338 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005339 status_t res;
5340 Mutex::Autolock l(mPauseLock);
5341 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005342 if (mPaused == false) {
5343 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005344 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07005345 if (mNotifyPipelineDrain) {
5346 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
5347 mNotifyPipelineDrain = false;
5348 mStreamIdsToBeDrained.clear();
5349 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08005350 // Let the tracker know
5351 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5352 if (statusTracker != 0) {
5353 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
5354 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005355 sp<Camera3Device> parent = mParent.promote();
5356 if (parent != nullptr) {
5357 parent->mRequestBufferSM.onRequestThreadPaused();
5358 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005359 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005360
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005361 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005362 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005363 return true;
5364 }
5365 }
5366 // We don't set mPaused to false here, because waitForNextRequest needs
5367 // to further manage the paused state in case of starvation.
5368 return false;
5369}
5370
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005371void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005372 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005373 // With work to do, mark thread as unpaused.
5374 // If paused by request (setPaused), don't resume, to avoid
5375 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005376 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005377 Mutex::Autolock p(mPauseLock);
5378 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005379 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
5380 if (mPaused) {
5381 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5382 if (statusTracker != 0) {
5383 statusTracker->markComponentActive(mStatusId);
5384 }
5385 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005386 mPaused = false;
5387 }
5388}
5389
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07005390void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
5391 sp<Camera3Device> parent = mParent.promote();
5392 if (parent != NULL) {
5393 va_list args;
5394 va_start(args, fmt);
5395
5396 parent->setErrorStateV(fmt, args);
5397
5398 va_end(args);
5399 }
5400}
5401
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005402status_t Camera3Device::RequestThread::insertTriggers(
5403 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005404 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005405 Mutex::Autolock al(mTriggerMutex);
5406
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005407 sp<Camera3Device> parent = mParent.promote();
5408 if (parent == NULL) {
5409 CLOGE("RequestThread: Parent is gone");
5410 return DEAD_OBJECT;
5411 }
5412
Emilian Peevaebbe412018-01-15 13:53:24 +00005413 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005414 size_t count = mTriggerMap.size();
5415
5416 for (size_t i = 0; i < count; ++i) {
5417 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005418 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005419
5420 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
5421 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
5422 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07005423 if (isAeTrigger) {
5424 request->mResultExtras.precaptureTriggerId = triggerId;
5425 mCurrentPreCaptureTriggerId = triggerId;
5426 } else {
5427 request->mResultExtras.afTriggerId = triggerId;
5428 mCurrentAfTriggerId = triggerId;
5429 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01005430 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005431 }
5432
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005433 camera_metadata_entry entry = metadata.find(tag);
5434
5435 if (entry.count > 0) {
5436 /**
5437 * Already has an entry for this trigger in the request.
5438 * Rewrite it with our requested trigger value.
5439 */
5440 RequestTrigger oldTrigger = trigger;
5441
5442 oldTrigger.entryValue = entry.data.u8[0];
5443
5444 mTriggerReplacedMap.add(tag, oldTrigger);
5445 } else {
5446 /**
5447 * More typical, no trigger entry, so we just add it
5448 */
5449 mTriggerRemovedMap.add(tag, trigger);
5450 }
5451
5452 status_t res;
5453
5454 switch (trigger.getTagType()) {
5455 case TYPE_BYTE: {
5456 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
5457 res = metadata.update(tag,
5458 &entryValue,
5459 /*count*/1);
5460 break;
5461 }
5462 case TYPE_INT32:
5463 res = metadata.update(tag,
5464 &trigger.entryValue,
5465 /*count*/1);
5466 break;
5467 default:
5468 ALOGE("%s: Type not supported: 0x%x",
5469 __FUNCTION__,
5470 trigger.getTagType());
5471 return INVALID_OPERATION;
5472 }
5473
5474 if (res != OK) {
5475 ALOGE("%s: Failed to update request metadata with trigger tag %s"
5476 ", value %d", __FUNCTION__, trigger.getTagName(),
5477 trigger.entryValue);
5478 return res;
5479 }
5480
5481 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
5482 trigger.getTagName(),
5483 trigger.entryValue);
5484 }
5485
5486 mTriggerMap.clear();
5487
5488 return count;
5489}
5490
5491status_t Camera3Device::RequestThread::removeTriggers(
5492 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005493 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005494 Mutex::Autolock al(mTriggerMutex);
5495
Emilian Peevaebbe412018-01-15 13:53:24 +00005496 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005497
5498 /**
5499 * Replace all old entries with their old values.
5500 */
5501 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
5502 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
5503
5504 status_t res;
5505
5506 uint32_t tag = trigger.metadataTag;
5507 switch (trigger.getTagType()) {
5508 case TYPE_BYTE: {
5509 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
5510 res = metadata.update(tag,
5511 &entryValue,
5512 /*count*/1);
5513 break;
5514 }
5515 case TYPE_INT32:
5516 res = metadata.update(tag,
5517 &trigger.entryValue,
5518 /*count*/1);
5519 break;
5520 default:
5521 ALOGE("%s: Type not supported: 0x%x",
5522 __FUNCTION__,
5523 trigger.getTagType());
5524 return INVALID_OPERATION;
5525 }
5526
5527 if (res != OK) {
5528 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
5529 ", trigger value %d", __FUNCTION__,
5530 trigger.getTagName(), trigger.entryValue);
5531 return res;
5532 }
5533 }
5534 mTriggerReplacedMap.clear();
5535
5536 /**
5537 * Remove all new entries.
5538 */
5539 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
5540 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
5541 status_t res = metadata.erase(trigger.metadataTag);
5542
5543 if (res != OK) {
5544 ALOGE("%s: Failed to erase metadata with trigger tag %s"
5545 ", trigger value %d", __FUNCTION__,
5546 trigger.getTagName(), trigger.entryValue);
5547 return res;
5548 }
5549 }
5550 mTriggerRemovedMap.clear();
5551
5552 return OK;
5553}
5554
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04005555status_t Camera3Device::RequestThread::addFakeTriggerIds(
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005556 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08005557 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04005558 static const int32_t fakeTriggerId = 1;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005559 status_t res;
5560
Emilian Peevaebbe412018-01-15 13:53:24 +00005561 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005562
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04005563 // If AF trigger is active, insert a fake AF trigger ID if none already
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005564 // exists
5565 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
5566 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
5567 if (afTrigger.count > 0 &&
5568 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
5569 afId.count == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04005570 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005571 if (res != OK) return res;
5572 }
5573
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04005574 // If AE precapture trigger is active, insert a fake precapture trigger ID
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005575 // if none already exists
5576 camera_metadata_entry pcTrigger =
5577 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
5578 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
5579 if (pcTrigger.count > 0 &&
5580 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
5581 pcId.count == 0) {
5582 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04005583 &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005584 if (res != OK) return res;
5585 }
5586
5587 return OK;
5588}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005589
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005590bool Camera3Device::RequestThread::overrideAutoRotateAndCrop(
5591 const sp<CaptureRequest> &request) {
5592 ATRACE_CALL();
5593
5594 if (request->mRotateAndCropAuto) {
5595 Mutex::Autolock l(mTriggerMutex);
5596 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
5597
5598 auto rotateAndCropEntry = metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
5599 if (rotateAndCropEntry.count > 0) {
5600 if (rotateAndCropEntry.data.u8[0] == mRotateAndCropOverride) {
5601 return false;
5602 } else {
5603 rotateAndCropEntry.data.u8[0] = mRotateAndCropOverride;
5604 return true;
5605 }
5606 } else {
5607 uint8_t rotateAndCrop_u8 = mRotateAndCropOverride;
5608 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP,
5609 &rotateAndCrop_u8, 1);
5610 return true;
5611 }
5612 }
5613 return false;
5614}
5615
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005616bool Camera3Device::RequestThread::overrideTestPattern(
5617 const sp<CaptureRequest> &request) {
5618 ATRACE_CALL();
5619
5620 Mutex::Autolock l(mTriggerMutex);
5621
5622 bool changed = false;
5623
5624 int32_t testPatternMode = request->mOriginalTestPatternMode;
5625 int32_t testPatternData[4] = {
5626 request->mOriginalTestPatternData[0],
5627 request->mOriginalTestPatternData[1],
5628 request->mOriginalTestPatternData[2],
5629 request->mOriginalTestPatternData[3]
5630 };
5631
5632 if (mCameraMute) {
5633 testPatternMode = ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR;
5634 testPatternData[0] = 0;
5635 testPatternData[1] = 0;
5636 testPatternData[2] = 0;
5637 testPatternData[3] = 0;
5638 }
5639
5640 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
5641
5642 auto testPatternEntry = metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
5643 if (testPatternEntry.count > 0) {
5644 if (testPatternEntry.data.i32[0] != testPatternMode) {
5645 testPatternEntry.data.i32[0] = testPatternMode;
5646 changed = true;
5647 }
5648 } else {
5649 metadata.update(ANDROID_SENSOR_TEST_PATTERN_MODE,
5650 &testPatternMode, 1);
5651 changed = true;
5652 }
5653
5654 auto testPatternColor = metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
5655 if (testPatternColor.count > 0) {
5656 for (size_t i = 0; i < 4; i++) {
5657 if (testPatternColor.data.i32[i] != (int32_t)testPatternData[i]) {
5658 testPatternColor.data.i32[i] = testPatternData[i];
5659 changed = true;
5660 }
5661 }
5662 } else {
5663 metadata.update(ANDROID_SENSOR_TEST_PATTERN_DATA,
5664 (int32_t*)testPatternData, 4);
5665 changed = true;
5666 }
5667
5668 return changed;
5669}
5670
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005671/**
5672 * PreparerThread inner class methods
5673 */
5674
5675Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07005676 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005677 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005678}
5679
5680Camera3Device::PreparerThread::~PreparerThread() {
5681 Thread::requestExitAndWait();
5682 if (mCurrentStream != nullptr) {
5683 mCurrentStream->cancelPrepare();
5684 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5685 mCurrentStream.clear();
5686 }
5687 clear();
5688}
5689
Ruben Brunkc78ac262015-08-13 17:58:46 -07005690status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005691 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005692 status_t res;
5693
5694 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005695 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005696
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07005697 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005698 if (res == OK) {
5699 // No preparation needed, fire listener right off
5700 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005701 if (listener != NULL) {
5702 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005703 }
5704 return OK;
5705 } else if (res != NOT_ENOUGH_DATA) {
5706 return res;
5707 }
5708
5709 // Need to prepare, start up thread if necessary
5710 if (!mActive) {
5711 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
5712 // isn't running
5713 Thread::requestExitAndWait();
5714 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5715 if (res != OK) {
5716 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005717 if (listener != NULL) {
5718 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005719 }
5720 return res;
5721 }
5722 mCancelNow = false;
5723 mActive = true;
5724 ALOGV("%s: Preparer stream started", __FUNCTION__);
5725 }
5726
5727 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005728 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005729 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
5730
5731 return OK;
5732}
5733
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005734void Camera3Device::PreparerThread::pause() {
5735 ATRACE_CALL();
5736
5737 Mutex::Autolock l(mLock);
5738
5739 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
5740 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
5741 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
5742 int currentMaxCount = mCurrentMaxCount;
5743 mPendingStreams.clear();
5744 mCancelNow = true;
5745 while (mActive) {
5746 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
5747 if (res == TIMED_OUT) {
5748 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
5749 return;
5750 } else if (res != OK) {
5751 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
5752 return;
5753 }
5754 }
5755
5756 //Check whether the prepare thread was able to complete the current
5757 //stream. In case work is still pending emplace it along with the rest
5758 //of the streams in the pending list.
5759 if (currentStream != nullptr) {
5760 if (!mCurrentPrepareComplete) {
5761 pendingStreams.emplace(currentMaxCount, currentStream);
5762 }
5763 }
5764
5765 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
5766 for (const auto& it : mPendingStreams) {
5767 it.second->cancelPrepare();
5768 }
5769}
5770
5771status_t Camera3Device::PreparerThread::resume() {
5772 ATRACE_CALL();
5773 status_t res;
5774
5775 Mutex::Autolock l(mLock);
5776 sp<NotificationListener> listener = mListener.promote();
5777
5778 if (mActive) {
5779 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
5780 return NO_INIT;
5781 }
5782
5783 auto it = mPendingStreams.begin();
5784 for (; it != mPendingStreams.end();) {
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07005785 res = it->second->startPrepare(it->first, true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005786 if (res == OK) {
5787 if (listener != NULL) {
5788 listener->notifyPrepared(it->second->getId());
5789 }
5790 it = mPendingStreams.erase(it);
5791 } else if (res != NOT_ENOUGH_DATA) {
5792 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
5793 res, strerror(-res));
5794 it = mPendingStreams.erase(it);
5795 } else {
5796 it++;
5797 }
5798 }
5799
5800 if (mPendingStreams.empty()) {
5801 return OK;
5802 }
5803
5804 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5805 if (res != OK) {
5806 ALOGE("%s: Unable to start preparer stream: %d (%s)",
5807 __FUNCTION__, res, strerror(-res));
5808 return res;
5809 }
5810 mCancelNow = false;
5811 mActive = true;
5812 ALOGV("%s: Preparer stream started", __FUNCTION__);
5813
5814 return OK;
5815}
5816
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005817status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005818 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005819 Mutex::Autolock l(mLock);
5820
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005821 for (const auto& it : mPendingStreams) {
5822 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005823 }
5824 mPendingStreams.clear();
5825 mCancelNow = true;
5826
5827 return OK;
5828}
5829
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005830void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005831 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005832 Mutex::Autolock l(mLock);
5833 mListener = listener;
5834}
5835
5836bool Camera3Device::PreparerThread::threadLoop() {
5837 status_t res;
5838 {
5839 Mutex::Autolock l(mLock);
5840 if (mCurrentStream == nullptr) {
5841 // End thread if done with work
5842 if (mPendingStreams.empty()) {
5843 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
5844 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
5845 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
5846 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005847 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005848 return false;
5849 }
5850
5851 // Get next stream to prepare
5852 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005853 mCurrentStream = it->second;
5854 mCurrentMaxCount = it->first;
5855 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005856 mPendingStreams.erase(it);
5857 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
5858 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
5859 } else if (mCancelNow) {
5860 mCurrentStream->cancelPrepare();
5861 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5862 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
5863 mCurrentStream.clear();
5864 mCancelNow = false;
5865 return true;
5866 }
5867 }
5868
5869 res = mCurrentStream->prepareNextBuffer();
5870 if (res == NOT_ENOUGH_DATA) return true;
5871 if (res != OK) {
5872 // Something bad happened; try to recover by cancelling prepare and
5873 // signalling listener anyway
5874 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
5875 mCurrentStream->getId(), res, strerror(-res));
5876 mCurrentStream->cancelPrepare();
5877 }
5878
5879 // This stream has finished, notify listener
5880 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005881 sp<NotificationListener> listener = mListener.promote();
5882 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005883 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
5884 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005885 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005886 }
5887
5888 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5889 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005890 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005891
5892 return true;
5893}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005894
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005895status_t Camera3Device::RequestBufferStateMachine::initialize(
5896 sp<camera3::StatusTracker> statusTracker) {
5897 if (statusTracker == nullptr) {
5898 ALOGE("%s: statusTracker is null", __FUNCTION__);
5899 return BAD_VALUE;
5900 }
5901
5902 std::lock_guard<std::mutex> lock(mLock);
5903 mStatusTracker = statusTracker;
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07005904 mRequestBufferStatusId = statusTracker->addComponent("BufferRequestSM");
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005905 return OK;
5906}
5907
5908bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
5909 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005910 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005911 mRequestBufferOngoing = true;
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005912 notifyTrackerLocked(/*active*/true);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005913 return true;
5914 }
5915 return false;
5916}
5917
5918void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
5919 std::lock_guard<std::mutex> lock(mLock);
5920 if (!mRequestBufferOngoing) {
5921 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
5922 return;
5923 }
5924 mRequestBufferOngoing = false;
5925 if (mStatus == RB_STATUS_PENDING_STOP) {
5926 checkSwitchToStopLocked();
5927 }
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005928 notifyTrackerLocked(/*active*/false);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005929}
5930
5931void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
5932 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005933 mSwitchedToOffline = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005934 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005935 return;
5936}
5937
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005938void Camera3Device::RequestBufferStateMachine::onSubmittingRequest() {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005939 std::lock_guard<std::mutex> lock(mLock);
5940 mRequestThreadPaused = false;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005941 // inflight map register actually happens in prepareHalRequest now, but it is close enough
5942 // approximation.
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005943 mInflightMapEmpty = false;
5944 if (mStatus == RB_STATUS_STOPPED) {
5945 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005946 }
5947 return;
5948}
5949
5950void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
5951 std::lock_guard<std::mutex> lock(mLock);
5952 mRequestThreadPaused = true;
5953 if (mStatus == RB_STATUS_PENDING_STOP) {
5954 checkSwitchToStopLocked();
5955 }
5956 return;
5957}
5958
5959void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
5960 std::lock_guard<std::mutex> lock(mLock);
5961 mInflightMapEmpty = true;
5962 if (mStatus == RB_STATUS_PENDING_STOP) {
5963 checkSwitchToStopLocked();
5964 }
5965 return;
5966}
5967
5968void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
5969 std::lock_guard<std::mutex> lock(mLock);
5970 if (!checkSwitchToStopLocked()) {
5971 mStatus = RB_STATUS_PENDING_STOP;
5972 }
5973 return;
5974}
5975
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005976bool Camera3Device::RequestBufferStateMachine::onSwitchToOfflineSuccess() {
5977 std::lock_guard<std::mutex> lock(mLock);
5978 if (mRequestBufferOngoing) {
5979 ALOGE("%s: HAL must not be requesting buffer after HAL returns switchToOffline!",
5980 __FUNCTION__);
5981 return false;
5982 }
5983 mSwitchedToOffline = true;
5984 mInflightMapEmpty = true;
5985 mRequestThreadPaused = true;
5986 mStatus = RB_STATUS_STOPPED;
5987 return true;
5988}
5989
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005990void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
5991 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5992 if (statusTracker != nullptr) {
5993 if (active) {
5994 statusTracker->markComponentActive(mRequestBufferStatusId);
5995 } else {
5996 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
5997 }
5998 }
5999}
6000
6001bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
6002 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
6003 mStatus = RB_STATUS_STOPPED;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006004 return true;
6005 }
6006 return false;
6007}
6008
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006009bool Camera3Device::startRequestBuffer() {
6010 return mRequestBufferSM.startRequestBuffer();
6011}
Shuzhen Wang268a1362018-10-16 16:32:59 -07006012
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006013void Camera3Device::endRequestBuffer() {
6014 mRequestBufferSM.endRequestBuffer();
6015}
Shuzhen Wang268a1362018-10-16 16:32:59 -07006016
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006017nsecs_t Camera3Device::getWaitDuration() {
6018 return kBaseGetBufferWait + getExpectedInFlightDuration();
6019}
Shuzhen Wang268a1362018-10-16 16:32:59 -07006020
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006021void Camera3Device::getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) {
6022 mInterface->getInflightBufferKeys(out);
6023}
Shuzhen Wang268a1362018-10-16 16:32:59 -07006024
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006025void Camera3Device::getInflightRequestBufferKeys(std::vector<uint64_t>* out) {
6026 mInterface->getInflightRequestBufferKeys(out);
6027}
Shuzhen Wang268a1362018-10-16 16:32:59 -07006028
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006029std::vector<sp<Camera3StreamInterface>> Camera3Device::getAllStreams() {
6030 std::vector<sp<Camera3StreamInterface>> ret;
6031 bool hasInputStream = mInputStream != nullptr;
6032 ret.reserve(mOutputStreams.size() + mDeletedStreams.size() + ((hasInputStream) ? 1 : 0));
6033 if (hasInputStream) {
6034 ret.push_back(mInputStream);
Shuzhen Wang268a1362018-10-16 16:32:59 -07006035 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006036 for (size_t i = 0; i < mOutputStreams.size(); i++) {
6037 ret.push_back(mOutputStreams[i]);
6038 }
6039 for (size_t i = 0; i < mDeletedStreams.size(); i++) {
6040 ret.push_back(mDeletedStreams[i]);
6041 }
6042 return ret;
Shuzhen Wang268a1362018-10-16 16:32:59 -07006043}
6044
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006045status_t Camera3Device::switchToOffline(
6046 const std::vector<int32_t>& streamsToKeep,
6047 /*out*/ sp<CameraOfflineSessionBase>* session) {
6048 ATRACE_CALL();
6049 if (session == nullptr) {
6050 ALOGE("%s: session must not be null", __FUNCTION__);
6051 return BAD_VALUE;
6052 }
6053
6054 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006055
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006056 bool hasInputStream = mInputStream != nullptr;
6057 int32_t inputStreamId = hasInputStream ? mInputStream->getId() : -1;
6058 bool inputStreamSupportsOffline = hasInputStream ?
6059 mInputStream->getOfflineProcessingSupport() : false;
6060 auto outputStreamIds = mOutputStreams.getStreamIds();
6061 auto streamIds = outputStreamIds;
6062 if (hasInputStream) {
6063 streamIds.push_back(mInputStream->getId());
6064 }
6065
6066 // Check all streams in streamsToKeep supports offline mode
6067 for (auto id : streamsToKeep) {
6068 if (std::find(streamIds.begin(), streamIds.end(), id) == streamIds.end()) {
6069 ALOGE("%s: Unknown stream ID %d", __FUNCTION__, id);
6070 return BAD_VALUE;
6071 } else if (id == inputStreamId) {
6072 if (!inputStreamSupportsOffline) {
6073 ALOGE("%s: input stream %d cannot be switched to offline",
6074 __FUNCTION__, id);
6075 return BAD_VALUE;
6076 }
6077 } else {
6078 sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
6079 if (!stream->getOfflineProcessingSupport()) {
6080 ALOGE("%s: output stream %d cannot be switched to offline",
6081 __FUNCTION__, id);
6082 return BAD_VALUE;
6083 }
6084 }
6085 }
6086
6087 // TODO: block surface sharing and surface group streams until we can support them
6088
6089 // Stop repeating request, wait until all remaining requests are submitted, then call into
6090 // HAL switchToOffline
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006091 hardware::camera::device::V3_6::CameraOfflineSessionInfo offlineSessionInfo;
6092 sp<hardware::camera::device::V3_6::ICameraOfflineSession> offlineSession;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006093 camera3::BufferRecords bufferRecords;
6094 status_t ret = mRequestThread->switchToOffline(
6095 streamsToKeep, &offlineSessionInfo, &offlineSession, &bufferRecords);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006096
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006097 if (ret != OK) {
6098 SET_ERR("Switch to offline failed: %s (%d)", strerror(-ret), ret);
6099 return ret;
6100 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006101
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006102 bool succ = mRequestBufferSM.onSwitchToOfflineSuccess();
6103 if (!succ) {
6104 SET_ERR("HAL must not be calling requestStreamBuffers call");
6105 // TODO: block ALL callbacks from HAL till app configured new streams?
6106 return UNKNOWN_ERROR;
6107 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006108
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006109 // Verify offlineSessionInfo
6110 std::vector<int32_t> offlineStreamIds;
6111 offlineStreamIds.reserve(offlineSessionInfo.offlineStreams.size());
6112 for (auto offlineStream : offlineSessionInfo.offlineStreams) {
6113 // verify stream IDs
6114 int32_t id = offlineStream.id;
6115 if (std::find(streamIds.begin(), streamIds.end(), id) == streamIds.end()) {
6116 SET_ERR("stream ID %d not found!", id);
6117 return UNKNOWN_ERROR;
6118 }
6119
6120 // When not using HAL buf manager, only allow streams requested by app to be preserved
6121 if (!mUseHalBufManager) {
6122 if (std::find(streamsToKeep.begin(), streamsToKeep.end(), id) == streamsToKeep.end()) {
6123 SET_ERR("stream ID %d must not be switched to offline!", id);
6124 return UNKNOWN_ERROR;
6125 }
6126 }
6127
6128 offlineStreamIds.push_back(id);
6129 sp<Camera3StreamInterface> stream = (id == inputStreamId) ?
6130 static_cast<sp<Camera3StreamInterface>>(mInputStream) :
6131 static_cast<sp<Camera3StreamInterface>>(mOutputStreams.get(id));
6132 // Verify number of outstanding buffers
6133 if (stream->getOutstandingBuffersCount() != offlineStream.numOutstandingBuffers) {
6134 SET_ERR("Offline stream %d # of remaining buffer mismatch: (%zu,%d) (service/HAL)",
6135 id, stream->getOutstandingBuffersCount(), offlineStream.numOutstandingBuffers);
6136 return UNKNOWN_ERROR;
6137 }
6138 }
6139
6140 // Verify all streams to be deleted don't have any outstanding buffers
6141 if (hasInputStream && std::find(offlineStreamIds.begin(), offlineStreamIds.end(),
6142 inputStreamId) == offlineStreamIds.end()) {
6143 if (mInputStream->hasOutstandingBuffers()) {
6144 SET_ERR("Input stream %d still has %zu outstanding buffer!",
6145 inputStreamId, mInputStream->getOutstandingBuffersCount());
6146 return UNKNOWN_ERROR;
6147 }
6148 }
6149
6150 for (const auto& outStreamId : outputStreamIds) {
6151 if (std::find(offlineStreamIds.begin(), offlineStreamIds.end(),
6152 outStreamId) == offlineStreamIds.end()) {
6153 auto outStream = mOutputStreams.get(outStreamId);
6154 if (outStream->hasOutstandingBuffers()) {
6155 SET_ERR("Output stream %d still has %zu outstanding buffer!",
6156 outStreamId, outStream->getOutstandingBuffersCount());
6157 return UNKNOWN_ERROR;
6158 }
6159 }
6160 }
6161
6162 InFlightRequestMap offlineReqs;
6163 // Verify inflight requests and their pending buffers
6164 {
6165 std::lock_guard<std::mutex> l(mInFlightLock);
6166 for (auto offlineReq : offlineSessionInfo.offlineRequests) {
6167 int idx = mInFlightMap.indexOfKey(offlineReq.frameNumber);
6168 if (idx == NAME_NOT_FOUND) {
6169 SET_ERR("Offline request frame number %d not found!", offlineReq.frameNumber);
6170 return UNKNOWN_ERROR;
6171 }
6172
6173 const auto& inflightReq = mInFlightMap.valueAt(idx);
6174 // TODO: check specific stream IDs
6175 size_t numBuffersLeft = static_cast<size_t>(inflightReq.numBuffersLeft);
6176 if (numBuffersLeft != offlineReq.pendingStreams.size()) {
6177 SET_ERR("Offline request # of remaining buffer mismatch: (%d,%d) (service/HAL)",
6178 inflightReq.numBuffersLeft, offlineReq.pendingStreams.size());
6179 return UNKNOWN_ERROR;
6180 }
6181 offlineReqs.add(offlineReq.frameNumber, inflightReq);
6182 }
6183 }
6184
6185 // Create Camera3OfflineSession and transfer object ownership
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006186 // (streams, inflight requests, buffer caches)
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006187 camera3::StreamSet offlineStreamSet;
6188 sp<camera3::Camera3Stream> inputStream;
6189 for (auto offlineStream : offlineSessionInfo.offlineStreams) {
6190 int32_t id = offlineStream.id;
6191 if (mInputStream != nullptr && id == mInputStream->getId()) {
6192 inputStream = mInputStream;
6193 } else {
6194 offlineStreamSet.add(id, mOutputStreams.get(id));
6195 }
6196 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006197
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006198 // TODO: check if we need to lock before copying states
6199 // though technically no other thread should be talking to Camera3Device at this point
6200 Camera3OfflineStates offlineStates(
6201 mTagMonitor, mVendorTagId, mUseHalBufManager, mNeedFixupMonochromeTags,
Shuzhen Wangb7b42652020-05-07 11:59:02 -07006202 mUsePartialResult, mNumPartialResults, mLastCompletedRegularFrameNumber,
6203 mLastCompletedReprocessFrameNumber, mLastCompletedZslFrameNumber,
6204 mNextResultFrameNumber, mNextReprocessResultFrameNumber,
6205 mNextZslStillResultFrameNumber, mNextShutterFrameNumber,
6206 mNextReprocessShutterFrameNumber, mNextZslStillShutterFrameNumber,
6207 mDeviceInfo, mPhysicalDeviceInfoMap, mDistortionMappers,
6208 mZoomRatioMappers, mRotateAndCropMappers);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006209
6210 *session = new Camera3OfflineSession(mId, inputStream, offlineStreamSet,
6211 std::move(bufferRecords), offlineReqs, offlineStates, offlineSession);
6212
6213 // Delete all streams that has been transferred to offline session
6214 Mutex::Autolock l(mLock);
6215 for (auto offlineStream : offlineSessionInfo.offlineStreams) {
6216 int32_t id = offlineStream.id;
6217 if (mInputStream != nullptr && id == mInputStream->getId()) {
6218 mInputStream.clear();
6219 } else {
6220 mOutputStreams.remove(id);
6221 }
6222 }
6223
6224 // disconnect all other streams and switch to UNCONFIGURED state
6225 if (mInputStream != nullptr) {
6226 ret = mInputStream->disconnect();
6227 if (ret != OK) {
6228 SET_ERR_L("disconnect input stream failed!");
6229 return UNKNOWN_ERROR;
6230 }
6231 }
6232
6233 for (auto streamId : mOutputStreams.getStreamIds()) {
6234 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
6235 ret = stream->disconnect();
6236 if (ret != OK) {
6237 SET_ERR_L("disconnect output stream %d failed!", streamId);
6238 return UNKNOWN_ERROR;
6239 }
6240 }
6241
6242 mInputStream.clear();
6243 mOutputStreams.clear();
6244 mNeedConfig = true;
6245 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
6246 mOperatingMode = NO_MODE;
6247 mIsConstrainedHighSpeedConfiguration = false;
Emilian Peevc0fe54c2020-03-11 14:05:07 -07006248 mRequestThread->clearPreviousRequest();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006249
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006250 return OK;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006251 // TO be done by CameraDeviceClient/Camera3OfflineSession
6252 // register the offline client to camera service
6253 // Setup result passthing threads etc
6254 // Initialize offline session so HAL can start sending callback to it (result Fmq)
6255 // TODO: check how many onIdle callback will be sent
6256 // Java side to make sure the CameraCaptureSession is properly closed
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006257}
6258
Emilian Peevcc0b7952020-01-07 13:54:47 -08006259void Camera3Device::getOfflineStreamIds(std::vector<int> *offlineStreamIds) {
6260 ATRACE_CALL();
6261
6262 if (offlineStreamIds == nullptr) {
6263 return;
6264 }
6265
6266 Mutex::Autolock il(mInterfaceLock);
6267
6268 auto streamIds = mOutputStreams.getStreamIds();
6269 bool hasInputStream = mInputStream != nullptr;
6270 if (hasInputStream && mInputStream->getOfflineProcessingSupport()) {
6271 offlineStreamIds->push_back(mInputStream->getId());
6272 }
6273
6274 for (const auto & streamId : streamIds) {
6275 sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
6276 // Streams that use the camera buffer manager are currently not supported in
6277 // offline mode
6278 if (stream->getOfflineProcessingSupport() &&
6279 (stream->getStreamSetId() == CAMERA3_STREAM_SET_ID_INVALID)) {
6280 offlineStreamIds->push_back(streamId);
6281 }
6282 }
6283}
6284
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08006285status_t Camera3Device::setRotateAndCropAutoBehavior(
6286 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
6287 ATRACE_CALL();
6288 Mutex::Autolock il(mInterfaceLock);
6289 Mutex::Autolock l(mLock);
6290 if (mRequestThread == nullptr) {
6291 return INVALID_OPERATION;
6292 }
6293 return mRequestThread->setRotateAndCropAutoBehavior(rotateAndCropValue);
6294}
6295
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08006296bool Camera3Device::supportsCameraMute() {
6297 Mutex::Autolock il(mInterfaceLock);
6298 Mutex::Autolock l(mLock);
6299
6300 return mSupportCameraMute;
6301}
6302
6303status_t Camera3Device::setCameraMute(bool enabled) {
6304 ATRACE_CALL();
6305 Mutex::Autolock il(mInterfaceLock);
6306 Mutex::Autolock l(mLock);
6307
6308 if (mRequestThread == nullptr || !mSupportCameraMute) {
6309 return INVALID_OPERATION;
6310 }
6311 return mRequestThread->setCameraMute(enabled);
6312}
6313
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08006314}; // namespace android