blob: e5228d315ce93eb9325ce5046ee33cef40ccb963 [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
Eino-Ville Talvalaf67e23e2014-07-23 17:17:59 -070055#include "CameraService.h"
Emilian Peeve579d8b2023-02-28 14:16:08 -080056#include "aidl/android/hardware/graphics/common/Dataspace.h"
Emilian Peeva1b26262023-02-06 17:27:41 -080057#include "aidl/AidlUtils.h"
58#include "device3/Camera3Device.h"
59#include "device3/Camera3FakeStream.h"
60#include "device3/Camera3InputStream.h"
61#include "device3/Camera3OutputStream.h"
62#include "device3/Camera3SharedOutputStream.h"
63#include "mediautils/SchedulingPolicyService.h"
Jayant Chowdhary12361932018-08-27 14:46:13 -070064#include "utils/CameraThreadState.h"
Emilian Peeva1b26262023-02-06 17:27:41 -080065#include "utils/CameraTraces.h"
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -080066#include "utils/SessionConfigurationUtils.h"
Jayant Chowdharyd4776262020-06-23 23:45:57 -070067#include "utils/TraceHFR.h"
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080068
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -080069#include <algorithm>
Yin-Chia Yeh84be5782019-03-01 11:47:02 -080070#include <tuple>
71
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -080072using namespace android::camera3;
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080073using namespace android::hardware::camera;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -080074
75namespace android {
76
Austin Borger74fca042022-05-23 12:41:21 -070077Camera3Device::Camera3Device(std::shared_ptr<CameraServiceProxyWrapper>& cameraServiceProxyWrapper,
Austin Borger18b30a72022-10-27 12:20:29 -070078 const String8 &id, bool overrideForPerfClass, bool overrideToPortrait, bool legacyClient):
Austin Borger74fca042022-05-23 12:41:21 -070079 mCameraServiceProxyWrapper(cameraServiceProxyWrapper),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -080080 mId(id),
Emilian Peev5104fe92021-10-21 14:27:09 -070081 mLegacyClient(legacyClient),
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -080082 mOperatingMode(NO_MODE),
Eino-Ville Talvala9a179412015-06-09 13:15:16 -070083 mIsConstrainedHighSpeedConfiguration(false),
Emilian Peev15230142023-04-27 20:22:54 +000084 mIsCompositeJpegRDisabled(false),
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -070085 mStatus(STATUS_UNINITIALIZED),
Ruben Brunk183f0562015-08-12 12:55:02 -070086 mStatusWaiters(0),
Zhijun He204e3292014-07-14 17:09:23 -070087 mUsePartialResult(false),
88 mNumPartialResults(1),
Shuzhen Wange4208922022-02-01 16:52:48 -080089 mDeviceTimeBaseIsRealtime(false),
Shuzhen Wangc28dccc2016-02-11 23:48:46 -080090 mTimestampOffset(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070091 mNextResultFrameNumber(0),
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -070092 mNextReprocessResultFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070093 mNextZslStillResultFrameNumber(0),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -070094 mNextShutterFrameNumber(0),
Chien-Yu Chen3df11ce2015-09-30 14:13:30 -070095 mNextReprocessShutterFrameNumber(0),
Shuzhen Wang5ee99842019-04-12 11:55:48 -070096 mNextZslStillShutterFrameNumber(0),
Emilian Peev71c73a22017-03-21 16:35:51 +000097 mListener(NULL),
Emilian Peev811d2952018-05-25 11:08:40 +010098 mVendorTagId(CAMERA_METADATA_INVALID_VENDOR_ID),
Shuzhen Wang268a1362018-10-16 16:32:59 -070099 mLastTemplateId(-1),
Shuzhen Wangd4abdf72021-05-28 11:22:50 -0700100 mNeedFixupMonochromeTags(false),
Austin Borger18b30a72022-10-27 12:20:29 -0700101 mOverrideForPerfClass(overrideForPerfClass),
Austin Borgerc8099762023-01-12 17:08:46 -0800102 mOverrideToPortrait(overrideToPortrait),
Emilian Peevd865f0d2023-03-17 17:13:07 -0700103 mRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE),
104 mComposerOutput(false),
105 mAutoframingOverride(ANDROID_CONTROL_AUTOFRAMING_OFF),
Shuzhen Wangaf22e912023-04-11 16:03:17 -0700106 mSettingsOverride(-1),
Austin Borgerc8099762023-01-12 17:08:46 -0800107 mActivePhysicalId("")
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800108{
109 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800110 ALOGV("%s: Created device for camera %s", __FUNCTION__, mId.string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800111}
112
113Camera3Device::~Camera3Device()
114{
115 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800116 ALOGV("%s: Tearing down for camera id %s", __FUNCTION__, mId.string());
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700117 disconnectImpl();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800118}
119
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800120const String8& Camera3Device::getId() const {
Igor Murashkin71381052013-03-04 14:53:08 -0800121 return mId;
122}
123
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800124status_t Camera3Device::initializeCommonLocked() {
125
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700126 /** Start up status tracker thread */
127 mStatusTracker = new StatusTracker(this);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800128 status_t res = mStatusTracker->run(String8::format("C3Dev-%s-Status", mId.string()).string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700129 if (res != OK) {
130 SET_ERR_L("Unable to start status tracking thread: %s (%d)",
131 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800132 mInterface->close();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700133 mStatusTracker.clear();
134 return res;
135 }
136
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700137 /** Register in-flight map to the status tracker */
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -0700138 mInFlightStatusId = mStatusTracker->addComponent("InflightRequests");
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -0700139
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -0700140 if (mUseHalBufManager) {
141 res = mRequestBufferSM.initialize(mStatusTracker);
142 if (res != OK) {
143 SET_ERR_L("Unable to start request buffer state machine: %s (%d)",
144 strerror(-res), res);
145 mInterface->close();
146 mStatusTracker.clear();
147 return res;
148 }
149 }
150
Yin-Chia Yeh651fe2e2018-11-13 11:49:31 -0800151 /** Create buffer manager */
152 mBufferManager = new Camera3BufferManager();
153
154 Vector<int32_t> sessionParamKeys;
155 camera_metadata_entry_t sessionKeysEntry = mDeviceInfo.find(
156 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
157 if (sessionKeysEntry.count > 0) {
158 sessionParamKeys.insertArrayAt(sessionKeysEntry.data.i32, 0, sessionKeysEntry.count);
159 }
160
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -0700161 camera_metadata_entry_t availableTestPatternModes = mDeviceInfo.find(
162 ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES);
163 for (size_t i = 0; i < availableTestPatternModes.count; i++) {
164 if (availableTestPatternModes.data.i32[i] ==
165 ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR) {
166 mSupportCameraMute = true;
167 mSupportTestPatternSolidColor = true;
168 break;
169 } else if (availableTestPatternModes.data.i32[i] ==
170 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK) {
171 mSupportCameraMute = true;
172 mSupportTestPatternSolidColor = false;
173 }
174 }
175
Shuzhen Wangaf22e912023-04-11 16:03:17 -0700176 camera_metadata_entry_t availableSettingsOverrides = mDeviceInfo.find(
177 ANDROID_CONTROL_AVAILABLE_SETTINGS_OVERRIDES);
178 for (size_t i = 0; i < availableSettingsOverrides.count; i++) {
179 if (availableSettingsOverrides.data.i32[i] ==
180 ANDROID_CONTROL_SETTINGS_OVERRIDE_ZOOM) {
181 mSupportZoomOverride = true;
182 break;
183 }
184 }
185
Chien-Yu Chenab5135b2015-06-30 11:20:58 -0700186 /** Start up request queue thread */
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800187 mRequestThread = createNewRequestThread(
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -0700188 this, mStatusTracker, mInterface, sessionParamKeys,
Shuzhen Wangaf22e912023-04-11 16:03:17 -0700189 mUseHalBufManager, mSupportCameraMute, mOverrideToPortrait,
190 mSupportZoomOverride);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800191 res = mRequestThread->run(String8::format("C3Dev-%s-ReqQueue", mId.string()).string());
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800192 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700193 SET_ERR_L("Unable to start request queue thread: %s (%d)",
194 strerror(-res), res);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800195 mInterface->close();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800196 mRequestThread.clear();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800197 return res;
198 }
199
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -0700200 mPreparerThread = new PreparerThread();
201
Ruben Brunk183f0562015-08-12 12:55:02 -0700202 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800203 mNextStreamId = 0;
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -0400204 mFakeStreamId = NO_STREAM;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -0700205 mNeedConfig = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700206 mPauseStateNotify = false;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800207 mIsInputStreamMultiResolution = false;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800208
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800209 // Measure the clock domain offset between camera and video/hw_composer
Shuzhen Wange4208922022-02-01 16:52:48 -0800210 mTimestampOffset = getMonoToBoottimeOffset();
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800211 camera_metadata_entry timestampSource =
212 mDeviceInfo.find(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE);
213 if (timestampSource.count > 0 && timestampSource.data.u8[0] ==
214 ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
Shuzhen Wange4208922022-02-01 16:52:48 -0800215 mDeviceTimeBaseIsRealtime = true;
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800216 }
217
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700218 // Will the HAL be sending in early partial result metadata?
Emilian Peev08dd2452017-04-06 16:55:14 +0100219 camera_metadata_entry partialResultsCount =
220 mDeviceInfo.find(ANDROID_REQUEST_PARTIAL_RESULT_COUNT);
221 if (partialResultsCount.count > 0) {
222 mNumPartialResults = partialResultsCount.data.i32[0];
223 mUsePartialResult = (mNumPartialResults > 1);
Eino-Ville Talvalafd6ecdd2013-10-11 09:51:09 -0700224 }
225
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800226 bool usePrecorrectArray = DistortionMapper::isDistortionSupported(mDeviceInfo);
227 if (usePrecorrectArray) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -0700228 res = mDistortionMappers[mId.c_str()].setupStaticInfo(mDeviceInfo);
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -0700229 if (res != OK) {
230 SET_ERR_L("Unable to read necessary calibration fields for distortion correction");
231 return res;
232 }
233 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800234
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800235 mZoomRatioMappers[mId.c_str()] = ZoomRatioMapper(&mDeviceInfo,
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800236 mSupportNativeZoomRatio, usePrecorrectArray);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800237
Jayant Chowdharydbd1efb2023-02-07 16:14:48 -0800238 if (SessionConfigurationUtils::supportsUltraHighResolutionCapture(mDeviceInfo)) {
Jayant Chowdhary9255ce02021-07-15 11:18:17 -0700239 mUHRCropAndMeteringRegionMappers[mId.c_str()] =
240 UHRCropAndMeteringRegionMapper(mDeviceInfo, usePrecorrectArray);
241 }
242
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800243 if (RotateAndCropMapper::isNeeded(&mDeviceInfo)) {
244 mRotateAndCropMappers.emplace(mId.c_str(), &mDeviceInfo);
245 }
246
Jayant Chowdhary22441f32021-12-26 18:35:41 -0800247 // Hidl/AidlCamera3DeviceInjectionMethods
248 mInjectionMethods = createCamera3DeviceInjectionMethods(this);
Cliff Wuc2ad9c82021-04-21 00:58:58 +0800249
Ravneetaeb20dc2022-03-30 05:33:03 +0000250 /** Start watchdog thread */
Shuzhen Wang03fe6232023-02-05 12:41:15 -0800251 mCameraServiceWatchdog = new CameraServiceWatchdog(mId, mCameraServiceProxyWrapper);
Austin Borger7b129542022-06-09 13:23:06 -0700252 res = mCameraServiceWatchdog->run("CameraServiceWatchdog");
253 if (res != OK) {
254 SET_ERR_L("Unable to start camera service watchdog thread: %s (%d)",
255 strerror(-res), res);
256 return res;
257 }
Ravneetaeb20dc2022-03-30 05:33:03 +0000258
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800259 return OK;
260}
261
262status_t Camera3Device::disconnect() {
Yin-Chia Yehc5248132018-08-15 12:19:20 -0700263 return disconnectImpl();
264}
265
266status_t Camera3Device::disconnectImpl() {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800267 ATRACE_CALL();
Bharatt Kukreja5681f782023-01-18 21:56:28 +0000268 Mutex::Autolock il(mInterfaceLock);
269
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700270 ALOGI("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800271
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700272 status_t res = OK;
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700273 std::vector<wp<Camera3StreamInterface>> streams;
Bharatt Kukreja5681f782023-01-18 21:56:28 +0000274 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700275 {
Bharatt Kukreja5681f782023-01-18 21:56:28 +0000276 Mutex::Autolock l(mLock);
277 if (mStatus == STATUS_UNINITIALIZED) return res;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700278
Bharatt Kukreja5681f782023-01-18 21:56:28 +0000279 if (mRequestThread != NULL) {
280 if (mStatus == STATUS_ACTIVE || mStatus == STATUS_ERROR) {
281 res = mRequestThread->clear();
282 if (res != OK) {
283 SET_ERR_L("Can't stop streaming");
284 // Continue to close device even in case of error
285 } else {
Bharatt Kukrejaf6f0ca12023-01-26 19:56:30 +0000286 res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration,
287 /*requestThreadInvocation*/ false);
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800288 if (res != OK) {
Bharatt Kukreja5681f782023-01-18 21:56:28 +0000289 SET_ERR_L("Timeout waiting for HAL to drain (% " PRIi64 " ns)",
290 maxExpectedDuration);
Jayant Chowdhary646c31b2020-01-30 13:09:59 -0800291 // Continue to close device even in case of error
292 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700293 }
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700294 }
Bharatt Kukreja5681f782023-01-18 21:56:28 +0000295 // Signal to request thread that we're not expecting any
296 // more requests. This will be true since once we're in
297 // disconnect and we've cleared off the request queue, the
298 // request thread can't receive any new requests through
299 // binder calls - since disconnect holds
300 // mBinderSerialization lock.
301 mRequestThread->setRequestClearing();
302 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800303
Bharatt Kukreja5681f782023-01-18 21:56:28 +0000304 if (mStatus == STATUS_ERROR) {
305 CLOGE("Shutting down in an error state");
306 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700307
Bharatt Kukreja5681f782023-01-18 21:56:28 +0000308 if (mStatusTracker != NULL) {
309 mStatusTracker->requestExit();
310 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700311
Bharatt Kukreja5681f782023-01-18 21:56:28 +0000312 if (mRequestThread != NULL) {
313 mRequestThread->requestExit();
314 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700315
Bharatt Kukreja5681f782023-01-18 21:56:28 +0000316 streams.reserve(mOutputStreams.size() + (mInputStream != nullptr ? 1 : 0));
317 for (size_t i = 0; i < mOutputStreams.size(); i++) {
318 streams.push_back(mOutputStreams[i]);
319 }
320 if (mInputStream != nullptr) {
321 streams.push_back(mInputStream);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700322 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700323 }
Bharatt Kukreja5681f782023-01-18 21:56:28 +0000324
325 // Joining done without holding mLock, otherwise deadlocks may ensue
326 // as the threads try to access parent state
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700327 if (mRequestThread != NULL && mStatus != STATUS_ERROR) {
328 // HAL may be in a bad state, so waiting for request thread
329 // (which may be stuck in the HAL processCaptureRequest call)
330 // could be dangerous.
331 mRequestThread->join();
332 }
Bharatt Kukreja5681f782023-01-18 21:56:28 +0000333
334 if (mStatusTracker != NULL) {
335 mStatusTracker->join();
336 }
337
338 if (mInjectionMethods->isInjecting()) {
339 mInjectionMethods->stopInjection();
340 }
341
342 HalInterface* interface;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700343 {
Bharatt Kukreja5681f782023-01-18 21:56:28 +0000344 Mutex::Autolock l(mLock);
345 mRequestThread.clear();
346 Mutex::Autolock stLock(mTrackerLock);
347 mStatusTracker.clear();
348 interface = mInterface.get();
349 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800350
Bharatt Kukreja5681f782023-01-18 21:56:28 +0000351 // Call close without internal mutex held, as the HAL close may need to
352 // wait on assorted callbacks,etc, to complete before it can return.
353 mCameraServiceWatchdog->WATCH(interface->close());
Cliff Wuc2ad9c82021-04-21 00:58:58 +0800354
Bharatt Kukreja5681f782023-01-18 21:56:28 +0000355 flushInflightRequests();
Eino-Ville Talvalaefff1c42015-08-28 16:27:27 -0700356
Bharatt Kukreja5681f782023-01-18 21:56:28 +0000357 {
358 Mutex::Autolock l(mLock);
359 mInterface->clear();
360 mOutputStreams.clear();
361 mInputStream.clear();
362 mDeletedStreams.clear();
363 mBufferManager.clear();
364 internalUpdateStatusLocked(STATUS_UNINITIALIZED);
365 }
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700366
Bharatt Kukreja5681f782023-01-18 21:56:28 +0000367 for (auto& weakStream : streams) {
368 sp<Camera3StreamInterface> stream = weakStream.promote();
369 if (stream != nullptr) {
370 ALOGE("%s: Stream %d leaked! strong reference (%d)!",
371 __FUNCTION__, stream->getId(), stream->getStrongCount() - 1);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -0700372 }
373 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -0700374 ALOGI("%s: X", __FUNCTION__);
Ravneetdbd5b242022-03-02 07:22:46 +0000375
376 if (mCameraServiceWatchdog != NULL) {
377 mCameraServiceWatchdog->requestExit();
378 mCameraServiceWatchdog.clear();
379 }
380
Eino-Ville Talvala214a17f2013-06-13 12:20:02 -0700381 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800382}
383
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700384// For dumping/debugging only -
385// try to acquire a lock a few times, eventually give up to proceed with
386// debug/dump operations
387bool Camera3Device::tryLockSpinRightRound(Mutex& lock) {
388 bool gotLock = false;
389 for (size_t i = 0; i < kDumpLockAttempts; ++i) {
390 if (lock.tryLock() == NO_ERROR) {
391 gotLock = true;
392 break;
393 } else {
394 usleep(kDumpSleepDuration);
395 }
396 }
397 return gotLock;
398}
399
Shuzhen Wangc28dccc2016-02-11 23:48:46 -0800400nsecs_t Camera3Device::getMonoToBoottimeOffset() {
401 // try three times to get the clock offset, choose the one
402 // with the minimum gap in measurements.
403 const int tries = 3;
404 nsecs_t bestGap, measured;
405 for (int i = 0; i < tries; ++i) {
406 const nsecs_t tmono = systemTime(SYSTEM_TIME_MONOTONIC);
407 const nsecs_t tbase = systemTime(SYSTEM_TIME_BOOTTIME);
408 const nsecs_t tmono2 = systemTime(SYSTEM_TIME_MONOTONIC);
409 const nsecs_t gap = tmono2 - tmono;
410 if (i == 0 || gap < bestGap) {
411 bestGap = gap;
412 measured = tbase - ((tmono + tmono2) >> 1);
413 }
414 }
415 return measured;
416}
417
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800418ssize_t Camera3Device::getJpegBufferSize(const CameraMetadata &info, uint32_t width,
419 uint32_t height) const {
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700420 // Get max jpeg size (area-wise) for default sensor pixel mode
421 camera3::Size maxDefaultJpegResolution =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800422 SessionConfigurationUtils::getMaxJpegResolution(info,
Jayant Chowdharydbd1efb2023-02-07 16:14:48 -0800423 /*supportsUltraHighResolutionCapture*/false);
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700424 // Get max jpeg size (area-wise) for max resolution sensor pixel mode / 0 if
425 // not ultra high res sensor
426 camera3::Size uhrMaxJpegResolution =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800427 SessionConfigurationUtils::getMaxJpegResolution(info,
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700428 /*isUltraHighResolution*/true);
429 if (maxDefaultJpegResolution.width == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800430 ALOGE("%s: Camera %s: Can't find valid available jpeg sizes in static metadata!",
431 __FUNCTION__, mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700432 return BAD_VALUE;
433 }
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700434 bool useMaxSensorPixelModeThreshold = false;
435 if (uhrMaxJpegResolution.width != 0 &&
436 width * height > maxDefaultJpegResolution.width * maxDefaultJpegResolution.height) {
437 // Use the ultra high res max jpeg size and max jpeg buffer size
438 useMaxSensorPixelModeThreshold = true;
439 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700440
Zhijun Hef7da0962014-04-24 13:27:56 -0700441 // Get max jpeg buffer size
442 ssize_t maxJpegBufferSize = 0;
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800443 camera_metadata_ro_entry jpegBufMaxSize = info.find(ANDROID_JPEG_MAX_SIZE);
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700444 if (jpegBufMaxSize.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800445 ALOGE("%s: Camera %s: Can't find maximum JPEG size in static metadata!", __FUNCTION__,
446 mId.string());
Zhijun Hef7da0962014-04-24 13:27:56 -0700447 return BAD_VALUE;
448 }
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700449 maxJpegBufferSize = jpegBufMaxSize.data.i32[0];
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700450
451 camera3::Size chosenMaxJpegResolution = maxDefaultJpegResolution;
452 if (useMaxSensorPixelModeThreshold) {
453 maxJpegBufferSize =
454 SessionConfigurationUtils::getUHRMaxJpegBufferSize(uhrMaxJpegResolution,
455 maxDefaultJpegResolution, maxJpegBufferSize);
456 chosenMaxJpegResolution = uhrMaxJpegResolution;
457 }
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800458 assert(kMinJpegBufferSize < maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700459
460 // Calculate final jpeg buffer size for the given resolution.
Yin-Chia Yehcd8fce82014-06-18 10:51:34 -0700461 float scaleFactor = ((float) (width * height)) /
Jayant Chowdharycd3d36b2021-07-10 10:53:53 -0700462 (chosenMaxJpegResolution.width * chosenMaxJpegResolution.height);
Yin-Chia Yeh0c4e56d2015-01-09 15:21:27 -0800463 ssize_t jpegBufferSize = scaleFactor * (maxJpegBufferSize - kMinJpegBufferSize) +
464 kMinJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700465 if (jpegBufferSize > maxJpegBufferSize) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800466 ALOGI("%s: jpeg buffer size calculated is > maxJpeg bufferSize(%zd), clamping",
467 __FUNCTION__, maxJpegBufferSize);
Zhijun Hef7da0962014-04-24 13:27:56 -0700468 jpegBufferSize = maxJpegBufferSize;
Zhijun Hef7da0962014-04-24 13:27:56 -0700469 }
Zhijun Hef7da0962014-04-24 13:27:56 -0700470 return jpegBufferSize;
471}
472
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800473ssize_t Camera3Device::getPointCloudBufferSize(const CameraMetadata &info) const {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700474 const int FLOATS_PER_POINT=4;
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800475 camera_metadata_ro_entry maxPointCount = info.find(ANDROID_DEPTH_MAX_DEPTH_SAMPLES);
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700476 if (maxPointCount.count == 0) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800477 ALOGE("%s: Camera %s: Can't find maximum depth point cloud size in static metadata!",
478 __FUNCTION__, mId.string());
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700479 return BAD_VALUE;
480 }
481 ssize_t maxBytesForPointCloud = sizeof(android_depth_points) +
482 maxPointCount.data.i32[0] * sizeof(float) * FLOATS_PER_POINT;
483 return maxBytesForPointCloud;
484}
485
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800486ssize_t Camera3Device::getRawOpaqueBufferSize(const CameraMetadata &info, int32_t width,
487 int32_t height, bool maxResolution) const {
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800488 const int PER_CONFIGURATION_SIZE = 3;
489 const int WIDTH_OFFSET = 0;
490 const int HEIGHT_OFFSET = 1;
491 const int SIZE_OFFSET = 2;
492 camera_metadata_ro_entry rawOpaqueSizes =
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -0800493 info.find(
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800494 camera3::SessionConfigurationUtils::getAppropriateModeTag(
495 ANDROID_SENSOR_OPAQUE_RAW_SIZE,
496 maxResolution));
Aurimas Liutikasbc57b122016-02-16 09:59:16 -0800497 size_t count = rawOpaqueSizes.count;
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800498 if (count == 0 || (count % PER_CONFIGURATION_SIZE)) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800499 ALOGE("%s: Camera %s: bad opaque RAW size static metadata length(%zu)!",
500 __FUNCTION__, mId.string(), count);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800501 return BAD_VALUE;
502 }
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700503
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800504 for (size_t i = 0; i < count; i += PER_CONFIGURATION_SIZE) {
505 if (width == rawOpaqueSizes.data.i32[i + WIDTH_OFFSET] &&
506 height == rawOpaqueSizes.data.i32[i + HEIGHT_OFFSET]) {
507 return rawOpaqueSizes.data.i32[i + SIZE_OFFSET];
508 }
509 }
510
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800511 ALOGE("%s: Camera %s: cannot find size for %dx%d opaque RAW image!",
512 __FUNCTION__, mId.string(), width, height);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -0800513 return BAD_VALUE;
514}
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -0700515
Jing Mikec7f9b132023-03-12 11:12:04 +0800516status_t Camera3Device::dump(int fd, [[maybe_unused]] const Vector<String16> &args) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800517 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700518
519 // Try to lock, but continue in case of failure (to avoid blocking in
520 // deadlocks)
521 bool gotInterfaceLock = tryLockSpinRightRound(mInterfaceLock);
522 bool gotLock = tryLockSpinRightRound(mLock);
523
524 ALOGW_IF(!gotInterfaceLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800525 "Camera %s: %s: Unable to lock interface lock, proceeding anyway",
526 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700527 ALOGW_IF(!gotLock,
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800528 "Camera %s: %s: Unable to lock main lock, proceeding anyway",
529 mId.string(), __FUNCTION__);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700530
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800531 bool dumpTemplates = false;
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700532
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800533 String16 templatesOption("-t");
534 int n = args.size();
535 for (int i = 0; i < n; i++) {
536 if (args[i] == templatesOption) {
537 dumpTemplates = true;
538 }
Emilian Peevbd8c5032018-02-14 23:05:40 +0000539 if (args[i] == TagMonitor::kMonitorOption) {
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700540 if (i + 1 < n) {
541 String8 monitorTags = String8(args[i + 1]);
542 if (monitorTags == "off") {
543 mTagMonitor.disableMonitoring();
544 } else {
545 mTagMonitor.parseTagsToMonitor(monitorTags);
546 }
547 } else {
548 mTagMonitor.disableMonitoring();
549 }
550 }
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800551 }
552
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800553 String8 lines;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800554
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800555 const char *status =
556 mStatus == STATUS_ERROR ? "ERROR" :
557 mStatus == STATUS_UNINITIALIZED ? "UNINITIALIZED" :
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700558 mStatus == STATUS_UNCONFIGURED ? "UNCONFIGURED" :
559 mStatus == STATUS_CONFIGURED ? "CONFIGURED" :
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800560 mStatus == STATUS_ACTIVE ? "ACTIVE" :
561 "Unknown";
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700562
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800563 lines.appendFormat(" Device status: %s\n", status);
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700564 if (mStatus == STATUS_ERROR) {
565 lines.appendFormat(" Error cause: %s\n", mErrorCause.string());
566 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800567 lines.appendFormat(" Stream configuration:\n");
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800568 const char *mode =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +0000569 mOperatingMode == CAMERA_STREAM_CONFIGURATION_NORMAL_MODE ? "NORMAL" :
570 mOperatingMode == CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE ?
571 "CONSTRAINED_HIGH_SPEED" : "CUSTOM";
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -0800572 lines.appendFormat(" Operation mode: %s (%d) \n", mode, mOperatingMode);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800573
574 if (mInputStream != NULL) {
575 write(fd, lines.string(), lines.size());
576 mInputStream->dump(fd, args);
577 } else {
578 lines.appendFormat(" No input stream.\n");
579 write(fd, lines.string(), lines.size());
580 }
581 for (size_t i = 0; i < mOutputStreams.size(); i++) {
582 mOutputStreams[i]->dump(fd,args);
583 }
584
Zhijun He431503c2016-03-07 17:30:16 -0800585 if (mBufferManager != NULL) {
586 lines = String8(" Camera3 Buffer Manager:\n");
587 write(fd, lines.string(), lines.size());
588 mBufferManager->dump(fd, args);
589 }
Zhijun He125684a2015-12-26 15:07:30 -0800590
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700591 lines = String8(" In-flight requests:\n");
Emilian Peeva6138c52021-06-24 12:52:38 -0700592 if (mInFlightLock.try_lock()) {
593 if (mInFlightMap.size() == 0) {
594 lines.append(" None\n");
595 } else {
596 for (size_t i = 0; i < mInFlightMap.size(); i++) {
597 InFlightRequest r = mInFlightMap.valueAt(i);
598 lines.appendFormat(" Frame %d | Timestamp: %" PRId64 ", metadata"
599 " arrived: %s, buffers left: %d\n", mInFlightMap.keyAt(i),
600 r.shutterTimestamp, r.haveResultMetadata ? "true" : "false",
601 r.numBuffersLeft);
602 }
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700603 }
Emilian Peeva6138c52021-06-24 12:52:38 -0700604 mInFlightLock.unlock();
605 } else {
606 lines.append(" Failed to acquire In-flight lock!\n");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -0700607 }
608 write(fd, lines.string(), lines.size());
609
Shuzhen Wang686f6442017-06-20 16:16:04 -0700610 if (mRequestThread != NULL) {
611 mRequestThread->dumpCaptureRequestLatency(fd,
612 " ProcessCaptureRequest latency histogram:");
613 }
614
Igor Murashkin1e479c02013-09-06 16:55:14 -0700615 {
616 lines = String8(" Last request sent:\n");
617 write(fd, lines.string(), lines.size());
618
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700619 CameraMetadata lastRequest = getLatestRequestLocked();
Igor Murashkin1e479c02013-09-06 16:55:14 -0700620 lastRequest.dump(fd, /*verbosity*/2, /*indentation*/6);
621 }
622
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800623 if (dumpTemplates) {
Emilian Peevf4816702020-04-03 15:44:51 -0700624 const char *templateNames[CAMERA_TEMPLATE_COUNT] = {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800625 "TEMPLATE_PREVIEW",
626 "TEMPLATE_STILL_CAPTURE",
627 "TEMPLATE_VIDEO_RECORD",
628 "TEMPLATE_VIDEO_SNAPSHOT",
629 "TEMPLATE_ZERO_SHUTTER_LAG",
Eino-Ville Talvala1a86df52018-01-17 16:00:35 -0800630 "TEMPLATE_MANUAL",
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800631 };
632
Emilian Peevf4816702020-04-03 15:44:51 -0700633 for (int i = 1; i < CAMERA_TEMPLATE_COUNT; i++) {
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800634 camera_metadata_t *templateRequest = nullptr;
635 mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -0700636 (camera_request_template_t) i, &templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800637 lines = String8::format(" HAL Request %s:\n", templateNames[i-1]);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800638 if (templateRequest == nullptr) {
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800639 lines.append(" Not supported\n");
640 write(fd, lines.string(), lines.size());
641 } else {
642 write(fd, lines.string(), lines.size());
643 dump_indented_camera_metadata(templateRequest,
644 fd, /*verbosity*/2, /*indentation*/8);
645 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800646 free_camera_metadata(templateRequest);
Eino-Ville Talvala7e7a62d2015-11-04 14:49:43 -0800647 }
648 }
649
Eino-Ville Talvala4d453832016-07-15 11:56:53 -0700650 mTagMonitor.dumpMonitoredMetadata(fd);
651
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800652 if (mInterface->valid()) {
Eino-Ville Talvalad00111e2017-01-31 11:59:12 -0800653 lines = String8(" HAL device dump:\n");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800654 write(fd, lines.string(), lines.size());
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800655 mInterface->dump(fd);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800656 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800657
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700658 if (gotLock) mLock.unlock();
659 if (gotInterfaceLock) mInterfaceLock.unlock();
660
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800661 return OK;
662}
663
Avichal Rakesh7e53cad2021-10-05 13:46:30 -0700664status_t Camera3Device::startWatchingTags(const String8 &tags) {
665 mTagMonitor.parseTagsToMonitor(tags);
666 return OK;
667}
668
669status_t Camera3Device::stopWatchingTags() {
670 mTagMonitor.disableMonitoring();
671 return OK;
672}
673
674status_t Camera3Device::dumpWatchedEventsToVector(std::vector<std::string> &out) {
675 mTagMonitor.getLatestMonitoredTagEvents(out);
676 return OK;
677}
678
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800679const CameraMetadata& Camera3Device::infoPhysical(const String8& physicalId) const {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800680 ALOGVV("%s: E", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800681 if (CC_UNLIKELY(mStatus == STATUS_UNINITIALIZED ||
682 mStatus == STATUS_ERROR)) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700683 ALOGW("%s: Access to static info %s!", __FUNCTION__,
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800684 mStatus == STATUS_ERROR ?
685 "when in error state" : "before init");
686 }
Shuzhen Wang2e7f58f2018-07-11 14:00:29 -0700687 if (physicalId.isEmpty()) {
688 return mDeviceInfo;
689 } else {
690 std::string id(physicalId.c_str());
691 if (mPhysicalDeviceInfoMap.find(id) != mPhysicalDeviceInfoMap.end()) {
692 return mPhysicalDeviceInfoMap.at(id);
693 } else {
694 ALOGE("%s: Invalid physical camera id %s", __FUNCTION__, physicalId.c_str());
695 return mDeviceInfo;
696 }
697 }
698}
699
700const CameraMetadata& Camera3Device::info() const {
701 String8 emptyId;
Emilian Peevfaa4bde2020-01-23 12:19:37 -0800702 return infoPhysical(emptyId);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800703}
704
Jianing Wei90e59c92014-03-12 18:29:36 -0700705status_t Camera3Device::checkStatusOkToCaptureLocked() {
706 switch (mStatus) {
707 case STATUS_ERROR:
708 CLOGE("Device has encountered a serious error");
709 return INVALID_OPERATION;
710 case STATUS_UNINITIALIZED:
711 CLOGE("Device not initialized");
712 return INVALID_OPERATION;
713 case STATUS_UNCONFIGURED:
714 case STATUS_CONFIGURED:
715 case STATUS_ACTIVE:
716 // OK
717 break;
718 default:
719 SET_ERR_L("Unexpected status: %d", mStatus);
720 return INVALID_OPERATION;
721 }
722 return OK;
723}
724
725status_t Camera3Device::convertMetadataListToRequestListLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000726 const List<const PhysicalCameraSettingsList> &metadataList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700727 const std::list<const SurfaceMap> &surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700728 bool repeating, nsecs_t requestTimeNs,
Shuzhen Wang9d066012016-09-30 11:30:20 -0700729 RequestList *requestList) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700730 if (requestList == NULL) {
731 CLOGE("requestList cannot be NULL.");
732 return BAD_VALUE;
733 }
734
Jianing Weicb0652e2014-03-12 18:29:36 -0700735 int32_t burstId = 0;
Emilian Peevaebbe412018-01-15 13:53:24 +0000736 List<const PhysicalCameraSettingsList>::const_iterator metadataIt = metadataList.begin();
Shuzhen Wang0129d522016-10-30 22:43:41 -0700737 std::list<const SurfaceMap>::const_iterator surfaceMapIt = surfaceMaps.begin();
738 for (; metadataIt != metadataList.end() && surfaceMapIt != surfaceMaps.end();
739 ++metadataIt, ++surfaceMapIt) {
740 sp<CaptureRequest> newRequest = setUpRequestLocked(*metadataIt, *surfaceMapIt);
Jianing Wei90e59c92014-03-12 18:29:36 -0700741 if (newRequest == 0) {
742 CLOGE("Can't create capture request");
743 return BAD_VALUE;
744 }
Jianing Weicb0652e2014-03-12 18:29:36 -0700745
Shuzhen Wang9d066012016-09-30 11:30:20 -0700746 newRequest->mRepeating = repeating;
Shuzhen Wang316781a2020-08-18 18:11:01 -0700747 newRequest->mRequestTimeNs = requestTimeNs;
Shuzhen Wang9d066012016-09-30 11:30:20 -0700748
Jianing Weicb0652e2014-03-12 18:29:36 -0700749 // Setup burst Id and request Id
750 newRequest->mResultExtras.burstId = burstId++;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800751 auto requestIdEntry = metadataIt->begin()->metadata.find(ANDROID_REQUEST_ID);
752 if (requestIdEntry.count == 0) {
Jianing Weicb0652e2014-03-12 18:29:36 -0700753 CLOGE("RequestID does not exist in metadata");
754 return BAD_VALUE;
755 }
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -0800756 newRequest->mResultExtras.requestId = requestIdEntry.data.i32[0];
Jianing Weicb0652e2014-03-12 18:29:36 -0700757
Jianing Wei90e59c92014-03-12 18:29:36 -0700758 requestList->push_back(newRequest);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700759
760 ALOGV("%s: requestId = %" PRId32, __FUNCTION__, newRequest->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700761 }
Shuzhen Wang0129d522016-10-30 22:43:41 -0700762 if (metadataIt != metadataList.end() || surfaceMapIt != surfaceMaps.end()) {
763 ALOGE("%s: metadataList and surfaceMaps are not the same size!", __FUNCTION__);
764 return BAD_VALUE;
765 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700766
767 // Setup batch size if this is a high speed video recording request.
768 if (mIsConstrainedHighSpeedConfiguration && requestList->size() > 0) {
769 auto firstRequest = requestList->begin();
770 for (auto& outputStream : (*firstRequest)->mOutputStreams) {
771 if (outputStream->isVideoStream()) {
772 (*firstRequest)->mBatchSize = requestList->size();
Yin-Chia Yeh14ef48d2020-02-10 15:06:37 -0800773 outputStream->setBatchSize(requestList->size());
Chien-Yu Chen85a64552015-08-28 15:46:12 -0700774 break;
775 }
776 }
777 }
778
Jianing Wei90e59c92014-03-12 18:29:36 -0700779 return OK;
780}
781
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800782status_t Camera3Device::capture(CameraMetadata &request, int64_t* lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800783 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800784
Emilian Peevaebbe412018-01-15 13:53:24 +0000785 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700786 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000787 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700788
Yin-Chia Yeh7e5a2042019-02-06 16:01:06 -0800789 return captureList(requestsList, surfaceMaps, lastFrameNumber);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700790}
791
Emilian Peevaebbe412018-01-15 13:53:24 +0000792void Camera3Device::convertToRequestList(List<const PhysicalCameraSettingsList>& requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700793 std::list<const SurfaceMap>& surfaceMaps,
794 const CameraMetadata& request) {
Emilian Peevaebbe412018-01-15 13:53:24 +0000795 PhysicalCameraSettingsList requestList;
796 requestList.push_back({std::string(getId().string()), request});
797 requestsList.push_back(requestList);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700798
799 SurfaceMap surfaceMap;
800 camera_metadata_ro_entry streams = request.find(ANDROID_REQUEST_OUTPUT_STREAMS);
801 // With no surface list passed in, stream and surface will have 1-to-1
802 // mapping. So the surface index is 0 for each stream in the surfaceMap.
803 for (size_t i = 0; i < streams.count; i++) {
804 surfaceMap[streams.data.i32[i]].push_back(0);
805 }
806 surfaceMaps.push_back(surfaceMap);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800807}
808
Jianing Wei90e59c92014-03-12 18:29:36 -0700809status_t Camera3Device::submitRequestsHelper(
Emilian Peevaebbe412018-01-15 13:53:24 +0000810 const List<const PhysicalCameraSettingsList> &requests,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700811 const std::list<const SurfaceMap> &surfaceMaps,
812 bool repeating,
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700813 /*out*/
814 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700815 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -0700816 nsecs_t requestTimeNs = systemTime();
817
Jianing Wei90e59c92014-03-12 18:29:36 -0700818 Mutex::Autolock il(mInterfaceLock);
819 Mutex::Autolock l(mLock);
820
821 status_t res = checkStatusOkToCaptureLocked();
822 if (res != OK) {
823 // error logged by previous call
824 return res;
825 }
826
827 RequestList requestList;
828
Shuzhen Wang0129d522016-10-30 22:43:41 -0700829 res = convertMetadataListToRequestListLocked(requests, surfaceMaps,
Shuzhen Wang316781a2020-08-18 18:11:01 -0700830 repeating, requestTimeNs, /*out*/&requestList);
Jianing Wei90e59c92014-03-12 18:29:36 -0700831 if (res != OK) {
832 // error logged by previous call
833 return res;
834 }
835
836 if (repeating) {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700837 res = mRequestThread->setRepeatingRequests(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700838 } else {
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700839 res = mRequestThread->queueRequestList(requestList, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700840 }
841
842 if (res == OK) {
Bharatt Kukrejaf6f0ca12023-01-26 19:56:30 +0000843 waitUntilStateThenRelock(/*active*/true, kActiveTimeout, /*requestThreadInvocation*/false);
Jianing Wei90e59c92014-03-12 18:29:36 -0700844 if (res != OK) {
845 SET_ERR_L("Can't transition to active in %f seconds!",
846 kActiveTimeout/1e9);
847 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800848 ALOGV("Camera %s: Capture request %" PRId32 " enqueued", mId.string(),
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700849 (*(requestList.begin()))->mResultExtras.requestId);
Jianing Wei90e59c92014-03-12 18:29:36 -0700850 } else {
851 CLOGE("Cannot queue request. Impossible.");
852 return BAD_VALUE;
853 }
854
855 return res;
856}
857
Emilian Peevaebbe412018-01-15 13:53:24 +0000858status_t Camera3Device::captureList(const List<const PhysicalCameraSettingsList> &requestsList,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700859 const std::list<const SurfaceMap> &surfaceMaps,
Jianing Weicb0652e2014-03-12 18:29:36 -0700860 int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700861 ATRACE_CALL();
862
Emilian Peevaebbe412018-01-15 13:53:24 +0000863 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/false, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700864}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800865
Jianing Weicb0652e2014-03-12 18:29:36 -0700866status_t Camera3Device::setStreamingRequest(const CameraMetadata &request,
867 int64_t* /*lastFrameNumber*/) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800868 ATRACE_CALL();
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800869
Emilian Peevaebbe412018-01-15 13:53:24 +0000870 List<const PhysicalCameraSettingsList> requestsList;
Shuzhen Wang0129d522016-10-30 22:43:41 -0700871 std::list<const SurfaceMap> surfaceMaps;
Emilian Peevaebbe412018-01-15 13:53:24 +0000872 convertToRequestList(requestsList, surfaceMaps, request);
Shuzhen Wang0129d522016-10-30 22:43:41 -0700873
Emilian Peevaebbe412018-01-15 13:53:24 +0000874 return setStreamingRequestList(requestsList, /*surfaceMap*/surfaceMaps,
Shuzhen Wang0129d522016-10-30 22:43:41 -0700875 /*lastFrameNumber*/NULL);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800876}
877
Emilian Peevaebbe412018-01-15 13:53:24 +0000878status_t Camera3Device::setStreamingRequestList(
879 const List<const PhysicalCameraSettingsList> &requestsList,
880 const std::list<const SurfaceMap> &surfaceMaps, int64_t *lastFrameNumber) {
Jianing Wei90e59c92014-03-12 18:29:36 -0700881 ATRACE_CALL();
882
Emilian Peevaebbe412018-01-15 13:53:24 +0000883 return submitRequestsHelper(requestsList, surfaceMaps, /*repeating*/true, lastFrameNumber);
Jianing Wei90e59c92014-03-12 18:29:36 -0700884}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800885
886sp<Camera3Device::CaptureRequest> Camera3Device::setUpRequestLocked(
Emilian Peevaebbe412018-01-15 13:53:24 +0000887 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800888 status_t res;
889
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700890 if (mStatus == STATUS_UNCONFIGURED || mNeedConfig) {
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -0800891 // This point should only be reached via API1 (API2 must explicitly call configureStreams)
892 // so unilaterally select normal operating mode.
Emilian Peevaebbe412018-01-15 13:53:24 +0000893 res = filterParamsAndConfigureLocked(request.begin()->metadata,
Emilian Peevf4816702020-04-03 15:44:51 -0700894 CAMERA_STREAM_CONFIGURATION_NORMAL_MODE);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700895 // Stream configuration failed. Client might try other configuraitons.
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800896 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700897 CLOGE("Can't set up streams: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800898 return NULL;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -0700899 } else if (mStatus == STATUS_UNCONFIGURED) {
900 // Stream configuration successfully configure to empty stream configuration.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700901 CLOGE("No streams configured");
902 return NULL;
903 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800904 }
905
Shuzhen Wang0129d522016-10-30 22:43:41 -0700906 sp<CaptureRequest> newRequest = createCaptureRequest(request, surfaceMap);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800907 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800908}
909
Jianing Weicb0652e2014-03-12 18:29:36 -0700910status_t Camera3Device::clearStreamingRequest(int64_t *lastFrameNumber) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800911 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700912 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800913 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800914
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800915 switch (mStatus) {
916 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700917 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800918 return INVALID_OPERATION;
919 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700920 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800921 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700922 case STATUS_UNCONFIGURED:
923 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800924 case STATUS_ACTIVE:
925 // OK
926 break;
927 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -0700928 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -0800929 return INVALID_OPERATION;
930 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800931 ALOGV("Camera %s: Clearing repeating request", mId.string());
Jianing Weicb0652e2014-03-12 18:29:36 -0700932
Jianing Wei2d6bb3f2014-04-11 10:00:31 -0700933 return mRequestThread->clearRepeatingRequests(lastFrameNumber);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800934}
935
936status_t Camera3Device::waitUntilRequestReceived(int32_t requestId, nsecs_t timeout) {
937 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700938 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800939
Igor Murashkin4d2f2e82013-04-01 17:29:07 -0700940 return mRequestThread->waitUntilRequestProcessed(requestId, timeout);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -0800941}
942
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700943status_t Camera3Device::createInputStream(
Shuzhen Wang83bff122020-11-20 15:51:39 -0800944 uint32_t width, uint32_t height, int format, bool isMultiResolution, int *id) {
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700945 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700946 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -0700947 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700948 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -0800949 ALOGV("Camera %s: Creating new input stream %d: %d x %d, format %d",
950 mId.string(), mNextStreamId, width, height, format);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700951
952 status_t res;
953 bool wasActive = false;
954
955 switch (mStatus) {
956 case STATUS_ERROR:
957 ALOGE("%s: Device has encountered a serious error", __FUNCTION__);
958 return INVALID_OPERATION;
959 case STATUS_UNINITIALIZED:
960 ALOGE("%s: Device not initialized", __FUNCTION__);
961 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700962 case STATUS_UNCONFIGURED:
963 case STATUS_CONFIGURED:
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700964 // OK
965 break;
966 case STATUS_ACTIVE:
967 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Bharatt Kukrejaf6f0ca12023-01-26 19:56:30 +0000968 res = internalPauseAndWaitLocked(maxExpectedDuration,
969 /*requestThreadInvocation*/ false);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700970 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700971 SET_ERR_L("Can't pause captures to reconfigure streams!");
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700972 return res;
973 }
974 wasActive = true;
975 break;
976 default:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700977 SET_ERR_L("%s: Unexpected status: %d", mStatus);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700978 return INVALID_OPERATION;
979 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700980 assert(mStatus != STATUS_ACTIVE);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700981
982 if (mInputStream != 0) {
983 ALOGE("%s: Cannot create more than 1 input stream", __FUNCTION__);
984 return INVALID_OPERATION;
985 }
986
987 sp<Camera3InputStream> newStream = new Camera3InputStream(mNextStreamId,
988 width, height, format);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -0700989 newStream->setStatusTracker(mStatusTracker);
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700990
991 mInputStream = newStream;
Shuzhen Wang83bff122020-11-20 15:51:39 -0800992 mIsInputStreamMultiResolution = isMultiResolution;
Igor Murashkin5a269fa2013-04-15 14:59:22 -0700993
994 *id = mNextStreamId++;
995
996 // Continue captures if active at start
997 if (wasActive) {
998 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +0100999 // Reuse current operating mode and session parameters for new stream config
1000 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001001 if (res != OK) {
1002 ALOGE("%s: Can't reconfigure device for new stream %d: %s (%d)",
1003 __FUNCTION__, mNextStreamId, strerror(-res), res);
1004 return res;
1005 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001006 internalResumeLocked();
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001007 }
1008
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001009 ALOGV("Camera %s: Created input stream", mId.string());
Igor Murashkin5a269fa2013-04-15 14:59:22 -07001010 return OK;
1011}
1012
Eino-Ville Talvala727d1722015-06-09 13:44:19 -07001013status_t Camera3Device::createStream(sp<Surface> consumer,
Shuzhen Wang0129d522016-10-30 22:43:41 -07001014 uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -07001015 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Shuzhen Wangc28189a2017-11-27 23:05:10 -08001016 const String8& physicalCameraId,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001017 const std::unordered_set<int32_t> &sensorPixelModesUsed,
1018 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001019 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001020 int timestampBase, int mirrorMode, int32_t colorSpace, bool useReadoutTimestamp) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001021 ATRACE_CALL();
1022
1023 if (consumer == nullptr) {
1024 ALOGE("%s: consumer must not be null", __FUNCTION__);
1025 return BAD_VALUE;
1026 }
1027
1028 std::vector<sp<Surface>> consumers;
1029 consumers.push_back(consumer);
1030
1031 return createStream(consumers, /*hasDeferredConsumer*/ false, width, height,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001032 format, dataSpace, rotation, id, physicalCameraId, sensorPixelModesUsed, surfaceIds,
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001033 streamSetId, isShared, isMultiResolution, consumerUsage, dynamicRangeProfile,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001034 streamUseCase, timestampBase, mirrorMode, colorSpace, useReadoutTimestamp);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001035}
1036
1037static bool isRawFormat(int format) {
1038 switch (format) {
1039 case HAL_PIXEL_FORMAT_RAW16:
1040 case HAL_PIXEL_FORMAT_RAW12:
1041 case HAL_PIXEL_FORMAT_RAW10:
1042 case HAL_PIXEL_FORMAT_RAW_OPAQUE:
1043 return true;
1044 default:
1045 return false;
1046 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001047}
1048
1049status_t Camera3Device::createStream(const std::vector<sp<Surface>>& consumers,
1050 bool hasDeferredConsumer, uint32_t width, uint32_t height, int format,
Emilian Peevf4816702020-04-03 15:44:51 -07001051 android_dataspace dataSpace, camera_stream_rotation_t rotation, int *id,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001052 const String8& physicalCameraId, const std::unordered_set<int32_t> &sensorPixelModesUsed,
Shuzhen Wang83bff122020-11-20 15:51:39 -08001053 std::vector<int> *surfaceIds, int streamSetId, bool isShared, bool isMultiResolution,
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001054 uint64_t consumerUsage, int64_t dynamicRangeProfile, int64_t streamUseCase,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001055 int timestampBase, int mirrorMode, int32_t colorSpace, bool useReadoutTimestamp) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001056 ATRACE_CALL();
Emilian Peev40ead602017-09-26 15:46:36 +01001057
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001058 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001059 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001060 Mutex::Autolock l(mLock);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001061 ALOGV("Camera %s: Creating new stream %d: %d x %d, format %d, dataspace %d rotation %d"
Shuzhen Wange4208922022-02-01 16:52:48 -08001062 " consumer usage %" PRIu64 ", isShared %d, physicalCameraId %s, isMultiResolution %d"
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001063 " dynamicRangeProfile 0x%" PRIx64 ", streamUseCase %" PRId64 ", timestampBase %d,"
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001064 " mirrorMode %d, colorSpace %d, useReadoutTimestamp %d",
Shuzhen Wang83bff122020-11-20 15:51:39 -08001065 mId.string(), mNextStreamId, width, height, format, dataSpace, rotation,
Shuzhen Wange4208922022-02-01 16:52:48 -08001066 consumerUsage, isShared, physicalCameraId.string(), isMultiResolution,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001067 dynamicRangeProfile, streamUseCase, timestampBase, mirrorMode, colorSpace,
1068 useReadoutTimestamp);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001069
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001070 status_t res;
1071 bool wasActive = false;
1072
1073 switch (mStatus) {
1074 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001075 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001076 return INVALID_OPERATION;
1077 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001078 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001079 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001080 case STATUS_UNCONFIGURED:
1081 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001082 // OK
1083 break;
1084 case STATUS_ACTIVE:
1085 ALOGV("%s: Stopping activity to reconfigure streams", __FUNCTION__);
Bharatt Kukrejaf6f0ca12023-01-26 19:56:30 +00001086 res = internalPauseAndWaitLocked(maxExpectedDuration,
1087 /*requestThreadInvocation*/ false);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001088 if (res != OK) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001089 SET_ERR_L("Can't pause captures to reconfigure streams!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001090 return res;
1091 }
1092 wasActive = true;
1093 break;
1094 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001095 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001096 return INVALID_OPERATION;
1097 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001098 assert(mStatus != STATUS_ACTIVE);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001099
1100 sp<Camera3OutputStream> newStream;
Zhijun He5d677d12016-05-29 16:52:39 -07001101
Shuzhen Wang0129d522016-10-30 22:43:41 -07001102 if (consumers.size() == 0 && !hasDeferredConsumer) {
1103 ALOGE("%s: Number of consumers cannot be smaller than 1", __FUNCTION__);
1104 return BAD_VALUE;
1105 }
Zhijun He5d677d12016-05-29 16:52:39 -07001106
Shuzhen Wang0129d522016-10-30 22:43:41 -07001107 if (hasDeferredConsumer && format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
Zhijun He5d677d12016-05-29 16:52:39 -07001108 ALOGE("Deferred consumer stream creation only support IMPLEMENTATION_DEFINED format");
1109 return BAD_VALUE;
1110 }
1111
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001112 if (isRawFormat(format) && sensorPixelModesUsed.size() > 1) {
1113 // We can't use one stream with a raw format in both sensor pixel modes since its going to
1114 // be found in only one sensor pixel mode.
1115 ALOGE("%s: RAW opaque stream cannot be used with > 1 sensor pixel modes", __FUNCTION__);
1116 return BAD_VALUE;
1117 }
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001118 IPCTransport transport = getTransportType();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001119 if (format == HAL_PIXEL_FORMAT_BLOB) {
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001120 ssize_t blobBufferSize;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001121 if (dataSpace == HAL_DATASPACE_DEPTH) {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001122 blobBufferSize = getPointCloudBufferSize(infoPhysical(physicalCameraId));
Eino-Ville Talvala95a1d0f2015-08-11 15:08:53 -07001123 if (blobBufferSize <= 0) {
1124 SET_ERR_L("Invalid point cloud buffer size %zd", blobBufferSize);
1125 return BAD_VALUE;
1126 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001127 } else if (dataSpace == static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
1128 blobBufferSize = width * height;
1129 } else {
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001130 blobBufferSize = getJpegBufferSize(infoPhysical(physicalCameraId), width, height);
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08001131 if (blobBufferSize <= 0) {
1132 SET_ERR_L("Invalid jpeg buffer size %zd", blobBufferSize);
1133 return BAD_VALUE;
1134 }
Zhijun Hef7da0962014-04-24 13:27:56 -07001135 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001136 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001137 width, height, blobBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001138 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001139 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001140 timestampBase, mirrorMode, colorSpace, useReadoutTimestamp);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001141 } else if (format == HAL_PIXEL_FORMAT_RAW_OPAQUE) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -08001142 bool maxResolution =
1143 sensorPixelModesUsed.find(ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) !=
1144 sensorPixelModesUsed.end();
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08001145 ssize_t rawOpaqueBufferSize = getRawOpaqueBufferSize(infoPhysical(physicalCameraId), width,
1146 height, maxResolution);
Yin-Chia Yehe9154ce2015-12-07 14:38:04 -08001147 if (rawOpaqueBufferSize <= 0) {
1148 SET_ERR_L("Invalid RAW opaque buffer size %zd", rawOpaqueBufferSize);
1149 return BAD_VALUE;
1150 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07001151 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001152 width, height, rawOpaqueBufferSize, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001153 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001154 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001155 timestampBase, mirrorMode, colorSpace, useReadoutTimestamp);
Shuzhen Wang758c2152017-01-10 18:26:18 -08001156 } else if (isShared) {
1157 newStream = new Camera3SharedOutputStream(mNextStreamId, consumers,
1158 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001159 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001160 mUseHalBufManager, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001161 timestampBase, mirrorMode, colorSpace, useReadoutTimestamp);
Shuzhen Wang0129d522016-10-30 22:43:41 -07001162 } else if (consumers.size() == 0 && hasDeferredConsumer) {
Zhijun He5d677d12016-05-29 16:52:39 -07001163 newStream = new Camera3OutputStream(mNextStreamId,
1164 width, height, format, consumerUsage, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001165 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001166 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001167 timestampBase, mirrorMode, colorSpace, useReadoutTimestamp);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001168 } else {
Shuzhen Wang0129d522016-10-30 22:43:41 -07001169 newStream = new Camera3OutputStream(mNextStreamId, consumers[0],
Shuzhen Wangc28dccc2016-02-11 23:48:46 -08001170 width, height, format, dataSpace, rotation,
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00001171 mTimestampOffset, physicalCameraId, sensorPixelModesUsed, transport, streamSetId,
Shuzhen Wange4208922022-02-01 16:52:48 -08001172 isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
Shuzhen Wangbce53db2022-12-03 00:38:20 +00001173 timestampBase, mirrorMode, colorSpace, useReadoutTimestamp);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001174 }
Emilian Peev40ead602017-09-26 15:46:36 +01001175
1176 size_t consumerCount = consumers.size();
1177 for (size_t i = 0; i < consumerCount; i++) {
1178 int id = newStream->getSurfaceId(consumers[i]);
1179 if (id < 0) {
1180 SET_ERR_L("Invalid surface id");
1181 return BAD_VALUE;
1182 }
1183 if (surfaceIds != nullptr) {
1184 surfaceIds->push_back(id);
1185 }
1186 }
1187
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001188 newStream->setStatusTracker(mStatusTracker);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001189
Emilian Peev08dd2452017-04-06 16:55:14 +01001190 newStream->setBufferManager(mBufferManager);
Zhijun He125684a2015-12-26 15:07:30 -08001191
Shuzhen Wangabbcb6b2020-12-09 22:32:44 -08001192 newStream->setImageDumpMask(mImageDumpMask);
1193
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001194 res = mOutputStreams.add(mNextStreamId, newStream);
1195 if (res < 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001196 SET_ERR_L("Can't add new stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001197 return res;
1198 }
1199
Shuzhen Wang316781a2020-08-18 18:11:01 -07001200 mSessionStatsBuilder.addStream(mNextStreamId);
1201
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001202 *id = mNextStreamId++;
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001203 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001204
1205 // Continue captures if active at start
1206 if (wasActive) {
1207 ALOGV("%s: Restarting activity to reconfigure streams", __FUNCTION__);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001208 // Reuse current operating mode and session parameters for new stream config
1209 res = configureStreamsLocked(mOperatingMode, mSessionParams);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001210 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001211 CLOGE("Can't reconfigure device for new stream %d: %s (%d)",
1212 mNextStreamId, strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001213 return res;
1214 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001215 internalResumeLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001216 }
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001217 ALOGV("Camera %s: Created new stream", mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001218 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001219}
1220
Emilian Peev710c1422017-08-30 11:19:38 +01001221status_t Camera3Device::getStreamInfo(int id, StreamInfo *streamInfo) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001222 ATRACE_CALL();
Emilian Peev710c1422017-08-30 11:19:38 +01001223 if (nullptr == streamInfo) {
1224 return BAD_VALUE;
1225 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001226 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001227 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001228
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001229 switch (mStatus) {
1230 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001231 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001232 return INVALID_OPERATION;
1233 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001234 CLOGE("Device not initialized!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001235 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001236 case STATUS_UNCONFIGURED:
1237 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001238 case STATUS_ACTIVE:
1239 // OK
1240 break;
1241 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001242 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001243 return INVALID_OPERATION;
1244 }
1245
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001246 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
1247 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001248 CLOGE("Stream %d is unknown", id);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001249 return BAD_VALUE;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001250 }
1251
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001252 streamInfo->width = stream->getWidth();
1253 streamInfo->height = stream->getHeight();
1254 streamInfo->format = stream->getFormat();
1255 streamInfo->dataSpace = stream->getDataSpace();
1256 streamInfo->formatOverridden = stream->isFormatOverridden();
1257 streamInfo->originalFormat = stream->getOriginalFormat();
1258 streamInfo->dataSpaceOverridden = stream->isDataSpaceOverridden();
1259 streamInfo->originalDataSpace = stream->getOriginalDataSpace();
Emilian Peev2295df72021-11-12 18:14:10 -08001260 streamInfo->dynamicRangeProfile = stream->getDynamicRangeProfile();
Austin Borger9e2b27c2022-07-15 11:27:24 -07001261 streamInfo->colorSpace = stream->getColorSpace();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001262 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001263}
1264
1265status_t Camera3Device::setStreamTransform(int id,
1266 int transform) {
1267 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001268 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001269 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001270
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001271 switch (mStatus) {
1272 case STATUS_ERROR:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001273 CLOGE("Device has encountered a serious error");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001274 return INVALID_OPERATION;
1275 case STATUS_UNINITIALIZED:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001276 CLOGE("Device not initialized");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001277 return INVALID_OPERATION;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001278 case STATUS_UNCONFIGURED:
1279 case STATUS_CONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001280 case STATUS_ACTIVE:
1281 // OK
1282 break;
1283 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001284 SET_ERR_L("Unexpected status: %d", mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001285 return INVALID_OPERATION;
1286 }
1287
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001288 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(id);
1289 if (stream == nullptr) {
1290 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001291 return BAD_VALUE;
1292 }
Shuzhen Wang610d7b82022-02-08 14:37:22 -08001293 return stream->setTransform(transform, false /*mayChangeMirror*/);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001294}
1295
1296status_t Camera3Device::deleteStream(int id) {
1297 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001298 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001299 Mutex::Autolock l(mLock);
1300 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001301
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001302 ALOGV("%s: Camera %s: Deleting stream %d", __FUNCTION__, mId.string(), id);
Igor Murashkine2172be2013-05-28 15:31:39 -07001303
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001304 // CameraDevice semantics require device to already be idle before
1305 // deleteStream is called, unlike for createStream.
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001306 if (mStatus == STATUS_ACTIVE) {
Yin-Chia Yeh693047d2018-03-08 12:14:19 -08001307 ALOGW("%s: Camera %s: Device not idle", __FUNCTION__, mId.string());
Igor Murashkin52827132013-05-13 14:53:44 -07001308 return -EBUSY;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001309 }
1310
Yin-Chia Yeh5090c732017-07-20 16:05:29 -07001311 if (mStatus == STATUS_ERROR) {
1312 ALOGW("%s: Camera %s: deleteStream not allowed in ERROR state",
1313 __FUNCTION__, mId.string());
1314 return -EBUSY;
1315 }
1316
Igor Murashkin2fba5842013-04-22 14:03:54 -07001317 sp<Camera3StreamInterface> deletedStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001318 sp<Camera3StreamInterface> stream = mOutputStreams.get(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001319 if (mInputStream != NULL && id == mInputStream->getId()) {
1320 deletedStream = mInputStream;
1321 mInputStream.clear();
1322 } else {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001323 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001324 CLOGE("Stream %d does not exist", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001325 return BAD_VALUE;
1326 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07001327 mSessionStatsBuilder.removeStream(id);
Zhijun He5f446352014-01-22 09:49:33 -08001328 }
1329
1330 // Delete output stream or the output part of a bi-directional stream.
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001331 if (stream != nullptr) {
1332 deletedStream = stream;
1333 mOutputStreams.remove(id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001334 }
1335
1336 // Free up the stream endpoint so that it can be used by some other stream
1337 res = deletedStream->disconnect();
1338 if (res != OK) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001339 SET_ERR_L("Can't disconnect deleted stream %d", id);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001340 // fall through since we want to still list the stream as deleted.
1341 }
1342 mDeletedStreams.add(deletedStream);
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07001343 mNeedConfig = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001344
1345 return res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001346}
1347
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001348status_t Camera3Device::configureStreams(const CameraMetadata& sessionParams, int operatingMode) {
Igor Murashkine2d167e2014-08-19 16:19:59 -07001349 ATRACE_CALL();
1350 ALOGV("%s: E", __FUNCTION__);
1351
1352 Mutex::Autolock il(mInterfaceLock);
1353 Mutex::Autolock l(mLock);
Chien-Yu Chen17338fc2015-06-18 16:30:12 -07001354
Emilian Peev811d2952018-05-25 11:08:40 +01001355 // In case the client doesn't include any session parameter, try a
1356 // speculative configuration using the values from the last cached
1357 // default request.
1358 if (sessionParams.isEmpty() &&
Emilian Peevf4816702020-04-03 15:44:51 -07001359 ((mLastTemplateId > 0) && (mLastTemplateId < CAMERA_TEMPLATE_COUNT)) &&
Emilian Peev811d2952018-05-25 11:08:40 +01001360 (!mRequestTemplateCache[mLastTemplateId].isEmpty())) {
1361 ALOGV("%s: Speculative session param configuration with template id: %d", __func__,
1362 mLastTemplateId);
1363 return filterParamsAndConfigureLocked(mRequestTemplateCache[mLastTemplateId],
1364 operatingMode);
1365 }
1366
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001367 return filterParamsAndConfigureLocked(sessionParams, operatingMode);
1368}
1369
1370status_t Camera3Device::filterParamsAndConfigureLocked(const CameraMetadata& sessionParams,
1371 int operatingMode) {
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001372 //Filter out any incoming session parameters
1373 const CameraMetadata params(sessionParams);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001374 camera_metadata_entry_t availableSessionKeys = mDeviceInfo.find(
1375 ANDROID_REQUEST_AVAILABLE_SESSION_KEYS);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00001376 CameraMetadata filteredParams(availableSessionKeys.count);
1377 camera_metadata_t *meta = const_cast<camera_metadata_t *>(
1378 filteredParams.getAndLock());
1379 set_camera_metadata_vendor_id(meta, mVendorTagId);
1380 filteredParams.unlock(meta);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001381 if (availableSessionKeys.count > 0) {
Emilian Peevd865f0d2023-03-17 17:13:07 -07001382 bool rotateAndCropSessionKey = false;
1383 bool autoframingSessionKey = false;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001384 for (size_t i = 0; i < availableSessionKeys.count; i++) {
1385 camera_metadata_ro_entry entry = params.find(
1386 availableSessionKeys.data.i32[i]);
1387 if (entry.count > 0) {
1388 filteredParams.update(entry);
1389 }
Emilian Peevd865f0d2023-03-17 17:13:07 -07001390 if (ANDROID_SCALER_ROTATE_AND_CROP == availableSessionKeys.data.i32[i]) {
1391 rotateAndCropSessionKey = true;
1392 }
1393 if (ANDROID_CONTROL_AUTOFRAMING == availableSessionKeys.data.i32[i]) {
1394 autoframingSessionKey = true;
1395 }
1396 }
1397
1398 if (rotateAndCropSessionKey || autoframingSessionKey) {
1399 sp<CaptureRequest> request = new CaptureRequest();
1400 PhysicalCameraSettings settingsList;
1401 settingsList.metadata = filteredParams;
1402 request->mSettingsList.push_back(settingsList);
1403
1404 if (rotateAndCropSessionKey) {
Emilian Peevd865f0d2023-03-17 17:13:07 -07001405 auto rotateAndCropEntry = filteredParams.find(ANDROID_SCALER_ROTATE_AND_CROP);
1406 if (rotateAndCropEntry.count > 0 &&
1407 rotateAndCropEntry.data.u8[0] == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
1408 request->mRotateAndCropAuto = true;
1409 } else {
1410 request->mRotateAndCropAuto = false;
1411 }
1412
1413 overrideAutoRotateAndCrop(request, mOverrideToPortrait, mRotateAndCropOverride);
1414 }
1415
1416 if (autoframingSessionKey) {
1417 auto autoframingEntry = filteredParams.find(ANDROID_CONTROL_AUTOFRAMING);
1418 if (autoframingEntry.count > 0 &&
1419 autoframingEntry.data.u8[0] == ANDROID_CONTROL_AUTOFRAMING_AUTO) {
1420 overrideAutoframing(request, mAutoframingOverride);
1421 }
1422 }
1423
1424 filteredParams = request->mSettingsList.begin()->metadata;
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01001425 }
1426 }
1427
1428 return configureStreamsLocked(operatingMode, filteredParams);
Igor Murashkine2d167e2014-08-19 16:19:59 -07001429}
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001430
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001431status_t Camera3Device::getInputBufferProducer(
1432 sp<IGraphicBufferProducer> *producer) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001433 ATRACE_CALL();
Chien-Yu Chen618ff8a2015-03-13 11:27:17 -07001434 Mutex::Autolock il(mInterfaceLock);
1435 Mutex::Autolock l(mLock);
1436
1437 if (producer == NULL) {
1438 return BAD_VALUE;
1439 } else if (mInputStream == NULL) {
1440 return INVALID_OPERATION;
1441 }
1442
1443 return mInputStream->getInputBufferProducer(producer);
1444}
1445
Emilian Peevf4816702020-04-03 15:44:51 -07001446status_t Camera3Device::createDefaultRequest(camera_request_template_t templateId,
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001447 CameraMetadata *request) {
1448 ATRACE_CALL();
Alex Rayfe7e0c62013-05-30 00:12:13 -07001449 ALOGV("%s: for template %d", __FUNCTION__, templateId);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001450
Emilian Peevf4816702020-04-03 15:44:51 -07001451 if (templateId <= 0 || templateId >= CAMERA_TEMPLATE_COUNT) {
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001452 android_errorWriteWithInfoLog(CameraService::SN_EVENT_LOG_ID, "26866110",
Jayant Chowdhary12361932018-08-27 14:46:13 -07001453 CameraThreadState::getCallingUid(), nullptr, 0);
Chien-Yu Chen9cd14022016-03-09 12:21:01 -08001454 return BAD_VALUE;
1455 }
1456
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001457 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001458
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001459 {
1460 Mutex::Autolock l(mLock);
1461 switch (mStatus) {
1462 case STATUS_ERROR:
1463 CLOGE("Device has encountered a serious error");
1464 return INVALID_OPERATION;
1465 case STATUS_UNINITIALIZED:
1466 CLOGE("Device is not initialized!");
1467 return INVALID_OPERATION;
1468 case STATUS_UNCONFIGURED:
1469 case STATUS_CONFIGURED:
1470 case STATUS_ACTIVE:
1471 // OK
1472 break;
1473 default:
1474 SET_ERR_L("Unexpected status: %d", mStatus);
1475 return INVALID_OPERATION;
1476 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001477
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001478 if (!mRequestTemplateCache[templateId].isEmpty()) {
1479 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001480 mLastTemplateId = templateId;
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001481 return OK;
1482 }
Zhijun Hea1530f12014-09-14 12:44:20 -07001483 }
1484
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001485 camera_metadata_t *rawRequest;
1486 status_t res = mInterface->constructDefaultRequestSettings(
Emilian Peevf4816702020-04-03 15:44:51 -07001487 (camera_request_template_t) templateId, &rawRequest);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001488
1489 {
1490 Mutex::Autolock l(mLock);
1491 if (res == BAD_VALUE) {
1492 ALOGI("%s: template %d is not supported on this camera device",
1493 __FUNCTION__, templateId);
1494 return res;
1495 } else if (res != OK) {
1496 CLOGE("Unable to construct request template %d: %s (%d)",
1497 templateId, strerror(-res), res);
1498 return res;
1499 }
1500
1501 set_camera_metadata_vendor_id(rawRequest, mVendorTagId);
1502 mRequestTemplateCache[templateId].acquire(rawRequest);
1503
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08001504 // Override the template request with zoomRatioMapper
1505 res = mZoomRatioMappers[mId.c_str()].initZoomRatioInTemplate(
1506 &mRequestTemplateCache[templateId]);
1507 if (res != OK) {
1508 CLOGE("Failed to update zoom ratio for template %d: %s (%d)",
1509 templateId, strerror(-res), res);
1510 return res;
1511 }
1512
Shuzhen Wangd25dc972020-03-24 17:11:43 -07001513 // Fill in JPEG_QUALITY if not available
1514 if (!mRequestTemplateCache[templateId].exists(ANDROID_JPEG_QUALITY)) {
1515 static const uint8_t kDefaultJpegQuality = 95;
1516 mRequestTemplateCache[templateId].update(ANDROID_JPEG_QUALITY,
1517 &kDefaultJpegQuality, 1);
1518 }
1519
Bharatt Kukrejad33fe9f2022-11-23 21:52:52 +00001520 // Fill in AUTOFRAMING if not available
1521 if (!mRequestTemplateCache[templateId].exists(ANDROID_CONTROL_AUTOFRAMING)) {
1522 static const uint8_t kDefaultAutoframingMode = ANDROID_CONTROL_AUTOFRAMING_OFF;
1523 mRequestTemplateCache[templateId].update(ANDROID_CONTROL_AUTOFRAMING,
1524 &kDefaultAutoframingMode, 1);
1525 }
1526
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001527 *request = mRequestTemplateCache[templateId];
Emilian Peev811d2952018-05-25 11:08:40 +01001528 mLastTemplateId = templateId;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001529 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001530 return OK;
1531}
1532
1533status_t Camera3Device::waitUntilDrained() {
1534 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001535 Mutex::Autolock il(mInterfaceLock);
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001536 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001537 Mutex::Autolock l(mLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001538
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001539 return waitUntilDrainedLocked(maxExpectedDuration);
Zhijun He69a37482014-03-23 18:44:49 -07001540}
1541
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07001542status_t Camera3Device::waitUntilDrainedLocked(nsecs_t maxExpectedDuration) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001543 switch (mStatus) {
1544 case STATUS_UNINITIALIZED:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001545 case STATUS_UNCONFIGURED:
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001546 ALOGV("%s: Already idle", __FUNCTION__);
1547 return OK;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001548 case STATUS_CONFIGURED:
1549 // To avoid race conditions, check with tracker to be sure
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001550 case STATUS_ERROR:
1551 case STATUS_ACTIVE:
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001552 // Need to verify shut down
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001553 break;
1554 default:
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07001555 SET_ERR_L("Unexpected status: %d",mStatus);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001556 return INVALID_OPERATION;
1557 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001558 ALOGV("%s: Camera %s: Waiting until idle (%" PRIi64 "ns)", __FUNCTION__, mId.string(),
1559 maxExpectedDuration);
Bharatt Kukrejaf6f0ca12023-01-26 19:56:30 +00001560 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration,
1561 /*requestThreadInvocation*/ false);
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001562 if (res != OK) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07001563 mStatusTracker->dumpActiveComponents();
Eino-Ville Talvala9c8a0912014-09-14 14:52:19 -07001564 SET_ERR_L("Error waiting for HAL to drain: %s (%d)", strerror(-res),
1565 res);
1566 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001567 return res;
1568}
1569
Ruben Brunk183f0562015-08-12 12:55:02 -07001570void Camera3Device::internalUpdateStatusLocked(Status status) {
1571 mStatus = status;
Bharatt Kukrejaf6f0ca12023-01-26 19:56:30 +00001572 mStatusIsInternal = mPauseStateNotify ? true : false;
1573 mRecentStatusUpdates.add({mStatus, mStatusIsInternal});
Ruben Brunk183f0562015-08-12 12:55:02 -07001574 mStatusChanged.broadcast();
1575}
1576
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001577// Pause to reconfigure
Bharatt Kukrejaf6f0ca12023-01-26 19:56:30 +00001578status_t Camera3Device::internalPauseAndWaitLocked(nsecs_t maxExpectedDuration,
1579 bool requestThreadInvocation) {
Emilian Peeve86358b2019-02-15 13:51:39 -08001580 if (mRequestThread.get() != nullptr) {
1581 mRequestThread->setPaused(true);
1582 } else {
1583 return NO_INIT;
1584 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001585
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001586 ALOGV("%s: Camera %s: Internal wait until idle (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1587 maxExpectedDuration);
Bharatt Kukrejaf6f0ca12023-01-26 19:56:30 +00001588 status_t res = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration,
1589 requestThreadInvocation);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001590 if (res != OK) {
Bharatt Kukreja086e57f2022-08-13 00:56:10 +00001591 mStatusTracker->dumpActiveComponents();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001592 SET_ERR_L("Can't idle device in %f seconds!",
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07001593 maxExpectedDuration/1e9);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001594 }
1595
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001596 return res;
1597}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001598
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001599// Resume after internalPauseAndWaitLocked
1600status_t Camera3Device::internalResumeLocked() {
1601 status_t res;
1602
1603 mRequestThread->setPaused(false);
1604
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001605 ALOGV("%s: Camera %s: Internal wait until active (% " PRIi64 " ns)", __FUNCTION__, mId.string(),
1606 kActiveTimeout);
Bharatt Kukrejaf6f0ca12023-01-26 19:56:30 +00001607 // internalResumeLocked is always called from a binder thread.
1608 res = waitUntilStateThenRelock(/*active*/ true, kActiveTimeout,
1609 /*requestThreadInvocation*/ false);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001610 if (res != OK) {
1611 SET_ERR_L("Can't transition to active in %f seconds!",
1612 kActiveTimeout/1e9);
1613 }
1614 mPauseStateNotify = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001615 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001616}
1617
Bharatt Kukrejaf6f0ca12023-01-26 19:56:30 +00001618status_t Camera3Device::waitUntilStateThenRelock(bool active, nsecs_t timeout,
1619 bool requestThreadInvocation) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001620 status_t res = OK;
Ruben Brunk183f0562015-08-12 12:55:02 -07001621
1622 size_t startIndex = 0;
1623 if (mStatusWaiters == 0) {
1624 // Clear the list of recent statuses if there are no existing threads waiting on updates to
1625 // this status list
1626 mRecentStatusUpdates.clear();
1627 } else {
1628 // If other threads are waiting on updates to this status list, set the position of the
1629 // first element that this list will check rather than clearing the list.
1630 startIndex = mRecentStatusUpdates.size();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001631 }
1632
Ruben Brunk183f0562015-08-12 12:55:02 -07001633 mStatusWaiters++;
1634
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001635 bool signalPipelineDrain = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001636 if (!active && mUseHalBufManager) {
1637 auto streamIds = mOutputStreams.getStreamIds();
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001638 if (mStatus == STATUS_ACTIVE) {
1639 mRequestThread->signalPipelineDrain(streamIds);
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001640 signalPipelineDrain = true;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08001641 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07001642 mRequestBufferSM.onWaitUntilIdle();
1643 }
1644
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001645 bool stateSeen = false;
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001646 nsecs_t startTime = systemTime();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001647 do {
Shuzhen Wang376f04c2023-06-06 04:50:01 +00001648 if (mStatus == STATUS_ERROR) {
1649 // Device in error state. Return right away.
1650 break;
1651 }
Bharatt Kukrejaf6f0ca12023-01-26 19:56:30 +00001652 if (active == (mStatus == STATUS_ACTIVE) &&
1653 (requestThreadInvocation || !mStatusIsInternal)) {
Ruben Brunk183f0562015-08-12 12:55:02 -07001654 // Desired state is current
1655 break;
1656 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001657
Avichal Rakesh976bb4d2022-05-02 15:23:00 -07001658 nsecs_t timeElapsed = systemTime() - startTime;
1659 nsecs_t timeToWait = timeout - timeElapsed;
1660 if (timeToWait <= 0) {
1661 // Thread woke up spuriously but has timed out since.
1662 // Force out of loop with TIMED_OUT result.
1663 res = TIMED_OUT;
1664 break;
1665 }
1666 res = mStatusChanged.waitRelative(mLock, timeToWait);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001667 if (res != OK) break;
1668
Ruben Brunk183f0562015-08-12 12:55:02 -07001669 // This is impossible, but if not, could result in subtle deadlocks and invalid state
1670 // transitions.
1671 LOG_ALWAYS_FATAL_IF(startIndex > mRecentStatusUpdates.size(),
1672 "%s: Skipping status updates in Camera3Device, may result in deadlock.",
1673 __FUNCTION__);
1674
Bharatt Kukrejaf6f0ca12023-01-26 19:56:30 +00001675 // Encountered desired state since we began waiting. Internal invocations coming from
1676 // request threads (such as reconfigureCamera) should be woken up immediately, whereas
1677 // invocations from binder threads (such as createInputStream) should only be woken up if
1678 // they are not paused. This avoids intermediate pause signals from reconfigureCamera as it
1679 // changes the status to active right after.
Ruben Brunk183f0562015-08-12 12:55:02 -07001680 for (size_t i = startIndex; i < mRecentStatusUpdates.size(); i++) {
Shuzhen Wang376f04c2023-06-06 04:50:01 +00001681 if (mRecentStatusUpdates[i].status == STATUS_ERROR) {
1682 // Device in error state. Return right away.
1683 stateSeen = true;
1684 break;
1685 }
Bharatt Kukrejaf6f0ca12023-01-26 19:56:30 +00001686 if (active == (mRecentStatusUpdates[i].status == STATUS_ACTIVE) &&
1687 (requestThreadInvocation || !mRecentStatusUpdates[i].isInternal)) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001688 stateSeen = true;
1689 break;
1690 }
1691 }
1692 } while (!stateSeen);
1693
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07001694 if (signalPipelineDrain) {
1695 mRequestThread->resetPipelineDrain();
1696 }
1697
Ruben Brunk183f0562015-08-12 12:55:02 -07001698 mStatusWaiters--;
1699
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001700 return res;
1701}
1702
1703
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001704status_t Camera3Device::setNotifyCallback(wp<NotificationListener> listener) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001705 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001706 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001707
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001708 if (listener != NULL && mListener != NULL) {
1709 ALOGW("%s: Replacing old callback listener", __FUNCTION__);
1710 }
1711 mListener = listener;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001712 mRequestThread->setNotificationListener(listener);
1713 mPreparerThread->setNotificationListener(listener);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001714
1715 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001716}
1717
Eino-Ville Talvala46910bd2013-07-18 19:15:17 -07001718bool Camera3Device::willNotify3A() {
1719 return false;
1720}
1721
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001722status_t Camera3Device::waitForNextFrame(nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001723 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001724 std::unique_lock<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001725
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001726 while (mResultQueue.empty()) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001727 auto st = mResultSignal.wait_for(l, std::chrono::nanoseconds(timeout));
1728 if (st == std::cv_status::timeout) {
1729 return TIMED_OUT;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001730 }
1731 }
1732 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001733}
1734
Jianing Weicb0652e2014-03-12 18:29:36 -07001735status_t Camera3Device::getNextResult(CaptureResult *frame) {
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001736 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001737 std::lock_guard<std::mutex> l(mOutputLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001738
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001739 if (mResultQueue.empty()) {
1740 return NOT_ENOUGH_DATA;
1741 }
1742
Jianing Weicb0652e2014-03-12 18:29:36 -07001743 if (frame == NULL) {
1744 ALOGE("%s: argument cannot be NULL", __FUNCTION__);
1745 return BAD_VALUE;
1746 }
1747
1748 CaptureResult &result = *(mResultQueue.begin());
1749 frame->mResultExtras = result.mResultExtras;
1750 frame->mMetadata.acquire(result.mMetadata);
Shuzhen Wang5c22c152017-12-31 17:12:25 -08001751 frame->mPhysicalMetadatas = std::move(result.mPhysicalMetadatas);
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07001752 mResultQueue.erase(mResultQueue.begin());
1753
1754 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001755}
1756
1757status_t Camera3Device::triggerAutofocus(uint32_t id) {
1758 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001759 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001760
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001761 ALOGV("%s: Triggering autofocus, id %d", __FUNCTION__, id);
1762 // Mix-in this trigger into the next request and only the next request.
1763 RequestTrigger trigger[] = {
1764 {
1765 ANDROID_CONTROL_AF_TRIGGER,
1766 ANDROID_CONTROL_AF_TRIGGER_START
1767 },
1768 {
1769 ANDROID_CONTROL_AF_TRIGGER_ID,
1770 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001771 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001772 };
1773
1774 return mRequestThread->queueTrigger(trigger,
1775 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001776}
1777
1778status_t Camera3Device::triggerCancelAutofocus(uint32_t id) {
1779 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001780 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001781
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001782 ALOGV("%s: Triggering cancel autofocus, id %d", __FUNCTION__, id);
1783 // Mix-in this trigger into the next request and only the next request.
1784 RequestTrigger trigger[] = {
1785 {
1786 ANDROID_CONTROL_AF_TRIGGER,
1787 ANDROID_CONTROL_AF_TRIGGER_CANCEL
1788 },
1789 {
1790 ANDROID_CONTROL_AF_TRIGGER_ID,
1791 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001792 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001793 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001794
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001795 return mRequestThread->queueTrigger(trigger,
1796 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001797}
1798
1799status_t Camera3Device::triggerPrecaptureMetering(uint32_t id) {
1800 ATRACE_CALL();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001801 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001802
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001803 ALOGV("%s: Triggering precapture metering, id %d", __FUNCTION__, id);
1804 // Mix-in this trigger into the next request and only the next request.
1805 RequestTrigger trigger[] = {
1806 {
1807 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1808 ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START
1809 },
1810 {
1811 ANDROID_CONTROL_AE_PRECAPTURE_ID,
1812 static_cast<int32_t>(id)
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07001813 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001814 };
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001815
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07001816 return mRequestThread->queueTrigger(trigger,
1817 sizeof(trigger)/sizeof(trigger[0]));
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08001818}
1819
Jianing Weicb0652e2014-03-12 18:29:36 -07001820status_t Camera3Device::flush(int64_t *frameNumber) {
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001821 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001822 ALOGV("%s: Camera %s: Flushing all requests", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001823 Mutex::Autolock il(mInterfaceLock);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001824
Zhijun He7ef20392014-04-21 16:04:17 -07001825 {
1826 Mutex::Autolock l(mLock);
Emilian Peeved2ebe42018-09-25 16:59:09 +01001827
1828 // b/116514106 "disconnect()" can get called twice for the same device. The
1829 // camera device will not be initialized during the second run.
1830 if (mStatus == STATUS_UNINITIALIZED) {
1831 return OK;
1832 }
1833
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001834 mRequestThread->clear(/*out*/frameNumber);
Shuzhen Wang316781a2020-08-18 18:11:01 -07001835
1836 // Stop session and stream counter
1837 mSessionStatsBuilder.stopCounter();
Zhijun He7ef20392014-04-21 16:04:17 -07001838 }
1839
Ravneetdbd5b242022-03-02 07:22:46 +00001840 // Calculate expected duration for flush with additional buffer time in ms for watchdog
Ravneet Dhanjal7d412d22022-06-29 01:34:01 +00001841 uint64_t maxExpectedDuration = ns2ms(getExpectedInFlightDuration() + kBaseGetBufferWait);
Ravneetdbd5b242022-03-02 07:22:46 +00001842 status_t res = mCameraServiceWatchdog->WATCH_CUSTOM_TIMER(mRequestThread->flush(),
1843 maxExpectedDuration / kCycleLengthMs, kCycleLengthMs);
1844
1845 return res;
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07001846}
1847
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001848status_t Camera3Device::prepare(int streamId) {
Ruben Brunkc78ac262015-08-13 17:58:46 -07001849 return prepare(camera3::Camera3StreamInterface::ALLOCATE_PIPELINE_MAX, streamId);
1850}
1851
1852status_t Camera3Device::prepare(int maxCount, int streamId) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001853 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001854 ALOGV("%s: Camera %s: Preparing stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001855 Mutex::Autolock il(mInterfaceLock);
1856 Mutex::Autolock l(mLock);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001857
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001858 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1859 if (stream == nullptr) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001860 CLOGE("Stream %d does not exist", streamId);
1861 return BAD_VALUE;
1862 }
1863
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001864 if (stream->isUnpreparable() || stream->hasOutstandingBuffers() ) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001865 CLOGE("Stream %d has already been a request target", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001866 return BAD_VALUE;
1867 }
1868
1869 if (mRequestThread->isStreamPending(stream)) {
Eino-Ville Talvala261394e2015-05-13 14:28:38 -07001870 CLOGE("Stream %d is already a target in a pending request", streamId);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001871 return BAD_VALUE;
1872 }
1873
Ruben Brunkc78ac262015-08-13 17:58:46 -07001874 return mPreparerThread->prepare(maxCount, stream);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07001875}
1876
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001877status_t Camera3Device::tearDown(int streamId) {
1878 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001879 ALOGV("%s: Camera %s: Tearing down stream %d", __FUNCTION__, mId.string(), streamId);
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001880 Mutex::Autolock il(mInterfaceLock);
1881 Mutex::Autolock l(mLock);
1882
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001883 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1884 if (stream == nullptr) {
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001885 CLOGE("Stream %d does not exist", streamId);
1886 return BAD_VALUE;
1887 }
1888
Eino-Ville Talvalab25e3c82015-07-15 16:04:27 -07001889 if (stream->hasOutstandingBuffers() || mRequestThread->isStreamPending(stream)) {
1890 CLOGE("Stream %d is a target of a in-progress request", streamId);
1891 return BAD_VALUE;
1892 }
1893
1894 return stream->tearDown();
1895}
1896
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001897status_t Camera3Device::addBufferListenerForStream(int streamId,
1898 wp<Camera3StreamBufferListener> listener) {
1899 ATRACE_CALL();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08001900 ALOGV("%s: Camera %s: Adding buffer listener for stream %d", __FUNCTION__, mId.string(), streamId);
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001901 Mutex::Autolock il(mInterfaceLock);
1902 Mutex::Autolock l(mLock);
1903
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07001904 sp<Camera3StreamInterface> stream = mOutputStreams.get(streamId);
1905 if (stream == nullptr) {
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001906 CLOGE("Stream %d does not exist", streamId);
1907 return BAD_VALUE;
1908 }
Shuzhen Wangb0fdc1e2016-03-20 23:21:39 -07001909 stream->addBufferListener(listener);
1910
1911 return OK;
1912}
1913
Austin Borger4a870a32022-02-25 01:48:41 +00001914float Camera3Device::getMaxPreviewFps(sp<camera3::Camera3OutputStreamInterface> stream) {
1915 camera_metadata_entry minDurations =
1916 mDeviceInfo.find(ANDROID_SCALER_AVAILABLE_MIN_FRAME_DURATIONS);
1917 for (size_t i = 0; i < minDurations.count; i += 4) {
Shuzhen Wangfa23af12023-05-31 19:14:03 +00001918 if (minDurations.data.i64[i] == stream->getOriginalFormat()
Austin Borger4a870a32022-02-25 01:48:41 +00001919 && minDurations.data.i64[i+1] == stream->getWidth()
1920 && minDurations.data.i64[i+2] == stream->getHeight()) {
1921 int64_t minFrameDuration = minDurations.data.i64[i+3];
1922 return 1e9f / minFrameDuration;
1923 }
1924 }
1925 return 0.0f;
1926}
1927
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08001928/**
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001929 * Methods called by subclasses
1930 */
1931
1932void Camera3Device::notifyStatus(bool idle) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07001933 ATRACE_CALL();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001934 std::vector<int> streamIds;
1935 std::vector<hardware::CameraStreamStats> streamStats;
Austin Borger4a870a32022-02-25 01:48:41 +00001936 float sessionMaxPreviewFps = 0.0f;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001937
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001938 {
1939 // Need mLock to safely update state and synchronize to current
1940 // state of methods in flight.
1941 Mutex::Autolock l(mLock);
1942 // We can get various system-idle notices from the status tracker
1943 // while starting up. Only care about them if we've actually sent
1944 // in some requests recently.
1945 if (mStatus != STATUS_ACTIVE && mStatus != STATUS_CONFIGURED) {
1946 return;
1947 }
Eino-Ville Talvala002001b2018-01-23 16:53:50 -08001948 ALOGV("%s: Camera %s: Now %s, pauseState: %s", __FUNCTION__, mId.string(),
1949 idle ? "idle" : "active", mPauseStateNotify ? "true" : "false");
Ruben Brunk183f0562015-08-12 12:55:02 -07001950 internalUpdateStatusLocked(idle ? STATUS_CONFIGURED : STATUS_ACTIVE);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001951
1952 // Skip notifying listener if we're doing some user-transparent
1953 // state changes
1954 if (mPauseStateNotify) return;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001955
Austin Borger4a870a32022-02-25 01:48:41 +00001956 for (size_t i = 0; i < mOutputStreams.size(); i++) {
1957 auto stream = mOutputStreams[i];
1958 if (stream.get() == nullptr) continue;
1959
1960 float streamMaxPreviewFps = getMaxPreviewFps(stream);
1961 sessionMaxPreviewFps = std::max(sessionMaxPreviewFps, streamMaxPreviewFps);
1962
1963 // Populate stream statistics in case of Idle
1964 if (idle) {
Shuzhen Wang316781a2020-08-18 18:11:01 -07001965 streamIds.push_back(stream->getId());
1966 Camera3Stream* camera3Stream = Camera3Stream::cast(stream->asHalStream());
1967 int64_t usage = 0LL;
Shuzhen Wang8ed1e872022-03-08 16:34:33 -08001968 int64_t streamUseCase = ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT;
Shuzhen Wang316781a2020-08-18 18:11:01 -07001969 if (camera3Stream != nullptr) {
1970 usage = camera3Stream->getUsage();
Shuzhen Wangc8ab4522021-12-14 20:12:42 -08001971 streamUseCase = camera3Stream->getStreamUseCase();
Shuzhen Wang316781a2020-08-18 18:11:01 -07001972 }
1973 streamStats.emplace_back(stream->getWidth(), stream->getHeight(),
Shuzhen Wangb02d3fc2023-02-23 21:01:25 +00001974 stream->getOriginalFormat(), streamMaxPreviewFps, stream->getDataSpace(), usage,
Shuzhen Wang316781a2020-08-18 18:11:01 -07001975 stream->getMaxHalBuffers(),
Emilian Peev2295df72021-11-12 18:14:10 -08001976 stream->getMaxTotalBuffers() - stream->getMaxHalBuffers(),
Austin Borger9e2b27c2022-07-15 11:27:24 -07001977 stream->getDynamicRangeProfile(), streamUseCase,
1978 stream->getColorSpace());
Shuzhen Wang316781a2020-08-18 18:11:01 -07001979 }
1980 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001981 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001982
1983 sp<NotificationListener> listener;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001984 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08001985 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07001986 listener = mListener.promote();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07001987 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07001988 status_t res = OK;
1989 if (listener != nullptr) {
1990 if (idle) {
1991 // Get session stats from the builder, and notify the listener.
1992 int64_t requestCount, resultErrorCount;
1993 bool deviceError;
1994 std::map<int, StreamStats> streamStatsMap;
1995 mSessionStatsBuilder.buildAndReset(&requestCount, &resultErrorCount,
1996 &deviceError, &streamStatsMap);
1997 for (size_t i = 0; i < streamIds.size(); i++) {
1998 int streamId = streamIds[i];
1999 auto stats = streamStatsMap.find(streamId);
2000 if (stats != streamStatsMap.end()) {
2001 streamStats[i].mRequestCount = stats->second.mRequestedFrameCount;
2002 streamStats[i].mErrorCount = stats->second.mDroppedFrameCount;
2003 streamStats[i].mStartLatencyMs = stats->second.mStartLatencyMs;
2004 streamStats[i].mHistogramType =
2005 hardware::CameraStreamStats::HISTOGRAM_TYPE_CAPTURE_LATENCY;
2006 streamStats[i].mHistogramBins.assign(
2007 stats->second.mCaptureLatencyBins.begin(),
2008 stats->second.mCaptureLatencyBins.end());
2009 streamStats[i].mHistogramCounts.assign(
2010 stats->second.mCaptureLatencyHistogram.begin(),
2011 stats->second.mCaptureLatencyHistogram.end());
2012 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07002013 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07002014 listener->notifyIdle(requestCount, resultErrorCount, deviceError, streamStats);
2015 } else {
Austin Borger4a870a32022-02-25 01:48:41 +00002016 res = listener->notifyActive(sessionMaxPreviewFps);
Shuzhen Wang316781a2020-08-18 18:11:01 -07002017 }
Eino-Ville Talvala178e8232021-04-16 18:41:39 -07002018 }
2019 if (res != OK) {
2020 SET_ERR("Camera access permission lost mid-operation: %s (%d)",
2021 strerror(-res), res);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002022 }
2023}
2024
Shuzhen Wang758c2152017-01-10 18:26:18 -08002025status_t Camera3Device::setConsumerSurfaces(int streamId,
Emilian Peev40ead602017-09-26 15:46:36 +01002026 const std::vector<sp<Surface>>& consumers, std::vector<int> *surfaceIds) {
Zhijun He5d677d12016-05-29 16:52:39 -07002027 ATRACE_CALL();
Shuzhen Wang758c2152017-01-10 18:26:18 -08002028 ALOGV("%s: Camera %s: set consumer surface for stream %d",
2029 __FUNCTION__, mId.string(), streamId);
Emilian Peev40ead602017-09-26 15:46:36 +01002030
2031 if (surfaceIds == nullptr) {
2032 return BAD_VALUE;
2033 }
2034
Zhijun He5d677d12016-05-29 16:52:39 -07002035 Mutex::Autolock il(mInterfaceLock);
2036 Mutex::Autolock l(mLock);
2037
Shuzhen Wang758c2152017-01-10 18:26:18 -08002038 if (consumers.size() == 0) {
2039 CLOGE("No consumer is passed!");
Zhijun He5d677d12016-05-29 16:52:39 -07002040 return BAD_VALUE;
2041 }
2042
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002043 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2044 if (stream == nullptr) {
Zhijun He5d677d12016-05-29 16:52:39 -07002045 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002046 return BAD_VALUE;
Zhijun He5d677d12016-05-29 16:52:39 -07002047 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002048
2049 // isConsumerConfigurationDeferred will be off after setConsumers
2050 bool isDeferred = stream->isConsumerConfigurationDeferred();
Shuzhen Wang758c2152017-01-10 18:26:18 -08002051 status_t res = stream->setConsumers(consumers);
Zhijun He5d677d12016-05-29 16:52:39 -07002052 if (res != OK) {
2053 CLOGE("Stream %d set consumer failed (error %d %s) ", streamId, res, strerror(-res));
2054 return res;
2055 }
2056
Emilian Peev40ead602017-09-26 15:46:36 +01002057 for (auto &consumer : consumers) {
2058 int id = stream->getSurfaceId(consumer);
2059 if (id < 0) {
2060 CLOGE("Invalid surface id!");
2061 return BAD_VALUE;
2062 }
2063 surfaceIds->push_back(id);
2064 }
2065
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002066 if (isDeferred) {
Shuzhen Wang0129d522016-10-30 22:43:41 -07002067 if (!stream->isConfiguring()) {
2068 CLOGE("Stream %d was already fully configured.", streamId);
2069 return INVALID_OPERATION;
2070 }
Zhijun He5d677d12016-05-29 16:52:39 -07002071
Shuzhen Wang0129d522016-10-30 22:43:41 -07002072 res = stream->finishConfiguration();
2073 if (res != OK) {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002074 // If finishConfiguration fails due to abandoned surface, do not set
2075 // device to error state.
2076 bool isSurfaceAbandoned =
2077 (res == NO_INIT || res == DEAD_OBJECT) && stream->isAbandoned();
2078 if (!isSurfaceAbandoned) {
2079 SET_ERR_L("Can't finish configuring output stream %d: %s (%d)",
2080 stream->getId(), strerror(-res), res);
2081 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07002082 return res;
2083 }
Zhijun He5d677d12016-05-29 16:52:39 -07002084 }
2085
2086 return OK;
2087}
2088
Emilian Peev40ead602017-09-26 15:46:36 +01002089status_t Camera3Device::updateStream(int streamId, const std::vector<sp<Surface>> &newSurfaces,
2090 const std::vector<OutputStreamInfo> &outputInfo,
2091 const std::vector<size_t> &removedSurfaceIds, KeyedVector<sp<Surface>, size_t> *outputMap) {
2092 Mutex::Autolock il(mInterfaceLock);
2093 Mutex::Autolock l(mLock);
2094
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002095 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2096 if (stream == nullptr) {
Emilian Peev40ead602017-09-26 15:46:36 +01002097 CLOGE("Stream %d is unknown", streamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002098 return BAD_VALUE;
Emilian Peev40ead602017-09-26 15:46:36 +01002099 }
2100
2101 for (const auto &it : removedSurfaceIds) {
2102 if (mRequestThread->isOutputSurfacePending(streamId, it)) {
2103 CLOGE("Shared surface still part of a pending request!");
2104 return -EBUSY;
2105 }
2106 }
2107
Emilian Peev40ead602017-09-26 15:46:36 +01002108 status_t res = stream->updateStream(newSurfaces, outputInfo, removedSurfaceIds, outputMap);
2109 if (res != OK) {
2110 CLOGE("Stream %d failed to update stream (error %d %s) ",
2111 streamId, res, strerror(-res));
2112 if (res == UNKNOWN_ERROR) {
2113 SET_ERR_L("%s: Stream update failed to revert to previous output configuration!",
2114 __FUNCTION__);
2115 }
2116 return res;
2117 }
2118
2119 return res;
2120}
2121
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002122status_t Camera3Device::dropStreamBuffers(bool dropping, int streamId) {
2123 Mutex::Autolock il(mInterfaceLock);
2124 Mutex::Autolock l(mLock);
2125
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002126 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
2127 if (stream == nullptr) {
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002128 ALOGE("%s: Stream %d is not found.", __FUNCTION__, streamId);
2129 return BAD_VALUE;
2130 }
Shuzhen Wang316781a2020-08-18 18:11:01 -07002131
2132 if (dropping) {
2133 mSessionStatsBuilder.stopCounter(streamId);
2134 } else {
2135 mSessionStatsBuilder.startCounter(streamId);
2136 }
Chien-Yu Chena936ac22017-10-23 15:59:49 -07002137 return stream->dropBuffers(dropping);
2138}
2139
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002140/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002141 * Camera3Device private methods
2142 */
2143
2144sp<Camera3Device::CaptureRequest> Camera3Device::createCaptureRequest(
Emilian Peevaebbe412018-01-15 13:53:24 +00002145 const PhysicalCameraSettingsList &request, const SurfaceMap &surfaceMap) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002146 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002147
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002148 sp<CaptureRequest> newRequest = new CaptureRequest();
Emilian Peevaebbe412018-01-15 13:53:24 +00002149 newRequest->mSettingsList = request;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002150
2151 camera_metadata_entry_t inputStreams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002152 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002153 if (inputStreams.count > 0) {
2154 if (mInputStream == NULL ||
Zhijun Hed1d64672013-09-06 15:00:01 -07002155 mInputStream->getId() != inputStreams.data.i32[0]) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002156 CLOGE("Request references unknown input stream %d",
2157 inputStreams.data.u8[0]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002158 return NULL;
2159 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002160
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002161 if (mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002162 SET_ERR_L("%s: input stream %d is not configured!",
2163 __FUNCTION__, mInputStream->getId());
2164 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002165 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002166 // Check if stream prepare is blocking requests.
2167 if (mInputStream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002168 CLOGE("Request references an input stream that's being prepared!");
2169 return NULL;
2170 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002171
2172 newRequest->mInputStream = mInputStream;
Emilian Peevaebbe412018-01-15 13:53:24 +00002173 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_INPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002174 }
2175
2176 camera_metadata_entry_t streams =
Emilian Peevaebbe412018-01-15 13:53:24 +00002177 newRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_OUTPUT_STREAMS);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002178 if (streams.count == 0) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002179 CLOGE("Zero output streams specified!");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002180 return NULL;
2181 }
2182
2183 for (size_t i = 0; i < streams.count; i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002184 sp<Camera3OutputStreamInterface> stream = mOutputStreams.get(streams.data.i32[i]);
2185 if (stream == nullptr) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002186 CLOGE("Request references unknown stream %d",
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002187 streams.data.i32[i]);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002188 return NULL;
2189 }
Zhijun He5d677d12016-05-29 16:52:39 -07002190 // It is illegal to include a deferred consumer output stream into a request
Shuzhen Wang0129d522016-10-30 22:43:41 -07002191 auto iter = surfaceMap.find(streams.data.i32[i]);
2192 if (iter != surfaceMap.end()) {
2193 const std::vector<size_t>& surfaces = iter->second;
2194 for (const auto& surface : surfaces) {
2195 if (stream->isConsumerConfigurationDeferred(surface)) {
2196 CLOGE("Stream %d surface %zu hasn't finished configuration yet "
2197 "due to deferred consumer", stream->getId(), surface);
2198 return NULL;
2199 }
2200 }
Yin-Chia Yeh0b287572018-10-15 12:38:13 -07002201 newRequest->mOutputSurfaces[streams.data.i32[i]] = surfaces;
Zhijun He5d677d12016-05-29 16:52:39 -07002202 }
2203
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002204 if (stream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002205 SET_ERR_L("%s: stream %d is not configured!", __FUNCTION__, stream->getId());
2206 return NULL;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002207 }
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07002208 // Check if stream prepare is blocking requests.
2209 if (stream->isBlockedByPrepare()) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07002210 CLOGE("Request references an output stream that's being prepared!");
2211 return NULL;
2212 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002213
2214 newRequest->mOutputStreams.push(stream);
2215 }
Emilian Peevaebbe412018-01-15 13:53:24 +00002216 newRequest->mSettingsList.begin()->metadata.erase(ANDROID_REQUEST_OUTPUT_STREAMS);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07002217 newRequest->mBatchSize = 1;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002218
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08002219 auto rotateAndCropEntry =
2220 newRequest->mSettingsList.begin()->metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
2221 if (rotateAndCropEntry.count > 0 &&
2222 rotateAndCropEntry.data.u8[0] == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
2223 newRequest->mRotateAndCropAuto = true;
2224 } else {
2225 newRequest->mRotateAndCropAuto = false;
2226 }
2227
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00002228 auto autoframingEntry =
2229 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_AUTOFRAMING);
2230 if (autoframingEntry.count > 0 &&
2231 autoframingEntry.data.u8[0] == ANDROID_CONTROL_AUTOFRAMING_AUTO) {
2232 newRequest->mAutoframingAuto = true;
2233 } else {
2234 newRequest->mAutoframingAuto = false;
2235 }
2236
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07002237 auto zoomRatioEntry =
2238 newRequest->mSettingsList.begin()->metadata.find(ANDROID_CONTROL_ZOOM_RATIO);
2239 if (zoomRatioEntry.count > 0 &&
2240 zoomRatioEntry.data.f[0] == 1.0f) {
2241 newRequest->mZoomRatioIs1x = true;
2242 } else {
2243 newRequest->mZoomRatioIs1x = false;
2244 }
2245
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002246 if (mSupportCameraMute) {
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002247 for (auto& settings : newRequest->mSettingsList) {
2248 auto testPatternModeEntry =
2249 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
2250 settings.mOriginalTestPatternMode = testPatternModeEntry.count > 0 ?
2251 testPatternModeEntry.data.i32[0] :
2252 ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002253
Shuzhen Wang911c6a32021-10-27 13:36:03 -07002254 auto testPatternDataEntry =
2255 settings.metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
2256 if (testPatternDataEntry.count >= 4) {
2257 memcpy(settings.mOriginalTestPatternData, testPatternDataEntry.data.i32,
2258 sizeof(PhysicalCameraSettings::mOriginalTestPatternData));
2259 } else {
2260 settings.mOriginalTestPatternData[0] = 0;
2261 settings.mOriginalTestPatternData[1] = 0;
2262 settings.mOriginalTestPatternData[2] = 0;
2263 settings.mOriginalTestPatternData[3] = 0;
2264 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08002265 }
2266 }
2267
Shuzhen Wangaf22e912023-04-11 16:03:17 -07002268 if (mSupportZoomOverride) {
2269 for (auto& settings : newRequest->mSettingsList) {
2270 auto settingsOverrideEntry =
2271 settings.metadata.find(ANDROID_CONTROL_SETTINGS_OVERRIDE);
2272 settings.mOriginalSettingsOverride = settingsOverrideEntry.count > 0 ?
2273 settingsOverrideEntry.data.i32[0] :
2274 ANDROID_CONTROL_SETTINGS_OVERRIDE_OFF;
2275 }
2276 }
2277
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002278 return newRequest;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002279}
2280
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002281void Camera3Device::cancelStreamsConfigurationLocked() {
2282 int res = OK;
2283 if (mInputStream != NULL && mInputStream->isConfiguring()) {
2284 res = mInputStream->cancelConfiguration();
2285 if (res != OK) {
2286 CLOGE("Can't cancel configuring input stream %d: %s (%d)",
2287 mInputStream->getId(), strerror(-res), res);
2288 }
2289 }
2290
2291 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002292 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002293 if (outputStream->isConfiguring()) {
2294 res = outputStream->cancelConfiguration();
2295 if (res != OK) {
2296 CLOGE("Can't cancel configuring output stream %d: %s (%d)",
2297 outputStream->getId(), strerror(-res), res);
2298 }
2299 }
2300 }
2301
2302 // Return state to that at start of call, so that future configures
2303 // properly clean things up
2304 internalUpdateStatusLocked(STATUS_UNCONFIGURED);
2305 mNeedConfig = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002306
2307 res = mPreparerThread->resume();
2308 if (res != OK) {
2309 ALOGE("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2310 }
2311}
2312
Emilian Peev0d0191e2020-04-21 17:01:18 -07002313bool Camera3Device::checkAbandonedStreamsLocked() {
2314 if ((mInputStream.get() != nullptr) && (mInputStream->isAbandoned())) {
2315 return true;
2316 }
2317
2318 for (size_t i = 0; i < mOutputStreams.size(); i++) {
2319 auto stream = mOutputStreams[i];
2320 if ((stream.get() != nullptr) && (stream->isAbandoned())) {
2321 return true;
2322 }
2323 }
2324
2325 return false;
2326}
2327
Emilian Peev3bead5f2020-05-28 17:29:08 -07002328bool Camera3Device::reconfigureCamera(const CameraMetadata& sessionParams, int clientStatusId) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002329 ATRACE_CALL();
2330 bool ret = false;
2331
Shuzhen Wang316781a2020-08-18 18:11:01 -07002332 nsecs_t startTime = systemTime();
2333
Bharatt Kukreja5681f782023-01-18 21:56:28 +00002334 // We must not hold mInterfaceLock here since this function is called from
2335 // RequestThread::threadLoop and holding mInterfaceLock could lead to
2336 // deadlocks (http://b/143513518)
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002337 nsecs_t maxExpectedDuration = getExpectedInFlightDuration();
2338
2339 Mutex::Autolock l(mLock);
Emilian Peev0d0191e2020-04-21 17:01:18 -07002340 if (checkAbandonedStreamsLocked()) {
2341 ALOGW("%s: Abandoned stream detected, session parameters can't be applied correctly!",
2342 __FUNCTION__);
2343 return true;
2344 }
2345
Emilian Peev3bead5f2020-05-28 17:29:08 -07002346 status_t rc = NO_ERROR;
2347 bool markClientActive = false;
2348 if (mStatus == STATUS_ACTIVE) {
2349 markClientActive = true;
2350 mPauseStateNotify = true;
Bharatt Kukrejaf6f0ca12023-01-26 19:56:30 +00002351 mStatusTracker->markComponentIdle(clientStatusId, Fence::NO_FENCE);
Emilian Peev3bead5f2020-05-28 17:29:08 -07002352
Bharatt Kukrejaf6f0ca12023-01-26 19:56:30 +00002353 // This is essentially the same as calling rc = internalPauseAndWaitLocked(..), except that
2354 // we don't want to call setPaused(true) to avoid it interfering with setPaused() called
2355 // from createInputStream/createStream.
2356 rc = waitUntilStateThenRelock(/*active*/ false, maxExpectedDuration,
2357 /*requestThreadInvocation*/ true);
2358 if (rc != OK) {
2359 mStatusTracker->dumpActiveComponents();
2360 SET_ERR_L("Can't idle device in %f seconds!",
2361 maxExpectedDuration/1e9);
2362 }
Emilian Peev3bead5f2020-05-28 17:29:08 -07002363 }
2364
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002365 if (rc == NO_ERROR) {
2366 mNeedConfig = true;
2367 rc = configureStreamsLocked(mOperatingMode, sessionParams, /*notifyRequestThread*/ false);
2368 if (rc == NO_ERROR) {
2369 ret = true;
2370 mPauseStateNotify = false;
2371 //Moving to active state while holding 'mLock' is important.
2372 //There could be pending calls to 'create-/deleteStream' which
2373 //will trigger another stream configuration while the already
2374 //present streams end up with outstanding buffers that will
2375 //not get drained.
2376 internalUpdateStatusLocked(STATUS_ACTIVE);
Shuzhen Wang25152e42023-06-13 22:18:58 +00002377
2378 mCameraServiceProxyWrapper->logStreamConfigured(mId, mOperatingMode,
2379 true /*internalReconfig*/, ns2ms(systemTime() - startTime));
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002380 } else if (rc == DEAD_OBJECT) {
2381 // DEAD_OBJECT can be returned if either the consumer surface is
2382 // abandoned, or the HAL has died.
2383 // - If the HAL has died, configureStreamsLocked call will set
2384 // device to error state,
2385 // - If surface is abandoned, we should not set device to error
2386 // state.
2387 ALOGE("Failed to re-configure camera due to abandoned surface");
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002388 } else {
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002389 SET_ERR_L("Failed to re-configure camera: %d", rc);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002390 }
2391 } else {
2392 ALOGE("%s: Failed to pause streaming: %d", __FUNCTION__, rc);
2393 }
2394
Emilian Peev3bead5f2020-05-28 17:29:08 -07002395 if (markClientActive) {
2396 mStatusTracker->markComponentActive(clientStatusId);
2397 }
2398
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002399 return ret;
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002400}
2401
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002402status_t Camera3Device::configureStreamsLocked(int operatingMode,
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002403 const CameraMetadata& sessionParams, bool notifyRequestThread) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002404 ATRACE_CALL();
2405 status_t res;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002406
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002407 if (mStatus != STATUS_UNCONFIGURED && mStatus != STATUS_CONFIGURED) {
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002408 CLOGE("Not idle");
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002409 return INVALID_OPERATION;
2410 }
2411
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002412 if (operatingMode < 0) {
2413 CLOGE("Invalid operating mode: %d", operatingMode);
2414 return BAD_VALUE;
2415 }
2416
2417 bool isConstrainedHighSpeed =
Jayant Chowdharyc67af1b2022-04-07 18:05:04 +00002418 CAMERA_STREAM_CONFIGURATION_CONSTRAINED_HIGH_SPEED_MODE == operatingMode;
Eino-Ville Talvalae7091aa2017-03-07 15:23:06 -08002419
2420 if (mOperatingMode != operatingMode) {
2421 mNeedConfig = true;
2422 mIsConstrainedHighSpeedConfiguration = isConstrainedHighSpeed;
2423 mOperatingMode = operatingMode;
2424 }
2425
Shuzhen Wangcddfdbf2022-06-15 14:22:02 -07002426 // Reset min expected duration when session is reconfigured.
2427 mMinExpectedDuration = 0;
2428
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002429 // In case called from configureStreams, abort queued input buffers not belonging to
2430 // any pending requests.
2431 if (mInputStream != NULL && notifyRequestThread) {
2432 while (true) {
Emilian Peevf4816702020-04-03 15:44:51 -07002433 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08002434 camera3::Size inputBufferSize;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002435 status_t res = mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08002436 &inputBufferSize, /*respectHalLimit*/ false);
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002437 if (res != OK) {
2438 // Exhausted acquiring all input buffers.
2439 break;
2440 }
2441
Emilian Peevf4816702020-04-03 15:44:51 -07002442 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang0cf01cb2019-09-05 13:33:06 -07002443 res = mInputStream->returnInputBuffer(inputBuffer);
2444 if (res != OK) {
2445 ALOGE("%s: %d: couldn't return input buffer while clearing input queue: "
2446 "%s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
2447 }
2448 }
2449 }
2450
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002451 if (!mNeedConfig) {
2452 ALOGV("%s: Skipping config, no stream changes", __FUNCTION__);
2453 return OK;
2454 }
2455
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002456 // Workaround for device HALv3.2 or older spec bug - zero streams requires
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002457 // adding a fake stream instead.
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002458 // TODO: Bug: 17321404 for fixing the HAL spec and removing this workaround.
2459 if (mOutputStreams.size() == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002460 addFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002461 } else {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002462 tryRemoveFakeStreamLocked();
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002463 }
2464
Shuzhen Wang16610a62022-12-15 22:38:07 -08002465 // Override stream use case based on "adb shell command"
2466 overrideStreamUseCaseLocked();
2467
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002468 // Start configuring the streams
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002469 ALOGV("%s: Camera %s: Starting stream configuration", __FUNCTION__, mId.string());
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002470
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002471 mPreparerThread->pause();
2472
Emilian Peevf4816702020-04-03 15:44:51 -07002473 camera_stream_configuration config;
Eino-Ville Talvalabbbbe842017-02-28 17:50:56 -08002474 config.operation_mode = mOperatingMode;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002475 config.num_streams = (mInputStream != NULL) + mOutputStreams.size();
Shuzhen Wang83bff122020-11-20 15:51:39 -08002476 config.input_is_multi_resolution = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002477
Emilian Peevf4816702020-04-03 15:44:51 -07002478 Vector<camera3::camera_stream_t*> streams;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002479 streams.setCapacity(config.num_streams);
Emilian Peev192ee832018-01-31 14:46:47 +00002480 std::vector<uint32_t> bufferSizes(config.num_streams, 0);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002481
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002482
2483 if (mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07002484 camera3::camera_stream_t *inputStream;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002485 inputStream = mInputStream->startConfiguration();
2486 if (inputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002487 CLOGE("Can't start input stream configuration");
2488 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002489 return INVALID_OPERATION;
2490 }
2491 streams.add(inputStream);
Shuzhen Wang83bff122020-11-20 15:51:39 -08002492
2493 config.input_is_multi_resolution = mIsInputStreamMultiResolution;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002494 }
2495
Shuzhen Wang99080502021-03-07 21:08:20 -08002496 mGroupIdPhysicalCameraMap.clear();
Emilian Peevd865f0d2023-03-17 17:13:07 -07002497 mComposerOutput = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002498 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Igor Murashkin2fba5842013-04-22 14:03:54 -07002499
2500 // Don't configure bidi streams twice, nor add them twice to the list
2501 if (mOutputStreams[i].get() ==
2502 static_cast<Camera3StreamInterface*>(mInputStream.get())) {
2503
2504 config.num_streams--;
2505 continue;
2506 }
2507
Emilian Peevf4816702020-04-03 15:44:51 -07002508 camera3::camera_stream_t *outputStream;
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002509 outputStream = mOutputStreams[i]->startConfiguration();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002510 if (outputStream == NULL) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002511 CLOGE("Can't start output stream configuration");
2512 cancelStreamsConfigurationLocked();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002513 return INVALID_OPERATION;
2514 }
2515 streams.add(outputStream);
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002516
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002517 if (outputStream->format == HAL_PIXEL_FORMAT_BLOB) {
Emilian Peev192ee832018-01-31 14:46:47 +00002518 size_t k = i + ((mInputStream != nullptr) ? 1 : 0); // Input stream if present should
2519 // always occupy the initial entry.
Emilian Peeve579d8b2023-02-28 14:16:08 -08002520 if ((outputStream->data_space == HAL_DATASPACE_V0_JFIF) ||
2521 (outputStream->data_space ==
2522 static_cast<android_dataspace_t>(
2523 aidl::android::hardware::graphics::common::Dataspace::JPEG_R))) {
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002524 bufferSizes[k] = static_cast<uint32_t>(
Jayant Chowdhary6a6d3a82021-11-17 16:54:34 -08002525 getJpegBufferSize(infoPhysical(String8(outputStream->physical_camera_id)),
2526 outputStream->width, outputStream->height));
Shuzhen Wangb7ab1e42019-02-20 15:42:23 -08002527 } else if (outputStream->data_space ==
2528 static_cast<android_dataspace>(HAL_DATASPACE_JPEG_APP_SEGMENTS)) {
2529 bufferSizes[k] = outputStream->width * outputStream->height;
2530 } else {
2531 ALOGW("%s: Blob dataSpace %d not supported",
2532 __FUNCTION__, outputStream->data_space);
2533 }
Yin-Chia Yehfb6e55b2018-01-25 10:50:26 -08002534 }
Shuzhen Wang99080502021-03-07 21:08:20 -08002535
2536 if (mOutputStreams[i]->isMultiResolution()) {
2537 int32_t streamGroupId = mOutputStreams[i]->getHalStreamGroupId();
2538 const String8& physicalCameraId = mOutputStreams[i]->getPhysicalCameraId();
2539 mGroupIdPhysicalCameraMap[streamGroupId].insert(physicalCameraId);
2540 }
Emilian Peeve23f1d92021-09-20 14:56:01 -07002541
2542 if (outputStream->usage & GraphicBuffer::USAGE_HW_COMPOSER) {
Emilian Peevd865f0d2023-03-17 17:13:07 -07002543 mComposerOutput = true;
Emilian Peeve23f1d92021-09-20 14:56:01 -07002544 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002545 }
2546
2547 config.streams = streams.editArray();
2548
2549 // Do the HAL configuration; will potentially touch stream
Shuzhen Wang92653952019-05-07 15:11:43 -07002550 // max_buffers, usage, and priv fields, as well as data_space and format
2551 // fields for IMPLEMENTATION_DEFINED formats.
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002552
Avichal Rakesh1fff2d12023-03-03 15:05:48 -08002553 int64_t logId = mCameraServiceProxyWrapper->getCurrentLogIdForCamera(mId);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002554 const camera_metadata_t *sessionBuffer = sessionParams.getAndLock();
Avichal Rakesh1fff2d12023-03-03 15:05:48 -08002555 res = mInterface->configureStreams(sessionBuffer, &config, bufferSizes, logId);
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002556 sessionParams.unlock(sessionBuffer);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002557
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002558 if (res == BAD_VALUE) {
2559 // HAL rejected this set of streams as unsupported, clean up config
2560 // attempt and return to unconfigured state
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002561 CLOGE("Set of requested inputs/outputs not supported by HAL");
2562 cancelStreamsConfigurationLocked();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002563 return BAD_VALUE;
2564 } else if (res != OK) {
2565 // Some other kind of error from configure_streams - this is not
2566 // expected
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002567 SET_ERR_L("Unable to configure streams with HAL: %s (%d)",
2568 strerror(-res), res);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002569 return res;
2570 }
2571
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002572 // Finish all stream configuration immediately.
2573 // TODO: Try to relax this later back to lazy completion, which should be
2574 // faster
2575
Igor Murashkin073f8572013-05-02 14:59:28 -07002576 if (mInputStream != NULL && mInputStream->isConfiguring()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002577 bool streamReConfigured = false;
2578 res = mInputStream->finishConfiguration(&streamReConfigured);
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002579 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002580 CLOGE("Can't finish configuring input stream %d: %s (%d)",
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002581 mInputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002582 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002583 if ((res == NO_INIT || res == DEAD_OBJECT) && mInputStream->isAbandoned()) {
2584 return DEAD_OBJECT;
2585 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002586 return BAD_VALUE;
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002587 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002588 if (streamReConfigured) {
2589 mInterface->onStreamReConfigured(mInputStream->getId());
2590 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002591 }
2592
2593 for (size_t i = 0; i < mOutputStreams.size(); i++) {
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002594 sp<Camera3OutputStreamInterface> outputStream = mOutputStreams[i];
Zhijun He5d677d12016-05-29 16:52:39 -07002595 if (outputStream->isConfiguring() && !outputStream->isConsumerConfigurationDeferred()) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002596 bool streamReConfigured = false;
2597 res = outputStream->finishConfiguration(&streamReConfigured);
Igor Murashkin073f8572013-05-02 14:59:28 -07002598 if (res != OK) {
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002599 CLOGE("Can't finish configuring output stream %d: %s (%d)",
Igor Murashkin073f8572013-05-02 14:59:28 -07002600 outputStream->getId(), strerror(-res), res);
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002601 cancelStreamsConfigurationLocked();
Shuzhen Wang210ba5c2018-07-25 16:47:40 -07002602 if ((res == NO_INIT || res == DEAD_OBJECT) && outputStream->isAbandoned()) {
2603 return DEAD_OBJECT;
2604 }
Chien-Yu Chen9b5860b2016-06-10 13:39:09 -07002605 return BAD_VALUE;
Igor Murashkin073f8572013-05-02 14:59:28 -07002606 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07002607 if (streamReConfigured) {
2608 mInterface->onStreamReConfigured(outputStream->getId());
2609 }
Eino-Ville Talvala4c956762013-04-19 17:26:13 -07002610 }
2611 }
2612
Emilian Peevd865f0d2023-03-17 17:13:07 -07002613 mRequestThread->setComposerSurface(mComposerOutput);
Emilian Peeve23f1d92021-09-20 14:56:01 -07002614
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002615 // Request thread needs to know to avoid using repeat-last-settings protocol
2616 // across configure_streams() calls
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002617 if (notifyRequestThread) {
Shuzhen Wang99080502021-03-07 21:08:20 -08002618 mRequestThread->configurationComplete(mIsConstrainedHighSpeedConfiguration,
2619 sessionParams, mGroupIdPhysicalCameraMap);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002620 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002621
Zhijun He90f7c372016-08-16 16:19:43 -07002622 char value[PROPERTY_VALUE_MAX];
2623 property_get("camera.fifo.disable", value, "0");
2624 int32_t disableFifo = atoi(value);
2625 if (disableFifo != 1) {
2626 // Boost priority of request thread to SCHED_FIFO.
2627 pid_t requestThreadTid = mRequestThread->getTid();
2628 res = requestPriority(getpid(), requestThreadTid,
Mikhail Naganov83f04272017-02-07 10:45:09 -08002629 kRequestThreadPriority, /*isForApp*/ false, /*asynchronous*/ false);
Zhijun He90f7c372016-08-16 16:19:43 -07002630 if (res != OK) {
2631 ALOGW("Can't set realtime priority for request processing thread: %s (%d)",
2632 strerror(-res), res);
2633 } else {
2634 ALOGD("Set real time priority for request queue thread (tid %d)", requestThreadTid);
2635 }
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -07002636 }
2637
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002638 // Update device state
Emilian Peev5fbe0ba2017-10-20 15:45:45 +01002639 const camera_metadata_t *newSessionParams = sessionParams.getAndLock();
2640 const camera_metadata_t *currentSessionParams = mSessionParams.getAndLock();
2641 bool updateSessionParams = (newSessionParams != currentSessionParams) ? true : false;
2642 sessionParams.unlock(newSessionParams);
2643 mSessionParams.unlock(currentSessionParams);
2644 if (updateSessionParams) {
2645 mSessionParams = sessionParams;
2646 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002647
Eino-Ville Talvalaea26c772013-06-11 16:04:06 -07002648 mNeedConfig = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002649
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002650 internalUpdateStatusLocked((mFakeStreamId == NO_STREAM) ?
Ruben Brunk183f0562015-08-12 12:55:02 -07002651 STATUS_CONFIGURED : STATUS_UNCONFIGURED);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002652
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002653 ALOGV("%s: Camera %s: Stream configuration complete", __FUNCTION__, mId.string());
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002654
Zhijun He0a210512014-07-24 13:45:15 -07002655 // tear down the deleted streams after configure streams.
2656 mDeletedStreams.clear();
2657
Emilian Peevac3ce6c2017-12-12 15:27:02 +00002658 auto rc = mPreparerThread->resume();
2659 if (rc != OK) {
2660 SET_ERR_L("%s: Camera %s: Preparer thread failed to resume!", __FUNCTION__, mId.string());
2661 return rc;
2662 }
2663
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002664 if (mFakeStreamId == NO_STREAM) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002665 mRequestBufferSM.onStreamsConfigured();
2666 }
2667
Cliff Wu3b268182021-07-06 15:44:43 +08002668 // First call injectCamera() and then run configureStreamsLocked() case:
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002669 // Since the streams configuration of the injection camera is based on the internal camera, we
Cliff Wu3b268182021-07-06 15:44:43 +08002670 // must wait until the internal camera configure streams before running the injection job to
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002671 // configure the injection streams.
2672 if (mInjectionMethods->isInjecting()) {
Cliff Wu3b268182021-07-06 15:44:43 +08002673 ALOGD("%s: Injection camera %s: Start to configure streams.",
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002674 __FUNCTION__, mInjectionMethods->getInjectedCamId().string());
2675 res = mInjectionMethods->injectCamera(config, bufferSizes);
2676 if (res != OK) {
2677 ALOGE("Can't finish inject camera process!");
2678 return res;
2679 }
Cliff Wu3b268182021-07-06 15:44:43 +08002680 } else {
2681 // First run configureStreamsLocked() and then call injectCamera() case:
2682 // If the stream configuration has been completed and camera deive is active, but the
2683 // injection camera has not been injected yet, we need to store the stream configuration of
2684 // the internal camera (because the stream configuration of the injection camera is based
2685 // on the internal camera). When injecting occurs later, this configuration can be used by
2686 // the injection camera.
2687 ALOGV("%s: The stream configuration is complete and the camera device is active, but the"
2688 " injection camera has not been injected yet.", __FUNCTION__);
2689 mInjectionMethods->storeInjectionConfig(config, bufferSizes);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08002690 }
2691
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002692 return OK;
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002693}
2694
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002695status_t Camera3Device::addFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002696 ATRACE_CALL();
2697 status_t res;
2698
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002699 if (mFakeStreamId != NO_STREAM) {
2700 // Should never be adding a second fake stream when one is already
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002701 // active
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002702 SET_ERR_L("%s: Camera %s: A fake stream already exists!",
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002703 __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002704 return INVALID_OPERATION;
2705 }
2706
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002707 ALOGV("%s: Camera %s: Adding a fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002708
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002709 sp<Camera3OutputStreamInterface> fakeStream =
2710 new Camera3FakeStream(mNextStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002711
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002712 res = mOutputStreams.add(mNextStreamId, fakeStream);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002713 if (res < 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002714 SET_ERR_L("Can't add fake stream to set: %s (%d)", strerror(-res), res);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002715 return res;
2716 }
2717
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002718 mFakeStreamId = mNextStreamId;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002719 mNextStreamId++;
2720
2721 return OK;
2722}
2723
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002724status_t Camera3Device::tryRemoveFakeStreamLocked() {
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002725 ATRACE_CALL();
2726 status_t res;
2727
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002728 if (mFakeStreamId == NO_STREAM) return OK;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002729 if (mOutputStreams.size() == 1) return OK;
2730
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002731 ALOGV("%s: Camera %s: Removing the fake stream", __FUNCTION__, mId.string());
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002732
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002733 // Ok, have a fake stream and there's at least one other output stream,
2734 // so remove the fake
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002735
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002736 sp<Camera3StreamInterface> deletedStream = mOutputStreams.get(mFakeStreamId);
Yin-Chia Yeh4ee35432018-10-10 13:52:31 -07002737 if (deletedStream == nullptr) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002738 SET_ERR_L("Fake stream %d does not appear to exist", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002739 return INVALID_OPERATION;
2740 }
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002741 mOutputStreams.remove(mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002742
2743 // Free up the stream endpoint so that it can be used by some other stream
2744 res = deletedStream->disconnect();
2745 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002746 SET_ERR_L("Can't disconnect deleted fake stream %d", mFakeStreamId);
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002747 // fall through since we want to still list the stream as deleted.
2748 }
2749 mDeletedStreams.add(deletedStream);
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002750 mFakeStreamId = NO_STREAM;
Eino-Ville Talvala16a2ada2014-08-27 14:41:33 -07002751
2752 return res;
2753}
2754
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002755void Camera3Device::setErrorState(const char *fmt, ...) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002756 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002757 Mutex::Autolock l(mLock);
2758 va_list args;
2759 va_start(args, fmt);
2760
2761 setErrorStateLockedV(fmt, args);
2762
2763 va_end(args);
2764}
2765
2766void Camera3Device::setErrorStateV(const char *fmt, va_list args) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002767 ATRACE_CALL();
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002768 Mutex::Autolock l(mLock);
2769 setErrorStateLockedV(fmt, args);
2770}
2771
2772void Camera3Device::setErrorStateLocked(const char *fmt, ...) {
2773 va_list args;
2774 va_start(args, fmt);
2775
2776 setErrorStateLockedV(fmt, args);
2777
2778 va_end(args);
2779}
2780
2781void Camera3Device::setErrorStateLockedV(const char *fmt, va_list args) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002782 // Print out all error messages to log
2783 String8 errorCause = String8::formatV(fmt, args);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002784 ALOGE("Camera %s: %s", mId.string(), errorCause.string());
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002785
2786 // But only do error state transition steps for the first error
Zhijun Heb05eeae2013-06-06 13:51:22 -07002787 if (mStatus == STATUS_ERROR || mStatus == STATUS_UNINITIALIZED) return;
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002788
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002789 mErrorCause = errorCause;
2790
Yin-Chia Yeh3d145ae2017-07-27 12:47:03 -07002791 if (mRequestThread != nullptr) {
2792 mRequestThread->setPaused(true);
2793 }
Ruben Brunk183f0562015-08-12 12:55:02 -07002794 internalUpdateStatusLocked(STATUS_ERROR);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002795
2796 // Notify upstream about a device error
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002797 sp<NotificationListener> listener = mListener.promote();
2798 if (listener != NULL) {
2799 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002800 CaptureResultExtras());
Shuzhen Wang316781a2020-08-18 18:11:01 -07002801 mSessionStatsBuilder.onDeviceError();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002802 }
2803
2804 // Save stack trace. View by dumping it later.
2805 CameraTraces::saveTrace();
2806 // TODO: consider adding errorCause and client pid/procname
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07002807}
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08002808
2809/**
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002810 * In-flight request management
2811 */
2812
Jianing Weicb0652e2014-03-12 18:29:36 -07002813status_t Camera3Device::registerInFlight(uint32_t frameNumber,
Chien-Yu Chend196d612015-06-22 19:49:01 -07002814 int32_t numBuffers, CaptureResultExtras resultExtras, bool hasInput,
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08002815 bool hasAppCallback, nsecs_t minExpectedDuration, nsecs_t maxExpectedDuration,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07002816 bool isFixedFps, const std::set<std::set<String8>>& physicalCameraIds,
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00002817 bool isStillCapture, bool isZslCapture, bool rotateAndCropAuto, bool autoframingAuto,
Shuzhen Wang99080502021-03-07 21:08:20 -08002818 const std::set<std::string>& cameraIdsWithZoom,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002819 const SurfaceMap& outputSurfaces, nsecs_t requestTimeNs) {
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002820 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002821 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002822
2823 ssize_t res;
Chien-Yu Chend196d612015-06-22 19:49:01 -07002824 res = mInFlightMap.add(frameNumber, InFlightRequest(numBuffers, resultExtras, hasInput,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07002825 hasAppCallback, minExpectedDuration, maxExpectedDuration, isFixedFps, physicalCameraIds,
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00002826 isStillCapture, isZslCapture, rotateAndCropAuto, autoframingAuto, cameraIdsWithZoom,
2827 requestTimeNs, outputSurfaces));
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002828 if (res < 0) return res;
2829
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002830 if (mInFlightMap.size() == 1) {
Emilian Peev26d975d2018-07-05 14:52:57 +01002831 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2832 // avoid a deadlock during reprocess requests.
2833 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002834 if (mStatusTracker != nullptr) {
2835 mStatusTracker->markComponentActive(mInFlightStatusId);
2836 }
Eino-Ville Talvala24b366e2016-07-21 12:53:07 -07002837 }
2838
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002839 mExpectedInflightDuration += maxExpectedDuration;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07002840 return OK;
2841}
2842
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002843void Camera3Device::onInflightEntryRemovedLocked(nsecs_t duration) {
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002844 // Indicate idle inFlightMap to the status tracker
2845 if (mInFlightMap.size() == 0) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07002846 mRequestBufferSM.onInflightMapEmpty();
Emilian Peev26d975d2018-07-05 14:52:57 +01002847 // Hold a separate dedicated tracker lock to prevent race with disconnect and also
2848 // avoid a deadlock during reprocess requests.
2849 Mutex::Autolock l(mTrackerLock);
Yin-Chia Yeh38dfde52017-06-27 17:13:33 -07002850 if (mStatusTracker != nullptr) {
2851 mStatusTracker->markComponentIdle(mInFlightStatusId, Fence::NO_FENCE);
2852 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002853 }
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07002854 mExpectedInflightDuration -= duration;
Shuzhen Wangcadb3302016-11-04 14:17:56 -07002855}
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002856
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002857void Camera3Device::checkInflightMapLengthLocked() {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04002858 // Validation check - if we have too many in-flight frames with long total inflight duration,
Yin-Chia Yeh99fd0972019-06-27 14:22:44 -07002859 // something has likely gone wrong. This might still be legit only if application send in
2860 // a long burst of long exposure requests.
2861 if (mExpectedInflightDuration > kMinWarnInflightDuration) {
2862 if (!mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() > kInFlightWarnLimit) {
2863 CLOGW("In-flight list too large: %zu, total inflight duration %" PRIu64,
2864 mInFlightMap.size(), mExpectedInflightDuration);
2865 } else if (mIsConstrainedHighSpeedConfiguration && mInFlightMap.size() >
2866 kInFlightWarnLimitHighSpeed) {
2867 CLOGW("In-flight list too large for high speed configuration: %zu,"
2868 "total inflight duration %" PRIu64,
2869 mInFlightMap.size(), mExpectedInflightDuration);
2870 }
Chien-Yu Chen43e69a62014-11-25 16:38:33 -08002871 }
2872}
2873
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002874void Camera3Device::onInflightMapFlushedLocked() {
2875 mExpectedInflightDuration = 0;
2876}
2877
2878void Camera3Device::removeInFlightMapEntryLocked(int idx) {
Jayant Chowdharyd4776262020-06-23 23:45:57 -07002879 ATRACE_HFR_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002880 nsecs_t duration = mInFlightMap.valueAt(idx).maxExpectedDuration;
2881 mInFlightMap.removeItemsAt(idx, 1);
2882
2883 onInflightEntryRemovedLocked(duration);
2884}
2885
2886
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002887void Camera3Device::flushInflightRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07002888 ATRACE_CALL();
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002889 sp<NotificationListener> listener;
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002890 {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002891 std::lock_guard<std::mutex> l(mOutputLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07002892 listener = mListener.promote();
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002893 }
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08002894
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002895 FlushInflightReqStates states {
2896 mId, mInFlightLock, mInFlightMap, mUseHalBufManager,
Shuzhen Wang316781a2020-08-18 18:11:01 -07002897 listener, *this, *mInterface, *this, mSessionStatsBuilder};
Eino-Ville Talvala7d346fa2013-03-11 14:13:50 -07002898
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002899 camera3::flushInflightRequests(states);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07002900}
2901
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07002902CameraMetadata Camera3Device::getLatestRequestLocked() {
Igor Murashkin1e479c02013-09-06 16:55:14 -07002903 ALOGV("%s", __FUNCTION__);
2904
Igor Murashkin1e479c02013-09-06 16:55:14 -07002905 CameraMetadata retVal;
2906
2907 if (mRequestThread != NULL) {
2908 retVal = mRequestThread->getLatestRequest();
2909 }
2910
Igor Murashkin1e479c02013-09-06 16:55:14 -07002911 return retVal;
2912}
2913
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002914void Camera3Device::monitorMetadata(TagMonitor::eventSource source,
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002915 int64_t frameNumber, nsecs_t timestamp, const CameraMetadata& metadata,
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07002916 const std::unordered_map<std::string, CameraMetadata>& physicalMetadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002917 const camera_stream_buffer_t *outputBuffers, uint32_t numOutputBuffers,
2918 int32_t inputStreamId) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07002919
2920 mTagMonitor.monitorMetadata(source, frameNumber, timestamp, metadata,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07002921 physicalMetadata, outputBuffers, numOutputBuffers, inputStreamId);
Eino-Ville Talvala4d453832016-07-15 11:56:53 -07002922}
2923
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002924void Camera3Device::cleanupNativeHandles(
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002925 std::vector<native_handle_t*> *handles, bool closeFd) {
2926 if (handles == nullptr) {
2927 return;
2928 }
2929 if (closeFd) {
2930 for (auto& handle : *handles) {
2931 native_handle_close(handle);
2932 }
2933 }
2934 for (auto& handle : *handles) {
2935 native_handle_delete(handle);
2936 }
2937 handles->clear();
2938 return;
2939}
2940
Jayant Chowdhary35642f22022-01-08 00:39:39 +00002941/**
2942 * HalInterface inner class methods
2943 */
2944
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002945void Camera3Device::HalInterface::getInflightBufferKeys(
2946 std::vector<std::pair<int32_t, int32_t>>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002947 mBufferRecords.getInflightBufferKeys(out);
Yin-Chia Yehf3fe36f2017-07-07 18:23:18 -07002948 return;
2949}
2950
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002951void Camera3Device::HalInterface::getInflightRequestBufferKeys(
2952 std::vector<uint64_t>* out) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002953 mBufferRecords.getInflightRequestBufferKeys(out);
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002954 return;
2955}
2956
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002957bool Camera3Device::HalInterface::verifyBufferIds(
2958 int32_t streamId, std::vector<uint64_t>& bufIds) {
2959 return mBufferRecords.verifyBufferIds(streamId, bufIds);
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08002960}
2961
2962status_t Camera3Device::HalInterface::popInflightBuffer(
Yin-Chia Yehf4650602017-01-10 13:13:39 -08002963 int32_t frameNumber, int32_t streamId,
2964 /*out*/ buffer_handle_t **buffer) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002965 return mBufferRecords.popInflightBuffer(frameNumber, streamId, buffer);
Yin-Chia Yehf8e28fb2019-05-16 11:46:54 -07002966}
2967
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002968status_t Camera3Device::HalInterface::pushInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002969 uint64_t bufferId, buffer_handle_t* buf, int32_t streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002970 return mBufferRecords.pushInflightRequestBuffer(bufferId, buf, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002971}
2972
2973// Find and pop a buffer_handle_t based on bufferId
2974status_t Camera3Device::HalInterface::popInflightRequestBuffer(
Yin-Chia Yeh84be5782019-03-01 11:47:02 -08002975 uint64_t bufferId,
2976 /*out*/ buffer_handle_t** buffer,
2977 /*optional out*/ int32_t* streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002978 return mBufferRecords.popInflightRequestBuffer(bufferId, buffer, streamId);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07002979}
2980
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002981std::pair<bool, uint64_t> Camera3Device::HalInterface::getBufferId(
2982 const buffer_handle_t& buf, int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002983 return mBufferRecords.getBufferId(buf, streamId);
Yin-Chia Yeh77327052017-01-09 18:23:07 -08002984}
2985
Shuzhen Wangcd5b1822021-09-07 11:52:48 -07002986uint64_t Camera3Device::HalInterface::removeOneBufferCache(int streamId,
2987 const native_handle_t* handle) {
2988 return mBufferRecords.removeOneBufferCache(streamId, handle);
2989}
2990
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002991void Camera3Device::HalInterface::onBufferFreed(
2992 int streamId, const native_handle_t* handle) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08002993 uint32_t bufferId = mBufferRecords.removeOneBufferCache(streamId, handle);
2994 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
2995 if (bufferId != BUFFER_ID_NO_BUFFER) {
2996 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002997 }
Yin-Chia Yehbe83fa72017-03-30 13:35:36 -07002998}
2999
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07003000void Camera3Device::HalInterface::onStreamReConfigured(int streamId) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08003001 std::vector<uint64_t> bufIds = mBufferRecords.clearBufferCaches(streamId);
3002 std::lock_guard<std::mutex> lock(mFreedBuffersLock);
3003 for (auto bufferId : bufIds) {
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07003004 mFreedBuffers.push_back(std::make_pair(streamId, bufferId));
3005 }
Yin-Chia Yeh573a2702019-04-17 10:08:55 -07003006}
3007
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003008/**
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003009 * RequestThread inner class methods
3010 */
3011
3012Camera3Device::RequestThread::RequestThread(wp<Camera3Device> parent,
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003013 sp<StatusTracker> statusTracker,
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003014 sp<HalInterface> interface, const Vector<int32_t>& sessionParamKeys,
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07003015 bool useHalBufManager,
Austin Borger18b30a72022-10-27 12:20:29 -07003016 bool supportCameraMute,
Shuzhen Wangaf22e912023-04-11 16:03:17 -07003017 bool overrideToPortrait,
3018 bool supportSettingsOverride) :
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003019 Thread(/*canCallJava*/false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003020 mParent(parent),
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003021 mStatusTracker(statusTracker),
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003022 mInterface(interface),
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07003023 mListener(nullptr),
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003024 mId(getId(parent)),
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07003025 mRequestClearing(false),
Shuzhen Wang316781a2020-08-18 18:11:01 -07003026 mFirstRepeating(false),
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003027 mReconfigured(false),
3028 mDoPause(false),
3029 mPaused(true),
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07003030 mNotifyPipelineDrain(false),
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003031 mFrameNumber(0),
Jianing Weicb0652e2014-03-12 18:29:36 -07003032 mLatestRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003033 mCurrentAfTriggerId(0),
3034 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003035 mRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE),
Bharatt Kukreja0e13db32022-12-07 21:38:45 +00003036 mAutoframingOverride(ANDROID_CONTROL_AUTOFRAMING_OFF),
Emilian Peeve23f1d92021-09-20 14:56:01 -07003037 mComposerOutput(false),
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07003038 mCameraMute(ANDROID_SENSOR_TEST_PATTERN_MODE_OFF),
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003039 mCameraMuteChanged(false),
Shuzhen Wangaf22e912023-04-11 16:03:17 -07003040 mSettingsOverride(ANDROID_CONTROL_SETTINGS_OVERRIDE_OFF),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003041 mRepeatingLastFrameNumber(
3042 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07003043 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003044 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003045 mRequestLatency(kRequestLatencyBinSize),
3046 mSessionParamKeys(sessionParamKeys),
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003047 mLatestSessionParams(sessionParamKeys.size()),
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07003048 mUseHalBufManager(useHalBufManager),
Austin Borger18b30a72022-10-27 12:20:29 -07003049 mSupportCameraMute(supportCameraMute),
Shuzhen Wangaf22e912023-04-11 16:03:17 -07003050 mOverrideToPortrait(overrideToPortrait),
3051 mSupportSettingsOverride(supportSettingsOverride) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07003052 mStatusId = statusTracker->addComponent("RequestThread");
Emilian Peeva1b26262023-02-06 17:27:41 -08003053 mVndkVersion = property_get_int32("ro.vndk.version", __ANDROID_API_FUTURE__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003054}
3055
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003056Camera3Device::RequestThread::~RequestThread() {}
3057
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003058void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003059 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003060 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003061 Mutex::Autolock l(mRequestLock);
3062 mListener = listener;
3063}
3064
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003065void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
Shuzhen Wang99080502021-03-07 21:08:20 -08003066 const CameraMetadata& sessionParams,
3067 const std::map<int32_t, std::set<String8>>& groupIdPhysicalCameraMap) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003068 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003069 Mutex::Autolock l(mRequestLock);
3070 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003071 mLatestSessionParams = sessionParams;
Shuzhen Wang99080502021-03-07 21:08:20 -08003072 mGroupIdPhysicalCameraMap = groupIdPhysicalCameraMap;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003073 // Prepare video stream for high speed recording.
3074 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003075 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003076}
3077
Jianing Wei90e59c92014-03-12 18:29:36 -07003078status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003079 List<sp<CaptureRequest> > &requests,
3080 /*out*/
3081 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003082 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07003083 Mutex::Autolock l(mRequestLock);
3084 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
3085 ++it) {
3086 mRequestQueue.push_back(*it);
3087 }
3088
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003089 if (lastFrameNumber != NULL) {
3090 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
3091 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
3092 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
3093 *lastFrameNumber);
3094 }
Jianing Weicb0652e2014-03-12 18:29:36 -07003095
Jianing Wei90e59c92014-03-12 18:29:36 -07003096 unpauseForNewRequests();
3097
3098 return OK;
3099}
3100
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003101
3102status_t Camera3Device::RequestThread::queueTrigger(
3103 RequestTrigger trigger[],
3104 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003105 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003106 Mutex::Autolock l(mTriggerMutex);
3107 status_t ret;
3108
3109 for (size_t i = 0; i < count; ++i) {
3110 ret = queueTriggerLocked(trigger[i]);
3111
3112 if (ret != OK) {
3113 return ret;
3114 }
3115 }
3116
3117 return OK;
3118}
3119
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003120const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
3121 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003122 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003123 if (d != nullptr) return d->mId;
3124 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003125}
3126
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003127status_t Camera3Device::RequestThread::queueTriggerLocked(
3128 RequestTrigger trigger) {
3129
3130 uint32_t tag = trigger.metadataTag;
3131 ssize_t index = mTriggerMap.indexOfKey(tag);
3132
3133 switch (trigger.getTagType()) {
3134 case TYPE_BYTE:
3135 // fall-through
3136 case TYPE_INT32:
3137 break;
3138 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003139 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
3140 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003141 return INVALID_OPERATION;
3142 }
3143
3144 /**
3145 * Collect only the latest trigger, since we only have 1 field
3146 * in the request settings per trigger tag, and can't send more than 1
3147 * trigger per request.
3148 */
3149 if (index != NAME_NOT_FOUND) {
3150 mTriggerMap.editValueAt(index) = trigger;
3151 } else {
3152 mTriggerMap.add(tag, trigger);
3153 }
3154
3155 return OK;
3156}
3157
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003158status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003159 const RequestList &requests,
3160 /*out*/
3161 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003162 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003163 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003164 if (lastFrameNumber != NULL) {
3165 *lastFrameNumber = mRepeatingLastFrameNumber;
3166 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003167 mRepeatingRequests.clear();
Shuzhen Wang316781a2020-08-18 18:11:01 -07003168 mFirstRepeating = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003169 mRepeatingRequests.insert(mRepeatingRequests.begin(),
3170 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003171
3172 unpauseForNewRequests();
3173
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003174 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003175 return OK;
3176}
3177
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003178bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003179 if (mRepeatingRequests.empty()) {
3180 return false;
3181 }
3182 int32_t requestId = requestIn->mResultExtras.requestId;
3183 const RequestList &repeatRequests = mRepeatingRequests;
3184 // All repeating requests are guaranteed to have same id so only check first quest
3185 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
3186 return (firstRequest->mResultExtras.requestId == requestId);
3187}
3188
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003189status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003190 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003191 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003192 return clearRepeatingRequestsLocked(lastFrameNumber);
3193
3194}
3195
Jayant Chowdharya93f3462022-11-01 23:32:45 +00003196status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(
3197 /*out*/int64_t *lastFrameNumber) {
Emilian Peev2295df72021-11-12 18:14:10 -08003198 std::vector<int32_t> streamIds;
3199 for (const auto& request : mRepeatingRequests) {
3200 for (const auto& stream : request->mOutputStreams) {
3201 streamIds.push_back(stream->getId());
3202 }
3203 }
3204
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003205 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003206 if (lastFrameNumber != NULL) {
3207 *lastFrameNumber = mRepeatingLastFrameNumber;
3208 }
Emilian Peev2295df72021-11-12 18:14:10 -08003209
3210 mInterface->repeatingRequestEnd(mRepeatingLastFrameNumber, streamIds);
3211
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003212 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003213 return OK;
3214}
3215
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003216status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003217 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003218 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003219 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003220 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003221
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003222 // Send errors for all requests pending in the request queue, including
3223 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003224 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003225 if (listener != NULL) {
3226 for (RequestList::iterator it = mRequestQueue.begin();
3227 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003228 // Abort the input buffers for reprocess requests.
3229 if ((*it)->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07003230 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003231 camera3::Size inputBufferSize;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07003232 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003233 &inputBufferSize, /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003234 if (res != OK) {
3235 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
3236 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3237 } else {
Emilian Peevf4816702020-04-03 15:44:51 -07003238 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003239 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
3240 if (res != OK) {
3241 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
3242 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3243 }
3244 }
3245 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003246 // Set the frame number this request would have had, if it
3247 // had been submitted; this frame number will not be reused.
3248 // The requestId and burstId fields were set when the request was
3249 // submitted originally (in convertMetadataListToRequestListLocked)
3250 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003251 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003252 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003253 }
3254 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003255 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08003256
3257 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003258 mTriggerMap.clear();
Jayant Chowdharya93f3462022-11-01 23:32:45 +00003259 clearRepeatingRequestsLocked(lastFrameNumber);
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07003260 mRequestClearing = true;
Emilian Peev8dae54c2019-12-02 15:17:17 -08003261 mRequestSignal.signal();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003262 return OK;
3263}
3264
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003265status_t Camera3Device::RequestThread::flush() {
3266 ATRACE_CALL();
3267 Mutex::Autolock l(mFlushLock);
3268
Emilian Peev08dd2452017-04-06 16:55:14 +01003269 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003270}
3271
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003272void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003273 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003274 Mutex::Autolock l(mPauseLock);
3275 mDoPause = paused;
3276 mDoPauseSignal.signal();
3277}
3278
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003279status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
3280 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003281 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003282 Mutex::Autolock l(mLatestRequestMutex);
3283 status_t res;
3284 while (mLatestRequestId != requestId) {
3285 nsecs_t startTime = systemTime();
3286
3287 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3288 if (res != OK) return res;
3289
3290 timeout -= (systemTime() - startTime);
3291 }
3292
3293 return OK;
3294}
3295
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003296void Camera3Device::RequestThread::requestExit() {
3297 // Call parent to set up shutdown
3298 Thread::requestExit();
3299 // The exit from any possible waits
3300 mDoPauseSignal.signal();
3301 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07003302
3303 mRequestLatency.log("ProcessCaptureRequest latency histogram");
3304 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003305}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003306
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003307void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003308 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003309 bool surfaceAbandoned = false;
3310 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003311 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003312 {
3313 Mutex::Autolock l(mRequestLock);
3314 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3315 // repeating requests.
3316 for (const auto& request : mRepeatingRequests) {
3317 for (const auto& s : request->mOutputStreams) {
3318 if (s->isAbandoned()) {
3319 surfaceAbandoned = true;
3320 clearRepeatingRequestsLocked(&lastFrameNumber);
3321 break;
3322 }
3323 }
3324 if (surfaceAbandoned) {
3325 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003326 }
3327 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003328 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003329 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003330
3331 if (listener != NULL && surfaceAbandoned) {
3332 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003333 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003334}
3335
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003336bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003337 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003338 status_t res;
3339 size_t batchSize = mNextRequests.size();
Emilian Peevf4816702020-04-03 15:44:51 -07003340 std::vector<camera_capture_request_t*> requests(batchSize);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003341 uint32_t numRequestProcessed = 0;
3342 for (size_t i = 0; i < batchSize; i++) {
3343 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07003344 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003345 }
3346
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003347 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
3348
3349 bool triggerRemoveFailed = false;
3350 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
3351 for (size_t i = 0; i < numRequestProcessed; i++) {
3352 NextRequest& nextRequest = mNextRequests.editItemAt(i);
3353 nextRequest.submitted = true;
3354
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003355 updateNextRequest(nextRequest);
Emilian Peevaebbe412018-01-15 13:53:24 +00003356
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003357 if (!triggerRemoveFailed) {
3358 // Remove any previously queued triggers (after unlock)
3359 status_t removeTriggerRes = removeTriggers(mPrevRequest);
3360 if (removeTriggerRes != OK) {
3361 triggerRemoveFailed = true;
3362 triggerFailedRequest = nextRequest;
3363 }
3364 }
3365 }
3366
3367 if (triggerRemoveFailed) {
3368 SET_ERR("RequestThread: Unable to remove triggers "
3369 "(capture request %d, HAL device: %s (%d)",
3370 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
3371 cleanUpFailedRequests(/*sendRequestError*/ false);
3372 return false;
3373 }
3374
3375 if (res != OK) {
3376 // Should only get a failure here for malformed requests or device-level
3377 // errors, so consider all errors fatal. Bad metadata failures should
3378 // come through notify.
3379 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
3380 mNextRequests[numRequestProcessed].halRequest.frame_number,
3381 strerror(-res), res);
3382 cleanUpFailedRequests(/*sendRequestError*/ false);
3383 return false;
3384 }
3385 return true;
3386}
3387
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003388Camera3Device::RequestThread::ExpectedDurationInfo
3389 Camera3Device::RequestThread::calculateExpectedDurationRange(
3390 const camera_metadata_t *request) {
3391 ExpectedDurationInfo expectedDurationInfo = {
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003392 InFlightRequest::kDefaultMinExpectedDuration,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003393 InFlightRequest::kDefaultMaxExpectedDuration,
3394 /*isFixedFps*/false};
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003395 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3396 find_camera_metadata_ro_entry(request,
3397 ANDROID_CONTROL_AE_MODE,
3398 &e);
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003399 if (e.count == 0) return expectedDurationInfo;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003400
3401 switch (e.data.u8[0]) {
3402 case ANDROID_CONTROL_AE_MODE_OFF:
3403 find_camera_metadata_ro_entry(request,
3404 ANDROID_SENSOR_EXPOSURE_TIME,
3405 &e);
3406 if (e.count > 0) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003407 expectedDurationInfo.minDuration = e.data.i64[0];
3408 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003409 }
3410 find_camera_metadata_ro_entry(request,
3411 ANDROID_SENSOR_FRAME_DURATION,
3412 &e);
3413 if (e.count > 0) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003414 expectedDurationInfo.minDuration =
3415 std::max(e.data.i64[0], expectedDurationInfo.minDuration);
3416 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003417 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003418 expectedDurationInfo.isFixedFps = false;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003419 break;
3420 default:
3421 find_camera_metadata_ro_entry(request,
3422 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
3423 &e);
3424 if (e.count > 1) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003425 expectedDurationInfo.minDuration = 1e9 / e.data.i32[1];
3426 expectedDurationInfo.maxDuration = 1e9 / e.data.i32[0];
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003427 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003428 expectedDurationInfo.isFixedFps = (e.data.i32[1] == e.data.i32[0]);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003429 break;
3430 }
3431
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003432 return expectedDurationInfo;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003433}
3434
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003435bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
3436 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
3437 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
3438 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
3439 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
3440 return true;
3441 }
3442
3443 return false;
3444}
3445
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003446void Camera3Device::RequestThread::updateNextRequest(NextRequest& nextRequest) {
3447 // Update the latest request sent to HAL
Shuzhen Wang83bff122020-11-20 15:51:39 -08003448 camera_capture_request_t& halRequest = nextRequest.halRequest;
3449 if (halRequest.settings != NULL) { // Don't update if they were unchanged
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003450 Mutex::Autolock al(mLatestRequestMutex);
3451
Shuzhen Wang83bff122020-11-20 15:51:39 -08003452 camera_metadata_t* cloned = clone_camera_metadata(halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003453 mLatestRequest.acquire(cloned);
3454
3455 mLatestPhysicalRequest.clear();
Shuzhen Wang83bff122020-11-20 15:51:39 -08003456 for (uint32_t i = 0; i < halRequest.num_physcam_settings; i++) {
3457 cloned = clone_camera_metadata(halRequest.physcam_settings[i]);
3458 mLatestPhysicalRequest.emplace(halRequest.physcam_id[i],
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003459 CameraMetadata(cloned));
3460 }
3461
3462 sp<Camera3Device> parent = mParent.promote();
3463 if (parent != NULL) {
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07003464 int32_t inputStreamId = -1;
3465 if (halRequest.input_buffer != nullptr) {
3466 inputStreamId = Camera3Stream::cast(halRequest.input_buffer->stream)->getId();
3467 }
3468
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003469 parent->monitorMetadata(TagMonitor::REQUEST,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003470 halRequest.frame_number,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07003471 0, mLatestRequest, mLatestPhysicalRequest, halRequest.output_buffers,
3472 halRequest.num_output_buffers, inputStreamId);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003473 }
3474 }
3475
Shuzhen Wang83bff122020-11-20 15:51:39 -08003476 if (halRequest.settings != NULL) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003477 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
Shuzhen Wang83bff122020-11-20 15:51:39 -08003478 halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003479 }
3480
Shuzhen Wang83bff122020-11-20 15:51:39 -08003481 cleanupPhysicalSettings(nextRequest.captureRequest, &halRequest);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003482}
3483
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003484bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
3485 ATRACE_CALL();
3486 bool updatesDetected = false;
3487
Emilian Peev4ec17882019-01-24 17:16:58 -08003488 CameraMetadata updatedParams(mLatestSessionParams);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003489 for (auto tag : mSessionParamKeys) {
3490 camera_metadata_ro_entry entry = settings.find(tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003491 camera_metadata_entry lastEntry = updatedParams.find(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003492
3493 if (entry.count > 0) {
3494 bool isDifferent = false;
3495 if (lastEntry.count > 0) {
3496 // Have a last value, compare to see if changed
3497 if (lastEntry.type == entry.type &&
3498 lastEntry.count == entry.count) {
3499 // Same type and count, compare values
3500 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
3501 size_t entryBytes = bytesPerValue * lastEntry.count;
3502 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
3503 if (cmp != 0) {
3504 isDifferent = true;
3505 }
3506 } else {
3507 // Count or type has changed
3508 isDifferent = true;
3509 }
3510 } else {
3511 // No last entry, so always consider to be different
3512 isDifferent = true;
3513 }
3514
3515 if (isDifferent) {
3516 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003517 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
3518 updatesDetected = true;
3519 }
Emilian Peev4ec17882019-01-24 17:16:58 -08003520 updatedParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003521 }
3522 } else if (lastEntry.count > 0) {
3523 // Value has been removed
3524 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003525 updatedParams.erase(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003526 updatesDetected = true;
3527 }
3528 }
3529
Emilian Peev4ec17882019-01-24 17:16:58 -08003530 bool reconfigureRequired;
3531 if (updatesDetected) {
3532 reconfigureRequired = mInterface->isReconfigurationRequired(mLatestSessionParams,
3533 updatedParams);
3534 mLatestSessionParams = updatedParams;
3535 } else {
3536 reconfigureRequired = false;
3537 }
3538
3539 return reconfigureRequired;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003540}
3541
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003542bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003543 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003544 status_t res;
Jayant Chowdhary37eca242019-11-18 08:55:56 -08003545 // Any function called from threadLoop() must not hold mInterfaceLock since
3546 // it could lead to deadlocks (disconnect() -> hold mInterfaceMutex -> wait for request thread
3547 // to finish -> request thread waits on mInterfaceMutex) http://b/143513518
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003548
3549 // Handle paused state.
3550 if (waitIfPaused()) {
3551 return true;
3552 }
3553
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003554 // Wait for the next batch of requests.
3555 waitForNextRequestBatch();
3556 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003557 return true;
3558 }
3559
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003560 // Get the latest request ID, if any
3561 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003562 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00003563 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003564 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003565 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003566 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003567 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3568 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003569 }
3570
Emilian Peevd865f0d2023-03-17 17:13:07 -07003571 for (size_t i = 0; i < mNextRequests.size(); i++) {
3572 auto& nextRequest = mNextRequests.editItemAt(i);
3573 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3574 // Do not override rotate&crop for stream configurations that include
3575 // SurfaceViews(HW_COMPOSER) output, unless mOverrideToPortrait is set.
3576 // The display rotation there will be compensated by NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY
3577 captureRequest->mRotateAndCropChanged = (mComposerOutput && !mOverrideToPortrait) ? false :
3578 overrideAutoRotateAndCrop(captureRequest);
3579 captureRequest->mAutoframingChanged = overrideAutoframing(captureRequest);
3580 }
3581
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003582 // 'mNextRequests' will at this point contain either a set of HFR batched requests
3583 // or a single request from streaming or burst. In either case the first element
3584 // should contain the latest camera settings that we need to check for any session
3585 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00003586 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003587 res = OK;
3588
3589 //Input stream buffers are already acquired at this point so an input stream
3590 //will not be able to move to idle state unless we force it.
3591 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3592 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
3593 if (res != OK) {
3594 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
3595 cleanUpFailedRequests(/*sendRequestError*/ false);
3596 return false;
3597 }
3598 }
3599
3600 if (res == OK) {
Emilian Peev3bead5f2020-05-28 17:29:08 -07003601 sp<Camera3Device> parent = mParent.promote();
3602 if (parent != nullptr) {
3603 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams, mStatusId);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003604 }
3605
3606 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3607 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
3608 if (res != OK) {
3609 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
3610 cleanUpFailedRequests(/*sendRequestError*/ false);
3611 return false;
3612 }
3613 }
3614 }
3615 }
3616
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003617 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003618 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003619 if (res == TIMED_OUT) {
3620 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003621 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003622 // Check if any stream is abandoned.
3623 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003624 return true;
3625 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003626 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003627 return false;
3628 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003629
Zhijun Hecc27e112013-10-03 16:12:43 -07003630 // Inform waitUntilRequestProcessed thread of a new request ID
3631 {
3632 Mutex::Autolock al(mLatestRequestMutex);
3633
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003634 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003635 mLatestRequestSignal.signal();
3636 }
3637
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003638 // Submit a batch of requests to HAL.
3639 // Use flush lock only when submitting multilple requests in a batch.
3640 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3641 // which may take a long time to finish so synchronizing flush() and
3642 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3643 // For now, only synchronize for high speed recording and we should figure something out for
3644 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003645 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003646
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003647 if (useFlushLock) {
3648 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003649 }
3650
Zhijun Hef0645c12016-08-02 00:58:11 -07003651 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003652 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07003653
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08003654 sp<Camera3Device> parent = mParent.promote();
3655 if (parent != nullptr) {
3656 parent->mRequestBufferSM.onSubmittingRequest();
3657 }
3658
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003659 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07003660 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh11648852019-05-16 10:42:54 -07003661 submitRequestSuccess = sendRequestsBatch();
3662
Shuzhen Wang686f6442017-06-20 16:16:04 -07003663 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
3664 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07003665
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003666 if (useFlushLock) {
3667 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003668 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003669
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003670 // Unset as current request
3671 {
3672 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003673 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003674 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003675 mRequestSubmittedSignal.signal();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003676
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003677 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003678}
3679
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003680status_t Camera3Device::removeFwkOnlyRegionKeys(CameraMetadata *request) {
3681 static const std::array<uint32_t, 4> kFwkOnlyRegionKeys = {ANDROID_CONTROL_AF_REGIONS_SET,
3682 ANDROID_CONTROL_AE_REGIONS_SET, ANDROID_CONTROL_AWB_REGIONS_SET,
3683 ANDROID_SCALER_CROP_REGION_SET};
3684 if (request == nullptr) {
3685 ALOGE("%s request metadata nullptr", __FUNCTION__);
3686 return BAD_VALUE;
3687 }
3688 status_t res = OK;
3689 for (const auto &key : kFwkOnlyRegionKeys) {
3690 if (request->exists(key)) {
3691 res = request->erase(key);
3692 if (res != OK) {
3693 return res;
3694 }
3695 }
3696 }
3697 return OK;
3698}
3699
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003700status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003701 ATRACE_CALL();
3702
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003703 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003704 for (size_t i = 0; i < mNextRequests.size(); i++) {
3705 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003706 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07003707 camera_capture_request_t* halRequest = &nextRequest.halRequest;
3708 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003709
3710 // Prepare a request to HAL
3711 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3712
3713 // Insert any queued triggers (before metadata is locked)
3714 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003715 if (res < 0) {
3716 SET_ERR("RequestThread: Unable to insert triggers "
3717 "(capture request %d, HAL device: %s (%d)",
3718 halRequest->frame_number, strerror(-res), res);
3719 return INVALID_OPERATION;
3720 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003721
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003722 int triggerCount = res;
3723 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3724 mPrevTriggers = triggerCount;
3725
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003726 bool testPatternChanged = overrideTestPattern(captureRequest);
Shuzhen Wangaf22e912023-04-11 16:03:17 -07003727 bool settingsOverrideChanged = overrideSettingsOverride(captureRequest);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003728
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003729 // If the request is the same as last, or we had triggers now or last time or
3730 // changing overrides this time
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003731 bool newRequest =
Emilian Peevd865f0d2023-03-17 17:13:07 -07003732 (mPrevRequest != captureRequest || triggersMixedIn ||
3733 captureRequest->mRotateAndCropChanged ||
3734 captureRequest->mAutoframingChanged ||
Shuzhen Wangaf22e912023-04-11 16:03:17 -07003735 testPatternChanged || settingsOverrideChanged) &&
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003736 // Request settings are all the same within one batch, so only treat the first
3737 // request in a batch as new
Zhijun He54c36822018-07-18 09:33:39 -07003738 !(batchedRequest && i > 0);
Emilian Peev00420d22018-02-05 21:33:13 +00003739 if (newRequest) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003740 std::set<std::string> cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003741 /**
3742 * HAL workaround:
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003743 * Insert a fake trigger ID if a trigger is set but no trigger ID is
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003744 */
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003745 res = addFakeTriggerIds(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003746 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003747 SET_ERR("RequestThread: Unable to insert fake trigger IDs "
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003748 "(capture request %d, HAL device: %s (%d)",
3749 halRequest->frame_number, strerror(-res), res);
3750 return INVALID_OPERATION;
3751 }
3752
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003753 {
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003754 sp<Camera3Device> parent = mParent.promote();
3755 if (parent != nullptr) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003756 List<PhysicalCameraSettings>::iterator it;
3757 for (it = captureRequest->mSettingsList.begin();
3758 it != captureRequest->mSettingsList.end(); it++) {
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003759 if (parent->mUHRCropAndMeteringRegionMappers.find(it->cameraId) ==
3760 parent->mUHRCropAndMeteringRegionMappers.end()) {
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003761 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3762 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3763 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3764 res);
3765 return INVALID_OPERATION;
3766 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003767 continue;
3768 }
3769
3770 if (!captureRequest->mUHRCropAndMeteringRegionsUpdated) {
3771 res = parent->mUHRCropAndMeteringRegionMappers[it->cameraId].
3772 updateCaptureRequest(&(it->metadata));
3773 if (res != OK) {
3774 SET_ERR("RequestThread: Unable to correct capture requests "
3775 "for scaler crop region and metering regions for request "
3776 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3777 res);
3778 return INVALID_OPERATION;
3779 }
3780 captureRequest->mUHRCropAndMeteringRegionsUpdated = true;
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003781 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3782 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3783 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3784 res);
3785 return INVALID_OPERATION;
3786 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003787 }
3788 }
3789
3790 // Correct metadata regions for distortion correction if enabled
3791 for (it = captureRequest->mSettingsList.begin();
3792 it != captureRequest->mSettingsList.end(); it++) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003793 if (parent->mDistortionMappers.find(it->cameraId) ==
3794 parent->mDistortionMappers.end()) {
3795 continue;
3796 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003797
3798 if (!captureRequest->mDistortionCorrectionUpdated) {
3799 res = parent->mDistortionMappers[it->cameraId].correctCaptureRequest(
3800 &(it->metadata));
3801 if (res != OK) {
3802 SET_ERR("RequestThread: Unable to correct capture requests "
3803 "for lens distortion for request %d: %s (%d)",
3804 halRequest->frame_number, strerror(-res), res);
3805 return INVALID_OPERATION;
3806 }
3807 captureRequest->mDistortionCorrectionUpdated = true;
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003808 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003809 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003810
3811 for (it = captureRequest->mSettingsList.begin();
3812 it != captureRequest->mSettingsList.end(); it++) {
3813 if (parent->mZoomRatioMappers.find(it->cameraId) ==
3814 parent->mZoomRatioMappers.end()) {
3815 continue;
3816 }
3817
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003818 if (!captureRequest->mZoomRatioIs1x) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003819 cameraIdsWithZoom.insert(it->cameraId);
3820 }
3821
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003822 if (!captureRequest->mZoomRatioUpdated) {
3823 res = parent->mZoomRatioMappers[it->cameraId].updateCaptureRequest(
3824 &(it->metadata));
3825 if (res != OK) {
3826 SET_ERR("RequestThread: Unable to correct capture requests "
3827 "for zoom ratio for request %d: %s (%d)",
3828 halRequest->frame_number, strerror(-res), res);
3829 return INVALID_OPERATION;
3830 }
3831 captureRequest->mZoomRatioUpdated = true;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003832 }
3833 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003834 if (captureRequest->mRotateAndCropAuto &&
3835 !captureRequest->mRotationAndCropUpdated) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003836 for (it = captureRequest->mSettingsList.begin();
3837 it != captureRequest->mSettingsList.end(); it++) {
3838 auto mapper = parent->mRotateAndCropMappers.find(it->cameraId);
3839 if (mapper != parent->mRotateAndCropMappers.end()) {
3840 res = mapper->second.updateCaptureRequest(&(it->metadata));
3841 if (res != OK) {
3842 SET_ERR("RequestThread: Unable to correct capture requests "
3843 "for rotate-and-crop for request %d: %s (%d)",
3844 halRequest->frame_number, strerror(-res), res);
3845 return INVALID_OPERATION;
3846 }
3847 }
3848 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003849 captureRequest->mRotationAndCropUpdated = true;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003850 }
Emilian Peeva1b26262023-02-06 17:27:41 -08003851
3852 for (it = captureRequest->mSettingsList.begin();
3853 it != captureRequest->mSettingsList.end(); it++) {
3854 res = hardware::cameraservice::utils::conversion::aidl::filterVndkKeys(
3855 mVndkVersion, it->metadata, false /*isStatic*/);
3856 if (res != OK) {
3857 SET_ERR("RequestThread: Failed during VNDK filter of capture requests "
3858 "%d: %s (%d)", halRequest->frame_number, strerror(-res), res);
3859 return INVALID_OPERATION;
3860 }
3861 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003862 }
3863 }
3864
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003865 /**
3866 * The request should be presorted so accesses in HAL
3867 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3868 */
Emilian Peevaebbe412018-01-15 13:53:24 +00003869 captureRequest->mSettingsList.begin()->metadata.sort();
3870 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003871 mPrevRequest = captureRequest;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003872 mPrevCameraIdsWithZoom = cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003873 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3874
3875 IF_ALOGV() {
3876 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3877 find_camera_metadata_ro_entry(
3878 halRequest->settings,
3879 ANDROID_CONTROL_AF_TRIGGER,
3880 &e
3881 );
3882 if (e.count > 0) {
3883 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3884 __FUNCTION__,
3885 halRequest->frame_number,
3886 e.data.u8[0]);
3887 }
3888 }
3889 } else {
3890 // leave request.settings NULL to indicate 'reuse latest given'
3891 ALOGVV("%s: Request settings are REUSED",
3892 __FUNCTION__);
3893 }
3894
Emilian Peevaebbe412018-01-15 13:53:24 +00003895 if (captureRequest->mSettingsList.size() > 1) {
3896 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
3897 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00003898 if (newRequest) {
3899 halRequest->physcam_settings =
3900 new const camera_metadata* [halRequest->num_physcam_settings];
3901 } else {
3902 halRequest->physcam_settings = nullptr;
3903 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003904 auto it = ++captureRequest->mSettingsList.begin();
3905 size_t i = 0;
3906 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
3907 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00003908 if (newRequest) {
3909 it->metadata.sort();
3910 halRequest->physcam_settings[i] = it->metadata.getAndLock();
3911 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003912 }
3913 }
3914
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003915 uint32_t totalNumBuffers = 0;
3916
3917 // Fill in buffers
3918 if (captureRequest->mInputStream != NULL) {
3919 halRequest->input_buffer = &captureRequest->mInputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003920
3921 halRequest->input_width = captureRequest->mInputBufferSize.width;
3922 halRequest->input_height = captureRequest->mInputBufferSize.height;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003923 totalNumBuffers += 1;
3924 } else {
3925 halRequest->input_buffer = NULL;
3926 }
3927
Emilian Peevf4816702020-04-03 15:44:51 -07003928 outputBuffers->insertAt(camera_stream_buffer_t(), 0,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003929 captureRequest->mOutputStreams.size());
3930 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang99080502021-03-07 21:08:20 -08003931 std::set<std::set<String8>> requestedPhysicalCameras;
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07003932
3933 sp<Camera3Device> parent = mParent.promote();
3934 if (parent == NULL) {
3935 // Should not happen, and nowhere to send errors to, so just log it
3936 CLOGE("RequestThread: Parent is gone");
3937 return INVALID_OPERATION;
3938 }
3939 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
3940
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003941 SurfaceMap uniqueSurfaceIdMap;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003942 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003943 sp<Camera3OutputStreamInterface> outputStream =
3944 captureRequest->mOutputStreams.editItemAt(j);
3945 int streamId = outputStream->getId();
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003946
3947 // Prepare video buffers for high speed recording on the first video request.
3948 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3949 // Only try to prepare video stream on the first video request.
3950 mPrepareVideoStream = false;
3951
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07003952 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
3953 false /*blockRequest*/);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003954 while (res == NOT_ENOUGH_DATA) {
3955 res = outputStream->prepareNextBuffer();
3956 }
3957 if (res != OK) {
3958 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3959 __FUNCTION__, strerror(-res), res);
3960 outputStream->cancelPrepare();
3961 }
3962 }
3963
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003964 std::vector<size_t> uniqueSurfaceIds;
3965 res = outputStream->getUniqueSurfaceIds(
3966 captureRequest->mOutputSurfaces[streamId],
3967 &uniqueSurfaceIds);
3968 // INVALID_OPERATION is normal output for streams not supporting surfaceIds
3969 if (res != OK && res != INVALID_OPERATION) {
3970 ALOGE("%s: failed to query stream %d unique surface IDs",
3971 __FUNCTION__, streamId);
3972 return res;
3973 }
3974 if (res == OK) {
3975 uniqueSurfaceIdMap.insert({streamId, std::move(uniqueSurfaceIds)});
3976 }
3977
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003978 if (mUseHalBufManager) {
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003979 if (outputStream->isAbandoned()) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003980 ALOGV("%s: stream %d is abandoned, skipping request", __FUNCTION__, streamId);
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003981 return TIMED_OUT;
3982 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003983 // HAL will request buffer through requestStreamBuffer API
Emilian Peevf4816702020-04-03 15:44:51 -07003984 camera_stream_buffer_t& buffer = outputBuffers->editItemAt(j);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003985 buffer.stream = outputStream->asHalStream();
3986 buffer.buffer = nullptr;
Emilian Peevf4816702020-04-03 15:44:51 -07003987 buffer.status = CAMERA_BUFFER_STATUS_OK;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003988 buffer.acquire_fence = -1;
3989 buffer.release_fence = -1;
Emilian Peevf0348ae2021-01-13 13:39:45 -08003990 // Mark the output stream as unpreparable to block clients from calling
3991 // 'prepare' after this request reaches CameraHal and before the respective
3992 // buffers are requested.
3993 outputStream->markUnpreparable();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003994 } else {
3995 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
3996 waitDuration,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003997 captureRequest->mOutputSurfaces[streamId]);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003998 if (res != OK) {
3999 // Can't get output buffer from gralloc queue - this could be due to
4000 // abandoned queue or other consumer misbehavior, so not a fatal
4001 // error
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07004002 ALOGV("RequestThread: Can't get output buffer, skipping request:"
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004003 " %s (%d)", strerror(-res), res);
4004
4005 return TIMED_OUT;
4006 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004007 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08004008
4009 {
4010 sp<Camera3Device> parent = mParent.promote();
4011 if (parent != nullptr) {
4012 const String8& streamCameraId = outputStream->getPhysicalCameraId();
Emilian Peevdf6d0502023-07-26 22:39:33 +00004013 // Consider the case where clients are sending a single logical camera request
4014 // to physical output/outputs
4015 bool singleRequest = captureRequest->mSettingsList.size() == 1;
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08004016 for (const auto& settings : captureRequest->mSettingsList) {
Emilian Peevdf6d0502023-07-26 22:39:33 +00004017 if (((streamCameraId.isEmpty() || singleRequest) &&
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08004018 parent->getId() == settings.cameraId.c_str()) ||
4019 streamCameraId == settings.cameraId.c_str()) {
4020 outputStream->fireBufferRequestForFrameNumber(
4021 captureRequest->mResultExtras.frameNumber,
4022 settings.metadata);
4023 }
4024 }
4025 }
4026 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07004027
Shuzhen Wang5c22c152017-12-31 17:12:25 -08004028 String8 physicalCameraId = outputStream->getPhysicalCameraId();
Shuzhen Wang99080502021-03-07 21:08:20 -08004029 int32_t streamGroupId = outputStream->getHalStreamGroupId();
4030 if (streamGroupId != -1 && mGroupIdPhysicalCameraMap.count(streamGroupId) == 1) {
4031 requestedPhysicalCameras.insert(mGroupIdPhysicalCameraMap[streamGroupId]);
4032 } else if (!physicalCameraId.isEmpty()) {
4033 requestedPhysicalCameras.insert(std::set<String8>({physicalCameraId}));
Shuzhen Wang5c22c152017-12-31 17:12:25 -08004034 }
4035 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004036 }
4037 totalNumBuffers += halRequest->num_output_buffers;
4038
4039 // Log request in the in-flight queue
Shuzhen Wang4a472662017-02-26 23:29:04 -08004040 // If this request list is for constrained high speed recording (not
4041 // preview), and the current request is not the last one in the batch,
4042 // do not send callback to the app.
4043 bool hasCallback = true;
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07004044 if (batchedRequest && i != mNextRequests.size()-1) {
Shuzhen Wang4a472662017-02-26 23:29:04 -08004045 hasCallback = false;
4046 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01004047 bool isStillCapture = false;
Shuzhen Wang26abaf42018-08-28 15:41:20 -07004048 bool isZslCapture = false;
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004049 const camera_metadata_t* settings = halRequest->settings;
4050 bool shouldUnlockSettings = false;
4051 if (settings == nullptr) {
4052 shouldUnlockSettings = true;
4053 settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
4054 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01004055 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
4056 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004057 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
Emilian Peev9dd21f42018-08-03 13:39:29 +01004058 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
4059 isStillCapture = true;
4060 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
4061 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07004062
Emilian Peevaf8416e2021-02-04 17:52:43 -08004063 e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004064 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
Shuzhen Wang26abaf42018-08-28 15:41:20 -07004065 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
4066 isZslCapture = true;
4067 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01004068 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07004069 auto expectedDurationInfo = calculateExpectedDurationRange(settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004070 res = parent->registerInFlight(halRequest->frame_number,
4071 totalNumBuffers, captureRequest->mResultExtras,
4072 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004073 hasCallback,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07004074 expectedDurationInfo.minDuration,
4075 expectedDurationInfo.maxDuration,
4076 expectedDurationInfo.isFixedFps,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004077 requestedPhysicalCameras, isStillCapture, isZslCapture,
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004078 captureRequest->mRotateAndCropAuto, captureRequest->mAutoframingAuto,
4079 mPrevCameraIdsWithZoom,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004080 (mUseHalBufManager) ? uniqueSurfaceIdMap :
Shuzhen Wang316781a2020-08-18 18:11:01 -07004081 SurfaceMap{}, captureRequest->mRequestTimeNs);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004082 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
4083 ", burstId = %" PRId32 ".",
4084 __FUNCTION__,
4085 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
4086 captureRequest->mResultExtras.burstId);
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004087
4088 if (shouldUnlockSettings) {
4089 captureRequest->mSettingsList.begin()->metadata.unlock(settings);
4090 }
4091
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004092 if (res != OK) {
4093 SET_ERR("RequestThread: Unable to register new in-flight request:"
4094 " %s (%d)", strerror(-res), res);
4095 return INVALID_OPERATION;
4096 }
4097 }
4098
4099 return OK;
4100}
4101
Igor Murashkin1e479c02013-09-06 16:55:14 -07004102CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004103 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07004104 Mutex::Autolock al(mLatestRequestMutex);
4105
4106 ALOGV("RequestThread::%s", __FUNCTION__);
4107
4108 return mLatestRequest;
4109}
4110
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004111bool Camera3Device::RequestThread::isStreamPending(
4112 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004113 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004114 Mutex::Autolock l(mRequestLock);
4115
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004116 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004117 if (!nextRequest.submitted) {
4118 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
4119 if (stream == s) return true;
4120 }
4121 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004122 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004123 }
4124
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004125 for (const auto& request : mRequestQueue) {
4126 for (const auto& s : request->mOutputStreams) {
4127 if (stream == s) return true;
4128 }
4129 if (stream == request->mInputStream) return true;
4130 }
4131
4132 for (const auto& request : mRepeatingRequests) {
4133 for (const auto& s : request->mOutputStreams) {
4134 if (stream == s) return true;
4135 }
4136 if (stream == request->mInputStream) return true;
4137 }
4138
4139 return false;
4140}
Jianing Weicb0652e2014-03-12 18:29:36 -07004141
Emilian Peev40ead602017-09-26 15:46:36 +01004142bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
4143 ATRACE_CALL();
4144 Mutex::Autolock l(mRequestLock);
4145
4146 for (const auto& nextRequest : mNextRequests) {
4147 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
4148 if (s.first == streamId) {
4149 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4150 if (it != s.second.end()) {
4151 return true;
4152 }
4153 }
4154 }
4155 }
4156
4157 for (const auto& request : mRequestQueue) {
4158 for (const auto& s : request->mOutputSurfaces) {
4159 if (s.first == streamId) {
4160 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4161 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004162 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004163 }
4164 }
4165 }
4166 }
4167
4168 for (const auto& request : mRepeatingRequests) {
4169 for (const auto& s : request->mOutputSurfaces) {
4170 if (s.first == streamId) {
4171 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4172 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004173 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004174 }
4175 }
4176 }
4177 }
4178
4179 return false;
4180}
4181
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004182void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
4183 if (!mUseHalBufManager) {
4184 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
4185 return;
4186 }
4187
4188 Mutex::Autolock pl(mPauseLock);
4189 if (mPaused) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004190 mInterface->signalPipelineDrain(streamIds);
4191 return;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004192 }
4193 // If request thread is still busy, wait until paused then notify HAL
4194 mNotifyPipelineDrain = true;
4195 mStreamIdsToBeDrained = streamIds;
4196}
4197
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07004198void Camera3Device::RequestThread::resetPipelineDrain() {
4199 Mutex::Autolock pl(mPauseLock);
4200 mNotifyPipelineDrain = false;
4201 mStreamIdsToBeDrained.clear();
4202}
4203
Emilian Peevc0fe54c2020-03-11 14:05:07 -07004204void Camera3Device::RequestThread::clearPreviousRequest() {
4205 Mutex::Autolock l(mRequestLock);
4206 mPrevRequest.clear();
4207}
4208
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004209status_t Camera3Device::RequestThread::setRotateAndCropAutoBehavior(
4210 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
4211 ATRACE_CALL();
4212 Mutex::Autolock l(mTriggerMutex);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004213 mRotateAndCropOverride = rotateAndCropValue;
4214 return OK;
4215}
4216
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004217status_t Camera3Device::RequestThread::setAutoframingAutoBehaviour(
4218 camera_metadata_enum_android_control_autoframing_t autoframingValue) {
4219 ATRACE_CALL();
4220 Mutex::Autolock l(mTriggerMutex);
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004221 mAutoframingOverride = autoframingValue;
4222 return OK;
4223}
4224
Emilian Peeve23f1d92021-09-20 14:56:01 -07004225status_t Camera3Device::RequestThread::setComposerSurface(bool composerSurfacePresent) {
4226 ATRACE_CALL();
4227 Mutex::Autolock l(mTriggerMutex);
4228 mComposerOutput = composerSurfacePresent;
4229 return OK;
4230}
4231
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004232status_t Camera3Device::RequestThread::setCameraMute(int32_t muteMode) {
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004233 ATRACE_CALL();
4234 Mutex::Autolock l(mTriggerMutex);
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004235 if (muteMode != mCameraMute) {
4236 mCameraMute = muteMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004237 mCameraMuteChanged = true;
4238 }
4239 return OK;
4240}
4241
Shuzhen Wangaf22e912023-04-11 16:03:17 -07004242status_t Camera3Device::RequestThread::setZoomOverride(int32_t zoomOverride) {
4243 ATRACE_CALL();
4244 Mutex::Autolock l(mTriggerMutex);
4245 mSettingsOverride = zoomOverride;
4246 return OK;
4247}
4248
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004249nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004250 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004251 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004252 return mExpectedInflightDuration > kMinInflightDuration ?
4253 mExpectedInflightDuration : kMinInflightDuration;
4254}
4255
Emilian Peevaebbe412018-01-15 13:53:24 +00004256void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
Emilian Peevf4816702020-04-03 15:44:51 -07004257 camera_capture_request_t *halRequest) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004258 if ((request == nullptr) || (halRequest == nullptr)) {
4259 ALOGE("%s: Invalid request!", __FUNCTION__);
4260 return;
4261 }
4262
4263 if (halRequest->num_physcam_settings > 0) {
4264 if (halRequest->physcam_id != nullptr) {
4265 delete [] halRequest->physcam_id;
4266 halRequest->physcam_id = nullptr;
4267 }
4268 if (halRequest->physcam_settings != nullptr) {
4269 auto it = ++(request->mSettingsList.begin());
4270 size_t i = 0;
4271 for (; it != request->mSettingsList.end(); it++, i++) {
4272 it->metadata.unlock(halRequest->physcam_settings[i]);
4273 }
4274 delete [] halRequest->physcam_settings;
4275 halRequest->physcam_settings = nullptr;
4276 }
4277 }
4278}
4279
Ravneetaeb20dc2022-03-30 05:33:03 +00004280status_t Camera3Device::setCameraServiceWatchdog(bool enabled) {
4281 Mutex::Autolock il(mInterfaceLock);
4282 Mutex::Autolock l(mLock);
4283
4284 if (mCameraServiceWatchdog != NULL) {
4285 mCameraServiceWatchdog->setEnabled(enabled);
4286 }
4287
4288 return OK;
4289}
4290
Shuzhen Wang16610a62022-12-15 22:38:07 -08004291void Camera3Device::setStreamUseCaseOverrides(
4292 const std::vector<int64_t>& useCaseOverrides) {
4293 Mutex::Autolock il(mInterfaceLock);
4294 Mutex::Autolock l(mLock);
4295 mStreamUseCaseOverrides = useCaseOverrides;
4296}
4297
4298void Camera3Device::clearStreamUseCaseOverrides() {
4299 Mutex::Autolock il(mInterfaceLock);
4300 Mutex::Autolock l(mLock);
4301 mStreamUseCaseOverrides.clear();
4302}
4303
Shuzhen Wang03fe6232023-02-05 12:41:15 -08004304bool Camera3Device::hasDeviceError() {
4305 Mutex::Autolock il(mInterfaceLock);
4306 Mutex::Autolock l(mLock);
4307 return mStatus == STATUS_ERROR;
4308}
4309
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004310void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4311 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004312 return;
4313 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004314
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004315 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004316 // Skip the ones that have been submitted successfully.
4317 if (nextRequest.submitted) {
4318 continue;
4319 }
4320
4321 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07004322 camera_capture_request_t* halRequest = &nextRequest.halRequest;
4323 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004324
4325 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004326 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004327 }
4328
Emilian Peevaebbe412018-01-15 13:53:24 +00004329 cleanupPhysicalSettings(captureRequest, halRequest);
4330
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004331 if (captureRequest->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07004332 captureRequest->mInputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004333 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4334 }
4335
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004336 // No output buffer can be returned when using HAL buffer manager
4337 if (!mUseHalBufManager) {
4338 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
4339 //Buffers that failed processing could still have
4340 //valid acquire fence.
4341 int acquireFence = (*outputBuffers)[i].acquire_fence;
4342 if (0 <= acquireFence) {
4343 close(acquireFence);
4344 outputBuffers->editItemAt(i).acquire_fence = -1;
4345 }
Emilian Peevf4816702020-04-03 15:44:51 -07004346 outputBuffers->editItemAt(i).status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang90708ea2021-11-04 11:40:49 -07004347 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i],
4348 /*timestamp*/0, /*readoutTimestamp*/0,
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004349 /*timestampIncreasing*/true, std::vector<size_t> (),
4350 captureRequest->mResultExtras.frameNumber);
Emilian Peevc58cf4c2017-05-11 17:23:41 +01004351 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004352 }
4353
4354 if (sendRequestError) {
4355 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004356 sp<NotificationListener> listener = mListener.promote();
4357 if (listener != NULL) {
4358 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004359 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004360 captureRequest->mResultExtras);
4361 }
4362 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004363
4364 // Remove yet-to-be submitted inflight request from inflightMap
4365 {
4366 sp<Camera3Device> parent = mParent.promote();
4367 if (parent != NULL) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004368 std::lock_guard<std::mutex> l(parent->mInFlightLock);
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004369 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4370 if (idx >= 0) {
4371 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4372 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4373 parent->removeInFlightMapEntryLocked(idx);
4374 }
4375 }
4376 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004377 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004378
4379 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004380 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004381}
4382
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004383void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004384 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004385 // Optimized a bit for the simple steady-state case (single repeating
4386 // request), to avoid putting that request in the queue temporarily.
4387 Mutex::Autolock l(mRequestLock);
4388
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004389 assert(mNextRequests.empty());
4390
4391 NextRequest nextRequest;
4392 nextRequest.captureRequest = waitForNextRequestLocked();
4393 if (nextRequest.captureRequest == nullptr) {
4394 return;
4395 }
4396
Emilian Peevf4816702020-04-03 15:44:51 -07004397 nextRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004398 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004399 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004400
4401 // Wait for additional requests
4402 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4403
4404 for (size_t i = 1; i < batchSize; i++) {
4405 NextRequest additionalRequest;
4406 additionalRequest.captureRequest = waitForNextRequestLocked();
4407 if (additionalRequest.captureRequest == nullptr) {
4408 break;
4409 }
4410
Emilian Peevf4816702020-04-03 15:44:51 -07004411 additionalRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004412 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004413 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004414 }
4415
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004416 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004417 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004418 mNextRequests.size(), batchSize);
4419 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004420 }
4421
4422 return;
4423}
4424
Jayant Chowdharya93f3462022-11-01 23:32:45 +00004425void Camera3Device::RequestThread::setRequestClearing() {
4426 Mutex::Autolock l(mRequestLock);
4427 mRequestClearing = true;
4428}
4429
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004430sp<Camera3Device::CaptureRequest>
4431 Camera3Device::RequestThread::waitForNextRequestLocked() {
4432 status_t res;
4433 sp<CaptureRequest> nextRequest;
4434
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004435 while (mRequestQueue.empty()) {
4436 if (!mRepeatingRequests.empty()) {
4437 // Always atomically enqueue all requests in a repeating request
4438 // list. Guarantees a complete in-sequence set of captures to
4439 // application.
4440 const RequestList &requests = mRepeatingRequests;
Shuzhen Wang316781a2020-08-18 18:11:01 -07004441 if (mFirstRepeating) {
4442 mFirstRepeating = false;
4443 } else {
4444 for (auto& request : requests) {
4445 // For repeating requests, override timestamp request using
4446 // the time a request is inserted into the request queue,
4447 // because the original repeating request will have an old
4448 // fixed timestamp.
4449 request->mRequestTimeNs = systemTime();
4450 }
4451 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004452 RequestList::const_iterator firstRequest =
4453 requests.begin();
4454 nextRequest = *firstRequest;
4455 mRequestQueue.insert(mRequestQueue.end(),
4456 ++firstRequest,
4457 requests.end());
4458 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004459
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004460 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004461
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004462 break;
4463 }
4464
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07004465 if (!mRequestClearing) {
4466 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4467 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004468
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004469 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4470 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004471 Mutex::Autolock pl(mPauseLock);
4472 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004473 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004474 mPaused = true;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004475 if (mNotifyPipelineDrain) {
4476 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4477 mNotifyPipelineDrain = false;
4478 mStreamIdsToBeDrained.clear();
4479 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004480 // Let the tracker know
4481 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4482 if (statusTracker != 0) {
4483 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4484 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004485 sp<Camera3Device> parent = mParent.promote();
4486 if (parent != nullptr) {
4487 parent->mRequestBufferSM.onRequestThreadPaused();
4488 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004489 }
Shuzhen Wangb8696c02022-05-20 13:31:02 -07004490 mRequestClearing = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004491 // Stop waiting for now and let thread management happen
4492 return NULL;
4493 }
4494 }
4495
4496 if (nextRequest == NULL) {
4497 // Don't have a repeating request already in hand, so queue
4498 // must have an entry now.
4499 RequestList::iterator firstRequest =
4500 mRequestQueue.begin();
4501 nextRequest = *firstRequest;
4502 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004503 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4504 sp<NotificationListener> listener = mListener.promote();
4505 if (listener != NULL) {
4506 listener->notifyRequestQueueEmpty();
4507 }
4508 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004509 }
4510
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004511 // In case we've been unpaused by setPaused clearing mDoPause, need to
4512 // update internal pause state (capture/setRepeatingRequest unpause
4513 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004514 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004515 if (mPaused) {
4516 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4517 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4518 if (statusTracker != 0) {
4519 statusTracker->markComponentActive(mStatusId);
4520 }
4521 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004522 mPaused = false;
4523
4524 // Check if we've reconfigured since last time, and reset the preview
4525 // request if so. Can't use 'NULL request == repeat' across configure calls.
4526 if (mReconfigured) {
4527 mPrevRequest.clear();
4528 mReconfigured = false;
4529 }
4530
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004531 if (nextRequest != NULL) {
4532 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004533 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4534 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004535
4536 // Since RequestThread::clear() removes buffers from the input stream,
4537 // get the right buffer here before unlocking mRequestLock
4538 if (nextRequest->mInputStream != NULL) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08004539 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer,
4540 &nextRequest->mInputBufferSize);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004541 if (res != OK) {
4542 // Can't get input buffer from gralloc queue - this could be due to
4543 // disconnected queue or other producer misbehavior, so not a fatal
4544 // error
4545 ALOGE("%s: Can't get input buffer, skipping request:"
4546 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004547
4548 sp<NotificationListener> listener = mListener.promote();
4549 if (listener != NULL) {
4550 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004551 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004552 nextRequest->mResultExtras);
4553 }
4554 return NULL;
4555 }
4556 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004557 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07004558
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004559 return nextRequest;
4560}
4561
4562bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004563 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004564 status_t res;
4565 Mutex::Autolock l(mPauseLock);
4566 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004567 if (mPaused == false) {
4568 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004569 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004570 if (mNotifyPipelineDrain) {
4571 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4572 mNotifyPipelineDrain = false;
4573 mStreamIdsToBeDrained.clear();
4574 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004575 // Let the tracker know
4576 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4577 if (statusTracker != 0) {
4578 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4579 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004580 sp<Camera3Device> parent = mParent.promote();
4581 if (parent != nullptr) {
4582 parent->mRequestBufferSM.onRequestThreadPaused();
4583 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004584 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004585
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004586 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004587 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004588 return true;
4589 }
4590 }
4591 // We don't set mPaused to false here, because waitForNextRequest needs
4592 // to further manage the paused state in case of starvation.
4593 return false;
4594}
4595
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004596void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004597 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004598 // With work to do, mark thread as unpaused.
4599 // If paused by request (setPaused), don't resume, to avoid
4600 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004601 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004602 Mutex::Autolock p(mPauseLock);
4603 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004604 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4605 if (mPaused) {
4606 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4607 if (statusTracker != 0) {
4608 statusTracker->markComponentActive(mStatusId);
4609 }
4610 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004611 mPaused = false;
4612 }
4613}
4614
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07004615void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4616 sp<Camera3Device> parent = mParent.promote();
4617 if (parent != NULL) {
4618 va_list args;
4619 va_start(args, fmt);
4620
4621 parent->setErrorStateV(fmt, args);
4622
4623 va_end(args);
4624 }
4625}
4626
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004627status_t Camera3Device::RequestThread::insertTriggers(
4628 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004629 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004630 Mutex::Autolock al(mTriggerMutex);
4631
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004632 sp<Camera3Device> parent = mParent.promote();
4633 if (parent == NULL) {
4634 CLOGE("RequestThread: Parent is gone");
4635 return DEAD_OBJECT;
4636 }
4637
Emilian Peevaebbe412018-01-15 13:53:24 +00004638 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004639 size_t count = mTriggerMap.size();
4640
4641 for (size_t i = 0; i < count; ++i) {
4642 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004643 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004644
4645 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4646 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4647 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004648 if (isAeTrigger) {
4649 request->mResultExtras.precaptureTriggerId = triggerId;
4650 mCurrentPreCaptureTriggerId = triggerId;
4651 } else {
4652 request->mResultExtras.afTriggerId = triggerId;
4653 mCurrentAfTriggerId = triggerId;
4654 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01004655 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004656 }
4657
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004658 camera_metadata_entry entry = metadata.find(tag);
4659
4660 if (entry.count > 0) {
4661 /**
4662 * Already has an entry for this trigger in the request.
4663 * Rewrite it with our requested trigger value.
4664 */
4665 RequestTrigger oldTrigger = trigger;
4666
4667 oldTrigger.entryValue = entry.data.u8[0];
4668
4669 mTriggerReplacedMap.add(tag, oldTrigger);
4670 } else {
4671 /**
4672 * More typical, no trigger entry, so we just add it
4673 */
4674 mTriggerRemovedMap.add(tag, trigger);
4675 }
4676
4677 status_t res;
4678
4679 switch (trigger.getTagType()) {
4680 case TYPE_BYTE: {
4681 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4682 res = metadata.update(tag,
4683 &entryValue,
4684 /*count*/1);
4685 break;
4686 }
4687 case TYPE_INT32:
4688 res = metadata.update(tag,
4689 &trigger.entryValue,
4690 /*count*/1);
4691 break;
4692 default:
4693 ALOGE("%s: Type not supported: 0x%x",
4694 __FUNCTION__,
4695 trigger.getTagType());
4696 return INVALID_OPERATION;
4697 }
4698
4699 if (res != OK) {
4700 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4701 ", value %d", __FUNCTION__, trigger.getTagName(),
4702 trigger.entryValue);
4703 return res;
4704 }
4705
4706 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4707 trigger.getTagName(),
4708 trigger.entryValue);
4709 }
4710
4711 mTriggerMap.clear();
4712
4713 return count;
4714}
4715
4716status_t Camera3Device::RequestThread::removeTriggers(
4717 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004718 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004719 Mutex::Autolock al(mTriggerMutex);
4720
Emilian Peevaebbe412018-01-15 13:53:24 +00004721 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004722
4723 /**
4724 * Replace all old entries with their old values.
4725 */
4726 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4727 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4728
4729 status_t res;
4730
4731 uint32_t tag = trigger.metadataTag;
4732 switch (trigger.getTagType()) {
4733 case TYPE_BYTE: {
4734 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4735 res = metadata.update(tag,
4736 &entryValue,
4737 /*count*/1);
4738 break;
4739 }
4740 case TYPE_INT32:
4741 res = metadata.update(tag,
4742 &trigger.entryValue,
4743 /*count*/1);
4744 break;
4745 default:
4746 ALOGE("%s: Type not supported: 0x%x",
4747 __FUNCTION__,
4748 trigger.getTagType());
4749 return INVALID_OPERATION;
4750 }
4751
4752 if (res != OK) {
4753 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4754 ", trigger value %d", __FUNCTION__,
4755 trigger.getTagName(), trigger.entryValue);
4756 return res;
4757 }
4758 }
4759 mTriggerReplacedMap.clear();
4760
4761 /**
4762 * Remove all new entries.
4763 */
4764 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4765 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4766 status_t res = metadata.erase(trigger.metadataTag);
4767
4768 if (res != OK) {
4769 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4770 ", trigger value %d", __FUNCTION__,
4771 trigger.getTagName(), trigger.entryValue);
4772 return res;
4773 }
4774 }
4775 mTriggerRemovedMap.clear();
4776
4777 return OK;
4778}
4779
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004780status_t Camera3Device::RequestThread::addFakeTriggerIds(
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004781 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004782 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004783 static const int32_t fakeTriggerId = 1;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004784 status_t res;
4785
Emilian Peevaebbe412018-01-15 13:53:24 +00004786 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004787
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004788 // If AF trigger is active, insert a fake AF trigger ID if none already
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004789 // exists
4790 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4791 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4792 if (afTrigger.count > 0 &&
4793 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4794 afId.count == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004795 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004796 if (res != OK) return res;
4797 }
4798
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004799 // If AE precapture trigger is active, insert a fake precapture trigger ID
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004800 // if none already exists
4801 camera_metadata_entry pcTrigger =
4802 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4803 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4804 if (pcTrigger.count > 0 &&
4805 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4806 pcId.count == 0) {
4807 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004808 &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004809 if (res != OK) return res;
4810 }
4811
4812 return OK;
4813}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004814
Emilian Peevd865f0d2023-03-17 17:13:07 -07004815bool Camera3Device::RequestThread::overrideAutoRotateAndCrop(const sp<CaptureRequest> &request) {
4816 ATRACE_CALL();
4817 Mutex::Autolock l(mTriggerMutex);
4818 return Camera3Device::overrideAutoRotateAndCrop(request, this->mOverrideToPortrait,
4819 this->mRotateAndCropOverride);
4820}
4821
4822bool Camera3Device::overrideAutoRotateAndCrop(const sp<CaptureRequest> &request,
4823 bool overrideToPortrait,
4824 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropOverride) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004825 ATRACE_CALL();
4826
Emilian Peevd865f0d2023-03-17 17:13:07 -07004827 if (overrideToPortrait) {
4828 uint8_t rotateAndCrop_u8 = rotateAndCropOverride;
Austin Borger18b30a72022-10-27 12:20:29 -07004829 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4830 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP,
4831 &rotateAndCrop_u8, 1);
4832 return true;
4833 }
4834
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004835 if (request->mRotateAndCropAuto) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004836 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4837
4838 auto rotateAndCropEntry = metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
4839 if (rotateAndCropEntry.count > 0) {
Emilian Peevd865f0d2023-03-17 17:13:07 -07004840 if (rotateAndCropEntry.data.u8[0] == rotateAndCropOverride) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004841 return false;
4842 } else {
Emilian Peevd865f0d2023-03-17 17:13:07 -07004843 rotateAndCropEntry.data.u8[0] = rotateAndCropOverride;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004844 return true;
4845 }
4846 } else {
Emilian Peevd865f0d2023-03-17 17:13:07 -07004847 uint8_t rotateAndCrop_u8 = rotateAndCropOverride;
4848 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP, &rotateAndCrop_u8, 1);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004849 return true;
4850 }
4851 }
Emilian Peevd865f0d2023-03-17 17:13:07 -07004852
4853 return false;
4854}
4855
4856bool Camera3Device::overrideAutoframing(const sp<CaptureRequest> &request /*out*/,
4857 camera_metadata_enum_android_control_autoframing_t autoframingOverride) {
4858 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4859 auto autoframingEntry = metadata.find(ANDROID_CONTROL_AUTOFRAMING);
4860 if (autoframingEntry.count > 0) {
4861 if (autoframingEntry.data.u8[0] == autoframingOverride) {
4862 return false;
4863 } else {
4864 autoframingEntry.data.u8[0] = autoframingOverride;
4865 return true;
4866 }
4867 } else {
4868 uint8_t autoframing_u8 = autoframingOverride;
4869 metadata.update(ANDROID_CONTROL_AUTOFRAMING,
4870 &autoframing_u8, 1);
4871 return true;
4872 }
4873
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004874 return false;
4875}
4876
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004877bool Camera3Device::RequestThread::overrideAutoframing(const sp<CaptureRequest> &request) {
4878 ATRACE_CALL();
4879
4880 if (request->mAutoframingAuto) {
4881 Mutex::Autolock l(mTriggerMutex);
Emilian Peevd865f0d2023-03-17 17:13:07 -07004882 return Camera3Device::overrideAutoframing(request, mAutoframingOverride);
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004883 }
Emilian Peevd865f0d2023-03-17 17:13:07 -07004884
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004885 return false;
4886}
4887
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004888bool Camera3Device::RequestThread::overrideTestPattern(
4889 const sp<CaptureRequest> &request) {
4890 ATRACE_CALL();
4891
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07004892 if (!mSupportCameraMute) return false;
Eino-Ville Talvalac2459842021-07-22 16:36:36 -07004893
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004894 Mutex::Autolock l(mTriggerMutex);
4895
4896 bool changed = false;
4897
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004898 // For a multi-camera, the physical cameras support the same set of
4899 // test pattern modes as the logical camera.
4900 for (auto& settings : request->mSettingsList) {
4901 CameraMetadata &metadata = settings.metadata;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004902
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004903 int32_t testPatternMode = settings.mOriginalTestPatternMode;
4904 int32_t testPatternData[4] = {
4905 settings.mOriginalTestPatternData[0],
4906 settings.mOriginalTestPatternData[1],
4907 settings.mOriginalTestPatternData[2],
4908 settings.mOriginalTestPatternData[3]
4909 };
4910 if (mCameraMute != ANDROID_SENSOR_TEST_PATTERN_MODE_OFF) {
4911 testPatternMode = mCameraMute;
4912 testPatternData[0] = 0;
4913 testPatternData[1] = 0;
4914 testPatternData[2] = 0;
4915 testPatternData[3] = 0;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004916 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004917
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004918 auto testPatternEntry = metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
4919 bool supportTestPatternModeKey = settings.mHasTestPatternModeTag;
4920 if (testPatternEntry.count > 0) {
4921 if (testPatternEntry.data.i32[0] != testPatternMode) {
4922 testPatternEntry.data.i32[0] = testPatternMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004923 changed = true;
4924 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004925 } else if (supportTestPatternModeKey) {
4926 metadata.update(ANDROID_SENSOR_TEST_PATTERN_MODE,
4927 &testPatternMode, 1);
4928 changed = true;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004929 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004930
4931 auto testPatternColor = metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
4932 bool supportTestPatternDataKey = settings.mHasTestPatternDataTag;
4933 if (testPatternColor.count >= 4) {
4934 for (size_t i = 0; i < 4; i++) {
4935 if (testPatternColor.data.i32[i] != testPatternData[i]) {
4936 testPatternColor.data.i32[i] = testPatternData[i];
4937 changed = true;
4938 }
4939 }
4940 } else if (supportTestPatternDataKey) {
4941 metadata.update(ANDROID_SENSOR_TEST_PATTERN_DATA,
4942 testPatternData, 4);
4943 changed = true;
4944 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004945 }
4946
4947 return changed;
4948}
4949
Shuzhen Wangaf22e912023-04-11 16:03:17 -07004950bool Camera3Device::RequestThread::overrideSettingsOverride(
4951 const sp<CaptureRequest> &request) {
4952 ATRACE_CALL();
4953
4954 if (!mSupportSettingsOverride) return false;
4955
4956 Mutex::Autolock l(mTriggerMutex);
4957
4958 // For a multi-camera, only override the logical camera's metadata.
4959 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4960 camera_metadata_entry entry = metadata.find(ANDROID_CONTROL_SETTINGS_OVERRIDE);
4961 int32_t originalValue = request->mSettingsList.begin()->mOriginalSettingsOverride;
4962 if (mSettingsOverride != -1 &&
4963 (entry.count == 0 || entry.data.i32[0] != mSettingsOverride)) {
4964 metadata.update(ANDROID_CONTROL_SETTINGS_OVERRIDE,
4965 &mSettingsOverride, 1);
4966 return true;
4967 } else if (mSettingsOverride == -1 &&
4968 (entry.count == 0 || entry.data.i32[0] != originalValue)) {
4969 metadata.update(ANDROID_CONTROL_SETTINGS_OVERRIDE,
4970 &originalValue, 1);
4971 return true;
4972 }
4973
4974 return false;
4975}
4976
Cliff Wuc2ad9c82021-04-21 00:58:58 +08004977status_t Camera3Device::RequestThread::setHalInterface(
4978 sp<HalInterface> newHalInterface) {
4979 if (newHalInterface.get() == nullptr) {
4980 ALOGE("%s: The newHalInterface does not exist!", __FUNCTION__);
4981 return DEAD_OBJECT;
4982 }
4983
4984 mInterface = newHalInterface;
4985
4986 return OK;
4987}
4988
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004989/**
4990 * PreparerThread inner class methods
4991 */
4992
4993Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004994 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004995 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004996}
4997
4998Camera3Device::PreparerThread::~PreparerThread() {
4999 Thread::requestExitAndWait();
5000 if (mCurrentStream != nullptr) {
5001 mCurrentStream->cancelPrepare();
5002 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5003 mCurrentStream.clear();
5004 }
5005 clear();
5006}
5007
Ruben Brunkc78ac262015-08-13 17:58:46 -07005008status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005009 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005010 status_t res;
5011
5012 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005013 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005014
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07005015 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005016 if (res == OK) {
5017 // No preparation needed, fire listener right off
5018 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005019 if (listener != NULL) {
5020 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005021 }
5022 return OK;
5023 } else if (res != NOT_ENOUGH_DATA) {
5024 return res;
5025 }
5026
5027 // Need to prepare, start up thread if necessary
5028 if (!mActive) {
5029 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
5030 // isn't running
5031 Thread::requestExitAndWait();
5032 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5033 if (res != OK) {
5034 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005035 if (listener != NULL) {
5036 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005037 }
5038 return res;
5039 }
5040 mCancelNow = false;
5041 mActive = true;
5042 ALOGV("%s: Preparer stream started", __FUNCTION__);
5043 }
5044
5045 // queue up the work
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005046 mPendingStreams.push_back(
5047 std::tuple<int, sp<camera3::Camera3StreamInterface>>(maxCount, stream));
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005048 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
5049
5050 return OK;
5051}
5052
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005053void Camera3Device::PreparerThread::pause() {
5054 ATRACE_CALL();
5055
5056 Mutex::Autolock l(mLock);
5057
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005058 std::list<std::tuple<int, sp<camera3::Camera3StreamInterface>>> pendingStreams;
5059 pendingStreams.insert(pendingStreams.begin(), mPendingStreams.begin(), mPendingStreams.end());
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005060 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
5061 int currentMaxCount = mCurrentMaxCount;
5062 mPendingStreams.clear();
5063 mCancelNow = true;
5064 while (mActive) {
5065 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
5066 if (res == TIMED_OUT) {
5067 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
5068 return;
5069 } else if (res != OK) {
5070 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
5071 return;
5072 }
5073 }
5074
5075 //Check whether the prepare thread was able to complete the current
5076 //stream. In case work is still pending emplace it along with the rest
5077 //of the streams in the pending list.
5078 if (currentStream != nullptr) {
5079 if (!mCurrentPrepareComplete) {
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005080 pendingStreams.push_back(std::tuple(currentMaxCount, currentStream));
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005081 }
5082 }
5083
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005084 mPendingStreams.insert(mPendingStreams.begin(), pendingStreams.begin(), pendingStreams.end());
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005085 for (const auto& it : mPendingStreams) {
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005086 std::get<1>(it)->cancelPrepare();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005087 }
5088}
5089
5090status_t Camera3Device::PreparerThread::resume() {
5091 ATRACE_CALL();
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005092 ALOGV("%s: PreparerThread", __FUNCTION__);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005093 status_t res;
5094
5095 Mutex::Autolock l(mLock);
5096 sp<NotificationListener> listener = mListener.promote();
5097
5098 if (mActive) {
5099 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
5100 return NO_INIT;
5101 }
5102
5103 auto it = mPendingStreams.begin();
5104 for (; it != mPendingStreams.end();) {
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005105 res = std::get<1>(*it)->startPrepare(std::get<0>(*it), true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005106 if (res == OK) {
5107 if (listener != NULL) {
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005108 listener->notifyPrepared(std::get<1>(*it)->getId());
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005109 }
5110 it = mPendingStreams.erase(it);
5111 } else if (res != NOT_ENOUGH_DATA) {
5112 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
5113 res, strerror(-res));
5114 it = mPendingStreams.erase(it);
5115 } else {
5116 it++;
5117 }
5118 }
5119
5120 if (mPendingStreams.empty()) {
5121 return OK;
5122 }
5123
5124 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5125 if (res != OK) {
5126 ALOGE("%s: Unable to start preparer stream: %d (%s)",
5127 __FUNCTION__, res, strerror(-res));
5128 return res;
5129 }
5130 mCancelNow = false;
5131 mActive = true;
5132 ALOGV("%s: Preparer stream started", __FUNCTION__);
5133
5134 return OK;
5135}
5136
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005137status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005138 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005139 Mutex::Autolock l(mLock);
5140
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005141 for (const auto& it : mPendingStreams) {
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005142 std::get<1>(it)->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005143 }
5144 mPendingStreams.clear();
5145 mCancelNow = true;
5146
5147 return OK;
5148}
5149
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005150void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005151 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005152 Mutex::Autolock l(mLock);
5153 mListener = listener;
5154}
5155
5156bool Camera3Device::PreparerThread::threadLoop() {
5157 status_t res;
5158 {
5159 Mutex::Autolock l(mLock);
5160 if (mCurrentStream == nullptr) {
5161 // End thread if done with work
5162 if (mPendingStreams.empty()) {
5163 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
5164 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
5165 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
5166 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005167 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005168 return false;
5169 }
5170
5171 // Get next stream to prepare
5172 auto it = mPendingStreams.begin();
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005173 mCurrentMaxCount = std::get<0>(*it);
5174 mCurrentStream = std::get<1>(*it);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005175 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005176 mPendingStreams.erase(it);
5177 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
5178 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
5179 } else if (mCancelNow) {
5180 mCurrentStream->cancelPrepare();
5181 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5182 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
5183 mCurrentStream.clear();
5184 mCancelNow = false;
5185 return true;
5186 }
5187 }
5188
5189 res = mCurrentStream->prepareNextBuffer();
5190 if (res == NOT_ENOUGH_DATA) return true;
5191 if (res != OK) {
5192 // Something bad happened; try to recover by cancelling prepare and
5193 // signalling listener anyway
5194 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
5195 mCurrentStream->getId(), res, strerror(-res));
5196 mCurrentStream->cancelPrepare();
5197 }
5198
5199 // This stream has finished, notify listener
5200 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005201 sp<NotificationListener> listener = mListener.promote();
5202 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005203 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
5204 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005205 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005206 }
5207
5208 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5209 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005210 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005211
5212 return true;
5213}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005214
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005215status_t Camera3Device::RequestBufferStateMachine::initialize(
5216 sp<camera3::StatusTracker> statusTracker) {
5217 if (statusTracker == nullptr) {
5218 ALOGE("%s: statusTracker is null", __FUNCTION__);
5219 return BAD_VALUE;
5220 }
5221
5222 std::lock_guard<std::mutex> lock(mLock);
5223 mStatusTracker = statusTracker;
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07005224 mRequestBufferStatusId = statusTracker->addComponent("BufferRequestSM");
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005225 return OK;
5226}
5227
5228bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
5229 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005230 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005231 mRequestBufferOngoing = true;
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005232 notifyTrackerLocked(/*active*/true);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005233 return true;
5234 }
5235 return false;
5236}
5237
5238void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
5239 std::lock_guard<std::mutex> lock(mLock);
5240 if (!mRequestBufferOngoing) {
5241 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
5242 return;
5243 }
5244 mRequestBufferOngoing = false;
5245 if (mStatus == RB_STATUS_PENDING_STOP) {
5246 checkSwitchToStopLocked();
5247 }
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005248 notifyTrackerLocked(/*active*/false);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005249}
5250
5251void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
5252 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005253 mSwitchedToOffline = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005254 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005255 return;
5256}
5257
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005258void Camera3Device::RequestBufferStateMachine::onSubmittingRequest() {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005259 std::lock_guard<std::mutex> lock(mLock);
5260 mRequestThreadPaused = false;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005261 // inflight map register actually happens in prepareHalRequest now, but it is close enough
5262 // approximation.
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005263 mInflightMapEmpty = false;
5264 if (mStatus == RB_STATUS_STOPPED) {
5265 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005266 }
5267 return;
5268}
5269
5270void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
5271 std::lock_guard<std::mutex> lock(mLock);
5272 mRequestThreadPaused = true;
5273 if (mStatus == RB_STATUS_PENDING_STOP) {
5274 checkSwitchToStopLocked();
5275 }
5276 return;
5277}
5278
5279void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
5280 std::lock_guard<std::mutex> lock(mLock);
5281 mInflightMapEmpty = true;
5282 if (mStatus == RB_STATUS_PENDING_STOP) {
5283 checkSwitchToStopLocked();
5284 }
5285 return;
5286}
5287
5288void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
5289 std::lock_guard<std::mutex> lock(mLock);
5290 if (!checkSwitchToStopLocked()) {
5291 mStatus = RB_STATUS_PENDING_STOP;
5292 }
5293 return;
5294}
5295
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005296bool Camera3Device::RequestBufferStateMachine::onSwitchToOfflineSuccess() {
5297 std::lock_guard<std::mutex> lock(mLock);
5298 if (mRequestBufferOngoing) {
5299 ALOGE("%s: HAL must not be requesting buffer after HAL returns switchToOffline!",
5300 __FUNCTION__);
5301 return false;
5302 }
5303 mSwitchedToOffline = true;
5304 mInflightMapEmpty = true;
5305 mRequestThreadPaused = true;
5306 mStatus = RB_STATUS_STOPPED;
5307 return true;
5308}
5309
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005310void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
5311 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5312 if (statusTracker != nullptr) {
5313 if (active) {
5314 statusTracker->markComponentActive(mRequestBufferStatusId);
5315 } else {
5316 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
5317 }
5318 }
5319}
5320
5321bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
5322 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
5323 mStatus = RB_STATUS_STOPPED;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005324 return true;
5325 }
5326 return false;
5327}
5328
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005329bool Camera3Device::startRequestBuffer() {
5330 return mRequestBufferSM.startRequestBuffer();
5331}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005332
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005333void Camera3Device::endRequestBuffer() {
5334 mRequestBufferSM.endRequestBuffer();
5335}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005336
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005337nsecs_t Camera3Device::getWaitDuration() {
5338 return kBaseGetBufferWait + getExpectedInFlightDuration();
5339}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005340
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005341void Camera3Device::getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) {
5342 mInterface->getInflightBufferKeys(out);
5343}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005344
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005345void Camera3Device::getInflightRequestBufferKeys(std::vector<uint64_t>* out) {
5346 mInterface->getInflightRequestBufferKeys(out);
5347}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005348
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005349std::vector<sp<Camera3StreamInterface>> Camera3Device::getAllStreams() {
5350 std::vector<sp<Camera3StreamInterface>> ret;
5351 bool hasInputStream = mInputStream != nullptr;
5352 ret.reserve(mOutputStreams.size() + mDeletedStreams.size() + ((hasInputStream) ? 1 : 0));
5353 if (hasInputStream) {
5354 ret.push_back(mInputStream);
Shuzhen Wang268a1362018-10-16 16:32:59 -07005355 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005356 for (size_t i = 0; i < mOutputStreams.size(); i++) {
5357 ret.push_back(mOutputStreams[i]);
5358 }
5359 for (size_t i = 0; i < mDeletedStreams.size(); i++) {
5360 ret.push_back(mDeletedStreams[i]);
5361 }
5362 return ret;
Shuzhen Wang268a1362018-10-16 16:32:59 -07005363}
5364
Emilian Peevcc0b7952020-01-07 13:54:47 -08005365void Camera3Device::getOfflineStreamIds(std::vector<int> *offlineStreamIds) {
5366 ATRACE_CALL();
5367
5368 if (offlineStreamIds == nullptr) {
5369 return;
5370 }
5371
5372 Mutex::Autolock il(mInterfaceLock);
5373
5374 auto streamIds = mOutputStreams.getStreamIds();
5375 bool hasInputStream = mInputStream != nullptr;
5376 if (hasInputStream && mInputStream->getOfflineProcessingSupport()) {
5377 offlineStreamIds->push_back(mInputStream->getId());
5378 }
5379
5380 for (const auto & streamId : streamIds) {
5381 sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
5382 // Streams that use the camera buffer manager are currently not supported in
5383 // offline mode
5384 if (stream->getOfflineProcessingSupport() &&
5385 (stream->getStreamSetId() == CAMERA3_STREAM_SET_ID_INVALID)) {
5386 offlineStreamIds->push_back(streamId);
5387 }
5388 }
5389}
5390
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005391status_t Camera3Device::setRotateAndCropAutoBehavior(
5392 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
5393 ATRACE_CALL();
5394 Mutex::Autolock il(mInterfaceLock);
5395 Mutex::Autolock l(mLock);
5396 if (mRequestThread == nullptr) {
5397 return INVALID_OPERATION;
5398 }
Emilian Peevd865f0d2023-03-17 17:13:07 -07005399 if (rotateAndCropValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
5400 return BAD_VALUE;
5401 }
5402 mRotateAndCropOverride = rotateAndCropValue;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005403 return mRequestThread->setRotateAndCropAutoBehavior(rotateAndCropValue);
5404}
5405
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00005406status_t Camera3Device::setAutoframingAutoBehavior(
5407 camera_metadata_enum_android_control_autoframing_t autoframingValue) {
5408 ATRACE_CALL();
5409 Mutex::Autolock il(mInterfaceLock);
5410 Mutex::Autolock l(mLock);
5411 if (mRequestThread == nullptr) {
5412 return INVALID_OPERATION;
5413 }
Emilian Peevd865f0d2023-03-17 17:13:07 -07005414 if (autoframingValue == ANDROID_CONTROL_AUTOFRAMING_AUTO) {
5415 return BAD_VALUE;
5416 }
5417 mAutoframingOverride = autoframingValue;
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00005418 return mRequestThread->setAutoframingAutoBehaviour(autoframingValue);
5419}
5420
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005421bool Camera3Device::supportsCameraMute() {
5422 Mutex::Autolock il(mInterfaceLock);
5423 Mutex::Autolock l(mLock);
5424
5425 return mSupportCameraMute;
5426}
5427
5428status_t Camera3Device::setCameraMute(bool enabled) {
5429 ATRACE_CALL();
5430 Mutex::Autolock il(mInterfaceLock);
5431 Mutex::Autolock l(mLock);
5432
5433 if (mRequestThread == nullptr || !mSupportCameraMute) {
5434 return INVALID_OPERATION;
5435 }
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07005436 int32_t muteMode =
5437 !enabled ? ANDROID_SENSOR_TEST_PATTERN_MODE_OFF :
5438 mSupportTestPatternSolidColor ? ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR :
5439 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK;
5440 return mRequestThread->setCameraMute(muteMode);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005441}
5442
Shuzhen Wangaf22e912023-04-11 16:03:17 -07005443bool Camera3Device::supportsZoomOverride() {
5444 Mutex::Autolock il(mInterfaceLock);
5445 Mutex::Autolock l(mLock);
5446
5447 return mSupportZoomOverride;
5448}
5449
5450status_t Camera3Device::setZoomOverride(int32_t zoomOverride) {
5451 ATRACE_CALL();
5452 Mutex::Autolock il(mInterfaceLock);
5453 Mutex::Autolock l(mLock);
5454
5455 if (mRequestThread == nullptr || !mSupportZoomOverride) {
5456 return INVALID_OPERATION;
5457 }
5458
5459 return mRequestThread->setZoomOverride(zoomOverride);
5460}
5461
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005462status_t Camera3Device::injectCamera(const String8& injectedCamId,
5463 sp<CameraProviderManager> manager) {
5464 ALOGI("%s Injection camera: injectedCamId = %s", __FUNCTION__, injectedCamId.string());
5465 ATRACE_CALL();
5466 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005467 // When the camera device is active, injectCamera() and stopInjection() will call
5468 // internalPauseAndWaitLocked() and internalResumeLocked(), and then they will call
5469 // mStatusChanged.waitRelative(mLock, timeout) of waitUntilStateThenRelock(). But
5470 // mStatusChanged.waitRelative(mLock, timeout)'s parameter: mutex "mLock" must be in the locked
5471 // state, so we need to add "Mutex::Autolock l(mLock)" to lock the "mLock" before calling
5472 // waitUntilStateThenRelock().
5473 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005474
5475 status_t res = NO_ERROR;
5476 if (mInjectionMethods->isInjecting()) {
5477 if (injectedCamId == mInjectionMethods->getInjectedCamId()) {
5478 return OK;
5479 } else {
5480 res = mInjectionMethods->stopInjection();
5481 if (res != OK) {
5482 ALOGE("%s: Failed to stop the injection camera! ret != NO_ERROR: %d",
5483 __FUNCTION__, res);
5484 return res;
5485 }
5486 }
5487 }
5488
Jayant Chowdhary22441f32021-12-26 18:35:41 -08005489 res = injectionCameraInitialize(injectedCamId, manager);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005490 if (res != OK) {
5491 ALOGE("%s: Failed to initialize the injection camera! ret != NO_ERROR: %d",
5492 __FUNCTION__, res);
5493 return res;
5494 }
5495
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005496 // When the second display of android is cast to the remote device, and the opened camera is
5497 // also cast to the second display, in this case, because the camera has configured the streams
5498 // at this time, we can directly call injectCamera() to replace the internal camera with
5499 // injection camera.
Cliff Wu3b268182021-07-06 15:44:43 +08005500 if (mInjectionMethods->isStreamConfigCompleteButNotInjected()) {
5501 ALOGD("%s: The opened camera is directly cast to the remote device.", __FUNCTION__);
5502
5503 camera3::camera_stream_configuration injectionConfig;
5504 std::vector<uint32_t> injectionBufferSizes;
5505 mInjectionMethods->getInjectionConfig(&injectionConfig, &injectionBufferSizes);
5506 if (mOperatingMode < 0 || injectionConfig.num_streams <= 0
5507 || injectionBufferSizes.size() <= 0) {
5508 ALOGE("Failed to inject camera due to abandoned configuration! "
5509 "mOperatingMode: %d injectionConfig.num_streams: %d "
5510 "injectionBufferSizes.size(): %zu", mOperatingMode,
5511 injectionConfig.num_streams, injectionBufferSizes.size());
5512 return DEAD_OBJECT;
5513 }
5514
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005515 res = mInjectionMethods->injectCamera(
5516 injectionConfig, injectionBufferSizes);
5517 if (res != OK) {
5518 ALOGE("Can't finish inject camera process!");
5519 return res;
5520 }
5521 }
5522
5523 return OK;
5524}
5525
5526status_t Camera3Device::stopInjection() {
5527 ALOGI("%s: Injection camera: stopInjection", __FUNCTION__);
5528 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005529 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005530 return mInjectionMethods->stopInjection();
5531}
5532
Shuzhen Wang16610a62022-12-15 22:38:07 -08005533void Camera3Device::overrideStreamUseCaseLocked() {
5534 if (mStreamUseCaseOverrides.size() == 0) {
5535 return;
5536 }
5537
5538 // Start from an array of indexes in mStreamUseCaseOverrides, and sort them
5539 // based first on size, and second on formats of [JPEG, RAW, YUV, PRIV].
5540 // Refer to CameraService::printHelp for details.
5541 std::vector<int> outputStreamsIndices(mOutputStreams.size());
5542 for (size_t i = 0; i < outputStreamsIndices.size(); i++) {
5543 outputStreamsIndices[i] = i;
5544 }
5545
5546 std::sort(outputStreamsIndices.begin(), outputStreamsIndices.end(),
5547 [&](int a, int b) -> bool {
5548
5549 auto formatScore = [](int format) {
5550 switch (format) {
5551 case HAL_PIXEL_FORMAT_BLOB:
5552 return 4;
5553 case HAL_PIXEL_FORMAT_RAW16:
5554 case HAL_PIXEL_FORMAT_RAW10:
5555 case HAL_PIXEL_FORMAT_RAW12:
5556 return 3;
5557 case HAL_PIXEL_FORMAT_YCBCR_420_888:
5558 return 2;
5559 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
5560 return 1;
5561 default:
5562 return 0;
5563 }
5564 };
5565
5566 int sizeA = mOutputStreams[a]->getWidth() * mOutputStreams[a]->getHeight();
5567 int sizeB = mOutputStreams[a]->getWidth() * mOutputStreams[a]->getHeight();
5568 int formatAScore = formatScore(mOutputStreams[a]->getFormat());
5569 int formatBScore = formatScore(mOutputStreams[b]->getFormat());
5570 if (sizeA > sizeB ||
5571 (sizeA == sizeB && formatAScore >= formatBScore)) {
5572 return true;
5573 } else {
5574 return false;
5575 }
5576 });
5577
5578 size_t overlapSize = std::min(mStreamUseCaseOverrides.size(), mOutputStreams.size());
5579 for (size_t i = 0; i < mOutputStreams.size(); i++) {
5580 mOutputStreams[outputStreamsIndices[i]]->setStreamUseCase(
5581 mStreamUseCaseOverrides[std::min(i, overlapSize-1)]);
5582 }
5583}
5584
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005585}; // namespace android