blob: 8925b1572c5bc92725f24a642e0ed9421a8033d1 [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
Cliff Wuc2ad9c82021-04-21 00:58:58 +080052#include <android/hardware/camera/device/3.7/ICameraInjectionSession.h>
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -080053#include <android/hardware/camera2/ICameraDeviceUser.h>
54
Igor Murashkinff3e31d2013-10-23 16:40:06 -070055#include "utils/CameraTraces.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070056#include "mediautils/SchedulingPolicyService.h"
Eino-Ville Talvala7b82efe2013-07-25 17:12:35 -070057#include "device3/Camera3Device.h"
58#include "device3/Camera3OutputStream.h"
59#include "device3/Camera3InputStream.h"
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -040060#include "device3/Camera3FakeStream.h"
Shuzhen Wang0129d522016-10-30 22:43:41 -070061#include "device3/Camera3SharedOutputStream.h"
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070062#include "CameraService.h"
Jayant Chowdhary12361932018-08-27 14:46:13 -070063#include "utils/CameraThreadState.h"
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080064#include "utils/SessionConfigurationUtils.h"
Jayant Chowdharyd4776262020-06-23 23:45:57 -070065#include "utils/TraceHFR.h"
Shuzhen Wang316781a2020-08-18 18:11:01 -070066#include "utils/CameraServiceProxyWrapper.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080067
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080068#include <algorithm>
Yin-Chia Yeh84be5782019-03-01 11:47:02 -080069#include <tuple>
70
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080071using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080072using namespace android::hardware::camera;
73using namespace android::hardware::camera::device::V3_2;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080074using android::hardware::camera::metadata::V3_6::CameraMetadataEnumAndroidSensorPixelMode;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080075
76namespace android {
77
Shuzhen Wangd4abdf72021-05-28 11:22:50 -070078Camera3Device::Camera3Device(const String8 &id, bool overrideForPerfClass):
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080079 mId(id),
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080080 mOperatingMode(NO_MODE),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070081 mIsConstrainedHighSpeedConfiguration(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070082 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070083 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070084 mUsePartialResult(false),
85 mNumPartialResults(1),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080086 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070087 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070088 mNextReprocessResultFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070089 mNextZslStillResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070090 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070091 mNextReprocessShutterFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070092 mNextZslStillShutterFrameNumber(0),
Emilian Peev71c73a22017-03-21 16:35:51 +000093 mListener(NULL),
Emilian Peev811d2952018-05-25 11:08:40 +010094 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID),
Shuzhen Wang268a1362018-10-16 16:32:59 -070095 mLastTemplateId(-1),
Shuzhen Wangd4abdf72021-05-28 11:22:50 -070096 mNeedFixupMonochromeTags(false),
97 mOverrideForPerfClass(overrideForPerfClass)
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080098{
99 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800100 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800101}
102
103Camera3Device::~Camera3Device()
104{
105 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800106 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700107 disconnectImpl();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800108}
109
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800110const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -0800111 return mId;
112}
113
Emilian Peevbd8c5032018-02-14 23:05:40 +0000114status_t Camera3Device::initialize(sp<CameraProviderManager> manager, const String8& monitorTags) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800115 ATRACE_CALL();
116 Mutex::Autolock il(mInterfaceLock);
117 Mutex::Autolock l(mLock);
118
119 ALOGV("%s: Initializing HIDL device for camera %s", __FUNCTION__, mId.string());
120 if (mStatus != STATUS_UNINITIALIZED) {
121 CLOGE("Already initialized!");
122 return INVALID_OPERATION;
123 }
124 if (manager == nullptr) return INVALID_OPERATION;
125
126 sp<ICameraDeviceSession> session;
127 ATRACE_BEGIN("CameraHal::openSession");
Steven Moreland5ff9c912017-03-09 23:13:00 -0800128 status_t res = manager->openSession(mId.string(), this,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800129 /*out*/ &session);
130 ATRACE_END();
131 if (res != OK) {
132 SET_ERR_L("Could not open camera session: %s (%d)", strerror(-res), res);
133 return res;
134 }
135
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700136 res = manager->getCameraCharacteristics(mId.string(), mOverrideForPerfClass, &mDeviceInfo);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800137 if (res != OK) {
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700138 SET_ERR_L("Could not retrieve camera characteristics: %s (%d)", strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800139 session->close();
140 return res;
141 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800142 mSupportNativeZoomRatio = manager->supportNativeZoomRatio(mId.string());
Yin-Chia Yeh52778d42016-12-22 18:20:43 -0800143
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700144 std::vector<std::string> physicalCameraIds;
Shuzhen Wang03d8cc12018-09-12 14:17:09 -0700145 bool isLogical = manager->isLogicalCamera(mId.string(), &physicalCameraIds);
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700146 if (isLogical) {
147 for (auto& physicalId : physicalCameraIds) {
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700148 // Do not override characteristics for physical cameras
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700149 res = manager->getCameraCharacteristics(
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700150 physicalId, /*overrideForPerfClass*/false, &mPhysicalDeviceInfoMap[physicalId]);
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700151 if (res != OK) {
152 SET_ERR_L("Could not retrieve camera %s characteristics: %s (%d)",
153 physicalId.c_str(), strerror(-res), res);
154 session->close();
155 return res;
156 }
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700157
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800158 bool usePrecorrectArray =
159 DistortionMapper::isDistortionSupported(mPhysicalDeviceInfoMap[physicalId]);
160 if (usePrecorrectArray) {
161 res = mDistortionMappers[physicalId].setupStaticInfo(
162 mPhysicalDeviceInfoMap[physicalId]);
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700163 if (res != OK) {
164 SET_ERR_L("Unable to read camera %s's calibration fields for distortion "
165 "correction", physicalId.c_str());
166 session->close();
167 return res;
168 }
169 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800170
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800171 mZoomRatioMappers[physicalId] = ZoomRatioMapper(
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800172 &mPhysicalDeviceInfoMap[physicalId],
173 mSupportNativeZoomRatio, usePrecorrectArray);
Jayant Chowdhary9255ce02021-07-15 11:18:17 -0700174
175 if (SessionConfigurationUtils::isUltraHighResolutionSensor(
176 mPhysicalDeviceInfoMap[physicalId])) {
177 mUHRCropAndMeteringRegionMappers[physicalId] =
178 UHRCropAndMeteringRegionMapper(mPhysicalDeviceInfoMap[physicalId],
179 usePrecorrectArray);
180 }
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700181 }
182 }
183
Yifan Hongf79b5542017-04-11 14:44:25 -0700184 std::shared_ptr<RequestMetadataQueue> queue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700185 auto requestQueueRet = session->getCaptureRequestMetadataQueue(
186 [&queue](const auto& descriptor) {
187 queue = std::make_shared<RequestMetadataQueue>(descriptor);
188 if (!queue->isValid() || queue->availableToWrite() <= 0) {
189 ALOGE("HAL returns empty request metadata fmq, not use it");
190 queue = nullptr;
191 // don't use the queue onwards.
192 }
193 });
194 if (!requestQueueRet.isOk()) {
195 ALOGE("Transaction error when getting request metadata fmq: %s, not use it",
196 requestQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700197 return DEAD_OBJECT;
Yifan Hongf79b5542017-04-11 14:44:25 -0700198 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700199
200 std::unique_ptr<ResultMetadataQueue>& resQueue = mResultMetadataQueue;
Yifan Honga640c5a2017-04-12 16:30:31 -0700201 auto resultQueueRet = session->getCaptureResultMetadataQueue(
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700202 [&resQueue](const auto& descriptor) {
203 resQueue = std::make_unique<ResultMetadataQueue>(descriptor);
204 if (!resQueue->isValid() || resQueue->availableToWrite() <= 0) {
Yifan Honga640c5a2017-04-12 16:30:31 -0700205 ALOGE("HAL returns empty result metadata fmq, not use it");
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700206 resQueue = nullptr;
207 // Don't use the resQueue onwards.
Yifan Honga640c5a2017-04-12 16:30:31 -0700208 }
209 });
210 if (!resultQueueRet.isOk()) {
211 ALOGE("Transaction error when getting result metadata queue from camera session: %s",
212 resultQueueRet.description().c_str());
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -0700213 return DEAD_OBJECT;
Yifan Honga640c5a2017-04-12 16:30:31 -0700214 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700215 IF_ALOGV() {
216 session->interfaceChain([](
217 ::android::hardware::hidl_vec<::android::hardware::hidl_string> interfaceChain) {
218 ALOGV("Session interface chain:");
Chih-Hung Hsieh3ef324d2018-12-11 11:48:12 -0800219 for (const auto& iface : interfaceChain) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700220 ALOGV(" %s", iface.c_str());
221 }
222 });
223 }
Yifan Hongf79b5542017-04-11 14:44:25 -0700224
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -0800225 camera_metadata_entry bufMgrMode =
226 mDeviceInfo.find(ANDROID_INFO_SUPPORTED_BUFFER_MANAGEMENT_VERSION);
227 if (bufMgrMode.count > 0) {
228 mUseHalBufManager = (bufMgrMode.data.u8[0] ==
229 ANDROID_INFO_SUPPORTED_BUFFER_MANAGEMENT_VERSION_HIDL_DEVICE_3_5);
230 }
231
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700232 camera_metadata_entry_t capabilities = mDeviceInfo.find(ANDROID_REQUEST_AVAILABLE_CAPABILITIES);
233 for (size_t i = 0; i < capabilities.count; i++) {
234 uint8_t capability = capabilities.data.u8[i];
235 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_OFFLINE_PROCESSING) {
236 mSupportOfflineProcessing = true;
237 }
238 }
239
240 mInterface = new HalInterface(session, queue, mUseHalBufManager, mSupportOfflineProcessing);
Emilian Peev71c73a22017-03-21 16:35:51 +0000241 std::string providerType;
242 mVendorTagId = manager->getProviderTagIdLocked(mId.string());
Emilian Peevbd8c5032018-02-14 23:05:40 +0000243 mTagMonitor.initialize(mVendorTagId);
244 if (!monitorTags.isEmpty()) {
245 mTagMonitor.parseTagsToMonitor(String8(monitorTags));
246 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800247
Shuzhen Wang268a1362018-10-16 16:32:59 -0700248 // Metadata tags needs fixup for monochrome camera device version less
249 // than 3.5.
250 hardware::hidl_version maxVersion{0,0};
251 res = manager->getHighestSupportedVersion(mId.string(), &maxVersion);
252 if (res != OK) {
253 ALOGE("%s: Error in getting camera device version id: %s (%d)",
254 __FUNCTION__, strerror(-res), res);
255 return res;
256 }
257 int deviceVersion = HARDWARE_DEVICE_API_VERSION(
258 maxVersion.get_major(), maxVersion.get_minor());
259
260 bool isMonochrome = false;
Yin-Chia Yehb978c382019-10-30 00:22:37 -0700261 for (size_t i = 0; i < capabilities.count; i++) {
262 uint8_t capability = capabilities.data.u8[i];
Shuzhen Wang268a1362018-10-16 16:32:59 -0700263 if (capability == ANDROID_REQUEST_AVAILABLE_CAPABILITIES_MONOCHROME) {
264 isMonochrome = true;
265 }
266 }
267 mNeedFixupMonochromeTags = (isMonochrome && deviceVersion < CAMERA_DEVICE_API_VERSION_3_5);
268
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800269 return initializeCommonLocked();
270}
271
272status_t Camera3Device::initializeCommonLocked() {
273
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700274 /** Start up status tracker thread */
275 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800276 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700277 if (res != OK) {
278 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
279 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800280 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700281 mStatusTracker.clear();
282 return res;
283 }
284
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700285 /** Register in-flight map to the status tracker */
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -0700286 mInFlightStatusId = mStatusTracker->addComponent("InflightRequests");
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700287
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -0700288 if (mUseHalBufManager) {
289 res = mRequestBufferSM.initialize(mStatusTracker);
290 if (res != OK) {
291 SET_ERR_L("Unable to start request buffer state machine: %s (%d)",
292 strerror(-res), res);
293 mInterface->close();
294 mStatusTracker.clear();
295 return res;
296 }
297 }
298
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -0800299 /** Create buffer manager */
300 mBufferManager = new Camera3BufferManager();
301
302 Vector<int32_t> sessionParamKeys;
303 camera_metadata_entry_t sessionKeysEntry = mDeviceInfo.find(
304 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
305 if (sessionKeysEntry.count > 0) {
306 sessionParamKeys.insertArrayAt(sessionKeysEntry.data.i32, 0, sessionKeysEntry.count);
307 }
308
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -0700309 camera_metadata_entry_t availableTestPatternModes = mDeviceInfo.find(
310 ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES);
311 for (size_t i = 0; i < availableTestPatternModes.count; i++) {
312 if (availableTestPatternModes.data.i32[i] ==
313 ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR) {
314 mSupportCameraMute = true;
315 mSupportTestPatternSolidColor = true;
316 break;
317 } else if (availableTestPatternModes.data.i32[i] ==
318 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK) {
319 mSupportCameraMute = true;
320 mSupportTestPatternSolidColor = false;
321 }
322 }
323
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700324 /** Start up request queue thread */
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -0700325 mRequestThread = new RequestThread(
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -0700326 this, mStatusTracker, mInterface, sessionParamKeys,
327 mUseHalBufManager, mSupportCameraMute);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800328 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800329 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700330 SET_ERR_L("Unable to start request queue thread: %s (%d)",
331 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800332 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800333 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800334 return res;
335 }
336
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700337 mPreparerThread = new PreparerThread();
338
Ruben Brunk183f0562015-08-12 12:55:02 -0700339 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800340 mNextStreamId = 0;
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -0400341 mFakeStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700342 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700343 mPauseStateNotify = false;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800344 mIsInputStreamMultiResolution = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800345
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800346 // Measure the clock domain offset between camera and video/hw_composer
347 camera_metadata_entry timestampSource =
348 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
349 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
350 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
351 mTimestampOffset = getMonoToBoottimeOffset();
352 }
353
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700354 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100355 camera_metadata_entry partialResultsCount =
356 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
357 if (partialResultsCount.count > 0) {
358 mNumPartialResults = partialResultsCount.data.i32[0];
359 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700360 }
361
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800362 bool usePrecorrectArray = DistortionMapper::isDistortionSupported(mDeviceInfo);
363 if (usePrecorrectArray) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700364 res = mDistortionMappers[mId.c_str()].setupStaticInfo(mDeviceInfo);
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -0700365 if (res != OK) {
366 SET_ERR_L("Unable to read necessary calibration fields for distortion correction");
367 return res;
368 }
369 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800370
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800371 mZoomRatioMappers[mId.c_str()] = ZoomRatioMapper(&mDeviceInfo,
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800372 mSupportNativeZoomRatio, usePrecorrectArray);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800373
Jayant Chowdhary9255ce02021-07-15 11:18:17 -0700374 if (SessionConfigurationUtils::isUltraHighResolutionSensor(mDeviceInfo)) {
375 mUHRCropAndMeteringRegionMappers[mId.c_str()] =
376 UHRCropAndMeteringRegionMapper(mDeviceInfo, usePrecorrectArray);
377 }
378
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800379 if (RotateAndCropMapper::isNeeded(&mDeviceInfo)) {
380 mRotateAndCropMappers.emplace(mId.c_str(), &mDeviceInfo);
381 }
382
Cliff Wuc2ad9c82021-04-21 00:58:58 +0800383 mInjectionMethods = new Camera3DeviceInjectionMethods(this);
384
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800385 return OK;
386}
387
388status_t Camera3Device::disconnect() {
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700389 return disconnectImpl();
390}
391
392status_t Camera3Device::disconnectImpl() {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800393 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700394 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800395
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700396 status_t res = OK;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700397 std::vector<wp<Camera3StreamInterface>> streams;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700398 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800399 Mutex::Autolock il(mInterfaceLock);
400 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
401 {
402 Mutex::Autolock l(mLock);
403 if (mStatus == STATUS_UNINITIALIZED) return res;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700404
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800405 if (mStatus == STATUS_ACTIVE ||
406 (mStatus == STATUS_ERROR && mRequestThread != NULL)) {
407 res = mRequestThread->clearRepeatingRequests();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700408 if (res != OK) {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800409 SET_ERR_L("Can't stop streaming");
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700410 // Continue to close device even in case of error
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800411 } else {
412 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
413 if (res != OK) {
414 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
415 maxExpectedDuration);
416 // Continue to close device even in case of error
417 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700418 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700419 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800420
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800421 if (mStatus == STATUS_ERROR) {
422 CLOGE("Shutting down in an error state");
423 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700424
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800425 if (mStatusTracker != NULL) {
426 mStatusTracker->requestExit();
427 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700428
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800429 if (mRequestThread != NULL) {
430 mRequestThread->requestExit();
431 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700432
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800433 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
434 for (size_t i = 0; i < mOutputStreams.size(); i++) {
435 streams.push_back(mOutputStreams[i]);
436 }
437 if (mInputStream != nullptr) {
438 streams.push_back(mInputStream);
439 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700440 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700441 }
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800442 // Joining done without holding mLock and mInterfaceLock, otherwise deadlocks may ensue
443 // as the threads try to access parent state (b/143513518)
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700444 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
445 // HAL may be in a bad state, so waiting for request thread
446 // (which may be stuck in the HAL processCaptureRequest call)
447 // could be dangerous.
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800448 // give up mInterfaceLock here and then lock it again. Could this lead
449 // to other deadlocks
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700450 mRequestThread->join();
451 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700452 {
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800453 Mutex::Autolock il(mInterfaceLock);
454 if (mStatusTracker != NULL) {
455 mStatusTracker->join();
456 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800457
Cliff Wuc2ad9c82021-04-21 00:58:58 +0800458 if (mInjectionMethods->isInjecting()) {
459 mInjectionMethods->stopInjection();
460 }
461
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800462 HalInterface* interface;
463 {
464 Mutex::Autolock l(mLock);
465 mRequestThread.clear();
466 Mutex::Autolock stLock(mTrackerLock);
467 mStatusTracker.clear();
468 interface = mInterface.get();
469 }
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700470
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800471 // Call close without internal mutex held, as the HAL close may need to
472 // wait on assorted callbacks,etc, to complete before it can return.
473 interface->close();
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700474
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800475 flushInflightRequests();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800476
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800477 {
478 Mutex::Autolock l(mLock);
479 mInterface->clear();
480 mOutputStreams.clear();
481 mInputStream.clear();
482 mDeletedStreams.clear();
483 mBufferManager.clear();
484 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
485 }
486
487 for (auto& weakStream : streams) {
488 sp<Camera3StreamInterface> stream = weakStream.promote();
489 if (stream != nullptr) {
490 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
491 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
492 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700493 }
494 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700495 ALOGI("%s: X", __FUNCTION__);
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700496 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800497}
498
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700499// For dumping/debugging only -
500// try to acquire a lock a few times, eventually give up to proceed with
501// debug/dump operations
502bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
503 bool gotLock = false;
504 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
505 if (lock.tryLock() == NO_ERROR) {
506 gotLock = true;
507 break;
508 } else {
509 usleep(kDumpSleepDuration);
510 }
511 }
512 return gotLock;
513}
514
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800515nsecs_t Camera3Device::getMonoToBoottimeOffset() {
516 // try three times to get the clock offset, choose the one
517 // with the minimum gap in measurements.
518 const int tries = 3;
519 nsecs_t bestGap, measured;
520 for (int i = 0; i < tries; ++i) {
521 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
522 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
523 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
524 const nsecs_t gap = tmono2 - tmono;
525 if (i == 0 || gap < bestGap) {
526 bestGap = gap;
527 measured = tbase - ((tmono + tmono2) >> 1);
528 }
529 }
530 return measured;
531}
532
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800533hardware::graphics::common::V1_0::PixelFormat Camera3Device::mapToPixelFormat(
534 int frameworkFormat) {
535 return (hardware::graphics::common::V1_0::PixelFormat) frameworkFormat;
536}
537
538DataspaceFlags Camera3Device::mapToHidlDataspace(
539 android_dataspace dataSpace) {
540 return dataSpace;
541}
542
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700543BufferUsageFlags Camera3Device::mapToConsumerUsage(
Emilian Peev050f5dc2017-05-18 14:43:56 +0100544 uint64_t usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700545 return usage;
546}
547
Emilian Peevf4816702020-04-03 15:44:51 -0700548StreamRotation Camera3Device::mapToStreamRotation(camera_stream_rotation_t rotation) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800549 switch (rotation) {
Emilian Peevf4816702020-04-03 15:44:51 -0700550 case CAMERA_STREAM_ROTATION_0:
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800551 return StreamRotation::ROTATION_0;
Emilian Peevf4816702020-04-03 15:44:51 -0700552 case CAMERA_STREAM_ROTATION_90:
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800553 return StreamRotation::ROTATION_90;
Emilian Peevf4816702020-04-03 15:44:51 -0700554 case CAMERA_STREAM_ROTATION_180:
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800555 return StreamRotation::ROTATION_180;
Emilian Peevf4816702020-04-03 15:44:51 -0700556 case CAMERA_STREAM_ROTATION_270:
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800557 return StreamRotation::ROTATION_270;
558 }
559 ALOGE("%s: Unknown stream rotation %d", __FUNCTION__, rotation);
560 return StreamRotation::ROTATION_0;
561}
562
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800563status_t Camera3Device::mapToStreamConfigurationMode(
Emilian Peevf4816702020-04-03 15:44:51 -0700564 camera_stream_configuration_mode_t operationMode, StreamConfigurationMode *mode) {
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800565 if (mode == nullptr) return BAD_VALUE;
Emilian Peevf4816702020-04-03 15:44:51 -0700566 if (operationMode < CAMERA_VENDOR_STREAM_CONFIGURATION_MODE_START) {
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800567 switch(operationMode) {
Emilian Peevf4816702020-04-03 15:44:51 -0700568 case CAMERA_STREAM_CONFIGURATION_NORMAL_MODE:
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800569 *mode = StreamConfigurationMode::NORMAL_MODE;
570 break;
Emilian Peevf4816702020-04-03 15:44:51 -0700571 case CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE:
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800572 *mode = StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE;
573 break;
574 default:
575 ALOGE("%s: Unknown stream configuration mode %d", __FUNCTION__, operationMode);
576 return BAD_VALUE;
577 }
578 } else {
579 *mode = static_cast<StreamConfigurationMode>(operationMode);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800580 }
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800581 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800582}
583
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800584int Camera3Device::mapToFrameworkFormat(
585 hardware::graphics::common::V1_0::PixelFormat pixelFormat) {
586 return static_cast<uint32_t>(pixelFormat);
587}
588
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -0700589android_dataspace Camera3Device::mapToFrameworkDataspace(
590 DataspaceFlags dataSpace) {
591 return static_cast<android_dataspace>(dataSpace);
592}
593
Emilian Peev050f5dc2017-05-18 14:43:56 +0100594uint64_t Camera3Device::mapConsumerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700595 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700596 return usage;
597}
598
Emilian Peev050f5dc2017-05-18 14:43:56 +0100599uint64_t Camera3Device::mapProducerToFrameworkUsage(
Chia-I Wu67a0c0e2017-04-06 13:37:01 -0700600 BufferUsageFlags usage) {
Yin-Chia Yeh47cf8e62017-04-04 13:00:03 -0700601 return usage;
602}
603
Zhijun Hef7da0962014-04-24 13:27:56 -0700604ssize_t Camera3Device::getJpegBufferSize(uint32_t width, uint32_t height) const {
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700605 // Get max jpeg size (area-wise) for default sensor pixel mode
606 camera3::Size maxDefaultJpegResolution =
607 SessionConfigurationUtils::getMaxJpegResolution(mDeviceInfo,
608 /*isUltraHighResolutionSensor*/false);
609 // Get max jpeg size (area-wise) for max resolution sensor pixel mode / 0 if
610 // not ultra high res sensor
611 camera3::Size uhrMaxJpegResolution =
612 SessionConfigurationUtils::getMaxJpegResolution(mDeviceInfo,
613 /*isUltraHighResolution*/true);
614 if (maxDefaultJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800615 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
616 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700617 return BAD_VALUE;
618 }
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700619 bool useMaxSensorPixelModeThreshold = false;
620 if (uhrMaxJpegResolution.width != 0 &&
621 width * height > maxDefaultJpegResolution.width * maxDefaultJpegResolution.height) {
622 // Use the ultra high res max jpeg size and max jpeg buffer size
623 useMaxSensorPixelModeThreshold = true;
624 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700625
Zhijun Hef7da0962014-04-24 13:27:56 -0700626 // Get max jpeg buffer size
627 ssize_t maxJpegBufferSize = 0;
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700628 camera_metadata_ro_entry jpegBufMaxSize = mDeviceInfo.find(ANDROID_JPEG_MAX_SIZE);
629 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800630 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
631 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700632 return BAD_VALUE;
633 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700634 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700635
636 camera3::Size chosenMaxJpegResolution = maxDefaultJpegResolution;
637 if (useMaxSensorPixelModeThreshold) {
638 maxJpegBufferSize =
639 SessionConfigurationUtils::getUHRMaxJpegBufferSize(uhrMaxJpegResolution,
640 maxDefaultJpegResolution, maxJpegBufferSize);
641 chosenMaxJpegResolution = uhrMaxJpegResolution;
642 }
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800643 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700644
645 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700646 float scaleFactor = ((float) (width * height)) /
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700647 (chosenMaxJpegResolution.width * chosenMaxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800648 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
649 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700650 if (jpegBufferSize > maxJpegBufferSize) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800651 ALOGI("%s: jpeg buffer size calculated is > maxJpeg bufferSize(%zd), clamping",
652 __FUNCTION__, maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700653 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700654 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700655 return jpegBufferSize;
656}
657
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700658ssize_t Camera3Device::getPointCloudBufferSize() const {
659 const int FLOATS_PER_POINT=4;
660 camera_metadata_ro_entry maxPointCount = mDeviceInfo.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
661 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800662 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
663 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700664 return BAD_VALUE;
665 }
666 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
667 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
668 return maxBytesForPointCloud;
669}
670
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800671ssize_t Camera3Device::getRawOpaqueBufferSize(int32_t width, int32_t height,
672 bool maxResolution) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800673 const int PER_CONFIGURATION_SIZE = 3;
674 const int WIDTH_OFFSET = 0;
675 const int HEIGHT_OFFSET = 1;
676 const int SIZE_OFFSET = 2;
677 camera_metadata_ro_entry rawOpaqueSizes =
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800678 mDeviceInfo.find(
679 camera3::SessionConfigurationUtils::getAppropriateModeTag(
680 ANDROID_SENSOR_OPAQUE_RAW_SIZE,
681 maxResolution));
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800682 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800683 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800684 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
685 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800686 return BAD_VALUE;
687 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700688
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800689 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
690 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
691 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
692 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
693 }
694 }
695
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800696 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
697 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800698 return BAD_VALUE;
699}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700700
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800701status_t Camera3Device::dump(int fd, const Vector<String16> &args) {
702 ATRACE_CALL();
703 (void)args;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700704
705 // Try to lock, but continue in case of failure (to avoid blocking in
706 // deadlocks)
707 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
708 bool gotLock = tryLockSpinRightRound(mLock);
709
710 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800711 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
712 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700713 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800714 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
715 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700716
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800717 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700718
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800719 String16 templatesOption("-t");
720 int n = args.size();
721 for (int i = 0; i < n; i++) {
722 if (args[i] == templatesOption) {
723 dumpTemplates = true;
724 }
Emilian Peevbd8c5032018-02-14 23:05:40 +0000725 if (args[i] == TagMonitor::kMonitorOption) {
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700726 if (i + 1 < n) {
727 String8 monitorTags = String8(args[i + 1]);
728 if (monitorTags == "off") {
729 mTagMonitor.disableMonitoring();
730 } else {
731 mTagMonitor.parseTagsToMonitor(monitorTags);
732 }
733 } else {
734 mTagMonitor.disableMonitoring();
735 }
736 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800737 }
738
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800739 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800740
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800741 const char *status =
742 mStatus == STATUS_ERROR ? "ERROR" :
743 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700744 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
745 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800746 mStatus == STATUS_ACTIVE ? "ACTIVE" :
747 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700748
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800749 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700750 if (mStatus == STATUS_ERROR) {
751 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
752 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800753 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800754 const char *mode =
755 mOperatingMode == static_cast<int>(StreamConfigurationMode::NORMAL_MODE) ? "NORMAL" :
756 mOperatingMode == static_cast<int>(
757 StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ? "CONSTRAINED_HIGH_SPEED" :
758 "CUSTOM";
759 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800760
761 if (mInputStream != NULL) {
762 write(fd, lines.string(), lines.size());
763 mInputStream->dump(fd, args);
764 } else {
765 lines.appendFormat(" No input stream.\n");
766 write(fd, lines.string(), lines.size());
767 }
768 for (size_t i = 0; i < mOutputStreams.size(); i++) {
769 mOutputStreams[i]->dump(fd,args);
770 }
771
Zhijun He431503c2016-03-07 17:30:16 -0800772 if (mBufferManager != NULL) {
773 lines = String8(" Camera3 Buffer Manager:\n");
774 write(fd, lines.string(), lines.size());
775 mBufferManager->dump(fd, args);
776 }
Zhijun He125684a2015-12-26 15:07:30 -0800777
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700778 lines = String8(" In-flight requests:\n");
Emilian Peeva6138c52021-06-24 12:52:38 -0700779 if (mInFlightLock.try_lock()) {
780 if (mInFlightMap.size() == 0) {
781 lines.append(" None\n");
782 } else {
783 for (size_t i = 0; i < mInFlightMap.size(); i++) {
784 InFlightRequest r = mInFlightMap.valueAt(i);
785 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
786 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
787 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
788 r.numBuffersLeft);
789 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700790 }
Emilian Peeva6138c52021-06-24 12:52:38 -0700791 mInFlightLock.unlock();
792 } else {
793 lines.append(" Failed to acquire In-flight lock!\n");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700794 }
795 write(fd, lines.string(), lines.size());
796
Shuzhen Wang686f6442017-06-20 16:16:04 -0700797 if (mRequestThread != NULL) {
798 mRequestThread->dumpCaptureRequestLatency(fd,
799 " ProcessCaptureRequest latency histogram:");
800 }
801
Igor Murashkin1e479c02013-09-06 16:55:14 -0700802 {
803 lines = String8(" Last request sent:\n");
804 write(fd, lines.string(), lines.size());
805
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700806 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700807 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
808 }
809
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800810 if (dumpTemplates) {
Emilian Peevf4816702020-04-03 15:44:51 -0700811 const char *templateNames[CAMERA_TEMPLATE_COUNT] = {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800812 "TEMPLATE_PREVIEW",
813 "TEMPLATE_STILL_CAPTURE",
814 "TEMPLATE_VIDEO_RECORD",
815 "TEMPLATE_VIDEO_SNAPSHOT",
816 "TEMPLATE_ZERO_SHUTTER_LAG",
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800817 "TEMPLATE_MANUAL",
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800818 };
819
Emilian Peevf4816702020-04-03 15:44:51 -0700820 for (int i = 1; i < CAMERA_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800821 camera_metadata_t *templateRequest = nullptr;
822 mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -0700823 (camera_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800824 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800825 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800826 lines.append(" Not supported\n");
827 write(fd, lines.string(), lines.size());
828 } else {
829 write(fd, lines.string(), lines.size());
830 dump_indented_camera_metadata(templateRequest,
831 fd, /*verbosity*/2, /*indentation*/8);
832 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800833 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800834 }
835 }
836
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700837 mTagMonitor.dumpMonitoredMetadata(fd);
838
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800839 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800840 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800841 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800842 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800843 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800844
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700845 if (gotLock) mLock.unlock();
846 if (gotInterfaceLock) mInterfaceLock.unlock();
847
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800848 return OK;
849}
850
Avichal Rakesh7e53cad2021-10-05 13:46:30 -0700851status_t Camera3Device::startWatchingTags(const String8 &tags) {
852 mTagMonitor.parseTagsToMonitor(tags);
853 return OK;
854}
855
856status_t Camera3Device::stopWatchingTags() {
857 mTagMonitor.disableMonitoring();
858 return OK;
859}
860
861status_t Camera3Device::dumpWatchedEventsToVector(std::vector<std::string> &out) {
862 mTagMonitor.getLatestMonitoredTagEvents(out);
863 return OK;
864}
865
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800866const CameraMetadata& Camera3Device::infoPhysical(const String8& physicalId) const {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800867 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800868 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
869 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700870 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800871 mStatus == STATUS_ERROR ?
872 "when in error state" : "before init");
873 }
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700874 if (physicalId.isEmpty()) {
875 return mDeviceInfo;
876 } else {
877 std::string id(physicalId.c_str());
878 if (mPhysicalDeviceInfoMap.find(id) != mPhysicalDeviceInfoMap.end()) {
879 return mPhysicalDeviceInfoMap.at(id);
880 } else {
881 ALOGE("%s: Invalid physical camera id %s", __FUNCTION__, physicalId.c_str());
882 return mDeviceInfo;
883 }
884 }
885}
886
887const CameraMetadata& Camera3Device::info() const {
888 String8 emptyId;
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800889 return infoPhysical(emptyId);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800890}
891
Jianing Wei90e59c92014-03-12 18:29:36 -0700892status_t Camera3Device::checkStatusOkToCaptureLocked() {
893 switch (mStatus) {
894 case STATUS_ERROR:
895 CLOGE("Device has encountered a serious error");
896 return INVALID_OPERATION;
897 case STATUS_UNINITIALIZED:
898 CLOGE("Device not initialized");
899 return INVALID_OPERATION;
900 case STATUS_UNCONFIGURED:
901 case STATUS_CONFIGURED:
902 case STATUS_ACTIVE:
903 // OK
904 break;
905 default:
906 SET_ERR_L("Unexpected status: %d", mStatus);
907 return INVALID_OPERATION;
908 }
909 return OK;
910}
911
912status_t Camera3Device::convertMetadataListToRequestListLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000913 const List<const PhysicalCameraSettingsList> &metadataList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700914 const std::list<const SurfaceMap> &surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700915 bool repeating, nsecs_t requestTimeNs,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700916 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700917 if (requestList == NULL) {
918 CLOGE("requestList cannot be NULL.");
919 return BAD_VALUE;
920 }
921
Jianing Weicb0652e2014-03-12 18:29:36 -0700922 int32_t burstId = 0;
Emilian Peevaebbe412018-01-15 13:53:24 +0000923 List<const PhysicalCameraSettingsList>::const_iterator metadataIt = metadataList.begin();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700924 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
925 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
926 ++metadataIt, ++surfaceMapIt) {
927 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700928 if (newRequest == 0) {
929 CLOGE("Can't create capture request");
930 return BAD_VALUE;
931 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700932
Shuzhen Wang9d066012016-09-30 11:30:20 -0700933 newRequest->mRepeating = repeating;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700934 newRequest->mRequestTimeNs = requestTimeNs;
Shuzhen Wang9d066012016-09-30 11:30:20 -0700935
Jianing Weicb0652e2014-03-12 18:29:36 -0700936 // Setup burst Id and request Id
937 newRequest->mResultExtras.burstId = burstId++;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800938 auto requestIdEntry = metadataIt->begin()->metadata.find(ANDROID_REQUEST_ID);
939 if (requestIdEntry.count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700940 CLOGE("RequestID does not exist in metadata");
941 return BAD_VALUE;
942 }
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800943 newRequest->mResultExtras.requestId = requestIdEntry.data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700944
Jianing Wei90e59c92014-03-12 18:29:36 -0700945 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700946
947 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700948 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700949 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
950 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
951 return BAD_VALUE;
952 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700953
954 // Setup batch size if this is a high speed video recording request.
955 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
956 auto firstRequest = requestList->begin();
957 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
958 if (outputStream->isVideoStream()) {
959 (*firstRequest)->mBatchSize = requestList->size();
Yin-Chia Yeh14ef48d2020-02-10 15:06:37 -0800960 outputStream->setBatchSize(requestList->size());
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700961 break;
962 }
963 }
964 }
965
Jianing Wei90e59c92014-03-12 18:29:36 -0700966 return OK;
967}
968
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800969status_t Camera3Device::capture(CameraMetadata &request, int64_t* lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800970 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800971
Emilian Peevaebbe412018-01-15 13:53:24 +0000972 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700973 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000974 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700975
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800976 return captureList(requestsList, surfaceMaps, lastFrameNumber);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700977}
978
Emilian Peevaebbe412018-01-15 13:53:24 +0000979void Camera3Device::convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700980 std::list<const SurfaceMap>& surfaceMaps,
981 const CameraMetadata& request) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000982 PhysicalCameraSettingsList requestList;
983 requestList.push_back({std::string(getId().string()), request});
984 requestsList.push_back(requestList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700985
986 SurfaceMap surfaceMap;
987 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
988 // With no surface list passed in, stream and surface will have 1-to-1
989 // mapping. So the surface index is 0 for each stream in the surfaceMap.
990 for (size_t i = 0; i < streams.count; i++) {
991 surfaceMap[streams.data.i32[i]].push_back(0);
992 }
993 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800994}
995
Jianing Wei90e59c92014-03-12 18:29:36 -0700996status_t Camera3Device::submitRequestsHelper(
Emilian Peevaebbe412018-01-15 13:53:24 +0000997 const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700998 const std::list<const SurfaceMap> &surfaceMaps,
999 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001000 /*out*/
1001 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001002 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001003 nsecs_t requestTimeNs = systemTime();
1004
Jianing Wei90e59c92014-03-12 18:29:36 -07001005 Mutex::Autolock il(mInterfaceLock);
1006 Mutex::Autolock l(mLock);
1007
1008 status_t res = checkStatusOkToCaptureLocked();
1009 if (res != OK) {
1010 // error logged by previous call
1011 return res;
1012 }
1013
1014 RequestList requestList;
1015
Shuzhen Wang0129d522016-10-30 22:43:41 -07001016 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -07001017 repeating, requestTimeNs, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -07001018 if (res != OK) {
1019 // error logged by previous call
1020 return res;
1021 }
1022
1023 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001024 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001025 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001026 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001027 }
1028
1029 if (res == OK) {
1030 waitUntilStateThenRelock(/*active*/true, kActiveTimeout);
1031 if (res != OK) {
1032 SET_ERR_L("Can't transition to active in %f seconds!",
1033 kActiveTimeout/1e9);
1034 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001035 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001036 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -07001037 } else {
1038 CLOGE("Cannot queue request. Impossible.");
1039 return BAD_VALUE;
1040 }
1041
1042 return res;
1043}
1044
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001045hardware::Return<void> Camera3Device::requestStreamBuffers(
1046 const hardware::hidl_vec<hardware::camera::device::V3_5::BufferRequest>& bufReqs,
1047 requestStreamBuffers_cb _hidl_cb) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001048 RequestBufferStates states {
Shuzhen Wang316781a2020-08-18 18:11:01 -07001049 mId, mRequestBufferInterfaceLock, mUseHalBufManager, mOutputStreams, mSessionStatsBuilder,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001050 *this, *mInterface, *this};
1051 camera3::requestStreamBuffers(states, bufReqs, _hidl_cb);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001052 return hardware::Void();
1053}
1054
1055hardware::Return<void> Camera3Device::returnStreamBuffers(
1056 const hardware::hidl_vec<hardware::camera::device::V3_2::StreamBuffer>& buffers) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001057 ReturnBufferStates states {
Shuzhen Wang316781a2020-08-18 18:11:01 -07001058 mId, mUseHalBufManager, mOutputStreams, mSessionStatsBuilder, *mInterface};
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001059 camera3::returnStreamBuffers(states, buffers);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07001060 return hardware::Void();
1061}
1062
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001063hardware::Return<void> Camera3Device::processCaptureResult_3_4(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001064 const hardware::hidl_vec<
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001065 hardware::camera::device::V3_4::CaptureResult>& results) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001066 // Ideally we should grab mLock, but that can lead to deadlock, and
1067 // it's not super important to get up to date value of mStatus for this
1068 // warning print, hence skipping the lock here
1069 if (mStatus == STATUS_ERROR) {
1070 // Per API contract, HAL should act as closed after device error
1071 // But mStatus can be set to error by framework as well, so just log
1072 // a warning here.
1073 ALOGW("%s: received capture result in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07001074 }
Yifan Honga640c5a2017-04-12 16:30:31 -07001075
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001076 sp<NotificationListener> listener;
1077 {
1078 std::lock_guard<std::mutex> l(mOutputLock);
1079 listener = mListener.promote();
1080 }
1081
Yifan Honga640c5a2017-04-12 16:30:31 -07001082 if (mProcessCaptureResultLock.tryLock() != OK) {
1083 // This should never happen; it indicates a wrong client implementation
1084 // that doesn't follow the contract. But, we can be tolerant here.
1085 ALOGE("%s: callback overlapped! waiting 1s...",
1086 __FUNCTION__);
1087 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
1088 ALOGE("%s: cannot acquire lock in 1s, dropping results",
1089 __FUNCTION__);
1090 // really don't know what to do, so bail out.
1091 return hardware::Void();
1092 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001093 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001094 CaptureOutputStates states {
1095 mId,
Shuzhen Wangb7b42652020-05-07 11:59:02 -07001096 mInFlightLock, mLastCompletedRegularFrameNumber,
1097 mLastCompletedReprocessFrameNumber, mLastCompletedZslFrameNumber,
1098 mInFlightMap, mOutputLock, mResultQueue, mResultSignal,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001099 mNextShutterFrameNumber,
1100 mNextReprocessShutterFrameNumber, mNextZslStillShutterFrameNumber,
1101 mNextResultFrameNumber,
1102 mNextReprocessResultFrameNumber, mNextZslStillResultFrameNumber,
1103 mUseHalBufManager, mUsePartialResult, mNeedFixupMonochromeTags,
1104 mNumPartialResults, mVendorTagId, mDeviceInfo, mPhysicalDeviceInfoMap,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001105 mResultMetadataQueue, mDistortionMappers, mZoomRatioMappers, mRotateAndCropMappers,
Shuzhen Wang316781a2020-08-18 18:11:01 -07001106 mTagMonitor, mInputStream, mOutputStreams, mSessionStatsBuilder, listener, *this, *this,
1107 *mInterface
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001108 };
1109
Yifan Honga640c5a2017-04-12 16:30:31 -07001110 for (const auto& result : results) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001111 processOneCaptureResultLocked(states, result.v3_2, result.physicalCameraMetadata);
Yifan Honga640c5a2017-04-12 16:30:31 -07001112 }
1113 mProcessCaptureResultLock.unlock();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001114 return hardware::Void();
1115}
1116
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001117// Only one processCaptureResult should be called at a time, so
1118// the locks won't block. The locks are present here simply to enforce this.
1119hardware::Return<void> Camera3Device::processCaptureResult(
1120 const hardware::hidl_vec<
1121 hardware::camera::device::V3_2::CaptureResult>& results) {
1122 hardware::hidl_vec<hardware::camera::device::V3_4::PhysicalCameraMetadata> noPhysMetadata;
1123
1124 // Ideally we should grab mLock, but that can lead to deadlock, and
1125 // it's not super important to get up to date value of mStatus for this
1126 // warning print, hence skipping the lock here
1127 if (mStatus == STATUS_ERROR) {
1128 // Per API contract, HAL should act as closed after device error
1129 // But mStatus can be set to error by framework as well, so just log
1130 // a warning here.
1131 ALOGW("%s: received capture result in error state.", __FUNCTION__);
1132 }
1133
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001134 sp<NotificationListener> listener;
1135 {
1136 std::lock_guard<std::mutex> l(mOutputLock);
1137 listener = mListener.promote();
1138 }
1139
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001140 if (mProcessCaptureResultLock.tryLock() != OK) {
1141 // This should never happen; it indicates a wrong client implementation
1142 // that doesn't follow the contract. But, we can be tolerant here.
1143 ALOGE("%s: callback overlapped! waiting 1s...",
1144 __FUNCTION__);
1145 if (mProcessCaptureResultLock.timedLock(1000000000 /* 1s */) != OK) {
1146 ALOGE("%s: cannot acquire lock in 1s, dropping results",
1147 __FUNCTION__);
1148 // really don't know what to do, so bail out.
1149 return hardware::Void();
1150 }
1151 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001152
1153 CaptureOutputStates states {
1154 mId,
Shuzhen Wangb7b42652020-05-07 11:59:02 -07001155 mInFlightLock, mLastCompletedRegularFrameNumber,
1156 mLastCompletedReprocessFrameNumber, mLastCompletedZslFrameNumber,
1157 mInFlightMap, mOutputLock, mResultQueue, mResultSignal,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001158 mNextShutterFrameNumber,
1159 mNextReprocessShutterFrameNumber, mNextZslStillShutterFrameNumber,
1160 mNextResultFrameNumber,
1161 mNextReprocessResultFrameNumber, mNextZslStillResultFrameNumber,
1162 mUseHalBufManager, mUsePartialResult, mNeedFixupMonochromeTags,
1163 mNumPartialResults, mVendorTagId, mDeviceInfo, mPhysicalDeviceInfoMap,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001164 mResultMetadataQueue, mDistortionMappers, mZoomRatioMappers, mRotateAndCropMappers,
Shuzhen Wang316781a2020-08-18 18:11:01 -07001165 mTagMonitor, mInputStream, mOutputStreams, mSessionStatsBuilder, listener, *this, *this,
1166 *mInterface
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001167 };
1168
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001169 for (const auto& result : results) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001170 processOneCaptureResultLocked(states, result, noPhysMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001171 }
1172 mProcessCaptureResultLock.unlock();
1173 return hardware::Void();
1174}
1175
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001176hardware::Return<void> Camera3Device::notify(
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001177 const hardware::hidl_vec<hardware::camera::device::V3_2::NotifyMsg>& msgs) {
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001178 // Ideally we should grab mLock, but that can lead to deadlock, and
1179 // it's not super important to get up to date value of mStatus for this
1180 // warning print, hence skipping the lock here
1181 if (mStatus == STATUS_ERROR) {
1182 // Per API contract, HAL should act as closed after device error
1183 // But mStatus can be set to error by framework as well, so just log
1184 // a warning here.
1185 ALOGW("%s: received notify message in error state.", __FUNCTION__);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07001186 }
Yin-Chia Yeh657c1872017-07-18 18:09:57 -07001187
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001188 sp<NotificationListener> listener;
1189 {
1190 std::lock_guard<std::mutex> l(mOutputLock);
1191 listener = mListener.promote();
1192 }
1193
1194 CaptureOutputStates states {
1195 mId,
Shuzhen Wangb7b42652020-05-07 11:59:02 -07001196 mInFlightLock, mLastCompletedRegularFrameNumber,
1197 mLastCompletedReprocessFrameNumber, mLastCompletedZslFrameNumber,
1198 mInFlightMap, mOutputLock, mResultQueue, mResultSignal,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001199 mNextShutterFrameNumber,
1200 mNextReprocessShutterFrameNumber, mNextZslStillShutterFrameNumber,
1201 mNextResultFrameNumber,
1202 mNextReprocessResultFrameNumber, mNextZslStillResultFrameNumber,
1203 mUseHalBufManager, mUsePartialResult, mNeedFixupMonochromeTags,
1204 mNumPartialResults, mVendorTagId, mDeviceInfo, mPhysicalDeviceInfoMap,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08001205 mResultMetadataQueue, mDistortionMappers, mZoomRatioMappers, mRotateAndCropMappers,
Shuzhen Wang316781a2020-08-18 18:11:01 -07001206 mTagMonitor, mInputStream, mOutputStreams, mSessionStatsBuilder, listener, *this, *this,
1207 *mInterface
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001208 };
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001209 for (const auto& msg : msgs) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001210 camera3::notify(states, msg);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08001211 }
1212 return hardware::Void();
1213}
1214
Emilian Peevaebbe412018-01-15 13:53:24 +00001215status_t Camera3Device::captureList(const List<const PhysicalCameraSettingsList> &requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001216 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -07001217 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001218 ATRACE_CALL();
1219
Emilian Peevaebbe412018-01-15 13:53:24 +00001220 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001221}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001222
Jianing Weicb0652e2014-03-12 18:29:36 -07001223status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
1224 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001225 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001226
Emilian Peevaebbe412018-01-15 13:53:24 +00001227 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -07001228 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +00001229 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001230
Emilian Peevaebbe412018-01-15 13:53:24 +00001231 return setStreamingRequestList(requestsList, /*surfaceMap*/surfaceMaps,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001232 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001233}
1234
Emilian Peevaebbe412018-01-15 13:53:24 +00001235status_t Camera3Device::setStreamingRequestList(
1236 const List<const PhysicalCameraSettingsList> &requestsList,
1237 const std::list<const SurfaceMap> &surfaceMaps, int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -07001238 ATRACE_CALL();
1239
Emilian Peevaebbe412018-01-15 13:53:24 +00001240 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -07001241}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001242
1243sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +00001244 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001245 status_t res;
1246
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001247 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08001248 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
1249 // so unilaterally select normal operating mode.
Emilian Peevaebbe412018-01-15 13:53:24 +00001250 res = filterParamsAndConfigureLocked(request.begin()->metadata,
Emilian Peevf4816702020-04-03 15:44:51 -07001251 CAMERA_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001252 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001253 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001254 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001255 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07001256 } else if (mStatus == STATUS_UNCONFIGURED) {
1257 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001258 CLOGE("No streams configured");
1259 return NULL;
1260 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001261 }
1262
Shuzhen Wang0129d522016-10-30 22:43:41 -07001263 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001264 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001265}
1266
Jianing Weicb0652e2014-03-12 18:29:36 -07001267status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001268 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001269 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001270 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001271
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001272 switch (mStatus) {
1273 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001274 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001275 return INVALID_OPERATION;
1276 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001277 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001278 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001279 case STATUS_UNCONFIGURED:
1280 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001281 case STATUS_ACTIVE:
1282 // OK
1283 break;
1284 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001285 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001286 return INVALID_OPERATION;
1287 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001288 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -07001289
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07001290 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001291}
1292
1293status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
1294 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001295 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001296
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001297 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001298}
1299
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001300status_t Camera3Device::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -08001301 uint32_t width, uint32_t height, int format, bool isMultiResolution, int *id) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001302 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001303 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001304 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001305 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001306 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
1307 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001308
1309 status_t res;
1310 bool wasActive = false;
1311
1312 switch (mStatus) {
1313 case STATUS_ERROR:
1314 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
1315 return INVALID_OPERATION;
1316 case STATUS_UNINITIALIZED:
1317 ALOGE("%s: Device not initialized", __FUNCTION__);
1318 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001319 case STATUS_UNCONFIGURED:
1320 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001321 // OK
1322 break;
1323 case STATUS_ACTIVE:
1324 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001325 res = internalPauseAndWaitLocked(maxExpectedDuration);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001326 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001327 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001328 return res;
1329 }
1330 wasActive = true;
1331 break;
1332 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001333 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001334 return INVALID_OPERATION;
1335 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001336 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001337
1338 if (mInputStream != 0) {
1339 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
1340 return INVALID_OPERATION;
1341 }
1342
1343 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
1344 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001345 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001346
1347 mInputStream = newStream;
Shuzhen Wang83bff122020-11-20 15:51:39 -08001348 mIsInputStreamMultiResolution = isMultiResolution;
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001349
1350 *id = mNextStreamId++;
1351
1352 // Continue captures if active at start
1353 if (wasActive) {
1354 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001355 // Reuse current operating mode and session parameters for new stream config
1356 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001357 if (res != OK) {
1358 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1359 __FUNCTION__, mNextStreamId, strerror(-res), res);
1360 return res;
1361 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001362 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001363 }
1364
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001365 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001366 return OK;
1367}
1368
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001369status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001370 uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -07001371 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001372 const String8& physicalCameraId,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001373 const std::unordered_set<int32_t> &sensorPixelModesUsed,
1374 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
1375 uint64_t consumerUsage) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001376 ATRACE_CALL();
1377
1378 if (consumer == nullptr) {
1379 ALOGE("%s: consumer must not be null", __FUNCTION__);
1380 return BAD_VALUE;
1381 }
1382
1383 std::vector<sp<Surface>> consumers;
1384 consumers.push_back(consumer);
1385
1386 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001387 format, dataSpace, rotation, id, physicalCameraId, sensorPixelModesUsed, surfaceIds,
1388 streamSetId, isShared, isMultiResolution, consumerUsage);
1389}
1390
1391static bool isRawFormat(int format) {
1392 switch (format) {
1393 case HAL_PIXEL_FORMAT_RAW16:
1394 case HAL_PIXEL_FORMAT_RAW12:
1395 case HAL_PIXEL_FORMAT_RAW10:
1396 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
1397 return true;
1398 default:
1399 return false;
1400 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001401}
1402
1403status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1404 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -07001405 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001406 const String8& physicalCameraId, const std::unordered_set<int32_t> &sensorPixelModesUsed,
Shuzhen Wang83bff122020-11-20 15:51:39 -08001407 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
1408 uint64_t consumerUsage) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001409 ATRACE_CALL();
Emilian Peev40ead602017-09-26 15:46:36 +01001410
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001411 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001412 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001413 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001414 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wang83bff122020-11-20 15:51:39 -08001415 " consumer usage %" PRIu64 ", isShared %d, physicalCameraId %s, isMultiResolution %d",
1416 mId.string(), mNextStreamId, width, height, format, dataSpace, rotation,
1417 consumerUsage, isShared, physicalCameraId.string(), isMultiResolution);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001418
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001419 status_t res;
1420 bool wasActive = false;
1421
1422 switch (mStatus) {
1423 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001424 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001425 return INVALID_OPERATION;
1426 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001427 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001428 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001429 case STATUS_UNCONFIGURED:
1430 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001431 // OK
1432 break;
1433 case STATUS_ACTIVE:
1434 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001435 res = internalPauseAndWaitLocked(maxExpectedDuration);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001436 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001437 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001438 return res;
1439 }
1440 wasActive = true;
1441 break;
1442 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001443 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001444 return INVALID_OPERATION;
1445 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001446 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001447
1448 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001449
Shuzhen Wang0129d522016-10-30 22:43:41 -07001450 if (consumers.size() == 0 && !hasDeferredConsumer) {
1451 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1452 return BAD_VALUE;
1453 }
Zhijun He5d677d12016-05-29 16:52:39 -07001454
Shuzhen Wang0129d522016-10-30 22:43:41 -07001455 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001456 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1457 return BAD_VALUE;
1458 }
1459
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001460 if (isRawFormat(format) && sensorPixelModesUsed.size() > 1) {
1461 // We can't use one stream with a raw format in both sensor pixel modes since its going to
1462 // be found in only one sensor pixel mode.
1463 ALOGE("%s: RAW opaque stream cannot be used with > 1 sensor pixel modes", __FUNCTION__);
1464 return BAD_VALUE;
1465 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001466 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001467 ssize_t blobBufferSize;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001468 if (dataSpace == HAL_DATASPACE_DEPTH) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001469 blobBufferSize = getPointCloudBufferSize();
1470 if (blobBufferSize <= 0) {
1471 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1472 return BAD_VALUE;
1473 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001474 } else if (dataSpace == static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
1475 blobBufferSize = width * height;
1476 } else {
1477 blobBufferSize = getJpegBufferSize(width, height);
1478 if (blobBufferSize <= 0) {
1479 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1480 return BAD_VALUE;
1481 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001482 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001483 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001484 width, height, blobBufferSize, format, dataSpace, rotation,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001485 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, streamSetId,
1486 isMultiResolution);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001487 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001488 bool maxResolution =
1489 sensorPixelModesUsed.find(ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) !=
1490 sensorPixelModesUsed.end();
1491 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(width, height, maxResolution);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001492 if (rawOpaqueBufferSize <= 0) {
1493 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1494 return BAD_VALUE;
1495 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001496 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001497 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001498 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, streamSetId,
1499 isMultiResolution);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001500 } else if (isShared) {
1501 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1502 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001503 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, streamSetId,
1504 mUseHalBufManager);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001505 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001506 newStream = new Camera3OutputStream(mNextStreamId,
1507 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001508 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, streamSetId,
1509 isMultiResolution);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001510 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001511 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001512 width, height, format, dataSpace, rotation,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001513 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, streamSetId,
1514 isMultiResolution);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001515 }
Emilian Peev40ead602017-09-26 15:46:36 +01001516
1517 size_t consumerCount = consumers.size();
1518 for (size_t i = 0; i < consumerCount; i++) {
1519 int id = newStream->getSurfaceId(consumers[i]);
1520 if (id < 0) {
1521 SET_ERR_L("Invalid surface id");
1522 return BAD_VALUE;
1523 }
1524 if (surfaceIds != nullptr) {
1525 surfaceIds->push_back(id);
1526 }
1527 }
1528
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001529 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001530
Emilian Peev08dd2452017-04-06 16:55:14 +01001531 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001532
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -08001533 newStream->setImageDumpMask(mImageDumpMask);
1534
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001535 res = mOutputStreams.add(mNextStreamId, newStream);
1536 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001537 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001538 return res;
1539 }
1540
Shuzhen Wang316781a2020-08-18 18:11:01 -07001541 mSessionStatsBuilder.addStream(mNextStreamId);
1542
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001543 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001544 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001545
1546 // Continue captures if active at start
1547 if (wasActive) {
1548 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001549 // Reuse current operating mode and session parameters for new stream config
1550 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001551 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001552 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1553 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001554 return res;
1555 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001556 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001557 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001558 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001559 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001560}
1561
Emilian Peev710c1422017-08-30 11:19:38 +01001562status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001563 ATRACE_CALL();
Emilian Peev710c1422017-08-30 11:19:38 +01001564 if (nullptr == streamInfo) {
1565 return BAD_VALUE;
1566 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001567 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001568 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001569
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001570 switch (mStatus) {
1571 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001572 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001573 return INVALID_OPERATION;
1574 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001575 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001576 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001577 case STATUS_UNCONFIGURED:
1578 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001579 case STATUS_ACTIVE:
1580 // OK
1581 break;
1582 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001583 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001584 return INVALID_OPERATION;
1585 }
1586
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001587 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
1588 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001589 CLOGE("Stream %d is unknown", id);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001590 return BAD_VALUE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001591 }
1592
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001593 streamInfo->width = stream->getWidth();
1594 streamInfo->height = stream->getHeight();
1595 streamInfo->format = stream->getFormat();
1596 streamInfo->dataSpace = stream->getDataSpace();
1597 streamInfo->formatOverridden = stream->isFormatOverridden();
1598 streamInfo->originalFormat = stream->getOriginalFormat();
1599 streamInfo->dataSpaceOverridden = stream->isDataSpaceOverridden();
1600 streamInfo->originalDataSpace = stream->getOriginalDataSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001601 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001602}
1603
1604status_t Camera3Device::setStreamTransform(int id,
1605 int transform) {
1606 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001607 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001608 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001609
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001610 switch (mStatus) {
1611 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001612 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001613 return INVALID_OPERATION;
1614 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001615 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001616 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001617 case STATUS_UNCONFIGURED:
1618 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001619 case STATUS_ACTIVE:
1620 // OK
1621 break;
1622 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001623 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001624 return INVALID_OPERATION;
1625 }
1626
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001627 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
1628 if (stream == nullptr) {
1629 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001630 return BAD_VALUE;
1631 }
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001632 return stream->setTransform(transform);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001633}
1634
1635status_t Camera3Device::deleteStream(int id) {
1636 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001637 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001638 Mutex::Autolock l(mLock);
1639 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001640
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001641 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001642
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001643 // CameraDevice semantics require device to already be idle before
1644 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001645 if (mStatus == STATUS_ACTIVE) {
Yin-Chia Yeh693047d2018-03-08 12:14:19 -08001646 ALOGW("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001647 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001648 }
1649
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001650 if (mStatus == STATUS_ERROR) {
1651 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1652 __FUNCTION__, mId.string());
1653 return -EBUSY;
1654 }
1655
Igor Murashkin2fba5842013-04-22 14:03:54 -07001656 sp<Camera3StreamInterface> deletedStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001657 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001658 if (mInputStream != NULL && id == mInputStream->getId()) {
1659 deletedStream = mInputStream;
1660 mInputStream.clear();
1661 } else {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001662 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001663 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001664 return BAD_VALUE;
1665 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001666 mSessionStatsBuilder.removeStream(id);
Zhijun He5f446352014-01-22 09:49:33 -08001667 }
1668
1669 // Delete output stream or the output part of a bi-directional stream.
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001670 if (stream != nullptr) {
1671 deletedStream = stream;
1672 mOutputStreams.remove(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001673 }
1674
1675 // Free up the stream endpoint so that it can be used by some other stream
1676 res = deletedStream->disconnect();
1677 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001678 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001679 // fall through since we want to still list the stream as deleted.
1680 }
1681 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001682 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001683
1684 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001685}
1686
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001687status_t Camera3Device::configureStreams(const CameraMetadata& sessionParams, int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001688 ATRACE_CALL();
1689 ALOGV("%s: E", __FUNCTION__);
1690
1691 Mutex::Autolock il(mInterfaceLock);
1692 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001693
Emilian Peev811d2952018-05-25 11:08:40 +01001694 // In case the client doesn't include any session parameter, try a
1695 // speculative configuration using the values from the last cached
1696 // default request.
1697 if (sessionParams.isEmpty() &&
Emilian Peevf4816702020-04-03 15:44:51 -07001698 ((mLastTemplateId > 0) && (mLastTemplateId < CAMERA_TEMPLATE_COUNT)) &&
Emilian Peev811d2952018-05-25 11:08:40 +01001699 (!mRequestTemplateCache[mLastTemplateId].isEmpty())) {
1700 ALOGV("%s: Speculative session param configuration with template id: %d", __func__,
1701 mLastTemplateId);
1702 return filterParamsAndConfigureLocked(mRequestTemplateCache[mLastTemplateId],
1703 operatingMode);
1704 }
1705
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001706 return filterParamsAndConfigureLocked(sessionParams, operatingMode);
1707}
1708
1709status_t Camera3Device::filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
1710 int operatingMode) {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001711 //Filter out any incoming session parameters
1712 const CameraMetadata params(sessionParams);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001713 camera_metadata_entry_t availableSessionKeys = mDeviceInfo.find(
1714 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001715 CameraMetadata filteredParams(availableSessionKeys.count);
1716 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
1717 filteredParams.getAndLock());
1718 set_camera_metadata_vendor_id(meta, mVendorTagId);
1719 filteredParams.unlock(meta);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001720 if (availableSessionKeys.count > 0) {
1721 for (size_t i = 0; i < availableSessionKeys.count; i++) {
1722 camera_metadata_ro_entry entry = params.find(
1723 availableSessionKeys.data.i32[i]);
1724 if (entry.count > 0) {
1725 filteredParams.update(entry);
1726 }
1727 }
1728 }
1729
1730 return configureStreamsLocked(operatingMode, filteredParams);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001731}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001732
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001733status_t Camera3Device::getInputBufferProducer(
1734 sp<IGraphicBufferProducer> *producer) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001735 ATRACE_CALL();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001736 Mutex::Autolock il(mInterfaceLock);
1737 Mutex::Autolock l(mLock);
1738
1739 if (producer == NULL) {
1740 return BAD_VALUE;
1741 } else if (mInputStream == NULL) {
1742 return INVALID_OPERATION;
1743 }
1744
1745 return mInputStream->getInputBufferProducer(producer);
1746}
1747
Emilian Peevf4816702020-04-03 15:44:51 -07001748status_t Camera3Device::createDefaultRequest(camera_request_template_t templateId,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001749 CameraMetadata *request) {
1750 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001751 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001752
Emilian Peevf4816702020-04-03 15:44:51 -07001753 if (templateId <= 0 || templateId >= CAMERA_TEMPLATE_COUNT) {
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001754 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
Jayant Chowdhary12361932018-08-27 14:46:13 -07001755 CameraThreadState::getCallingUid(), nullptr, 0);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001756 return BAD_VALUE;
1757 }
1758
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001759 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001760
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001761 {
1762 Mutex::Autolock l(mLock);
1763 switch (mStatus) {
1764 case STATUS_ERROR:
1765 CLOGE("Device has encountered a serious error");
1766 return INVALID_OPERATION;
1767 case STATUS_UNINITIALIZED:
1768 CLOGE("Device is not initialized!");
1769 return INVALID_OPERATION;
1770 case STATUS_UNCONFIGURED:
1771 case STATUS_CONFIGURED:
1772 case STATUS_ACTIVE:
1773 // OK
1774 break;
1775 default:
1776 SET_ERR_L("Unexpected status: %d", mStatus);
1777 return INVALID_OPERATION;
1778 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001779
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001780 if (!mRequestTemplateCache[templateId].isEmpty()) {
1781 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001782 mLastTemplateId = templateId;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001783 return OK;
1784 }
Zhijun Hea1530f12014-09-14 12:44:20 -07001785 }
1786
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001787 camera_metadata_t *rawRequest;
1788 status_t res = mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -07001789 (camera_request_template_t) templateId, &rawRequest);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001790
1791 {
1792 Mutex::Autolock l(mLock);
1793 if (res == BAD_VALUE) {
1794 ALOGI("%s: template %d is not supported on this camera device",
1795 __FUNCTION__, templateId);
1796 return res;
1797 } else if (res != OK) {
1798 CLOGE("Unable to construct request template %d: %s (%d)",
1799 templateId, strerror(-res), res);
1800 return res;
1801 }
1802
1803 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
1804 mRequestTemplateCache[templateId].acquire(rawRequest);
1805
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08001806 // Override the template request with zoomRatioMapper
1807 res = mZoomRatioMappers[mId.c_str()].initZoomRatioInTemplate(
1808 &mRequestTemplateCache[templateId]);
1809 if (res != OK) {
1810 CLOGE("Failed to update zoom ratio for template %d: %s (%d)",
1811 templateId, strerror(-res), res);
1812 return res;
1813 }
1814
Shuzhen Wangd25dc972020-03-24 17:11:43 -07001815 // Fill in JPEG_QUALITY if not available
1816 if (!mRequestTemplateCache[templateId].exists(ANDROID_JPEG_QUALITY)) {
1817 static const uint8_t kDefaultJpegQuality = 95;
1818 mRequestTemplateCache[templateId].update(ANDROID_JPEG_QUALITY,
1819 &kDefaultJpegQuality, 1);
1820 }
1821
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001822 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001823 mLastTemplateId = templateId;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001824 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001825 return OK;
1826}
1827
1828status_t Camera3Device::waitUntilDrained() {
1829 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001830 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001831 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001832 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001833
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001834 return waitUntilDrainedLocked(maxExpectedDuration);
Zhijun He69a37482014-03-23 18:44:49 -07001835}
1836
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001837status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001838 switch (mStatus) {
1839 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001840 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001841 ALOGV("%s: Already idle", __FUNCTION__);
1842 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001843 case STATUS_CONFIGURED:
1844 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001845 case STATUS_ERROR:
1846 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001847 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001848 break;
1849 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001850 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001851 return INVALID_OPERATION;
1852 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001853 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
1854 maxExpectedDuration);
1855 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001856 if (res != OK) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07001857 mStatusTracker->dumpActiveComponents();
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001858 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1859 res);
1860 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001861 return res;
1862}
1863
Ruben Brunk183f0562015-08-12 12:55:02 -07001864void Camera3Device::internalUpdateStatusLocked(Status status) {
1865 mStatus = status;
1866 mRecentStatusUpdates.add(mStatus);
1867 mStatusChanged.broadcast();
1868}
1869
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001870// Pause to reconfigure
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001871status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration) {
Emilian Peeve86358b2019-02-15 13:51:39 -08001872 if (mRequestThread.get() != nullptr) {
1873 mRequestThread->setPaused(true);
1874 } else {
1875 return NO_INIT;
1876 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001877
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001878 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1879 maxExpectedDuration);
1880 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001881 if (res != OK) {
1882 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001883 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001884 }
1885
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001886 return res;
1887}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001888
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001889// Resume after internalPauseAndWaitLocked
1890status_t Camera3Device::internalResumeLocked() {
1891 status_t res;
1892
1893 mRequestThread->setPaused(false);
1894
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001895 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1896 kActiveTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001897 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout);
1898 if (res != OK) {
1899 SET_ERR_L("Can't transition to active in %f seconds!",
1900 kActiveTimeout/1e9);
1901 }
1902 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001903 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001904}
1905
Ruben Brunk183f0562015-08-12 12:55:02 -07001906status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001907 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001908
1909 size_t startIndex = 0;
1910 if (mStatusWaiters == 0) {
1911 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1912 // this status list
1913 mRecentStatusUpdates.clear();
1914 } else {
1915 // If other threads are waiting on updates to this status list, set the position of the
1916 // first element that this list will check rather than clearing the list.
1917 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001918 }
1919
Ruben Brunk183f0562015-08-12 12:55:02 -07001920 mStatusWaiters++;
1921
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001922 bool signalPipelineDrain = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001923 if (!active && mUseHalBufManager) {
1924 auto streamIds = mOutputStreams.getStreamIds();
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001925 if (mStatus == STATUS_ACTIVE) {
1926 mRequestThread->signalPipelineDrain(streamIds);
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001927 signalPipelineDrain = true;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001928 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001929 mRequestBufferSM.onWaitUntilIdle();
1930 }
1931
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001932 bool stateSeen = false;
1933 do {
Ruben Brunk183f0562015-08-12 12:55:02 -07001934 if (active == (mStatus == STATUS_ACTIVE)) {
1935 // Desired state is current
1936 break;
1937 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001938
1939 res = mStatusChanged.waitRelative(mLock, timeout);
1940 if (res != OK) break;
1941
Ruben Brunk183f0562015-08-12 12:55:02 -07001942 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1943 // transitions.
1944 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1945 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1946 __FUNCTION__);
1947
1948 // Encountered desired state since we began waiting
1949 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001950 if (active == (mRecentStatusUpdates[i] == STATUS_ACTIVE) ) {
1951 stateSeen = true;
1952 break;
1953 }
1954 }
1955 } while (!stateSeen);
1956
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001957 if (signalPipelineDrain) {
1958 mRequestThread->resetPipelineDrain();
1959 }
1960
Ruben Brunk183f0562015-08-12 12:55:02 -07001961 mStatusWaiters--;
1962
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001963 return res;
1964}
1965
1966
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001967status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001968 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001969 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001970
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001971 if (listener != NULL && mListener != NULL) {
1972 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1973 }
1974 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001975 mRequestThread->setNotificationListener(listener);
1976 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001977
1978 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001979}
1980
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001981bool Camera3Device::willNotify3A() {
1982 return false;
1983}
1984
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001985status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001986 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001987 std::unique_lock<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001988
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001989 while (mResultQueue.empty()) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001990 auto st = mResultSignal.wait_for(l, std::chrono::nanoseconds(timeout));
1991 if (st == std::cv_status::timeout) {
1992 return TIMED_OUT;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001993 }
1994 }
1995 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001996}
1997
Jianing Weicb0652e2014-03-12 18:29:36 -07001998status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001999 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002000 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002001
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002002 if (mResultQueue.empty()) {
2003 return NOT_ENOUGH_DATA;
2004 }
2005
Jianing Weicb0652e2014-03-12 18:29:36 -07002006 if (frame == NULL) {
2007 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
2008 return BAD_VALUE;
2009 }
2010
2011 CaptureResult &result = *(mResultQueue.begin());
2012 frame->mResultExtras = result.mResultExtras;
2013 frame->mMetadata.acquire(result.mMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08002014 frame->mPhysicalMetadatas = std::move(result.mPhysicalMetadatas);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002015 mResultQueue.erase(mResultQueue.begin());
2016
2017 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002018}
2019
2020status_t Camera3Device::triggerAutofocus(uint32_t id) {
2021 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002022 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002023
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002024 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
2025 // Mix-in this trigger into the next request and only the next request.
2026 RequestTrigger trigger[] = {
2027 {
2028 ANDROID_CONTROL_AF_TRIGGER,
2029 ANDROID_CONTROL_AF_TRIGGER_START
2030 },
2031 {
2032 ANDROID_CONTROL_AF_TRIGGER_ID,
2033 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002034 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002035 };
2036
2037 return mRequestThread->queueTrigger(trigger,
2038 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002039}
2040
2041status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
2042 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002043 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002044
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002045 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
2046 // Mix-in this trigger into the next request and only the next request.
2047 RequestTrigger trigger[] = {
2048 {
2049 ANDROID_CONTROL_AF_TRIGGER,
2050 ANDROID_CONTROL_AF_TRIGGER_CANCEL
2051 },
2052 {
2053 ANDROID_CONTROL_AF_TRIGGER_ID,
2054 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002055 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002056 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002057
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002058 return mRequestThread->queueTrigger(trigger,
2059 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002060}
2061
2062status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
2063 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002064 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002065
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002066 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
2067 // Mix-in this trigger into the next request and only the next request.
2068 RequestTrigger trigger[] = {
2069 {
2070 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
2071 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
2072 },
2073 {
2074 ANDROID_CONTROL_AE_PRECAPTURE_ID,
2075 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07002076 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002077 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002078
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07002079 return mRequestThread->queueTrigger(trigger,
2080 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002081}
2082
Jianing Weicb0652e2014-03-12 18:29:36 -07002083status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002084 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002085 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002086 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002087
Zhijun He7ef20392014-04-21 16:04:17 -07002088 {
2089 Mutex::Autolock l(mLock);
Emilian Peeved2ebe42018-09-25 16:59:09 +01002090
2091 // b/116514106 "disconnect()" can get called twice for the same device. The
2092 // camera device will not be initialized during the second run.
2093 if (mStatus == STATUS_UNINITIALIZED) {
2094 return OK;
2095 }
2096
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002097 mRequestThread->clear(/*out*/frameNumber);
Shuzhen Wang316781a2020-08-18 18:11:01 -07002098
2099 // Stop session and stream counter
2100 mSessionStatsBuilder.stopCounter();
Zhijun He7ef20392014-04-21 16:04:17 -07002101 }
2102
Emilian Peev08dd2452017-04-06 16:55:14 +01002103 return mRequestThread->flush();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07002104}
2105
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002106status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07002107 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
2108}
2109
2110status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002111 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002112 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002113 Mutex::Autolock il(mInterfaceLock);
2114 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002115
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002116 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
2117 if (stream == nullptr) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002118 CLOGE("Stream %d does not exist", streamId);
2119 return BAD_VALUE;
2120 }
2121
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002122 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002123 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002124 return BAD_VALUE;
2125 }
2126
2127 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07002128 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002129 return BAD_VALUE;
2130 }
2131
Ruben Brunkc78ac262015-08-13 17:58:46 -07002132 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002133}
2134
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002135status_t Camera3Device::tearDown(int streamId) {
2136 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002137 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002138 Mutex::Autolock il(mInterfaceLock);
2139 Mutex::Autolock l(mLock);
2140
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002141 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
2142 if (stream == nullptr) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002143 CLOGE("Stream %d does not exist", streamId);
2144 return BAD_VALUE;
2145 }
2146
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07002147 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
2148 CLOGE("Stream %d is a target of a in-progress request", streamId);
2149 return BAD_VALUE;
2150 }
2151
2152 return stream->tearDown();
2153}
2154
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002155status_t Camera3Device::addBufferListenerForStream(int streamId,
2156 wp<Camera3StreamBufferListener> listener) {
2157 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002158 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002159 Mutex::Autolock il(mInterfaceLock);
2160 Mutex::Autolock l(mLock);
2161
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002162 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
2163 if (stream == nullptr) {
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002164 CLOGE("Stream %d does not exist", streamId);
2165 return BAD_VALUE;
2166 }
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07002167 stream->addBufferListener(listener);
2168
2169 return OK;
2170}
2171
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002172/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002173 * Methods called by subclasses
2174 */
2175
2176void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002177 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -07002178 std::vector<int> streamIds;
2179 std::vector<hardware::CameraStreamStats> streamStats;
2180
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002181 {
2182 // Need mLock to safely update state and synchronize to current
2183 // state of methods in flight.
2184 Mutex::Autolock l(mLock);
2185 // We can get various system-idle notices from the status tracker
2186 // while starting up. Only care about them if we've actually sent
2187 // in some requests recently.
2188 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
2189 return;
2190 }
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08002191 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
2192 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
Ruben Brunk183f0562015-08-12 12:55:02 -07002193 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002194
2195 // Skip notifying listener if we're doing some user-transparent
2196 // state changes
2197 if (mPauseStateNotify) return;
Shuzhen Wang316781a2020-08-18 18:11:01 -07002198
2199 // Populate stream statistics in case of Idle
2200 if (idle) {
2201 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2202 auto stream = mOutputStreams[i];
2203 if (stream.get() == nullptr) continue;
2204 streamIds.push_back(stream->getId());
2205 Camera3Stream* camera3Stream = Camera3Stream::cast(stream->asHalStream());
2206 int64_t usage = 0LL;
2207 if (camera3Stream != nullptr) {
2208 usage = camera3Stream->getUsage();
2209 }
2210 streamStats.emplace_back(stream->getWidth(), stream->getHeight(),
2211 stream->getFormat(), stream->getDataSpace(), usage,
2212 stream->getMaxHalBuffers(),
2213 stream->getMaxTotalBuffers() - stream->getMaxHalBuffers());
2214 }
2215 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002216 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002217
2218 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002219 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002220 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002221 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002222 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07002223 status_t res = OK;
2224 if (listener != nullptr) {
2225 if (idle) {
2226 // Get session stats from the builder, and notify the listener.
2227 int64_t requestCount, resultErrorCount;
2228 bool deviceError;
2229 std::map<int, StreamStats> streamStatsMap;
2230 mSessionStatsBuilder.buildAndReset(&requestCount, &resultErrorCount,
2231 &deviceError, &streamStatsMap);
2232 for (size_t i = 0; i < streamIds.size(); i++) {
2233 int streamId = streamIds[i];
2234 auto stats = streamStatsMap.find(streamId);
2235 if (stats != streamStatsMap.end()) {
2236 streamStats[i].mRequestCount = stats->second.mRequestedFrameCount;
2237 streamStats[i].mErrorCount = stats->second.mDroppedFrameCount;
2238 streamStats[i].mStartLatencyMs = stats->second.mStartLatencyMs;
2239 streamStats[i].mHistogramType =
2240 hardware::CameraStreamStats::HISTOGRAM_TYPE_CAPTURE_LATENCY;
2241 streamStats[i].mHistogramBins.assign(
2242 stats->second.mCaptureLatencyBins.begin(),
2243 stats->second.mCaptureLatencyBins.end());
2244 streamStats[i].mHistogramCounts.assign(
2245 stats->second.mCaptureLatencyHistogram.begin(),
2246 stats->second.mCaptureLatencyHistogram.end());
2247 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07002248 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07002249 listener->notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
2250 } else {
2251 res = listener->notifyActive();
Shuzhen Wang316781a2020-08-18 18:11:01 -07002252 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07002253 }
2254 if (res != OK) {
2255 SET_ERR("Camera access permission lost mid-operation: %s (%d)",
2256 strerror(-res), res);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002257 }
2258}
2259
Shuzhen Wang758c2152017-01-10 18:26:18 -08002260status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01002261 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07002262 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08002263 ALOGV("%s: Camera %s: set consumer surface for stream %d",
2264 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01002265
2266 if (surfaceIds == nullptr) {
2267 return BAD_VALUE;
2268 }
2269
Zhijun He5d677d12016-05-29 16:52:39 -07002270 Mutex::Autolock il(mInterfaceLock);
2271 Mutex::Autolock l(mLock);
2272
Shuzhen Wang758c2152017-01-10 18:26:18 -08002273 if (consumers.size() == 0) {
2274 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07002275 return BAD_VALUE;
2276 }
2277
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002278 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2279 if (stream == nullptr) {
Zhijun He5d677d12016-05-29 16:52:39 -07002280 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002281 return BAD_VALUE;
Zhijun He5d677d12016-05-29 16:52:39 -07002282 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002283
2284 // isConsumerConfigurationDeferred will be off after setConsumers
2285 bool isDeferred = stream->isConsumerConfigurationDeferred();
Shuzhen Wang758c2152017-01-10 18:26:18 -08002286 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07002287 if (res != OK) {
2288 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
2289 return res;
2290 }
2291
Emilian Peev40ead602017-09-26 15:46:36 +01002292 for (auto &consumer : consumers) {
2293 int id = stream->getSurfaceId(consumer);
2294 if (id < 0) {
2295 CLOGE("Invalid surface id!");
2296 return BAD_VALUE;
2297 }
2298 surfaceIds->push_back(id);
2299 }
2300
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002301 if (isDeferred) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07002302 if (!stream->isConfiguring()) {
2303 CLOGE("Stream %d was already fully configured.", streamId);
2304 return INVALID_OPERATION;
2305 }
Zhijun He5d677d12016-05-29 16:52:39 -07002306
Shuzhen Wang0129d522016-10-30 22:43:41 -07002307 res = stream->finishConfiguration();
2308 if (res != OK) {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002309 // If finishConfiguration fails due to abandoned surface, do not set
2310 // device to error state.
2311 bool isSurfaceAbandoned =
2312 (res == NO_INIT || res == DEAD_OBJECT) && stream->isAbandoned();
2313 if (!isSurfaceAbandoned) {
2314 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
2315 stream->getId(), strerror(-res), res);
2316 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07002317 return res;
2318 }
Zhijun He5d677d12016-05-29 16:52:39 -07002319 }
2320
2321 return OK;
2322}
2323
Emilian Peev40ead602017-09-26 15:46:36 +01002324status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
2325 const std::vector<OutputStreamInfo> &outputInfo,
2326 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
2327 Mutex::Autolock il(mInterfaceLock);
2328 Mutex::Autolock l(mLock);
2329
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002330 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2331 if (stream == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +01002332 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002333 return BAD_VALUE;
Emilian Peev40ead602017-09-26 15:46:36 +01002334 }
2335
2336 for (const auto &it : removedSurfaceIds) {
2337 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2338 CLOGE("Shared surface still part of a pending request!");
2339 return -EBUSY;
2340 }
2341 }
2342
Emilian Peev40ead602017-09-26 15:46:36 +01002343 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2344 if (res != OK) {
2345 CLOGE("Stream %d failed to update stream (error %d %s) ",
2346 streamId, res, strerror(-res));
2347 if (res == UNKNOWN_ERROR) {
2348 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2349 __FUNCTION__);
2350 }
2351 return res;
2352 }
2353
2354 return res;
2355}
2356
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002357status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2358 Mutex::Autolock il(mInterfaceLock);
2359 Mutex::Autolock l(mLock);
2360
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002361 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2362 if (stream == nullptr) {
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002363 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2364 return BAD_VALUE;
2365 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07002366
2367 if (dropping) {
2368 mSessionStatsBuilder.stopCounter(streamId);
2369 } else {
2370 mSessionStatsBuilder.startCounter(streamId);
2371 }
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002372 return stream->dropBuffers(dropping);
2373}
2374
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002375/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002376 * Camera3Device private methods
2377 */
2378
2379sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002380 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002381 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002382
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002383 sp<CaptureRequest> newRequest = new CaptureRequest();
Emilian Peevaebbe412018-01-15 13:53:24 +00002384 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002385
2386 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002387 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002388 if (inputStreams.count > 0) {
2389 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002390 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002391 CLOGE("Request references unknown input stream %d",
2392 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002393 return NULL;
2394 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002395
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002396 if (mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002397 SET_ERR_L("%s: input stream %d is not configured!",
2398 __FUNCTION__, mInputStream->getId());
2399 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002400 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002401 // Check if stream prepare is blocking requests.
2402 if (mInputStream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002403 CLOGE("Request references an input stream that's being prepared!");
2404 return NULL;
2405 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002406
2407 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002408 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002409 }
2410
2411 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002412 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002413 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002414 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002415 return NULL;
2416 }
2417
2418 for (size_t i = 0; i < streams.count; i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002419 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streams.data.i32[i]);
2420 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002421 CLOGE("Request references unknown stream %d",
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002422 streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002423 return NULL;
2424 }
Zhijun He5d677d12016-05-29 16:52:39 -07002425 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002426 auto iter = surfaceMap.find(streams.data.i32[i]);
2427 if (iter != surfaceMap.end()) {
2428 const std::vector<size_t>& surfaces = iter->second;
2429 for (const auto& surface : surfaces) {
2430 if (stream->isConsumerConfigurationDeferred(surface)) {
2431 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2432 "due to deferred consumer", stream->getId(), surface);
2433 return NULL;
2434 }
2435 }
Yin-Chia Yeh0b287572018-10-15 12:38:13 -07002436 newRequest->mOutputSurfaces[streams.data.i32[i]] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002437 }
2438
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002439 if (stream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002440 SET_ERR_L("%s: stream %d is not configured!", __FUNCTION__, stream->getId());
2441 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002442 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002443 // Check if stream prepare is blocking requests.
2444 if (stream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002445 CLOGE("Request references an output stream that's being prepared!");
2446 return NULL;
2447 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002448
2449 newRequest->mOutputStreams.push(stream);
2450 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002451 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002452 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002453
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002454 auto rotateAndCropEntry =
2455 newRequest->mSettingsList.begin()->metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
2456 if (rotateAndCropEntry.count > 0 &&
2457 rotateAndCropEntry.data.u8[0] == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
2458 newRequest->mRotateAndCropAuto = true;
2459 } else {
2460 newRequest->mRotateAndCropAuto = false;
2461 }
2462
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07002463 auto zoomRatioEntry =
2464 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_ZOOM_RATIO);
2465 if (zoomRatioEntry.count > 0 &&
2466 zoomRatioEntry.data.f[0] == 1.0f) {
2467 newRequest->mZoomRatioIs1x = true;
2468 } else {
2469 newRequest->mZoomRatioIs1x = false;
2470 }
2471
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002472 if (mSupportCameraMute) {
2473 auto testPatternModeEntry =
2474 newRequest->mSettingsList.begin()->metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
2475 newRequest->mOriginalTestPatternMode = testPatternModeEntry.count > 0 ?
2476 testPatternModeEntry.data.i32[0] :
2477 ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;
2478
2479 auto testPatternDataEntry =
2480 newRequest->mSettingsList.begin()->metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
Eino-Ville Talvalac2459842021-07-22 16:36:36 -07002481 if (testPatternDataEntry.count >= 4) {
2482 memcpy(newRequest->mOriginalTestPatternData, testPatternDataEntry.data.i32,
2483 sizeof(CaptureRequest::mOriginalTestPatternData));
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002484 } else {
2485 newRequest->mOriginalTestPatternData[0] = 0;
2486 newRequest->mOriginalTestPatternData[1] = 0;
2487 newRequest->mOriginalTestPatternData[2] = 0;
2488 newRequest->mOriginalTestPatternData[3] = 0;
2489 }
2490 }
2491
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002492 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002493}
2494
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002495void Camera3Device::cancelStreamsConfigurationLocked() {
2496 int res = OK;
2497 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2498 res = mInputStream->cancelConfiguration();
2499 if (res != OK) {
2500 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2501 mInputStream->getId(), strerror(-res), res);
2502 }
2503 }
2504
2505 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002506 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002507 if (outputStream->isConfiguring()) {
2508 res = outputStream->cancelConfiguration();
2509 if (res != OK) {
2510 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2511 outputStream->getId(), strerror(-res), res);
2512 }
2513 }
2514 }
2515
2516 // Return state to that at start of call, so that future configures
2517 // properly clean things up
2518 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2519 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002520
2521 res = mPreparerThread->resume();
2522 if (res != OK) {
2523 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2524 }
2525}
2526
Emilian Peev0d0191e2020-04-21 17:01:18 -07002527bool Camera3Device::checkAbandonedStreamsLocked() {
2528 if ((mInputStream.get() != nullptr) && (mInputStream->isAbandoned())) {
2529 return true;
2530 }
2531
2532 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2533 auto stream = mOutputStreams[i];
2534 if ((stream.get() != nullptr) && (stream->isAbandoned())) {
2535 return true;
2536 }
2537 }
2538
2539 return false;
2540}
2541
Emilian Peev3bead5f2020-05-28 17:29:08 -07002542bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams, int clientStatusId) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002543 ATRACE_CALL();
2544 bool ret = false;
2545
Shuzhen Wang316781a2020-08-18 18:11:01 -07002546 nsecs_t startTime = systemTime();
2547
Jayant Chowdhary646c31b2020-01-30 13:09:59 -08002548 Mutex::Autolock il(mInterfaceLock);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002549 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2550
2551 Mutex::Autolock l(mLock);
Emilian Peev0d0191e2020-04-21 17:01:18 -07002552 if (checkAbandonedStreamsLocked()) {
2553 ALOGW("%s: Abandoned stream detected, session parameters can't be applied correctly!",
2554 __FUNCTION__);
2555 return true;
2556 }
2557
Emilian Peev3bead5f2020-05-28 17:29:08 -07002558 status_t rc = NO_ERROR;
2559 bool markClientActive = false;
2560 if (mStatus == STATUS_ACTIVE) {
2561 markClientActive = true;
2562 mPauseStateNotify = true;
2563 mStatusTracker->markComponentIdle(clientStatusId, Fence::NO_FENCE);
2564
2565 rc = internalPauseAndWaitLocked(maxExpectedDuration);
2566 }
2567
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002568 if (rc == NO_ERROR) {
2569 mNeedConfig = true;
2570 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2571 if (rc == NO_ERROR) {
2572 ret = true;
2573 mPauseStateNotify = false;
2574 //Moving to active state while holding 'mLock' is important.
2575 //There could be pending calls to 'create-/deleteStream' which
2576 //will trigger another stream configuration while the already
2577 //present streams end up with outstanding buffers that will
2578 //not get drained.
2579 internalUpdateStatusLocked(STATUS_ACTIVE);
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002580 } else if (rc == DEAD_OBJECT) {
2581 // DEAD_OBJECT can be returned if either the consumer surface is
2582 // abandoned, or the HAL has died.
2583 // - If the HAL has died, configureStreamsLocked call will set
2584 // device to error state,
2585 // - If surface is abandoned, we should not set device to error
2586 // state.
2587 ALOGE("Failed to re-configure camera due to abandoned surface");
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002588 } else {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002589 SET_ERR_L("Failed to re-configure camera: %d", rc);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002590 }
2591 } else {
2592 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2593 }
2594
Shuzhen Wang316781a2020-08-18 18:11:01 -07002595 CameraServiceProxyWrapper::logStreamConfigured(mId, mOperatingMode, true /*internalReconfig*/,
2596 ns2ms(systemTime() - startTime));
2597
Emilian Peev3bead5f2020-05-28 17:29:08 -07002598 if (markClientActive) {
2599 mStatusTracker->markComponentActive(clientStatusId);
2600 }
2601
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002602 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002603}
2604
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002605status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002606 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002607 ATRACE_CALL();
2608 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002609
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002610 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002611 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002612 return INVALID_OPERATION;
2613 }
2614
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002615 if (operatingMode < 0) {
2616 CLOGE("Invalid operating mode: %d", operatingMode);
2617 return BAD_VALUE;
2618 }
2619
2620 bool isConstrainedHighSpeed =
2621 static_cast<int>(StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE) ==
2622 operatingMode;
2623
2624 if (mOperatingMode != operatingMode) {
2625 mNeedConfig = true;
2626 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2627 mOperatingMode = operatingMode;
2628 }
2629
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002630 // In case called from configureStreams, abort queued input buffers not belonging to
2631 // any pending requests.
2632 if (mInputStream != NULL && notifyRequestThread) {
2633 while (true) {
Emilian Peevf4816702020-04-03 15:44:51 -07002634 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08002635 camera3::Size inputBufferSize;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002636 status_t res = mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08002637 &inputBufferSize, /*respectHalLimit*/ false);
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002638 if (res != OK) {
2639 // Exhausted acquiring all input buffers.
2640 break;
2641 }
2642
Emilian Peevf4816702020-04-03 15:44:51 -07002643 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002644 res = mInputStream->returnInputBuffer(inputBuffer);
2645 if (res != OK) {
2646 ALOGE("%s: %d: couldn't return input buffer while clearing input queue: "
2647 "%s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2648 }
2649 }
2650 }
2651
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002652 if (!mNeedConfig) {
2653 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2654 return OK;
2655 }
2656
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002657 // Workaround for device HALv3.2 or older spec bug - zero streams requires
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002658 // adding a fake stream instead.
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002659 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2660 if (mOutputStreams.size() == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002661 addFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002662 } else {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002663 tryRemoveFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002664 }
2665
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002666 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002667 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002668
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002669 mPreparerThread->pause();
2670
Emilian Peevf4816702020-04-03 15:44:51 -07002671 camera_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002672 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002673 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
Shuzhen Wang83bff122020-11-20 15:51:39 -08002674 config.input_is_multi_resolution = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002675
Emilian Peevf4816702020-04-03 15:44:51 -07002676 Vector<camera3::camera_stream_t*> streams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002677 streams.setCapacity(config.num_streams);
Emilian Peev192ee832018-01-31 14:46:47 +00002678 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002679
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002680
2681 if (mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07002682 camera3::camera_stream_t *inputStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002683 inputStream = mInputStream->startConfiguration();
2684 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002685 CLOGE("Can't start input stream configuration");
2686 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002687 return INVALID_OPERATION;
2688 }
2689 streams.add(inputStream);
Shuzhen Wang83bff122020-11-20 15:51:39 -08002690
2691 config.input_is_multi_resolution = mIsInputStreamMultiResolution;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002692 }
2693
Shuzhen Wang99080502021-03-07 21:08:20 -08002694 mGroupIdPhysicalCameraMap.clear();
Emilian Peeve23f1d92021-09-20 14:56:01 -07002695 bool composerSurfacePresent = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002696 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002697
2698 // Don't configure bidi streams twice, nor add them twice to the list
2699 if (mOutputStreams[i].get() ==
2700 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2701
2702 config.num_streams--;
2703 continue;
2704 }
2705
Emilian Peevf4816702020-04-03 15:44:51 -07002706 camera3::camera_stream_t *outputStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002707 outputStream = mOutputStreams[i]->startConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002708 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002709 CLOGE("Can't start output stream configuration");
2710 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002711 return INVALID_OPERATION;
2712 }
2713 streams.add(outputStream);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002714
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002715 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB) {
Emilian Peev192ee832018-01-31 14:46:47 +00002716 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2717 // always occupy the initial entry.
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002718 if (outputStream->data_space == HAL_DATASPACE_V0_JFIF) {
2719 bufferSizes[k] = static_cast<uint32_t>(
2720 getJpegBufferSize(outputStream->width, outputStream->height));
2721 } else if (outputStream->data_space ==
2722 static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
2723 bufferSizes[k] = outputStream->width * outputStream->height;
2724 } else {
2725 ALOGW("%s: Blob dataSpace %d not supported",
2726 __FUNCTION__, outputStream->data_space);
2727 }
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002728 }
Shuzhen Wang99080502021-03-07 21:08:20 -08002729
2730 if (mOutputStreams[i]->isMultiResolution()) {
2731 int32_t streamGroupId = mOutputStreams[i]->getHalStreamGroupId();
2732 const String8& physicalCameraId = mOutputStreams[i]->getPhysicalCameraId();
2733 mGroupIdPhysicalCameraMap[streamGroupId].insert(physicalCameraId);
2734 }
Emilian Peeve23f1d92021-09-20 14:56:01 -07002735
2736 if (outputStream->usage & GraphicBuffer::USAGE_HW_COMPOSER) {
2737 composerSurfacePresent = true;
2738 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002739 }
2740
2741 config.streams = streams.editArray();
2742
2743 // Do the HAL configuration; will potentially touch stream
Shuzhen Wang92653952019-05-07 15:11:43 -07002744 // max_buffers, usage, and priv fields, as well as data_space and format
2745 // fields for IMPLEMENTATION_DEFINED formats.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002746
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002747 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
Emilian Peev192ee832018-01-31 14:46:47 +00002748 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002749 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002750
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002751 if (res == BAD_VALUE) {
2752 // HAL rejected this set of streams as unsupported, clean up config
2753 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002754 CLOGE("Set of requested inputs/outputs not supported by HAL");
2755 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002756 return BAD_VALUE;
2757 } else if (res != OK) {
2758 // Some other kind of error from configure_streams - this is not
2759 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002760 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2761 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002762 return res;
2763 }
2764
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002765 // Finish all stream configuration immediately.
2766 // TODO: Try to relax this later back to lazy completion, which should be
2767 // faster
2768
Igor Murashkin073f8572013-05-02 14:59:28 -07002769 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002770 bool streamReConfigured = false;
2771 res = mInputStream->finishConfiguration(&streamReConfigured);
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002772 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002773 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002774 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002775 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002776 if ((res == NO_INIT || res == DEAD_OBJECT) && mInputStream->isAbandoned()) {
2777 return DEAD_OBJECT;
2778 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002779 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002780 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002781 if (streamReConfigured) {
2782 mInterface->onStreamReConfigured(mInputStream->getId());
2783 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002784 }
2785
2786 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002787 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Zhijun He5d677d12016-05-29 16:52:39 -07002788 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002789 bool streamReConfigured = false;
2790 res = outputStream->finishConfiguration(&streamReConfigured);
Igor Murashkin073f8572013-05-02 14:59:28 -07002791 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002792 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002793 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002794 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002795 if ((res == NO_INIT || res == DEAD_OBJECT) && outputStream->isAbandoned()) {
2796 return DEAD_OBJECT;
2797 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002798 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002799 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002800 if (streamReConfigured) {
2801 mInterface->onStreamReConfigured(outputStream->getId());
2802 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002803 }
2804 }
2805
Emilian Peeve23f1d92021-09-20 14:56:01 -07002806 mRequestThread->setComposerSurface(composerSurfacePresent);
2807
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002808 // Request thread needs to know to avoid using repeat-last-settings protocol
2809 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002810 if (notifyRequestThread) {
Shuzhen Wang99080502021-03-07 21:08:20 -08002811 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration,
2812 sessionParams, mGroupIdPhysicalCameraMap);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002813 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002814
Zhijun He90f7c372016-08-16 16:19:43 -07002815 char value[PROPERTY_VALUE_MAX];
2816 property_get("camera.fifo.disable", value, "0");
2817 int32_t disableFifo = atoi(value);
2818 if (disableFifo != 1) {
2819 // Boost priority of request thread to SCHED_FIFO.
2820 pid_t requestThreadTid = mRequestThread->getTid();
2821 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002822 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002823 if (res != OK) {
2824 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2825 strerror(-res), res);
2826 } else {
2827 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2828 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002829 }
2830
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002831 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002832 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2833 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2834 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2835 sessionParams.unlock(newSessionParams);
2836 mSessionParams.unlock(currentSessionParams);
2837 if (updateSessionParams) {
2838 mSessionParams = sessionParams;
2839 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002840
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002841 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002842
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002843 internalUpdateStatusLocked((mFakeStreamId == NO_STREAM) ?
Ruben Brunk183f0562015-08-12 12:55:02 -07002844 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002845
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002846 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002847
Zhijun He0a210512014-07-24 13:45:15 -07002848 // tear down the deleted streams after configure streams.
2849 mDeletedStreams.clear();
2850
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002851 auto rc = mPreparerThread->resume();
2852 if (rc != OK) {
2853 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2854 return rc;
2855 }
2856
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002857 if (mFakeStreamId == NO_STREAM) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002858 mRequestBufferSM.onStreamsConfigured();
2859 }
2860
Cliff Wu3b268182021-07-06 15:44:43 +08002861 // First call injectCamera() and then run configureStreamsLocked() case:
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002862 // Since the streams configuration of the injection camera is based on the internal camera, we
Cliff Wu3b268182021-07-06 15:44:43 +08002863 // must wait until the internal camera configure streams before running the injection job to
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002864 // configure the injection streams.
2865 if (mInjectionMethods->isInjecting()) {
Cliff Wu3b268182021-07-06 15:44:43 +08002866 ALOGD("%s: Injection camera %s: Start to configure streams.",
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002867 __FUNCTION__, mInjectionMethods->getInjectedCamId().string());
2868 res = mInjectionMethods->injectCamera(config, bufferSizes);
2869 if (res != OK) {
2870 ALOGE("Can't finish inject camera process!");
2871 return res;
2872 }
Cliff Wu3b268182021-07-06 15:44:43 +08002873 } else {
2874 // First run configureStreamsLocked() and then call injectCamera() case:
2875 // If the stream configuration has been completed and camera deive is active, but the
2876 // injection camera has not been injected yet, we need to store the stream configuration of
2877 // the internal camera (because the stream configuration of the injection camera is based
2878 // on the internal camera). When injecting occurs later, this configuration can be used by
2879 // the injection camera.
2880 ALOGV("%s: The stream configuration is complete and the camera device is active, but the"
2881 " injection camera has not been injected yet.", __FUNCTION__);
2882 mInjectionMethods->storeInjectionConfig(config, bufferSizes);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002883 }
2884
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002885 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002886}
2887
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002888status_t Camera3Device::addFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002889 ATRACE_CALL();
2890 status_t res;
2891
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002892 if (mFakeStreamId != NO_STREAM) {
2893 // Should never be adding a second fake stream when one is already
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002894 // active
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002895 SET_ERR_L("%s: Camera %s: A fake stream already exists!",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002896 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002897 return INVALID_OPERATION;
2898 }
2899
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002900 ALOGV("%s: Camera %s: Adding a fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002901
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002902 sp<Camera3OutputStreamInterface> fakeStream =
2903 new Camera3FakeStream(mNextStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002904
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002905 res = mOutputStreams.add(mNextStreamId, fakeStream);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002906 if (res < 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002907 SET_ERR_L("Can't add fake stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002908 return res;
2909 }
2910
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002911 mFakeStreamId = mNextStreamId;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002912 mNextStreamId++;
2913
2914 return OK;
2915}
2916
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002917status_t Camera3Device::tryRemoveFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002918 ATRACE_CALL();
2919 status_t res;
2920
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002921 if (mFakeStreamId == NO_STREAM) return OK;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002922 if (mOutputStreams.size() == 1) return OK;
2923
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002924 ALOGV("%s: Camera %s: Removing the fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002925
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002926 // Ok, have a fake stream and there's at least one other output stream,
2927 // so remove the fake
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002928
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002929 sp<Camera3StreamInterface> deletedStream = mOutputStreams.get(mFakeStreamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002930 if (deletedStream == nullptr) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002931 SET_ERR_L("Fake stream %d does not appear to exist", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002932 return INVALID_OPERATION;
2933 }
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002934 mOutputStreams.remove(mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002935
2936 // Free up the stream endpoint so that it can be used by some other stream
2937 res = deletedStream->disconnect();
2938 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002939 SET_ERR_L("Can't disconnect deleted fake stream %d", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002940 // fall through since we want to still list the stream as deleted.
2941 }
2942 mDeletedStreams.add(deletedStream);
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002943 mFakeStreamId = NO_STREAM;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002944
2945 return res;
2946}
2947
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002948void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002949 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002950 Mutex::Autolock l(mLock);
2951 va_list args;
2952 va_start(args, fmt);
2953
2954 setErrorStateLockedV(fmt, args);
2955
2956 va_end(args);
2957}
2958
2959void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002960 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002961 Mutex::Autolock l(mLock);
2962 setErrorStateLockedV(fmt, args);
2963}
2964
2965void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2966 va_list args;
2967 va_start(args, fmt);
2968
2969 setErrorStateLockedV(fmt, args);
2970
2971 va_end(args);
2972}
2973
2974void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002975 // Print out all error messages to log
2976 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002977 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002978
2979 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002980 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002981
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002982 mErrorCause = errorCause;
2983
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07002984 if (mRequestThread != nullptr) {
2985 mRequestThread->setPaused(true);
2986 }
Ruben Brunk183f0562015-08-12 12:55:02 -07002987 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002988
2989 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002990 sp<NotificationListener> listener = mListener.promote();
2991 if (listener != NULL) {
2992 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002993 CaptureResultExtras());
Shuzhen Wang316781a2020-08-18 18:11:01 -07002994 mSessionStatsBuilder.onDeviceError();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002995 }
2996
2997 // Save stack trace. View by dumping it later.
2998 CameraTraces::saveTrace();
2999 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07003000}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003001
3002/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003003 * In-flight request management
3004 */
3005
Jianing Weicb0652e2014-03-12 18:29:36 -07003006status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07003007 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang5c22c152017-12-31 17:12:25 -08003008 bool hasAppCallback, nsecs_t maxExpectedDuration,
Shuzhen Wang99080502021-03-07 21:08:20 -08003009 const std::set<std::set<String8>>& physicalCameraIds,
3010 bool isStillCapture, bool isZslCapture, bool rotateAndCropAuto,
3011 const std::set<std::string>& cameraIdsWithZoom,
Shuzhen Wang316781a2020-08-18 18:11:01 -07003012 const SurfaceMap& outputSurfaces, nsecs_t requestTimeNs) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003013 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003014 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003015
3016 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07003017 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003018 hasAppCallback, maxExpectedDuration, physicalCameraIds, isStillCapture, isZslCapture,
Shuzhen Wang316781a2020-08-18 18:11:01 -07003019 rotateAndCropAuto, cameraIdsWithZoom, requestTimeNs, outputSurfaces));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003020 if (res < 0) return res;
3021
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07003022 if (mInFlightMap.size() == 1) {
Emilian Peev26d975d2018-07-05 14:52:57 +01003023 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
3024 // avoid a deadlock during reprocess requests.
3025 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07003026 if (mStatusTracker != nullptr) {
3027 mStatusTracker->markComponentActive(mInFlightStatusId);
3028 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07003029 }
3030
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003031 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003032 return OK;
3033}
3034
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003035void Camera3Device::onInflightEntryRemovedLocked(nsecs_t duration) {
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003036 // Indicate idle inFlightMap to the status tracker
3037 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07003038 mRequestBufferSM.onInflightMapEmpty();
Emilian Peev26d975d2018-07-05 14:52:57 +01003039 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
3040 // avoid a deadlock during reprocess requests.
3041 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07003042 if (mStatusTracker != nullptr) {
3043 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
3044 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003045 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003046 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07003047}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003048
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003049void Camera3Device::checkInflightMapLengthLocked() {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003050 // Validation check - if we have too many in-flight frames with long total inflight duration,
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -07003051 // something has likely gone wrong. This might still be legit only if application send in
3052 // a long burst of long exposure requests.
3053 if (mExpectedInflightDuration > kMinWarnInflightDuration) {
3054 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
3055 CLOGW("In-flight list too large: %zu, total inflight duration %" PRIu64,
3056 mInFlightMap.size(), mExpectedInflightDuration);
3057 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
3058 kInFlightWarnLimitHighSpeed) {
3059 CLOGW("In-flight list too large for high speed configuration: %zu,"
3060 "total inflight duration %" PRIu64,
3061 mInFlightMap.size(), mExpectedInflightDuration);
3062 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08003063 }
3064}
3065
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003066void Camera3Device::onInflightMapFlushedLocked() {
3067 mExpectedInflightDuration = 0;
3068}
3069
3070void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Jayant Chowdharyd4776262020-06-23 23:45:57 -07003071 ATRACE_HFR_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003072 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
3073 mInFlightMap.removeItemsAt(idx, 1);
3074
3075 onInflightEntryRemovedLocked(duration);
3076}
3077
3078
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07003079void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003080 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003081 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003082 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003083 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003084 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003085 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003086
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003087 FlushInflightReqStates states {
3088 mId, mInFlightLock, mInFlightMap, mUseHalBufManager,
Shuzhen Wang316781a2020-08-18 18:11:01 -07003089 listener, *this, *mInterface, *this, mSessionStatsBuilder};
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07003090
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003091 camera3::flushInflightRequests(states);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003092}
3093
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003094CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07003095 ALOGV("%s", __FUNCTION__);
3096
Igor Murashkin1e479c02013-09-06 16:55:14 -07003097 CameraMetadata retVal;
3098
3099 if (mRequestThread != NULL) {
3100 retVal = mRequestThread->getLatestRequest();
3101 }
3102
Igor Murashkin1e479c02013-09-06 16:55:14 -07003103 return retVal;
3104}
3105
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003106void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003107 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata,
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07003108 const std::unordered_map<std::string, CameraMetadata>& physicalMetadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07003109 const camera_stream_buffer_t *outputBuffers, uint32_t numOutputBuffers,
3110 int32_t inputStreamId) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003111
3112 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07003113 physicalMetadata, outputBuffers, numOutputBuffers, inputStreamId);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07003114}
3115
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08003116/**
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003117 * HalInterface inner class methods
3118 */
3119
Yifan Hongf79b5542017-04-11 14:44:25 -07003120Camera3Device::HalInterface::HalInterface(
3121 sp<ICameraDeviceSession> &session,
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003122 std::shared_ptr<RequestMetadataQueue> queue,
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003123 bool useHalBufManager, bool supportOfflineProcessing) :
Yifan Hongf79b5542017-04-11 14:44:25 -07003124 mHidlSession(session),
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003125 mRequestMetadataQueue(queue),
Emilian Peev4ec17882019-01-24 17:16:58 -08003126 mUseHalBufManager(useHalBufManager),
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003127 mIsReconfigurationQuerySupported(true),
3128 mSupportOfflineProcessing(supportOfflineProcessing) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003129 // Check with hardware service manager if we can downcast these interfaces
3130 // Somewhat expensive, so cache the results at startup
Shuzhen Wang83bff122020-11-20 15:51:39 -08003131 auto castResult_3_7 = device::V3_7::ICameraDeviceSession::castFrom(mHidlSession);
3132 if (castResult_3_7.isOk()) {
3133 mHidlSession_3_7 = castResult_3_7;
3134 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003135 auto castResult_3_6 = device::V3_6::ICameraDeviceSession::castFrom(mHidlSession);
3136 if (castResult_3_6.isOk()) {
3137 mHidlSession_3_6 = castResult_3_6;
3138 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003139 auto castResult_3_5 = device::V3_5::ICameraDeviceSession::castFrom(mHidlSession);
3140 if (castResult_3_5.isOk()) {
3141 mHidlSession_3_5 = castResult_3_5;
3142 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003143 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
3144 if (castResult_3_4.isOk()) {
3145 mHidlSession_3_4 = castResult_3_4;
3146 }
3147 auto castResult_3_3 = device::V3_3::ICameraDeviceSession::castFrom(mHidlSession);
3148 if (castResult_3_3.isOk()) {
3149 mHidlSession_3_3 = castResult_3_3;
3150 }
3151}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003152
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003153Camera3Device::HalInterface::HalInterface() :
3154 mUseHalBufManager(false),
3155 mSupportOfflineProcessing(false) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003156
3157Camera3Device::HalInterface::HalInterface(const HalInterface& other) :
Yifan Hongf79b5542017-04-11 14:44:25 -07003158 mHidlSession(other.mHidlSession),
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003159 mRequestMetadataQueue(other.mRequestMetadataQueue),
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003160 mUseHalBufManager(other.mUseHalBufManager),
3161 mSupportOfflineProcessing(other.mSupportOfflineProcessing) {}
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003162
3163bool Camera3Device::HalInterface::valid() {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003164 return (mHidlSession != nullptr);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003165}
3166
3167void Camera3Device::HalInterface::clear() {
Shuzhen Wang83bff122020-11-20 15:51:39 -08003168 mHidlSession_3_7.clear();
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003169 mHidlSession_3_6.clear();
Emilian Peev644a3e12018-11-23 13:52:39 +00003170 mHidlSession_3_5.clear();
Emilian Peev9e740b02018-01-30 18:28:03 +00003171 mHidlSession_3_4.clear();
3172 mHidlSession_3_3.clear();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003173 mHidlSession.clear();
3174}
3175
3176status_t Camera3Device::HalInterface::constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -07003177 camera_request_template_t templateId,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003178 /*out*/ camera_metadata_t **requestTemplate) {
3179 ATRACE_NAME("CameraHal::constructDefaultRequestSettings");
3180 if (!valid()) return INVALID_OPERATION;
3181 status_t res = OK;
3182
Emilian Peev31abd0a2017-05-11 18:37:46 +01003183 common::V1_0::Status status;
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003184
3185 auto requestCallback = [&status, &requestTemplate]
Emilian Peev31abd0a2017-05-11 18:37:46 +01003186 (common::V1_0::Status s, const device::V3_2::CameraMetadata& request) {
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003187 status = s;
3188 if (status == common::V1_0::Status::OK) {
3189 const camera_metadata *r =
3190 reinterpret_cast<const camera_metadata_t*>(request.data());
3191 size_t expectedSize = request.size();
3192 int ret = validate_camera_metadata_structure(r, &expectedSize);
3193 if (ret == OK || ret == CAMERA_METADATA_VALIDATION_SHIFTED) {
3194 *requestTemplate = clone_camera_metadata(r);
3195 if (*requestTemplate == nullptr) {
3196 ALOGE("%s: Unable to clone camera metadata received from HAL",
3197 __FUNCTION__);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003198 status = common::V1_0::Status::INTERNAL_ERROR;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003199 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003200 } else {
3201 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
3202 status = common::V1_0::Status::INTERNAL_ERROR;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003203 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003204 }
3205 };
3206 hardware::Return<void> err;
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003207 RequestTemplate id;
3208 switch (templateId) {
Emilian Peevf4816702020-04-03 15:44:51 -07003209 case CAMERA_TEMPLATE_PREVIEW:
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003210 id = RequestTemplate::PREVIEW;
3211 break;
Emilian Peevf4816702020-04-03 15:44:51 -07003212 case CAMERA_TEMPLATE_STILL_CAPTURE:
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003213 id = RequestTemplate::STILL_CAPTURE;
3214 break;
Emilian Peevf4816702020-04-03 15:44:51 -07003215 case CAMERA_TEMPLATE_VIDEO_RECORD:
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003216 id = RequestTemplate::VIDEO_RECORD;
3217 break;
Emilian Peevf4816702020-04-03 15:44:51 -07003218 case CAMERA_TEMPLATE_VIDEO_SNAPSHOT:
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003219 id = RequestTemplate::VIDEO_SNAPSHOT;
3220 break;
Emilian Peevf4816702020-04-03 15:44:51 -07003221 case CAMERA_TEMPLATE_ZERO_SHUTTER_LAG:
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003222 id = RequestTemplate::ZERO_SHUTTER_LAG;
3223 break;
Emilian Peevf4816702020-04-03 15:44:51 -07003224 case CAMERA_TEMPLATE_MANUAL:
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003225 id = RequestTemplate::MANUAL;
3226 break;
3227 default:
3228 // Unknown template ID, or this HAL is too old to support it
3229 return BAD_VALUE;
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003230 }
Eino-Ville Talvala96441462018-02-06 11:41:55 -08003231 err = mHidlSession->constructDefaultRequestSettings(id, requestCallback);
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003232
Emilian Peev31abd0a2017-05-11 18:37:46 +01003233 if (!err.isOk()) {
3234 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3235 res = DEAD_OBJECT;
3236 } else {
3237 res = CameraProviderManager::mapToStatusT(status);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003238 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003239
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003240 return res;
3241}
3242
Emilian Peev4ec17882019-01-24 17:16:58 -08003243bool Camera3Device::HalInterface::isReconfigurationRequired(CameraMetadata& oldSessionParams,
3244 CameraMetadata& newSessionParams) {
3245 // We do reconfiguration by default;
3246 bool ret = true;
3247 if ((mHidlSession_3_5 != nullptr) && mIsReconfigurationQuerySupported) {
3248 android::hardware::hidl_vec<uint8_t> oldParams, newParams;
3249 camera_metadata_t* oldSessioMeta = const_cast<camera_metadata_t*>(
3250 oldSessionParams.getAndLock());
3251 camera_metadata_t* newSessioMeta = const_cast<camera_metadata_t*>(
3252 newSessionParams.getAndLock());
3253 oldParams.setToExternal(reinterpret_cast<uint8_t*>(oldSessioMeta),
3254 get_camera_metadata_size(oldSessioMeta));
3255 newParams.setToExternal(reinterpret_cast<uint8_t*>(newSessioMeta),
3256 get_camera_metadata_size(newSessioMeta));
3257 hardware::camera::common::V1_0::Status callStatus;
3258 bool required;
3259 auto hidlCb = [&callStatus, &required] (hardware::camera::common::V1_0::Status s,
3260 bool requiredFlag) {
3261 callStatus = s;
3262 required = requiredFlag;
3263 };
3264 auto err = mHidlSession_3_5->isReconfigurationRequired(oldParams, newParams, hidlCb);
3265 oldSessionParams.unlock(oldSessioMeta);
3266 newSessionParams.unlock(newSessioMeta);
3267 if (err.isOk()) {
3268 switch (callStatus) {
3269 case hardware::camera::common::V1_0::Status::OK:
3270 ret = required;
3271 break;
3272 case hardware::camera::common::V1_0::Status::METHOD_NOT_SUPPORTED:
3273 mIsReconfigurationQuerySupported = false;
3274 ret = true;
3275 break;
3276 default:
3277 ALOGV("%s: Reconfiguration query failed: %d", __FUNCTION__, callStatus);
3278 ret = true;
3279 }
3280 } else {
3281 ALOGE("%s: Unexpected binder error: %s", __FUNCTION__, err.description().c_str());
3282 ret = true;
3283 }
3284 }
3285
3286 return ret;
3287}
3288
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003289status_t Camera3Device::HalInterface::configureStreams(const camera_metadata_t *sessionParams,
Emilian Peevf4816702020-04-03 15:44:51 -07003290 camera_stream_configuration *config, const std::vector<uint32_t>& bufferSizes) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003291 ATRACE_NAME("CameraHal::configureStreams");
3292 if (!valid()) return INVALID_OPERATION;
3293 status_t res = OK;
3294
Shuzhen Wang83bff122020-11-20 15:51:39 -08003295 if (config->input_is_multi_resolution && mHidlSession_3_7 == nullptr) {
3296 ALOGE("%s: Camera device doesn't support multi-resolution input stream", __FUNCTION__);
3297 return BAD_VALUE;
3298 }
3299
Emilian Peev31abd0a2017-05-11 18:37:46 +01003300 // Convert stream config to HIDL
3301 std::set<int> activeStreams;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003302 device::V3_2::StreamConfiguration requestedConfiguration3_2;
3303 device::V3_4::StreamConfiguration requestedConfiguration3_4;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003304 device::V3_7::StreamConfiguration requestedConfiguration3_7;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003305 requestedConfiguration3_2.streams.resize(config->num_streams);
3306 requestedConfiguration3_4.streams.resize(config->num_streams);
Shuzhen Wang83bff122020-11-20 15:51:39 -08003307 requestedConfiguration3_7.streams.resize(config->num_streams);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003308 for (size_t i = 0; i < config->num_streams; i++) {
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003309 device::V3_2::Stream &dst3_2 = requestedConfiguration3_2.streams[i];
3310 device::V3_4::Stream &dst3_4 = requestedConfiguration3_4.streams[i];
Shuzhen Wang83bff122020-11-20 15:51:39 -08003311 device::V3_7::Stream &dst3_7 = requestedConfiguration3_7.streams[i];
Emilian Peevf4816702020-04-03 15:44:51 -07003312 camera3::camera_stream_t *src = config->streams[i];
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003313
Emilian Peev31abd0a2017-05-11 18:37:46 +01003314 Camera3Stream* cam3stream = Camera3Stream::cast(src);
3315 cam3stream->setBufferFreedListener(this);
3316 int streamId = cam3stream->getId();
3317 StreamType streamType;
3318 switch (src->stream_type) {
Emilian Peevf4816702020-04-03 15:44:51 -07003319 case CAMERA_STREAM_OUTPUT:
Emilian Peev31abd0a2017-05-11 18:37:46 +01003320 streamType = StreamType::OUTPUT;
3321 break;
Emilian Peevf4816702020-04-03 15:44:51 -07003322 case CAMERA_STREAM_INPUT:
Emilian Peev31abd0a2017-05-11 18:37:46 +01003323 streamType = StreamType::INPUT;
3324 break;
3325 default:
3326 ALOGE("%s: Stream %d: Unsupported stream type %d",
3327 __FUNCTION__, streamId, config->streams[i]->stream_type);
3328 return BAD_VALUE;
3329 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003330 dst3_2.id = streamId;
3331 dst3_2.streamType = streamType;
3332 dst3_2.width = src->width;
3333 dst3_2.height = src->height;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003334 dst3_2.usage = mapToConsumerUsage(cam3stream->getUsage());
Emilian Peevf4816702020-04-03 15:44:51 -07003335 dst3_2.rotation = mapToStreamRotation((camera_stream_rotation_t) src->rotation);
Shuzhen Wang92653952019-05-07 15:11:43 -07003336 // For HidlSession version 3.5 or newer, the format and dataSpace sent
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08003337 // to HAL are original, not the overridden ones.
Shuzhen Wang92653952019-05-07 15:11:43 -07003338 if (mHidlSession_3_5 != nullptr) {
3339 dst3_2.format = mapToPixelFormat(cam3stream->isFormatOverridden() ?
3340 cam3stream->getOriginalFormat() : src->format);
3341 dst3_2.dataSpace = mapToHidlDataspace(cam3stream->isDataSpaceOverridden() ?
3342 cam3stream->getOriginalDataSpace() : src->data_space);
3343 } else {
3344 dst3_2.format = mapToPixelFormat(src->format);
3345 dst3_2.dataSpace = mapToHidlDataspace(src->data_space);
3346 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003347 dst3_4.v3_2 = dst3_2;
Emilian Peev192ee832018-01-31 14:46:47 +00003348 dst3_4.bufferSize = bufferSizes[i];
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003349 if (src->physical_camera_id != nullptr) {
3350 dst3_4.physicalCameraId = src->physical_camera_id;
3351 }
Shuzhen Wang83bff122020-11-20 15:51:39 -08003352 dst3_7.v3_4 = dst3_4;
3353 dst3_7.groupId = cam3stream->getHalStreamGroupId();
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08003354 dst3_7.sensorPixelModesUsed.resize(src->sensor_pixel_modes_used.size());
3355 size_t j = 0;
3356 for (int mode : src->sensor_pixel_modes_used) {
3357 dst3_7.sensorPixelModesUsed[j++] =
3358 static_cast<CameraMetadataEnumAndroidSensorPixelMode>(mode);
3359 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003360 activeStreams.insert(streamId);
3361 // Create Buffer ID map if necessary
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003362 mBufferRecords.tryCreateBufferCache(streamId);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003363 }
3364 // remove BufferIdMap for deleted streams
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003365 mBufferRecords.removeInactiveBufferCaches(activeStreams);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003366
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003367 StreamConfigurationMode operationMode;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003368 res = mapToStreamConfigurationMode(
Emilian Peevf4816702020-04-03 15:44:51 -07003369 (camera_stream_configuration_mode_t) config->operation_mode,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003370 /*out*/ &operationMode);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003371 if (res != OK) {
3372 return res;
3373 }
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003374 requestedConfiguration3_2.operationMode = operationMode;
3375 requestedConfiguration3_4.operationMode = operationMode;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08003376 requestedConfiguration3_7.operationMode = operationMode;
3377 size_t sessionParamSize = get_camera_metadata_size(sessionParams);
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003378 requestedConfiguration3_4.sessionParams.setToExternal(
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003379 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(sessionParams)),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08003380 sessionParamSize);
Shuzhen Wang83bff122020-11-20 15:51:39 -08003381 requestedConfiguration3_7.operationMode = operationMode;
3382 requestedConfiguration3_7.sessionParams.setToExternal(
3383 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(sessionParams)),
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08003384 sessionParamSize);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003385
Emilian Peev31abd0a2017-05-11 18:37:46 +01003386 // Invoke configureStreams
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003387 device::V3_3::HalStreamConfiguration finalConfiguration;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003388 device::V3_4::HalStreamConfiguration finalConfiguration3_4;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003389 device::V3_6::HalStreamConfiguration finalConfiguration3_6;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003390 common::V1_0::Status status;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003391
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003392 auto configStream34Cb = [&status, &finalConfiguration3_4]
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003393 (common::V1_0::Status s, const device::V3_4::HalStreamConfiguration& halConfiguration) {
3394 finalConfiguration3_4 = halConfiguration;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003395 status = s;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003396 };
3397
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003398 auto configStream36Cb = [&status, &finalConfiguration3_6]
3399 (common::V1_0::Status s, const device::V3_6::HalStreamConfiguration& halConfiguration) {
3400 finalConfiguration3_6 = halConfiguration;
3401 status = s;
3402 };
3403
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003404 auto postprocConfigStream34 = [&finalConfiguration, &finalConfiguration3_4]
3405 (hardware::Return<void>& err) -> status_t {
3406 if (!err.isOk()) {
3407 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3408 return DEAD_OBJECT;
3409 }
3410 finalConfiguration.streams.resize(finalConfiguration3_4.streams.size());
3411 for (size_t i = 0; i < finalConfiguration3_4.streams.size(); i++) {
3412 finalConfiguration.streams[i] = finalConfiguration3_4.streams[i].v3_3;
3413 }
3414 return OK;
3415 };
3416
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003417 auto postprocConfigStream36 = [&finalConfiguration, &finalConfiguration3_6]
3418 (hardware::Return<void>& err) -> status_t {
3419 if (!err.isOk()) {
3420 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3421 return DEAD_OBJECT;
3422 }
3423 finalConfiguration.streams.resize(finalConfiguration3_6.streams.size());
3424 for (size_t i = 0; i < finalConfiguration3_6.streams.size(); i++) {
3425 finalConfiguration.streams[i] = finalConfiguration3_6.streams[i].v3_4.v3_3;
3426 }
3427 return OK;
3428 };
3429
Shuzhen Wang92653952019-05-07 15:11:43 -07003430 // See which version of HAL we have
Shuzhen Wang83bff122020-11-20 15:51:39 -08003431 if (mHidlSession_3_7 != nullptr) {
3432 ALOGV("%s: v3.7 device found", __FUNCTION__);
3433 requestedConfiguration3_7.streamConfigCounter = mNextStreamConfigCounter++;
3434 requestedConfiguration3_7.multiResolutionInputImage = config->input_is_multi_resolution;
3435 auto err = mHidlSession_3_7->configureStreams_3_7(
3436 requestedConfiguration3_7, configStream36Cb);
3437 res = postprocConfigStream36(err);
3438 if (res != OK) {
3439 return res;
3440 }
3441 } else if (mHidlSession_3_6 != nullptr) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003442 ALOGV("%s: v3.6 device found", __FUNCTION__);
3443 device::V3_5::StreamConfiguration requestedConfiguration3_5;
3444 requestedConfiguration3_5.v3_4 = requestedConfiguration3_4;
3445 requestedConfiguration3_5.streamConfigCounter = mNextStreamConfigCounter++;
3446 auto err = mHidlSession_3_6->configureStreams_3_6(
3447 requestedConfiguration3_5, configStream36Cb);
3448 res = postprocConfigStream36(err);
3449 if (res != OK) {
3450 return res;
3451 }
3452 } else if (mHidlSession_3_5 != nullptr) {
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003453 ALOGV("%s: v3.5 device found", __FUNCTION__);
3454 device::V3_5::StreamConfiguration requestedConfiguration3_5;
3455 requestedConfiguration3_5.v3_4 = requestedConfiguration3_4;
3456 requestedConfiguration3_5.streamConfigCounter = mNextStreamConfigCounter++;
3457 auto err = mHidlSession_3_5->configureStreams_3_5(
3458 requestedConfiguration3_5, configStream34Cb);
3459 res = postprocConfigStream34(err);
3460 if (res != OK) {
3461 return res;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01003462 }
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003463 } else if (mHidlSession_3_4 != nullptr) {
3464 // We do; use v3.4 for the call
3465 ALOGV("%s: v3.4 device found", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003466 auto err = mHidlSession_3_4->configureStreams_3_4(
3467 requestedConfiguration3_4, configStream34Cb);
3468 res = postprocConfigStream34(err);
3469 if (res != OK) {
3470 return res;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003471 }
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003472 } else if (mHidlSession_3_3 != nullptr) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003473 // We do; use v3.3 for the call
3474 ALOGV("%s: v3.3 device found", __FUNCTION__);
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -08003475 auto err = mHidlSession_3_3->configureStreams_3_3(requestedConfiguration3_2,
Emilian Peev31abd0a2017-05-11 18:37:46 +01003476 [&status, &finalConfiguration]
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003477 (common::V1_0::Status s, const device::V3_3::HalStreamConfiguration& halConfiguration) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003478 finalConfiguration = halConfiguration;
3479 status = s;
3480 });
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003481 if (!err.isOk()) {
3482 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3483 return DEAD_OBJECT;
3484 }
3485 } else {
3486 // We don't; use v3.2 call and construct a v3.3 HalStreamConfiguration
3487 ALOGV("%s: v3.2 device found", __FUNCTION__);
3488 HalStreamConfiguration finalConfiguration_3_2;
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003489 auto err = mHidlSession->configureStreams(requestedConfiguration3_2,
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003490 [&status, &finalConfiguration_3_2]
3491 (common::V1_0::Status s, const HalStreamConfiguration& halConfiguration) {
3492 finalConfiguration_3_2 = halConfiguration;
3493 status = s;
3494 });
3495 if (!err.isOk()) {
3496 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
3497 return DEAD_OBJECT;
3498 }
3499 finalConfiguration.streams.resize(finalConfiguration_3_2.streams.size());
3500 for (size_t i = 0; i < finalConfiguration_3_2.streams.size(); i++) {
3501 finalConfiguration.streams[i].v3_2 = finalConfiguration_3_2.streams[i];
3502 finalConfiguration.streams[i].overrideDataSpace =
Shuzhen Wangc28189a2017-11-27 23:05:10 -08003503 requestedConfiguration3_2.streams[i].dataSpace;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003504 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003505 }
3506
3507 if (status != common::V1_0::Status::OK ) {
3508 return CameraProviderManager::mapToStatusT(status);
3509 }
3510
3511 // And convert output stream configuration from HIDL
3512
3513 for (size_t i = 0; i < config->num_streams; i++) {
Emilian Peevf4816702020-04-03 15:44:51 -07003514 camera3::camera_stream_t *dst = config->streams[i];
Emilian Peev31abd0a2017-05-11 18:37:46 +01003515 int streamId = Camera3Stream::cast(dst)->getId();
3516
3517 // Start scan at i, with the assumption that the stream order matches
3518 size_t realIdx = i;
3519 bool found = false;
Eino-Ville Talvala370875f2019-04-12 12:40:27 -07003520 size_t halStreamCount = finalConfiguration.streams.size();
3521 for (size_t idx = 0; idx < halStreamCount; idx++) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003522 if (finalConfiguration.streams[realIdx].v3_2.id == streamId) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003523 found = true;
3524 break;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003525 }
Eino-Ville Talvala370875f2019-04-12 12:40:27 -07003526 realIdx = (realIdx >= halStreamCount - 1) ? 0 : realIdx + 1;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003527 }
3528 if (!found) {
3529 ALOGE("%s: Stream %d not found in stream configuration response from HAL",
3530 __FUNCTION__, streamId);
3531 return INVALID_OPERATION;
3532 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003533 device::V3_3::HalStream &src = finalConfiguration.streams[realIdx];
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003534 device::V3_6::HalStream &src_36 = finalConfiguration3_6.streams[realIdx];
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003535
Emilian Peev710c1422017-08-30 11:19:38 +01003536 Camera3Stream* dstStream = Camera3Stream::cast(dst);
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003537 int overrideFormat = mapToFrameworkFormat(src.v3_2.overrideFormat);
3538 android_dataspace overrideDataSpace = mapToFrameworkDataspace(src.overrideDataSpace);
3539
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003540 if (mHidlSession_3_6 != nullptr) {
3541 dstStream->setOfflineProcessingSupport(src_36.supportOffline);
3542 }
3543
Yin-Chia Yeh90667662019-07-01 15:45:00 -07003544 if (dstStream->getOriginalFormat() != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Shuzhen Wang92653952019-05-07 15:11:43 -07003545 dstStream->setFormatOverride(false);
3546 dstStream->setDataSpaceOverride(false);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003547 if (dst->format != overrideFormat) {
3548 ALOGE("%s: Stream %d: Format override not allowed for format 0x%x", __FUNCTION__,
3549 streamId, dst->format);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003550 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003551 if (dst->data_space != overrideDataSpace) {
3552 ALOGE("%s: Stream %d: DataSpace override not allowed for format 0x%x", __FUNCTION__,
3553 streamId, dst->format);
3554 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003555 } else {
Shuzhen Wang92653952019-05-07 15:11:43 -07003556 bool needFormatOverride =
3557 requestedConfiguration3_2.streams[i].format != src.v3_2.overrideFormat;
3558 bool needDataspaceOverride =
3559 requestedConfiguration3_2.streams[i].dataSpace != src.overrideDataSpace;
Emilian Peev31abd0a2017-05-11 18:37:46 +01003560 // Override allowed with IMPLEMENTATION_DEFINED
Shuzhen Wang92653952019-05-07 15:11:43 -07003561 dstStream->setFormatOverride(needFormatOverride);
3562 dstStream->setDataSpaceOverride(needDataspaceOverride);
Emilian Peev31abd0a2017-05-11 18:37:46 +01003563 dst->format = overrideFormat;
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003564 dst->data_space = overrideDataSpace;
Yin-Chia Yeh77327052017-01-09 18:23:07 -08003565 }
3566
Emilian Peevf4816702020-04-03 15:44:51 -07003567 if (dst->stream_type == CAMERA_STREAM_INPUT) {
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003568 if (src.v3_2.producerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003569 ALOGE("%s: Stream %d: INPUT streams must have 0 for producer usage",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003570 __FUNCTION__, streamId);
3571 return INVALID_OPERATION;
3572 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003573 dstStream->setUsage(
3574 mapConsumerToFrameworkUsage(src.v3_2.consumerUsage));
Emilian Peev31abd0a2017-05-11 18:37:46 +01003575 } else {
3576 // OUTPUT
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003577 if (src.v3_2.consumerUsage != 0) {
Emilian Peev31abd0a2017-05-11 18:37:46 +01003578 ALOGE("%s: Stream %d: OUTPUT streams must have 0 for consumer usage",
3579 __FUNCTION__, streamId);
3580 return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003581 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003582 dstStream->setUsage(
3583 mapProducerToFrameworkUsage(src.v3_2.producerUsage));
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003584 }
Eino-Ville Talvala91cd3f82017-08-21 16:12:50 -07003585 dst->max_buffers = src.v3_2.maxBuffers;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003586 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01003587
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003588 return res;
3589}
3590
Cliff Wuc2ad9c82021-04-21 00:58:58 +08003591status_t Camera3Device::HalInterface::configureInjectedStreams(
3592 const camera_metadata_t* sessionParams, camera_stream_configuration* config,
3593 const std::vector<uint32_t>& bufferSizes,
3594 const CameraMetadata& cameraCharacteristics) {
3595 ATRACE_NAME("InjectionCameraHal::configureStreams");
3596 if (!valid()) return INVALID_OPERATION;
3597 status_t res = OK;
3598
3599 if (config->input_is_multi_resolution) {
3600 ALOGE("%s: Injection camera device doesn't support multi-resolution input "
3601 "stream", __FUNCTION__);
3602 return BAD_VALUE;
3603 }
3604
3605 // Convert stream config to HIDL
3606 std::set<int> activeStreams;
3607 device::V3_2::StreamConfiguration requestedConfiguration3_2;
3608 device::V3_4::StreamConfiguration requestedConfiguration3_4;
3609 device::V3_7::StreamConfiguration requestedConfiguration3_7;
3610 requestedConfiguration3_2.streams.resize(config->num_streams);
3611 requestedConfiguration3_4.streams.resize(config->num_streams);
3612 requestedConfiguration3_7.streams.resize(config->num_streams);
3613 for (size_t i = 0; i < config->num_streams; i++) {
3614 device::V3_2::Stream& dst3_2 = requestedConfiguration3_2.streams[i];
3615 device::V3_4::Stream& dst3_4 = requestedConfiguration3_4.streams[i];
3616 device::V3_7::Stream& dst3_7 = requestedConfiguration3_7.streams[i];
3617 camera3::camera_stream_t* src = config->streams[i];
3618
3619 Camera3Stream* cam3stream = Camera3Stream::cast(src);
3620 cam3stream->setBufferFreedListener(this);
3621 int streamId = cam3stream->getId();
3622 StreamType streamType;
3623 switch (src->stream_type) {
3624 case CAMERA_STREAM_OUTPUT:
3625 streamType = StreamType::OUTPUT;
3626 break;
3627 case CAMERA_STREAM_INPUT:
3628 streamType = StreamType::INPUT;
3629 break;
3630 default:
3631 ALOGE("%s: Stream %d: Unsupported stream type %d", __FUNCTION__,
3632 streamId, config->streams[i]->stream_type);
3633 return BAD_VALUE;
3634 }
3635 dst3_2.id = streamId;
3636 dst3_2.streamType = streamType;
3637 dst3_2.width = src->width;
3638 dst3_2.height = src->height;
3639 dst3_2.usage = mapToConsumerUsage(cam3stream->getUsage());
3640 dst3_2.rotation =
3641 mapToStreamRotation((camera_stream_rotation_t)src->rotation);
3642 // For HidlSession version 3.5 or newer, the format and dataSpace sent
3643 // to HAL are original, not the overridden ones.
3644 if (mHidlSession_3_5 != nullptr) {
3645 dst3_2.format = mapToPixelFormat(cam3stream->isFormatOverridden()
3646 ? cam3stream->getOriginalFormat()
3647 : src->format);
3648 dst3_2.dataSpace =
3649 mapToHidlDataspace(cam3stream->isDataSpaceOverridden()
3650 ? cam3stream->getOriginalDataSpace()
3651 : src->data_space);
3652 } else {
3653 dst3_2.format = mapToPixelFormat(src->format);
3654 dst3_2.dataSpace = mapToHidlDataspace(src->data_space);
3655 }
3656 dst3_4.v3_2 = dst3_2;
3657 dst3_4.bufferSize = bufferSizes[i];
3658 if (src->physical_camera_id != nullptr) {
3659 dst3_4.physicalCameraId = src->physical_camera_id;
3660 }
3661 dst3_7.v3_4 = dst3_4;
3662 dst3_7.groupId = cam3stream->getHalStreamGroupId();
3663 dst3_7.sensorPixelModesUsed.resize(src->sensor_pixel_modes_used.size());
3664 size_t j = 0;
3665 for (int mode : src->sensor_pixel_modes_used) {
3666 dst3_7.sensorPixelModesUsed[j++] =
3667 static_cast<CameraMetadataEnumAndroidSensorPixelMode>(mode);
3668 }
3669 activeStreams.insert(streamId);
3670 // Create Buffer ID map if necessary
3671 mBufferRecords.tryCreateBufferCache(streamId);
3672 }
3673 // remove BufferIdMap for deleted streams
3674 mBufferRecords.removeInactiveBufferCaches(activeStreams);
3675
3676 StreamConfigurationMode operationMode;
3677 res = mapToStreamConfigurationMode(
3678 (camera_stream_configuration_mode_t)config->operation_mode,
3679 /*out*/ &operationMode);
3680 if (res != OK) {
3681 return res;
3682 }
3683 requestedConfiguration3_7.operationMode = operationMode;
3684 size_t sessionParamSize = get_camera_metadata_size(sessionParams);
3685 requestedConfiguration3_7.operationMode = operationMode;
3686 requestedConfiguration3_7.sessionParams.setToExternal(
3687 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(sessionParams)),
3688 sessionParamSize);
3689
3690 // See which version of HAL we have
3691 if (mHidlSession_3_7 != nullptr) {
3692 requestedConfiguration3_7.streamConfigCounter = mNextStreamConfigCounter++;
3693 requestedConfiguration3_7.multiResolutionInputImage =
3694 config->input_is_multi_resolution;
3695
3696 const camera_metadata_t* rawMetadata = cameraCharacteristics.getAndLock();
3697 ::android::hardware::camera::device::V3_2::CameraMetadata hidlChars = {};
3698 hidlChars.setToExternal(
3699 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(rawMetadata)),
3700 get_camera_metadata_size(rawMetadata));
3701 cameraCharacteristics.unlock(rawMetadata);
3702
3703 sp<hardware::camera::device::V3_7::ICameraInjectionSession>
3704 hidlInjectionSession_3_7;
3705 auto castInjectionResult_3_7 =
3706 device::V3_7::ICameraInjectionSession::castFrom(mHidlSession_3_7);
3707 if (castInjectionResult_3_7.isOk()) {
3708 hidlInjectionSession_3_7 = castInjectionResult_3_7;
3709 } else {
3710 ALOGE("%s: Transaction error: %s", __FUNCTION__,
3711 castInjectionResult_3_7.description().c_str());
3712 return DEAD_OBJECT;
3713 }
3714
3715 auto err = hidlInjectionSession_3_7->configureInjectionStreams(
3716 requestedConfiguration3_7, hidlChars);
3717 if (!err.isOk()) {
3718 ALOGE("%s: Transaction error: %s", __FUNCTION__,
3719 err.description().c_str());
3720 return DEAD_OBJECT;
3721 }
3722 } else {
3723 ALOGE("%s: mHidlSession_3_7 does not exist, the lowest version of injection "
3724 "session is 3.7", __FUNCTION__);
3725 return DEAD_OBJECT;
3726 }
3727
3728 return res;
3729}
3730
Emilian Peevf4816702020-04-03 15:44:51 -07003731status_t Camera3Device::HalInterface::wrapAsHidlRequest(camera_capture_request_t* request,
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003732 /*out*/device::V3_2::CaptureRequest* captureRequest,
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003733 /*out*/std::vector<native_handle_t*>* handlesCreated,
3734 /*out*/std::vector<std::pair<int32_t, int32_t>>* inflightBuffers) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003735 ATRACE_CALL();
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003736 if (captureRequest == nullptr || handlesCreated == nullptr || inflightBuffers == nullptr) {
3737 ALOGE("%s: captureRequest (%p), handlesCreated (%p), and inflightBuffers(%p) "
3738 "must not be null", __FUNCTION__, captureRequest, handlesCreated, inflightBuffers);
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003739 return BAD_VALUE;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003740 }
3741
3742 captureRequest->frameNumber = request->frame_number;
Yifan Hongf79b5542017-04-11 14:44:25 -07003743
3744 captureRequest->fmqSettingsSize = 0;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003745
3746 {
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003747 if (request->input_buffer != nullptr) {
3748 int32_t streamId = Camera3Stream::cast(request->input_buffer->stream)->getId();
3749 buffer_handle_t buf = *(request->input_buffer->buffer);
3750 auto pair = getBufferId(buf, streamId);
3751 bool isNewBuffer = pair.first;
3752 uint64_t bufferId = pair.second;
3753 captureRequest->inputBuffer.streamId = streamId;
3754 captureRequest->inputBuffer.bufferId = bufferId;
3755 captureRequest->inputBuffer.buffer = (isNewBuffer) ? buf : nullptr;
3756 captureRequest->inputBuffer.status = BufferStatus::OK;
3757 native_handle_t *acquireFence = nullptr;
3758 if (request->input_buffer->acquire_fence != -1) {
3759 acquireFence = native_handle_create(1,0);
3760 acquireFence->data[0] = request->input_buffer->acquire_fence;
3761 handlesCreated->push_back(acquireFence);
3762 }
3763 captureRequest->inputBuffer.acquireFence = acquireFence;
3764 captureRequest->inputBuffer.releaseFence = nullptr;
3765
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003766 mBufferRecords.pushInflightBuffer(captureRequest->frameNumber, streamId,
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003767 request->input_buffer->buffer);
3768 inflightBuffers->push_back(std::make_pair(captureRequest->frameNumber, streamId));
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003769 } else {
3770 captureRequest->inputBuffer.streamId = -1;
3771 captureRequest->inputBuffer.bufferId = BUFFER_ID_NO_BUFFER;
3772 }
3773
3774 captureRequest->outputBuffers.resize(request->num_output_buffers);
3775 for (size_t i = 0; i < request->num_output_buffers; i++) {
Emilian Peevf4816702020-04-03 15:44:51 -07003776 const camera_stream_buffer_t *src = request->output_buffers + i;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003777 StreamBuffer &dst = captureRequest->outputBuffers[i];
3778 int32_t streamId = Camera3Stream::cast(src->stream)->getId();
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003779 if (src->buffer != nullptr) {
3780 buffer_handle_t buf = *(src->buffer);
3781 auto pair = getBufferId(buf, streamId);
3782 bool isNewBuffer = pair.first;
3783 dst.bufferId = pair.second;
3784 dst.buffer = isNewBuffer ? buf : nullptr;
3785 native_handle_t *acquireFence = nullptr;
3786 if (src->acquire_fence != -1) {
3787 acquireFence = native_handle_create(1,0);
3788 acquireFence->data[0] = src->acquire_fence;
3789 handlesCreated->push_back(acquireFence);
3790 }
3791 dst.acquireFence = acquireFence;
3792 } else if (mUseHalBufManager) {
3793 // HAL buffer management path
3794 dst.bufferId = BUFFER_ID_NO_BUFFER;
3795 dst.buffer = nullptr;
3796 dst.acquireFence = nullptr;
3797 } else {
3798 ALOGE("%s: cannot send a null buffer in capture request!", __FUNCTION__);
3799 return BAD_VALUE;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003800 }
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003801 dst.streamId = streamId;
3802 dst.status = BufferStatus::OK;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003803 dst.releaseFence = nullptr;
3804
Yin-Chia Yeh5f840f82019-03-05 11:59:04 -08003805 // Output buffers are empty when using HAL buffer manager
3806 if (!mUseHalBufManager) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003807 mBufferRecords.pushInflightBuffer(
3808 captureRequest->frameNumber, streamId, src->buffer);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003809 inflightBuffers->push_back(std::make_pair(captureRequest->frameNumber, streamId));
Yin-Chia Yeh5f840f82019-03-05 11:59:04 -08003810 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003811 }
3812 }
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003813 return OK;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003814}
3815
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003816void Camera3Device::HalInterface::cleanupNativeHandles(
3817 std::vector<native_handle_t*> *handles, bool closeFd) {
3818 if (handles == nullptr) {
3819 return;
3820 }
3821 if (closeFd) {
3822 for (auto& handle : *handles) {
3823 native_handle_close(handle);
3824 }
3825 }
3826 for (auto& handle : *handles) {
3827 native_handle_delete(handle);
3828 }
3829 handles->clear();
3830 return;
3831}
3832
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003833status_t Camera3Device::HalInterface::processBatchCaptureRequests(
Emilian Peevf4816702020-04-03 15:44:51 -07003834 std::vector<camera_capture_request_t*>& requests,/*out*/uint32_t* numRequestProcessed) {
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003835 ATRACE_NAME("CameraHal::processBatchCaptureRequests");
3836 if (!valid()) return INVALID_OPERATION;
3837
Emilian Peevaebbe412018-01-15 13:53:24 +00003838 sp<device::V3_4::ICameraDeviceSession> hidlSession_3_4;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003839 sp<device::V3_7::ICameraDeviceSession> hidlSession_3_7;
3840 auto castResult_3_7 = device::V3_7::ICameraDeviceSession::castFrom(mHidlSession);
3841 if (castResult_3_7.isOk()) {
3842 hidlSession_3_7 = castResult_3_7;
3843 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003844 auto castResult_3_4 = device::V3_4::ICameraDeviceSession::castFrom(mHidlSession);
3845 if (castResult_3_4.isOk()) {
3846 hidlSession_3_4 = castResult_3_4;
3847 }
3848
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003849 hardware::hidl_vec<device::V3_2::CaptureRequest> captureRequests;
Emilian Peevaebbe412018-01-15 13:53:24 +00003850 hardware::hidl_vec<device::V3_4::CaptureRequest> captureRequests_3_4;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003851 hardware::hidl_vec<device::V3_7::CaptureRequest> captureRequests_3_7;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003852 size_t batchSize = requests.size();
Shuzhen Wang83bff122020-11-20 15:51:39 -08003853 if (hidlSession_3_7 != nullptr) {
3854 captureRequests_3_7.resize(batchSize);
3855 } else if (hidlSession_3_4 != nullptr) {
Emilian Peevaebbe412018-01-15 13:53:24 +00003856 captureRequests_3_4.resize(batchSize);
3857 } else {
3858 captureRequests.resize(batchSize);
3859 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003860 std::vector<native_handle_t*> handlesCreated;
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003861 std::vector<std::pair<int32_t, int32_t>> inflightBuffers;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003862
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003863 status_t res = OK;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003864 for (size_t i = 0; i < batchSize; i++) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08003865 if (hidlSession_3_7 != nullptr) {
3866 res = wrapAsHidlRequest(requests[i], /*out*/&captureRequests_3_7[i].v3_4.v3_2,
3867 /*out*/&handlesCreated, /*out*/&inflightBuffers);
3868 } else if (hidlSession_3_4 != nullptr) {
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003869 res = wrapAsHidlRequest(requests[i], /*out*/&captureRequests_3_4[i].v3_2,
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003870 /*out*/&handlesCreated, /*out*/&inflightBuffers);
Emilian Peevaebbe412018-01-15 13:53:24 +00003871 } else {
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003872 res = wrapAsHidlRequest(requests[i], /*out*/&captureRequests[i],
3873 /*out*/&handlesCreated, /*out*/&inflightBuffers);
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003874 }
3875 if (res != OK) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003876 mBufferRecords.popInflightBuffers(inflightBuffers);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003877 cleanupNativeHandles(&handlesCreated);
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -08003878 return res;
Emilian Peevaebbe412018-01-15 13:53:24 +00003879 }
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003880 }
3881
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003882 std::vector<device::V3_2::BufferCache> cachesToRemove;
3883 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003884 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003885 for (auto& pair : mFreedBuffers) {
3886 // The stream might have been removed since onBufferFreed
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003887 if (mBufferRecords.isStreamCached(pair.first)) {
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07003888 cachesToRemove.push_back({pair.first, pair.second});
3889 }
3890 }
3891 mFreedBuffers.clear();
3892 }
3893
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003894 common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
3895 *numRequestProcessed = 0;
Yifan Hongf79b5542017-04-11 14:44:25 -07003896
3897 // Write metadata to FMQ.
3898 for (size_t i = 0; i < batchSize; i++) {
Emilian Peevf4816702020-04-03 15:44:51 -07003899 camera_capture_request_t* request = requests[i];
Emilian Peevaebbe412018-01-15 13:53:24 +00003900 device::V3_2::CaptureRequest* captureRequest;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003901 if (hidlSession_3_7 != nullptr) {
3902 captureRequest = &captureRequests_3_7[i].v3_4.v3_2;
3903 } else if (hidlSession_3_4 != nullptr) {
Emilian Peevaebbe412018-01-15 13:53:24 +00003904 captureRequest = &captureRequests_3_4[i].v3_2;
3905 } else {
3906 captureRequest = &captureRequests[i];
3907 }
Yifan Hongf79b5542017-04-11 14:44:25 -07003908
3909 if (request->settings != nullptr) {
3910 size_t settingsSize = get_camera_metadata_size(request->settings);
3911 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
3912 reinterpret_cast<const uint8_t*>(request->settings), settingsSize)) {
3913 captureRequest->settings.resize(0);
3914 captureRequest->fmqSettingsSize = settingsSize;
3915 } else {
3916 if (mRequestMetadataQueue != nullptr) {
3917 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
3918 }
3919 captureRequest->settings.setToExternal(
3920 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(request->settings)),
3921 get_camera_metadata_size(request->settings));
3922 captureRequest->fmqSettingsSize = 0u;
3923 }
3924 } else {
3925 // A null request settings maps to a size-0 CameraMetadata
3926 captureRequest->settings.resize(0);
3927 captureRequest->fmqSettingsSize = 0u;
3928 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003929
Shuzhen Wang83bff122020-11-20 15:51:39 -08003930 // hidl session 3.7 specific handling.
3931 if (hidlSession_3_7 != nullptr) {
3932 captureRequests_3_7[i].inputWidth = request->input_width;
3933 captureRequests_3_7[i].inputHeight = request->input_height;
3934 }
3935
3936 // hidl session 3.7 and 3.4 specific handling.
3937 if (hidlSession_3_7 != nullptr || hidlSession_3_4 != nullptr) {
3938 hardware::hidl_vec<device::V3_4::PhysicalCameraSetting>& physicalCameraSettings =
3939 (hidlSession_3_7 != nullptr) ?
3940 captureRequests_3_7[i].v3_4.physicalCameraSettings :
3941 captureRequests_3_4[i].physicalCameraSettings;
3942 physicalCameraSettings.resize(request->num_physcam_settings);
Emilian Peevaebbe412018-01-15 13:53:24 +00003943 for (size_t j = 0; j < request->num_physcam_settings; j++) {
Emilian Peev00420d22018-02-05 21:33:13 +00003944 if (request->physcam_settings != nullptr) {
3945 size_t settingsSize = get_camera_metadata_size(request->physcam_settings[j]);
3946 if (mRequestMetadataQueue != nullptr && mRequestMetadataQueue->write(
3947 reinterpret_cast<const uint8_t*>(request->physcam_settings[j]),
3948 settingsSize)) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08003949 physicalCameraSettings[j].settings.resize(0);
3950 physicalCameraSettings[j].fmqSettingsSize = settingsSize;
Emilian Peev00420d22018-02-05 21:33:13 +00003951 } else {
3952 if (mRequestMetadataQueue != nullptr) {
3953 ALOGW("%s: couldn't utilize fmq, fallback to hwbinder", __FUNCTION__);
3954 }
Shuzhen Wang83bff122020-11-20 15:51:39 -08003955 physicalCameraSettings[j].settings.setToExternal(
Emilian Peev00420d22018-02-05 21:33:13 +00003956 reinterpret_cast<uint8_t*>(const_cast<camera_metadata_t*>(
3957 request->physcam_settings[j])),
3958 get_camera_metadata_size(request->physcam_settings[j]));
Shuzhen Wang83bff122020-11-20 15:51:39 -08003959 physicalCameraSettings[j].fmqSettingsSize = 0u;
Emilian Peevaebbe412018-01-15 13:53:24 +00003960 }
Emilian Peev00420d22018-02-05 21:33:13 +00003961 } else {
Yin-Chia Yeha2849702021-03-10 10:11:33 -08003962 physicalCameraSettings[j].fmqSettingsSize = 0u;
3963 physicalCameraSettings[j].settings.resize(0);
Emilian Peevaebbe412018-01-15 13:53:24 +00003964 }
Shuzhen Wang83bff122020-11-20 15:51:39 -08003965 physicalCameraSettings[j].physicalCameraId = request->physcam_id[j];
Emilian Peevaebbe412018-01-15 13:53:24 +00003966 }
3967 }
Yifan Hongf79b5542017-04-11 14:44:25 -07003968 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003969
3970 hardware::details::return_status err;
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07003971 auto resultCallback =
3972 [&status, &numRequestProcessed] (auto s, uint32_t n) {
3973 status = s;
3974 *numRequestProcessed = n;
3975 };
Shuzhen Wang83bff122020-11-20 15:51:39 -08003976 if (hidlSession_3_7 != nullptr) {
3977 err = hidlSession_3_7->processCaptureRequest_3_7(captureRequests_3_7, cachesToRemove,
3978 resultCallback);
3979 } else if (hidlSession_3_4 != nullptr) {
Emilian Peevaebbe412018-01-15 13:53:24 +00003980 err = hidlSession_3_4->processCaptureRequest_3_4(captureRequests_3_4, cachesToRemove,
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07003981 resultCallback);
Emilian Peevaebbe412018-01-15 13:53:24 +00003982 } else {
3983 err = mHidlSession->processCaptureRequest(captureRequests, cachesToRemove,
Jayant Chowdharyc8d581e2018-07-16 14:46:23 -07003984 resultCallback);
Emilian Peevaebbe412018-01-15 13:53:24 +00003985 }
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003986 if (!err.isOk()) {
3987 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003988 status = common::V1_0::Status::CAMERA_DISCONNECTED;
Eino-Ville Talvalac5cbb872017-04-27 12:48:33 -07003989 }
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003990
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003991 if (status == common::V1_0::Status::OK && *numRequestProcessed != batchSize) {
3992 ALOGE("%s: processCaptureRequest returns OK but processed %d/%zu requests",
3993 __FUNCTION__, *numRequestProcessed, batchSize);
3994 status = common::V1_0::Status::INTERNAL_ERROR;
3995 }
3996
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07003997 res = CameraProviderManager::mapToStatusT(status);
3998 if (res == OK) {
3999 if (mHidlSession->isRemote()) {
4000 // Only close acquire fence FDs when the HIDL transaction succeeds (so the FDs have been
4001 // sent to camera HAL processes)
4002 cleanupNativeHandles(&handlesCreated, /*closeFd*/true);
4003 } else {
4004 // In passthrough mode the FDs are now owned by HAL
4005 cleanupNativeHandles(&handlesCreated);
4006 }
4007 } else {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004008 mBufferRecords.popInflightBuffers(inflightBuffers);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004009 cleanupNativeHandles(&handlesCreated);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004010 }
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004011 return res;
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004012}
4013
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004014status_t Camera3Device::HalInterface::flush() {
4015 ATRACE_NAME("CameraHal::flush");
4016 if (!valid()) return INVALID_OPERATION;
4017 status_t res = OK;
4018
Emilian Peev31abd0a2017-05-11 18:37:46 +01004019 auto err = mHidlSession->flush();
4020 if (!err.isOk()) {
4021 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4022 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004023 } else {
Emilian Peev31abd0a2017-05-11 18:37:46 +01004024 res = CameraProviderManager::mapToStatusT(err);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004025 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004026
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004027 return res;
4028}
4029
Emilian Peev31abd0a2017-05-11 18:37:46 +01004030status_t Camera3Device::HalInterface::dump(int /*fd*/) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004031 ATRACE_NAME("CameraHal::dump");
4032 if (!valid()) return INVALID_OPERATION;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004033
Emilian Peev31abd0a2017-05-11 18:37:46 +01004034 // Handled by CameraProviderManager::dump
4035
4036 return OK;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004037}
4038
4039status_t Camera3Device::HalInterface::close() {
4040 ATRACE_NAME("CameraHal::close()");
4041 if (!valid()) return INVALID_OPERATION;
4042 status_t res = OK;
4043
Emilian Peev31abd0a2017-05-11 18:37:46 +01004044 auto err = mHidlSession->close();
4045 // Interface will be dead shortly anyway, so don't log errors
4046 if (!err.isOk()) {
4047 res = DEAD_OBJECT;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004048 }
Emilian Peev31abd0a2017-05-11 18:37:46 +01004049
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004050 return res;
4051}
4052
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004053void Camera3Device::HalInterface::signalPipelineDrain(const std::vector<int>& streamIds) {
4054 ATRACE_NAME("CameraHal::signalPipelineDrain");
4055 if (!valid() || mHidlSession_3_5 == nullptr) {
4056 ALOGE("%s called on invalid camera!", __FUNCTION__);
4057 return;
4058 }
4059
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004060 auto err = mHidlSession_3_5->signalStreamFlush(streamIds, mNextStreamConfigCounter - 1);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004061 if (!err.isOk()) {
4062 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4063 return;
4064 }
4065}
4066
Yin-Chia Yehb978c382019-10-30 00:22:37 -07004067status_t Camera3Device::HalInterface::switchToOffline(
4068 const std::vector<int32_t>& streamsToKeep,
4069 /*out*/hardware::camera::device::V3_6::CameraOfflineSessionInfo* offlineSessionInfo,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004070 /*out*/sp<hardware::camera::device::V3_6::ICameraOfflineSession>* offlineSession,
4071 /*out*/camera3::BufferRecords* bufferRecords) {
Yin-Chia Yehb978c382019-10-30 00:22:37 -07004072 ATRACE_NAME("CameraHal::switchToOffline");
4073 if (!valid() || mHidlSession_3_6 == nullptr) {
4074 ALOGE("%s called on invalid camera!", __FUNCTION__);
4075 return INVALID_OPERATION;
4076 }
4077
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004078 if (offlineSessionInfo == nullptr || offlineSession == nullptr || bufferRecords == nullptr) {
4079 ALOGE("%s: output arguments must not be null!", __FUNCTION__);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07004080 return INVALID_OPERATION;
4081 }
4082
4083 common::V1_0::Status status = common::V1_0::Status::INTERNAL_ERROR;
Yin-Chia Yehb978c382019-10-30 00:22:37 -07004084 auto resultCallback =
4085 [&status, &offlineSessionInfo, &offlineSession] (auto s, auto info, auto session) {
4086 status = s;
4087 *offlineSessionInfo = info;
4088 *offlineSession = session;
4089 };
4090 auto err = mHidlSession_3_6->switchToOffline(streamsToKeep, resultCallback);
4091
4092 if (!err.isOk()) {
4093 ALOGE("%s: Transaction error: %s", __FUNCTION__, err.description().c_str());
4094 return DEAD_OBJECT;
4095 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004096
4097 status_t ret = CameraProviderManager::mapToStatusT(status);
4098 if (ret != OK) {
4099 return ret;
4100 }
4101
4102 // TODO: assert no ongoing requestBuffer/returnBuffer call here
4103 // TODO: update RequestBufferStateMachine to block requestBuffer/returnBuffer once HAL
4104 // returns from switchToOffline.
4105
4106
4107 // Validate buffer caches
4108 std::vector<int32_t> streams;
4109 streams.reserve(offlineSessionInfo->offlineStreams.size());
4110 for (auto offlineStream : offlineSessionInfo->offlineStreams) {
4111 int32_t id = offlineStream.id;
4112 streams.push_back(id);
4113 // Verify buffer caches
4114 std::vector<uint64_t> bufIds(offlineStream.circulatingBufferIds.begin(),
4115 offlineStream.circulatingBufferIds.end());
4116 if (!verifyBufferIds(id, bufIds)) {
4117 ALOGE("%s: stream ID %d buffer cache records mismatch!", __FUNCTION__, id);
4118 return UNKNOWN_ERROR;
4119 }
4120 }
4121
4122 // Move buffer records
4123 bufferRecords->takeBufferCaches(mBufferRecords, streams);
4124 bufferRecords->takeInflightBufferMap(mBufferRecords);
4125 bufferRecords->takeRequestedBufferMap(mBufferRecords);
4126 return ret;
Yin-Chia Yehb978c382019-10-30 00:22:37 -07004127}
4128
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07004129void Camera3Device::HalInterface::getInflightBufferKeys(
4130 std::vector<std::pair<int32_t, int32_t>>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004131 mBufferRecords.getInflightBufferKeys(out);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07004132 return;
4133}
4134
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08004135void Camera3Device::HalInterface::getInflightRequestBufferKeys(
4136 std::vector<uint64_t>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004137 mBufferRecords.getInflightRequestBufferKeys(out);
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08004138 return;
4139}
4140
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004141bool Camera3Device::HalInterface::verifyBufferIds(
4142 int32_t streamId, std::vector<uint64_t>& bufIds) {
4143 return mBufferRecords.verifyBufferIds(streamId, bufIds);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004144}
4145
4146status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08004147 int32_t frameNumber, int32_t streamId,
4148 /*out*/ buffer_handle_t **buffer) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004149 return mBufferRecords.popInflightBuffer(frameNumber, streamId, buffer);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07004150}
4151
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004152status_t Camera3Device::HalInterface::pushInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08004153 uint64_t bufferId, buffer_handle_t* buf, int32_t streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004154 return mBufferRecords.pushInflightRequestBuffer(bufferId, buf, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004155}
4156
4157// Find and pop a buffer_handle_t based on bufferId
4158status_t Camera3Device::HalInterface::popInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08004159 uint64_t bufferId,
4160 /*out*/ buffer_handle_t** buffer,
4161 /*optional out*/ int32_t* streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004162 return mBufferRecords.popInflightRequestBuffer(bufferId, buffer, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004163}
4164
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004165std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
4166 const buffer_handle_t& buf, int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004167 return mBufferRecords.getBufferId(buf, streamId);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08004168}
4169
Shuzhen Wangcd5b1822021-09-07 11:52:48 -07004170uint64_t Camera3Device::HalInterface::removeOneBufferCache(int streamId,
4171 const native_handle_t* handle) {
4172 return mBufferRecords.removeOneBufferCache(streamId, handle);
4173}
4174
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07004175void Camera3Device::HalInterface::onBufferFreed(
4176 int streamId, const native_handle_t* handle) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004177 uint32_t bufferId = mBufferRecords.removeOneBufferCache(streamId, handle);
4178 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
4179 if (bufferId != BUFFER_ID_NO_BUFFER) {
4180 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07004181 }
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07004182}
4183
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07004184void Camera3Device::HalInterface::onStreamReConfigured(int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004185 std::vector<uint64_t> bufIds = mBufferRecords.clearBufferCaches(streamId);
4186 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
4187 for (auto bufferId : bufIds) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07004188 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
4189 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07004190}
4191
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004192/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004193 * RequestThread inner class methods
4194 */
4195
4196Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004197 sp<StatusTracker> statusTracker,
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004198 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys,
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07004199 bool useHalBufManager,
4200 bool supportCameraMute) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004201 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004202 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004203 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004204 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004205 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004206 mId(getId(parent)),
Shuzhen Wang316781a2020-08-18 18:11:01 -07004207 mFirstRepeating(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004208 mReconfigured(false),
4209 mDoPause(false),
4210 mPaused(true),
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004211 mNotifyPipelineDrain(false),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004212 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07004213 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004214 mCurrentAfTriggerId(0),
4215 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004216 mRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE),
Emilian Peeve23f1d92021-09-20 14:56:01 -07004217 mComposerOutput(false),
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004218 mCameraMute(ANDROID_SENSOR_TEST_PATTERN_MODE_OFF),
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004219 mCameraMuteChanged(false),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004220 mRepeatingLastFrameNumber(
4221 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07004222 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01004223 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004224 mRequestLatency(kRequestLatencyBinSize),
4225 mSessionParamKeys(sessionParamKeys),
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004226 mLatestSessionParams(sessionParamKeys.size()),
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07004227 mUseHalBufManager(useHalBufManager),
4228 mSupportCameraMute(supportCameraMute){
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07004229 mStatusId = statusTracker->addComponent("RequestThread");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004230}
4231
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004232Camera3Device::RequestThread::~RequestThread() {}
4233
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004234void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004235 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004236 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004237 Mutex::Autolock l(mRequestLock);
4238 mListener = listener;
4239}
4240
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004241void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
Shuzhen Wang99080502021-03-07 21:08:20 -08004242 const CameraMetadata& sessionParams,
4243 const std::map<int32_t, std::set<String8>>& groupIdPhysicalCameraMap) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004244 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004245 Mutex::Autolock l(mRequestLock);
4246 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004247 mLatestSessionParams = sessionParams;
Shuzhen Wang99080502021-03-07 21:08:20 -08004248 mGroupIdPhysicalCameraMap = groupIdPhysicalCameraMap;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07004249 // Prepare video stream for high speed recording.
4250 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01004251 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004252}
4253
Jianing Wei90e59c92014-03-12 18:29:36 -07004254status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004255 List<sp<CaptureRequest> > &requests,
4256 /*out*/
4257 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004258 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07004259 Mutex::Autolock l(mRequestLock);
4260 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
4261 ++it) {
4262 mRequestQueue.push_back(*it);
4263 }
4264
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004265 if (lastFrameNumber != NULL) {
4266 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
4267 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
4268 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
4269 *lastFrameNumber);
4270 }
Jianing Weicb0652e2014-03-12 18:29:36 -07004271
Jianing Wei90e59c92014-03-12 18:29:36 -07004272 unpauseForNewRequests();
4273
4274 return OK;
4275}
4276
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004277
4278status_t Camera3Device::RequestThread::queueTrigger(
4279 RequestTrigger trigger[],
4280 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004281 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004282 Mutex::Autolock l(mTriggerMutex);
4283 status_t ret;
4284
4285 for (size_t i = 0; i < count; ++i) {
4286 ret = queueTriggerLocked(trigger[i]);
4287
4288 if (ret != OK) {
4289 return ret;
4290 }
4291 }
4292
4293 return OK;
4294}
4295
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004296const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
4297 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004298 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08004299 if (d != nullptr) return d->mId;
4300 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004301}
4302
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004303status_t Camera3Device::RequestThread::queueTriggerLocked(
4304 RequestTrigger trigger) {
4305
4306 uint32_t tag = trigger.metadataTag;
4307 ssize_t index = mTriggerMap.indexOfKey(tag);
4308
4309 switch (trigger.getTagType()) {
4310 case TYPE_BYTE:
4311 // fall-through
4312 case TYPE_INT32:
4313 break;
4314 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004315 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
4316 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004317 return INVALID_OPERATION;
4318 }
4319
4320 /**
4321 * Collect only the latest trigger, since we only have 1 field
4322 * in the request settings per trigger tag, and can't send more than 1
4323 * trigger per request.
4324 */
4325 if (index != NAME_NOT_FOUND) {
4326 mTriggerMap.editValueAt(index) = trigger;
4327 } else {
4328 mTriggerMap.add(tag, trigger);
4329 }
4330
4331 return OK;
4332}
4333
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004334status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004335 const RequestList &requests,
4336 /*out*/
4337 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004338 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004339 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004340 if (lastFrameNumber != NULL) {
4341 *lastFrameNumber = mRepeatingLastFrameNumber;
4342 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004343 mRepeatingRequests.clear();
Shuzhen Wang316781a2020-08-18 18:11:01 -07004344 mFirstRepeating = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004345 mRepeatingRequests.insert(mRepeatingRequests.begin(),
4346 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004347
4348 unpauseForNewRequests();
4349
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004350 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004351 return OK;
4352}
4353
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07004354bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004355 if (mRepeatingRequests.empty()) {
4356 return false;
4357 }
4358 int32_t requestId = requestIn->mResultExtras.requestId;
4359 const RequestList &repeatRequests = mRepeatingRequests;
4360 // All repeating requests are guaranteed to have same id so only check first quest
4361 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
4362 return (firstRequest->mResultExtras.requestId == requestId);
4363}
4364
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004365status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004366 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004367 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004368 return clearRepeatingRequestsLocked(lastFrameNumber);
4369
4370}
4371
4372status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004373 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004374 if (lastFrameNumber != NULL) {
4375 *lastFrameNumber = mRepeatingLastFrameNumber;
4376 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004377 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004378 return OK;
4379}
4380
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004381status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004382 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004383 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004384 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004385 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004386
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004387 mRepeatingRequests.clear();
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004388
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004389 // Send errors for all requests pending in the request queue, including
4390 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004391 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004392 if (listener != NULL) {
4393 for (RequestList::iterator it = mRequestQueue.begin();
4394 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004395 // Abort the input buffers for reprocess requests.
4396 if ((*it)->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07004397 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08004398 camera3::Size inputBufferSize;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07004399 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08004400 &inputBufferSize, /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004401 if (res != OK) {
4402 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
4403 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
4404 } else {
Emilian Peevf4816702020-04-03 15:44:51 -07004405 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004406 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
4407 if (res != OK) {
4408 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
4409 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
4410 }
4411 }
4412 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004413 // Set the frame number this request would have had, if it
4414 // had been submitted; this frame number will not be reused.
4415 // The requestId and burstId fields were set when the request was
4416 // submitted originally (in convertMetadataListToRequestListLocked)
4417 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004418 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07004419 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07004420 }
4421 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004422 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08004423
4424 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004425 mTriggerMap.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004426 if (lastFrameNumber != NULL) {
4427 *lastFrameNumber = mRepeatingLastFrameNumber;
4428 }
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004429 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Emilian Peev8dae54c2019-12-02 15:17:17 -08004430 mRequestSignal.signal();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07004431 return OK;
4432}
4433
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004434status_t Camera3Device::RequestThread::flush() {
4435 ATRACE_CALL();
4436 Mutex::Autolock l(mFlushLock);
4437
Emilian Peev08dd2452017-04-06 16:55:14 +01004438 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004439}
4440
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004441void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004442 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004443 Mutex::Autolock l(mPauseLock);
4444 mDoPause = paused;
4445 mDoPauseSignal.signal();
4446}
4447
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004448status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
4449 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004450 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004451 Mutex::Autolock l(mLatestRequestMutex);
4452 status_t res;
4453 while (mLatestRequestId != requestId) {
4454 nsecs_t startTime = systemTime();
4455
4456 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
4457 if (res != OK) return res;
4458
4459 timeout -= (systemTime() - startTime);
4460 }
4461
4462 return OK;
4463}
4464
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004465void Camera3Device::RequestThread::requestExit() {
4466 // Call parent to set up shutdown
4467 Thread::requestExit();
4468 // The exit from any possible waits
4469 mDoPauseSignal.signal();
4470 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07004471
4472 mRequestLatency.log("ProcessCaptureRequest latency histogram");
4473 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004474}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004475
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004476void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004477 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004478 bool surfaceAbandoned = false;
4479 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004480 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004481 {
4482 Mutex::Autolock l(mRequestLock);
4483 // Check all streams needed by repeating requests are still valid. Otherwise, stop
4484 // repeating requests.
4485 for (const auto& request : mRepeatingRequests) {
4486 for (const auto& s : request->mOutputStreams) {
4487 if (s->isAbandoned()) {
4488 surfaceAbandoned = true;
4489 clearRepeatingRequestsLocked(&lastFrameNumber);
4490 break;
4491 }
4492 }
4493 if (surfaceAbandoned) {
4494 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004495 }
4496 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004497 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004498 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004499
4500 if (listener != NULL && surfaceAbandoned) {
4501 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07004502 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004503}
4504
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004505bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004506 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004507 status_t res;
4508 size_t batchSize = mNextRequests.size();
Emilian Peevf4816702020-04-03 15:44:51 -07004509 std::vector<camera_capture_request_t*> requests(batchSize);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004510 uint32_t numRequestProcessed = 0;
4511 for (size_t i = 0; i < batchSize; i++) {
4512 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07004513 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004514 }
4515
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004516 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
4517
4518 bool triggerRemoveFailed = false;
4519 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
4520 for (size_t i = 0; i < numRequestProcessed; i++) {
4521 NextRequest& nextRequest = mNextRequests.editItemAt(i);
4522 nextRequest.submitted = true;
4523
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004524 updateNextRequest(nextRequest);
Emilian Peevaebbe412018-01-15 13:53:24 +00004525
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004526 if (!triggerRemoveFailed) {
4527 // Remove any previously queued triggers (after unlock)
4528 status_t removeTriggerRes = removeTriggers(mPrevRequest);
4529 if (removeTriggerRes != OK) {
4530 triggerRemoveFailed = true;
4531 triggerFailedRequest = nextRequest;
4532 }
4533 }
4534 }
4535
4536 if (triggerRemoveFailed) {
4537 SET_ERR("RequestThread: Unable to remove triggers "
4538 "(capture request %d, HAL device: %s (%d)",
4539 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
4540 cleanUpFailedRequests(/*sendRequestError*/ false);
4541 return false;
4542 }
4543
4544 if (res != OK) {
4545 // Should only get a failure here for malformed requests or device-level
4546 // errors, so consider all errors fatal. Bad metadata failures should
4547 // come through notify.
4548 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
4549 mNextRequests[numRequestProcessed].halRequest.frame_number,
4550 strerror(-res), res);
4551 cleanUpFailedRequests(/*sendRequestError*/ false);
4552 return false;
4553 }
4554 return true;
4555}
4556
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004557nsecs_t Camera3Device::RequestThread::calculateMaxExpectedDuration(const camera_metadata_t *request) {
4558 nsecs_t maxExpectedDuration = kDefaultExpectedDuration;
4559 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
4560 find_camera_metadata_ro_entry(request,
4561 ANDROID_CONTROL_AE_MODE,
4562 &e);
4563 if (e.count == 0) return maxExpectedDuration;
4564
4565 switch (e.data.u8[0]) {
4566 case ANDROID_CONTROL_AE_MODE_OFF:
4567 find_camera_metadata_ro_entry(request,
4568 ANDROID_SENSOR_EXPOSURE_TIME,
4569 &e);
4570 if (e.count > 0) {
4571 maxExpectedDuration = e.data.i64[0];
4572 }
4573 find_camera_metadata_ro_entry(request,
4574 ANDROID_SENSOR_FRAME_DURATION,
4575 &e);
4576 if (e.count > 0) {
4577 maxExpectedDuration = std::max(e.data.i64[0], maxExpectedDuration);
4578 }
4579 break;
4580 default:
4581 find_camera_metadata_ro_entry(request,
4582 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
4583 &e);
4584 if (e.count > 1) {
4585 maxExpectedDuration = 1e9 / e.data.u8[0];
4586 }
4587 break;
4588 }
4589
4590 return maxExpectedDuration;
4591}
4592
Emilian Peeva14b4dd2018-05-15 11:00:31 +01004593bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
4594 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
4595 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
4596 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
4597 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
4598 return true;
4599 }
4600
4601 return false;
4602}
4603
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004604void Camera3Device::RequestThread::updateNextRequest(NextRequest& nextRequest) {
4605 // Update the latest request sent to HAL
Shuzhen Wang83bff122020-11-20 15:51:39 -08004606 camera_capture_request_t& halRequest = nextRequest.halRequest;
4607 if (halRequest.settings != NULL) { // Don't update if they were unchanged
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004608 Mutex::Autolock al(mLatestRequestMutex);
4609
Shuzhen Wang83bff122020-11-20 15:51:39 -08004610 camera_metadata_t* cloned = clone_camera_metadata(halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004611 mLatestRequest.acquire(cloned);
4612
4613 mLatestPhysicalRequest.clear();
Shuzhen Wang83bff122020-11-20 15:51:39 -08004614 for (uint32_t i = 0; i < halRequest.num_physcam_settings; i++) {
4615 cloned = clone_camera_metadata(halRequest.physcam_settings[i]);
4616 mLatestPhysicalRequest.emplace(halRequest.physcam_id[i],
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004617 CameraMetadata(cloned));
4618 }
4619
4620 sp<Camera3Device> parent = mParent.promote();
4621 if (parent != NULL) {
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07004622 int32_t inputStreamId = -1;
4623 if (halRequest.input_buffer != nullptr) {
4624 inputStreamId = Camera3Stream::cast(halRequest.input_buffer->stream)->getId();
4625 }
4626
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004627 parent->monitorMetadata(TagMonitor::REQUEST,
Shuzhen Wang83bff122020-11-20 15:51:39 -08004628 halRequest.frame_number,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07004629 0, mLatestRequest, mLatestPhysicalRequest, halRequest.output_buffers,
4630 halRequest.num_output_buffers, inputStreamId);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004631 }
4632 }
4633
Shuzhen Wang83bff122020-11-20 15:51:39 -08004634 if (halRequest.settings != NULL) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004635 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
Shuzhen Wang83bff122020-11-20 15:51:39 -08004636 halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004637 }
4638
Shuzhen Wang83bff122020-11-20 15:51:39 -08004639 cleanupPhysicalSettings(nextRequest.captureRequest, &halRequest);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07004640}
4641
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004642bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
4643 ATRACE_CALL();
4644 bool updatesDetected = false;
4645
Emilian Peev4ec17882019-01-24 17:16:58 -08004646 CameraMetadata updatedParams(mLatestSessionParams);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004647 for (auto tag : mSessionParamKeys) {
4648 camera_metadata_ro_entry entry = settings.find(tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08004649 camera_metadata_entry lastEntry = updatedParams.find(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004650
4651 if (entry.count > 0) {
4652 bool isDifferent = false;
4653 if (lastEntry.count > 0) {
4654 // Have a last value, compare to see if changed
4655 if (lastEntry.type == entry.type &&
4656 lastEntry.count == entry.count) {
4657 // Same type and count, compare values
4658 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
4659 size_t entryBytes = bytesPerValue * lastEntry.count;
4660 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
4661 if (cmp != 0) {
4662 isDifferent = true;
4663 }
4664 } else {
4665 // Count or type has changed
4666 isDifferent = true;
4667 }
4668 } else {
4669 // No last entry, so always consider to be different
4670 isDifferent = true;
4671 }
4672
4673 if (isDifferent) {
4674 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01004675 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
4676 updatesDetected = true;
4677 }
Emilian Peev4ec17882019-01-24 17:16:58 -08004678 updatedParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004679 }
4680 } else if (lastEntry.count > 0) {
4681 // Value has been removed
4682 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08004683 updatedParams.erase(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004684 updatesDetected = true;
4685 }
4686 }
4687
Emilian Peev4ec17882019-01-24 17:16:58 -08004688 bool reconfigureRequired;
4689 if (updatesDetected) {
4690 reconfigureRequired = mInterface->isReconfigurationRequired(mLatestSessionParams,
4691 updatedParams);
4692 mLatestSessionParams = updatedParams;
4693 } else {
4694 reconfigureRequired = false;
4695 }
4696
4697 return reconfigureRequired;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004698}
4699
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004700bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004701 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004702 status_t res;
Jayant Chowdhary37eca242019-11-18 08:55:56 -08004703 // Any function called from threadLoop() must not hold mInterfaceLock since
4704 // it could lead to deadlocks (disconnect() -> hold mInterfaceMutex -> wait for request thread
4705 // to finish -> request thread waits on mInterfaceMutex) http://b/143513518
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004706
4707 // Handle paused state.
4708 if (waitIfPaused()) {
4709 return true;
4710 }
4711
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004712 // Wait for the next batch of requests.
4713 waitForNextRequestBatch();
4714 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004715 return true;
4716 }
4717
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004718 // Get the latest request ID, if any
4719 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004720 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00004721 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004722 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004723 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004724 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004725 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
4726 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004727 }
4728
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004729 // 'mNextRequests' will at this point contain either a set of HFR batched requests
4730 // or a single request from streaming or burst. In either case the first element
4731 // should contain the latest camera settings that we need to check for any session
4732 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00004733 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004734 res = OK;
4735
4736 //Input stream buffers are already acquired at this point so an input stream
4737 //will not be able to move to idle state unless we force it.
4738 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
4739 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
4740 if (res != OK) {
4741 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
4742 cleanUpFailedRequests(/*sendRequestError*/ false);
4743 return false;
4744 }
4745 }
4746
4747 if (res == OK) {
Emilian Peev3bead5f2020-05-28 17:29:08 -07004748 sp<Camera3Device> parent = mParent.promote();
4749 if (parent != nullptr) {
4750 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams, mStatusId);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004751 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07004752 setPaused(false);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004753
4754 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
4755 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
4756 if (res != OK) {
4757 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
4758 cleanUpFailedRequests(/*sendRequestError*/ false);
4759 return false;
4760 }
4761 }
4762 }
4763 }
4764
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004765 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004766 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004767 if (res == TIMED_OUT) {
4768 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004769 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07004770 // Check if any stream is abandoned.
4771 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004772 return true;
4773 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004774 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07004775 return false;
4776 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004777
Zhijun Hecc27e112013-10-03 16:12:43 -07004778 // Inform waitUntilRequestProcessed thread of a new request ID
4779 {
4780 Mutex::Autolock al(mLatestRequestMutex);
4781
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004782 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07004783 mLatestRequestSignal.signal();
4784 }
4785
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004786 // Submit a batch of requests to HAL.
4787 // Use flush lock only when submitting multilple requests in a batch.
4788 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
4789 // which may take a long time to finish so synchronizing flush() and
4790 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
4791 // For now, only synchronize for high speed recording and we should figure something out for
4792 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004793 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07004794
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004795 if (useFlushLock) {
4796 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004797 }
4798
Zhijun Hef0645c12016-08-02 00:58:11 -07004799 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004800 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07004801
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08004802 sp<Camera3Device> parent = mParent.promote();
4803 if (parent != nullptr) {
4804 parent->mRequestBufferSM.onSubmittingRequest();
4805 }
4806
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004807 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07004808 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh11648852019-05-16 10:42:54 -07004809 submitRequestSuccess = sendRequestsBatch();
4810
Shuzhen Wang686f6442017-06-20 16:16:04 -07004811 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
4812 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07004813
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004814 if (useFlushLock) {
4815 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004816 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004817
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004818 // Unset as current request
4819 {
4820 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004821 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004822 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07004823 mRequestSubmittedSignal.signal();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004824
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08004825 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004826}
4827
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00004828status_t Camera3Device::removeFwkOnlyRegionKeys(CameraMetadata *request) {
4829 static const std::array<uint32_t, 4> kFwkOnlyRegionKeys = {ANDROID_CONTROL_AF_REGIONS_SET,
4830 ANDROID_CONTROL_AE_REGIONS_SET, ANDROID_CONTROL_AWB_REGIONS_SET,
4831 ANDROID_SCALER_CROP_REGION_SET};
4832 if (request == nullptr) {
4833 ALOGE("%s request metadata nullptr", __FUNCTION__);
4834 return BAD_VALUE;
4835 }
4836 status_t res = OK;
4837 for (const auto &key : kFwkOnlyRegionKeys) {
4838 if (request->exists(key)) {
4839 res = request->erase(key);
4840 if (res != OK) {
4841 return res;
4842 }
4843 }
4844 }
4845 return OK;
4846}
4847
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004848status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004849 ATRACE_CALL();
4850
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07004851 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
Shuzhen Wang4a472662017-02-26 23:29:04 -08004852 for (size_t i = 0; i < mNextRequests.size(); i++) {
4853 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004854 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07004855 camera_capture_request_t* halRequest = &nextRequest.halRequest;
4856 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004857
4858 // Prepare a request to HAL
4859 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
4860
4861 // Insert any queued triggers (before metadata is locked)
4862 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004863 if (res < 0) {
4864 SET_ERR("RequestThread: Unable to insert triggers "
4865 "(capture request %d, HAL device: %s (%d)",
4866 halRequest->frame_number, strerror(-res), res);
4867 return INVALID_OPERATION;
4868 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07004869
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004870 int triggerCount = res;
4871 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
4872 mPrevTriggers = triggerCount;
4873
Emilian Peeve23f1d92021-09-20 14:56:01 -07004874 // Do not override rotate&crop for stream configurations that include
4875 // SurfaceViews(HW_COMPOSER) output. The display rotation there will be
4876 // compensated by NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY
4877 bool rotateAndCropChanged = mComposerOutput ? false :
4878 overrideAutoRotateAndCrop(captureRequest);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004879 bool testPatternChanged = overrideTestPattern(captureRequest);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004880
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004881 // If the request is the same as last, or we had triggers now or last time or
4882 // changing overrides this time
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004883 bool newRequest =
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004884 (mPrevRequest != captureRequest || triggersMixedIn ||
4885 rotateAndCropChanged || testPatternChanged) &&
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07004886 // Request settings are all the same within one batch, so only treat the first
4887 // request in a batch as new
Zhijun He54c36822018-07-18 09:33:39 -07004888 !(batchedRequest && i > 0);
Emilian Peev00420d22018-02-05 21:33:13 +00004889 if (newRequest) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08004890 std::set<std::string> cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004891 /**
4892 * HAL workaround:
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004893 * Insert a fake trigger ID if a trigger is set but no trigger ID is
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004894 */
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004895 res = addFakeTriggerIds(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004896 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004897 SET_ERR("RequestThread: Unable to insert fake trigger IDs "
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004898 "(capture request %d, HAL device: %s (%d)",
4899 halRequest->frame_number, strerror(-res), res);
4900 return INVALID_OPERATION;
4901 }
4902
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07004903 {
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07004904 sp<Camera3Device> parent = mParent.promote();
4905 if (parent != nullptr) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07004906 List<PhysicalCameraSettings>::iterator it;
4907 for (it = captureRequest->mSettingsList.begin();
4908 it != captureRequest->mSettingsList.end(); it++) {
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07004909 if (parent->mUHRCropAndMeteringRegionMappers.find(it->cameraId) ==
4910 parent->mUHRCropAndMeteringRegionMappers.end()) {
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00004911 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
4912 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
4913 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
4914 res);
4915 return INVALID_OPERATION;
4916 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07004917 continue;
4918 }
4919
4920 if (!captureRequest->mUHRCropAndMeteringRegionsUpdated) {
4921 res = parent->mUHRCropAndMeteringRegionMappers[it->cameraId].
4922 updateCaptureRequest(&(it->metadata));
4923 if (res != OK) {
4924 SET_ERR("RequestThread: Unable to correct capture requests "
4925 "for scaler crop region and metering regions for request "
4926 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
4927 res);
4928 return INVALID_OPERATION;
4929 }
4930 captureRequest->mUHRCropAndMeteringRegionsUpdated = true;
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00004931 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
4932 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
4933 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
4934 res);
4935 return INVALID_OPERATION;
4936 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07004937 }
4938 }
4939
4940 // Correct metadata regions for distortion correction if enabled
4941 for (it = captureRequest->mSettingsList.begin();
4942 it != captureRequest->mSettingsList.end(); it++) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07004943 if (parent->mDistortionMappers.find(it->cameraId) ==
4944 parent->mDistortionMappers.end()) {
4945 continue;
4946 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07004947
4948 if (!captureRequest->mDistortionCorrectionUpdated) {
4949 res = parent->mDistortionMappers[it->cameraId].correctCaptureRequest(
4950 &(it->metadata));
4951 if (res != OK) {
4952 SET_ERR("RequestThread: Unable to correct capture requests "
4953 "for lens distortion for request %d: %s (%d)",
4954 halRequest->frame_number, strerror(-res), res);
4955 return INVALID_OPERATION;
4956 }
4957 captureRequest->mDistortionCorrectionUpdated = true;
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07004958 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07004959 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08004960
4961 for (it = captureRequest->mSettingsList.begin();
4962 it != captureRequest->mSettingsList.end(); it++) {
4963 if (parent->mZoomRatioMappers.find(it->cameraId) ==
4964 parent->mZoomRatioMappers.end()) {
4965 continue;
4966 }
4967
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07004968 if (!captureRequest->mZoomRatioIs1x) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08004969 cameraIdsWithZoom.insert(it->cameraId);
4970 }
4971
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07004972 if (!captureRequest->mZoomRatioUpdated) {
4973 res = parent->mZoomRatioMappers[it->cameraId].updateCaptureRequest(
4974 &(it->metadata));
4975 if (res != OK) {
4976 SET_ERR("RequestThread: Unable to correct capture requests "
4977 "for zoom ratio for request %d: %s (%d)",
4978 halRequest->frame_number, strerror(-res), res);
4979 return INVALID_OPERATION;
4980 }
4981 captureRequest->mZoomRatioUpdated = true;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08004982 }
4983 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07004984 if (captureRequest->mRotateAndCropAuto &&
4985 !captureRequest->mRotationAndCropUpdated) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004986 for (it = captureRequest->mSettingsList.begin();
4987 it != captureRequest->mSettingsList.end(); it++) {
4988 auto mapper = parent->mRotateAndCropMappers.find(it->cameraId);
4989 if (mapper != parent->mRotateAndCropMappers.end()) {
4990 res = mapper->second.updateCaptureRequest(&(it->metadata));
4991 if (res != OK) {
4992 SET_ERR("RequestThread: Unable to correct capture requests "
4993 "for rotate-and-crop for request %d: %s (%d)",
4994 halRequest->frame_number, strerror(-res), res);
4995 return INVALID_OPERATION;
4996 }
4997 }
4998 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07004999 captureRequest->mRotationAndCropUpdated = true;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005000 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07005001 }
5002 }
5003
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005004 /**
5005 * The request should be presorted so accesses in HAL
5006 * are O(logn). Sidenote, sorting a sorted metadata is nop.
5007 */
Emilian Peevaebbe412018-01-15 13:53:24 +00005008 captureRequest->mSettingsList.begin()->metadata.sort();
5009 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005010 mPrevRequest = captureRequest;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08005011 mPrevCameraIdsWithZoom = cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005012 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
5013
5014 IF_ALOGV() {
5015 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
5016 find_camera_metadata_ro_entry(
5017 halRequest->settings,
5018 ANDROID_CONTROL_AF_TRIGGER,
5019 &e
5020 );
5021 if (e.count > 0) {
5022 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
5023 __FUNCTION__,
5024 halRequest->frame_number,
5025 e.data.u8[0]);
5026 }
5027 }
5028 } else {
5029 // leave request.settings NULL to indicate 'reuse latest given'
5030 ALOGVV("%s: Request settings are REUSED",
5031 __FUNCTION__);
5032 }
5033
Emilian Peevaebbe412018-01-15 13:53:24 +00005034 if (captureRequest->mSettingsList.size() > 1) {
5035 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
5036 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00005037 if (newRequest) {
5038 halRequest->physcam_settings =
5039 new const camera_metadata* [halRequest->num_physcam_settings];
5040 } else {
5041 halRequest->physcam_settings = nullptr;
5042 }
Emilian Peevaebbe412018-01-15 13:53:24 +00005043 auto it = ++captureRequest->mSettingsList.begin();
5044 size_t i = 0;
5045 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
5046 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00005047 if (newRequest) {
5048 it->metadata.sort();
5049 halRequest->physcam_settings[i] = it->metadata.getAndLock();
5050 }
Emilian Peevaebbe412018-01-15 13:53:24 +00005051 }
5052 }
5053
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005054 uint32_t totalNumBuffers = 0;
5055
5056 // Fill in buffers
5057 if (captureRequest->mInputStream != NULL) {
5058 halRequest->input_buffer = &captureRequest->mInputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08005059
5060 halRequest->input_width = captureRequest->mInputBufferSize.width;
5061 halRequest->input_height = captureRequest->mInputBufferSize.height;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005062 totalNumBuffers += 1;
5063 } else {
5064 halRequest->input_buffer = NULL;
5065 }
5066
Emilian Peevf4816702020-04-03 15:44:51 -07005067 outputBuffers->insertAt(camera_stream_buffer_t(), 0,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005068 captureRequest->mOutputStreams.size());
5069 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang99080502021-03-07 21:08:20 -08005070 std::set<std::set<String8>> requestedPhysicalCameras;
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07005071
5072 sp<Camera3Device> parent = mParent.promote();
5073 if (parent == NULL) {
5074 // Should not happen, and nowhere to send errors to, so just log it
5075 CLOGE("RequestThread: Parent is gone");
5076 return INVALID_OPERATION;
5077 }
5078 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
5079
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005080 SurfaceMap uniqueSurfaceIdMap;
Shuzhen Wang4a472662017-02-26 23:29:04 -08005081 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005082 sp<Camera3OutputStreamInterface> outputStream =
5083 captureRequest->mOutputStreams.editItemAt(j);
5084 int streamId = outputStream->getId();
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07005085
5086 // Prepare video buffers for high speed recording on the first video request.
5087 if (mPrepareVideoStream && outputStream->isVideoStream()) {
5088 // Only try to prepare video stream on the first video request.
5089 mPrepareVideoStream = false;
5090
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07005091 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
5092 false /*blockRequest*/);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07005093 while (res == NOT_ENOUGH_DATA) {
5094 res = outputStream->prepareNextBuffer();
5095 }
5096 if (res != OK) {
5097 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
5098 __FUNCTION__, strerror(-res), res);
5099 outputStream->cancelPrepare();
5100 }
5101 }
5102
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005103 std::vector<size_t> uniqueSurfaceIds;
5104 res = outputStream->getUniqueSurfaceIds(
5105 captureRequest->mOutputSurfaces[streamId],
5106 &uniqueSurfaceIds);
5107 // INVALID_OPERATION is normal output for streams not supporting surfaceIds
5108 if (res != OK && res != INVALID_OPERATION) {
5109 ALOGE("%s: failed to query stream %d unique surface IDs",
5110 __FUNCTION__, streamId);
5111 return res;
5112 }
5113 if (res == OK) {
5114 uniqueSurfaceIdMap.insert({streamId, std::move(uniqueSurfaceIds)});
5115 }
5116
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07005117 if (mUseHalBufManager) {
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08005118 if (outputStream->isAbandoned()) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07005119 ALOGV("%s: stream %d is abandoned, skipping request", __FUNCTION__, streamId);
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08005120 return TIMED_OUT;
5121 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07005122 // HAL will request buffer through requestStreamBuffer API
Emilian Peevf4816702020-04-03 15:44:51 -07005123 camera_stream_buffer_t& buffer = outputBuffers->editItemAt(j);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07005124 buffer.stream = outputStream->asHalStream();
5125 buffer.buffer = nullptr;
Emilian Peevf4816702020-04-03 15:44:51 -07005126 buffer.status = CAMERA_BUFFER_STATUS_OK;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07005127 buffer.acquire_fence = -1;
5128 buffer.release_fence = -1;
Emilian Peevf0348ae2021-01-13 13:39:45 -08005129 // Mark the output stream as unpreparable to block clients from calling
5130 // 'prepare' after this request reaches CameraHal and before the respective
5131 // buffers are requested.
5132 outputStream->markUnpreparable();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07005133 } else {
5134 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
5135 waitDuration,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005136 captureRequest->mOutputSurfaces[streamId]);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07005137 if (res != OK) {
5138 // Can't get output buffer from gralloc queue - this could be due to
5139 // abandoned queue or other consumer misbehavior, so not a fatal
5140 // error
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07005141 ALOGV("RequestThread: Can't get output buffer, skipping request:"
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07005142 " %s (%d)", strerror(-res), res);
5143
5144 return TIMED_OUT;
5145 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005146 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08005147
5148 {
5149 sp<Camera3Device> parent = mParent.promote();
5150 if (parent != nullptr) {
5151 const String8& streamCameraId = outputStream->getPhysicalCameraId();
5152 for (const auto& settings : captureRequest->mSettingsList) {
5153 if ((streamCameraId.isEmpty() &&
5154 parent->getId() == settings.cameraId.c_str()) ||
5155 streamCameraId == settings.cameraId.c_str()) {
5156 outputStream->fireBufferRequestForFrameNumber(
5157 captureRequest->mResultExtras.frameNumber,
5158 settings.metadata);
5159 }
5160 }
5161 }
5162 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07005163
Shuzhen Wang5c22c152017-12-31 17:12:25 -08005164 String8 physicalCameraId = outputStream->getPhysicalCameraId();
Shuzhen Wang99080502021-03-07 21:08:20 -08005165 int32_t streamGroupId = outputStream->getHalStreamGroupId();
5166 if (streamGroupId != -1 && mGroupIdPhysicalCameraMap.count(streamGroupId) == 1) {
5167 requestedPhysicalCameras.insert(mGroupIdPhysicalCameraMap[streamGroupId]);
5168 } else if (!physicalCameraId.isEmpty()) {
5169 requestedPhysicalCameras.insert(std::set<String8>({physicalCameraId}));
Shuzhen Wang5c22c152017-12-31 17:12:25 -08005170 }
5171 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005172 }
5173 totalNumBuffers += halRequest->num_output_buffers;
5174
5175 // Log request in the in-flight queue
Shuzhen Wang4a472662017-02-26 23:29:04 -08005176 // If this request list is for constrained high speed recording (not
5177 // preview), and the current request is not the last one in the batch,
5178 // do not send callback to the app.
5179 bool hasCallback = true;
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07005180 if (batchedRequest && i != mNextRequests.size()-1) {
Shuzhen Wang4a472662017-02-26 23:29:04 -08005181 hasCallback = false;
5182 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01005183 bool isStillCapture = false;
Shuzhen Wang26abaf42018-08-28 15:41:20 -07005184 bool isZslCapture = false;
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08005185 const camera_metadata_t* settings = halRequest->settings;
5186 bool shouldUnlockSettings = false;
5187 if (settings == nullptr) {
5188 shouldUnlockSettings = true;
5189 settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
5190 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01005191 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
5192 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08005193 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
Emilian Peev9dd21f42018-08-03 13:39:29 +01005194 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
5195 isStillCapture = true;
5196 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
5197 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07005198
Emilian Peevaf8416e2021-02-04 17:52:43 -08005199 e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08005200 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
Shuzhen Wang26abaf42018-08-28 15:41:20 -07005201 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
5202 isZslCapture = true;
5203 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01005204 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005205 res = parent->registerInFlight(halRequest->frame_number,
5206 totalNumBuffers, captureRequest->mResultExtras,
5207 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07005208 hasCallback,
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08005209 calculateMaxExpectedDuration(settings),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005210 requestedPhysicalCameras, isStillCapture, isZslCapture,
5211 captureRequest->mRotateAndCropAuto, mPrevCameraIdsWithZoom,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005212 (mUseHalBufManager) ? uniqueSurfaceIdMap :
Shuzhen Wang316781a2020-08-18 18:11:01 -07005213 SurfaceMap{}, captureRequest->mRequestTimeNs);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005214 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
5215 ", burstId = %" PRId32 ".",
5216 __FUNCTION__,
5217 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
5218 captureRequest->mResultExtras.burstId);
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08005219
5220 if (shouldUnlockSettings) {
5221 captureRequest->mSettingsList.begin()->metadata.unlock(settings);
5222 }
5223
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005224 if (res != OK) {
5225 SET_ERR("RequestThread: Unable to register new in-flight request:"
5226 " %s (%d)", strerror(-res), res);
5227 return INVALID_OPERATION;
5228 }
5229 }
5230
5231 return OK;
5232}
5233
Igor Murashkin1e479c02013-09-06 16:55:14 -07005234CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005235 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07005236 Mutex::Autolock al(mLatestRequestMutex);
5237
5238 ALOGV("RequestThread::%s", __FUNCTION__);
5239
5240 return mLatestRequest;
5241}
5242
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005243bool Camera3Device::RequestThread::isStreamPending(
5244 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005245 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005246 Mutex::Autolock l(mRequestLock);
5247
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005248 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005249 if (!nextRequest.submitted) {
5250 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
5251 if (stream == s) return true;
5252 }
5253 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005254 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005255 }
5256
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005257 for (const auto& request : mRequestQueue) {
5258 for (const auto& s : request->mOutputStreams) {
5259 if (stream == s) return true;
5260 }
5261 if (stream == request->mInputStream) return true;
5262 }
5263
5264 for (const auto& request : mRepeatingRequests) {
5265 for (const auto& s : request->mOutputStreams) {
5266 if (stream == s) return true;
5267 }
5268 if (stream == request->mInputStream) return true;
5269 }
5270
5271 return false;
5272}
Jianing Weicb0652e2014-03-12 18:29:36 -07005273
Emilian Peev40ead602017-09-26 15:46:36 +01005274bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
5275 ATRACE_CALL();
5276 Mutex::Autolock l(mRequestLock);
5277
5278 for (const auto& nextRequest : mNextRequests) {
5279 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
5280 if (s.first == streamId) {
5281 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
5282 if (it != s.second.end()) {
5283 return true;
5284 }
5285 }
5286 }
5287 }
5288
5289 for (const auto& request : mRequestQueue) {
5290 for (const auto& s : request->mOutputSurfaces) {
5291 if (s.first == streamId) {
5292 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
5293 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005294 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01005295 }
5296 }
5297 }
5298 }
5299
5300 for (const auto& request : mRepeatingRequests) {
5301 for (const auto& s : request->mOutputSurfaces) {
5302 if (s.first == streamId) {
5303 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
5304 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07005305 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01005306 }
5307 }
5308 }
5309 }
5310
5311 return false;
5312}
5313
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07005314void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
5315 if (!mUseHalBufManager) {
5316 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
5317 return;
5318 }
5319
5320 Mutex::Autolock pl(mPauseLock);
5321 if (mPaused) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005322 mInterface->signalPipelineDrain(streamIds);
5323 return;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07005324 }
5325 // If request thread is still busy, wait until paused then notify HAL
5326 mNotifyPipelineDrain = true;
5327 mStreamIdsToBeDrained = streamIds;
5328}
5329
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07005330void Camera3Device::RequestThread::resetPipelineDrain() {
5331 Mutex::Autolock pl(mPauseLock);
5332 mNotifyPipelineDrain = false;
5333 mStreamIdsToBeDrained.clear();
5334}
5335
Emilian Peevc0fe54c2020-03-11 14:05:07 -07005336void Camera3Device::RequestThread::clearPreviousRequest() {
5337 Mutex::Autolock l(mRequestLock);
5338 mPrevRequest.clear();
5339}
5340
Yin-Chia Yehb978c382019-10-30 00:22:37 -07005341status_t Camera3Device::RequestThread::switchToOffline(
5342 const std::vector<int32_t>& streamsToKeep,
5343 /*out*/hardware::camera::device::V3_6::CameraOfflineSessionInfo* offlineSessionInfo,
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005344 /*out*/sp<hardware::camera::device::V3_6::ICameraOfflineSession>* offlineSession,
5345 /*out*/camera3::BufferRecords* bufferRecords) {
Yin-Chia Yehb978c382019-10-30 00:22:37 -07005346 Mutex::Autolock l(mRequestLock);
5347 clearRepeatingRequestsLocked(/*lastFrameNumber*/nullptr);
5348
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005349 // Wait until request thread is fully stopped
5350 // TBD: check if request thread is being paused by other APIs (shouldn't be)
5351
5352 // We could also check for mRepeatingRequests.empty(), but the API interface
5353 // is serialized by Camera3Device::mInterfaceLock so no one should be able to submit any
5354 // new requests during the call; hence skip that check.
Yin-Chia Yehb978c382019-10-30 00:22:37 -07005355 bool queueEmpty = mNextRequests.empty() && mRequestQueue.empty();
5356 while (!queueEmpty) {
5357 status_t res = mRequestSubmittedSignal.waitRelative(mRequestLock, kRequestSubmitTimeout);
5358 if (res == TIMED_OUT) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005359 ALOGE("%s: request thread failed to submit one request within timeout!", __FUNCTION__);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07005360 return res;
5361 } else if (res != OK) {
5362 ALOGE("%s: request thread failed to submit a request: %s (%d)!",
5363 __FUNCTION__, strerror(-res), res);
5364 return res;
5365 }
5366 queueEmpty = mNextRequests.empty() && mRequestQueue.empty();
5367 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005368
Yin-Chia Yehb978c382019-10-30 00:22:37 -07005369 return mInterface->switchToOffline(
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005370 streamsToKeep, offlineSessionInfo, offlineSession, bufferRecords);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07005371}
5372
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005373status_t Camera3Device::RequestThread::setRotateAndCropAutoBehavior(
5374 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
5375 ATRACE_CALL();
5376 Mutex::Autolock l(mTriggerMutex);
5377 if (rotateAndCropValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
5378 return BAD_VALUE;
5379 }
5380 mRotateAndCropOverride = rotateAndCropValue;
5381 return OK;
5382}
5383
Emilian Peeve23f1d92021-09-20 14:56:01 -07005384status_t Camera3Device::RequestThread::setComposerSurface(bool composerSurfacePresent) {
5385 ATRACE_CALL();
5386 Mutex::Autolock l(mTriggerMutex);
5387 mComposerOutput = composerSurfacePresent;
5388 return OK;
5389}
5390
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07005391status_t Camera3Device::RequestThread::setCameraMute(int32_t muteMode) {
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005392 ATRACE_CALL();
5393 Mutex::Autolock l(mTriggerMutex);
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07005394 if (muteMode != mCameraMute) {
5395 mCameraMute = muteMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005396 mCameraMuteChanged = true;
5397 }
5398 return OK;
5399}
5400
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07005401nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005402 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005403 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07005404 return mExpectedInflightDuration > kMinInflightDuration ?
5405 mExpectedInflightDuration : kMinInflightDuration;
5406}
5407
Emilian Peevaebbe412018-01-15 13:53:24 +00005408void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
Emilian Peevf4816702020-04-03 15:44:51 -07005409 camera_capture_request_t *halRequest) {
Emilian Peevaebbe412018-01-15 13:53:24 +00005410 if ((request == nullptr) || (halRequest == nullptr)) {
5411 ALOGE("%s: Invalid request!", __FUNCTION__);
5412 return;
5413 }
5414
5415 if (halRequest->num_physcam_settings > 0) {
5416 if (halRequest->physcam_id != nullptr) {
5417 delete [] halRequest->physcam_id;
5418 halRequest->physcam_id = nullptr;
5419 }
5420 if (halRequest->physcam_settings != nullptr) {
5421 auto it = ++(request->mSettingsList.begin());
5422 size_t i = 0;
5423 for (; it != request->mSettingsList.end(); it++, i++) {
5424 it->metadata.unlock(halRequest->physcam_settings[i]);
5425 }
5426 delete [] halRequest->physcam_settings;
5427 halRequest->physcam_settings = nullptr;
5428 }
5429 }
5430}
5431
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005432void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
5433 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005434 return;
5435 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005436
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005437 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005438 // Skip the ones that have been submitted successfully.
5439 if (nextRequest.submitted) {
5440 continue;
5441 }
5442
5443 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07005444 camera_capture_request_t* halRequest = &nextRequest.halRequest;
5445 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005446
5447 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00005448 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005449 }
5450
Emilian Peevaebbe412018-01-15 13:53:24 +00005451 cleanupPhysicalSettings(captureRequest, halRequest);
5452
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005453 if (captureRequest->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07005454 captureRequest->mInputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005455 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
5456 }
5457
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08005458 // No output buffer can be returned when using HAL buffer manager
5459 if (!mUseHalBufManager) {
5460 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
5461 //Buffers that failed processing could still have
5462 //valid acquire fence.
5463 int acquireFence = (*outputBuffers)[i].acquire_fence;
5464 if (0 <= acquireFence) {
5465 close(acquireFence);
5466 outputBuffers->editItemAt(i).acquire_fence = -1;
5467 }
Emilian Peevf4816702020-04-03 15:44:51 -07005468 outputBuffers->editItemAt(i).status = CAMERA_BUFFER_STATUS_ERROR;
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08005469 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i], 0,
5470 /*timestampIncreasing*/true, std::vector<size_t> (),
5471 captureRequest->mResultExtras.frameNumber);
Emilian Peevc58cf4c2017-05-11 17:23:41 +01005472 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005473 }
5474
5475 if (sendRequestError) {
5476 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005477 sp<NotificationListener> listener = mListener.promote();
5478 if (listener != NULL) {
5479 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005480 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005481 captureRequest->mResultExtras);
5482 }
5483 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07005484
5485 // Remove yet-to-be submitted inflight request from inflightMap
5486 {
5487 sp<Camera3Device> parent = mParent.promote();
5488 if (parent != NULL) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005489 std::lock_guard<std::mutex> l(parent->mInFlightLock);
Shuzhen Wangcadb3302016-11-04 14:17:56 -07005490 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
5491 if (idx >= 0) {
5492 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
5493 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
5494 parent->removeInFlightMapEntryLocked(idx);
5495 }
5496 }
5497 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005498 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07005499
5500 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005501 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005502}
5503
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005504void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005505 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005506 // Optimized a bit for the simple steady-state case (single repeating
5507 // request), to avoid putting that request in the queue temporarily.
5508 Mutex::Autolock l(mRequestLock);
5509
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005510 assert(mNextRequests.empty());
5511
5512 NextRequest nextRequest;
5513 nextRequest.captureRequest = waitForNextRequestLocked();
5514 if (nextRequest.captureRequest == nullptr) {
5515 return;
5516 }
5517
Emilian Peevf4816702020-04-03 15:44:51 -07005518 nextRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005519 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005520 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005521
5522 // Wait for additional requests
5523 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
5524
5525 for (size_t i = 1; i < batchSize; i++) {
5526 NextRequest additionalRequest;
5527 additionalRequest.captureRequest = waitForNextRequestLocked();
5528 if (additionalRequest.captureRequest == nullptr) {
5529 break;
5530 }
5531
Emilian Peevf4816702020-04-03 15:44:51 -07005532 additionalRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005533 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005534 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005535 }
5536
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005537 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08005538 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07005539 mNextRequests.size(), batchSize);
5540 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07005541 }
5542
5543 return;
5544}
5545
5546sp<Camera3Device::CaptureRequest>
5547 Camera3Device::RequestThread::waitForNextRequestLocked() {
5548 status_t res;
5549 sp<CaptureRequest> nextRequest;
5550
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005551 while (mRequestQueue.empty()) {
5552 if (!mRepeatingRequests.empty()) {
5553 // Always atomically enqueue all requests in a repeating request
5554 // list. Guarantees a complete in-sequence set of captures to
5555 // application.
5556 const RequestList &requests = mRepeatingRequests;
Shuzhen Wang316781a2020-08-18 18:11:01 -07005557 if (mFirstRepeating) {
5558 mFirstRepeating = false;
5559 } else {
5560 for (auto& request : requests) {
5561 // For repeating requests, override timestamp request using
5562 // the time a request is inserted into the request queue,
5563 // because the original repeating request will have an old
5564 // fixed timestamp.
5565 request->mRequestTimeNs = systemTime();
5566 }
5567 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005568 RequestList::const_iterator firstRequest =
5569 requests.begin();
5570 nextRequest = *firstRequest;
5571 mRequestQueue.insert(mRequestQueue.end(),
5572 ++firstRequest,
5573 requests.end());
5574 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07005575
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005576 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07005577
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005578 break;
5579 }
5580
5581 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
5582
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005583 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
5584 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005585 Mutex::Autolock pl(mPauseLock);
5586 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005587 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005588 mPaused = true;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07005589 if (mNotifyPipelineDrain) {
5590 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
5591 mNotifyPipelineDrain = false;
5592 mStreamIdsToBeDrained.clear();
5593 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08005594 // Let the tracker know
5595 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5596 if (statusTracker != 0) {
5597 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
5598 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005599 sp<Camera3Device> parent = mParent.promote();
5600 if (parent != nullptr) {
5601 parent->mRequestBufferSM.onRequestThreadPaused();
5602 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005603 }
5604 // Stop waiting for now and let thread management happen
5605 return NULL;
5606 }
5607 }
5608
5609 if (nextRequest == NULL) {
5610 // Don't have a repeating request already in hand, so queue
5611 // must have an entry now.
5612 RequestList::iterator firstRequest =
5613 mRequestQueue.begin();
5614 nextRequest = *firstRequest;
5615 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07005616 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
5617 sp<NotificationListener> listener = mListener.promote();
5618 if (listener != NULL) {
5619 listener->notifyRequestQueueEmpty();
5620 }
5621 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005622 }
5623
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005624 // In case we've been unpaused by setPaused clearing mDoPause, need to
5625 // update internal pause state (capture/setRepeatingRequest unpause
5626 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005627 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005628 if (mPaused) {
5629 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
5630 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5631 if (statusTracker != 0) {
5632 statusTracker->markComponentActive(mStatusId);
5633 }
5634 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005635 mPaused = false;
5636
5637 // Check if we've reconfigured since last time, and reset the preview
5638 // request if so. Can't use 'NULL request == repeat' across configure calls.
5639 if (mReconfigured) {
5640 mPrevRequest.clear();
5641 mReconfigured = false;
5642 }
5643
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005644 if (nextRequest != NULL) {
5645 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07005646 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
5647 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005648
5649 // Since RequestThread::clear() removes buffers from the input stream,
5650 // get the right buffer here before unlocking mRequestLock
5651 if (nextRequest->mInputStream != NULL) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08005652 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer,
5653 &nextRequest->mInputBufferSize);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005654 if (res != OK) {
5655 // Can't get input buffer from gralloc queue - this could be due to
5656 // disconnected queue or other producer misbehavior, so not a fatal
5657 // error
5658 ALOGE("%s: Can't get input buffer, skipping request:"
5659 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005660
5661 sp<NotificationListener> listener = mListener.promote();
5662 if (listener != NULL) {
5663 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08005664 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07005665 nextRequest->mResultExtras);
5666 }
5667 return NULL;
5668 }
5669 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07005670 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07005671
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005672 return nextRequest;
5673}
5674
5675bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005676 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005677 status_t res;
5678 Mutex::Autolock l(mPauseLock);
5679 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005680 if (mPaused == false) {
5681 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005682 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07005683 if (mNotifyPipelineDrain) {
5684 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
5685 mNotifyPipelineDrain = false;
5686 mStreamIdsToBeDrained.clear();
5687 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08005688 // Let the tracker know
5689 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5690 if (statusTracker != 0) {
5691 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
5692 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005693 sp<Camera3Device> parent = mParent.promote();
5694 if (parent != nullptr) {
5695 parent->mRequestBufferSM.onRequestThreadPaused();
5696 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005697 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005698
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005699 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005700 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08005701 return true;
5702 }
5703 }
5704 // We don't set mPaused to false here, because waitForNextRequest needs
5705 // to further manage the paused state in case of starvation.
5706 return false;
5707}
5708
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005709void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005710 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005711 // With work to do, mark thread as unpaused.
5712 // If paused by request (setPaused), don't resume, to avoid
5713 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005714 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005715 Mutex::Autolock p(mPauseLock);
5716 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07005717 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
5718 if (mPaused) {
5719 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5720 if (statusTracker != 0) {
5721 statusTracker->markComponentActive(mStatusId);
5722 }
5723 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07005724 mPaused = false;
5725 }
5726}
5727
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07005728void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
5729 sp<Camera3Device> parent = mParent.promote();
5730 if (parent != NULL) {
5731 va_list args;
5732 va_start(args, fmt);
5733
5734 parent->setErrorStateV(fmt, args);
5735
5736 va_end(args);
5737 }
5738}
5739
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005740status_t Camera3Device::RequestThread::insertTriggers(
5741 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005742 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005743 Mutex::Autolock al(mTriggerMutex);
5744
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005745 sp<Camera3Device> parent = mParent.promote();
5746 if (parent == NULL) {
5747 CLOGE("RequestThread: Parent is gone");
5748 return DEAD_OBJECT;
5749 }
5750
Emilian Peevaebbe412018-01-15 13:53:24 +00005751 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005752 size_t count = mTriggerMap.size();
5753
5754 for (size_t i = 0; i < count; ++i) {
5755 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005756 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005757
5758 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
5759 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
5760 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07005761 if (isAeTrigger) {
5762 request->mResultExtras.precaptureTriggerId = triggerId;
5763 mCurrentPreCaptureTriggerId = triggerId;
5764 } else {
5765 request->mResultExtras.afTriggerId = triggerId;
5766 mCurrentAfTriggerId = triggerId;
5767 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01005768 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07005769 }
5770
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005771 camera_metadata_entry entry = metadata.find(tag);
5772
5773 if (entry.count > 0) {
5774 /**
5775 * Already has an entry for this trigger in the request.
5776 * Rewrite it with our requested trigger value.
5777 */
5778 RequestTrigger oldTrigger = trigger;
5779
5780 oldTrigger.entryValue = entry.data.u8[0];
5781
5782 mTriggerReplacedMap.add(tag, oldTrigger);
5783 } else {
5784 /**
5785 * More typical, no trigger entry, so we just add it
5786 */
5787 mTriggerRemovedMap.add(tag, trigger);
5788 }
5789
5790 status_t res;
5791
5792 switch (trigger.getTagType()) {
5793 case TYPE_BYTE: {
5794 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
5795 res = metadata.update(tag,
5796 &entryValue,
5797 /*count*/1);
5798 break;
5799 }
5800 case TYPE_INT32:
5801 res = metadata.update(tag,
5802 &trigger.entryValue,
5803 /*count*/1);
5804 break;
5805 default:
5806 ALOGE("%s: Type not supported: 0x%x",
5807 __FUNCTION__,
5808 trigger.getTagType());
5809 return INVALID_OPERATION;
5810 }
5811
5812 if (res != OK) {
5813 ALOGE("%s: Failed to update request metadata with trigger tag %s"
5814 ", value %d", __FUNCTION__, trigger.getTagName(),
5815 trigger.entryValue);
5816 return res;
5817 }
5818
5819 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
5820 trigger.getTagName(),
5821 trigger.entryValue);
5822 }
5823
5824 mTriggerMap.clear();
5825
5826 return count;
5827}
5828
5829status_t Camera3Device::RequestThread::removeTriggers(
5830 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005831 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005832 Mutex::Autolock al(mTriggerMutex);
5833
Emilian Peevaebbe412018-01-15 13:53:24 +00005834 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005835
5836 /**
5837 * Replace all old entries with their old values.
5838 */
5839 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
5840 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
5841
5842 status_t res;
5843
5844 uint32_t tag = trigger.metadataTag;
5845 switch (trigger.getTagType()) {
5846 case TYPE_BYTE: {
5847 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
5848 res = metadata.update(tag,
5849 &entryValue,
5850 /*count*/1);
5851 break;
5852 }
5853 case TYPE_INT32:
5854 res = metadata.update(tag,
5855 &trigger.entryValue,
5856 /*count*/1);
5857 break;
5858 default:
5859 ALOGE("%s: Type not supported: 0x%x",
5860 __FUNCTION__,
5861 trigger.getTagType());
5862 return INVALID_OPERATION;
5863 }
5864
5865 if (res != OK) {
5866 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
5867 ", trigger value %d", __FUNCTION__,
5868 trigger.getTagName(), trigger.entryValue);
5869 return res;
5870 }
5871 }
5872 mTriggerReplacedMap.clear();
5873
5874 /**
5875 * Remove all new entries.
5876 */
5877 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
5878 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
5879 status_t res = metadata.erase(trigger.metadataTag);
5880
5881 if (res != OK) {
5882 ALOGE("%s: Failed to erase metadata with trigger tag %s"
5883 ", trigger value %d", __FUNCTION__,
5884 trigger.getTagName(), trigger.entryValue);
5885 return res;
5886 }
5887 }
5888 mTriggerRemovedMap.clear();
5889
5890 return OK;
5891}
5892
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04005893status_t Camera3Device::RequestThread::addFakeTriggerIds(
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005894 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08005895 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04005896 static const int32_t fakeTriggerId = 1;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005897 status_t res;
5898
Emilian Peevaebbe412018-01-15 13:53:24 +00005899 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005900
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04005901 // If AF trigger is active, insert a fake AF trigger ID if none already
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005902 // exists
5903 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
5904 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
5905 if (afTrigger.count > 0 &&
5906 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
5907 afId.count == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04005908 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005909 if (res != OK) return res;
5910 }
5911
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04005912 // If AE precapture trigger is active, insert a fake precapture trigger ID
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005913 // if none already exists
5914 camera_metadata_entry pcTrigger =
5915 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
5916 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
5917 if (pcTrigger.count > 0 &&
5918 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
5919 pcId.count == 0) {
5920 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04005921 &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07005922 if (res != OK) return res;
5923 }
5924
5925 return OK;
5926}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005927
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005928bool Camera3Device::RequestThread::overrideAutoRotateAndCrop(
5929 const sp<CaptureRequest> &request) {
5930 ATRACE_CALL();
5931
5932 if (request->mRotateAndCropAuto) {
5933 Mutex::Autolock l(mTriggerMutex);
5934 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
5935
5936 auto rotateAndCropEntry = metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
5937 if (rotateAndCropEntry.count > 0) {
5938 if (rotateAndCropEntry.data.u8[0] == mRotateAndCropOverride) {
5939 return false;
5940 } else {
5941 rotateAndCropEntry.data.u8[0] = mRotateAndCropOverride;
5942 return true;
5943 }
5944 } else {
5945 uint8_t rotateAndCrop_u8 = mRotateAndCropOverride;
5946 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP,
5947 &rotateAndCrop_u8, 1);
5948 return true;
5949 }
5950 }
5951 return false;
5952}
5953
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005954bool Camera3Device::RequestThread::overrideTestPattern(
5955 const sp<CaptureRequest> &request) {
5956 ATRACE_CALL();
5957
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07005958 if (!mSupportCameraMute) return false;
Eino-Ville Talvalac2459842021-07-22 16:36:36 -07005959
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005960 Mutex::Autolock l(mTriggerMutex);
5961
5962 bool changed = false;
5963
5964 int32_t testPatternMode = request->mOriginalTestPatternMode;
5965 int32_t testPatternData[4] = {
5966 request->mOriginalTestPatternData[0],
5967 request->mOriginalTestPatternData[1],
5968 request->mOriginalTestPatternData[2],
5969 request->mOriginalTestPatternData[3]
5970 };
5971
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07005972 if (mCameraMute != ANDROID_SENSOR_TEST_PATTERN_MODE_OFF) {
5973 testPatternMode = mCameraMute;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005974 testPatternData[0] = 0;
5975 testPatternData[1] = 0;
5976 testPatternData[2] = 0;
5977 testPatternData[3] = 0;
5978 }
5979
5980 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
5981
5982 auto testPatternEntry = metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
5983 if (testPatternEntry.count > 0) {
5984 if (testPatternEntry.data.i32[0] != testPatternMode) {
5985 testPatternEntry.data.i32[0] = testPatternMode;
5986 changed = true;
5987 }
5988 } else {
5989 metadata.update(ANDROID_SENSOR_TEST_PATTERN_MODE,
5990 &testPatternMode, 1);
5991 changed = true;
5992 }
5993
5994 auto testPatternColor = metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
Eino-Ville Talvalac2459842021-07-22 16:36:36 -07005995 if (testPatternColor.count >= 4) {
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005996 for (size_t i = 0; i < 4; i++) {
Eino-Ville Talvalac2459842021-07-22 16:36:36 -07005997 if (testPatternColor.data.i32[i] != testPatternData[i]) {
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005998 testPatternColor.data.i32[i] = testPatternData[i];
5999 changed = true;
6000 }
6001 }
6002 } else {
6003 metadata.update(ANDROID_SENSOR_TEST_PATTERN_DATA,
Eino-Ville Talvalac2459842021-07-22 16:36:36 -07006004 testPatternData, 4);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08006005 changed = true;
6006 }
6007
6008 return changed;
6009}
6010
Cliff Wuc2ad9c82021-04-21 00:58:58 +08006011status_t Camera3Device::RequestThread::setHalInterface(
6012 sp<HalInterface> newHalInterface) {
6013 if (newHalInterface.get() == nullptr) {
6014 ALOGE("%s: The newHalInterface does not exist!", __FUNCTION__);
6015 return DEAD_OBJECT;
6016 }
6017
6018 mInterface = newHalInterface;
6019
6020 return OK;
6021}
6022
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006023/**
6024 * PreparerThread inner class methods
6025 */
6026
6027Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07006028 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006029 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006030}
6031
6032Camera3Device::PreparerThread::~PreparerThread() {
6033 Thread::requestExitAndWait();
6034 if (mCurrentStream != nullptr) {
6035 mCurrentStream->cancelPrepare();
6036 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
6037 mCurrentStream.clear();
6038 }
6039 clear();
6040}
6041
Ruben Brunkc78ac262015-08-13 17:58:46 -07006042status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006043 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006044 status_t res;
6045
6046 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006047 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006048
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07006049 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006050 if (res == OK) {
6051 // No preparation needed, fire listener right off
6052 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006053 if (listener != NULL) {
6054 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006055 }
6056 return OK;
6057 } else if (res != NOT_ENOUGH_DATA) {
6058 return res;
6059 }
6060
6061 // Need to prepare, start up thread if necessary
6062 if (!mActive) {
6063 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
6064 // isn't running
6065 Thread::requestExitAndWait();
6066 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
6067 if (res != OK) {
6068 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006069 if (listener != NULL) {
6070 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006071 }
6072 return res;
6073 }
6074 mCancelNow = false;
6075 mActive = true;
6076 ALOGV("%s: Preparer stream started", __FUNCTION__);
6077 }
6078
6079 // queue up the work
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006080 mPendingStreams.emplace(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006081 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
6082
6083 return OK;
6084}
6085
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006086void Camera3Device::PreparerThread::pause() {
6087 ATRACE_CALL();
6088
6089 Mutex::Autolock l(mLock);
6090
6091 std::unordered_map<int, sp<camera3::Camera3StreamInterface> > pendingStreams;
6092 pendingStreams.insert(mPendingStreams.begin(), mPendingStreams.end());
6093 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
6094 int currentMaxCount = mCurrentMaxCount;
6095 mPendingStreams.clear();
6096 mCancelNow = true;
6097 while (mActive) {
6098 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
6099 if (res == TIMED_OUT) {
6100 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
6101 return;
6102 } else if (res != OK) {
6103 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
6104 return;
6105 }
6106 }
6107
6108 //Check whether the prepare thread was able to complete the current
6109 //stream. In case work is still pending emplace it along with the rest
6110 //of the streams in the pending list.
6111 if (currentStream != nullptr) {
6112 if (!mCurrentPrepareComplete) {
6113 pendingStreams.emplace(currentMaxCount, currentStream);
6114 }
6115 }
6116
6117 mPendingStreams.insert(pendingStreams.begin(), pendingStreams.end());
6118 for (const auto& it : mPendingStreams) {
6119 it.second->cancelPrepare();
6120 }
6121}
6122
6123status_t Camera3Device::PreparerThread::resume() {
6124 ATRACE_CALL();
6125 status_t res;
6126
6127 Mutex::Autolock l(mLock);
6128 sp<NotificationListener> listener = mListener.promote();
6129
6130 if (mActive) {
6131 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
6132 return NO_INIT;
6133 }
6134
6135 auto it = mPendingStreams.begin();
6136 for (; it != mPendingStreams.end();) {
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07006137 res = it->second->startPrepare(it->first, true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006138 if (res == OK) {
6139 if (listener != NULL) {
6140 listener->notifyPrepared(it->second->getId());
6141 }
6142 it = mPendingStreams.erase(it);
6143 } else if (res != NOT_ENOUGH_DATA) {
6144 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
6145 res, strerror(-res));
6146 it = mPendingStreams.erase(it);
6147 } else {
6148 it++;
6149 }
6150 }
6151
6152 if (mPendingStreams.empty()) {
6153 return OK;
6154 }
6155
6156 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
6157 if (res != OK) {
6158 ALOGE("%s: Unable to start preparer stream: %d (%s)",
6159 __FUNCTION__, res, strerror(-res));
6160 return res;
6161 }
6162 mCancelNow = false;
6163 mActive = true;
6164 ALOGV("%s: Preparer stream started", __FUNCTION__);
6165
6166 return OK;
6167}
6168
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006169status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006170 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006171 Mutex::Autolock l(mLock);
6172
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006173 for (const auto& it : mPendingStreams) {
6174 it.second->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006175 }
6176 mPendingStreams.clear();
6177 mCancelNow = true;
6178
6179 return OK;
6180}
6181
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006182void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07006183 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006184 Mutex::Autolock l(mLock);
6185 mListener = listener;
6186}
6187
6188bool Camera3Device::PreparerThread::threadLoop() {
6189 status_t res;
6190 {
6191 Mutex::Autolock l(mLock);
6192 if (mCurrentStream == nullptr) {
6193 // End thread if done with work
6194 if (mPendingStreams.empty()) {
6195 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
6196 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
6197 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
6198 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006199 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006200 return false;
6201 }
6202
6203 // Get next stream to prepare
6204 auto it = mPendingStreams.begin();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006205 mCurrentStream = it->second;
6206 mCurrentMaxCount = it->first;
6207 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006208 mPendingStreams.erase(it);
6209 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
6210 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
6211 } else if (mCancelNow) {
6212 mCurrentStream->cancelPrepare();
6213 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
6214 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
6215 mCurrentStream.clear();
6216 mCancelNow = false;
6217 return true;
6218 }
6219 }
6220
6221 res = mCurrentStream->prepareNextBuffer();
6222 if (res == NOT_ENOUGH_DATA) return true;
6223 if (res != OK) {
6224 // Something bad happened; try to recover by cancelling prepare and
6225 // signalling listener anyway
6226 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
6227 mCurrentStream->getId(), res, strerror(-res));
6228 mCurrentStream->cancelPrepare();
6229 }
6230
6231 // This stream has finished, notify listener
6232 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006233 sp<NotificationListener> listener = mListener.promote();
6234 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006235 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
6236 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07006237 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006238 }
6239
6240 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
6241 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00006242 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07006243
6244 return true;
6245}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07006246
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006247status_t Camera3Device::RequestBufferStateMachine::initialize(
6248 sp<camera3::StatusTracker> statusTracker) {
6249 if (statusTracker == nullptr) {
6250 ALOGE("%s: statusTracker is null", __FUNCTION__);
6251 return BAD_VALUE;
6252 }
6253
6254 std::lock_guard<std::mutex> lock(mLock);
6255 mStatusTracker = statusTracker;
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07006256 mRequestBufferStatusId = statusTracker->addComponent("BufferRequestSM");
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006257 return OK;
6258}
6259
6260bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
6261 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08006262 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006263 mRequestBufferOngoing = true;
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08006264 notifyTrackerLocked(/*active*/true);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006265 return true;
6266 }
6267 return false;
6268}
6269
6270void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
6271 std::lock_guard<std::mutex> lock(mLock);
6272 if (!mRequestBufferOngoing) {
6273 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
6274 return;
6275 }
6276 mRequestBufferOngoing = false;
6277 if (mStatus == RB_STATUS_PENDING_STOP) {
6278 checkSwitchToStopLocked();
6279 }
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08006280 notifyTrackerLocked(/*active*/false);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006281}
6282
6283void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
6284 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006285 mSwitchedToOffline = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006286 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006287 return;
6288}
6289
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08006290void Camera3Device::RequestBufferStateMachine::onSubmittingRequest() {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006291 std::lock_guard<std::mutex> lock(mLock);
6292 mRequestThreadPaused = false;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08006293 // inflight map register actually happens in prepareHalRequest now, but it is close enough
6294 // approximation.
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006295 mInflightMapEmpty = false;
6296 if (mStatus == RB_STATUS_STOPPED) {
6297 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006298 }
6299 return;
6300}
6301
6302void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
6303 std::lock_guard<std::mutex> lock(mLock);
6304 mRequestThreadPaused = true;
6305 if (mStatus == RB_STATUS_PENDING_STOP) {
6306 checkSwitchToStopLocked();
6307 }
6308 return;
6309}
6310
6311void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
6312 std::lock_guard<std::mutex> lock(mLock);
6313 mInflightMapEmpty = true;
6314 if (mStatus == RB_STATUS_PENDING_STOP) {
6315 checkSwitchToStopLocked();
6316 }
6317 return;
6318}
6319
6320void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
6321 std::lock_guard<std::mutex> lock(mLock);
6322 if (!checkSwitchToStopLocked()) {
6323 mStatus = RB_STATUS_PENDING_STOP;
6324 }
6325 return;
6326}
6327
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006328bool Camera3Device::RequestBufferStateMachine::onSwitchToOfflineSuccess() {
6329 std::lock_guard<std::mutex> lock(mLock);
6330 if (mRequestBufferOngoing) {
6331 ALOGE("%s: HAL must not be requesting buffer after HAL returns switchToOffline!",
6332 __FUNCTION__);
6333 return false;
6334 }
6335 mSwitchedToOffline = true;
6336 mInflightMapEmpty = true;
6337 mRequestThreadPaused = true;
6338 mStatus = RB_STATUS_STOPPED;
6339 return true;
6340}
6341
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006342void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
6343 sp<StatusTracker> statusTracker = mStatusTracker.promote();
6344 if (statusTracker != nullptr) {
6345 if (active) {
6346 statusTracker->markComponentActive(mRequestBufferStatusId);
6347 } else {
6348 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
6349 }
6350 }
6351}
6352
6353bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
6354 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
6355 mStatus = RB_STATUS_STOPPED;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07006356 return true;
6357 }
6358 return false;
6359}
6360
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006361bool Camera3Device::startRequestBuffer() {
6362 return mRequestBufferSM.startRequestBuffer();
6363}
Shuzhen Wang268a1362018-10-16 16:32:59 -07006364
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006365void Camera3Device::endRequestBuffer() {
6366 mRequestBufferSM.endRequestBuffer();
6367}
Shuzhen Wang268a1362018-10-16 16:32:59 -07006368
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006369nsecs_t Camera3Device::getWaitDuration() {
6370 return kBaseGetBufferWait + getExpectedInFlightDuration();
6371}
Shuzhen Wang268a1362018-10-16 16:32:59 -07006372
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006373void Camera3Device::getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) {
6374 mInterface->getInflightBufferKeys(out);
6375}
Shuzhen Wang268a1362018-10-16 16:32:59 -07006376
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006377void Camera3Device::getInflightRequestBufferKeys(std::vector<uint64_t>* out) {
6378 mInterface->getInflightRequestBufferKeys(out);
6379}
Shuzhen Wang268a1362018-10-16 16:32:59 -07006380
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006381std::vector<sp<Camera3StreamInterface>> Camera3Device::getAllStreams() {
6382 std::vector<sp<Camera3StreamInterface>> ret;
6383 bool hasInputStream = mInputStream != nullptr;
6384 ret.reserve(mOutputStreams.size() + mDeletedStreams.size() + ((hasInputStream) ? 1 : 0));
6385 if (hasInputStream) {
6386 ret.push_back(mInputStream);
Shuzhen Wang268a1362018-10-16 16:32:59 -07006387 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006388 for (size_t i = 0; i < mOutputStreams.size(); i++) {
6389 ret.push_back(mOutputStreams[i]);
6390 }
6391 for (size_t i = 0; i < mDeletedStreams.size(); i++) {
6392 ret.push_back(mDeletedStreams[i]);
6393 }
6394 return ret;
Shuzhen Wang268a1362018-10-16 16:32:59 -07006395}
6396
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006397status_t Camera3Device::switchToOffline(
6398 const std::vector<int32_t>& streamsToKeep,
6399 /*out*/ sp<CameraOfflineSessionBase>* session) {
6400 ATRACE_CALL();
6401 if (session == nullptr) {
6402 ALOGE("%s: session must not be null", __FUNCTION__);
6403 return BAD_VALUE;
6404 }
6405
6406 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006407
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006408 bool hasInputStream = mInputStream != nullptr;
6409 int32_t inputStreamId = hasInputStream ? mInputStream->getId() : -1;
6410 bool inputStreamSupportsOffline = hasInputStream ?
6411 mInputStream->getOfflineProcessingSupport() : false;
6412 auto outputStreamIds = mOutputStreams.getStreamIds();
6413 auto streamIds = outputStreamIds;
6414 if (hasInputStream) {
6415 streamIds.push_back(mInputStream->getId());
6416 }
6417
6418 // Check all streams in streamsToKeep supports offline mode
6419 for (auto id : streamsToKeep) {
6420 if (std::find(streamIds.begin(), streamIds.end(), id) == streamIds.end()) {
6421 ALOGE("%s: Unknown stream ID %d", __FUNCTION__, id);
6422 return BAD_VALUE;
6423 } else if (id == inputStreamId) {
6424 if (!inputStreamSupportsOffline) {
6425 ALOGE("%s: input stream %d cannot be switched to offline",
6426 __FUNCTION__, id);
6427 return BAD_VALUE;
6428 }
6429 } else {
6430 sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
6431 if (!stream->getOfflineProcessingSupport()) {
6432 ALOGE("%s: output stream %d cannot be switched to offline",
6433 __FUNCTION__, id);
6434 return BAD_VALUE;
6435 }
6436 }
6437 }
6438
6439 // TODO: block surface sharing and surface group streams until we can support them
6440
6441 // Stop repeating request, wait until all remaining requests are submitted, then call into
6442 // HAL switchToOffline
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006443 hardware::camera::device::V3_6::CameraOfflineSessionInfo offlineSessionInfo;
6444 sp<hardware::camera::device::V3_6::ICameraOfflineSession> offlineSession;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006445 camera3::BufferRecords bufferRecords;
6446 status_t ret = mRequestThread->switchToOffline(
6447 streamsToKeep, &offlineSessionInfo, &offlineSession, &bufferRecords);
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006448
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006449 if (ret != OK) {
6450 SET_ERR("Switch to offline failed: %s (%d)", strerror(-ret), ret);
6451 return ret;
6452 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006453
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006454 bool succ = mRequestBufferSM.onSwitchToOfflineSuccess();
6455 if (!succ) {
6456 SET_ERR("HAL must not be calling requestStreamBuffers call");
6457 // TODO: block ALL callbacks from HAL till app configured new streams?
6458 return UNKNOWN_ERROR;
6459 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006460
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006461 // Verify offlineSessionInfo
6462 std::vector<int32_t> offlineStreamIds;
6463 offlineStreamIds.reserve(offlineSessionInfo.offlineStreams.size());
6464 for (auto offlineStream : offlineSessionInfo.offlineStreams) {
6465 // verify stream IDs
6466 int32_t id = offlineStream.id;
6467 if (std::find(streamIds.begin(), streamIds.end(), id) == streamIds.end()) {
6468 SET_ERR("stream ID %d not found!", id);
6469 return UNKNOWN_ERROR;
6470 }
6471
6472 // When not using HAL buf manager, only allow streams requested by app to be preserved
6473 if (!mUseHalBufManager) {
6474 if (std::find(streamsToKeep.begin(), streamsToKeep.end(), id) == streamsToKeep.end()) {
6475 SET_ERR("stream ID %d must not be switched to offline!", id);
6476 return UNKNOWN_ERROR;
6477 }
6478 }
6479
6480 offlineStreamIds.push_back(id);
6481 sp<Camera3StreamInterface> stream = (id == inputStreamId) ?
6482 static_cast<sp<Camera3StreamInterface>>(mInputStream) :
6483 static_cast<sp<Camera3StreamInterface>>(mOutputStreams.get(id));
6484 // Verify number of outstanding buffers
6485 if (stream->getOutstandingBuffersCount() != offlineStream.numOutstandingBuffers) {
6486 SET_ERR("Offline stream %d # of remaining buffer mismatch: (%zu,%d) (service/HAL)",
6487 id, stream->getOutstandingBuffersCount(), offlineStream.numOutstandingBuffers);
6488 return UNKNOWN_ERROR;
6489 }
6490 }
6491
6492 // Verify all streams to be deleted don't have any outstanding buffers
6493 if (hasInputStream && std::find(offlineStreamIds.begin(), offlineStreamIds.end(),
6494 inputStreamId) == offlineStreamIds.end()) {
6495 if (mInputStream->hasOutstandingBuffers()) {
6496 SET_ERR("Input stream %d still has %zu outstanding buffer!",
6497 inputStreamId, mInputStream->getOutstandingBuffersCount());
6498 return UNKNOWN_ERROR;
6499 }
6500 }
6501
6502 for (const auto& outStreamId : outputStreamIds) {
6503 if (std::find(offlineStreamIds.begin(), offlineStreamIds.end(),
6504 outStreamId) == offlineStreamIds.end()) {
6505 auto outStream = mOutputStreams.get(outStreamId);
6506 if (outStream->hasOutstandingBuffers()) {
6507 SET_ERR("Output stream %d still has %zu outstanding buffer!",
6508 outStreamId, outStream->getOutstandingBuffersCount());
6509 return UNKNOWN_ERROR;
6510 }
6511 }
6512 }
6513
6514 InFlightRequestMap offlineReqs;
6515 // Verify inflight requests and their pending buffers
6516 {
6517 std::lock_guard<std::mutex> l(mInFlightLock);
6518 for (auto offlineReq : offlineSessionInfo.offlineRequests) {
6519 int idx = mInFlightMap.indexOfKey(offlineReq.frameNumber);
6520 if (idx == NAME_NOT_FOUND) {
6521 SET_ERR("Offline request frame number %d not found!", offlineReq.frameNumber);
6522 return UNKNOWN_ERROR;
6523 }
6524
6525 const auto& inflightReq = mInFlightMap.valueAt(idx);
6526 // TODO: check specific stream IDs
6527 size_t numBuffersLeft = static_cast<size_t>(inflightReq.numBuffersLeft);
6528 if (numBuffersLeft != offlineReq.pendingStreams.size()) {
6529 SET_ERR("Offline request # of remaining buffer mismatch: (%d,%d) (service/HAL)",
6530 inflightReq.numBuffersLeft, offlineReq.pendingStreams.size());
6531 return UNKNOWN_ERROR;
6532 }
6533 offlineReqs.add(offlineReq.frameNumber, inflightReq);
6534 }
6535 }
6536
6537 // Create Camera3OfflineSession and transfer object ownership
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006538 // (streams, inflight requests, buffer caches)
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006539 camera3::StreamSet offlineStreamSet;
6540 sp<camera3::Camera3Stream> inputStream;
6541 for (auto offlineStream : offlineSessionInfo.offlineStreams) {
6542 int32_t id = offlineStream.id;
6543 if (mInputStream != nullptr && id == mInputStream->getId()) {
6544 inputStream = mInputStream;
6545 } else {
6546 offlineStreamSet.add(id, mOutputStreams.get(id));
6547 }
6548 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006549
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006550 // TODO: check if we need to lock before copying states
6551 // though technically no other thread should be talking to Camera3Device at this point
6552 Camera3OfflineStates offlineStates(
6553 mTagMonitor, mVendorTagId, mUseHalBufManager, mNeedFixupMonochromeTags,
Shuzhen Wangb7b42652020-05-07 11:59:02 -07006554 mUsePartialResult, mNumPartialResults, mLastCompletedRegularFrameNumber,
6555 mLastCompletedReprocessFrameNumber, mLastCompletedZslFrameNumber,
6556 mNextResultFrameNumber, mNextReprocessResultFrameNumber,
6557 mNextZslStillResultFrameNumber, mNextShutterFrameNumber,
6558 mNextReprocessShutterFrameNumber, mNextZslStillShutterFrameNumber,
6559 mDeviceInfo, mPhysicalDeviceInfoMap, mDistortionMappers,
6560 mZoomRatioMappers, mRotateAndCropMappers);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006561
6562 *session = new Camera3OfflineSession(mId, inputStream, offlineStreamSet,
6563 std::move(bufferRecords), offlineReqs, offlineStates, offlineSession);
6564
6565 // Delete all streams that has been transferred to offline session
6566 Mutex::Autolock l(mLock);
6567 for (auto offlineStream : offlineSessionInfo.offlineStreams) {
6568 int32_t id = offlineStream.id;
6569 if (mInputStream != nullptr && id == mInputStream->getId()) {
6570 mInputStream.clear();
6571 } else {
6572 mOutputStreams.remove(id);
6573 }
6574 }
6575
6576 // disconnect all other streams and switch to UNCONFIGURED state
6577 if (mInputStream != nullptr) {
6578 ret = mInputStream->disconnect();
6579 if (ret != OK) {
6580 SET_ERR_L("disconnect input stream failed!");
6581 return UNKNOWN_ERROR;
6582 }
6583 }
6584
6585 for (auto streamId : mOutputStreams.getStreamIds()) {
6586 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
6587 ret = stream->disconnect();
6588 if (ret != OK) {
6589 SET_ERR_L("disconnect output stream %d failed!", streamId);
6590 return UNKNOWN_ERROR;
6591 }
6592 }
6593
6594 mInputStream.clear();
6595 mOutputStreams.clear();
6596 mNeedConfig = true;
6597 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
6598 mOperatingMode = NO_MODE;
6599 mIsConstrainedHighSpeedConfiguration = false;
Emilian Peevc0fe54c2020-03-11 14:05:07 -07006600 mRequestThread->clearPreviousRequest();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006601
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006602 return OK;
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08006603 // TO be done by CameraDeviceClient/Camera3OfflineSession
6604 // register the offline client to camera service
6605 // Setup result passthing threads etc
6606 // Initialize offline session so HAL can start sending callback to it (result Fmq)
6607 // TODO: check how many onIdle callback will be sent
6608 // Java side to make sure the CameraCaptureSession is properly closed
Yin-Chia Yehb978c382019-10-30 00:22:37 -07006609}
6610
Emilian Peevcc0b7952020-01-07 13:54:47 -08006611void Camera3Device::getOfflineStreamIds(std::vector<int> *offlineStreamIds) {
6612 ATRACE_CALL();
6613
6614 if (offlineStreamIds == nullptr) {
6615 return;
6616 }
6617
6618 Mutex::Autolock il(mInterfaceLock);
6619
6620 auto streamIds = mOutputStreams.getStreamIds();
6621 bool hasInputStream = mInputStream != nullptr;
6622 if (hasInputStream && mInputStream->getOfflineProcessingSupport()) {
6623 offlineStreamIds->push_back(mInputStream->getId());
6624 }
6625
6626 for (const auto & streamId : streamIds) {
6627 sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
6628 // Streams that use the camera buffer manager are currently not supported in
6629 // offline mode
6630 if (stream->getOfflineProcessingSupport() &&
6631 (stream->getStreamSetId() == CAMERA3_STREAM_SET_ID_INVALID)) {
6632 offlineStreamIds->push_back(streamId);
6633 }
6634 }
6635}
6636
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08006637status_t Camera3Device::setRotateAndCropAutoBehavior(
6638 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
6639 ATRACE_CALL();
6640 Mutex::Autolock il(mInterfaceLock);
6641 Mutex::Autolock l(mLock);
6642 if (mRequestThread == nullptr) {
6643 return INVALID_OPERATION;
6644 }
6645 return mRequestThread->setRotateAndCropAutoBehavior(rotateAndCropValue);
6646}
6647
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08006648bool Camera3Device::supportsCameraMute() {
6649 Mutex::Autolock il(mInterfaceLock);
6650 Mutex::Autolock l(mLock);
6651
6652 return mSupportCameraMute;
6653}
6654
6655status_t Camera3Device::setCameraMute(bool enabled) {
6656 ATRACE_CALL();
6657 Mutex::Autolock il(mInterfaceLock);
6658 Mutex::Autolock l(mLock);
6659
6660 if (mRequestThread == nullptr || !mSupportCameraMute) {
6661 return INVALID_OPERATION;
6662 }
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07006663 int32_t muteMode =
6664 !enabled ? ANDROID_SENSOR_TEST_PATTERN_MODE_OFF :
6665 mSupportTestPatternSolidColor ? ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR :
6666 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK;
6667 return mRequestThread->setCameraMute(muteMode);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08006668}
6669
Cliff Wuc2ad9c82021-04-21 00:58:58 +08006670status_t Camera3Device::injectCamera(const String8& injectedCamId,
6671 sp<CameraProviderManager> manager) {
6672 ALOGI("%s Injection camera: injectedCamId = %s", __FUNCTION__, injectedCamId.string());
6673 ATRACE_CALL();
6674 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08006675 // When the camera device is active, injectCamera() and stopInjection() will call
6676 // internalPauseAndWaitLocked() and internalResumeLocked(), and then they will call
6677 // mStatusChanged.waitRelative(mLock, timeout) of waitUntilStateThenRelock(). But
6678 // mStatusChanged.waitRelative(mLock, timeout)'s parameter: mutex "mLock" must be in the locked
6679 // state, so we need to add "Mutex::Autolock l(mLock)" to lock the "mLock" before calling
6680 // waitUntilStateThenRelock().
6681 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08006682
6683 status_t res = NO_ERROR;
6684 if (mInjectionMethods->isInjecting()) {
6685 if (injectedCamId == mInjectionMethods->getInjectedCamId()) {
6686 return OK;
6687 } else {
6688 res = mInjectionMethods->stopInjection();
6689 if (res != OK) {
6690 ALOGE("%s: Failed to stop the injection camera! ret != NO_ERROR: %d",
6691 __FUNCTION__, res);
6692 return res;
6693 }
6694 }
6695 }
6696
6697 res = mInjectionMethods->injectionInitialize(injectedCamId, manager, this);
6698 if (res != OK) {
6699 ALOGE("%s: Failed to initialize the injection camera! ret != NO_ERROR: %d",
6700 __FUNCTION__, res);
6701 return res;
6702 }
6703
Cliff Wuc2ad9c82021-04-21 00:58:58 +08006704 // When the second display of android is cast to the remote device, and the opened camera is
6705 // also cast to the second display, in this case, because the camera has configured the streams
6706 // at this time, we can directly call injectCamera() to replace the internal camera with
6707 // injection camera.
Cliff Wu3b268182021-07-06 15:44:43 +08006708 if (mInjectionMethods->isStreamConfigCompleteButNotInjected()) {
6709 ALOGD("%s: The opened camera is directly cast to the remote device.", __FUNCTION__);
6710
6711 camera3::camera_stream_configuration injectionConfig;
6712 std::vector<uint32_t> injectionBufferSizes;
6713 mInjectionMethods->getInjectionConfig(&injectionConfig, &injectionBufferSizes);
6714 if (mOperatingMode < 0 || injectionConfig.num_streams <= 0
6715 || injectionBufferSizes.size() <= 0) {
6716 ALOGE("Failed to inject camera due to abandoned configuration! "
6717 "mOperatingMode: %d injectionConfig.num_streams: %d "
6718 "injectionBufferSizes.size(): %zu", mOperatingMode,
6719 injectionConfig.num_streams, injectionBufferSizes.size());
6720 return DEAD_OBJECT;
6721 }
6722
Cliff Wuc2ad9c82021-04-21 00:58:58 +08006723 res = mInjectionMethods->injectCamera(
6724 injectionConfig, injectionBufferSizes);
6725 if (res != OK) {
6726 ALOGE("Can't finish inject camera process!");
6727 return res;
6728 }
6729 }
6730
6731 return OK;
6732}
6733
6734status_t Camera3Device::stopInjection() {
6735 ALOGI("%s: Injection camera: stopInjection", __FUNCTION__);
6736 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08006737 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08006738 return mInjectionMethods->stopInjection();
6739}
6740
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08006741}; // namespace android