blob: 9b5f09fda58eef58b3679695bcc8ee8564ebe5c6 [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),
Kwangkyu Park9dd5eda2023-06-20 21:12:06 +09003033 mLatestFailedRequestId(NAME_NOT_FOUND),
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07003034 mCurrentAfTriggerId(0),
3035 mCurrentPreCaptureTriggerId(0),
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003036 mRotateAndCropOverride(ANDROID_SCALER_ROTATE_AND_CROP_NONE),
Bharatt Kukreja0e13db32022-12-07 21:38:45 +00003037 mAutoframingOverride(ANDROID_CONTROL_AUTOFRAMING_OFF),
Emilian Peeve23f1d92021-09-20 14:56:01 -07003038 mComposerOutput(false),
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07003039 mCameraMute(ANDROID_SENSOR_TEST_PATTERN_MODE_OFF),
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003040 mCameraMuteChanged(false),
Shuzhen Wangaf22e912023-04-11 16:03:17 -07003041 mSettingsOverride(ANDROID_CONTROL_SETTINGS_OVERRIDE_OFF),
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003042 mRepeatingLastFrameNumber(
3043 hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES),
Shuzhen Wang686f6442017-06-20 16:16:04 -07003044 mPrepareVideoStream(false),
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003045 mConstrainedMode(false),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003046 mRequestLatency(kRequestLatencyBinSize),
3047 mSessionParamKeys(sessionParamKeys),
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003048 mLatestSessionParams(sessionParamKeys.size()),
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07003049 mUseHalBufManager(useHalBufManager),
Austin Borger18b30a72022-10-27 12:20:29 -07003050 mSupportCameraMute(supportCameraMute),
Shuzhen Wangaf22e912023-04-11 16:03:17 -07003051 mOverrideToPortrait(overrideToPortrait),
3052 mSupportSettingsOverride(supportSettingsOverride) {
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07003053 mStatusId = statusTracker->addComponent("RequestThread");
Emilian Peeva1b26262023-02-06 17:27:41 -08003054 mVndkVersion = property_get_int32("ro.vndk.version", __ANDROID_API_FUTURE__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003055}
3056
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003057Camera3Device::RequestThread::~RequestThread() {}
3058
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07003059void Camera3Device::RequestThread::setNotificationListener(
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003060 wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003061 ATRACE_CALL();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003062 Mutex::Autolock l(mRequestLock);
3063 mListener = listener;
3064}
3065
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003066void Camera3Device::RequestThread::configurationComplete(bool isConstrainedHighSpeed,
Shuzhen Wang99080502021-03-07 21:08:20 -08003067 const CameraMetadata& sessionParams,
3068 const std::map<int32_t, std::set<String8>>& groupIdPhysicalCameraMap) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003069 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003070 Mutex::Autolock l(mRequestLock);
3071 mReconfigured = true;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003072 mLatestSessionParams = sessionParams;
Shuzhen Wang99080502021-03-07 21:08:20 -08003073 mGroupIdPhysicalCameraMap = groupIdPhysicalCameraMap;
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003074 // Prepare video stream for high speed recording.
3075 mPrepareVideoStream = isConstrainedHighSpeed;
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003076 mConstrainedMode = isConstrainedHighSpeed;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003077}
3078
Jianing Wei90e59c92014-03-12 18:29:36 -07003079status_t Camera3Device::RequestThread::queueRequestList(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003080 List<sp<CaptureRequest> > &requests,
3081 /*out*/
3082 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003083 ATRACE_CALL();
Jianing Wei90e59c92014-03-12 18:29:36 -07003084 Mutex::Autolock l(mRequestLock);
3085 for (List<sp<CaptureRequest> >::iterator it = requests.begin(); it != requests.end();
3086 ++it) {
3087 mRequestQueue.push_back(*it);
3088 }
3089
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003090 if (lastFrameNumber != NULL) {
3091 *lastFrameNumber = mFrameNumber + mRequestQueue.size() - 1;
3092 ALOGV("%s: requestId %d, mFrameNumber %" PRId32 ", lastFrameNumber %" PRId64 ".",
3093 __FUNCTION__, (*(requests.begin()))->mResultExtras.requestId, mFrameNumber,
3094 *lastFrameNumber);
3095 }
Jianing Weicb0652e2014-03-12 18:29:36 -07003096
Jianing Wei90e59c92014-03-12 18:29:36 -07003097 unpauseForNewRequests();
3098
3099 return OK;
3100}
3101
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003102
3103status_t Camera3Device::RequestThread::queueTrigger(
3104 RequestTrigger trigger[],
3105 size_t count) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003106 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003107 Mutex::Autolock l(mTriggerMutex);
3108 status_t ret;
3109
3110 for (size_t i = 0; i < count; ++i) {
3111 ret = queueTriggerLocked(trigger[i]);
3112
3113 if (ret != OK) {
3114 return ret;
3115 }
3116 }
3117
3118 return OK;
3119}
3120
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003121const String8& Camera3Device::RequestThread::getId(const wp<Camera3Device> &device) {
3122 static String8 deadId("<DeadDevice>");
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003123 sp<Camera3Device> d = device.promote();
Eino-Ville Talvala0b1cb142016-12-19 16:29:17 -08003124 if (d != nullptr) return d->mId;
3125 return deadId;
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003126}
3127
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003128status_t Camera3Device::RequestThread::queueTriggerLocked(
3129 RequestTrigger trigger) {
3130
3131 uint32_t tag = trigger.metadataTag;
3132 ssize_t index = mTriggerMap.indexOfKey(tag);
3133
3134 switch (trigger.getTagType()) {
3135 case TYPE_BYTE:
3136 // fall-through
3137 case TYPE_INT32:
3138 break;
3139 default:
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003140 ALOGE("%s: Type not supported: 0x%x", __FUNCTION__,
3141 trigger.getTagType());
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003142 return INVALID_OPERATION;
3143 }
3144
3145 /**
3146 * Collect only the latest trigger, since we only have 1 field
3147 * in the request settings per trigger tag, and can't send more than 1
3148 * trigger per request.
3149 */
3150 if (index != NAME_NOT_FOUND) {
3151 mTriggerMap.editValueAt(index) = trigger;
3152 } else {
3153 mTriggerMap.add(tag, trigger);
3154 }
3155
3156 return OK;
3157}
3158
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003159status_t Camera3Device::RequestThread::setRepeatingRequests(
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003160 const RequestList &requests,
3161 /*out*/
3162 int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003163 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003164 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003165 if (lastFrameNumber != NULL) {
3166 *lastFrameNumber = mRepeatingLastFrameNumber;
3167 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003168 mRepeatingRequests.clear();
Shuzhen Wang316781a2020-08-18 18:11:01 -07003169 mFirstRepeating = true;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003170 mRepeatingRequests.insert(mRepeatingRequests.begin(),
3171 requests.begin(), requests.end());
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07003172
3173 unpauseForNewRequests();
3174
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003175 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003176 return OK;
3177}
3178
Chih-Hung Hsieh8b0b9712016-08-09 14:25:53 -07003179bool Camera3Device::RequestThread::isRepeatingRequestLocked(const sp<CaptureRequest>& requestIn) {
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003180 if (mRepeatingRequests.empty()) {
3181 return false;
3182 }
3183 int32_t requestId = requestIn->mResultExtras.requestId;
3184 const RequestList &repeatRequests = mRepeatingRequests;
3185 // All repeating requests are guaranteed to have same id so only check first quest
3186 const sp<CaptureRequest> firstRequest = *repeatRequests.begin();
3187 return (firstRequest->mResultExtras.requestId == requestId);
3188}
3189
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003190status_t Camera3Device::RequestThread::clearRepeatingRequests(/*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003191 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003192 Mutex::Autolock l(mRequestLock);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003193 return clearRepeatingRequestsLocked(lastFrameNumber);
3194
3195}
3196
Jayant Chowdharya93f3462022-11-01 23:32:45 +00003197status_t Camera3Device::RequestThread::clearRepeatingRequestsLocked(
3198 /*out*/int64_t *lastFrameNumber) {
Emilian Peev2295df72021-11-12 18:14:10 -08003199 std::vector<int32_t> streamIds;
3200 for (const auto& request : mRepeatingRequests) {
3201 for (const auto& stream : request->mOutputStreams) {
3202 streamIds.push_back(stream->getId());
3203 }
3204 }
3205
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003206 mRepeatingRequests.clear();
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003207 if (lastFrameNumber != NULL) {
3208 *lastFrameNumber = mRepeatingLastFrameNumber;
3209 }
Emilian Peev2295df72021-11-12 18:14:10 -08003210
3211 mInterface->repeatingRequestEnd(mRepeatingLastFrameNumber, streamIds);
3212
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003213 mRepeatingLastFrameNumber = hardware::camera2::ICameraDeviceUser::NO_IN_FLIGHT_REPEATING_FRAMES;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003214 return OK;
3215}
3216
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003217status_t Camera3Device::RequestThread::clear(
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003218 /*out*/int64_t *lastFrameNumber) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003219 ATRACE_CALL();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003220 Mutex::Autolock l(mRequestLock);
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07003221 ALOGV("RequestThread::%s:", __FUNCTION__);
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003222
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003223 // Send errors for all requests pending in the request queue, including
3224 // pending repeating requests
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003225 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003226 if (listener != NULL) {
3227 for (RequestList::iterator it = mRequestQueue.begin();
3228 it != mRequestQueue.end(); ++it) {
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003229 // Abort the input buffers for reprocess requests.
3230 if ((*it)->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07003231 camera_stream_buffer_t inputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003232 camera3::Size inputBufferSize;
Eino-Ville Talvalaba435252017-06-21 16:07:25 -07003233 status_t res = (*it)->mInputStream->getInputBuffer(&inputBuffer,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003234 &inputBufferSize, /*respectHalLimit*/ false);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003235 if (res != OK) {
3236 ALOGW("%s: %d: couldn't get input buffer while clearing the request "
3237 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3238 } else {
Emilian Peevf4816702020-04-03 15:44:51 -07003239 inputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07003240 res = (*it)->mInputStream->returnInputBuffer(inputBuffer);
3241 if (res != OK) {
3242 ALOGE("%s: %d: couldn't return input buffer while clearing the request "
3243 "list: %s (%d)", __FUNCTION__, __LINE__, strerror(-res), res);
3244 }
3245 }
3246 }
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003247 // Set the frame number this request would have had, if it
3248 // had been submitted; this frame number will not be reused.
3249 // The requestId and burstId fields were set when the request was
3250 // submitted originally (in convertMetadataListToRequestListLocked)
3251 (*it)->mResultExtras.frameNumber = mFrameNumber++;
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08003252 listener->notifyError(hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Eino-Ville Talvala17543512014-08-06 14:32:02 -07003253 (*it)->mResultExtras);
Yin-Chia Yeh8684b7f2014-06-13 14:53:05 -07003254 }
3255 }
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003256 mRequestQueue.clear();
Jinguang Dongb26e7a02016-11-14 16:04:02 +08003257
3258 Mutex::Autolock al(mTriggerMutex);
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003259 mTriggerMap.clear();
Jayant Chowdharya93f3462022-11-01 23:32:45 +00003260 clearRepeatingRequestsLocked(lastFrameNumber);
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07003261 mRequestClearing = true;
Emilian Peev8dae54c2019-12-02 15:17:17 -08003262 mRequestSignal.signal();
Eino-Ville Talvalaabaa51d2013-08-14 11:37:00 -07003263 return OK;
3264}
3265
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003266status_t Camera3Device::RequestThread::flush() {
3267 ATRACE_CALL();
3268 Mutex::Autolock l(mFlushLock);
3269
Emilian Peev08dd2452017-04-06 16:55:14 +01003270 return mInterface->flush();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003271}
3272
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003273void Camera3Device::RequestThread::setPaused(bool paused) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003274 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003275 Mutex::Autolock l(mPauseLock);
3276 mDoPause = paused;
3277 mDoPauseSignal.signal();
3278}
3279
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003280status_t Camera3Device::RequestThread::waitUntilRequestProcessed(
3281 int32_t requestId, nsecs_t timeout) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003282 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003283 Mutex::Autolock l(mLatestRequestMutex);
3284 status_t res;
Kwangkyu Park9dd5eda2023-06-20 21:12:06 +09003285 while (mLatestRequestId != requestId && mLatestFailedRequestId != requestId) {
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003286 nsecs_t startTime = systemTime();
3287
3288 res = mLatestRequestSignal.waitRelative(mLatestRequestMutex, timeout);
3289 if (res != OK) return res;
3290
3291 timeout -= (systemTime() - startTime);
3292 }
3293
3294 return OK;
3295}
3296
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003297void Camera3Device::RequestThread::requestExit() {
3298 // Call parent to set up shutdown
3299 Thread::requestExit();
3300 // The exit from any possible waits
3301 mDoPauseSignal.signal();
3302 mRequestSignal.signal();
Shuzhen Wang686f6442017-06-20 16:16:04 -07003303
3304 mRequestLatency.log("ProcessCaptureRequest latency histogram");
3305 mRequestLatency.reset();
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003306}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003307
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003308void Camera3Device::RequestThread::checkAndStopRepeatingRequest() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003309 ATRACE_CALL();
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003310 bool surfaceAbandoned = false;
3311 int64_t lastFrameNumber = 0;
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003312 sp<NotificationListener> listener;
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003313 {
3314 Mutex::Autolock l(mRequestLock);
3315 // Check all streams needed by repeating requests are still valid. Otherwise, stop
3316 // repeating requests.
3317 for (const auto& request : mRepeatingRequests) {
3318 for (const auto& s : request->mOutputStreams) {
3319 if (s->isAbandoned()) {
3320 surfaceAbandoned = true;
3321 clearRepeatingRequestsLocked(&lastFrameNumber);
3322 break;
3323 }
3324 }
3325 if (surfaceAbandoned) {
3326 break;
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003327 }
3328 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003329 listener = mListener.promote();
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003330 }
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07003331
3332 if (listener != NULL && surfaceAbandoned) {
3333 listener->notifyRepeatingRequestError(lastFrameNumber);
Yin-Chia Yeh473fad92016-05-23 15:54:41 -07003334 }
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003335}
3336
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003337bool Camera3Device::RequestThread::sendRequestsBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07003338 ATRACE_CALL();
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003339 status_t res;
3340 size_t batchSize = mNextRequests.size();
Emilian Peevf4816702020-04-03 15:44:51 -07003341 std::vector<camera_capture_request_t*> requests(batchSize);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003342 uint32_t numRequestProcessed = 0;
3343 for (size_t i = 0; i < batchSize; i++) {
3344 requests[i] = &mNextRequests.editItemAt(i).halRequest;
Yin-Chia Yeh885691c2018-05-01 15:54:24 -07003345 ATRACE_ASYNC_BEGIN("frame capture", mNextRequests[i].halRequest.frame_number);
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003346 }
3347
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003348 res = mInterface->processBatchCaptureRequests(requests, &numRequestProcessed);
3349
3350 bool triggerRemoveFailed = false;
3351 NextRequest& triggerFailedRequest = mNextRequests.editItemAt(0);
3352 for (size_t i = 0; i < numRequestProcessed; i++) {
3353 NextRequest& nextRequest = mNextRequests.editItemAt(i);
3354 nextRequest.submitted = true;
3355
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003356 updateNextRequest(nextRequest);
Emilian Peevaebbe412018-01-15 13:53:24 +00003357
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003358 if (!triggerRemoveFailed) {
3359 // Remove any previously queued triggers (after unlock)
3360 status_t removeTriggerRes = removeTriggers(mPrevRequest);
3361 if (removeTriggerRes != OK) {
3362 triggerRemoveFailed = true;
3363 triggerFailedRequest = nextRequest;
3364 }
3365 }
3366 }
3367
3368 if (triggerRemoveFailed) {
3369 SET_ERR("RequestThread: Unable to remove triggers "
3370 "(capture request %d, HAL device: %s (%d)",
3371 triggerFailedRequest.halRequest.frame_number, strerror(-res), res);
3372 cleanUpFailedRequests(/*sendRequestError*/ false);
3373 return false;
3374 }
3375
3376 if (res != OK) {
3377 // Should only get a failure here for malformed requests or device-level
3378 // errors, so consider all errors fatal. Bad metadata failures should
3379 // come through notify.
3380 SET_ERR("RequestThread: Unable to submit capture request %d to HAL device: %s (%d)",
3381 mNextRequests[numRequestProcessed].halRequest.frame_number,
3382 strerror(-res), res);
3383 cleanUpFailedRequests(/*sendRequestError*/ false);
3384 return false;
3385 }
3386 return true;
3387}
3388
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003389Camera3Device::RequestThread::ExpectedDurationInfo
3390 Camera3Device::RequestThread::calculateExpectedDurationRange(
3391 const camera_metadata_t *request) {
3392 ExpectedDurationInfo expectedDurationInfo = {
Shuzhen Wang00abbeb2022-02-25 17:14:42 -08003393 InFlightRequest::kDefaultMinExpectedDuration,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003394 InFlightRequest::kDefaultMaxExpectedDuration,
3395 /*isFixedFps*/false};
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003396 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3397 find_camera_metadata_ro_entry(request,
3398 ANDROID_CONTROL_AE_MODE,
3399 &e);
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003400 if (e.count == 0) return expectedDurationInfo;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003401
3402 switch (e.data.u8[0]) {
3403 case ANDROID_CONTROL_AE_MODE_OFF:
3404 find_camera_metadata_ro_entry(request,
3405 ANDROID_SENSOR_EXPOSURE_TIME,
3406 &e);
3407 if (e.count > 0) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003408 expectedDurationInfo.minDuration = e.data.i64[0];
3409 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003410 }
3411 find_camera_metadata_ro_entry(request,
3412 ANDROID_SENSOR_FRAME_DURATION,
3413 &e);
3414 if (e.count > 0) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003415 expectedDurationInfo.minDuration =
3416 std::max(e.data.i64[0], expectedDurationInfo.minDuration);
3417 expectedDurationInfo.maxDuration = expectedDurationInfo.minDuration;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003418 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003419 expectedDurationInfo.isFixedFps = false;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003420 break;
3421 default:
3422 find_camera_metadata_ro_entry(request,
3423 ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
3424 &e);
3425 if (e.count > 1) {
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003426 expectedDurationInfo.minDuration = 1e9 / e.data.i32[1];
3427 expectedDurationInfo.maxDuration = 1e9 / e.data.i32[0];
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003428 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003429 expectedDurationInfo.isFixedFps = (e.data.i32[1] == e.data.i32[0]);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003430 break;
3431 }
3432
Shuzhen Wang696e4da2022-09-08 14:31:13 -07003433 return expectedDurationInfo;
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07003434}
3435
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003436bool Camera3Device::RequestThread::skipHFRTargetFPSUpdate(int32_t tag,
3437 const camera_metadata_ro_entry_t& newEntry, const camera_metadata_entry_t& currentEntry) {
3438 if (mConstrainedMode && (ANDROID_CONTROL_AE_TARGET_FPS_RANGE == tag) &&
3439 (newEntry.count == currentEntry.count) && (currentEntry.count == 2) &&
3440 (currentEntry.data.i32[1] == newEntry.data.i32[1])) {
3441 return true;
3442 }
3443
3444 return false;
3445}
3446
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003447void Camera3Device::RequestThread::updateNextRequest(NextRequest& nextRequest) {
3448 // Update the latest request sent to HAL
Shuzhen Wang83bff122020-11-20 15:51:39 -08003449 camera_capture_request_t& halRequest = nextRequest.halRequest;
3450 if (halRequest.settings != NULL) { // Don't update if they were unchanged
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003451 Mutex::Autolock al(mLatestRequestMutex);
3452
Shuzhen Wang83bff122020-11-20 15:51:39 -08003453 camera_metadata_t* cloned = clone_camera_metadata(halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003454 mLatestRequest.acquire(cloned);
3455
3456 mLatestPhysicalRequest.clear();
Shuzhen Wang83bff122020-11-20 15:51:39 -08003457 for (uint32_t i = 0; i < halRequest.num_physcam_settings; i++) {
3458 cloned = clone_camera_metadata(halRequest.physcam_settings[i]);
3459 mLatestPhysicalRequest.emplace(halRequest.physcam_id[i],
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003460 CameraMetadata(cloned));
3461 }
3462
3463 sp<Camera3Device> parent = mParent.promote();
3464 if (parent != NULL) {
Jayant Chowdharycd277cd2021-08-11 15:48:40 -07003465 int32_t inputStreamId = -1;
3466 if (halRequest.input_buffer != nullptr) {
3467 inputStreamId = Camera3Stream::cast(halRequest.input_buffer->stream)->getId();
3468 }
3469
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003470 parent->monitorMetadata(TagMonitor::REQUEST,
Shuzhen Wang83bff122020-11-20 15:51:39 -08003471 halRequest.frame_number,
Jayant Chowdharyc30b4c32021-08-18 11:43:16 -07003472 0, mLatestRequest, mLatestPhysicalRequest, halRequest.output_buffers,
3473 halRequest.num_output_buffers, inputStreamId);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003474 }
3475 }
3476
Shuzhen Wang83bff122020-11-20 15:51:39 -08003477 if (halRequest.settings != NULL) {
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003478 nextRequest.captureRequest->mSettingsList.begin()->metadata.unlock(
Shuzhen Wang83bff122020-11-20 15:51:39 -08003479 halRequest.settings);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003480 }
3481
Shuzhen Wang83bff122020-11-20 15:51:39 -08003482 cleanupPhysicalSettings(nextRequest.captureRequest, &halRequest);
Shuzhen Wangc2cba122018-05-17 18:10:24 -07003483}
3484
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003485bool Camera3Device::RequestThread::updateSessionParameters(const CameraMetadata& settings) {
3486 ATRACE_CALL();
3487 bool updatesDetected = false;
3488
Emilian Peev4ec17882019-01-24 17:16:58 -08003489 CameraMetadata updatedParams(mLatestSessionParams);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003490 for (auto tag : mSessionParamKeys) {
3491 camera_metadata_ro_entry entry = settings.find(tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003492 camera_metadata_entry lastEntry = updatedParams.find(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003493
3494 if (entry.count > 0) {
3495 bool isDifferent = false;
3496 if (lastEntry.count > 0) {
3497 // Have a last value, compare to see if changed
3498 if (lastEntry.type == entry.type &&
3499 lastEntry.count == entry.count) {
3500 // Same type and count, compare values
3501 size_t bytesPerValue = camera_metadata_type_size[lastEntry.type];
3502 size_t entryBytes = bytesPerValue * lastEntry.count;
3503 int cmp = memcmp(entry.data.u8, lastEntry.data.u8, entryBytes);
3504 if (cmp != 0) {
3505 isDifferent = true;
3506 }
3507 } else {
3508 // Count or type has changed
3509 isDifferent = true;
3510 }
3511 } else {
3512 // No last entry, so always consider to be different
3513 isDifferent = true;
3514 }
3515
3516 if (isDifferent) {
3517 ALOGV("%s: Session parameter tag id %d changed", __FUNCTION__, tag);
Emilian Peeva14b4dd2018-05-15 11:00:31 +01003518 if (!skipHFRTargetFPSUpdate(tag, entry, lastEntry)) {
3519 updatesDetected = true;
3520 }
Emilian Peev4ec17882019-01-24 17:16:58 -08003521 updatedParams.update(entry);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003522 }
3523 } else if (lastEntry.count > 0) {
3524 // Value has been removed
3525 ALOGV("%s: Session parameter tag id %d removed", __FUNCTION__, tag);
Emilian Peev4ec17882019-01-24 17:16:58 -08003526 updatedParams.erase(tag);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003527 updatesDetected = true;
3528 }
3529 }
3530
Emilian Peev4ec17882019-01-24 17:16:58 -08003531 bool reconfigureRequired;
3532 if (updatesDetected) {
3533 reconfigureRequired = mInterface->isReconfigurationRequired(mLatestSessionParams,
3534 updatedParams);
3535 mLatestSessionParams = updatedParams;
3536 } else {
3537 reconfigureRequired = false;
3538 }
3539
3540 return reconfigureRequired;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003541}
3542
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003543bool Camera3Device::RequestThread::threadLoop() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003544 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003545 status_t res;
Jayant Chowdhary37eca242019-11-18 08:55:56 -08003546 // Any function called from threadLoop() must not hold mInterfaceLock since
3547 // it could lead to deadlocks (disconnect() -> hold mInterfaceMutex -> wait for request thread
3548 // to finish -> request thread waits on mInterfaceMutex) http://b/143513518
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003549
3550 // Handle paused state.
3551 if (waitIfPaused()) {
3552 return true;
3553 }
3554
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003555 // Wait for the next batch of requests.
3556 waitForNextRequestBatch();
3557 if (mNextRequests.size() == 0) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003558 return true;
3559 }
3560
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003561 // Get the latest request ID, if any
3562 int latestRequestId;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003563 camera_metadata_entry_t requestIdEntry = mNextRequests[mNextRequests.size() - 1].
Emilian Peevaebbe412018-01-15 13:53:24 +00003564 captureRequest->mSettingsList.begin()->metadata.find(ANDROID_REQUEST_ID);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003565 if (requestIdEntry.count > 0) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003566 latestRequestId = requestIdEntry.data.i32[0];
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003567 } else {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003568 ALOGW("%s: Did not have android.request.id set in the request.", __FUNCTION__);
3569 latestRequestId = NAME_NOT_FOUND;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07003570 }
3571
Emilian Peevd865f0d2023-03-17 17:13:07 -07003572 for (size_t i = 0; i < mNextRequests.size(); i++) {
3573 auto& nextRequest = mNextRequests.editItemAt(i);
3574 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
3575 // Do not override rotate&crop for stream configurations that include
3576 // SurfaceViews(HW_COMPOSER) output, unless mOverrideToPortrait is set.
3577 // The display rotation there will be compensated by NATIVE_WINDOW_TRANSFORM_INVERSE_DISPLAY
3578 captureRequest->mRotateAndCropChanged = (mComposerOutput && !mOverrideToPortrait) ? false :
3579 overrideAutoRotateAndCrop(captureRequest);
3580 captureRequest->mAutoframingChanged = overrideAutoframing(captureRequest);
3581 }
3582
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003583 // 'mNextRequests' will at this point contain either a set of HFR batched requests
3584 // or a single request from streaming or burst. In either case the first element
3585 // should contain the latest camera settings that we need to check for any session
3586 // parameter updates.
Emilian Peevaebbe412018-01-15 13:53:24 +00003587 if (updateSessionParameters(mNextRequests[0].captureRequest->mSettingsList.begin()->metadata)) {
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003588 res = OK;
3589
3590 //Input stream buffers are already acquired at this point so an input stream
3591 //will not be able to move to idle state unless we force it.
3592 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3593 res = mNextRequests[0].captureRequest->mInputStream->forceToIdle();
3594 if (res != OK) {
3595 ALOGE("%s: Failed to force idle input stream: %d", __FUNCTION__, res);
3596 cleanUpFailedRequests(/*sendRequestError*/ false);
3597 return false;
3598 }
3599 }
3600
3601 if (res == OK) {
Emilian Peev3bead5f2020-05-28 17:29:08 -07003602 sp<Camera3Device> parent = mParent.promote();
3603 if (parent != nullptr) {
3604 mReconfigured |= parent->reconfigureCamera(mLatestSessionParams, mStatusId);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00003605 }
3606
3607 if (mNextRequests[0].captureRequest->mInputStream != nullptr) {
3608 mNextRequests[0].captureRequest->mInputStream->restoreConfiguredState();
3609 if (res != OK) {
3610 ALOGE("%s: Failed to restore configured input stream: %d", __FUNCTION__, res);
3611 cleanUpFailedRequests(/*sendRequestError*/ false);
3612 return false;
3613 }
3614 }
3615 }
3616 }
3617
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003618 // Prepare a batch of HAL requests and output buffers.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003619 res = prepareHalRequests();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003620 if (res == TIMED_OUT) {
3621 // Not a fatal error if getting output buffers time out.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003622 cleanUpFailedRequests(/*sendRequestError*/ true);
Chien-Yu Chene8c535e2016-04-14 12:18:26 -07003623 // Check if any stream is abandoned.
3624 checkAndStopRepeatingRequest();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003625 return true;
3626 } else if (res != OK) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003627 cleanUpFailedRequests(/*sendRequestError*/ false);
Eino-Ville Talvala42368d92013-04-09 14:13:50 -07003628 return false;
3629 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003630
Zhijun Hecc27e112013-10-03 16:12:43 -07003631 // Inform waitUntilRequestProcessed thread of a new request ID
3632 {
3633 Mutex::Autolock al(mLatestRequestMutex);
3634
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003635 mLatestRequestId = latestRequestId;
Zhijun Hecc27e112013-10-03 16:12:43 -07003636 mLatestRequestSignal.signal();
3637 }
3638
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003639 // Submit a batch of requests to HAL.
3640 // Use flush lock only when submitting multilple requests in a batch.
3641 // TODO: The problem with flush lock is flush() will be blocked by process_capture_request()
3642 // which may take a long time to finish so synchronizing flush() and
3643 // process_capture_request() defeats the purpose of cancelling requests ASAP with flush().
3644 // For now, only synchronize for high speed recording and we should figure something out for
3645 // removing the synchronization.
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003646 bool useFlushLock = mNextRequests.size() > 1;
Eino-Ville Talvala17a61ad2013-06-03 16:53:32 -07003647
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003648 if (useFlushLock) {
3649 mFlushLock.lock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003650 }
3651
Zhijun Hef0645c12016-08-02 00:58:11 -07003652 ALOGVV("%s: %d: submitting %zu requests in a batch.", __FUNCTION__, __LINE__,
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003653 mNextRequests.size());
Igor Murashkin1e479c02013-09-06 16:55:14 -07003654
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08003655 sp<Camera3Device> parent = mParent.promote();
3656 if (parent != nullptr) {
3657 parent->mRequestBufferSM.onSubmittingRequest();
3658 }
3659
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003660 bool submitRequestSuccess = false;
Shuzhen Wang686f6442017-06-20 16:16:04 -07003661 nsecs_t tRequestStart = systemTime(SYSTEM_TIME_MONOTONIC);
Yin-Chia Yeh11648852019-05-16 10:42:54 -07003662 submitRequestSuccess = sendRequestsBatch();
3663
Shuzhen Wang686f6442017-06-20 16:16:04 -07003664 nsecs_t tRequestEnd = systemTime(SYSTEM_TIME_MONOTONIC);
3665 mRequestLatency.add(tRequestStart, tRequestEnd);
Igor Murashkin1e479c02013-09-06 16:55:14 -07003666
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003667 if (useFlushLock) {
3668 mFlushLock.unlock();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003669 }
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07003670
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003671 // Unset as current request
3672 {
3673 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003674 mNextRequests.clear();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003675 }
Yin-Chia Yehb978c382019-10-30 00:22:37 -07003676 mRequestSubmittedSignal.signal();
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07003677
Yin-Chia Yeh94c68e02017-03-06 14:09:44 -08003678 return submitRequestSuccess;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08003679}
3680
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003681status_t Camera3Device::removeFwkOnlyRegionKeys(CameraMetadata *request) {
3682 static const std::array<uint32_t, 4> kFwkOnlyRegionKeys = {ANDROID_CONTROL_AF_REGIONS_SET,
3683 ANDROID_CONTROL_AE_REGIONS_SET, ANDROID_CONTROL_AWB_REGIONS_SET,
3684 ANDROID_SCALER_CROP_REGION_SET};
3685 if (request == nullptr) {
3686 ALOGE("%s request metadata nullptr", __FUNCTION__);
3687 return BAD_VALUE;
3688 }
3689 status_t res = OK;
3690 for (const auto &key : kFwkOnlyRegionKeys) {
3691 if (request->exists(key)) {
3692 res = request->erase(key);
3693 if (res != OK) {
3694 return res;
3695 }
3696 }
3697 }
3698 return OK;
3699}
3700
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07003701status_t Camera3Device::RequestThread::prepareHalRequests() {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003702 ATRACE_CALL();
3703
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003704 bool batchedRequest = mNextRequests[0].captureRequest->mBatchSize > 1;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003705 for (size_t i = 0; i < mNextRequests.size(); i++) {
3706 auto& nextRequest = mNextRequests.editItemAt(i);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003707 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07003708 camera_capture_request_t* halRequest = &nextRequest.halRequest;
3709 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003710
3711 // Prepare a request to HAL
3712 halRequest->frame_number = captureRequest->mResultExtras.frameNumber;
3713
3714 // Insert any queued triggers (before metadata is locked)
3715 status_t res = insertTriggers(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003716 if (res < 0) {
3717 SET_ERR("RequestThread: Unable to insert triggers "
3718 "(capture request %d, HAL device: %s (%d)",
3719 halRequest->frame_number, strerror(-res), res);
3720 return INVALID_OPERATION;
3721 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003722
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003723 int triggerCount = res;
3724 bool triggersMixedIn = (triggerCount > 0 || mPrevTriggers > 0);
3725 mPrevTriggers = triggerCount;
3726
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003727 bool testPatternChanged = overrideTestPattern(captureRequest);
Shuzhen Wangaf22e912023-04-11 16:03:17 -07003728 bool settingsOverrideChanged = overrideSettingsOverride(captureRequest);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003729
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08003730 // If the request is the same as last, or we had triggers now or last time or
3731 // changing overrides this time
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003732 bool newRequest =
Emilian Peevd865f0d2023-03-17 17:13:07 -07003733 (mPrevRequest != captureRequest || triggersMixedIn ||
3734 captureRequest->mRotateAndCropChanged ||
3735 captureRequest->mAutoframingChanged ||
Shuzhen Wangaf22e912023-04-11 16:03:17 -07003736 testPatternChanged || settingsOverrideChanged) &&
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07003737 // Request settings are all the same within one batch, so only treat the first
3738 // request in a batch as new
Zhijun He54c36822018-07-18 09:33:39 -07003739 !(batchedRequest && i > 0);
Emilian Peev00420d22018-02-05 21:33:13 +00003740 if (newRequest) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003741 std::set<std::string> cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003742 /**
3743 * HAL workaround:
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003744 * Insert a fake trigger ID if a trigger is set but no trigger ID is
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003745 */
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003746 res = addFakeTriggerIds(captureRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003747 if (res != OK) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04003748 SET_ERR("RequestThread: Unable to insert fake trigger IDs "
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003749 "(capture request %d, HAL device: %s (%d)",
3750 halRequest->frame_number, strerror(-res), res);
3751 return INVALID_OPERATION;
3752 }
3753
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003754 {
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003755 sp<Camera3Device> parent = mParent.promote();
3756 if (parent != nullptr) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003757 List<PhysicalCameraSettings>::iterator it;
3758 for (it = captureRequest->mSettingsList.begin();
3759 it != captureRequest->mSettingsList.end(); it++) {
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003760 if (parent->mUHRCropAndMeteringRegionMappers.find(it->cameraId) ==
3761 parent->mUHRCropAndMeteringRegionMappers.end()) {
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003762 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3763 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3764 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3765 res);
3766 return INVALID_OPERATION;
3767 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003768 continue;
3769 }
3770
3771 if (!captureRequest->mUHRCropAndMeteringRegionsUpdated) {
3772 res = parent->mUHRCropAndMeteringRegionMappers[it->cameraId].
3773 updateCaptureRequest(&(it->metadata));
3774 if (res != OK) {
3775 SET_ERR("RequestThread: Unable to correct capture requests "
3776 "for scaler crop region and metering regions for request "
3777 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3778 res);
3779 return INVALID_OPERATION;
3780 }
3781 captureRequest->mUHRCropAndMeteringRegionsUpdated = true;
Jayant Chowdhary65c9bf02021-09-03 16:44:16 +00003782 if (removeFwkOnlyRegionKeys(&(it->metadata)) != OK) {
3783 SET_ERR("RequestThread: Unable to remove fwk-only keys from request"
3784 "%d: %s (%d)", halRequest->frame_number, strerror(-res),
3785 res);
3786 return INVALID_OPERATION;
3787 }
Jayant Chowdhary9255ce02021-07-15 11:18:17 -07003788 }
3789 }
3790
3791 // Correct metadata regions for distortion correction if enabled
3792 for (it = captureRequest->mSettingsList.begin();
3793 it != captureRequest->mSettingsList.end(); it++) {
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003794 if (parent->mDistortionMappers.find(it->cameraId) ==
3795 parent->mDistortionMappers.end()) {
3796 continue;
3797 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003798
3799 if (!captureRequest->mDistortionCorrectionUpdated) {
3800 res = parent->mDistortionMappers[it->cameraId].correctCaptureRequest(
3801 &(it->metadata));
3802 if (res != OK) {
3803 SET_ERR("RequestThread: Unable to correct capture requests "
3804 "for lens distortion for request %d: %s (%d)",
3805 halRequest->frame_number, strerror(-res), res);
3806 return INVALID_OPERATION;
3807 }
3808 captureRequest->mDistortionCorrectionUpdated = true;
Shuzhen Wang4f6fa9d2019-03-29 10:40:35 -07003809 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003810 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003811
3812 for (it = captureRequest->mSettingsList.begin();
3813 it != captureRequest->mSettingsList.end(); it++) {
3814 if (parent->mZoomRatioMappers.find(it->cameraId) ==
3815 parent->mZoomRatioMappers.end()) {
3816 continue;
3817 }
3818
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003819 if (!captureRequest->mZoomRatioIs1x) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003820 cameraIdsWithZoom.insert(it->cameraId);
3821 }
3822
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003823 if (!captureRequest->mZoomRatioUpdated) {
3824 res = parent->mZoomRatioMappers[it->cameraId].updateCaptureRequest(
3825 &(it->metadata));
3826 if (res != OK) {
3827 SET_ERR("RequestThread: Unable to correct capture requests "
3828 "for zoom ratio for request %d: %s (%d)",
3829 halRequest->frame_number, strerror(-res), res);
3830 return INVALID_OPERATION;
3831 }
3832 captureRequest->mZoomRatioUpdated = true;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003833 }
3834 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003835 if (captureRequest->mRotateAndCropAuto &&
3836 !captureRequest->mRotationAndCropUpdated) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003837 for (it = captureRequest->mSettingsList.begin();
3838 it != captureRequest->mSettingsList.end(); it++) {
3839 auto mapper = parent->mRotateAndCropMappers.find(it->cameraId);
3840 if (mapper != parent->mRotateAndCropMappers.end()) {
3841 res = mapper->second.updateCaptureRequest(&(it->metadata));
3842 if (res != OK) {
3843 SET_ERR("RequestThread: Unable to correct capture requests "
3844 "for rotate-and-crop for request %d: %s (%d)",
3845 halRequest->frame_number, strerror(-res), res);
3846 return INVALID_OPERATION;
3847 }
3848 }
3849 }
Shuzhen Wangd1d051a2020-08-20 15:42:23 -07003850 captureRequest->mRotationAndCropUpdated = true;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08003851 }
Emilian Peeva1b26262023-02-06 17:27:41 -08003852
3853 for (it = captureRequest->mSettingsList.begin();
3854 it != captureRequest->mSettingsList.end(); it++) {
3855 res = hardware::cameraservice::utils::conversion::aidl::filterVndkKeys(
3856 mVndkVersion, it->metadata, false /*isStatic*/);
3857 if (res != OK) {
3858 SET_ERR("RequestThread: Failed during VNDK filter of capture requests "
3859 "%d: %s (%d)", halRequest->frame_number, strerror(-res), res);
3860 return INVALID_OPERATION;
3861 }
3862 }
Eino-Ville Talvala7b8a1fd2018-05-22 15:30:35 -07003863 }
3864 }
3865
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003866 /**
3867 * The request should be presorted so accesses in HAL
3868 * are O(logn). Sidenote, sorting a sorted metadata is nop.
3869 */
Emilian Peevaebbe412018-01-15 13:53:24 +00003870 captureRequest->mSettingsList.begin()->metadata.sort();
3871 halRequest->settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003872 mPrevRequest = captureRequest;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08003873 mPrevCameraIdsWithZoom = cameraIdsWithZoom;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003874 ALOGVV("%s: Request settings are NEW", __FUNCTION__);
3875
3876 IF_ALOGV() {
3877 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
3878 find_camera_metadata_ro_entry(
3879 halRequest->settings,
3880 ANDROID_CONTROL_AF_TRIGGER,
3881 &e
3882 );
3883 if (e.count > 0) {
3884 ALOGV("%s: Request (frame num %d) had AF trigger 0x%x",
3885 __FUNCTION__,
3886 halRequest->frame_number,
3887 e.data.u8[0]);
3888 }
3889 }
3890 } else {
3891 // leave request.settings NULL to indicate 'reuse latest given'
3892 ALOGVV("%s: Request settings are REUSED",
3893 __FUNCTION__);
3894 }
3895
Emilian Peevaebbe412018-01-15 13:53:24 +00003896 if (captureRequest->mSettingsList.size() > 1) {
3897 halRequest->num_physcam_settings = captureRequest->mSettingsList.size() - 1;
3898 halRequest->physcam_id = new const char* [halRequest->num_physcam_settings];
Emilian Peev00420d22018-02-05 21:33:13 +00003899 if (newRequest) {
3900 halRequest->physcam_settings =
3901 new const camera_metadata* [halRequest->num_physcam_settings];
3902 } else {
3903 halRequest->physcam_settings = nullptr;
3904 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003905 auto it = ++captureRequest->mSettingsList.begin();
3906 size_t i = 0;
3907 for (; it != captureRequest->mSettingsList.end(); it++, i++) {
3908 halRequest->physcam_id[i] = it->cameraId.c_str();
Emilian Peev00420d22018-02-05 21:33:13 +00003909 if (newRequest) {
3910 it->metadata.sort();
3911 halRequest->physcam_settings[i] = it->metadata.getAndLock();
3912 }
Emilian Peevaebbe412018-01-15 13:53:24 +00003913 }
3914 }
3915
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003916 uint32_t totalNumBuffers = 0;
3917
3918 // Fill in buffers
3919 if (captureRequest->mInputStream != NULL) {
3920 halRequest->input_buffer = &captureRequest->mInputBuffer;
Shuzhen Wang83bff122020-11-20 15:51:39 -08003921
3922 halRequest->input_width = captureRequest->mInputBufferSize.width;
3923 halRequest->input_height = captureRequest->mInputBufferSize.height;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003924 totalNumBuffers += 1;
3925 } else {
3926 halRequest->input_buffer = NULL;
3927 }
3928
Emilian Peevf4816702020-04-03 15:44:51 -07003929 outputBuffers->insertAt(camera_stream_buffer_t(), 0,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07003930 captureRequest->mOutputStreams.size());
3931 halRequest->output_buffers = outputBuffers->array();
Shuzhen Wang99080502021-03-07 21:08:20 -08003932 std::set<std::set<String8>> requestedPhysicalCameras;
Yin-Chia Yehb3a80b12018-09-04 12:13:05 -07003933
3934 sp<Camera3Device> parent = mParent.promote();
3935 if (parent == NULL) {
3936 // Should not happen, and nowhere to send errors to, so just log it
3937 CLOGE("RequestThread: Parent is gone");
3938 return INVALID_OPERATION;
3939 }
3940 nsecs_t waitDuration = kBaseGetBufferWait + parent->getExpectedInFlightDuration();
3941
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003942 SurfaceMap uniqueSurfaceIdMap;
Shuzhen Wang4a472662017-02-26 23:29:04 -08003943 for (size_t j = 0; j < captureRequest->mOutputStreams.size(); j++) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003944 sp<Camera3OutputStreamInterface> outputStream =
3945 captureRequest->mOutputStreams.editItemAt(j);
3946 int streamId = outputStream->getId();
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003947
3948 // Prepare video buffers for high speed recording on the first video request.
3949 if (mPrepareVideoStream && outputStream->isVideoStream()) {
3950 // Only try to prepare video stream on the first video request.
3951 mPrepareVideoStream = false;
3952
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07003953 res = outputStream->startPrepare(Camera3StreamInterface::ALLOCATE_PIPELINE_MAX,
3954 false /*blockRequest*/);
Chien-Yu Chenc66969b2016-05-19 16:37:51 -07003955 while (res == NOT_ENOUGH_DATA) {
3956 res = outputStream->prepareNextBuffer();
3957 }
3958 if (res != OK) {
3959 ALOGW("%s: Preparing video buffers for high speed failed: %s (%d)",
3960 __FUNCTION__, strerror(-res), res);
3961 outputStream->cancelPrepare();
3962 }
3963 }
3964
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003965 std::vector<size_t> uniqueSurfaceIds;
3966 res = outputStream->getUniqueSurfaceIds(
3967 captureRequest->mOutputSurfaces[streamId],
3968 &uniqueSurfaceIds);
3969 // INVALID_OPERATION is normal output for streams not supporting surfaceIds
3970 if (res != OK && res != INVALID_OPERATION) {
3971 ALOGE("%s: failed to query stream %d unique surface IDs",
3972 __FUNCTION__, streamId);
3973 return res;
3974 }
3975 if (res == OK) {
3976 uniqueSurfaceIdMap.insert({streamId, std::move(uniqueSurfaceIds)});
3977 }
3978
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003979 if (mUseHalBufManager) {
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003980 if (outputStream->isAbandoned()) {
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07003981 ALOGV("%s: stream %d is abandoned, skipping request", __FUNCTION__, streamId);
Yin-Chia Yeh110342b2018-11-19 11:47:46 -08003982 return TIMED_OUT;
3983 }
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003984 // HAL will request buffer through requestStreamBuffer API
Emilian Peevf4816702020-04-03 15:44:51 -07003985 camera_stream_buffer_t& buffer = outputBuffers->editItemAt(j);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003986 buffer.stream = outputStream->asHalStream();
3987 buffer.buffer = nullptr;
Emilian Peevf4816702020-04-03 15:44:51 -07003988 buffer.status = CAMERA_BUFFER_STATUS_OK;
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003989 buffer.acquire_fence = -1;
3990 buffer.release_fence = -1;
Emilian Peevf0348ae2021-01-13 13:39:45 -08003991 // Mark the output stream as unpreparable to block clients from calling
3992 // 'prepare' after this request reaches CameraHal and before the respective
3993 // buffers are requested.
3994 outputStream->markUnpreparable();
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003995 } else {
3996 res = outputStream->getBuffer(&outputBuffers->editItemAt(j),
3997 waitDuration,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07003998 captureRequest->mOutputSurfaces[streamId]);
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07003999 if (res != OK) {
4000 // Can't get output buffer from gralloc queue - this could be due to
4001 // abandoned queue or other consumer misbehavior, so not a fatal
4002 // error
Yin-Chia Yeha1b56c82019-03-27 15:50:39 -07004003 ALOGV("RequestThread: Can't get output buffer, skipping request:"
Yin-Chia Yehd5cd5ff2018-10-01 14:43:04 -07004004 " %s (%d)", strerror(-res), res);
4005
4006 return TIMED_OUT;
4007 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004008 }
Shuzhen Wang68ac7ad2019-01-30 14:03:28 -08004009
4010 {
4011 sp<Camera3Device> parent = mParent.promote();
4012 if (parent != nullptr) {
4013 const String8& streamCameraId = outputStream->getPhysicalCameraId();
4014 for (const auto& settings : captureRequest->mSettingsList) {
4015 if ((streamCameraId.isEmpty() &&
4016 parent->getId() == settings.cameraId.c_str()) ||
4017 streamCameraId == settings.cameraId.c_str()) {
4018 outputStream->fireBufferRequestForFrameNumber(
4019 captureRequest->mResultExtras.frameNumber,
4020 settings.metadata);
4021 }
4022 }
4023 }
4024 }
Shuzhen Wang0129d522016-10-30 22:43:41 -07004025
Shuzhen Wang5c22c152017-12-31 17:12:25 -08004026 String8 physicalCameraId = outputStream->getPhysicalCameraId();
Shuzhen Wang99080502021-03-07 21:08:20 -08004027 int32_t streamGroupId = outputStream->getHalStreamGroupId();
4028 if (streamGroupId != -1 && mGroupIdPhysicalCameraMap.count(streamGroupId) == 1) {
4029 requestedPhysicalCameras.insert(mGroupIdPhysicalCameraMap[streamGroupId]);
4030 } else if (!physicalCameraId.isEmpty()) {
4031 requestedPhysicalCameras.insert(std::set<String8>({physicalCameraId}));
Shuzhen Wang5c22c152017-12-31 17:12:25 -08004032 }
4033 halRequest->num_output_buffers++;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004034 }
4035 totalNumBuffers += halRequest->num_output_buffers;
4036
4037 // Log request in the in-flight queue
Shuzhen Wang4a472662017-02-26 23:29:04 -08004038 // If this request list is for constrained high speed recording (not
4039 // preview), and the current request is not the last one in the batch,
4040 // do not send callback to the app.
4041 bool hasCallback = true;
Yin-Chia Yehd07b11e2018-06-01 12:50:02 -07004042 if (batchedRequest && i != mNextRequests.size()-1) {
Shuzhen Wang4a472662017-02-26 23:29:04 -08004043 hasCallback = false;
4044 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01004045 bool isStillCapture = false;
Shuzhen Wang26abaf42018-08-28 15:41:20 -07004046 bool isZslCapture = false;
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004047 const camera_metadata_t* settings = halRequest->settings;
4048 bool shouldUnlockSettings = false;
4049 if (settings == nullptr) {
4050 shouldUnlockSettings = true;
4051 settings = captureRequest->mSettingsList.begin()->metadata.getAndLock();
4052 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01004053 if (!mNextRequests[0].captureRequest->mSettingsList.begin()->metadata.isEmpty()) {
4054 camera_metadata_ro_entry_t e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004055 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_CAPTURE_INTENT, &e);
Emilian Peev9dd21f42018-08-03 13:39:29 +01004056 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE)) {
4057 isStillCapture = true;
4058 ATRACE_ASYNC_BEGIN("still capture", mNextRequests[i].halRequest.frame_number);
4059 }
Shuzhen Wang26abaf42018-08-28 15:41:20 -07004060
Emilian Peevaf8416e2021-02-04 17:52:43 -08004061 e = camera_metadata_ro_entry_t();
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004062 find_camera_metadata_ro_entry(settings, ANDROID_CONTROL_ENABLE_ZSL, &e);
Shuzhen Wang26abaf42018-08-28 15:41:20 -07004063 if ((e.count > 0) && (e.data.u8[0] == ANDROID_CONTROL_ENABLE_ZSL_TRUE)) {
4064 isZslCapture = true;
4065 }
Emilian Peev9dd21f42018-08-03 13:39:29 +01004066 }
Shuzhen Wang696e4da2022-09-08 14:31:13 -07004067 auto expectedDurationInfo = calculateExpectedDurationRange(settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004068 res = parent->registerInFlight(halRequest->frame_number,
4069 totalNumBuffers, captureRequest->mResultExtras,
4070 /*hasInput*/halRequest->input_buffer != NULL,
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004071 hasCallback,
Shuzhen Wang696e4da2022-09-08 14:31:13 -07004072 expectedDurationInfo.minDuration,
4073 expectedDurationInfo.maxDuration,
4074 expectedDurationInfo.isFixedFps,
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004075 requestedPhysicalCameras, isStillCapture, isZslCapture,
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004076 captureRequest->mRotateAndCropAuto, captureRequest->mAutoframingAuto,
4077 mPrevCameraIdsWithZoom,
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004078 (mUseHalBufManager) ? uniqueSurfaceIdMap :
Shuzhen Wang316781a2020-08-18 18:11:01 -07004079 SurfaceMap{}, captureRequest->mRequestTimeNs);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004080 ALOGVV("%s: registered in flight requestId = %" PRId32 ", frameNumber = %" PRId64
4081 ", burstId = %" PRId32 ".",
4082 __FUNCTION__,
4083 captureRequest->mResultExtras.requestId, captureRequest->mResultExtras.frameNumber,
4084 captureRequest->mResultExtras.burstId);
Yin-Chia Yehf7057ca2020-11-16 14:11:40 -08004085
4086 if (shouldUnlockSettings) {
4087 captureRequest->mSettingsList.begin()->metadata.unlock(settings);
4088 }
4089
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004090 if (res != OK) {
4091 SET_ERR("RequestThread: Unable to register new in-flight request:"
4092 " %s (%d)", strerror(-res), res);
4093 return INVALID_OPERATION;
4094 }
4095 }
4096
4097 return OK;
4098}
4099
Igor Murashkin1e479c02013-09-06 16:55:14 -07004100CameraMetadata Camera3Device::RequestThread::getLatestRequest() const {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004101 ATRACE_CALL();
Igor Murashkin1e479c02013-09-06 16:55:14 -07004102 Mutex::Autolock al(mLatestRequestMutex);
4103
4104 ALOGV("RequestThread::%s", __FUNCTION__);
4105
4106 return mLatestRequest;
4107}
4108
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004109bool Camera3Device::RequestThread::isStreamPending(
4110 sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004111 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004112 Mutex::Autolock l(mRequestLock);
4113
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004114 for (const auto& nextRequest : mNextRequests) {
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004115 if (!nextRequest.submitted) {
4116 for (const auto& s : nextRequest.captureRequest->mOutputStreams) {
4117 if (stream == s) return true;
4118 }
4119 if (stream == nextRequest.captureRequest->mInputStream) return true;
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004120 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004121 }
4122
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004123 for (const auto& request : mRequestQueue) {
4124 for (const auto& s : request->mOutputStreams) {
4125 if (stream == s) return true;
4126 }
4127 if (stream == request->mInputStream) return true;
4128 }
4129
4130 for (const auto& request : mRepeatingRequests) {
4131 for (const auto& s : request->mOutputStreams) {
4132 if (stream == s) return true;
4133 }
4134 if (stream == request->mInputStream) return true;
4135 }
4136
4137 return false;
4138}
Jianing Weicb0652e2014-03-12 18:29:36 -07004139
Emilian Peev40ead602017-09-26 15:46:36 +01004140bool Camera3Device::RequestThread::isOutputSurfacePending(int streamId, size_t surfaceId) {
4141 ATRACE_CALL();
4142 Mutex::Autolock l(mRequestLock);
4143
4144 for (const auto& nextRequest : mNextRequests) {
4145 for (const auto& s : nextRequest.captureRequest->mOutputSurfaces) {
4146 if (s.first == streamId) {
4147 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4148 if (it != s.second.end()) {
4149 return true;
4150 }
4151 }
4152 }
4153 }
4154
4155 for (const auto& request : mRequestQueue) {
4156 for (const auto& s : request->mOutputSurfaces) {
4157 if (s.first == streamId) {
4158 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4159 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004160 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004161 }
4162 }
4163 }
4164 }
4165
4166 for (const auto& request : mRepeatingRequests) {
4167 for (const auto& s : request->mOutputSurfaces) {
4168 if (s.first == streamId) {
4169 const auto &it = std::find(s.second.begin(), s.second.end(), surfaceId);
4170 if (it != s.second.end()) {
Yin-Chia Yeh58b1b4e2018-10-15 12:18:36 -07004171 return true;
Emilian Peev40ead602017-09-26 15:46:36 +01004172 }
4173 }
4174 }
4175 }
4176
4177 return false;
4178}
4179
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004180void Camera3Device::RequestThread::signalPipelineDrain(const std::vector<int>& streamIds) {
4181 if (!mUseHalBufManager) {
4182 ALOGE("%s called for camera device not supporting HAL buffer management", __FUNCTION__);
4183 return;
4184 }
4185
4186 Mutex::Autolock pl(mPauseLock);
4187 if (mPaused) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004188 mInterface->signalPipelineDrain(streamIds);
4189 return;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004190 }
4191 // If request thread is still busy, wait until paused then notify HAL
4192 mNotifyPipelineDrain = true;
4193 mStreamIdsToBeDrained = streamIds;
4194}
4195
Yin-Chia Yehe52b8fa2020-07-28 00:17:58 -07004196void Camera3Device::RequestThread::resetPipelineDrain() {
4197 Mutex::Autolock pl(mPauseLock);
4198 mNotifyPipelineDrain = false;
4199 mStreamIdsToBeDrained.clear();
4200}
4201
Emilian Peevc0fe54c2020-03-11 14:05:07 -07004202void Camera3Device::RequestThread::clearPreviousRequest() {
4203 Mutex::Autolock l(mRequestLock);
4204 mPrevRequest.clear();
4205}
4206
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004207status_t Camera3Device::RequestThread::setRotateAndCropAutoBehavior(
4208 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
4209 ATRACE_CALL();
4210 Mutex::Autolock l(mTriggerMutex);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004211 mRotateAndCropOverride = rotateAndCropValue;
4212 return OK;
4213}
4214
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004215status_t Camera3Device::RequestThread::setAutoframingAutoBehaviour(
4216 camera_metadata_enum_android_control_autoframing_t autoframingValue) {
4217 ATRACE_CALL();
4218 Mutex::Autolock l(mTriggerMutex);
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004219 mAutoframingOverride = autoframingValue;
4220 return OK;
4221}
4222
Emilian Peeve23f1d92021-09-20 14:56:01 -07004223status_t Camera3Device::RequestThread::setComposerSurface(bool composerSurfacePresent) {
4224 ATRACE_CALL();
4225 Mutex::Autolock l(mTriggerMutex);
4226 mComposerOutput = composerSurfacePresent;
4227 return OK;
4228}
4229
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004230status_t Camera3Device::RequestThread::setCameraMute(int32_t muteMode) {
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004231 ATRACE_CALL();
4232 Mutex::Autolock l(mTriggerMutex);
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07004233 if (muteMode != mCameraMute) {
4234 mCameraMute = muteMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004235 mCameraMuteChanged = true;
4236 }
4237 return OK;
4238}
4239
Shuzhen Wangaf22e912023-04-11 16:03:17 -07004240status_t Camera3Device::RequestThread::setZoomOverride(int32_t zoomOverride) {
4241 ATRACE_CALL();
4242 Mutex::Autolock l(mTriggerMutex);
4243 mSettingsOverride = zoomOverride;
4244 return OK;
4245}
4246
Yin-Chia Yeh598fc602017-07-24 11:37:23 -07004247nsecs_t Camera3Device::getExpectedInFlightDuration() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004248 ATRACE_CALL();
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004249 std::lock_guard<std::mutex> l(mInFlightLock);
Eino-Ville Talvala10bd57e2017-06-23 16:22:44 -07004250 return mExpectedInflightDuration > kMinInflightDuration ?
4251 mExpectedInflightDuration : kMinInflightDuration;
4252}
4253
Emilian Peevaebbe412018-01-15 13:53:24 +00004254void Camera3Device::RequestThread::cleanupPhysicalSettings(sp<CaptureRequest> request,
Emilian Peevf4816702020-04-03 15:44:51 -07004255 camera_capture_request_t *halRequest) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004256 if ((request == nullptr) || (halRequest == nullptr)) {
4257 ALOGE("%s: Invalid request!", __FUNCTION__);
4258 return;
4259 }
4260
4261 if (halRequest->num_physcam_settings > 0) {
4262 if (halRequest->physcam_id != nullptr) {
4263 delete [] halRequest->physcam_id;
4264 halRequest->physcam_id = nullptr;
4265 }
4266 if (halRequest->physcam_settings != nullptr) {
4267 auto it = ++(request->mSettingsList.begin());
4268 size_t i = 0;
4269 for (; it != request->mSettingsList.end(); it++, i++) {
4270 it->metadata.unlock(halRequest->physcam_settings[i]);
4271 }
4272 delete [] halRequest->physcam_settings;
4273 halRequest->physcam_settings = nullptr;
4274 }
4275 }
4276}
4277
Ravneetaeb20dc2022-03-30 05:33:03 +00004278status_t Camera3Device::setCameraServiceWatchdog(bool enabled) {
4279 Mutex::Autolock il(mInterfaceLock);
4280 Mutex::Autolock l(mLock);
4281
4282 if (mCameraServiceWatchdog != NULL) {
4283 mCameraServiceWatchdog->setEnabled(enabled);
4284 }
4285
4286 return OK;
4287}
4288
Shuzhen Wang16610a62022-12-15 22:38:07 -08004289void Camera3Device::setStreamUseCaseOverrides(
4290 const std::vector<int64_t>& useCaseOverrides) {
4291 Mutex::Autolock il(mInterfaceLock);
4292 Mutex::Autolock l(mLock);
4293 mStreamUseCaseOverrides = useCaseOverrides;
4294}
4295
4296void Camera3Device::clearStreamUseCaseOverrides() {
4297 Mutex::Autolock il(mInterfaceLock);
4298 Mutex::Autolock l(mLock);
4299 mStreamUseCaseOverrides.clear();
4300}
4301
Shuzhen Wang03fe6232023-02-05 12:41:15 -08004302bool Camera3Device::hasDeviceError() {
4303 Mutex::Autolock il(mInterfaceLock);
4304 Mutex::Autolock l(mLock);
4305 return mStatus == STATUS_ERROR;
4306}
4307
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004308void Camera3Device::RequestThread::cleanUpFailedRequests(bool sendRequestError) {
4309 if (mNextRequests.empty()) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004310 return;
4311 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004312
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004313 for (auto& nextRequest : mNextRequests) {
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004314 // Skip the ones that have been submitted successfully.
4315 if (nextRequest.submitted) {
4316 continue;
4317 }
4318
4319 sp<CaptureRequest> captureRequest = nextRequest.captureRequest;
Emilian Peevf4816702020-04-03 15:44:51 -07004320 camera_capture_request_t* halRequest = &nextRequest.halRequest;
4321 Vector<camera_stream_buffer_t>* outputBuffers = &nextRequest.outputBuffers;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004322
4323 if (halRequest->settings != NULL) {
Emilian Peevaebbe412018-01-15 13:53:24 +00004324 captureRequest->mSettingsList.begin()->metadata.unlock(halRequest->settings);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004325 }
4326
Emilian Peevaebbe412018-01-15 13:53:24 +00004327 cleanupPhysicalSettings(captureRequest, halRequest);
4328
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004329 if (captureRequest->mInputStream != NULL) {
Emilian Peevf4816702020-04-03 15:44:51 -07004330 captureRequest->mInputBuffer.status = CAMERA_BUFFER_STATUS_ERROR;
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004331 captureRequest->mInputStream->returnInputBuffer(captureRequest->mInputBuffer);
4332 }
4333
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004334 // No output buffer can be returned when using HAL buffer manager
4335 if (!mUseHalBufManager) {
4336 for (size_t i = 0; i < halRequest->num_output_buffers; i++) {
4337 //Buffers that failed processing could still have
4338 //valid acquire fence.
4339 int acquireFence = (*outputBuffers)[i].acquire_fence;
4340 if (0 <= acquireFence) {
4341 close(acquireFence);
4342 outputBuffers->editItemAt(i).acquire_fence = -1;
4343 }
Emilian Peevf4816702020-04-03 15:44:51 -07004344 outputBuffers->editItemAt(i).status = CAMERA_BUFFER_STATUS_ERROR;
Shuzhen Wang90708ea2021-11-04 11:40:49 -07004345 captureRequest->mOutputStreams.editItemAt(i)->returnBuffer((*outputBuffers)[i],
4346 /*timestamp*/0, /*readoutTimestamp*/0,
Yin-Chia Yeh21cb47b2019-01-18 15:08:17 -08004347 /*timestampIncreasing*/true, std::vector<size_t> (),
4348 captureRequest->mResultExtras.frameNumber);
Emilian Peevc58cf4c2017-05-11 17:23:41 +01004349 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004350 }
4351
4352 if (sendRequestError) {
4353 Mutex::Autolock l(mRequestLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004354 sp<NotificationListener> listener = mListener.promote();
4355 if (listener != NULL) {
4356 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004357 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004358 captureRequest->mResultExtras);
4359 }
Kwangkyu Park9dd5eda2023-06-20 21:12:06 +09004360 {
4361 Mutex::Autolock al(mLatestRequestMutex);
4362
4363 mLatestFailedRequestId = captureRequest->mResultExtras.requestId;
4364 mLatestRequestSignal.signal();
4365 }
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004366 }
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004367
4368 // Remove yet-to-be submitted inflight request from inflightMap
4369 {
4370 sp<Camera3Device> parent = mParent.promote();
4371 if (parent != NULL) {
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08004372 std::lock_guard<std::mutex> l(parent->mInFlightLock);
Shuzhen Wangcadb3302016-11-04 14:17:56 -07004373 ssize_t idx = parent->mInFlightMap.indexOfKey(captureRequest->mResultExtras.frameNumber);
4374 if (idx >= 0) {
4375 ALOGV("%s: Remove inflight request from queue: frameNumber %" PRId64,
4376 __FUNCTION__, captureRequest->mResultExtras.frameNumber);
4377 parent->removeInFlightMapEntryLocked(idx);
4378 }
4379 }
4380 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004381 }
Eino-Ville Talvalae74c2282015-05-27 14:46:23 -07004382
4383 Mutex::Autolock l(mRequestLock);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004384 mNextRequests.clear();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004385}
4386
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004387void Camera3Device::RequestThread::waitForNextRequestBatch() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004388 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004389 // Optimized a bit for the simple steady-state case (single repeating
4390 // request), to avoid putting that request in the queue temporarily.
4391 Mutex::Autolock l(mRequestLock);
4392
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004393 assert(mNextRequests.empty());
4394
4395 NextRequest nextRequest;
4396 nextRequest.captureRequest = waitForNextRequestLocked();
4397 if (nextRequest.captureRequest == nullptr) {
4398 return;
4399 }
4400
Emilian Peevf4816702020-04-03 15:44:51 -07004401 nextRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004402 nextRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004403 mNextRequests.add(nextRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004404
4405 // Wait for additional requests
4406 const size_t batchSize = nextRequest.captureRequest->mBatchSize;
4407
4408 for (size_t i = 1; i < batchSize; i++) {
4409 NextRequest additionalRequest;
4410 additionalRequest.captureRequest = waitForNextRequestLocked();
4411 if (additionalRequest.captureRequest == nullptr) {
4412 break;
4413 }
4414
Emilian Peevf4816702020-04-03 15:44:51 -07004415 additionalRequest.halRequest = camera_capture_request_t();
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004416 additionalRequest.submitted = false;
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004417 mNextRequests.add(additionalRequest);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004418 }
4419
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004420 if (mNextRequests.size() < batchSize) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004421 ALOGE("RequestThread: only get %zu out of %zu requests. Skipping requests.",
Chien-Yu Chen57ea2922015-09-04 12:58:56 -07004422 mNextRequests.size(), batchSize);
4423 cleanUpFailedRequests(/*sendRequestError*/true);
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004424 }
4425
4426 return;
4427}
4428
Jayant Chowdharya93f3462022-11-01 23:32:45 +00004429void Camera3Device::RequestThread::setRequestClearing() {
4430 Mutex::Autolock l(mRequestLock);
4431 mRequestClearing = true;
4432}
4433
Chien-Yu Chen85a64552015-08-28 15:46:12 -07004434sp<Camera3Device::CaptureRequest>
4435 Camera3Device::RequestThread::waitForNextRequestLocked() {
4436 status_t res;
4437 sp<CaptureRequest> nextRequest;
4438
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004439 while (mRequestQueue.empty()) {
4440 if (!mRepeatingRequests.empty()) {
4441 // Always atomically enqueue all requests in a repeating request
4442 // list. Guarantees a complete in-sequence set of captures to
4443 // application.
4444 const RequestList &requests = mRepeatingRequests;
Shuzhen Wang316781a2020-08-18 18:11:01 -07004445 if (mFirstRepeating) {
4446 mFirstRepeating = false;
4447 } else {
4448 for (auto& request : requests) {
4449 // For repeating requests, override timestamp request using
4450 // the time a request is inserted into the request queue,
4451 // because the original repeating request will have an old
4452 // fixed timestamp.
4453 request->mRequestTimeNs = systemTime();
4454 }
4455 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004456 RequestList::const_iterator firstRequest =
4457 requests.begin();
4458 nextRequest = *firstRequest;
4459 mRequestQueue.insert(mRequestQueue.end(),
4460 ++firstRequest,
4461 requests.end());
4462 // No need to wait any longer
Jianing Weicb0652e2014-03-12 18:29:36 -07004463
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004464 mRepeatingLastFrameNumber = mFrameNumber + requests.size() - 1;
Jianing Weicb0652e2014-03-12 18:29:36 -07004465
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004466 break;
4467 }
4468
Shuzhen Wangbb9b93d2022-04-07 13:22:48 -07004469 if (!mRequestClearing) {
4470 res = mRequestSignal.waitRelative(mRequestLock, kRequestTimeout);
4471 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004472
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004473 if ((mRequestQueue.empty() && mRepeatingRequests.empty()) ||
4474 exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004475 Mutex::Autolock pl(mPauseLock);
4476 if (mPaused == false) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004477 ALOGV("%s: RequestThread: Going idle", __FUNCTION__);
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004478 mPaused = true;
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004479 if (mNotifyPipelineDrain) {
4480 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4481 mNotifyPipelineDrain = false;
4482 mStreamIdsToBeDrained.clear();
4483 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004484 // Let the tracker know
4485 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4486 if (statusTracker != 0) {
4487 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4488 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004489 sp<Camera3Device> parent = mParent.promote();
4490 if (parent != nullptr) {
4491 parent->mRequestBufferSM.onRequestThreadPaused();
4492 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004493 }
Shuzhen Wangb8696c02022-05-20 13:31:02 -07004494 mRequestClearing = false;
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004495 // Stop waiting for now and let thread management happen
4496 return NULL;
4497 }
4498 }
4499
4500 if (nextRequest == NULL) {
4501 // Don't have a repeating request already in hand, so queue
4502 // must have an entry now.
4503 RequestList::iterator firstRequest =
4504 mRequestQueue.begin();
4505 nextRequest = *firstRequest;
4506 mRequestQueue.erase(firstRequest);
Shuzhen Wang9d066012016-09-30 11:30:20 -07004507 if (mRequestQueue.empty() && !nextRequest->mRepeating) {
4508 sp<NotificationListener> listener = mListener.promote();
4509 if (listener != NULL) {
4510 listener->notifyRequestQueueEmpty();
4511 }
4512 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004513 }
4514
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004515 // In case we've been unpaused by setPaused clearing mDoPause, need to
4516 // update internal pause state (capture/setRepeatingRequest unpause
4517 // directly).
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004518 Mutex::Autolock pl(mPauseLock);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004519 if (mPaused) {
4520 ALOGV("%s: RequestThread: Unpaused", __FUNCTION__);
4521 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4522 if (statusTracker != 0) {
4523 statusTracker->markComponentActive(mStatusId);
4524 }
4525 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004526 mPaused = false;
4527
4528 // Check if we've reconfigured since last time, and reset the preview
4529 // request if so. Can't use 'NULL request == repeat' across configure calls.
4530 if (mReconfigured) {
4531 mPrevRequest.clear();
4532 mReconfigured = false;
4533 }
4534
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004535 if (nextRequest != NULL) {
4536 nextRequest->mResultExtras.frameNumber = mFrameNumber++;
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004537 nextRequest->mResultExtras.afTriggerId = mCurrentAfTriggerId;
4538 nextRequest->mResultExtras.precaptureTriggerId = mCurrentPreCaptureTriggerId;
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004539
4540 // Since RequestThread::clear() removes buffers from the input stream,
4541 // get the right buffer here before unlocking mRequestLock
4542 if (nextRequest->mInputStream != NULL) {
Shuzhen Wang83bff122020-11-20 15:51:39 -08004543 res = nextRequest->mInputStream->getInputBuffer(&nextRequest->mInputBuffer,
4544 &nextRequest->mInputBufferSize);
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004545 if (res != OK) {
4546 // Can't get input buffer from gralloc queue - this could be due to
4547 // disconnected queue or other producer misbehavior, so not a fatal
4548 // error
4549 ALOGE("%s: Can't get input buffer, skipping request:"
4550 " %s (%d)", __FUNCTION__, strerror(-res), res);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07004551
4552 sp<NotificationListener> listener = mListener.promote();
4553 if (listener != NULL) {
4554 listener->notifyError(
Eino-Ville Talvalad56db1d2015-12-17 16:50:35 -08004555 hardware::camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST,
Chien-Yu Chenc2adf482015-05-27 14:27:49 -07004556 nextRequest->mResultExtras);
4557 }
4558 return NULL;
4559 }
4560 }
Jianing Wei2d6bb3f2014-04-11 10:00:31 -07004561 }
Chien-Yu Chend196d612015-06-22 19:49:01 -07004562
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004563 return nextRequest;
4564}
4565
4566bool Camera3Device::RequestThread::waitIfPaused() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004567 ATRACE_CALL();
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004568 status_t res;
4569 Mutex::Autolock l(mPauseLock);
4570 while (mDoPause) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004571 if (mPaused == false) {
4572 mPaused = true;
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004573 ALOGV("%s: RequestThread: Paused", __FUNCTION__);
Yin-Chia Yeh7447f0f2018-10-11 15:28:12 -07004574 if (mNotifyPipelineDrain) {
4575 mInterface->signalPipelineDrain(mStreamIdsToBeDrained);
4576 mNotifyPipelineDrain = false;
4577 mStreamIdsToBeDrained.clear();
4578 }
Yin-Chia Yehc300a072019-02-13 14:56:57 -08004579 // Let the tracker know
4580 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4581 if (statusTracker != 0) {
4582 statusTracker->markComponentIdle(mStatusId, Fence::NO_FENCE);
4583 }
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07004584 sp<Camera3Device> parent = mParent.promote();
4585 if (parent != nullptr) {
4586 parent->mRequestBufferSM.onRequestThreadPaused();
4587 }
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004588 }
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004589
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004590 res = mDoPauseSignal.waitRelative(mPauseLock, kRequestTimeout);
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004591 if (res == TIMED_OUT || exitPending()) {
Eino-Ville Talvalaf76e0272013-02-27 18:02:26 -08004592 return true;
4593 }
4594 }
4595 // We don't set mPaused to false here, because waitForNextRequest needs
4596 // to further manage the paused state in case of starvation.
4597 return false;
4598}
4599
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004600void Camera3Device::RequestThread::unpauseForNewRequests() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004601 ATRACE_CALL();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004602 // With work to do, mark thread as unpaused.
4603 // If paused by request (setPaused), don't resume, to avoid
4604 // extra signaling/waiting overhead to waitUntilPaused
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004605 mRequestSignal.signal();
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004606 Mutex::Autolock p(mPauseLock);
4607 if (!mDoPause) {
Eino-Ville Talvalaf1e98d82013-09-06 09:32:43 -07004608 ALOGV("%s: RequestThread: Going active", __FUNCTION__);
4609 if (mPaused) {
4610 sp<StatusTracker> statusTracker = mStatusTracker.promote();
4611 if (statusTracker != 0) {
4612 statusTracker->markComponentActive(mStatusId);
4613 }
4614 }
Eino-Ville Talvala26fe6c72013-08-29 12:46:18 -07004615 mPaused = false;
4616 }
4617}
4618
Eino-Ville Talvalab2058d12013-04-09 13:49:56 -07004619void Camera3Device::RequestThread::setErrorState(const char *fmt, ...) {
4620 sp<Camera3Device> parent = mParent.promote();
4621 if (parent != NULL) {
4622 va_list args;
4623 va_start(args, fmt);
4624
4625 parent->setErrorStateV(fmt, args);
4626
4627 va_end(args);
4628 }
4629}
4630
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004631status_t Camera3Device::RequestThread::insertTriggers(
4632 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004633 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004634 Mutex::Autolock al(mTriggerMutex);
4635
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004636 sp<Camera3Device> parent = mParent.promote();
4637 if (parent == NULL) {
4638 CLOGE("RequestThread: Parent is gone");
4639 return DEAD_OBJECT;
4640 }
4641
Emilian Peevaebbe412018-01-15 13:53:24 +00004642 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004643 size_t count = mTriggerMap.size();
4644
4645 for (size_t i = 0; i < count; ++i) {
4646 RequestTrigger trigger = mTriggerMap.valueAt(i);
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004647 uint32_t tag = trigger.metadataTag;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004648
4649 if (tag == ANDROID_CONTROL_AF_TRIGGER_ID || tag == ANDROID_CONTROL_AE_PRECAPTURE_ID) {
4650 bool isAeTrigger = (trigger.metadataTag == ANDROID_CONTROL_AE_PRECAPTURE_ID);
4651 uint32_t triggerId = static_cast<uint32_t>(trigger.entryValue);
Yin-Chia Yehc00a25c2014-08-21 14:27:44 -07004652 if (isAeTrigger) {
4653 request->mResultExtras.precaptureTriggerId = triggerId;
4654 mCurrentPreCaptureTriggerId = triggerId;
4655 } else {
4656 request->mResultExtras.afTriggerId = triggerId;
4657 mCurrentAfTriggerId = triggerId;
4658 }
Emilian Peev7e25e5e2017-04-07 15:48:49 +01004659 continue;
Yin-Chia Yeh741ace82014-06-23 14:07:56 -07004660 }
4661
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004662 camera_metadata_entry entry = metadata.find(tag);
4663
4664 if (entry.count > 0) {
4665 /**
4666 * Already has an entry for this trigger in the request.
4667 * Rewrite it with our requested trigger value.
4668 */
4669 RequestTrigger oldTrigger = trigger;
4670
4671 oldTrigger.entryValue = entry.data.u8[0];
4672
4673 mTriggerReplacedMap.add(tag, oldTrigger);
4674 } else {
4675 /**
4676 * More typical, no trigger entry, so we just add it
4677 */
4678 mTriggerRemovedMap.add(tag, trigger);
4679 }
4680
4681 status_t res;
4682
4683 switch (trigger.getTagType()) {
4684 case TYPE_BYTE: {
4685 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4686 res = metadata.update(tag,
4687 &entryValue,
4688 /*count*/1);
4689 break;
4690 }
4691 case TYPE_INT32:
4692 res = metadata.update(tag,
4693 &trigger.entryValue,
4694 /*count*/1);
4695 break;
4696 default:
4697 ALOGE("%s: Type not supported: 0x%x",
4698 __FUNCTION__,
4699 trigger.getTagType());
4700 return INVALID_OPERATION;
4701 }
4702
4703 if (res != OK) {
4704 ALOGE("%s: Failed to update request metadata with trigger tag %s"
4705 ", value %d", __FUNCTION__, trigger.getTagName(),
4706 trigger.entryValue);
4707 return res;
4708 }
4709
4710 ALOGV("%s: Mixed in trigger %s, value %d", __FUNCTION__,
4711 trigger.getTagName(),
4712 trigger.entryValue);
4713 }
4714
4715 mTriggerMap.clear();
4716
4717 return count;
4718}
4719
4720status_t Camera3Device::RequestThread::removeTriggers(
4721 const sp<CaptureRequest> &request) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07004722 ATRACE_CALL();
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004723 Mutex::Autolock al(mTriggerMutex);
4724
Emilian Peevaebbe412018-01-15 13:53:24 +00004725 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004726
4727 /**
4728 * Replace all old entries with their old values.
4729 */
4730 for (size_t i = 0; i < mTriggerReplacedMap.size(); ++i) {
4731 RequestTrigger trigger = mTriggerReplacedMap.valueAt(i);
4732
4733 status_t res;
4734
4735 uint32_t tag = trigger.metadataTag;
4736 switch (trigger.getTagType()) {
4737 case TYPE_BYTE: {
4738 uint8_t entryValue = static_cast<uint8_t>(trigger.entryValue);
4739 res = metadata.update(tag,
4740 &entryValue,
4741 /*count*/1);
4742 break;
4743 }
4744 case TYPE_INT32:
4745 res = metadata.update(tag,
4746 &trigger.entryValue,
4747 /*count*/1);
4748 break;
4749 default:
4750 ALOGE("%s: Type not supported: 0x%x",
4751 __FUNCTION__,
4752 trigger.getTagType());
4753 return INVALID_OPERATION;
4754 }
4755
4756 if (res != OK) {
4757 ALOGE("%s: Failed to restore request metadata with trigger tag %s"
4758 ", trigger value %d", __FUNCTION__,
4759 trigger.getTagName(), trigger.entryValue);
4760 return res;
4761 }
4762 }
4763 mTriggerReplacedMap.clear();
4764
4765 /**
4766 * Remove all new entries.
4767 */
4768 for (size_t i = 0; i < mTriggerRemovedMap.size(); ++i) {
4769 RequestTrigger trigger = mTriggerRemovedMap.valueAt(i);
4770 status_t res = metadata.erase(trigger.metadataTag);
4771
4772 if (res != OK) {
4773 ALOGE("%s: Failed to erase metadata with trigger tag %s"
4774 ", trigger value %d", __FUNCTION__,
4775 trigger.getTagName(), trigger.entryValue);
4776 return res;
4777 }
4778 }
4779 mTriggerRemovedMap.clear();
4780
4781 return OK;
4782}
4783
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004784status_t Camera3Device::RequestThread::addFakeTriggerIds(
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004785 const sp<CaptureRequest> &request) {
Eino-Ville Talvalad309fb92015-11-25 12:12:45 -08004786 // Trigger ID 0 had special meaning in the HAL2 spec, so avoid it here
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004787 static const int32_t fakeTriggerId = 1;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004788 status_t res;
4789
Emilian Peevaebbe412018-01-15 13:53:24 +00004790 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004791
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004792 // If AF trigger is active, insert a fake AF trigger ID if none already
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004793 // exists
4794 camera_metadata_entry afTrigger = metadata.find(ANDROID_CONTROL_AF_TRIGGER);
4795 camera_metadata_entry afId = metadata.find(ANDROID_CONTROL_AF_TRIGGER_ID);
4796 if (afTrigger.count > 0 &&
4797 afTrigger.data.u8[0] != ANDROID_CONTROL_AF_TRIGGER_IDLE &&
4798 afId.count == 0) {
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004799 res = metadata.update(ANDROID_CONTROL_AF_TRIGGER_ID, &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004800 if (res != OK) return res;
4801 }
4802
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004803 // If AE precapture trigger is active, insert a fake precapture trigger ID
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004804 // if none already exists
4805 camera_metadata_entry pcTrigger =
4806 metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER);
4807 camera_metadata_entry pcId = metadata.find(ANDROID_CONTROL_AE_PRECAPTURE_ID);
4808 if (pcTrigger.count > 0 &&
4809 pcTrigger.data.u8[0] != ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE &&
4810 pcId.count == 0) {
4811 res = metadata.update(ANDROID_CONTROL_AE_PRECAPTURE_ID,
Ivan Lozanoc0ad82f2020-07-30 09:32:57 -04004812 &fakeTriggerId, 1);
Eino-Ville Talvala2f876f92013-09-13 11:39:24 -07004813 if (res != OK) return res;
4814 }
4815
4816 return OK;
4817}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07004818
Emilian Peevd865f0d2023-03-17 17:13:07 -07004819bool Camera3Device::RequestThread::overrideAutoRotateAndCrop(const sp<CaptureRequest> &request) {
4820 ATRACE_CALL();
4821 Mutex::Autolock l(mTriggerMutex);
4822 return Camera3Device::overrideAutoRotateAndCrop(request, this->mOverrideToPortrait,
4823 this->mRotateAndCropOverride);
4824}
4825
4826bool Camera3Device::overrideAutoRotateAndCrop(const sp<CaptureRequest> &request,
4827 bool overrideToPortrait,
4828 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropOverride) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004829 ATRACE_CALL();
4830
Emilian Peevd865f0d2023-03-17 17:13:07 -07004831 if (overrideToPortrait) {
4832 uint8_t rotateAndCrop_u8 = rotateAndCropOverride;
Austin Borger18b30a72022-10-27 12:20:29 -07004833 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4834 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP,
4835 &rotateAndCrop_u8, 1);
4836 return true;
4837 }
4838
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004839 if (request->mRotateAndCropAuto) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004840 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4841
4842 auto rotateAndCropEntry = metadata.find(ANDROID_SCALER_ROTATE_AND_CROP);
4843 if (rotateAndCropEntry.count > 0) {
Emilian Peevd865f0d2023-03-17 17:13:07 -07004844 if (rotateAndCropEntry.data.u8[0] == rotateAndCropOverride) {
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004845 return false;
4846 } else {
Emilian Peevd865f0d2023-03-17 17:13:07 -07004847 rotateAndCropEntry.data.u8[0] = rotateAndCropOverride;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004848 return true;
4849 }
4850 } else {
Emilian Peevd865f0d2023-03-17 17:13:07 -07004851 uint8_t rotateAndCrop_u8 = rotateAndCropOverride;
4852 metadata.update(ANDROID_SCALER_ROTATE_AND_CROP, &rotateAndCrop_u8, 1);
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004853 return true;
4854 }
4855 }
Emilian Peevd865f0d2023-03-17 17:13:07 -07004856
4857 return false;
4858}
4859
4860bool Camera3Device::overrideAutoframing(const sp<CaptureRequest> &request /*out*/,
4861 camera_metadata_enum_android_control_autoframing_t autoframingOverride) {
4862 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4863 auto autoframingEntry = metadata.find(ANDROID_CONTROL_AUTOFRAMING);
4864 if (autoframingEntry.count > 0) {
4865 if (autoframingEntry.data.u8[0] == autoframingOverride) {
4866 return false;
4867 } else {
4868 autoframingEntry.data.u8[0] = autoframingOverride;
4869 return true;
4870 }
4871 } else {
4872 uint8_t autoframing_u8 = autoframingOverride;
4873 metadata.update(ANDROID_CONTROL_AUTOFRAMING,
4874 &autoframing_u8, 1);
4875 return true;
4876 }
4877
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08004878 return false;
4879}
4880
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004881bool Camera3Device::RequestThread::overrideAutoframing(const sp<CaptureRequest> &request) {
4882 ATRACE_CALL();
4883
4884 if (request->mAutoframingAuto) {
4885 Mutex::Autolock l(mTriggerMutex);
Emilian Peevd865f0d2023-03-17 17:13:07 -07004886 return Camera3Device::overrideAutoframing(request, mAutoframingOverride);
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004887 }
Emilian Peevd865f0d2023-03-17 17:13:07 -07004888
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00004889 return false;
4890}
4891
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004892bool Camera3Device::RequestThread::overrideTestPattern(
4893 const sp<CaptureRequest> &request) {
4894 ATRACE_CALL();
4895
Eino-Ville Talvala1646c3c2021-07-29 13:48:40 -07004896 if (!mSupportCameraMute) return false;
Eino-Ville Talvalac2459842021-07-22 16:36:36 -07004897
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004898 Mutex::Autolock l(mTriggerMutex);
4899
4900 bool changed = false;
4901
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004902 // For a multi-camera, the physical cameras support the same set of
4903 // test pattern modes as the logical camera.
4904 for (auto& settings : request->mSettingsList) {
4905 CameraMetadata &metadata = settings.metadata;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004906
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004907 int32_t testPatternMode = settings.mOriginalTestPatternMode;
4908 int32_t testPatternData[4] = {
4909 settings.mOriginalTestPatternData[0],
4910 settings.mOriginalTestPatternData[1],
4911 settings.mOriginalTestPatternData[2],
4912 settings.mOriginalTestPatternData[3]
4913 };
4914 if (mCameraMute != ANDROID_SENSOR_TEST_PATTERN_MODE_OFF) {
4915 testPatternMode = mCameraMute;
4916 testPatternData[0] = 0;
4917 testPatternData[1] = 0;
4918 testPatternData[2] = 0;
4919 testPatternData[3] = 0;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004920 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004921
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004922 auto testPatternEntry = metadata.find(ANDROID_SENSOR_TEST_PATTERN_MODE);
4923 bool supportTestPatternModeKey = settings.mHasTestPatternModeTag;
4924 if (testPatternEntry.count > 0) {
4925 if (testPatternEntry.data.i32[0] != testPatternMode) {
4926 testPatternEntry.data.i32[0] = testPatternMode;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004927 changed = true;
4928 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004929 } else if (supportTestPatternModeKey) {
4930 metadata.update(ANDROID_SENSOR_TEST_PATTERN_MODE,
4931 &testPatternMode, 1);
4932 changed = true;
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004933 }
Shuzhen Wang911c6a32021-10-27 13:36:03 -07004934
4935 auto testPatternColor = metadata.find(ANDROID_SENSOR_TEST_PATTERN_DATA);
4936 bool supportTestPatternDataKey = settings.mHasTestPatternDataTag;
4937 if (testPatternColor.count >= 4) {
4938 for (size_t i = 0; i < 4; i++) {
4939 if (testPatternColor.data.i32[i] != testPatternData[i]) {
4940 testPatternColor.data.i32[i] = testPatternData[i];
4941 changed = true;
4942 }
4943 }
4944 } else if (supportTestPatternDataKey) {
4945 metadata.update(ANDROID_SENSOR_TEST_PATTERN_DATA,
4946 testPatternData, 4);
4947 changed = true;
4948 }
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08004949 }
4950
4951 return changed;
4952}
4953
Shuzhen Wangaf22e912023-04-11 16:03:17 -07004954bool Camera3Device::RequestThread::overrideSettingsOverride(
4955 const sp<CaptureRequest> &request) {
4956 ATRACE_CALL();
4957
4958 if (!mSupportSettingsOverride) return false;
4959
4960 Mutex::Autolock l(mTriggerMutex);
4961
4962 // For a multi-camera, only override the logical camera's metadata.
4963 CameraMetadata &metadata = request->mSettingsList.begin()->metadata;
4964 camera_metadata_entry entry = metadata.find(ANDROID_CONTROL_SETTINGS_OVERRIDE);
4965 int32_t originalValue = request->mSettingsList.begin()->mOriginalSettingsOverride;
4966 if (mSettingsOverride != -1 &&
4967 (entry.count == 0 || entry.data.i32[0] != mSettingsOverride)) {
4968 metadata.update(ANDROID_CONTROL_SETTINGS_OVERRIDE,
4969 &mSettingsOverride, 1);
4970 return true;
4971 } else if (mSettingsOverride == -1 &&
4972 (entry.count == 0 || entry.data.i32[0] != originalValue)) {
4973 metadata.update(ANDROID_CONTROL_SETTINGS_OVERRIDE,
4974 &originalValue, 1);
4975 return true;
4976 }
4977
4978 return false;
4979}
4980
Cliff Wuc2ad9c82021-04-21 00:58:58 +08004981status_t Camera3Device::RequestThread::setHalInterface(
4982 sp<HalInterface> newHalInterface) {
4983 if (newHalInterface.get() == nullptr) {
4984 ALOGE("%s: The newHalInterface does not exist!", __FUNCTION__);
4985 return DEAD_OBJECT;
4986 }
4987
4988 mInterface = newHalInterface;
4989
4990 return OK;
4991}
4992
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07004993/**
4994 * PreparerThread inner class methods
4995 */
4996
4997Camera3Device::PreparerThread::PreparerThread() :
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -07004998 Thread(/*canCallJava*/false), mListener(nullptr),
Emilian Peevac3ce6c2017-12-12 15:27:02 +00004999 mActive(false), mCancelNow(false), mCurrentMaxCount(0), mCurrentPrepareComplete(false) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005000}
5001
5002Camera3Device::PreparerThread::~PreparerThread() {
5003 Thread::requestExitAndWait();
5004 if (mCurrentStream != nullptr) {
5005 mCurrentStream->cancelPrepare();
5006 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5007 mCurrentStream.clear();
5008 }
5009 clear();
5010}
5011
Ruben Brunkc78ac262015-08-13 17:58:46 -07005012status_t Camera3Device::PreparerThread::prepare(int maxCount, sp<Camera3StreamInterface>& stream) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005013 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005014 status_t res;
5015
5016 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005017 sp<NotificationListener> listener = mListener.promote();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005018
Shuzhen Wangb3a0fb52018-09-13 17:24:08 -07005019 res = stream->startPrepare(maxCount, true /*blockRequest*/);
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005020 if (res == OK) {
5021 // No preparation needed, fire listener right off
5022 ALOGV("%s: Stream %d already prepared", __FUNCTION__, stream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005023 if (listener != NULL) {
5024 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005025 }
5026 return OK;
5027 } else if (res != NOT_ENOUGH_DATA) {
5028 return res;
5029 }
5030
5031 // Need to prepare, start up thread if necessary
5032 if (!mActive) {
5033 // mRunning will change to false before the thread fully shuts down, so wait to be sure it
5034 // isn't running
5035 Thread::requestExitAndWait();
5036 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5037 if (res != OK) {
5038 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__, res, strerror(-res));
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005039 if (listener != NULL) {
5040 listener->notifyPrepared(stream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005041 }
5042 return res;
5043 }
5044 mCancelNow = false;
5045 mActive = true;
5046 ALOGV("%s: Preparer stream started", __FUNCTION__);
5047 }
5048
5049 // queue up the work
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005050 mPendingStreams.push_back(
5051 std::tuple<int, sp<camera3::Camera3StreamInterface>>(maxCount, stream));
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005052 ALOGV("%s: Stream %d queued for preparing", __FUNCTION__, stream->getId());
5053
5054 return OK;
5055}
5056
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005057void Camera3Device::PreparerThread::pause() {
5058 ATRACE_CALL();
5059
5060 Mutex::Autolock l(mLock);
5061
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005062 std::list<std::tuple<int, sp<camera3::Camera3StreamInterface>>> pendingStreams;
5063 pendingStreams.insert(pendingStreams.begin(), mPendingStreams.begin(), mPendingStreams.end());
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005064 sp<camera3::Camera3StreamInterface> currentStream = mCurrentStream;
5065 int currentMaxCount = mCurrentMaxCount;
5066 mPendingStreams.clear();
5067 mCancelNow = true;
5068 while (mActive) {
5069 auto res = mThreadActiveSignal.waitRelative(mLock, kActiveTimeout);
5070 if (res == TIMED_OUT) {
5071 ALOGE("%s: Timed out waiting on prepare thread!", __FUNCTION__);
5072 return;
5073 } else if (res != OK) {
5074 ALOGE("%s: Encountered an error: %d waiting on prepare thread!", __FUNCTION__, res);
5075 return;
5076 }
5077 }
5078
5079 //Check whether the prepare thread was able to complete the current
5080 //stream. In case work is still pending emplace it along with the rest
5081 //of the streams in the pending list.
5082 if (currentStream != nullptr) {
5083 if (!mCurrentPrepareComplete) {
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005084 pendingStreams.push_back(std::tuple(currentMaxCount, currentStream));
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005085 }
5086 }
5087
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005088 mPendingStreams.insert(mPendingStreams.begin(), pendingStreams.begin(), pendingStreams.end());
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005089 for (const auto& it : mPendingStreams) {
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005090 std::get<1>(it)->cancelPrepare();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005091 }
5092}
5093
5094status_t Camera3Device::PreparerThread::resume() {
5095 ATRACE_CALL();
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005096 ALOGV("%s: PreparerThread", __FUNCTION__);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005097 status_t res;
5098
5099 Mutex::Autolock l(mLock);
5100 sp<NotificationListener> listener = mListener.promote();
5101
5102 if (mActive) {
5103 ALOGE("%s: Trying to resume an already active prepare thread!", __FUNCTION__);
5104 return NO_INIT;
5105 }
5106
5107 auto it = mPendingStreams.begin();
5108 for (; it != mPendingStreams.end();) {
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005109 res = std::get<1>(*it)->startPrepare(std::get<0>(*it), true /*blockRequest*/);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005110 if (res == OK) {
5111 if (listener != NULL) {
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005112 listener->notifyPrepared(std::get<1>(*it)->getId());
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005113 }
5114 it = mPendingStreams.erase(it);
5115 } else if (res != NOT_ENOUGH_DATA) {
5116 ALOGE("%s: Unable to start preparer stream: %d (%s)", __FUNCTION__,
5117 res, strerror(-res));
5118 it = mPendingStreams.erase(it);
5119 } else {
5120 it++;
5121 }
5122 }
5123
5124 if (mPendingStreams.empty()) {
5125 return OK;
5126 }
5127
5128 res = Thread::run("C3PrepThread", PRIORITY_BACKGROUND);
5129 if (res != OK) {
5130 ALOGE("%s: Unable to start preparer stream: %d (%s)",
5131 __FUNCTION__, res, strerror(-res));
5132 return res;
5133 }
5134 mCancelNow = false;
5135 mActive = true;
5136 ALOGV("%s: Preparer stream started", __FUNCTION__);
5137
5138 return OK;
5139}
5140
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005141status_t Camera3Device::PreparerThread::clear() {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005142 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005143 Mutex::Autolock l(mLock);
5144
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005145 for (const auto& it : mPendingStreams) {
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005146 std::get<1>(it)->cancelPrepare();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005147 }
5148 mPendingStreams.clear();
5149 mCancelNow = true;
5150
5151 return OK;
5152}
5153
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005154void Camera3Device::PreparerThread::setNotificationListener(wp<NotificationListener> listener) {
Eino-Ville Talvala6aeb8882017-08-07 17:40:49 -07005155 ATRACE_CALL();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005156 Mutex::Autolock l(mLock);
5157 mListener = listener;
5158}
5159
5160bool Camera3Device::PreparerThread::threadLoop() {
5161 status_t res;
5162 {
5163 Mutex::Autolock l(mLock);
5164 if (mCurrentStream == nullptr) {
5165 // End thread if done with work
5166 if (mPendingStreams.empty()) {
5167 ALOGV("%s: Preparer stream out of work", __FUNCTION__);
5168 // threadLoop _must not_ re-acquire mLock after it sets mActive to false; would
5169 // cause deadlock with prepare()'s requestExitAndWait triggered by !mActive.
5170 mActive = false;
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005171 mThreadActiveSignal.signal();
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005172 return false;
5173 }
5174
5175 // Get next stream to prepare
5176 auto it = mPendingStreams.begin();
Jayant Chowdhary57184d52023-02-14 20:54:39 +00005177 mCurrentMaxCount = std::get<0>(*it);
5178 mCurrentStream = std::get<1>(*it);
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005179 mCurrentPrepareComplete = false;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005180 mPendingStreams.erase(it);
5181 ATRACE_ASYNC_BEGIN("stream prepare", mCurrentStream->getId());
5182 ALOGV("%s: Preparing stream %d", __FUNCTION__, mCurrentStream->getId());
5183 } else if (mCancelNow) {
5184 mCurrentStream->cancelPrepare();
5185 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5186 ALOGV("%s: Cancelling stream %d prepare", __FUNCTION__, mCurrentStream->getId());
5187 mCurrentStream.clear();
5188 mCancelNow = false;
5189 return true;
5190 }
5191 }
5192
5193 res = mCurrentStream->prepareNextBuffer();
5194 if (res == NOT_ENOUGH_DATA) return true;
5195 if (res != OK) {
5196 // Something bad happened; try to recover by cancelling prepare and
5197 // signalling listener anyway
5198 ALOGE("%s: Stream %d returned error %d (%s) during prepare", __FUNCTION__,
5199 mCurrentStream->getId(), res, strerror(-res));
5200 mCurrentStream->cancelPrepare();
5201 }
5202
5203 // This stream has finished, notify listener
5204 Mutex::Autolock l(mLock);
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005205 sp<NotificationListener> listener = mListener.promote();
5206 if (listener != NULL) {
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005207 ALOGV("%s: Stream %d prepare done, signaling listener", __FUNCTION__,
5208 mCurrentStream->getId());
Yin-Chia Yehe1c80632016-08-08 14:48:05 -07005209 listener->notifyPrepared(mCurrentStream->getId());
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005210 }
5211
5212 ATRACE_ASYNC_END("stream prepare", mCurrentStream->getId());
5213 mCurrentStream.clear();
Emilian Peevac3ce6c2017-12-12 15:27:02 +00005214 mCurrentPrepareComplete = true;
Eino-Ville Talvala4d44cad2015-04-11 13:15:45 -07005215
5216 return true;
5217}
Igor Murashkin4d2f2e82013-04-01 17:29:07 -07005218
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005219status_t Camera3Device::RequestBufferStateMachine::initialize(
5220 sp<camera3::StatusTracker> statusTracker) {
5221 if (statusTracker == nullptr) {
5222 ALOGE("%s: statusTracker is null", __FUNCTION__);
5223 return BAD_VALUE;
5224 }
5225
5226 std::lock_guard<std::mutex> lock(mLock);
5227 mStatusTracker = statusTracker;
Yin-Chia Yeh87b3ec02019-06-03 10:44:39 -07005228 mRequestBufferStatusId = statusTracker->addComponent("BufferRequestSM");
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005229 return OK;
5230}
5231
5232bool Camera3Device::RequestBufferStateMachine::startRequestBuffer() {
5233 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005234 if (mStatus == RB_STATUS_READY || mStatus == RB_STATUS_PENDING_STOP) {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005235 mRequestBufferOngoing = true;
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005236 notifyTrackerLocked(/*active*/true);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005237 return true;
5238 }
5239 return false;
5240}
5241
5242void Camera3Device::RequestBufferStateMachine::endRequestBuffer() {
5243 std::lock_guard<std::mutex> lock(mLock);
5244 if (!mRequestBufferOngoing) {
5245 ALOGE("%s called without a successful startRequestBuffer call first!", __FUNCTION__);
5246 return;
5247 }
5248 mRequestBufferOngoing = false;
5249 if (mStatus == RB_STATUS_PENDING_STOP) {
5250 checkSwitchToStopLocked();
5251 }
Yin-Chia Yeh8a4ccb02018-11-16 15:43:36 -08005252 notifyTrackerLocked(/*active*/false);
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005253}
5254
5255void Camera3Device::RequestBufferStateMachine::onStreamsConfigured() {
5256 std::lock_guard<std::mutex> lock(mLock);
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005257 mSwitchedToOffline = false;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005258 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005259 return;
5260}
5261
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005262void Camera3Device::RequestBufferStateMachine::onSubmittingRequest() {
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005263 std::lock_guard<std::mutex> lock(mLock);
5264 mRequestThreadPaused = false;
Yin-Chia Yehcd333fe2019-02-08 13:45:41 -08005265 // inflight map register actually happens in prepareHalRequest now, but it is close enough
5266 // approximation.
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005267 mInflightMapEmpty = false;
5268 if (mStatus == RB_STATUS_STOPPED) {
5269 mStatus = RB_STATUS_READY;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005270 }
5271 return;
5272}
5273
5274void Camera3Device::RequestBufferStateMachine::onRequestThreadPaused() {
5275 std::lock_guard<std::mutex> lock(mLock);
5276 mRequestThreadPaused = true;
5277 if (mStatus == RB_STATUS_PENDING_STOP) {
5278 checkSwitchToStopLocked();
5279 }
5280 return;
5281}
5282
5283void Camera3Device::RequestBufferStateMachine::onInflightMapEmpty() {
5284 std::lock_guard<std::mutex> lock(mLock);
5285 mInflightMapEmpty = true;
5286 if (mStatus == RB_STATUS_PENDING_STOP) {
5287 checkSwitchToStopLocked();
5288 }
5289 return;
5290}
5291
5292void Camera3Device::RequestBufferStateMachine::onWaitUntilIdle() {
5293 std::lock_guard<std::mutex> lock(mLock);
5294 if (!checkSwitchToStopLocked()) {
5295 mStatus = RB_STATUS_PENDING_STOP;
5296 }
5297 return;
5298}
5299
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005300bool Camera3Device::RequestBufferStateMachine::onSwitchToOfflineSuccess() {
5301 std::lock_guard<std::mutex> lock(mLock);
5302 if (mRequestBufferOngoing) {
5303 ALOGE("%s: HAL must not be requesting buffer after HAL returns switchToOffline!",
5304 __FUNCTION__);
5305 return false;
5306 }
5307 mSwitchedToOffline = true;
5308 mInflightMapEmpty = true;
5309 mRequestThreadPaused = true;
5310 mStatus = RB_STATUS_STOPPED;
5311 return true;
5312}
5313
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005314void Camera3Device::RequestBufferStateMachine::notifyTrackerLocked(bool active) {
5315 sp<StatusTracker> statusTracker = mStatusTracker.promote();
5316 if (statusTracker != nullptr) {
5317 if (active) {
5318 statusTracker->markComponentActive(mRequestBufferStatusId);
5319 } else {
5320 statusTracker->markComponentIdle(mRequestBufferStatusId, Fence::NO_FENCE);
5321 }
5322 }
5323}
5324
5325bool Camera3Device::RequestBufferStateMachine::checkSwitchToStopLocked() {
5326 if (mInflightMapEmpty && mRequestThreadPaused && !mRequestBufferOngoing) {
5327 mStatus = RB_STATUS_STOPPED;
Yin-Chia Yeh30ab5ed2018-10-12 15:57:04 -07005328 return true;
5329 }
5330 return false;
5331}
5332
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005333bool Camera3Device::startRequestBuffer() {
5334 return mRequestBufferSM.startRequestBuffer();
5335}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005336
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005337void Camera3Device::endRequestBuffer() {
5338 mRequestBufferSM.endRequestBuffer();
5339}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005340
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005341nsecs_t Camera3Device::getWaitDuration() {
5342 return kBaseGetBufferWait + getExpectedInFlightDuration();
5343}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005344
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005345void Camera3Device::getInflightBufferKeys(std::vector<std::pair<int32_t, int32_t>>* out) {
5346 mInterface->getInflightBufferKeys(out);
5347}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005348
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005349void Camera3Device::getInflightRequestBufferKeys(std::vector<uint64_t>* out) {
5350 mInterface->getInflightRequestBufferKeys(out);
5351}
Shuzhen Wang268a1362018-10-16 16:32:59 -07005352
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005353std::vector<sp<Camera3StreamInterface>> Camera3Device::getAllStreams() {
5354 std::vector<sp<Camera3StreamInterface>> ret;
5355 bool hasInputStream = mInputStream != nullptr;
5356 ret.reserve(mOutputStreams.size() + mDeletedStreams.size() + ((hasInputStream) ? 1 : 0));
5357 if (hasInputStream) {
5358 ret.push_back(mInputStream);
Shuzhen Wang268a1362018-10-16 16:32:59 -07005359 }
Yin-Chia Yeh5fd603e2019-11-20 11:22:27 -08005360 for (size_t i = 0; i < mOutputStreams.size(); i++) {
5361 ret.push_back(mOutputStreams[i]);
5362 }
5363 for (size_t i = 0; i < mDeletedStreams.size(); i++) {
5364 ret.push_back(mDeletedStreams[i]);
5365 }
5366 return ret;
Shuzhen Wang268a1362018-10-16 16:32:59 -07005367}
5368
Emilian Peevcc0b7952020-01-07 13:54:47 -08005369void Camera3Device::getOfflineStreamIds(std::vector<int> *offlineStreamIds) {
5370 ATRACE_CALL();
5371
5372 if (offlineStreamIds == nullptr) {
5373 return;
5374 }
5375
5376 Mutex::Autolock il(mInterfaceLock);
5377
5378 auto streamIds = mOutputStreams.getStreamIds();
5379 bool hasInputStream = mInputStream != nullptr;
5380 if (hasInputStream && mInputStream->getOfflineProcessingSupport()) {
5381 offlineStreamIds->push_back(mInputStream->getId());
5382 }
5383
5384 for (const auto & streamId : streamIds) {
5385 sp<camera3::Camera3OutputStreamInterface> stream = mOutputStreams.get(streamId);
5386 // Streams that use the camera buffer manager are currently not supported in
5387 // offline mode
5388 if (stream->getOfflineProcessingSupport() &&
5389 (stream->getStreamSetId() == CAMERA3_STREAM_SET_ID_INVALID)) {
5390 offlineStreamIds->push_back(streamId);
5391 }
5392 }
5393}
5394
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005395status_t Camera3Device::setRotateAndCropAutoBehavior(
5396 camera_metadata_enum_android_scaler_rotate_and_crop_t rotateAndCropValue) {
5397 ATRACE_CALL();
5398 Mutex::Autolock il(mInterfaceLock);
5399 Mutex::Autolock l(mLock);
5400 if (mRequestThread == nullptr) {
5401 return INVALID_OPERATION;
5402 }
Emilian Peevd865f0d2023-03-17 17:13:07 -07005403 if (rotateAndCropValue == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
5404 return BAD_VALUE;
5405 }
5406 mRotateAndCropOverride = rotateAndCropValue;
Eino-Ville Talvalaf2e37092020-01-07 15:32:32 -08005407 return mRequestThread->setRotateAndCropAutoBehavior(rotateAndCropValue);
5408}
5409
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00005410status_t Camera3Device::setAutoframingAutoBehavior(
5411 camera_metadata_enum_android_control_autoframing_t autoframingValue) {
5412 ATRACE_CALL();
5413 Mutex::Autolock il(mInterfaceLock);
5414 Mutex::Autolock l(mLock);
5415 if (mRequestThread == nullptr) {
5416 return INVALID_OPERATION;
5417 }
Emilian Peevd865f0d2023-03-17 17:13:07 -07005418 if (autoframingValue == ANDROID_CONTROL_AUTOFRAMING_AUTO) {
5419 return BAD_VALUE;
5420 }
5421 mAutoframingOverride = autoframingValue;
Bharatt Kukreja7146ced2022-10-25 15:45:29 +00005422 return mRequestThread->setAutoframingAutoBehaviour(autoframingValue);
5423}
5424
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005425bool Camera3Device::supportsCameraMute() {
5426 Mutex::Autolock il(mInterfaceLock);
5427 Mutex::Autolock l(mLock);
5428
5429 return mSupportCameraMute;
5430}
5431
5432status_t Camera3Device::setCameraMute(bool enabled) {
5433 ATRACE_CALL();
5434 Mutex::Autolock il(mInterfaceLock);
5435 Mutex::Autolock l(mLock);
5436
5437 if (mRequestThread == nullptr || !mSupportCameraMute) {
5438 return INVALID_OPERATION;
5439 }
Eino-Ville Talvala11afe4f2021-05-27 14:45:31 -07005440 int32_t muteMode =
5441 !enabled ? ANDROID_SENSOR_TEST_PATTERN_MODE_OFF :
5442 mSupportTestPatternSolidColor ? ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR :
5443 ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK;
5444 return mRequestThread->setCameraMute(muteMode);
Eino-Ville Talvala305cec62020-11-12 14:18:17 -08005445}
5446
Shuzhen Wangaf22e912023-04-11 16:03:17 -07005447bool Camera3Device::supportsZoomOverride() {
5448 Mutex::Autolock il(mInterfaceLock);
5449 Mutex::Autolock l(mLock);
5450
5451 return mSupportZoomOverride;
5452}
5453
5454status_t Camera3Device::setZoomOverride(int32_t zoomOverride) {
5455 ATRACE_CALL();
5456 Mutex::Autolock il(mInterfaceLock);
5457 Mutex::Autolock l(mLock);
5458
5459 if (mRequestThread == nullptr || !mSupportZoomOverride) {
5460 return INVALID_OPERATION;
5461 }
5462
5463 return mRequestThread->setZoomOverride(zoomOverride);
5464}
5465
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005466status_t Camera3Device::injectCamera(const String8& injectedCamId,
5467 sp<CameraProviderManager> manager) {
5468 ALOGI("%s Injection camera: injectedCamId = %s", __FUNCTION__, injectedCamId.string());
5469 ATRACE_CALL();
5470 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005471 // When the camera device is active, injectCamera() and stopInjection() will call
5472 // internalPauseAndWaitLocked() and internalResumeLocked(), and then they will call
5473 // mStatusChanged.waitRelative(mLock, timeout) of waitUntilStateThenRelock(). But
5474 // mStatusChanged.waitRelative(mLock, timeout)'s parameter: mutex "mLock" must be in the locked
5475 // state, so we need to add "Mutex::Autolock l(mLock)" to lock the "mLock" before calling
5476 // waitUntilStateThenRelock().
5477 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005478
5479 status_t res = NO_ERROR;
5480 if (mInjectionMethods->isInjecting()) {
5481 if (injectedCamId == mInjectionMethods->getInjectedCamId()) {
5482 return OK;
5483 } else {
5484 res = mInjectionMethods->stopInjection();
5485 if (res != OK) {
5486 ALOGE("%s: Failed to stop the injection camera! ret != NO_ERROR: %d",
5487 __FUNCTION__, res);
5488 return res;
5489 }
5490 }
5491 }
5492
Jayant Chowdhary22441f32021-12-26 18:35:41 -08005493 res = injectionCameraInitialize(injectedCamId, manager);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005494 if (res != OK) {
5495 ALOGE("%s: Failed to initialize the injection camera! ret != NO_ERROR: %d",
5496 __FUNCTION__, res);
5497 return res;
5498 }
5499
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005500 // When the second display of android is cast to the remote device, and the opened camera is
5501 // also cast to the second display, in this case, because the camera has configured the streams
5502 // at this time, we can directly call injectCamera() to replace the internal camera with
5503 // injection camera.
Cliff Wu3b268182021-07-06 15:44:43 +08005504 if (mInjectionMethods->isStreamConfigCompleteButNotInjected()) {
5505 ALOGD("%s: The opened camera is directly cast to the remote device.", __FUNCTION__);
5506
5507 camera3::camera_stream_configuration injectionConfig;
5508 std::vector<uint32_t> injectionBufferSizes;
5509 mInjectionMethods->getInjectionConfig(&injectionConfig, &injectionBufferSizes);
5510 if (mOperatingMode < 0 || injectionConfig.num_streams <= 0
5511 || injectionBufferSizes.size() <= 0) {
5512 ALOGE("Failed to inject camera due to abandoned configuration! "
5513 "mOperatingMode: %d injectionConfig.num_streams: %d "
5514 "injectionBufferSizes.size(): %zu", mOperatingMode,
5515 injectionConfig.num_streams, injectionBufferSizes.size());
5516 return DEAD_OBJECT;
5517 }
5518
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005519 res = mInjectionMethods->injectCamera(
5520 injectionConfig, injectionBufferSizes);
5521 if (res != OK) {
5522 ALOGE("Can't finish inject camera process!");
5523 return res;
5524 }
5525 }
5526
5527 return OK;
5528}
5529
5530status_t Camera3Device::stopInjection() {
5531 ALOGI("%s: Injection camera: stopInjection", __FUNCTION__);
5532 Mutex::Autolock il(mInterfaceLock);
Cliff Wu3b268182021-07-06 15:44:43 +08005533 Mutex::Autolock l(mLock);
Cliff Wuc2ad9c82021-04-21 00:58:58 +08005534 return mInjectionMethods->stopInjection();
5535}
5536
Shuzhen Wang16610a62022-12-15 22:38:07 -08005537void Camera3Device::overrideStreamUseCaseLocked() {
5538 if (mStreamUseCaseOverrides.size() == 0) {
5539 return;
5540 }
5541
5542 // Start from an array of indexes in mStreamUseCaseOverrides, and sort them
5543 // based first on size, and second on formats of [JPEG, RAW, YUV, PRIV].
5544 // Refer to CameraService::printHelp for details.
5545 std::vector<int> outputStreamsIndices(mOutputStreams.size());
5546 for (size_t i = 0; i < outputStreamsIndices.size(); i++) {
5547 outputStreamsIndices[i] = i;
5548 }
5549
5550 std::sort(outputStreamsIndices.begin(), outputStreamsIndices.end(),
5551 [&](int a, int b) -> bool {
5552
5553 auto formatScore = [](int format) {
5554 switch (format) {
5555 case HAL_PIXEL_FORMAT_BLOB:
5556 return 4;
5557 case HAL_PIXEL_FORMAT_RAW16:
5558 case HAL_PIXEL_FORMAT_RAW10:
5559 case HAL_PIXEL_FORMAT_RAW12:
5560 return 3;
5561 case HAL_PIXEL_FORMAT_YCBCR_420_888:
5562 return 2;
5563 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
5564 return 1;
5565 default:
5566 return 0;
5567 }
5568 };
5569
5570 int sizeA = mOutputStreams[a]->getWidth() * mOutputStreams[a]->getHeight();
5571 int sizeB = mOutputStreams[a]->getWidth() * mOutputStreams[a]->getHeight();
5572 int formatAScore = formatScore(mOutputStreams[a]->getFormat());
5573 int formatBScore = formatScore(mOutputStreams[b]->getFormat());
5574 if (sizeA > sizeB ||
5575 (sizeA == sizeB && formatAScore >= formatBScore)) {
5576 return true;
5577 } else {
5578 return false;
5579 }
5580 });
5581
5582 size_t overlapSize = std::min(mStreamUseCaseOverrides.size(), mOutputStreams.size());
5583 for (size_t i = 0; i < mOutputStreams.size(); i++) {
5584 mOutputStreams[outputStreamsIndices[i]]->setStreamUseCase(
5585 mStreamUseCaseOverrides[std::min(i, overlapSize-1)]);
5586 }
5587}
5588
Eino-Ville Talvala7fa43f32013-02-06 17:20:07 -08005589}; // namespace android